[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <20230216121621.37063-1-sergey.temerkhanov@intel.com>
Date: Thu, 16 Feb 2023 13:16:21 +0100
From: "Temerkhanov, Sergey" <sergey.temerkhanov@...el.com>
To: netdev@...r.kernel.org
Cc: Sergey Temerkhanov <sergey.temerkhanov@...el.com>
Subject: [PATCH net-next] auxiliary: Implement refcounting
From: Sergey Temerkhanov <sergey.temerkhanov@...el.com>
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.
Signed-off-by: Sergey Temerkhanov <sergey.temerkhanov@...el.com>
---
drivers/base/auxiliary.c | 18 ++++++++++++++++++
include/linux/auxiliary_bus.h | 34 +++++++++++++++++++++++++---------
2 files changed, 43 insertions(+), 9 deletions(-)
diff --git a/drivers/base/auxiliary.c b/drivers/base/auxiliary.c
index 8c5e65930617..082b3ebd143d 100644
--- a/drivers/base/auxiliary.c
+++ b/drivers/base/auxiliary.c
@@ -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
diff --git a/include/linux/auxiliary_bus.h b/include/linux/auxiliary_bus.h
index de21d9d24a95..0610ccee320e 100644
--- a/include/linux/auxiliary_bus.h
+++ b/include/linux/auxiliary_bus.h
@@ -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);
--
2.35.3
Powered by blists - more mailing lists