tty_buffer.c 14 KB

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