mirror of https://lore.kernel.org/lkml/
 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 15:24 ` Christoph Hellwig
  0 siblings, 1 reply; 2+ 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] 2+ 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 15:24 ` Christoph Hellwig
  0 siblings, 0 replies; 2+ 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] 2+ messages in thread

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

Thread overview: 2+ 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 15:24 ` Christoph Hellwig

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®