The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH v2 0/2] loop, nbd: drop partitions left behind by the GD_NEED_PART_SCAN removal
@ 2026-08-24 13:32 Daan De Meyer via B4 Relay
  2026-08-24 13:32 ` [PATCH v2 1/2] loop: drop stale partitions on LOOP_CHANGE_FD Daan De Meyer via B4 Relay
  2026-08-24 13:32 ` [PATCH v2 2/2] nbd: drop stale partitions on NBD_CLEAR_SOCK Daan De Meyer via B4 Relay
  0 siblings, 2 replies; 5+ messages in thread
From: Daan De Meyer via B4 Relay @ 2026-08-24 13:32 UTC (permalink / raw)
  To: Jens Axboe, Christian Brauner, Josef Bacik
  Cc: linux-block, linux-kernel, nbd, Christoph Hellwig,
	Bart Van Assche, Shin'ichiro Kawasaki, Daan De Meyer, stable

Commit 267ec4d7223a ("loop: fix partition scan race between udev and
loop_reread_partitions()") stopped disk_force_media_change() from setting
GD_NEED_PART_SCAN. The caller audit in that commit only considered the bit
as a request to rescan partitions, but it did more than that:
bdev_disk_changed() drops every entry in disk->part_tbl before it consults
disk_has_partscan(), and only the re-adding half is gated on partition
scanning being enabled. The lazy scan on the next open was therefore also
the only thing removing partitions from a device that has partitions but
no partition scanning.

Loop devices without LO_FLAGS_PARTSCAN are exactly such devices, and they
are not unusual. bdev_add_partition() only rejects GENHD_FL_NO_PART disks,
so BLKPG_ADD_PARTITION works while GD_SUPPRESS_PART_SCAN is set, and
parted, libfdisk and systemd all fall back to BLKPG when BLKRRPART fails
with -EINVAL, which is what a loop device without LO_FLAGS_PARTSCAN
returns.

Commit c4f4c0fc551c ("loop: remove manually added partitions on detach")
fixed the detach path after this was reported as loop devices picking up
partition devices from a previously built image:

  https://bugs.debian.org/1141434

These two patches fix the two remaining places that relied on the same
lazy cleanup, LOOP_CHANGE_FD and NBD_CLEAR_SOCK.

Regression tests for both paths have been submitted to blktests:

  https://github.com/linux-blktests/blktests/pull/259
  https://github.com/linux-blktests/blktests/pull/260

Signed-off-by: Daan De Meyer <daan@amutable.com>
---
Changes in v2:
- Rewrap the comments added by both patches so they fit in 80 columns
  (Christoph Hellwig).
- No functional change, so the review and test tags from v1 were carried
  over.
- Link to v1: https://patch.msgid.link/20260806-b4-loop-nbd-stale-partitions-v1-0-67bb75a8d7be@amutable.com

---
Daan De Meyer (2):
      loop: drop stale partitions on LOOP_CHANGE_FD
      nbd: drop stale partitions on NBD_CLEAR_SOCK

 drivers/block/loop.c | 10 ++++++----
 drivers/block/nbd.c  |  7 +++++++
 2 files changed, 13 insertions(+), 4 deletions(-)
---
base-commit: 0a0d1d55dad570724bf8c7ea83409639cfb4be9b
change-id: 20260806-b4-loop-nbd-stale-partitions-2d10ede71a2a

Best regards,
--  
Daan De Meyer <daan@amutable.com>



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

* [PATCH v2 1/2] loop: drop stale partitions on LOOP_CHANGE_FD
  2026-08-24 13:32 [PATCH v2 0/2] loop, nbd: drop partitions left behind by the GD_NEED_PART_SCAN removal Daan De Meyer via B4 Relay
@ 2026-08-24 13:32 ` Daan De Meyer via B4 Relay
  2026-08-25  5:45   ` Christoph Hellwig
  2026-08-24 13:32 ` [PATCH v2 2/2] nbd: drop stale partitions on NBD_CLEAR_SOCK Daan De Meyer via B4 Relay
  1 sibling, 1 reply; 5+ messages in thread
From: Daan De Meyer via B4 Relay @ 2026-08-24 13:32 UTC (permalink / raw)
  To: Jens Axboe, Christian Brauner, Josef Bacik
  Cc: linux-block, linux-kernel, nbd, Christoph Hellwig,
	Bart Van Assche, Shin'ichiro Kawasaki, Daan De Meyer, stable

From: Daan De Meyer <daan@amutable.com>

Commit 267ec4d7223a ("loop: fix partition scan race between udev and
loop_reread_partitions()") stopped disk_force_media_change() from
setting GD_NEED_PART_SCAN. That bit did more than request a rescan:
bdev_disk_changed() drops every partition before it consults
disk_has_partscan(), so the lazy scan on the next open was also what
removed partitions from a loop device without LO_FLAGS_PARTSCAN.

Such devices are not unusual. bdev_add_partition() only rejects
GENHD_FL_NO_PART disks, so BLKPG_ADD_PARTITION works while
GD_SUPPRESS_PART_SCAN is set, and parted, libfdisk and systemd all fall
back to BLKPG when BLKRRPART fails with -EINVAL, which is what a loop
device without LO_FLAGS_PARTSCAN returns.

loop_change_fd() only rescans when LO_FLAGS_PARTSCAN is set, so those
partitions now survive the backing file swap and keep describing the old
file. The new backing file must have the same size, but its partition
table can be completely different, leaving the partition devices mapping
the wrong ranges.

Call loop_reread_partitions() unconditionally. Without LO_FLAGS_PARTSCAN,
blk_add_partitions() returns early, so this drops the stale partitions
without scanning the new backing file.

Fixes: 267ec4d7223a ("loop: fix partition scan race between udev and loop_reread_partitions()")
Cc: stable@vger.kernel.org
Reviewed-by: Christian Brauner (Amutable) <brauner@kernel.org>
Reviewed-by: Bart Van Assche <bvanassche@acm.org>
Tested-by: Shin'ichiro Kawasaki <shinichiro.kawasaki@wdc.com>
Signed-off-by: Daan De Meyer <daan@amutable.com>
---
 drivers/block/loop.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index 6f12976035b0..89935cfc591f 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -550,7 +550,6 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
 	struct file *old_file;
 	unsigned int memflags;
 	int error;
-	bool partscan;
 	bool is_loop;
 
 	if (!file)
@@ -604,7 +603,6 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
 	loop_assign_backing_file(lo, file);
 	loop_update_dio(lo);
 	blk_mq_unfreeze_queue(lo->lo_queue, memflags);
-	partscan = lo->lo_flags & LO_FLAGS_PARTSCAN;
 	loop_global_unlock(lo, is_loop);
 
 	/*
@@ -622,8 +620,12 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
 	 */
 	fput(old_file);
 	dev_set_uevent_suppress(disk_to_dev(lo->lo_disk), 0);
-	if (partscan)
-		loop_reread_partitions(lo);
+	/*
+	 * Rescan or, without LO_FLAGS_PARTSCAN, just drop the partitions of
+	 * the old backing file. They can exist without LO_FLAGS_PARTSCAN as
+	 * they may have been added manually with BLKPG.
+	 */
+	loop_reread_partitions(lo);
 
 	error = 0;
 done:

-- 
2.54.0



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

* [PATCH v2 2/2] nbd: drop stale partitions on NBD_CLEAR_SOCK
  2026-08-24 13:32 [PATCH v2 0/2] loop, nbd: drop partitions left behind by the GD_NEED_PART_SCAN removal Daan De Meyer via B4 Relay
  2026-08-24 13:32 ` [PATCH v2 1/2] loop: drop stale partitions on LOOP_CHANGE_FD Daan De Meyer via B4 Relay
@ 2026-08-24 13:32 ` Daan De Meyer via B4 Relay
  2026-08-25  5:45   ` Christoph Hellwig
  1 sibling, 1 reply; 5+ messages in thread
From: Daan De Meyer via B4 Relay @ 2026-08-24 13:32 UTC (permalink / raw)
  To: Jens Axboe, Christian Brauner, Josef Bacik
  Cc: linux-block, linux-kernel, nbd, Christoph Hellwig,
	Bart Van Assche, Shin'ichiro Kawasaki, Daan De Meyer, stable

From: Daan De Meyer <daan@amutable.com>

Commit 267ec4d7223a ("loop: fix partition scan race between udev and
loop_reread_partitions()") stopped disk_force_media_change() from
setting GD_NEED_PART_SCAN. Besides requesting a rescan, that bit was what
removed stale partitions on the next open, as bdev_disk_changed() drops
all partitions before it consults disk_has_partscan().

nbd_clear_sock_ioctl() relied on that. It zeroes the capacity through
nbd_bdev_reset(), but nothing removes the partitions of the disconnected
device anymore. With the default max_part=16 they linger until the next
connect sets GD_NEED_PART_SCAN again. With max_part=0 nothing ever sets
it, so they are never removed at all, even though nbd does not set
GENHD_FL_NO_PART and partitions can therefore still be added with BLKPG.

Set GD_NEED_PART_SCAN in nbd_clear_sock_ioctl() so the partitions are
dropped on the next open. Calling bdev_disk_changed() directly is not an
option as it needs open_mutex, which nbd_open() acquires under
config_lock.

Fixes: 267ec4d7223a ("loop: fix partition scan race between udev and loop_reread_partitions()")
Cc: stable@vger.kernel.org
Reviewed-by: Christian Brauner (Amutable) <brauner@kernel.org>
Reviewed-by: Bart Van Assche <bvanassche@acm.org>
Tested-by: Shin'ichiro Kawasaki <shinichiro.kawasaki@wdc.com>
Signed-off-by: Daan De Meyer <daan@amutable.com>
---
 drivers/block/nbd.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index ffce519bf008..09b4ad70a5d7 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -1616,6 +1616,13 @@ static void nbd_clear_sock_ioctl(struct nbd_device *nbd)
 	nbd_clear_sock(nbd);
 	disk_force_media_change(nbd->disk);
 	nbd_bdev_reset(nbd);
+	/*
+	 * Drop the partitions of the disconnected device on the next open.
+	 * They can exist even with max_part zero as they may have been added
+	 * manually with BLKPG. Dropping them here is not possible as that
+	 * needs open_mutex, which nbd_open() acquires under config_lock.
+	 */
+	set_bit(GD_NEED_PART_SCAN, &nbd->disk->state);
 	if (test_and_clear_bit(NBD_RT_HAS_CONFIG_REF,
 			       &nbd->config->runtime_flags))
 		nbd_config_put(nbd);

-- 
2.54.0



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

* Re: [PATCH v2 1/2] loop: drop stale partitions on LOOP_CHANGE_FD
  2026-08-24 13:32 ` [PATCH v2 1/2] loop: drop stale partitions on LOOP_CHANGE_FD Daan De Meyer via B4 Relay
@ 2026-08-25  5:45   ` Christoph Hellwig
  0 siblings, 0 replies; 5+ messages in thread
From: Christoph Hellwig @ 2026-08-25  5:45 UTC (permalink / raw)
  To: daan
  Cc: Jens Axboe, Christian Brauner, Josef Bacik, linux-block,
	linux-kernel, nbd, Christoph Hellwig, Bart Van Assche,
	Shin'ichiro Kawasaki, stable

Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>

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

* Re: [PATCH v2 2/2] nbd: drop stale partitions on NBD_CLEAR_SOCK
  2026-08-24 13:32 ` [PATCH v2 2/2] nbd: drop stale partitions on NBD_CLEAR_SOCK Daan De Meyer via B4 Relay
@ 2026-08-25  5:45   ` Christoph Hellwig
  0 siblings, 0 replies; 5+ messages in thread
From: Christoph Hellwig @ 2026-08-25  5:45 UTC (permalink / raw)
  To: daan
  Cc: Jens Axboe, Christian Brauner, Josef Bacik, linux-block,
	linux-kernel, nbd, Christoph Hellwig, Bart Van Assche,
	Shin'ichiro Kawasaki, stable

Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>


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

end of thread, other threads:[~2026-08-25  5:45 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-24 13:32 [PATCH v2 0/2] loop, nbd: drop partitions left behind by the GD_NEED_PART_SCAN removal Daan De Meyer via B4 Relay
2026-08-24 13:32 ` [PATCH v2 1/2] loop: drop stale partitions on LOOP_CHANGE_FD Daan De Meyer via B4 Relay
2026-08-25  5:45   ` Christoph Hellwig
2026-08-24 13:32 ` [PATCH v2 2/2] nbd: drop stale partitions on NBD_CLEAR_SOCK Daan De Meyer via B4 Relay
2026-08-25  5:45   ` Christoph Hellwig

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