mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v4] block: fix discard alignment for non-power-of-two granularities
@ 2026-11-20 10:17 Hungyu Lin
  2026-11-20 15:22 ` Christoph Hellwig
  2026-11-27 15:00 ` Usama Arif
  0 siblings, 2 replies; 3+ messages in thread
From: Hungyu Lin @ 2026-11-20 10:17 UTC (permalink / raw)
  To: axboe
  Cc: hch, linux-block, linux-kernel, dm-devel, mtsai, stable,
	dennylin0707, hch

bio_discard_limit() uses round_up() and round_down(), which require a
power-of-two alignment. However, discard_granularity need not be a power
of two. For example, dm-thin supports data block sizes that are arbitrary
multiples of 64 KiB and advertises the block size as its granularity.

With a 192 KiB granularity, discarding one complete block at offset
384 KiB is split into 128 KiB and 64 KiB bios. Neither covers a complete
thin block, so dm-thin ignores both and the mapping remains allocated.

Use div_u64_rem() to find the starting sector's remainder without
modifying the sector. Round the maximum bio size down with rounddown()
and subtract the remainder so that the next bio starts on a granularity
boundary. This handles non-power-of-two granularities and avoids a small
initial bio when a large discard starts at an unaligned sector.

When discard is unsupported or disabled, discard_granularity can be zero.
Return BIO_MAX_SECTORS in that case so the existing bio submission checks
can handle unsupported discard requests without dividing by zero.

Fixes: 9b15d109a6b2 ("block: improve discard bio alignment in __blkdev_issue_discard()")
Reported-by: Ming-Hung Tsai <mtsai@redhat.com>
Link: https://lore.kernel.org/r/CALjSBEvjbiGDVewuapWKhGDtaxZwcn4=22raUiBEOyj22rY7-g@mail.gmail.com
Suggested-by: Christoph Hellwig <hch@infradead.org>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Cc: stable@vger.kernel.org
Signed-off-by: Hungyu Lin <dennylin0707@gmail.com>
---
Changes in v4:
- Add Christoph Hellwig's Reviewed-by.

v3: https://lore.kernel.org/r/20261103185909.71843-1-dennylin0707@gmail.com

Changes in v3:
- Use div_u64_rem() instead of sector_div(), as suggested by Christoph
  Hellwig, so the input sector is not modified.
- Subtract the starting-sector remainder from the rounded maximum size
  instead of emitting a short initial bio, following Christoph's proposal.
- Include <linux/math64.h> explicitly and retain v2's zero-granularity guard.

Changes in v2:
- Handle zero discard_granularity before dividing. Leave unsupported
  discard handling to the bio submission path.

v2: https://lore.kernel.org/r/20261026195131.46699-1-dennylin0707@gmail.com

 block/blk-lib.c | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)

diff --git a/block/blk-lib.c b/block/blk-lib.c
index 688bc67cbf7..e98aa483707 100644
--- a/block/blk-lib.c
+++ b/block/blk-lib.c
@@ -3,6 +3,7 @@
  * Functions related to generic helpers functions
  */
 #include <linux/kernel.h>
+#include <linux/math64.h>
 #include <linux/module.h>
 #include <linux/bio.h>
 #include <linux/blkdev.h>
