tty_buffer.c 14 KB

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