blob: ca9c82ee6c35ddc924e6057bf5c05f6ed5bf69e8 [file] [log] [blame]
Alan Coxe0495732008-10-13 10:36:58 +01001/*
2 * Tty buffer allocation management
3 */
4
5#include <linux/types.h>
6#include <linux/errno.h>
7#include <linux/tty.h>
8#include <linux/tty_driver.h>
9#include <linux/tty_flip.h>
10#include <linux/timer.h>
11#include <linux/string.h>
12#include <linux/slab.h>
13#include <linux/sched.h>
Alan Coxe0495732008-10-13 10:36:58 +010014#include <linux/wait.h>
15#include <linux/bitops.h>
16#include <linux/delay.h>
17#include <linux/module.h>
George Spelvin593fb1ae42013-02-12 02:00:43 -050018#include <linux/ratelimit.h>
Alan Coxe0495732008-10-13 10:36:58 +010019
Peter Hurley1cef50e2013-06-15 09:36:02 -040020
21#define MIN_TTYB_SIZE 256
22#define TTYB_ALIGN_MASK 255
23
Peter Hurley7bfe0b72013-06-15 09:36:08 -040024/*
25 * Byte threshold to limit memory consumption for flip buffers.
26 * The actual memory limit is > 2x this amount.
27 */
Manfred Schlaegl876c9782019-01-28 19:01:10 +010028#define TTYB_DEFAULT_MEM_LIMIT (640 * 1024UL)
Peter Hurley7bfe0b72013-06-15 09:36:08 -040029
Peter Hurley9114fe82013-06-15 09:36:16 -040030/*
31 * We default to dicing tty buffer allocations to this many characters
32 * in order to avoid multiple page allocations. We know the size of
33 * tty_buffer itself but it must also be taken into account that the
34 * the buffer is 256 byte aligned. See tty_buffer_find for the allocation
35 * logic this must match
36 */
37
38#define TTY_BUFFER_PAGE (((PAGE_SIZE - sizeof(struct tty_buffer)) / 2) & ~0xFF)
39
Peter Hurley7bfe0b72013-06-15 09:36:08 -040040/**
Peter Hurleya7c8d582013-06-15 09:36:15 -040041 * tty_buffer_lock_exclusive - gain exclusive access to buffer
42 * tty_buffer_unlock_exclusive - release exclusive access
43 *
44 * @port - tty_port owning the flip buffer
45 *
46 * Guarantees safe use of the line discipline's receive_buf() method by
47 * excluding the buffer work and any pending flush from using the flip
48 * buffer. Data can continue to be added concurrently to the flip buffer
49 * from the driver side.
50 *
51 * On release, the buffer work is restarted if there is data in the
52 * flip buffer
53 */
54
55void tty_buffer_lock_exclusive(struct tty_port *port)
56{
57 struct tty_bufhead *buf = &port->buf;
58
59 atomic_inc(&buf->priority);
60 mutex_lock(&buf->lock);
61}
Ben Hutchings28a821c2014-05-19 01:03:06 +010062EXPORT_SYMBOL_GPL(tty_buffer_lock_exclusive);
Peter Hurleya7c8d582013-06-15 09:36:15 -040063
64void tty_buffer_unlock_exclusive(struct tty_port *port)
65{
66 struct tty_bufhead *buf = &port->buf;
67 int restart;
68
69 restart = buf->head->commit != buf->head->read;
70
71 atomic_dec(&buf->priority);
72 mutex_unlock(&buf->lock);
73 if (restart)
74 queue_work(system_unbound_wq, &buf->work);
75}
Ben Hutchings28a821c2014-05-19 01:03:06 +010076EXPORT_SYMBOL_GPL(tty_buffer_unlock_exclusive);
Peter Hurleya7c8d582013-06-15 09:36:15 -040077
78/**
Peter Hurley7bfe0b72013-06-15 09:36:08 -040079 * tty_buffer_space_avail - return unused buffer space
80 * @port - tty_port owning the flip buffer
81 *
82 * Returns the # of bytes which can be written by the driver without
83 * reaching the buffer limit.
84 *
85 * Note: this does not guarantee that memory is available to write
86 * the returned # of bytes (use tty_prepare_flip_string_xxx() to
87 * pre-allocate if memory guarantee is required).
88 */
89
90int tty_buffer_space_avail(struct tty_port *port)
91{
Peter Hurley5dda4ca2013-11-22 12:09:56 -050092 int space = port->buf.mem_limit - atomic_read(&port->buf.mem_used);
Peter Hurley7bfe0b72013-06-15 09:36:08 -040093 return max(space, 0);
94}
Peter Hurleyc4a8dab2013-11-22 13:06:08 -050095EXPORT_SYMBOL_GPL(tty_buffer_space_avail);
Peter Hurley7bfe0b72013-06-15 09:36:08 -040096
Peter Hurley9dd51392013-06-15 09:36:03 -040097static void tty_buffer_reset(struct tty_buffer *p, size_t size)
98{
99 p->used = 0;
100 p->size = size;
101 p->next = NULL;
102 p->commit = 0;
103 p->read = 0;
Peter Hurleyacc0f672013-12-09 09:23:52 -0500104 p->flags = 0;
Peter Hurley9dd51392013-06-15 09:36:03 -0400105}
106
Alan Coxe0495732008-10-13 10:36:58 +0100107/**
108 * tty_buffer_free_all - free buffers used by a tty
109 * @tty: tty to free from
110 *
111 * Remove all the buffers pending on a tty whether queued with data
112 * or in the free ring. Must be called when the tty is no longer in use
Alan Coxe0495732008-10-13 10:36:58 +0100113 */
114
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200115void tty_buffer_free_all(struct tty_port *port)
Alan Coxe0495732008-10-13 10:36:58 +0100116{
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200117 struct tty_bufhead *buf = &port->buf;
Peter Hurley809850b2013-06-15 09:36:06 -0400118 struct tty_buffer *p, *next;
119 struct llist_node *llist;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200120
Peter Hurley2cf7b672013-06-15 09:36:05 -0400121 while ((p = buf->head) != NULL) {
122 buf->head = p->next;
Peter Hurley7391ee12013-06-15 09:36:07 -0400123 if (p->size > 0)
124 kfree(p);
Alan Coxe0495732008-10-13 10:36:58 +0100125 }
Peter Hurley809850b2013-06-15 09:36:06 -0400126 llist = llist_del_all(&buf->free);
127 llist_for_each_entry_safe(p, next, llist, free)
Peter Hurley2cf7b672013-06-15 09:36:05 -0400128 kfree(p);
Peter Hurley809850b2013-06-15 09:36:06 -0400129
Peter Hurley7391ee12013-06-15 09:36:07 -0400130 tty_buffer_reset(&buf->sentinel, 0);
131 buf->head = &buf->sentinel;
132 buf->tail = &buf->sentinel;
Peter Hurley7bfe0b72013-06-15 09:36:08 -0400133
Peter Hurley5dda4ca2013-11-22 12:09:56 -0500134 atomic_set(&buf->mem_used, 0);
Alan Coxe0495732008-10-13 10:36:58 +0100135}
136
137/**
138 * tty_buffer_alloc - allocate a tty buffer
139 * @tty: tty device
140 * @size: desired size (characters)
141 *
142 * Allocate a new tty buffer to hold the desired number of characters.
Peter Hurley11b9faa2013-06-15 09:36:04 -0400143 * We round our buffers off in 256 character chunks to get better
144 * allocation behaviour.
Alan Coxe0495732008-10-13 10:36:58 +0100145 * Return NULL if out of memory or the allocation would exceed the
146 * per device queue
Alan Coxe0495732008-10-13 10:36:58 +0100147 */
148
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200149static struct tty_buffer *tty_buffer_alloc(struct tty_port *port, size_t size)
Alan Coxe0495732008-10-13 10:36:58 +0100150{
Peter Hurley809850b2013-06-15 09:36:06 -0400151 struct llist_node *free;
Alan Coxe0495732008-10-13 10:36:58 +0100152 struct tty_buffer *p;
153
Peter Hurley11b9faa2013-06-15 09:36:04 -0400154 /* Round the buffer size out */
155 size = __ALIGN_MASK(size, TTYB_ALIGN_MASK);
156
157 if (size <= MIN_TTYB_SIZE) {
Peter Hurley809850b2013-06-15 09:36:06 -0400158 free = llist_del_first(&port->buf.free);
159 if (free) {
160 p = llist_entry(free, struct tty_buffer, free);
Peter Hurley11b9faa2013-06-15 09:36:04 -0400161 goto found;
162 }
163 }
164
165 /* Should possibly check if this fails for the largest buffer we
166 have queued and recycle that ? */
Peter Hurley5dda4ca2013-11-22 12:09:56 -0500167 if (atomic_read(&port->buf.mem_used) > port->buf.mem_limit)
Alan Coxe0495732008-10-13 10:36:58 +0100168 return NULL;
169 p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC);
170 if (p == NULL)
171 return NULL;
Peter Hurley9dd51392013-06-15 09:36:03 -0400172
Peter Hurley11b9faa2013-06-15 09:36:04 -0400173found:
Peter Hurley9dd51392013-06-15 09:36:03 -0400174 tty_buffer_reset(p, size);
Peter Hurley5dda4ca2013-11-22 12:09:56 -0500175 atomic_add(size, &port->buf.mem_used);
Alan Coxe0495732008-10-13 10:36:58 +0100176 return p;
177}
178
179/**
180 * tty_buffer_free - free a tty buffer
181 * @tty: tty owning the buffer
182 * @b: the buffer to free
183 *
184 * Free a tty buffer, or add it to the free list according to our
185 * internal strategy
Alan Coxe0495732008-10-13 10:36:58 +0100186 */
187
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200188static void tty_buffer_free(struct tty_port *port, struct tty_buffer *b)
Alan Coxe0495732008-10-13 10:36:58 +0100189{
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200190 struct tty_bufhead *buf = &port->buf;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200191
Alan Coxe0495732008-10-13 10:36:58 +0100192 /* Dumb strategy for now - should keep some stats */
Peter Hurley5dda4ca2013-11-22 12:09:56 -0500193 WARN_ON(atomic_sub_return(b->size, &buf->mem_used) < 0);
Alan Coxe0495732008-10-13 10:36:58 +0100194
Peter Hurley1cef50e2013-06-15 09:36:02 -0400195 if (b->size > MIN_TTYB_SIZE)
Alan Coxe0495732008-10-13 10:36:58 +0100196 kfree(b);
Peter Hurley7391ee12013-06-15 09:36:07 -0400197 else if (b->size > 0)
Peter Hurley809850b2013-06-15 09:36:06 -0400198 llist_add(&b->free, &buf->free);
Alan Coxe0495732008-10-13 10:36:58 +0100199}
200
201/**
Alan Coxe0495732008-10-13 10:36:58 +0100202 * tty_buffer_flush - flush full tty buffers
203 * @tty: tty to flush
Peter Hurley86c80a82014-11-05 12:13:09 -0500204 * @ld: optional ldisc ptr (must be referenced)
Alan Coxe0495732008-10-13 10:36:58 +0100205 *
Peter Hurley86c80a82014-11-05 12:13:09 -0500206 * flush all the buffers containing receive data. If ld != NULL,
207 * flush the ldisc input buffer.
Alan Coxe0495732008-10-13 10:36:58 +0100208 *
Peter Hurleya7c8d582013-06-15 09:36:15 -0400209 * Locking: takes buffer lock to ensure single-threaded flip buffer
Peter Hurleye9975fd2013-06-15 09:36:10 -0400210 * 'consumer'
Alan Coxe0495732008-10-13 10:36:58 +0100211 */
212
Peter Hurley86c80a82014-11-05 12:13:09 -0500213void tty_buffer_flush(struct tty_struct *tty, struct tty_ldisc *ld)
Alan Coxe0495732008-10-13 10:36:58 +0100214{
Jiri Slaby2fc20662012-10-18 22:26:44 +0200215 struct tty_port *port = tty->port;
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200216 struct tty_bufhead *buf = &port->buf;
Peter Hurley47aa6582013-06-15 09:36:14 -0400217 struct tty_buffer *next;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200218
Peter Hurleya7c8d582013-06-15 09:36:15 -0400219 atomic_inc(&buf->priority);
Peter Hurleye9975fd2013-06-15 09:36:10 -0400220
Peter Hurleya7c8d582013-06-15 09:36:15 -0400221 mutex_lock(&buf->lock);
Dmitry Vyukov9e6b7cd2015-09-17 17:17:09 +0200222 /* paired w/ release in __tty_buffer_request_room; ensures there are
223 * no pending memory accesses to the freed buffer
224 */
225 while ((next = smp_load_acquire(&buf->head->next)) != NULL) {
Peter Hurley47aa6582013-06-15 09:36:14 -0400226 tty_buffer_free(port, buf->head);
227 buf->head = next;
228 }
229 buf->head->read = buf->head->commit;
Peter Hurley86c80a82014-11-05 12:13:09 -0500230
231 if (ld && ld->ops->flush_buffer)
232 ld->ops->flush_buffer(tty);
233
Peter Hurleya7c8d582013-06-15 09:36:15 -0400234 atomic_dec(&buf->priority);
235 mutex_unlock(&buf->lock);
Alan Coxe0495732008-10-13 10:36:58 +0100236}
237
238/**
Ilya Zykov64325a32013-01-19 18:16:20 +0400239 * tty_buffer_request_room - grow tty buffer if needed
Alan Coxe0495732008-10-13 10:36:58 +0100240 * @tty: tty structure
241 * @size: size desired
Peter Hurleyacc0f672013-12-09 09:23:52 -0500242 * @flags: buffer flags if new buffer allocated (default = 0)
Alan Coxe0495732008-10-13 10:36:58 +0100243 *
244 * Make at least size bytes of linear space available for the tty
245 * buffer. If we fail return the size we managed to find.
Peter Hurleyacc0f672013-12-09 09:23:52 -0500246 *
247 * Will change over to a new buffer if the current buffer is encoded as
248 * TTY_NORMAL (so has no flags buffer) and the new buffer requires
249 * a flags buffer.
Alan Coxe0495732008-10-13 10:36:58 +0100250 */
Peter Hurleyacc0f672013-12-09 09:23:52 -0500251static int __tty_buffer_request_room(struct tty_port *port, size_t size,
252 int flags)
Alan Coxe0495732008-10-13 10:36:58 +0100253{
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200254 struct tty_bufhead *buf = &port->buf;
Alan Coxe0495732008-10-13 10:36:58 +0100255 struct tty_buffer *b, *n;
Peter Hurleyacc0f672013-12-09 09:23:52 -0500256 int left, change;
Peter Hurleye8437d72013-06-15 09:36:09 -0400257
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200258 b = buf->tail;
Peter Hurleyacc0f672013-12-09 09:23:52 -0500259 if (b->flags & TTYB_NORMAL)
260 left = 2 * b->size - b->used;
261 else
262 left = b->size - b->used;
Alan Coxe0495732008-10-13 10:36:58 +0100263
Peter Hurleyacc0f672013-12-09 09:23:52 -0500264 change = (b->flags & TTYB_NORMAL) && (~flags & TTYB_NORMAL);
265 if (change || left < size) {
Alan Coxe0495732008-10-13 10:36:58 +0100266 /* This is the slow path - looking for new buffers to use */
Greg Kroah-Hartmane16cb0a2015-04-30 11:22:17 +0200267 n = tty_buffer_alloc(port, size);
268 if (n != NULL) {
Peter Hurleyacc0f672013-12-09 09:23:52 -0500269 n->flags = flags;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200270 buf->tail = n;
Dmitry Vyukovfacd8852015-09-17 17:17:10 +0200271 /* paired w/ acquire in flush_to_ldisc(); ensures
272 * flush_to_ldisc() sees buffer data.
273 */
274 smp_store_release(&b->commit, b->used);
Peter Hurley069f38b2015-07-12 20:50:49 -0400275 /* paired w/ acquire in flush_to_ldisc(); ensures the
Peter Hurley62a0d8d2014-05-02 10:56:12 -0400276 * latest commit value can be read before the head is
277 * advanced to the next buffer
278 */
Peter Hurley069f38b2015-07-12 20:50:49 -0400279 smp_store_release(&b->next, n);
Peter Hurleyacc0f672013-12-09 09:23:52 -0500280 } else if (change)
281 size = 0;
282 else
Alan Coxe0495732008-10-13 10:36:58 +0100283 size = left;
284 }
Alan Coxe0495732008-10-13 10:36:58 +0100285 return size;
286}
Peter Hurleyacc0f672013-12-09 09:23:52 -0500287
288int tty_buffer_request_room(struct tty_port *port, size_t size)
289{
290 return __tty_buffer_request_room(port, size, 0);
291}
Alan Coxe0495732008-10-13 10:36:58 +0100292EXPORT_SYMBOL_GPL(tty_buffer_request_room);
293
294/**
Alan Cox2832fc12010-02-18 16:43:54 +0000295 * tty_insert_flip_string_fixed_flag - Add characters to the tty buffer
Jiri Slaby2f693352013-01-03 15:53:02 +0100296 * @port: tty port
Alan Coxe0495732008-10-13 10:36:58 +0100297 * @chars: characters
Alan Cox2832fc12010-02-18 16:43:54 +0000298 * @flag: flag value for each character
Alan Coxe0495732008-10-13 10:36:58 +0100299 * @size: size
300 *
301 * Queue a series of bytes to the tty buffering. All the characters
Johan Hovoldccc5ca82010-05-07 19:58:32 +0200302 * passed are marked with the supplied flag. Returns the number added.
Alan Coxe0495732008-10-13 10:36:58 +0100303 */
304
Jiri Slaby2f693352013-01-03 15:53:02 +0100305int tty_insert_flip_string_fixed_flag(struct tty_port *port,
Alan Cox2832fc12010-02-18 16:43:54 +0000306 const unsigned char *chars, char flag, size_t size)
Alan Coxe0495732008-10-13 10:36:58 +0100307{
308 int copied = 0;
309 do {
Fang Wenqid4bee0a2010-03-09 18:54:28 +0800310 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
Peter Hurleyacc0f672013-12-09 09:23:52 -0500311 int flags = (flag == TTY_NORMAL) ? TTYB_NORMAL : 0;
312 int space = __tty_buffer_request_room(port, goal, flags);
Ilya Zykov64325a32013-01-19 18:16:20 +0400313 struct tty_buffer *tb = port->buf.tail;
Peter Hurley7391ee12013-06-15 09:36:07 -0400314 if (unlikely(space == 0))
Alan Coxe0495732008-10-13 10:36:58 +0100315 break;
Peter Hurley1fc359f2013-06-15 09:36:01 -0400316 memcpy(char_buf_ptr(tb, tb->used), chars, space);
Peter Hurleyacc0f672013-12-09 09:23:52 -0500317 if (~tb->flags & TTYB_NORMAL)
318 memset(flag_buf_ptr(tb, tb->used), flag, space);
Alan Coxe0495732008-10-13 10:36:58 +0100319 tb->used += space;
320 copied += space;
321 chars += space;
322 /* There is a small chance that we need to split the data over
323 several buffers. If this is the case we must loop */
324 } while (unlikely(size > copied));
325 return copied;
326}
Alan Cox2832fc12010-02-18 16:43:54 +0000327EXPORT_SYMBOL(tty_insert_flip_string_fixed_flag);
Alan Coxe0495732008-10-13 10:36:58 +0100328
329/**
330 * tty_insert_flip_string_flags - Add characters to the tty buffer
Jiri Slaby2f693352013-01-03 15:53:02 +0100331 * @port: tty port
Alan Coxe0495732008-10-13 10:36:58 +0100332 * @chars: characters
333 * @flags: flag bytes
334 * @size: size
335 *
336 * Queue a series of bytes to the tty buffering. For each character
337 * the flags array indicates the status of the character. Returns the
338 * number added.
Alan Coxe0495732008-10-13 10:36:58 +0100339 */
340
Jiri Slaby2f693352013-01-03 15:53:02 +0100341int tty_insert_flip_string_flags(struct tty_port *port,
Alan Coxe0495732008-10-13 10:36:58 +0100342 const unsigned char *chars, const char *flags, size_t size)
343{
344 int copied = 0;
345 do {
Fang Wenqid4bee0a2010-03-09 18:54:28 +0800346 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
Ilya Zykov64325a32013-01-19 18:16:20 +0400347 int space = tty_buffer_request_room(port, goal);
348 struct tty_buffer *tb = port->buf.tail;
Peter Hurley7391ee12013-06-15 09:36:07 -0400349 if (unlikely(space == 0))
Alan Coxe0495732008-10-13 10:36:58 +0100350 break;
Peter Hurley1fc359f2013-06-15 09:36:01 -0400351 memcpy(char_buf_ptr(tb, tb->used), chars, space);
352 memcpy(flag_buf_ptr(tb, tb->used), flags, space);
Alan Coxe0495732008-10-13 10:36:58 +0100353 tb->used += space;
354 copied += space;
355 chars += space;
356 flags += space;
357 /* There is a small chance that we need to split the data over
358 several buffers. If this is the case we must loop */
359 } while (unlikely(size > copied));
360 return copied;
361}
362EXPORT_SYMBOL(tty_insert_flip_string_flags);
363
364/**
Arnd Bergmannf61a07f2017-06-20 23:10:41 +0200365 * __tty_insert_flip_char - Add one character to the tty buffer
366 * @port: tty port
367 * @ch: character
368 * @flag: flag byte
369 *
370 * Queue a single byte to the tty buffering, with an optional flag.
371 * This is the slow path of tty_insert_flip_char.
372 */
373int __tty_insert_flip_char(struct tty_port *port, unsigned char ch, char flag)
374{
Arnd Bergmann346abf22017-08-02 13:11:39 +0200375 struct tty_buffer *tb;
Arnd Bergmannf61a07f2017-06-20 23:10:41 +0200376 int flags = (flag == TTY_NORMAL) ? TTYB_NORMAL : 0;
377
Arnd Bergmann75046242017-06-20 23:10:42 +0200378 if (!__tty_buffer_request_room(port, 1, flags))
Arnd Bergmannf61a07f2017-06-20 23:10:41 +0200379 return 0;
380
Arnd Bergmann346abf22017-08-02 13:11:39 +0200381 tb = port->buf.tail;
Arnd Bergmann75046242017-06-20 23:10:42 +0200382 if (~tb->flags & TTYB_NORMAL)
383 *flag_buf_ptr(tb, tb->used) = flag;
Arnd Bergmannf61a07f2017-06-20 23:10:41 +0200384 *char_buf_ptr(tb, tb->used++) = ch;
385
386 return 1;
387}
388EXPORT_SYMBOL(__tty_insert_flip_char);
389
390/**
Alan Coxe0495732008-10-13 10:36:58 +0100391 * tty_schedule_flip - push characters to ldisc
Jiri Slaby6732c8b2013-01-03 15:53:07 +0100392 * @port: tty port to push from
Alan Coxe0495732008-10-13 10:36:58 +0100393 *
394 * Takes any pending buffers and transfers their ownership to the
395 * ldisc side of the queue. It then schedules those characters for
396 * processing by the line discipline.
Alan Coxe0495732008-10-13 10:36:58 +0100397 */
398
Jiri Slaby6732c8b2013-01-03 15:53:07 +0100399void tty_schedule_flip(struct tty_port *port)
Alan Coxe0495732008-10-13 10:36:58 +0100400{
Jiri Slaby6732c8b2013-01-03 15:53:07 +0100401 struct tty_bufhead *buf = &port->buf;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200402
Dmitry Vyukovfacd8852015-09-17 17:17:10 +0200403 /* paired w/ acquire in flush_to_ldisc(); ensures
404 * flush_to_ldisc() sees buffer data.
405 */
406 smp_store_release(&buf->tail->commit, buf->tail->used);
Peter Hurleye052c6d2015-10-17 16:36:24 -0400407 queue_work(system_unbound_wq, &buf->work);
Alan Coxe0495732008-10-13 10:36:58 +0100408}
409EXPORT_SYMBOL(tty_schedule_flip);
410
411/**
412 * tty_prepare_flip_string - make room for characters
Jiri Slaby2f693352013-01-03 15:53:02 +0100413 * @port: tty port
Alan Coxe0495732008-10-13 10:36:58 +0100414 * @chars: return pointer for character write area
415 * @size: desired size
416 *
417 * Prepare a block of space in the buffer for data. Returns the length
418 * available and buffer pointer to the space which is now allocated and
419 * accounted for as ready for normal characters. This is used for drivers
420 * that need their own block copy routines into the buffer. There is no
421 * guarantee the buffer is a DMA target!
Alan Coxe0495732008-10-13 10:36:58 +0100422 */
423
Jiri Slaby2f693352013-01-03 15:53:02 +0100424int tty_prepare_flip_string(struct tty_port *port, unsigned char **chars,
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200425 size_t size)
Alan Coxe0495732008-10-13 10:36:58 +0100426{
Peter Hurleyacc0f672013-12-09 09:23:52 -0500427 int space = __tty_buffer_request_room(port, size, TTYB_NORMAL);
Alan Coxe0495732008-10-13 10:36:58 +0100428 if (likely(space)) {
Ilya Zykov64325a32013-01-19 18:16:20 +0400429 struct tty_buffer *tb = port->buf.tail;
Peter Hurley1fc359f2013-06-15 09:36:01 -0400430 *chars = char_buf_ptr(tb, tb->used);
Peter Hurleyacc0f672013-12-09 09:23:52 -0500431 if (~tb->flags & TTYB_NORMAL)
432 memset(flag_buf_ptr(tb, tb->used), TTY_NORMAL, space);
Alan Coxe0495732008-10-13 10:36:58 +0100433 tb->used += space;
434 }
435 return space;
436}
437EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
438
Peter Hurley8d082cd2016-01-10 20:36:13 -0800439/**
440 * tty_ldisc_receive_buf - forward data to line discipline
441 * @ld: line discipline to process input
442 * @p: char buffer
443 * @f: TTY_* flags buffer
444 * @count: number of bytes to process
445 *
446 * Callers other than flush_to_ldisc() need to exclude the kworker
447 * from concurrent use of the line discipline, see paste_selection().
448 *
Johan Hovoldd76dabb2017-11-03 15:18:05 +0100449 * Returns the number of bytes processed
Peter Hurley8d082cd2016-01-10 20:36:13 -0800450 */
451int tty_ldisc_receive_buf(struct tty_ldisc *ld, unsigned char *p,
452 char *f, int count)
453{
454 if (ld->ops->receive_buf2)
455 count = ld->ops->receive_buf2(ld->tty, p, f, count);
456 else {
457 count = min_t(int, count, ld->tty->receive_room);
458 if (count && ld->ops->receive_buf)
459 ld->ops->receive_buf(ld->tty, p, f, count);
460 }
Linus Torvalds35404602018-10-04 11:06:13 -0700461 if (count > 0)
462 memset(p, 0, count);
Peter Hurley8d082cd2016-01-10 20:36:13 -0800463 return count;
464}
465EXPORT_SYMBOL_GPL(tty_ldisc_receive_buf);
Alan Coxe0495732008-10-13 10:36:58 +0100466
Peter Hurleyda261e72013-06-15 09:14:14 -0400467static int
Peter Hurley8d082cd2016-01-10 20:36:13 -0800468receive_buf(struct tty_ldisc *ld, struct tty_buffer *head, int count)
Peter Hurleyda261e72013-06-15 09:14:14 -0400469{
Peter Hurley1fc359f2013-06-15 09:36:01 -0400470 unsigned char *p = char_buf_ptr(head, head->read);
Peter Hurleyacc0f672013-12-09 09:23:52 -0500471 char *f = NULL;
472
473 if (~head->flags & TTYB_NORMAL)
474 f = flag_buf_ptr(head, head->read);
Peter Hurleyda261e72013-06-15 09:14:14 -0400475
Peter Hurley8d082cd2016-01-10 20:36:13 -0800476 return tty_ldisc_receive_buf(ld, p, f, count);
Peter Hurleyda261e72013-06-15 09:14:14 -0400477}
Alan Coxe0495732008-10-13 10:36:58 +0100478
479/**
480 * flush_to_ldisc
481 * @work: tty structure passed from work queue.
482 *
483 * This routine is called out of the software interrupt to flush data
484 * from the buffer chain to the line discipline.
485 *
Peter Hurleye9975fd2013-06-15 09:36:10 -0400486 * The receive_buf method is single threaded for each tty instance.
487 *
Peter Hurleya7c8d582013-06-15 09:36:15 -0400488 * Locking: takes buffer lock to ensure single-threaded flip buffer
Peter Hurleye9975fd2013-06-15 09:36:10 -0400489 * 'consumer'
Alan Coxe0495732008-10-13 10:36:58 +0100490 */
491
492static void flush_to_ldisc(struct work_struct *work)
493{
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200494 struct tty_port *port = container_of(work, struct tty_port, buf.work);
495 struct tty_bufhead *buf = &port->buf;
496 struct tty_struct *tty;
Alan Coxe0495732008-10-13 10:36:58 +0100497 struct tty_ldisc *disc;
Alan Coxe0495732008-10-13 10:36:58 +0100498
Dmitry Vyukov70982962015-09-17 17:17:08 +0200499 tty = READ_ONCE(port->itty);
Jiri Slaby34dcfb82013-02-27 22:30:24 +0100500 if (tty == NULL)
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200501 return;
502
Alan Coxe0495732008-10-13 10:36:58 +0100503 disc = tty_ldisc_ref(tty);
Peter Hurley36697522013-06-15 07:04:48 -0400504 if (disc == NULL)
Alan Coxe0495732008-10-13 10:36:58 +0100505 return;
506
Peter Hurleya7c8d582013-06-15 09:36:15 -0400507 mutex_lock(&buf->lock);
Linus Torvalds45242002009-10-14 08:59:49 -0700508
Peter Hurleyd7a68be2013-06-15 09:36:11 -0400509 while (1) {
510 struct tty_buffer *head = buf->head;
Peter Hurley62a0d8d2014-05-02 10:56:12 -0400511 struct tty_buffer *next;
Peter Hurleyd7a68be2013-06-15 09:36:11 -0400512 int count;
Linus Torvalds45242002009-10-14 08:59:49 -0700513
Peter Hurleya7c8d582013-06-15 09:36:15 -0400514 /* Ldisc or user is trying to gain exclusive access */
515 if (atomic_read(&buf->priority))
Peter Hurleyd7a68be2013-06-15 09:36:11 -0400516 break;
Peter Hurleye9975fd2013-06-15 09:36:10 -0400517
Peter Hurley069f38b2015-07-12 20:50:49 -0400518 /* paired w/ release in __tty_buffer_request_room();
Peter Hurley62a0d8d2014-05-02 10:56:12 -0400519 * ensures commit value read is not stale if the head
520 * is advancing to the next buffer
521 */
Peter Hurley069f38b2015-07-12 20:50:49 -0400522 next = smp_load_acquire(&head->next);
Dmitry Vyukovfacd8852015-09-17 17:17:10 +0200523 /* paired w/ release in __tty_buffer_request_room() or in
524 * tty_buffer_flush(); ensures we see the committed buffer data
525 */
526 count = smp_load_acquire(&head->commit) - head->read;
Peter Hurleyd7a68be2013-06-15 09:36:11 -0400527 if (!count) {
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -0800528 if (next == NULL)
Peter Hurley39f610e2013-03-20 13:20:43 -0400529 break;
Peter Hurley62a0d8d2014-05-02 10:56:12 -0400530 buf->head = next;
Peter Hurleyd7a68be2013-06-15 09:36:11 -0400531 tty_buffer_free(port, head);
532 continue;
Alan Coxe0495732008-10-13 10:36:58 +0100533 }
Peter Hurleyd7a68be2013-06-15 09:36:11 -0400534
Peter Hurley8d082cd2016-01-10 20:36:13 -0800535 count = receive_buf(disc, head, count);
Peter Hurleyd7a68be2013-06-15 09:36:11 -0400536 if (!count)
537 break;
Peter Hurleyaf5554f2015-07-12 20:50:50 -0400538 head->read += count;
Alan Coxe0495732008-10-13 10:36:58 +0100539 }
Linus Torvalds45242002009-10-14 08:59:49 -0700540
Peter Hurleya7c8d582013-06-15 09:36:15 -0400541 mutex_unlock(&buf->lock);
Alan Coxe0495732008-10-13 10:36:58 +0100542
543 tty_ldisc_deref(disc);
544}
545
546/**
Alan Coxe0495732008-10-13 10:36:58 +0100547 * tty_flip_buffer_push - terminal
Jiri Slaby2e124b42013-01-03 15:53:06 +0100548 * @port: tty port to push
Alan Coxe0495732008-10-13 10:36:58 +0100549 *
Peter Hurleya9c3f682014-02-22 07:31:21 -0500550 * Queue a push of the terminal flip buffers to the line discipline.
551 * Can be called from IRQ/atomic context.
Alan Coxe0495732008-10-13 10:36:58 +0100552 *
553 * In the event of the queue being busy for flipping the work will be
554 * held off and retried later.
Alan Coxe0495732008-10-13 10:36:58 +0100555 */
556
Jiri Slaby2e124b42013-01-03 15:53:06 +0100557void tty_flip_buffer_push(struct tty_port *port)
Alan Coxe0495732008-10-13 10:36:58 +0100558{
Peter Hurleya9c3f682014-02-22 07:31:21 -0500559 tty_schedule_flip(port);
Alan Coxe0495732008-10-13 10:36:58 +0100560}
561EXPORT_SYMBOL(tty_flip_buffer_push);
562
563/**
564 * tty_buffer_init - prepare a tty buffer structure
565 * @tty: tty to initialise
566 *
567 * Set up the initial state of the buffer management for a tty device.
568 * Must be called before the other tty buffer functions are used.
Alan Coxe0495732008-10-13 10:36:58 +0100569 */
570
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200571void tty_buffer_init(struct tty_port *port)
Alan Coxe0495732008-10-13 10:36:58 +0100572{
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200573 struct tty_bufhead *buf = &port->buf;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200574
Peter Hurleya7c8d582013-06-15 09:36:15 -0400575 mutex_init(&buf->lock);
Peter Hurley7391ee12013-06-15 09:36:07 -0400576 tty_buffer_reset(&buf->sentinel, 0);
577 buf->head = &buf->sentinel;
578 buf->tail = &buf->sentinel;
Peter Hurley809850b2013-06-15 09:36:06 -0400579 init_llist_head(&buf->free);
Peter Hurley5dda4ca2013-11-22 12:09:56 -0500580 atomic_set(&buf->mem_used, 0);
Peter Hurleya7c8d582013-06-15 09:36:15 -0400581 atomic_set(&buf->priority, 0);
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200582 INIT_WORK(&buf->work, flush_to_ldisc);
Peter Hurley4d18e6ef2013-11-22 12:09:55 -0500583 buf->mem_limit = TTYB_DEFAULT_MEM_LIMIT;
Alan Coxe0495732008-10-13 10:36:58 +0100584}
Peter Hurley4d18e6ef2013-11-22 12:09:55 -0500585
586/**
587 * tty_buffer_set_limit - change the tty buffer memory limit
588 * @port: tty port to change
589 *
590 * Change the tty buffer memory limit.
591 * Must be called before the other tty buffer functions are used.
592 */
593
594int tty_buffer_set_limit(struct tty_port *port, int limit)
595{
596 if (limit < MIN_TTYB_SIZE)
597 return -EINVAL;
598 port->buf.mem_limit = limit;
599 return 0;
600}
601EXPORT_SYMBOL_GPL(tty_buffer_set_limit);
Peter Hurley1d1d14d2015-01-17 15:42:05 -0500602
603/* slave ptys can claim nested buffer lock when handling BRK and INTR */
604void tty_buffer_set_lock_subclass(struct tty_port *port)
605{
606 lockdep_set_subclass(&port->buf.lock, TTY_LOCK_SLAVE);
607}
Peter Hurleye1760582015-10-17 16:36:23 -0400608
609bool tty_buffer_restart_work(struct tty_port *port)
610{
611 return queue_work(system_unbound_wq, &port->buf.work);
612}
613
614bool tty_buffer_cancel_work(struct tty_port *port)
615{
616 return cancel_work_sync(&port->buf.work);
617}
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -0800618
619void tty_buffer_flush_work(struct tty_port *port)
620{
621 flush_work(&port->buf.work);
622}