Skip to content

Commit 5cff39c

Browse files
Jiri Slabygregkh
authored andcommitted
TTY: tty_buffer, cache pointer to tty->buf
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 <[email protected]> Acked-by: Alan Cox <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 2fc2066 commit 5cff39c

File tree

1 file changed

+76
-56
lines changed

1 file changed

+76
-56
lines changed

drivers/tty/tty_buffer.c

Lines changed: 76 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,19 @@
2929

3030
void tty_buffer_free_all(struct tty_struct *tty)
3131
{
32+
struct tty_bufhead *buf = &tty->buf;
3233
struct tty_buffer *thead;
33-
while ((thead = tty->buf.head) != NULL) {
34-
tty->buf.head = thead->next;
34+
35+
while ((thead = buf->head) != NULL) {
36+
buf->head = thead->next;
3537
kfree(thead);
3638
}
37-
while ((thead = tty->buf.free) != NULL) {
38-
tty->buf.free = thead->next;
39+
while ((thead = buf->free) != NULL) {
40+
buf->free = thead->next;
3941
kfree(thead);
4042
}
41-
tty->buf.tail = NULL;
42-
tty->buf.memory_used = 0;
43+
buf->tail = NULL;
44+
buf->memory_used = 0;
4345
}
4446

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

8890
static void tty_buffer_free(struct tty_struct *tty, struct tty_buffer *b)
8991
{
92+
struct tty_bufhead *buf = &tty->buf;
93+
9094
/* Dumb strategy for now - should keep some stats */
91-
tty->buf.memory_used -= b->size;
92-
WARN_ON(tty->buf.memory_used < 0);
95+
buf->memory_used -= b->size;
96+
WARN_ON(buf->memory_used < 0);
9397

9498
if (b->size >= 512)
9599
kfree(b);
96100
else {
97-
b->next = tty->buf.free;
98-
tty->buf.free = b;
101+
b->next = buf->free;
102+
buf->free = b;
99103
}
100104
}
101105

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

113117
static void __tty_buffer_flush(struct tty_struct *tty)
114118
{
119+
struct tty_bufhead *buf = &tty->buf;
115120
struct tty_buffer *thead;
116121

117-
while ((thead = tty->buf.head) != NULL) {
118-
tty->buf.head = thead->next;
122+
while ((thead = buf->head) != NULL) {
123+
buf->head = thead->next;
119124
tty_buffer_free(tty, thead);
120125
}
121-
tty->buf.tail = NULL;
126+
buf->tail = NULL;
122127
}
123128

124129
/**
@@ -135,21 +140,23 @@ static void __tty_buffer_flush(struct tty_struct *tty)
135140
void tty_buffer_flush(struct tty_struct *tty)
136141
{
137142
struct tty_port *port = tty->port;
143+
struct tty_bufhead *buf = &tty->buf;
138144
unsigned long flags;
139-
spin_lock_irqsave(&tty->buf.lock, flags);
145+
146+
spin_lock_irqsave(&buf->lock, flags);
140147

141148
/* If the data is being pushed to the tty layer then we can't
142149
process it here. Instead set a flag and the flush_to_ldisc
143150
path will process the flush request before it exits */
144151
if (test_bit(TTYP_FLUSHING, &port->iflags)) {
145152
set_bit(TTYP_FLUSHPENDING, &port->iflags);
146-
spin_unlock_irqrestore(&tty->buf.lock, flags);
153+
spin_unlock_irqrestore(&buf->lock, flags);
147154
wait_event(tty->read_wait,
148155
test_bit(TTYP_FLUSHPENDING, &port->iflags) == 0);
149156
return;
150157
} else
151158
__tty_buffer_flush(tty);
152-
spin_unlock_irqrestore(&tty->buf.lock, flags);
159+
spin_unlock_irqrestore(&buf->lock, flags);
153160
}
154161

155162
/**
@@ -197,12 +204,14 @@ static struct tty_buffer *tty_buffer_find(struct tty_struct *tty, size_t size)
197204
*/
198205
static int __tty_buffer_request_room(struct tty_struct *tty, size_t size)
199206
{
207+
struct tty_bufhead *buf = &tty->buf;
200208
struct tty_buffer *b, *n;
201209
int left;
202210
/* OPTIMISATION: We could keep a per tty "zero" sized buffer to
203211
remove this conditional if its worth it. This would be invisible
204212
to the callers */
205-
if ((b = tty->buf.tail) != NULL)
213+
b = buf->tail;
214+
if (b != NULL)
206215
left = b->size - b->used;
207216
else
208217
left = 0;
@@ -214,8 +223,8 @@ static int __tty_buffer_request_room(struct tty_struct *tty, size_t size)
214223
b->next = n;
215224
b->commit = b->used;
216225
} else
217-
tty->buf.head = n;
218-
tty->buf.tail = n;
226+
buf->head = n;
227+
buf->tail = n;
219228
} else
220229
size = left;
221230
}
@@ -262,25 +271,26 @@ EXPORT_SYMBOL_GPL(tty_buffer_request_room);
262271
int tty_insert_flip_string_fixed_flag(struct tty_struct *tty,
263272
const unsigned char *chars, char flag, size_t size)
264273
{
274+
struct tty_bufhead *buf = &tty->buf;
265275
int copied = 0;
266276
do {
267277
int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
268278
int space;
269279
unsigned long flags;
270280
struct tty_buffer *tb;
271281

272-
spin_lock_irqsave(&tty->buf.lock, flags);
282+
spin_lock_irqsave(&buf->lock, flags);
273283
space = __tty_buffer_request_room(tty, goal);
274-
tb = tty->buf.tail;
284+
tb = buf->tail;
275285
/* If there is no space then tb may be NULL */
276286
if (unlikely(space == 0)) {
277-
spin_unlock_irqrestore(&tty->buf.lock, flags);
287+
spin_unlock_irqrestore(&buf->lock, flags);
278288
break;
279289
}
280290
memcpy(tb->char_buf_ptr + tb->used, chars, space);
281291
memset(tb->flag_buf_ptr + tb->used, flag, space);
282292
tb->used += space;
283-
spin_unlock_irqrestore(&tty->buf.lock, flags);
293+
spin_unlock_irqrestore(&buf->lock, flags);
284294
copied += space;
285295
chars += space;
286296
/* There is a small chance that we need to split the data over
@@ -307,25 +317,26 @@ EXPORT_SYMBOL(tty_insert_flip_string_fixed_flag);
307317
int tty_insert_flip_string_flags(struct tty_struct *tty,
308318
const unsigned char *chars, const char *flags, size_t size)
309319
{
320+
struct tty_bufhead *buf = &tty->buf;
310321
int copied = 0;
311322
do {
312323
int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
313324
int space;
314325
unsigned long __flags;
315326
struct tty_buffer *tb;
316327

317-
spin_lock_irqsave(&tty->buf.lock, __flags);
328+
spin_lock_irqsave(&buf->lock, __flags);
318329
space = __tty_buffer_request_room(tty, goal);
319-
tb = tty->buf.tail;
330+
tb = buf->tail;
320331
/* If there is no space then tb may be NULL */
321332
if (unlikely(space == 0)) {
322-
spin_unlock_irqrestore(&tty->buf.lock, __flags);
333+
spin_unlock_irqrestore(&buf->lock, __flags);
323334
break;
324335
}
325336
memcpy(tb->char_buf_ptr + tb->used, chars, space);
326337
memcpy(tb->flag_buf_ptr + tb->used, flags, space);
327338
tb->used += space;
328-
spin_unlock_irqrestore(&tty->buf.lock, __flags);
339+
spin_unlock_irqrestore(&buf->lock, __flags);
329340
copied += space;
330341
chars += space;
331342
flags += space;
@@ -351,12 +362,14 @@ EXPORT_SYMBOL(tty_insert_flip_string_flags);
351362

352363
void tty_schedule_flip(struct tty_struct *tty)
353364
{
365+
struct tty_bufhead *buf = &tty->buf;
354366
unsigned long flags;
355-
spin_lock_irqsave(&tty->buf.lock, flags);
356-
if (tty->buf.tail != NULL)
357-
tty->buf.tail->commit = tty->buf.tail->used;
358-
spin_unlock_irqrestore(&tty->buf.lock, flags);
359-
schedule_work(&tty->buf.work);
367+
368+
spin_lock_irqsave(&buf->lock, flags);
369+
if (buf->tail != NULL)
370+
buf->tail->commit = buf->tail->used;
371+
spin_unlock_irqrestore(&buf->lock, flags);
372+
schedule_work(&buf->work);
360373
}
361374
EXPORT_SYMBOL(tty_schedule_flip);
362375

@@ -378,20 +391,21 @@ EXPORT_SYMBOL(tty_schedule_flip);
378391
int tty_prepare_flip_string(struct tty_struct *tty, unsigned char **chars,
379392
size_t size)
380393
{
394+
struct tty_bufhead *buf = &tty->buf;
381395
int space;
382396
unsigned long flags;
383397
struct tty_buffer *tb;
384398

385-
spin_lock_irqsave(&tty->buf.lock, flags);
399+
spin_lock_irqsave(&buf->lock, flags);
386400
space = __tty_buffer_request_room(tty, size);
387401

388-
tb = tty->buf.tail;
402+
tb = buf->tail;
389403
if (likely(space)) {
390404
*chars = tb->char_buf_ptr + tb->used;
391405
memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
392406
tb->used += space;
393407
}
394-
spin_unlock_irqrestore(&tty->buf.lock, flags);
408+
spin_unlock_irqrestore(&buf->lock, flags);
395409
return space;
396410
}
397411
EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
@@ -415,20 +429,21 @@ EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
415429
int tty_prepare_flip_string_flags(struct tty_struct *tty,
416430
unsigned char **chars, char **flags, size_t size)
417431
{
432+
struct tty_bufhead *buf = &tty->buf;
418433
int space;
419434
unsigned long __flags;
420435
struct tty_buffer *tb;
421436

422-
spin_lock_irqsave(&tty->buf.lock, __flags);
437+
spin_lock_irqsave(&buf->lock, __flags);
423438
space = __tty_buffer_request_room(tty, size);
424439

425-
tb = tty->buf.tail;
440+
tb = buf->tail;
426441
if (likely(space)) {
427442
*chars = tb->char_buf_ptr + tb->used;
428443
*flags = tb->flag_buf_ptr + tb->used;
429444
tb->used += space;
430445
}
431-
spin_unlock_irqrestore(&tty->buf.lock, __flags);
446+
spin_unlock_irqrestore(&buf->lock, __flags);
432447
return space;
433448
}
434449
EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
@@ -452,18 +467,19 @@ static void flush_to_ldisc(struct work_struct *work)
452467
struct tty_struct *tty =
453468
container_of(work, struct tty_struct, buf.work);
454469
struct tty_port *port = tty->port;
470+
struct tty_bufhead *buf = &tty->buf;
455471
unsigned long flags;
456472
struct tty_ldisc *disc;
457473

458474
disc = tty_ldisc_ref(tty);
459475
if (disc == NULL) /* !TTY_LDISC */
460476
return;
461477

462-
spin_lock_irqsave(&tty->buf.lock, flags);
478+
spin_lock_irqsave(&buf->lock, flags);
463479

464480
if (!test_and_set_bit(TTYP_FLUSHING, &port->iflags)) {
465481
struct tty_buffer *head;
466-
while ((head = tty->buf.head) != NULL) {
482+
while ((head = buf->head) != NULL) {
467483
int count;
468484
char *char_buf;
469485
unsigned char *flag_buf;
@@ -472,7 +488,7 @@ static void flush_to_ldisc(struct work_struct *work)
472488
if (!count) {
473489
if (head->next == NULL)
474490
break;
475-
tty->buf.head = head->next;
491+
buf->head = head->next;
476492
tty_buffer_free(tty, head);
477493
continue;
478494
}
@@ -488,10 +504,10 @@ static void flush_to_ldisc(struct work_struct *work)
488504
char_buf = head->char_buf_ptr + head->read;
489505
flag_buf = head->flag_buf_ptr + head->read;
490506
head->read += count;
491-
spin_unlock_irqrestore(&tty->buf.lock, flags);
507+
spin_unlock_irqrestore(&buf->lock, flags);
492508
disc->ops->receive_buf(tty, char_buf,
493509
flag_buf, count);
494-
spin_lock_irqsave(&tty->buf.lock, flags);
510+
spin_lock_irqsave(&buf->lock, flags);
495511
}
496512
clear_bit(TTYP_FLUSHING, &port->iflags);
497513
}
@@ -503,7 +519,7 @@ static void flush_to_ldisc(struct work_struct *work)
503519
clear_bit(TTYP_FLUSHPENDING, &port->iflags);
504520
wake_up(&tty->read_wait);
505521
}
506-
spin_unlock_irqrestore(&tty->buf.lock, flags);
522+
spin_unlock_irqrestore(&buf->lock, flags);
507523

508524
tty_ldisc_deref(disc);
509525
}
@@ -537,16 +553,18 @@ void tty_flush_to_ldisc(struct tty_struct *tty)
537553

538554
void tty_flip_buffer_push(struct tty_struct *tty)
539555
{
556+
struct tty_bufhead *buf = &tty->buf;
540557
unsigned long flags;
541-
spin_lock_irqsave(&tty->buf.lock, flags);
542-
if (tty->buf.tail != NULL)
543-
tty->buf.tail->commit = tty->buf.tail->used;
544-
spin_unlock_irqrestore(&tty->buf.lock, flags);
558+
559+
spin_lock_irqsave(&buf->lock, flags);
560+
if (buf->tail != NULL)
561+
buf->tail->commit = buf->tail->used;
562+
spin_unlock_irqrestore(&buf->lock, flags);
545563

546564
if (tty->low_latency)
547-
flush_to_ldisc(&tty->buf.work);
565+
flush_to_ldisc(&buf->work);
548566
else
549-
schedule_work(&tty->buf.work);
567+
schedule_work(&buf->work);
550568
}
551569
EXPORT_SYMBOL(tty_flip_buffer_push);
552570

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

563581
void tty_buffer_init(struct tty_struct *tty)
564582
{
565-
spin_lock_init(&tty->buf.lock);
566-
tty->buf.head = NULL;
567-
tty->buf.tail = NULL;
568-
tty->buf.free = NULL;
569-
tty->buf.memory_used = 0;
570-
INIT_WORK(&tty->buf.work, flush_to_ldisc);
583+
struct tty_bufhead *buf = &tty->buf;
584+
585+
spin_lock_init(&buf->lock);
586+
buf->head = NULL;
587+
buf->tail = NULL;
588+
buf->free = NULL;
589+
buf->memory_used = 0;
590+
INIT_WORK(&buf->work, flush_to_ldisc);
571591
}
572592

0 commit comments

Comments
 (0)