tty_buffer.c 14 KB

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