tty_buffer.c 13 KB

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