blob: 756a4bfa6a693049c78697a74214c797b979832e [file] [log] [blame]
Greg Kroah-Hartmane3b3d0f2017-11-06 18:11:51 +01001// SPDX-License-Identifier: GPL-2.0
Alan Cox01e1abb2008-07-22 11:16:55 +01002#include <linux/types.h>
Alan Cox01e1abb2008-07-22 11:16:55 +01003#include <linux/errno.h>
Jiri Slaby8b3ffa12011-11-16 16:27:10 +01004#include <linux/kmod.h>
Alan Cox01e1abb2008-07-22 11:16:55 +01005#include <linux/sched.h>
6#include <linux/interrupt.h>
7#include <linux/tty.h>
8#include <linux/tty_driver.h>
Alan Cox01e1abb2008-07-22 11:16:55 +01009#include <linux/file.h>
Alan Cox01e1abb2008-07-22 11:16:55 +010010#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>
Alan Cox01e1abb2008-07-22 11:16:55 +010015#include <linux/module.h>
Alan Cox01e1abb2008-07-22 11:16:55 +010016#include <linux/device.h>
17#include <linux/wait.h>
18#include <linux/bitops.h>
Alan Cox01e1abb2008-07-22 11:16:55 +010019#include <linux/seq_file.h>
Alan Cox01e1abb2008-07-22 11:16:55 +010020#include <linux/uaccess.h>
Jiri Slaby0c73c082011-11-16 16:27:09 +010021#include <linux/ratelimit.h>
Greg Kroah-Hartman98602c02021-04-08 14:51:22 +020022#include "tty.h"
Alan Cox01e1abb2008-07-22 11:16:55 +010023
Peter Hurleyfc575ee2013-03-11 16:44:38 -040024#undef LDISC_DEBUG_HANGUP
25
26#ifdef LDISC_DEBUG_HANGUP
Peter Hurley0a6adc12015-07-12 22:49:10 -040027#define tty_ldisc_debug(tty, f, args...) tty_debug(tty, f, ##args)
Peter Hurleyfc575ee2013-03-11 16:44:38 -040028#else
29#define tty_ldisc_debug(tty, f, args...)
30#endif
31
Peter Hurleyd2c43892013-06-15 07:04:47 -040032/* lockdep nested classes for tty->ldisc_sem */
33enum {
34 LDISC_SEM_NORMAL,
35 LDISC_SEM_OTHER,
36};
37
38
Alan Cox01e1abb2008-07-22 11:16:55 +010039/*
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
Peter Hurley137084b2013-06-15 07:04:46 -040045static DEFINE_RAW_SPINLOCK(tty_ldiscs_lock);
Alan Cox01e1abb2008-07-22 11:16:55 +010046/* Line disc dispatch table */
47static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
48
49/**
50 * tty_register_ldisc - install a line discipline
Alan Cox01e1abb2008-07-22 11:16:55 +010051 * @new_ldisc: pointer to the ldisc object
52 *
53 * Installs a new line discipline into the kernel. The discipline
54 * is set up as unreferenced and then made available to the kernel
55 * from this point onwards.
56 *
57 * Locking:
Peter Hurley137084b2013-06-15 07:04:46 -040058 * takes tty_ldiscs_lock to guard against ldisc races
Alan Cox01e1abb2008-07-22 11:16:55 +010059 */
60
Jiri Slabyfbadf70a2021-05-05 11:19:07 +020061int tty_register_ldisc(struct tty_ldisc_ops *new_ldisc)
Alan Cox01e1abb2008-07-22 11:16:55 +010062{
63 unsigned long flags;
64 int ret = 0;
65
Jiri Slabyfbadf70a2021-05-05 11:19:07 +020066 if (new_ldisc->num < N_TTY || new_ldisc->num >= NR_LDISCS)
Alan Cox01e1abb2008-07-22 11:16:55 +010067 return -EINVAL;
68
Peter Hurley137084b2013-06-15 07:04:46 -040069 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
Jiri Slabyfbadf70a2021-05-05 11:19:07 +020070 tty_ldiscs[new_ldisc->num] = new_ldisc;
Peter Hurley137084b2013-06-15 07:04:46 -040071 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
Alan Cox01e1abb2008-07-22 11:16:55 +010072
73 return ret;
74}
75EXPORT_SYMBOL(tty_register_ldisc);
76
77/**
78 * tty_unregister_ldisc - unload a line discipline
Baokun Lie2129552021-06-08 10:48:43 +080079 * @ldisc: ldisc number
Alan Cox01e1abb2008-07-22 11:16:55 +010080 *
81 * Remove a line discipline from the kernel providing it is not
82 * currently in use.
83 *
84 * Locking:
Peter Hurley137084b2013-06-15 07:04:46 -040085 * takes tty_ldiscs_lock to guard against ldisc races
Alan Cox01e1abb2008-07-22 11:16:55 +010086 */
87
Jiri Slabyf6f19592021-05-05 11:19:12 +020088void tty_unregister_ldisc(struct tty_ldisc_ops *ldisc)
Alan Cox01e1abb2008-07-22 11:16:55 +010089{
90 unsigned long flags;
Alan Cox01e1abb2008-07-22 11:16:55 +010091
Peter Hurley137084b2013-06-15 07:04:46 -040092 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
Jiri Slaby19475202021-05-05 11:19:10 +020093 tty_ldiscs[ldisc->num] = NULL;
Peter Hurley137084b2013-06-15 07:04:46 -040094 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
Alan Cox01e1abb2008-07-22 11:16:55 +010095}
96EXPORT_SYMBOL(tty_unregister_ldisc);
97
Linus Torvaldsf0de0e82009-08-03 16:00:15 -070098static struct tty_ldisc_ops *get_ldops(int disc)
99{
100 unsigned long flags;
101 struct tty_ldisc_ops *ldops, *ret;
102
Peter Hurley137084b2013-06-15 07:04:46 -0400103 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700104 ret = ERR_PTR(-EINVAL);
105 ldops = tty_ldiscs[disc];
106 if (ldops) {
107 ret = ERR_PTR(-EAGAIN);
Jiri Slaby19475202021-05-05 11:19:10 +0200108 if (try_module_get(ldops->owner))
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700109 ret = ldops;
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700110 }
Peter Hurley137084b2013-06-15 07:04:46 -0400111 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700112 return ret;
113}
114
115static void put_ldops(struct tty_ldisc_ops *ldops)
116{
117 unsigned long flags;
118
Peter Hurley137084b2013-06-15 07:04:46 -0400119 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700120 module_put(ldops->owner);
Peter Hurley137084b2013-06-15 07:04:46 -0400121 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700122}
Alan Cox01e1abb2008-07-22 11:16:55 +0100123
Lee Jones8eddcca2020-11-12 10:58:54 +0000124static int tty_ldisc_autoload = IS_BUILTIN(CONFIG_LDISC_AUTOLOAD);
Alan Cox01e1abb2008-07-22 11:16:55 +0100125/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100126 * tty_ldisc_get - take a reference to an ldisc
Lee Jones8a3bdec2020-11-04 19:35:42 +0000127 * @tty: tty device
Alan Cox01e1abb2008-07-22 11:16:55 +0100128 * @disc: ldisc number
Alan Cox01e1abb2008-07-22 11:16:55 +0100129 *
130 * Takes a reference to a line discipline. Deals with refcounts and
Peter Hurleyc0cc1c52016-01-10 22:40:59 -0800131 * module locking counts.
132 *
133 * Returns: -EINVAL if the discipline index is not [N_TTY..NR_LDISCS] or
134 * if the discipline is not registered
135 * -EAGAIN if request_module() failed to load or register the
Xiaofei Tanb8958542021-04-07 15:06:50 +0800136 * discipline
Peter Hurleyc0cc1c52016-01-10 22:40:59 -0800137 * -ENOMEM if allocation failure
138 *
139 * Otherwise, returns a pointer to the discipline and bumps the
140 * ref count
Alan Cox01e1abb2008-07-22 11:16:55 +0100141 *
142 * Locking:
Peter Hurley137084b2013-06-15 07:04:46 -0400143 * takes tty_ldiscs_lock to guard against ldisc races
Alan Cox01e1abb2008-07-22 11:16:55 +0100144 */
145
Peter Hurley36697522013-06-15 07:04:48 -0400146static struct tty_ldisc *tty_ldisc_get(struct tty_struct *tty, int disc)
Alan Cox01e1abb2008-07-22 11:16:55 +0100147{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100148 struct tty_ldisc *ld;
Linus Torvalds182274f2009-08-03 16:01:28 -0700149 struct tty_ldisc_ops *ldops;
Alan Cox01e1abb2008-07-22 11:16:55 +0100150
151 if (disc < N_TTY || disc >= NR_LDISCS)
Alan Coxc65c9bc2009-06-11 12:50:12 +0100152 return ERR_PTR(-EINVAL);
Linus Torvalds182274f2009-08-03 16:01:28 -0700153
154 /*
155 * Get the ldisc ops - we may need to request them to be loaded
156 * dynamically and try again.
157 */
158 ldops = get_ldops(disc);
159 if (IS_ERR(ldops)) {
Greg Kroah-Hartman7c0cca72019-01-21 17:26:42 +0100160 if (!capable(CAP_SYS_MODULE) && !tty_ldisc_autoload)
161 return ERR_PTR(-EPERM);
Alan Cox01e1abb2008-07-22 11:16:55 +0100162 request_module("tty-ldisc-%d", disc);
Linus Torvalds182274f2009-08-03 16:01:28 -0700163 ldops = get_ldops(disc);
164 if (IS_ERR(ldops))
165 return ERR_CAST(ldops);
Alan Cox01e1abb2008-07-22 11:16:55 +0100166 }
Linus Torvalds182274f2009-08-03 16:01:28 -0700167
Tetsuo Handabcdd0ca2018-04-25 20:12:31 +0900168 /*
169 * There is no way to handle allocation failure of only 16 bytes.
170 * Let's simplify error handling and save more memory.
171 */
172 ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL | __GFP_NOFAIL);
Linus Torvalds182274f2009-08-03 16:01:28 -0700173 ld->ops = ldops;
Peter Hurley36697522013-06-15 07:04:48 -0400174 ld->tty = tty;
Ivo Sieben1541f842012-05-03 14:37:43 +0200175
Alan Coxc65c9bc2009-06-11 12:50:12 +0100176 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100177}
178
Lee Jones8eddcca2020-11-12 10:58:54 +0000179/*
Peter Hurley734de242013-03-11 16:44:43 -0400180 * tty_ldisc_put - release the ldisc
181 *
182 * Complement of tty_ldisc_get().
183 */
Denys Vlasenkocb128f62015-10-27 17:40:02 +0100184static void tty_ldisc_put(struct tty_ldisc *ld)
Peter Hurley734de242013-03-11 16:44:43 -0400185{
Peter Hurley734de242013-03-11 16:44:43 -0400186 if (WARN_ON_ONCE(!ld))
187 return;
188
Peter Hurley36697522013-06-15 07:04:48 -0400189 put_ldops(ld->ops);
Peter Hurley734de242013-03-11 16:44:43 -0400190 kfree(ld);
Peter Hurley734de242013-03-11 16:44:43 -0400191}
192
Alan Cox852e99d2009-06-11 12:51:41 +0100193static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
Alan Cox01e1abb2008-07-22 11:16:55 +0100194{
195 return (*pos < NR_LDISCS) ? pos : NULL;
196}
197
Alan Cox852e99d2009-06-11 12:51:41 +0100198static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
Alan Cox01e1abb2008-07-22 11:16:55 +0100199{
200 (*pos)++;
201 return (*pos < NR_LDISCS) ? pos : NULL;
202}
203
204static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
205{
206}
207
208static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
209{
210 int i = *(loff_t *)v;
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700211 struct tty_ldisc_ops *ldops;
Alan Cox852e99d2009-06-11 12:51:41 +0100212
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700213 ldops = get_ldops(i);
214 if (IS_ERR(ldops))
Alan Cox01e1abb2008-07-22 11:16:55 +0100215 return 0;
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700216 seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
217 put_ldops(ldops);
Alan Cox01e1abb2008-07-22 11:16:55 +0100218 return 0;
219}
220
Christoph Hellwigfddda2b2018-04-13 19:44:18 +0200221const struct seq_operations tty_ldiscs_seq_ops = {
Alan Cox01e1abb2008-07-22 11:16:55 +0100222 .start = tty_ldiscs_seq_start,
223 .next = tty_ldiscs_seq_next,
224 .stop = tty_ldiscs_seq_stop,
225 .show = tty_ldiscs_seq_show,
226};
227
Alan Cox01e1abb2008-07-22 11:16:55 +0100228/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100229 * tty_ldisc_ref_wait - wait for the tty ldisc
230 * @tty: tty device
231 *
232 * Dereference the line discipline for the terminal and take a
233 * reference to it. If the line discipline is in flux then
234 * wait patiently until it changes.
235 *
Peter Hurley892d1fa2016-01-10 22:41:06 -0800236 * Returns: NULL if the tty has been hungup and not re-opened with
237 * a new file descriptor, otherwise valid ldisc reference
238 *
Lee Jones8eddcca2020-11-12 10:58:54 +0000239 * Note 1: Must not be called from an IRQ/timer context. The caller
Alan Cox01e1abb2008-07-22 11:16:55 +0100240 * must also be careful not to hold other locks that will deadlock
241 * against a discipline change, such as an existing ldisc reference
242 * (which we check for)
243 *
Lee Jones8eddcca2020-11-12 10:58:54 +0000244 * Note 2: a file_operations routine (read/poll/write) should use this
Peter Hurleye55afd12016-01-10 22:41:01 -0800245 * function to wait for any ldisc lifetime events to finish.
Alan Cox01e1abb2008-07-22 11:16:55 +0100246 */
247
248struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
249{
Dmitry Vyukova4a3e062017-03-04 13:46:12 +0100250 struct tty_ldisc *ld;
251
Peter Hurley36697522013-06-15 07:04:48 -0400252 ldsem_down_read(&tty->ldisc_sem, MAX_SCHEDULE_TIMEOUT);
Dmitry Vyukova4a3e062017-03-04 13:46:12 +0100253 ld = tty->ldisc;
254 if (!ld)
Peter Hurleya570a492016-01-10 22:41:02 -0800255 ldsem_up_read(&tty->ldisc_sem);
Dmitry Vyukova4a3e062017-03-04 13:46:12 +0100256 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100257}
Alan Cox01e1abb2008-07-22 11:16:55 +0100258EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
259
260/**
261 * tty_ldisc_ref - get the tty ldisc
262 * @tty: tty device
263 *
264 * Dereference the line discipline for the terminal and take a
265 * reference to it. If the line discipline is in flux then
266 * return NULL. Can be called from IRQ and timer functions.
Alan Cox01e1abb2008-07-22 11:16:55 +0100267 */
268
269struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
270{
Peter Hurley36697522013-06-15 07:04:48 -0400271 struct tty_ldisc *ld = NULL;
272
273 if (ldsem_down_read_trylock(&tty->ldisc_sem)) {
274 ld = tty->ldisc;
275 if (!ld)
276 ldsem_up_read(&tty->ldisc_sem);
277 }
278 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100279}
Alan Cox01e1abb2008-07-22 11:16:55 +0100280EXPORT_SYMBOL_GPL(tty_ldisc_ref);
281
282/**
283 * tty_ldisc_deref - free a tty ldisc reference
284 * @ld: reference to free up
285 *
286 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
287 * be called in IRQ context.
Alan Cox01e1abb2008-07-22 11:16:55 +0100288 */
289
290void tty_ldisc_deref(struct tty_ldisc *ld)
291{
Peter Hurley36697522013-06-15 07:04:48 -0400292 ldsem_up_read(&ld->tty->ldisc_sem);
Alan Cox01e1abb2008-07-22 11:16:55 +0100293}
Alan Cox01e1abb2008-07-22 11:16:55 +0100294EXPORT_SYMBOL_GPL(tty_ldisc_deref);
295
Peter Hurleyd2c43892013-06-15 07:04:47 -0400296
Peter Hurleyc2bb5242016-01-09 21:13:51 -0800297static inline int
Peter Hurleye80a10e2014-11-05 12:13:06 -0500298__tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
Peter Hurleyd2c43892013-06-15 07:04:47 -0400299{
300 return ldsem_down_write(&tty->ldisc_sem, timeout);
301}
302
Peter Hurleyc2bb5242016-01-09 21:13:51 -0800303static inline int
Peter Hurleye80a10e2014-11-05 12:13:06 -0500304__tty_ldisc_lock_nested(struct tty_struct *tty, unsigned long timeout)
Peter Hurleyd2c43892013-06-15 07:04:47 -0400305{
306 return ldsem_down_write_nested(&tty->ldisc_sem,
307 LDISC_SEM_OTHER, timeout);
308}
309
Peter Hurleye80a10e2014-11-05 12:13:06 -0500310static inline void __tty_ldisc_unlock(struct tty_struct *tty)
Peter Hurleyd2c43892013-06-15 07:04:47 -0400311{
Guillaume Gomez52772ea2015-10-04 21:19:18 +0200312 ldsem_up_write(&tty->ldisc_sem);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400313}
314
Gaurav Kohlib027e222018-01-23 13:16:34 +0530315int tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
Peter Hurleyfae76e92014-11-05 12:13:07 -0500316{
317 int ret;
318
Dmitry Safonovc96cf922018-11-01 00:24:48 +0000319 /* Kindly asking blocked readers to release the read side */
320 set_bit(TTY_LDISC_CHANGING, &tty->flags);
321 wake_up_interruptible_all(&tty->read_wait);
322 wake_up_interruptible_all(&tty->write_wait);
323
Peter Hurleyfae76e92014-11-05 12:13:07 -0500324 ret = __tty_ldisc_lock(tty, timeout);
325 if (!ret)
326 return -EBUSY;
327 set_bit(TTY_LDISC_HALTED, &tty->flags);
328 return 0;
329}
330
Gaurav Kohlib027e222018-01-23 13:16:34 +0530331void tty_ldisc_unlock(struct tty_struct *tty)
Peter Hurleyfae76e92014-11-05 12:13:07 -0500332{
333 clear_bit(TTY_LDISC_HALTED, &tty->flags);
Dmitry Safonovc96cf922018-11-01 00:24:48 +0000334 /* Can be cleared here - ldisc_unlock will wake up writers firstly */
335 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
Peter Hurleyfae76e92014-11-05 12:13:07 -0500336 __tty_ldisc_unlock(tty);
337}
338
Peter Hurleyc2bb5242016-01-09 21:13:51 -0800339static int
Peter Hurleyd2c43892013-06-15 07:04:47 -0400340tty_ldisc_lock_pair_timeout(struct tty_struct *tty, struct tty_struct *tty2,
341 unsigned long timeout)
342{
343 int ret;
344
345 if (tty < tty2) {
Peter Hurleye80a10e2014-11-05 12:13:06 -0500346 ret = __tty_ldisc_lock(tty, timeout);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400347 if (ret) {
Peter Hurleye80a10e2014-11-05 12:13:06 -0500348 ret = __tty_ldisc_lock_nested(tty2, timeout);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400349 if (!ret)
Peter Hurleye80a10e2014-11-05 12:13:06 -0500350 __tty_ldisc_unlock(tty);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400351 }
352 } else {
353 /* if this is possible, it has lots of implications */
354 WARN_ON_ONCE(tty == tty2);
355 if (tty2 && tty != tty2) {
Peter Hurleye80a10e2014-11-05 12:13:06 -0500356 ret = __tty_ldisc_lock(tty2, timeout);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400357 if (ret) {
Peter Hurleye80a10e2014-11-05 12:13:06 -0500358 ret = __tty_ldisc_lock_nested(tty, timeout);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400359 if (!ret)
Peter Hurleye80a10e2014-11-05 12:13:06 -0500360 __tty_ldisc_unlock(tty2);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400361 }
362 } else
Peter Hurleye80a10e2014-11-05 12:13:06 -0500363 ret = __tty_ldisc_lock(tty, timeout);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400364 }
365
366 if (!ret)
367 return -EBUSY;
368
369 set_bit(TTY_LDISC_HALTED, &tty->flags);
370 if (tty2)
371 set_bit(TTY_LDISC_HALTED, &tty2->flags);
372 return 0;
373}
374
Peter Hurleyc2bb5242016-01-09 21:13:51 -0800375static void tty_ldisc_lock_pair(struct tty_struct *tty, struct tty_struct *tty2)
Peter Hurleyd2c43892013-06-15 07:04:47 -0400376{
377 tty_ldisc_lock_pair_timeout(tty, tty2, MAX_SCHEDULE_TIMEOUT);
378}
379
Peter Hurleyc2bb5242016-01-09 21:13:51 -0800380static void tty_ldisc_unlock_pair(struct tty_struct *tty,
381 struct tty_struct *tty2)
Peter Hurleyd2c43892013-06-15 07:04:47 -0400382{
Peter Hurleye80a10e2014-11-05 12:13:06 -0500383 __tty_ldisc_unlock(tty);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400384 if (tty2)
Peter Hurleye80a10e2014-11-05 12:13:06 -0500385 __tty_ldisc_unlock(tty2);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400386}
387
Alan Cox01e1abb2008-07-22 11:16:55 +0100388/**
Alan Coxf2c4c652009-06-11 12:50:58 +0100389 * tty_ldisc_flush - flush line discipline queue
390 * @tty: tty
391 *
Peter Hurley86c80a82014-11-05 12:13:09 -0500392 * Flush the line discipline queue (if any) and the tty flip buffers
393 * for this tty.
Alan Coxf2c4c652009-06-11 12:50:58 +0100394 */
395
396void tty_ldisc_flush(struct tty_struct *tty)
397{
398 struct tty_ldisc *ld = tty_ldisc_ref(tty);
Peter Hurley86c80a82014-11-05 12:13:09 -0500399
400 tty_buffer_flush(tty, ld);
401 if (ld)
Alan Coxf2c4c652009-06-11 12:50:58 +0100402 tty_ldisc_deref(ld);
Alan Coxf2c4c652009-06-11 12:50:58 +0100403}
Alan Coxf2c4c652009-06-11 12:50:58 +0100404EXPORT_SYMBOL_GPL(tty_ldisc_flush);
405
406/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100407 * tty_set_termios_ldisc - set ldisc field
408 * @tty: tty structure
Peter Hurleyc12da962016-01-10 22:41:04 -0800409 * @disc: line discipline number
Alan Cox01e1abb2008-07-22 11:16:55 +0100410 *
411 * This is probably overkill for real world processors but
412 * they are not on hot paths so a little discipline won't do
413 * any harm.
414 *
Peter Hurleydd42bf12015-11-27 14:30:21 -0500415 * The line discipline-related tty_struct fields are reset to
416 * prevent the ldisc driver from re-using stale information for
417 * the new ldisc instance.
418 *
Peter Hurley6a1c0682013-06-15 09:14:23 -0400419 * Locking: takes termios_rwsem
Alan Cox01e1abb2008-07-22 11:16:55 +0100420 */
421
Peter Hurleyc12da962016-01-10 22:41:04 -0800422static void tty_set_termios_ldisc(struct tty_struct *tty, int disc)
Alan Cox01e1abb2008-07-22 11:16:55 +0100423{
Peter Hurley6a1c0682013-06-15 09:14:23 -0400424 down_write(&tty->termios_rwsem);
Peter Hurleyc12da962016-01-10 22:41:04 -0800425 tty->termios.c_line = disc;
Peter Hurley6a1c0682013-06-15 09:14:23 -0400426 up_write(&tty->termios_rwsem);
Peter Hurleydd42bf12015-11-27 14:30:21 -0500427
428 tty->disc_data = NULL;
429 tty->receive_room = 0;
Alan Cox01e1abb2008-07-22 11:16:55 +0100430}
431
Alan Coxc65c9bc2009-06-11 12:50:12 +0100432/**
433 * tty_ldisc_open - open a line discipline
434 * @tty: tty we are opening the ldisc on
435 * @ld: discipline to open
436 *
437 * A helper opening method. Also a convenient debugging and check
438 * point.
Arnd Bergmannec79d602010-06-01 22:53:01 +0200439 *
440 * Locking: always called with BTM already held.
Alan Coxc65c9bc2009-06-11 12:50:12 +0100441 */
442
443static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
444{
445 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
Alan Coxf18f9492009-11-30 13:18:35 +0000446 if (ld->ops->open) {
447 int ret;
Xiaofei Tan5d3945e2021-04-07 15:06:46 +0800448 /* BTM here locks versus a hangup event */
Alan Coxf18f9492009-11-30 13:18:35 +0000449 ret = ld->ops->open(tty);
Jiri Slaby7f90cfc2010-11-25 00:27:54 +0100450 if (ret)
451 clear_bit(TTY_LDISC_OPEN, &tty->flags);
Peter Hurleyfb6edc92015-07-12 22:49:12 -0400452
Peter Hurleya570a492016-01-10 22:41:02 -0800453 tty_ldisc_debug(tty, "%p: opened\n", ld);
Alan Coxf18f9492009-11-30 13:18:35 +0000454 return ret;
455 }
Alan Coxc65c9bc2009-06-11 12:50:12 +0100456 return 0;
457}
458
459/**
460 * tty_ldisc_close - close a line discipline
461 * @tty: tty we are opening the ldisc on
462 * @ld: discipline to close
463 *
464 * A helper close method. Also a convenient debugging and check
465 * point.
466 */
467
468static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
469{
Nikolay Borisov9ffbe8a2019-05-31 13:06:51 +0300470 lockdep_assert_held_write(&tty->ldisc_sem);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100471 WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
472 clear_bit(TTY_LDISC_OPEN, &tty->flags);
473 if (ld->ops->close)
474 ld->ops->close(tty);
Peter Hurleya570a492016-01-10 22:41:02 -0800475 tty_ldisc_debug(tty, "%p: closed\n", ld);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100476}
Alan Cox01e1abb2008-07-22 11:16:55 +0100477
478/**
Alan Cox8a8dabf2017-06-02 13:49:30 +0100479 * tty_ldisc_failto - helper for ldisc failback
480 * @tty: tty to open the ldisc on
481 * @ld: ldisc we are trying to fail back to
482 *
483 * Helper to try and recover a tty when switching back to the old
484 * ldisc fails and we need something attached.
485 */
486
487static int tty_ldisc_failto(struct tty_struct *tty, int ld)
488{
489 struct tty_ldisc *disc = tty_ldisc_get(tty, ld);
490 int r;
491
Nikolay Borisov9ffbe8a2019-05-31 13:06:51 +0300492 lockdep_assert_held_write(&tty->ldisc_sem);
Alan Cox8a8dabf2017-06-02 13:49:30 +0100493 if (IS_ERR(disc))
494 return PTR_ERR(disc);
495 tty->ldisc = disc;
496 tty_set_termios_ldisc(tty, ld);
Xiaofei Tan408795b2021-04-07 15:06:49 +0800497 r = tty_ldisc_open(tty, disc);
498 if (r < 0)
Alan Cox8a8dabf2017-06-02 13:49:30 +0100499 tty_ldisc_put(disc);
500 return r;
501}
502
503/**
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200504 * tty_ldisc_restore - helper for tty ldisc change
505 * @tty: tty to recover
506 * @old: previous ldisc
507 *
508 * Restore the previous line discipline or N_TTY when a line discipline
509 * change fails due to an open error
510 */
511
512static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
513{
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200514 /* There is an outstanding reference here so this is safe */
Tetsuo Handa598c2d42018-04-16 20:06:34 +0900515 if (tty_ldisc_failto(tty, old->ops->num) < 0) {
516 const char *name = tty_name(tty);
517
518 pr_warn("Falling back ldisc for %s.\n", name);
Xiaofei Tan72a8dcd2021-04-07 15:06:48 +0800519 /*
520 * The traditional behaviour is to fall back to N_TTY, we
521 * want to avoid falling back to N_NULL unless we have no
522 * choice to avoid the risk of breaking anything
523 */
Alan Cox8a8dabf2017-06-02 13:49:30 +0100524 if (tty_ldisc_failto(tty, N_TTY) < 0 &&
525 tty_ldisc_failto(tty, N_NULL) < 0)
Tetsuo Handa598c2d42018-04-16 20:06:34 +0900526 panic("Couldn't open N_NULL ldisc for %s.", name);
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200527 }
528}
529
530/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100531 * tty_set_ldisc - set line discipline
532 * @tty: the terminal to set
Jiri Slabyfa441952020-08-18 10:56:51 +0200533 * @disc: the line discipline number
Alan Cox01e1abb2008-07-22 11:16:55 +0100534 *
535 * Set the discipline of a tty line. Must be called from a process
Alan Coxc65c9bc2009-06-11 12:50:12 +0100536 * context. The ldisc change logic has to protect itself against any
537 * overlapping ldisc change (including on the other end of pty pairs),
538 * the close of one side of a tty/pty pair, and eventually hangup.
Alan Cox01e1abb2008-07-22 11:16:55 +0100539 */
540
Peter Hurleyc12da962016-01-10 22:41:04 -0800541int tty_set_ldisc(struct tty_struct *tty, int disc)
Alan Cox01e1abb2008-07-22 11:16:55 +0100542{
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200543 int retval;
544 struct tty_ldisc *old_ldisc, *new_ldisc;
545
546 new_ldisc = tty_ldisc_get(tty, disc);
547 if (IS_ERR(new_ldisc))
548 return PTR_ERR(new_ldisc);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100549
Peter Hurleyc8483bc2014-11-05 12:12:45 -0500550 tty_lock(tty);
Peter Hurley276a6612014-11-05 12:13:08 -0500551 retval = tty_ldisc_lock(tty, 5 * HZ);
Peter Hurley63d8cb32015-11-08 09:29:38 -0500552 if (retval)
553 goto err;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100554
Peter Hurleya570a492016-01-10 22:41:02 -0800555 if (!tty->ldisc) {
556 retval = -EIO;
557 goto out;
558 }
559
Peter Hurley63d8cb32015-11-08 09:29:38 -0500560 /* Check the no-op case */
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200561 if (tty->ldisc->ops->num == disc)
Peter Hurley63d8cb32015-11-08 09:29:38 -0500562 goto out;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100563
Peter Hurley63d8cb32015-11-08 09:29:38 -0500564 if (test_bit(TTY_HUPPED, &tty->flags)) {
565 /* We were raced by hangup */
566 retval = -EIO;
567 goto out;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100568 }
Alan Cox01e1abb2008-07-22 11:16:55 +0100569
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200570 old_ldisc = tty->ldisc;
571
572 /* Shutdown the old discipline. */
573 tty_ldisc_close(tty, old_ldisc);
574
575 /* Now set up the new line discipline. */
576 tty->ldisc = new_ldisc;
577 tty_set_termios_ldisc(tty, disc);
578
579 retval = tty_ldisc_open(tty, new_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100580 if (retval < 0) {
Alan Coxc65c9bc2009-06-11 12:50:12 +0100581 /* Back to the old one or N_TTY if we can't */
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200582 tty_ldisc_put(new_ldisc);
583 tty_ldisc_restore(tty, old_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100584 }
Alan Coxc65c9bc2009-06-11 12:50:12 +0100585
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200586 if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc) {
Peter Hurley9191aaa2014-11-05 13:11:41 -0500587 down_read(&tty->termios_rwsem);
Alan Cox01e1abb2008-07-22 11:16:55 +0100588 tty->ops->set_ldisc(tty);
Peter Hurley9191aaa2014-11-05 13:11:41 -0500589 up_read(&tty->termios_rwsem);
590 }
Alan Cox01e1abb2008-07-22 11:16:55 +0100591
Xiaofei Tan72a8dcd2021-04-07 15:06:48 +0800592 /*
593 * At this point we hold a reference to the new ldisc and a
594 * reference to the old ldisc, or we hold two references to
595 * the old ldisc (if it was restored as part of error cleanup
596 * above). In either case, releasing a single reference from
597 * the old ldisc is correct.
598 */
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200599 new_ldisc = old_ldisc;
Peter Hurley63d8cb32015-11-08 09:29:38 -0500600out:
Peter Hurley276a6612014-11-05 12:13:08 -0500601 tty_ldisc_unlock(tty);
Alan Cox01e1abb2008-07-22 11:16:55 +0100602
Xiaofei Tan72a8dcd2021-04-07 15:06:48 +0800603 /*
604 * Restart the work queue in case no characters kick it off. Safe if
605 * already running
606 */
Peter Hurley17a69212015-11-08 07:53:06 -0500607 tty_buffer_restart_work(tty->port);
Peter Hurley63d8cb32015-11-08 09:29:38 -0500608err:
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200609 tty_ldisc_put(new_ldisc); /* drop the extra reference */
Alan Cox89c8d912012-08-08 16:30:13 +0100610 tty_unlock(tty);
Alan Cox01e1abb2008-07-22 11:16:55 +0100611 return retval;
612}
Okash Khawaja1ab92da2017-05-15 18:45:33 +0100613EXPORT_SYMBOL_GPL(tty_set_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100614
Alan Coxc65c9bc2009-06-11 12:50:12 +0100615/**
Peter Hurley6ffeb4b2016-01-10 22:41:03 -0800616 * tty_ldisc_kill - teardown ldisc
617 * @tty: tty being released
618 *
619 * Perform final close of the ldisc and reset tty->ldisc
620 */
621static void tty_ldisc_kill(struct tty_struct *tty)
622{
Nikolay Borisov9ffbe8a2019-05-31 13:06:51 +0300623 lockdep_assert_held_write(&tty->ldisc_sem);
Peter Hurley6ffeb4b2016-01-10 22:41:03 -0800624 if (!tty->ldisc)
625 return;
626 /*
627 * Now kill off the ldisc
628 */
629 tty_ldisc_close(tty, tty->ldisc);
630 tty_ldisc_put(tty->ldisc);
631 /* Force an oops if we mess this up */
632 tty->ldisc = NULL;
633}
634
635/**
Alan Coxc65c9bc2009-06-11 12:50:12 +0100636 * tty_reset_termios - reset terminal state
637 * @tty: tty to reset
638 *
639 * Restore a terminal to the driver default state.
640 */
641
642static void tty_reset_termios(struct tty_struct *tty)
643{
Peter Hurley6a1c0682013-06-15 09:14:23 -0400644 down_write(&tty->termios_rwsem);
Alan Coxadc8d742012-07-14 15:31:47 +0100645 tty->termios = tty->driver->init_termios;
646 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
647 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
Peter Hurley6a1c0682013-06-15 09:14:23 -0400648 up_write(&tty->termios_rwsem);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100649}
650
651
652/**
653 * tty_ldisc_reinit - reinitialise the tty ldisc
654 * @tty: tty to reinit
Peter Hurleyc12da962016-01-10 22:41:04 -0800655 * @disc: line discipline to reinitialize
Alan Coxc65c9bc2009-06-11 12:50:12 +0100656 *
Peter Hurley7896f302016-01-10 22:41:05 -0800657 * Completely reinitialize the line discipline state, by closing the
Peter Hurley892d1fa2016-01-10 22:41:06 -0800658 * current instance, if there is one, and opening a new instance. If
659 * an error occurs opening the new non-N_TTY instance, the instance
660 * is dropped and tty->ldisc reset to NULL. The caller can then retry
661 * with N_TTY instead.
Peter Hurley7896f302016-01-10 22:41:05 -0800662 *
663 * Returns 0 if successful, otherwise error code < 0
Alan Coxc65c9bc2009-06-11 12:50:12 +0100664 */
665
Peter Hurley892d1fa2016-01-10 22:41:06 -0800666int tty_ldisc_reinit(struct tty_struct *tty, int disc)
Alan Coxc65c9bc2009-06-11 12:50:12 +0100667{
Peter Hurley7896f302016-01-10 22:41:05 -0800668 struct tty_ldisc *ld;
669 int retval;
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200670
Nikolay Borisov9ffbe8a2019-05-31 13:06:51 +0300671 lockdep_assert_held_write(&tty->ldisc_sem);
Peter Hurley7896f302016-01-10 22:41:05 -0800672 ld = tty_ldisc_get(tty, disc);
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200673 if (IS_ERR(ld)) {
674 BUG_ON(disc == N_TTY);
Peter Hurley7896f302016-01-10 22:41:05 -0800675 return PTR_ERR(ld);
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200676 }
Alan Coxc65c9bc2009-06-11 12:50:12 +0100677
Peter Hurley7896f302016-01-10 22:41:05 -0800678 if (tty->ldisc) {
679 tty_ldisc_close(tty, tty->ldisc);
680 tty_ldisc_put(tty->ldisc);
681 }
682
683 /* switch the line discipline */
Peter Hurleyf48070452013-03-11 16:44:42 -0400684 tty->ldisc = ld;
Peter Hurleyc12da962016-01-10 22:41:04 -0800685 tty_set_termios_ldisc(tty, disc);
Peter Hurley7896f302016-01-10 22:41:05 -0800686 retval = tty_ldisc_open(tty, tty->ldisc);
687 if (retval) {
Johannes Weinere65c62b2017-10-13 15:58:08 -0700688 tty_ldisc_put(tty->ldisc);
689 tty->ldisc = NULL;
Peter Hurley7896f302016-01-10 22:41:05 -0800690 }
691 return retval;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100692}
693
694/**
695 * tty_ldisc_hangup - hangup ldisc reset
696 * @tty: tty being hung up
Lee Jones8eddcca2020-11-12 10:58:54 +0000697 * @reinit: whether to re-initialise the tty
Alan Coxc65c9bc2009-06-11 12:50:12 +0100698 *
699 * Some tty devices reset their termios when they receive a hangup
700 * event. In that situation we must also switch back to N_TTY properly
701 * before we reset the termios data.
702 *
703 * Locking: We can take the ldisc mutex as the rest of the code is
704 * careful to allow for this.
705 *
706 * In the pty pair case this occurs in the close() path of the
707 * tty itself so we must be careful about locking rules.
708 */
709
Peter Hurley892d1fa2016-01-10 22:41:06 -0800710void tty_ldisc_hangup(struct tty_struct *tty, bool reinit)
Alan Coxc65c9bc2009-06-11 12:50:12 +0100711{
712 struct tty_ldisc *ld;
713
Peter Hurleya570a492016-01-10 22:41:02 -0800714 tty_ldisc_debug(tty, "%p: hangup\n", tty->ldisc);
Peter Hurleyfc575ee2013-03-11 16:44:38 -0400715
Alan Coxc65c9bc2009-06-11 12:50:12 +0100716 ld = tty_ldisc_ref(tty);
717 if (ld != NULL) {
Alan Coxc65c9bc2009-06-11 12:50:12 +0100718 if (ld->ops->flush_buffer)
719 ld->ops->flush_buffer(tty);
720 tty_driver_flush_buffer(tty);
721 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
722 ld->ops->write_wakeup)
723 ld->ops->write_wakeup(tty);
724 if (ld->ops->hangup)
725 ld->ops->hangup(tty);
726 tty_ldisc_deref(ld);
727 }
Peter Hurley36697522013-06-15 07:04:48 -0400728
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800729 wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT);
730 wake_up_interruptible_poll(&tty->read_wait, EPOLLIN);
Peter Hurley36697522013-06-15 07:04:48 -0400731
Alan Coxc65c9bc2009-06-11 12:50:12 +0100732 /*
733 * Shutdown the current line discipline, and reset it to
Alan Cox638b9642010-02-08 10:09:26 +0000734 * N_TTY if need be.
735 *
736 * Avoid racing set_ldisc or tty_ldisc_release
Alan Coxc65c9bc2009-06-11 12:50:12 +0100737 */
Peter Hurleyfae76e92014-11-05 12:13:07 -0500738 tty_ldisc_lock(tty, MAX_SCHEDULE_TIMEOUT);
Arnd Bergmann60af22d2010-06-01 22:53:06 +0200739
Peter Hurley892d1fa2016-01-10 22:41:06 -0800740 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
Alan Cox638b9642010-02-08 10:09:26 +0000741 tty_reset_termios(tty);
Peter Hurleyfc575ee2013-03-11 16:44:38 -0400742
Peter Hurley892d1fa2016-01-10 22:41:06 -0800743 if (tty->ldisc) {
744 if (reinit) {
Johannes Weinere65c62b2017-10-13 15:58:08 -0700745 if (tty_ldisc_reinit(tty, tty->termios.c_line) < 0 &&
746 tty_ldisc_reinit(tty, N_TTY) < 0)
747 WARN_ON(tty_ldisc_reinit(tty, N_NULL) < 0);
Peter Hurley892d1fa2016-01-10 22:41:06 -0800748 } else
749 tty_ldisc_kill(tty);
750 }
751 tty_ldisc_unlock(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100752}
Alan Cox01e1abb2008-07-22 11:16:55 +0100753
754/**
755 * tty_ldisc_setup - open line discipline
756 * @tty: tty being shut down
757 * @o_tty: pair tty for pty/tty pairs
758 *
759 * Called during the initial open of a tty/pty pair in order to set up the
Alan Coxc65c9bc2009-06-11 12:50:12 +0100760 * line disciplines and bind them to the tty. This has no locking issues
761 * as the device isn't yet active.
Alan Cox01e1abb2008-07-22 11:16:55 +0100762 */
763
764int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
765{
Peter Hurley9de2a7c2016-01-10 22:41:08 -0800766 int retval = tty_ldisc_open(tty, tty->ldisc);
Xiaofei Tand7238352021-04-07 15:06:47 +0800767
Alan Coxc65c9bc2009-06-11 12:50:12 +0100768 if (retval)
769 return retval;
770
771 if (o_tty) {
Dmitry Safonov110b8922018-11-01 00:24:51 +0000772 /*
773 * Called without o_tty->ldisc_sem held, as o_tty has been
774 * just allocated and no one has a reference to it.
775 */
Alan Coxc65c9bc2009-06-11 12:50:12 +0100776 retval = tty_ldisc_open(o_tty, o_tty->ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100777 if (retval) {
Peter Hurley9de2a7c2016-01-10 22:41:08 -0800778 tty_ldisc_close(tty, tty->ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100779 return retval;
780 }
Alan Cox01e1abb2008-07-22 11:16:55 +0100781 }
Alan Cox01e1abb2008-07-22 11:16:55 +0100782 return 0;
783}
Alan Cox89c8d912012-08-08 16:30:13 +0100784
Alan Cox01e1abb2008-07-22 11:16:55 +0100785/**
786 * tty_ldisc_release - release line discipline
Peter Hurley62462ae2014-11-05 12:12:58 -0500787 * @tty: tty being shut down (or one end of pty pair)
Alan Cox01e1abb2008-07-22 11:16:55 +0100788 *
Peter Hurley62462ae2014-11-05 12:12:58 -0500789 * Called during the final close of a tty or a pty pair in order to shut
Peter Hurley5b6e6832016-01-10 22:41:00 -0800790 * down the line discpline layer. On exit, each tty's ldisc is NULL.
Alan Cox01e1abb2008-07-22 11:16:55 +0100791 */
792
Peter Hurley62462ae2014-11-05 12:12:58 -0500793void tty_ldisc_release(struct tty_struct *tty)
Alan Cox01e1abb2008-07-22 11:16:55 +0100794{
Peter Hurley62462ae2014-11-05 12:12:58 -0500795 struct tty_struct *o_tty = tty->link;
796
Alan Cox01e1abb2008-07-22 11:16:55 +0100797 /*
Peter Hurleya2965b72013-03-11 16:44:35 -0400798 * Shutdown this line discipline. As this is the final close,
799 * it does not race with the set_ldisc code path.
Alan Cox01e1abb2008-07-22 11:16:55 +0100800 */
Alan Cox01e1abb2008-07-22 11:16:55 +0100801
Peter Hurley36697522013-06-15 07:04:48 -0400802 tty_ldisc_lock_pair(tty, o_tty);
Alan Cox89c8d912012-08-08 16:30:13 +0100803 tty_ldisc_kill(tty);
Alan Cox89c8d912012-08-08 16:30:13 +0100804 if (o_tty)
805 tty_ldisc_kill(o_tty);
Peter Hurley36697522013-06-15 07:04:48 -0400806 tty_ldisc_unlock_pair(tty, o_tty);
807
Xiaofei Tan72a8dcd2021-04-07 15:06:48 +0800808 /*
809 * And the memory resources remaining (buffers, termios) will be
810 * disposed of when the kref hits zero
811 */
Peter Hurleyfc575ee2013-03-11 16:44:38 -0400812
Peter Hurleyfb6edc92015-07-12 22:49:12 -0400813 tty_ldisc_debug(tty, "released\n");
Alan Cox01e1abb2008-07-22 11:16:55 +0100814}
Okash Khawaja1ab92da2017-05-15 18:45:33 +0100815EXPORT_SYMBOL_GPL(tty_ldisc_release);
Alan Cox01e1abb2008-07-22 11:16:55 +0100816
817/**
818 * tty_ldisc_init - ldisc setup for new tty
819 * @tty: tty being allocated
820 *
821 * Set up the line discipline objects for a newly allocated tty. Note that
822 * the tty structure is not completely set up when this call is made.
823 */
824
Tetsuo Handa903f9db2018-04-05 19:40:16 +0900825int tty_ldisc_init(struct tty_struct *tty)
Alan Cox01e1abb2008-07-22 11:16:55 +0100826{
Peter Hurley36697522013-06-15 07:04:48 -0400827 struct tty_ldisc *ld = tty_ldisc_get(tty, N_TTY);
Xiaofei Tand7238352021-04-07 15:06:47 +0800828
Alan Coxc65c9bc2009-06-11 12:50:12 +0100829 if (IS_ERR(ld))
Tetsuo Handa903f9db2018-04-05 19:40:16 +0900830 return PTR_ERR(ld);
Peter Hurleyf48070452013-03-11 16:44:42 -0400831 tty->ldisc = ld;
Tetsuo Handa903f9db2018-04-05 19:40:16 +0900832 return 0;
Alan Cox01e1abb2008-07-22 11:16:55 +0100833}
834
Jiri Slaby67166712011-03-23 10:48:35 +0100835/**
Peter Hurleyc8b710b2016-01-09 21:13:46 -0800836 * tty_ldisc_deinit - ldisc cleanup for new tty
Jiri Slaby67166712011-03-23 10:48:35 +0100837 * @tty: tty that was allocated recently
838 *
839 * The tty structure must not becompletely set up (tty_ldisc_setup) when
840 * this call is made.
841 */
842void tty_ldisc_deinit(struct tty_struct *tty)
843{
Dmitry Safonov110b8922018-11-01 00:24:51 +0000844 /* no ldisc_sem, tty is being destroyed */
Peter Hurleyc8b710b2016-01-09 21:13:46 -0800845 if (tty->ldisc)
846 tty_ldisc_put(tty->ldisc);
Peter Hurleyf48070452013-03-11 16:44:42 -0400847 tty->ldisc = NULL;
Jiri Slaby67166712011-03-23 10:48:35 +0100848}
Greg Kroah-Hartman7c0cca72019-01-21 17:26:42 +0100849
Greg Kroah-Hartman7c0cca72019-01-21 17:26:42 +0100850static struct ctl_table tty_table[] = {
851 {
852 .procname = "ldisc_autoload",
853 .data = &tty_ldisc_autoload,
854 .maxlen = sizeof(tty_ldisc_autoload),
855 .mode = 0644,
856 .proc_handler = proc_dointvec,
Matteo Croceeec48442019-07-18 15:58:50 -0700857 .extra1 = SYSCTL_ZERO,
858 .extra2 = SYSCTL_ONE,
Greg Kroah-Hartman7c0cca72019-01-21 17:26:42 +0100859 },
860 { }
861};
862
863static struct ctl_table tty_dir_table[] = {
864 {
865 .procname = "tty",
866 .mode = 0555,
867 .child = tty_table,
868 },
869 { }
870};
871
872static struct ctl_table tty_root_table[] = {
873 {
874 .procname = "dev",
875 .mode = 0555,
876 .child = tty_dir_table,
877 },
878 { }
879};
880
881void tty_sysctl_init(void)
882{
883 register_sysctl_table(tty_root_table);
884}