FAILED: patch "[PATCH] dm snapshot: fix discard_granularity unit in" failed to apply to 6.6-stable tree
Subsystems (2)
[email protected]
maintainerThe patch below does not apply to the 6.6-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.6.y
git checkout FETCH_HEAD
git cherry-pick -x d7216d7be2bbfdf3312695b6e41a4fc7c5e22902
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026112506-afraid-flogging-8ebf@gregkh' --subject-prefix 'PATCH 6.6.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From d7216d7be2bbfdf3312695b6e41a4fc7c5e22902 Mon Sep 17 00:00:00 2001
From: wuyuehong <[email protected]>
Date: Mon, 26 Oct 2026 17:35:00 +0800
Subject: [PATCH] dm snapshot: fix discard_granularity unit in
snapshot_io_hints
snapshot_io_hints() copies the snapshot store's chunk_size, which is in 512-byte sectors, into queue_limits.discard_granularity, which, like all other members of struct queue_limits, is in bytes.
For snapshots with a chunk size smaller than 512 sectors, e.g. the 16 KiB (32-sector) chunks created by the syzkaller reproducer, the resulting granularity is smaller than a single sector. When dm_calculate_queue_limits() then stacks the target limits into the table limits:
blk_stack_limits(limits, &ti_limits, 0)
queue_limit_discard_alignment() truncates that granularity to zero sectors and sector_div() divides by zero:
Oops: divide error: 0000 [#1] SMP KASAN NOPTI RIP: 0010:queue_limit_discard_alignment block/blk-settings.c:645 [inline] RIP: 0010:blk_stack_limits+0x1105/0x2d00 block/blk-settings.c:916 Call Trace: dm_calculate_queue_limits+0x70c/0x1510 drivers/md/dm-table.c:1823 dm_setup_md_queue+0x199/0x4f0 drivers/md/dm.c:2618 table_load+0x4fd/0x8a0 drivers/md/dm-ioctl.c:1661
The divide fault itself only became reachable when commit e924da7d6622 ("block: Drop granularity check in queue_limit_discard_alignment()") removed the check for a sub-sector granularity in v6.13. Kernels before that returned a zero discard alignment instead of faulting, but they still advertised a granularity 512 times too small.
Use the same chunk_size << SECTOR_SHIFT conversion that io_overlaps_chunk() already applies to turn chunk_size into bytes.
drivers/md/dm-snap.c +1 -1
@@ -2414,7 +2414,7 @@ static void snapshot_io_hints(struct dm_target *ti, struct queue_limits *limits) snap = snap_src;/* All discards are split on chunk_size boundary */ - limits->discard_granularity = snap->store->chunk_size; + limits->discard_granularity = snap->store->chunk_size <<SECTOR_SHIFT ; limits->max_hw_discard_sectors = snap->store->chunk_size;up_read (&_origins_lock);
Sasha Levin
maintainerPATCH 1/2 dm: always manage discard support in terms of max_hw_discard_sectors
From: Mike Snitzer <[email protected]>
[ Upstream commit 825d8bbd2f32cb229c3b6653bd454832c3c20acb ]
Commit 4f563a64732d ("block: add a max_user_discard_sectors queue limit") changed block core to set max_discard_sectors to:
min(lim->max_hw_discard_sectors, lim->max_user_discard_sectors)
Since commit 1c0e720228ad ("dm: use queue_limits_set") it was reported dm-thinp was failing in a few fstests (generic/347 and generic/405) with the first WARN_ON_ONCE in dm_cell_key_has_valid_range() being reported, e.g.:
WARNING: CPU: 1 PID: 30 at drivers/md/dm-bio-prison-v1.c:128 dm_cell_key_has_valid_range+0x3d/0x50
blk_set_stacking_limits() sets max_user_discard_sectors to UINT_MAX, so given how block core now sets max_discard_sectors (detailed above) it follows that blk_stack_limits() stacks up the underlying device's max_hw_discard_sectors and max_discard_sectors is set to match it. If max_hw_discard_sectors exceeds dm's BIO_PRISON_MAX_RANGE, then dm_cell_key_has_valid_range() will trigger the warning with:WARN_ON_ONCE(key->block_end - key->block_begin > BIO_PRISON_MAX_RANGE)
Aside from this warning, the discard will fail. Fix this and other DM issues by governing discard support in terms of max_hw_discard_sectors instead of max_discard_sectors.
Stable-dep-of: d7216d7be2bb ("dm snapshot: fix discard_granularity unit in snapshot_io_hints")
--- drivers/md/dm-cache-target.c | 5 ++--- drivers/md/dm-clone-target.c | 4 ++-- drivers/md/dm-log-writes.c | 2 +- drivers/md/dm-snap.c | 2 +- drivers/md/dm-target.c | 1 - drivers/md/dm-thin.c | 4 ++-- drivers/md/dm-zero.c | 1 - drivers/md/dm-zoned-target.c | 1 - drivers/md/dm.c | 2 +- 9 files changed, 9 insertions(+), 13 deletions(-)
drivers/md/dm-cache-target.c +2 -3
@@ -3515,8 +3515,8 @@ static void set_discard_limits(struct cache *cache, struct queue_limits *limits)if (!cache->features.discard_passdown) {/* No passdown is done so setting own virtual limits */ - limits->max_discard_sectors =min_t (sector_t, cache->discard_block_size *1024 , - cache->origin_sectors); + limits->max_hw_discard_sectors =min_t (sector_t, cache->discard_block_size *1024 , + cache->origin_sectors); limits->discard_granularity = cache->discard_block_size <<SECTOR_SHIFT ;return ; }
@@ -3525,7 +3525,6 @@ static void set_discard_limits(struct cache *cache, struct queue_limits *limits)* cache_iterate_devices() is stacking both origin and fast device limits * but discards aren't passed to fast device, so inherit origin's limits. */ - limits->max_discard_sectors = origin_limits->max_discard_sectors; limits->max_hw_discard_sectors = origin_limits->max_hw_discard_sectors; limits->discard_granularity = origin_limits->discard_granularity; limits->discard_alignment = origin_limits->discard_alignment;
drivers/md/dm-clone-target.c +2 -2
@@ -2050,7 +2050,8 @@ static void set_discard_limits(struct clone *clone, struct queue_limits *limits)if (!test_bit (DM_CLONE_DISCARD_PASSDOWN , &clone->flags)) {/* No passdown is done so we set our own virtual limits */ limits->discard_granularity = clone->region_size <<SECTOR_SHIFT ; - limits->max_discard_sectors =round_down (UINT_MAX >>SECTOR_SHIFT , clone->region_size); + limits->max_hw_discard_sectors =round_down (UINT_MAX >>SECTOR_SHIFT , + clone->region_size);return ; }
@@ -2059,7 +2060,6 @@ static void set_discard_limits(struct clone *clone, struct queue_limits *limits)* device limits but discards aren't passed to the source device, so * inherit destination's limits. */ - limits->max_discard_sectors = dest_limits->max_discard_sectors; limits->max_hw_discard_sectors = dest_limits->max_hw_discard_sectors; limits->discard_granularity = dest_limits->discard_granularity; limits->discard_alignment = dest_limits->discard_alignment;
drivers/md/dm-log-writes.c +1 -1
@@ -872,7 +872,7 @@ static void log_writes_io_hints(struct dm_target *ti, struct queue_limits *limitif (!bdev_max_discard_sectors (lc->dev->bdev)) { lc->device_supports_discard = false; limits->discard_granularity = lc->sectorsize; - limits->max_discard_sectors = (UINT_MAX >>SECTOR_SHIFT ); + limits->max_hw_discard_sectors = (UINT_MAX >>SECTOR_SHIFT ); } limits->logical_block_size =bdev_logical_block_size (lc->dev->bdev); limits->physical_block_size =bdev_physical_block_size (lc->dev->bdev);
drivers/md/dm-snap.c +1 -1
@@ -2425,7 +2425,7 @@ static void snapshot_io_hints(struct dm_target *ti, struct queue_limits *limits)/* All discards are split on chunk_size boundary */ limits->discard_granularity = snap->store->chunk_size; - limits->max_discard_sectors = snap->store->chunk_size; + limits->max_hw_discard_sectors = snap->store->chunk_size;up_read (&_origins_lock); }
drivers/md/dm-target.c +0 -1
@@ -151,7 +151,6 @@ static void io_err_release_clone_rq(struct request *clone,static void io_err_io_hints (struct dm_target *ti,struct queue_limits *limits) { - limits->max_discard_sectors =UINT_MAX ; limits->max_hw_discard_sectors =UINT_MAX ; limits->discard_granularity =512 ; }
drivers/md/dm-thin.c +2 -2
@@ -4099,7 +4099,7 @@ static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits)if (pt->adjusted_pf.discard_enabled) {disable_discard_passdown_if_not_supported (pt);if (!pt->adjusted_pf.discard_passdown) - limits->max_discard_sectors =0 ; + limits->max_hw_discard_sectors =0 ;/* * The pool uses the same discard limits as the underlying data * device. DM core has already set this up.
@@ -4496,7 +4496,7 @@ static void thin_io_hints(struct dm_target *ti, struct queue_limits *limits)if (pool->pf.discard_enabled) { limits->discard_granularity = pool->sectors_per_block <<SECTOR_SHIFT ; - limits->max_discard_sectors = pool->sectors_per_block *BIO_PRISON_MAX_RANGE ; + limits->max_hw_discard_sectors = pool->sectors_per_block *BIO_PRISON_MAX_RANGE ; } }
drivers/md/dm-zero.c +0 -1
@@ -61,7 +61,6 @@ static int zero_map(struct dm_target *ti, struct bio *bio)static void zero_io_hints (struct dm_target *ti,struct queue_limits *limits) { - limits->max_discard_sectors =UINT_MAX ; limits->max_hw_discard_sectors =UINT_MAX ; limits->discard_granularity =512 ; }
drivers/md/dm-zoned-target.c +0 -1
@@ -1001,7 +1001,6 @@ static void dmz_io_hints(struct dm_target *ti, struct queue_limits *limits) limits->discard_alignment =0 ; limits->discard_granularity =DMZ_BLOCK_SIZE ; - limits->max_discard_sectors = chunk_sectors; limits->max_hw_discard_sectors = chunk_sectors; limits->max_write_zeroes_sectors = chunk_sectors;
drivers/md/dm.c +1 -1
@@ -1073,7 +1073,7 @@ void disable_discard(struct mapped_device *md)struct queue_limits *limits =dm_get_queue_limits (md);/* device doesn't really support DISCARD, disable it */ - limits->max_discard_sectors =0 ; + limits->max_hw_discard_sectors =0 ; }void disable_write_zeroes (struct mapped_device *md)
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/2 dm snapshot: fix discard_granularity unit in snapshot_io_hints
From: wuyuehong <[email protected]>
[ Upstream commit d7216d7be2bbfdf3312695b6e41a4fc7c5e22902 ]
snapshot_io_hints() copies the snapshot store's chunk_size, which is in 512-byte sectors, into queue_limits.discard_granularity, which, like all other members of struct queue_limits, is in bytes.
For snapshots with a chunk size smaller than 512 sectors, e.g. the 16 KiB (32-sector) chunks created by the syzkaller reproducer, the resulting granularity is smaller than a single sector. When dm_calculate_queue_limits() then stacks the target limits into the table limits:
blk_stack_limits(limits, &ti_limits, 0)
queue_limit_discard_alignment() truncates that granularity to zero sectors and sector_div() divides by zero:
Oops: divide error: 0000 [#1] SMP KASAN NOPTI RIP: 0010:queue_limit_discard_alignment block/blk-settings.c:645 [inline] RIP: 0010:blk_stack_limits+0x1105/0x2d00 block/blk-settings.c:916 Call Trace: dm_calculate_queue_limits+0x70c/0x1510 drivers/md/dm-table.c:1823 dm_setup_md_queue+0x199/0x4f0 drivers/md/dm.c:2618 table_load+0x4fd/0x8a0 drivers/md/dm-ioctl.c:1661
The divide fault itself only became reachable when commit e924da7d6622 ("block: Drop granularity check in queue_limit_discard_alignment()") removed the check for a sub-sector granularity in v6.13. Kernels before that returned a zero discard alignment instead of faulting, but they still advertised a granularity 512 times too small.
Use the same chunk_size << SECTOR_SHIFT conversion that io_overlaps_chunk() already applies to turn chunk_size into bytes.
--- drivers/md/dm-snap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/dm-snap.c +1 -1
@@ -2424,7 +2424,7 @@ static void snapshot_io_hints(struct dm_target *ti, struct queue_limits *limits) snap = snap_src;/* All discards are split on chunk_size boundary */ - limits->discard_granularity = snap->store->chunk_size; + limits->discard_granularity = snap->store->chunk_size <<SECTOR_SHIFT ; limits->max_hw_discard_sectors = snap->store->chunk_size;up_read (&_origins_lock);
Signature · 1 line
2.53.0