tty_ldisc.c 24 KB

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