Skip to contentSkip to conversation

hvc: ensure hvc_init is only ever called once in hvc_console.c

PATCH In review Paul Gortmaker 2 participants 2 messages Jan 14, 2014

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

PATCH 1/1 hvc: ensure hvc_init is only ever called once in hvc_console.c

Commit 3e6c6f630a5282df8f3393a59f10eb9c56536d23 ("Delay creation of khcvd thread") moved the call of hvc_init from being a device_initcall into hvc_alloc, and used a non-null hvc_driver as indication of whether hvc_init had already been called.

The problem with this is that hvc_driver is only assigned a value at the bottom of hvc_init, and so there is a window where multiple hvc_alloc calls can be in progress at the same time and hence try and call hvc_init multiple times. Previously the use of device_init guaranteed that hvc_init was only called once.

This manifests itself as sporadic instances of two hvc_init calls racing each other, and with the loser of the race getting -EBUSY from tty_register_driver() and hence that virtual console fails:

Couldn't register hvc console driver virtio-ports vport0p1: error -16 allocating hvc for port

Here we add an atomic_t to guarantee we'll never run hvc_init twice.

Cc [email protected] # v2.6.24+
Cc Rusty Russell <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Fixes 3e6c6f630a52 ("Delay creation of khcvd thread")

---

[Greg: I realize tty-next is closed; given that this is an ancient
bug, I don't see any real urgency here, so feel free to queue it whenever/whereever is most convenient - assuming it is an OK fix.]

 drivers/tty/hvc/hvc_console.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/tty/hvc/hvc_console.c +5 -1
index 9eba119bcdd3..620da5f88e91 100644
@@ -31,6 +31,7 @@
 #include <linux/list.h>
 #include <linux/module.h>
 #include <linux/major.h>
+#include <linux/atomic.h>
 #include <linux/sysrq.h>
 #include <linux/tty.h>
 #include <linux/tty_flip.h>
@@ -70,6 +71,9 @@ static struct task_struct *hvc_task;
 /* Picks up late kicks after list walk but before schedule() */
 static int hvc_kicked;
 
+/* hvc_init is triggered from hvc_alloc, i.e. only when actually used */
+static atomic_t hvc_needs_init __read_mostly = ATOMIC_INIT(-1);
+
 static int hvc_init(void);
 
 #ifdef CONFIG_MAGIC_SYSRQ
@@ -851,7 +855,7 @@ struct hvc_struct *hvc_alloc(uint32_t vtermno, int data,
 	int i;
 
 	/* We wait until a driver actually comes along */
-	if (!hvc_driver) {
+	if (atomic_inc_not_zero(&hvc_needs_init)) {
 		int err = hvc_init();
 		if (err)
 			return ERR_PTR(err);
Signature · 1 line
1.8.5.2
#1 lore raw
Quoted 27 lines · Paul Gortmaker

Commit 3e6c6f630a5282df8f3393a59f10eb9c56536d23 ("Delay creation of khcvd thread") moved the call of hvc_init from being a device_initcall into hvc_alloc, and used a non-null hvc_driver as indication of whether hvc_init had already been called.

The problem with this is that hvc_driver is only assigned a value at the bottom of hvc_init, and so there is a window where multiple hvc_alloc calls can be in progress at the same time and hence try and call hvc_init multiple times. Previously the use of device_init guaranteed that hvc_init was only called once.

This manifests itself as sporadic instances of two hvc_init calls racing each other, and with the loser of the race getting -EBUSY from tty_register_driver() and hence that virtual console fails:

Couldn't register hvc console driver virtio-ports vport0p1: error -16 allocating hvc for port

Here we add an atomic_t to guarantee we'll never run hvc_init twice.

Cc [email protected] # v2.6.24+
Cc Rusty Russell <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Fixes 3e6c6f630a52 ("Delay creation of khcvd thread")
Quoted 5 lines

---

[Greg: I realize tty-next is closed; given that this is an ancient
bug, I don't see any real urgency here, so feel free to queue it whenever/whereever is most convenient - assuming it is an OK fix.]

Looks ok, I'll queue it up after 3.14-rc1 is out, thanks.

greg k-h