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