All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] block: fix divide by zero in queue_limit_discard_alignment()
@ 2026-11-20 10:30 tongzhiwei
  2026-11-20 10:49 ` sashiko-bot
  2026-11-20 15:24 ` Christoph Hellwig
  0 siblings, 2 replies; 3+ messages in thread
From: tongzhiwei @ 2026-11-20 10:30 UTC (permalink / raw)
  To: Jens Axboe
  Cc: tongzhiwei, linux-block, linux-kernel, dm-devel, syzkaller-bugs,
	syzbot+1288484b13bf6af3620d


NOTE: this commit is worked completely by LLM

Commit e924da7d6622 ("block: Drop granularity check in
queue_limit_discard_alignment()") removed the check for a sub-sector
discard granularity on the assumption that lim->discard_granularity
is always at least SECTOR_SIZE.

That assumption does not hold for every queue: a backing device can
report a non-zero discard granularity smaller than 512 bytes.  When
blk_stack_limits() stacks such limits, only the non-zero granularity
is checked, and queue_limit_discard_alignment() shifts the value down
to zero and divides by it:

  Oops: divide error: 0000 [#1] SMP KASAN NOPTI
  RIP: 0010:queue_limit_discard_alignment block/blk-settings.c:645 [inline]
  RIP: 0010:blk_stack_limits+0x1105/0x2d00 block/blk-settings.c:916
  dm_calculate_queue_limits+0x70c/0x1510 drivers/md/dm-table.c:1823
  dm_setup_md_queue+0x199/0x4f0 drivers/md/dm.c:2618
  table_load+0x4fd/0x8a0 drivers/md/dm-ioctl.c:1661

Restore the check, which was originally added in commit 59771079c18c
("blk: avoid divide-by-zero with zero discard granularity") and had
survived the move of the helper into block/blk-settings.c: with a
sub-sector granularity there is no meaningful discard alignment to
compute.

Fixes: e924da7d6622 ("block: Drop granularity check in queue_limit_discard_alignment()")
Reported-by: syzbot+1288484b13bf6af3620d@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=1288484b13bf6af3620d
Signed-off-by: tongzhiwei <tongzhiwei@cmss.chinamobile.com>
---
 block/blk-settings.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/block/blk-settings.c b/block/blk-settings.c
--- a/block/blk-settings.c
+++ b/block/blk-settings.c
@@ -643,6 +643,10 @@
 	alignment = lim->discard_alignment >> SECTOR_SHIFT;
 	granularity = lim->discard_granularity >> SECTOR_SHIFT;
 
+	/* sub-sector granularity: no meaningful alignment, avoid div by 0 */
+	if (!granularity)
+		return 0;
+
 	/* Offset of the partition start in 'granularity' sectors */
 	offset = sector_div(sector, granularity);

-- 
2.43.0



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

* Re: [PATCH] block: fix divide by zero in queue_limit_discard_alignment()
  2026-11-20 10:30 [PATCH] block: fix divide by zero in queue_limit_discard_alignment() tongzhiwei
@ 2026-11-20 10:49 ` sashiko-bot
  2026-11-20 15:24 ` Christoph Hellwig
  1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-20 10:49 UTC (permalink / raw)
  To: tongzhiwei, Jens Axboe
  Cc: dm-devel, syzkaller-bugs, syzbot+1288484b13bf6af3620d

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] block: misdiagnosed sub-sector check in
  queue_limit_discard_alignment()
--

commit 863891ebff09d5d7c6b856e9fe20f0c7b376f993
Author: tongzhiwei <tongzhiwei@cmss.chinamobile.com>

block: fix divide by zero in queue_limit_discard_alignment()

This commit restores the zero check for the sector-shifted discard
granularity in queue_limit_discard_alignment() to avoid a divide by zero
when discard_granularity is smaller than a sector.

> diff --git a/block/blk-settings.c b/block/blk-settings.c
> index 1f5ee2453269f..a0487226f48b5 100644
> --- a/block/blk-settings.c
> +++ b/block/blk-settings.c
> @@ -643,6 +643,10 @@ static unsigned int queue_limit_discard_alignment(
>  	alignment = lim->discard_alignment >> SECTOR_SHIFT;
>  	granularity = lim->discard_granularity >> SECTOR_SHIFT;
>  
> +	/* sub-sector granularity: no meaningful alignment, avoid div by 0 */
> +	if (!granularity)
> +		return 0;

[Severity: Medium]
Can a validated queue ever have a non-zero discard_granularity smaller
than SECTOR_SIZE, or does this check only paper over callers passing
unvalidated sector units instead of bytes?

blk_validate_limits() enforces physical_block_size >= logical_block_size
>= SECTOR_SIZE and clamps discard_granularity to at least
physical_block_size whenever max_discard_sectors is non-zero:

block/blk-settings.c:blk_validate_limits() {
	...
	if (lim->max_discard_sectors)
		lim->discard_granularity =
			max(lim->discard_granularity, lim->physical_block_size);
	else
		lim->discard_granularity = 0;
	...
}

The syzbot crash cited in the commit message happened because
dm_calculate_queue_limits() passed unvalidated target limits from
snapshot_io_hints() to blk_stack_limits(), and snapshot_io_hints()
stored chunk_size in sectors rather than bytes.  That root cause was
already fixed by d7216d7be2bb ("dm snapshot: fix discard_granularity
unit in snapshot_io_hints"), so returning 0 here is redundant and would
silently mask future callers that pass sector counts in
discard_granularity while leaving the bogus sub-sector value in the
stacked limits.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261120103040.610-1-tongzhiwei@cmss.chinamobile.com?part=1

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

* Re: [PATCH] block: fix divide by zero in queue_limit_discard_alignment()
  2026-11-20 10:30 [PATCH] block: fix divide by zero in queue_limit_discard_alignment() tongzhiwei
  2026-11-20 10:49 ` sashiko-bot
@ 2026-11-20 15:24 ` Christoph Hellwig
  1 sibling, 0 replies; 3+ messages in thread
From: Christoph Hellwig @ 2026-11-20 15:24 UTC (permalink / raw)
  To: tongzhiwei
  Cc: Jens Axboe, linux-block, linux-kernel, dm-devel, syzkaller-bugs,
	syzbot+1288484b13bf6af3620d

On Fri, Nov 20, 2026 at 06:30:13PM +0800, tongzhiwei wrote:
> Commit e924da7d6622 ("block: Drop granularity check in
> queue_limit_discard_alignment()") removed the check for a sub-sector
> discard granularity on the assumption that lim->discard_granularity
> is always at least SECTOR_SIZE.
> 
> That assumption does not hold for every queue: a backing device can
> report a non-zero discard granularity smaller than 512 bytes.  When
> blk_stack_limits() stacks such limits, only the non-zero granularity
> is checked, and queue_limit_discard_alignment() shifts the value down
> to zero and divides by it:

Well, then let's fix the setup time to reject this.  Because a discard
granularity less than a sector by definition makes no sense whatsoever.


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

end of thread, other threads:[~2026-11-20 15:24 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:30 [PATCH] block: fix divide by zero in queue_limit_discard_alignment() tongzhiwei
2026-11-20 10:49 ` sashiko-bot
2026-11-20 15:24 ` Christoph Hellwig

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.