Skip to contentSkip to conversation

tty: serial: add and use a managed variant of uart_add_one_port()

PATCH Superseded Bartosz Golaszewski 1 participant 3 messages Dec 9, 2022

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

cover tty: serial: add and use a managed variant of uart_add_one_port()

From: Bartosz Golaszewski <[email protected]>

This series adds a managed variant of uart_add_one_port() and uses it in the qcom-geni-serial driver.

I've been asked by Greg to send it separately and he didn't seem to be impressed by the proposition of adding devres interfaces to the tty layer in general. I can only assume it has something to do with the ongoing discussion about the supposed danger of using devres interfaces in conjunction with exporting character devices to user-space.

The bug in question can be triggered by opening a device file, unbinding the driver that exported it and then calling any of the system calls on the associated file descriptor.

After some testing I noticed that many subsystems are indeed either crashing or deadlocking in the above situation. I've sent patches that attempt to fix the GPIO and I2C subsystems[1][2]. Neither of these issues have anything to do with devres and all to do with the fact that certain resources are freed on driver unbind and others need to live for as long as the character device exists. More details on that in the cover letters and commit messages in the links.

I'd like to point out that the serial code is immune to this issue as before every operation, the serial core takes the port lock and checks the uart state. If the device no longer exists (when the uart port is removed, the pointer to uart_port inside uart_state is to NULL), it gracefully returns -ENODEV to user-space.

Please consider applying the patches in the series as devres is the easiest way to lessen the burden on driver developers when dealing with complex error paths and resource leaks. The general rule for devres is: if it can be freed in .remove() then it can be managed by devres, which is the case for this new helper.

Bart

[1] https://lkml.org/lkml/2022/12/8/826
[2] https://lkml.org/lkml/2022/12/5/414

Bartosz Golaszewski (2):
tty: serial: provide devm_uart_add_one_port()
tty: serial: qcom-geni-serial: use devres for uart port management

 .../driver-api/driver-model/devres.rst        |  3 ++
 drivers/tty/serial/qcom_geni_serial.c         |  8 +---
 drivers/tty/serial/serial_core.c              | 48 +++++++++++++++++++
 include/linux/serial_core.h                   |  6 +++
 4 files changed, 58 insertions(+), 7 deletions(-)
Signature · 1 line
2.37.2
#1 lore raw

PATCH 1/2 tty: serial: provide devm_uart_add_one_port()

From: Bartosz Golaszewski <[email protected]>

Provide a devres variant of uart_add_one_port() that removes the managed port at device detach.

---
 .../driver-api/driver-model/devres.rst        |  3 ++
 drivers/tty/serial/serial_core.c              | 48 +++++++++++++++++++
 include/linux/serial_core.h                   |  6 +++
 3 files changed, 57 insertions(+)
Documentation/driver-api/driver-model/devres.rst +3 -0
index 56082265e8e5..5d07a8c1eadb 100644
@@ -436,6 +436,9 @@ RTC
 SERDEV
   devm_serdev_device_open()
 
+SERIAL
+  devm_uart_add_one_port()
+
 SLAVE DMA ENGINE
   devm_acpi_dma_controller_register()
 
drivers/tty/serial/serial_core.c +48 -0

54 lines not on this page · raw message

include/linux/serial_core.h +6 -0
index d657f2a42a7b..d0911f04706e 100644
@@ -771,6 +771,12 @@ int uart_remove_one_port(struct uart_driver *reg, struct uart_port *port);
 bool uart_match_port(const struct uart_port *port1,
 		const struct uart_port *port2);
 
+/*
+ * UART devres
+ */
+int devm_uart_add_one_port(struct device *dev,
+			   struct uart_driver *drv, struct uart_port *port);
+
 /*
  * Power Management
  */
Signature · 1 line
2.37.2
#2 lore raw

PATCH 2/2 tty: serial: qcom-geni-serial: use devres for uart port management

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Shrink and simplify the probe() and remove() code by using the managed variant of uart_add_one_port().

---
 drivers/tty/serial/qcom_geni_serial.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
drivers/tty/serial/qcom_geni_serial.c +1 -7
index 83b66b73303a..16532cb64465 100644
@@ -1469,7 +1469,7 @@ static int qcom_geni_serial_probe(struct platform_device *pdev)
 	platform_set_drvdata(pdev, port);
 	port->handle_rx = console ? handle_rx_console : handle_rx_uart;
 
-	ret = uart_add_one_port(drv, uport);
+	ret = devm_uart_add_one_port(&pdev->dev, drv, uport);
 	if (ret)
 		return ret;
 
@@ -1478,7 +1478,6 @@ static int qcom_geni_serial_probe(struct platform_device *pdev)
 			IRQF_TRIGGER_HIGH, port->name, uport);
 	if (ret) {
 		dev_err(uport->dev, "Failed to get IRQ ret %d\n", ret);
-		uart_remove_one_port(drv, uport);
 		return ret;
 	}
 
@@ -1495,7 +1494,6 @@ static int qcom_geni_serial_probe(struct platform_device *pdev)
 						port->wakeup_irq);
 		if (ret) {
 			device_init_wakeup(&pdev->dev, false);
-			uart_remove_one_port(drv, uport);
 			return ret;
 		}
 	}
@@ -1505,12 +1503,8 @@ static int qcom_geni_serial_probe(struct platform_device *pdev)
 
 static int qcom_geni_serial_remove(struct platform_device *pdev)
 {
-	struct qcom_geni_serial_port *port = platform_get_drvdata(pdev);
-	struct uart_driver *drv = port->private_data.drv;
-
 	dev_pm_clear_wake_irq(&pdev->dev);
 	device_init_wakeup(&pdev->dev, false);
-	uart_remove_one_port(drv, &port->uport);
 
 	return 0;
 }
Signature · 1 line
2.37.2