tty_buffer.c 13 KB

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