blob: 6b1dc32cfa2249dbaa1c735069373e149113c380 [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 */
Peter Hurley4d18e6ef2013-11-22 12:09:55 -050028#define TTYB_DEFAULT_MEM_LIMIT 65536
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{
375 struct tty_buffer *tb = port->buf.tail;
376 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 Bergmann75046242017-06-20 23:10:42 +0200381 if (~tb->flags & TTYB_NORMAL)
382 *flag_buf_ptr(tb, tb->used) = flag;
Arnd Bergmannf61a07f2017-06-20 23:10:41 +0200383 *char_buf_ptr(tb, tb->used++) = ch;
384
385 return 1;
386}
387EXPORT_SYMBOL(__tty_insert_flip_char);
388
389/**
Alan Coxe0495732008-10-13 10:36:58 +0100390 * tty_schedule_flip - push characters to ldisc
Jiri Slaby6732c8b2013-01-03 15:53:07 +0100391 * @port: tty port to push from
Alan Coxe0495732008-10-13 10:36:58 +0100392 *
393 * Takes any pending buffers and transfers their ownership to the
394 * ldisc side of the queue. It then schedules those characters for
395 * processing by the line discipline.
Alan Coxe0495732008-10-13 10:36:58 +0100396 */
397
Jiri Slaby6732c8b2013-01-03 15:53:07 +0100398void tty_schedule_flip(struct tty_port *port)
Alan Coxe0495732008-10-13 10:36:58 +0100399{
Jiri Slaby6732c8b2013-01-03 15:53:07 +0100400 struct tty_bufhead *buf = &port->buf;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200401
Dmitry Vyukovfacd8852015-09-17 17:17:10 +0200402 /* paired w/ acquire in flush_to_ldisc(); ensures
403 * flush_to_ldisc() sees buffer data.
404 */
405 smp_store_release(&buf->tail->commit, buf->tail->used);
Peter Hurleye052c6d2015-10-17 16:36:24 -0400406 queue_work(system_unbound_wq, &buf->work);
Alan Coxe0495732008-10-13 10:36:58 +0100407}
408EXPORT_SYMBOL(tty_schedule_flip);
409
410/**
411 * tty_prepare_flip_string - make room for characters
Jiri Slaby2f693352013-01-03 15:53:02 +0100412 * @port: tty port
Alan Coxe0495732008-10-13 10:36:58 +0100413 * @chars: return pointer for character write area
414 * @size: desired size
415 *
416 * Prepare a block of space in the buffer for data. Returns the length
417 * available and buffer pointer to the space which is now allocated and
418 * accounted for as ready for normal characters. This is used for drivers
419 * that need their own block copy routines into the buffer. There is no
420 * guarantee the buffer is a DMA target!
Alan Coxe0495732008-10-13 10:36:58 +0100421 */
422
Jiri Slaby2f693352013-01-03 15:53:02 +0100423int tty_prepare_flip_string(struct tty_port *port, unsigned char **chars,
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200424 size_t size)
Alan Coxe0495732008-10-13 10:36:58 +0100425{
Peter Hurleyacc0f672013-12-09 09:23:52 -0500426 int space = __tty_buffer_request_room(port, size, TTYB_NORMAL);
Alan Coxe0495732008-10-13 10:36:58 +0100427 if (likely(space)) {
Ilya Zykov64325a32013-01-19 18:16:20 +0400428 struct tty_buffer *tb = port->buf.tail;
Peter Hurley1fc359f2013-06-15 09:36:01 -0400429 *chars = char_buf_ptr(tb, tb->used);
Peter Hurleyacc0f672013-12-09 09:23:52 -0500430 if (~tb->flags & TTYB_NORMAL)
431 memset(flag_buf_ptr(tb, tb->used), TTY_NORMAL, space);
Alan Coxe0495732008-10-13 10:36:58 +0100432 tb->used += space;
433 }
434 return space;
435}
436EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
437
Peter Hurley8d082cd2016-01-10 20:36:13 -0800438/**
439 * tty_ldisc_receive_buf - forward data to line discipline
440 * @ld: line discipline to process input
441 * @p: char buffer
442 * @f: TTY_* flags buffer
443 * @count: number of bytes to process
444 *
445 * Callers other than flush_to_ldisc() need to exclude the kworker
446 * from concurrent use of the line discipline, see paste_selection().
447 *
448 * Returns the number of bytes not processed
449 */
450int tty_ldisc_receive_buf(struct tty_ldisc *ld, unsigned char *p,
451 char *f, int count)
452{
453 if (ld->ops->receive_buf2)
454 count = ld->ops->receive_buf2(ld->tty, p, f, count);
455 else {
456 count = min_t(int, count, ld->tty->receive_room);
457 if (count && ld->ops->receive_buf)
458 ld->ops->receive_buf(ld->tty, p, f, count);
459 }
460 return count;
461}
462EXPORT_SYMBOL_GPL(tty_ldisc_receive_buf);
Alan Coxe0495732008-10-13 10:36:58 +0100463
Peter Hurleyda261e72013-06-15 09:14:14 -0400464static int
Peter Hurley8d082cd2016-01-10 20:36:13 -0800465receive_buf(struct tty_ldisc *ld, struct tty_buffer *head, int count)
Peter Hurleyda261e72013-06-15 09:14:14 -0400466{
Peter Hurley1fc359f2013-06-15 09:36:01 -0400467 unsigned char *p = char_buf_ptr(head, head->read);
Peter Hurleyacc0f672013-12-09 09:23:52 -0500468 char *f = NULL;
469
470 if (~head->flags & TTYB_NORMAL)
471 f = flag_buf_ptr(head, head->read);
Peter Hurleyda261e72013-06-15 09:14:14 -0400472
Peter Hurley8d082cd2016-01-10 20:36:13 -0800473 return tty_ldisc_receive_buf(ld, p, f, count);
Peter Hurleyda261e72013-06-15 09:14:14 -0400474}
Alan Coxe0495732008-10-13 10:36:58 +0100475
476/**
477 * flush_to_ldisc
478 * @work: tty structure passed from work queue.
479 *
480 * This routine is called out of the software interrupt to flush data
481 * from the buffer chain to the line discipline.
482 *
Peter Hurleye9975fd2013-06-15 09:36:10 -0400483 * The receive_buf method is single threaded for each tty instance.
484 *
Peter Hurleya7c8d582013-06-15 09:36:15 -0400485 * Locking: takes buffer lock to ensure single-threaded flip buffer
Peter Hurleye9975fd2013-06-15 09:36:10 -0400486 * 'consumer'
Alan Coxe0495732008-10-13 10:36:58 +0100487 */
488
489static void flush_to_ldisc(struct work_struct *work)
490{
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200491 struct tty_port *port = container_of(work, struct tty_port, buf.work);
492 struct tty_bufhead *buf = &port->buf;
493 struct tty_struct *tty;
Alan Coxe0495732008-10-13 10:36:58 +0100494 struct tty_ldisc *disc;
Alan Coxe0495732008-10-13 10:36:58 +0100495
Dmitry Vyukov70982962015-09-17 17:17:08 +0200496 tty = READ_ONCE(port->itty);
Jiri Slaby34dcfb82013-02-27 22:30:24 +0100497 if (tty == NULL)
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200498 return;
499
Alan Coxe0495732008-10-13 10:36:58 +0100500 disc = tty_ldisc_ref(tty);
Peter Hurley36697522013-06-15 07:04:48 -0400501 if (disc == NULL)
Alan Coxe0495732008-10-13 10:36:58 +0100502 return;
503
Peter Hurleya7c8d582013-06-15 09:36:15 -0400504 mutex_lock(&buf->lock);
Linus Torvalds45242002009-10-14 08:59:49 -0700505
Peter Hurleyd7a68be2013-06-15 09:36:11 -0400506 while (1) {
507 struct tty_buffer *head = buf->head;
Peter Hurley62a0d8d2014-05-02 10:56:12 -0400508 struct tty_buffer *next;
Peter Hurleyd7a68be2013-06-15 09:36:11 -0400509 int count;
Linus Torvalds45242002009-10-14 08:59:49 -0700510
Peter Hurleya7c8d582013-06-15 09:36:15 -0400511 /* Ldisc or user is trying to gain exclusive access */
512 if (atomic_read(&buf->priority))
Peter Hurleyd7a68be2013-06-15 09:36:11 -0400513 break;
Peter Hurleye9975fd2013-06-15 09:36:10 -0400514
Peter Hurley069f38b2015-07-12 20:50:49 -0400515 /* paired w/ release in __tty_buffer_request_room();
Peter Hurley62a0d8d2014-05-02 10:56:12 -0400516 * ensures commit value read is not stale if the head
517 * is advancing to the next buffer
518 */
Peter Hurley069f38b2015-07-12 20:50:49 -0400519 next = smp_load_acquire(&head->next);
Dmitry Vyukovfacd8852015-09-17 17:17:10 +0200520 /* paired w/ release in __tty_buffer_request_room() or in
521 * tty_buffer_flush(); ensures we see the committed buffer data
522 */
523 count = smp_load_acquire(&head->commit) - head->read;
Peter Hurleyd7a68be2013-06-15 09:36:11 -0400524 if (!count) {
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -0800525 if (next == NULL)
Peter Hurley39f610e2013-03-20 13:20:43 -0400526 break;
Peter Hurley62a0d8d2014-05-02 10:56:12 -0400527 buf->head = next;
Peter Hurleyd7a68be2013-06-15 09:36:11 -0400528 tty_buffer_free(port, head);
529 continue;
Alan Coxe0495732008-10-13 10:36:58 +0100530 }
Peter Hurleyd7a68be2013-06-15 09:36:11 -0400531
Peter Hurley8d082cd2016-01-10 20:36:13 -0800532 count = receive_buf(disc, head, count);
Peter Hurleyd7a68be2013-06-15 09:36:11 -0400533 if (!count)
534 break;
Peter Hurleyaf5554f2015-07-12 20:50:50 -0400535 head->read += count;
Alan Coxe0495732008-10-13 10:36:58 +0100536 }
Linus Torvalds45242002009-10-14 08:59:49 -0700537
Peter Hurleya7c8d582013-06-15 09:36:15 -0400538 mutex_unlock(&buf->lock);
Alan Coxe0495732008-10-13 10:36:58 +0100539
540 tty_ldisc_deref(disc);
541}
542
543/**
Alan Coxe0495732008-10-13 10:36:58 +0100544 * tty_flip_buffer_push - terminal
Jiri Slaby2e124b42013-01-03 15:53:06 +0100545 * @port: tty port to push
Alan Coxe0495732008-10-13 10:36:58 +0100546 *
Peter Hurleya9c3f682014-02-22 07:31:21 -0500547 * Queue a push of the terminal flip buffers to the line discipline.
548 * Can be called from IRQ/atomic context.
Alan Coxe0495732008-10-13 10:36:58 +0100549 *
550 * In the event of the queue being busy for flipping the work will be
551 * held off and retried later.
Alan Coxe0495732008-10-13 10:36:58 +0100552 */
553
Jiri Slaby2e124b42013-01-03 15:53:06 +0100554void tty_flip_buffer_push(struct tty_port *port)
Alan Coxe0495732008-10-13 10:36:58 +0100555{
Peter Hurleya9c3f682014-02-22 07:31:21 -0500556 tty_schedule_flip(port);
Alan Coxe0495732008-10-13 10:36:58 +0100557}
558EXPORT_SYMBOL(tty_flip_buffer_push);
559
560/**
561 * tty_buffer_init - prepare a tty buffer structure
562 * @tty: tty to initialise
563 *
564 * Set up the initial state of the buffer management for a tty device.
565 * Must be called before the other tty buffer functions are used.
Alan Coxe0495732008-10-13 10:36:58 +0100566 */
567
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200568void tty_buffer_init(struct tty_port *port)
Alan Coxe0495732008-10-13 10:36:58 +0100569{
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200570 struct tty_bufhead *buf = &port->buf;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200571
Peter Hurleya7c8d582013-06-15 09:36:15 -0400572 mutex_init(&buf->lock);
Peter Hurley7391ee12013-06-15 09:36:07 -0400573 tty_buffer_reset(&buf->sentinel, 0);
574 buf->head = &buf->sentinel;
575 buf->tail = &buf->sentinel;
Peter Hurley809850b2013-06-15 09:36:06 -0400576 init_llist_head(&buf->free);
Peter Hurley5dda4ca2013-11-22 12:09:56 -0500577 atomic_set(&buf->mem_used, 0);
Peter Hurleya7c8d582013-06-15 09:36:15 -0400578 atomic_set(&buf->priority, 0);
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200579 INIT_WORK(&buf->work, flush_to_ldisc);
Peter Hurley4d18e6ef2013-11-22 12:09:55 -0500580 buf->mem_limit = TTYB_DEFAULT_MEM_LIMIT;
Alan Coxe0495732008-10-13 10:36:58 +0100581}
Peter Hurley4d18e6ef2013-11-22 12:09:55 -0500582
583/**
584 * tty_buffer_set_limit - change the tty buffer memory limit
585 * @port: tty port to change
586 *
587 * Change the tty buffer memory limit.
588 * Must be called before the other tty buffer functions are used.
589 */
590
591int tty_buffer_set_limit(struct tty_port *port, int limit)
592{
593 if (limit < MIN_TTYB_SIZE)
594 return -EINVAL;
595 port->buf.mem_limit = limit;
596 return 0;
597}
598EXPORT_SYMBOL_GPL(tty_buffer_set_limit);
Peter Hurley1d1d14d2015-01-17 15:42:05 -0500599
600/* slave ptys can claim nested buffer lock when handling BRK and INTR */
601void tty_buffer_set_lock_subclass(struct tty_port *port)
602{
603 lockdep_set_subclass(&port->buf.lock, TTY_LOCK_SLAVE);
604}
Peter Hurleye1760582015-10-17 16:36:23 -0400605
606bool tty_buffer_restart_work(struct tty_port *port)
607{
608 return queue_work(system_unbound_wq, &port->buf.work);
609}
610
611bool tty_buffer_cancel_work(struct tty_port *port)
612{
613 return cancel_work_sync(&port->buf.work);
614}
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -0800615
616void tty_buffer_flush_work(struct tty_port *port)
617{
618 flush_work(&port->buf.work);
619}