Lines Matching +full:buffer +full:- +full:size

1 // SPDX-License-Identifier: GPL-2.0
3 * Tty buffer allocation management
34 * We default to dicing tty buffer allocations to this many characters
35 * in order to avoid multiple page allocations. We know the size of
37 * buffer is 256 byte aligned. See tty_buffer_find for the allocation
41 #define TTY_BUFFER_PAGE (((PAGE_SIZE - sizeof(struct tty_buffer)) / 2) & ~TTYB_ALIGN_MASK)
44 * tty_buffer_lock_exclusive - gain exclusive access to buffer
45 * @port: tty port owning the flip buffer
48 * the buffer work and any pending flush from using the flip buffer. Data can
49 * continue to be added concurrently to the flip buffer from the driver side.
55 struct tty_bufhead *buf = &port->buf; in tty_buffer_lock_exclusive()
57 atomic_inc(&buf->priority); in tty_buffer_lock_exclusive()
58 mutex_lock(&buf->lock); in tty_buffer_lock_exclusive()
63 * tty_buffer_unlock_exclusive - release exclusive access
64 * @port: tty port owning the flip buffer
66 * The buffer work is restarted if there is data in the flip buffer.
72 struct tty_bufhead *buf = &port->buf; in tty_buffer_unlock_exclusive()
73 bool restart = buf->head->commit != buf->head->read; in tty_buffer_unlock_exclusive()
75 atomic_dec(&buf->priority); in tty_buffer_unlock_exclusive()
76 mutex_unlock(&buf->lock); in tty_buffer_unlock_exclusive()
79 queue_work(system_unbound_wq, &buf->work); in tty_buffer_unlock_exclusive()
84 * tty_buffer_space_avail - return unused buffer space
85 * @port: tty port owning the flip buffer
88 * the buffer limit.
91 * # of bytes (use tty_prepare_flip_string() to pre-allocate if memory
96 int space = port->buf.mem_limit - atomic_read(&port->buf.mem_used); in tty_buffer_space_avail()
102 static void tty_buffer_reset(struct tty_buffer *p, size_t size) in tty_buffer_reset() argument
104 p->used = 0; in tty_buffer_reset()
105 p->size = size; in tty_buffer_reset()
106 p->next = NULL; in tty_buffer_reset()
107 p->commit = 0; in tty_buffer_reset()
108 p->lookahead = 0; in tty_buffer_reset()
109 p->read = 0; in tty_buffer_reset()
110 p->flags = true; in tty_buffer_reset()
114 * tty_buffer_free_all - free buffers used by a tty
122 struct tty_bufhead *buf = &port->buf; in tty_buffer_free_all()
128 while ((p = buf->head) != NULL) { in tty_buffer_free_all()
129 buf->head = p->next; in tty_buffer_free_all()
130 freed += p->size; in tty_buffer_free_all()
131 if (p->size > 0) in tty_buffer_free_all()
134 llist = llist_del_all(&buf->free); in tty_buffer_free_all()
138 tty_buffer_reset(&buf->sentinel, 0); in tty_buffer_free_all()
139 buf->head = &buf->sentinel; in tty_buffer_free_all()
140 buf->tail = &buf->sentinel; in tty_buffer_free_all()
142 still_used = atomic_xchg(&buf->mem_used, 0); in tty_buffer_free_all()
144 still_used - freed); in tty_buffer_free_all()
148 * tty_buffer_alloc - allocate a tty buffer
150 * @size: desired size (characters)
152 * Allocate a new tty buffer to hold the desired number of characters. We
159 static struct tty_buffer *tty_buffer_alloc(struct tty_port *port, size_t size) in tty_buffer_alloc() argument
164 /* Round the buffer size out */ in tty_buffer_alloc()
165 size = __ALIGN_MASK(size, TTYB_ALIGN_MASK); in tty_buffer_alloc()
167 if (size <= MIN_TTYB_SIZE) { in tty_buffer_alloc()
168 free = llist_del_first(&port->buf.free); in tty_buffer_alloc()
175 /* Should possibly check if this fails for the largest buffer we in tty_buffer_alloc()
178 if (atomic_read(&port->buf.mem_used) > port->buf.mem_limit) in tty_buffer_alloc()
180 p = kmalloc(struct_size(p, data, 2 * size), GFP_ATOMIC | __GFP_NOWARN); in tty_buffer_alloc()
185 tty_buffer_reset(p, size); in tty_buffer_alloc()
186 atomic_add(size, &port->buf.mem_used); in tty_buffer_alloc()
191 * tty_buffer_free - free a tty buffer
192 * @port: tty port owning the buffer
193 * @b: the buffer to free
195 * Free a tty buffer, or add it to the free list according to our internal
200 struct tty_bufhead *buf = &port->buf; in tty_buffer_free()
202 /* Dumb strategy for now - should keep some stats */ in tty_buffer_free()
203 WARN_ON(atomic_sub_return(b->size, &buf->mem_used) < 0); in tty_buffer_free()
205 if (b->size > MIN_TTYB_SIZE) in tty_buffer_free()
207 else if (b->size > 0) in tty_buffer_free()
208 llist_add(&b->free, &buf->free); in tty_buffer_free()
212 * tty_buffer_flush - flush full tty buffers
217 * ldisc input buffer.
219 * Locking: takes buffer lock to ensure single-threaded flip buffer 'consumer'.
223 struct tty_port *port = tty->port; in tty_buffer_flush()
224 struct tty_bufhead *buf = &port->buf; in tty_buffer_flush()
227 atomic_inc(&buf->priority); in tty_buffer_flush()
229 mutex_lock(&buf->lock); in tty_buffer_flush()
231 * no pending memory accesses to the freed buffer in tty_buffer_flush()
233 while ((next = smp_load_acquire(&buf->head->next)) != NULL) { in tty_buffer_flush()
234 tty_buffer_free(port, buf->head); in tty_buffer_flush()
235 buf->head = next; in tty_buffer_flush()
237 buf->head->read = buf->head->commit; in tty_buffer_flush()
238 buf->head->lookahead = buf->head->read; in tty_buffer_flush()
240 if (ld && ld->ops->flush_buffer) in tty_buffer_flush()
241 ld->ops->flush_buffer(tty); in tty_buffer_flush()
243 atomic_dec(&buf->priority); in tty_buffer_flush()
244 mutex_unlock(&buf->lock); in tty_buffer_flush()
248 * __tty_buffer_request_room - grow tty buffer if needed
250 * @size: size desired
251 * @flags: buffer has to store flags along character data
253 * Make at least @size bytes of linear space available for the tty buffer.
255 * Will change over to a new buffer if the current buffer is encoded as
256 * %TTY_NORMAL (so has no flags buffer) and the new buffer requires a flags
257 * buffer.
259 * Returns: the size we managed to find.
261 static int __tty_buffer_request_room(struct tty_port *port, size_t size, in __tty_buffer_request_room() argument
264 struct tty_bufhead *buf = &port->buf; in __tty_buffer_request_room()
265 struct tty_buffer *n, *b = buf->tail; in __tty_buffer_request_room()
266 size_t left = (b->flags ? 1 : 2) * b->size - b->used; in __tty_buffer_request_room()
267 bool change = !b->flags && flags; in __tty_buffer_request_room()
269 if (!change && left >= size) in __tty_buffer_request_room()
270 return size; in __tty_buffer_request_room()
272 /* This is the slow path - looking for new buffers to use */ in __tty_buffer_request_room()
273 n = tty_buffer_alloc(port, size); in __tty_buffer_request_room()
277 n->flags = flags; in __tty_buffer_request_room()
278 buf->tail = n; in __tty_buffer_request_room()
281 * ensures they see all buffer data. in __tty_buffer_request_room()
283 smp_store_release(&b->commit, b->used); in __tty_buffer_request_room()
287 * is advanced to the next buffer. in __tty_buffer_request_room()
289 smp_store_release(&b->next, n); in __tty_buffer_request_room()
291 return size; in __tty_buffer_request_room()
294 int tty_buffer_request_room(struct tty_port *port, size_t size) in tty_buffer_request_room() argument
296 return __tty_buffer_request_room(port, size, true); in tty_buffer_request_room()
302 size_t size) in __tty_insert_flip_string_flags() argument
308 size_t goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE); in __tty_insert_flip_string_flags()
310 struct tty_buffer *tb = port->buf.tail; in __tty_insert_flip_string_flags()
315 memcpy(char_buf_ptr(tb, tb->used), chars, space); in __tty_insert_flip_string_flags()
318 memcpy(flag_buf_ptr(tb, tb->used), flags, space); in __tty_insert_flip_string_flags()
320 } else if (tb->flags) { in __tty_insert_flip_string_flags()
321 memset(flag_buf_ptr(tb, tb->used), flags[0], space); in __tty_insert_flip_string_flags()
323 /* tb->flags should be available once requested */ in __tty_insert_flip_string_flags()
327 tb->used += space; in __tty_insert_flip_string_flags()
334 } while (unlikely(size > copied)); in __tty_insert_flip_string_flags()
341 * tty_prepare_flip_string - make room for characters
344 * @size: desired size
346 * Prepare a block of space in the buffer for data.
349 * buffer. There is no guarantee the buffer is a DMA target!
351 * Returns: the length available and buffer pointer (@chars) to the space which
354 size_t tty_prepare_flip_string(struct tty_port *port, u8 **chars, size_t size) in tty_prepare_flip_string() argument
356 size_t space = __tty_buffer_request_room(port, size, false); in tty_prepare_flip_string()
359 struct tty_buffer *tb = port->buf.tail; in tty_prepare_flip_string()
361 *chars = char_buf_ptr(tb, tb->used); in tty_prepare_flip_string()
362 if (tb->flags) in tty_prepare_flip_string()
363 memset(flag_buf_ptr(tb, tb->used), TTY_NORMAL, space); in tty_prepare_flip_string()
364 tb->used += space; in tty_prepare_flip_string()
372 * tty_ldisc_receive_buf - forward data to line discipline
374 * @p: char buffer
375 * @f: %TTY_NORMAL, %TTY_BREAK, etc. flags buffer
386 if (ld->ops->receive_buf2) in tty_ldisc_receive_buf()
387 count = ld->ops->receive_buf2(ld->tty, p, f, count); in tty_ldisc_receive_buf()
389 count = min_t(size_t, count, ld->tty->receive_room); in tty_ldisc_receive_buf()
390 if (count && ld->ops->receive_buf) in tty_ldisc_receive_buf()
391 ld->ops->receive_buf(ld->tty, p, f, count); in tty_ldisc_receive_buf()
399 head->lookahead = max(head->lookahead, head->read); in lookahead_bufs()
408 * is advancing to the next buffer. in lookahead_bufs()
410 next = smp_load_acquire(&head->next); in lookahead_bufs()
413 * tty_buffer_flush(); ensures we see the committed buffer data. in lookahead_bufs()
415 count = smp_load_acquire(&head->commit) - head->lookahead; in lookahead_bufs()
421 if (port->client_ops->lookahead_buf) { in lookahead_bufs()
424 p = char_buf_ptr(head, head->lookahead); in lookahead_bufs()
425 if (head->flags) in lookahead_bufs()
426 f = flag_buf_ptr(head, head->lookahead); in lookahead_bufs()
428 port->client_ops->lookahead_buf(port, p, f, count); in lookahead_bufs()
431 head->lookahead += count; in lookahead_bufs()
438 u8 *p = char_buf_ptr(head, head->read); in receive_buf()
442 if (head->flags) in receive_buf()
443 f = flag_buf_ptr(head, head->read); in receive_buf()
445 n = port->client_ops->receive_buf(port, p, f, count); in receive_buf()
452 * flush_to_ldisc - flush data from buffer to ldisc
456 * buffer chain to the line discipline.
460 * Locking: takes buffer lock to ensure single-threaded flip buffer 'consumer'.
465 struct tty_bufhead *buf = &port->buf; in flush_to_ldisc()
467 mutex_lock(&buf->lock); in flush_to_ldisc()
470 struct tty_buffer *head = buf->head; in flush_to_ldisc()
475 if (atomic_read(&buf->priority)) in flush_to_ldisc()
480 * is advancing to the next buffer in flush_to_ldisc()
482 next = smp_load_acquire(&head->next); in flush_to_ldisc()
484 * tty_buffer_flush(); ensures we see the committed buffer data in flush_to_ldisc()
486 count = smp_load_acquire(&head->commit) - head->read; in flush_to_ldisc()
490 buf->head = next; in flush_to_ldisc()
496 head->read += rcvd; in flush_to_ldisc()
506 mutex_unlock(&buf->lock); in flush_to_ldisc()
514 * buffer data. in tty_flip_buffer_commit()
516 smp_store_release(&tail->commit, tail->used); in tty_flip_buffer_commit()
520 * tty_flip_buffer_push - push terminal buffers
531 struct tty_bufhead *buf = &port->buf; in tty_flip_buffer_push()
533 tty_flip_buffer_commit(buf->tail); in tty_flip_buffer_push()
534 queue_work(system_unbound_wq, &buf->work); in tty_flip_buffer_push()
539 * tty_insert_flip_string_and_push_buffer - add characters to the tty buffer and
543 * @size: size
546 * with the exception of properly holding the @port->lock.
553 const u8 *chars, size_t size) in tty_insert_flip_string_and_push_buffer() argument
555 struct tty_bufhead *buf = &port->buf; in tty_insert_flip_string_and_push_buffer()
558 spin_lock_irqsave(&port->lock, flags); in tty_insert_flip_string_and_push_buffer()
559 size = tty_insert_flip_string(port, chars, size); in tty_insert_flip_string_and_push_buffer()
560 if (size) in tty_insert_flip_string_and_push_buffer()
561 tty_flip_buffer_commit(buf->tail); in tty_insert_flip_string_and_push_buffer()
562 spin_unlock_irqrestore(&port->lock, flags); in tty_insert_flip_string_and_push_buffer()
564 queue_work(system_unbound_wq, &buf->work); in tty_insert_flip_string_and_push_buffer()
566 return size; in tty_insert_flip_string_and_push_buffer()
570 * tty_buffer_init - prepare a tty buffer structure
573 * Set up the initial state of the buffer management for a tty device. Must be
574 * called before the other tty buffer functions are used.
578 struct tty_bufhead *buf = &port->buf; in tty_buffer_init()
580 mutex_init(&buf->lock); in tty_buffer_init()
581 tty_buffer_reset(&buf->sentinel, 0); in tty_buffer_init()
582 buf->head = &buf->sentinel; in tty_buffer_init()
583 buf->tail = &buf->sentinel; in tty_buffer_init()
584 init_llist_head(&buf->free); in tty_buffer_init()
585 atomic_set(&buf->mem_used, 0); in tty_buffer_init()
586 atomic_set(&buf->priority, 0); in tty_buffer_init()
587 INIT_WORK(&buf->work, flush_to_ldisc); in tty_buffer_init()
588 buf->mem_limit = TTYB_DEFAULT_MEM_LIMIT; in tty_buffer_init()
592 * tty_buffer_set_limit - change the tty buffer memory limit
596 * Change the tty buffer memory limit.
598 * Must be called before the other tty buffer functions are used.
603 return -EINVAL; in tty_buffer_set_limit()
604 port->buf.mem_limit = limit; in tty_buffer_set_limit()
609 /* slave ptys can claim nested buffer lock when handling BRK and INTR */
612 lockdep_set_subclass(&port->buf.lock, TTY_LOCK_SLAVE); in tty_buffer_set_lock_subclass()
617 return queue_work(system_unbound_wq, &port->buf.work); in tty_buffer_restart_work()
622 return cancel_work_sync(&port->buf.work); in tty_buffer_cancel_work()
627 flush_work(&port->buf.work); in tty_buffer_flush_work()