[v2] ubi: don't decrease ubi->ref_count on detach error
Commit Message
If attempting to detach a UBI device while it is still busy, detaching
is refused. However, the reference counter is still being decreased
despite the error. Rework detach function to only decrease the reference
counter once all conditions for detachment are met.
Fixes: cdfa788acd13 ("UBI: prepare attach and detach functions")
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
---
v2: also catch negative ref_count and add ubi_assert as suggested
drivers/mtd/ubi/build.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
Comments
在 2023/11/28 8:45, Daniel Golle 写道:
> If attempting to detach a UBI device while it is still busy, detaching
> is refused. However, the reference counter is still being decreased
> despite the error. Rework detach function to only decrease the reference
> counter once all conditions for detachment are met.
>
> Fixes: cdfa788acd13 ("UBI: prepare attach and detach functions")
> Signed-off-by: Daniel Golle <daniel@makrotopia.org>
> ---
> v2: also catch negative ref_count and add ubi_assert as suggested
>
> drivers/mtd/ubi/build.c | 7 ++++---
> 1 file changed, 4 insertions(+), 3 deletions(-)
Reviewed-by: Zhihao Cheng <chengzhihao1@huawei.com>
>
> diff --git a/drivers/mtd/ubi/build.c b/drivers/mtd/ubi/build.c
> index 7d4ff1193db6f..2316f6014c7f5 100644
> --- a/drivers/mtd/ubi/build.c
> +++ b/drivers/mtd/ubi/build.c
> @@ -1099,16 +1099,17 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway)
>
> spin_lock(&ubi_devices_lock);
> put_device(&ubi->dev);
> - ubi->ref_count -= 1;
> - if (ubi->ref_count) {
> + if (ubi->ref_count != 1) {
> if (!anyway) {
> spin_unlock(&ubi_devices_lock);
> return -EBUSY;
> }
> /* This may only happen if there is a bug */
> ubi_err(ubi, "%s reference count %d, destroy anyway",
> - ubi->ubi_name, ubi->ref_count);
> + ubi->ubi_name, ubi->ref_count - 1);
> }
> + ubi->ref_count -= 1;
> + ubi_assert(ubi->ref_count == 0);
> ubi_devices[ubi_num] = NULL;
> spin_unlock(&ubi_devices_lock);
>
@@ -1099,16 +1099,17 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway)
spin_lock(&ubi_devices_lock);
put_device(&ubi->dev);
- ubi->ref_count -= 1;
- if (ubi->ref_count) {
+ if (ubi->ref_count != 1) {
if (!anyway) {
spin_unlock(&ubi_devices_lock);
return -EBUSY;
}
/* This may only happen if there is a bug */
ubi_err(ubi, "%s reference count %d, destroy anyway",
- ubi->ubi_name, ubi->ref_count);
+ ubi->ubi_name, ubi->ref_count - 1);
}
+ ubi->ref_count -= 1;
+ ubi_assert(ubi->ref_count == 0);
ubi_devices[ubi_num] = NULL;
spin_unlock(&ubi_devices_lock);