Skip to contentSkip to conversation

dm-bufio: fix wrong count calculation in dm_bufio_issue_discard

PATCH New Mikulas Patocka 1 participant 1 message Jul 10 Device mapper

lore
Share & tools
lore

Mikulas Patocka

author maintainer
#0 lore raw

PATCH 1/1 dm-bufio: fix wrong count calculation in dm_bufio_issue_discard

block_to_sector converts a block number to a sector number and adds c->start to the result. It is inappropriate to use this function for converting the number of blocks to a number to sectors because c->start would be incorrectly added to the result.

Luckily, the only target that uses dm_bufio_issue_discard is dm-ebs, which sets c->start to 0, so this bug is latent.

Assisted-by Claude:claude-opus-4-6
Fixes 6fbeb0048e6b ("dm bufio: implement discard")
---
 drivers/md/dm-bufio.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/md/dm-bufio.c +3 -1
Index: linux-2.6/drivers/md/dm-bufio.c
===================================================================
@@ -2238,7 +2238,9 @@ int dm_bufio_issue_discard(struct dm_buf
 	struct dm_io_region io_reg = {
 		.bdev = c->bdev,
 		.sector = block_to_sector(c, block),
-		.count = block_to_sector(c, count),
+		.count = likely(c->sectors_per_block_bits >= 0) ?
+			count << c->sectors_per_block_bits :
+			count * (c->block_size >> SECTOR_SHIFT),
 	};
 
 	if (WARN_ON_ONCE(dm_bufio_in_request()))