0001
0002
0003
0004
0005
0006 #include <linux/types.h>
0007 #include <linux/errno.h>
0008 #include <linux/minmax.h>
0009 #include <linux/tty.h>
0010 #include <linux/tty_driver.h>
0011 #include <linux/tty_flip.h>
0012 #include <linux/timer.h>
0013 #include <linux/string.h>
0014 #include <linux/slab.h>
0015 #include <linux/sched.h>
0016 #include <linux/wait.h>
0017 #include <linux/bitops.h>
0018 #include <linux/delay.h>
0019 #include <linux/module.h>
0020 #include <linux/ratelimit.h>
0021 #include "tty.h"
0022
0023 #define MIN_TTYB_SIZE 256
0024 #define TTYB_ALIGN_MASK 255
0025
0026
0027
0028
0029
0030 #define TTYB_DEFAULT_MEM_LIMIT (640 * 1024UL)
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040 #define TTY_BUFFER_PAGE (((PAGE_SIZE - sizeof(struct tty_buffer)) / 2) & ~0xFF)
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052 void tty_buffer_lock_exclusive(struct tty_port *port)
0053 {
0054 struct tty_bufhead *buf = &port->buf;
0055
0056 atomic_inc(&buf->priority);
0057 mutex_lock(&buf->lock);
0058 }
0059 EXPORT_SYMBOL_GPL(tty_buffer_lock_exclusive);
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069 void tty_buffer_unlock_exclusive(struct tty_port *port)
0070 {
0071 struct tty_bufhead *buf = &port->buf;
0072 int restart;
0073
0074 restart = buf->head->commit != buf->head->read;
0075
0076 atomic_dec(&buf->priority);
0077 mutex_unlock(&buf->lock);
0078 if (restart)
0079 queue_work(system_unbound_wq, &buf->work);
0080 }
0081 EXPORT_SYMBOL_GPL(tty_buffer_unlock_exclusive);
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094 unsigned int tty_buffer_space_avail(struct tty_port *port)
0095 {
0096 int space = port->buf.mem_limit - atomic_read(&port->buf.mem_used);
0097
0098 return max(space, 0);
0099 }
0100 EXPORT_SYMBOL_GPL(tty_buffer_space_avail);
0101
0102 static void tty_buffer_reset(struct tty_buffer *p, size_t size)
0103 {
0104 p->used = 0;
0105 p->size = size;
0106 p->next = NULL;
0107 p->commit = 0;
0108 p->lookahead = 0;
0109 p->read = 0;
0110 p->flags = 0;
0111 }
0112
0113
0114
0115
0116
0117
0118
0119
0120 void tty_buffer_free_all(struct tty_port *port)
0121 {
0122 struct tty_bufhead *buf = &port->buf;
0123 struct tty_buffer *p, *next;
0124 struct llist_node *llist;
0125 unsigned int freed = 0;
0126 int still_used;
0127
0128 while ((p = buf->head) != NULL) {
0129 buf->head = p->next;
0130 freed += p->size;
0131 if (p->size > 0)
0132 kfree(p);
0133 }
0134 llist = llist_del_all(&buf->free);
0135 llist_for_each_entry_safe(p, next, llist, free)
0136 kfree(p);
0137
0138 tty_buffer_reset(&buf->sentinel, 0);
0139 buf->head = &buf->sentinel;
0140 buf->tail = &buf->sentinel;
0141
0142 still_used = atomic_xchg(&buf->mem_used, 0);
0143 WARN(still_used != freed, "we still have not freed %d bytes!",
0144 still_used - freed);
0145 }
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159 static struct tty_buffer *tty_buffer_alloc(struct tty_port *port, size_t size)
0160 {
0161 struct llist_node *free;
0162 struct tty_buffer *p;
0163
0164
0165 size = __ALIGN_MASK(size, TTYB_ALIGN_MASK);
0166
0167 if (size <= MIN_TTYB_SIZE) {
0168 free = llist_del_first(&port->buf.free);
0169 if (free) {
0170 p = llist_entry(free, struct tty_buffer, free);
0171 goto found;
0172 }
0173 }
0174
0175
0176
0177
0178 if (atomic_read(&port->buf.mem_used) > port->buf.mem_limit)
0179 return NULL;
0180 p = kmalloc(sizeof(struct tty_buffer) + 2 * size,
0181 GFP_ATOMIC | __GFP_NOWARN);
0182 if (p == NULL)
0183 return NULL;
0184
0185 found:
0186 tty_buffer_reset(p, size);
0187 atomic_add(size, &port->buf.mem_used);
0188 return p;
0189 }
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199 static void tty_buffer_free(struct tty_port *port, struct tty_buffer *b)
0200 {
0201 struct tty_bufhead *buf = &port->buf;
0202
0203
0204 WARN_ON(atomic_sub_return(b->size, &buf->mem_used) < 0);
0205
0206 if (b->size > MIN_TTYB_SIZE)
0207 kfree(b);
0208 else if (b->size > 0)
0209 llist_add(&b->free, &buf->free);
0210 }
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222 void tty_buffer_flush(struct tty_struct *tty, struct tty_ldisc *ld)
0223 {
0224 struct tty_port *port = tty->port;
0225 struct tty_bufhead *buf = &port->buf;
0226 struct tty_buffer *next;
0227
0228 atomic_inc(&buf->priority);
0229
0230 mutex_lock(&buf->lock);
0231
0232
0233
0234 while ((next = smp_load_acquire(&buf->head->next)) != NULL) {
0235 tty_buffer_free(port, buf->head);
0236 buf->head = next;
0237 }
0238 buf->head->read = buf->head->commit;
0239 buf->head->lookahead = buf->head->read;
0240
0241 if (ld && ld->ops->flush_buffer)
0242 ld->ops->flush_buffer(tty);
0243
0244 atomic_dec(&buf->priority);
0245 mutex_unlock(&buf->lock);
0246 }
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262 static int __tty_buffer_request_room(struct tty_port *port, size_t size,
0263 int flags)
0264 {
0265 struct tty_bufhead *buf = &port->buf;
0266 struct tty_buffer *b, *n;
0267 int left, change;
0268
0269 b = buf->tail;
0270 if (b->flags & TTYB_NORMAL)
0271 left = 2 * b->size - b->used;
0272 else
0273 left = b->size - b->used;
0274
0275 change = (b->flags & TTYB_NORMAL) && (~flags & TTYB_NORMAL);
0276 if (change || left < size) {
0277
0278 n = tty_buffer_alloc(port, size);
0279 if (n != NULL) {
0280 n->flags = flags;
0281 buf->tail = n;
0282
0283
0284
0285
0286 smp_store_release(&b->commit, b->used);
0287
0288
0289
0290
0291
0292 smp_store_release(&b->next, n);
0293 } else if (change)
0294 size = 0;
0295 else
0296 size = left;
0297 }
0298 return size;
0299 }
0300
0301 int tty_buffer_request_room(struct tty_port *port, size_t size)
0302 {
0303 return __tty_buffer_request_room(port, size, 0);
0304 }
0305 EXPORT_SYMBOL_GPL(tty_buffer_request_room);
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319 int tty_insert_flip_string_fixed_flag(struct tty_port *port,
0320 const unsigned char *chars, char flag, size_t size)
0321 {
0322 int copied = 0;
0323
0324 do {
0325 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
0326 int flags = (flag == TTY_NORMAL) ? TTYB_NORMAL : 0;
0327 int space = __tty_buffer_request_room(port, goal, flags);
0328 struct tty_buffer *tb = port->buf.tail;
0329
0330 if (unlikely(space == 0))
0331 break;
0332 memcpy(char_buf_ptr(tb, tb->used), chars, space);
0333 if (~tb->flags & TTYB_NORMAL)
0334 memset(flag_buf_ptr(tb, tb->used), flag, space);
0335 tb->used += space;
0336 copied += space;
0337 chars += space;
0338
0339
0340
0341 } while (unlikely(size > copied));
0342 return copied;
0343 }
0344 EXPORT_SYMBOL(tty_insert_flip_string_fixed_flag);
0345
0346
0347
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358 int tty_insert_flip_string_flags(struct tty_port *port,
0359 const unsigned char *chars, const char *flags, size_t size)
0360 {
0361 int copied = 0;
0362
0363 do {
0364 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
0365 int space = tty_buffer_request_room(port, goal);
0366 struct tty_buffer *tb = port->buf.tail;
0367
0368 if (unlikely(space == 0))
0369 break;
0370 memcpy(char_buf_ptr(tb, tb->used), chars, space);
0371 memcpy(flag_buf_ptr(tb, tb->used), flags, space);
0372 tb->used += space;
0373 copied += space;
0374 chars += space;
0375 flags += space;
0376
0377
0378
0379 } while (unlikely(size > copied));
0380 return copied;
0381 }
0382 EXPORT_SYMBOL(tty_insert_flip_string_flags);
0383
0384
0385
0386
0387
0388
0389
0390
0391
0392
0393 int __tty_insert_flip_char(struct tty_port *port, unsigned char ch, char flag)
0394 {
0395 struct tty_buffer *tb;
0396 int flags = (flag == TTY_NORMAL) ? TTYB_NORMAL : 0;
0397
0398 if (!__tty_buffer_request_room(port, 1, flags))
0399 return 0;
0400
0401 tb = port->buf.tail;
0402 if (~tb->flags & TTYB_NORMAL)
0403 *flag_buf_ptr(tb, tb->used) = flag;
0404 *char_buf_ptr(tb, tb->used++) = ch;
0405
0406 return 1;
0407 }
0408 EXPORT_SYMBOL(__tty_insert_flip_char);
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424 int tty_prepare_flip_string(struct tty_port *port, unsigned char **chars,
0425 size_t size)
0426 {
0427 int space = __tty_buffer_request_room(port, size, TTYB_NORMAL);
0428
0429 if (likely(space)) {
0430 struct tty_buffer *tb = port->buf.tail;
0431
0432 *chars = char_buf_ptr(tb, tb->used);
0433 if (~tb->flags & TTYB_NORMAL)
0434 memset(flag_buf_ptr(tb, tb->used), TTY_NORMAL, space);
0435 tb->used += space;
0436 }
0437 return space;
0438 }
0439 EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
0440
0441
0442
0443
0444
0445
0446
0447
0448
0449
0450
0451
0452
0453 int tty_ldisc_receive_buf(struct tty_ldisc *ld, const unsigned char *p,
0454 const char *f, int count)
0455 {
0456 if (ld->ops->receive_buf2)
0457 count = ld->ops->receive_buf2(ld->tty, p, f, count);
0458 else {
0459 count = min_t(int, count, ld->tty->receive_room);
0460 if (count && ld->ops->receive_buf)
0461 ld->ops->receive_buf(ld->tty, p, f, count);
0462 }
0463 return count;
0464 }
0465 EXPORT_SYMBOL_GPL(tty_ldisc_receive_buf);
0466
0467 static void lookahead_bufs(struct tty_port *port, struct tty_buffer *head)
0468 {
0469 head->lookahead = max(head->lookahead, head->read);
0470
0471 while (head) {
0472 struct tty_buffer *next;
0473 unsigned int count;
0474
0475
0476
0477
0478
0479
0480 next = smp_load_acquire(&head->next);
0481
0482
0483
0484
0485 count = smp_load_acquire(&head->commit) - head->lookahead;
0486 if (!count) {
0487 head = next;
0488 continue;
0489 }
0490
0491 if (port->client_ops->lookahead_buf) {
0492 unsigned char *p, *f = NULL;
0493
0494 p = char_buf_ptr(head, head->lookahead);
0495 if (~head->flags & TTYB_NORMAL)
0496 f = flag_buf_ptr(head, head->lookahead);
0497
0498 port->client_ops->lookahead_buf(port, p, f, count);
0499 }
0500
0501 head->lookahead += count;
0502 }
0503 }
0504
0505 static int
0506 receive_buf(struct tty_port *port, struct tty_buffer *head, int count)
0507 {
0508 unsigned char *p = char_buf_ptr(head, head->read);
0509 const char *f = NULL;
0510 int n;
0511
0512 if (~head->flags & TTYB_NORMAL)
0513 f = flag_buf_ptr(head, head->read);
0514
0515 n = port->client_ops->receive_buf(port, p, f, count);
0516 if (n > 0)
0517 memset(p, 0, n);
0518 return n;
0519 }
0520
0521
0522
0523
0524
0525
0526
0527
0528
0529
0530
0531
0532 static void flush_to_ldisc(struct work_struct *work)
0533 {
0534 struct tty_port *port = container_of(work, struct tty_port, buf.work);
0535 struct tty_bufhead *buf = &port->buf;
0536
0537 mutex_lock(&buf->lock);
0538
0539 while (1) {
0540 struct tty_buffer *head = buf->head;
0541 struct tty_buffer *next;
0542 int count, rcvd;
0543
0544
0545 if (atomic_read(&buf->priority))
0546 break;
0547
0548
0549
0550
0551
0552 next = smp_load_acquire(&head->next);
0553
0554
0555
0556 count = smp_load_acquire(&head->commit) - head->read;
0557 if (!count) {
0558 if (next == NULL)
0559 break;
0560 buf->head = next;
0561 tty_buffer_free(port, head);
0562 continue;
0563 }
0564
0565 rcvd = receive_buf(port, head, count);
0566 head->read += rcvd;
0567 if (rcvd < count)
0568 lookahead_bufs(port, head);
0569 if (!rcvd)
0570 break;
0571
0572 if (need_resched())
0573 cond_resched();
0574 }
0575
0576 mutex_unlock(&buf->lock);
0577
0578 }
0579
0580 static inline void tty_flip_buffer_commit(struct tty_buffer *tail)
0581 {
0582
0583
0584
0585
0586 smp_store_release(&tail->commit, tail->used);
0587 }
0588
0589
0590
0591
0592
0593
0594
0595
0596
0597
0598
0599 void tty_flip_buffer_push(struct tty_port *port)
0600 {
0601 struct tty_bufhead *buf = &port->buf;
0602
0603 tty_flip_buffer_commit(buf->tail);
0604 queue_work(system_unbound_wq, &buf->work);
0605 }
0606 EXPORT_SYMBOL(tty_flip_buffer_push);
0607
0608
0609
0610
0611
0612
0613
0614
0615
0616
0617
0618
0619
0620
0621
0622 int tty_insert_flip_string_and_push_buffer(struct tty_port *port,
0623 const unsigned char *chars, size_t size)
0624 {
0625 struct tty_bufhead *buf = &port->buf;
0626 unsigned long flags;
0627
0628 spin_lock_irqsave(&port->lock, flags);
0629 size = tty_insert_flip_string(port, chars, size);
0630 if (size)
0631 tty_flip_buffer_commit(buf->tail);
0632 spin_unlock_irqrestore(&port->lock, flags);
0633
0634 queue_work(system_unbound_wq, &buf->work);
0635
0636 return size;
0637 }
0638
0639
0640
0641
0642
0643
0644
0645
0646 void tty_buffer_init(struct tty_port *port)
0647 {
0648 struct tty_bufhead *buf = &port->buf;
0649
0650 mutex_init(&buf->lock);
0651 tty_buffer_reset(&buf->sentinel, 0);
0652 buf->head = &buf->sentinel;
0653 buf->tail = &buf->sentinel;
0654 init_llist_head(&buf->free);
0655 atomic_set(&buf->mem_used, 0);
0656 atomic_set(&buf->priority, 0);
0657 INIT_WORK(&buf->work, flush_to_ldisc);
0658 buf->mem_limit = TTYB_DEFAULT_MEM_LIMIT;
0659 }
0660
0661
0662
0663
0664
0665
0666
0667
0668
0669
0670 int tty_buffer_set_limit(struct tty_port *port, int limit)
0671 {
0672 if (limit < MIN_TTYB_SIZE)
0673 return -EINVAL;
0674 port->buf.mem_limit = limit;
0675 return 0;
0676 }
0677 EXPORT_SYMBOL_GPL(tty_buffer_set_limit);
0678
0679
0680 void tty_buffer_set_lock_subclass(struct tty_port *port)
0681 {
0682 lockdep_set_subclass(&port->buf.lock, TTY_LOCK_SLAVE);
0683 }
0684
0685 bool tty_buffer_restart_work(struct tty_port *port)
0686 {
0687 return queue_work(system_unbound_wq, &port->buf.work);
0688 }
0689
0690 bool tty_buffer_cancel_work(struct tty_port *port)
0691 {
0692 return cancel_work_sync(&port->buf.work);
0693 }
0694
0695 void tty_buffer_flush_work(struct tty_port *port)
0696 {
0697 flush_work(&port->buf.work);
0698 }