Skip to contentSkip to conversation

auxiliary: Implement refcounting

PATCH In review Temerkhanov, Sergey 3 participants 4 messages Feb 16, 2023

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

PATCH 1/1 auxiliary: Implement refcounting

From: Sergey Temerkhanov <[email protected]>

Implement reference counting to make it possible to synchronize deinitialization and removal of interfaces published via aux bus with the client modules.
Reference counting can be used in both sleeping and non-sleeping contexts so this approach is intended to replace device_lock() (mutex acquisition) with an additional lock on top of it which is not always possible to take in client code.

---
 drivers/base/auxiliary.c      | 18 ++++++++++++++++++
 include/linux/auxiliary_bus.h | 34 +++++++++++++++++++++++++---------
 2 files changed, 43 insertions(+), 9 deletions(-)
drivers/base/auxiliary.c +18 -0
index 8c5e65930617..082b3ebd143d 100644
@@ -287,10 +287,28 @@ int auxiliary_device_init(struct auxiliary_device *auxdev)
 
 	dev->bus = &auxiliary_bus_type;
 	device_initialize(&auxdev->dev);
+	init_waitqueue_head(&auxdev->wq_head);
+	refcount_set(&auxdev->refcnt, 1);
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(auxiliary_device_init);
 
+void auxiliary_device_uninit(struct auxiliary_device *auxdev)
+{
+	wait_event_interruptible(auxdev->wq_head,
+				 refcount_dec_if_one(&auxdev->refcnt));
+}
+EXPORT_SYMBOL_GPL(auxiliary_device_uninit);
+
+void auxiliary_device_delete(struct auxiliary_device *auxdev)
+{
+	WARN_ON(refcount_read(&auxdev->refcnt));
+
+	device_del(&auxdev->dev);
+}
+EXPORT_SYMBOL_GPL(auxiliary_device_delete);
+
 /**
  * __auxiliary_device_add - add an auxiliary bus device
  * @auxdev: auxiliary bus device to add to the bus
include/linux/auxiliary_bus.h +25 -9
index de21d9d24a95..0610ccee320e 100644
@@ -10,6 +10,8 @@
 
 #include <linux/device.h>
 #include <linux/mod_devicetable.h>
+#include <linux/wait.h>
+#include <linux/refcount.h>
 
 /**
  * DOC: DEVICE_LIFESPAN
@@ -137,7 +139,9 @@
  */
 struct auxiliary_device {
 	struct device dev;
+	refcount_t refcnt;
 	const char *name;
+	struct wait_queue_head wq_head;
 	u32 id;
 };
 
@@ -198,6 +202,25 @@ static inline void auxiliary_set_drvdata(struct auxiliary_device *auxdev, void *
 	dev_set_drvdata(&auxdev->dev, data);
 }
 
