blob: aa80dc94ddc2fe6969049a62479548c22a7931b5 [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/**
365 * tty_schedule_flip - push characters to ldisc
Jiri Slaby6732c8b2013-01-03 15:53:07 +0100366 * @port: tty port to push from
Alan Coxe0495732008-10-13 10:36:58 +0100367 *
368 * Takes any pending buffers and transfers their ownership to the
369 * ldisc side of the queue. It then schedules those characters for
370 * processing by the line discipline.
Alan Coxe0495732008-10-13 10:36:58 +0100371 */
372
Jiri Slaby6732c8b2013-01-03 15:53:07 +0100373void tty_schedule_flip(struct tty_port *port)
Alan Coxe0495732008-10-13 10:36:58 +0100374{
Jiri Slaby6732c8b2013-01-03 15:53:07 +0100375 struct tty_bufhead *buf = &port->buf;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200376
Dmitry Vyukovfacd8852015-09-17 17:17:10 +0200377 /* paired w/ acquire in flush_to_ldisc(); ensures
378 * flush_to_ldisc() sees buffer data.
379 */
380 smp_store_release(&buf->tail->commit, buf->tail->used);
Peter Hurleye052c6d2015-10-17 16:36:24 -0400381 queue_work(system_unbound_wq, &buf->work);
Alan Coxe0495732008-10-13 10:36:58 +0100382}
383EXPORT_SYMBOL(tty_schedule_flip);
384
385/**
386 * tty_prepare_flip_string - make room for characters
Jiri Slaby2f693352013-01-03 15:53:02 +0100387 * @port: tty port
Alan Coxe0495732008-10-13 10:36:58 +0100388 * @chars: return pointer for character write area
389 * @size: desired size
390 *
391 * Prepare a block of space in the buffer for data. Returns the length
392 * available and buffer pointer to the space which is now allocated and
393 * accounted for as ready for normal characters. This is used for drivers
394 * that need their own block copy routines into the buffer. There is no
395 * guarantee the buffer is a DMA target!
Alan Coxe0495732008-10-13 10:36:58 +0100396 */
397
Jiri Slaby2f693352013-01-03 15:53:02 +0100398int tty_prepare_flip_string(struct tty_port *port, unsigned char **chars,
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200399 size_t size)
Alan Coxe0495732008-10-13 10:36:58 +0100400{
Peter Hurleyacc0f672013-12-09 09:23:52 -0500401 int space = __tty_buffer_request_room(port, size, TTYB_NORMAL);
Alan Coxe0495732008-10-13 10:36:58 +0100402 if (likely(space)) {
Ilya Zykov64325a32013-01-19 18:16:20 +0400403 struct tty_buffer *tb = port->buf.tail;
Peter Hurley1fc359f2013-06-15 09:36:01 -0400404 *chars = char_buf_ptr(tb, tb->used);
Peter Hurleyacc0f672013-12-09 09:23:52 -0500405 if (~tb->flags & TTYB_NORMAL)
406 memset(flag_buf_ptr(tb, tb->used), TTY_NORMAL, space);
Alan Coxe0495732008-10-13 10:36:58 +0100407 tb->used += space;
408 }
409 return space;
410}
411EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
412
Peter Hurley8d082cd2016-01-10 20:36:13 -0800413/**
414 * tty_ldisc_receive_buf - forward data to line discipline
415 * @ld: line discipline to process input
416 * @p: char buffer
417 * @f: TTY_* flags buffer
418 * @count: number of bytes to process
419 *
420 * Callers other than flush_to_ldisc() need to exclude the kworker
421 * from concurrent use of the line discipline, see paste_selection().
422 *
423 * Returns the number of bytes not processed
424 */
425int tty_ldisc_receive_buf(struct tty_ldisc *ld, unsigned char *p,
426 char *f, int count)
427{
428 if (ld->ops->receive_buf2)
429 count = ld->ops->receive_buf2(ld->tty, p, f, count);
430 else {
431 count = min_t(int, count, ld->tty->receive_room);
432 if (count && ld->ops->receive_buf)
433 ld->ops->receive_buf(ld->tty, p, f, count);
434 }
435 return count;
436}
437EXPORT_SYMBOL_GPL(tty_ldisc_receive_buf);
Alan Coxe0495732008-10-13 10:36:58 +0100438
Peter Hurleyda261e72013-06-15 09:14:14 -0400439static int
Peter Hurley8d082cd2016-01-10 20:36:13 -0800440receive_buf(struct tty_ldisc *ld, struct tty_buffer *head, int count)
Peter Hurleyda261e72013-06-15 09:14:14 -0400441{
Peter Hurley1fc359f2013-06-15 09:36:01 -0400442 unsigned char *p = char_buf_ptr(head, head->read);
Peter Hurleyacc0f672013-12-09 09:23:52 -0500443 char *f = NULL;
444
445 if (~head->flags & TTYB_NORMAL)
446 f = flag_buf_ptr(head, head->read);
Peter Hurleyda261e72013-06-15 09:14:14 -0400447
Peter Hurley8d082cd2016-01-10 20:36:13 -0800448 return tty_ldisc_receive_buf(ld, p, f, count);
Peter Hurleyda261e72013-06-15 09:14:14 -0400449}
Alan Coxe0495732008-10-13 10:36:58 +0100450
451/**
452 * flush_to_ldisc
453 * @work: tty structure passed from work queue.
454 *
455 * This routine is called out of the software interrupt to flush data
456 * from the buffer chain to the line discipline.
457 *
Peter Hurleye9975fd2013-06-15 09:36:10 -0400458 * The receive_buf method is single threaded for each tty instance.
459 *
Peter Hurleya7c8d582013-06-15 09:36:15 -0400460 * Locking: takes buffer lock to ensure single-threaded flip buffer
Peter Hurleye9975fd2013-06-15 09:36:10 -0400461 * 'consumer'
Alan Coxe0495732008-10-13 10:36:58 +0100462 */
463
464static void flush_to_ldisc(struct work_struct *work)
465{
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200466 struct tty_port *port = container_of(work, struct tty_port, buf.work);
467 struct tty_bufhead *buf = &port->buf;
468 struct tty_struct *tty;
Alan Coxe0495732008-10-13 10:36:58 +0100469 struct tty_ldisc *disc;
Alan Coxe0495732008-10-13 10:36:58 +0100470
Dmitry Vyukov70982962015-09-17 17:17:08 +0200471 tty = READ_ONCE(port->itty);
Jiri Slaby34dcfb82013-02-27 22:30:24 +0100472 if (tty == NULL)
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200473 return;
474
Alan Coxe0495732008-10-13 10:36:58 +0100475 disc = tty_ldisc_ref(tty);
Peter Hurley36697522013-06-15 07:04:48 -0400476 if (disc == NULL)
Alan Coxe0495732008-10-13 10:36:58 +0100477 return;
478
Peter Hurleya7c8d582013-06-15 09:36:15 -0400479 mutex_lock(&buf->lock);
Linus Torvalds45242002009-10-14 08:59:49 -0700480
Peter Hurleyd7a68be2013-06-15 09:36:11 -0400481 while (1) {
482 struct tty_buffer *head = buf->head;
Peter Hurley62a0d8d2014-05-02 10:56:12 -0400483 struct tty_buffer *next;
Peter Hurleyd7a68be2013-06-15 09:36:11 -0400484 int count;
Linus Torvalds45242002009-10-14 08:59:49 -0700485
Peter Hurleya7c8d582013-06-15 09:36:15 -0400486 /* Ldisc or user is trying to gain exclusive access */
487 if (atomic_read(&buf->priority))
Peter Hurleyd7a68be2013-06-15 09:36:11 -0400488 break;
Peter Hurleye9975fd2013-06-15 09:36:10 -0400489
Peter Hurley069f38b2015-07-12 20:50:49 -0400490 /* paired w/ release in __tty_buffer_request_room();
Peter Hurley62a0d8d2014-05-02 10:56:12 -0400491 * ensures commit value read is not stale if the head
492 * is advancing to the next buffer
493 */
Peter Hurley069f38b2015-07-12 20:50:49 -0400494 next = smp_load_acquire(&head->next);
Dmitry Vyukovfacd8852015-09-17 17:17:10 +0200495 /* paired w/ release in __tty_buffer_request_room() or in
496 * tty_buffer_flush(); ensures we see the committed buffer data
497 */
498 count = smp_load_acquire(&head->commit) - head->read;
Peter Hurleyd7a68be2013-06-15 09:36:11 -0400499 if (!count) {
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -0800500 if (next == NULL)
Peter Hurley39f610e2013-03-20 13:20:43 -0400501 break;
Peter Hurley62a0d8d2014-05-02 10:56:12 -0400502 buf->head = next;
Peter Hurleyd7a68be2013-06-15 09:36:11 -0400503 tty_buffer_free(port, head);
504 continue;
Alan Coxe0495732008-10-13 10:36:58 +0100505 }
Peter Hurleyd7a68be2013-06-15 09:36:11 -0400506
Peter Hurley8d082cd2016-01-10 20:36:13 -0800507 count = receive_buf(disc, head, count);
Peter Hurleyd7a68be2013-06-15 09:36:11 -0400508 if (!count)
509 break;
Peter Hurleyaf5554f2015-07-12 20:50:50 -0400510 head->read += count;
Alan Coxe0495732008-10-13 10:36:58 +0100511 }
Linus Torvalds45242002009-10-14 08:59:49 -0700512
Peter Hurleya7c8d582013-06-15 09:36:15 -0400513 mutex_unlock(&buf->lock);
Alan Coxe0495732008-10-13 10:36:58 +0100514
515 tty_ldisc_deref(disc);
516}
517
518/**
Alan Coxe0495732008-10-13 10:36:58 +0100519 * tty_flip_buffer_push - terminal
Jiri Slaby2e124b42013-01-03 15:53:06 +0100520 * @port: tty port to push
Alan Coxe0495732008-10-13 10:36:58 +0100521 *
Peter Hurleya9c3f682014-02-22 07:31:21 -0500522 * Queue a push of the terminal flip buffers to the line discipline.
523 * Can be called from IRQ/atomic context.
Alan Coxe0495732008-10-13 10:36:58 +0100524 *
525 * In the event of the queue being busy for flipping the work will be
526 * held off and retried later.
Alan Coxe0495732008-10-13 10:36:58 +0100527 */
528
Jiri Slaby2e124b42013-01-03 15:53:06 +0100529void tty_flip_buffer_push(struct tty_port *port)
Alan Coxe0495732008-10-13 10:36:58 +0100530{
Peter Hurleya9c3f682014-02-22 07:31:21 -0500531 tty_schedule_flip(port);
Alan Coxe0495732008-10-13 10:36:58 +0100532}
533EXPORT_SYMBOL(tty_flip_buffer_push);
534
535/**
536 * tty_buffer_init - prepare a tty buffer structure
537 * @tty: tty to initialise
538 *
539 * Set up the initial state of the buffer management for a tty device.
540 * Must be called before the other tty buffer functions are used.
Alan Coxe0495732008-10-13 10:36:58 +0100541 */
542
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200543void tty_buffer_init(struct tty_port *port)
Alan Coxe0495732008-10-13 10:36:58 +0100544{
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200545 struct tty_bufhead *buf = &port->buf;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200546
Peter Hurleya7c8d582013-06-15 09:36:15 -0400547 mutex_init(&buf->lock);
Peter Hurley7391ee12013-06-15 09:36:07 -0400548 tty_buffer_reset(&buf->sentinel, 0);
549 buf->head = &buf->sentinel;
550 buf->tail = &buf->sentinel;
Peter Hurley809850b2013-06-15 09:36:06 -0400551 init_llist_head(&buf->free);
Peter Hurley5dda4ca2013-11-22 12:09:56 -0500552 atomic_set(&buf->mem_used, 0);
Peter Hurleya7c8d582013-06-15 09:36:15 -0400553 atomic_set(&buf->priority, 0);
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200554 INIT_WORK(&buf->work, flush_to_ldisc);
Peter Hurley4d18e6ef2013-11-22 12:09:55 -0500555 buf->mem_limit = TTYB_DEFAULT_MEM_LIMIT;
Alan Coxe0495732008-10-13 10:36:58 +0100556}
Peter Hurley4d18e6ef2013-11-22 12:09:55 -0500557
558/**
559 * tty_buffer_set_limit - change the tty buffer memory limit
560 * @port: tty port to change
561 *
562 * Change the tty buffer memory limit.
563 * Must be called before the other tty buffer functions are used.
564 */
565
566int tty_buffer_set_limit(struct tty_port *port, int limit)
567{
568 if (limit < MIN_TTYB_SIZE)
569 return -EINVAL;
570 port->buf.mem_limit = limit;
571 return 0;
572}
573EXPORT_SYMBOL_GPL(tty_buffer_set_limit);
Peter Hurley1d1d14d2015-01-17 15:42:05 -0500574
575/* slave ptys can claim nested buffer lock when handling BRK and INTR */
576void tty_buffer_set_lock_subclass(struct tty_port *port)
577{
578 lockdep_set_subclass(&port->buf.lock, TTY_LOCK_SLAVE);
579}
Peter Hurleye1760582015-10-17 16:36:23 -0400580
581bool tty_buffer_restart_work(struct tty_port *port)
582{
583 return queue_work(system_unbound_wq, &port->buf.work);
584}
585
586bool tty_buffer_cancel_work(struct tty_port *port)
587{
588 return cancel_work_sync(&port->buf.work);
589}
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -0800590
591void tty_buffer_flush_work(struct tty_port *port)
592{
593 flush_work(&port->buf.work);
594}