mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/3] driver: make CONFIG_DEBUG_PROBES more useful for removal
@ 2024-02-15 10:33 Ahmad Fatoum
  2024-02-15 10:33 ` [PATCH 2/3] drivers: drop simple bus remove in favor of common implementation Ahmad Fatoum
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2024-02-15 10:33 UTC (permalink / raw)
  To: barebox; +Cc: ske, Ahmad Fatoum

When CONFIG_DEBUG_PROBES is enabled, barebox will print a message on
every device probe and removal. Unfortunately, the removal prints are not
very useful, because the removal happens in the bus remove function,
which is often a no-op, but that's not known to driver core.

To make this a bit more useful, let's allow skipping bus remove
functions like Linux does and only print that a remove is happening if
either a bus or driver remove function is available.

At present, this doesn't change much, but the follow-up commit will drop
bus remove functions that only call the driver remove function, which
will shorten the CONFIG_DEBUG_PROBES output on shutdown a fair bit.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 commands/devunbind.c  | 14 +++++++++-----
 drivers/base/driver.c | 28 ++++++++++++++++++----------
 include/driver.h      | 10 ++++++++++
 3 files changed, 37 insertions(+), 15 deletions(-)

diff --git a/commands/devunbind.c b/commands/devunbind.c
index dab7f834db9f..d30193b28527 100644
--- a/commands/devunbind.c
+++ b/commands/devunbind.c
@@ -20,9 +20,14 @@ static int do_devunbind(int argc, char *argv[])
 			break;
 		case 'l':
 			list_for_each_entry(dev, &active_device_list, active) {
+				void *rm_dev;
+
 				BUG_ON(!dev->driver);
-				if (dev->bus->remove)
-					printf("%pS(%s, %s)\n", dev->bus->remove,
+
+				rm_dev = dev->bus->remove ?: dev->driver->remove;
+
+				if (rm_dev)
+					printf("%pS(%s, %s)\n", rm_dev,
 					       dev->driver->name, dev_name(dev));
 			}
 			return 0;
@@ -47,13 +52,12 @@ static int do_devunbind(int argc, char *argv[])
 			continue;
 		}
 
-		if (!dev->driver || !dev->bus->remove) {
-			printf("skipping unbound %s\n", argv[i]);
+		if (!device_remove(dev)) {
+			printf("no remove callback registered for %s\n", argv[i]);
 			ret = COMMAND_ERROR;
 			continue;
 		}
 
-		dev->bus->remove(dev);
 		dev->driver = NULL;
 		list_del(&dev->active);
 	}
diff --git a/drivers/base/driver.c b/drivers/base/driver.c
index 6548aec9b27b..8bd187eef57f 100644
--- a/drivers/base/driver.c
+++ b/drivers/base/driver.c
@@ -280,7 +280,7 @@ int unregister_device(struct device *old_dev)
 	dev_remove_parameters(old_dev);
 
 	if (old_dev->driver)
-		old_dev->bus->remove(old_dev);
+		device_remove(old_dev);
 
 	list_for_each_entry_safe(alias, at, &device_alias_list, list) {
 		if(alias->dev == old_dev)
@@ -432,7 +432,7 @@ void unregister_driver(struct driver *drv)
 
 	bus_for_each_device(drv->bus, dev) {
 		if (dev->driver == drv) {
-			drv->bus->remove(dev);
+			device_remove(dev);
 			dev->driver = NULL;
 			list_del(&dev->active);
 			INIT_LIST_HEAD(&dev->active);
@@ -641,19 +641,27 @@ int dev_add_alias(struct device *dev, const char *fmt, ...)
 }
 EXPORT_SYMBOL_GPL(dev_add_alias);
 
+bool device_remove(struct device *dev)
+{
+	if (dev->bus && dev->bus->remove)
+		dev->bus->remove(dev);
+	else if (dev->driver->remove)
+		dev->driver->remove(dev);
+	else
+		return false; /* nothing to do */
+
+	return true;
+}
+EXPORT_SYMBOL_GPL(device_remove);
+
 static void devices_shutdown(void)
 {
 	struct device *dev;
-	int depth = 0;
 
 	list_for_each_entry(dev, &active_device_list, active) {
-		if (dev->bus->remove) {
-			depth++;
-			pr_report_probe("%*sremove-> %s\n", depth * 4, "", dev_name(dev));
-			dev->bus->remove(dev);
-			dev->driver = NULL;
-			depth--;
-		}
+		if (device_remove(dev))
+			pr_report_probe("%*sremove-> %s\n", 1 * 4, "", dev_name(dev));
+		dev->driver = NULL;
 	}
 }
 devshutdown_exitcall(devices_shutdown);
diff --git a/include/driver.h b/include/driver.h
index 638426b9606a..b7c950620bca 100644
--- a/include/driver.h
+++ b/include/driver.h
@@ -166,6 +166,16 @@ int register_device(struct device *);
  */
 int device_probe(struct device *dev);
 
+/**
+ * device_remove - Remove a device from its bus and driver
+ *
+ * @dev: Device
+ *
+ * Returns true if there was any bus or driver specific removal
+ * code that was executed and false if the function was a no-op.
+ */
+bool device_remove(struct device *dev);
+
 /* detect devices attached to this device (cards, disks,...) */
 int device_detect(struct device *dev);
 int device_detect_by_name(const char *devname);
-- 
2.39.2




^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/3] drivers: drop simple bus remove in favor of common implementation
  2024-02-15 10:33 [PATCH 1/3] driver: make CONFIG_DEBUG_PROBES more useful for removal Ahmad Fatoum
@ 2024-02-15 10:33 ` Ahmad Fatoum
  2024-02-15 10:33 ` [PATCH 3/3] firmware: arm_scmi: call device driver remove if defined Ahmad Fatoum
  2024-02-16 11:59 ` [PATCH 1/3] driver: make CONFIG_DEBUG_PROBES more useful for removal Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2024-02-15 10:33 UTC (permalink / raw)
  To: barebox; +Cc: ske, Ahmad Fatoum

