tty_buffer.c 14 KB

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