Skip to contentSkip to conversation

missing NULL check in drivers/char/n_tty.c

PATCH In review Dan Aloni 3 participants 10 messages Jun 21, 2004 Char and misc drivers

lore
Share & tools
lore

Dan Aloni

author
#0 lore raw

PATCH 1/1 missing NULL check in drivers/char/n_tty.c

Hello,

The rest of the kernel treats tty->driver->chars_in_buffer as a possible NULL. This patch changes normal_poll() to be consistent with the rest of the code.

BTW, who is currently maintaining the tty subsystem?

drivers/char/n_tty.c +2 -2
@@ -1272,8 +1272,8 @@
 		else
 			tty->minimum_to_wake = 1;
 	}
-	if (tty->driver->chars_in_buffer(tty) < WAKEUP_CHARS &&
-			tty->driver->write_room(tty) > 0)
+	if ((!tty->driver->chars_in_buffer || tty->driver->chars_in_buffer(tty) < WAKEUP_CHARS)
+	   && tty->driver->write_room(tty) > 0)
 		mask |= POLLOUT | POLLWRNORM;
 	return mask;
 }
Signature · 2 lines
Dan Aloni
[email protected]
#1 lore raw
Quoted 4 lines · Dan Aloni

The rest of the kernel treats tty->driver->chars_in_buffer as a possible
NULL. This patch changes normal_poll() to be consistent with the rest of the code.

It would be better to change the rest of the kernel - remove the tests.

If any driver fails to implement ->chars_in_buffer() then we get a nice oops which tells us that driver needs a stub handler.

author
#2 lore raw
Quoted 10 lines · Andrew Morton
Quoted 4 lines · Dan Aloni

The rest of the kernel treats tty->driver->chars_in_buffer as a possible
NULL. This patch changes normal_poll() to be consistent with the rest of the code.

It would be better to change the rest of the kernel - remove the tests.

If any driver fails to implement ->chars_in_buffer() then we get a nice oops which tells us that driver needs a stub handler.

Are you sure that it won't affect the logic in tty_wait_until_sent() drastically? It acts quite differently when ->chars_in_buffer == NULL.

Signature · 2 lines
Dan Aloni
[email protected]
#3 lore raw
Quoted 8 lines · Dan Aloni
Quoted 6 lines · Andrew Morton
Quoted 4 lines · Dan Aloni

The rest of the kernel treats tty->driver->chars_in_buffer as a possible
NULL. This patch changes normal_poll() to be consistent with the rest of the code.

Quoted 7 lines
Quoted 4 lines

It would be better to change the rest of the kernel - remove the tests.

If any driver fails to implement ->chars_in_buffer() then we get a nice oops which tells us that driver needs a stub handler.

Are you sure that it won't affect the logic in tty_wait_until_sent() drastically? It acts quite differently when ->chars_in_buffer == NULL.

I did a quick grep and it appears that all drivers have set ->chars_in_buffer().

I suspect there are no drivers which fail to set chars_in_buffer. Otherwise normal_poll() would have been oopsing in 2.4, 2.5 and 2.6?

author
#4 lore raw
Quoted 14 lines · Andrew Morton
Quoted 13 lines · Dan Aloni
Quoted 10 lines · Andrew Morton
Quoted 4 lines · Dan Aloni

The rest of the kernel treats tty->driver->chars_in_buffer as a possible
NULL. This patch changes normal_poll() to be consistent with the rest of the code.

It would be better to change the rest of the kernel - remove the tests.

If any driver fails to implement ->chars_in_buffer() then we get a nice oops which tells us that driver needs a stub handler.

Quoted 7 lines
Quoted 2 lines

Are you sure that it won't affect the logic in tty_wait_until_sent() drastically? It acts quite differently when ->chars_in_buffer == NULL.

I did a quick grep and it appears that all drivers have set ->chars_in_buffer().

I suspect there are no drivers which fail to set chars_in_buffer. Otherwise normal_poll() would have been oopsing in 2.4, 2.5 and 2.6?

Right. Perhaps this should be applied:

drivers/char/n_hdlc.c +1 -2
@@ -760,8 +760,7 @@
 
 	case TIOCOUTQ:
 		/* get the pending tx byte count in the driver */
-		count = tty->driver->chars_in_buffer ?
-				tty->driver->chars_in_buffer(tty) : 0;
+		count = tty->driver->chars_in_buffer(tty);
 		/* add size of next output frame in queue */
 		spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock,flags);
 		if (n_hdlc->tx_buf_list.head)
drivers/char/tty_ioctl.c +1 -4
@@ -45,8 +45,6 @@
 	
 	printk(KERN_DEBUG "%s wait until sent...\n", tty_name(tty, buf));
 #endif
-	if (!tty->driver->chars_in_buffer)
-		return;
 	add_wait_queue(&tty->write_wait, &wait);
 	if (!timeout)
 		timeout = MAX_SCHEDULE_TIMEOUT;
