Lines Matching +full:buffer +full:- +full:size
1 // SPDX-License-Identifier: GPL-2.0
3 * Tty buffer allocation management
33 * We default to dicing tty buffer allocations to this many characters
34 * in order to avoid multiple page allocations. We know the size of
36 * buffer is 256 byte aligned. See tty_buffer_find for the allocation
40 #define TTY_BUFFER_PAGE (((PAGE_SIZE - sizeof(struct tty_buffer)) / 2) & ~TTYB_ALIGN_MASK)
43 * tty_buffer_lock_exclusive - gain exclusive access to buffer
44 * @port: tty port owning the flip buffer
47 * the buffer work and any pending flush from using the flip buffer. Data can
48 * continue to be added concurrently to the flip buffer from the driver side.
54 struct tty_bufhead *buf = &port->buf; in tty_buffer_lock_exclusive()
56 atomic_inc(&buf->priority); in tty_buffer_lock_exclusive()
57 mutex_lock(&buf->lock); in tty_buffer_lock_exclusive()
62 * tty_buffer_unlock_exclusive - release exclusive access
63 * @port: tty port owning the flip buffer
65 * The buffer work is restarted if there is data in the flip buffer.
71 struct tty_bufhead *buf = &port->buf; in tty_buffer_unlock_exclusive()
72 bool restart = buf->head->commit != buf->head->read; in tty_buffer_unlock_exclusive()
74 atomic_dec(&buf->priority); in tty_buffer_unlock_exclusive()
75 mutex_unlock(&buf->lock); in tty_buffer_unlock_exclusive()
78 queue_work(system_unbound_wq, &buf->work); in tty_buffer_unlock_exclusive()
83 * tty_buffer_space_avail - return unused buffer space
84 * @port: tty port owning the flip buffer
87 * the buffer limit.
90 * # of bytes (use tty_prepare_flip_string() to pre-allocate if memory
95 int space = port->buf.mem_limit - atomic_read(&port->buf.mem_used); in tty_buffer_space_avail()
101 static void tty_buffer_reset(struct tty_buffer *p, size_t size) in tty_buffer_reset() argument
103 p->used = 0; in tty_buffer_reset()
104 p->size = size; in tty_buffer_reset()
105 p->next = NULL; in tty_buffer_reset()
106 p->commit = 0; in tty_buffer_reset()
107 p->lookahead = 0; in tty_buffer_reset()
108 p->read = 0; in tty_buffer_reset()
109 p->flags = true; in tty_buffer_reset()
113 * tty_buffer_free_all - free buffers used by a tty
121 struct tty_bufhead *buf = &port->buf; in tty_buffer_free_all()
127 while ((p = buf->head) != NULL) { in tty_buffer_free_all()
128 buf->head = p->next; in tty_buffer_free_all()
129 freed += p->size; in tty_buffer_free_all()
130 if (p->size > 0) in tty_buffer_free_all()
133 llist = llist_del_all(&buf->free); in tty_buffer_free_all()
137 tty_buffer_reset(&buf->sentinel, 0); in tty_buffer_free_all()
138 buf->head = &buf->sentinel; in tty_buffer_free_all()
139 buf->tail = &buf->sentinel; in tty_buffer_free_all()
141 still_used = atomic_xchg(&buf->mem_used, 0); in tty_buffer_free_all()
143 still_used - freed); in tty_buffer_free_all()
147 * tty_buffer_alloc - allocate a tty buffer
149 * @size: desired size (characters)
151 * Allocate a new tty buffer to hold the desired number of characters. We
158 static struct tty_buffer *tty_buffer_alloc(struct tty_port *port, size_t size) in tty_buffer_alloc() argument
163 /* Round the buffer size out */ in tty_buffer_alloc()
164 size = __ALIGN_MASK(size, TTYB_ALIGN_MASK); in tty_buffer_alloc()
166 if (size <= MIN_TTYB_SIZE) { in tty_buffer_alloc()
167 free = llist_del_first(&port->buf.free); in tty_buffer_alloc()
174 /* Should possibly check if this fails for the largest buffer we in tty_buffer_alloc()
177 if (atomic_read(&port->buf.mem_used) > port->buf.mem_limit) in tty_buffer_alloc()
179 p = kmalloc(struct_size(p, data, 2 * size), GFP_ATOMIC | __GFP_NOWARN); in tty_buffer_alloc()
184 tty_buffer_reset(p, size); in tty_buffer_alloc()
185 atomic_add(size, &port->buf.mem_used); in tty_buffer_alloc()
190 * tty_buffer_free - free a tty buffer
191 * @port: tty port owning the buffer
192 * @b: the buffer to free
194 * Free a tty buffer, or add it to the free list according to our internal
199 struct tty_bufhead *buf = &port->buf; in tty_buffer_free()
201 /* Dumb strategy for now - should keep some stats */ in tty_buffer_free()
202 WARN_ON(atomic_sub_return(b->size, &buf->mem_used) < 0); in tty_buffer_free()
204 if (b->size > MIN_TTYB_SIZE) in tty_buffer_free()
206 else if (b->size > 0) in tty_buffer_free()
207 llist_add(&b->free, &buf->free); in tty_buffer_free()
211 * tty_buffer_flush - flush full tty buffers
216 * ldisc input buffer.
218 * Locking: takes buffer lock to ensure single-threaded flip buffer 'consumer'.
222 struct tty_port *port = tty->port; in tty_buffer_flush()
223 struct tty_bufhead *buf = &port->buf; in tty_buffer_flush()
226 atomic_inc(&buf->priority); in tty_buffer_flush()
228 mutex_lock(&buf->lock); in tty_buffer_flush()
230 * no pending memory accesses to the freed buffer in tty_buffer_flush()
232 while ((next = smp_load_acquire(&buf->head->next)) != NULL) { in tty_buffer_flush()
233 tty_buffer_free(port, buf->head); in tty_buffer_flush()
234 buf->head = next; in tty_buffer_flush()
236 buf->head->read = buf->head->commit; in tty_buffer_flush()
237 buf->head->lookahead = buf->head->read; in tty_buffer_flush()
239 if (ld && ld->ops->flush_buffer) in tty_buffer_flush()
240 ld->ops->flush_buffer(tty); in tty_buffer_flush()
242 atomic_dec(&buf->priority); in tty_buffer_flush()
243 mutex_unlock(&buf->lock); in tty_buffer_flush()
247 * __tty_buffer_request_room - grow tty buffer if needed
249 * @size: size desired
250 * @flags: buffer has to store flags along character data
252 * Make at least @size bytes of linear space available for the tty buffer.
254 * Will change over to a new buffer if the current buffer is encoded as
255 * %TTY_NORMAL (so has no flags buffer) and the new buffer requires a flags
256 * buffer.
258 * Returns: the size we managed to find.
260 static int __tty_buffer_request_room(struct tty_port *port, size_t size, in __tty_buffer_request_room() argument
263 struct tty_bufhead *buf = &port->buf; in __tty_buffer_request_room()
264 struct tty_buffer *n, *b = buf->tail; in __tty_buffer_request_room()
265 size_t left = (b->flags ? 1 : 2) * b->size - b->used; in __tty_buffer_request_room()
266 bool change = !b->flags && flags; in __tty_buffer_request_room()
268 if (!change && left >= size) in __tty_buffer_request_room()
269 return size; in __tty_buffer_request_room()
271 /* This is the slow path - looking for new buffers to use */ in __tty_buffer_request_room()
272 n = tty_buffer_alloc(port, size); in __tty_buffer_request_room()
276 n->flags = flags; in __tty_buffer_request_room()
277 buf->tail = n; in __tty_buffer_request_room()
280 * ensures they see all buffer data. in __tty_buffer_request_room()
282 smp_store_release(&b->commit, b->used); in __tty_buffer_request_room()
286 * is advanced to the next buffer. in __tty_buffer_request_room()
288 smp_store_release(&b->next, n); in __tty_buffer_request_room()
290 return size; in __tty_buffer_request_room()
293 int tty_buffer_request_room(struct tty_port *port, size_t size) in tty_buffer_request_room() argument
295 return __tty_buffer_request_room(port, size, true); in tty_buffer_request_room()
301 size_t size) in __tty_insert_flip_string_flags() argument
307 size_t goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE); in __tty_insert_flip_string_flags()
309 struct tty_buffer *tb = port->buf.tail; in __tty_insert_flip_string_flags()
314 memcpy(char_buf_ptr(tb, tb->used), chars, space); in __tty_insert_flip_string_flags()
317 memcpy(flag_buf_ptr(tb, tb->used), flags, space); in __tty_insert_flip_string_flags()
319 } else if (tb->flags) { in __tty_insert_flip_string_flags()
320 memset(flag_buf_ptr(tb, tb->used), flags[0], space); in __tty_insert_flip_string_flags()
322 /* tb->flags should be available once requested */ in __tty_insert_flip_string_flags()
326 tb->used += space; in __tty_insert_flip_string_flags()
333 } while (unlikely(size > copied)); in __tty_insert_flip_string_flags()
340 * tty_prepare_flip_string - make room for characters
343 * @size: desired size
345 * Prepare a block of space in the buffer for data.
348 * buffer. There is no guarantee the buffer is a DMA target!
350 * Returns: the length available and buffer pointer (@chars) to the space which
353 size_t tty_prepare_flip_string(struct tty_port *port, u8 **chars, size_t size) in tty_prepare_flip_string() argument
355 size_t space = __tty_buffer_request_room(port, size, false); in tty_prepare_flip_string()
358 struct tty_buffer *tb = port->buf.tail; in tty_prepare_flip_string()
360 *chars = char_buf_ptr(tb, tb->used); in tty_prepare_flip_string()
361 if (tb->flags) in tty_prepare_flip_string()
362 memset(flag_buf_ptr(tb, tb->used), TTY_NORMAL, space); in tty_prepare_flip_string()
363 tb->used += space; in tty_prepare_flip_string()
371 * tty_ldisc_receive_buf - forward data to line discipline
373 * @p: char buffer
374 * @f: %TTY_NORMAL, %TTY_BREAK, etc. flags buffer
385 if (ld->ops->receive_buf2) in tty_ldisc_receive_buf()
386 count = ld->ops->receive_buf2(ld->tty, p, f, count); in tty_ldisc_receive_buf()
388 count = min_t(size_t, count, ld->tty->receive_room); in tty_ldisc_receive_buf()
389 if (count && ld->ops->receive_buf) in tty_ldisc_receive_buf()
390 ld->ops->receive_buf(ld->tty, p, f, count); in tty_ldisc_receive_buf()
398 head->lookahead = max(head->lookahead, head->read); in lookahead_bufs()
407 * is advancing to the next buffer. in lookahead_bufs()
409 next = smp_load_acquire(&head->next); in lookahead_bufs()
412 * tty_buffer_flush(); ensures we see the committed buffer data. in lookahead_bufs()
414 count = smp_load_acquire(&head->commit) - head->lookahead; in lookahead_bufs()
420 if (port->client_ops->lookahead_buf) { in lookahead_bufs()
423 p = char_buf_ptr(head, head->lookahead); in lookahead_bufs()
424 if (head->flags) in lookahead_bufs()
425 f = flag_buf_ptr(head, head->lookahead); in lookahead_bufs()
427 port->client_ops->lookahead_buf(port, p, f, count); in lookahead_bufs()
430 head->lookahead += count; in lookahead_bufs()
437 u8 *p = char_buf_ptr(head, head->read); in receive_buf()
441 if (head->flags) in receive_buf()
442 f = flag_buf_ptr(head, head->read); in receive_buf()
444 n = port->client_ops->receive_buf(port, p, f, count); in receive_buf()
451 * flush_to_ldisc - flush data from buffer to ldisc
455 * buffer chain to the line discipline.
459 * Locking: takes buffer lock to ensure single-threaded flip buffer 'consumer'.
464 struct tty_bufhead *buf = &port->buf; in flush_to_ldisc()
466 mutex_lock(&buf->lock); in flush_to_ldisc()
469 struct tty_buffer *head = buf->head; in flush_to_ldisc()
474 if (atomic_read(&buf->priority)) in flush_to_ldisc()
479 * is advancing to the next buffer in flush_to_ldisc()
481 next = smp_load_acquire(&head->next); in flush_to_ldisc()
483 * tty_buffer_flush(); ensures we see the committed buffer data in flush_to_ldisc()
485 count = smp_load_acquire(&head->commit) - head->read; in flush_to_ldisc()
489 buf->head = next; in flush_to_ldisc()
495 head->read += rcvd; in flush_to_ldisc()
505 mutex_unlock(&buf->lock); in flush_to_ldisc()
513 * buffer data. in tty_flip_buffer_commit()
515 smp_store_release(&tail->commit, tail->used); in tty_flip_buffer_commit()
519 * tty_flip_buffer_push - push terminal buffers
530 struct tty_bufhead *buf = &port->buf; in tty_flip_buffer_push()
532 tty_flip_buffer_commit(buf->tail); in tty_flip_buffer_push()
533 queue_work(system_unbound_wq, &buf->work); in tty_flip_buffer_push()
538 * tty_insert_flip_string_and_push_buffer - add characters to the tty buffer and
542 * @size: size
545 * with the exception of properly holding the @port->lock.
552 const u8 *chars, size_t size) in tty_insert_flip_string_and_push_buffer() argument
554 struct tty_bufhead *buf = &port->buf; in tty_insert_flip_string_and_push_buffer()
557 spin_lock_irqsave(&port->lock, flags); in tty_insert_flip_string_and_push_buffer()
558 size = tty_insert_flip_string(port, chars, size); in tty_insert_flip_string_and_push_buffer()
559 if (size) in tty_insert_flip_string_and_push_buffer()
560 tty_flip_buffer_commit(buf->tail); in tty_insert_flip_string_and_push_buffer()
561 spin_unlock_irqrestore(&port->lock, flags); in tty_insert_flip_string_and_push_buffer()
563 queue_work(system_unbound_wq, &buf->work); in tty_insert_flip_string_and_push_buffer()
565 return size; in tty_insert_flip_string_and_push_buffer()
569 * tty_buffer_init - prepare a tty buffer structure
572 * Set up the initial state of the buffer management for a tty device. Must be
573 * called before the other tty buffer functions are used.
577 struct tty_bufhead *buf = &port->buf; in tty_buffer_init()
579 mutex_init(&buf->lock); in tty_buffer_init()
580 tty_buffer_reset(&buf->sentinel, 0); in tty_buffer_init()
581 buf->head = &buf->sentinel; in tty_buffer_init()
582 buf->tail = &buf->sentinel; in tty_buffer_init()
583 init_llist_head(&buf->free); in tty_buffer_init()
584 atomic_set(&buf->mem_used, 0); in tty_buffer_init()
585 atomic_set(&buf->priority, 0); in tty_buffer_init()
586 INIT_WORK(&buf->work, flush_to_ldisc); in tty_buffer_init()
587 buf->mem_limit = TTYB_DEFAULT_MEM_LIMIT; in tty_buffer_init()
591 * tty_buffer_set_limit - change the tty buffer memory limit
595 * Change the tty buffer memory limit.
597 * Must be called before the other tty buffer functions are used.
602 return -EINVAL; in tty_buffer_set_limit()
603 port->buf.mem_limit = limit; in tty_buffer_set_limit()
608 /* slave ptys can claim nested buffer lock when handling BRK and INTR */
611 lockdep_set_subclass(&port->buf.lock, TTY_LOCK_SLAVE); in tty_buffer_set_lock_subclass()
616 return queue_work(system_unbound_wq, &port->buf.work); in tty_buffer_restart_work()
621 return cancel_work_sync(&port->buf.work); in tty_buffer_cancel_work()
626 flush_work(&port->buf.work); in tty_buffer_flush_work()