Skip to contentSkip to conversation

firmware: Don't attempt to allocate zero bytes with vmalloc()

PATCH In review Mark Brown 2 participants 11 messages Oct 5, 2012 – Oct 10, 2012 Driver core

lore
Share & tools
lore

Mark Brown

author
#0 lore raw

PATCH 1/1 firmware: Don't attempt to allocate zero bytes with vmalloc()

vmalloc() will fail (very loudly) if we try to allocate zero bytes to read a zero byte file. Instead report that we successfully read in all zero bytes.

It's not immediately obvious to me that this is better than returning an error but it seems better to punt the decision about that to the caller on the off chance that it's sensible.

---
 drivers/base/firmware_class.c |    5 +++++
 1 file changed, 5 insertions(+)
drivers/base/firmware_class.c +5 -0
index 8154145..a14eb92 100644
@@ -62,6 +62,11 @@ static bool fw_read_file_contents(struct file *file, struct firmware *fw)
 	char *buf;
 
 	size = fw_file_size(file);
+	if (size == 0) {
+		fw->data = NULL;
+		fw->size = 0;
+		return true;
+	}
 	if (size < 0)
 		return false;
 	buf = vmalloc(size);
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 18 lines · Mark Brown

vmalloc() will fail (very loudly) if we try to allocate zero bytes to read a zero byte file. Instead report that we successfully read in all zero bytes.

It's not immediately obvious to me that this is better than returning an error but it seems better to punt the decision about that to the caller on the off chance that it's sensible.

---
 drivers/base/firmware_class.c |    5 +++++
 1 file changed, 5 insertions(+)
drivers/base/firmware_class.c +0 -0
index 8154145..a14eb92 100644

drivers/base/firmware_class.c static bool fw_read_file_contents(struct file *file, struct firmware *fw) · line 69

Quoted 8 lines
drivers/base/firmware_class.c +5 -0
@@ -62,6 +62,11 @@ static bool fw_read_file_contents(struct file *file, struct firmware *fw)
        char *buf;
 
        size = fw_file_size(file);
+       if (size == 0) {
+               fw->data = NULL;
+               fw->size = 0;
+               return true;
+       }

Considered that zero-length firmware image doesn't make sense for drivers (callers), maybe it is a insane firmware image, so how about treating it as a failure?

Thanks,
--
Ming Lei

author
#2 lore raw
Quoted 3 lines · Ming Lei

Considered that zero-length firmware image doesn't make sense for drivers (callers), maybe it is a insane firmware image, so how about treating it as a failure?

It seems better to punt that decision to callers - for example, the case I ran into this with was a driver that was using a zero length firmware to say that it didn't want to load an optional image but also didn't want to have to time out if that was the case. That doesn't seem unreasonable to me, and drivers already have to validate that what they're getting makes sense.

#3 lore raw

On Tue, Oct 9, 2012 at 12:19 PM, Mark Brown

Quoted 1 line

It seems better to punt that decision to callers - for example, the case

In fact, -ENOENT is returned to caller for non-direct loading situation, see_request_firmware_load().

I understand drivers(caller) may be cheated if a zero-length firmware image is obtained. In normal situation, one firmware image should include something, instead of nothing, :-)

Quoted 3 lines

I ran into this with was a driver that was using a zero length firmware to say that it didn't want to load an optional image but also didn't want to have to time out if that was the case. That doesn't seem

If so, I am wondering why the driver has to call request_firmware()? Looks just bypassing request_firmware() is fine for the driver, doesn't it?

Thanks,
--
Ming Lei

author
#4 lore raw
Quoted 10 lines · Ming Lei

On Tue, Oct 9, 2012 at 12:19 PM, Mark Brown

Quoted 1 line

It seems better to punt that decision to callers - for example, the case

In fact, -ENOENT is returned to caller for non-direct loading situation, see_request_firmware_load().

I understand drivers(caller) may be cheated if a zero-length firmware image is obtained. In normal situation, one firmware image should include something, instead of nothing, :-)

Hrm, that didn't seem to be happening for me - the firmware load completed successfully. Have to check how that happened.

Quoted 6 lines
Quoted 3 lines

I ran into this with was a driver that was using a zero length firmware to say that it didn't want to load an optional image but also didn't want to have to time out if that was the case. That doesn't seem

If so, I am wondering why the driver has to call request_firmware()? Looks just bypassing request_firmware() is fine for the driver, doesn't it?

