提交 7bfe0b71 编写于 作者: P Peter Hurley 提交者: Greg Kroah-Hartman

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: NPeter Hurley <peter@hurleysoftware.com>
Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
上级 7391ee16
...@@ -89,17 +89,13 @@ static void pty_unthrottle(struct tty_struct *tty) ...@@ -89,17 +89,13 @@ static void pty_unthrottle(struct tty_struct *tty)
* pty_space - report space left for writing * pty_space - report space left for writing
* @to: tty we are writing into * @to: tty we are writing into
* *
* The tty buffers allow 64K but we sneak a peak and clip at 8K this * Limit the buffer space used by ptys to 8k.
* allows a lot of overspill room for echo and other fun messes to
* be handled properly
*/ */
static int pty_space(struct tty_struct *to) static int pty_space(struct tty_struct *to)
{ {
int n = 8192 - to->port->buf.memory_used; int n = tty_buffer_space_avail(to->port);
if (n < 0) return min(n, 8192);
return 0;
return n;
} }
/** /**
......
...@@ -22,6 +22,31 @@ ...@@ -22,6 +22,31 @@
#define MIN_TTYB_SIZE 256 #define MIN_TTYB_SIZE 256
#define TTYB_ALIGN_MASK 255 #define TTYB_ALIGN_MASK 255
/*
* Byte threshold to limit memory consumption for flip buffers.
* The actual memory limit is > 2x this amount.
*/
#define TTYB_MEM_LIMIT 65536
/**
* tty_buffer_space_avail - return unused buffer space
* @port - tty_port owning the flip buffer
*
* Returns the # of bytes which can be written by the driver without
* reaching the buffer limit.
*
* Note: this does not guarantee that memory is available to write
* the returned # of bytes (use tty_prepare_flip_string_xxx() to
* pre-allocate if memory guarantee is required).
*/
int tty_buffer_space_avail(struct tty_port *port)
{
int space = TTYB_MEM_LIMIT - atomic_read(&port->buf.memory_used);
return max(space, 0);
}
static void tty_buffer_reset(struct tty_buffer *p, size_t size) static void tty_buffer_reset(struct tty_buffer *p, size_t size)
{ {
p->used = 0; p->used = 0;
...@@ -59,7 +84,8 @@ void tty_buffer_free_all(struct tty_port *port) ...@@ -59,7 +84,8 @@ void tty_buffer_free_all(struct tty_port *port)
tty_buffer_reset(&buf->sentinel, 0); tty_buffer_reset(&buf->sentinel, 0);
buf->head = &buf->sentinel; buf->head = &buf->sentinel;
buf->tail = &buf->sentinel; buf->tail = &buf->sentinel;
buf->memory_used = 0;
atomic_set(&buf->memory_used, 0);
} }
/** /**
...@@ -92,7 +118,7 @@ static struct tty_buffer *tty_buffer_alloc(struct tty_port *port, size_t size) ...@@ -92,7 +118,7 @@ static struct tty_buffer *tty_buffer_alloc(struct tty_port *port, size_t size)
/* Should possibly check if this fails for the largest buffer we /* Should possibly check if this fails for the largest buffer we
have queued and recycle that ? */ have queued and recycle that ? */
if (port->buf.memory_used + size > 65536) if (atomic_read(&port->buf.memory_used) > TTYB_MEM_LIMIT)
return NULL; return NULL;
p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC); p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC);
if (p == NULL) if (p == NULL)
...@@ -100,7 +126,7 @@ static struct tty_buffer *tty_buffer_alloc(struct tty_port *port, size_t size) ...@@ -100,7 +126,7 @@ static struct tty_buffer *tty_buffer_alloc(struct tty_port *port, size_t size)
found: found:
tty_buffer_reset(p, size); tty_buffer_reset(p, size);
port->buf.memory_used += size; atomic_add(size, &port->buf.memory_used);
return p; return p;
} }
...@@ -118,8 +144,7 @@ static void tty_buffer_free(struct tty_port *port, struct tty_buffer *b) ...@@ -118,8 +144,7 @@ static void tty_buffer_free(struct tty_port *port, struct tty_buffer *b)
struct tty_bufhead *buf = &port->buf; struct tty_bufhead *buf = &port->buf;
/* Dumb strategy for now - should keep some stats */ /* Dumb strategy for now - should keep some stats */
buf->memory_used -= b->size; WARN_ON(atomic_sub_return(b->size, &buf->memory_used) < 0);
WARN_ON(buf->memory_used < 0);
if (b->size > MIN_TTYB_SIZE) if (b->size > MIN_TTYB_SIZE)
kfree(b); kfree(b);
...@@ -525,7 +550,7 @@ void tty_buffer_init(struct tty_port *port) ...@@ -525,7 +550,7 @@ void tty_buffer_init(struct tty_port *port)
buf->head = &buf->sentinel; buf->head = &buf->sentinel;
buf->tail = &buf->sentinel; buf->tail = &buf->sentinel;
init_llist_head(&buf->free); init_llist_head(&buf->free);
buf->memory_used = 0; atomic_set(&buf->memory_used, 0);
INIT_WORK(&buf->work, flush_to_ldisc); INIT_WORK(&buf->work, flush_to_ldisc);
} }
...@@ -71,8 +71,7 @@ struct tty_bufhead { ...@@ -71,8 +71,7 @@ struct tty_bufhead {
struct tty_buffer *head; /* Queue head */ struct tty_buffer *head; /* Queue head */
struct tty_buffer *tail; /* Active buffer */ struct tty_buffer *tail; /* Active buffer */
struct llist_head free; /* Free queue head */ struct llist_head free; /* Free queue head */
int memory_used; /* Buffer space used excluding atomic_t memory_used; /* In-use buffers excluding free list */
free queue */
}; };
/* /*
* When a break, frame error, or parity error happens, these codes are * When a break, frame error, or parity error happens, these codes are
......
#ifndef _LINUX_TTY_FLIP_H #ifndef _LINUX_TTY_FLIP_H
#define _LINUX_TTY_FLIP_H #define _LINUX_TTY_FLIP_H
extern int tty_buffer_space_avail(struct tty_port *port);
extern int tty_buffer_request_room(struct tty_port *port, size_t size); extern int tty_buffer_request_room(struct tty_port *port, size_t size);
extern int tty_insert_flip_string_flags(struct tty_port *port, extern int tty_insert_flip_string_flags(struct tty_port *port,
const unsigned char *chars, const char *flags, size_t size); const unsigned char *chars, const char *flags, size_t size);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册