firmware_loader: introduce __free() cleanup hanler
authorDmitry Torokhov <dmitry.torokhov@gmail.com>
Wed, 17 Jan 2024 08:33:07 +0000 (00:33 -0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 7 Mar 2024 22:02:12 +0000 (22:02 +0000)
Define cleanup handler using facilities from linux/cleanup.h to simplify
error handling in code using firmware loader. This will allow writing code
like this:

int driver_update_firmware(...)
{
const struct firmware *fw_entry __free(firmware) = NULL;
int error;

...
error = request_firmware(&fw_entry, fw_name, dev);
if (error) {
dev_err(dev, "failed to request firmware %s: %d",
fw_name, error);
return error;
}

error = check_firmware_valid(fw_entry);
if (error)
return error;

guard(mutex)(&instance->lock);

error = use_firmware(instance, fw);
if (error)
return error;

return 0;
}

Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Acked-by: Luis Chamberalin <mcgrof@kernel.org>
Link: https://lore.kernel.org/r/ZaeQw7VXhnirX4pQ@google.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
include/linux/firmware.h

index 0311858b46cef2d0b966e7420159f580187f95bc..f026f8926d79255553f4bdabea9b49539c8ed499 100644 (file)
@@ -4,6 +4,7 @@
 
 #include <linux/types.h>
 #include <linux/compiler.h>
+#include <linux/cleanup.h>
 #include <linux/gfp.h>
 
 #define FW_ACTION_NOUEVENT 0
@@ -198,4 +199,6 @@ static inline void firmware_upload_unregister(struct fw_upload *fw_upload)
 
 int firmware_request_cache(struct device *device, const char *name);
 
+DEFINE_FREE(firmware, struct firmware *, release_firmware(_T))
+
 #endif