Skip to contentSkip to conversation

ttyprintk: Fix wrong tty_unregister_driver() call in the error path

PATCH Superseded Takashi Iwai 5 participants 10 messages Apr 2, 2014 – Apr 5, 2014 Char and misc drivers

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 ttyprintk: Fix wrong tty_unregister_driver() call in the error path

ttyprintk driver calls tty_unregister_driver() wrongly in the error path of tty_register_driver(). Also, setting ttyprintk_driver to NULL is utterly superfluous, so let's get rid of it, too.

---
 drivers/char/ttyprintk.c | 2 --
 1 file changed, 2 deletions(-)
drivers/char/ttyprintk.c +0 -2
index daea84c41743..2a39c5790364 100644
@@ -210,10 +210,8 @@ static int __init ttyprintk_init(void)
 	return 0;
 
 error:
-	tty_unregister_driver(ttyprintk_driver);
 	put_tty_driver(ttyprintk_driver);
 	tty_port_destroy(&tpk_port.port);
-	ttyprintk_driver = NULL;
 	return ret;
 }
 device_initcall(ttyprintk_init);
Signature · 1 line
1.9.1
#1 lore raw

PATCH 2/2 ttyprintk: Allow built as a module

The driver is well written to be used as a module, just the exit call is missing.

---
 drivers/char/Kconfig     |  2 +-
 drivers/char/ttyprintk.c | 13 ++++++++++++-
 2 files changed, 13 insertions(+), 2 deletions(-)
drivers/char/Kconfig +1 -1
index 1386749b48ff..97816b133c7f 100644
@@ -40,7 +40,7 @@ config SGI_MBCS
 source "drivers/tty/serial/Kconfig"
 
 config TTY_PRINTK
-	bool "TTY driver to output user messages via printk"
+	tristate "TTY driver to output user messages via printk"
 	depends on EXPERT && TTY
 	default n
 	---help---
drivers/char/ttyprintk.c +12 -1
index 2a39c5790364..73606eaaba71 100644
@@ -17,7 +17,7 @@
 #include <linux/device.h>
 #include <linux/serial.h>
 #include <linux/tty.h>
-#include <linux/export.h>
+#include <linux/module.h>
 
 struct ttyprintk_port {
 	struct tty_port port;
@@ -214,4 +214,15 @@ error:
 	tty_port_destroy(&tpk_port.port);
 	return ret;
 }
+
+static void ttyprintk_exit(void)
+{
+	tty_unregister_driver(ttyprintk_driver);
+	put_tty_driver(ttyprintk_driver);
+	tty_port_destroy(&tpk_port.port);
+}
+
 device_initcall(ttyprintk_init);
+module_exit(ttyprintk_exit);
+
+MODULE_LICENSE("GPL");
Signature · 1 line
1.9.1
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] ttyprintk: Allow built as a module

Hi Takashi,

Quoted 36 lines · Takashi Iwai

The driver is well written to be used as a module, just the exit call is missing.

---
 drivers/char/Kconfig     |  2 +-
 drivers/char/ttyprintk.c | 13 ++++++++++++-
 2 files changed, 13 insertions(+), 2 deletions(-)
drivers/char/Kconfig +1 -1
index 1386749b48ff..97816b133c7f 100644
@@ -40,7 +40,7 @@ config SGI_MBCS
 source "drivers/tty/serial/Kconfig"
 
 config TTY_PRINTK
-	bool "TTY driver to output user messages via printk"
+	tristate "TTY driver to output user messages via printk"
 	depends on EXPERT && TTY
 	default n
 	---help---
drivers/char/ttyprintk.c +1 -1
index 2a39c5790364..73606eaaba71 100644
@@ -17,7 +17,7 @@
 #include <linux/device.h>
 #include <linux/serial.h>
 #include <linux/tty.h>
-#include <linux/export.h>
+#include <linux/module.h>
 
 struct ttyprintk_port {
 	struct tty_port port;

drivers/char/ttyprintk.c error: · line 218

Quoted 5 lines
drivers/char/ttyprintk.c +2 -0
@@ -214,4 +214,15 @@ error:
 	tty_port_destroy(&tpk_port.port);
 	return ret;
 }
+
+static void ttyprintk_exit(void)

Could be marked __exit.

drivers/char/ttyprintk.c error: · line 228

Quoted 10 lines
drivers/char/ttyprintk.c +9 -0
+{
+	tty_unregister_driver(ttyprintk_driver);
+	put_tty_driver(ttyprintk_driver);
+	tty_port_destroy(&tpk_port.port);
+}
+
 device_initcall(ttyprintk_init);
+module_exit(ttyprintk_exit);
+
+MODULE_LICENSE("GPL");

Other than this, this looks good, thanks for doing that.

Signature · 2 lines
Jean Delvare
SUSE L3 Support
#3 lore raw

Re: [PATCH 2/2] ttyprintk: Allow built as a module

At Wed, 02 Apr 2014 14:31:46 +0200,

