Skip to contentSkip to conversation

serial: core: Fix probing serial_base_bus devices

PATCH In review Tony Lindgren 3 participants 7 messages Jun 1, 2023 TTY layer and serial drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 serial: core: Fix probing serial_base_bus devices

If a physical serial port device driver uses arch_initcall() we fail to probe the serial_base_bus devices and the serial port tx fails. This is because as serial_base_bus uses module_initcall().

Let's fix the issue by changing serial_base_bus to use arch_initcall().

Let's also return an error if a driver attempts to call uart_add_one_port() too early.

Fixes 84a9582fd203 ("serial: core: Start managing serial controllers to enable runtime PM")
---
 drivers/tty/serial/serial_base_bus.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/tty/serial/serial_base_bus.c +10 -1
@@ -17,6 +17,8 @@
 
 #include "serial_base.h"
 
+static bool serial_base_initialized;
+
 static int serial_base_match(struct device *dev, struct device_driver *drv)
 {
 	int len = strlen(drv->name);
@@ -48,6 +50,11 @@ static int serial_base_device_init(struct uart_port *port,
 				   void (*release)(struct device *dev),
 				   int id)
 {
+	if (!serial_base_initialized) {
+		dev_err(port->dev, "uart_add_one_port() called before arch_initcall()?\n");
+		return -EPROBE_DEFER;
+	}
+
 	device_initialize(dev);
 	dev->type = type;
 	dev->parent = parent_dev;
@@ -175,6 +182,8 @@ static int serial_base_init(void)
 	if (ret)
 		goto err_ctrl_exit;
 
+	serial_base_initialized = true;
+
 	return 0;
 
 err_ctrl_exit:
@@ -185,7 +194,7 @@ static int serial_base_init(void)
 
 	return ret;
 }
-module_init(serial_base_init);
+arch_initcall(serial_base_init);
 
 static void serial_base_exit(void)
 {
Signature · 1 line
2.40.1
#1 lore raw
Quoted 5 lines · Tony Lindgren

If a physical serial port device driver uses arch_initcall() we fail to probe the serial_base_bus devices and the serial port tx fails. This is because as serial_base_bus uses module_initcall().

Let's fix the issue by changing serial_base_bus to use arch_initcall().

This will only work if the linking order is such that this will always come before the drivers. Is that the case here?

thanks,

greg k-h

#2 lore raw
Quoted 8 lines · Greg Kroah-Hartman
Quoted 5 lines · Tony Lindgren

If a physical serial port device driver uses arch_initcall() we fail to probe the serial_base_bus devices and the serial port tx fails. This is because as serial_base_bus uses module_initcall().

Let's fix the issue by changing serial_base_bus to use arch_initcall().

This will only work if the linking order is such that this will always come before the drivers. Is that the case here?

Yes, serial_base_bus is linked as a second object, just after the serial_core. Device drivers come later.

Best regards

Signature · 2 lines
Marek Szyprowski, PhD
Samsung R&D Institute Poland
#3 lore raw
Quoted 6 lines · Marek Szyprowski
Quoted 5 lines · Greg Kroah-Hartman
Quoted 4 lines · Tony Lindgren

If a physical serial port device driver uses arch_initcall() we fail to probe the serial_base_bus devices and the serial port tx fails. This is because as serial_base_bus uses module_initcall().

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Let's fix the issue by changing serial_base_bus to use arch_initcall().

This will only work if the linking order is such that this will always come before the drivers. Is that the case here?

Yes, serial_base_bus is linked as a second object, just after the serial_core. Device drivers come later.

Oh good, I guess it wouldn't work at all as the serial_core is needed by all of those drivers first too, so this should work, thanks for checking.

greg k-h

#4 lore raw
In reply to Marek Szyprowski
Quoted 6 lines · Marek Szyprowski
Quoted 5 lines · Greg Kroah-Hartman
Quoted 4 lines · Tony Lindgren

If a physical serial port device driver uses arch_initcall() we fail to probe the serial_base_bus devices and the serial port tx fails. This is because as serial_base_bus uses module_initcall().

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Let's fix the issue by changing serial_base_bus to use arch_initcall().

This will only work if the linking order is such that this will always come before the drivers. Is that the case here?

Yes, serial_base_bus is linked as a second object, just after the serial_core. Device drivers come later.

If we don't want to rely on the Makefile order here, we could do something like the patch below.

Regards,

Tony

8< ---------------------

drivers/tty/serial/serial_base_bus.c +30 -0
@@ -11,12 +11,18 @@
 #include <linux/container_of.h>
 #include <linux/device.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/serial_core.h>
 #include <linux/slab.h>
 #include <linux/spinlock.h>
 
 #include "serial_base.h"
 
+static bool serial_base_initialized;
+static DEFINE_MUTEX(serial_base_lock);
+
+static int serial_base_init(void);
+
 static int serial_base_match(struct device *dev, struct device_driver *drv)
 {
 	int len = strlen(drv->name);
@@ -48,6 +54,12 @@ static int serial_base_device_init(struct uart_port *port,
 				   void (*release)(struct device *dev),
 				   int id)
 {
+	/*
+	 * Initialize bus if not yet initialized. Some drivers are using
+	 * arch_initcall()
+	 */
+	serial_base_init();
+
 	device_initialize(dev);
 	dev->type = type;
 	dev->parent = parent_dev;
@@ -163,6 +175,14 @@ static int serial_base_init(void)
 {
 	int ret;
 
+	mutex_lock(&serial_base_lock);
+
+	/* Also called from serial_base_device_init() in some cases */
+	if (serial_base_initialized) {
+		ret = 0;
+		goto out_unlock;
+	}
+
 	ret = bus_register(&serial_base_bus_type);
 	if (ret)
 		return ret;
@@ -175,6 +195,10 @@ static int serial_base_init(void)
 	if (ret)
 		goto err_ctrl_exit;
 
+	serial_base_initialized = true;
+
+	mutex_unlock(&serial_base_lock);
+
 	return 0;
 
 err_ctrl_exit:
@@ -183,15 +207,21 @@ static int serial_base_init(void)
 err_bus_unregister:
 	bus_unregister(&serial_base_bus_type);
 
+out_unlock:
+	mutex_unlock(&serial_base_lock);
+
 	return ret;
 }
 module_init(serial_base_init);
 
 static void serial_base_exit(void)
 {
+	mutex_lock(&serial_base_lock);
 	serial_base_port_exit();
 	serial_base_ctrl_exit();
 	bus_unregister(&serial_base_bus_type);
+	serial_base_initialized = false;
+	mutex_unlock(&serial_base_lock);
 }
 module_exit(serial_base_exit);
 
Signature · 1 line
2.40.1
#5 lore raw
In reply to Greg Kroah-Hartman
Quoted 9 lines · Greg Kroah-Hartman
Quoted 5 lines · Tony Lindgren

If a physical serial port device driver uses arch_initcall() we fail to probe the serial_base_bus devices and the serial port tx fails. This is because as serial_base_bus uses module_initcall().

Let's fix the issue by changing serial_base_bus to use arch_initcall().

This will only work if the linking order is such that this will always come before the drivers. Is that the case here?

I guess based on Makefile. And also if serial drivers are modules as we export uart_add_one_port() from serial_base.ko. But yeah this is pretty fragile potentially.

Hmm maybe we could keep module_init() and then also call serial_base_init() on uart_add_one_port() path if not yet initialized?

Probably the module_init() should be still there for case when no serial port device drivers are loaded and serial_base is unloaded..

Regards,

Tony

#6 lore raw
Quoted 6 lines · Tony Lindgren
Quoted 5 lines · Greg Kroah-Hartman
Quoted 4 lines · Tony Lindgren

If a physical serial port device driver uses arch_initcall() we fail to probe the serial_base_bus devices and the serial port tx fails. This is because as serial_base_bus uses module_initcall().

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Let's fix the issue by changing serial_base_bus to use arch_initcall().

This will only work if the linking order is such that this will always come before the drivers. Is that the case here?

I guess based on Makefile. And also if serial drivers are modules as we export uart_add_one_port() from serial_base.ko. But yeah this is pretty fragile potentially.

It's fine, and normal, the Makefile is the ordering here so all is good.

Quoted 5 lines

Hmm maybe we could keep module_init() and then also call serial_base_init() on uart_add_one_port() path if not yet initialized?

Probably the module_init() should be still there for case when no serial port device drivers are loaded and serial_base is unloaded..

I'll leave this as-is, it looks correct, and I'll queue it up now, thanks.

greg k-h