tty_ldisc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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. static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
  157. {
  158. return (*pos < NR_LDISCS) ? pos : NULL;
  159. }
  160. static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
  161. {
  162. (*pos)++;
  163. return (*pos < NR_LDISCS) ? pos : NULL;
  164. }
  165. static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
  166. {
  167. }
  168. static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
  169. {
  170. int i = *(loff_t *)v;
  171. struct tty_ldisc_ops *ldops;
  172. ldops = get_ldops(i);
  173. if (IS_ERR(ldops))
  174. return 0;
  175. seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
  176. put_ldops(ldops);
  177. return 0;
  178. }
  179. static const struct seq_operations tty_ldiscs_seq_ops = {
  180. .start = tty_ldiscs_seq_start,
  181. .next = tty_ldiscs_seq_next,
  182. .stop = tty_ldiscs_seq_stop,
  183. .show = tty_ldiscs_seq_show,
  184. };
  185. static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
  186. {
  187. return seq_open(file, &tty_ldiscs_seq_ops);
  188. }
  189. const struct file_operations tty_ldiscs_proc_fops = {
  190. .owner = THIS_MODULE,
  191. .open = proc_tty_ldiscs_open,
  192. .read = seq_read,
  193. .llseek = seq_lseek,
  194. .release = seq_release,
  195. };
  196. /**
  197. * tty_ldisc_try - internal helper
  198. * @tty: the tty
  199. *
  200. * Make a single attempt to grab and bump the refcount on
  201. * the tty ldisc. Return 0 on failure or 1 on success. This is
  202. * used to implement both the waiting and non waiting versions
  203. * of tty_ldisc_ref
  204. *
  205. * Locking: takes tty_ldisc_lock
  206. */
  207. static struct tty_ldisc *tty_ldisc_try(struct tty_struct *tty)
  208. {
  209. unsigned long flags;
  210. struct tty_ldisc *ld;
  211. /* FIXME: this allows reference acquire after TTY_LDISC is cleared */
  212. raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
  213. ld = NULL;
  214. if (test_bit(TTY_LDISC, &tty->flags) && tty->ldisc) {
  215. ld = tty->ldisc;
  216. atomic_inc(&ld->users);
  217. }
  218. raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  219. return ld;
  220. }
  221. /**
  222. * tty_ldisc_ref_wait - wait for the tty ldisc
  223. * @tty: tty device
  224. *
  225. * Dereference the line discipline for the terminal and take a
  226. * reference to it. If the line discipline is in flux then
  227. * wait patiently until it changes.
  228. *
  229. * Note: Must not be called from an IRQ/timer context. The caller
  230. * must also be careful not to hold other locks that will deadlock
  231. * against a discipline change, such as an existing ldisc reference
  232. * (which we check for)
  233. *
  234. * Locking: call functions take tty_ldisc_lock
  235. */
  236. struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
  237. {
  238. struct tty_ldisc *ld;
  239. /* wait_event is a macro */
  240. wait_event(tty_ldisc_wait, (ld = tty_ldisc_try(tty)) != NULL);
  241. return ld;
  242. }
  243. EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
  244. /**
  245. * tty_ldisc_ref - get the tty ldisc
  246. * @tty: tty device
  247. *
  248. * Dereference the line discipline for the terminal and take a
  249. * reference to it. If the line discipline is in flux then
  250. * return NULL. Can be called from IRQ and timer functions.
  251. *
  252. * Locking: called functions take tty_ldisc_lock
  253. */
  254. struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
  255. {
  256. return tty_ldisc_try(tty);
  257. }
  258. EXPORT_SYMBOL_GPL(tty_ldisc_ref);
  259. /**
  260. * tty_ldisc_deref - free a tty ldisc reference
  261. * @ld: reference to free up
  262. *
  263. * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
  264. * be called in IRQ context.
  265. *
  266. * Locking: takes tty_ldisc_lock
  267. */
  268. void tty_ldisc_deref(struct tty_ldisc *ld)
  269. {
  270. unsigned long flags;
  271. if (WARN_ON_ONCE(!ld))
  272. return;
  273. raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
  274. /*
  275. * WARNs if one-too-many reader references were released
  276. * - the last reference must be released with tty_ldisc_put
  277. */
  278. WARN_ON(atomic_dec_and_test(&ld->users));
  279. raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  280. if (waitqueue_active(&ld->wq_idle))
  281. wake_up(&ld->wq_idle);
  282. }
  283. EXPORT_SYMBOL_GPL(tty_ldisc_deref);
  284. /**
  285. * tty_ldisc_put - release the ldisc
  286. *
  287. * Complement of tty_ldisc_get().
  288. */
  289. static inline void tty_ldisc_put(struct tty_ldisc *ld)
  290. {
  291. unsigned long flags;
  292. if (WARN_ON_ONCE(!ld))
  293. return;
  294. raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
  295. /* unreleased reader reference(s) will cause this WARN */
  296. WARN_ON(!atomic_dec_and_test(&ld->users));
  297. ld->ops->refcount--;
  298. module_put(ld->ops->owner);
  299. kfree(ld);
  300. raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  301. }
  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. tty_unlock(tty);
  543. /*
  544. * Problem: What do we do if this blocks ?
  545. * We could deadlock here
  546. */
  547. tty_wait_until_sent(tty, 0);
  548. tty_lock(tty);
  549. mutex_lock(&tty->ldisc_mutex);
  550. /*
  551. * We could be midstream of another ldisc change which has
  552. * dropped the lock during processing. If so we need to wait.
  553. */
  554. while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) {
  555. mutex_unlock(&tty->ldisc_mutex);
  556. tty_unlock(tty);
  557. wait_event(tty_ldisc_wait,
  558. test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0);
  559. tty_lock(tty);
  560. mutex_lock(&tty->ldisc_mutex);
  561. }
  562. set_bit(TTY_LDISC_CHANGING, &tty->flags);
  563. /*
  564. * No more input please, we are switching. The new ldisc
  565. * will update this value in the ldisc open function
  566. */
  567. tty->receive_room = 0;
  568. o_ldisc = tty->ldisc;
  569. tty_unlock(tty);
  570. /*
  571. * Make sure we don't change while someone holds a
  572. * reference to the line discipline. The TTY_LDISC bit
  573. * prevents anyone taking a reference once it is clear.
  574. * We need the lock to avoid racing reference takers.
  575. *
  576. * We must clear the TTY_LDISC bit here to avoid a livelock
  577. * with a userspace app continually trying to use the tty in
  578. * parallel to the change and re-referencing the tty.
  579. */
  580. retval = tty_ldisc_halt(tty, o_tty, 5 * HZ);
  581. /*
  582. * Wait for hangup to complete, if pending.
  583. * We must drop the mutex here in case a hangup is also in process.
  584. */
  585. mutex_unlock(&tty->ldisc_mutex);
  586. flush_work(&tty->hangup_work);
  587. tty_lock(tty);
  588. mutex_lock(&tty->ldisc_mutex);
  589. /* handle wait idle failure locked */
  590. if (retval) {
  591. tty_ldisc_put(new_ldisc);
  592. goto enable;
  593. }
  594. if (test_bit(TTY_HUPPING, &tty->flags)) {
  595. /* We were raced by the hangup method. It will have stomped
  596. the ldisc data and closed the ldisc down */
  597. clear_bit(TTY_LDISC_CHANGING, &tty->flags);
  598. mutex_unlock(&tty->ldisc_mutex);
  599. tty_ldisc_put(new_ldisc);
  600. tty_unlock(tty);
  601. return -EIO;
  602. }
  603. /* Shutdown the current discipline. */
  604. tty_ldisc_close(tty, o_ldisc);
  605. /* Now set up the new line discipline. */
  606. tty->ldisc = new_ldisc;
  607. tty_set_termios_ldisc(tty, ldisc);
  608. retval = tty_ldisc_open(tty, new_ldisc);
  609. if (retval < 0) {
  610. /* Back to the old one or N_TTY if we can't */
  611. tty_ldisc_put(new_ldisc);
  612. tty_ldisc_restore(tty, o_ldisc);
  613. }
  614. /* At this point we hold a reference to the new ldisc and a
  615. a reference to the old ldisc. If we ended up flipping back
  616. to the existing ldisc we have two references to it */
  617. if (tty->ldisc->ops->num != o_ldisc->ops->num && tty->ops->set_ldisc)
  618. tty->ops->set_ldisc(tty);
  619. tty_ldisc_put(o_ldisc);
  620. enable:
  621. /*
  622. * Allow ldisc referencing to occur again
  623. */
  624. tty_ldisc_enable(tty);
  625. if (o_tty)
  626. tty_ldisc_enable(o_tty);
  627. /* Restart the work queue in case no characters kick it off. Safe if
  628. already running */
  629. schedule_work(&tty->port->buf.work);
  630. if (o_tty)
  631. schedule_work(&o_tty->port->buf.work);
  632. mutex_unlock(&tty->ldisc_mutex);
  633. tty_unlock(tty);
  634. return retval;
  635. }
  636. /**
  637. * tty_reset_termios - reset terminal state
  638. * @tty: tty to reset
  639. *
  640. * Restore a terminal to the driver default state.
  641. */
  642. static void tty_reset_termios(struct tty_struct *tty)
  643. {
  644. mutex_lock(&tty->termios_mutex);
  645. tty->termios = tty->driver->init_termios;
  646. tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
  647. tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
  648. mutex_unlock(&tty->termios_mutex);
  649. }
  650. /**
  651. * tty_ldisc_reinit - reinitialise the tty ldisc
  652. * @tty: tty to reinit
  653. * @ldisc: line discipline to reinitialize
  654. *
  655. * Switch the tty to a line discipline and leave the ldisc
  656. * state closed
  657. */
  658. static int tty_ldisc_reinit(struct tty_struct *tty, int ldisc)
  659. {
  660. struct tty_ldisc *ld = tty_ldisc_get(ldisc);
  661. if (IS_ERR(ld))
  662. return -1;
  663. tty_ldisc_close(tty, tty->ldisc);
  664. tty_ldisc_put(tty->ldisc);
  665. /*
  666. * Switch the line discipline back
  667. */
  668. tty->ldisc = ld;
  669. tty_set_termios_ldisc(tty, ldisc);
  670. return 0;
  671. }
  672. /**
  673. * tty_ldisc_hangup - hangup ldisc reset
  674. * @tty: tty being hung up
  675. *
  676. * Some tty devices reset their termios when they receive a hangup
  677. * event. In that situation we must also switch back to N_TTY properly
  678. * before we reset the termios data.
  679. *
  680. * Locking: We can take the ldisc mutex as the rest of the code is
  681. * careful to allow for this.
  682. *
  683. * In the pty pair case this occurs in the close() path of the
  684. * tty itself so we must be careful about locking rules.
  685. */
  686. void tty_ldisc_hangup(struct tty_struct *tty)
  687. {
  688. struct tty_ldisc *ld;
  689. int reset = tty->driver->flags & TTY_DRIVER_RESET_TERMIOS;
  690. int err = 0;
  691. tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc);
  692. /*
  693. * FIXME! What are the locking issues here? This may me overdoing
  694. * things... This question is especially important now that we've
  695. * removed the irqlock.
  696. */
  697. ld = tty_ldisc_ref(tty);
  698. if (ld != NULL) {
  699. /* We may have no line discipline at this point */
  700. if (ld->ops->flush_buffer)
  701. ld->ops->flush_buffer(tty);
  702. tty_driver_flush_buffer(tty);
  703. if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
  704. ld->ops->write_wakeup)
  705. ld->ops->write_wakeup(tty);
  706. if (ld->ops->hangup)
  707. ld->ops->hangup(tty);
  708. tty_ldisc_deref(ld);
  709. }
  710. /*
  711. * FIXME: Once we trust the LDISC code better we can wait here for
  712. * ldisc completion and fix the driver call race
  713. */
  714. wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
  715. wake_up_interruptible_poll(&tty->read_wait, POLLIN);
  716. /*
  717. * Shutdown the current line discipline, and reset it to
  718. * N_TTY if need be.
  719. *
  720. * Avoid racing set_ldisc or tty_ldisc_release
  721. */
  722. mutex_lock(&tty->ldisc_mutex);
  723. if (tty_ldisc_hangup_halt(tty)) {
  724. /* At this point we have a halted ldisc; we want to close it and
  725. reopen a new ldisc. We could defer the reopen to the next
  726. open but it means auditing a lot of other paths so this is
  727. a FIXME */
  728. if (reset == 0) {
  729. if (!tty_ldisc_reinit(tty, tty->termios.c_line))
  730. err = tty_ldisc_open(tty, tty->ldisc);
  731. else
  732. err = 1;
  733. }
  734. /* If the re-open fails or we reset then go to N_TTY. The
  735. N_TTY open cannot fail */
  736. if (reset || err) {
  737. BUG_ON(tty_ldisc_reinit(tty, N_TTY));
  738. WARN_ON(tty_ldisc_open(tty, tty->ldisc));
  739. }
  740. tty_ldisc_enable(tty);
  741. }
  742. mutex_unlock(&tty->ldisc_mutex);
  743. if (reset)
  744. tty_reset_termios(tty);
  745. tty_ldisc_debug(tty, "re-opened ldisc: %p\n", tty->ldisc);
  746. }
  747. /**
  748. * tty_ldisc_setup - open line discipline
  749. * @tty: tty being shut down
  750. * @o_tty: pair tty for pty/tty pairs
  751. *
  752. * Called during the initial open of a tty/pty pair in order to set up the
  753. * line disciplines and bind them to the tty. This has no locking issues
  754. * as the device isn't yet active.
  755. */
  756. int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
  757. {
  758. struct tty_ldisc *ld = tty->ldisc;
  759. int retval;
  760. retval = tty_ldisc_open(tty, ld);
  761. if (retval)
  762. return retval;
  763. if (o_tty) {
  764. retval = tty_ldisc_open(o_tty, o_tty->ldisc);
  765. if (retval) {
  766. tty_ldisc_close(tty, ld);
  767. return retval;
  768. }
  769. tty_ldisc_enable(o_tty);
  770. }
  771. tty_ldisc_enable(tty);
  772. return 0;
  773. }
  774. static void tty_ldisc_kill(struct tty_struct *tty)
  775. {
  776. mutex_lock(&tty->ldisc_mutex);
  777. /*
  778. * Now kill off the ldisc
  779. */
  780. tty_ldisc_close(tty, tty->ldisc);
  781. tty_ldisc_put(tty->ldisc);
  782. /* Force an oops if we mess this up */
  783. tty->ldisc = NULL;
  784. /* Ensure the next open requests the N_TTY ldisc */
  785. tty_set_termios_ldisc(tty, N_TTY);
  786. mutex_unlock(&tty->ldisc_mutex);
  787. }
  788. /**
  789. * tty_ldisc_release - release line discipline
  790. * @tty: tty being shut down
  791. * @o_tty: pair tty for pty/tty pairs
  792. *
  793. * Called during the final close of a tty/pty pair in order to shut down
  794. * the line discpline layer. On exit the ldisc assigned is N_TTY and the
  795. * ldisc has not been opened.
  796. */
  797. void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
  798. {
  799. /*
  800. * Shutdown this line discipline. As this is the final close,
  801. * it does not race with the set_ldisc code path.
  802. */
  803. tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc);
  804. tty_ldisc_halt(tty, o_tty, MAX_SCHEDULE_TIMEOUT);
  805. tty_lock_pair(tty, o_tty);
  806. /* This will need doing differently if we need to lock */
  807. tty_ldisc_kill(tty);
  808. if (o_tty)
  809. tty_ldisc_kill(o_tty);
  810. tty_unlock_pair(tty, o_tty);
  811. /* And the memory resources remaining (buffers, termios) will be
  812. disposed of when the kref hits zero */
  813. tty_ldisc_debug(tty, "ldisc closed\n");
  814. }
  815. /**
  816. * tty_ldisc_init - ldisc setup for new tty
  817. * @tty: tty being allocated
  818. *
  819. * Set up the line discipline objects for a newly allocated tty. Note that
  820. * the tty structure is not completely set up when this call is made.
  821. */
  822. void tty_ldisc_init(struct tty_struct *tty)
  823. {
  824. struct tty_ldisc *ld = tty_ldisc_get(N_TTY);
  825. if (IS_ERR(ld))
  826. panic("n_tty: init_tty");
  827. tty->ldisc = ld;
  828. }
  829. /**
  830. * tty_ldisc_init - ldisc cleanup for new tty
  831. * @tty: tty that was allocated recently
  832. *
  833. * The tty structure must not becompletely set up (tty_ldisc_setup) when
  834. * this call is made.
  835. */
  836. void tty_ldisc_deinit(struct tty_struct *tty)
  837. {
  838. tty_ldisc_put(tty->ldisc);
  839. tty->ldisc = NULL;
  840. }
  841. void tty_ldisc_begin(void)
  842. {
  843. /* Setup the default TTY line discipline. */
  844. (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
  845. }