Skip to contentSkip to conversation

firmware_loader: introduce __free() cleanup hanler

PATCH In review Dmitry Torokhov 2 participants 2 messages Jan 17, 2024 – Jan 25, 2024 Firmware loader (request_firmware)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 firmware_loader: introduce __free() cleanup hanler

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;
}

---

If this is acceptable sticking it into an immutable branch would be very helpful. Thanks!

 include/linux/firmware.h | 3 +++
 1 file changed, 3 insertions(+)
include/linux/firmware.h +3 -0
index de7fea3bca51..2035a0d9cc86 100644
@@ -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
@@ -196,4 +197,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
Signature · 5 lines
2.43.0.381.gb435a96ce8-goog


-- 
Dmitry
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 30 lines · Dmitry Torokhov

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;
}
Quoted 1 line

Luis