Quoted 61 lines · Jean Delvare

Hi Takashi,

Quoted 36 lines · Takashi Iwai

The driver is well written to be used as a module, just the exit call is missing.

---
 drivers/char/Kconfig     |  2 +-
 drivers/char/ttyprintk.c | 13 ++++++++++++-
 2 files changed, 13 insertions(+), 2 deletions(-)
drivers/char/Kconfig +1 -1
index 1386749b48ff..97816b133c7f 100644
@@ -40,7 +40,7 @@ config SGI_MBCS
 source "drivers/tty/serial/Kconfig"
 
 config TTY_PRINTK
-	bool "TTY driver to output user messages via printk"
+	tristate "TTY driver to output user messages via printk"
 	depends on EXPERT && TTY
 	default n
 	---help---
drivers/char/ttyprintk.c +1 -1
index 2a39c5790364..73606eaaba71 100644
@@ -17,7 +17,7 @@
 #include <linux/device.h>
 #include <linux/serial.h>
 #include <linux/tty.h>
-#include <linux/export.h>
+#include <linux/module.h>
 
 struct ttyprintk_port {
 	struct tty_port port;

drivers/char/ttyprintk.c error: · line 218

Quoted 5 lines
drivers/char/ttyprintk.c +2 -0
@@ -214,4 +214,15 @@ error:
 	tty_port_destroy(&tpk_port.port);
 	return ret;
 }
+
+static void ttyprintk_exit(void)

Could be marked __exit.

drivers/char/ttyprintk.c error: · line 228

Quoted 10 lines
drivers/char/ttyprintk.c +9 -0
+{
+	tty_unregister_driver(ttyprintk_driver);
+	put_tty_driver(ttyprintk_driver);
+	tty_port_destroy(&tpk_port.port);
+}
+
 device_initcall(ttyprintk_init);
+module_exit(ttyprintk_exit);
+
+MODULE_LICENSE("GPL");

Other than this, this looks good, thanks for doing that.

Quoted 1 line

Thanks! I'll resubmit the fixed patch.

Takashi

#4 lore raw

Re: [PATCH 2/2] ttyprintk: Allow built as a module

In reply to Takashi Iwai
Quoted 8 lines · Takashi Iwai

The driver is well written to be used as a module, just the exit call is missing.

---
 drivers/char/Kconfig     |  2 +-
 drivers/char/ttyprintk.c | 13 ++++++++++++-
 2 files changed, 13 insertions(+), 2 deletions(-)

There was some reason we didn't do this back when the code was accepted, but I really can't remember why.

If you have tested this out, I don't have any objections to taking these patches, thanks.

greg k-h

#5 lore raw

Re: [PATCH 2/2] ttyprintk: Allow built as a module

At Wed, 2 Apr 2014 12:32:22 -0700,

Quoted 11 lines · Greg Kroah-Hartman
Quoted 8 lines · Takashi Iwai

The driver is well written to be used as a module, just the exit call is missing.

---
 drivers/char/Kconfig     |  2 +-
 drivers/char/ttyprintk.c | 13 ++++++++++++-
 2 files changed, 13 insertions(+), 2 deletions(-)
Quoted 5 lines

There was some reason we didn't do this back when the code was accepted, but I really can't remember why.

If you have tested this out, I don't have any objections to taking these patches, thanks.

The module worked fine with my quick tests (load/unload/feed something via /dev/ttyprintk), at least. But it'd be helpful if anyone remembers and clarifies.

thanks,

Takashi

#6 lore raw

Re: [PATCH 2/2] ttyprintk: Allow built as a module

Dne 03.04.2014 (čet) ob 08:13 +0200 je Takashi Iwai napisal(a):

Quoted 16 lines

At Wed, 2 Apr 2014 12:32:22 -0700,

Quoted 14 lines · Greg Kroah-Hartman
Quoted 8 lines · Takashi Iwai

The driver is well written to be used as a module, just the exit call is missing.

---
 drivers/char/Kconfig     |  2 +-
 drivers/char/ttyprintk.c | 13 ++++++++++++-
 2 files changed, 13 insertions(+), 2 deletions(-)

There was some reason we didn't do this back when the code was accepted, but I really can't remember why.

Quoted 7 lines
Quoted 2 lines

If you have tested this out, I don't have any objections to taking these patches, thanks.

The module worked fine with my quick tests (load/unload/feed something via /dev/ttyprintk), at least. But it'd be helpful if anyone remembers and clarifies.

My initial requirement was to be able to use this functionality as soon as possible at the beginning of user-space initialisation, so i never thought of the ttyprintk code being loaded afterwards as module. Also nobody brought modularisation subject to the table during code acceptance.

Quoted 4 lines

thanks,

Takashi

regards, Samo

Reviewed-by on v1 1/2
#7 lore raw
In reply to Takashi Iwai
Quoted 15 lines · Takashi Iwai

