xref: /freebsd/sys/kern/tty_outq.c (revision 39beb93c3f8bdbf72a61fda42300b5ebed7390c8)
1 /*-
2  * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Portions of this software were developed under sponsorship from Snow
6  * B.V., the Netherlands.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/lock.h>
36 #include <sys/queue.h>
37 #include <sys/sysctl.h>
38 #include <sys/systm.h>
39 #include <sys/tty.h>
40 #include <sys/uio.h>
41 
42 #include <vm/uma.h>
43 
44 /*
45  * TTY output queue buffering.
46  *
47  * The previous design of the TTY layer offered the so-called clists.
48  * These clists were used for both the input queues and the output
49  * queue. We don't use certain features on the output side, like quoting
50  * bits for parity marking and such. This mechanism is similar to the
51  * old clists, but only contains the features we need to buffer the
52  * output.
53  */
54 
55 /* Statistics. */
56 static unsigned long ttyoutq_nfast = 0;
57 SYSCTL_ULONG(_kern, OID_AUTO, tty_outq_nfast, CTLFLAG_RD,
58 	&ttyoutq_nfast, 0, "Unbuffered reads to userspace on output");
59 static unsigned long ttyoutq_nslow = 0;
60 SYSCTL_ULONG(_kern, OID_AUTO, tty_outq_nslow, CTLFLAG_RD,
61 	&ttyoutq_nslow, 0, "Buffered reads to userspace on output");
62 
63 struct ttyoutq_block {
64 	struct ttyoutq_block	*tob_next;
65 	char			tob_data[TTYOUTQ_DATASIZE];
66 };
67 
68 static uma_zone_t ttyoutq_zone;
69 
70 #define	TTYOUTQ_INSERT_TAIL(to, tob) do {				\
71 	if (to->to_end == 0) {						\
72 		tob->tob_next = to->to_firstblock;			\
73 		to->to_firstblock = tob;				\
74 	} else {							\
75 		tob->tob_next = to->to_lastblock->tob_next;		\
76 		to->to_lastblock->tob_next = tob;			\
77 	}								\
78 	to->to_nblocks++;						\
79 } while (0)
80 
81 #define	TTYOUTQ_REMOVE_HEAD(to) do {					\
82 	to->to_firstblock = to->to_firstblock->tob_next;		\
83 	to->to_nblocks--;						\
84 } while (0)
85 
86 #define	TTYOUTQ_RECYCLE(to, tob) do {					\
87 	if (to->to_quota <= to->to_nblocks)				\
88 		uma_zfree(ttyoutq_zone, tob);				\
89 	else								\
90 		TTYOUTQ_INSERT_TAIL(to, tob);				\
91 } while(0)
92 
93 void
94 ttyoutq_flush(struct ttyoutq *to)
95 {
96 
97 	to->to_begin = 0;
98 	to->to_end = 0;
99 }
100 
101 void
102 ttyoutq_setsize(struct ttyoutq *to, struct tty *tp, size_t size)
103 {
104 	struct ttyoutq_block *tob;
105 
106 	to->to_quota = howmany(size, TTYOUTQ_DATASIZE);
107 
108 	while (to->to_quota > to->to_nblocks) {
109 		/*
110 		 * List is getting bigger.
111 		 * Add new blocks to the tail of the list.
112 		 *
113 		 * We must unlock the TTY temporarily, because we need
114 		 * to allocate memory. This won't be a problem, because
115 		 * in the worst case, another thread ends up here, which
116 		 * may cause us to allocate too many blocks, but this
117 		 * will be caught by the loop below.
118 		 */
119 		tty_unlock(tp);
120 		tob = uma_zalloc(ttyoutq_zone, M_WAITOK);
121 		tty_lock(tp);
122 
123 		TTYOUTQ_INSERT_TAIL(to, tob);
124 	}
125 }
126 
127 void
128 ttyoutq_free(struct ttyoutq *to)
129 {
130 	struct ttyoutq_block *tob;
131 
132 	ttyoutq_flush(to);
133 	to->to_quota = 0;
134 
135 	while ((tob = to->to_firstblock) != NULL) {
136 		TTYOUTQ_REMOVE_HEAD(to);
137 		uma_zfree(ttyoutq_zone, tob);
138 	}
139 
140 	MPASS(to->to_nblocks == 0);
141 }
142 
143 size_t
144 ttyoutq_read(struct ttyoutq *to, void *buf, size_t len)
145 {
146 	char *cbuf = buf;
147 
148 	while (len > 0) {
149 		struct ttyoutq_block *tob;
150 		size_t cbegin, cend, clen;
151 
152 		/* See if there still is data. */
153 		if (to->to_begin == to->to_end)
154 			break;
155 		tob = to->to_firstblock;
156 		if (tob == NULL)
157 			break;
158 
159 		/*
160 		 * The end address should be the lowest of these three:
161 		 * - The write pointer
162 		 * - The blocksize - we can't read beyond the block
163 		 * - The end address if we could perform the full read
164 		 */
165 		cbegin = to->to_begin;
166 		cend = MIN(MIN(to->to_end, to->to_begin + len),
167 		    TTYOUTQ_DATASIZE);
168 		clen = cend - cbegin;
169 
170 		/* Copy the data out of the buffers. */
171 		memcpy(cbuf, tob->tob_data + cbegin, clen);
172 		cbuf += clen;
173 		len -= clen;
174 
175 		if (cend == to->to_end) {
176 			/* Read the complete queue. */
177 			to->to_begin = 0;
178 			to->to_end = 0;
179 		} else if (cend == TTYOUTQ_DATASIZE) {
180 			/* Read the block until the end. */
181 			TTYOUTQ_REMOVE_HEAD(to);
182 			to->to_begin = 0;
183 			to->to_end -= TTYOUTQ_DATASIZE;
184 			TTYOUTQ_RECYCLE(to, tob);
185 		} else {
186 			/* Read the block partially. */
187 			to->to_begin += clen;
188 		}
189 	}
190 
191 	return (cbuf - (char *)buf);
192 }
193 
194 /*
195  * An optimized version of ttyoutq_read() which can be used in pseudo
196  * TTY drivers to directly copy data from the outq to userspace, instead
197  * of buffering it.
198  *
199  * We can only copy data directly if we need to read the entire block
200  * back to the user, because we temporarily remove the block from the
201  * queue. Otherwise we need to copy it to a temporary buffer first, to
202  * make sure data remains in the correct order.
203  */
204 int
205 ttyoutq_read_uio(struct ttyoutq *to, struct tty *tp, struct uio *uio)
206 {
207 
208 	while (uio->uio_resid > 0) {
209 		int error;
210 		struct ttyoutq_block *tob;
211 		size_t cbegin, cend, clen;
212 
213 		/* See if there still is data. */
214 		if (to->to_begin == to->to_end)
215 			return (0);
216 		tob = to->to_firstblock;
217 		if (tob == NULL)
218 			return (0);
219 
220 		/*
221 		 * The end address should be the lowest of these three:
222 		 * - The write pointer
223 		 * - The blocksize - we can't read beyond the block
224 		 * - The end address if we could perform the full read
225 		 */
226 		cbegin = to->to_begin;
227 		cend = MIN(MIN(to->to_end, to->to_begin + uio->uio_resid),
228 		    TTYOUTQ_DATASIZE);
229 		clen = cend - cbegin;
230 
231 		/*
232 		 * We can prevent buffering in some cases:
233 		 * - We need to read the block until the end.
234 		 * - We don't need to read the block until the end, but
235 		 *   there is no data beyond it, which allows us to move
236 		 *   the write pointer to a new block.
237 		 */
238 		if (cend == TTYOUTQ_DATASIZE || cend == to->to_end) {
239 			atomic_add_long(&ttyoutq_nfast, 1);
240 
241 			/*
242 			 * Fast path: zero copy. Remove the first block,
243 			 * so we can unlock the TTY temporarily.
244 			 */
245 			TTYOUTQ_REMOVE_HEAD(to);
246 			to->to_begin = 0;
247 			if (to->to_end <= TTYOUTQ_DATASIZE)
248 				to->to_end = 0;
249 			else
250 				to->to_end -= TTYOUTQ_DATASIZE;
251 
252 			/* Temporary unlock and copy the data to userspace. */
253 			tty_unlock(tp);
254 			error = uiomove(tob->tob_data + cbegin, clen, uio);
255 			tty_lock(tp);
256 
257 			/* Block can now be readded to the list. */
258 			TTYOUTQ_RECYCLE(to, tob);
259 		} else {
260 			char ob[TTYOUTQ_DATASIZE - 1];
261 			atomic_add_long(&ttyoutq_nslow, 1);
262 
263 			/*
264 			 * Slow path: store data in a temporary buffer.
265 			 */
266 			memcpy(ob, tob->tob_data + cbegin, clen);
267 			to->to_begin += clen;
268 			MPASS(to->to_begin < TTYOUTQ_DATASIZE);
269 
270 			/* Temporary unlock and copy the data to userspace. */
271 			tty_unlock(tp);
272 			error = uiomove(ob, clen, uio);
273 			tty_lock(tp);
274 		}
275 
276 		if (error != 0)
277 			return (error);
278 	}
279 
280 	return (0);
281 }
282 
283 size_t
284 ttyoutq_write(struct ttyoutq *to, const void *buf, size_t nbytes)
285 {
286 	const char *cbuf = buf;
287 	struct ttyoutq_block *tob;
288 	unsigned int boff;
289 	size_t l;
290 
291 	while (nbytes > 0) {
292 		boff = to->to_end % TTYOUTQ_DATASIZE;
293 
294 		if (to->to_end == 0) {
295 			/* First time we're being used or drained. */
296 			MPASS(to->to_begin == 0);
297 			tob = to->to_firstblock;
298 			if (tob == NULL) {
299 				/* Queue has no blocks. */
300 				break;
301 			}
302 			to->to_lastblock = tob;
303 		} else if (boff == 0) {
304 			/* We reached the end of this block on last write. */
305 			tob = to->to_lastblock->tob_next;
306 			if (tob == NULL) {
307 				/* We've reached the watermark. */
308 				break;
309 			}
310 			to->to_lastblock = tob;
311 		} else {
312 			tob = to->to_lastblock;
313 		}
314 
315 		/* Don't copy more than was requested. */
316 		l = MIN(nbytes, TTYOUTQ_DATASIZE - boff);
317 		MPASS(l > 0);
318 		memcpy(tob->tob_data + boff, cbuf, l);
319 
320 		cbuf += l;
321 		nbytes -= l;
322 		to->to_end += l;
323 	}
324 
325 	return (cbuf - (const char *)buf);
326 }
327 
328 int
329 ttyoutq_write_nofrag(struct ttyoutq *to, const void *buf, size_t nbytes)
330 {
331 	size_t ret;
332 
333 	if (ttyoutq_bytesleft(to) < nbytes)
334 		return (-1);
335 
336 	/* We should always be able to write it back. */
337 	ret = ttyoutq_write(to, buf, nbytes);
338 	MPASS(ret == nbytes);
339 
340 	return (0);
341 }
342 
343 static void
344 ttyoutq_startup(void *dummy)
345 {
346 
347 	ttyoutq_zone = uma_zcreate("ttyoutq", sizeof(struct ttyoutq_block),
348 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
349 }
350 
351 SYSINIT(ttyoutq, SI_SUB_DRIVERS, SI_ORDER_FIRST, ttyoutq_startup, NULL);
352