All of lore.kernel.org
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	"Rafael J. Wysocki" <rafael@kernel.org>
Subject: [PATCH 06/21] driver core: bus: bus_add/probe/remove_device() cleanups
Date: Wed,  8 Feb 2023 12:13:15 +0100	[thread overview]
Message-ID: <20230208111330.439504-7-gregkh@linuxfoundation.org> (raw)
In-Reply-To: <20230208111330.439504-1-gregkh@linuxfoundation.org>

Convert the bus_add_device(), bus_probe_device(), and
bus_remove_device() functions to use bus_to_subsys() and not use the
back-pointer to the private structure.

Cc: "Rafael J. Wysocki" <rafael@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 drivers/base/bus.c | 87 ++++++++++++++++++++++++++++------------------
 1 file changed, 54 insertions(+), 33 deletions(-)

diff --git a/drivers/base/bus.c b/drivers/base/bus.c
index f0d3ad41fd5e..f4e4efd81b29 100644
--- a/drivers/base/bus.c
+++ b/drivers/base/bus.c
@@ -466,32 +466,46 @@ EXPORT_SYMBOL_GPL(bus_for_each_drv);
  */
 int bus_add_device(struct device *dev)
 {
-	struct bus_type *bus = bus_get(dev->bus);
-	int error = 0;
+	struct subsys_private *sp = bus_to_subsys(dev->bus);
+	int error;
 
-	if (bus) {
-		pr_debug("bus: '%s': add device %s\n", bus->name, dev_name(dev));
-		error = device_add_groups(dev, bus->dev_groups);
-		if (error)
-			goto out_put;
-		error = sysfs_create_link(&bus->p->devices_kset->kobj,
-						&dev->kobj, dev_name(dev));
-		if (error)
-			goto out_groups;
-		error = sysfs_create_link(&dev->kobj,
-				&dev->bus->p->subsys.kobj, "subsystem");
-		if (error)
-			goto out_subsys;
-		klist_add_tail(&dev->p->knode_bus, &bus->p->klist_devices);
+	if (!sp) {
+		/*
+		 * This is a normal operation for many devices that do not
+		 * have a bus assigned to them, just say that all went
+		 * well.
+		 */
+		return 0;
 	}
+
+	/*
+	 * Reference in sp is now incremented and will be dropped when
+	 * the device is removed from the bus
+	 */
+
+	pr_debug("bus: '%s': add device %s\n", sp->bus->name, dev_name(dev));
+
+	error = device_add_groups(dev, sp->bus->dev_groups);
+	if (error)
+		goto out_put;
+
+	error = sysfs_create_link(&sp->devices_kset->kobj, &dev->kobj, dev_name(dev));
+	if (error)
+		goto out_groups;
+
+	error = sysfs_create_link(&dev->kobj, &sp->subsys.kobj, "subsystem");
+	if (error)
+		goto out_subsys;
+
+	klist_add_tail(&dev->p->knode_bus, &sp->klist_devices);
 	return 0;
 
 out_subsys:
-	sysfs_remove_link(&bus->p->devices_kset->kobj, dev_name(dev));
+	sysfs_remove_link(&sp->devices_kset->kobj, dev_name(dev));
 out_groups:
-	device_remove_groups(dev, bus->dev_groups);
+	device_remove_groups(dev, sp->bus->dev_groups);
 out_put:
-	bus_put(dev->bus);
+	subsys_put(sp);
 	return error;
 }
 
@@ -503,20 +517,21 @@ int bus_add_device(struct device *dev)
  */
 void bus_probe_device(struct device *dev)
 {
-	struct bus_type *bus = dev->bus;
+	struct subsys_private *sp = bus_to_subsys(dev->bus);
 	struct subsys_interface *sif;
 
-	if (!bus)
+	if (!sp)
 		return;
 
-	if (bus->p->drivers_autoprobe)
+	if (sp->drivers_autoprobe)
 		device_initial_probe(dev);
 
-	mutex_lock(&bus->p->mutex);
-	list_for_each_entry(sif, &bus->p->interfaces, node)
+	mutex_lock(&sp->mutex);
+	list_for_each_entry(sif, &sp->interfaces, node)
 		if (sif->add_dev)
 			sif->add_dev(dev, sif);
-	mutex_unlock(&bus->p->mutex);
+	mutex_unlock(&sp->mutex);
+	subsys_put(sp);
 }
 
 /**
@@ -531,21 +546,20 @@ void bus_probe_device(struct device *dev)
  */
 void bus_remove_device(struct device *dev)
 {
-	struct bus_type *bus = dev->bus;
+	struct subsys_private *sp = bus_to_subsys(dev->bus);
 	struct subsys_interface *sif;
 
-	if (!bus)
+	if (!sp)
 		return;
 
-	mutex_lock(&bus->p->mutex);
-	list_for_each_entry(sif, &bus->p->interfaces, node)
+	mutex_lock(&sp->mutex);
+	list_for_each_entry(sif, &sp->interfaces, node)
 		if (sif->remove_dev)
 			sif->remove_dev(dev, sif);
-	mutex_unlock(&bus->p->mutex);
+	mutex_unlock(&sp->mutex);
 
 	sysfs_remove_link(&dev->kobj, "subsystem");
-	sysfs_remove_link(&dev->bus->p->devices_kset->kobj,
-			  dev_name(dev));
+	sysfs_remove_link(&sp->devices_kset->kobj, dev_name(dev));
 	device_remove_groups(dev, dev->bus->dev_groups);
 	if (klist_node_attached(&dev->p->knode_bus))
 		klist_del(&dev->p->knode_bus);
@@ -553,7 +567,14 @@ void bus_remove_device(struct device *dev)
 	pr_debug("bus: '%s': remove device %s\n",
 		 dev->bus->name, dev_name(dev));
 	device_release_driver(dev);
-	bus_put(dev->bus);
+
+	/*
+	 * Decrement the reference count twice, once for the bus_to_subsys()
+	 * call in the start of this function, and the second one from the
+	 * reference increment in bus_add_device()
+	 */
+	subsys_put(sp);
+	subsys_put(sp);
 }
 
 static int __must_check add_bind_files(struct device_driver *drv)
-- 
2.39.1


  parent reply	other threads:[~2023-02-08 11:15 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-08 11:13 [PATCH 00/21] driver core: bus: remove private "backpointer" from struct bus_type Greg Kroah-Hartman
2023-02-08 11:13 ` [PATCH 01/21] driver core: add local subsys_get and subsys_put functions Greg Kroah-Hartman
2023-02-08 11:13 ` [PATCH 02/21] driver core: bus: implement bus_get/put() without the private pointer Greg Kroah-Hartman
2023-02-08 11:13 ` [PATCH 03/21] driver core: bus: constantify the bus_find_* functions Greg Kroah-Hartman
2023-02-08 11:13 ` [PATCH 04/21] driver core: bus: convert bus_create/remove_file to be constant Greg Kroah-Hartman
2023-02-08 11:13 ` [PATCH 05/21] driver core: bus: sysfs function cleanups Greg Kroah-Hartman
2023-02-08 11:13 ` Greg Kroah-Hartman [this message]
2023-02-08 11:13 ` [PATCH 07/21] driver core: bus: bus_register/unregister() cleanups Greg Kroah-Hartman
2023-02-08 11:13 ` [PATCH 08/21] driver core: bus: subsys_interface_register/unregister() cleanups Greg Kroah-Hartman
2023-02-08 11:13 ` [PATCH 09/21] driver core: bus: bus_get_kset() cleanup Greg Kroah-Hartman
2023-02-08 11:13 ` [PATCH 10/21] driver core: bus: bus_register/unregister_notifier() cleanups Greg Kroah-Hartman
2023-02-08 11:13 ` [PATCH 11/21] driver core: bus: bus_add/remove_driver() cleanups Greg Kroah-Hartman
2023-02-08 11:13 ` [PATCH 12/21] driver core: bus: bus iterator cleanups Greg Kroah-Hartman
2023-02-21 12:54   ` Geert Uytterhoeven
2023-02-08 11:13 ` [PATCH 13/21] driver core: bus: clean up bus_sort_breadthfirst() Greg Kroah-Hartman
2023-02-08 11:13 ` [PATCH 14/21] driver core: move driver_find() to bus.c Greg Kroah-Hartman
2023-02-08 11:13 ` [PATCH 15/21] driver core: bus: clean up driver_find() Greg Kroah-Hartman
2023-02-08 11:13 ` [PATCH 16/21] driver core: create bus_is_registered() Greg Kroah-Hartman
2023-02-08 11:13 ` [PATCH 17/21] driver core: remove private pointer from struct bus_type Greg Kroah-Hartman
2023-02-08 11:13 ` [PATCH 18/21] driver core: bus: constify bus_register/unregister_notifier() Greg Kroah-Hartman
2023-02-08 11:13 ` [PATCH 19/21] driver core: bus: constify bus_get_kset() Greg Kroah-Hartman
2023-02-08 11:13 ` [PATCH 20/21] driver core: bus: constify some internal functions Greg Kroah-Hartman
2023-02-08 11:13 ` [PATCH 21/21] driver core: bus: constify bus_unregister() Greg Kroah-Hartman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230208111330.439504-7-gregkh@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rafael@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.