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 * tty_buffer_free_all - free buffers used by a tty
23 * @tty: tty to free from
25 * Remove all the buffers pending on a tty whether queued with data
26 * or in the free ring. Must be called when the tty is no longer in use
31 void tty_buffer_free_all(struct tty_port *port)
33 struct tty_bufhead *buf = &port->buf;
34 struct tty_buffer *thead;
36 while ((thead = buf->head) != NULL) {
37 buf->head = thead->next;
40 while ((thead = buf->free) != NULL) {
41 buf->free = thead->next;
49 * tty_buffer_alloc - allocate a tty buffer
51 * @size: desired size (characters)
53 * Allocate a new tty buffer to hold the desired number of characters.
54 * Return NULL if out of memory or the allocation would exceed the
57 * Locking: Caller must hold tty->buf.lock
60 static struct tty_buffer *tty_buffer_alloc(struct tty_port *port, size_t size)
64 if (port->buf.memory_used + size > 65536)
66 p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC);
74 p->char_buf_ptr = (char *)(p->data);
75 p->flag_buf_ptr = (unsigned char *)p->char_buf_ptr + size;
76 port->buf.memory_used += size;
81 * tty_buffer_free - free a tty buffer
82 * @tty: tty owning the buffer
83 * @b: the buffer to free
85 * Free a tty buffer, or add it to the free list according to our
88 * Locking: Caller must hold tty->buf.lock
91 static void tty_buffer_free(struct tty_port *port, struct tty_buffer *b)
93 struct tty_bufhead *buf = &port->buf;
95 /* Dumb strategy for now - should keep some stats */
96 buf->memory_used -= b->size;
97 WARN_ON(buf->memory_used < 0);
108 * __tty_buffer_flush - flush full tty buffers
111 * flush all the buffers containing receive data. Caller must
112 * hold the buffer lock and must have ensured no parallel flush to
115 * Locking: Caller must hold tty->buf.lock
118 static void __tty_buffer_flush(struct tty_port *port)
120 struct tty_bufhead *buf = &port->buf;
121 struct tty_buffer *thead;
123 if (unlikely(buf->head == NULL))
125 while ((thead = buf->head->next) != NULL) {
126 tty_buffer_free(port, buf->head);
129 WARN_ON(buf->head != buf->tail);
130 buf->head->read = buf->head->commit;
134 * tty_buffer_flush - flush full tty buffers
137 * flush all the buffers containing receive data. If the buffer is
138 * being processed by flush_to_ldisc then we defer the processing
144 void tty_buffer_flush(struct tty_struct *tty)
146 struct tty_port *port = tty->port;
147 struct tty_bufhead *buf = &port->buf;
150 spin_lock_irqsave(&buf->lock, flags);
152 /* If the data is being pushed to the tty layer then we can't
153 process it here. Instead set a flag and the flush_to_ldisc
154 path will process the flush request before it exits */
155 if (test_bit(TTYP_FLUSHING, &port->iflags)) {
156 set_bit(TTYP_FLUSHPENDING, &port->iflags);
157 spin_unlock_irqrestore(&buf->lock, flags);
158 wait_event(tty->read_wait,
159 test_bit(TTYP_FLUSHPENDING, &port->iflags) == 0);
162 __tty_buffer_flush(port);
163 spin_unlock_irqrestore(&buf->lock, flags);
167 * tty_buffer_find - find a free tty buffer
168 * @tty: tty owning the buffer
169 * @size: characters wanted
171 * Locate an existing suitable tty buffer or if we are lacking one then
172 * allocate a new one. We round our buffers off in 256 character chunks
173 * to get better allocation behaviour.
175 * Locking: Caller must hold tty->buf.lock
178 static struct tty_buffer *tty_buffer_find(struct tty_port *port, size_t size)
180 struct tty_buffer **tbh = &port->buf.free;
181 while ((*tbh) != NULL) {
182 struct tty_buffer *t = *tbh;
183 if (t->size >= size) {
189 port->buf.memory_used += t->size;
192 tbh = &((*tbh)->next);
194 /* Round the buffer size out */
195 size = (size + 0xFF) & ~0xFF;
196 return tty_buffer_alloc(port, size);
197 /* Should possibly check if this fails for the largest buffer we
198 have queued and recycle that ? */
201 * tty_buffer_request_room - grow tty buffer if needed
202 * @tty: tty structure
203 * @size: size desired
205 * Make at least size bytes of linear space available for the tty
206 * buffer. If we fail return the size we managed to find.
208 * Locking: Takes port->buf.lock
210 int tty_buffer_request_room(struct tty_port *port, size_t size)
212 struct tty_bufhead *buf = &port->buf;
213 struct tty_buffer *b, *n;
216 spin_lock_irqsave(&buf->lock, flags);
217 /* OPTIMISATION: We could keep a per tty "zero" sized buffer to
218 remove this conditional if its worth it. This would be invisible
222 left = b->size - b->used;
227 /* This is the slow path - looking for new buffers to use */
228 if ((n = tty_buffer_find(port, size)) != NULL) {
238 spin_unlock_irqrestore(&buf->lock, flags);
241 EXPORT_SYMBOL_GPL(tty_buffer_request_room);
244 * tty_insert_flip_string_fixed_flag - Add characters to the tty buffer
247 * @flag: flag value for each character
250 * Queue a series of bytes to the tty buffering. All the characters
251 * passed are marked with the supplied flag. Returns the number added.
253 * Locking: Called functions may take port->buf.lock
256 int tty_insert_flip_string_fixed_flag(struct tty_port *port,
257 const unsigned char *chars, char flag, size_t size)
261 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
262 int space = tty_buffer_request_room(port, goal);
263 struct tty_buffer *tb = port->buf.tail;
264 /* If there is no space then tb may be NULL */
265 if (unlikely(space == 0)) {
268 memcpy(tb->char_buf_ptr + tb->used, chars, space);
269 memset(tb->flag_buf_ptr + tb->used, flag, space);
273 /* There is a small chance that we need to split the data over
274 several buffers. If this is the case we must loop */
275 } while (unlikely(size > copied));
278 EXPORT_SYMBOL(tty_insert_flip_string_fixed_flag);
281 * tty_insert_flip_string_flags - Add characters to the tty buffer
287 * Queue a series of bytes to the tty buffering. For each character
288 * the flags array indicates the status of the character. Returns the
291 * Locking: Called functions may take port->buf.lock
294 int tty_insert_flip_string_flags(struct tty_port *port,
295 const unsigned char *chars, const char *flags, size_t size)
299 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
300 int space = tty_buffer_request_room(port, goal);
301 struct tty_buffer *tb = port->buf.tail;
302 /* If there is no space then tb may be NULL */
303 if (unlikely(space == 0)) {
306 memcpy(tb->char_buf_ptr + tb->used, chars, space);
307 memcpy(tb->flag_buf_ptr + tb->used, flags, space);
312 /* There is a small chance that we need to split the data over
313 several buffers. If this is the case we must loop */
314 } while (unlikely(size > copied));
317 EXPORT_SYMBOL(tty_insert_flip_string_flags);
320 * tty_schedule_flip - push characters to ldisc
321 * @port: tty port to push from
323 * Takes any pending buffers and transfers their ownership to the
324 * ldisc side of the queue. It then schedules those characters for
325 * processing by the line discipline.
326 * Note that this function can only be used when the low_latency flag
327 * is unset. Otherwise the workqueue won't be flushed.
329 * Locking: Takes port->buf.lock
332 void tty_schedule_flip(struct tty_port *port)
334 struct tty_bufhead *buf = &port->buf;
336 WARN_ON(port->low_latency);
338 spin_lock_irqsave(&buf->lock, flags);
339 if (buf->tail != NULL)
340 buf->tail->commit = buf->tail->used;
341 spin_unlock_irqrestore(&buf->lock, flags);
342 schedule_work(&buf->work);
344 EXPORT_SYMBOL(tty_schedule_flip);
347 * tty_prepare_flip_string - make room for characters
349 * @chars: return pointer for character write area
350 * @size: desired size
352 * Prepare a block of space in the buffer for data. Returns the length
353 * available and buffer pointer to the space which is now allocated and
354 * accounted for as ready for normal characters. This is used for drivers
355 * that need their own block copy routines into the buffer. There is no
356 * guarantee the buffer is a DMA target!
358 * Locking: May call functions taking port->buf.lock
361 int tty_prepare_flip_string(struct tty_port *port, unsigned char **chars,
364 int space = tty_buffer_request_room(port, size);
366 struct tty_buffer *tb = port->buf.tail;
367 *chars = tb->char_buf_ptr + tb->used;
368 memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
373 EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
376 * tty_prepare_flip_string_flags - make room for characters
378 * @chars: return pointer for character write area
379 * @flags: return pointer for status flag write area
380 * @size: desired size
382 * Prepare a block of space in the buffer for data. Returns the length
383 * available and buffer pointer to the space which is now allocated and
384 * accounted for as ready for characters. This is used for drivers
385 * that need their own block copy routines into the buffer. There is no
386 * guarantee the buffer is a DMA target!
388 * Locking: May call functions taking port->buf.lock
391 int tty_prepare_flip_string_flags(struct tty_port *port,
392 unsigned char **chars, char **flags, size_t size)
394 int space = tty_buffer_request_room(port, size);
396 struct tty_buffer *tb = port->buf.tail;
397 *chars = tb->char_buf_ptr + tb->used;
398 *flags = tb->flag_buf_ptr + tb->used;
403 EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
409 * @work: tty structure passed from work queue.
411 * This routine is called out of the software interrupt to flush data
412 * from the buffer chain to the line discipline.
414 * Locking: holds tty->buf.lock to guard buffer list. Drops the lock
415 * while invoking the line discipline receive_buf method. The
416 * receive_buf method is single threaded for each tty instance.
419 static void flush_to_ldisc(struct work_struct *work)
421 struct tty_port *port = container_of(work, struct tty_port, buf.work);
422 struct tty_bufhead *buf = &port->buf;
423 struct tty_struct *tty;
425 struct tty_ldisc *disc;
431 disc = tty_ldisc_ref(tty);
432 if (disc == NULL) /* !TTY_LDISC */
435 spin_lock_irqsave(&buf->lock, flags);
437 if (!test_and_set_bit(TTYP_FLUSHING, &port->iflags)) {
438 struct tty_buffer *head;
439 while ((head = buf->head) != NULL) {
442 unsigned char *flag_buf;
444 count = head->commit - head->read;
446 if (head->next == NULL)
448 buf->head = head->next;
449 tty_buffer_free(port, head);
452 if (!tty->receive_room)
454 if (count > tty->receive_room)
455 count = tty->receive_room;
456 char_buf = head->char_buf_ptr + head->read;
457 flag_buf = head->flag_buf_ptr + head->read;
459 spin_unlock_irqrestore(&buf->lock, flags);
460 disc->ops->receive_buf(tty, char_buf,
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);
474 clear_bit(TTYP_FLUSHING, &port->iflags);
477 spin_unlock_irqrestore(&buf->lock, flags);
479 tty_ldisc_deref(disc);
486 * Push the terminal flip buffers to the line discipline.
488 * Must not be called from IRQ context.
490 void tty_flush_to_ldisc(struct tty_struct *tty)
492 if (!tty->port->low_latency)
493 flush_work(&tty->port->buf.work);
497 * tty_flip_buffer_push - terminal
498 * @port: tty port to push
500 * Queue a push of the terminal flip buffers to the line discipline. This
501 * function must not be called from IRQ context if port->low_latency is
504 * In the event of the queue being busy for flipping the work will be
505 * held off and retried later.
507 * Locking: tty buffer lock. Driver locks in low latency mode.
510 void tty_flip_buffer_push(struct tty_port *port)
512 struct tty_bufhead *buf = &port->buf;
515 spin_lock_irqsave(&buf->lock, flags);
516 if (buf->tail != NULL)
517 buf->tail->commit = buf->tail->used;
518 spin_unlock_irqrestore(&buf->lock, flags);
520 if (port->low_latency)
521 flush_to_ldisc(&buf->work);
523 schedule_work(&buf->work);
525 EXPORT_SYMBOL(tty_flip_buffer_push);
528 * tty_buffer_init - prepare a tty buffer structure
529 * @tty: tty to initialise
531 * Set up the initial state of the buffer management for a tty device.
532 * Must be called before the other tty buffer functions are used.
537 void tty_buffer_init(struct tty_port *port)
539 struct tty_bufhead *buf = &port->buf;
541 spin_lock_init(&buf->lock);
545 buf->memory_used = 0;
546 INIT_WORK(&buf->work, flush_to_ldisc);