Skip to contentSkip to conversation

2.4.0, 2.2.18: A critical problem with tty_io.c

PATCH Al Borchers 3 participants 10 messages May 17, 2001 – May 18, 2001

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

Maciej, Ted --

Maciej's patch to tty_io.c from lkml 2/22/01

http://www.uwsg.indiana.edu/hypermail/linux/kernel/0101.2/1049.html

has been incorporated in RH 7.1's kernel (for example) but not in the main 2.4.x kernels.

This presents two different interfaces for serial drivers, and it is awkward to detect and support both interfaces. The change breaks some drivers from Digi International, for example.

In Maciej's patch, if a driver open is interrupted the driver close IS NOT called. In the 2.4.x series the close IS called.

A serial driver needs to know whether close is going to be called or not after an interrupted open, so it can do clean up--like DEC_MOD_USE_COUNT-- appropriately.

Suppose the driver open calls INC_MOD_USE_COUNT, sleeps for the port to get carrier, then gets interrupted. If the open calls DEC_MOD_USE_COUNT after the interrupt, the use count will be right under Maciej's patch but too low on 2.4.x. If the open does not call DEC_MOD_USE_COUNT after an interrupt, instead deferring that for the close, then the use count will be too high under Maciej's patch, but right under 2.4.x. This is particularly a problem if there is another outstanding open on the port--in that case you can end up with zero use count (and a closed port!) while the port is open or a non-zero use count when no one has the port open.

Personally, I think Maciej's patch is correct and interrupted opens should clean up after themselves--however, this is a different interface than presented by the tty subsystem up to now, and will require changes in some serial drivers.

Ted can you get this patch in the kernel or ban it as interface breaking heresy? It would be much nicer for us device driver writers to have just one interface to support.

Thanks,
-- Al Borchers, Peter Berger

PS. James Puzzo at Digi first pointed out this problem to us.

#1 lore raw
Quoted 3 lines

Ted can you get this patch in the kernel or ban it as interface breaking heresy? It would be much nicer for us device driver writers to have just one interface to support.

It has to be changed, the race is basically unfixable any other way. I didn't lightly make that change

#2 lore raw
Quoted 2 lines · Alan Cox

It has to be changed, the race is basically unfixable any other way. I didn't lightly make that change

I agree. The patch seems like the correct solution. What will it take to get the patch in the 2.4.x kernels? Do we need someone to go through the serial drivers and fix their open/close routines to work with this patch? Peter and I can take some time to do that--if that would help.

-- Al

#3 lore raw
Quoted 8 lines
Quoted 2 lines · Alan Cox

It has to be changed, the race is basically unfixable any other way. I didn't lightly make that change

I agree. The patch seems like the correct solution. What will it take to get the patch in the 2.4.x kernels? Do we need someone to go through the serial drivers and fix their open/close routines to work with this patch? Peter and I can take some time to do that--if that would help.

That would be one big help. Having done that I'd like to go over it all with Ted first (if he has time) before I push it to Linus

#4 lore raw
Quoted 5 lines
Quoted 2 lines

drivers and fix their open/close routines to work with this patch? Peter and I can take some time to do that--if that would help.

That would be one big help. Having done that I'd like to go over it all with Ted first (if he has time) before I push it to Linus

So I stuck my justify this change to Ted hat on. And failed.

For one the cleanest way to handle all the locking is to propogate the other locking fix styles into both the ldisc and serial drivers. At least for 2.4 until the 2.5 folks get their deep magic inactivity based clean up working

The advantage of doing that is that modules that do play with use counts will not do anything worse than they do now, and will remain fully compatible.

The ldisc race is also real and completely unfixed right now.

Alan

#5 lore raw
Quoted 4 lines · Alan Cox

So I stuck my justify this change to Ted hat on. And failed.

For one the cleanest way to handle all the locking is to propogate the other locking fix styles into both the ldisc and serial drivers. At least for 2.4

If I understand you right, the plan is to leave tty_open unchanged (it calls driver close on failed driver open) and try to fix ldisc and serial driver races as much as possible. Correct?

Where is an example of the "other locking fix styles" that you and Ted want to apply to the serial drivers?

Quoted 2 lines

The advantage of doing that is that modules that do play with use counts will not do anything worse than they do now, and will remain fully compatible.

Leaving tty_open unchanged does have compatibility going for it.

Quoted 1 line

The ldisc race is also real and completely unfixed right now.

I would be interested to try to figure this out and fix it--can you give me more of an idea of what the problem is?

-- Al

#6 lore raw
Quoted 4 lines

Where is an example of the "other locking fix styles" that you and Ted want to apply to the serial drivers?
I would be interested to try to figure this out and fix it--can you give me more of an idea of what the problem is?

