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
201 * @state: state of fpga manager201 * @state: state of fpga manager
202 * @compat_id: FPGA manager id for compatibility check.202 * @compat_id: FPGA manager id for compatibility check.
203 * @mops: pointer to struct of fpga manager ops203 * @mops: pointer to struct of fpga manager ops
204 * @mops_owner: module containing the mops
204 * @priv: low level driver private date205 * @priv: low level driver private date
205 */206 */
206struct fpga_manager {207struct fpga_manager {
@@ -210,6 +211,7 @@ struct fpga_manager { · 3 unmodified lines
210 enum fpga_mgr_states state;211 enum fpga_mgr_states state;
211 struct fpga_compat_id *compat_id;212 struct fpga_compat_id *compat_id;
212 const struct fpga_manager_ops *mops;213 const struct fpga_manager_ops *mops;
214 struct module *mops_owner;
213 void *priv;215 void *priv;
214};216};
215 217
@@ -230,18 +232,30 @@ struct fpga_manager *fpga_mgr_get(struct device *dev); · 14 unmodified lines
230 232
231void fpga_mgr_put(struct fpga_manager *mgr);233void fpga_mgr_put(struct fpga_manager *mgr);
232 234
235#define fpga_mgr_register_full(parent, info) \
236 __fpga_mgr_register_full(parent, info, THIS_MODULE)
233struct fpga_manager *237struct fpga_manager *
234fpga_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);
235 240
241#define fpga_mgr_register(parent, name, mops, priv) \
242 __fpga_mgr_register(parent, name, mops, priv, THIS_MODULE)
236struct fpga_manager *243struct fpga_manager *
237fpga_mgr_register(struct device *parent, const char *name,244__fpga_mgr_register(struct device *parent, const char *name,
238 const struct fpga_manager_ops *mops, void *priv);245 const struct fpga_manager_ops *mops, void *priv, struct module *owner);
246
239void fpga_mgr_unregister(struct fpga_manager *mgr);247void fpga_mgr_unregister(struct fpga_manager *mgr);
240 248
249#define devm_fpga_mgr_register_full(parent, info) \
250 __devm_fpga_mgr_register_full(parent, info, THIS_MODULE)
241struct fpga_manager *251struct fpga_manager *
242devm_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)
243struct fpga_manager *256struct fpga_manager *
244devm_fpga_mgr_register(struct device *parent, const char *name,257__devm_fpga_mgr_register(struct device *parent, const char *name,
245 const struct fpga_manager_ops *mops, void *priv);258 const struct fpga_manager_ops *mops, void *priv,
259 struct module *owner);
246 260
247#endif /*_LINUX_FPGA_MGR_H */261#endif /*_LINUX_FPGA_MGR_H */