public inbox for linux-mtd@lists.infradead.org
 help / color / mirror / Atom feed
* [PATCH] ubi: don't decrease ubi->ref_count on detach error
@ 2023-12-05  8:11 Ryder W
  2023-12-05  9:01 ` Zhihao Cheng
  0 siblings, 1 reply; 6+ messages in thread
From: Ryder W @ 2023-12-05  8:11 UTC (permalink / raw)
  To: linux-mtd@lists.infradead.org, Zhihao Cheng,
	daniel@makrotopia.org

> > Fixes: cdfa788acd13 ("UBI: prepare attach and detach functions")
> > Signed-off-by: Daniel Golle <daniel at makrotopia.org>
> > ---
> >  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);

In the last code of ubi_detach_mtd_dev, the line "ubi->ref_count -= 1" after the line "put_device(&ubi->dev)" is just to decrease ubi->ref_count, which is increased from calling put_device. I don't understand why it should be moved after the sanity check of ubi->ref_count. It may introduce some more critical bug. 










______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 6+ messages in thread
* [PATCH] ubi: don't decrease ubi->ref_count on detach error
@ 2023-11-27 17:09 Daniel Golle
  2023-11-27 20:25 ` Richard Weinberger
  0 siblings, 1 reply; 6+ messages in thread
From: Daniel Golle @ 2023-11-27 17:09 UTC (permalink / raw)
  To: Richard Weinberger, Miquel Raynal, Vignesh Raghavendra,
	Artem Bityutskiy, linux-mtd, linux-kernel
  Cc: John Crispin

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@makrotopia.org>
---
 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

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-12-05 12:24 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-12-05  8:11 [PATCH] ubi: don't decrease ubi->ref_count on detach error Ryder W
2023-12-05  9:01 ` Zhihao Cheng
2023-12-05 12:23   ` Daniel Golle
  -- strict thread matches above, loose matches on Subject: below --
2023-11-27 17:09 Daniel Golle
2023-11-27 20:25 ` Richard Weinberger
2023-11-27 22:06   ` Daniel Golle

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox