[RFC,1/2] fpga: add a module owner field to fpga_manager and fpga_manager_ops
Commit Message
Add a module *owner field to the fpga_manager_ops and fpga_manager
structs to protect the fpga manager against the unloading of the
low-level control module while someone is holding a reference to the
manager device. Low-level control modules should statically set the
owner field of the fpga_manager_ops struct to THIS_MODULE. Then, when
the manager is registered using fpga_mgr_register(), the value is copied
into the owner field of the fpga_manager struct (that contains the
device context). In this way, the manager can later use it in
fpga_mgr_get() to take the low-level module's refcount. To prevent races
while unloading the low-level control module, the fpga_mgr_get() and
fpga_mgr_put() methods are protected with a mutex.
Other changes: moved put_device() form __fpga_mgr_get() to
fpga_mgr_get() and of_fpga_mgr_get() to improve code clarity.
Fixes: 654ba4cc0f3e ("fpga manager: ensure lifetime with of_fpga_mgr_get")
Signed-off-by: Marco Pagani <marpagan@redhat.com>
---
drivers/fpga/fpga-mgr.c | 56 +++++++++++++++++++++++++----------
include/linux/fpga/fpga-mgr.h | 4 +++
2 files changed, 44 insertions(+), 16 deletions(-)
@@ -21,6 +21,8 @@
static DEFINE_IDA(fpga_mgr_ida);
static const struct class fpga_mgr_class;
+static DEFINE_MUTEX(mgr_lock);
+
struct fpga_mgr_devres {
struct fpga_manager *mgr;
};
@@ -667,17 +669,15 @@ ATTRIBUTE_GROUPS(fpga_mgr);
static struct fpga_manager *__fpga_mgr_get(struct device *dev)
{
struct fpga_manager *mgr;
+ struct module *owner;
mgr = to_fpga_manager(dev);
+ owner = mgr->owner;
- if (!try_module_get(dev->parent->driver->owner))
- goto err_dev;
+ if (owner && !try_module_get(owner))
+ mgr = ERR_PTR(-ENODEV);
return mgr;
-
-err_dev:
- put_device(dev);
- return ERR_PTR(-ENODEV);
}
static int fpga_mgr_dev_match(struct device *dev, const void *data)
@@ -693,12 +693,22 @@ static int fpga_mgr_dev_match(struct device *dev, const void *data)
*/
struct fpga_manager *fpga_mgr_get(struct device *dev)
{
- struct device *mgr_dev = class_find_device(&fpga_mgr_class, NULL, dev,
- fpga_mgr_dev_match);
+ struct fpga_manager *mgr = ERR_PTR(-ENODEV);
+ struct device *mgr_dev;
+
+ mutex_lock(&mgr_lock);
+
+ mgr_dev = class_find_device(&fpga_mgr_class, NULL, dev, fpga_mgr_dev_match);
if (!mgr_dev)
- return ERR_PTR(-ENODEV);
+ goto out;
+
+ mgr = __fpga_mgr_get(mgr_dev);
+ if (IS_ERR(mgr))
+ put_device(mgr_dev);
- return __fpga_mgr_get(mgr_dev);
+out:
+ mutex_unlock(&mgr_lock);
+ return mgr;
}
EXPORT_SYMBOL_GPL(fpga_mgr_get);
@@ -711,13 +721,22 @@ EXPORT_SYMBOL_GPL(fpga_mgr_get);
*/
struct fpga_manager *of_fpga_mgr_get(struct device_node *node)
{
- struct device *dev;
+ struct fpga_manager *mgr = ERR_PTR(-ENODEV);
+ struct device *mgr_dev;
+
+ mutex_lock(&mgr_lock);
+
+ mgr_dev = class_find_device_by_of_node(&fpga_mgr_class, node);
+ if (!mgr_dev)
+ goto out;
- dev = class_find_device_by_of_node(&fpga_mgr_class, node);
- if (!dev)
- return ERR_PTR(-ENODEV);
+ mgr = __fpga_mgr_get(mgr_dev);
+ if (IS_ERR(mgr))
+ put_device(mgr_dev);
- return __fpga_mgr_get(dev);
+out:
+ mutex_unlock(&mgr_lock);
+ return mgr;
}
EXPORT_SYMBOL_GPL(of_fpga_mgr_get);
@@ -727,8 +746,12 @@ EXPORT_SYMBOL_GPL(of_fpga_mgr_get);
*/
void fpga_mgr_put(struct fpga_manager *mgr)
{
- module_put(mgr->dev.parent->driver->owner);
+ mutex_lock(&mgr_lock);
+
+ module_put(mgr->owner);
put_device(&mgr->dev);
+
+ mutex_unlock(&mgr_lock);
}
EXPORT_SYMBOL_GPL(fpga_mgr_put);
@@ -806,6 +829,7 @@ fpga_mgr_register_full(struct device *parent, const struct fpga_manager_info *in
mgr->name = info->name;
mgr->mops = info->mops;
+ mgr->owner = info->mops->owner;
mgr->priv = info->priv;
mgr->compat_id = info->compat_id;
@@ -162,6 +162,7 @@ struct fpga_manager_info {
* @write_complete: set FPGA to operating state after writing is done
* @fpga_remove: optional: Set FPGA into a specific state during driver remove
* @groups: optional attribute groups.
+ * @owner: owner module.
*
* fpga_manager_ops are the low level functions implemented by a specific
* fpga manager driver. The optional ones are tested for NULL before being
@@ -184,6 +185,7 @@ struct fpga_manager_ops {
struct fpga_image_info *info);
void (*fpga_remove)(struct fpga_manager *mgr);
const struct attribute_group **groups;
+ struct module *owner;
};
/* FPGA manager status: Partial/Full Reconfiguration errors */
@@ -201,6 +203,7 @@ struct fpga_manager_ops {
* @state: state of fpga manager
* @compat_id: FPGA manager id for compatibility check.
* @mops: pointer to struct of fpga manager ops
+ * @owner: owner module.
* @priv: low level driver private date
*/
struct fpga_manager {
@@ -210,6 +213,7 @@ struct fpga_manager {
enum fpga_mgr_states state;
struct fpga_compat_id *compat_id;
const struct fpga_manager_ops *mops;
+ struct module *owner;
void *priv;
};