tty_ldisc.c 25 KB

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