tty_buffer.c 15 KB

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