Commit | Line | Data |
---|---|---|
e3b3d0f5 | 1 | // SPDX-License-Identifier: GPL-2.0 |
01e1abb2 | 2 | #include <linux/types.h> |
01e1abb2 | 3 | #include <linux/errno.h> |
8b3ffa17 | 4 | #include <linux/kmod.h> |
01e1abb2 AC |
5 | #include <linux/sched.h> |
6 | #include <linux/interrupt.h> | |
7 | #include <linux/tty.h> | |
8 | #include <linux/tty_driver.h> | |
01e1abb2 | 9 | #include <linux/file.h> |
01e1abb2 AC |
10 | #include <linux/mm.h> |
11 | #include <linux/string.h> | |
12 | #include <linux/slab.h> | |
13 | #include <linux/poll.h> | |
14 | #include <linux/proc_fs.h> | |
01e1abb2 | 15 | #include <linux/module.h> |
01e1abb2 AC |
16 | #include <linux/device.h> |
17 | #include <linux/wait.h> | |
18 | #include <linux/bitops.h> | |
01e1abb2 | 19 | #include <linux/seq_file.h> |
01e1abb2 | 20 | #include <linux/uaccess.h> |
0c73c08e | 21 | #include <linux/ratelimit.h> |
98602c01 | 22 | #include "tty.h" |
01e1abb2 | 23 | |
fc575ee6 PH |
24 | #undef LDISC_DEBUG_HANGUP |
25 | ||
26 | #ifdef LDISC_DEBUG_HANGUP | |
0a6adc13 | 27 | #define tty_ldisc_debug(tty, f, args...) tty_debug(tty, f, ##args) |
fc575ee6 PH |
28 | #else |
29 | #define tty_ldisc_debug(tty, f, args...) | |
30 | #endif | |
31 | ||
d2c43890 PH |
32 | /* lockdep nested classes for tty->ldisc_sem */ |
33 | enum { | |
34 | LDISC_SEM_NORMAL, | |
35 | LDISC_SEM_OTHER, | |
36 | }; | |
37 | ||
38 | ||
01e1abb2 AC |
39 | /* |
40 | * This guards the refcounted line discipline lists. The lock | |
41 | * must be taken with irqs off because there are hangup path | |
42 | * callers who will do ldisc lookups and cannot sleep. | |
43 | */ | |
44 | ||
137084bb | 45 | static DEFINE_RAW_SPINLOCK(tty_ldiscs_lock); |
01e1abb2 AC |
46 | /* Line disc dispatch table */ |
47 | static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS]; | |
48 | ||
49 | /** | |
cbb68f91 JS |
50 | * tty_register_ldisc - install a line discipline |
51 | * @new_ldisc: pointer to the ldisc object | |
01e1abb2 | 52 | * |
cbb68f91 JS |
53 | * Installs a new line discipline into the kernel. The discipline is set up as |
54 | * unreferenced and then made available to the kernel from this point onwards. | |
01e1abb2 | 55 | * |
cbb68f91 | 56 | * Locking: takes %tty_ldiscs_lock to guard against ldisc races |
01e1abb2 | 57 | */ |
fbadf70a | 58 | int tty_register_ldisc(struct tty_ldisc_ops *new_ldisc) |
01e1abb2 AC |
59 | { |
60 | unsigned long flags; | |
61 | int ret = 0; | |
62 | ||
fbadf70a | 63 | if (new_ldisc->num < N_TTY || new_ldisc->num >= NR_LDISCS) |
01e1abb2 AC |
64 | return -EINVAL; |
65 | ||
137084bb | 66 | raw_spin_lock_irqsave(&tty_ldiscs_lock, flags); |
fbadf70a | 67 | tty_ldiscs[new_ldisc->num] = new_ldisc; |
137084bb | 68 | raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags); |
01e1abb2 AC |
69 | |
70 | return ret; | |
71 | } | |
72 | EXPORT_SYMBOL(tty_register_ldisc); | |
73 | ||
74 | /** | |
cbb68f91 JS |
75 | * tty_unregister_ldisc - unload a line discipline |
76 | * @ldisc: ldisc number | |
01e1abb2 | 77 | * |
cbb68f91 JS |
78 | * Remove a line discipline from the kernel providing it is not currently in |
79 | * use. | |
01e1abb2 | 80 | * |
cbb68f91 | 81 | * Locking: takes %tty_ldiscs_lock to guard against ldisc races |
01e1abb2 AC |
82 | */ |
83 | ||
f6f19595 | 84 | void tty_unregister_ldisc(struct tty_ldisc_ops *ldisc) |
01e1abb2 AC |
85 | { |
86 | unsigned long flags; | |
01e1abb2 | 87 | |
137084bb | 88 | raw_spin_lock_irqsave(&tty_ldiscs_lock, flags); |
19475209 | 89 | tty_ldiscs[ldisc->num] = NULL; |
137084bb | 90 | raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags); |
01e1abb2 AC |
91 | } |
92 | EXPORT_SYMBOL(tty_unregister_ldisc); | |
93 | ||
f0de0e8d LT |
94 | static struct tty_ldisc_ops *get_ldops(int disc) |
95 | { | |
96 | unsigned long flags; | |
97 | struct tty_ldisc_ops *ldops, *ret; | |
98 | ||
137084bb | 99 | raw_spin_lock_irqsave(&tty_ldiscs_lock, flags); |
f0de0e8d LT |
100 | ret = ERR_PTR(-EINVAL); |
101 | ldops = tty_ldiscs[disc]; | |
102 | if (ldops) { | |
103 | ret = ERR_PTR(-EAGAIN); | |
19475209 | 104 | if (try_module_get(ldops->owner)) |
f0de0e8d | 105 | ret = ldops; |
f0de0e8d | 106 | } |
137084bb | 107 | raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags); |
f0de0e8d LT |
108 | return ret; |
109 | } | |
110 | ||
111 | static void put_ldops(struct tty_ldisc_ops *ldops) | |
112 | { | |
113 | unsigned long flags; | |
114 | ||
137084bb | 115 | raw_spin_lock_irqsave(&tty_ldiscs_lock, flags); |
f0de0e8d | 116 | module_put(ldops->owner); |
137084bb | 117 | raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags); |
f0de0e8d | 118 | } |
01e1abb2 | 119 | |
5fd8c2d3 | 120 | int tty_ldisc_autoload = IS_BUILTIN(CONFIG_LDISC_AUTOLOAD); |
cbb68f91 | 121 | |
01e1abb2 | 122 | /** |
cbb68f91 JS |
123 | * tty_ldisc_get - take a reference to an ldisc |
124 | * @tty: tty device | |
125 | * @disc: ldisc number | |
126 | * | |
127 | * Takes a reference to a line discipline. Deals with refcounts and module | |
128 | * locking counts. If the discipline is not available, its module loaded, if | |
129 | * possible. | |
130 | * | |
131 | * Returns: | |
132 | * * -%EINVAL if the discipline index is not [%N_TTY .. %NR_LDISCS] or if the | |
133 | * discipline is not registered | |
134 | * * -%EAGAIN if request_module() failed to load or register the discipline | |
135 | * * -%ENOMEM if allocation failure | |
136 | * * Otherwise, returns a pointer to the discipline and bumps the ref count | |
137 | * | |
138 | * Locking: takes %tty_ldiscs_lock to guard against ldisc races | |
01e1abb2 | 139 | */ |
36697529 | 140 | static struct tty_ldisc *tty_ldisc_get(struct tty_struct *tty, int disc) |
01e1abb2 | 141 | { |
c65c9bc3 | 142 | struct tty_ldisc *ld; |
182274f8 | 143 | struct tty_ldisc_ops *ldops; |
01e1abb2 AC |
144 | |
145 | if (disc < N_TTY || disc >= NR_LDISCS) | |
c65c9bc3 | 146 | return ERR_PTR(-EINVAL); |
182274f8 LT |
147 | |
148 | /* | |
149 | * Get the ldisc ops - we may need to request them to be loaded | |
150 | * dynamically and try again. | |
151 | */ | |
152 | ldops = get_ldops(disc); | |
153 | if (IS_ERR(ldops)) { | |
7c0cca7c GKH |
154 | if (!capable(CAP_SYS_MODULE) && !tty_ldisc_autoload) |
155 | return ERR_PTR(-EPERM); | |
01e1abb2 | 156 | request_module("tty-ldisc-%d", disc); |
182274f8 LT |
157 | ldops = get_ldops(disc); |
158 | if (IS_ERR(ldops)) | |
159 | return ERR_CAST(ldops); | |
160 | } | |
161 | ||
bcdd0ca8 TH |
162 | /* |
163 | * There is no way to handle allocation failure of only 16 bytes. | |
164 | * Let's simplify error handling and save more memory. | |
165 | */ | |
166 | ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL | __GFP_NOFAIL); | |
182274f8 | 167 | ld->ops = ldops; |
36697529 | 168 | ld->tty = tty; |
1541f845 | 169 | |
c65c9bc3 | 170 | return ld; |
01e1abb2 AC |
171 | } |
172 | ||
cbb68f91 JS |
173 | /** |
174 | * tty_ldisc_put - release the ldisc | |
175 | * @ld: lisdsc to release | |
734de249 | 176 | * |
cbb68f91 | 177 | * Complement of tty_ldisc_get(). |
734de249 | 178 | */ |
cb128f69 | 179 | static void tty_ldisc_put(struct tty_ldisc *ld) |
734de249 | 180 | { |
734de249 PH |
181 | if (WARN_ON_ONCE(!ld)) |
182 | return; | |
183 | ||
36697529 | 184 | put_ldops(ld->ops); |
734de249 | 185 | kfree(ld); |
734de249 PH |
186 | } |
187 | ||
852e99d2 | 188 | static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos) |
01e1abb2 AC |
189 | { |
190 | return (*pos < NR_LDISCS) ? pos : NULL; | |
191 | } | |
192 | ||
852e99d2 | 193 | static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos) |
01e1abb2 AC |
194 | { |
195 | (*pos)++; | |
196 | return (*pos < NR_LDISCS) ? pos : NULL; | |
197 | } | |
198 | ||
199 | static void tty_ldiscs_seq_stop(struct seq_file *m, void *v) | |
200 | { | |
201 | } | |
202 | ||
203 | static int tty_ldiscs_seq_show(struct seq_file *m, void *v) | |
204 | { | |
205 | int i = *(loff_t *)v; | |
f0de0e8d | 206 | struct tty_ldisc_ops *ldops; |
852e99d2 | 207 | |
f0de0e8d LT |
208 | ldops = get_ldops(i); |
209 | if (IS_ERR(ldops)) | |
01e1abb2 | 210 | return 0; |
f0de0e8d LT |
211 | seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i); |
212 | put_ldops(ldops); | |
01e1abb2 AC |
213 | return 0; |
214 | } | |
215 | ||
fddda2b7 | 216 | const struct seq_operations tty_ldiscs_seq_ops = { |
01e1abb2 AC |
217 | .start = tty_ldiscs_seq_start, |
218 | .next = tty_ldiscs_seq_next, | |
219 | .stop = tty_ldiscs_seq_stop, | |
220 | .show = tty_ldiscs_seq_show, | |
221 | }; | |
222 | ||
01e1abb2 | 223 | /** |
cbb68f91 JS |
224 | * tty_ldisc_ref_wait - wait for the tty ldisc |
225 | * @tty: tty device | |
01e1abb2 | 226 | * |
cbb68f91 JS |
227 | * Dereference the line discipline for the terminal and take a reference to it. |
228 | * If the line discipline is in flux then wait patiently until it changes. | |
01e1abb2 | 229 | * |
cbb68f91 JS |
230 | * Returns: %NULL if the tty has been hungup and not re-opened with a new file |
231 | * descriptor, otherwise valid ldisc reference | |
892d1fa7 | 232 | * |
cbb68f91 JS |
233 | * Note 1: Must not be called from an IRQ/timer context. The caller must also |
234 | * be careful not to hold other locks that will deadlock against a discipline | |
235 | * change, such as an existing ldisc reference (which we check for). | |
01e1abb2 | 236 | * |
cbb68f91 JS |
237 | * Note 2: a file_operations routine (read/poll/write) should use this function |
238 | * to wait for any ldisc lifetime events to finish. | |
01e1abb2 | 239 | */ |
01e1abb2 AC |
240 | struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty) |
241 | { | |
a4a3e061 DV |
242 | struct tty_ldisc *ld; |
243 | ||
36697529 | 244 | ldsem_down_read(&tty->ldisc_sem, MAX_SCHEDULE_TIMEOUT); |
a4a3e061 DV |
245 | ld = tty->ldisc; |
246 | if (!ld) | |
a570a49a | 247 | ldsem_up_read(&tty->ldisc_sem); |
a4a3e061 | 248 | return ld; |
01e1abb2 | 249 | } |
01e1abb2 AC |
250 | EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait); |
251 | ||
252 | /** | |
cbb68f91 JS |
253 | * tty_ldisc_ref - get the tty ldisc |
254 | * @tty: tty device | |
01e1abb2 | 255 | * |
cbb68f91 JS |
256 | * Dereference the line discipline for the terminal and take a reference to it. |
257 | * If the line discipline is in flux then return %NULL. Can be called from IRQ | |
258 | * and timer functions. | |
01e1abb2 | 259 | */ |
01e1abb2 AC |
260 | struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty) |
261 | { | |
36697529 PH |
262 | struct tty_ldisc *ld = NULL; |
263 | ||
264 | if (ldsem_down_read_trylock(&tty->ldisc_sem)) { | |
265 | ld = tty->ldisc; | |
266 | if (!ld) | |
267 | ldsem_up_read(&tty->ldisc_sem); | |
268 | } | |
269 | return ld; | |
01e1abb2 | 270 | } |
01e1abb2 AC |
271 | EXPORT_SYMBOL_GPL(tty_ldisc_ref); |
272 | ||
273 | /** | |
cbb68f91 JS |
274 | * tty_ldisc_deref - free a tty ldisc reference |
275 | * @ld: reference to free up | |
01e1abb2 | 276 | * |
cbb68f91 JS |
277 | * Undoes the effect of tty_ldisc_ref() or tty_ldisc_ref_wait(). May be called |
278 | * in IRQ context. | |
01e1abb2 | 279 | */ |
01e1abb2 AC |
280 | void tty_ldisc_deref(struct tty_ldisc *ld) |
281 | { | |
36697529 | 282 | ldsem_up_read(&ld->tty->ldisc_sem); |
01e1abb2 | 283 | } |
01e1abb2 AC |
284 | EXPORT_SYMBOL_GPL(tty_ldisc_deref); |
285 | ||
d2c43890 | 286 | |
c2bb524b | 287 | static inline int |
e80a10ee | 288 | __tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout) |
d2c43890 PH |
289 | { |
290 | return ldsem_down_write(&tty->ldisc_sem, timeout); | |
291 | } | |
292 | ||
c2bb524b | 293 | static inline int |
e80a10ee | 294 | __tty_ldisc_lock_nested(struct tty_struct *tty, unsigned long timeout) |
d2c43890 PH |
295 | { |
296 | return ldsem_down_write_nested(&tty->ldisc_sem, | |
297 | LDISC_SEM_OTHER, timeout); | |
298 | } | |
299 | ||
e80a10ee | 300 | static inline void __tty_ldisc_unlock(struct tty_struct *tty) |
d2c43890 | 301 | { |
52772ea6 | 302 | ldsem_up_write(&tty->ldisc_sem); |
d2c43890 PH |
303 | } |
304 | ||
b027e229 | 305 | int tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout) |
fae76e9a PH |
306 | { |
307 | int ret; | |
308 | ||
c96cf923 DS |
309 | /* Kindly asking blocked readers to release the read side */ |
310 | set_bit(TTY_LDISC_CHANGING, &tty->flags); | |
311 | wake_up_interruptible_all(&tty->read_wait); | |
312 | wake_up_interruptible_all(&tty->write_wait); | |
313 | ||
fae76e9a PH |
314 | ret = __tty_ldisc_lock(tty, timeout); |
315 | if (!ret) | |
316 | return -EBUSY; | |
317 | set_bit(TTY_LDISC_HALTED, &tty->flags); | |
318 | return 0; | |
319 | } | |
320 | ||
b027e229 | 321 | void tty_ldisc_unlock(struct tty_struct *tty) |
fae76e9a PH |
322 | { |
323 | clear_bit(TTY_LDISC_HALTED, &tty->flags); | |
c96cf923 DS |
324 | /* Can be cleared here - ldisc_unlock will wake up writers firstly */ |
325 | clear_bit(TTY_LDISC_CHANGING, &tty->flags); | |
fae76e9a PH |
326 | __tty_ldisc_unlock(tty); |
327 | } | |
328 | ||
c2bb524b | 329 | static int |
d2c43890 PH |
330 | tty_ldisc_lock_pair_timeout(struct tty_struct *tty, struct tty_struct *tty2, |
331 | unsigned long timeout) | |
332 | { | |
333 | int ret; | |
334 | ||
335 | if (tty < tty2) { | |
e80a10ee | 336 | ret = __tty_ldisc_lock(tty, timeout); |
d2c43890 | 337 | if (ret) { |
e80a10ee | 338 | ret = __tty_ldisc_lock_nested(tty2, timeout); |
d2c43890 | 339 | if (!ret) |
e80a10ee | 340 | __tty_ldisc_unlock(tty); |
d2c43890 PH |
341 | } |
342 | } else { | |
343 | /* if this is possible, it has lots of implications */ | |
344 | WARN_ON_ONCE(tty == tty2); | |
345 | if (tty2 && tty != tty2) { | |
e80a10ee | 346 | ret = __tty_ldisc_lock(tty2, timeout); |
d2c43890 | 347 | if (ret) { |
e80a10ee | 348 | ret = __tty_ldisc_lock_nested(tty, timeout); |
d2c43890 | 349 | if (!ret) |
e80a10ee | 350 | __tty_ldisc_unlock(tty2); |
d2c43890 PH |
351 | } |
352 | } else | |
e80a10ee | 353 | ret = __tty_ldisc_lock(tty, timeout); |
d2c43890 PH |
354 | } |
355 | ||
356 | if (!ret) | |
357 | return -EBUSY; | |
358 | ||
359 | set_bit(TTY_LDISC_HALTED, &tty->flags); | |
360 | if (tty2) | |
361 | set_bit(TTY_LDISC_HALTED, &tty2->flags); | |
362 | return 0; | |
363 | } | |
364 | ||
c2bb524b | 365 | static void tty_ldisc_lock_pair(struct tty_struct *tty, struct tty_struct *tty2) |
d2c43890 PH |
366 | { |
367 | tty_ldisc_lock_pair_timeout(tty, tty2, MAX_SCHEDULE_TIMEOUT); | |
368 | } | |
369 | ||
c2bb524b PH |
370 | static void tty_ldisc_unlock_pair(struct tty_struct *tty, |
371 | struct tty_struct *tty2) | |
d2c43890 | 372 | { |
e80a10ee | 373 | __tty_ldisc_unlock(tty); |
d2c43890 | 374 | if (tty2) |
e80a10ee | 375 | __tty_ldisc_unlock(tty2); |
d2c43890 PH |
376 | } |
377 | ||
f2c4c65c | 378 | /** |
cbb68f91 JS |
379 | * tty_ldisc_flush - flush line discipline queue |
380 | * @tty: tty to flush ldisc for | |
f2c4c65c | 381 | * |
cbb68f91 JS |
382 | * Flush the line discipline queue (if any) and the tty flip buffers for this |
383 | * @tty. | |
f2c4c65c | 384 | */ |
f2c4c65c AC |
385 | void tty_ldisc_flush(struct tty_struct *tty) |
386 | { | |
387 | struct tty_ldisc *ld = tty_ldisc_ref(tty); | |
86c80a8e PH |
388 | |
389 | tty_buffer_flush(tty, ld); | |
390 | if (ld) | |
f2c4c65c | 391 | tty_ldisc_deref(ld); |
f2c4c65c | 392 | } |
f2c4c65c AC |
393 | EXPORT_SYMBOL_GPL(tty_ldisc_flush); |
394 | ||
01e1abb2 | 395 | /** |
cbb68f91 JS |
396 | * tty_set_termios_ldisc - set ldisc field |
397 | * @tty: tty structure | |
398 | * @disc: line discipline number | |
01e1abb2 | 399 | * |
cbb68f91 JS |
400 | * This is probably overkill for real world processors but they are not on hot |
401 | * paths so a little discipline won't do any harm. | |
01e1abb2 | 402 | * |
cbb68f91 JS |
403 | * The line discipline-related tty_struct fields are reset to prevent the ldisc |
404 | * driver from re-using stale information for the new ldisc instance. | |
dd42bf11 | 405 | * |
cbb68f91 | 406 | * Locking: takes termios_rwsem |
01e1abb2 | 407 | */ |
c12da96f | 408 | static void tty_set_termios_ldisc(struct tty_struct *tty, int disc) |
01e1abb2 | 409 | { |
6a1c0680 | 410 | down_write(&tty->termios_rwsem); |
c12da96f | 411 | tty->termios.c_line = disc; |
6a1c0680 | 412 | up_write(&tty->termios_rwsem); |
dd42bf11 PH |
413 | |
414 | tty->disc_data = NULL; | |
415 | tty->receive_room = 0; | |
01e1abb2 AC |
416 | } |
417 | ||
c65c9bc3 | 418 | /** |
cbb68f91 JS |
419 | * tty_ldisc_open - open a line discipline |
420 | * @tty: tty we are opening the ldisc on | |
421 | * @ld: discipline to open | |
c65c9bc3 | 422 | * |
cbb68f91 | 423 | * A helper opening method. Also a convenient debugging and check point. |
ec79d605 | 424 | * |
cbb68f91 | 425 | * Locking: always called with BTM already held. |
c65c9bc3 | 426 | */ |
c65c9bc3 AC |
427 | static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld) |
428 | { | |
429 | WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags)); | |
f18f9498 AC |
430 | if (ld->ops->open) { |
431 | int ret; | |
5d3945e8 | 432 | /* BTM here locks versus a hangup event */ |
f18f9498 | 433 | ret = ld->ops->open(tty); |
7f90cfc5 JS |
434 | if (ret) |
435 | clear_bit(TTY_LDISC_OPEN, &tty->flags); | |
fb6edc91 | 436 | |
a570a49a | 437 | tty_ldisc_debug(tty, "%p: opened\n", ld); |
f18f9498 AC |
438 | return ret; |
439 | } | |
c65c9bc3 AC |
440 | return 0; |
441 | } | |
442 | ||
443 | /** | |
cbb68f91 JS |
444 | * tty_ldisc_close - close a line discipline |
445 | * @tty: tty we are opening the ldisc on | |
446 | * @ld: discipline to close | |
c65c9bc3 | 447 | * |
cbb68f91 | 448 | * A helper close method. Also a convenient debugging and check point. |
c65c9bc3 | 449 | */ |
c65c9bc3 AC |
450 | static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld) |
451 | { | |
9ffbe8ac | 452 | lockdep_assert_held_write(&tty->ldisc_sem); |
c65c9bc3 AC |
453 | WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags)); |
454 | clear_bit(TTY_LDISC_OPEN, &tty->flags); | |
455 | if (ld->ops->close) | |
456 | ld->ops->close(tty); | |
a570a49a | 457 | tty_ldisc_debug(tty, "%p: closed\n", ld); |
c65c9bc3 | 458 | } |
01e1abb2 | 459 | |
8a8dabf2 | 460 | /** |
cbb68f91 JS |
461 | * tty_ldisc_failto - helper for ldisc failback |
462 | * @tty: tty to open the ldisc on | |
463 | * @ld: ldisc we are trying to fail back to | |
8a8dabf2 | 464 | * |
cbb68f91 JS |
465 | * Helper to try and recover a tty when switching back to the old ldisc fails |
466 | * and we need something attached. | |
8a8dabf2 | 467 | */ |
8a8dabf2 AC |
468 | static int tty_ldisc_failto(struct tty_struct *tty, int ld) |
469 | { | |
470 | struct tty_ldisc *disc = tty_ldisc_get(tty, ld); | |
471 | int r; | |
472 | ||
9ffbe8ac | 473 | lockdep_assert_held_write(&tty->ldisc_sem); |
8a8dabf2 AC |
474 | if (IS_ERR(disc)) |
475 | return PTR_ERR(disc); | |
476 | tty->ldisc = disc; | |
477 | tty_set_termios_ldisc(tty, ld); | |
408795b0 XT |
478 | r = tty_ldisc_open(tty, disc); |
479 | if (r < 0) | |
8a8dabf2 AC |
480 | tty_ldisc_put(disc); |
481 | return r; | |
482 | } | |
483 | ||
a8983d01 | 484 | /** |
cbb68f91 JS |
485 | * tty_ldisc_restore - helper for tty ldisc change |
486 | * @tty: tty to recover | |
487 | * @old: previous ldisc | |
a8983d01 | 488 | * |
cbb68f91 JS |
489 | * Restore the previous line discipline or %N_TTY when a line discipline change |
490 | * fails due to an open error | |
a8983d01 | 491 | */ |
a8983d01 GKH |
492 | static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old) |
493 | { | |
a8983d01 | 494 | /* There is an outstanding reference here so this is safe */ |
598c2d41 TH |
495 | if (tty_ldisc_failto(tty, old->ops->num) < 0) { |
496 | const char *name = tty_name(tty); | |
497 | ||
498 | pr_warn("Falling back ldisc for %s.\n", name); | |
72a8dcd7 XT |
499 | /* |
500 | * The traditional behaviour is to fall back to N_TTY, we | |
501 | * want to avoid falling back to N_NULL unless we have no | |
502 | * choice to avoid the risk of breaking anything | |
503 | */ | |
8a8dabf2 AC |
504 | if (tty_ldisc_failto(tty, N_TTY) < 0 && |
505 | tty_ldisc_failto(tty, N_NULL) < 0) | |
598c2d41 | 506 | panic("Couldn't open N_NULL ldisc for %s.", name); |
a8983d01 GKH |
507 | } |
508 | } | |
509 | ||
01e1abb2 | 510 | /** |
cbb68f91 JS |
511 | * tty_set_ldisc - set line discipline |
512 | * @tty: the terminal to set | |
513 | * @disc: the line discipline number | |
514 | * | |
515 | * Set the discipline of a tty line. Must be called from a process context. The | |
516 | * ldisc change logic has to protect itself against any overlapping ldisc | |
517 | * change (including on the other end of pty pairs), the close of one side of a | |
518 | * tty/pty pair, and eventually hangup. | |
01e1abb2 | 519 | */ |
c12da96f | 520 | int tty_set_ldisc(struct tty_struct *tty, int disc) |
01e1abb2 | 521 | { |
a8983d01 GKH |
522 | int retval; |
523 | struct tty_ldisc *old_ldisc, *new_ldisc; | |
524 | ||
525 | new_ldisc = tty_ldisc_get(tty, disc); | |
526 | if (IS_ERR(new_ldisc)) | |
527 | return PTR_ERR(new_ldisc); | |
01e1abb2 | 528 | |
c8483bc9 | 529 | tty_lock(tty); |
276a661a | 530 | retval = tty_ldisc_lock(tty, 5 * HZ); |
63d8cb3f PH |
531 | if (retval) |
532 | goto err; | |
01e1abb2 | 533 | |
a570a49a PH |
534 | if (!tty->ldisc) { |
535 | retval = -EIO; | |
536 | goto out; | |
537 | } | |
538 | ||
63d8cb3f | 539 | /* Check the no-op case */ |
a8983d01 | 540 | if (tty->ldisc->ops->num == disc) |
63d8cb3f | 541 | goto out; |
c65c9bc3 | 542 | |
63d8cb3f PH |
543 | if (test_bit(TTY_HUPPED, &tty->flags)) { |
544 | /* We were raced by hangup */ | |
545 | retval = -EIO; | |
546 | goto out; | |
01e1abb2 AC |
547 | } |
548 | ||
a8983d01 GKH |
549 | old_ldisc = tty->ldisc; |
550 | ||
551 | /* Shutdown the old discipline. */ | |
552 | tty_ldisc_close(tty, old_ldisc); | |
553 | ||
554 | /* Now set up the new line discipline. */ | |
555 | tty->ldisc = new_ldisc; | |
556 | tty_set_termios_ldisc(tty, disc); | |
557 | ||
558 | retval = tty_ldisc_open(tty, new_ldisc); | |
01e1abb2 | 559 | if (retval < 0) { |
c65c9bc3 | 560 | /* Back to the old one or N_TTY if we can't */ |
a8983d01 GKH |
561 | tty_ldisc_put(new_ldisc); |
562 | tty_ldisc_restore(tty, old_ldisc); | |
01e1abb2 | 563 | } |
c65c9bc3 | 564 | |
a8983d01 | 565 | if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc) { |
9191aaaa | 566 | down_read(&tty->termios_rwsem); |
01e1abb2 | 567 | tty->ops->set_ldisc(tty); |
9191aaaa PH |
568 | up_read(&tty->termios_rwsem); |
569 | } | |
01e1abb2 | 570 | |
72a8dcd7 XT |
571 | /* |
572 | * At this point we hold a reference to the new ldisc and a | |
573 | * reference to the old ldisc, or we hold two references to | |
574 | * the old ldisc (if it was restored as part of error cleanup | |
575 | * above). In either case, releasing a single reference from | |
576 | * the old ldisc is correct. | |
577 | */ | |
a8983d01 | 578 | new_ldisc = old_ldisc; |
63d8cb3f | 579 | out: |
276a661a | 580 | tty_ldisc_unlock(tty); |
01e1abb2 | 581 | |
72a8dcd7 XT |
582 | /* |
583 | * Restart the work queue in case no characters kick it off. Safe if | |
584 | * already running | |
585 | */ | |
17a69219 | 586 | tty_buffer_restart_work(tty->port); |
63d8cb3f | 587 | err: |
a8983d01 | 588 | tty_ldisc_put(new_ldisc); /* drop the extra reference */ |
89c8d91e | 589 | tty_unlock(tty); |
01e1abb2 AC |
590 | return retval; |
591 | } | |
1ab92da3 | 592 | EXPORT_SYMBOL_GPL(tty_set_ldisc); |
01e1abb2 | 593 | |
6ffeb4b2 | 594 | /** |
cbb68f91 JS |
595 | * tty_ldisc_kill - teardown ldisc |
596 | * @tty: tty being released | |
6ffeb4b2 | 597 | * |
cbb68f91 | 598 | * Perform final close of the ldisc and reset @tty->ldisc |
6ffeb4b2 PH |
599 | */ |
600 | static void tty_ldisc_kill(struct tty_struct *tty) | |
601 | { | |
9ffbe8ac | 602 | lockdep_assert_held_write(&tty->ldisc_sem); |
6ffeb4b2 PH |
603 | if (!tty->ldisc) |
604 | return; | |
605 | /* | |
606 | * Now kill off the ldisc | |
607 | */ | |
608 | tty_ldisc_close(tty, tty->ldisc); | |
609 | tty_ldisc_put(tty->ldisc); | |
610 | /* Force an oops if we mess this up */ | |
611 | tty->ldisc = NULL; | |
612 | } | |
613 | ||
c65c9bc3 | 614 | /** |
cbb68f91 JS |
615 | * tty_reset_termios - reset terminal state |
616 | * @tty: tty to reset | |
c65c9bc3 | 617 | * |
cbb68f91 | 618 | * Restore a terminal to the driver default state. |
c65c9bc3 | 619 | */ |
c65c9bc3 AC |
620 | static void tty_reset_termios(struct tty_struct *tty) |
621 | { | |
6a1c0680 | 622 | down_write(&tty->termios_rwsem); |
adc8d746 AC |
623 | tty->termios = tty->driver->init_termios; |
624 | tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios); | |
625 | tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios); | |
6a1c0680 | 626 | up_write(&tty->termios_rwsem); |
c65c9bc3 AC |
627 | } |
628 | ||
629 | ||
630 | /** | |
cbb68f91 JS |
631 | * tty_ldisc_reinit - reinitialise the tty ldisc |
632 | * @tty: tty to reinit | |
633 | * @disc: line discipline to reinitialize | |
c65c9bc3 | 634 | * |
cbb68f91 JS |
635 | * Completely reinitialize the line discipline state, by closing the current |
636 | * instance, if there is one, and opening a new instance. If an error occurs | |
637 | * opening the new non-%N_TTY instance, the instance is dropped and @tty->ldisc | |
638 | * reset to %NULL. The caller can then retry with %N_TTY instead. | |
7896f30d | 639 | * |
cbb68f91 | 640 | * Returns: 0 if successful, otherwise error code < 0 |
c65c9bc3 | 641 | */ |
892d1fa7 | 642 | int tty_ldisc_reinit(struct tty_struct *tty, int disc) |
c65c9bc3 | 643 | { |
7896f30d PH |
644 | struct tty_ldisc *ld; |
645 | int retval; | |
1c95ba1e | 646 | |
9ffbe8ac | 647 | lockdep_assert_held_write(&tty->ldisc_sem); |
7896f30d | 648 | ld = tty_ldisc_get(tty, disc); |
a8983d01 GKH |
649 | if (IS_ERR(ld)) { |
650 | BUG_ON(disc == N_TTY); | |
7896f30d | 651 | return PTR_ERR(ld); |
a8983d01 | 652 | } |
c65c9bc3 | 653 | |
7896f30d PH |
654 | if (tty->ldisc) { |
655 | tty_ldisc_close(tty, tty->ldisc); | |
656 | tty_ldisc_put(tty->ldisc); | |
657 | } | |
658 | ||
659 | /* switch the line discipline */ | |
f4807045 | 660 | tty->ldisc = ld; |
c12da96f | 661 | tty_set_termios_ldisc(tty, disc); |
7896f30d PH |
662 | retval = tty_ldisc_open(tty, tty->ldisc); |
663 | if (retval) { | |
e65c62b1 JW |
664 | tty_ldisc_put(tty->ldisc); |
665 | tty->ldisc = NULL; | |
7896f30d PH |
666 | } |
667 | return retval; | |
c65c9bc3 AC |
668 | } |
669 | ||
670 | /** | |
cbb68f91 JS |
671 | * tty_ldisc_hangup - hangup ldisc reset |
672 | * @tty: tty being hung up | |
673 | * @reinit: whether to re-initialise the tty | |
c65c9bc3 | 674 | * |
cbb68f91 JS |
675 | * Some tty devices reset their termios when they receive a hangup event. In |
676 | * that situation we must also switch back to %N_TTY properly before we reset | |
677 | * the termios data. | |
c65c9bc3 | 678 | * |
cbb68f91 JS |
679 | * Locking: We can take the ldisc mutex as the rest of the code is careful to |
680 | * allow for this. | |
c65c9bc3 | 681 | * |
cbb68f91 JS |
682 | * In the pty pair case this occurs in the close() path of the tty itself so we |
683 | * must be careful about locking rules. | |
c65c9bc3 | 684 | */ |
892d1fa7 | 685 | void tty_ldisc_hangup(struct tty_struct *tty, bool reinit) |
c65c9bc3 AC |
686 | { |
687 | struct tty_ldisc *ld; | |
688 | ||
a570a49a | 689 | tty_ldisc_debug(tty, "%p: hangup\n", tty->ldisc); |
fc575ee6 | 690 | |
c65c9bc3 AC |
691 | ld = tty_ldisc_ref(tty); |
692 | if (ld != NULL) { | |
c65c9bc3 AC |
693 | if (ld->ops->flush_buffer) |
694 | ld->ops->flush_buffer(tty); | |
695 | tty_driver_flush_buffer(tty); | |
696 | if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) && | |
697 | ld->ops->write_wakeup) | |
698 | ld->ops->write_wakeup(tty); | |
699 | if (ld->ops->hangup) | |
700 | ld->ops->hangup(tty); | |
701 | tty_ldisc_deref(ld); | |
702 | } | |
36697529 | 703 | |
a9a08845 LT |
704 | wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT); |
705 | wake_up_interruptible_poll(&tty->read_wait, EPOLLIN); | |
36697529 | 706 | |
c65c9bc3 AC |
707 | /* |
708 | * Shutdown the current line discipline, and reset it to | |
638b9648 AC |
709 | * N_TTY if need be. |
710 | * | |
711 | * Avoid racing set_ldisc or tty_ldisc_release | |
c65c9bc3 | 712 | */ |
fae76e9a | 713 | tty_ldisc_lock(tty, MAX_SCHEDULE_TIMEOUT); |
60af22d2 | 714 | |
892d1fa7 PH |
715 | if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) |
716 | tty_reset_termios(tty); | |
c8785241 | 717 | |
892d1fa7 PH |
718 | if (tty->ldisc) { |
719 | if (reinit) { | |
e65c62b1 JW |
720 | if (tty_ldisc_reinit(tty, tty->termios.c_line) < 0 && |
721 | tty_ldisc_reinit(tty, N_TTY) < 0) | |
722 | WARN_ON(tty_ldisc_reinit(tty, N_NULL) < 0); | |
892d1fa7 PH |
723 | } else |
724 | tty_ldisc_kill(tty); | |
c65c9bc3 | 725 | } |
fae76e9a | 726 | tty_ldisc_unlock(tty); |
c65c9bc3 | 727 | } |
01e1abb2 AC |
728 | |
729 | /** | |
cbb68f91 JS |
730 | * tty_ldisc_setup - open line discipline |
731 | * @tty: tty being shut down | |
732 | * @o_tty: pair tty for pty/tty pairs | |
01e1abb2 | 733 | * |
cbb68f91 JS |
734 | * Called during the initial open of a tty/pty pair in order to set up the line |
735 | * disciplines and bind them to the @tty. This has no locking issues as the | |
736 | * device isn't yet active. | |
01e1abb2 | 737 | */ |
01e1abb2 AC |
738 | int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty) |
739 | { | |
9de2a7ce | 740 | int retval = tty_ldisc_open(tty, tty->ldisc); |
d7238359 | 741 | |
c65c9bc3 AC |
742 | if (retval) |
743 | return retval; | |
744 | ||
745 | if (o_tty) { | |
110b8928 DS |
746 | /* |
747 | * Called without o_tty->ldisc_sem held, as o_tty has been | |
748 | * just allocated and no one has a reference to it. | |
749 | */ | |
c65c9bc3 | 750 | retval = tty_ldisc_open(o_tty, o_tty->ldisc); |
01e1abb2 | 751 | if (retval) { |
9de2a7ce | 752 | tty_ldisc_close(tty, tty->ldisc); |
01e1abb2 AC |
753 | return retval; |
754 | } | |
01e1abb2 | 755 | } |
01e1abb2 AC |
756 | return 0; |
757 | } | |
89c8d91e | 758 | |
01e1abb2 | 759 | /** |
cbb68f91 JS |
760 | * tty_ldisc_release - release line discipline |
761 | * @tty: tty being shut down (or one end of pty pair) | |
3ee175d9 | 762 | * |
cbb68f91 JS |
763 | * Called during the final close of a tty or a pty pair in order to shut down |
764 | * the line discpline layer. On exit, each tty's ldisc is %NULL. | |
01e1abb2 | 765 | */ |
62462aef | 766 | void tty_ldisc_release(struct tty_struct *tty) |
01e1abb2 | 767 | { |
62462aef PH |
768 | struct tty_struct *o_tty = tty->link; |
769 | ||
01e1abb2 | 770 | /* |
a2965b7b PH |
771 | * Shutdown this line discipline. As this is the final close, |
772 | * it does not race with the set_ldisc code path. | |
01e1abb2 | 773 | */ |
01e1abb2 | 774 | |
36697529 | 775 | tty_ldisc_lock_pair(tty, o_tty); |
89c8d91e | 776 | tty_ldisc_kill(tty); |
c65c9bc3 | 777 | if (o_tty) |
89c8d91e | 778 | tty_ldisc_kill(o_tty); |
36697529 PH |
779 | tty_ldisc_unlock_pair(tty, o_tty); |
780 | ||
72a8dcd7 XT |
781 | /* |
782 | * And the memory resources remaining (buffers, termios) will be | |
783 | * disposed of when the kref hits zero | |
784 | */ | |
fc575ee6 | 785 | |
fb6edc91 | 786 | tty_ldisc_debug(tty, "released\n"); |
01e1abb2 AC |
787 | } |
788 | ||
789 | /** | |
cbb68f91 JS |
790 | * tty_ldisc_init - ldisc setup for new tty |
791 | * @tty: tty being allocated | |
01e1abb2 | 792 | * |
cbb68f91 JS |
793 | * Set up the line discipline objects for a newly allocated tty. Note that the |
794 | * tty structure is not completely set up when this call is made. | |
01e1abb2 | 795 | */ |
903f9db1 | 796 | int tty_ldisc_init(struct tty_struct *tty) |
01e1abb2 | 797 | { |
36697529 | 798 | struct tty_ldisc *ld = tty_ldisc_get(tty, N_TTY); |
d7238359 | 799 | |
c65c9bc3 | 800 | if (IS_ERR(ld)) |
903f9db1 | 801 | return PTR_ERR(ld); |
f4807045 | 802 | tty->ldisc = ld; |
903f9db1 | 803 | return 0; |
01e1abb2 AC |
804 | } |
805 | ||
6716671d | 806 | /** |
cbb68f91 JS |
807 | * tty_ldisc_deinit - ldisc cleanup for new tty |
808 | * @tty: tty that was allocated recently | |
6716671d | 809 | * |
cbb68f91 JS |
810 | * The tty structure must not be completely set up (tty_ldisc_setup()) when |
811 | * this call is made. | |
6716671d JS |
812 | */ |
813 | void tty_ldisc_deinit(struct tty_struct *tty) | |
814 | { | |
110b8928 | 815 | /* no ldisc_sem, tty is being destroyed */ |
c8b710b3 PH |
816 | if (tty->ldisc) |
817 | tty_ldisc_put(tty->ldisc); | |
f4807045 | 818 | tty->ldisc = NULL; |
6716671d | 819 | } |