Skip to content

Commit 4b04646

Browse files
Matthew Wilcox (Oracle)akpm00
authored andcommitted
buffer: fix various functions for block size > PAGE_SIZE
If i_blkbits is larger than PAGE_SHIFT, we shift by a negative number, which is undefined. It is safe to shift the block left as a block device must be smaller than MAX_LFS_FILESIZE, which is guaranteed to fit in loff_t. Link: https://lkml.kernel.org/r/20231109210608.2252323-6-willy@infradead.org Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org> Reviewed-by: Pankaj Raghav <p.raghav@samsung.com> Cc: Hannes Reinecke <hare@suse.de> Cc: Luis Chamberlain <mcgrof@kernel.org> Cc: Ryusuke Konishi <konishi.ryusuke@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 8084419 commit 4b04646

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

fs/buffer.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ __find_get_block_slow(struct block_device *bdev, sector_t block)
199199
int all_mapped = 1;
200200
static DEFINE_RATELIMIT_STATE(last_warned, HZ, 1);
201201

202-
index = block >> (PAGE_SHIFT - bd_inode->i_blkbits);
202+
index = ((loff_t)block << bd_inode->i_blkbits) / PAGE_SIZE;
203203
folio = __filemap_get_folio(bd_mapping, index, FGP_ACCESSED, 0);
204204
if (IS_ERR(folio))
205205
goto out;
@@ -1693,13 +1693,13 @@ void clean_bdev_aliases(struct block_device *bdev, sector_t block, sector_t len)
16931693
struct inode *bd_inode = bdev->bd_inode;
16941694
struct address_space *bd_mapping = bd_inode->i_mapping;
16951695
struct folio_batch fbatch;
1696-
pgoff_t index = block >> (PAGE_SHIFT - bd_inode->i_blkbits);
1696+
pgoff_t index = ((loff_t)block << bd_inode->i_blkbits) / PAGE_SIZE;
16971697
pgoff_t end;
16981698
int i, count;
16991699
struct buffer_head *bh;
17001700
struct buffer_head *head;
17011701

1702-
end = (block + len - 1) >> (PAGE_SHIFT - bd_inode->i_blkbits);
1702+
end = ((loff_t)(block + len - 1) << bd_inode->i_blkbits) / PAGE_SIZE;
17031703
folio_batch_init(&fbatch);
17041704
while (filemap_get_folios(bd_mapping, &index, end, &fbatch)) {
17051705
count = folio_batch_count(&fbatch);
@@ -2660,8 +2660,8 @@ int block_truncate_page(struct address_space *mapping,
26602660
return 0;
26612661

26622662
length = blocksize - length;
2663-
iblock = (sector_t)index << (PAGE_SHIFT - inode->i_blkbits);
2664-
2663+
iblock = ((loff_t)index * PAGE_SIZE) >> inode->i_blkbits;
2664+
26652665
folio = filemap_grab_folio(mapping, index);
26662666
if (IS_ERR(folio))
26672667
return PTR_ERR(folio);

0 commit comments

Comments
 (0)