History

fpga: manager: add owner module and take its refcount

The current implementation of the fpga manager assumes that the low-level module registers a driver for the parent device and uses its owner pointer to take the module's refcount. This approach is problematic since it can lead to a null pointer dereference while attempting to get the manager if the parent device does not have a driver. To address this problem, add a module owner pointer to the fpga_manager struct and use it to take the module's refcount. Modify the functions for registering the manager to take an additional owner module parameter and rename them to avoid conflicts. Use the old function names for helper macros that automatically set the module that registers the manager as the owner. This ensures compatibility with existing low-level control modules and reduces the chances of registering a manager without setting the owner. Also, update the documentation to keep it consistent with the new interface for registering an fpga manager. Other changes: opportunistically move put_device() from __fpga_mgr_get() to fpga_mgr_get() and of_fpga_mgr_get() to improve code clarity since the manager device is taken in these functions.

Marco Pagani committed 2 years ago

Commit 4d4d2d41 parent 4cece763 files · +89 −53Browse files

Showing the changes to include/linux/fpga (1 of 3 files). Show all

Unified · Split

include/linux/fpga/fpga-mgr.h+20−6
@@ -201,6 +201,7 @@ struct fpga_manager_ops { · 200 unmodified lines
201201 * @state: state of fpga manager
202202 * @compat_id: FPGA manager id for compatibility check.
203203 * @mops: pointer to struct of fpga manager ops
204+ * @mops_owner: module containing the mops
204205 * @priv: low level driver private date
205206 */
206207 struct fpga_manager {
@@ -210,6 +211,7 @@ struct fpga_manager { · 3 unmodified lines
210211 enum fpga_mgr_states state;
211212 struct fpga_compat_id *compat_id;
212213 const struct fpga_manager_ops *mops;
214+ struct module *mops_owner;
213215 void *priv;
214216 };
215217
@@ -230,18 +232,30 @@ struct fpga_manager *fpga_mgr_get(struct device *dev); · 14 unmodified lines
230232
231233 void fpga_mgr_put(struct fpga_manager *mgr);
232234
235+#define fpga_mgr_register_full(parent, info) \
236+ __fpga_mgr_register_full(parent, info, THIS_MODULE)
233237 struct fpga_manager *
234−fpga_mgr_register_full(struct device *parent, const struct fpga_manager_info *info);
238+__fpga_mgr_register_full(struct device *parent, const struct fpga_manager_info *info,
239+ struct module *owner);
235240
241+#define fpga_mgr_register(parent, name, mops, priv) \
242+ __fpga_mgr_register(parent, name, mops, priv, THIS_MODULE)
236243 struct fpga_manager *
237−fpga_mgr_register(struct device *parent, const char *name,
238− const struct fpga_manager_ops *mops, void *priv);
244+__fpga_mgr_register(struct device *parent, const char *name,
245+ const struct fpga_manager_ops *mops, void *priv, struct module *owner);
246+
239247 void fpga_mgr_unregister(struct fpga_manager *mgr);
240248
249+#define devm_fpga_mgr_register_full(parent, info) \
250+ __devm_fpga_mgr_register_full(parent, info, THIS_MODULE)
241251 struct fpga_manager *
242−devm_fpga_mgr_register_full(struct device *parent, const struct fpga_manager_info *info);
252+__devm_fpga_mgr_register_full(struct device *parent, const struct fpga_manager_info *info,
253+ struct module *owner);
254+#define devm_fpga_mgr_register(parent, name, mops, priv) \
255+ __devm_fpga_mgr_register(parent, name, mops, priv, THIS_MODULE)
243256 struct fpga_manager *
244−devm_fpga_mgr_register(struct device *parent, const char *name,
245− const struct fpga_manager_ops *mops, void *priv);
257+__devm_fpga_mgr_register(struct device *parent, const char *name,
258+ const struct fpga_manager_ops *mops, void *priv,
259+ struct module *owner);
246260
247261 #endif /*_LINUX_FPGA_MGR_H */