@@ -13,26 +14,25 @@
 static sector_t bio_discard_limit(struct block_device *bdev, sector_t sector)
 {
 	unsigned int discard_granularity = bdev_discard_granularity(bdev);
-	sector_t granularity_aligned_sector;
+	u32 unaligned_sectors;
+
+	if (!discard_granularity)
+		return BIO_MAX_SECTORS;
 
 	if (bdev_is_partition(bdev))
 		sector += bdev->bd_start_sect;
 
-	granularity_aligned_sector =
-		round_up(sector, discard_granularity >> SECTOR_SHIFT);
-
-	/*
-	 * Make sure subsequent bios start aligned to the discard granularity if
-	 * it needs to be split.
-	 */
-	if (granularity_aligned_sector != sector)
-		return granularity_aligned_sector - sector;
-
 	/*
-	 * Align the bio size to the discard granularity to make splitting the bio
-	 * at discard granularity boundaries easier in the driver if needed.
+	 * Align the bio size to the discard granularity to make splitting the
+	 * bio at discard granularity boundaries easier in the driver if needed.
+	 *
+	 * If the current sector is not aligned, shorten the range so that the
+	 * next one is aligned.
 	 */
-	return round_down(BIO_MAX_SIZE, discard_granularity) >> SECTOR_SHIFT;
+	div_u64_rem(sector, discard_granularity >> SECTOR_SHIFT,
+		    &unaligned_sectors);
+	return (rounddown(BIO_MAX_SIZE, discard_granularity) >> SECTOR_SHIFT) -
+		unaligned_sectors;
 }
 
 struct bio *blk_alloc_discard_bio(struct block_device *bdev,

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
-- 
2.50.1 (Apple Git-155)


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

* Re: [PATCH v4] block: fix discard alignment for non-power-of-two granularities
  2026-11-20 10:17 [PATCH v4] block: fix discard alignment for non-power-of-two granularities Hungyu Lin
@ 2026-11-20 15:22 ` Christoph Hellwig
  2026-11-27 15:00 ` Usama Arif
  1 sibling, 0 replies; 3+ messages in thread
From: Christoph Hellwig @ 2026-11-20 15:22 UTC (permalink / raw)
  To: Hungyu Lin
  Cc: axboe, hch, linux-block, linux-kernel, dm-devel, mtsai, stable, hch

FYI, this still looks good, and it would be great to get this into
7.4-rc ASAP.


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

* Re: [PATCH v4] block: fix discard alignment for non-power-of-two granularities
  2026-11-20 10:17 [PATCH v4] block: fix discard alignment for non-power-of-two granularities Hungyu Lin
  2026-11-20 15:22 ` Christoph Hellwig
@ 2026-11-27 15:00 ` Usama Arif
  1 sibling, 0 replies; 3+ messages in thread
From: Usama Arif @ 2026-11-27 15:00 UTC (permalink / raw)
  To: Hungyu Lin
  Cc: Usama Arif, axboe, hch, linux-block, linux-kernel, dm-devel,
	mtsai, stable, hch

On Fri, 20 Nov 2026 10:17:58 +0000 Hungyu Lin <dennylin0707@gmail.com> wrote:

> bio_discard_limit() uses round_up() and round_down(), which require a
> power-of-two alignment. However, discard_granularity need not be a power
> of two. For example, dm-thin supports data block sizes that are arbitrary
> multiples of 64 KiB and advertises the block size as its granularity.
> 
> With a 192 KiB granularity, discarding one complete block at offset
> 384 KiB is split into 128 KiB and 64 KiB bios. Neither covers a complete
> thin block, so dm-thin ignores both and the mapping remains allocated.
> 
> Use div_u64_rem() to find the starting sector's remainder without
> modifying the sector. Round the maximum bio size down with rounddown()
> and subtract the remainder so that the next bio starts on a granularity
> boundary. This handles non-power-of-two granularities and avoids a small
> initial bio when a large discard starts at an unaligned sector.
> 
> When discard is unsupported or disabled, discard_granularity can be zero.
> Return BIO_MAX_SECTORS in that case so the existing bio submission checks
> can handle unsupported discard requests without dividing by zero.
> 
> Fixes: 9b15d109a6b2 ("block: improve discard bio alignment in __blkdev_issue_discard()")
> Reported-by: Ming-Hung Tsai <mtsai@redhat.com>
> Link: https://lore.kernel.org/r/CALjSBEvjbiGDVewuapWKhGDtaxZwcn4=22raUiBEOyj22rY7-g@mail.gmail.com
> Suggested-by: Christoph Hellwig <hch@infradead.org>
> Reviewed-by: Christoph Hellwig <hch@lst.de>
> Cc: stable@vger.kernel.org
> Signed-off-by: Hungyu Lin <dennylin0707@gmail.com>

This also fixes a 7.1 regression for us. Since commit 1029298da365
("nvme: set discard_granularity from NPDG/NPDA"), NVMe namespaces can
report a granularity well above the LBA size (128K on the drives we hit
this on). The old code then split every unaligned discard in two, even
when the range fits in one bio, so XFS online discard of unaligned 1 MiB
extents sent two DSM ranges per extent and about twice the DSM commands
after large unlinks. With this patch it is one range per extent again.

Tested-by: Usama Arif <usama.arif@linux.dev>

> ---
> Changes in v4:
> - Add Christoph Hellwig's Reviewed-by.
> 
> v3: https://lore.kernel.org/r/20261103185909.71843-1-dennylin0707@gmail.com
> 
> Changes in v3:
> - Use div_u64_rem() instead of sector_div(), as suggested by Christoph
>   Hellwig, so the input sector is not modified.
> - Subtract the starting-sector remainder from the rounded maximum size
>   instead of emitting a short initial bio, following Christoph's proposal.
> - Include <linux/math64.h> explicitly and retain v2's zero-granularity guard.
> 
> Changes in v2:
> - Handle zero discard_granularity before dividing. Leave unsupported
>   discard handling to the bio submission path.
> 
> v2: https://lore.kernel.org/r/20261026195131.46699-1-dennylin0707@gmail.com
> 
>  block/blk-lib.c | 28 ++++++++++++++--------------
>  1 file changed, 14 insertions(+), 14 deletions(-)
> 
> diff --git a/block/blk-lib.c b/block/blk-lib.c
> index 688bc67cbf7..e98aa483707 100644
> --- a/block/blk-lib.c
> +++ b/block/blk-lib.c
> @@ -3,6 +3,7 @@
>   * Functions related to generic helpers functions
>   */
>  #include <linux/kernel.h>
> +#include <linux/math64.h>
>  #include <linux/module.h>
>  #include <linux/bio.h>
>  #include <linux/blkdev.h>
> @@ -13,26 +14,25 @@
>  static sector_t bio_discard_limit(struct block_device *bdev, sector_t sector)
>  {
>  	unsigned int discard_granularity = bdev_discard_granularity(bdev);
> -	sector_t granularity_aligned_sector;
> +	u32 unaligned_sectors;
> +
> +	if (!discard_granularity)
> +		return BIO_MAX_SECTORS;
>  
>  	if (bdev_is_partition(bdev))
>  		sector += bdev->bd_start_sect;
>  
> -	granularity_aligned_sector =
> -		round_up(sector, discard_granularity >> SECTOR_SHIFT);
> -
> -	/*
> -	 * Make sure subsequent bios start aligned to the discard granularity if
> -	 * it needs to be split.
> -	 */
> -	if (granularity_aligned_sector != sector)
> -		return granularity_aligned_sector - sector;
> -
>  	/*
> -	 * Align the bio size to the discard granularity to make splitting the bio
> -	 * at discard granularity boundaries easier in the driver if needed.
> +	 * Align the bio size to the discard granularity to make splitting the
> +	 * bio at discard granularity boundaries easier in the driver if needed.
> +	 *
> +	 * If the current sector is not aligned, shorten the range so that the
> +	 * next one is aligned.
>  	 */
> -	return round_down(BIO_MAX_SIZE, discard_granularity) >> SECTOR_SHIFT;
> +	div_u64_rem(sector, discard_granularity >> SECTOR_SHIFT,
> +		    &unaligned_sectors);
> +	return (rounddown(BIO_MAX_SIZE, discard_granularity) >> SECTOR_SHIFT) -
> +		unaligned_sectors;
>  }
>  
>  struct bio *blk_alloc_discard_bio(struct block_device *bdev,
> 
> base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
> -- 
> 2.50.1 (Apple Git-155)
> 
> 

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

end of thread, other threads:[~2026-11-27 15:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-20 10:17 [PATCH v4] block: fix discard alignment for non-power-of-two granularities Hungyu Lin
2026-11-20 15:22 ` Christoph Hellwig
2026-11-27 15:00 ` Usama Arif

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®