tty_buffer.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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. p->char_buf_ptr = (char *)(p->data);
  68. p->flag_buf_ptr = (unsigned char *)p->char_buf_ptr + size;
  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 >= 512)
  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. while ((*tbh) != NULL) {
  162. struct tty_buffer *t = *tbh;
  163. if (t->size >= size) {
  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. tbh = &((*tbh)->next);
  173. }
  174. /* Round the buffer size out */
  175. size = (size + 0xFF) & ~0xFF;
  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(tb->char_buf_ptr + tb->used, chars, space);
  246. memset(tb->flag_buf_ptr + 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(tb->char_buf_ptr + tb->used, chars, space);
  282. memcpy(tb->flag_buf_ptr + 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 = tb->char_buf_ptr + tb->used;
  338. memset(tb->flag_buf_ptr + 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 = tb->char_buf_ptr + tb->used;
  366. *flags = tb->flag_buf_ptr + tb->used;
  367. tb->used += space;
  368. }
  369. return space;
  370. }
  371. EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
  372. /**
  373. * flush_to_ldisc
  374. * @work: tty structure passed from work queue.
  375. *
  376. * This routine is called out of the software interrupt to flush data
  377. * from the buffer chain to the line discipline.
  378. *
  379. * Locking: holds tty->buf.lock to guard buffer list. Drops the lock
  380. * while invoking the line discipline receive_buf method. The
  381. * receive_buf method is single threaded for each tty instance.
  382. */
  383. static void flush_to_ldisc(struct work_struct *work)
  384. {
  385. struct tty_port *port = container_of(work, struct tty_port, buf.work);
  386. struct tty_bufhead *buf = &port->buf;
  387. struct tty_struct *tty;
  388. unsigned long flags;
  389. struct tty_ldisc *disc;
  390. tty = port->itty;
  391. if (tty == NULL)
  392. return;
  393. disc = tty_ldisc_ref(tty);
  394. if (disc == NULL) /* !TTY_LDISC */
  395. return;
  396. spin_lock_irqsave(&buf->lock, flags);
  397. if (!test_and_set_bit(TTYP_FLUSHING, &port->iflags)) {
  398. struct tty_buffer *head;
  399. while ((head = buf->head) != NULL) {
  400. int count;
  401. char *char_buf;
  402. unsigned char *flag_buf;
  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. /* Ldisc or user is trying to flush the buffers
  412. we are feeding to the ldisc, stop feeding the
  413. line discipline as we want to empty the queue */
  414. if (test_bit(TTYP_FLUSHPENDING, &port->iflags))
  415. break;
  416. if (!tty->receive_room)
  417. break;
  418. if (count > tty->receive_room)
  419. count = tty->receive_room;
  420. char_buf = head->char_buf_ptr + head->read;
  421. flag_buf = head->flag_buf_ptr + head->read;
  422. head->read += count;
  423. spin_unlock_irqrestore(&buf->lock, flags);
  424. disc->ops->receive_buf(tty, char_buf,
  425. flag_buf, count);
  426. spin_lock_irqsave(&buf->lock, flags);
  427. }
  428. clear_bit(TTYP_FLUSHING, &port->iflags);
  429. }
  430. /* We may have a deferred request to flush the input buffer,
  431. if so pull the chain under the lock 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. }
  437. spin_unlock_irqrestore(&buf->lock, flags);
  438. tty_ldisc_deref(disc);
  439. }
  440. /**
  441. * tty_flush_to_ldisc
  442. * @tty: tty to push
  443. *
  444. * Push the terminal flip buffers to the line discipline.
  445. *
  446. * Must not be called from IRQ context.
  447. */
  448. void tty_flush_to_ldisc(struct tty_struct *tty)
  449. {
  450. if (!tty->port->low_latency)
  451. flush_work(&tty->port->buf.work);
  452. }
  453. /**
  454. * tty_flip_buffer_push - terminal
  455. * @port: tty port to push
  456. *
  457. * Queue a push of the terminal flip buffers to the line discipline. This
  458. * function must not be called from IRQ context if port->low_latency is
  459. * set.
  460. *
  461. * In the event of the queue being busy for flipping the work will be
  462. * held off and retried later.
  463. *
  464. * Locking: tty buffer lock. Driver locks in low latency mode.
  465. */
  466. void tty_flip_buffer_push(struct tty_port *port)
  467. {
  468. struct tty_bufhead *buf = &port->buf;
  469. unsigned long flags;
  470. spin_lock_irqsave(&buf->lock, flags);
  471. if (buf->tail != NULL)
  472. buf->tail->commit = buf->tail->used;
  473. spin_unlock_irqrestore(&buf->lock, flags);
  474. if (port->low_latency)
  475. flush_to_ldisc(&buf->work);
  476. else
  477. schedule_work(&buf->work);
  478. }
  479. EXPORT_SYMBOL(tty_flip_buffer_push);
  480. /**
  481. * tty_buffer_init - prepare a tty buffer structure
  482. * @tty: tty to initialise
  483. *
  484. * Set up the initial state of the buffer management for a tty device.
  485. * Must be called before the other tty buffer functions are used.
  486. *
  487. * Locking: none
  488. */
  489. void tty_buffer_init(struct tty_port *port)
  490. {
  491. struct tty_bufhead *buf = &port->buf;
  492. spin_lock_init(&buf->lock);
  493. buf->head = NULL;
  494. buf->tail = NULL;
  495. buf->free = NULL;
  496. buf->memory_used = 0;
  497. INIT_WORK(&buf->work, flush_to_ldisc);
  498. }