tty_ldisc.c 25 KB

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