[PATCH] ubi: don't decrease ubi->ref_count on detach error

From: Daniel Golle
Date: Mon Nov 27 2023 - 12:09:45 EST


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 refcnt
once all conditions for detachment are met.

Fixes: cdfa788acd13 ("UBI: prepare attach and detach functions")
Signed-off-by: Daniel Golle <daniel@xxxxxxxxxxxxxx>
---
drivers/mtd/ubi/build.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/mtd/ubi/build.c b/drivers/mtd/ubi/build.c
index 7d4ff1193db6f..f47987ee9a31b 100644
--- a/drivers/mtd/ubi/build.c
+++ b/drivers/mtd/ubi/build.c
@@ -1099,16 +1099,16 @@ 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_devices[ubi_num] = NULL;
spin_unlock(&ubi_devices_lock);

--
2.43.0