tty_ldisc.c 25 KB

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