tty_buffer.c 14 KB

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