Patch: Fix serial module use count (2.4.16 _and_ 2.5)
Quoted 4 lines
Err,
close(-ENOMEM);What's that going to close? Hint: you _can't_ close
a descriptor that
Quoted 1 line
failed to open, since you don't have a descriptor to
close. You can
Quoted 1 line
only try to close an error code, but that's not going
to make it anywhere
Quoted 1 line
near the kernel driver level.
Let me make it clearer to you,
lets say I call rs_open() on /dev/ttyS0 and if it fails then I should not call rs_close() after a failed rs_open().
I hope this is clear now.
Quoted 1 line
The same thing applies to the code below. I think
that the open routine
Quoted 1 line
should instead set
tty->driver_datato NULL upon
failure.
Quoted 15 lines
Here's an example why that'd be real bad:
1. process A opens
/dev/ttyS0as a normal device. This initialises
tty->driver_data.
2. process B tries to open/dev/cua0
3. process B fails with-EBUSYsince the normal device is open and active
(seeblock_til_ready)
4. sincers_openfailed, we settty->driver_datato be NULL (note that this
is the same tty device pointer as (1) above.
5. process A writes to/dev/ttyS0
6.rs_writedoes the following:
Quoted 1 line
struct
async_struct*info = (struct
async_struct *)tty->driver_data;
Quoted 1 line
7. Oops.
Lets see what happens with your approach
1. I call rs_open(), it fails, ref_count set to 1
2. I am sane enough not to call rs_close() on the device which failed to open with rs_open the first time, count is set to 1, driver never unloads.
I do not have access to block_til_ready currently. But, I will see that function and revert with more comments.
Comments,
Balbir Singh.
--
Russell King ([email protected]) The
developer of ARM Linux
http://www.arm.linux.org.uk/personal/aboutme.html
__________________________________________________
Do You Yahoo!?
Yahoo! GeoCities - quick and easy web site hosting, just $8.95/month.
http://geocities.yahoo.com/ps/info1
Quoted 5 lines · Balbir Singh
Let me make it clearer to you,
lets say I call
rs_open()on/dev/ttyS0and if it fails then I should not callrs_close()after a failedrs_open().
You don't call rs_open. The tty layer does that for you. The tty layer also cleans up on close by calling the driver specific close function.
Yes I agree with you that it might not, but that is a 2.5 kernel issue, not a 2.4 "lets do a massive change" issue. The tty layer is complex and messy, and we shouldn't go around randomly changing it in 2.4.
Quoted 3 lines
Lets see what happens with your approach
1. I call
rs_open(), it fails,ref_countset to 1
Ok, so you're poking around in kernel code calling kernel functions that were previously declared static and not visible to anything but the tty layer. That immediately makes your example invalid because you're not following the rules that the tty layers lays down for opening tty devices.
--
Russell King ([email protected]) The developer of ARM Linux
http://www.arm.linux.org.uk/personal/aboutme.html
Quoted 11 lines · Balbir Singh
Quoted 4 lines
Err,
close(-ENOMEM);What's that going to close? Hint: you _can't_ close
a descriptor that
Quoted 1 line
failed to open, since you don't have a descriptor to
close. You can
Quoted 1 line
only try to close an error code, but that's not going
to make it anywhere
Quoted 1 line
near the kernel driver level.
Quoted 7 lines
Let me make it clearer to you,
lets say I call
rs_open()on/dev/ttyS0and if it fails then I should not callrs_close()after a failedrs_open().I hope this is clear now.
All of the other UNIX variants I've dealth with behave that way. However, you cannot just make that change without having some means of identifying that behavior change because all of the linux serial drivers have been written to assume that their close() will be called even after their open() has failed.
I'm not opposed to such a change in behavior, but at least be sure that it's somehow identifiable (kernel version, a define set in a header, etc) so that the 3rd party drivers have a means to identify the change.
Redhat 7.1 included that behavior change in the kernel they shipped and it caused no end of problems for those of us that were doing serial drivers since there was no way to easily identify that the patch had been included.
Signature · 1 line
[email protected]
Quoted 11 lines · Jeff Randall
Quoted 5 lines
lets say I call
rs_open()on/dev/ttyS0and if it fails then I should not callrs_close()after a failedrs_open().I hope this is clear now.
All of the other UNIX variants I've dealth with behave that way. However, you cannot just make that change without having some means of identifying that behavior change because all of the linux serial drivers have been written to assume that their
close()will be called even after theiropen()has failed.
Nope, at least serial.c decrements its use count in rs_close()
unconditionally (I believe other drivers do so as well), whether it was called after a failed rs_open() or not. As a result you may suddenly have the use count zero while the driver is still in use. Then modprobe unloads it and a crash is due out soon. That's sad the problem persists at least since 2.2 (a temporary fix was made to 2.2.x, then it was removed again with no better one in 2.3.x).
Here is a fix I'm using for 2.4. It was submitted to Alan once, then a
discussion was performed with a conclusion like "that may be a temporary fix for 2.4, but it needs a complete rework for 2.5". I'm not sure if the patch went in anywhere.
Signature · 78 lines
+ Maciej W. Rozycki, Technical University of Gdansk, Poland + +--------------------------------------------------------------+ + e-mail: [email protected], PGP key available + patch-2.4.5-tty_io-3 diff -up --recursive --new-file linux-2.4.5.macro/drivers/char/tty_io.c linux-2.4.5/drivers/char/tty_io.c --- linux-2.4.5.macro/drivers/char/tty_io.c Tue May 1 17:24:08 2001 +++ linux-2.4.5/drivers/char/tty_io.c Wed Jun 27 20:46:57 2001 @@ -61,6 +61,9 @@ * Reduced memory usage for older ARM systems * -- Russell King <[email protected]> * + * Don't call close after a failed open. + * -- Maciej W. Rozycki <[email protected]>, 21-Jan-2001 + * * Move do_SAK() into process context. Less stack use in devfs functions. * alloc_tty_struct() always uses kmalloc() -- Andrew Morton <[email protected]> 17Mar01 */ @@ -1043,7 +1046,7 @@ static void release_mem(struct tty_struc * WSH 09/09/97: rewritten to avoid some nasty race conditions that could * lead to double frees or releasing memory still in use. */ -static void release_dev(struct file * filp) +static void release_dev(struct file * filp, int do_close) { struct tty_struct *tty, *o_tty; int pty_master, tty_closing, o_tty_closing, do_sleep; @@ -1121,7 +1124,7 @@ static void release_dev(struct file * fi } #endif - if (tty->driver.close) + if (do_close && tty->driver.close) tty->driver.close(tty, filp); /* @@ -1284,7 +1287,7 @@ static void release_dev(struct file * fi static int tty_open(struct inode * inode, struct file * filp) { struct tty_struct *tty; - int noctty, retval; + int noctty, retval, open_ok; kdev_t device; unsigned short saved_flags; char buf[64]; @@ -1369,9 +1372,12 @@ init_dev_done: #ifdef TTY_DEBUG_HANGUP printk("opening %s...", tty_name(tty, buf)); #endif - if (tty->driver.open) + open_ok = 0; + if (tty->driver.open) { retval = tty->driver.open(tty, filp); - else + if (!retval) + open_ok = 1; + } else retval = -ENODEV; filp->f_flags = saved_flags; @@ -1384,7 +1390,7 @@ init_dev_done: tty_name(tty, buf)); #endif - release_dev(filp); + release_dev(filp, open_ok); if (retval != -ERESTARTSYS) return retval; if (signal_pending(current)) @@ -1426,7 +1432,7 @@ init_dev_done: static int tty_release(struct inode * inode, struct file * filp) { lock_kernel(); - release_dev(filp); + release_dev(filp, 1); unlock_kernel(); return 0; }
Quoted 4 lines · Maciej W. Rozycki
Here is a fix I'm using for 2.4. It was submitted to Alan once, then a
discussion was performed with a conclusion like "that may be a temporary fix for 2.4, but it needs a complete rework for 2.5". I'm not sure if the patch went in anywhere.
Have you audited all the tty drivers in 2.4 to make sure that they clean up safely?
I don't believe the serial code will clean up safely as it stands for starters if block_til_ready in serial.c fails, leaving an interrupt in use. Further attempts to open the serial device will probably fail.
Try this as any user with your patch applied:
$ stty -clocal -F /dev/ttyS0 $ cat /proc/interrupts $ cat /dev/ttyS0 ^c $ cat /proc/interrupts
I think you'll find your serial port interrupt is still claimed, despite the module being marked as not in use.
--
Russell King ([email protected]) The developer of ARM Linux
http://www.arm.linux.org.uk/personal/aboutme.html
Quoted 2 lines · Russell King
Have you audited all the tty drivers in 2.4 to make sure that they clean up safely?
No, of course not -- if I had got a response like "this looks mostly OK,
but please check other drivers", then I would have certainly done. I think drivers/tc/zs.c is OK, too, but this was more than a year ago, so I can't recall now, sorry.
Quoted 6 lines
I don't believe the serial code will clean up safely as it stands for starters if
block_til_readyinserial.cfails, leaving an interrupt in use. Further attempts to open the serial device will probably fail.Try this as any user with your patch applied:
Quoted 8 lines
$ stty -clocal -F /dev/ttyS0 $ cat /proc/interrupts $ cat /dev/ttyS0 ^c $ cat /proc/interruptsI think you'll find your serial port interrupt is still claimed, despite the module being marked as not in use.
Indeed -- maybe something was changed past 2.4.5, after all. I'll check
how things look like these days. I nowhere use serial.c as a module anymore, as all systems I maintain are now configured for the serial console, so I might have missed something.
Signature · 3 lines
+ Maciej W. Rozycki, Technical University of Gdansk, Poland + +--------------------------------------------------------------+ + e-mail: [email protected], PGP key available +
Quoted 5 lines · Jeff Randall
All of the other UNIX variants I've dealth with behave that way. However, you cannot just make that change without having some means of identifying that behavior change because all of the linux serial drivers have been written to assume that their
close()will be called even after theiropen()has failed.
It's not only serial drivers, its everything that is a tty driver. I believe that auditing and fixing that lot in 2.4 just isn't going to happen - it's supposed to be a stable kernel after all.
Quoted 4 lines
I'm not opposed to such a change in behavior, but at least be sure that it's somehow identifiable (kernel version, a define set in a header, etc) so that the 3rd party drivers have a means to identify the change.
eg, #if KERNEL_VERSION >= LINUX_VERSION(2,5,0)
Quoted 4 lines
Redhat 7.1 included that behavior change in the kernel they shipped and it caused no end of problems for those of us that were doing serial drivers since there was no way to easily identify that the patch had been included.
The change which adds the MOD_DEC_USE_COUNT stuff is bogus, and it shouldn't have been made. (I'm assuming this is what you're talking about).
--
Russell King ([email protected]) The developer of ARM Linux
http://www.arm.linux.org.uk/personal/aboutme.html
Quoted 11 lines · Russell King
Quoted 5 lines · Jeff Randall
All of the other UNIX variants I've dealth with behave that way. However, you cannot just make that change without having some means of identifying that behavior change because all of the linux serial drivers have been written to assume that their
close()will be called even after theiropen()has failed.It's not only serial drivers, its everything that is a tty driver. I believe that auditing and fixing that lot in 2.4 just isn't going to happen - it's supposed to be a stable kernel after all.
Quoted 6 lines
Quoted 4 lines
I'm not opposed to such a change in behavior, but at least be sure that it's somehow identifiable (kernel version, a define set in a header, etc) so that the 3rd party drivers have a means to identify the change.
eg, #if
KERNEL_VERSION>=LINUX_VERSION(2,5,0)
Exactly. This is not a minor behavior change that can be isolated to a few drivers. It's a fundamental change in behavior that is widespread (I mentioned serial drivers specifically as that's what I was concerned with but you are correct that it affects everything that uses the tty layer).
It's probably a good change to make at some point in the future, but it needs to happen in a well defined and clearly identifiable way. It would probably be best for it to happen in the 2.5 tree.
Quoted 8 lines
Quoted 4 lines
Redhat 7.1 included that behavior change in the kernel they shipped and it caused no end of problems for those of us that were doing serial drivers since there was no way to easily identify that the patch had been included.
The change which adds the
MOD_DEC_USE_COUNTstuff is bogus, and it shouldn't have been made. (I'm assuming this is what you're talking about).
Yes. Redhat's default kernel in 7.1 (2.4.2-2 as it identifies itself) will not call the close() routines if the open() routines return an error. The real problem isn't that the behavior changed, but that there's no easy way to tell that they had changed it.
Signature · 1 line
[email protected]
Quoted 4 lines
It's not only serial drivers, its everything that is a tty driver. I believe that auditing and fixing that lot in 2.4 just isn't going to happen - it's supposed to be a stable kernel after all.
Yeap. I'm working on a rewrite of the tty layer. Yesterday alone I found a nasty flaw with the lpr console. So it will be reworked for 2.5.X.
Is their going to be a mailing for serial developement. I have lots of ideas and a few issues to work out.
Quoted 18 lines · Russell King
Quoted 6 lines · Balbir Singh
Let me make it clearer to you,
lets say I call
rs_open()on/dev/ttyS0and if it fails then I should not callrs_close()after a failedrs_open().You don't call
rs_open. The tty layer does that for you. The tty layer also cleans up on close by calling the driver specific close function.Yes I agree with you that it might not, but that is a 2.5 kernel issue, not a 2.4 "lets do a massive change" issue. The tty layer is complex and messy, and we shouldn't go around randomly changing it in 2.4.
Quoted 2 lines
Lets see what happens with your approach
Quoted 8 lines
Quoted 2 lines
1. I call
rs_open(), it fails,ref_countset to 1Ok, so you're poking around in kernel code calling kernel functions that were previously declared static and not visible to anything but the tty layer. That immediately makes your example invalid because you're not following the rules that the tty layers lays down for opening tty devices.
What I mean by I call rs_open() is that the person who writes the tty layer should not call close() after open() fails, I cannot directly invoke rs_open() and rs_close(), I KNOW that.
If I were to define an API like rs_open(), I would expect the person who uses these API (rs_*) in our case the tty layer, to call rs_close() only if rs_open() is successfull. I do not want to leave a dependency that rs_close() SHOULD BE called even if rs_open() failed.
As you say this fix works for 2.4, Since we are into 2.5, I hope that it will be fixed in a better manner in 2.5. I have some suggestions, ideas for the serial driver in 2.5, are u willing to discuss them?
Balbir
Quoted 11 lines
--
Russell King ([email protected]) The developer of ARM Linux
http://www.arm.linux.org.uk/personal/aboutme.html-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
-------------------------------------------------------------------------------------------------------------------------
Information transmitted by this E-MAIL is proprietary to Wipro and/or its Customers and is intended for use only by the individual or entity to which it is addressed, and may contain information that is privileged, confidential or exempt from disclosure under applicable law. If you are not the intended recipient or it appears that this mail has been forwarded to you without proper authority, you are notified that any use or dissemination of this information in any manner is strictly prohibited. In such cases, please notify us immediately at mailto:[email protected] and delete this mail from your records.
----------------------------------------------------------------------------------------------------------------------
Quoted 3 lines · BALBIR SINGH
What I mean by I call
rs_open()is that the person who writes the tty layer should not callclose()afteropen()fails, I cannot directly invokers_open()andrs_close(), I KNOW that.
We can't change the tty layer in 2.4 at this point. We can do it in 2.5. It requires an audit of all drivers though. I believe we're going over ground covered earlier in this thread now.
Quoted 2 lines
I have some suggestions, ideas for the serial driver in 2.5, are u willing to discuss them?
Please look at the cvs stuff at:
:pserver:[email protected]:/mnt/src/cvsroot, module serial (no password)
Thanks.
--
Russell King ([email protected]) The developer of ARM Linux
http://www.arm.linux.org.uk/personal/aboutme.html
Quoted 4 lines · BALBIR SINGH
As you say this fix works for 2.4, Since we are into 2.5, I hope that it will be fixed in a better manner in 2.5. I have some suggestions, ideas for the serial driver in 2.5, are u willing to discuss them?
Would it not be better if the current code in 2.5 had the fix from 2.4 until the entire layer is audited?
Quoted 8 lines · Mike Fedyk
Quoted 4 lines · BALBIR SINGH
As you say this fix works for 2.4, Since we are into 2.5, I hope that it will be fixed in a better manner in 2.5. I have some suggestions, ideas for the serial driver in 2.5, are u willing to discuss them?
Would it not be better if the current code in 2.5 had the fix from 2.4 until the entire layer is audited?
That was my original intention. IIRC, James Simmons has said he's rewriting the tty layer anyway in 2.5.
--
Russell King ([email protected]) The developer of ARM Linux
http://www.arm.linux.org.uk/personal/aboutme.html
Quoted 7 lines · Russell King
Quoted 6 lines · Mike Fedyk
Quoted 4 lines · BALBIR SINGH
As you say this fix works for 2.4, Since we are into 2.5, I hope that it will be fixed in a better manner in 2.5. I have some suggestions, ideas for the serial driver in 2.5, are u willing to discuss them?
Quoted 6 lines
Quoted 2 lines
Would it not be better if the current code in 2.5 had the fix from 2.4 until the entire layer is audited?
That was my original intention. IIRC, James Simmons has said he's rewriting the tty layer anyway in 2.5.
ACK.
I'm just looping on the issue. ;)