^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Tty buffer allocation management
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/tty.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/tty_driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/tty_flip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/ratelimit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define MIN_TTYB_SIZE 256
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define TTYB_ALIGN_MASK 255
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * Byte threshold to limit memory consumption for flip buffers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * The actual memory limit is > 2x this amount.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define TTYB_DEFAULT_MEM_LIMIT (640 * 1024UL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * We default to dicing tty buffer allocations to this many characters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * in order to avoid multiple page allocations. We know the size of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * tty_buffer itself but it must also be taken into account that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * the buffer is 256 byte aligned. See tty_buffer_find for the allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * logic this must match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #define TTY_BUFFER_PAGE (((PAGE_SIZE - sizeof(struct tty_buffer)) / 2) & ~0xFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * tty_buffer_lock_exclusive - gain exclusive access to buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * tty_buffer_unlock_exclusive - release exclusive access
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * @port: tty port owning the flip buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * Guarantees safe use of the line discipline's receive_buf() method by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * excluding the buffer work and any pending flush from using the flip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * buffer. Data can continue to be added concurrently to the flip buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * from the driver side.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * On release, the buffer work is restarted if there is data in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * flip buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) void tty_buffer_lock_exclusive(struct tty_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct tty_bufhead *buf = &port->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) atomic_inc(&buf->priority);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) mutex_lock(&buf->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) EXPORT_SYMBOL_GPL(tty_buffer_lock_exclusive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) void tty_buffer_unlock_exclusive(struct tty_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct tty_bufhead *buf = &port->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) int restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) restart = buf->head->commit != buf->head->read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) atomic_dec(&buf->priority);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) mutex_unlock(&buf->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (restart)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) queue_work(system_unbound_wq, &buf->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) EXPORT_SYMBOL_GPL(tty_buffer_unlock_exclusive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * tty_buffer_space_avail - return unused buffer space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * @port: tty port owning the flip buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * Returns the # of bytes which can be written by the driver without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * reaching the buffer limit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * Note: this does not guarantee that memory is available to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * the returned # of bytes (use tty_prepare_flip_string_xxx() to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * pre-allocate if memory guarantee is required).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) int tty_buffer_space_avail(struct tty_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) int space = port->buf.mem_limit - atomic_read(&port->buf.mem_used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return max(space, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) EXPORT_SYMBOL_GPL(tty_buffer_space_avail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) static void tty_buffer_reset(struct tty_buffer *p, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) p->used = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) p->size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) p->next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) p->commit = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) p->read = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) p->flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * tty_buffer_free_all - free buffers used by a tty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * @port: tty port to free from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * Remove all the buffers pending on a tty whether queued with data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * or in the free ring. Must be called when the tty is no longer in use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) void tty_buffer_free_all(struct tty_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct tty_bufhead *buf = &port->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct tty_buffer *p, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct llist_node *llist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) unsigned int freed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) int still_used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) while ((p = buf->head) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) buf->head = p->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) freed += p->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (p->size > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) kfree(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) llist = llist_del_all(&buf->free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) llist_for_each_entry_safe(p, next, llist, free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) kfree(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) tty_buffer_reset(&buf->sentinel, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) buf->head = &buf->sentinel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) buf->tail = &buf->sentinel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) still_used = atomic_xchg(&buf->mem_used, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) WARN(still_used != freed, "we still have not freed %d bytes!",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) still_used - freed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * tty_buffer_alloc - allocate a tty buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * @port: tty port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * @size: desired size (characters)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * Allocate a new tty buffer to hold the desired number of characters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * We round our buffers off in 256 character chunks to get better
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * allocation behaviour.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * Return NULL if out of memory or the allocation would exceed the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * per device queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static struct tty_buffer *tty_buffer_alloc(struct tty_port *port, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) struct llist_node *free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct tty_buffer *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) /* Round the buffer size out */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) size = __ALIGN_MASK(size, TTYB_ALIGN_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (size <= MIN_TTYB_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) free = llist_del_first(&port->buf.free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (free) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) p = llist_entry(free, struct tty_buffer, free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) /* Should possibly check if this fails for the largest buffer we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) have queued and recycle that ? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (atomic_read(&port->buf.mem_used) > port->buf.mem_limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (p == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) tty_buffer_reset(p, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) atomic_add(size, &port->buf.mem_used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * tty_buffer_free - free a tty buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) * @port: tty port owning the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * @b: the buffer to free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) * Free a tty buffer, or add it to the free list according to our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) * internal strategy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static void tty_buffer_free(struct tty_port *port, struct tty_buffer *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) struct tty_bufhead *buf = &port->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) /* Dumb strategy for now - should keep some stats */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) WARN_ON(atomic_sub_return(b->size, &buf->mem_used) < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (b->size > MIN_TTYB_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) kfree(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) else if (b->size > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) llist_add(&b->free, &buf->free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * tty_buffer_flush - flush full tty buffers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * @tty: tty to flush
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * @ld: optional ldisc ptr (must be referenced)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * flush all the buffers containing receive data. If ld != NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * flush the ldisc input buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * Locking: takes buffer lock to ensure single-threaded flip buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * 'consumer'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) void tty_buffer_flush(struct tty_struct *tty, struct tty_ldisc *ld)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) struct tty_port *port = tty->port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) struct tty_bufhead *buf = &port->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) struct tty_buffer *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) atomic_inc(&buf->priority);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) mutex_lock(&buf->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) /* paired w/ release in __tty_buffer_request_room; ensures there are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) * no pending memory accesses to the freed buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) while ((next = smp_load_acquire(&buf->head->next)) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) tty_buffer_free(port, buf->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) buf->head = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) buf->head->read = buf->head->commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (ld && ld->ops->flush_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) ld->ops->flush_buffer(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) atomic_dec(&buf->priority);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) mutex_unlock(&buf->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * tty_buffer_request_room - grow tty buffer if needed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * @port: tty port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) * @size: size desired
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) * @flags: buffer flags if new buffer allocated (default = 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * Make at least size bytes of linear space available for the tty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * buffer. If we fail return the size we managed to find.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) * Will change over to a new buffer if the current buffer is encoded as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) * TTY_NORMAL (so has no flags buffer) and the new buffer requires
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) * a flags buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) static int __tty_buffer_request_room(struct tty_port *port, size_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) struct tty_bufhead *buf = &port->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) struct tty_buffer *b, *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) int left, change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) b = buf->tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) if (b->flags & TTYB_NORMAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) left = 2 * b->size - b->used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) left = b->size - b->used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) change = (b->flags & TTYB_NORMAL) && (~flags & TTYB_NORMAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (change || left < size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) /* This is the slow path - looking for new buffers to use */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) n = tty_buffer_alloc(port, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (n != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) n->flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) buf->tail = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) /* paired w/ acquire in flush_to_ldisc(); ensures
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * flush_to_ldisc() sees buffer data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) smp_store_release(&b->commit, b->used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) /* paired w/ acquire in flush_to_ldisc(); ensures the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) * latest commit value can be read before the head is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * advanced to the next buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) smp_store_release(&b->next, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) } else if (change)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) size = left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) int tty_buffer_request_room(struct tty_port *port, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) return __tty_buffer_request_room(port, size, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) EXPORT_SYMBOL_GPL(tty_buffer_request_room);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) * tty_insert_flip_string_fixed_flag - Add characters to the tty buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * @port: tty port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * @chars: characters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) * @flag: flag value for each character
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) * @size: size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) * Queue a series of bytes to the tty buffering. All the characters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * passed are marked with the supplied flag. Returns the number added.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) int tty_insert_flip_string_fixed_flag(struct tty_port *port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) const unsigned char *chars, char flag, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) int copied = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) int flags = (flag == TTY_NORMAL) ? TTYB_NORMAL : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) int space = __tty_buffer_request_room(port, goal, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) struct tty_buffer *tb = port->buf.tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (unlikely(space == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) memcpy(char_buf_ptr(tb, tb->used), chars, space);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (~tb->flags & TTYB_NORMAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) memset(flag_buf_ptr(tb, tb->used), flag, space);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) tb->used += space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) copied += space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) chars += space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) /* There is a small chance that we need to split the data over
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) several buffers. If this is the case we must loop */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) } while (unlikely(size > copied));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) return copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) EXPORT_SYMBOL(tty_insert_flip_string_fixed_flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) * tty_insert_flip_string_flags - Add characters to the tty buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) * @port: tty port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) * @chars: characters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) * @flags: flag bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) * @size: size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) * Queue a series of bytes to the tty buffering. For each character
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) * the flags array indicates the status of the character. Returns the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) * number added.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) int tty_insert_flip_string_flags(struct tty_port *port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) const unsigned char *chars, const char *flags, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) int copied = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) int space = tty_buffer_request_room(port, goal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) struct tty_buffer *tb = port->buf.tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) if (unlikely(space == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) memcpy(char_buf_ptr(tb, tb->used), chars, space);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) memcpy(flag_buf_ptr(tb, tb->used), flags, space);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) tb->used += space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) copied += space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) chars += space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) flags += space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) /* There is a small chance that we need to split the data over
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) several buffers. If this is the case we must loop */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) } while (unlikely(size > copied));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) return copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) EXPORT_SYMBOL(tty_insert_flip_string_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) * __tty_insert_flip_char - Add one character to the tty buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) * @port: tty port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) * @ch: character
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) * @flag: flag byte
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) * Queue a single byte to the tty buffering, with an optional flag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) * This is the slow path of tty_insert_flip_char.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) int __tty_insert_flip_char(struct tty_port *port, unsigned char ch, char flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) struct tty_buffer *tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) int flags = (flag == TTY_NORMAL) ? TTYB_NORMAL : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) if (!__tty_buffer_request_room(port, 1, flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) tb = port->buf.tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) if (~tb->flags & TTYB_NORMAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) *flag_buf_ptr(tb, tb->used) = flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) *char_buf_ptr(tb, tb->used++) = ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) EXPORT_SYMBOL(__tty_insert_flip_char);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) * tty_schedule_flip - push characters to ldisc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) * @port: tty port to push from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) * Takes any pending buffers and transfers their ownership to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) * ldisc side of the queue. It then schedules those characters for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) * processing by the line discipline.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) void tty_schedule_flip(struct tty_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) struct tty_bufhead *buf = &port->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) /* paired w/ acquire in flush_to_ldisc(); ensures
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) * flush_to_ldisc() sees buffer data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) smp_store_release(&buf->tail->commit, buf->tail->used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) queue_work(system_unbound_wq, &buf->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) EXPORT_SYMBOL(tty_schedule_flip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) * tty_prepare_flip_string - make room for characters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) * @port: tty port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) * @chars: return pointer for character write area
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) * @size: desired size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) * Prepare a block of space in the buffer for data. Returns the length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) * available and buffer pointer to the space which is now allocated and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) * accounted for as ready for normal characters. This is used for drivers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) * that need their own block copy routines into the buffer. There is no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) * guarantee the buffer is a DMA target!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) int tty_prepare_flip_string(struct tty_port *port, unsigned char **chars,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) int space = __tty_buffer_request_room(port, size, TTYB_NORMAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) if (likely(space)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) struct tty_buffer *tb = port->buf.tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) *chars = char_buf_ptr(tb, tb->used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) if (~tb->flags & TTYB_NORMAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) memset(flag_buf_ptr(tb, tb->used), TTY_NORMAL, space);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) tb->used += space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) return space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) * tty_ldisc_receive_buf - forward data to line discipline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) * @ld: line discipline to process input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) * @p: char buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) * @f: TTY_* flags buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) * @count: number of bytes to process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) * Callers other than flush_to_ldisc() need to exclude the kworker
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) * from concurrent use of the line discipline, see paste_selection().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) * Returns the number of bytes processed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) int tty_ldisc_receive_buf(struct tty_ldisc *ld, const unsigned char *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) char *f, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) if (ld->ops->receive_buf2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) count = ld->ops->receive_buf2(ld->tty, p, f, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) count = min_t(int, count, ld->tty->receive_room);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) if (count && ld->ops->receive_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) ld->ops->receive_buf(ld->tty, p, f, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) EXPORT_SYMBOL_GPL(tty_ldisc_receive_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) receive_buf(struct tty_port *port, struct tty_buffer *head, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) unsigned char *p = char_buf_ptr(head, head->read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) char *f = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) if (~head->flags & TTYB_NORMAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) f = flag_buf_ptr(head, head->read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) n = port->client_ops->receive_buf(port, p, f, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) if (n > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) memset(p, 0, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) * flush_to_ldisc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) * @work: tty structure passed from work queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) * This routine is called out of the software interrupt to flush data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) * from the buffer chain to the line discipline.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) * The receive_buf method is single threaded for each tty instance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) * Locking: takes buffer lock to ensure single-threaded flip buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) * 'consumer'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) static void flush_to_ldisc(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) struct tty_port *port = container_of(work, struct tty_port, buf.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) struct tty_bufhead *buf = &port->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) mutex_lock(&buf->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) struct tty_buffer *head = buf->head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) struct tty_buffer *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) int count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) /* Ldisc or user is trying to gain exclusive access */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) if (atomic_read(&buf->priority))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) /* paired w/ release in __tty_buffer_request_room();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) * ensures commit value read is not stale if the head
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) * is advancing to the next buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) next = smp_load_acquire(&head->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) /* paired w/ release in __tty_buffer_request_room() or in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) * tty_buffer_flush(); ensures we see the committed buffer data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) count = smp_load_acquire(&head->commit) - head->read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) if (!count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) if (next == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) buf->head = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) tty_buffer_free(port, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) count = receive_buf(port, head, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) if (!count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) head->read += count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) if (need_resched())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) mutex_unlock(&buf->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) * tty_flip_buffer_push - terminal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) * @port: tty port to push
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) * Queue a push of the terminal flip buffers to the line discipline.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) * Can be called from IRQ/atomic context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) * In the event of the queue being busy for flipping the work will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) * held off and retried later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) void tty_flip_buffer_push(struct tty_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) tty_schedule_flip(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) EXPORT_SYMBOL(tty_flip_buffer_push);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) * tty_buffer_init - prepare a tty buffer structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) * @port: tty port to initialise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) * Set up the initial state of the buffer management for a tty device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) * Must be called before the other tty buffer functions are used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) void tty_buffer_init(struct tty_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) struct tty_bufhead *buf = &port->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) mutex_init(&buf->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) tty_buffer_reset(&buf->sentinel, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) buf->head = &buf->sentinel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) buf->tail = &buf->sentinel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) init_llist_head(&buf->free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) atomic_set(&buf->mem_used, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) atomic_set(&buf->priority, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) INIT_WORK(&buf->work, flush_to_ldisc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) buf->mem_limit = TTYB_DEFAULT_MEM_LIMIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) * tty_buffer_set_limit - change the tty buffer memory limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) * @port: tty port to change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) * Change the tty buffer memory limit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) * Must be called before the other tty buffer functions are used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) int tty_buffer_set_limit(struct tty_port *port, int limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) if (limit < MIN_TTYB_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) port->buf.mem_limit = limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) EXPORT_SYMBOL_GPL(tty_buffer_set_limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) /* slave ptys can claim nested buffer lock when handling BRK and INTR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) void tty_buffer_set_lock_subclass(struct tty_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) lockdep_set_subclass(&port->buf.lock, TTY_LOCK_SLAVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) bool tty_buffer_restart_work(struct tty_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) return queue_work(system_unbound_wq, &port->buf.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) bool tty_buffer_cancel_work(struct tty_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) return cancel_work_sync(&port->buf.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) void tty_buffer_flush_work(struct tty_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) flush_work(&port->buf.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) }