Skip to content

Commit ecbbfd4

Browse files
Jiri Slabygregkh
authored andcommitted
TTY: move tty buffers to tty_port
So this is it. The big step why we did all the work over the past kernel releases. Now everything is prepared, so nothing protects us from doing that big step. | | \ \ nnnn/^l | | | | \ / / | | | '-,.__ => \/ ,-` => | '-,.__ | O __.´´) ( .` | O __.´´) ~~~ ~~ `` ~~~ ~~ The buffers are now in the tty_port structure and we can start teaching the buffer helpers (insert char/string, flip etc.) to use tty_port instead of tty_struct all around. 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>
1 parent 967fab6 commit ecbbfd4

File tree

8 files changed

+70
-63
lines changed

8 files changed

+70
-63
lines changed

drivers/tty/n_tty.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,11 @@ static void n_tty_set_room(struct tty_struct *tty)
149149
tty->receive_room = left;
150150

151151
/* Did this open up the receive buffer? We may need to flip */
152-
if (left && !old_left)
153-
schedule_work(&tty->buf.work);
152+
if (left && !old_left) {
153+
WARN_RATELIMIT(tty->port->itty == NULL,
154+
"scheduling with invalid itty");
155+
schedule_work(&tty->port->buf.work);
156+
}
154157
}
155158

156159
static void put_tty_queue_nolock(unsigned char c, struct n_tty_data *ldata)

drivers/tty/pty.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static void pty_unthrottle(struct tty_struct *tty)
9393

9494
static int pty_space(struct tty_struct *to)
9595
{
96-
int n = 8192 - to->buf.memory_used;
96+
int n = 8192 - to->port->buf.memory_used;
9797
if (n < 0)
9898
return 0;
9999
return n;

drivers/tty/tty_buffer.c

Lines changed: 53 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
* Locking: none
2828
*/
2929

30-
void tty_buffer_free_all(struct tty_struct *tty)
30+
void tty_buffer_free_all(struct tty_port *port)
3131
{
32-
struct tty_bufhead *buf = &tty->buf;
32+
struct tty_bufhead *buf = &port->buf;
3333
struct tty_buffer *thead;
3434

3535
while ((thead = buf->head) != NULL) {
@@ -56,11 +56,11 @@ void tty_buffer_free_all(struct tty_struct *tty)
5656
* Locking: Caller must hold tty->buf.lock
5757
*/
5858

59-
static struct tty_buffer *tty_buffer_alloc(struct tty_struct *tty, size_t size)
59+
static struct tty_buffer *tty_buffer_alloc(struct tty_port *port, size_t size)
6060
{
6161
struct tty_buffer *p;
6262

63-
if (tty->buf.memory_used + size > 65536)
63+
if (port->buf.memory_used + size > 65536)
6464
return NULL;
6565
p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC);
6666
if (p == NULL)
@@ -72,7 +72,7 @@ static struct tty_buffer *tty_buffer_alloc(struct tty_struct *tty, size_t size)
7272
p->read = 0;
7373
p->char_buf_ptr = (char *)(p->data);
7474
p->flag_buf_ptr = (unsigned char *)p->char_buf_ptr + size;
75-
tty->buf.memory_used += size;
75+
port->buf.memory_used += size;
7676
return p;
7777
}
7878

@@ -87,9 +87,9 @@ static struct tty_buffer *tty_buffer_alloc(struct tty_struct *tty, size_t size)
8787
* Locking: Caller must hold tty->buf.lock
8888
*/
8989

90-
static void tty_buffer_free(struct tty_struct *tty, struct tty_buffer *b)
90+
static void tty_buffer_free(struct tty_port *port, struct tty_buffer *b)
9191
{
92-
struct tty_bufhead *buf = &tty->buf;
92+
struct tty_bufhead *buf = &port->buf;
9393

9494
/* Dumb strategy for now - should keep some stats */
9595
buf->memory_used -= b->size;
@@ -114,14 +114,14 @@ static void tty_buffer_free(struct tty_struct *tty, struct tty_buffer *b)
114114
* Locking: Caller must hold tty->buf.lock
115115
*/
116116

117-
static void __tty_buffer_flush(struct tty_struct *tty)
117+
static void __tty_buffer_flush(struct tty_port *port)
118118
{
119-
struct tty_bufhead *buf = &tty->buf;
119+
struct tty_bufhead *buf = &port->buf;
120120
struct tty_buffer *thead;
121121

122122
while ((thead = buf->head) != NULL) {
123123
buf->head = thead->next;
124-
tty_buffer_free(tty, thead);
124+
tty_buffer_free(port, thead);
125125
}
126126
buf->tail = NULL;
127127
}
@@ -140,7 +140,7 @@ static void __tty_buffer_flush(struct tty_struct *tty)
140140
void tty_buffer_flush(struct tty_struct *tty)
141141
{
142142
struct tty_port *port = tty->port;
143-
struct tty_bufhead *buf = &tty->buf;
143+
struct tty_bufhead *buf = &port->buf;
144144
unsigned long flags;
145145

146146
spin_lock_irqsave(&buf->lock, flags);
@@ -155,7 +155,7 @@ void tty_buffer_flush(struct tty_struct *tty)
155155
test_bit(TTYP_FLUSHPENDING, &port->iflags) == 0);
156156
return;
157157
} else
158-
__tty_buffer_flush(tty);
158+
__tty_buffer_flush(port);
159159
spin_unlock_irqrestore(&buf->lock, flags);
160160
}
161161

@@ -171,9 +171,9 @@ void tty_buffer_flush(struct tty_struct *tty)
171171
* Locking: Caller must hold tty->buf.lock
172172
*/
173173

174-
static struct tty_buffer *tty_buffer_find(struct tty_struct *tty, size_t size)
174+
static struct tty_buffer *tty_buffer_find(struct tty_port *port, size_t size)
175175
{
176-
struct tty_buffer **tbh = &tty->buf.free;
176+
struct tty_buffer **tbh = &port->buf.free;
177177
while ((*tbh) != NULL) {
178178
struct tty_buffer *t = *tbh;
179179
if (t->size >= size) {
@@ -182,14 +182,14 @@ static struct tty_buffer *tty_buffer_find(struct tty_struct *tty, size_t size)
182182
t->used = 0;
183183
t->commit = 0;
184184
t->read = 0;
185-
tty->buf.memory_used += t->size;
185+
port->buf.memory_used += t->size;
186186
return t;
187187
}
188188
tbh = &((*tbh)->next);
189189
}
190190
/* Round the buffer size out */
191191
size = (size + 0xFF) & ~0xFF;
192-
return tty_buffer_alloc(tty, size);
192+
return tty_buffer_alloc(port, size);
193193
/* Should possibly check if this fails for the largest buffer we
194194
have queued and recycle that ? */
195195
}
@@ -200,11 +200,11 @@ static struct tty_buffer *tty_buffer_find(struct tty_struct *tty, size_t size)
200200
*
201201
* Make at least size bytes of linear space available for the tty
202202
* buffer. If we fail return the size we managed to find.
203-
* Locking: Caller must hold tty->buf.lock
203+
* Locking: Caller must hold port->buf.lock
204204
*/
205-
static int __tty_buffer_request_room(struct tty_struct *tty, size_t size)
205+
static int __tty_buffer_request_room(struct tty_port *port, size_t size)
206206
{
207-
struct tty_bufhead *buf = &tty->buf;
207+
struct tty_bufhead *buf = &port->buf;
208208
struct tty_buffer *b, *n;
209209
int left;
210210
/* OPTIMISATION: We could keep a per tty "zero" sized buffer to
@@ -218,7 +218,7 @@ static int __tty_buffer_request_room(struct tty_struct *tty, size_t size)
218218

219219
if (left < size) {
220220
/* This is the slow path - looking for new buffers to use */
221-
if ((n = tty_buffer_find(tty, size)) != NULL) {
221+
if ((n = tty_buffer_find(port, size)) != NULL) {
222222
if (b != NULL) {
223223
b->next = n;
224224
b->commit = b->used;
@@ -241,16 +241,17 @@ static int __tty_buffer_request_room(struct tty_struct *tty, size_t size)
241241
* Make at least size bytes of linear space available for the tty
242242
* buffer. If we fail return the size we managed to find.
243243
*
244-
* Locking: Takes tty->buf.lock
244+
* Locking: Takes port->buf.lock
245245
*/
246246
int tty_buffer_request_room(struct tty_struct *tty, size_t size)
247247
{
248+
struct tty_port *port = tty->port;
248249
unsigned long flags;
249250
int length;
250251

251-
spin_lock_irqsave(&tty->buf.lock, flags);
252-
length = __tty_buffer_request_room(tty, size);
253-
spin_unlock_irqrestore(&tty->buf.lock, flags);
252+
spin_lock_irqsave(&port->buf.lock, flags);
253+
length = __tty_buffer_request_room(port, size);
254+
spin_unlock_irqrestore(&port->buf.lock, flags);
254255
return length;
255256
}
256257
EXPORT_SYMBOL_GPL(tty_buffer_request_room);
@@ -265,13 +266,13 @@ EXPORT_SYMBOL_GPL(tty_buffer_request_room);
265266
* Queue a series of bytes to the tty buffering. All the characters
266267
* passed are marked with the supplied flag. Returns the number added.
267268
*
268-
* Locking: Called functions may take tty->buf.lock
269+
* Locking: Called functions may take port->buf.lock
269270
*/
270271

271272
int tty_insert_flip_string_fixed_flag(struct tty_struct *tty,
272273
const unsigned char *chars, char flag, size_t size)
273274
{
274-
struct tty_bufhead *buf = &tty->buf;
275+
struct tty_bufhead *buf = &tty->port->buf;
275276
int copied = 0;
276277
do {
277278
int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
@@ -280,7 +281,7 @@ int tty_insert_flip_string_fixed_flag(struct tty_struct *tty,
280281
struct tty_buffer *tb;
281282

282283
spin_lock_irqsave(&buf->lock, flags);
283-
space = __tty_buffer_request_room(tty, goal);
284+
space = __tty_buffer_request_room(tty->port, goal);
284285
tb = buf->tail;
285286
/* If there is no space then tb may be NULL */
286287
if (unlikely(space == 0)) {
@@ -311,13 +312,13 @@ EXPORT_SYMBOL(tty_insert_flip_string_fixed_flag);
311312
* the flags array indicates the status of the character. Returns the
312313
* number added.
313314
*
314-
* Locking: Called functions may take tty->buf.lock
315+
* Locking: Called functions may take port->buf.lock
315316
*/
316317

317318
int tty_insert_flip_string_flags(struct tty_struct *tty,
318319
const unsigned char *chars, const char *flags, size_t size)
319320
{
320-
struct tty_bufhead *buf = &tty->buf;
321+
struct tty_bufhead *buf = &tty->port->buf;
321322
int copied = 0;
322323
do {
323324
int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
@@ -326,7 +327,7 @@ int tty_insert_flip_string_flags(struct tty_struct *tty,
326327
struct tty_buffer *tb;
327328

328329
spin_lock_irqsave(&buf->lock, __flags);
329-
space = __tty_buffer_request_room(tty, goal);
330+
space = __tty_buffer_request_room(tty->port, goal);
330331
tb = buf->tail;
331332
/* If there is no space then tb may be NULL */
332333
if (unlikely(space == 0)) {
@@ -357,12 +358,12 @@ EXPORT_SYMBOL(tty_insert_flip_string_flags);
357358
* Note that this function can only be used when the low_latency flag
358359
* is unset. Otherwise the workqueue won't be flushed.
359360
*
360-
* Locking: Takes tty->buf.lock
361+
* Locking: Takes port->buf.lock
361362
*/
362363

363364
void tty_schedule_flip(struct tty_struct *tty)
364365
{
365-
struct tty_bufhead *buf = &tty->buf;
366+
struct tty_bufhead *buf = &tty->port->buf;
366367
unsigned long flags;
367368

368369
spin_lock_irqsave(&buf->lock, flags);
@@ -385,19 +386,19 @@ EXPORT_SYMBOL(tty_schedule_flip);
385386
* that need their own block copy routines into the buffer. There is no
386387
* guarantee the buffer is a DMA target!
387388
*
388-
* Locking: May call functions taking tty->buf.lock
389+
* Locking: May call functions taking port->buf.lock
389390
*/
390391

391392
int tty_prepare_flip_string(struct tty_struct *tty, unsigned char **chars,
392-
size_t size)
393+
size_t size)
393394
{
394-
struct tty_bufhead *buf = &tty->buf;
395+
struct tty_bufhead *buf = &tty->port->buf;
395396
int space;
396397
unsigned long flags;
397398
struct tty_buffer *tb;
398399

399400
spin_lock_irqsave(&buf->lock, flags);
400-
space = __tty_buffer_request_room(tty, size);
401+
space = __tty_buffer_request_room(tty->port, size);
401402

402403
tb = buf->tail;
403404
if (likely(space)) {
@@ -423,19 +424,19 @@ EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
423424
* that need their own block copy routines into the buffer. There is no
424425
* guarantee the buffer is a DMA target!
425426
*
426-
* Locking: May call functions taking tty->buf.lock
427+
* Locking: May call functions taking port->buf.lock
427428
*/
428429

429430
int tty_prepare_flip_string_flags(struct tty_struct *tty,
430431
unsigned char **chars, char **flags, size_t size)
431432
{
432-
struct tty_bufhead *buf = &tty->buf;
433+
struct tty_bufhead *buf = &tty->port->buf;
433434
int space;
434435
unsigned long __flags;
435436
struct tty_buffer *tb;
436437

437438
spin_lock_irqsave(&buf->lock, __flags);
438-
space = __tty_buffer_request_room(tty, size);
439+
space = __tty_buffer_request_room(tty->port, size);
439440

440441
tb = buf->tail;
441442
if (likely(space)) {
@@ -464,13 +465,16 @@ EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
464465

465466
static void flush_to_ldisc(struct work_struct *work)
466467
{
467-
struct tty_struct *tty =
468-
container_of(work, struct tty_struct, buf.work);
469-
struct tty_port *port = tty->port;
470-
struct tty_bufhead *buf = &tty->buf;
468+
struct tty_port *port = container_of(work, struct tty_port, buf.work);
469+
struct tty_bufhead *buf = &port->buf;
470+
struct tty_struct *tty;
471471
unsigned long flags;
472472
struct tty_ldisc *disc;
473473

474+
tty = port->itty;
475+
if (WARN_RATELIMIT(tty == NULL, "tty is NULL"))
476+
return;
477+
474478
disc = tty_ldisc_ref(tty);
475479
if (disc == NULL) /* !TTY_LDISC */
476480
return;
@@ -489,7 +493,7 @@ static void flush_to_ldisc(struct work_struct *work)
489493
if (head->next == NULL)
490494
break;
491495
buf->head = head->next;
492-
tty_buffer_free(tty, head);
496+
tty_buffer_free(port, head);
493497
continue;
494498
}
495499
/* Ldisc or user is trying to flush the buffers
@@ -515,7 +519,7 @@ static void flush_to_ldisc(struct work_struct *work)
515519
/* We may have a deferred request to flush the input buffer,
516520
if so pull the chain under the lock and empty the queue */
517521
if (test_bit(TTYP_FLUSHPENDING, &port->iflags)) {
518-
__tty_buffer_flush(tty);
522+
__tty_buffer_flush(port);
519523
clear_bit(TTYP_FLUSHPENDING, &port->iflags);
520524
wake_up(&tty->read_wait);
521525
}
@@ -535,7 +539,7 @@ static void flush_to_ldisc(struct work_struct *work)
535539
void tty_flush_to_ldisc(struct tty_struct *tty)
536540
{
537541
if (!tty->low_latency)
538-
flush_work(&tty->buf.work);
542+
flush_work(&tty->port->buf.work);
539543
}
540544

541545
/**
@@ -553,7 +557,7 @@ void tty_flush_to_ldisc(struct tty_struct *tty)
553557

554558
void tty_flip_buffer_push(struct tty_struct *tty)
555559
{
556-
struct tty_bufhead *buf = &tty->buf;
560+
struct tty_bufhead *buf = &tty->port->buf;
557561
unsigned long flags;
558562

559563
spin_lock_irqsave(&buf->lock, flags);
@@ -578,9 +582,9 @@ EXPORT_SYMBOL(tty_flip_buffer_push);
578582
* Locking: none
579583
*/
580584

581-
void tty_buffer_init(struct tty_struct *tty)
585+
void tty_buffer_init(struct tty_port *port)
582586
{
583-
struct tty_bufhead *buf = &tty->buf;
587+
struct tty_bufhead *buf = &port->buf;
584588

585589
spin_lock_init(&buf->lock);
586590
buf->head = NULL;

drivers/tty/tty_io.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ void free_tty_struct(struct tty_struct *tty)
186186
if (tty->dev)
187187
put_device(tty->dev);
188188
kfree(tty->write_buf);
189-
tty_buffer_free_all(tty);
190189
tty->magic = 0xDEADDEAD;
191190
kfree(tty);
192191
}
@@ -2935,7 +2934,6 @@ void initialize_tty_struct(struct tty_struct *tty,
29352934
tty_ldisc_init(tty);
29362935
tty->session = NULL;
29372936
tty->pgrp = NULL;
2938-
tty_buffer_init(tty);
29392937
mutex_init(&tty->legacy_mutex);
29402938
mutex_init(&tty->termios_mutex);
29412939
mutex_init(&tty->ldisc_mutex);

0 commit comments

Comments
 (0)