Re: [PATCH 3/3] firmware: add mutex fw_lock_fallback for race condition
From: Luis Chamberlain
Date: Mon Aug 19 2019 - 01:39:45 EST
On Thu, Aug 15, 2019 at 05:09:45PM -0700, Scott Branden wrote:
> A race condition exists between _request_firmware_prepare checking
> if firmware is assigned and firmware_fallback_sysfs creating a sysfs
> entry (kernel trace below). To avoid such condition add a mutex
> fw_lock_fallback to protect against such condition.
I am not buying this fix, and it seems sloppy. More below.
> misc test_firmware: Falling back to sysfs fallback for: nope-test-firmware.bin
So the fallback kicks in with the file that is not there.
> sysfs: cannot create duplicate filename '/devices/virtual/misc/test_firmware/nope-test-firmware.bin'
And we have a duplicate entry, for the *device* created to allow us to
create a file entry to allow us to copy the file. Your tests had a loop,
so there is actually a race between two entries being created while
one one failed.
> CPU: 4 PID: 2059 Comm: test_firmware-3 Not tainted 5.3.0-rc4 #1
> Hardware name: Dell Inc. OptiPlex 7010/0KRC95, BIOS A13 03/25/2013
> Call Trace:
> ? _cond_resched+0x15/0x30
> ? devres_find+0x63/0xc0
> ? reset_store+0x30/0x30
> ? kthread_park+0x80/0x80
> kobject_add_internal failed for nope-test-firmware.bin with -EEXIST, don't try to register things with the same name in the same directory.
So above it makes it even clearer, two kobjets with the same name.
> Signed-off-by: Scott Branden <scott.branden@xxxxxxxxxxxx>
> drivers/base/firmware_loader/main.c | 15 +++++++++++++++
> 1 file changed, 15 insertions(+)
> diff --git a/drivers/base/firmware_loader/main.c b/drivers/base/firmware_loader/main.c
> index bf44c79beae9..ce9896e3b782 100644
> --- a/drivers/base/firmware_loader/main.c
> +++ b/drivers/base/firmware_loader/main.c
> @@ -88,6 +88,7 @@ static inline struct fw_priv *to_fw_priv(struct kref *ref)
> /* fw_lock could be moved to 'struct fw_sysfs' but since it is just
> * guarding for corner cases a global lock should be OK */
The reason I don't like this fix is that this mutex is named after ther
fallback interface... but...
> static struct firmware_cache fw_cache;
> @@ -758,6 +759,17 @@ _request_firmware(const struct firmware **firmware_p, const char *name,
> if (!firmware_p)
> return -EINVAL;
> + /*
> + * There is a race condition between _request_firmware_prepare checking
> + * if firmware is assigned and firmware_fallback_sysfs creating sysfs
> + * entries with duplicate names.
> + * Yet, with this lock the firmware_test locks up with cache enabled
> + * and no event used during firmware test.
> + * This points to some very racy code I don't know how to entirely fix.
> + */
> + if (opt_flags & FW_OPT_NOCACHE)
> + mutex_lock(&fw_lock_fallback);
Whoa.. What does no-cache have anything to do with the fallback interface
other than the fact we enable this feature for the fallback interface?
We don't need to penalize non-fallback users who *also* may want to
enable the no-cache feature.
So, the fix should be within the boundaries of the creation / deletion
of the kobject, not this nocache feature. Can you please re-evaluate
this code and look for a more compartamentalized solution to the
fallback code only?