Now that driver core will call dev->driver->remove if dev->bus->remove
is NULL, we cann remove all bus_type::remove functions that do the same.

This has the welcome side effect that devices that device_remove will
return false when called on a device the neither has a bus remove or
driver remove function and thus we can skip tracing these calls when
CONFIG_DEBUG_PROBES is enabled.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/base/platform.c | 7 -------
 drivers/bus/acpi.c      | 7 -------
 drivers/i2c/i2c.c       | 7 -------
 drivers/spi/spi.c       | 7 -------
 drivers/tee/tee_core.c  | 7 -------
 5 files changed, 35 deletions(-)

diff --git a/drivers/base/platform.c b/drivers/base/platform.c
index f9954785608b..ac7c473c8c7b 100644
--- a/drivers/base/platform.c
+++ b/drivers/base/platform.c
@@ -22,12 +22,6 @@ static int platform_probe(struct device *dev)
 	return dev->driver->probe(dev);
 }
 
-static void platform_remove(struct device *dev)
-{
-	if (dev->driver->remove)
-		dev->driver->remove(dev);
-}
-
 int platform_driver_register(struct driver *drv)
 {
 	drv->bus = &platform_bus;
@@ -46,7 +40,6 @@ struct bus_type platform_bus = {
 	.name = "platform",
 	.match = device_match,
 	.probe = platform_probe,
-	.remove = platform_remove,
 };
 
 static int platform_init(void)
diff --git a/drivers/bus/acpi.c b/drivers/bus/acpi.c
index ac034cce4569..cf85e886d0a2 100644
--- a/drivers/bus/acpi.c
+++ b/drivers/bus/acpi.c
@@ -210,17 +210,10 @@ static int acpi_bus_probe(struct device *dev)
 	return dev->driver->probe(dev);
 }
 
-static void acpi_bus_remove(struct device *dev)
-{
-	if (dev->driver->remove)
-		dev->driver->remove(dev);
-}
-
 struct bus_type acpi_bus = {
 	.name = "acpi",
 	.match = acpi_bus_match,
 	.probe = acpi_bus_probe,
-	.remove = acpi_bus_remove,
 };
 
 static int efi_acpi_probe(void)
diff --git a/drivers/i2c/i2c.c b/drivers/i2c/i2c.c
index 70d1b810c1c3..d518bf2c9dcf 100644
--- a/drivers/i2c/i2c.c
+++ b/drivers/i2c/i2c.c
@@ -748,17 +748,10 @@ static int i2c_probe(struct device *dev)
 	return dev->driver->probe(dev);
 }
 
-static void i2c_remove(struct device *dev)
-{
-	if (dev->driver->remove)
-		dev->driver->remove(dev);
-}
-
 struct bus_type i2c_bus = {
 	.name = "i2c",
 	.match = device_match_of_modalias,
 	.probe = i2c_probe,
-	.remove = i2c_remove,
 };
 
 static int i2c_bus_init(void)
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 83f2b0ee1b2e..fd9c8da17100 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -417,17 +417,10 @@ static int spi_probe(struct device *dev)
 	return dev->driver->probe(dev);
 }
 
-static void spi_remove(struct device *dev)
-{
-	if (dev->driver->remove)
-		dev->driver->remove(dev);
-}
-
 struct bus_type spi_bus = {
 	.name = "spi",
 	.match = device_match_of_modalias,
 	.probe = spi_probe,
-	.remove = spi_remove,
 };
 
 static int spi_bus_init(void)
diff --git a/drivers/tee/tee_core.c b/drivers/tee/tee_core.c
index 0bf645a310eb..36c6210b8b1d 100644
--- a/drivers/tee/tee_core.c
+++ b/drivers/tee/tee_core.c
@@ -775,17 +775,10 @@ static int tee_bus_probe(struct device *dev)
 	return dev->driver->probe(dev);
 }
 
