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>
19 #include <linux/ratelimit.h>
22 #define MIN_TTYB_SIZE 256
23 #define TTYB_ALIGN_MASK 255
25 static void tty_buffer_reset(struct tty_buffer *p, size_t size)
35 * tty_buffer_free_all - free buffers used by a tty
36 * @tty: tty to free from
38 * Remove all the buffers pending on a tty whether queued with data
39 * or in the free ring. Must be called when the tty is no longer in use
44 void tty_buffer_free_all(struct tty_port *port)
46 struct tty_bufhead *buf = &port->buf;
47 struct tty_buffer *thead;
49 while ((thead = buf->head) != NULL) {
50 buf->head = thead->next;
53 while ((thead = buf->free) != NULL) {
54 buf->free = thead->next;
62 * tty_buffer_alloc - allocate a tty buffer
64 * @size: desired size (characters)
66 * Allocate a new tty buffer to hold the desired number of characters.
67 * We round our buffers off in 256 character chunks to get better
68 * allocation behaviour.
69 * Return NULL if out of memory or the allocation would exceed the
72 * Locking: Caller must hold tty->buf.lock
75 static struct tty_buffer *tty_buffer_alloc(struct tty_port *port, size_t size)
77 struct tty_buffer **tbh = &port->buf.free;
80 /* Round the buffer size out */
81 size = __ALIGN_MASK(size, TTYB_ALIGN_MASK);
83 if (size <= MIN_TTYB_SIZE) {
91 /* Should possibly check if this fails for the largest buffer we
92 have queued and recycle that ? */
93 if (port->buf.memory_used + size > 65536)
95 p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC);
100 tty_buffer_reset(p, size);
101 port->buf.memory_used += size;
106 * tty_buffer_free - free a tty buffer
107 * @tty: tty owning the buffer
108 * @b: the buffer to free
110 * Free a tty buffer, or add it to the free list according to our
113 * Locking: Caller must hold tty->buf.lock
116 static void tty_buffer_free(struct tty_port *port, struct tty_buffer *b)
118 struct tty_bufhead *buf = &port->buf;
120 /* Dumb strategy for now - should keep some stats */
121 buf->memory_used -= b->size;
122 WARN_ON(buf->memory_used < 0);
124 if (b->size > MIN_TTYB_SIZE)
133 * __tty_buffer_flush - flush full tty buffers
136 * flush all the buffers containing receive data. Caller must
137 * hold the buffer lock and must have ensured no parallel flush to
140 * Locking: Caller must hold tty->buf.lock
143 static void __tty_buffer_flush(struct tty_port *port)
145 struct tty_bufhead *buf = &port->buf;
146 struct tty_buffer *thead;
148 if (unlikely(buf->head == NULL))
150 while ((thead = buf->head->next) != NULL) {
151 tty_buffer_free(port, buf->head);
154 WARN_ON(buf->head != buf->tail);
155 buf->head->read = buf->head->commit;
159 * tty_buffer_flush - flush full tty buffers
162 * flush all the buffers containing receive data. If the buffer is
163 * being processed by flush_to_ldisc then we defer the processing
169 void tty_buffer_flush(struct tty_struct *tty)
171 struct tty_port *port = tty->port;
172 struct tty_bufhead *buf = &port->buf;
175 spin_lock_irqsave(&buf->lock, flags);
177 /* If the data is being pushed to the tty layer then we can't
178 process it here. Instead set a flag and the flush_to_ldisc
179 path will process the flush request before it exits */
180 if (test_bit(TTYP_FLUSHING, &port->iflags)) {
181 set_bit(TTYP_FLUSHPENDING, &port->iflags);
182 spin_unlock_irqrestore(&buf->lock, flags);
183 wait_event(tty->read_wait,
184 test_bit(TTYP_FLUSHPENDING, &port->iflags) == 0);
187 __tty_buffer_flush(port);
188 spin_unlock_irqrestore(&buf->lock, flags);
192 * tty_buffer_request_room - grow tty buffer if needed
193 * @tty: tty structure
194 * @size: size desired
196 * Make at least size bytes of linear space available for the tty
197 * buffer. If we fail return the size we managed to find.
199 * Locking: Takes port->buf.lock
201 int tty_buffer_request_room(struct tty_port *port, size_t size)
203 struct tty_bufhead *buf = &port->buf;
204 struct tty_buffer *b, *n;
207 spin_lock_irqsave(&buf->lock, flags);
208 /* OPTIMISATION: We could keep a per tty "zero" sized buffer to
209 remove this conditional if its worth it. This would be invisible
213 left = b->size - b->used;
218 /* This is the slow path - looking for new buffers to use */
219 if ((n = tty_buffer_alloc(port, size)) != NULL) {
229 spin_unlock_irqrestore(&buf->lock, flags);
232 EXPORT_SYMBOL_GPL(tty_buffer_request_room);
235 * tty_insert_flip_string_fixed_flag - Add characters to the tty buffer
238 * @flag: flag value for each character
241 * Queue a series of bytes to the tty buffering. All the characters
242 * passed are marked with the supplied flag. Returns the number added.
244 * Locking: Called functions may take port->buf.lock
247 int tty_insert_flip_string_fixed_flag(struct tty_port *port,
248 const unsigned char *chars, char flag, size_t size)
252 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
253 int space = tty_buffer_request_room(port, goal);
254 struct tty_buffer *tb = port->buf.tail;
255 /* If there is no space then tb may be NULL */
256 if (unlikely(space == 0)) {
259 memcpy(char_buf_ptr(tb, tb->used), chars, space);
260 memset(flag_buf_ptr(tb, tb->used), flag, space);
264 /* There is a small chance that we need to split the data over
265 several buffers. If this is the case we must loop */
266 } while (unlikely(size > copied));
269 EXPORT_SYMBOL(tty_insert_flip_string_fixed_flag);
272 * tty_insert_flip_string_flags - Add characters to the tty buffer
278 * Queue a series of bytes to the tty buffering. For each character
279 * the flags array indicates the status of the character. Returns the
282 * Locking: Called functions may take port->buf.lock
285 int tty_insert_flip_string_flags(struct tty_port *port,
286 const unsigned char *chars, const char *flags, size_t size)
290 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
291 int space = tty_buffer_request_room(port, goal);
292 struct tty_buffer *tb = port->buf.tail;
293 /* If there is no space then tb may be NULL */
294 if (unlikely(space == 0)) {
297 memcpy(char_buf_ptr(tb, tb->used), chars, space);
298 memcpy(flag_buf_ptr(tb, tb->used), flags, space);
303 /* There is a small chance that we need to split the data over
304 several buffers. If this is the case we must loop */
305 } while (unlikely(size > copied));
308 EXPORT_SYMBOL(tty_insert_flip_string_flags);
311 * tty_schedule_flip - push characters to ldisc
312 * @port: tty port to push from
314 * Takes any pending buffers and transfers their ownership to the
315 * ldisc side of the queue. It then schedules those characters for
316 * processing by the line discipline.
317 * Note that this function can only be used when the low_latency flag
318 * is unset. Otherwise the workqueue won't be flushed.
320 * Locking: Takes port->buf.lock
323 void tty_schedule_flip(struct tty_port *port)
325 struct tty_bufhead *buf = &port->buf;
327 WARN_ON(port->low_latency);
329 spin_lock_irqsave(&buf->lock, flags);
330 if (buf->tail != NULL)
331 buf->tail->commit = buf->tail->used;
332 spin_unlock_irqrestore(&buf->lock, flags);
333 schedule_work(&buf->work);
335 EXPORT_SYMBOL(tty_schedule_flip);
338 * tty_prepare_flip_string - make room for characters
340 * @chars: return pointer for character write area
341 * @size: desired size
343 * Prepare a block of space in the buffer for data. Returns the length
344 * available and buffer pointer to the space which is now allocated and
345 * accounted for as ready for normal characters. This is used for drivers
346 * that need their own block copy routines into the buffer. There is no
347 * guarantee the buffer is a DMA target!
349 * Locking: May call functions taking port->buf.lock
352 int tty_prepare_flip_string(struct tty_port *port, unsigned char **chars,
355 int space = tty_buffer_request_room(port, size);
357 struct tty_buffer *tb = port->buf.tail;
358 *chars = char_buf_ptr(tb, tb->used);
359 memset(flag_buf_ptr(tb, tb->used), TTY_NORMAL, space);
364 EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
367 * tty_prepare_flip_string_flags - make room for characters
369 * @chars: return pointer for character write area
370 * @flags: return pointer for status flag write area
371 * @size: desired size
373 * Prepare a block of space in the buffer for data. Returns the length
374 * available and buffer pointer to the space which is now allocated and
375 * accounted for as ready for characters. This is used for drivers
376 * that need their own block copy routines into the buffer. There is no
377 * guarantee the buffer is a DMA target!
379 * Locking: May call functions taking port->buf.lock
382 int tty_prepare_flip_string_flags(struct tty_port *port,
383 unsigned char **chars, char **flags, size_t size)
385 int space = tty_buffer_request_room(port, size);
387 struct tty_buffer *tb = port->buf.tail;
388 *chars = char_buf_ptr(tb, tb->used);
389 *flags = flag_buf_ptr(tb, tb->used);
394 EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
398 receive_buf(struct tty_struct *tty, struct tty_buffer *head, int count)
400 struct tty_ldisc *disc = tty->ldisc;
401 unsigned char *p = char_buf_ptr(head, head->read);
402 char *f = flag_buf_ptr(head, head->read);
404 if (disc->ops->receive_buf2)
405 count = disc->ops->receive_buf2(tty, p, f, count);
407 count = min_t(int, count, tty->receive_room);
409 disc->ops->receive_buf(tty, p, f, count);
417 * @work: tty structure passed from work queue.
419 * This routine is called out of the software interrupt to flush data
420 * from the buffer chain to the line discipline.
422 * Locking: holds tty->buf.lock to guard buffer list. Drops the lock
423 * while invoking the line discipline receive_buf method. The
424 * receive_buf method is single threaded for each tty instance.
427 static void flush_to_ldisc(struct work_struct *work)
429 struct tty_port *port = container_of(work, struct tty_port, buf.work);
430 struct tty_bufhead *buf = &port->buf;
431 struct tty_struct *tty;
433 struct tty_ldisc *disc;
439 disc = tty_ldisc_ref(tty);
443 spin_lock_irqsave(&buf->lock, flags);
445 if (!test_and_set_bit(TTYP_FLUSHING, &port->iflags)) {
446 struct tty_buffer *head;
447 while ((head = buf->head) != NULL) {
450 count = head->commit - head->read;
452 if (head->next == NULL)
454 buf->head = head->next;
455 tty_buffer_free(port, head);
458 spin_unlock_irqrestore(&buf->lock, flags);
460 count = receive_buf(tty, head, count);
462 spin_lock_irqsave(&buf->lock, flags);
463 /* Ldisc or user is trying to flush the buffers.
464 We may have a deferred request to flush the
465 input buffer, if so pull the chain under the lock
466 and empty the queue */
467 if (test_bit(TTYP_FLUSHPENDING, &port->iflags)) {
468 __tty_buffer_flush(port);
469 clear_bit(TTYP_FLUSHPENDING, &port->iflags);
470 wake_up(&tty->read_wait);
475 clear_bit(TTYP_FLUSHING, &port->iflags);
478 spin_unlock_irqrestore(&buf->lock, flags);
480 tty_ldisc_deref(disc);
487 * Push the terminal flip buffers to the line discipline.
489 * Must not be called from IRQ context.
491 void tty_flush_to_ldisc(struct tty_struct *tty)
493 if (!tty->port->low_latency)
494 flush_work(&tty->port->buf.work);
498 * tty_flip_buffer_push - terminal
499 * @port: tty port to push
501 * Queue a push of the terminal flip buffers to the line discipline. This
502 * function must not be called from IRQ context if port->low_latency is
505 * In the event of the queue being busy for flipping the work will be
506 * held off and retried later.
508 * Locking: tty buffer lock. Driver locks in low latency mode.
511 void tty_flip_buffer_push(struct tty_port *port)
513 struct tty_bufhead *buf = &port->buf;
516 spin_lock_irqsave(&buf->lock, flags);
517 if (buf->tail != NULL)
518 buf->tail->commit = buf->tail->used;
519 spin_unlock_irqrestore(&buf->lock, flags);
521 if (port->low_latency)
522 flush_to_ldisc(&buf->work);
524 schedule_work(&buf->work);
526 EXPORT_SYMBOL(tty_flip_buffer_push);
529 * tty_buffer_init - prepare a tty buffer structure
530 * @tty: tty to initialise
532 * Set up the initial state of the buffer management for a tty device.
533 * Must be called before the other tty buffer functions are used.
538 void tty_buffer_init(struct tty_port *port)
540 struct tty_bufhead *buf = &port->buf;
542 spin_lock_init(&buf->lock);
546 buf->memory_used = 0;
547 INIT_WORK(&buf->work, flush_to_ldisc);