linux-mtd.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1] mtd: core/part: trying to delete partition with usecount > 0 corrupt partition
@ 2024-11-15  8:55 Andreas Oetken
  2024-11-18 10:15 ` Miquel Raynal
  0 siblings, 1 reply; 3+ messages in thread
From: Andreas Oetken @ 2024-11-15  8:55 UTC (permalink / raw)
  Cc: Andreas Oetken, Miquel Raynal, Richard Weinberger,
	Vignesh Raghavendra, linux-mtd, linux-kernel

Check for usecount before deleting debugfs and sysfs entries.
Otherwise deleting the partition a second time leads to a kernel panic.

Signed-off-by: Andreas Oetken <andreas.oetken@siemens-energy.com>
---
 drivers/mtd/mtdcore.c | 25 ++++++++++++++-----------
 drivers/mtd/mtdcore.h |  2 +-
 drivers/mtd/mtdpart.c |  7 +++----
 3 files changed, 18 insertions(+), 16 deletions(-)

diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index 1c8c40728678..19ade7e53024 100644
--- a/drivers/mtd/mtdcore.c
+++ b/drivers/mtd/mtdcore.c
@@ -714,30 +714,33 @@ int add_mtd_device(struct mtd_info *mtd)
  *	if the requested device does not appear to be present in the list.
  */
 
-int del_mtd_device(struct mtd_info *mtd)
+int del_mtd_device(struct mtd_info *mtd, const struct attribute *mtd_partition_attrs[])
 {
 	int ret;
 	struct mtd_notifier *not;
 
 	mutex_lock(&mtd_table_mutex);
-
-	debugfs_remove_recursive(mtd->dbg.dfs_dir);
-
 	if (idr_find(&mtd_idr, mtd->index) != mtd) {
 		ret = -ENODEV;
 		goto out_error;
 	}
 
-	/* No need to get a refcount on the module containing
-		the notifier, since we hold the mtd_table_mutex */
-	list_for_each_entry(not, &mtd_notifiers, list)
-		not->remove(mtd);
-
 	if (mtd->usecount) {
 		printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
 		       mtd->index, mtd->name, mtd->usecount);
 		ret = -EBUSY;
 	} else {
+		/* No need to get a refcount on the module containing
+		 * the notifier, since we hold the mtd_table_mutex
+		 */
+		debugfs_remove_recursive(mtd->dbg.dfs_dir);
+
+		list_for_each_entry(not, &mtd_notifiers, list)
+			not->remove(mtd);
+
+		if (mtd_partition_attrs != NULL)
+			sysfs_remove_files(&mtd->dev.kobj, mtd_partition_attrs);
+
 		/* Try to remove the NVMEM provider */
 		if (mtd->nvmem)
 			nvmem_unregister(mtd->nvmem);
@@ -852,7 +855,7 @@ int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
 
 out:
 	if (ret && device_is_registered(&mtd->dev))
-		del_mtd_device(mtd);
+		del_mtd_device(mtd, NULL);
 
 	return ret;
 }
@@ -878,7 +881,7 @@ int mtd_device_unregister(struct mtd_info *master)
 	if (!device_is_registered(&master->dev))
 		return 0;
 
-	return del_mtd_device(master);
+	return del_mtd_device(master, NULL);
 }
 EXPORT_SYMBOL_GPL(mtd_device_unregister);
 
diff --git a/drivers/mtd/mtdcore.h b/drivers/mtd/mtdcore.h
index b5eefeabf310..0f8e815e99b2 100644
--- a/drivers/mtd/mtdcore.h
+++ b/drivers/mtd/mtdcore.h
@@ -9,7 +9,7 @@ extern struct backing_dev_info *mtd_bdi;
 
 struct mtd_info *__mtd_next_device(int i);
 int __must_check add_mtd_device(struct mtd_info *mtd);
-int del_mtd_device(struct mtd_info *mtd);
+int del_mtd_device(struct mtd_info *mtd, const struct attribute *mtd_partition_attrs[]);
 int add_mtd_partitions(struct mtd_info *, const struct mtd_partition *, int);
 int del_mtd_partitions(struct mtd_info *);
 
diff --git a/drivers/mtd/mtdpart.c b/drivers/mtd/mtdpart.c
index 5725818fa199..96d4deb4d9b5 100644
--- a/drivers/mtd/mtdpart.c
+++ b/drivers/mtd/mtdpart.c
@@ -307,12 +307,11 @@ static int __mtd_del_partition(struct mtd_info *mtd)
 			return err;
 	}
 
-	sysfs_remove_files(&mtd->dev.kobj, mtd_partition_attrs);
-
-	err = del_mtd_device(mtd);
+	err = del_mtd_device(mtd, mtd_partition_attrs);
 	if (err)
 		return err;
 
+
 	list_del(&mtd->part.node);
 	free_partition(mtd);
 
@@ -334,7 +333,7 @@ static int __del_mtd_partitions(struct mtd_info *mtd)
 			__del_mtd_partitions(child);
 
 		pr_info("Deleting %s MTD partition\n", child->name);
-		ret = del_mtd_device(child);
+		ret = del_mtd_device(child, mtd_partition_attrs);
 		if (ret < 0) {
 			pr_err("Error when deleting partition \"%s\" (%d)\n",
 			       child->name, ret);
-- 
2.45.2


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

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

* Re: [PATCH v1] mtd: core/part: trying to delete partition with usecount > 0 corrupt partition
  2024-11-15  8:55 [PATCH v1] mtd: core/part: trying to delete partition with usecount > 0 corrupt partition Andreas Oetken
@ 2024-11-18 10:15 ` Miquel Raynal
       [not found]   ` <FR0P281MB1626159D92BE16A35E8272BAAE272@FR0P281MB1626.DEUP281.PROD.OUTLOOK.COM>
  0 siblings, 1 reply; 3+ messages in thread
From: Miquel Raynal @ 2024-11-18 10:15 UTC (permalink / raw)
  To: Andreas Oetken
  Cc: Andreas Oetken, Richard Weinberger, Vignesh Raghavendra,
	linux-mtd, linux-kernel

Hi Andreas,

On 15/11/2024 at 09:55:13 +01, Andreas Oetken <ennoerlangen@gmail.com> wrote:

> Check for usecount before deleting debugfs and sysfs entries.
> Otherwise deleting the partition a second time leads to a kernel
> panic.

What kernel version are you using? I believe this bug no longer exists
in mainline. Let me know if I disregarded something though.

Thanks for the patch anyway, it was clean.

Cheers,
Miquèl

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

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

* Re: [PATCH v1] mtd: core/part: trying to delete partition with usecount > 0 corrupt partition
       [not found]   ` <FR0P281MB1626159D92BE16A35E8272BAAE272@FR0P281MB1626.DEUP281.PROD.OUTLOOK.COM>
@ 2024-11-18 13:41     ` Miquel Raynal
  0 siblings, 0 replies; 3+ messages in thread
From: Miquel Raynal @ 2024-11-18 13:41 UTC (permalink / raw)
  To: Oetken, Andreas
  Cc: Andreas Oetken, Richard Weinberger, Vignesh Raghavendra,
	linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org

Hi Andreas,

On 18/11/2024 at 12:03:18 GMT, "Oetken, Andreas" <andreas.oetken@siemens-energy.com> wrote:

> Hi Miquèl,
>
> Thanks for reviewing. You are right. The issue was already fixed with 19bfa9eb. 
> I had the issue with v5.10.x.
> Sorry for submitting the patch in a hurry without rebasing and
> checking if it's already fixed.

No problem. You can however submit your patch to stable@ if you want to
fix this in 5.10.

Also, mind you have a double \n\n somewhere which will have to be fixed.

Cheers,
Miquèl

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

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

end of thread, other threads:[~2024-11-18 13:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-15  8:55 [PATCH v1] mtd: core/part: trying to delete partition with usecount > 0 corrupt partition Andreas Oetken
2024-11-18 10:15 ` Miquel Raynal
     [not found]   ` <FR0P281MB1626159D92BE16A35E8272BAAE272@FR0P281MB1626.DEUP281.PROD.OUTLOOK.COM>
2024-11-18 13:41     ` Miquel Raynal

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).