The dm-cache and dm-clone target's io_hints currently set
max_hw_discard_sector based on the origin/destination device's
max_hw_discard_sector value. This can allow them to send discard
requests larger than those underlying device's max_discard_sector value,
forcing the underlying device to split the discard again.

blk_validate_limits() sets a device's max_discard_sectors from the
minimum of its max_hw_discard_sectors and max_user_discard_sectors. If a
user sets the max_user_discard_sector to a smaller value on the
uderlying device, the current set_discard_limits() code in both dm-cache
and dm-clone will ignore that lower limit for setting the device's
max_hw_discard_sectors. Stacked devices to not inherit the
max_user_discard_sectors of their underlying devices, and DM always sets
max_user_discard_sectors to UNIT_MAX in dm_set_stacking_limits(), so
max_discard_sectors starts out equal to max_hw_discard_sectors. The
disable_passdown_if_not_supported() functions in these targets already
correctly use the underlying device's max_discard_sectors value for
checking their discard support.

Fix this by setting max_hw_discard_sector based on the underlying
device's max_discard_sectors value. This limits to cache and clone
discard sectors to the actual limits of the underlying devices.

Signed-off-by: Benjamin Marzinski <[email protected]>
---

This patch is related to
https://lore.kernel.org/all/[email protected]/
which sets max_hw_discard_sectors to the underlying device's
max_discard_sectors during regular device stacking. But this patch
is independent of it and can go in without it.

 drivers/md/dm-cache-target.c | 2 +-
 drivers/md/dm-clone-target.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/md/dm-cache-target.c b/drivers/md/dm-cache-target.c
index 850c4b651f09..969d60cf653e 100644
--- a/drivers/md/dm-cache-target.c
+++ b/drivers/md/dm-cache-target.c
@@ -3534,7 +3534,7 @@ 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_hw_discard_sectors = origin_limits->max_hw_discard_sectors;
+       limits->max_hw_discard_sectors = origin_limits->max_discard_sectors;
        limits->discard_granularity = origin_limits->discard_granularity;
        limits->discard_alignment = origin_limits->discard_alignment;
 }
diff --git a/drivers/md/dm-clone-target.c b/drivers/md/dm-clone-target.c
index bc4a5ce4c81a..d634d9111580 100644
--- a/drivers/md/dm-clone-target.c
+++ b/drivers/md/dm-clone-target.c
@@ -2041,7 +2041,7 @@ 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_hw_discard_sectors = dest_limits->max_hw_discard_sectors;
+       limits->max_hw_discard_sectors = dest_limits->max_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;
-- 
2.56.0


Reply via email to