* [PATCH v2 RESEND] block: stack max_hw_discard_sectors on bottom max_discard_sectors
@ 2026-11-23 19:30 Benjamin Marzinski
2026-11-23 19:41 ` sashiko-bot
0 siblings, 1 reply; 3+ messages in thread
From: Benjamin Marzinski @ 2026-11-23 19:30 UTC (permalink / raw)
To: Jens Axboe, Christoph Hellwig
Cc: Hannes Reinecke, Keith Busch, Martin K . Petersen,
Mikulas Patocka, Mike Snitzer, linux-block, linux-kernel,
dm-devel
blk_validate_limits() sets a device's max_discard_sectors from the
minimum of max_hw_discard_sectors and max_user_discard_sectors. For a
stacked device to correctly limit max_discard_sectors to a value that
the lower device can handle, max_hw_discard_sectors should be based on
the lower device's max_discard_sectors. Otherwise the stacked device can
pass down discards that are over the size limit of a lower device whose
max_discard_sectors is smaller than its max_hw_discard_sectors. This is
a particular problem for request-based dm-multipath devices, where the
underlying device does not have a chance to split the discard.
Use the upper device's max_hw_discard_sectors to limit this, instead of
max_user_discard_sectors, to ensure that it respects the lower device's
limits, including their user settings. A user raising the upper device's
max_user_discard_sectors should not be able to make it send larger
discards than the underlying devices can handle.
Device-mapper does stacking in two passes. Each target calculates its
limits from its underlying devices, and applies any target io_hints.
Then the overall device calculates its limits from all its targets. For
the overall limits to be stacked correctly with this change, each target
needs to set its max_discard_sectors to its max_hw_discard_sectors
before being stacked into the overall limit. Otherwise the
max_hw_discard_sectors io_hints values from the targets will be ignored.
Fixes: 4f563a64732d ("block: add a max_user_discard_sectors queue limit")
Signed-off-by: Benjamin Marzinski <bmarzins@redhat.com>
---
Resent because I forgot to include dm-devel. Sorry for the noise. See:
https://lore.kernel.org/all/20261109193927.595104-1-bmarzins@redhat.com/
for the original thread.
Changes from v1 (sent as "block: stack max_user_discard_sectors")
- switch from stacking max_user_discard_sectors to
max_hw_discard_sectors
- Adapt device-mapper to the change.
block/blk-settings.c | 6 +++++-
drivers/md/dm-table.c | 5 ++++-
2 files changed, 9 insertions(+), 2 deletions(-)
diff --git a/block/blk-settings.c b/block/blk-settings.c
index 1f5ee2453269..52ac00c59a0e 100644
--- a/block/blk-settings.c
+++ b/block/blk-settings.c
@@ -919,8 +919,12 @@ int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
t->max_discard_sectors = min_not_zero(t->max_discard_sectors,
b->max_discard_sectors);
+ /*
+ * Use bottom device's max_discard_sectors to limit the top
+ * device by what the bottom device can actually handle.
+ */
t->max_hw_discard_sectors = min_not_zero(t->max_hw_discard_sectors,
- b->max_hw_discard_sectors);
+ b->max_discard_sectors);
t->discard_granularity = max(t->discard_granularity,
b->discard_granularity);
t->discard_alignment = lcm_not_zero(t->discard_alignment, alignment) %
diff --git a/drivers/md/dm-table.c b/drivers/md/dm-table.c
index 48f25ab6b7fa..4cabe2645094 100644
--- a/drivers/md/dm-table.c
+++ b/drivers/md/dm-table.c
@@ -1834,8 +1834,11 @@ int dm_calculate_queue_limits(struct dm_table *t,
combine_limits:
/*
* Merge this target's queue limits into the overall limits
- * for the table.
+ * for the table. In blk_stack_limits() the upper device's
+ * max_hw_discard_sectors is based on the lower device's
+ * max_discard_sectors, so copy this value over first.
*/
+ ti_limits.max_discard_sectors = ti_limits.max_hw_discard_sectors;
if (blk_stack_limits(limits, &ti_limits, 0) < 0)
DMWARN("%s: adding target device (start sect %llu len %llu) "
"caused an alignment inconsistency",
--
2.56.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH v2 RESEND] block: stack max_hw_discard_sectors on bottom max_discard_sectors
2026-11-23 19:30 [PATCH v2 RESEND] block: stack max_hw_discard_sectors on bottom max_discard_sectors Benjamin Marzinski
@ 2026-11-23 19:41 ` sashiko-bot
2026-11-23 20:32 ` Benjamin Marzinski
0 siblings, 1 reply; 3+ messages in thread
From: sashiko-bot @ 2026-11-23 19:41 UTC (permalink / raw)
To: Benjamin Marzinski, Jens Axboe, Christoph Hellwig
Cc: Hannes Reinecke, Keith Busch, Martin K . Petersen,
Mikulas Patocka, Mike Snitzer, dm-devel
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] dm: set_discard_limits() copies underlying
max_hw_discard_sectors
--
commit 54b96a4a0a953173383ea0e1a1b7ffcb363bc984
Author: Benjamin Marzinski <bmarzins@redhat.com>
block: stack max_hw_discard_sectors on bottom max_discard_sectors
This commit stacks the upper device's max_hw_discard_sectors from the
bottom device's max_discard_sectors in blk_stack_limits() and updates
dm_calculate_queue_limits() to copy each target's max_hw_discard_sectors
into max_discard_sectors before stacking it into the table limits.
> diff --git a/drivers/md/dm-table.c b/drivers/md/dm-table.c
> index 48f25ab6b7fa6..4cabe26450941 100644
> --- a/drivers/md/dm-table.c
> +++ b/drivers/md/dm-table.c
> @@ -1834,8 +1834,11 @@ int dm_calculate_queue_limits(struct dm_table *t,
> combine_limits:
> /*
> * Merge this target's queue limits into the overall limits
> - * for the table.
> + * for the table. In blk_stack_limits() the upper device's
> + * max_hw_discard_sectors is based on the lower device's
> + * max_discard_sectors, so copy this value over first.
> */
> + ti_limits.max_discard_sectors = ti_limits.max_hw_discard_sectors;
> if (blk_stack_limits(limits, &ti_limits, 0) < 0)
> DMWARN("%s: adding target device (start sect %llu len %llu) "
> "caused an alignment inconsistency",
[Severity: Medium]
Should set_discard_limits() in dm-cache and dm-clone also use the
underlying device's max_discard_sectors instead of max_hw_discard_sectors?
When discard passdown is enabled, cache_io_hints() and clone_io_hints()
call set_discard_limits() to replace the target's stacked discard limits
with those of the origin or destination device alone:
drivers/md/dm-cache-target.c:set_discard_limits() {
...
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:set_discard_limits() {
...
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;
limits->max_discard_segments = dest_limits->max_discard_segments;
}
Copying ti_limits.max_hw_discard_sectors into ti_limits.max_discard_sectors
at combine_limits propagates origin_limits->max_hw_discard_sectors (or
dest_limits->max_hw_discard_sectors) into the table limits, bypassing any
smaller max_discard_sectors configured on the origin or destination device.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261123193000.3419445-1-bmarzins@redhat.com?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH v2 RESEND] block: stack max_hw_discard_sectors on bottom max_discard_sectors
2026-11-23 19:41 ` sashiko-bot
@ 2026-11-23 20:32 ` Benjamin Marzinski
0 siblings, 0 replies; 3+ messages in thread
From: Benjamin Marzinski @ 2026-11-23 20:32 UTC (permalink / raw)
To: sashiko-reviews
Cc: Jens Axboe, Christoph Hellwig, Hannes Reinecke, Keith Busch,
Martin K . Petersen, Mikulas Patocka, Mike Snitzer, dm-devel
On Mon, Nov 23, 2026 at 07:41:43PM +0000, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential
> issue to consider:
> - [Medium] dm: set_discard_limits() copies underlying
> max_hw_discard_sectors
> --
>
> commit 54b96a4a0a953173383ea0e1a1b7ffcb363bc984
> Author: Benjamin Marzinski <bmarzins@redhat.com>
>
> block: stack max_hw_discard_sectors on bottom max_discard_sectors
>
> This commit stacks the upper device's max_hw_discard_sectors from the
> bottom device's max_discard_sectors in blk_stack_limits() and updates
> dm_calculate_queue_limits() to copy each target's max_hw_discard_sectors
> into max_discard_sectors before stacking it into the table limits.
>
> > diff --git a/drivers/md/dm-table.c b/drivers/md/dm-table.c
> > index 48f25ab6b7fa6..4cabe26450941 100644
> > --- a/drivers/md/dm-table.c
> > +++ b/drivers/md/dm-table.c
> > @@ -1834,8 +1834,11 @@ int dm_calculate_queue_limits(struct dm_table *t,
> > combine_limits:
> > /*
> > * Merge this target's queue limits into the overall limits
> > - * for the table.
> > + * for the table. In blk_stack_limits() the upper device's
> > + * max_hw_discard_sectors is based on the lower device's
> > + * max_discard_sectors, so copy this value over first.
> > */
> > + ti_limits.max_discard_sectors = ti_limits.max_hw_discard_sectors;
> > if (blk_stack_limits(limits, &ti_limits, 0) < 0)
> > DMWARN("%s: adding target device (start sect %llu len %llu) "
> > "caused an alignment inconsistency",
>
> [Severity: Medium]
> Should set_discard_limits() in dm-cache and dm-clone also use the
> underlying device's max_discard_sectors instead of max_hw_discard_sectors?
This is a valid issue, but fixing dm-cache and dm-clone is not dependent
on this patch, and that fix should go in through linux-dm instead of
linux-block, like this patch. I have already sent a separate patch to
dm-devel to fix dm-cache and dm-clone.
https://lore.kernel.org/all/20261123053235.3060754-1-bmarzins@redhat.com/
-Ben
> When discard passdown is enabled, cache_io_hints() and clone_io_hints()
> call set_discard_limits() to replace the target's stacked discard limits
> with those of the origin or destination device alone:
>
> drivers/md/dm-cache-target.c:set_discard_limits() {
> ...
> 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:set_discard_limits() {
> ...
> 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;
> limits->max_discard_segments = dest_limits->max_discard_segments;
> }
>
> Copying ti_limits.max_hw_discard_sectors into ti_limits.max_discard_sectors
> at combine_limits propagates origin_limits->max_hw_discard_sectors (or
> dest_limits->max_hw_discard_sectors) into the table limits, bypassing any
> smaller max_discard_sectors configured on the origin or destination device.
>
> --
> Sashiko AI review · https://sashiko.dev/#/patchset/20261123193000.3419445-1-bmarzins@redhat.com?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-23 20:32 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 19:30 [PATCH v2 RESEND] block: stack max_hw_discard_sectors on bottom max_discard_sectors Benjamin Marzinski
2026-11-23 19:41 ` sashiko-bot
2026-11-23 20:32 ` Benjamin Marzinski
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox