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

Unified · Split

3 files
  1. Documentation/driver-api/fpga/fpga-mgr.rst +20 −14
  2. drivers/fpga/fpga-mgr.c +49 −33
  3. include/linux/fpga/fpga-mgr.h +20 −6
Documentation/driver-api/fpga/fpga-mgr.rst+20−14
@@ -24,7 +24,8 @@ How to support a new FPGA device · 23 unmodified lines
2424 --------------------------------
2525
2626 To add another FPGA manager, write a driver that implements a set of ops. The
27−probe function calls fpga_mgr_register() or fpga_mgr_register_full(), such as::
27+probe function calls ``fpga_mgr_register()`` or ``fpga_mgr_register_full()``,
28+such as::
2829
2930 static const struct fpga_manager_ops socfpga_fpga_ops = {
3031 .write_init = socfpga_fpga_ops_configure_init,
@@ -69,10 +70,11 @@ probe function calls fpga_mgr_register() or fpga_mgr_register_full(), such as:: · 38 unmodified lines
6970 }
7071
7172 Alternatively, the probe function could call one of the resource managed
72−register functions, devm_fpga_mgr_register() or devm_fpga_mgr_register_full().
73−When these functions are used, the parameter syntax is the same, but the call
74−to fpga_mgr_unregister() should be removed. In the above example, the
75−socfpga_fpga_remove() function would not be required.
73+register functions, ``devm_fpga_mgr_register()`` or
74+``devm_fpga_mgr_register_full()``. When these functions are used, the
75+parameter syntax is the same, but the call to ``fpga_mgr_unregister()`` should be
76+removed. In the above example, the ``socfpga_fpga_remove()`` function would not be
77+required.
7678
7779 The ops will implement whatever device specific register writes are needed to
7880 do the programming sequence for this particular FPGA. These ops return 0 for
@@ -125,15 +127,19 @@ API for implementing a new FPGA Manager driver · 46 unmodified lines
125127 * struct fpga_manager - the FPGA manager struct
126128 * struct fpga_manager_ops - Low level FPGA manager driver ops
127129 * struct fpga_manager_info - Parameter structure for fpga_mgr_register_full()
128−* fpga_mgr_register_full() - Create and register an FPGA manager using the
130+* __fpga_mgr_register_full() - Create and register an FPGA manager using the
129131 fpga_mgr_info structure to provide the full flexibility of options
130−* fpga_mgr_register() - Create and register an FPGA manager using standard
132+* __fpga_mgr_register() - Create and register an FPGA manager using standard
131133 arguments
132−* devm_fpga_mgr_register_full() - Resource managed version of
133− fpga_mgr_register_full()
134−* devm_fpga_mgr_register() - Resource managed version of fpga_mgr_register()
134+* __devm_fpga_mgr_register_full() - Resource managed version of
135+ __fpga_mgr_register_full()
136+* __devm_fpga_mgr_register() - Resource managed version of __fpga_mgr_register()
135137 * fpga_mgr_unregister() - Unregister an FPGA manager
136138
139+Helper macros ``fpga_mgr_register_full()``, ``fpga_mgr_register()``,
140+``devm_fpga_mgr_register_full()``, and ``devm_fpga_mgr_register()`` are available
141+to ease the registration.
142+
137143 .. kernel-doc:: include/linux/fpga/fpga-mgr.h
138144 :functions: fpga_mgr_states
139145
@@ -147,16 +153,16 @@ API for implementing a new FPGA Manager driver · 7 unmodified lines
147153 :functions: fpga_manager_info
148154
149155 .. kernel-doc:: drivers/fpga/fpga-mgr.c
150− :functions: fpga_mgr_register_full
156+ :functions: __fpga_mgr_register_full
151157
152158 .. kernel-doc:: drivers/fpga/fpga-mgr.c
153− :functions: fpga_mgr_register
159+ :functions: __fpga_mgr_register
154160
155161 .. kernel-doc:: drivers/fpga/fpga-mgr.c
156− :functions: devm_fpga_mgr_register_full
162+ :functions: __devm_fpga_mgr_register_full
157163
158164 .. kernel-doc:: drivers/fpga/fpga-mgr.c
159− :functions: devm_fpga_mgr_register
165+ :functions: __devm_fpga_mgr_register
160166
161167 .. kernel-doc:: drivers/fpga/fpga-mgr.c
162168 :functions: fpga_mgr_unregister
drivers/fpga/fpga-mgr.c+49−33
@@ -664,20 +664,16 @@ static struct attribute *fpga_mgr_attrs[] = { · 663 unmodified lines
664664 };
665665 ATTRIBUTE_GROUPS(fpga_mgr);
666666
667−static struct fpga_manager *__fpga_mgr_get(struct device *dev)
667+static struct fpga_manager *__fpga_mgr_get(struct device *mgr_dev)
668668 {
669669 struct fpga_manager *mgr;
670670
671− mgr = to_fpga_manager(dev);
671+ mgr = to_fpga_manager(mgr_dev);
672672
673− if (!try_module_get(dev->parent->driver->owner))
674− goto err_dev;
673+ if (!try_module_get(mgr->mops_owner))
674+ mgr = ERR_PTR(-ENODEV);
675675
676676 return mgr;
677−
678−err_dev:
679− put_device(dev);
680− return ERR_PTR(-ENODEV);
681677 }
682678
683679 static int fpga_mgr_dev_match(struct device *dev, const void *data)
@@ -693,12 +689,18 @@ static int fpga_mgr_dev_match(struct device *dev, const void *data) · 9 unmodified lines
693689 */
694690 struct fpga_manager *fpga_mgr_get(struct device *dev)
695691 {
696− struct device *mgr_dev = class_find_device(&fpga_mgr_class, NULL, dev,
697− fpga_mgr_dev_match);
692+ struct fpga_manager *mgr;
693+ struct device *mgr_dev;
694+
695+ mgr_dev = class_find_device(&fpga_mgr_class, NULL, dev, fpga_mgr_dev_match);
698696 if (!mgr_dev)
699697 return ERR_PTR(-ENODEV);
700698
701− return __fpga_mgr_get(mgr_dev);
699+ mgr = __fpga_mgr_get(mgr_dev);
700+ if (IS_ERR(mgr))
701+ put_device(mgr_dev);
702+
703+ return mgr;
702704 }
703705 EXPORT_SYMBOL_GPL(fpga_mgr_get);
704706
@@ -711,13 +713,18 @@ EXPORT_SYMBOL_GPL(fpga_mgr_get); · 6 unmodified lines
711713 */
712714 struct fpga_manager *of_fpga_mgr_get(struct device_node *node)
713715 {
714− struct device *dev;
716+ struct fpga_manager *mgr;
717+ struct device *mgr_dev;
715718
716− dev = class_find_device_by_of_node(&fpga_mgr_class, node);
717− if (!dev)
719+ mgr_dev = class_find_device_by_of_node(&fpga_mgr_class, node);
720+ if (!mgr_dev)
718721 return ERR_PTR(-ENODEV);
719722
720− return __fpga_mgr_get(dev);
723+ mgr = __fpga_mgr_get(mgr_dev);
724+ if (IS_ERR(mgr))
725+ put_device(mgr_dev);
726+
727+ return mgr;
721728 }
722729 EXPORT_SYMBOL_GPL(of_fpga_mgr_get);
723730
@@ -727,7 +734,7 @@ EXPORT_SYMBOL_GPL(of_fpga_mgr_get); · 3 unmodified lines
727734 */
728735 void fpga_mgr_put(struct fpga_manager *mgr)
729736 {
730− module_put(mgr->dev.parent->driver->owner);
737+ module_put(mgr->mops_owner);
731738 put_device(&mgr->dev);
732739 }
733740 EXPORT_SYMBOL_GPL(fpga_mgr_put);
@@ -766,9 +773,10 @@ void fpga_mgr_unlock(struct fpga_manager *mgr) · 32 unmodified lines
766773 EXPORT_SYMBOL_GPL(fpga_mgr_unlock);
767774
768775 /**
769− * fpga_mgr_register_full - create and register an FPGA Manager device
776+ * __fpga_mgr_register_full - create and register an FPGA Manager device
770777 * @parent: fpga manager device from pdev
771778 * @info: parameters for fpga manager
779+ * @owner: owner module containing the ops
772780 *
773781 * The caller of this function is responsible for calling fpga_mgr_unregister().
774782 * Using devm_fpga_mgr_register_full() instead is recommended.
@@ -776,7 +784,8 @@ EXPORT_SYMBOL_GPL(fpga_mgr_unlock); · 1 unmodified line
776784 * Return: pointer to struct fpga_manager pointer or ERR_PTR()
777785 */
778786 struct fpga_manager *
779−fpga_mgr_register_full(struct device *parent, const struct fpga_manager_info *info)
787+__fpga_mgr_register_full(struct device *parent, const struct fpga_manager_info *info,
788+ struct module *owner)
780789 {
781790 const struct fpga_manager_ops *mops = info->mops;
782791 struct fpga_manager *mgr;
@@ -804,6 +813,8 @@ fpga_mgr_register_full(struct device *parent, const struct fpga_manager_info *in · 21 unmodified lines
804813
805814 mutex_init(&mgr->ref_mutex);
806815
816+ mgr->mops_owner = owner;
817+
807818 mgr->name = info->name;
808819 mgr->mops = info->mops;
809820 mgr->priv = info->priv;
@@ -841,14 +852,15 @@ error_kfree: · 31 unmodified lines
841852
842853 return ERR_PTR(ret);
843854 }
844−EXPORT_SYMBOL_GPL(fpga_mgr_register_full);
855+EXPORT_SYMBOL_GPL(__fpga_mgr_register_full);
845856
846857 /**
847− * fpga_mgr_register - create and register an FPGA Manager device
858+ * __fpga_mgr_register - create and register an FPGA Manager device
848859 * @parent: fpga manager device from pdev
849860 * @name: fpga manager name
850861 * @mops: pointer to structure of fpga manager ops
851862 * @priv: fpga manager private data
863+ * @owner: owner module containing the ops
852864 *
853865 * The caller of this function is responsible for calling fpga_mgr_unregister().
854866 * Using devm_fpga_mgr_register() instead is recommended. This simple
@@ -859,8 +871,8 @@ EXPORT_SYMBOL_GPL(fpga_mgr_register_full); · 4 unmodified lines
859871 * Return: pointer to struct fpga_manager pointer or ERR_PTR()
860872 */
861873 struct fpga_manager *
862−fpga_mgr_register(struct device *parent, const char *name,
863− const struct fpga_manager_ops *mops, void *priv)
874+__fpga_mgr_register(struct device *parent, const char *name,
875+ const struct fpga_manager_ops *mops, void *priv, struct module *owner)
864876 {
865877 struct fpga_manager_info info = { 0 };
866878
@@ -868,9 +880,9 @@ fpga_mgr_register(struct device *parent, const char *name, · 1 unmodified line
868880 info.mops = mops;
869881 info.priv = priv;
870882
871− return fpga_mgr_register_full(parent, &info);
883+ return __fpga_mgr_register_full(parent, &info, owner);
872884 }
873−EXPORT_SYMBOL_GPL(fpga_mgr_register);
885+EXPORT_SYMBOL_GPL(__fpga_mgr_register);
874886
875887 /**
876888 * fpga_mgr_unregister - unregister an FPGA manager
@@ -900,9 +912,10 @@ static void devm_fpga_mgr_unregister(struct device *dev, void *res) · 23 unmodified lines
900912 }
901913
902914 /**
903− * devm_fpga_mgr_register_full - resource managed variant of fpga_mgr_register()
915+ * __devm_fpga_mgr_register_full - resource managed variant of fpga_mgr_register()
904916 * @parent: fpga manager device from pdev
905917 * @info: parameters for fpga manager
918+ * @owner: owner module containing the ops
906919 *
907920 * Return: fpga manager pointer on success, negative error code otherwise.
908921 *
@@ -910,7 +923,8 @@ static void devm_fpga_mgr_unregister(struct device *dev, void *res) · 1 unmodified line
910923 * function will be called automatically when the managing device is detached.
911924 */
912925 struct fpga_manager *
913−devm_fpga_mgr_register_full(struct device *parent, const struct fpga_manager_info *info)
926+__devm_fpga_mgr_register_full(struct device *parent, const struct fpga_manager_info *info,
927+ struct module *owner)
914928 {
915929 struct fpga_mgr_devres *dr;
916930 struct fpga_manager *mgr;
@@ -919,7 +933,7 @@ devm_fpga_mgr_register_full(struct device *parent, const struct fpga_manager_inf · 2 unmodified lines
919933 if (!dr)
920934 return ERR_PTR(-ENOMEM);
921935
922− mgr = fpga_mgr_register_full(parent, info);
936+ mgr = __fpga_mgr_register_full(parent, info, owner);
923937 if (IS_ERR(mgr)) {
924938 devres_free(dr);
925939 return mgr;
@@ -930,14 +944,15 @@ devm_fpga_mgr_register_full(struct device *parent, const struct fpga_manager_inf · 4 unmodified lines
930944
931945 return mgr;
932946 }
933−EXPORT_SYMBOL_GPL(devm_fpga_mgr_register_full);
947+EXPORT_SYMBOL_GPL(__devm_fpga_mgr_register_full);
934948
935949 /**
936− * devm_fpga_mgr_register - resource managed variant of fpga_mgr_register()
950+ * __devm_fpga_mgr_register - resource managed variant of fpga_mgr_register()
937951 * @parent: fpga manager device from pdev
938952 * @name: fpga manager name
939953 * @mops: pointer to structure of fpga manager ops
940954 * @priv: fpga manager private data
955+ * @owner: owner module containing the ops
941956 *
942957 * Return: fpga manager pointer on success, negative error code otherwise.
943958 *
@@ -946,8 +961,9 @@ EXPORT_SYMBOL_GPL(devm_fpga_mgr_register_full); · 2 unmodified lines
946961 * device is detached.
947962 */
948963 struct fpga_manager *
949−devm_fpga_mgr_register(struct device *parent, const char *name,
950− const struct fpga_manager_ops *mops, void *priv)
964+__devm_fpga_mgr_register(struct device *parent, const char *name,
965+ const struct fpga_manager_ops *mops, void *priv,
966+ struct module *owner)
951967 {
952968 struct fpga_manager_info info = { 0 };
953969
@@ -955,9 +971,9 @@ devm_fpga_mgr_register(struct device *parent, const char *name, · 1 unmodified line
955971 info.mops = mops;
956972 info.priv = priv;
957973
958− return devm_fpga_mgr_register_full(parent, &info);
974+ return __devm_fpga_mgr_register_full(parent, &info, owner);
959975 }
960−EXPORT_SYMBOL_GPL(devm_fpga_mgr_register);
976+EXPORT_SYMBOL_GPL(__devm_fpga_mgr_register);
961977
962978 static void fpga_mgr_dev_release(struct device *dev)
963979 {
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 */