aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/tty/tty_buffer.c
diff options
context:
space:
mode:
authorPeter Hurley <peter@hurleysoftware.com>2013-06-15 09:36:08 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2013-07-23 19:47:08 -0400
commit7bfe0b7116be207cf2204ae06335cc89d8f8ee02 (patch)
tree03175cd2ce9dd8013c4aaa8e2dd426767aa1a4c7 /drivers/tty/tty_buffer.c
parent7391ee16950e772076d321792d9fbf030f921345 (diff)
tty: Track flip buffer memory limit atomically
Lockless flip buffers require atomically updating the bytes-in-use watermark. The pty driver also peeks at the watermark value to limit memory consumption to a much lower value than the default; query the watermark with new fn, tty_buffer_space_avail(). Signed-off-by: Peter Hurley <peter@hurleysoftware.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'drivers/tty/tty_buffer.c')
-rw-r--r--drivers/tty/tty_buffer.c37
1 files changed, 31 insertions, 6 deletions
diff --git a/drivers/tty/tty_buffer.c b/drivers/tty/tty_buffer.c
index 231b7a8710f1..5d5a56407aa8 100644
--- a/drivers/tty/tty_buffer.c
+++ b/drivers/tty/tty_buffer.c
@@ -22,6 +22,31 @@
22#define MIN_TTYB_SIZE 256 22#define MIN_TTYB_SIZE 256
23#define TTYB_ALIGN_MASK 255 23#define TTYB_ALIGN_MASK 255
24 24
25/*
26 * Byte threshold to limit memory consumption for flip buffers.
27 * The actual memory limit is > 2x this amount.
28 */
29#define TTYB_MEM_LIMIT 65536
30
31
32/**
33 * tty_buffer_space_avail - return unused buffer space
34 * @port - tty_port owning the flip buffer
35 *
36 * Returns the # of bytes which can be written by the driver without
37 * reaching the buffer limit.
38 *
39 * Note: this does not guarantee that memory is available to write
40 * the returned # of bytes (use tty_prepare_flip_string_xxx() to
41 * pre-allocate if memory guarantee is required).
42 */
43
44int tty_buffer_space_avail(struct tty_port *port)
45{
46 int space = TTYB_MEM_LIMIT - atomic_read(&port->buf.memory_used);
47 return max(space, 0);
48}
49
25static void tty_buffer_reset(struct tty_buffer *p, size_t size) 50static void tty_buffer_reset(struct tty_buffer *p, size_t size)
26{ 51{
27 p->used = 0; 52 p->used = 0;
@@ -59,7 +84,8 @@ void tty_buffer_free_all(struct tty_port *port)
59 tty_buffer_reset(&buf->sentinel, 0); 84 tty_buffer_reset(&buf->sentinel, 0);
60 buf->head = &buf->sentinel; 85 buf->head = &buf->sentinel;
61 buf->tail = &buf->sentinel; 86 buf->tail = &buf->sentinel;
62 buf->memory_used = 0; 87
88 atomic_set(&buf->memory_used, 0);
63} 89}
64 90
65/** 91/**
@@ -92,7 +118,7 @@ static struct tty_buffer *tty_buffer_alloc(struct tty_port *port, size_t size)
92 118
93 /* Should possibly check if this fails for the largest buffer we 119 /* Should possibly check if this fails for the largest buffer we
94 have queued and recycle that ? */ 120 have queued and recycle that ? */
95 if (port->buf.memory_used + size > 65536) 121 if (atomic_read(&port->buf.memory_used) > TTYB_MEM_LIMIT)
96 return NULL; 122 return NULL;
97 p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC); 123 p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC);
98 if (p == NULL) 124 if (p == NULL)
@@ -100,7 +126,7 @@ static struct tty_buffer *tty_buffer_alloc(struct tty_port *port, size_t size)
100 126
101found: 127found:
102 tty_buffer_reset(p, size); 128 tty_buffer_reset(p, size);
103 port->buf.memory_used += size; 129 atomic_add(size, &port->buf.memory_used);
104 return p; 130 return p;
105} 131}
106 132
@@ -118,8 +144,7 @@ static void tty_buffer_free(struct tty_port *port, struct tty_buffer *b)
118 struct tty_bufhead *buf = &port->buf; 144 struct tty_bufhead *buf = &port->buf;
119 145
120 /* Dumb strategy for now - should keep some stats */ 146 /* Dumb strategy for now - should keep some stats */
121 buf->memory_used -= b->size; 147 WARN_ON(atomic_sub_return(b->size, &buf->memory_used) < 0);
122 WARN_ON(buf->memory_used < 0);
123 148
124 if (b->size > MIN_TTYB_SIZE) 149 if (b->size > MIN_TTYB_SIZE)
125 kfree(b); 150 kfree(b);
@@ -525,7 +550,7 @@ void tty_buffer_init(struct tty_port *port)
525 buf->head = &buf->sentinel; 550 buf->head = &buf->sentinel;
526 buf->tail = &buf->sentinel; 551 buf->tail = &buf->sentinel;
527 init_llist_head(&buf->free); 552 init_llist_head(&buf->free);
528 buf->memory_used = 0; 553 atomic_set(&buf->memory_used, 0);
529 INIT_WORK(&buf->work, flush_to_ldisc); 554 INIT_WORK(&buf->work, flush_to_ldisc);
530} 555}
531 556