tty_buffer.c 14 KB

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