-static void tee_bus_remove(struct device *dev)
-{
-	if (dev->driver->remove)
-		dev->driver->remove(dev);
-}
-
 struct bus_type tee_bus_type = {
 	.name		= "tee",
 	.match		= tee_client_device_match,
 	.probe		= tee_bus_probe,
-	.remove		= tee_bus_remove,
 };
 EXPORT_SYMBOL_GPL(tee_bus_type);
 
-- 
2.39.2




^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 3/3] firmware: arm_scmi: call device driver remove if defined
  2024-02-15 10:33 [PATCH 1/3] driver: make CONFIG_DEBUG_PROBES more useful for removal Ahmad Fatoum
  2024-02-15 10:33 ` [PATCH 2/3] drivers: drop simple bus remove in favor of common implementation Ahmad Fatoum
@ 2024-02-15 10:33 ` Ahmad Fatoum
  2024-02-16 11:59 ` [PATCH 1/3] driver: make CONFIG_DEBUG_PROBES more useful for removal Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2024-02-15 10:33 UTC (permalink / raw)
  To: barebox; +Cc: ske, Ahmad Fatoum

We don't have SCMI driver remove callbacks like Linux does, so
scmi_bus_type.remove is just an empty function. As no SCMI driver in
barebox populates its remove callback, we can just replace this empty
implementation by the default one of calling driver::remove.

This has the added benefit that the SCMI devices won't be printed on
shutdown when CONFIG_DEBUG_PROBES=y is enabled.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/firmware/arm_scmi/bus.c | 5 -----
 1 file changed, 5 deletions(-)

diff --git a/drivers/firmware/arm_scmi/bus.c b/drivers/firmware/arm_scmi/bus.c
index 5a80911119e0..1d9a0f089b44 100644
--- a/drivers/firmware/arm_scmi/bus.c
+++ b/drivers/firmware/arm_scmi/bus.c
@@ -206,15 +206,10 @@ static int scmi_dev_probe(struct device *dev)
 	return scmi_drv->probe(scmi_dev);
 }
 
-static void scmi_dev_remove(struct device *dev)
-{
-}
-
 struct bus_type scmi_bus_type = {
 	.name =	"scmi_protocol",
 	.match = scmi_dev_match,
 	.probe = scmi_dev_probe,
-	.remove = scmi_dev_remove,
 };
 EXPORT_SYMBOL_GPL(scmi_bus_type);
 
-- 
2.39.2




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/3] driver: make CONFIG_DEBUG_PROBES more useful for removal
  2024-02-15 10:33 [PATCH 1/3] driver: make CONFIG_DEBUG_PROBES more useful for removal Ahmad Fatoum
  2024-02-15 10:33 ` [PATCH 2/3] drivers: drop simple bus remove in favor of common implementation Ahmad Fatoum
  2024-02-15 10:33 ` [PATCH 3/3] firmware: arm_scmi: call device driver remove if defined Ahmad Fatoum
@ 2024-02-16 11:59 ` Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Sascha Hauer @ 2024-02-16 11:59 UTC (permalink / raw)
  To: barebox, Ahmad Fatoum; +Cc: ske


On Thu, 15 Feb 2024 11:33:51 +0100, Ahmad Fatoum wrote:
> When CONFIG_DEBUG_PROBES is enabled, barebox will print a message on
> every device probe and removal. Unfortunately, the removal prints are not
> very useful, because the removal happens in the bus remove function,
> which is often a no-op, but that's not known to driver core.
> 
> To make this a bit more useful, let's allow skipping bus remove
> functions like Linux does and only print that a remove is happening if
> either a bus or driver remove function is available.
> 
> [...]

Applied, thanks!

[1/3] driver: make CONFIG_DEBUG_PROBES more useful for removal
      https://git.pengutronix.de/cgit/barebox/commit/?id=4d515afe56eb (link may not be stable)
[2/3] drivers: drop simple bus remove in favor of common implementation
      https://git.pengutronix.de/cgit/barebox/commit/?id=e8ec515026fd (link may not be stable)
[3/3] firmware: arm_scmi: call device driver remove if defined
      https://git.pengutronix.de/cgit/barebox/commit/?id=59aab0bebb1b (link may not be stable)

Best regards,
-- 
Sascha Hauer <s.hauer@pengutronix.de>




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-02-16 12:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-15 10:33 [PATCH 1/3] driver: make CONFIG_DEBUG_PROBES more useful for removal Ahmad Fatoum
2024-02-15 10:33 ` [PATCH 2/3] drivers: drop simple bus remove in favor of common implementation Ahmad Fatoum
2024-02-15 10:33 ` [PATCH 3/3] firmware: arm_scmi: call device driver remove if defined Ahmad Fatoum
2024-02-16 11:59 ` [PATCH 1/3] driver: make CONFIG_DEBUG_PROBES more useful for removal Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox