tty_buffer.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. mutex_lock(&buf->flush_mutex);
  164. /* If the data is being pushed to the tty layer then we can't
  165. process it here. Instead set a flag and the flush_to_ldisc
  166. path will process the flush request before it exits */
  167. if (test_bit(TTYP_FLUSHING, &port->iflags)) {
  168. set_bit(TTYP_FLUSHPENDING, &port->iflags);
  169. mutex_unlock(&buf->flush_mutex);
  170. wait_event(tty->read_wait,
  171. test_bit(TTYP_FLUSHPENDING, &port->iflags) == 0);
  172. return;
  173. }
  174. __tty_buffer_flush(port);
  175. mutex_unlock(&buf->flush_mutex);
  176. }
  177. /**
  178. * tty_buffer_request_room - grow tty buffer if needed
  179. * @tty: tty structure
  180. * @size: size desired
  181. *
  182. * Make at least size bytes of linear space available for the tty
  183. * buffer. If we fail return the size we managed to find.
  184. */
  185. int tty_buffer_request_room(struct tty_port *port, size_t size)
  186. {
  187. struct tty_bufhead *buf = &port->buf;
  188. struct tty_buffer *b, *n;
  189. int left;
  190. b = buf->tail;
  191. left = b->size - b->used;
  192. if (left < size) {
  193. /* This is the slow path - looking for new buffers to use */
  194. if ((n = tty_buffer_alloc(port, size)) != NULL) {
  195. buf->tail = n;
  196. b->commit = b->used;
  197. smp_mb();
  198. b->next = n;
  199. } else
  200. size = left;
  201. }
  202. return size;
  203. }
  204. EXPORT_SYMBOL_GPL(tty_buffer_request_room);
  205. /**
  206. * tty_insert_flip_string_fixed_flag - Add characters to the tty buffer
  207. * @port: tty port
  208. * @chars: characters
  209. * @flag: flag value for each character
  210. * @size: size
  211. *
  212. * Queue a series of bytes to the tty buffering. All the characters
  213. * passed are marked with the supplied flag. Returns the number added.
  214. */
  215. int tty_insert_flip_string_fixed_flag(struct tty_port *port,
  216. const unsigned char *chars, char flag, size_t size)
  217. {
  218. int copied = 0;
  219. do {
  220. int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
  221. int space = tty_buffer_request_room(port, goal);
  222. struct tty_buffer *tb = port->buf.tail;
  223. if (unlikely(space == 0))
  224. break;
  225. memcpy(char_buf_ptr(tb, tb->used), chars, space);
  226. memset(flag_buf_ptr(tb, tb->used), flag, space);
  227. tb->used += space;
  228. copied += space;
  229. chars += space;
  230. /* There is a small chance that we need to split the data over
  231. several buffers. If this is the case we must loop */
  232. } while (unlikely(size > copied));
  233. return copied;
  234. }
  235. EXPORT_SYMBOL(tty_insert_flip_string_fixed_flag);
  236. /**
  237. * tty_insert_flip_string_flags - Add characters to the tty buffer
  238. * @port: tty port
  239. * @chars: characters
  240. * @flags: flag bytes
  241. * @size: size
  242. *
  243. * Queue a series of bytes to the tty buffering. For each character
  244. * the flags array indicates the status of the character. Returns the
  245. * number added.
  246. */
  247. int tty_insert_flip_string_flags(struct tty_port *port,
  248. const unsigned char *chars, const char *flags, size_t size)
  249. {
  250. int copied = 0;
  251. do {
  252. int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
  253. int space = tty_buffer_request_room(port, goal);
  254. struct tty_buffer *tb = port->buf.tail;
  255. if (unlikely(space == 0))
  256. break;
  257. memcpy(char_buf_ptr(tb, tb->used), chars, space);
  258. memcpy(flag_buf_ptr(tb, tb->used), flags, space);
  259. tb->used += space;
  260. copied += space;
  261. chars += space;
  262. flags += space;
  263. /* There is a small chance that we need to split the data over
  264. several buffers. If this is the case we must loop */
  265. } while (unlikely(size > copied));
  266. return copied;
  267. }
  268. EXPORT_SYMBOL(tty_insert_flip_string_flags);
  269. /**
  270. * tty_schedule_flip - push characters to ldisc
  271. * @port: tty port to push from
  272. *
  273. * Takes any pending buffers and transfers their ownership to the
  274. * ldisc side of the queue. It then schedules those characters for
  275. * processing by the line discipline.
  276. * Note that this function can only be used when the low_latency flag
  277. * is unset. Otherwise the workqueue won't be flushed.
  278. */
  279. void tty_schedule_flip(struct tty_port *port)
  280. {
  281. struct tty_bufhead *buf = &port->buf;
  282. WARN_ON(port->low_latency);
  283. buf->tail->commit = buf->tail->used;
  284. schedule_work(&buf->work);
  285. }
  286. EXPORT_SYMBOL(tty_schedule_flip);
  287. /**
  288. * tty_prepare_flip_string - make room for characters
  289. * @port: tty port
  290. * @chars: return pointer for character write area
  291. * @size: desired size
  292. *
  293. * Prepare a block of space in the buffer for data. Returns the length
  294. * available and buffer pointer to the space which is now allocated and
  295. * accounted for as ready for normal characters. This is used for drivers
  296. * that need their own block copy routines into the buffer. There is no
  297. * guarantee the buffer is a DMA target!
  298. */
  299. int tty_prepare_flip_string(struct tty_port *port, unsigned char **chars,
  300. size_t size)
  301. {
  302. int space = tty_buffer_request_room(port, size);
  303. if (likely(space)) {
  304. struct tty_buffer *tb = port->buf.tail;
  305. *chars = char_buf_ptr(tb, tb->used);
  306. memset(flag_buf_ptr(tb, tb->used), TTY_NORMAL, space);
  307. tb->used += space;
  308. }
  309. return space;
  310. }
  311. EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
  312. /**
  313. * tty_prepare_flip_string_flags - make room for characters
  314. * @port: tty port
  315. * @chars: return pointer for character write area
  316. * @flags: return pointer for status flag write area
  317. * @size: desired size
  318. *
  319. * Prepare a block of space in the buffer for data. Returns the length
  320. * available and buffer pointer to the space which is now allocated and
  321. * accounted for as ready for characters. This is used for drivers
  322. * that need their own block copy routines into the buffer. There is no
  323. * guarantee the buffer is a DMA target!
  324. */
  325. int tty_prepare_flip_string_flags(struct tty_port *port,
  326. unsigned char **chars, char **flags, size_t size)
  327. {
  328. int space = tty_buffer_request_room(port, size);
  329. if (likely(space)) {
  330. struct tty_buffer *tb = port->buf.tail;
  331. *chars = char_buf_ptr(tb, tb->used);
  332. *flags = flag_buf_ptr(tb, tb->used);
  333. tb->used += space;
  334. }
  335. return space;
  336. }
  337. EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
  338. static int
  339. receive_buf(struct tty_struct *tty, struct tty_buffer *head, int count)
  340. {
  341. struct tty_ldisc *disc = tty->ldisc;
  342. unsigned char *p = char_buf_ptr(head, head->read);
  343. char *f = flag_buf_ptr(head, head->read);
  344. if (disc->ops->receive_buf2)
  345. count = disc->ops->receive_buf2(tty, p, f, count);
  346. else {
  347. count = min_t(int, count, tty->receive_room);
  348. if (count)
  349. disc->ops->receive_buf(tty, p, f, count);
  350. }
  351. head->read += count;
  352. return count;
  353. }
  354. /**
  355. * flush_to_ldisc
  356. * @work: tty structure passed from work queue.
  357. *
  358. * This routine is called out of the software interrupt to flush data
  359. * from the buffer chain to the line discipline.
  360. *
  361. * The receive_buf method is single threaded for each tty instance.
  362. *
  363. * Locking: takes flush_mutex to ensure single-threaded flip buffer
  364. * 'consumer'
  365. */
  366. static void flush_to_ldisc(struct work_struct *work)
  367. {
  368. struct tty_port *port = container_of(work, struct tty_port, buf.work);
  369. struct tty_bufhead *buf = &port->buf;
  370. struct tty_struct *tty;
  371. struct tty_ldisc *disc;
  372. tty = port->itty;
  373. if (tty == NULL)
  374. return;
  375. disc = tty_ldisc_ref(tty);
  376. if (disc == NULL)
  377. return;
  378. mutex_lock(&buf->flush_mutex);
  379. if (!test_and_set_bit(TTYP_FLUSHING, &port->iflags)) {
  380. while (1) {
  381. struct tty_buffer *head = buf->head;
  382. int count;
  383. count = head->commit - head->read;
  384. if (!count) {
  385. if (head->next == NULL)
  386. break;
  387. buf->head = head->next;
  388. tty_buffer_free(port, head);
  389. continue;
  390. }
  391. mutex_unlock(&buf->flush_mutex);
  392. count = receive_buf(tty, head, count);
  393. mutex_lock(&buf->flush_mutex);
  394. /* Ldisc or user is trying to flush the buffers.
  395. We may have a deferred request to flush the
  396. input buffer, if so pull the chain under the lock
  397. and empty the queue */
  398. if (test_bit(TTYP_FLUSHPENDING, &port->iflags)) {
  399. __tty_buffer_flush(port);
  400. clear_bit(TTYP_FLUSHPENDING, &port->iflags);
  401. wake_up(&tty->read_wait);
  402. break;
  403. } else if (!count)
  404. break;
  405. }
  406. clear_bit(TTYP_FLUSHING, &port->iflags);
  407. }
  408. mutex_unlock(&buf->flush_mutex);
  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->flush_mutex);
  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->memory_used, 0);
  461. INIT_WORK(&buf->work, flush_to_ldisc);
  462. }