blk: Fix duplicate partition devices after re-probing a block device
cover blk: Fix duplicate partition devices after re-probing a block device
The partition devices that blk_post_probe() binds for a block device are not unbound when the block device is removed. When it is probed again, a second set of partition devices is bound, and the EFI disk code fails to add the stale ones:
Adding partition for [email protected]:1 failed
switch to partitions #0, ERROR
This was seen on an i.MX93 board where a single failed read during the first probe of the SD card made the card unusable for the rest of the boot.
Patch 1 unbinds the partition devices in the pre_remove hook of the block uclass. Patch 2 adds a sandbox test for it.
---
Frieder Schrempf (2):
blk: Unbind partition devices when removing a block device
test: blk: Check that partition devices are unbound on remove
drivers/block/blk-uclass.c | 22 ++++++++++++++++ test/dm/blk.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+)
---
base-commit: dfcfc4c9f78d1743f490a497082a4ec8e058aae8
change-id: 20261027-blk-part-unbind-e6ba8f1ac8ca
Best regards,
Signature · 1 line
Frieder Schrempf <[email protected]>
PATCH 1/2 blk: Unbind partition devices when removing a block device
From: Frieder Schrempf <[email protected]>
blk_post_probe() binds a UCLASS_PARTITION child for each partition found on the device. Removing the block device removes these children, but does not unbind them. When the block device is probed again, a second set of partition devices is bound next to the first one.
This happens for example when the probe of a block device fails in the EVT_DM_POST_PROBE event and is retried later. The EFI disk code then iterates over all children and tries to install a second EFI handle with the same device path for the stale partition device, which fails with EFI_ALREADY_STARTED:
Adding partition for [email protected]:1 failed
switch to partitions #0, ERROR
As every further probe adds another set of partition devices, the block device can't be used anymore until the next reset. This was seen on an i.MX93 board where a single failed read during the first probe of the SD card made it unusable for the rest of the boot.
Unbind the partition devices in the pre_remove hook of the block uclass, so that they are bound and unbound together with the probe and remove of the block device. When only some devices are removed, like before booting an OS, the partition devices don't match the remove flags and are still active at this point, so remove them first.
--- drivers/block/blk-uclass.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
drivers/block/blk-uclass.c +22 -0
@@ -841,9 +841,31 @@ static int blk_post_probe(struct udevice *dev)return 0 ; } +static int blk_pre_remove (struct udevice *dev) +{ +struct driver *drv =DM_DRIVER_GET (blk_partition); +int ret; + +if (!CONFIG_IS_ENABLED (PARTITIONS ) || !blk_enabled ()) +return 0 ; + +/* +* The partition devices are bound in blk_post_probe(), so unbind them +* here. Otherwise the next probe adds a second set of them. They are +* still active if the remove flags don't match their driver, so +* remove them first. +*/ + ret =device_chld_remove (dev, drv,DM_REMOVE_NORMAL ); +if (ret) +return ret; + +return device_chld_unbind (dev, drv); +} +UCLASS_DRIVER (blk) = { .id =UCLASS_BLK , .name ="blk" , .post_probe = blk_post_probe, + .pre_remove = blk_pre_remove, .per_device_plat_auto =sizeof (struct blk_desc ), };
Signature · 1 line
2.55.0
Re: [PATCH 1/2] blk: Unbind partition devices when removing a block device
Hi Frieder,
Quoted 21 lines · Frieder Schrempf
blk: Unbind partition devices when removing a block device
blk_post_probe()binds aUCLASS_PARTITIONchild for each partition found on the device. Removing the block device removes these children, but does not unbind them. When the block device is probed again, a second set of partition devices is bound next to the first one.This happens for example when the probe of a block device fails in the
EVT_DM_POST_PROBEevent and is retried later. The EFI disk code then iterates over all children and tries to install a second EFI handle with the same device path for the stale partition device, which fails withEFI_ALREADY_STARTED:Adding partition for [email protected]:1 failed
switch to partitions #0, ERRORAs every further probe adds another set of partition devices, the block device can't be used anymore until the next reset. This was seen on an i.MX93 board where a single failed read during the first probe of the SD card made it unusable for the rest of the boot.
Quoted 5 lines
Unbind the partition devices in the
pre_removehook of the block uclass, so that they are bound and unbound together with the probe and remove of the block device. When only some devices are removed, like before booting an OS, the partition devices don't match the remove flags and are still active at this point, so remove them first.
I don't think this is true any more. Since commit 3632b5d63c8 ("dm:
Remove children when parent is removed by flags"), when the parent matches the flags, device_remove() removes the children with DM_REMOVE_ACTIVE_ALL masked out, before uclass_pre_remove_device() is called. The partition driver is not vital, so by the time blk_pre_remove() runs the partition devices are always inactive. Please can you drop this sentence?
Quoted 8 lines
[...]
drivers/block/blk-uclass.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)drivers/block/blk-uclass.c +1
-0@@ -841,9 +841,31 @@ static int blk_post_probe(struct udevice *dev) +/*
drivers/block/blk-uclass.c static int blk_post_probe(struct udevice *dev) · line 851
Quoted 10 lines
drivers/block/blk-uclass.c +10
-0+* The partition devices are bound in blk_post_probe(), so unbind them +* here. Otherwise the next probe adds a second set of them. They are +* still active if the remove flags don't match their driver, so +* remove them first. +*/ + ret =device_chld_remove (dev, drv,DM_REMOVE_NORMAL ); +if (ret) +return ret; + +return device_chld_unbind (dev, drv);
For the same reason, the device_chld_remove() call does nothing, since device_remove() has already removed all the children by this point. You could just call device_chld_unbind() and trim the last sentence of the comment. If you want to keep the call as a safety net, please change the comment so it doesn't say the partitions can still be active here. If they ever were, device_unbind() would fail with -EINVAL rather than leave things in a bad state.
Regards,
Simon
Re: [PATCH 1/2] blk: Unbind partition devices when removing a block device
Hi Simon,
Quoted 57 lines · Simon Glass
Hi Frieder,
Quoted 21 lines · Frieder Schrempf
blk: Unbind partition devices when removing a block device
blk_post_probe()binds aUCLASS_PARTITIONchild for each partition found on the device. Removing the block device removes these children, but does not unbind them. When the block device is probed again, a second set of partition devices is bound next to the first one.This happens for example when the probe of a block device fails in the
EVT_DM_POST_PROBEevent and is retried later. The EFI disk code then iterates over all children and tries to install a second EFI handle with the same device path for the stale partition device, which fails withEFI_ALREADY_STARTED:Adding partition for [email protected]:1 failed
switch to partitions #0, ERRORAs every further probe adds another set of partition devices, the block device can't be used anymore until the next reset. This was seen on an i.MX93 board where a single failed read during the first probe of the SD card made it unusable for the rest of the boot.
Quoted 5 lines
Unbind the partition devices in the
pre_removehook of the block uclass, so that they are bound and unbound together with the probe and remove of the block device. When only some devices are removed, like before booting an OS, the partition devices don't match the remove flags and are still active at this point, so remove them first.I don't think this is true any more. Since commit
3632b5d63c8("dm:
Remove children when parent is removed by flags"), when the parent matches the flags,device_remove()removes the children withDM_REMOVE_ACTIVE_ALLmasked out, beforeuclass_pre_remove_device()is called. The partition driver is not vital, so by the timeblk_pre_remove()runs the partition devices are always inactive. Please can you drop this sentence?Quoted 8 lines
[...]
drivers/block/blk-uclass.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)drivers/block/blk-uclass.c +1
-0@@ -841,9 +841,31 @@ static int blk_post_probe(struct udevice *dev) +/* drivers/block/blk-uclass.c static int blk_post_probe(struct udevice *dev) · line 851
Quoted 10 lines
drivers/block/blk-uclass.c +10
-0+* The partition devices are bound in blk_post_probe(), so unbind them +* here. Otherwise the next probe adds a second set of them. They are +* still active if the remove flags don't match their driver, so +* remove them first. +*/ + ret =device_chld_remove (dev, drv,DM_REMOVE_NORMAL ); +if (ret) +return ret; + +return device_chld_unbind (dev, drv);
Quoted 7 lines
For the same reason, the
device_chld_remove()call does nothing, sincedevice_remove()has already removed all the children by this point. You could just calldevice_chld_unbind()and trim the last sentence of the comment. If you want to keep the call as a safety net, please change the comment so it doesn't say the partitions can still be active here. If they ever were,device_unbind()would fail with-EINVALrather than leave things in a bad state.
Thanks for pointing that out. I was forward porting this from an older version and missed the upstream 3632b5d63c8. I addressed this in v2.
Best regards
Frieder
PATCH 2/2 test: blk: Check that partition devices are unbound on remove
In reply to Frieder SchrempfFrom: Frieder Schrempf <[email protected]>
Add a test that removes and probes a block device with a GPT several times and checks that its partition devices are unbound on remove and bound again only once on probe. Also remove the block device with the flags used before booting an OS, which don't match the partition driver.
--- test/dm/blk.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+)
test/dm/blk.c +64 -0
@@ -9,6 +9,7 @@#include <sandbox_host.h> #include <usb.h> #include <asm/state.h> +#include <dm/device-internal.h> #include <dm/test.h> #include <test/test.h> #include <test/ut.h>
@@ -291,3 +292,66 @@ static int dm_test_blk_foreach(struct unit_test_state *uts)return 0 ; }DM_TEST (dm_test_blk_foreach,UTF_SCAN_PDATA |UTF_SCAN_FDT ); + +static int count_part_devs (struct udevice *blk) +{ +struct udevice *dev; +int count =0 ; + + device_foreach_child(dev, blk) { +if (device_get_uclass_id (dev) ==UCLASS_PARTITION ) + count++; + } + +return count; +} + +/* Test that probing a block device again does not duplicate its partitions */ +static int dm_test_blk_part_reprobe (struct unit_test_state *uts) +{ +char str_disk_guid[UUID_STR_LEN +1 ]; +struct disk_partition parts[2 ] = { + { + .start =48 ,/* GPT data takes up the first 34 blocks or so */ + .size =1 , + .name ="test1" , + }, + { + .start =49 , + .size =1 , + .name ="test2" , + }, + }; +struct blk_desc *desc; +struct udevice *blk; +int i; + +ut_asserteq (2 ,blk_get_device_by_str ("mmc" ,"2" , &desc)); +if (CONFIG_IS_ENABLED (RANDOM_UUID )) { +gen_rand_uuid_str (parts[0 ].uuid,UUID_STR_FORMAT_STD ); +gen_rand_uuid_str (parts[1 ].uuid,UUID_STR_FORMAT_STD ); +gen_rand_uuid_str (str_disk_guid,UUID_STR_FORMAT_STD ); + } +ut_assertok (gpt_restore (desc, str_disk_guid, parts,ARRAY_SIZE (parts))); + blk = desc->bdev; + +for (i =0 ; i <2 ; i++) { +ut_assertok (device_remove (blk,DM_REMOVE_NORMAL )); +ut_asserteq (0 ,count_part_devs (blk)); +ut_assertok (device_probe (blk)); +ut_asserteq (ARRAY_SIZE (parts),count_part_devs (blk)); + } + +/* +* Removing only the active devices, as done before booting an OS, +* removes the block device but doesn't match the partition driver +*/ +ut_assertok (device_remove (blk,DM_REMOVE_ACTIVE_ALL )); +ut_assert (!device_active (blk)); +ut_asserteq (0 ,count_part_devs (blk)); +ut_assertok (device_probe (blk)); +ut_asserteq (ARRAY_SIZE (parts),count_part_devs (blk)); + +return 0 ; +} +DM_TEST (dm_test_blk_part_reprobe,UTF_SCAN_PDATA |UTF_SCAN_FDT );
Signature · 1 line
2.55.0
Re: [PATCH 2/2] test: blk: Check that partition devices are unbound on remove
Hi Frieder,
Quoted 16 lines · Frieder Schrempf
test: blk: Check that partition devices are unbound on remove
Add a test that removes and probes a block device with a GPT several times and checks that its partition devices are unbound on remove and bound again only once on probe. Also remove the block device with the flags used before booting an OS, which don't match the partition driver.
Assisted-by LLMSigned-off-by Frieder Schrempf <[email protected]>test/dm/blk.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+)
test/dm/blk.c static int dm_test_blk_foreach(struct unit_test_state *uts) · line 298
Quoted 7 lines
test/dm/blk.c +7
-0@@ -291,3 +292,66 @@ static int dm_test_blk_foreach(struct unit_test_state *uts) +/* +* Removing only the active devices, as done before booting an OS, +* removes the block device but doesn't match the partition driver +*/ +ut_assertok (device_remove (blk,DM_REMOVE_ACTIVE_ALL )); +ut_assert (!device_active (blk)); +ut_asserteq (0 ,count_part_devs (blk));
The test passes on sandbox, but I'm not sure this part tests what the comment says. As mentioned on patch 1, once 'mmc_blk' matches DM_FLAG_OS_PREPARE, device_remove() calls device_chld_remove() with 'flags & ~DM_REMOVE_ACTIVE_ALL' (i.e. 0), so the partition devices are removed before blk_pre_remove() runs. This part of the test would pass without the device_chld_remove() call in patch 1. Please can you update the comment and the commit message to match?
Regards,
Simon