blob: 98e8316fd28adef5179bddf3d4b38e052035a242 [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
51 * @disc: ldisc number
52 * @new_ldisc: pointer to the ldisc object
53 *
54 * Installs a new line discipline into the kernel. The discipline
55 * is set up as unreferenced and then made available to the kernel
56 * from this point onwards.
57 *
58 * Locking:
Peter Hurley137084b2013-06-15 07:04:46 -040059 * takes tty_ldiscs_lock to guard against ldisc races
Alan Cox01e1abb2008-07-22 11:16:55 +010060 */
61
Jiri Slabyfbadf70a2021-05-05 11:19:07 +020062int tty_register_ldisc(struct tty_ldisc_ops *new_ldisc)
Alan Cox01e1abb2008-07-22 11:16:55 +010063{
64 unsigned long flags;
65 int ret = 0;
66
Jiri Slabyfbadf70a2021-05-05 11:19:07 +020067 if (new_ldisc->num < N_TTY || new_ldisc->num >= NR_LDISCS)
Alan Cox01e1abb2008-07-22 11:16:55 +010068 return -EINVAL;
69
Peter Hurley137084b2013-06-15 07:04:46 -040070 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
Jiri Slabyfbadf70a2021-05-05 11:19:07 +020071 tty_ldiscs[new_ldisc->num] = new_ldisc;
Peter Hurley137084b2013-06-15 07:04:46 -040072 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
Alan Cox01e1abb2008-07-22 11:16:55 +010073
74 return ret;
75}
76EXPORT_SYMBOL(tty_register_ldisc);
77
78/**
79 * tty_unregister_ldisc - unload a line discipline
80 * @disc: ldisc number
Alan Cox01e1abb2008-07-22 11:16:55 +010081 *
82 * Remove a line discipline from the kernel providing it is not
83 * currently in use.
84 *
85 * Locking:
Peter Hurley137084b2013-06-15 07:04:46 -040086 * takes tty_ldiscs_lock to guard against ldisc races
Alan Cox01e1abb2008-07-22 11:16:55 +010087 */
88
Jiri Slabyf81ee8b2021-05-05 11:19:09 +020089int tty_unregister_ldisc(struct tty_ldisc_ops *ldisc)
Alan Cox01e1abb2008-07-22 11:16:55 +010090{
91 unsigned long flags;
Alan Cox01e1abb2008-07-22 11:16:55 +010092
Peter Hurley137084b2013-06-15 07:04:46 -040093 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
Jiri Slaby19475202021-05-05 11:19:10 +020094 tty_ldiscs[ldisc->num] = NULL;
Peter Hurley137084b2013-06-15 07:04:46 -040095 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
Alan Cox01e1abb2008-07-22 11:16:55 +010096
Jiri Slaby19475202021-05-05 11:19:10 +020097 return 0;
Alan Cox01e1abb2008-07-22 11:16:55 +010098}
99EXPORT_SYMBOL(tty_unregister_ldisc);
100
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700101static struct tty_ldisc_ops *get_ldops(int disc)
102{
103 unsigned long flags;
104 struct tty_ldisc_ops *ldops, *ret;
105
Peter Hurley137084b2013-06-15 07:04:46 -0400106 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700107 ret = ERR_PTR(-EINVAL);
108 ldops = tty_ldiscs[disc];
109 if (ldops) {
110 ret = ERR_PTR(-EAGAIN);
Jiri Slaby19475202021-05-05 11:19:10 +0200111 if (try_module_get(ldops->owner))
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700112 ret = ldops;
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700113 }
Peter Hurley137084b2013-06-15 07:04:46 -0400114 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700115 return ret;
116}
117
118static void put_ldops(struct tty_ldisc_ops *ldops)
119{
120 unsigned long flags;
121
Peter Hurley137084b2013-06-15 07:04:46 -0400122 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700123 module_put(ldops->owner);
Peter Hurley137084b2013-06-15 07:04:46 -0400124 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700125}
Alan Cox01e1abb2008-07-22 11:16:55 +0100126
Lee Jones8eddcca2020-11-12 10:58:54 +0000127static int tty_ldisc_autoload = IS_BUILTIN(CONFIG_LDISC_AUTOLOAD);
Alan Cox01e1abb2008-07-22 11:16:55 +0100128/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100129 * tty_ldisc_get - take a reference to an ldisc
Lee Jones8a3bdec2020-11-04 19:35:42 +0000130 * @tty: tty device
Alan Cox01e1abb2008-07-22 11:16:55 +0100131 * @disc: ldisc number
Alan Cox01e1abb2008-07-22 11:16:55 +0100132 *
133 * Takes a reference to a line discipline. Deals with refcounts and
Peter Hurleyc0cc1c52016-01-10 22:40:59 -0800134 * module locking counts.
135 *
136 * Returns: -EINVAL if the discipline index is not [N_TTY..NR_LDISCS] or
137 * if the discipline is not registered
138 * -EAGAIN if request_module() failed to load or register the
Xiaofei Tanb8958542021-04-07 15:06:50 +0800139 * discipline
Peter Hurleyc0cc1c52016-01-10 22:40:59 -0800140 * -ENOMEM if allocation failure
141 *
142 * Otherwise, returns a pointer to the discipline and bumps the
143 * ref count
Alan Cox01e1abb2008-07-22 11:16:55 +0100144 *
145 * Locking:
Peter Hurley137084b2013-06-15 07:04:46 -0400146 * takes tty_ldiscs_lock to guard against ldisc races
Alan Cox01e1abb2008-07-22 11:16:55 +0100147 */
148
Peter Hurley36697522013-06-15 07:04:48 -0400149static struct tty_ldisc *tty_ldisc_get(struct tty_struct *tty, int disc)
Alan Cox01e1abb2008-07-22 11:16:55 +0100150{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100151 struct tty_ldisc *ld;
Linus Torvalds182274f2009-08-03 16:01:28 -0700152 struct tty_ldisc_ops *ldops;
Alan Cox01e1abb2008-07-22 11:16:55 +0100153
154 if (disc < N_TTY || disc >= NR_LDISCS)
Alan Coxc65c9bc2009-06-11 12:50:12 +0100155 return ERR_PTR(-EINVAL);
Linus Torvalds182274f2009-08-03 16:01:28 -0700156
157 /*
158 * Get the ldisc ops - we may need to request them to be loaded
159 * dynamically and try again.
160 */
161 ldops = get_ldops(disc);
162 if (IS_ERR(ldops)) {
Greg Kroah-Hartman7c0cca72019-01-21 17:26:42 +0100163 if (!capable(CAP_SYS_MODULE) && !tty_ldisc_autoload)
164 return ERR_PTR(-EPERM);
Alan Cox01e1abb2008-07-22 11:16:55 +0100165 request_module("tty-ldisc-%d", disc);
Linus Torvalds182274f2009-08-03 16:01:28 -0700166 ldops = get_ldops(disc);
167 if (IS_ERR(ldops))
168 return ERR_CAST(ldops);
Alan Cox01e1abb2008-07-22 11:16:55 +0100169 }
Linus Torvalds182274f2009-08-03 16:01:28 -0700170
Tetsuo Handabcdd0ca2018-04-25 20:12:31 +0900171 /*
172 * There is no way to handle allocation failure of only 16 bytes.
173 * Let's simplify error handling and save more memory.
174 */
175 ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL | __GFP_NOFAIL);
Linus Torvalds182274f2009-08-03 16:01:28 -0700176 ld->ops = ldops;
Peter Hurley36697522013-06-15 07:04:48 -0400177 ld->tty = tty;
Ivo Sieben1541f842012-05-03 14:37:43 +0200178
Alan Coxc65c9bc2009-06-11 12:50:12 +0100179 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100180}
181
Lee Jones8eddcca2020-11-12 10:58:54 +0000182/*
Peter Hurley734de242013-03-11 16:44:43 -0400183 * tty_ldisc_put - release the ldisc
184 *
185 * Complement of tty_ldisc_get().
186 */
Denys Vlasenkocb128f62015-10-27 17:40:02 +0100187static void tty_ldisc_put(struct tty_ldisc *ld)
Peter Hurley734de242013-03-11 16:44:43 -0400188{
Peter Hurley734de242013-03-11 16:44:43 -0400189 if (WARN_ON_ONCE(!ld))
190 return;
191
Peter Hurley36697522013-06-15 07:04:48 -0400192 put_ldops(ld->ops);
Peter Hurley734de242013-03-11 16:44:43 -0400193 kfree(ld);
Peter Hurley734de242013-03-11 16:44:43 -0400194}
195
Alan Cox852e99d2009-06-11 12:51:41 +0100196static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
Alan Cox01e1abb2008-07-22 11:16:55 +0100197{
198 return (*pos < NR_LDISCS) ? pos : NULL;
199}
200
Alan Cox852e99d2009-06-11 12:51:41 +0100201static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
Alan Cox01e1abb2008-07-22 11:16:55 +0100202{
203 (*pos)++;
204 return (*pos < NR_LDISCS) ? pos : NULL;
205}
206
207static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
208{
209}
210
211static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
212{
213 int i = *(loff_t *)v;
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700214 struct tty_ldisc_ops *ldops;
Alan Cox852e99d2009-06-11 12:51:41 +0100215
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700216 ldops = get_ldops(i);
217 if (IS_ERR(ldops))
Alan Cox01e1abb2008-07-22 11:16:55 +0100218 return 0;
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700219 seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
220 put_ldops(ldops);
Alan Cox01e1abb2008-07-22 11:16:55 +0100221 return 0;
222}
223
Christoph Hellwigfddda2b2018-04-13 19:44:18 +0200224const struct seq_operations tty_ldiscs_seq_ops = {
Alan Cox01e1abb2008-07-22 11:16:55 +0100225 .start = tty_ldiscs_seq_start,
226 .next = tty_ldiscs_seq_next,
227 .stop = tty_ldiscs_seq_stop,
228 .show = tty_ldiscs_seq_show,
229};
230
Alan Cox01e1abb2008-07-22 11:16:55 +0100231/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100232 * tty_ldisc_ref_wait - wait for the tty ldisc
233 * @tty: tty device
234 *
235 * Dereference the line discipline for the terminal and take a
236 * reference to it. If the line discipline is in flux then
237 * wait patiently until it changes.
238 *
Peter Hurley892d1fa2016-01-10 22:41:06 -0800239 * Returns: NULL if the tty has been hungup and not re-opened with
240 * a new file descriptor, otherwise valid ldisc reference
241 *
Lee Jones8eddcca2020-11-12 10:58:54 +0000242 * Note 1: Must not be called from an IRQ/timer context. The caller
Alan Cox01e1abb2008-07-22 11:16:55 +0100243 * must also be careful not to hold other locks that will deadlock
244 * against a discipline change, such as an existing ldisc reference
245 * (which we check for)
246 *
Lee Jones8eddcca2020-11-12 10:58:54 +0000247 * Note 2: a file_operations routine (read/poll/write) should use this
Peter Hurleye55afd12016-01-10 22:41:01 -0800248 * function to wait for any ldisc lifetime events to finish.
Alan Cox01e1abb2008-07-22 11:16:55 +0100249 */
250
251struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
252{
Dmitry Vyukova4a3e062017-03-04 13:46:12 +0100253 struct tty_ldisc *ld;
254
Peter Hurley36697522013-06-15 07:04:48 -0400255 ldsem_down_read(&tty->ldisc_sem, MAX_SCHEDULE_TIMEOUT);
Dmitry Vyukova4a3e062017-03-04 13:46:12 +0100256 ld = tty->ldisc;
257 if (!ld)
Peter Hurleya570a492016-01-10 22:41:02 -0800258 ldsem_up_read(&tty->ldisc_sem);
Dmitry Vyukova4a3e062017-03-04 13:46:12 +0100259 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100260}
Alan Cox01e1abb2008-07-22 11:16:55 +0100261EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
262
263/**
264 * tty_ldisc_ref - get the tty ldisc
265 * @tty: tty device
266 *
267 * Dereference the line discipline for the terminal and take a
268 * reference to it. If the line discipline is in flux then
269 * return NULL. Can be called from IRQ and timer functions.
Alan Cox01e1abb2008-07-22 11:16:55 +0100270 */
271
272struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
273{
Peter Hurley36697522013-06-15 07:04:48 -0400274 struct tty_ldisc *ld = NULL;
275
276 if (ldsem_down_read_trylock(&tty->ldisc_sem)) {
277 ld = tty->ldisc;
278 if (!ld)
279 ldsem_up_read(&tty->ldisc_sem);
280 }
281 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100282}
Alan Cox01e1abb2008-07-22 11:16:55 +0100283EXPORT_SYMBOL_GPL(tty_ldisc_ref);
284
285/**
286 * tty_ldisc_deref - free a tty ldisc reference
287 * @ld: reference to free up
288 *
289 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
290 * be called in IRQ context.
Alan Cox01e1abb2008-07-22 11:16:55 +0100291 */
292
293void tty_ldisc_deref(struct tty_ldisc *ld)
294{
Peter Hurley36697522013-06-15 07:04:48 -0400295 ldsem_up_read(&ld->tty->ldisc_sem);
Alan Cox01e1abb2008-07-22 11:16:55 +0100296}
Alan Cox01e1abb2008-07-22 11:16:55 +0100297EXPORT_SYMBOL_GPL(tty_ldisc_deref);
298
Peter Hurleyd2c43892013-06-15 07:04:47 -0400299
Peter Hurleyc2bb5242016-01-09 21:13:51 -0800300static inline int
Peter Hurleye80a10e2014-11-05 12:13:06 -0500301__tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
Peter Hurleyd2c43892013-06-15 07:04:47 -0400302{
303 return ldsem_down_write(&tty->ldisc_sem, timeout);
304}
305
Peter Hurleyc2bb5242016-01-09 21:13:51 -0800306static inline int
Peter Hurleye80a10e2014-11-05 12:13:06 -0500307__tty_ldisc_lock_nested(struct tty_struct *tty, unsigned long timeout)
Peter Hurleyd2c43892013-06-15 07:04:47 -0400308{
309 return ldsem_down_write_nested(&tty->ldisc_sem,
310 LDISC_SEM_OTHER, timeout);
311}
312
Peter Hurleye80a10e2014-11-05 12:13:06 -0500313static inline void __tty_ldisc_unlock(struct tty_struct *tty)
Peter Hurleyd2c43892013-06-15 07:04:47 -0400314{
Guillaume Gomez52772ea2015-10-04 21:19:18 +0200315 ldsem_up_write(&tty->ldisc_sem);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400316}
317
Gaurav Kohlib027e222018-01-23 13:16:34 +0530318int tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
Peter Hurleyfae76e92014-11-05 12:13:07 -0500319{
320 int ret;
321
Dmitry Safonovc96cf922018-11-01 00:24:48 +0000322 /* Kindly asking blocked readers to release the read side */
323 set_bit(TTY_LDISC_CHANGING, &tty->flags);
324 wake_up_interruptible_all(&tty->read_wait);
325 wake_up_interruptible_all(&tty->write_wait);
326
Peter Hurleyfae76e92014-11-05 12:13:07 -0500327 ret = __tty_ldisc_lock(tty, timeout);
328 if (!ret)
329 return -EBUSY;
330 set_bit(TTY_LDISC_HALTED, &tty->flags);
331 return 0;
332}
333
Gaurav Kohlib027e222018-01-23 13:16:34 +0530334void tty_ldisc_unlock(struct tty_struct *tty)
Peter Hurleyfae76e92014-11-05 12:13:07 -0500335{
336 clear_bit(TTY_LDISC_HALTED, &tty->flags);
Dmitry Safonovc96cf922018-11-01 00:24:48 +0000337 /* Can be cleared here - ldisc_unlock will wake up writers firstly */
338 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
Peter Hurleyfae76e92014-11-05 12:13:07 -0500339 __tty_ldisc_unlock(tty);
340}
341
Peter Hurleyc2bb5242016-01-09 21:13:51 -0800342static int
Peter Hurleyd2c43892013-06-15 07:04:47 -0400343tty_ldisc_lock_pair_timeout(struct tty_struct *tty, struct tty_struct *tty2,
344 unsigned long timeout)
345{
346 int ret;
347
348 if (tty < tty2) {
Peter Hurleye80a10e2014-11-05 12:13:06 -0500349 ret = __tty_ldisc_lock(tty, timeout);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400350 if (ret) {
Peter Hurleye80a10e2014-11-05 12:13:06 -0500351 ret = __tty_ldisc_lock_nested(tty2, timeout);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400352 if (!ret)
Peter Hurleye80a10e2014-11-05 12:13:06 -0500353 __tty_ldisc_unlock(tty);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400354 }
355 } else {
356 /* if this is possible, it has lots of implications */
357 WARN_ON_ONCE(tty == tty2);
358 if (tty2 && tty != tty2) {
Peter Hurleye80a10e2014-11-05 12:13:06 -0500359 ret = __tty_ldisc_lock(tty2, timeout);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400360 if (ret) {
Peter Hurleye80a10e2014-11-05 12:13:06 -0500361 ret = __tty_ldisc_lock_nested(tty, timeout);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400362 if (!ret)
Peter Hurleye80a10e2014-11-05 12:13:06 -0500363 __tty_ldisc_unlock(tty2);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400364 }
365 } else
Peter Hurleye80a10e2014-11-05 12:13:06 -0500366 ret = __tty_ldisc_lock(tty, timeout);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400367 }
368
369 if (!ret)
370 return -EBUSY;
371
372 set_bit(TTY_LDISC_HALTED, &tty->flags);
373 if (tty2)
374 set_bit(TTY_LDISC_HALTED, &tty2->flags);
375 return 0;
376}
377
Peter Hurleyc2bb5242016-01-09 21:13:51 -0800378static void tty_ldisc_lock_pair(struct tty_struct *tty, struct tty_struct *tty2)
Peter Hurleyd2c43892013-06-15 07:04:47 -0400379{
380 tty_ldisc_lock_pair_timeout(tty, tty2, MAX_SCHEDULE_TIMEOUT);
381}
382
Peter Hurleyc2bb5242016-01-09 21:13:51 -0800383static void tty_ldisc_unlock_pair(struct tty_struct *tty,
384 struct tty_struct *tty2)
Peter Hurleyd2c43892013-06-15 07:04:47 -0400385{
Peter Hurleye80a10e2014-11-05 12:13:06 -0500386 __tty_ldisc_unlock(tty);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400387 if (tty2)
Peter Hurleye80a10e2014-11-05 12:13:06 -0500388 __tty_ldisc_unlock(tty2);
Peter Hurleyd2c43892013-06-15 07:04:47 -0400389}
390
Alan Cox01e1abb2008-07-22 11:16:55 +0100391/**
Alan Coxf2c4c652009-06-11 12:50:58 +0100392 * tty_ldisc_flush - flush line discipline queue
393 * @tty: tty
394 *
Peter Hurley86c80a82014-11-05 12:13:09 -0500395 * Flush the line discipline queue (if any) and the tty flip buffers
396 * for this tty.
Alan Coxf2c4c652009-06-11 12:50:58 +0100397 */
398
399void tty_ldisc_flush(struct tty_struct *tty)
400{
401 struct tty_ldisc *ld = tty_ldisc_ref(tty);
Peter Hurley86c80a82014-11-05 12:13:09 -0500402
403 tty_buffer_flush(tty, ld);
404 if (ld)
Alan Coxf2c4c652009-06-11 12:50:58 +0100405 tty_ldisc_deref(ld);
Alan Coxf2c4c652009-06-11 12:50:58 +0100406}
Alan Coxf2c4c652009-06-11 12:50:58 +0100407EXPORT_SYMBOL_GPL(tty_ldisc_flush);
408
409/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100410 * tty_set_termios_ldisc - set ldisc field
411 * @tty: tty structure
Peter Hurleyc12da962016-01-10 22:41:04 -0800412 * @disc: line discipline number
Alan Cox01e1abb2008-07-22 11:16:55 +0100413 *
414 * This is probably overkill for real world processors but
415 * they are not on hot paths so a little discipline won't do
416 * any harm.
417 *
Peter Hurleydd42bf12015-11-27 14:30:21 -0500418 * The line discipline-related tty_struct fields are reset to
419 * prevent the ldisc driver from re-using stale information for
420 * the new ldisc instance.
421 *
Peter Hurley6a1c0682013-06-15 09:14:23 -0400422 * Locking: takes termios_rwsem
Alan Cox01e1abb2008-07-22 11:16:55 +0100423 */
424
Peter Hurleyc12da962016-01-10 22:41:04 -0800425static void tty_set_termios_ldisc(struct tty_struct *tty, int disc)
Alan Cox01e1abb2008-07-22 11:16:55 +0100426{
Peter Hurley6a1c0682013-06-15 09:14:23 -0400427 down_write(&tty->termios_rwsem);
Peter Hurleyc12da962016-01-10 22:41:04 -0800428 tty->termios.c_line = disc;
Peter Hurley6a1c0682013-06-15 09:14:23 -0400429 up_write(&tty->termios_rwsem);
Peter Hurleydd42bf12015-11-27 14:30:21 -0500430
431 tty->disc_data = NULL;
432 tty->receive_room = 0;
Alan Cox01e1abb2008-07-22 11:16:55 +0100433}
434
Alan Coxc65c9bc2009-06-11 12:50:12 +0100435/**
436 * tty_ldisc_open - open a line discipline
437 * @tty: tty we are opening the ldisc on
438 * @ld: discipline to open
439 *
440 * A helper opening method. Also a convenient debugging and check
441 * point.
Arnd Bergmannec79d602010-06-01 22:53:01 +0200442 *
443 * Locking: always called with BTM already held.
Alan Coxc65c9bc2009-06-11 12:50:12 +0100444 */
445
446static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
447{
448 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
Alan Coxf18f9492009-11-30 13:18:35 +0000449 if (ld->ops->open) {
450 int ret;
Xiaofei Tan5d3945e2021-04-07 15:06:46 +0800451 /* BTM here locks versus a hangup event */
Alan Coxf18f9492009-11-30 13:18:35 +0000452 ret = ld->ops->open(tty);
Jiri Slaby7f90cfc2010-11-25 00:27:54 +0100453 if (ret)
454 clear_bit(TTY_LDISC_OPEN, &tty->flags);
Peter Hurleyfb6edc92015-07-12 22:49:12 -0400455
Peter Hurleya570a492016-01-10 22:41:02 -0800456 tty_ldisc_debug(tty, "%p: opened\n", ld);
Alan Coxf18f9492009-11-30 13:18:35 +0000457 return ret;
458 }
Alan Coxc65c9bc2009-06-11 12:50:12 +0100459 return 0;
460}
461
462/**
463 * tty_ldisc_close - close a line discipline
464 * @tty: tty we are opening the ldisc on
465 * @ld: discipline to close
466 *
467 * A helper close method. Also a convenient debugging and check
468 * point.
469 */
470
471static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
472{
Nikolay Borisov9ffbe8a2019-05-31 13:06:51 +0300473 lockdep_assert_held_write(&tty->ldisc_sem);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100474 WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
475 clear_bit(TTY_LDISC_OPEN, &tty->flags);
476 if (ld->ops->close)
477 ld->ops->close(tty);
Peter Hurleya570a492016-01-10 22:41:02 -0800478 tty_ldisc_debug(tty, "%p: closed\n", ld);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100479}
Alan Cox01e1abb2008-07-22 11:16:55 +0100480
481/**
Alan Cox8a8dabf2017-06-02 13:49:30 +0100482 * tty_ldisc_failto - helper for ldisc failback
483 * @tty: tty to open the ldisc on
484 * @ld: ldisc we are trying to fail back to
485 *
486 * Helper to try and recover a tty when switching back to the old
487 * ldisc fails and we need something attached.
488 */
489
490static int tty_ldisc_failto(struct tty_struct *tty, int ld)
491{
492 struct tty_ldisc *disc = tty_ldisc_get(tty, ld);
493 int r;
494
Nikolay Borisov9ffbe8a2019-05-31 13:06:51 +0300495 lockdep_assert_held_write(&tty->ldisc_sem);
Alan Cox8a8dabf2017-06-02 13:49:30 +0100496 if (IS_ERR(disc))
497 return PTR_ERR(disc);
498 tty->ldisc = disc;
499 tty_set_termios_ldisc(tty, ld);
Xiaofei Tan408795b2021-04-07 15:06:49 +0800500 r = tty_ldisc_open(tty, disc);
501 if (r < 0)
Alan Cox8a8dabf2017-06-02 13:49:30 +0100502 tty_ldisc_put(disc);
503 return r;
504}
505
506/**
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200507 * tty_ldisc_restore - helper for tty ldisc change
508 * @tty: tty to recover
509 * @old: previous ldisc
510 *
511 * Restore the previous line discipline or N_TTY when a line discipline
512 * change fails due to an open error
513 */
514
515static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
516{
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200517 /* There is an outstanding reference here so this is safe */
Tetsuo Handa598c2d42018-04-16 20:06:34 +0900518 if (tty_ldisc_failto(tty, old->ops->num) < 0) {
519 const char *name = tty_name(tty);
520
521 pr_warn("Falling back ldisc for %s.\n", name);
Xiaofei Tan72a8dcd2021-04-07 15:06:48 +0800522 /*
523 * The traditional behaviour is to fall back to N_TTY, we
524 * want to avoid falling back to N_NULL unless we have no
525 * choice to avoid the risk of breaking anything
526 */
Alan Cox8a8dabf2017-06-02 13:49:30 +0100527 if (tty_ldisc_failto(tty, N_TTY) < 0 &&
528 tty_ldisc_failto(tty, N_NULL) < 0)
Tetsuo Handa598c2d42018-04-16 20:06:34 +0900529 panic("Couldn't open N_NULL ldisc for %s.", name);
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200530 }
531}
532
533/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100534 * tty_set_ldisc - set line discipline
535 * @tty: the terminal to set
Jiri Slabyfa441952020-08-18 10:56:51 +0200536 * @disc: the line discipline number
Alan Cox01e1abb2008-07-22 11:16:55 +0100537 *
538 * Set the discipline of a tty line. Must be called from a process
Alan Coxc65c9bc2009-06-11 12:50:12 +0100539 * context. The ldisc change logic has to protect itself against any
540 * overlapping ldisc change (including on the other end of pty pairs),
541 * the close of one side of a tty/pty pair, and eventually hangup.
Alan Cox01e1abb2008-07-22 11:16:55 +0100542 */
543
Peter Hurleyc12da962016-01-10 22:41:04 -0800544int tty_set_ldisc(struct tty_struct *tty, int disc)
Alan Cox01e1abb2008-07-22 11:16:55 +0100545{
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200546 int retval;
547 struct tty_ldisc *old_ldisc, *new_ldisc;
548
549 new_ldisc = tty_ldisc_get(tty, disc);
550 if (IS_ERR(new_ldisc))
551 return PTR_ERR(new_ldisc);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100552
Peter Hurleyc8483bc2014-11-05 12:12:45 -0500553 tty_lock(tty);
Peter Hurley276a6612014-11-05 12:13:08 -0500554 retval = tty_ldisc_lock(tty, 5 * HZ);
Peter Hurley63d8cb32015-11-08 09:29:38 -0500555 if (retval)
556 goto err;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100557
Peter Hurleya570a492016-01-10 22:41:02 -0800558 if (!tty->ldisc) {
559 retval = -EIO;
560 goto out;
561 }
562
Peter Hurley63d8cb32015-11-08 09:29:38 -0500563 /* Check the no-op case */
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200564 if (tty->ldisc->ops->num == disc)
Peter Hurley63d8cb32015-11-08 09:29:38 -0500565 goto out;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100566
Peter Hurley63d8cb32015-11-08 09:29:38 -0500567 if (test_bit(TTY_HUPPED, &tty->flags)) {
568 /* We were raced by hangup */
569 retval = -EIO;
570 goto out;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100571 }
Alan Cox01e1abb2008-07-22 11:16:55 +0100572
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200573 old_ldisc = tty->ldisc;
574
575 /* Shutdown the old discipline. */
576 tty_ldisc_close(tty, old_ldisc);
577
578 /* Now set up the new line discipline. */
579 tty->ldisc = new_ldisc;
580 tty_set_termios_ldisc(tty, disc);
581
582 retval = tty_ldisc_open(tty, new_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100583 if (retval < 0) {
Alan Coxc65c9bc2009-06-11 12:50:12 +0100584 /* Back to the old one or N_TTY if we can't */
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200585 tty_ldisc_put(new_ldisc);
586 tty_ldisc_restore(tty, old_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100587 }
Alan Coxc65c9bc2009-06-11 12:50:12 +0100588
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200589 if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc) {
Peter Hurley9191aaa2014-11-05 13:11:41 -0500590 down_read(&tty->termios_rwsem);
Alan Cox01e1abb2008-07-22 11:16:55 +0100591 tty->ops->set_ldisc(tty);
Peter Hurley9191aaa2014-11-05 13:11:41 -0500592 up_read(&tty->termios_rwsem);
593 }
Alan Cox01e1abb2008-07-22 11:16:55 +0100594
Xiaofei Tan72a8dcd2021-04-07 15:06:48 +0800595 /*
596 * At this point we hold a reference to the new ldisc and a
597 * reference to the old ldisc, or we hold two references to
598 * the old ldisc (if it was restored as part of error cleanup
599 * above). In either case, releasing a single reference from
600 * the old ldisc is correct.
601 */
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200602 new_ldisc = old_ldisc;
Peter Hurley63d8cb32015-11-08 09:29:38 -0500603out:
Peter Hurley276a6612014-11-05 12:13:08 -0500604 tty_ldisc_unlock(tty);
Alan Cox01e1abb2008-07-22 11:16:55 +0100605
Xiaofei Tan72a8dcd2021-04-07 15:06:48 +0800606 /*
607 * Restart the work queue in case no characters kick it off. Safe if
608 * already running
609 */
Peter Hurley17a69212015-11-08 07:53:06 -0500610 tty_buffer_restart_work(tty->port);
Peter Hurley63d8cb32015-11-08 09:29:38 -0500611err:
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200612 tty_ldisc_put(new_ldisc); /* drop the extra reference */
Alan Cox89c8d912012-08-08 16:30:13 +0100613 tty_unlock(tty);
Alan Cox01e1abb2008-07-22 11:16:55 +0100614 return retval;
615}
Okash Khawaja1ab92da2017-05-15 18:45:33 +0100616EXPORT_SYMBOL_GPL(tty_set_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100617
Alan Coxc65c9bc2009-06-11 12:50:12 +0100618/**
Peter Hurley6ffeb4b2016-01-10 22:41:03 -0800619 * tty_ldisc_kill - teardown ldisc
620 * @tty: tty being released
621 *
622 * Perform final close of the ldisc and reset tty->ldisc
623 */
624static void tty_ldisc_kill(struct tty_struct *tty)
625{
Nikolay Borisov9ffbe8a2019-05-31 13:06:51 +0300626 lockdep_assert_held_write(&tty->ldisc_sem);
Peter Hurley6ffeb4b2016-01-10 22:41:03 -0800627 if (!tty->ldisc)
628 return;
629 /*
630 * Now kill off the ldisc
631 */
632 tty_ldisc_close(tty, tty->ldisc);
633 tty_ldisc_put(tty->ldisc);
634 /* Force an oops if we mess this up */
635 tty->ldisc = NULL;
636}
637
638/**
Alan Coxc65c9bc2009-06-11 12:50:12 +0100639 * tty_reset_termios - reset terminal state
640 * @tty: tty to reset
641 *
642 * Restore a terminal to the driver default state.
643 */
644
645static void tty_reset_termios(struct tty_struct *tty)
646{
Peter Hurley6a1c0682013-06-15 09:14:23 -0400647 down_write(&tty->termios_rwsem);
Alan Coxadc8d742012-07-14 15:31:47 +0100648 tty->termios = tty->driver->init_termios;
649 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
650 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
Peter Hurley6a1c0682013-06-15 09:14:23 -0400651 up_write(&tty->termios_rwsem);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100652}
653
654
655/**
656 * tty_ldisc_reinit - reinitialise the tty ldisc
657 * @tty: tty to reinit
Peter Hurleyc12da962016-01-10 22:41:04 -0800658 * @disc: line discipline to reinitialize
Alan Coxc65c9bc2009-06-11 12:50:12 +0100659 *
Peter Hurley7896f302016-01-10 22:41:05 -0800660 * Completely reinitialize the line discipline state, by closing the
Peter Hurley892d1fa2016-01-10 22:41:06 -0800661 * current instance, if there is one, and opening a new instance. If
662 * an error occurs opening the new non-N_TTY instance, the instance
663 * is dropped and tty->ldisc reset to NULL. The caller can then retry
664 * with N_TTY instead.
Peter Hurley7896f302016-01-10 22:41:05 -0800665 *
666 * Returns 0 if successful, otherwise error code < 0
Alan Coxc65c9bc2009-06-11 12:50:12 +0100667 */
668
Peter Hurley892d1fa2016-01-10 22:41:06 -0800669int tty_ldisc_reinit(struct tty_struct *tty, int disc)
Alan Coxc65c9bc2009-06-11 12:50:12 +0100670{
Peter Hurley7896f302016-01-10 22:41:05 -0800671 struct tty_ldisc *ld;
672 int retval;
Philippe Rétornaz1c95ba12010-10-27 17:13:21 +0200673
Nikolay Borisov9ffbe8a2019-05-31 13:06:51 +0300674 lockdep_assert_held_write(&tty->ldisc_sem);
Peter Hurley7896f302016-01-10 22:41:05 -0800675 ld = tty_ldisc_get(tty, disc);
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200676 if (IS_ERR(ld)) {
677 BUG_ON(disc == N_TTY);
Peter Hurley7896f302016-01-10 22:41:05 -0800678 return PTR_ERR(ld);
Greg Kroah-Hartmana8983d02017-04-14 10:57:52 +0200679 }
Alan Coxc65c9bc2009-06-11 12:50:12 +0100680
Peter Hurley7896f302016-01-10 22:41:05 -0800681 if (tty->ldisc) {
682 tty_ldisc_close(tty, tty->ldisc);
683 tty_ldisc_put(tty->ldisc);
684 }
685
686 /* switch the line discipline */
Peter Hurleyf48070452013-03-11 16:44:42 -0400687 tty->ldisc = ld;
Peter Hurleyc12da962016-01-10 22:41:04 -0800688 tty_set_termios_ldisc(tty, disc);
Peter Hurley7896f302016-01-10 22:41:05 -0800689 retval = tty_ldisc_open(tty, tty->ldisc);
690 if (retval) {
Johannes Weinere65c62b2017-10-13 15:58:08 -0700691 tty_ldisc_put(tty->ldisc);
692 tty->ldisc = NULL;
Peter Hurley7896f302016-01-10 22:41:05 -0800693 }
694 return retval;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100695}
696
697/**
698 * tty_ldisc_hangup - hangup ldisc reset
699 * @tty: tty being hung up
Lee Jones8eddcca2020-11-12 10:58:54 +0000700 * @reinit: whether to re-initialise the tty
Alan Coxc65c9bc2009-06-11 12:50:12 +0100701 *
702 * Some tty devices reset their termios when they receive a hangup
703 * event. In that situation we must also switch back to N_TTY properly
704 * before we reset the termios data.
705 *
706 * Locking: We can take the ldisc mutex as the rest of the code is
707 * careful to allow for this.
708 *
709 * In the pty pair case this occurs in the close() path of the
710 * tty itself so we must be careful about locking rules.
711 */
712
Peter Hurley892d1fa2016-01-10 22:41:06 -0800713void tty_ldisc_hangup(struct tty_struct *tty, bool reinit)
Alan Coxc65c9bc2009-06-11 12:50:12 +0100714{
715 struct tty_ldisc *ld;
716
Peter Hurleya570a492016-01-10 22:41:02 -0800717 tty_ldisc_debug(tty, "%p: hangup\n", tty->ldisc);
Peter Hurleyfc575ee2013-03-11 16:44:38 -0400718
Alan Coxc65c9bc2009-06-11 12:50:12 +0100719 ld = tty_ldisc_ref(tty);
720 if (ld != NULL) {
Alan Coxc65c9bc2009-06-11 12:50:12 +0100721 if (ld->ops->flush_buffer)
722 ld->ops->flush_buffer(tty);
723 tty_driver_flush_buffer(tty);
724 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
725 ld->ops->write_wakeup)
726 ld->ops->write_wakeup(tty);
727 if (ld->ops->hangup)
728 ld->ops->hangup(tty);
729 tty_ldisc_deref(ld);
730 }
Peter Hurley36697522013-06-15 07:04:48 -0400731
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800732 wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT);
733 wake_up_interruptible_poll(&tty->read_wait, EPOLLIN);
Peter Hurley36697522013-06-15 07:04:48 -0400734
Alan Coxc65c9bc2009-06-11 12:50:12 +0100735 /*
736 * Shutdown the current line discipline, and reset it to
Alan Cox638b9642010-02-08 10:09:26 +0000737 * N_TTY if need be.
738 *
739 * Avoid racing set_ldisc or tty_ldisc_release
Alan Coxc65c9bc2009-06-11 12:50:12 +0100740 */
Peter Hurleyfae76e92014-11-05 12:13:07 -0500741 tty_ldisc_lock(tty, MAX_SCHEDULE_TIMEOUT);
Arnd Bergmann60af22d2010-06-01 22:53:06 +0200742
Peter Hurley892d1fa2016-01-10 22:41:06 -0800743 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
Alan Cox638b9642010-02-08 10:09:26 +0000744 tty_reset_termios(tty);
Peter Hurleyfc575ee2013-03-11 16:44:38 -0400745
Peter Hurley892d1fa2016-01-10 22:41:06 -0800746 if (tty->ldisc) {
747 if (reinit) {
Johannes Weinere65c62b2017-10-13 15:58:08 -0700748 if (tty_ldisc_reinit(tty, tty->termios.c_line) < 0 &&
749 tty_ldisc_reinit(tty, N_TTY) < 0)
750 WARN_ON(tty_ldisc_reinit(tty, N_NULL) < 0);
Peter Hurley892d1fa2016-01-10 22:41:06 -0800751 } else
752 tty_ldisc_kill(tty);
753 }
754 tty_ldisc_unlock(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100755}
Alan Cox01e1abb2008-07-22 11:16:55 +0100756
757/**
758 * tty_ldisc_setup - open line discipline
759 * @tty: tty being shut down
760 * @o_tty: pair tty for pty/tty pairs
761 *
762 * Called during the initial open of a tty/pty pair in order to set up the
Alan Coxc65c9bc2009-06-11 12:50:12 +0100763 * line disciplines and bind them to the tty. This has no locking issues
764 * as the device isn't yet active.
Alan Cox01e1abb2008-07-22 11:16:55 +0100765 */
766
767int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
768{
Peter Hurley9de2a7c2016-01-10 22:41:08 -0800769 int retval = tty_ldisc_open(tty, tty->ldisc);
Xiaofei Tand7238352021-04-07 15:06:47 +0800770
Alan Coxc65c9bc2009-06-11 12:50:12 +0100771 if (retval)
772 return retval;
773
774 if (o_tty) {
Dmitry Safonov110b8922018-11-01 00:24:51 +0000775 /*
776 * Called without o_tty->ldisc_sem held, as o_tty has been
777 * just allocated and no one has a reference to it.
778 */
Alan Coxc65c9bc2009-06-11 12:50:12 +0100779 retval = tty_ldisc_open(o_tty, o_tty->ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100780 if (retval) {
Peter Hurley9de2a7c2016-01-10 22:41:08 -0800781 tty_ldisc_close(tty, tty->ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100782 return retval;
783 }
Alan Cox01e1abb2008-07-22 11:16:55 +0100784 }
Alan Cox01e1abb2008-07-22 11:16:55 +0100785 return 0;
786}
Alan Cox89c8d912012-08-08 16:30:13 +0100787
Alan Cox01e1abb2008-07-22 11:16:55 +0100788/**
789 * tty_ldisc_release - release line discipline
Peter Hurley62462ae2014-11-05 12:12:58 -0500790 * @tty: tty being shut down (or one end of pty pair)
Alan Cox01e1abb2008-07-22 11:16:55 +0100791 *
Peter Hurley62462ae2014-11-05 12:12:58 -0500792 * Called during the final close of a tty or a pty pair in order to shut
Peter Hurley5b6e6832016-01-10 22:41:00 -0800793 * down the line discpline layer. On exit, each tty's ldisc is NULL.
Alan Cox01e1abb2008-07-22 11:16:55 +0100794 */
795
Peter Hurley62462ae2014-11-05 12:12:58 -0500796void tty_ldisc_release(struct tty_struct *tty)
Alan Cox01e1abb2008-07-22 11:16:55 +0100797{
Peter Hurley62462ae2014-11-05 12:12:58 -0500798 struct tty_struct *o_tty = tty->link;
799
Alan Cox01e1abb2008-07-22 11:16:55 +0100800 /*
Peter Hurleya2965b72013-03-11 16:44:35 -0400801 * Shutdown this line discipline. As this is the final close,
802 * it does not race with the set_ldisc code path.
Alan Cox01e1abb2008-07-22 11:16:55 +0100803 */
Alan Cox01e1abb2008-07-22 11:16:55 +0100804
Peter Hurley36697522013-06-15 07:04:48 -0400805 tty_ldisc_lock_pair(tty, o_tty);
Alan Cox89c8d912012-08-08 16:30:13 +0100806 tty_ldisc_kill(tty);
Alan Cox89c8d912012-08-08 16:30:13 +0100807 if (o_tty)
808 tty_ldisc_kill(o_tty);
Peter Hurley36697522013-06-15 07:04:48 -0400809 tty_ldisc_unlock_pair(tty, o_tty);
810
Xiaofei Tan72a8dcd2021-04-07 15:06:48 +0800811 /*
812 * And the memory resources remaining (buffers, termios) will be
813 * disposed of when the kref hits zero
814 */
Peter Hurleyfc575ee2013-03-11 16:44:38 -0400815
Peter Hurleyfb6edc92015-07-12 22:49:12 -0400816 tty_ldisc_debug(tty, "released\n");
Alan Cox01e1abb2008-07-22 11:16:55 +0100817}
Okash Khawaja1ab92da2017-05-15 18:45:33 +0100818EXPORT_SYMBOL_GPL(tty_ldisc_release);
Alan Cox01e1abb2008-07-22 11:16:55 +0100819
820/**
821 * tty_ldisc_init - ldisc setup for new tty
822 * @tty: tty being allocated
823 *
824 * Set up the line discipline objects for a newly allocated tty. Note that
825 * the tty structure is not completely set up when this call is made.
826 */
827
Tetsuo Handa903f9db2018-04-05 19:40:16 +0900828int tty_ldisc_init(struct tty_struct *tty)
Alan Cox01e1abb2008-07-22 11:16:55 +0100829{
Peter Hurley36697522013-06-15 07:04:48 -0400830 struct tty_ldisc *ld = tty_ldisc_get(tty, N_TTY);
Xiaofei Tand7238352021-04-07 15:06:47 +0800831
Alan Coxc65c9bc2009-06-11 12:50:12 +0100832 if (IS_ERR(ld))
Tetsuo Handa903f9db2018-04-05 19:40:16 +0900833 return PTR_ERR(ld);
Peter Hurleyf48070452013-03-11 16:44:42 -0400834 tty->ldisc = ld;
Tetsuo Handa903f9db2018-04-05 19:40:16 +0900835 return 0;
Alan Cox01e1abb2008-07-22 11:16:55 +0100836}
837
Jiri Slaby67166712011-03-23 10:48:35 +0100838/**
Peter Hurleyc8b710b2016-01-09 21:13:46 -0800839 * tty_ldisc_deinit - ldisc cleanup for new tty
Jiri Slaby67166712011-03-23 10:48:35 +0100840 * @tty: tty that was allocated recently
841 *
842 * The tty structure must not becompletely set up (tty_ldisc_setup) when
843 * this call is made.
844 */
845void tty_ldisc_deinit(struct tty_struct *tty)
846{
Dmitry Safonov110b8922018-11-01 00:24:51 +0000847 /* no ldisc_sem, tty is being destroyed */
Peter Hurleyc8b710b2016-01-09 21:13:46 -0800848 if (tty->ldisc)
849 tty_ldisc_put(tty->ldisc);
Peter Hurleyf48070452013-03-11 16:44:42 -0400850 tty->ldisc = NULL;
Jiri Slaby67166712011-03-23 10:48:35 +0100851}
Greg Kroah-Hartman7c0cca72019-01-21 17:26:42 +0100852
Greg Kroah-Hartman7c0cca72019-01-21 17:26:42 +0100853static struct ctl_table tty_table[] = {
854 {
855 .procname = "ldisc_autoload",
856 .data = &tty_ldisc_autoload,
857 .maxlen = sizeof(tty_ldisc_autoload),
858 .mode = 0644,
859 .proc_handler = proc_dointvec,
Matteo Croceeec48442019-07-18 15:58:50 -0700860 .extra1 = SYSCTL_ZERO,
861 .extra2 = SYSCTL_ONE,
Greg Kroah-Hartman7c0cca72019-01-21 17:26:42 +0100862 },
863 { }
864};
865
866static struct ctl_table tty_dir_table[] = {
867 {
868 .procname = "tty",
869 .mode = 0555,
870 .child = tty_table,
871 },
872 { }
873};
874
875static struct ctl_table tty_root_table[] = {
876 {
877 .procname = "dev",
878 .mode = 0555,
879 .child = tty_dir_table,
880 },
881 { }
882};
883
884void tty_sysctl_init(void)
885{
886 register_sysctl_table(tty_root_table);
887}