mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] block: stack max_user_discard_sectors
@ 2026-11-09 19:39 Benjamin Marzinski
  2026-11-20 15:40 ` Christoph Hellwig
  0 siblings, 1 reply; 6+ messages in thread
From: Benjamin Marzinski @ 2026-11-09 19:39 UTC (permalink / raw)
  To: Jens Axboe
  Cc: Christoph Hellwig, Hannes Reinecke, Keith Busch,
	Martin K . Petersen, linux-block, linux-kernel

blk_validate_limits() sets a device's max_discard_sectors partially
based on the value of max_user_discard_sectors. For this to be set
correctly on a stacked device, max_user_discard_sectors needs to be set
to the smallest value of its underlying devices. Otherwise the stacked
device can pass down discards that are over the size limit of the
underlying device. This is a particular problem for request-based
dm-multipath devices, where the underlying device does not have a chance
to split the discard.

Fixes: 4f563a64732d ("block: add a max_user_discard_sectors queue limit")
Signed-off-by: Benjamin Marzinski <bmarzins@redhat.com>
---
 block/blk-settings.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/block/blk-settings.c b/block/blk-settings.c
index 1f5ee2453269..1f02a867061e 100644
--- a/block/blk-settings.c
+++ b/block/blk-settings.c
@@ -919,6 +919,8 @@ 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);
+		t->max_user_discard_sectors = min_not_zero(t->max_user_discard_sectors,
+							   b->max_user_discard_sectors);
 		t->max_hw_discard_sectors = min_not_zero(t->max_hw_discard_sectors,
 							 b->max_hw_discard_sectors);
 		t->discard_granularity = max(t->discard_granularity,
-- 
2.56.0


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

* Re: [PATCH] block: stack max_user_discard_sectors
  2026-11-09 19:39 [PATCH] block: stack max_user_discard_sectors Benjamin Marzinski
@ 2026-11-20 15:40 ` Christoph Hellwig
  2026-11-20 19:43   ` Benjamin Marzinski
  0 siblings, 1 reply; 6+ messages in thread
From: Christoph Hellwig @ 2026-11-20 15:40 UTC (permalink / raw)
  To: Benjamin Marzinski
  Cc: Jens Axboe, Christoph Hellwig, Hannes Reinecke, Keith Busch,
	Martin K . Petersen, linux-block, linux-kernel

On Mon, Nov 09, 2026 at 02:39:27PM -0500, Benjamin Marzinski wrote:
>  		t->max_discard_sectors = min_not_zero(t->max_discard_sectors,
>  						      b->max_discard_sectors);
> +		t->max_user_discard_sectors = min_not_zero(t->max_user_discard_sectors,
> +							   b->max_user_discard_sectors);

I don't think we should take the underlying max_user_discard_sectors
into account here, just max_discard_sectors.  But otherwise yes, this
should be stacked.

Please avoid the overly long lines, though.

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

* Re: [PATCH] block: stack max_user_discard_sectors
  2026-11-20 15:40 ` Christoph Hellwig
@ 2026-11-20 19:43   ` Benjamin Marzinski
  2026-11-21  0:13     ` Christoph Hellwig
  0 siblings, 1 reply; 6+ messages in thread
From: Benjamin Marzinski @ 2026-11-20 19:43 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: Jens Axboe, Hannes Reinecke, Keith Busch, Martin K . Petersen,
	linux-block, linux-kernel

On Fri, Nov 20, 2026 at 04:40:40PM +0100, Christoph Hellwig wrote:
> On Mon, Nov 09, 2026 at 02:39:27PM -0500, Benjamin Marzinski wrote:
> >  		t->max_discard_sectors = min_not_zero(t->max_discard_sectors,
> >  						      b->max_discard_sectors);
> > +		t->max_user_discard_sectors = min_not_zero(t->max_user_discard_sectors,
> > +							   b->max_user_discard_sectors);
> 
> I don't think we should take the underlying max_user_discard_sectors
> into account here, just max_discard_sectors.

Is that to handle devices that call blk_queue_disable_discard()? I
didn't think about that. Makes sense.

> But otherwise yes, this
> should be stacked.
> 
> Please avoid the overly long lines, though.

Sure.

-Ben


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

