]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/tty/tty_buffer.c
tty: Fix flip buffer free list
[mirror_ubuntu-zesty-kernel.git] / drivers / tty / tty_buffer.c
CommitLineData
e0495736
AC
1/*
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>
14#include <linux/init.h>
15#include <linux/wait.h>
16#include <linux/bitops.h>
17#include <linux/delay.h>
18#include <linux/module.h>
593fb1ae 19#include <linux/ratelimit.h>
e0495736 20
1cef50e3
PH
21
22#define MIN_TTYB_SIZE 256
23#define TTYB_ALIGN_MASK 255
24
e0495736
AC
25/**
26 * tty_buffer_free_all - free buffers used by a tty
27 * @tty: tty to free from
28 *
29 * Remove all the buffers pending on a tty whether queued with data
30 * or in the free ring. Must be called when the tty is no longer in use
31 *
32 * Locking: none
33 */
34
ecbbfd44 35void tty_buffer_free_all(struct tty_port *port)
e0495736 36{
ecbbfd44 37 struct tty_bufhead *buf = &port->buf;
e0495736 38 struct tty_buffer *thead;
5cff39c6
JS
39
40 while ((thead = buf->head) != NULL) {
41 buf->head = thead->next;
e0495736
AC
42 kfree(thead);
43 }
5cff39c6
JS
44 while ((thead = buf->free) != NULL) {
45 buf->free = thead->next;
e0495736
AC
46 kfree(thead);
47 }
5cff39c6
JS
48 buf->tail = NULL;
49 buf->memory_used = 0;
e0495736
AC
50}
51
52/**
53 * tty_buffer_alloc - allocate a tty buffer
54 * @tty: tty device
55 * @size: desired size (characters)
56 *
57 * Allocate a new tty buffer to hold the desired number of characters.
58 * Return NULL if out of memory or the allocation would exceed the
59 * per device queue
60 *
61 * Locking: Caller must hold tty->buf.lock
62 */
63
ecbbfd44 64static struct tty_buffer *tty_buffer_alloc(struct tty_port *port, size_t size)
e0495736
AC
65{
66 struct tty_buffer *p;
67
ecbbfd44 68 if (port->buf.memory_used + size > 65536)
e0495736
AC
69 return NULL;
70 p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC);
71 if (p == NULL)
72 return NULL;
73 p->used = 0;
74 p->size = size;
75 p->next = NULL;
76 p->commit = 0;
77 p->read = 0;
ecbbfd44 78 port->buf.memory_used += size;
e0495736
AC
79 return p;
80}
81
82/**
83 * tty_buffer_free - free a tty buffer
84 * @tty: tty owning the buffer
85 * @b: the buffer to free
86 *
87 * Free a tty buffer, or add it to the free list according to our
88 * internal strategy
89 *
90 * Locking: Caller must hold tty->buf.lock
91 */
92
ecbbfd44 93static void tty_buffer_free(struct tty_port *port, struct tty_buffer *b)
e0495736 94{
ecbbfd44 95 struct tty_bufhead *buf = &port->buf;
5cff39c6 96
e0495736 97 /* Dumb strategy for now - should keep some stats */
5cff39c6
JS
98 buf->memory_used -= b->size;
99 WARN_ON(buf->memory_used < 0);
e0495736 100
1cef50e3 101 if (b->size > MIN_TTYB_SIZE)
e0495736
AC
102 kfree(b);
103 else {
5cff39c6
JS
104 b->next = buf->free;
105 buf->free = b;
e0495736
AC
106 }
107}
108
109/**
110 * __tty_buffer_flush - flush full tty buffers
111 * @tty: tty to flush
112 *
113 * flush all the buffers containing receive data. Caller must
114 * hold the buffer lock and must have ensured no parallel flush to
115 * ldisc is running.
116 *
117 * Locking: Caller must hold tty->buf.lock
118 */
119
ecbbfd44 120static void __tty_buffer_flush(struct tty_port *port)
e0495736 121{
ecbbfd44 122 struct tty_bufhead *buf = &port->buf;
e0495736
AC
123 struct tty_buffer *thead;
124
64325a3b
IZ
125 if (unlikely(buf->head == NULL))
126 return;
127 while ((thead = buf->head->next) != NULL) {
128 tty_buffer_free(port, buf->head);
129 buf->head = thead;
e0495736 130 }
64325a3b
IZ
131 WARN_ON(buf->head != buf->tail);
132 buf->head->read = buf->head->commit;
e0495736
AC
133}
134
135/**
136 * tty_buffer_flush - flush full tty buffers
137 * @tty: tty to flush
138 *
139 * flush all the buffers containing receive data. If the buffer is
140 * being processed by flush_to_ldisc then we defer the processing
141 * to that function
142 *
143 * Locking: none
144 */
145
146void tty_buffer_flush(struct tty_struct *tty)
147{
2fc20661 148 struct tty_port *port = tty->port;
ecbbfd44 149 struct tty_bufhead *buf = &port->buf;
e0495736 150 unsigned long flags;
5cff39c6
JS
151
152 spin_lock_irqsave(&buf->lock, flags);
e0495736
AC
153
154 /* If the data is being pushed to the tty layer then we can't
155 process it here. Instead set a flag and the flush_to_ldisc
156 path will process the flush request before it exits */
2fc20661
JS
157 if (test_bit(TTYP_FLUSHING, &port->iflags)) {
158 set_bit(TTYP_FLUSHPENDING, &port->iflags);
5cff39c6 159 spin_unlock_irqrestore(&buf->lock, flags);
e0495736 160 wait_event(tty->read_wait,
2fc20661 161 test_bit(TTYP_FLUSHPENDING, &port->iflags) == 0);
e0495736
AC
162 return;
163 } else
ecbbfd44 164 __tty_buffer_flush(port);
5cff39c6 165 spin_unlock_irqrestore(&buf->lock, flags);
e0495736
AC
166}
167
168/**
169 * tty_buffer_find - find a free tty buffer
170 * @tty: tty owning the buffer
171 * @size: characters wanted
172 *
173 * Locate an existing suitable tty buffer or if we are lacking one then
174 * allocate a new one. We round our buffers off in 256 character chunks
175 * to get better allocation behaviour.
176 *
177 * Locking: Caller must hold tty->buf.lock
178 */
179
ecbbfd44 180static struct tty_buffer *tty_buffer_find(struct tty_port *port, size_t size)
e0495736 181{
ecbbfd44 182 struct tty_buffer **tbh = &port->buf.free;
1cef50e3
PH
183 if (size <= MIN_TTYB_SIZE) {
184 if (*tbh) {
185 struct tty_buffer *t = *tbh;
186
e0495736
AC
187 *tbh = t->next;
188 t->next = NULL;
189 t->used = 0;
190 t->commit = 0;
191 t->read = 0;
ecbbfd44 192 port->buf.memory_used += t->size;
e0495736
AC
193 return t;
194 }
e0495736
AC
195 }
196 /* Round the buffer size out */
1cef50e3 197 size = __ALIGN_MASK(size, TTYB_ALIGN_MASK);
ecbbfd44 198 return tty_buffer_alloc(port, size);
e0495736
AC
199 /* Should possibly check if this fails for the largest buffer we
200 have queued and recycle that ? */
201}
e0495736 202/**
64325a3b 203 * tty_buffer_request_room - grow tty buffer if needed
e0495736
AC
204 * @tty: tty structure
205 * @size: size desired
206 *
207 * Make at least size bytes of linear space available for the tty
208 * buffer. If we fail return the size we managed to find.
64325a3b
IZ
209 *
210 * Locking: Takes port->buf.lock
e0495736 211 */
64325a3b 212int tty_buffer_request_room(struct tty_port *port, size_t size)
e0495736 213{
ecbbfd44 214 struct tty_bufhead *buf = &port->buf;
e0495736
AC
215 struct tty_buffer *b, *n;
216 int left;
64325a3b
IZ
217 unsigned long flags;
218 spin_lock_irqsave(&buf->lock, flags);
e0495736
AC
219 /* OPTIMISATION: We could keep a per tty "zero" sized buffer to
220 remove this conditional if its worth it. This would be invisible
221 to the callers */
5cff39c6
JS
222 b = buf->tail;
223 if (b != NULL)
e0495736
AC
224 left = b->size - b->used;
225 else
226 left = 0;
227
228 if (left < size) {
229 /* This is the slow path - looking for new buffers to use */
ecbbfd44 230 if ((n = tty_buffer_find(port, size)) != NULL) {
e0495736
AC
231 if (b != NULL) {
232 b->next = n;
233 b->commit = b->used;
234 } else
5cff39c6
JS
235 buf->head = n;
236 buf->tail = n;
e0495736
AC
237 } else
238 size = left;
239 }
64325a3b 240 spin_unlock_irqrestore(&buf->lock, flags);
e0495736
AC
241 return size;
242}
243EXPORT_SYMBOL_GPL(tty_buffer_request_room);
244
245/**
2832fc11 246 * tty_insert_flip_string_fixed_flag - Add characters to the tty buffer
2f693357 247 * @port: tty port
e0495736 248 * @chars: characters
2832fc11 249 * @flag: flag value for each character
e0495736
AC
250 * @size: size
251 *
252 * Queue a series of bytes to the tty buffering. All the characters
ccc5ca8d 253 * passed are marked with the supplied flag. Returns the number added.
e0495736 254 *
ecbbfd44 255 * Locking: Called functions may take port->buf.lock
e0495736
AC
256 */
257
2f693357 258int tty_insert_flip_string_fixed_flag(struct tty_port *port,
2832fc11 259 const unsigned char *chars, char flag, size_t size)
e0495736
AC
260{
261 int copied = 0;
262 do {
d4bee0a6 263 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
64325a3b
IZ
264 int space = tty_buffer_request_room(port, goal);
265 struct tty_buffer *tb = port->buf.tail;
e0495736 266 /* If there is no space then tb may be NULL */
c56a00a1 267 if (unlikely(space == 0)) {
e0495736 268 break;
c56a00a1 269 }
1fc359fc
PH
270 memcpy(char_buf_ptr(tb, tb->used), chars, space);
271 memset(flag_buf_ptr(tb, tb->used), flag, space);
e0495736
AC
272 tb->used += space;
273 copied += space;
274 chars += space;
275 /* There is a small chance that we need to split the data over
276 several buffers. If this is the case we must loop */
277 } while (unlikely(size > copied));
278 return copied;
279}
2832fc11 280EXPORT_SYMBOL(tty_insert_flip_string_fixed_flag);
e0495736
AC
281
282/**
283 * tty_insert_flip_string_flags - Add characters to the tty buffer
2f693357 284 * @port: tty port
e0495736
AC
285 * @chars: characters
286 * @flags: flag bytes
287 * @size: size
288 *
289 * Queue a series of bytes to the tty buffering. For each character
290 * the flags array indicates the status of the character. Returns the
291 * number added.
292 *
ecbbfd44 293 * Locking: Called functions may take port->buf.lock
e0495736
AC
294 */
295
2f693357 296int tty_insert_flip_string_flags(struct tty_port *port,
e0495736
AC
297 const unsigned char *chars, const char *flags, size_t size)
298{
299 int copied = 0;
300 do {
d4bee0a6 301 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
64325a3b
IZ
302 int space = tty_buffer_request_room(port, goal);
303 struct tty_buffer *tb = port->buf.tail;
e0495736 304 /* If there is no space then tb may be NULL */
c56a00a1 305 if (unlikely(space == 0)) {
e0495736 306 break;
c56a00a1 307 }
1fc359fc
PH
308 memcpy(char_buf_ptr(tb, tb->used), chars, space);
309 memcpy(flag_buf_ptr(tb, tb->used), flags, space);
e0495736
AC
310 tb->used += space;
311 copied += space;
312 chars += space;
313 flags += space;
314 /* There is a small chance that we need to split the data over
315 several buffers. If this is the case we must loop */
316 } while (unlikely(size > copied));
317 return copied;
318}
319EXPORT_SYMBOL(tty_insert_flip_string_flags);
320
321/**
322 * tty_schedule_flip - push characters to ldisc
6732c8bb 323 * @port: tty port to push from
e0495736
AC
324 *
325 * Takes any pending buffers and transfers their ownership to the
326 * ldisc side of the queue. It then schedules those characters for
327 * processing by the line discipline.
cee4ad1e
IS
328 * Note that this function can only be used when the low_latency flag
329 * is unset. Otherwise the workqueue won't be flushed.
e0495736 330 *
ecbbfd44 331 * Locking: Takes port->buf.lock
e0495736
AC
332 */
333
6732c8bb 334void tty_schedule_flip(struct tty_port *port)
e0495736 335{
6732c8bb 336 struct tty_bufhead *buf = &port->buf;
e0495736 337 unsigned long flags;
6732c8bb 338 WARN_ON(port->low_latency);
5cff39c6
JS
339
340 spin_lock_irqsave(&buf->lock, flags);
341 if (buf->tail != NULL)
342 buf->tail->commit = buf->tail->used;
343 spin_unlock_irqrestore(&buf->lock, flags);
344 schedule_work(&buf->work);
e0495736
AC
345}
346EXPORT_SYMBOL(tty_schedule_flip);
347
348/**
349 * tty_prepare_flip_string - make room for characters
2f693357 350 * @port: tty port
e0495736
AC
351 * @chars: return pointer for character write area
352 * @size: desired size
353 *
354 * Prepare a block of space in the buffer for data. Returns the length
355 * available and buffer pointer to the space which is now allocated and
356 * accounted for as ready for normal characters. This is used for drivers
357 * that need their own block copy routines into the buffer. There is no
358 * guarantee the buffer is a DMA target!
359 *
ecbbfd44 360 * Locking: May call functions taking port->buf.lock
e0495736
AC
361 */
362
2f693357 363int tty_prepare_flip_string(struct tty_port *port, unsigned char **chars,
ecbbfd44 364 size_t size)
e0495736 365{
64325a3b 366 int space = tty_buffer_request_room(port, size);
e0495736 367 if (likely(space)) {
64325a3b 368 struct tty_buffer *tb = port->buf.tail;
1fc359fc
PH
369 *chars = char_buf_ptr(tb, tb->used);
370 memset(flag_buf_ptr(tb, tb->used), TTY_NORMAL, space);
e0495736
AC
371 tb->used += space;
372 }
373 return space;
374}
375EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
376
377/**
378 * tty_prepare_flip_string_flags - make room for characters
2f693357 379 * @port: tty port
e0495736
AC
380 * @chars: return pointer for character write area
381 * @flags: return pointer for status flag write area
382 * @size: desired size
383 *
384 * Prepare a block of space in the buffer for data. Returns the length
385 * available and buffer pointer to the space which is now allocated and
386 * accounted for as ready for characters. This is used for drivers
387 * that need their own block copy routines into the buffer. There is no
388 * guarantee the buffer is a DMA target!
389 *
ecbbfd44 390 * Locking: May call functions taking port->buf.lock
e0495736
AC
391 */
392
2f693357 393int tty_prepare_flip_string_flags(struct tty_port *port,
e0495736
AC
394 unsigned char **chars, char **flags, size_t size)
395{
64325a3b 396 int space = tty_buffer_request_room(port, size);
e0495736 397 if (likely(space)) {
64325a3b 398 struct tty_buffer *tb = port->buf.tail;
1fc359fc
PH
399 *chars = char_buf_ptr(tb, tb->used);
400 *flags = flag_buf_ptr(tb, tb->used);
e0495736
AC
401 tb->used += space;
402 }
403 return space;
404}
405EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
406
407
da261e7f
PH
408static int
409receive_buf(struct tty_struct *tty, struct tty_buffer *head, int count)
410{
411 struct tty_ldisc *disc = tty->ldisc;
1fc359fc
PH
412 unsigned char *p = char_buf_ptr(head, head->read);
413 char *f = flag_buf_ptr(head, head->read);
da261e7f 414
24a89d1c
PH
415 if (disc->ops->receive_buf2)
416 count = disc->ops->receive_buf2(tty, p, f, count);
417 else {
418 count = min_t(int, count, tty->receive_room);
419 if (count)
420 disc->ops->receive_buf(tty, p, f, count);
421 }
da261e7f
PH
422 head->read += count;
423 return count;
424}
e0495736
AC
425
426/**
427 * flush_to_ldisc
428 * @work: tty structure passed from work queue.
429 *
430 * This routine is called out of the software interrupt to flush data
431 * from the buffer chain to the line discipline.
432 *
433 * Locking: holds tty->buf.lock to guard buffer list. Drops the lock
434 * while invoking the line discipline receive_buf method. The
435 * receive_buf method is single threaded for each tty instance.
436 */
437
438static void flush_to_ldisc(struct work_struct *work)
439{
ecbbfd44
JS
440 struct tty_port *port = container_of(work, struct tty_port, buf.work);
441 struct tty_bufhead *buf = &port->buf;
442 struct tty_struct *tty;
e0495736
AC
443 unsigned long flags;
444 struct tty_ldisc *disc;
e0495736 445
ecbbfd44 446 tty = port->itty;
34dcfb84 447 if (tty == NULL)
ecbbfd44
JS
448 return;
449
e0495736 450 disc = tty_ldisc_ref(tty);
36697529 451 if (disc == NULL)
e0495736
AC
452 return;
453
5cff39c6 454 spin_lock_irqsave(&buf->lock, flags);
45242006 455
2fc20661 456 if (!test_and_set_bit(TTYP_FLUSHING, &port->iflags)) {
81de916f 457 struct tty_buffer *head;
5cff39c6 458 while ((head = buf->head) != NULL) {
45242006 459 int count;
45242006
LT
460
461 count = head->commit - head->read;
e0495736
AC
462 if (!count) {
463 if (head->next == NULL)
464 break;
5cff39c6 465 buf->head = head->next;
ecbbfd44 466 tty_buffer_free(port, head);
e0495736
AC
467 continue;
468 }
5cff39c6 469 spin_unlock_irqrestore(&buf->lock, flags);
da261e7f
PH
470
471 count = receive_buf(tty, head, count);
472
5cff39c6 473 spin_lock_irqsave(&buf->lock, flags);
39f610e4
PH
474 /* Ldisc or user is trying to flush the buffers.
475 We may have a deferred request to flush the
476 input buffer, if so pull the chain under the lock
477 and empty the queue */
478 if (test_bit(TTYP_FLUSHPENDING, &port->iflags)) {
479 __tty_buffer_flush(port);
480 clear_bit(TTYP_FLUSHPENDING, &port->iflags);
481 wake_up(&tty->read_wait);
482 break;
da261e7f
PH
483 } else if (!count)
484 break;
e0495736 485 }
2fc20661 486 clear_bit(TTYP_FLUSHING, &port->iflags);
e0495736 487 }
45242006 488
5cff39c6 489 spin_unlock_irqrestore(&buf->lock, flags);
e0495736
AC
490
491 tty_ldisc_deref(disc);
492}
493
e043e42b
OH
494/**
495 * tty_flush_to_ldisc
496 * @tty: tty to push
497 *
498 * Push the terminal flip buffers to the line discipline.
499 *
500 * Must not be called from IRQ context.
501 */
502void tty_flush_to_ldisc(struct tty_struct *tty)
503{
d6c53c0e 504 if (!tty->port->low_latency)
ecbbfd44 505 flush_work(&tty->port->buf.work);
e043e42b
OH
506}
507
e0495736
AC
508/**
509 * tty_flip_buffer_push - terminal
2e124b4a 510 * @port: tty port to push
e0495736
AC
511 *
512 * Queue a push of the terminal flip buffers to the line discipline. This
d6c53c0e
JS
513 * function must not be called from IRQ context if port->low_latency is
514 * set.
e0495736
AC
515 *
516 * In the event of the queue being busy for flipping the work will be
517 * held off and retried later.
518 *
519 * Locking: tty buffer lock. Driver locks in low latency mode.
520 */
521
2e124b4a 522void tty_flip_buffer_push(struct tty_port *port)
e0495736 523{
2e124b4a 524 struct tty_bufhead *buf = &port->buf;
e0495736 525 unsigned long flags;
5cff39c6
JS
526
527 spin_lock_irqsave(&buf->lock, flags);
528 if (buf->tail != NULL)
529 buf->tail->commit = buf->tail->used;
530 spin_unlock_irqrestore(&buf->lock, flags);
e0495736 531
2e124b4a 532 if (port->low_latency)
5cff39c6 533 flush_to_ldisc(&buf->work);
e0495736 534 else
5cff39c6 535 schedule_work(&buf->work);
e0495736
AC
536}
537EXPORT_SYMBOL(tty_flip_buffer_push);
538
539/**
540 * tty_buffer_init - prepare a tty buffer structure
541 * @tty: tty to initialise
542 *
543 * Set up the initial state of the buffer management for a tty device.
544 * Must be called before the other tty buffer functions are used.
545 *
546 * Locking: none
547 */
548
ecbbfd44 549void tty_buffer_init(struct tty_port *port)
e0495736 550{
ecbbfd44 551 struct tty_bufhead *buf = &port->buf;
5cff39c6
JS
552
553 spin_lock_init(&buf->lock);
554 buf->head = NULL;
555 buf->tail = NULL;
556 buf->free = NULL;
557 buf->memory_used = 0;
558 INIT_WORK(&buf->work, flush_to_ldisc);
e0495736
AC
559}
560