mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Benjamin Marzinski <bmarzins@redhat.com>
To: Jens Axboe <axboe@kernel.dk>, Christoph Hellwig <hch@lst.de>
Cc: Hannes Reinecke <hare@suse.de>, Keith Busch <kbusch@kernel.org>,
	"Martin K . Petersen" <mkp@kernel.org>,
	Mikulas Patocka <mpatocka@redhat.com>,
	Mike Snitzer <snitzer@kernel.org>,
	linux-block@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v2] block: stack max_hw_discard_sectors on bottom max_discard_sectors
Date: Sun, 22 Nov 2026 16:56:41 -0500	[thread overview]
Message-ID: <20261122215641.2865290-1-bmarzins@redhat.com> (raw)
In-Reply-To: <awNF-dV2TacxpUqf@redhat.com>

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


      reply	other threads:[~2026-11-22 21:56 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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         ` Benjamin Marzinski [this message]

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=20261122215641.2865290-1-bmarzins@redhat.com \
    --to=bmarzins@redhat.com \
    --cc=axboe@kernel.dk \
    --cc=hare@suse.de \
    --cc=hch@lst.de \
    --cc=kbusch@kernel.org \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mkp@kernel.org \
    --cc=mpatocka@redhat.com \
    --cc=snitzer@kernel.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

all inboxes | Powered by JetHome®