@@ -461,8 +459,7 @@
 			}
 			return 0;
 		case TIOCOUTQ:
-			return put_user(tty->driver->chars_in_buffer ?
-					tty->driver->chars_in_buffer(tty) : 0,
+			return put_user(tty->driver->chars_in_buffer(tty),
 					(int __user *) arg);
 		case TIOCINQ:
 			retval = tty->read_cnt;
Signature · 2 lines
Dan Aloni
[email protected]
#5 lore raw
Quoted 4 lines · Dan Aloni

Andrew Morton wrote:
I did a quick grep and it appears that all drivers have set ->chars_in_buffer().

I suspect there are no drivers which fail to set chars_in_buffer. Otherwise normal_poll() would have been oopsing in 2.4, 2.5 and 2.6?

An addition should be made to include/linux/tty_driver.h to document the chars_in_buffer member of struct tty_driver and struct tty_operations as a required function. Currently, the documentation section of this header does not mention chars_in_buffer.

Related issue:

In looking at this, I noticed struct tty_ldisc (include/linux/tty_ldisc.h) defines and documents an optional (optional == NULL) member chars_in_buffer. N_TTY (drivers/char/n_tty.c) is the only line discipline that implements this member.

drivers/char/pty.c is the only driver that
uses ldisc.chars_in_buffer, and it checks for
ldisc.chars_in_buffer == NULL before calling.

13 other drivers call ldisc.chars_in_buffer without checking for ldisc.chars_in_buffer == NULL, but only inside conditional compilation for debug output. The value is not used, only logged. These conditional debug items look like cut and paste from one serial driver to another, and I doubt
they have been recently used (or used at all).

Which would be better?
1. Ignore this
2. Fix conditional debug output to check
for ldisc.chars_in_buffer==NULL
3. Remove conditional debug output

--
Paul Fulghum
[email protected]

#6 lore raw
Quoted 8 lines · Paul Fulghum

13 other drivers call ldisc.chars_in_buffer without checking
for ldisc.chars_in_buffer == NULL, but only inside conditional compilation for debug output. The value is not used, only logged. These conditional debug items look like cut and paste from one serial driver to another, and I doubt
they have been recently used (or used at all).

Quoted 5 lines

Which would be better?
1. Ignore this
2. Fix conditional debug output to check

     for ldisc.chars_in_buffer==NULL

3. Remove conditional debug output

Option 1 is quite valid. There are no bugs here, yes?

If someone for some reason wants to clean all this up, the best way would be to require that ->chars_in_buffer always be valid, hence remove all those checks.

#7 lore raw
Quoted 8 lines · Andrew Morton
Quoted 5 lines · Paul Fulghum

Which would be better?
1. Ignore this
2. Fix conditional debug output to check

    for ldisc.chars_in_buffer==NULL

3. Remove conditional debug output

Option 1 is quite valid. There are no bugs here, yes?

If the debug output is enabled and
a line discipline other than N_TTY is used, then you get an oops when the NULL method is called.

Since the debug output is not enabled by default, and is probably never really used, it is not a significant bug.

I thought it might be worth eliminating or correcting the debug outputs since they seem to get cloned into new serial drivers. It is certainly not a big problem.

Quoted 3 lines

If someone for some reason wants to clean all this up, the best way would be to require that ->chars_in_buffer always be valid, hence remove all those checks.

OK

--
Paul Fulghum
[email protected]

author
#8 lore raw
In reply to Andrew Morton
Quoted 15 lines · Andrew Morton
Quoted 8 lines · Paul Fulghum

13 other drivers call ldisc.chars_in_buffer without checking
for ldisc.chars_in_buffer == NULL, but only inside conditional compilation for debug output. The value is not used, only logged. These conditional debug items look like cut and paste from one serial driver to another, and I doubt
they have been recently used (or used at all).

Quoted 5 lines

Which would be better?
1. Ignore this
2. Fix conditional debug output to check

     for ldisc.chars_in_buffer==NULL

3. Remove conditional debug output

Quoted 5 lines

Option 1 is quite valid. There are no bugs here, yes?

If someone for some reason wants to clean all this up, the best way would be to require that ->chars_in_buffer always be valid, hence remove all those checks.

Apropos cleanups in the tty subsystem, what is the purpose of all the *_paranoia_check() calls that almost every driver duplicates for itself? Perhaps this can be removed.

Signature · 2 lines
Dan Aloni
[email protected]
#9 lore raw
Quoted 2 lines · Dan Aloni

Apropos cleanups in the tty subsystem, what is the purpose of all the *_paranoia_check() calls that almost every driver duplicates for itself? Perhaps this can be removed.

At some time in the distant past, it must have been a brute force attempt to debug some *serious* problems. I've never had the need to enable those checks in the last 6 years.

I agree with your thought, and will be removing them from at least the synclinkxx drivers. The checks clutter the code with little gain. And cruft like that keeps propogating to new drivers.

--
Paul Fulghum
[email protected]