tty_buffer.c 14 KB

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