tty_buffer.c 14 KB

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