Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/types.h>
0003 #include <linux/errno.h>
0004 #include <linux/kmod.h>
0005 #include <linux/sched.h>
0006 #include <linux/interrupt.h>
0007 #include <linux/tty.h>
0008 #include <linux/tty_driver.h>
0009 #include <linux/file.h>
0010 #include <linux/mm.h>
0011 #include <linux/string.h>
0012 #include <linux/slab.h>
0013 #include <linux/poll.h>
0014 #include <linux/proc_fs.h>
0015 #include <linux/module.h>
0016 #include <linux/device.h>
0017 #include <linux/wait.h>
0018 #include <linux/bitops.h>
0019 #include <linux/seq_file.h>
0020 #include <linux/uaccess.h>
0021 #include <linux/ratelimit.h>
0022 #include "tty.h"
0023 
0024 #undef LDISC_DEBUG_HANGUP
0025 
0026 #ifdef LDISC_DEBUG_HANGUP
0027 #define tty_ldisc_debug(tty, f, args...)    tty_debug(tty, f, ##args)
0028 #else
0029 #define tty_ldisc_debug(tty, f, args...)
0030 #endif
0031 
0032 /* lockdep nested classes for tty->ldisc_sem */
0033 enum {
0034     LDISC_SEM_NORMAL,
0035     LDISC_SEM_OTHER,
0036 };
0037 
0038 
0039 /*
0040  *  This guards the refcounted line discipline lists. The lock
0041  *  must be taken with irqs off because there are hangup path
0042  *  callers who will do ldisc lookups and cannot sleep.
0043  */
0044 
0045 static DEFINE_RAW_SPINLOCK(tty_ldiscs_lock);
0046 /* Line disc dispatch table */
0047 static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
0048 
0049 /**
0050  * tty_register_ldisc   -   install a line discipline
0051  * @new_ldisc: pointer to the ldisc object
0052  *
0053  * Installs a new line discipline into the kernel. The discipline is set up as
0054  * unreferenced and then made available to the kernel from this point onwards.
0055  *
0056  * Locking: takes %tty_ldiscs_lock to guard against ldisc races
0057  */
0058 int tty_register_ldisc(struct tty_ldisc_ops *new_ldisc)
0059 {
0060     unsigned long flags;
0061     int ret = 0;
0062 
0063     if (new_ldisc->num < N_TTY || new_ldisc->num >= NR_LDISCS)
0064         return -EINVAL;
0065 
0066     raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
0067     tty_ldiscs[new_ldisc->num] = new_ldisc;
0068     raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
0069 
0070     return ret;
0071 }
0072 EXPORT_SYMBOL(tty_register_ldisc);
0073 
0074 /**
0075  * tty_unregister_ldisc -   unload a line discipline
0076  * @ldisc: ldisc number
0077  *
0078  * Remove a line discipline from the kernel providing it is not currently in
0079  * use.
0080  *
0081  * Locking: takes %tty_ldiscs_lock to guard against ldisc races
0082  */
0083 
0084 void tty_unregister_ldisc(struct tty_ldisc_ops *ldisc)
0085 {
0086     unsigned long flags;
0087 
0088     raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
0089     tty_ldiscs[ldisc->num] = NULL;
0090     raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
0091 }
0092 EXPORT_SYMBOL(tty_unregister_ldisc);
0093 
0094 static struct tty_ldisc_ops *get_ldops(int disc)
0095 {
0096     unsigned long flags;
0097     struct tty_ldisc_ops *ldops, *ret;
0098 
0099     raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
0100     ret = ERR_PTR(-EINVAL);
0101     ldops = tty_ldiscs[disc];
0102     if (ldops) {
0103         ret = ERR_PTR(-EAGAIN);
0104         if (try_module_get(ldops->owner))
0105             ret = ldops;
0106     }
0107     raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
0108     return ret;
0109 }
0110 
0111 static void put_ldops(struct tty_ldisc_ops *ldops)
0112 {
0113     unsigned long flags;
0114 
0115     raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
0116     module_put(ldops->owner);
0117     raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
0118 }
0119 
0120 static int tty_ldisc_autoload = IS_BUILTIN(CONFIG_LDISC_AUTOLOAD);
0121 
0122 /**
0123  * tty_ldisc_get    -   take a reference to an ldisc
0124  * @tty: tty device
0125  * @disc: ldisc number
0126  *
0127  * Takes a reference to a line discipline. Deals with refcounts and module
0128  * locking counts. If the discipline is not available, its module loaded, if
0129  * possible.
0130  *
0131  * Returns:
0132  * * -%EINVAL if the discipline index is not [%N_TTY .. %NR_LDISCS] or if the
0133  *   discipline is not registered
0134  * * -%EAGAIN if request_module() failed to load or register the discipline
0135  * * -%ENOMEM if allocation failure
0136  * * Otherwise, returns a pointer to the discipline and bumps the ref count
0137  *
0138  * Locking: takes %tty_ldiscs_lock to guard against ldisc races
0139  */
0140 static struct tty_ldisc *tty_ldisc_get(struct tty_struct *tty, int disc)
0141 {
0142     struct tty_ldisc *ld;
0143     struct tty_ldisc_ops *ldops;
0144 
0145     if (disc < N_TTY || disc >= NR_LDISCS)
0146         return ERR_PTR(-EINVAL);
0147 
0148     /*
0149      * Get the ldisc ops - we may need to request them to be loaded
0150      * dynamically and try again.
0151      */
0152     ldops = get_ldops(disc);
0153     if (IS_ERR(ldops)) {
0154         if (!capable(CAP_SYS_MODULE) && !tty_ldisc_autoload)
0155             return ERR_PTR(-EPERM);
0156         request_module("tty-ldisc-%d", disc);
0157         ldops = get_ldops(disc);
0158         if (IS_ERR(ldops))
0159             return ERR_CAST(ldops);
0160     }
0161 
0162     /*
0163      * There is no way to handle allocation failure of only 16 bytes.
0164      * Let's simplify error handling and save more memory.
0165      */
0166     ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL | __GFP_NOFAIL);
0167     ld->ops = ldops;
0168     ld->tty = tty;
0169 
0170     return ld;
0171 }
0172 
0173 /**
0174  * tty_ldisc_put    -   release the ldisc
0175  * @ld: lisdsc to release
0176  *
0177  * Complement of tty_ldisc_get().
0178  */
0179 static void tty_ldisc_put(struct tty_ldisc *ld)
0180 {
0181     if (WARN_ON_ONCE(!ld))
0182         return;
0183 
0184     put_ldops(ld->ops);
0185     kfree(ld);
0186 }
0187 
0188 static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
0189 {
0190     return (*pos < NR_LDISCS) ? pos : NULL;
0191 }
0192 
0193 static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
0194 {
0195     (*pos)++;
0196     return (*pos < NR_LDISCS) ? pos : NULL;
0197 }
0198 
0199 static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
0200 {
0201 }
0202 
0203 static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
0204 {
0205     int i = *(loff_t *)v;
0206     struct tty_ldisc_ops *ldops;
0207 
0208     ldops = get_ldops(i);
0209     if (IS_ERR(ldops))
0210         return 0;
0211     seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
0212     put_ldops(ldops);
0213     return 0;
0214 }
0215 
0216 const struct seq_operations tty_ldiscs_seq_ops = {
0217     .start  = tty_ldiscs_seq_start,
0218     .next   = tty_ldiscs_seq_next,
0219     .stop   = tty_ldiscs_seq_stop,
0220     .show   = tty_ldiscs_seq_show,
0221 };
0222 
0223 /**
0224  * tty_ldisc_ref_wait   -   wait for the tty ldisc
0225  * @tty: tty device
0226  *
0227  * Dereference the line discipline for the terminal and take a reference to it.
0228  * If the line discipline is in flux then wait patiently until it changes.
0229  *
0230  * Returns: %NULL if the tty has been hungup and not re-opened with a new file
0231  * descriptor, otherwise valid ldisc reference
0232  *
0233  * Note 1: Must not be called from an IRQ/timer context. The caller must also
0234  * be careful not to hold other locks that will deadlock against a discipline
0235  * change, such as an existing ldisc reference (which we check for).
0236  *
0237  * Note 2: a file_operations routine (read/poll/write) should use this function
0238  * to wait for any ldisc lifetime events to finish.
0239  */
0240 struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
0241 {
0242     struct tty_ldisc *ld;
0243 
0244     ldsem_down_read(&tty->ldisc_sem, MAX_SCHEDULE_TIMEOUT);
0245     ld = tty->ldisc;
0246     if (!ld)
0247         ldsem_up_read(&tty->ldisc_sem);
0248     return ld;
0249 }
0250 EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
0251 
0252 /**
0253  * tty_ldisc_ref    -   get the tty ldisc
0254  * @tty: tty device
0255  *
0256  * Dereference the line discipline for the terminal and take a reference to it.
0257  * If the line discipline is in flux then return %NULL. Can be called from IRQ
0258  * and timer functions.
0259  */
0260 struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
0261 {
0262     struct tty_ldisc *ld = NULL;
0263 
0264     if (ldsem_down_read_trylock(&tty->ldisc_sem)) {
0265         ld = tty->ldisc;
0266         if (!ld)
0267             ldsem_up_read(&tty->ldisc_sem);
0268     }
0269     return ld;
0270 }
0271 EXPORT_SYMBOL_GPL(tty_ldisc_ref);
0272 
0273 /**
0274  * tty_ldisc_deref  -   free a tty ldisc reference
0275  * @ld: reference to free up
0276  *
0277  * Undoes the effect of tty_ldisc_ref() or tty_ldisc_ref_wait(). May be called
0278  * in IRQ context.
0279  */
0280 void tty_ldisc_deref(struct tty_ldisc *ld)
0281 {
0282     ldsem_up_read(&ld->tty->ldisc_sem);
0283 }
0284 EXPORT_SYMBOL_GPL(tty_ldisc_deref);
0285 
0286 
0287 static inline int
0288 __tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
0289 {
0290     return ldsem_down_write(&tty->ldisc_sem, timeout);
0291 }
0292 
0293 static inline int
0294 __tty_ldisc_lock_nested(struct tty_struct *tty, unsigned long timeout)
0295 {
0296     return ldsem_down_write_nested(&tty->ldisc_sem,
0297                        LDISC_SEM_OTHER, timeout);
0298 }
0299 
0300 static inline void __tty_ldisc_unlock(struct tty_struct *tty)
0301 {
0302     ldsem_up_write(&tty->ldisc_sem);
0303 }
0304 
0305 int tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
0306 {
0307     int ret;
0308 
0309     /* Kindly asking blocked readers to release the read side */
0310     set_bit(TTY_LDISC_CHANGING, &tty->flags);
0311     wake_up_interruptible_all(&tty->read_wait);
0312     wake_up_interruptible_all(&tty->write_wait);
0313 
0314     ret = __tty_ldisc_lock(tty, timeout);
0315     if (!ret)
0316         return -EBUSY;
0317     set_bit(TTY_LDISC_HALTED, &tty->flags);
0318     return 0;
0319 }
0320 
0321 void tty_ldisc_unlock(struct tty_struct *tty)
0322 {
0323     clear_bit(TTY_LDISC_HALTED, &tty->flags);
0324     /* Can be cleared here - ldisc_unlock will wake up writers firstly */
0325     clear_bit(TTY_LDISC_CHANGING, &tty->flags);
0326     __tty_ldisc_unlock(tty);
0327 }
0328 
0329 static int
0330 tty_ldisc_lock_pair_timeout(struct tty_struct *tty, struct tty_struct *tty2,
0331                 unsigned long timeout)
0332 {
0333     int ret;
0334 
0335     if (tty < tty2) {
0336         ret = __tty_ldisc_lock(tty, timeout);
0337         if (ret) {
0338             ret = __tty_ldisc_lock_nested(tty2, timeout);
0339             if (!ret)
0340                 __tty_ldisc_unlock(tty);
0341         }
0342     } else {
0343         /* if this is possible, it has lots of implications */
0344         WARN_ON_ONCE(tty == tty2);
0345         if (tty2 && tty != tty2) {
0346             ret = __tty_ldisc_lock(tty2, timeout);
0347             if (ret) {
0348                 ret = __tty_ldisc_lock_nested(tty, timeout);
0349                 if (!ret)
0350                     __tty_ldisc_unlock(tty2);
0351             }
0352         } else
0353             ret = __tty_ldisc_lock(tty, timeout);
0354     }
0355 
0356     if (!ret)
0357         return -EBUSY;
0358 
0359     set_bit(TTY_LDISC_HALTED, &tty->flags);
0360     if (tty2)
0361         set_bit(TTY_LDISC_HALTED, &tty2->flags);
0362     return 0;
0363 }
0364 
0365 static void tty_ldisc_lock_pair(struct tty_struct *tty, struct tty_struct *tty2)
0366 {
0367     tty_ldisc_lock_pair_timeout(tty, tty2, MAX_SCHEDULE_TIMEOUT);
0368 }
0369 
0370 static void tty_ldisc_unlock_pair(struct tty_struct *tty,
0371                   struct tty_struct *tty2)
0372 {
0373     __tty_ldisc_unlock(tty);
0374     if (tty2)
0375         __tty_ldisc_unlock(tty2);
0376 }
0377 
0378 /**
0379  * tty_ldisc_flush      -   flush line discipline queue
0380  * @tty: tty to flush ldisc for
0381  *
0382  * Flush the line discipline queue (if any) and the tty flip buffers for this
0383  * @tty.
0384  */
0385 void tty_ldisc_flush(struct tty_struct *tty)
0386 {
0387     struct tty_ldisc *ld = tty_ldisc_ref(tty);
0388 
0389     tty_buffer_flush(tty, ld);
0390     if (ld)
0391         tty_ldisc_deref(ld);
0392 }
0393 EXPORT_SYMBOL_GPL(tty_ldisc_flush);
0394 
0395 /**
0396  * tty_set_termios_ldisc    -   set ldisc field
0397  * @tty: tty structure
0398  * @disc: line discipline number
0399  *
0400  * This is probably overkill for real world processors but they are not on hot
0401  * paths so a little discipline won't do any harm.
0402  *
0403  * The line discipline-related tty_struct fields are reset to prevent the ldisc
0404  * driver from re-using stale information for the new ldisc instance.
0405  *
0406  * Locking: takes termios_rwsem
0407  */
0408 static void tty_set_termios_ldisc(struct tty_struct *tty, int disc)
0409 {
0410     down_write(&tty->termios_rwsem);
0411     tty->termios.c_line = disc;
0412     up_write(&tty->termios_rwsem);
0413 
0414     tty->disc_data = NULL;
0415     tty->receive_room = 0;
0416 }
0417 
0418 /**
0419  * tty_ldisc_open       -   open a line discipline
0420  * @tty: tty we are opening the ldisc on
0421  * @ld: discipline to open
0422  *
0423  * A helper opening method. Also a convenient debugging and check point.
0424  *
0425  * Locking: always called with BTM already held.
0426  */
0427 static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
0428 {
0429     WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
0430     if (ld->ops->open) {
0431         int ret;
0432         /* BTM here locks versus a hangup event */
0433         ret = ld->ops->open(tty);
0434         if (ret)
0435             clear_bit(TTY_LDISC_OPEN, &tty->flags);
0436 
0437         tty_ldisc_debug(tty, "%p: opened\n", ld);
0438         return ret;
0439     }
0440     return 0;
0441 }
0442 
0443 /**
0444  * tty_ldisc_close      -   close a line discipline
0445  * @tty: tty we are opening the ldisc on
0446  * @ld: discipline to close
0447  *
0448  * A helper close method. Also a convenient debugging and check point.
0449  */
0450 static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
0451 {
0452     lockdep_assert_held_write(&tty->ldisc_sem);
0453     WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
0454     clear_bit(TTY_LDISC_OPEN, &tty->flags);
0455     if (ld->ops->close)
0456         ld->ops->close(tty);
0457     tty_ldisc_debug(tty, "%p: closed\n", ld);
0458 }
0459 
0460 /**
0461  * tty_ldisc_failto -   helper for ldisc failback
0462  * @tty: tty to open the ldisc on
0463  * @ld: ldisc we are trying to fail back to
0464  *
0465  * Helper to try and recover a tty when switching back to the old ldisc fails
0466  * and we need something attached.
0467  */
0468 static int tty_ldisc_failto(struct tty_struct *tty, int ld)
0469 {
0470     struct tty_ldisc *disc = tty_ldisc_get(tty, ld);
0471     int r;
0472 
0473     lockdep_assert_held_write(&tty->ldisc_sem);
0474     if (IS_ERR(disc))
0475         return PTR_ERR(disc);
0476     tty->ldisc = disc;
0477     tty_set_termios_ldisc(tty, ld);
0478     r = tty_ldisc_open(tty, disc);
0479     if (r < 0)
0480         tty_ldisc_put(disc);
0481     return r;
0482 }
0483 
0484 /**
0485  * tty_ldisc_restore    -   helper for tty ldisc change
0486  * @tty: tty to recover
0487  * @old: previous ldisc
0488  *
0489  * Restore the previous line discipline or %N_TTY when a line discipline change
0490  * fails due to an open error
0491  */
0492 static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
0493 {
0494     /* There is an outstanding reference here so this is safe */
0495     if (tty_ldisc_failto(tty, old->ops->num) < 0) {
0496         const char *name = tty_name(tty);
0497 
0498         pr_warn("Falling back ldisc for %s.\n", name);
0499         /*
0500          * The traditional behaviour is to fall back to N_TTY, we
0501          * want to avoid falling back to N_NULL unless we have no
0502          * choice to avoid the risk of breaking anything
0503          */
0504         if (tty_ldisc_failto(tty, N_TTY) < 0 &&
0505             tty_ldisc_failto(tty, N_NULL) < 0)
0506             panic("Couldn't open N_NULL ldisc for %s.", name);
0507     }
0508 }
0509 
0510 /**
0511  * tty_set_ldisc        -   set line discipline
0512  * @tty: the terminal to set
0513  * @disc: the line discipline number
0514  *
0515  * Set the discipline of a tty line. Must be called from a process context. The
0516  * ldisc change logic has to protect itself against any overlapping ldisc
0517  * change (including on the other end of pty pairs), the close of one side of a
0518  * tty/pty pair, and eventually hangup.
0519  */
0520 int tty_set_ldisc(struct tty_struct *tty, int disc)
0521 {
0522     int retval;
0523     struct tty_ldisc *old_ldisc, *new_ldisc;
0524 
0525     new_ldisc = tty_ldisc_get(tty, disc);
0526     if (IS_ERR(new_ldisc))
0527         return PTR_ERR(new_ldisc);
0528 
0529     tty_lock(tty);
0530     retval = tty_ldisc_lock(tty, 5 * HZ);
0531     if (retval)
0532         goto err;
0533 
0534     if (!tty->ldisc) {
0535         retval = -EIO;
0536         goto out;
0537     }
0538 
0539     /* Check the no-op case */
0540     if (tty->ldisc->ops->num == disc)
0541         goto out;
0542 
0543     if (test_bit(TTY_HUPPED, &tty->flags)) {
0544         /* We were raced by hangup */
0545         retval = -EIO;
0546         goto out;
0547     }
0548 
0549     old_ldisc = tty->ldisc;
0550 
0551     /* Shutdown the old discipline. */
0552     tty_ldisc_close(tty, old_ldisc);
0553 
0554     /* Now set up the new line discipline. */
0555     tty->ldisc = new_ldisc;
0556     tty_set_termios_ldisc(tty, disc);
0557 
0558     retval = tty_ldisc_open(tty, new_ldisc);
0559     if (retval < 0) {
0560         /* Back to the old one or N_TTY if we can't */
0561         tty_ldisc_put(new_ldisc);
0562         tty_ldisc_restore(tty, old_ldisc);
0563     }
0564 
0565     if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc) {
0566         down_read(&tty->termios_rwsem);
0567         tty->ops->set_ldisc(tty);
0568         up_read(&tty->termios_rwsem);
0569     }
0570 
0571     /*
0572      * At this point we hold a reference to the new ldisc and a
0573      * reference to the old ldisc, or we hold two references to
0574      * the old ldisc (if it was restored as part of error cleanup
0575      * above). In either case, releasing a single reference from
0576      * the old ldisc is correct.
0577      */
0578     new_ldisc = old_ldisc;
0579 out:
0580     tty_ldisc_unlock(tty);
0581 
0582     /*
0583      * Restart the work queue in case no characters kick it off. Safe if
0584      * already running
0585      */
0586     tty_buffer_restart_work(tty->port);
0587 err:
0588     tty_ldisc_put(new_ldisc);   /* drop the extra reference */
0589     tty_unlock(tty);
0590     return retval;
0591 }
0592 EXPORT_SYMBOL_GPL(tty_set_ldisc);
0593 
0594 /**
0595  * tty_ldisc_kill   -   teardown ldisc
0596  * @tty: tty being released
0597  *
0598  * Perform final close of the ldisc and reset @tty->ldisc
0599  */
0600 static void tty_ldisc_kill(struct tty_struct *tty)
0601 {
0602     lockdep_assert_held_write(&tty->ldisc_sem);
0603     if (!tty->ldisc)
0604         return;
0605     /*
0606      * Now kill off the ldisc
0607      */
0608     tty_ldisc_close(tty, tty->ldisc);
0609     tty_ldisc_put(tty->ldisc);
0610     /* Force an oops if we mess this up */
0611     tty->ldisc = NULL;
0612 }
0613 
0614 /**
0615  * tty_reset_termios    -   reset terminal state
0616  * @tty: tty to reset
0617  *
0618  * Restore a terminal to the driver default state.
0619  */
0620 static void tty_reset_termios(struct tty_struct *tty)
0621 {
0622     down_write(&tty->termios_rwsem);
0623     tty->termios = tty->driver->init_termios;
0624     tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
0625     tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
0626     up_write(&tty->termios_rwsem);
0627 }
0628 
0629 
0630 /**
0631  * tty_ldisc_reinit -   reinitialise the tty ldisc
0632  * @tty: tty to reinit
0633  * @disc: line discipline to reinitialize
0634  *
0635  * Completely reinitialize the line discipline state, by closing the current
0636  * instance, if there is one, and opening a new instance. If an error occurs
0637  * opening the new non-%N_TTY instance, the instance is dropped and @tty->ldisc
0638  * reset to %NULL. The caller can then retry with %N_TTY instead.
0639  *
0640  * Returns: 0 if successful, otherwise error code < 0
0641  */
0642 int tty_ldisc_reinit(struct tty_struct *tty, int disc)
0643 {
0644     struct tty_ldisc *ld;
0645     int retval;
0646 
0647     lockdep_assert_held_write(&tty->ldisc_sem);
0648     ld = tty_ldisc_get(tty, disc);
0649     if (IS_ERR(ld)) {
0650         BUG_ON(disc == N_TTY);
0651         return PTR_ERR(ld);
0652     }
0653 
0654     if (tty->ldisc) {
0655         tty_ldisc_close(tty, tty->ldisc);
0656         tty_ldisc_put(tty->ldisc);
0657     }
0658 
0659     /* switch the line discipline */
0660     tty->ldisc = ld;
0661     tty_set_termios_ldisc(tty, disc);
0662     retval = tty_ldisc_open(tty, tty->ldisc);
0663     if (retval) {
0664         tty_ldisc_put(tty->ldisc);
0665         tty->ldisc = NULL;
0666     }
0667     return retval;
0668 }
0669 
0670 /**
0671  * tty_ldisc_hangup -   hangup ldisc reset
0672  * @tty: tty being hung up
0673  * @reinit: whether to re-initialise the tty
0674  *
0675  * Some tty devices reset their termios when they receive a hangup event. In
0676  * that situation we must also switch back to %N_TTY properly before we reset
0677  * the termios data.
0678  *
0679  * Locking: We can take the ldisc mutex as the rest of the code is careful to
0680  * allow for this.
0681  *
0682  * In the pty pair case this occurs in the close() path of the tty itself so we
0683  * must be careful about locking rules.
0684  */
0685 void tty_ldisc_hangup(struct tty_struct *tty, bool reinit)
0686 {
0687     struct tty_ldisc *ld;
0688 
0689     tty_ldisc_debug(tty, "%p: hangup\n", tty->ldisc);
0690 
0691     ld = tty_ldisc_ref(tty);
0692     if (ld != NULL) {
0693         if (ld->ops->flush_buffer)
0694             ld->ops->flush_buffer(tty);
0695         tty_driver_flush_buffer(tty);
0696         if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
0697             ld->ops->write_wakeup)
0698             ld->ops->write_wakeup(tty);
0699         if (ld->ops->hangup)
0700             ld->ops->hangup(tty);
0701         tty_ldisc_deref(ld);
0702     }
0703 
0704     wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT);
0705     wake_up_interruptible_poll(&tty->read_wait, EPOLLIN);
0706 
0707     /*
0708      * Shutdown the current line discipline, and reset it to
0709      * N_TTY if need be.
0710      *
0711      * Avoid racing set_ldisc or tty_ldisc_release
0712      */
0713     tty_ldisc_lock(tty, MAX_SCHEDULE_TIMEOUT);
0714 
0715     if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
0716         tty_reset_termios(tty);
0717 
0718     if (tty->ldisc) {
0719         if (reinit) {
0720             if (tty_ldisc_reinit(tty, tty->termios.c_line) < 0 &&
0721                 tty_ldisc_reinit(tty, N_TTY) < 0)
0722                 WARN_ON(tty_ldisc_reinit(tty, N_NULL) < 0);
0723         } else
0724             tty_ldisc_kill(tty);
0725     }
0726     tty_ldisc_unlock(tty);
0727 }
0728 
0729 /**
0730  * tty_ldisc_setup  -   open line discipline
0731  * @tty: tty being shut down
0732  * @o_tty: pair tty for pty/tty pairs
0733  *
0734  * Called during the initial open of a tty/pty pair in order to set up the line
0735  * disciplines and bind them to the @tty. This has no locking issues as the
0736  * device isn't yet active.
0737  */
0738 int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
0739 {
0740     int retval = tty_ldisc_open(tty, tty->ldisc);
0741 
0742     if (retval)
0743         return retval;
0744 
0745     if (o_tty) {
0746         /*
0747          * Called without o_tty->ldisc_sem held, as o_tty has been
0748          * just allocated and no one has a reference to it.
0749          */
0750         retval = tty_ldisc_open(o_tty, o_tty->ldisc);
0751         if (retval) {
0752             tty_ldisc_close(tty, tty->ldisc);
0753             return retval;
0754         }
0755     }
0756     return 0;
0757 }
0758 
0759 /**
0760  * tty_ldisc_release    -   release line discipline
0761  * @tty: tty being shut down (or one end of pty pair)
0762  *
0763  * Called during the final close of a tty or a pty pair in order to shut down
0764  * the line discpline layer. On exit, each tty's ldisc is %NULL.
0765  */
0766 void tty_ldisc_release(struct tty_struct *tty)
0767 {
0768     struct tty_struct *o_tty = tty->link;
0769 
0770     /*
0771      * Shutdown this line discipline. As this is the final close,
0772      * it does not race with the set_ldisc code path.
0773      */
0774 
0775     tty_ldisc_lock_pair(tty, o_tty);
0776     tty_ldisc_kill(tty);
0777     if (o_tty)
0778         tty_ldisc_kill(o_tty);
0779     tty_ldisc_unlock_pair(tty, o_tty);
0780 
0781     /*
0782      * And the memory resources remaining (buffers, termios) will be
0783      * disposed of when the kref hits zero
0784      */
0785 
0786     tty_ldisc_debug(tty, "released\n");
0787 }
0788 
0789 /**
0790  * tty_ldisc_init   -   ldisc setup for new tty
0791  * @tty: tty being allocated
0792  *
0793  * Set up the line discipline objects for a newly allocated tty. Note that the
0794  * tty structure is not completely set up when this call is made.
0795  */
0796 int tty_ldisc_init(struct tty_struct *tty)
0797 {
0798     struct tty_ldisc *ld = tty_ldisc_get(tty, N_TTY);
0799 
0800     if (IS_ERR(ld))
0801         return PTR_ERR(ld);
0802     tty->ldisc = ld;
0803     return 0;
0804 }
0805 
0806 /**
0807  * tty_ldisc_deinit -   ldisc cleanup for new tty
0808  * @tty: tty that was allocated recently
0809  *
0810  * The tty structure must not be completely set up (tty_ldisc_setup()) when
0811  * this call is made.
0812  */
0813 void tty_ldisc_deinit(struct tty_struct *tty)
0814 {
0815     /* no ldisc_sem, tty is being destroyed */
0816     if (tty->ldisc)
0817         tty_ldisc_put(tty->ldisc);
0818     tty->ldisc = NULL;
0819 }
0820 
0821 static struct ctl_table tty_table[] = {
0822     {
0823         .procname   = "ldisc_autoload",
0824         .data       = &tty_ldisc_autoload,
0825         .maxlen     = sizeof(tty_ldisc_autoload),
0826         .mode       = 0644,
0827         .proc_handler   = proc_dointvec,
0828         .extra1     = SYSCTL_ZERO,
0829         .extra2     = SYSCTL_ONE,
0830     },
0831     { }
0832 };
0833 
0834 static struct ctl_table tty_dir_table[] = {
0835     {
0836         .procname   = "tty",
0837         .mode       = 0555,
0838         .child      = tty_table,
0839     },
0840     { }
0841 };
0842 
0843 static struct ctl_table tty_root_table[] = {
0844     {
0845         .procname   = "dev",
0846         .mode       = 0555,
0847         .child      = tty_dir_table,
0848     },
0849     { }
0850 };
0851 
0852 void tty_sysctl_init(void)
0853 {
0854     register_sysctl_table(tty_root_table);
0855 }