ttyprintk driver calls tty_unregister_driver() wrongly in the error path of tty_register_driver(). Also, setting ttyprintk_driver to NULL is utterly superfluous, so let's get rid of it, too.

---
 drivers/char/ttyprintk.c | 2 --
 1 file changed, 2 deletions(-)
drivers/char/ttyprintk.c +0 -0
index daea84c41743..2a39c5790364 100644

drivers/char/ttyprintk.c static int __init ttyprintk_init(void) · line 217

Quoted 10 lines
drivers/char/ttyprintk.c +0 -2
@@ -210,10 +210,8 @@ static int __init ttyprintk_init(void)
 	return 0;
 
 error:
-	tty_unregister_driver(ttyprintk_driver);
 	put_tty_driver(ttyprintk_driver);
 	tty_port_destroy(&tpk_port.port);
-	ttyprintk_driver = NULL;
 	return ret;
 }
 device_initcall(ttyprintk_init);
Signature · 2 lines
Jean Delvare
SUSE L3 Support
#8 lore raw

At Wed, 02 Apr 2014 14:24:33 +0200,

Quoted 19 lines · Jean Delvare
Quoted 17 lines · Takashi Iwai

ttyprintk driver calls tty_unregister_driver() wrongly in the error path of tty_register_driver(). Also, setting ttyprintk_driver to NULL is utterly superfluous, so let's get rid of it, too.

---
 drivers/char/ttyprintk.c | 2 --
 1 file changed, 2 deletions(-)
drivers/char/ttyprintk.c +0 -0
index daea84c41743..2a39c5790364 100644
@@ -210,10 +210,8 @@ static int __init ttyprintk_init(void)
 	return 0;
 
Quoted 10 lines

drivers/char/ttyprintk.c static int __init ttyprintk_init(void) · line 217

Quoted 8 lines
drivers/char/ttyprintk.c +0 -2
 error:
-	tty_unregister_driver(ttyprintk_driver);
 	put_tty_driver(ttyprintk_driver);
 	tty_port_destroy(&tpk_port.port);
-	ttyprintk_driver = NULL;
 	return ret;
 }
 device_initcall(ttyprintk_init);

Meanwhile, I found that tty_unregister_driver() was added intentionally in the commit f06fb543c1d0,
TTY: ttyprintk, unregister tty driver on failure

When the tty_printk driver fails to create a node in sysfs, the system
crashes. It is because the driver registers a tty driver and frees it
without deregistering it first. The fix is easy: add a call to tty_unregister_driver to the fail path.

But, I failed to see why this is needed in the current code.

Jiri, is your fix still valid at all?

thanks,

Takashi

#9 lore raw
Quoted 43 lines · Takashi Iwai

At Wed, 02 Apr 2014 14:24:33 +0200,

Quoted 19 lines · Jean Delvare
Quoted 17 lines · Takashi Iwai

ttyprintk driver calls tty_unregister_driver() wrongly in the error path of tty_register_driver(). Also, setting ttyprintk_driver to NULL is utterly superfluous, so let's get rid of it, too.

---
 drivers/char/ttyprintk.c | 2 --
 1 file changed, 2 deletions(-)
drivers/char/ttyprintk.c +0 -0
index daea84c41743..2a39c5790364 100644
@@ -210,10 +210,8 @@ static int __init ttyprintk_init(void)
 	return 0;
 
Quoted 10 lines

drivers/char/ttyprintk.c static int __init ttyprintk_init(void) · line 217

Quoted 8 lines
drivers/char/ttyprintk.c +0 -2
 error:
-	tty_unregister_driver(ttyprintk_driver);
 	put_tty_driver(ttyprintk_driver);
 	tty_port_destroy(&tpk_port.port);
-	ttyprintk_driver = NULL;
 	return ret;
 }
 device_initcall(ttyprintk_init);

Meanwhile, I found that tty_unregister_driver() was added intentionally in the commit f06fb543c1d0,
TTY: ttyprintk, unregister tty driver on failure

When the tty_printk driver fails to create a node in sysfs, the system
crashes. It is because the driver registers a tty driver and frees it
without deregistering it first. The fix is easy: add a call to tty_unregister_driver to the fail path.

Quoted 3 lines

But, I failed to see why this is needed in the current code.

Jiri, is your fix still valid at all?

The code was different. There was also device_create after tty_register_driver. I must admit that I don't know why I didn't change 'goto error' in the tty_register_driver fail path.

        ret = tty_register_driver(ttyprintk_driver);
        if (ret < 0) {
                printk(KERN_ERR "Couldn't register ttyprintk driver\n");
                goto error;
        }

        /* create our unnumbered device */
        rp = device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 3), NULL,
                                ttyprintk_driver->name);
        if (IS_ERR(rp)) {
                printk(KERN_ERR "Couldn't create ttyprintk device\n");
                ret = PTR_ERR(rp);
                goto error;
        }

So yes, there should be no tty_unregister_driver in the fail path when device_create is gone now.

thanks,

Signature · 2 lines
js
suse labs