tty_buffer.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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. * Locking: Caller must hold tty->buf.lock
  138. */
  139. static void __tty_buffer_flush(struct tty_port *port)
  140. {
  141. struct tty_bufhead *buf = &port->buf;
  142. struct tty_buffer *next;
  143. while ((next = buf->head->next) != NULL) {
  144. tty_buffer_free(port, buf->head);
  145. buf->head = next;
  146. }
  147. WARN_ON(buf->head != buf->tail);
  148. buf->head->read = buf->head->commit;
  149. }
  150. /**
  151. * tty_buffer_flush - flush full tty buffers
  152. * @tty: tty to flush
  153. *
  154. * flush all the buffers containing receive data. If the buffer is
  155. * being processed by flush_to_ldisc then we defer the processing
  156. * to that function
  157. *
  158. * Locking: none
  159. */
  160. void tty_buffer_flush(struct tty_struct *tty)
  161. {
  162. struct tty_port *port = tty->port;
  163. struct tty_bufhead *buf = &port->buf;
  164. unsigned long flags;
  165. spin_lock_irqsave(&buf->lock, flags);
  166. /* If the data is being pushed to the tty layer then we can't
  167. process it here. Instead set a flag and the flush_to_ldisc
  168. path will process the flush request before it exits */
  169. if (test_bit(TTYP_FLUSHING, &port->iflags)) {
  170. set_bit(TTYP_FLUSHPENDING, &port->iflags);
  171. spin_unlock_irqrestore(&buf->lock, flags);
  172. wait_event(tty->read_wait,
  173. test_bit(TTYP_FLUSHPENDING, &port->iflags) == 0);
  174. return;
  175. } else
  176. __tty_buffer_flush(port);
  177. spin_unlock_irqrestore(&buf->lock, flags);
  178. }
  179. /**
  180. * tty_buffer_request_room - grow tty buffer if needed
  181. * @tty: tty structure
  182. * @size: size desired
  183. *
  184. * Make at least size bytes of linear space available for the tty
  185. * buffer. If we fail return the size we managed to find.
  186. */
  187. int tty_buffer_request_room(struct tty_port *port, size_t size)
  188. {
  189. struct tty_bufhead *buf = &port->buf;
  190. struct tty_buffer *b, *n;
  191. int left;
  192. b = buf->tail;
  193. left = b->size - b->used;
  194. if (left < size) {
  195. /* This is the slow path - looking for new buffers to use */
  196. if ((n = tty_buffer_alloc(port, size)) != NULL) {
  197. buf->tail = n;
  198. b->commit = b->used;
  199. smp_mb();
  200. b->next = n;
  201. } else
  202. size = left;
  203. }
  204. return size;
  205. }
  206. EXPORT_SYMBOL_GPL(tty_buffer_request_room);
  207. /**
  208. * tty_insert_flip_string_fixed_flag - Add characters to the tty buffer
  209. * @port: tty port
  210. * @chars: characters
  211. * @flag: flag value for each character
  212. * @size: size
  213. *
  214. * Queue a series of bytes to the tty buffering. All the characters
  215. * passed are marked with the supplied flag. Returns the number added.
  216. */
  217. int tty_insert_flip_string_fixed_flag(struct tty_port *port,
  218. const unsigned char *chars, char flag, size_t size)
  219. {
  220. int copied = 0;
  221. do {
  222. int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
  223. int space = tty_buffer_request_room(port, goal);
  224. struct tty_buffer *tb = port->buf.tail;
  225. if (unlikely(space == 0))
  226. break;
  227. memcpy(char_buf_ptr(tb, tb->used), chars, space);
  228. memset(flag_buf_ptr(tb, tb->used), flag, space);
  229. tb->used += space;
  230. copied += space;
  231. chars += space;
  232. /* There is a small chance that we need to split the data over
  233. several buffers. If this is the case we must loop */
  234. } while (unlikely(size > copied));
  235. return copied;
  236. }
  237. EXPORT_SYMBOL(tty_insert_flip_string_fixed_flag);
  238. /**
  239. * tty_insert_flip_string_flags - Add characters to the tty buffer
  240. * @port: tty port
  241. * @chars: characters
  242. * @flags: flag bytes
  243. * @size: size
  244. *
  245. * Queue a series of bytes to the tty buffering. For each character
  246. * the flags array indicates the status of the character. Returns the
  247. * number added.
  248. */
  249. int tty_insert_flip_string_flags(struct tty_port *port,
  250. const unsigned char *chars, const char *flags, size_t size)
  251. {
  252. int copied = 0;
  253. do {
  254. int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
  255. int space = tty_buffer_request_room(port, goal);
  256. struct tty_buffer *tb = port->buf.tail;
  257. if (unlikely(space == 0))
  258. break;
  259. memcpy(char_buf_ptr(tb, tb->used), chars, space);
  260. memcpy(flag_buf_ptr(tb, tb->used), flags, space);
  261. tb->used += space;
  262. copied += space;
  263. chars += space;
  264. flags += space;
  265. /* There is a small chance that we need to split the data over
  266. several buffers. If this is the case we must loop */
  267. } while (unlikely(size > copied));
  268. return copied;
  269. }
  270. EXPORT_SYMBOL(tty_insert_flip_string_flags);
  271. /**
  272. * tty_schedule_flip - push characters to ldisc
  273. * @port: tty port to push from
  274. *
  275. * Takes any pending buffers and transfers their ownership to the
  276. * ldisc side of the queue. It then schedules those characters for
  277. * processing by the line discipline.
  278. * Note that this function can only be used when the low_latency flag
  279. * is unset. Otherwise the workqueue won't be flushed.
  280. */
  281. void tty_schedule_flip(struct tty_port *port)
  282. {
  283. struct tty_bufhead *buf = &port->buf;
  284. WARN_ON(port->low_latency);
  285. buf->tail->commit = buf->tail->used;
  286. schedule_work(&buf->work);
  287. }
  288. EXPORT_SYMBOL(tty_schedule_flip);
  289. /**
  290. * tty_prepare_flip_string - make room for characters
  291. * @port: tty port
  292. * @chars: return pointer for character write area
  293. * @size: desired size
  294. *
  295. * Prepare a block of space in the buffer for data. Returns the length
  296. * available and buffer pointer to the space which is now allocated and
  297. * accounted for as ready for normal characters. This is used for drivers
  298. * that need their own block copy routines into the buffer. There is no
  299. * guarantee the buffer is a DMA target!
  300. */
  301. int tty_prepare_flip_string(struct tty_port *port, unsigned char **chars,
  302. size_t size)
  303. {
  304. int space = tty_buffer_request_room(port, size);
  305. if (likely(space)) {
  306. struct tty_buffer *tb = port->buf.tail;
  307. *chars = char_buf_ptr(tb, tb->used);
  308. memset(flag_buf_ptr(tb, tb->used), TTY_NORMAL, space);
  309. tb->used += space;
  310. }
  311. return space;
  312. }
  313. EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
  314. /**
  315. * tty_prepare_flip_string_flags - make room for characters
  316. * @port: tty port
  317. * @chars: return pointer for character write area
  318. * @flags: return pointer for status flag write area
  319. * @size: desired size
  320. *
  321. * Prepare a block of space in the buffer for data. Returns the length
  322. * available and buffer pointer to the space which is now allocated and
  323. * accounted for as ready for characters. This is used for drivers
  324. * that need their own block copy routines into the buffer. There is no
  325. * guarantee the buffer is a DMA target!
  326. */
  327. int tty_prepare_flip_string_flags(struct tty_port *port,
  328. unsigned char **chars, char **flags, size_t size)
  329. {
  330. int space = tty_buffer_request_room(port, size);
  331. if (likely(space)) {
  332. struct tty_buffer *tb = port->buf.tail;
  333. *chars = char_buf_ptr(tb, tb->used);
  334. *flags = flag_buf_ptr(tb, tb->used);
  335. tb->used += space;
  336. }
  337. return space;
  338. }
  339. EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
  340. static int
  341. receive_buf(struct tty_struct *tty, struct tty_buffer *head, int count)
  342. {
  343. struct tty_ldisc *disc = tty->ldisc;
  344. unsigned char *p = char_buf_ptr(head, head->read);
  345. char *f = flag_buf_ptr(head, head->read);
  346. if (disc->ops->receive_buf2)
  347. count = disc->ops->receive_buf2(tty, p, f, count);
  348. else {
  349. count = min_t(int, count, tty->receive_room);
  350. if (count)
  351. disc->ops->receive_buf(tty, p, f, count);
  352. }
  353. head->read += count;
  354. return count;
  355. }
  356. /**
  357. * flush_to_ldisc
  358. * @work: tty structure passed from work queue.
  359. *
  360. * This routine is called out of the software interrupt to flush data
  361. * from the buffer chain to the line discipline.
  362. *
  363. * Locking: holds tty->buf.lock to guard buffer list. Drops the lock
  364. * while invoking the line discipline receive_buf method. The
  365. * receive_buf method is single threaded for each tty instance.
  366. */
  367. static void flush_to_ldisc(struct work_struct *work)
  368. {
  369. struct tty_port *port = container_of(work, struct tty_port, buf.work);
  370. struct tty_bufhead *buf = &port->buf;
  371. struct tty_struct *tty;
  372. unsigned long flags;
  373. struct tty_ldisc *disc;
  374. tty = port->itty;
  375. if (tty == NULL)
  376. return;
  377. disc = tty_ldisc_ref(tty);
  378. if (disc == NULL)
  379. return;
  380. spin_lock_irqsave(&buf->lock, flags);
  381. if (!test_and_set_bit(TTYP_FLUSHING, &port->iflags)) {
  382. while (1) {
  383. struct tty_buffer *head = buf->head;
  384. int count;
  385. count = head->commit - head->read;
  386. if (!count) {
  387. if (head->next == NULL)
  388. break;
  389. buf->head = head->next;
  390. tty_buffer_free(port, head);
  391. continue;
  392. }
  393. spin_unlock_irqrestore(&buf->lock, flags);
  394. count = receive_buf(tty, head, count);
  395. spin_lock_irqsave(&buf->lock, flags);
  396. /* Ldisc or user is trying to flush the buffers.
  397. We may have a deferred request to flush the
  398. input buffer, if so pull the chain under the lock
  399. and empty the queue */
  400. if (test_bit(TTYP_FLUSHPENDING, &port->iflags)) {
  401. __tty_buffer_flush(port);
  402. clear_bit(TTYP_FLUSHPENDING, &port->iflags);
  403. wake_up(&tty->read_wait);
  404. break;
  405. } else if (!count)
  406. break;
  407. }
  408. clear_bit(TTYP_FLUSHING, &port->iflags);
  409. }
  410. spin_unlock_irqrestore(&buf->lock, flags);
  411. tty_ldisc_deref(disc);
  412. }
  413. /**
  414. * tty_flush_to_ldisc
  415. * @tty: tty to push
  416. *
  417. * Push the terminal flip buffers to the line discipline.
  418. *
  419. * Must not be called from IRQ context.
  420. */
  421. void tty_flush_to_ldisc(struct tty_struct *tty)
  422. {
  423. if (!tty->port->low_latency)
  424. flush_work(&tty->port->buf.work);
  425. }
  426. /**
  427. * tty_flip_buffer_push - terminal
  428. * @port: tty port to push
  429. *
  430. * Queue a push of the terminal flip buffers to the line discipline. This
  431. * function must not be called from IRQ context if port->low_latency is
  432. * set.
  433. *
  434. * In the event of the queue being busy for flipping the work will be
  435. * held off and retried later.
  436. */
  437. void tty_flip_buffer_push(struct tty_port *port)
  438. {
  439. struct tty_bufhead *buf = &port->buf;
  440. buf->tail->commit = buf->tail->used;
  441. if (port->low_latency)
  442. flush_to_ldisc(&buf->work);
  443. else
  444. schedule_work(&buf->work);
  445. }
  446. EXPORT_SYMBOL(tty_flip_buffer_push);
  447. /**
  448. * tty_buffer_init - prepare a tty buffer structure
  449. * @tty: tty to initialise
  450. *
  451. * Set up the initial state of the buffer management for a tty device.
  452. * Must be called before the other tty buffer functions are used.
  453. *
  454. * Locking: none
  455. */
  456. void tty_buffer_init(struct tty_port *port)
  457. {
  458. struct tty_bufhead *buf = &port->buf;
  459. spin_lock_init(&buf->lock);
  460. tty_buffer_reset(&buf->sentinel, 0);
  461. buf->head = &buf->sentinel;
  462. buf->tail = &buf->sentinel;
  463. init_llist_head(&buf->free);
  464. atomic_set(&buf->memory_used, 0);
  465. INIT_WORK(&buf->work, flush_to_ldisc);
  466. }