firmware_loader: Fix shutdown ordering and reference counting
Subsystems (14)
cover firmware_loader: Fix shutdown ordering and reference counting
Chao Gao raised a module reference circular dependency report resulting from *correct* usage of the firmware_upload_register() API [1]. The module reference count is not necessary nor sufficient for protecting against racing unregister against in-flight requests. After that is fixed, a couple more cleanups fall out.
[1]: https://sashiko.dev/#/patchset/20260326084448.29947-1-chao.gao%40intel.com?patch=10705
Dan Williams (3):
firmware_loader: Stop pinning modules on registration
firmware_loader: Stop pinning parent device per workqueue invocation
treewide: firmware_loader: Drop the unused @module argument
.../driver-api/firmware/fw_upload.rst | 2 +- include/linux/firmware.h | 15 +++--- drivers/base/firmware_loader/sysfs_upload.c | 48 ++++++++----------- drivers/cxl/core/memdev.c | 4 +- drivers/firmware/microchip/mpfs-auto-update.c | 2 +- drivers/fpga/intel-m10-bmc-sec-update.c | 4 +- drivers/greybus/gb-beagleplay.c | 2 +- drivers/media/i2c/thp7312.c | 2 +- drivers/net/pse-pd/pd692x0.c | 4 +- lib/test_firmware.c | 3 +- 10 files changed, 38 insertions(+), 48 deletions(-)
base-commit: f338e77383789c0cae23ca3d48adcc5e9e137e3c
Signature · 1 line
2.53.0
PATCH 1/3 firmware_loader: Stop pinning modules on registration
The module reference counting can result in callers pinning themselves in a circular loop. The module reference counting is unnecessary. firmware_upload_unregister() must be able to guarantee that all ops are idle at return.
All ops are either called from sysfs or the workqueue, so unregister sysfs to stop submissions, cancel any started transfers, flush cancelled transfers, and then release the device.
This also solves a theoretical race of new submissions starting between flush_work() and device_unregister(). The module reference was not protecting against that race.
--- drivers/base/firmware_loader/sysfs_upload.c | 31 ++++++++++----------- 1 file changed, 14 insertions(+), 17 deletions(-)
drivers/base/firmware_loader/sysfs_upload.c +14 -17
@@ -312,14 +312,9 @@ firmware_upload_register(struct module *module, struct device *parent,return ERR_PTR (-EINVAL ); } -if (!try_module_get (module)) -return ERR_PTR (-EFAULT ); - fw_upload =kzalloc_obj (*fw_upload); -if (!fw_upload) { - ret = -ENOMEM ; - goto exit_module_put; - } +if (!fw_upload) +return ERR_PTR (-ENOMEM ); fw_upload_priv =kzalloc_obj (*fw_upload_priv);if (!fw_upload_priv) {
@@ -360,7 +355,7 @@ firmware_upload_register(struct module *module, struct device *parent,if (ret) {dev_err (fw_dev,"%s: device_register failed\n" , __func__);put_device (fw_dev); - goto exit_module_put; +return ERR_PTR (ret); }return fw_upload;
@@ -374,9 +369,6 @@ firmware_upload_register(struct module *module, struct device *parent, free_fw_upload:kfree (fw_upload); -exit_module_put: -module_put (module); -return ERR_PTR (ret); }EXPORT_SYMBOL_GPL (firmware_upload_register);
@@ -388,23 +380,28 @@ EXPORT_SYMBOL_GPL(firmware_upload_register);void firmware_upload_unregister (struct fw_upload *fw_upload) {struct fw_sysfs *fw_sysfs = fw_upload->priv; +struct device *parent = fw_sysfs->dev.parent;struct fw_upload_priv *fw_upload_priv = fw_sysfs->fw_upload_priv; -struct module *module = fw_upload_priv->module; + +/* hold a parent reference while child is unregistered */ +get_device (parent); + +/* shutdown the sysfs interface to block new requests */ +device_del (&fw_sysfs->dev);mutex_lock (&fw_upload_priv->lock);if (fw_upload_priv->progress ==FW_UPLOAD_PROG_IDLE ) {mutex_unlock (&fw_upload_priv->lock); - goto unregister; + goto release; } fw_upload_priv->ops->cancel (fw_upload);mutex_unlock (&fw_upload_priv->lock); +release:/* Ensure lower-level device-driver is finished */ flush_work (&fw_upload_priv->work); - -unregister: -device_unregister (&fw_sysfs->dev); -module_put (module); +put_device (&fw_sysfs->dev); +put_device (parent); }EXPORT_SYMBOL_GPL (firmware_upload_unregister);
Signature · 1 line
2.53.0
PATCH 2/3 firmware_loader: Stop pinning parent device per workqueue invocation
In reply to Dan WilliamsThe device core pins parent devices while children are registered. As long as all usage of the parent device by the firmware_loader ends at firmware_upload_unregister(), no per queue_work() reference is needed.
Now that firmware_upload_unregister() holds its own parent device reference over the child device_del() and flush_work() events, the per queue_work() reference can be deleted.
--- drivers/base/firmware_loader/sysfs_upload.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/base/firmware_loader/sysfs_upload.c +2 -6
@@ -176,7 +176,7 @@ static void fw_upload_main(struct work_struct *work) ret = fwlp->ops->prepare (fwl, fwlp->data, fwlp->remaining_size);if (ret !=FW_UPLOAD_ERR_NONE ) {fw_upload_set_error (fwlp, ret); - goto putdev_exit; + goto out; }fw_upload_update_progress (fwlp,FW_UPLOAD_PROG_TRANSFERRING );
@@ -204,9 +204,7 @@ static void fw_upload_main(struct work_struct *work) done:if (fwlp->ops->cleanup) fwlp->ops->cleanup (fwl); - -putdev_exit: -put_device (fw_dev->parent); +out:/* * Note: fwlp->remaining_size is left unmodified here to provide
@@ -249,8 +247,6 @@ int fw_upload_start(struct fw_sysfs *fw_sysfs)return -EBUSY ; } -get_device (fw_dev->parent);/* released in fw_upload_main */ - fwlp->progress =FW_UPLOAD_PROG_RECEIVING ; fwlp->err_code =0 ; fwlp->remaining_size = fw_priv->size;
Signature · 1 line
2.53.0
PATCH 3/3 treewide: firmware_loader: Drop the unused @module argument
In reply to Dan WilliamsNow that the firmware loader properly ceases all operations at firmware_upload_unregister() and no longer takes module references, clean up the unused parameter.
--- Documentation/driver-api/firmware/fw_upload.rst | 2 +- include/linux/firmware.h | 15 +++++++-------- drivers/base/firmware_loader/sysfs_upload.c | 9 ++++----- drivers/cxl/core/memdev.c | 4 ++-- drivers/firmware/microchip/mpfs-auto-update.c | 2 +- drivers/fpga/intel-m10-bmc-sec-update.c | 4 ++-- drivers/greybus/gb-beagleplay.c | 2 +- drivers/media/i2c/thp7312.c | 2 +- drivers/net/pse-pd/pd692x0.c | 4 ++-- lib/test_firmware.c | 3 +-- 10 files changed, 22 insertions(+), 25 deletions(-)
Documentation/driver-api/firmware/fw_upload.rst +1 -1
@@ -57,7 +57,7 @@ function calls firmware_upload_unregister() such as:: len = (truncate) ? truncate - fw_name : strlen(fw_name); sec->fw_name = kmemdup_nul(fw_name, len, GFP_KERNEL); - fwl = firmware_upload_register(THIS_MODULE, sec->dev, sec->fw_name, + fwl = firmware_upload_register(sec->dev, sec->fw_name, &m10bmc_ops, sec); if (IS_ERR(fwl)) { dev_err(sec->dev, "Firmware Upload driver failed to start\n");
include/linux/firmware.h +7 -8
@@ -187,20 +187,19 @@ static inline int request_partial_firmware_into_buf#ifdef CONFIG_FW_UPLOAD -struct fw_upload * -firmware_upload_register (struct module *module,struct device *parent, -const char *name,const struct fw_upload_ops *ops, -void *dd_handle); +struct fw_upload *firmware_upload_register (struct device *parent, +const char *name, +const struct fw_upload_ops *ops, +void *dd_handle);void firmware_upload_unregister (struct fw_upload *fw_upload);#else static inline struct fw_upload * -firmware_upload_register (struct module *module,struct device *parent, -const char *name,const struct fw_upload_ops *ops, -void *dd_handle) +firmware_upload_register (struct device *parent,const char *name, +const struct fw_upload_ops *ops,void *dd_handle) { -return ERR_PTR (-EINVAL ); +return ERR_PTR (-EINVAL ); }static inline void firmware_upload_unregister (struct fw_upload *fw_upload)
drivers/base/firmware_loader/sysfs_upload.c +4 -5
@@ -274,7 +274,6 @@ void fw_upload_free(struct fw_sysfs *fw_sysfs)/** * firmware_upload_register() - register for the firmware upload sysfs API -* @module: kernel module of this device * @parent: parent device instantiating firmware upload * @name: firmware name to be associated with this device * @ops: pointer to structure of firmware upload ops
@@ -286,10 +285,10 @@ void fw_upload_free(struct fw_sysfs *fw_sysfs)* Return: struct fw_upload pointer or ERR_PTR() * **/ -struct fw_upload * -firmware_upload_register (struct module *module,struct device *parent, -const char *name,const struct fw_upload_ops *ops, -void *dd_handle) +struct fw_upload *firmware_upload_register (struct device *parent, +const char *name, +const struct fw_upload_ops *ops, +void *dd_handle) {u32 opt_flags =FW_OPT_NOCACHE ;struct fw_upload *fw_upload;
drivers/cxl/core/memdev.c +2 -2
@@ -1045,8 +1045,8 @@ int devm_cxl_setup_fw_upload(struct device *host, struct cxl_memdev_state *mds)if (!test_bit (CXL_MEM_COMMAND_ID_GET_FW_INFO , cxl_mbox->enabled_cmds))return 0 ; - fwl =firmware_upload_register (THIS_MODULE , dev,dev_name (dev), - &cxl_memdev_fw_ops, mds); + fwl =firmware_upload_register (dev,dev_name (dev), &cxl_memdev_fw_ops, + mds);if (IS_ERR (fwl))return PTR_ERR (fwl);return devm_add_action_or_reset (host, cxl_remove_fw_upload, fwl);
drivers/firmware/microchip/mpfs-auto-update.c +1 -1
@@ -435,7 +435,7 @@ static int mpfs_auto_update_probe(struct platform_device *pdev)return dev_err_probe (dev, ret,"The current bitstream does not support auto-update\n" ); - fw_uploader =firmware_upload_register (THIS_MODULE , dev,"mpfs-auto-update" , + fw_uploader =firmware_upload_register (dev,"mpfs-auto-update" , &mpfs_auto_update_ops, priv);if (IS_ERR (fw_uploader)) return dev_err_probe(dev, PTR_ERR(fw_uploader),
drivers/fpga/intel-m10-bmc-sec-update.c +2 -2
@@ -713,8 +713,8 @@ static int m10bmc_sec_probe(struct platform_device *pdev) goto fw_name_fail; } - fwl =firmware_upload_register (THIS_MODULE , sec->dev, sec->fw_name, - &m10bmc_ops, sec); + fwl =firmware_upload_register (sec->dev, sec->fw_name, &m10bmc_ops, + sec);if (IS_ERR (fwl)) {dev_err (sec->dev,"Firmware Upload driver failed to start\n" ); ret =PTR_ERR (fwl);
drivers/greybus/gb-beagleplay.c +1 -1
@@ -1067,7 +1067,7 @@ static int gb_fw_init(struct gb_beagleplay *bg)return PTR_ERR (desc); bg->rst_gpio = desc; - fwl =firmware_upload_register (THIS_MODULE , &bg->sd->dev,"cc1352p7" , + fwl =firmware_upload_register (&bg->sd->dev,"cc1352p7" , &cc1352_bootloader_ops, bg);if (IS_ERR (fwl))return PTR_ERR (fwl);
drivers/media/i2c/thp7312.c +1 -1
@@ -1909,7 +1909,7 @@ static int thp7312_register_flash_mode(struct thp7312_device *thp7312) goto error; } - fwl =firmware_upload_register (THIS_MODULE , dev,"thp7312-firmware" , + fwl =firmware_upload_register (dev,"thp7312-firmware" , &thp7312_fw_upload_ops, thp7312);if (IS_ERR (fwl)) { ret =PTR_ERR (fwl);
drivers/net/pse-pd/pd692x0.c +2 -2
@@ -1834,8 +1834,8 @@ static int pd692x0_i2c_probe(struct i2c_client *client)return dev_err_probe (dev, ret,"failed to register PSE controller\n" ); - fwl =firmware_upload_register (THIS_MODULE , dev,dev_name (dev), - &pd692x0_fw_ops, priv); + fwl =firmware_upload_register (dev,dev_name (dev), &pd692x0_fw_ops, + priv);if (IS_ERR (fwl))return dev_err_probe (dev,PTR_ERR (fwl),"failed to register to the Firmware Upload API\n" );
lib/test_firmware.c +1 -2
@@ -1322,8 +1322,7 @@ static ssize_t upload_register_store(struct device *dev, goto free_tst; } - fwl =firmware_upload_register (THIS_MODULE , dev, tst->name, - &upload_test_ops, tst); + fwl =firmware_upload_register (dev, tst->name, &upload_test_ops, tst);if (IS_ERR (fwl)) { ret =PTR_ERR (fwl); goto free_buf;
Signature · 1 line
2.53.0
Re: [PATCH 3/3] treewide: firmware_loader: Drop the unused @module argument
Quoted 11 lines · Dan Williams
Now that the firmware loader properly ceases all operations at
firmware_upload_unregister()and no longer takes module references, clean up the unused parameter.Cc Chao Gao <[email protected]>Cc Luis Chamberlain <[email protected]>Cc Russ Weight <[email protected]>Cc Danilo Krummrich <[email protected]>Cc Greg Kroah-Hartman <[email protected]>Cc "Rafael J. Wysocki" <[email protected]>Signed-off-by Dan Williams <[email protected]>
Quoted 3 lines
---
drivers/firmware/microchip/mpfs-auto-update.c | 2 +-
Quoted 27 lines · Dan Williams
Chao Gao raised a module reference circular dependency report resulting from *correct* usage of the
firmware_upload_register()API [1]. The module reference count is not necessary nor sufficient for protecting against racing unregister against in-flight requests. After that is fixed, a couple more cleanups fall out.[1]: https://sashiko.dev/#/patchset/20260326084448.29947-1-chao.gao%40intel.com?patch=10705
Dan Williams (3):
firmware_loader: Stop pinning modules on registration
firmware_loader: Stop pinning parent device per workqueue invocation
treewide:firmware_loader: Drop the unused @module argument.../driver-api/firmware/fw_upload.rst | 2 +- include/linux/firmware.h | 15 +++--- drivers/base/firmware_loader/sysfs_upload.c | 48 ++++++++----------- drivers/cxl/core/memdev.c | 4 +- drivers/firmware/microchip/mpfs-auto-update.c | 2 +- drivers/fpga/intel-m10-bmc-sec-update.c | 4 +- drivers/greybus/gb-beagleplay.c | 2 +- drivers/media/i2c/thp7312.c | 2 +- drivers/net/pse-pd/pd692x0.c | 4 +- lib/test_firmware.c | 3 +- 10 files changed, 38 insertions(+), 48 deletions(-)
Quoted 1 line
base-commit:
f338e77383789c0cae23ca3d48adcc5e9e137e3c
Hi Dan,
Thanks for making these changes! Overall, the changes look good to me. However, when I apply these changes to the specified base-commit and attempt to build, I'm getting these errors:
drivers/base/firmware_loader/sysfs_upload.c:229:24: warning: unused variable ‘fw_dev’ [-Wunused-variable]
229 | struct device *fw_dev = &fw_sysfs->dev;
| ^~~~~~drivers/base/firmware_loader/sysfs_upload.c: In function ‘firmware_upload_register’:drivers/base/firmware_loader/sysfs_upload.c:323:34: error: ‘module’ undeclared (first use in this function)
323 | fw_upload_priv->module = module;
| ^~~~~~
Thanks,
- Russ
Quoted 28 lines · Russ Weight
Quoted 27 lines · Dan Williams
Chao Gao raised a module reference circular dependency report resulting from *correct* usage of the
firmware_upload_register()API [1]. The module reference count is not necessary nor sufficient for protecting against racing unregister against in-flight requests. After that is fixed, a couple more cleanups fall out.[1]: https://sashiko.dev/#/patchset/20260326084448.29947-1-chao.gao%40intel.com?patch=10705
Dan Williams (3):
firmware_loader: Stop pinning modules on registration
firmware_loader: Stop pinning parent device per workqueue invocation
treewide:firmware_loader: Drop the unused @module argument.../driver-api/firmware/fw_upload.rst | 2 +- include/linux/firmware.h | 15 +++--- drivers/base/firmware_loader/sysfs_upload.c | 48 ++++++++----------- drivers/cxl/core/memdev.c | 4 +- drivers/firmware/microchip/mpfs-auto-update.c | 2 +- drivers/fpga/intel-m10-bmc-sec-update.c | 4 +- drivers/greybus/gb-beagleplay.c | 2 +- drivers/media/i2c/thp7312.c | 2 +- drivers/net/pse-pd/pd692x0.c | 4 +- lib/test_firmware.c | 3 +- 10 files changed, 38 insertions(+), 48 deletions(-)
Quoted 7 lines
Quoted 1 line
base-commit:
f338e77383789c0cae23ca3d48adcc5e9e137e3cHi Dan,
Thanks for making these changes! Overall, the changes look good to me. However, when I apply these changes to the specified base-commit and attempt to build, I'm getting these errors:
(+Dan's new email)
Yes, I noticed that Sashiko reported them.
Quoted 4 lines
drivers/base/firmware_loader/sysfs_upload.c:229:24: warning: unused variable ‘fw_dev’ [-Wunused-variable]
229 | struct device*fw_dev=&fw_sysfs->dev;
| ^~~~~~
This can be fixed by applying the following diff to patch 2:
drivers/base/firmware_loader/sysfs_upload.c +0 -0
@@ -226,7 +226,6 @@ static void fw_upload_main(struct work_struct *work)int fw_upload_start (struct fw_sysfs *fw_sysfs) {
struct fw_priv *fw_priv = fw_sysfs->fw_priv; - struct device *fw_dev = &fw_sysfs->dev; struct fw_upload_priv *fwlp; if (!fw_sysfs->fw_upload_priv)
Quoted 4 lines
drivers/base/firmware_loader/sysfs_upload.c: In function ‘firmware_upload_register’:drivers/base/firmware_loader/sysfs_upload.c:323:34: error: ‘module’ undeclared (first use in this function)
323 |fw_upload_priv->module= module;
| ^~~~~~
the @module field should be removed in patch 3:
drivers/base/firmware_loader/sysfs_upload.c +0 -0
@@ -319,7 +319,6 @@ struct fw_upload *firmware_upload_register(struct device *parent,
fw_upload_priv->fw_upload = fw_upload; fw_upload_priv->ops = ops; mutex_init(&fw_upload_priv->lock); - fw_upload_priv->module = module; fw_upload_priv->name = name; fw_upload_priv->err_code = 0; fw_upload_priv->progress = FW_UPLOAD_PROG_IDLE;
drivers/base/firmware_loader/sysfs_upload.h +0 -0
@@ -26,7 +26,6 @@ enum fw_upload_prog {struct fw_upload_priv {
struct fw_upload *fw_upload; - struct module *module; const char *name; const struct fw_upload_ops *ops; struct mutex lock; /* protect data structure contents */
Dan Williams
maintainerQuoted 31 lines · Chao Gao
Quoted 30 lines · Russ Weight
Quoted 28 lines · Dan Williams
Chao Gao raised a module reference circular dependency report resulting from *correct* usage of the
firmware_upload_register()API [1]. The module reference count is not necessary nor sufficient for protecting against racing unregister against in-flight requests. After that is fixed, a couple more cleanups fall out.[1]: https://sashiko.dev/#/patchset/20260326084448.29947-1-chao.gao%40intel.com?patch=10705
Dan Williams (3):
firmware_loader: Stop pinning modules on registration
firmware_loader: Stop pinning parent device per workqueue invocation
treewide:firmware_loader: Drop the unused @module argument.../driver-api/firmware/fw_upload.rst | 2 +- include/linux/firmware.h | 15 +++--- drivers/base/firmware_loader/sysfs_upload.c | 48 ++++++++----------- drivers/cxl/core/memdev.c | 4 +- drivers/firmware/microchip/mpfs-auto-update.c | 2 +- drivers/fpga/intel-m10-bmc-sec-update.c | 4 +- drivers/greybus/gb-beagleplay.c | 2 +- drivers/media/i2c/thp7312.c | 2 +- drivers/net/pse-pd/pd692x0.c | 4 +- lib/test_firmware.c | 3 +- 10 files changed, 38 insertions(+), 48 deletions(-)base-commit:
f338e77383789c0cae23ca3d48adcc5e9e137e3c
Quoted 7 lines
Quoted 5 lines
Hi Dan,
Thanks for making these changes! Overall, the changes look good to me. However, when I apply these changes to the specified base-commit and attempt to build, I'm getting these errors:
(+Dan's new email)
Thanks! I will note that thanks to the korgalore tool I am subscribed to my old email, at least if someone also copies a public-inbox list.
Quoted 1 line
Yes, I noticed that Sashiko reported them.
Let's just say last week was "hectic". Sorry the noise.
Quoted 5 lines
Quoted 3 lines
drivers/base/firmware_loader/sysfs_upload.c:229:24: warning: unused variable ‘fw_dev’ [-Wunused-variable]
229 | struct device*fw_dev=&fw_sysfs->dev;
| ^~~~~~This can be fixed by applying the following diff to patch 2:
[..]
Quoted 1 line
the @module field should be removed in patch 3:
Fixups look good.
Quoted 9 lines · Dan Williams
Quoted 8 lines · Chao Gao
Quoted 7 lines · Russ Weight
Quoted 6 lines · Dan Williams
Chao Gao raised a module reference circular dependency report resulting from *correct* usage of the
firmware_upload_register()API [1]. The module reference count is not necessary nor sufficient for protecting against racing unregister against in-flight requests. After that is fixed, a couple more cleanups fall out.
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
[1]: https://sashiko.dev/#/patchset/20260326084448.29947-1-chao.gao%40intel.com?patch=10705
Dan Williams (3):
firmware_loader: Stop pinning modules on registration
firmware_loader: Stop pinning parent device per workqueue invocation
treewide:firmware_loader: Drop the unused @module argument
Hi all,
I am preparing a download_firmware_ex live update series and, since these patches were close to complete, I would like to introduce them as a precursor to my patches.
I will rebase and fold in Chao's build fixups from this thread, and preserve the rest of the attributions.
Please let me know if you'd rather they go in through another tree first instead of being bundled with the SEV live firmware update patches.
Thanks,
Pratik
Quoted 39 lines
Quoted 15 lines
Quoted 15 lines
Quoted 14 lines
.../driver-api/firmware/fw_upload.rst | 2 +- include/linux/firmware.h | 15 +++--- drivers/base/firmware_loader/sysfs_upload.c | 48 ++++++++----------- drivers/cxl/core/memdev.c | 4 +- drivers/firmware/microchip/mpfs-auto-update.c | 2 +- drivers/fpga/intel-m10-bmc-sec-update.c | 4 +- drivers/greybus/gb-beagleplay.c | 2 +- drivers/media/i2c/thp7312.c | 2 +- drivers/net/pse-pd/pd692x0.c | 4 +- lib/test_firmware.c | 3 +- 10 files changed, 38 insertions(+), 48 deletions(-)base-commit:
f338e77383789c0cae23ca3d48adcc5e9e137e3cQuoted 7 lines
Quoted 5 lines
Hi Dan,
Thanks for making these changes! Overall, the changes look good to me. However, when I apply these changes to the specified base-commit and attempt to build, I'm getting these errors:
(+Dan's new email)
Thanks! I will note that thanks to the korgalore tool I am subscribed to my old email, at least if someone also copies a public-inbox list.
Quoted 1 line
Yes, I noticed that Sashiko reported them.
Let's just say last week was "hectic". Sorry the noise.
Quoted 5 lines
Quoted 3 lines
drivers/base/firmware_loader/sysfs_upload.c:229:24: warning: unused variable ‘fw_dev’ [-Wunused-variable]
229 | struct device*fw_dev=&fw_sysfs->dev;
| ^~~~~~This can be fixed by applying the following diff to patch 2:
[..]
Quoted 1 line
the @module field should be removed in patch 3:
Fixups look good.