Skip to content

Commit 5f3bd90

Browse files
Matthew Wilcox (Oracle)akpm00
authored andcommitted
buffer: fix grow_buffers() for block size > PAGE_SIZE
We must not shift by a negative number so work in terms of a byte offset to avoid the awkward shift left-or-right-depending-on-sign option. This means we need to use check_mul_overflow() to ensure that a large block number does not result in a wrap. Link: https://lkml.kernel.org/r/20231109210608.2252323-4-willy@infradead.org Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org> Signed-off-by: Nathan Chancellor <nathan@kernel.org> Cc: Hannes Reinecke <hare@suse.de> Cc: Luis Chamberlain <mcgrof@kernel.org> Cc: Pankaj Raghav <p.raghav@samsung.com> Cc: Ryusuke Konishi <konishi.ryusuke@gmail.com> [nathan@kernel.org: add cast in grow_buffers() to avoid a multiplication libcall] Link: https://lkml.kernel.org/r/20231128-avoid-muloti4-grow_buffers-v1-1-bc3d0f0ec483@kernel.org Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 382497a commit 5f3bd90

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

fs/buffer.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,26 +1085,21 @@ static bool grow_dev_folio(struct block_device *bdev, sector_t block,
10851085
static bool grow_buffers(struct block_device *bdev, sector_t block,
10861086
unsigned size, gfp_t gfp)
10871087
{
1088-
pgoff_t index;
1089-
int sizebits;
1090-
1091-
sizebits = PAGE_SHIFT - __ffs(size);
1092-
index = block >> sizebits;
1088+
loff_t pos;
10931089

10941090
/*
1095-
* Check for a block which wants to lie outside our maximum possible
1096-
* pagecache index. (this comparison is done using sector_t types).
1091+
* Check for a block which lies outside our maximum possible
1092+
* pagecache index.
10971093
*/
1098-
if (unlikely(index != block >> sizebits)) {
1099-
printk(KERN_ERR "%s: requested out-of-range block %llu for "
1100-
"device %pg\n",
1094+
if (check_mul_overflow(block, (sector_t)size, &pos) || pos > MAX_LFS_FILESIZE) {
1095+
printk(KERN_ERR "%s: requested out-of-range block %llu for device %pg\n",
11011096
__func__, (unsigned long long)block,
11021097
bdev);
11031098
return false;
11041099
}
11051100

11061101
/* Create a folio with the proper size buffers */
1107-
return grow_dev_folio(bdev, block, index, size, gfp);
1102+
return grow_dev_folio(bdev, block, pos / PAGE_SIZE, size, gfp);
11081103
}
11091104

11101105
static struct buffer_head *

0 commit comments

Comments
 (0)