+static inline bool __must_check
+auxiliary_device_get(struct auxiliary_device *adev)
+{
+	if (!adev)
+		return false;
+
+	return refcount_inc_not_zero(&adev->refcnt);
+}
+
+static inline void auxiliary_device_put(struct auxiliary_device *adev)
+{
+	if (!adev)
+		return;
+
+	refcount_dec(&adev->refcnt);
+
+	wake_up_interruptible(&adev->wq_head);
+}
+
 static inline struct auxiliary_device *to_auxiliary_dev(struct device *dev)
 {
 	return container_of(dev, struct auxiliary_device, dev);
@@ -212,15 +235,8 @@ int auxiliary_device_init(struct auxiliary_device *auxdev);
 int __auxiliary_device_add(struct auxiliary_device *auxdev, const char *modname);
 #define auxiliary_device_add(auxdev) __auxiliary_device_add(auxdev, KBUILD_MODNAME)
 
-static inline void auxiliary_device_uninit(struct auxiliary_device *auxdev)
-{
-	put_device(&auxdev->dev);
-}
-
-static inline void auxiliary_device_delete(struct auxiliary_device *auxdev)
-{
-	device_del(&auxdev->dev);
-}
+void auxiliary_device_uninit(struct auxiliary_device *auxdev);
+void auxiliary_device_delete(struct auxiliary_device *auxdev);
 
 int __auxiliary_driver_register(struct auxiliary_driver *auxdrv, struct module *owner,
 				const char *modname);
Signature · 1 line
2.35.3
#1 lore raw

+ Greg KH

Quoted 9 lines · Sergey

From: Sergey Temerkhanov <[email protected]>

Implement reference counting to make it possible to synchronize deinitialization and removal of interfaces published via aux bus with the client modules.
Reference counting can be used in both sleeping and non-sleeping contexts so this approach is intended to replace device_lock() (mutex acquisition) with an additional lock on top of it which is not always possible to take in client code.

I want to see this patch as part of your client code series. It is unclear why same aux device is used from different drivers.

Otherwise, this whole refcnt is useless and just hides bugs in driver which accesses released devices.

Quoted 64 lines
---
 drivers/base/auxiliary.c      | 18 ++++++++++++++++++
 include/linux/auxiliary_bus.h | 34 +++++++++++++++++++++++++---------
 2 files changed, 43 insertions(+), 9 deletions(-)
drivers/base/auxiliary.c +18 -0
index 8c5e65930617..082b3ebd143d 100644
@@ -287,10 +287,28 @@ int auxiliary_device_init(struct auxiliary_device *auxdev)
 
 	dev->bus = &auxiliary_bus_type;
 	device_initialize(&auxdev->dev);
+	init_waitqueue_head(&auxdev->wq_head);
+	refcount_set(&auxdev->refcnt, 1);
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(auxiliary_device_init);
 
+void auxiliary_device_uninit(struct auxiliary_device *auxdev)
+{
+	wait_event_interruptible(auxdev->wq_head,
+				 refcount_dec_if_one(&auxdev->refcnt));
+}
+EXPORT_SYMBOL_GPL(auxiliary_device_uninit);
+
+void auxiliary_device_delete(struct auxiliary_device *auxdev)
+{
+	WARN_ON(refcount_read(&auxdev->refcnt));
+
+	device_del(&auxdev->dev);
+}
+EXPORT_SYMBOL_GPL(auxiliary_device_delete);
+
 /**
  * __auxiliary_device_add - add an auxiliary bus device
  * @auxdev: auxiliary bus device to add to the bus
include/linux/auxiliary_bus.h +4 -0
index de21d9d24a95..0610ccee320e 100644
@@ -10,6 +10,8 @@
 
 #include <linux/device.h>
 #include <linux/mod_devicetable.h>
+#include <linux/wait.h>
+#include <linux/refcount.h>
 
 /**
  * DOC: DEVICE_LIFESPAN
@@ -137,7 +139,9 @@
  */
 struct auxiliary_device {
 	struct device dev;
+	refcount_t refcnt;
 	const char *name;
+	struct wait_queue_head wq_head;
 	u32 id;
 };
 

include/linux/auxiliary_bus.h static inline void auxiliary_set_drvdata(struct auxiliary_device *auxdev, void * · line 209

Quoted 8 lines
include/linux/auxiliary_bus.h +5 -0
@@ -198,6 +202,25 @@ static inline void auxiliary_set_drvdata(struct auxiliary_device *auxdev, void *
 	dev_set_drvdata(&auxdev->dev, data);
 }
 
+static inline bool __must_check
+auxiliary_device_get(struct auxiliary_device *adev)
+{
+	if (!adev)
+		return false;

Please don't check for validity of adev, it is caller's job.

include/linux/auxiliary_bus.h static inline void auxiliary_set_drvdata(struct auxiliary_device *auxdev, void * · line 217

Quoted 8 lines
include/linux/auxiliary_bus.h +8 -0
+
+	return refcount_inc_not_zero(&adev->refcnt);
+}
+
+static inline void auxiliary_device_put(struct auxiliary_device *adev)
+{
+	if (!adev)
+		return;

Same.

Thanks

#2 lore raw
Quoted 41 lines · Leon Romanovsky

+ Greg KH

Quoted 9 lines · Sergey

From: Sergey Temerkhanov <[email protected]>

Implement reference counting to make it possible to synchronize deinitialization and removal of interfaces published via aux bus with the client modules.
Reference counting can be used in both sleeping and non-sleeping contexts so this approach is intended to replace device_lock() (mutex acquisition) with an additional lock on top of it which is not always possible to take in client code.

I want to see this patch as part of your client code series. It is unclear why same aux device is used from different drivers.

Otherwise, this whole refcnt is useless and just hides bugs in driver which accesses released devices.

Quoted 22 lines
---
 drivers/base/auxiliary.c      | 18 ++++++++++++++++++
 include/linux/auxiliary_bus.h | 34 +++++++++++++++++++++++++---------
 2 files changed, 43 insertions(+), 9 deletions(-)
drivers/base/auxiliary.c +3 -0
index 8c5e65930617..082b3ebd143d 100644
@@ -287,10 +287,28 @@ int auxiliary_device_init(struct auxiliary_device *auxdev)
 
 	dev->bus = &auxiliary_bus_type;
 	device_initialize(&auxdev->dev);
+	init_waitqueue_head(&auxdev->wq_head);
+	refcount_set(&auxdev->refcnt, 1);
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(auxiliary_device_init);
 
Quoted 10 lines

drivers/base/auxiliary.c int auxiliary_device_init(struct auxiliary_device *auxdev) · line 306

Quoted 10 lines
drivers/base/auxiliary.c +10 -0
+void auxiliary_device_uninit(struct auxiliary_device *auxdev)
+{
+	wait_event_interruptible(auxdev->wq_head,
+				 refcount_dec_if_one(&auxdev->refcnt));
+}
+EXPORT_SYMBOL_GPL(auxiliary_device_uninit);
+
+void auxiliary_device_delete(struct auxiliary_device *auxdev)
+{
+	WARN_ON(refcount_read(&auxdev->refcnt));

There are 3 things wrong with this single line of code, pretty impressive!

First off, never use WARN_ON unless you want to reboot people's machines. Handle the issue if it can really happen and keep on moving. Don't just throw up your hands and yell at userspace and stop the box (remember panic-on-warn is real.)

Second, if this is a real problem, HANDLE IT! Don't just free memory as obviously someone actually has the memory in use!

Third, NEVER read the reference count and do something based on it. That's not how reference counts work at all, unless you are using the correct reference count function (hint, this is not it.)

Quoted 2 lines

drivers/base/auxiliary.c int auxiliary_device_init(struct auxiliary_device *auxdev) · line 308

Quoted 2 lines
drivers/base/auxiliary.c +2 -0
+
+	device_del(&auxdev->dev);

No, no no no no .

I see why you didn't cc me on this thread to start with, you were trying to get this by without review, that's not ok.

You now have the required penance of having to get an internal-Intel review and signed-off-by FIRST before sending this patch series out again.

greg k-h

#3 lore raw
In reply to Leon Romanovsky

Oh, one more fun thing:

Quoted 7 lines · Leon Romanovsky

include/linux/auxiliary_bus.h line 145

Quoted 6 lines · Sergey

struct auxiliary_device {

include/linux/auxiliary_bus.h +2 -0
 	struct device dev;
+	refcount_t refcnt;
 	const char *name;
+	struct wait_queue_head wq_head;
 	u32 id;

Sergey, you are adding an additional reference count to a structure that is already properly reference counted! So you are imposing 2 different counts and lifecycles on a single tiny structure? How is that even supposed to work?

Again, please get reviews from internal Intel developers FIRST as is required by all Intel kernel developers touching code like this. You know this, it is part of your internal development rules, please do not try to ignore them, that's a sure way to get everyone to just ignore your changes going forward.

thanks,

greg k-h