Add an 'owner' field to the objects we are using. Then we can lock the tty and the ldisc from the tyy_io code rather than in serial.c and friends. This removes the unload during open/close races we currently have in serial.c

Take a look at videodev.c for a fairly clear example.

Alan

#7 lore raw
Quoted 5 lines · Alan Cox

Add an 'owner' field to the objects we are using. Then we can lock the tty and the ldisc from the tyy_io code rather than in serial.c and friends. This removes the unload during open/close races we currently have in serial.c

Take a look at videodev.c for a fairly clear example.

Quoted 3 lines · Andrew Morton

I have a work-in-non-progress here which addresses a few of these things. It would be good if someone could review it and finish it off...

I will take a look.

-- Al

#8 lore raw
In reply to Alan Cox
Quoted 9 lines · Alan Cox
Quoted 6 lines
Quoted 3 lines

drivers and fix their open/close routines to work with this patch? Peter and I can take some time to do that--if that would help.

That would be one big help. Having done that I'd like to go over it all with Ted first (if he has time) before I push it to Linus

So I stuck my justify this change to Ted hat on. And failed.

Quoted 9 lines

For one the cleanest way to handle all the locking is to propogate the other locking fix styles into both the ldisc and serial drivers. At least for 2.4 until the 2.5 folks get their deep magic inactivity based clean up working

The advantage of doing that is that modules that do play with use counts will not do anything worse than they do now, and will remain fully compatible.

The ldisc race is also real and completely unfixed right now.

I have a work-in-non-progress here which addresses a few of these things. It would be good if someone could review it and finish it off...

- implements tty->ldisc_sem to plug race between do_tty_hangup() and tty_set_ldisc(). Is this the ldisc race to which you refer?

