2 * Tty buffer allocation management
5 #include <linux/types.h>
6 #include <linux/errno.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>
21 * tty_buffer_free_all - free buffers used by a tty
22 * @tty: tty to free from
24 * Remove all the buffers pending on a tty whether queued with data
25 * or in the free ring. Must be called when the tty is no longer in use
30 void tty_buffer_free_all(struct tty_struct *tty)
32 struct tty_buffer *thead;
33 while ((thead = tty->buf.head) != NULL) {
34 tty->buf.head = thead->next;
37 while ((thead = tty->buf.free) != NULL) {
38 tty->buf.free = thead->next;
42 tty->buf.memory_used = 0;
46 * tty_buffer_alloc - allocate a tty buffer
48 * @size: desired size (characters)
50 * Allocate a new tty buffer to hold the desired number of characters.
51 * Return NULL if out of memory or the allocation would exceed the
54 * Locking: Caller must hold tty->buf.lock
57 static struct tty_buffer *tty_buffer_alloc(struct tty_struct *tty, size_t size)
61 if (tty->buf.memory_used + size > 65536)
63 p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC);
71 p->char_buf_ptr = (char *)(p->data);
72 p->flag_buf_ptr = (unsigned char *)p->char_buf_ptr + size;
73 tty->buf.memory_used += size;
78 * tty_buffer_free - free a tty buffer
79 * @tty: tty owning the buffer
80 * @b: the buffer to free
82 * Free a tty buffer, or add it to the free list according to our
85 * Locking: Caller must hold tty->buf.lock
88 static void tty_buffer_free(struct tty_struct *tty, struct tty_buffer *b)
90 /* Dumb strategy for now - should keep some stats */
91 tty->buf.memory_used -= b->size;
92 WARN_ON(tty->buf.memory_used < 0);
97 b->next = tty->buf.free;
103 * __tty_buffer_flush - flush full tty buffers
106 * flush all the buffers containing receive data. Caller must
107 * hold the buffer lock and must have ensured no parallel flush to
110 * Locking: Caller must hold tty->buf.lock
113 static void __tty_buffer_flush(struct tty_struct *tty)
115 struct tty_buffer *thead;
117 while ((thead = tty->buf.head) != NULL) {
118 tty->buf.head = thead->next;
119 tty_buffer_free(tty, thead);
121 tty->buf.tail = NULL;
125 * tty_buffer_flush - flush full tty buffers
128 * flush all the buffers containing receive data. If the buffer is
129 * being processed by flush_to_ldisc then we defer the processing
135 void tty_buffer_flush(struct tty_struct *tty)
137 struct tty_port *port = tty->port;
139 spin_lock_irqsave(&tty->buf.lock, flags);
141 /* If the data is being pushed to the tty layer then we can't
142 process it here. Instead set a flag and the flush_to_ldisc
143 path will process the flush request before it exits */
144 if (test_bit(TTYP_FLUSHING, &port->iflags)) {
145 set_bit(TTYP_FLUSHPENDING, &port->iflags);
146 spin_unlock_irqrestore(&tty->buf.lock, flags);
147 wait_event(tty->read_wait,
148 test_bit(TTYP_FLUSHPENDING, &port->iflags) == 0);
151 __tty_buffer_flush(tty);
152 spin_unlock_irqrestore(&tty->buf.lock, flags);
156 * tty_buffer_find - find a free tty buffer
157 * @tty: tty owning the buffer
158 * @size: characters wanted
160 * Locate an existing suitable tty buffer or if we are lacking one then
161 * allocate a new one. We round our buffers off in 256 character chunks
162 * to get better allocation behaviour.
164 * Locking: Caller must hold tty->buf.lock
167 static struct tty_buffer *tty_buffer_find(struct tty_struct *tty, size_t size)
169 struct tty_buffer **tbh = &tty->buf.free;
170 while ((*tbh) != NULL) {
171 struct tty_buffer *t = *tbh;
172 if (t->size >= size) {
178 tty->buf.memory_used += t->size;
181 tbh = &((*tbh)->next);
183 /* Round the buffer size out */
184 size = (size + 0xFF) & ~0xFF;
185 return tty_buffer_alloc(tty, size);
186 /* Should possibly check if this fails for the largest buffer we
187 have queued and recycle that ? */
190 * __tty_buffer_request_room - grow tty buffer if needed
191 * @tty: tty structure
192 * @size: size desired
194 * Make at least size bytes of linear space available for the tty
195 * buffer. If we fail return the size we managed to find.
196 * Locking: Caller must hold tty->buf.lock
198 static int __tty_buffer_request_room(struct tty_struct *tty, size_t size)
200 struct tty_buffer *b, *n;
202 /* OPTIMISATION: We could keep a per tty "zero" sized buffer to
203 remove this conditional if its worth it. This would be invisible
205 if ((b = tty->buf.tail) != NULL)
206 left = b->size - b->used;
211 /* This is the slow path - looking for new buffers to use */
212 if ((n = tty_buffer_find(tty, size)) != NULL) {
228 * tty_buffer_request_room - grow tty buffer if needed
229 * @tty: tty structure
230 * @size: size desired
232 * Make at least size bytes of linear space available for the tty
233 * buffer. If we fail return the size we managed to find.
235 * Locking: Takes tty->buf.lock
237 int tty_buffer_request_room(struct tty_struct *tty, size_t size)
242 spin_lock_irqsave(&tty->buf.lock, flags);
243 length = __tty_buffer_request_room(tty, size);
244 spin_unlock_irqrestore(&tty->buf.lock, flags);
247 EXPORT_SYMBOL_GPL(tty_buffer_request_room);
250 * tty_insert_flip_string_fixed_flag - Add characters to the tty buffer
251 * @tty: tty structure
253 * @flag: flag value for each character
256 * Queue a series of bytes to the tty buffering. All the characters
257 * passed are marked with the supplied flag. Returns the number added.
259 * Locking: Called functions may take tty->buf.lock
262 int tty_insert_flip_string_fixed_flag(struct tty_struct *tty,
263 const unsigned char *chars, char flag, size_t size)
267 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
270 struct tty_buffer *tb;
272 spin_lock_irqsave(&tty->buf.lock, flags);
273 space = __tty_buffer_request_room(tty, goal);
275 /* If there is no space then tb may be NULL */
276 if (unlikely(space == 0)) {
277 spin_unlock_irqrestore(&tty->buf.lock, flags);
280 memcpy(tb->char_buf_ptr + tb->used, chars, space);
281 memset(tb->flag_buf_ptr + tb->used, flag, space);
283 spin_unlock_irqrestore(&tty->buf.lock, flags);
286 /* There is a small chance that we need to split the data over
287 several buffers. If this is the case we must loop */
288 } while (unlikely(size > copied));
291 EXPORT_SYMBOL(tty_insert_flip_string_fixed_flag);
294 * tty_insert_flip_string_flags - Add characters to the tty buffer
295 * @tty: tty structure
300 * Queue a series of bytes to the tty buffering. For each character
301 * the flags array indicates the status of the character. Returns the
304 * Locking: Called functions may take tty->buf.lock
307 int tty_insert_flip_string_flags(struct tty_struct *tty,
308 const unsigned char *chars, const char *flags, size_t size)
312 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
314 unsigned long __flags;
315 struct tty_buffer *tb;
317 spin_lock_irqsave(&tty->buf.lock, __flags);
318 space = __tty_buffer_request_room(tty, goal);
320 /* If there is no space then tb may be NULL */
321 if (unlikely(space == 0)) {
322 spin_unlock_irqrestore(&tty->buf.lock, __flags);
325 memcpy(tb->char_buf_ptr + tb->used, chars, space);
326 memcpy(tb->flag_buf_ptr + tb->used, flags, space);
328 spin_unlock_irqrestore(&tty->buf.lock, __flags);
332 /* There is a small chance that we need to split the data over
333 several buffers. If this is the case we must loop */
334 } while (unlikely(size > copied));
337 EXPORT_SYMBOL(tty_insert_flip_string_flags);
340 * tty_schedule_flip - push characters to ldisc
341 * @tty: tty to push from
343 * Takes any pending buffers and transfers their ownership to the
344 * ldisc side of the queue. It then schedules those characters for
345 * processing by the line discipline.
346 * Note that this function can only be used when the low_latency flag
347 * is unset. Otherwise the workqueue won't be flushed.
349 * Locking: Takes tty->buf.lock
352 void tty_schedule_flip(struct tty_struct *tty)
355 spin_lock_irqsave(&tty->buf.lock, flags);
356 if (tty->buf.tail != NULL)
357 tty->buf.tail->commit = tty->buf.tail->used;
358 spin_unlock_irqrestore(&tty->buf.lock, flags);
359 schedule_work(&tty->buf.work);
361 EXPORT_SYMBOL(tty_schedule_flip);
364 * tty_prepare_flip_string - make room for characters
366 * @chars: return pointer for character write area
367 * @size: desired size
369 * Prepare a block of space in the buffer for data. Returns the length
370 * available and buffer pointer to the space which is now allocated and
371 * accounted for as ready for normal characters. This is used for drivers
372 * that need their own block copy routines into the buffer. There is no
373 * guarantee the buffer is a DMA target!
375 * Locking: May call functions taking tty->buf.lock
378 int tty_prepare_flip_string(struct tty_struct *tty, unsigned char **chars,
383 struct tty_buffer *tb;
385 spin_lock_irqsave(&tty->buf.lock, flags);
386 space = __tty_buffer_request_room(tty, size);
390 *chars = tb->char_buf_ptr + tb->used;
391 memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
394 spin_unlock_irqrestore(&tty->buf.lock, flags);
397 EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
400 * tty_prepare_flip_string_flags - make room for characters
402 * @chars: return pointer for character write area
403 * @flags: return pointer for status flag write area
404 * @size: desired size
406 * Prepare a block of space in the buffer for data. Returns the length
407 * available and buffer pointer to the space which is now allocated and
408 * accounted for as ready for characters. This is used for drivers
409 * that need their own block copy routines into the buffer. There is no
410 * guarantee the buffer is a DMA target!
412 * Locking: May call functions taking tty->buf.lock
415 int tty_prepare_flip_string_flags(struct tty_struct *tty,
416 unsigned char **chars, char **flags, size_t size)
419 unsigned long __flags;
420 struct tty_buffer *tb;
422 spin_lock_irqsave(&tty->buf.lock, __flags);
423 space = __tty_buffer_request_room(tty, size);
427 *chars = tb->char_buf_ptr + tb->used;
428 *flags = tb->flag_buf_ptr + tb->used;
431 spin_unlock_irqrestore(&tty->buf.lock, __flags);
434 EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
440 * @work: tty structure passed from work queue.
442 * This routine is called out of the software interrupt to flush data
443 * from the buffer chain to the line discipline.
445 * Locking: holds tty->buf.lock to guard buffer list. Drops the lock
446 * while invoking the line discipline receive_buf method. The
447 * receive_buf method is single threaded for each tty instance.
450 static void flush_to_ldisc(struct work_struct *work)
452 struct tty_struct *tty =
453 container_of(work, struct tty_struct, buf.work);
454 struct tty_port *port = tty->port;
456 struct tty_ldisc *disc;
458 disc = tty_ldisc_ref(tty);
459 if (disc == NULL) /* !TTY_LDISC */
462 spin_lock_irqsave(&tty->buf.lock, flags);
464 if (!test_and_set_bit(TTYP_FLUSHING, &port->iflags)) {
465 struct tty_buffer *head;
466 while ((head = tty->buf.head) != NULL) {
469 unsigned char *flag_buf;
471 count = head->commit - head->read;
473 if (head->next == NULL)
475 tty->buf.head = head->next;
476 tty_buffer_free(tty, head);
479 /* Ldisc or user is trying to flush the buffers
480 we are feeding to the ldisc, stop feeding the
481 line discipline as we want to empty the queue */
482 if (test_bit(TTYP_FLUSHPENDING, &port->iflags))
484 if (!tty->receive_room)
486 if (count > tty->receive_room)
487 count = tty->receive_room;
488 char_buf = head->char_buf_ptr + head->read;
489 flag_buf = head->flag_buf_ptr + head->read;
491 spin_unlock_irqrestore(&tty->buf.lock, flags);
492 disc->ops->receive_buf(tty, char_buf,
494 spin_lock_irqsave(&tty->buf.lock, flags);
496 clear_bit(TTYP_FLUSHING, &port->iflags);
499 /* We may have a deferred request to flush the input buffer,
500 if so pull the chain under the lock and empty the queue */
501 if (test_bit(TTYP_FLUSHPENDING, &port->iflags)) {
502 __tty_buffer_flush(tty);
503 clear_bit(TTYP_FLUSHPENDING, &port->iflags);
504 wake_up(&tty->read_wait);
506 spin_unlock_irqrestore(&tty->buf.lock, flags);
508 tty_ldisc_deref(disc);
515 * Push the terminal flip buffers to the line discipline.
517 * Must not be called from IRQ context.
519 void tty_flush_to_ldisc(struct tty_struct *tty)
521 if (!tty->low_latency)
522 flush_work(&tty->buf.work);
526 * tty_flip_buffer_push - terminal
529 * Queue a push of the terminal flip buffers to the line discipline. This
530 * function must not be called from IRQ context if tty->low_latency is set.
532 * In the event of the queue being busy for flipping the work will be
533 * held off and retried later.
535 * Locking: tty buffer lock. Driver locks in low latency mode.
538 void tty_flip_buffer_push(struct tty_struct *tty)
541 spin_lock_irqsave(&tty->buf.lock, flags);
542 if (tty->buf.tail != NULL)
543 tty->buf.tail->commit = tty->buf.tail->used;
544 spin_unlock_irqrestore(&tty->buf.lock, flags);
546 if (tty->low_latency)
547 flush_to_ldisc(&tty->buf.work);
549 schedule_work(&tty->buf.work);
551 EXPORT_SYMBOL(tty_flip_buffer_push);
554 * tty_buffer_init - prepare a tty buffer structure
555 * @tty: tty to initialise
557 * Set up the initial state of the buffer management for a tty device.
558 * Must be called before the other tty buffer functions are used.
563 void tty_buffer_init(struct tty_struct *tty)
565 spin_lock_init(&tty->buf.lock);
566 tty->buf.head = NULL;
567 tty->buf.tail = NULL;
568 tty->buf.free = NULL;
569 tty->buf.memory_used = 0;
570 INIT_WORK(&tty->buf.work, flush_to_ldisc);