tty_buffer.c 15 KB

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