tty_ldisc.c 25 KB

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