Skip to content

Commit

Permalink
TTY: tty_buffer, cache pointer to tty->buf
Browse files Browse the repository at this point in the history
During the move of tty buffers from tty_struct to tty_port, we will
need to switch all users of buf to tty->port->buf. There are many
functions where this is accessed directly in their code many times.
Cache the tty->buf pointer in such functions now and change only
single lines in each function in the next patch.

Not that it is convenient for the next patch, but the code is now also
more readable.

Signed-off-by: Jiri Slaby <jslaby@suse.cz>
Acked-by: Alan Cox <alan@linux.intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
Jiri Slaby authored and Greg Kroah-Hartman committed Oct 22, 2012
1 parent 2fc2066 commit 5cff39c
Showing 1 changed file with 76 additions and 56 deletions.
132 changes: 76 additions & 56 deletions drivers/tty/tty_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,19 @@

void tty_buffer_free_all(struct tty_struct *tty)
{
struct tty_bufhead *buf = &tty->buf;
struct tty_buffer *thead;
while ((thead = tty->buf.head) != NULL) {
tty->buf.head = thead->next;

while ((thead = buf->head) != NULL) {
buf->head = thead->next;
kfree(thead);
}
while ((thead = tty->buf.free) != NULL) {
tty->buf.free = thead->next;
while ((thead = buf->free) != NULL) {
buf->free = thead->next;
kfree(thead);
}
tty->buf.tail = NULL;
tty->buf.memory_used = 0;
buf->tail = NULL;
buf->memory_used = 0;
}

/**
Expand Down Expand Up @@ -87,15 +89,17 @@ static struct tty_buffer *tty_buffer_alloc(struct tty_struct *tty, size_t size)

static void tty_buffer_free(struct tty_struct *tty, struct tty_buffer *b)
{
struct tty_bufhead *buf = &tty->buf;

/* Dumb strategy for now - should keep some stats */
tty->buf.memory_used -= b->size;
WARN_ON(tty->buf.memory_used < 0);
buf->memory_used -= b->size;
WARN_ON(buf->memory_used < 0);

if (b->size >= 512)
kfree(b);
else {
b->next = tty->buf.free;
tty->buf.free = b;
b->next = buf->free;
buf->free = b;
}
}

Expand All @@ -112,13 +116,14 @@ static void tty_buffer_free(struct tty_struct *tty, struct tty_buffer *b)

static void __tty_buffer_flush(struct tty_struct *tty)
{
struct tty_bufhead *buf = &tty->buf;
struct tty_buffer *thead;

while ((thead = tty->buf.head) != NULL) {
tty->buf.head = thead->next;
while ((thead = buf->head) != NULL) {
buf->head = thead->next;
tty_buffer_free(tty, thead);
}
tty->buf.tail = NULL;
buf->tail = NULL;
}

/**
Expand All @@ -135,21 +140,23 @@ static void __tty_buffer_flush(struct tty_struct *tty)
void tty_buffer_flush(struct tty_struct *tty)
{
struct tty_port *port = tty->port;
struct tty_bufhead *buf = &tty->buf;
unsigned long flags;
spin_lock_irqsave(&tty->buf.lock, flags);

spin_lock_irqsave(&buf->lock, flags);

/* If the data is being pushed to the tty layer then we can't
process it here. Instead set a flag and the flush_to_ldisc
path will process the flush request before it exits */
if (test_bit(TTYP_FLUSHING, &port->iflags)) {
set_bit(TTYP_FLUSHPENDING, &port->iflags);
spin_unlock_irqrestore(&tty->buf.lock, flags);
spin_unlock_irqrestore(&buf->lock, flags);
wait_event(tty->read_wait,
test_bit(TTYP_FLUSHPENDING, &port->iflags) == 0);
return;
} else
__tty_buffer_flush(tty);
spin_unlock_irqrestore(&tty->buf.lock, flags);
spin_unlock_irqrestore(&buf->lock, flags);
}

/**
Expand Down Expand Up @@ -197,12 +204,14 @@ static struct tty_buffer *tty_buffer_find(struct tty_struct *tty, size_t size)
*/
static int __tty_buffer_request_room(struct tty_struct *tty, size_t size)
{
struct tty_bufhead *buf = &tty->buf;
struct tty_buffer *b, *n;
int left;
/* OPTIMISATION: We could keep a per tty "zero" sized buffer to
remove this conditional if its worth it. This would be invisible
to the callers */
if ((b = tty->buf.tail) != NULL)
b = buf->tail;
if (b != NULL)
left = b->size - b->used;
else
left = 0;
Expand All @@ -214,8 +223,8 @@ static int __tty_buffer_request_room(struct tty_struct *tty, size_t size)
b->next = n;
b->commit = b->used;
} else
tty->buf.head = n;
tty->buf.tail = n;
buf->head = n;
buf->tail = n;
} else
size = left;
}
Expand Down Expand Up @@ -262,25 +271,26 @@ EXPORT_SYMBOL_GPL(tty_buffer_request_room);
int tty_insert_flip_string_fixed_flag(struct tty_struct *tty,
const unsigned char *chars, char flag, size_t size)
{
struct tty_bufhead *buf = &tty->buf;
int copied = 0;
do {
int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
int space;
unsigned long flags;
struct tty_buffer *tb;

spin_lock_irqsave(&tty->buf.lock, flags);
spin_lock_irqsave(&buf->lock, flags);
space = __tty_buffer_request_room(tty, goal);
tb = tty->buf.tail;
tb = buf->tail;
/* If there is no space then tb may be NULL */
if (unlikely(space == 0)) {
spin_unlock_irqrestore(&tty->buf.lock, flags);
spin_unlock_irqrestore(&buf->lock, flags);
break;
}
memcpy(tb->char_buf_ptr + tb->used, chars, space);
memset(tb->flag_buf_ptr + tb->used, flag, space);
tb->used += space;
spin_unlock_irqrestore(&tty->buf.lock, flags);
spin_unlock_irqrestore(&buf->lock, flags);
copied += space;
chars += space;
/* There is a small chance that we need to split the data over
Expand All @@ -307,25 +317,26 @@ EXPORT_SYMBOL(tty_insert_flip_string_fixed_flag);
int tty_insert_flip_string_flags(struct tty_struct *tty,
const unsigned char *chars, const char *flags, size_t size)
{
struct tty_bufhead *buf = &tty->buf;
int copied = 0;
do {
int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
int space;
unsigned long __flags;
struct tty_buffer *tb;

spin_lock_irqsave(&tty->buf.lock, __flags);
spin_lock_irqsave(&buf->lock, __flags);
space = __tty_buffer_request_room(tty, goal);
tb = tty->buf.tail;
tb = buf->tail;
/* If there is no space then tb may be NULL */
if (unlikely(space == 0)) {
spin_unlock_irqrestore(&tty->buf.lock, __flags);
spin_unlock_irqrestore(&buf->lock, __flags);
break;
}
memcpy(tb->char_buf_ptr + tb->used, chars, space);
memcpy(tb->flag_buf_ptr + tb->used, flags, space);
tb->used += space;
spin_unlock_irqrestore(&tty->buf.lock, __flags);
spin_unlock_irqrestore(&buf->lock, __flags);
copied += space;
chars += space;
flags += space;
Expand All @@ -351,12 +362,14 @@ EXPORT_SYMBOL(tty_insert_flip_string_flags);

void tty_schedule_flip(struct tty_struct *tty)
{
struct tty_bufhead *buf = &tty->buf;
unsigned long flags;
spin_lock_irqsave(&tty->buf.lock, flags);
if (tty->buf.tail != NULL)
tty->buf.tail->commit = tty->buf.tail->used;
spin_unlock_irqrestore(&tty->buf.lock, flags);
schedule_work(&tty->buf.work);

spin_lock_irqsave(&buf->lock, flags);
if (buf->tail != NULL)
buf->tail->commit = buf->tail->used;
spin_unlock_irqrestore(&buf->lock, flags);
schedule_work(&buf->work);
}
EXPORT_SYMBOL(tty_schedule_flip);

Expand All @@ -378,20 +391,21 @@ EXPORT_SYMBOL(tty_schedule_flip);
int tty_prepare_flip_string(struct tty_struct *tty, unsigned char **chars,
size_t size)
{
struct tty_bufhead *buf = &tty->buf;
int space;
unsigned long flags;
struct tty_buffer *tb;

spin_lock_irqsave(&tty->buf.lock, flags);
spin_lock_irqsave(&buf->lock, flags);
space = __tty_buffer_request_room(tty, size);

tb = tty->buf.tail;
tb = buf->tail;
if (likely(space)) {
*chars = tb->char_buf_ptr + tb->used;
memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
tb->used += space;
}
spin_unlock_irqrestore(&tty->buf.lock, flags);
spin_unlock_irqrestore(&buf->lock, flags);
return space;
}
EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
Expand All @@ -415,20 +429,21 @@ EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
int tty_prepare_flip_string_flags(struct tty_struct *tty,
unsigned char **chars, char **flags, size_t size)
{
struct tty_bufhead *buf = &tty->buf;
int space;
unsigned long __flags;
struct tty_buffer *tb;

spin_lock_irqsave(&tty->buf.lock, __flags);
spin_lock_irqsave(&buf->lock, __flags);
space = __tty_buffer_request_room(tty, size);

tb = tty->buf.tail;
tb = buf->tail;
if (likely(space)) {
*chars = tb->char_buf_ptr + tb->used;
*flags = tb->flag_buf_ptr + tb->used;
tb->used += space;
}
spin_unlock_irqrestore(&tty->buf.lock, __flags);
spin_unlock_irqrestore(&buf->lock, __flags);
return space;
}
EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
Expand All @@ -452,18 +467,19 @@ static void flush_to_ldisc(struct work_struct *work)
struct tty_struct *tty =
container_of(work, struct tty_struct, buf.work);
struct tty_port *port = tty->port;
struct tty_bufhead *buf = &tty->buf;
unsigned long flags;
struct tty_ldisc *disc;

disc = tty_ldisc_ref(tty);
if (disc == NULL) /* !TTY_LDISC */
return;

spin_lock_irqsave(&tty->buf.lock, flags);
spin_lock_irqsave(&buf->lock, flags);

if (!test_and_set_bit(TTYP_FLUSHING, &port->iflags)) {
struct tty_buffer *head;
while ((head = tty->buf.head) != NULL) {
while ((head = buf->head) != NULL) {
int count;
char *char_buf;
unsigned char *flag_buf;
Expand All @@ -472,7 +488,7 @@ static void flush_to_ldisc(struct work_struct *work)
if (!count) {
if (head->next == NULL)
break;
tty->buf.head = head->next;
buf->head = head->next;
tty_buffer_free(tty, head);
continue;
}
Expand All @@ -488,10 +504,10 @@ static void flush_to_ldisc(struct work_struct *work)
char_buf = head->char_buf_ptr + head->read;
flag_buf = head->flag_buf_ptr + head->read;
head->read += count;
spin_unlock_irqrestore(&tty->buf.lock, flags);
spin_unlock_irqrestore(&buf->lock, flags);
disc->ops->receive_buf(tty, char_buf,
flag_buf, count);
spin_lock_irqsave(&tty->buf.lock, flags);
spin_lock_irqsave(&buf->lock, flags);
}
clear_bit(TTYP_FLUSHING, &port->iflags);
}
Expand All @@ -503,7 +519,7 @@ static void flush_to_ldisc(struct work_struct *work)
clear_bit(TTYP_FLUSHPENDING, &port->iflags);
wake_up(&tty->read_wait);
}
spin_unlock_irqrestore(&tty->buf.lock, flags);
spin_unlock_irqrestore(&buf->lock, flags);

tty_ldisc_deref(disc);
}
Expand Down Expand Up @@ -537,16 +553,18 @@ void tty_flush_to_ldisc(struct tty_struct *tty)

void tty_flip_buffer_push(struct tty_struct *tty)
{
struct tty_bufhead *buf = &tty->buf;
unsigned long flags;
spin_lock_irqsave(&tty->buf.lock, flags);
if (tty->buf.tail != NULL)
tty->buf.tail->commit = tty->buf.tail->used;
spin_unlock_irqrestore(&tty->buf.lock, flags);

spin_lock_irqsave(&buf->lock, flags);
if (buf->tail != NULL)
buf->tail->commit = buf->tail->used;
spin_unlock_irqrestore(&buf->lock, flags);

if (tty->low_latency)
flush_to_ldisc(&tty->buf.work);
flush_to_ldisc(&buf->work);
else
schedule_work(&tty->buf.work);
schedule_work(&buf->work);
}
EXPORT_SYMBOL(tty_flip_buffer_push);

Expand All @@ -562,11 +580,13 @@ EXPORT_SYMBOL(tty_flip_buffer_push);

void tty_buffer_init(struct tty_struct *tty)
{
spin_lock_init(&tty->buf.lock);
tty->buf.head = NULL;
tty->buf.tail = NULL;
tty->buf.free = NULL;
tty->buf.memory_used = 0;
INIT_WORK(&tty->buf.work, flush_to_ldisc);
struct tty_bufhead *buf = &tty->buf;

spin_lock_init(&buf->lock);
buf->head = NULL;
buf->tail = NULL;
buf->free = NULL;
buf->memory_used = 0;
INIT_WORK(&buf->work, flush_to_ldisc);
}

0 comments on commit 5cff39c

Please sign in to comment.