- implements the `struct module *owner' in tty_driver and tty_ldisc. Manipulates this in all the right places.

- Actually *uses* ->owner in ppp_async.c, n_r3964.c and serial.c Other tty and ldisc drivers need to be changed to set ->owner.

There's a fight between n_hdlc.c and n_r3964.c which hasn't been fixed yet. If you attach r3964 to a tty and then detach it, it leaves a non-zero value in tty->disc_data. This then prevents you from being able to attach n_hdlc to the tty, because it checks for null ->disc_data. Best fix for this is to make sure all the ldisc close routines zero out disc_data when they're finished.

include/linux/tty_driver.h +11 -0
@@ -117,6 +117,14 @@
 
 #include <linux/fs.h>
 
+#ifdef CONFIG_MODULES
+#include <linux/module.h>
+#define SET_TTY_OWNER(driver)	\
+	do { (driver)->owner = THIS_MODULE; } while (0)
+#define SET_LDISC_OWNER(ldisc)	\
+	do { (ldisc)->owner = THIS_MODULE; } while (0)
+#endif
+
 struct tty_driver {
 	int	magic;		/* magic number for this structure */
 	const char	*driver_name;
@@ -176,6 +184,9 @@
 	 */
 	struct tty_driver *next;
 	struct tty_driver *prev;
+#ifdef CONFIG_MODULES
+	struct module *owner;
+#endif
 };
 
 /* tty driver magic number */
include/linux/tty_ldisc.h +5 -0
@@ -100,6 +100,8 @@
 #include <linux/fs.h>
 #include <linux/wait.h>
 
+struct module;
+
 struct tty_ldisc {
 	int	magic;
 	char	*name;
@@ -129,6 +131,9 @@
 			       char *fp, int count);
 	int	(*receive_room)(struct tty_struct *);
 	void	(*write_wakeup)(struct tty_struct *);
+#ifdef CONFIG_MODULES
+	struct module *owner;
+#endif
 };
 
 #define TTY_LDISC_MAGIC	0x5403
include/linux/tty.h +1 -0
@@ -306,6 +306,7 @@
 	unsigned int canon_column;
 	struct semaphore atomic_read;
 	struct semaphore atomic_write;
+	struct semaphore ldisc_sem;	/* Lock this while we're manipulating ldisc */
 	spinlock_t read_lock;
 	/* If the tty has a pending do_SAK, queue it here - akpm */
 	struct tq_struct SAK_tq;
drivers/char/tty_io.c +110 -24

271 lines not on this page · raw message

drivers/char/serial.c +28 -14

123 lines not on this page · raw message

drivers/net/ppp_async.c +3 -3
@@ -113,7 +113,6 @@
 	struct asyncppp *ap;
 	int err;
 
-	MOD_INC_USE_COUNT;
 	err = -ENOMEM;
 	ap = kmalloc(sizeof(*ap), GFP_KERNEL);
 	if (ap == 0)
@@ -146,7 +145,6 @@
  out_free:
 	kfree(ap);
  out:
-	MOD_DEC_USE_COUNT;
 	return err;
 }
 
@@ -171,7 +169,6 @@
 	if (ap->tpkt != 0)
 		kfree_skb(ap->tpkt);
 	kfree(ap);
-	MOD_DEC_USE_COUNT;
 }
 
 /*
@@ -310,6 +307,9 @@
 	receive_room: ppp_asynctty_room,
 	receive_buf: ppp_asynctty_receive,
 	write_wakeup: ppp_asynctty_wakeup,
+#ifdef CONFIG_MODULES
+	owner: THIS_MODULE,
+#endif
 };
 
 static int __init
drivers/char/n_hdlc.c +3 -3
@@ -305,7 +305,6 @@
 			n_hdlc->tty = n_hdlc->backup_tty;
 		} else {
 			n_hdlc_release (n_hdlc);
-			MOD_DEC_USE_COUNT;
 		}
 	}
 	
@@ -345,8 +344,6 @@
 	tty->disc_data = n_hdlc;
 	n_hdlc->tty    = tty;
 	
-	MOD_INC_USE_COUNT;
-	
 #if defined(TTY_NO_WRITE_SPLIT)
 	/* change tty_io write() to not split large writes into 8K chunks */
 	set_bit(TTY_NO_WRITE_SPLIT,&tty->flags);
@@ -1006,6 +1003,9 @@
 	n_hdlc_ldisc.receive_room	= n_hdlc_tty_room;
 	n_hdlc_ldisc.receive_buf	= n_hdlc_tty_receive;
 	n_hdlc_ldisc.write_wakeup	= n_hdlc_tty_wakeup;
+#ifdef CONFIG_MODULES
+	n_hdlc_ldisc.owner		= THIS_MODULE;
+#endif
 
 	status = tty_register_ldisc(N_HDLC, &n_hdlc_ldisc);
 	if (!status)
drivers/char/n_r3964.c +14 -20
@@ -146,22 +146,20 @@
 static int  r3964_receive_room(struct tty_struct *tty);
 
 static struct tty_ldisc tty_ldisc_N_R3964 = {
-        TTY_LDISC_MAGIC,       /* magic */
-	"R3964",               /* name */
-        0,                     /* num */
-        0,                     /* flags */
-        r3964_open,            /* open */
-        r3964_close,           /* close */
-        0,                     /* flush_buffer */
-        0,                     /* chars_in_buffer */
-        r3964_read,            /* read */
-        r3964_write,           /* write */
-        r3964_ioctl,           /* ioctl */
-        r3964_set_termios,     /* set_termios */
-        r3964_poll,            /* poll */            
-        r3964_receive_buf,     /* receive_buf */
-        r3964_receive_room,    /* receive_room */
-        0                      /* write_wakeup */
+        magic:		TTY_LDISC_MAGIC,
+	name:		"R3964",
+        open:		r3964_open,
+        close:		r3964_close,
+        read:		r3964_read,
+        write:		r3964_write,
+        ioctl:		r3964_ioctl,
+        set_termios:	r3964_set_termios,
+        poll:		r3964_poll,
+        receive_buf:	r3964_receive_buf,
+        receive_room:	r3964_receive_room,
+#ifdef CONFIG_MODULES
+	owner:		THIS_MODULE,
+#endif
 };
 
 
@@ -1098,8 +1096,6 @@
 {
    struct r3964_info *pInfo;
    
-   MOD_INC_USE_COUNT;
-
    TRACE_L("open");
    TRACE_L("tty=%x, PID=%d, disc_data=%x", 
           (int)tty, current->pid, (int)tty->disc_data);
@@ -1234,8 +1230,6 @@
     TRACE_M("r3964_close - tx_buf kfree %x",(int)pInfo->tx_buf);
     kfree(pInfo);
     TRACE_M("r3964_close - info kfree %x",(int)pInfo);
-
-    MOD_DEC_USE_COUNT;
 }
 
 static int r3964_read(struct tty_struct *tty, struct file *file,
#9 lore raw
Quoted 2 lines

- implements tty->ldisc_sem to plug race between do_tty_hangup() and tty_set_ldisc(). Is this the ldisc race to which you refer?

Actually I'd missed that one. I was referring to the module race on the ldisc

include/linux/tty_driver.h line 189

Quoted 3 lines
include/linux/tty_driver.h +3 -0
+#ifdef CONFIG_MODULES
+	struct module *owner;
+#endif

I'd rather the field was always there. THIS_MODULE is correctly NULL for non modules. Doing that makes all the ifdefs vanish and probably makes it a lot more likely to pass Linus