A driver has no way to tell if the firmware is there or not without asking for it.

#5 lore raw
Quoted 5 lines · Mark Brown
Quoted 2 lines

If so, I am wondering why the driver has to call request_firmware()? Looks just bypassing request_firmware() is fine for the driver, doesn't it?

A driver has no way to tell if the firmware is there or not without asking for it.

Yes, I agree, and my question is only on what you mentioned:

"it didn't want to load an optional image"

maybe I misunderstood the above, never mind, :-)

So one driver should suppose the firmware is there, and the firmware shouldn't be zero length, because the driver always expects getting some bytes by calling request_firmware().

Thanks,
--
Ming Lei

author
#6 lore raw
Quoted 9 lines · Ming Lei

Yes, I agree, and my question is only on what you mentioned:

"it didn't want to load an optional image"

maybe I misunderstood the above, never mind, :-)

So one driver should suppose the firmware is there, and the firmware shouldn't be zero length, because the driver always expects getting some bytes by calling request_firmware().

The point is that there's some firmware which the driver wants to load but where it's happy to continue if the user didn't provide one and doesn't want to introduce needless delays.

#7 lore raw
Quoted 3 lines · Mark Brown

The point is that there's some firmware which the driver wants to load but where it's happy to continue if the user didn't provide one and doesn't want to introduce needless delays.

OK, I got it, thank you for sharing the use case.

If loading directly, the patch isn't needed because filp_open() can return failure on non-existent file.

If loading via user space, timeout or not depends on userspace, at least udev won't timeout on non-existent firmware image.

Also looks request_firmware_nowait() is better for the case, _nowait() can avoid unnecessary delay and speedup firmware loading if there are more than one firmware to load.

Thanks,
--
Ming Lei

author
#8 lore raw
Quoted 2 lines · Ming Lei

If loading via user space, timeout or not depends on userspace, at least udev won't timeout on non-existent firmware image.

This may be a mdev or old udev thing... it's definitely prevelant.

Quoted 3 lines

Also looks request_firmware_nowait() is better for the case, _nowait() can avoid unnecessary delay and speedup firmware loading if there are more than one firmware to load.

It doesn't really help as the ABI is such that you can only have one request_firmware() in play at once (unless this changed since I last looked at it).

#9 lore raw
Quoted 6 lines · Mark Brown
Quoted 2 lines · Ming Lei

If loading via user space, timeout or not depends on userspace, at least udev won't timeout on non-existent firmware image.

This may be a mdev or old udev thing... it's definitely prevelant.

I just checked history of udev, and looks it can't timeout on non-existent firmware file, even with the oldest shell script of firmware.sh.

Also looks mdev of busybox checks firmware file too before loading.

IMO, firmware loader can't help the problem if userspace chooses to timeout on non-existent file. Maybe you have to depend on direct loading.

Quoted 6 lines
Quoted 4 lines

Also looks request_firmware_nowait() is better for the case, _nowait() can avoid unnecessary delay and speedup firmware loading if there are more than one firmware to load.

It doesn't really help as the ABI is such that you can only have one

Could you let me know where the ABI is?

Quoted 2 lines

request_firmware() in play at once (unless this changed since I last looked at it).

I guess you mean that only one firmware device can be added as child of the device which is requesting firmware.

The commit below(already merged into linus tree) should fix the problem:

99c2aa72306079976369aad7fc62cc71931d692a(firmware loader:
fix creation failure of fw loader device)

Could you test it to see if more than one request_firmware_nowait() can be called concurrently for the same device?

Thanks,
--
Ming Lei

author
#10 lore raw
Quoted 6 lines · Ming Lei

On Tue, Oct 9, 2012 at 8:36 PM, Mark Brown

Quoted 3 lines · Ming Lei

It doesn't really help as the ABI is such that you can only have one

Could you let me know where the ABI is?

It's defined by firmware_class?

Quoted 11 lines
Quoted 2 lines

request_firmware() in play at once (unless this changed since I last looked at it).

I guess you mean that only one firmware device can be added as child of the device which is requesting firmware.

The commit below(already merged into linus tree) should fix the problem:

99c2aa72306079976369aad7fc62cc71931d692a(firmware loader:
fix creation failure of fw loader device)

That's not been well advertised (and is extremely recent too)...

Quoted 2 lines

Could you test it to see if more than one request_firmware_nowait() can be called concurrently for the same device?

Not for some time.