tty_ldisc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. #include <linux/types.h>
  2. #include <linux/errno.h>
  3. #include <linux/kmod.h>
  4. #include <linux/sched.h>
  5. #include <linux/interrupt.h>
  6. #include <linux/tty.h>
  7. #include <linux/tty_driver.h>
  8. #include <linux/file.h>
  9. #include <linux/mm.h>
  10. #include <linux/string.h>
  11. #include <linux/slab.h>
  12. #include <linux/poll.h>
  13. #include <linux/proc_fs.h>
  14. #include <linux/init.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. #undef LDISC_DEBUG_HANGUP
  23. #ifdef LDISC_DEBUG_HANGUP
  24. #define tty_ldisc_debug(tty, f, args...) ({ \
  25. char __b[64]; \
  26. printk(KERN_DEBUG "%s: %s: " f, __func__, tty_name(tty, __b), ##args); \
  27. })
  28. #else
  29. #define tty_ldisc_debug(tty, f, args...)
  30. #endif
  31. /* lockdep nested classes for tty->ldisc_sem */
  32. enum {
  33. LDISC_SEM_NORMAL,
  34. LDISC_SEM_OTHER,
  35. };
  36. /*
  37. * This guards the refcounted line discipline lists. The lock
  38. * must be taken with irqs off because there are hangup path
  39. * callers who will do ldisc lookups and cannot sleep.
  40. */
  41. static DEFINE_RAW_SPINLOCK(tty_ldiscs_lock);
  42. static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
  43. /* Line disc dispatch table */
  44. static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
  45. /**
  46. * tty_register_ldisc - install a line discipline
  47. * @disc: ldisc number
  48. * @new_ldisc: pointer to the ldisc object
  49. *
  50. * Installs a new line discipline into the kernel. The discipline
  51. * is set up as unreferenced and then made available to the kernel
  52. * from this point onwards.
  53. *
  54. * Locking:
  55. * takes tty_ldiscs_lock to guard against ldisc races
  56. */
  57. int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
  58. {
  59. unsigned long flags;
  60. int ret = 0;
  61. if (disc < N_TTY || disc >= NR_LDISCS)
  62. return -EINVAL;
  63. raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
  64. tty_ldiscs[disc] = new_ldisc;
  65. new_ldisc->num = disc;
  66. new_ldisc->refcount = 0;
  67. raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
  68. return ret;
  69. }
  70. EXPORT_SYMBOL(tty_register_ldisc);
  71. /**
  72. * tty_unregister_ldisc - unload a line discipline
  73. * @disc: ldisc number
  74. * @new_ldisc: pointer to the ldisc object
  75. *
  76. * Remove a line discipline from the kernel providing it is not
  77. * currently in use.
  78. *
  79. * Locking:
  80. * takes tty_ldiscs_lock to guard against ldisc races
  81. */
  82. int tty_unregister_ldisc(int disc)
  83. {
  84. unsigned long flags;
  85. int ret = 0;
  86. if (disc < N_TTY || disc >= NR_LDISCS)
  87. return -EINVAL;
  88. raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
  89. if (tty_ldiscs[disc]->refcount)
  90. ret = -EBUSY;
  91. else
  92. tty_ldiscs[disc] = NULL;
  93. raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
  94. return ret;
  95. }
  96. EXPORT_SYMBOL(tty_unregister_ldisc);
  97. static struct tty_ldisc_ops *get_ldops(int disc)
  98. {
  99. unsigned long flags;
  100. struct tty_ldisc_ops *ldops, *ret;
  101. raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
  102. ret = ERR_PTR(-EINVAL);
  103. ldops = tty_ldiscs[disc];
  104. if (ldops) {
  105. ret = ERR_PTR(-EAGAIN);
  106. if (try_module_get(ldops->owner)) {
  107. ldops->refcount++;
  108. ret = ldops;
  109. }
  110. }
  111. raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
  112. return ret;
  113. }
  114. static void put_ldops(struct tty_ldisc_ops *ldops)
  115. {
  116. unsigned long flags;
  117. raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
  118. ldops->refcount--;
  119. module_put(ldops->owner);
  120. raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
  121. }
  122. /**
  123. * tty_ldisc_get - take a reference to an ldisc
  124. * @disc: ldisc number
  125. *
  126. * Takes a reference to a line discipline. Deals with refcounts and
  127. * module locking counts. Returns NULL if the discipline is not available.
  128. * Returns a pointer to the discipline and bumps the ref count if it is
  129. * available
  130. *
  131. * Locking:
  132. * takes tty_ldiscs_lock to guard against ldisc races
  133. */
  134. static struct tty_ldisc *tty_ldisc_get(int disc)
  135. {
  136. struct tty_ldisc *ld;
  137. struct tty_ldisc_ops *ldops;
  138. if (disc < N_TTY || disc >= NR_LDISCS)
  139. return ERR_PTR(-EINVAL);
  140. /*
  141. * Get the ldisc ops - we may need to request them to be loaded
  142. * dynamically and try again.
  143. */
  144. ldops = get_ldops(disc);
  145. if (IS_ERR(ldops)) {
  146. request_module("tty-ldisc-%d", disc);
  147. ldops = get_ldops(disc);
  148. if (IS_ERR(ldops))
  149. return ERR_CAST(ldops);
  150. }
  151. ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL);
  152. if (ld == NULL) {
  153. put_ldops(ldops);
  154. return ERR_PTR(-ENOMEM);
  155. }
  156. ld->ops = ldops;
  157. atomic_set(&ld->users, 1);
  158. init_waitqueue_head(&ld->wq_idle);
  159. return ld;
  160. }
  161. /**
  162. * tty_ldisc_put - release the ldisc
  163. *
  164. * Complement of tty_ldisc_get().
  165. */
  166. static inline void tty_ldisc_put(struct tty_ldisc *ld)
  167. {
  168. unsigned long flags;
  169. if (WARN_ON_ONCE(!ld))
  170. return;
  171. raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
  172. /* unreleased reader reference(s) will cause this WARN */
  173. WARN_ON(!atomic_dec_and_test(&ld->users));
  174. ld->ops->refcount--;
  175. module_put(ld->ops->owner);
  176. kfree(ld);
  177. raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
  178. }
  179. static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
  180. {
  181. return (*pos < NR_LDISCS) ? pos : NULL;
  182. }
  183. static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
  184. {
  185. (*pos)++;
  186. return (*pos < NR_LDISCS) ? pos : NULL;
  187. }
  188. static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
  189. {
  190. }
  191. static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
  192. {
  193. int i = *(loff_t *)v;
  194. struct tty_ldisc_ops *ldops;
  195. ldops = get_ldops(i);
  196. if (IS_ERR(ldops))
  197. return 0;
  198. seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
  199. put_ldops(ldops);
  200. return 0;
  201. }
  202. static const struct seq_operations tty_ldiscs_seq_ops = {
  203. .start = tty_ldiscs_seq_start,
  204. .next = tty_ldiscs_seq_next,
  205. .stop = tty_ldiscs_seq_stop,
  206. .show = tty_ldiscs_seq_show,
  207. };
  208. static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
  209. {
  210. return seq_open(file, &tty_ldiscs_seq_ops);
  211. }
  212. const struct file_operations tty_ldiscs_proc_fops = {
  213. .owner = THIS_MODULE,
  214. .open = proc_tty_ldiscs_open,
  215. .read = seq_read,
  216. .llseek = seq_lseek,
  217. .release = seq_release,
  218. };
  219. /**
  220. * tty_ldisc_try - internal helper
  221. * @tty: the tty
  222. *
  223. * Make a single attempt to grab and bump the refcount on
  224. * the tty ldisc. Return 0 on failure or 1 on success. This is
  225. * used to implement both the waiting and non waiting versions
  226. * of tty_ldisc_ref
  227. *
  228. * Locking: takes tty_ldiscs_lock
  229. */
  230. static struct tty_ldisc *tty_ldisc_try(struct tty_struct *tty)
  231. {
  232. unsigned long flags;
  233. struct tty_ldisc *ld;
  234. /* FIXME: this allows reference acquire after TTY_LDISC is cleared */
  235. raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
  236. ld = NULL;
  237. if (test_bit(TTY_LDISC, &tty->flags) && tty->ldisc) {
  238. ld = tty->ldisc;
  239. atomic_inc(&ld->users);
  240. }
  241. raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
  242. return ld;
  243. }
  244. /**
  245. * tty_ldisc_ref_wait - wait for the tty ldisc
  246. * @tty: tty device
  247. *
  248. * Dereference the line discipline for the terminal and take a
  249. * reference to it. If the line discipline is in flux then
  250. * wait patiently until it changes.
  251. *
  252. * Note: Must not be called from an IRQ/timer context. The caller
  253. * must also be careful not to hold other locks that will deadlock
  254. * against a discipline change, such as an existing ldisc reference
  255. * (which we check for)
  256. *
  257. * Locking: call functions take tty_ldiscs_lock
  258. */
  259. struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
  260. {
  261. struct tty_ldisc *ld;
  262. /* wait_event is a macro */
  263. wait_event(tty_ldisc_wait, (ld = tty_ldisc_try(tty)) != NULL);
  264. return ld;
  265. }
  266. EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
  267. /**
  268. * tty_ldisc_ref - get the tty ldisc
  269. * @tty: tty device
  270. *
  271. * Dereference the line discipline for the terminal and take a
  272. * reference to it. If the line discipline is in flux then
  273. * return NULL. Can be called from IRQ and timer functions.
  274. *
  275. * Locking: called functions take tty_ldiscs_lock
  276. */
  277. struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
  278. {
  279. return tty_ldisc_try(tty);
  280. }
  281. EXPORT_SYMBOL_GPL(tty_ldisc_ref);
  282. /**
  283. * tty_ldisc_deref - free a tty ldisc reference
  284. * @ld: reference to free up
  285. *
  286. * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
  287. * be called in IRQ context.
  288. *
  289. * Locking: takes tty_ldiscs_lock
  290. */
  291. void tty_ldisc_deref(struct tty_ldisc *ld)
  292. {
  293. unsigned long flags;
  294. if (WARN_ON_ONCE(!ld))
  295. return;
  296. raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
  297. /*
  298. * WARNs if one-too-many reader references were released
  299. * - the last reference must be released with tty_ldisc_put
  300. */
  301. WARN_ON(atomic_dec_and_test(&ld->users));
  302. raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
  303. if (waitqueue_active(&ld->wq_idle))
  304. wake_up(&ld->wq_idle);
  305. }
  306. EXPORT_SYMBOL_GPL(tty_ldisc_deref);
  307. static inline int __lockfunc
  308. tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
  309. {
  310. return ldsem_down_write(&tty->ldisc_sem, timeout);
  311. }
  312. static inline int __lockfunc
  313. tty_ldisc_lock_nested(struct tty_struct *tty, unsigned long timeout)
  314. {
  315. return ldsem_down_write_nested(&tty->ldisc_sem,
  316. LDISC_SEM_OTHER, timeout);
  317. }
  318. static inline void tty_ldisc_unlock(struct tty_struct *tty)
  319. {
  320. return ldsem_up_write(&tty->ldisc_sem);
  321. }
  322. static int __lockfunc
  323. tty_ldisc_lock_pair_timeout(struct tty_struct *tty, struct tty_struct *tty2,
  324. unsigned long timeout)
  325. {
  326. int ret;
  327. if (tty < tty2) {
  328. ret = tty_ldisc_lock(tty, timeout);
  329. if (ret) {
  330. ret = tty_ldisc_lock_nested(tty2, timeout);
  331. if (!ret)
  332. tty_ldisc_unlock(tty);
  333. }
  334. } else {
  335. /* if this is possible, it has lots of implications */
  336. WARN_ON_ONCE(tty == tty2);
  337. if (tty2 && tty != tty2) {
  338. ret = tty_ldisc_lock(tty2, timeout);
  339. if (ret) {
  340. ret = tty_ldisc_lock_nested(tty, timeout);
  341. if (!ret)
  342. tty_ldisc_unlock(tty2);
  343. }
  344. } else
  345. ret = tty_ldisc_lock(tty, timeout);
  346. }
  347. if (!ret)
  348. return -EBUSY;
  349. set_bit(TTY_LDISC_HALTED, &tty->flags);
  350. if (tty2)
  351. set_bit(TTY_LDISC_HALTED, &tty2->flags);
  352. return 0;
  353. }
  354. static void __lockfunc
  355. tty_ldisc_lock_pair(struct tty_struct *tty, struct tty_struct *tty2)
  356. {
  357. tty_ldisc_lock_pair_timeout(tty, tty2, MAX_SCHEDULE_TIMEOUT);
  358. }
  359. static void __lockfunc tty_ldisc_unlock_pair(struct tty_struct *tty,
  360. struct tty_struct *tty2)
  361. {
  362. tty_ldisc_unlock(tty);
  363. if (tty2)
  364. tty_ldisc_unlock(tty2);
  365. }
  366. static void __lockfunc tty_ldisc_enable_pair(struct tty_struct *tty,
  367. struct tty_struct *tty2)
  368. {
  369. clear_bit(TTY_LDISC_HALTED, &tty->flags);
  370. if (tty2)
  371. clear_bit(TTY_LDISC_HALTED, &tty2->flags);
  372. tty_ldisc_unlock_pair(tty, tty2);
  373. }
  374. /**
  375. * tty_ldisc_enable - allow ldisc use
  376. * @tty: terminal to activate ldisc on
  377. *
  378. * Set the TTY_LDISC flag when the line discipline can be called
  379. * again. Do necessary wakeups for existing sleepers. Clear the LDISC
  380. * changing flag to indicate any ldisc change is now over.
  381. *
  382. * Note: nobody should set the TTY_LDISC bit except via this function.
  383. * Clearing directly is allowed.
  384. */
  385. static void tty_ldisc_enable(struct tty_struct *tty)
  386. {
  387. clear_bit(TTY_LDISC_HALTED, &tty->flags);
  388. set_bit(TTY_LDISC, &tty->flags);
  389. clear_bit(TTY_LDISC_CHANGING, &tty->flags);
  390. wake_up(&tty_ldisc_wait);
  391. }
  392. /**
  393. * tty_ldisc_flush - flush line discipline queue
  394. * @tty: tty
  395. *
  396. * Flush the line discipline queue (if any) for this tty. If there
  397. * is no line discipline active this is a no-op.
  398. */
  399. void tty_ldisc_flush(struct tty_struct *tty)
  400. {
  401. struct tty_ldisc *ld = tty_ldisc_ref(tty);
  402. if (ld) {
  403. if (ld->ops->flush_buffer)
  404. ld->ops->flush_buffer(tty);
  405. tty_ldisc_deref(ld);
  406. }
  407. tty_buffer_flush(tty);
  408. }
  409. EXPORT_SYMBOL_GPL(tty_ldisc_flush);
  410. /**
  411. * tty_set_termios_ldisc - set ldisc field
  412. * @tty: tty structure
  413. * @num: line discipline number
  414. *
  415. * This is probably overkill for real world processors but
  416. * they are not on hot paths so a little discipline won't do
  417. * any harm.
  418. *
  419. * Locking: takes termios_mutex
  420. */
  421. static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
  422. {
  423. mutex_lock(&tty->termios_mutex);
  424. tty->termios.c_line = num;
  425. mutex_unlock(&tty->termios_mutex);
  426. }
  427. /**
  428. * tty_ldisc_open - open a line discipline
  429. * @tty: tty we are opening the ldisc on
  430. * @ld: discipline to open
  431. *
  432. * A helper opening method. Also a convenient debugging and check
  433. * point.
  434. *
  435. * Locking: always called with BTM already held.
  436. */
  437. static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
  438. {
  439. WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
  440. if (ld->ops->open) {
  441. int ret;
  442. /* BTM here locks versus a hangup event */
  443. ret = ld->ops->open(tty);
  444. if (ret)
  445. clear_bit(TTY_LDISC_OPEN, &tty->flags);
  446. return ret;
  447. }
  448. return 0;
  449. }
  450. /**
  451. * tty_ldisc_close - close a line discipline
  452. * @tty: tty we are opening the ldisc on
  453. * @ld: discipline to close
  454. *
  455. * A helper close method. Also a convenient debugging and check
  456. * point.
  457. */
  458. static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
  459. {
  460. WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
  461. clear_bit(TTY_LDISC_OPEN, &tty->flags);
  462. if (ld->ops->close)
  463. ld->ops->close(tty);
  464. }
  465. /**
  466. * tty_ldisc_restore - helper for tty ldisc change
  467. * @tty: tty to recover
  468. * @old: previous ldisc
  469. *
  470. * Restore the previous line discipline or N_TTY when a line discipline
  471. * change fails due to an open error
  472. */
  473. static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
  474. {
  475. char buf[64];
  476. struct tty_ldisc *new_ldisc;
  477. int r;
  478. /* There is an outstanding reference here so this is safe */
  479. old = tty_ldisc_get(old->ops->num);
  480. WARN_ON(IS_ERR(old));
  481. tty->ldisc = old;
  482. tty_set_termios_ldisc(tty, old->ops->num);
  483. if (tty_ldisc_open(tty, old) < 0) {
  484. tty_ldisc_put(old);
  485. /* This driver is always present */
  486. new_ldisc = tty_ldisc_get(N_TTY);
  487. if (IS_ERR(new_ldisc))
  488. panic("n_tty: get");
  489. tty->ldisc = new_ldisc;
  490. tty_set_termios_ldisc(tty, N_TTY);
  491. r = tty_ldisc_open(tty, new_ldisc);
  492. if (r < 0)
  493. panic("Couldn't open N_TTY ldisc for "
  494. "%s --- error %d.",
  495. tty_name(tty, buf), r);
  496. }
  497. }
  498. /**
  499. * tty_ldisc_wait_idle - wait for the ldisc to become idle
  500. * @tty: tty to wait for
  501. * @timeout: for how long to wait at most
  502. *
  503. * Wait for the line discipline to become idle. The discipline must
  504. * have been halted for this to guarantee it remains idle.
  505. */
  506. static int tty_ldisc_wait_idle(struct tty_struct *tty, long timeout)
  507. {
  508. long ret;
  509. ret = wait_event_timeout(tty->ldisc->wq_idle,
  510. atomic_read(&tty->ldisc->users) == 1, timeout);
  511. return ret > 0 ? 0 : -EBUSY;
  512. }
  513. /**
  514. * tty_ldisc_halt - shut down the line discipline
  515. * @tty: tty device
  516. * @o_tty: paired pty device (can be NULL)
  517. * @timeout: # of jiffies to wait for ldisc refs to be released
  518. *
  519. * Shut down the line discipline and work queue for this tty device and
  520. * its paired pty (if exists). Clearing the TTY_LDISC flag ensures
  521. * no further references can be obtained, while waiting for existing
  522. * references to be released ensures no more data is fed to the ldisc.
  523. *
  524. * You need to do a 'flush_scheduled_work()' (outside the ldisc_mutex)
  525. * in order to make sure any currently executing ldisc work is also
  526. * flushed.
  527. */
  528. static int tty_ldisc_halt(struct tty_struct *tty, struct tty_struct *o_tty,
  529. long timeout)
  530. {
  531. int retval;
  532. clear_bit(TTY_LDISC, &tty->flags);
  533. if (o_tty)
  534. clear_bit(TTY_LDISC, &o_tty->flags);
  535. retval = tty_ldisc_wait_idle(tty, timeout);
  536. if (!retval && o_tty)
  537. retval = tty_ldisc_wait_idle(o_tty, timeout);
  538. if (retval)
  539. return retval;
  540. set_bit(TTY_LDISC_HALTED, &tty->flags);
  541. if (o_tty)
  542. set_bit(TTY_LDISC_HALTED, &o_tty->flags);
  543. return 0;
  544. }
  545. /**
  546. * tty_ldisc_hangup_halt - halt the line discipline for hangup
  547. * @tty: tty being hung up
  548. *
  549. * Shut down the line discipline and work queue for the tty device
  550. * being hungup. Clear the TTY_LDISC flag to ensure no further
  551. * references can be obtained and wait for remaining references to be
  552. * released to ensure no more data is fed to this ldisc.
  553. * Caller must hold legacy and ->ldisc_mutex.
  554. *
  555. * NB: tty_set_ldisc() is prevented from changing the ldisc concurrently
  556. * with this function by checking the TTY_HUPPING flag.
  557. */
  558. static bool tty_ldisc_hangup_halt(struct tty_struct *tty)
  559. {
  560. char cur_n[TASK_COMM_LEN], tty_n[64];
  561. long timeout = 3 * HZ;
  562. clear_bit(TTY_LDISC, &tty->flags);
  563. if (tty->ldisc) { /* Not yet closed */
  564. tty_unlock(tty);
  565. while (tty_ldisc_wait_idle(tty, timeout) == -EBUSY) {
  566. timeout = MAX_SCHEDULE_TIMEOUT;
  567. printk_ratelimited(KERN_WARNING
  568. "%s: waiting (%s) for %s took too long, but we keep waiting...\n",
  569. __func__, get_task_comm(cur_n, current),
  570. tty_name(tty, tty_n));
  571. }
  572. set_bit(TTY_LDISC_HALTED, &tty->flags);
  573. /* must reacquire both locks and preserve lock order */
  574. mutex_unlock(&tty->ldisc_mutex);
  575. tty_lock(tty);
  576. mutex_lock(&tty->ldisc_mutex);
  577. }
  578. return !!tty->ldisc;
  579. }
  580. /**
  581. * tty_set_ldisc - set line discipline
  582. * @tty: the terminal to set
  583. * @ldisc: the line discipline
  584. *
  585. * Set the discipline of a tty line. Must be called from a process
  586. * context. The ldisc change logic has to protect itself against any
  587. * overlapping ldisc change (including on the other end of pty pairs),
  588. * the close of one side of a tty/pty pair, and eventually hangup.
  589. *
  590. * Locking: takes tty_ldiscs_lock, termios_mutex
  591. */
  592. int tty_set_ldisc(struct tty_struct *tty, int ldisc)
  593. {
  594. int retval;
  595. struct tty_ldisc *o_ldisc, *new_ldisc;
  596. struct tty_struct *o_tty;
  597. new_ldisc = tty_ldisc_get(ldisc);
  598. if (IS_ERR(new_ldisc))
  599. return PTR_ERR(new_ldisc);
  600. tty_lock(tty);
  601. /*
  602. * We need to look at the tty locking here for pty/tty pairs
  603. * when both sides try to change in parallel.
  604. */
  605. o_tty = tty->link; /* o_tty is the pty side or NULL */
  606. /*
  607. * Check the no-op case
  608. */
  609. if (tty->ldisc->ops->num == ldisc) {
  610. tty_unlock(tty);
  611. tty_ldisc_put(new_ldisc);
  612. return 0;
  613. }
  614. mutex_lock(&tty->ldisc_mutex);
  615. /*
  616. * We could be midstream of another ldisc change which has
  617. * dropped the lock during processing. If so we need to wait.
  618. */
  619. while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) {
  620. mutex_unlock(&tty->ldisc_mutex);
  621. tty_unlock(tty);
  622. wait_event(tty_ldisc_wait,
  623. test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0);
  624. tty_lock(tty);
  625. mutex_lock(&tty->ldisc_mutex);
  626. }
  627. set_bit(TTY_LDISC_CHANGING, &tty->flags);
  628. /*
  629. * No more input please, we are switching. The new ldisc
  630. * will update this value in the ldisc open function
  631. */
  632. tty->receive_room = 0;
  633. o_ldisc = tty->ldisc;
  634. tty_unlock(tty);
  635. /*
  636. * Make sure we don't change while someone holds a
  637. * reference to the line discipline. The TTY_LDISC bit
  638. * prevents anyone taking a reference once it is clear.
  639. * We need the lock to avoid racing reference takers.
  640. *
  641. * We must clear the TTY_LDISC bit here to avoid a livelock
  642. * with a userspace app continually trying to use the tty in
  643. * parallel to the change and re-referencing the tty.
  644. */
  645. retval = tty_ldisc_halt(tty, o_tty, 5 * HZ);
  646. /*
  647. * Wait for hangup to complete, if pending.
  648. * We must drop the mutex here in case a hangup is also in process.
  649. */
  650. mutex_unlock(&tty->ldisc_mutex);
  651. flush_work(&tty->hangup_work);
  652. tty_lock(tty);
  653. mutex_lock(&tty->ldisc_mutex);
  654. /* handle wait idle failure locked */
  655. if (retval) {
  656. tty_ldisc_put(new_ldisc);
  657. goto enable;
  658. }
  659. if (test_bit(TTY_HUPPING, &tty->flags)) {
  660. /* We were raced by the hangup method. It will have stomped
  661. the ldisc data and closed the ldisc down */
  662. clear_bit(TTY_LDISC_CHANGING, &tty->flags);
  663. mutex_unlock(&tty->ldisc_mutex);
  664. tty_ldisc_put(new_ldisc);
  665. tty_unlock(tty);
  666. return -EIO;
  667. }
  668. /* Shutdown the current discipline. */
  669. tty_ldisc_close(tty, o_ldisc);
  670. /* Now set up the new line discipline. */
  671. tty->ldisc = new_ldisc;
  672. tty_set_termios_ldisc(tty, ldisc);
  673. retval = tty_ldisc_open(tty, new_ldisc);
  674. if (retval < 0) {
  675. /* Back to the old one or N_TTY if we can't */
  676. tty_ldisc_put(new_ldisc);
  677. tty_ldisc_restore(tty, o_ldisc);
  678. }
  679. /* At this point we hold a reference to the new ldisc and a
  680. a reference to the old ldisc. If we ended up flipping back
  681. to the existing ldisc we have two references to it */
  682. if (tty->ldisc->ops->num != o_ldisc->ops->num && tty->ops->set_ldisc)
  683. tty->ops->set_ldisc(tty);
  684. tty_ldisc_put(o_ldisc);
  685. enable:
  686. /*
  687. * Allow ldisc referencing to occur again
  688. */
  689. tty_ldisc_enable(tty);
  690. if (o_tty)
  691. tty_ldisc_enable(o_tty);
  692. /* Restart the work queue in case no characters kick it off. Safe if
  693. already running */
  694. schedule_work(&tty->port->buf.work);
  695. if (o_tty)
  696. schedule_work(&o_tty->port->buf.work);
  697. mutex_unlock(&tty->ldisc_mutex);
  698. tty_unlock(tty);
  699. return retval;
  700. }
  701. /**
  702. * tty_reset_termios - reset terminal state
  703. * @tty: tty to reset
  704. *
  705. * Restore a terminal to the driver default state.
  706. */
  707. static void tty_reset_termios(struct tty_struct *tty)
  708. {
  709. mutex_lock(&tty->termios_mutex);
  710. tty->termios = tty->driver->init_termios;
  711. tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
  712. tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
  713. mutex_unlock(&tty->termios_mutex);
  714. }
  715. /**
  716. * tty_ldisc_reinit - reinitialise the tty ldisc
  717. * @tty: tty to reinit
  718. * @ldisc: line discipline to reinitialize
  719. *
  720. * Switch the tty to a line discipline and leave the ldisc
  721. * state closed
  722. */
  723. static int tty_ldisc_reinit(struct tty_struct *tty, int ldisc)
  724. {
  725. struct tty_ldisc *ld = tty_ldisc_get(ldisc);
  726. if (IS_ERR(ld))
  727. return -1;
  728. tty_ldisc_close(tty, tty->ldisc);
  729. tty_ldisc_put(tty->ldisc);
  730. /*
  731. * Switch the line discipline back
  732. */
  733. tty->ldisc = ld;
  734. tty_set_termios_ldisc(tty, ldisc);
  735. return 0;
  736. }
  737. /**
  738. * tty_ldisc_hangup - hangup ldisc reset
  739. * @tty: tty being hung up
  740. *
  741. * Some tty devices reset their termios when they receive a hangup
  742. * event. In that situation we must also switch back to N_TTY properly
  743. * before we reset the termios data.
  744. *
  745. * Locking: We can take the ldisc mutex as the rest of the code is
  746. * careful to allow for this.
  747. *
  748. * In the pty pair case this occurs in the close() path of the
  749. * tty itself so we must be careful about locking rules.
  750. */
  751. void tty_ldisc_hangup(struct tty_struct *tty)
  752. {
  753. struct tty_ldisc *ld;
  754. int reset = tty->driver->flags & TTY_DRIVER_RESET_TERMIOS;
  755. int err = 0;
  756. tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc);
  757. /*
  758. * FIXME! What are the locking issues here? This may me overdoing
  759. * things... This question is especially important now that we've
  760. * removed the irqlock.
  761. */
  762. ld = tty_ldisc_ref(tty);
  763. if (ld != NULL) {
  764. /* We may have no line discipline at this point */
  765. if (ld->ops->flush_buffer)
  766. ld->ops->flush_buffer(tty);
  767. tty_driver_flush_buffer(tty);
  768. if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
  769. ld->ops->write_wakeup)
  770. ld->ops->write_wakeup(tty);
  771. if (ld->ops->hangup)
  772. ld->ops->hangup(tty);
  773. tty_ldisc_deref(ld);
  774. }
  775. /*
  776. * FIXME: Once we trust the LDISC code better we can wait here for
  777. * ldisc completion and fix the driver call race
  778. */
  779. wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
  780. wake_up_interruptible_poll(&tty->read_wait, POLLIN);
  781. /*
  782. * Shutdown the current line discipline, and reset it to
  783. * N_TTY if need be.
  784. *
  785. * Avoid racing set_ldisc or tty_ldisc_release
  786. */
  787. mutex_lock(&tty->ldisc_mutex);
  788. if (tty_ldisc_hangup_halt(tty)) {
  789. /* At this point we have a halted ldisc; we want to close it and
  790. reopen a new ldisc. We could defer the reopen to the next
  791. open but it means auditing a lot of other paths so this is
  792. a FIXME */
  793. if (reset == 0) {
  794. if (!tty_ldisc_reinit(tty, tty->termios.c_line))
  795. err = tty_ldisc_open(tty, tty->ldisc);
  796. else
  797. err = 1;
  798. }
  799. /* If the re-open fails or we reset then go to N_TTY. The
  800. N_TTY open cannot fail */
  801. if (reset || err) {
  802. BUG_ON(tty_ldisc_reinit(tty, N_TTY));
  803. WARN_ON(tty_ldisc_open(tty, tty->ldisc));
  804. }
  805. tty_ldisc_enable(tty);
  806. }
  807. mutex_unlock(&tty->ldisc_mutex);
  808. if (reset)
  809. tty_reset_termios(tty);
  810. tty_ldisc_debug(tty, "re-opened ldisc: %p\n", tty->ldisc);
  811. }
  812. /**
  813. * tty_ldisc_setup - open line discipline
  814. * @tty: tty being shut down
  815. * @o_tty: pair tty for pty/tty pairs
  816. *
  817. * Called during the initial open of a tty/pty pair in order to set up the
  818. * line disciplines and bind them to the tty. This has no locking issues
  819. * as the device isn't yet active.
  820. */
  821. int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
  822. {
  823. struct tty_ldisc *ld = tty->ldisc;
  824. int retval;
  825. retval = tty_ldisc_open(tty, ld);
  826. if (retval)
  827. return retval;
  828. if (o_tty) {
  829. retval = tty_ldisc_open(o_tty, o_tty->ldisc);
  830. if (retval) {
  831. tty_ldisc_close(tty, ld);
  832. return retval;
  833. }
  834. tty_ldisc_enable(o_tty);
  835. }
  836. tty_ldisc_enable(tty);
  837. return 0;
  838. }
  839. static void tty_ldisc_kill(struct tty_struct *tty)
  840. {
  841. mutex_lock(&tty->ldisc_mutex);
  842. /*
  843. * Now kill off the ldisc
  844. */
  845. tty_ldisc_close(tty, tty->ldisc);
  846. tty_ldisc_put(tty->ldisc);
  847. /* Force an oops if we mess this up */
  848. tty->ldisc = NULL;
  849. /* Ensure the next open requests the N_TTY ldisc */
  850. tty_set_termios_ldisc(tty, N_TTY);
  851. mutex_unlock(&tty->ldisc_mutex);
  852. }
  853. /**
  854. * tty_ldisc_release - release line discipline
  855. * @tty: tty being shut down
  856. * @o_tty: pair tty for pty/tty pairs
  857. *
  858. * Called during the final close of a tty/pty pair in order to shut down
  859. * the line discpline layer. On exit the ldisc assigned is N_TTY and the
  860. * ldisc has not been opened.
  861. */
  862. void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
  863. {
  864. /*
  865. * Shutdown this line discipline. As this is the final close,
  866. * it does not race with the set_ldisc code path.
  867. */
  868. tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc);
  869. tty_ldisc_halt(tty, o_tty, MAX_SCHEDULE_TIMEOUT);
  870. tty_lock_pair(tty, o_tty);
  871. /* This will need doing differently if we need to lock */
  872. tty_ldisc_kill(tty);
  873. if (o_tty)
  874. tty_ldisc_kill(o_tty);
  875. tty_unlock_pair(tty, o_tty);
  876. /* And the memory resources remaining (buffers, termios) will be
  877. disposed of when the kref hits zero */
  878. tty_ldisc_debug(tty, "ldisc closed\n");
  879. }
  880. /**
  881. * tty_ldisc_init - ldisc setup for new tty
  882. * @tty: tty being allocated
  883. *
  884. * Set up the line discipline objects for a newly allocated tty. Note that
  885. * the tty structure is not completely set up when this call is made.
  886. */
  887. void tty_ldisc_init(struct tty_struct *tty)
  888. {
  889. struct tty_ldisc *ld = tty_ldisc_get(N_TTY);
  890. if (IS_ERR(ld))
  891. panic("n_tty: init_tty");
  892. tty->ldisc = ld;
  893. }
  894. /**
  895. * tty_ldisc_init - ldisc cleanup for new tty
  896. * @tty: tty that was allocated recently
  897. *
  898. * The tty structure must not becompletely set up (tty_ldisc_setup) when
  899. * this call is made.
  900. */
  901. void tty_ldisc_deinit(struct tty_struct *tty)
  902. {
  903. tty_ldisc_put(tty->ldisc);
  904. tty->ldisc = NULL;
  905. }
  906. void tty_ldisc_begin(void)
  907. {
  908. /* Setup the default TTY line discipline. */
  909. (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
  910. }