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