* Re: [PATCH] block: stack max_user_discard_sectors
  2026-11-20 19:43   ` Benjamin Marzinski
@ 2026-11-21  0:13     ` Christoph Hellwig
  2026-11-22 19:43       ` Benjamin Marzinski
  0 siblings, 1 reply; 6+ messages in thread
From: Christoph Hellwig @ 2026-11-21  0:13 UTC (permalink / raw)
  To: Benjamin Marzinski
  Cc: Christoph Hellwig, Jens Axboe, Hannes Reinecke, Keith Busch,
	Martin K . Petersen, linux-block, linux-kernel

On Fri, Nov 20, 2026 at 02:43:59PM -0500, Benjamin Marzinski wrote:
> > I don't think we should take the underlying max_user_discard_sectors
> > into account here, just max_discard_sectors.
> 
> Is that to handle devices that call blk_queue_disable_discard()? I
> didn't think about that. Makes sense.

That too.  But also because user limits on lower devices shouldn't
really affect upper devices for consistency, as new updates on the
lower device after the upper one was created don't get picked up
either.


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

* Re: [PATCH] block: stack max_user_discard_sectors
  2026-11-21  0:13     ` Christoph Hellwig
@ 2026-11-22 19:43       ` Benjamin Marzinski
  2026-11-22 21:56         ` [PATCH v2] block: stack max_hw_discard_sectors on bottom max_discard_sectors Benjamin Marzinski
  0 siblings, 1 reply; 6+ messages in thread
From: Benjamin Marzinski @ 2026-11-22 19:43 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: Jens Axboe, Hannes Reinecke, Keith Busch, Martin K . Petersen,
	linux-block, linux-kernel

On Sat, Nov 21, 2026 at 01:13:34AM +0100, Christoph Hellwig wrote:
> On Fri, Nov 20, 2026 at 02:43:59PM -0500, Benjamin Marzinski wrote:
> > > I don't think we should take the underlying max_user_discard_sectors
> > > into account here, just max_discard_sectors.
> > 
> > Is that to handle devices that call blk_queue_disable_discard()? I
> > didn't think about that. Makes sense.
> 
> That too.  But also because user limits on lower devices shouldn't
> really affect upper devices for consistency, as new updates on the
> lower device after the upper one was created don't get picked up
> either.

Um... Now it doesn't make sense again. 

If I do something like:

t->max_user_discard_sectors = min(t->max_user_discard_sectors,
                                  b->max_discard_sectors);

That mean that changes to underlying device user limits can effect upper
device user limits (since adjusting max_user_discard_sectors
via sysfs automatically adjusts max_discard_sectors).

Also, while this does make blk_stack_limits() disable discards on upper
devices that have a lower device that dissabled discards with
blk_queue_disable_discard(), it doesn't disable discards on upper
devices that have lower devices that have discards disabled normally
(since blk_validate_limits() sets discard_granularity to 0 for devices
that set max_discard_sectors to 0, and this part of blk_stack_limits()
only runs if the bottom device has discard_granularity != 0). That
seems wrong. So perhaps handling blk_queue_disable_discard() isn't the
right answer. Alternatively, blk_stack_limits() could disable discards
on upper device is any of the lower device has max_discard_sectors. But
I assume there's a good reason why it currently doesn't.

Additionally, if we use max_user_discard_sectors to clamp the upper
device's max_discard_sectors to the lower device's. It allows users to
just override it by changing max_user_discard_sectors to something that
doesn't work with the underlying device. That also seems wrong.

So what do you think about doing this instead:

t->max_hw_discard_sectors = min(t->max_hw_discard_sectors,
                                b->max_discard_sectors);

This doesn't handle blk_queue_disable_discard(), but like I said, the
other patch didn't handle it well either. It also keep users from being
able to send too large discards by setting max_user_discard_sectors too
high on stacked devices. I'll send a v2 that does this.

-Ben


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

* [PATCH v2] block: stack max_hw_discard_sectors on bottom max_discard_sectors
  2026-11-22 19:43       ` Benjamin Marzinski
@ 2026-11-22 21:56         ` Benjamin Marzinski
  0 siblings, 0 replies; 6+ messages in thread
From: Benjamin Marzinski @ 2026-11-22 21:56 UTC (permalink / raw)
  To: Jens Axboe, Christoph Hellwig
  Cc: Hannes Reinecke, Keith Busch, Martin K . Petersen,
	Mikulas Patocka, Mike Snitzer, linux-block, linux-kernel

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>
---

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	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-22 21:56 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-09 19:39 [PATCH] block: stack max_user_discard_sectors Benjamin Marzinski
2026-11-20 15:40 ` Christoph Hellwig
2026-11-20 19:43   ` Benjamin Marzinski
2026-11-21  0:13     ` Christoph Hellwig
2026-11-22 19:43       ` Benjamin Marzinski
2026-11-22 21:56         ` [PATCH v2] block: stack max_hw_discard_sectors on bottom max_discard_sectors Benjamin Marzinski

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

all inboxes | Powered by JetHome®