mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: BAREBOX <barebox@lists.infradead.org>
Subject: [PATCH 02/13] fs: devfs: count an open partition as an open device
Date: Mon, 31 Aug 2026 15:20:09 +0200	[thread overview]
Message-ID: <20260831-usb-device-lifetime-v1-2-6adf4054b909@pengutronix.de> (raw)
In-Reply-To: <20260831-usb-device-lifetime-v1-0-6adf4054b909@pengutronix.de>

devfs_remove() refuses to remove a cdev that is open, but opening a
partition only increments the open count of the partition itself:
cdev_open() passes the master to the ->open operation, but counts on the
cdev it was given. A disk whose partition is mounted therefore does not
look busy at all.

Removing it then gets half done. The master is unlinked from cdev_list
and its aliases and automount are dropped, then the loop over the
partitions calls cdevfs_del_partition(), which does return -EBUSY for the
mounted partition, but nobody looks at the return value. The partition
stays in cdev_list with its ->master and ->priv pointing at the block
device the caller is about to free, and the next access walks
cdev_get_master() into freed memory.

A device with an open partition is in use, so count the open along the
whole chain of masters instead of only on the cdev that was opened. The
open count then means what it says and devfs_remove() needs no special
case.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Assisted-by: Claude:claude-opus-5
---
 fs/devfs-core.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/fs/devfs-core.c b/fs/devfs-core.c
index 522d883e1c..c106e65a04 100644
--- a/fs/devfs-core.c
+++ b/fs/devfs-core.c
@@ -249,6 +249,7 @@ static struct cdev *cdev_get_master(struct cdev *cdev)
 int cdev_open(struct cdev *cdev, unsigned long flags)
 {
 	struct cdev *master = cdev_get_master(cdev);
+	struct cdev *c;
 	int ret;
 
 	if (cdev->ops->open) {
@@ -257,7 +258,14 @@ int cdev_open(struct cdev *cdev, unsigned long flags)
 			return ret;
 	}
 
-	cdev->open++;
+	/*
+	 * A device with an open partition is in use itself, so count the
+	 * open along the whole chain up to the device the partition lives
+	 * on. Without that a disk with a mounted partition looks idle and
+	 * could be removed from under the filesystem.
+	 */
+	for (c = cdev; c; c = c->master)
+		c->open++;
 
 	return 0;
 }
@@ -314,6 +322,7 @@ struct cdev *cdev_open_by_path_name(const char *name, unsigned long flags)
 int cdev_close(struct cdev *cdev)
 {
 	struct cdev *master = cdev_get_master(cdev);
+	struct cdev *c;
 
 	if (cdev->ops->close) {
 		int ret = cdev->ops->close(master);
@@ -321,7 +330,8 @@ int cdev_close(struct cdev *cdev)
 			return ret;
 	}
 
-	cdev->open--;
+	for (c = cdev; c; c = c->master)
+		c->open--;
 
 	return 0;
 }

-- 
2.47.3




  parent reply	other threads:[~2026-08-31 13:21 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-31 13:20 [PATCH 00/13] USB: Make USB devices removable Sascha Hauer
2026-08-31 13:20 ` [PATCH 01/13] usb: don't report device removal after the device name is gone Sascha Hauer
2026-08-31 13:20 ` Sascha Hauer [this message]
2026-08-31 13:20 ` [PATCH 03/13] block: propagate errors from blockdevice_unregister() Sascha Hauer
2026-08-31 13:20 ` [PATCH 04/13] fs: add cdev_umount_all() Sascha Hauer
2026-08-31 13:20 ` [PATCH 05/13] block: add blockdevice_unregister_removed() Sascha Hauer
2026-08-31 13:20 ` [PATCH 06/13] usb: storage: tear the disk down properly on disconnect Sascha Hauer
2026-08-31 13:20 ` [PATCH 07/13] usb: hub: cancel pending port scans of a removed device Sascha Hauer
2026-08-31 13:20 ` [PATCH 08/13] usb: reuse the addresses of removed devices Sascha Hauer
2026-08-31 13:20 ` [PATCH 09/13] usb: hub: detect disconnected devices Sascha Hauer
2026-08-31 13:20 ` [PATCH 10/13] usb: don't keep a dangling root device on enumeration failure Sascha Hauer
2026-08-31 13:20 ` [PATCH 11/13] usb: hub: limit the number of ports to USB_MAXCHILDREN Sascha Hauer
2026-08-31 13:20 ` [PATCH 12/13] usb: detect unplugged devices on transfer errors Sascha Hauer
2026-08-31 13:20 ` [PATCH 13/13] usb: storage: stop talking to a device that is gone Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260831-usb-device-lifetime-v1-2-6adf4054b909@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox