tty_ldisc.c 20 KB

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