tty_buffer.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /*
  2. * Tty buffer allocation management
  3. */
  4. #include <linux/types.h>
  5. #include <linux/errno.h>
  6. #include <linux/tty.h>
  7. #include <linux/tty_driver.h>
  8. #include <linux/tty_flip.h>
  9. #include <linux/timer.h>
  10. #include <linux/string.h>
  11. #include <linux/slab.h>
  12. #include <linux/sched.h>
  13. #include <linux/wait.h>
  14. #include <linux/bitops.h>
  15. #include <linux/delay.h>
  16. #include <linux/module.h>
  17. #include <linux/ratelimit.h>
  18. #define MIN_TTYB_SIZE 256
  19. #define TTYB_ALIGN_MASK 255
  20. /*
  21. * Byte threshold to limit memory consumption for flip buffers.
  22. * The actual memory limit is > 2x this amount.
  23. */
  24. #define TTYB_DEFAULT_MEM_LIMIT 65536
  25. /*
  26. * We default to dicing tty buffer allocations to this many characters
  27. * in order to avoid multiple page allocations. We know the size of
  28. * tty_buffer itself but it must also be taken into account that the
  29. * the buffer is 256 byte aligned. See tty_buffer_find for the allocation
  30. * logic this must match
  31. */
  32. #define TTY_BUFFER_PAGE (((PAGE_SIZE - sizeof(struct tty_buffer)) / 2) & ~0xFF)
  33. /*
  34. * If all tty flip buffers have been processed by flush_to_ldisc() or
  35. * dropped by tty_buffer_flush(), check if the linked pty has been closed.
  36. * If so, wake the reader/poll to process
  37. */
  38. static inline void check_other_closed(struct tty_struct *tty)
  39. {
  40. unsigned long flags, old;
  41. /* transition from TTY_OTHER_CLOSED => TTY_OTHER_DONE must be atomic */
  42. for (flags = ACCESS_ONCE(tty->flags);
  43. test_bit(TTY_OTHER_CLOSED, &flags);
  44. ) {
  45. old = flags;
  46. __set_bit(TTY_OTHER_DONE, &flags);
  47. flags = cmpxchg(&tty->flags, old, flags);
  48. if (old == flags) {
  49. wake_up_interruptible(&tty->read_wait);
  50. break;
  51. }
  52. }
  53. }
  54. /**
  55. * tty_buffer_lock_exclusive - gain exclusive access to buffer
  56. * tty_buffer_unlock_exclusive - release exclusive access
  57. *
  58. * @port - tty_port owning the flip buffer
  59. *
  60. * Guarantees safe use of the line discipline's receive_buf() method by
  61. * excluding the buffer work and any pending flush from using the flip
  62. * buffer. Data can continue to be added concurrently to the flip buffer
  63. * from the driver side.
  64. *
  65. * On release, the buffer work is restarted if there is data in the
  66. * flip buffer
  67. */
  68. void tty_buffer_lock_exclusive(struct tty_port *port)
  69. {
  70. struct tty_bufhead *buf = &port->buf;
  71. atomic_inc(&buf->priority);
  72. mutex_lock(&buf->lock);
  73. }
  74. EXPORT_SYMBOL_GPL(tty_buffer_lock_exclusive);
  75. void tty_buffer_unlock_exclusive(struct tty_port *port)
  76. {
  77. struct tty_bufhead *buf = &port->buf;
  78. int restart;
  79. restart = buf->head->commit != buf->head->read;
  80. atomic_dec(&buf->priority);
  81. mutex_unlock(&buf->lock);
  82. if (restart)
  83. queue_work(system_unbound_wq, &buf->work);
  84. }
  85. EXPORT_SYMBOL_GPL(tty_buffer_unlock_exclusive);
  86. /**
  87. * tty_buffer_space_avail - return unused buffer space
  88. * @port - tty_port owning the flip buffer
  89. *
  90. * Returns the # of bytes which can be written by the driver without
  91. * reaching the buffer limit.
  92. *
  93. * Note: this does not guarantee that memory is available to write
  94. * the returned # of bytes (use tty_prepare_flip_string_xxx() to
  95. * pre-allocate if memory guarantee is required).
  96. */
  97. int tty_buffer_space_avail(struct tty_port *port)
  98. {
  99. int space = port->buf.mem_limit - atomic_read(&port->buf.mem_used);
  100. return max(space, 0);
  101. }
  102. EXPORT_SYMBOL_GPL(tty_buffer_space_avail);
  103. static void tty_buffer_reset(struct tty_buffer *p, size_t size)
  104. {
  105. p->used = 0;
  106. p->size = size;
  107. p->next = NULL;
  108. p->commit = 0;
  109. p->read = 0;
  110. p->flags = 0;
  111. }
  112. /**
  113. * tty_buffer_free_all - free buffers used by a tty
  114. * @tty: tty to free from
  115. *
  116. * Remove all the buffers pending on a tty whether queued with data
  117. * or in the free ring. Must be called when the tty is no longer in use
  118. */
  119. void tty_buffer_free_all(struct tty_port *port)
  120. {
  121. struct tty_bufhead *buf = &port->buf;
  122. struct tty_buffer *p, *next;
  123. struct llist_node *llist;
  124. while ((p = buf->head) != NULL) {
  125. buf->head = p->next;
  126. if (p->size > 0)
  127. kfree(p);
  128. }
  129. llist = llist_del_all(&buf->free);
  130. llist_for_each_entry_safe(p, next, llist, free)
  131. kfree(p);
  132. tty_buffer_reset(&buf->sentinel, 0);
  133. buf->head = &buf->sentinel;
  134. buf->tail = &buf->sentinel;
  135. atomic_set(&buf->mem_used, 0);
  136. }
  137. /**
  138. * tty_buffer_alloc - allocate a tty buffer
  139. * @tty: tty device
  140. * @size: desired size (characters)
  141. *
  142. * Allocate a new tty buffer to hold the desired number of characters.
  143. * We round our buffers off in 256 character chunks to get better
  144. * allocation behaviour.
  145. * Return NULL if out of memory or the allocation would exceed the
  146. * per device queue
  147. */
  148. static struct tty_buffer *tty_buffer_alloc(struct tty_port *port, size_t size)
  149. {
  150. struct llist_node *free;
  151. struct tty_buffer *p;
  152. /* Round the buffer size out */
  153. size = __ALIGN_MASK(size, TTYB_ALIGN_MASK);
  154. if (size <= MIN_TTYB_SIZE) {
  155. free = llist_del_first(&port->buf.free);
  156. if (free) {
  157. p = llist_entry(free, struct tty_buffer, free);
  158. goto found;
  159. }
  160. }
  161. /* Should possibly check if this fails for the largest buffer we
  162. have queued and recycle that ? */
  163. if (atomic_read(&port->buf.mem_used) > port->buf.mem_limit)
  164. return NULL;
  165. p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC);
  166. if (p == NULL)
  167. return NULL;
  168. found:
  169. tty_buffer_reset(p, size);
  170. atomic_add(size, &port->buf.mem_used);
  171. return p;
  172. }
  173. /**
  174. * tty_buffer_free - free a tty buffer
  175. * @tty: tty owning the buffer
  176. * @b: the buffer to free
  177. *
  178. * Free a tty buffer, or add it to the free list according to our
  179. * internal strategy
  180. */
  181. static void tty_buffer_free(struct tty_port *port, struct tty_buffer *b)
  182. {
  183. struct tty_bufhead *buf = &port->buf;
  184. /* Dumb strategy for now - should keep some stats */
  185. WARN_ON(atomic_sub_return(b->size, &buf->mem_used) < 0);
  186. if (b->size > MIN_TTYB_SIZE)
  187. kfree(b);
  188. else if (b->size > 0)
  189. llist_add(&b->free, &buf->free);
  190. }
  191. /**
  192. * tty_buffer_flush - flush full tty buffers
  193. * @tty: tty to flush
  194. * @ld: optional ldisc ptr (must be referenced)
  195. *
  196. * flush all the buffers containing receive data. If ld != NULL,
  197. * flush the ldisc input buffer.
  198. *
  199. * Locking: takes buffer lock to ensure single-threaded flip buffer
  200. * 'consumer'
  201. */
  202. void tty_buffer_flush(struct tty_struct *tty, struct tty_ldisc *ld)
  203. {
  204. struct tty_port *port = tty->port;
  205. struct tty_bufhead *buf = &port->buf;
  206. struct tty_buffer *next;
  207. atomic_inc(&buf->priority);
  208. mutex_lock(&buf->lock);
  209. /* paired w/ release in __tty_buffer_request_room; ensures there are
  210. * no pending memory accesses to the freed buffer
  211. */
  212. while ((next = smp_load_acquire(&buf->head->next)) != NULL) {
  213. tty_buffer_free(port, buf->head);
  214. buf->head = next;
  215. }
  216. buf->head->read = buf->head->commit;
  217. if (ld && ld->ops->flush_buffer)
  218. ld->ops->flush_buffer(tty);
  219. check_other_closed(tty);
  220. atomic_dec(&buf->priority);
  221. mutex_unlock(&buf->lock);
  222. }
  223. /**
  224. * tty_buffer_request_room - grow tty buffer if needed
  225. * @tty: tty structure
  226. * @size: size desired
  227. * @flags: buffer flags if new buffer allocated (default = 0)
  228. *
  229. * Make at least size bytes of linear space available for the tty
  230. * buffer. If we fail return the size we managed to find.
  231. *
  232. * Will change over to a new buffer if the current buffer is encoded as
  233. * TTY_NORMAL (so has no flags buffer) and the new buffer requires
  234. * a flags buffer.
  235. */
  236. static int __tty_buffer_request_room(struct tty_port *port, size_t size,
  237. int flags)
  238. {
  239. struct tty_bufhead *buf = &port->buf;
  240. struct tty_buffer *b, *n;
  241. int left, change;
  242. b = buf->tail;
  243. if (b->flags & TTYB_NORMAL)
  244. left = 2 * b->size - b->used;
  245. else
  246. left = b->size - b->used;
  247. change = (b->flags & TTYB_NORMAL) && (~flags & TTYB_NORMAL);
  248. if (change || left < size) {
  249. /* This is the slow path - looking for new buffers to use */
  250. n = tty_buffer_alloc(port, size);
  251. if (n != NULL) {
  252. n->flags = flags;
  253. buf->tail = n;
  254. b->commit = b->used;
  255. /* paired w/ acquire in flush_to_ldisc(); ensures the
  256. * latest commit value can be read before the head is
  257. * advanced to the next buffer
  258. */
  259. smp_store_release(&b->next, n);
  260. } else if (change)
  261. size = 0;
  262. else
  263. size = left;
  264. }
  265. return size;
  266. }
  267. int tty_buffer_request_room(struct tty_port *port, size_t size)
  268. {
  269. return __tty_buffer_request_room(port, size, 0);
  270. }
  271. EXPORT_SYMBOL_GPL(tty_buffer_request_room);
  272. /**
  273. * tty_insert_flip_string_fixed_flag - Add characters to the tty buffer
  274. * @port: tty port
  275. * @chars: characters
  276. * @flag: flag value for each character
  277. * @size: size
  278. *
  279. * Queue a series of bytes to the tty buffering. All the characters
  280. * passed are marked with the supplied flag. Returns the number added.
  281. */
  282. int tty_insert_flip_string_fixed_flag(struct tty_port *port,
  283. const unsigned char *chars, char flag, size_t size)
  284. {
  285. int copied = 0;
  286. do {
  287. int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
  288. int flags = (flag == TTY_NORMAL) ? TTYB_NORMAL : 0;
  289. int space = __tty_buffer_request_room(port, goal, flags);
  290. struct tty_buffer *tb = port->buf.tail;
  291. if (unlikely(space == 0))
  292. break;
  293. memcpy(char_buf_ptr(tb, tb->used), chars, space);
  294. if (~tb->flags & TTYB_NORMAL)
  295. memset(flag_buf_ptr(tb, tb->used), flag, space);
  296. tb->used += space;
  297. copied += space;
  298. chars += space;
  299. /* There is a small chance that we need to split the data over
  300. several buffers. If this is the case we must loop */
  301. } while (unlikely(size > copied));
  302. return copied;
  303. }
  304. EXPORT_SYMBOL(tty_insert_flip_string_fixed_flag);
  305. /**
  306. * tty_insert_flip_string_flags - Add characters to the tty buffer
  307. * @port: tty port
  308. * @chars: characters
  309. * @flags: flag bytes
  310. * @size: size
  311. *
  312. * Queue a series of bytes to the tty buffering. For each character
  313. * the flags array indicates the status of the character. Returns the
  314. * number added.
  315. */
  316. int tty_insert_flip_string_flags(struct tty_port *port,
  317. const unsigned char *chars, const char *flags, size_t size)
  318. {
  319. int copied = 0;
  320. do {
  321. int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
  322. int space = tty_buffer_request_room(port, goal);
  323. struct tty_buffer *tb = port->buf.tail;
  324. if (unlikely(space == 0))
  325. break;
  326. memcpy(char_buf_ptr(tb, tb->used), chars, space);
  327. memcpy(flag_buf_ptr(tb, tb->used), flags, space);
  328. tb->used += space;
  329. copied += space;
  330. chars += space;
  331. flags += space;
  332. /* There is a small chance that we need to split the data over
  333. several buffers. If this is the case we must loop */
  334. } while (unlikely(size > copied));
  335. return copied;
  336. }
  337. EXPORT_SYMBOL(tty_insert_flip_string_flags);
  338. /**
  339. * tty_schedule_flip - push characters to ldisc
  340. * @port: tty port to push from
  341. *
  342. * Takes any pending buffers and transfers their ownership to the
  343. * ldisc side of the queue. It then schedules those characters for
  344. * processing by the line discipline.
  345. */
  346. void tty_schedule_flip(struct tty_port *port)
  347. {
  348. struct tty_bufhead *buf = &port->buf;
  349. buf->tail->commit = buf->tail->used;
  350. schedule_work(&buf->work);
  351. }
  352. EXPORT_SYMBOL(tty_schedule_flip);
  353. /**
  354. * tty_prepare_flip_string - make room for characters
  355. * @port: tty port
  356. * @chars: return pointer for character write area
  357. * @size: desired size
  358. *
  359. * Prepare a block of space in the buffer for data. Returns the length
  360. * available and buffer pointer to the space which is now allocated and
  361. * accounted for as ready for normal characters. This is used for drivers
  362. * that need their own block copy routines into the buffer. There is no
  363. * guarantee the buffer is a DMA target!
  364. */
  365. int tty_prepare_flip_string(struct tty_port *port, unsigned char **chars,
  366. size_t size)
  367. {
  368. int space = __tty_buffer_request_room(port, size, TTYB_NORMAL);
  369. if (likely(space)) {
  370. struct tty_buffer *tb = port->buf.tail;
  371. *chars = char_buf_ptr(tb, tb->used);
  372. if (~tb->flags & TTYB_NORMAL)
  373. memset(flag_buf_ptr(tb, tb->used), TTY_NORMAL, space);
  374. tb->used += space;
  375. }
  376. return space;
  377. }
  378. EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
  379. static int
  380. receive_buf(struct tty_struct *tty, struct tty_buffer *head, int count)
  381. {
  382. struct tty_ldisc *disc = tty->ldisc;
  383. unsigned char *p = char_buf_ptr(head, head->read);
  384. char *f = NULL;
  385. if (~head->flags & TTYB_NORMAL)
  386. f = flag_buf_ptr(head, head->read);
  387. if (disc->ops->receive_buf2)
  388. count = disc->ops->receive_buf2(tty, p, f, count);
  389. else {
  390. count = min_t(int, count, tty->receive_room);
  391. if (count)
  392. disc->ops->receive_buf(tty, p, f, count);
  393. }
  394. return count;
  395. }
  396. /**
  397. * flush_to_ldisc
  398. * @work: tty structure passed from work queue.
  399. *
  400. * This routine is called out of the software interrupt to flush data
  401. * from the buffer chain to the line discipline.
  402. *
  403. * The receive_buf method is single threaded for each tty instance.
  404. *
  405. * Locking: takes buffer lock to ensure single-threaded flip buffer
  406. * 'consumer'
  407. */
  408. static void flush_to_ldisc(struct work_struct *work)
  409. {
  410. struct tty_port *port = container_of(work, struct tty_port, buf.work);
  411. struct tty_bufhead *buf = &port->buf;
  412. struct tty_struct *tty;
  413. struct tty_ldisc *disc;
  414. tty = READ_ONCE(port->itty);
  415. if (tty == NULL)
  416. return;
  417. disc = tty_ldisc_ref(tty);
  418. if (disc == NULL)
  419. return;
  420. mutex_lock(&buf->lock);
  421. while (1) {
  422. struct tty_buffer *head = buf->head;
  423. struct tty_buffer *next;
  424. int count;
  425. /* Ldisc or user is trying to gain exclusive access */
  426. if (atomic_read(&buf->priority))
  427. break;
  428. /* paired w/ release in __tty_buffer_request_room();
  429. * ensures commit value read is not stale if the head
  430. * is advancing to the next buffer
  431. */
  432. next = smp_load_acquire(&head->next);
  433. count = head->commit - head->read;
  434. if (!count) {
  435. if (next == NULL) {
  436. check_other_closed(tty);
  437. break;
  438. }
  439. buf->head = next;
  440. tty_buffer_free(port, head);
  441. continue;
  442. }
  443. count = receive_buf(tty, head, count);
  444. if (!count)
  445. break;
  446. head->read += count;
  447. }
  448. mutex_unlock(&buf->lock);
  449. tty_ldisc_deref(disc);
  450. }
  451. /**
  452. * tty_flip_buffer_push - terminal
  453. * @port: tty port to push
  454. *
  455. * Queue a push of the terminal flip buffers to the line discipline.
  456. * Can be called from IRQ/atomic context.
  457. *
  458. * In the event of the queue being busy for flipping the work will be
  459. * held off and retried later.
  460. */
  461. void tty_flip_buffer_push(struct tty_port *port)
  462. {
  463. tty_schedule_flip(port);
  464. }
  465. EXPORT_SYMBOL(tty_flip_buffer_push);
  466. /**
  467. * tty_buffer_init - prepare a tty buffer structure
  468. * @tty: tty to initialise
  469. *
  470. * Set up the initial state of the buffer management for a tty device.
  471. * Must be called before the other tty buffer functions are used.
  472. */
  473. void tty_buffer_init(struct tty_port *port)
  474. {
  475. struct tty_bufhead *buf = &port->buf;
  476. mutex_init(&buf->lock);
  477. tty_buffer_reset(&buf->sentinel, 0);
  478. buf->head = &buf->sentinel;
  479. buf->tail = &buf->sentinel;
  480. init_llist_head(&buf->free);
  481. atomic_set(&buf->mem_used, 0);
  482. atomic_set(&buf->priority, 0);
  483. INIT_WORK(&buf->work, flush_to_ldisc);
  484. buf->mem_limit = TTYB_DEFAULT_MEM_LIMIT;
  485. }
  486. /**
  487. * tty_buffer_set_limit - change the tty buffer memory limit
  488. * @port: tty port to change
  489. *
  490. * Change the tty buffer memory limit.
  491. * Must be called before the other tty buffer functions are used.
  492. */
  493. int tty_buffer_set_limit(struct tty_port *port, int limit)
  494. {
  495. if (limit < MIN_TTYB_SIZE)
  496. return -EINVAL;
  497. port->buf.mem_limit = limit;
  498. return 0;
  499. }
  500. EXPORT_SYMBOL_GPL(tty_buffer_set_limit);
  501. /* slave ptys can claim nested buffer lock when handling BRK and INTR */
  502. void tty_buffer_set_lock_subclass(struct tty_port *port)
  503. {
  504. lockdep_set_subclass(&port->buf.lock, TTY_LOCK_SLAVE);
  505. }