Skip to content

Commit 3938fc2

Browse files
LiBaokun96tytso
authored andcommitted
ext4: support large block size in ext4_mb_get_buddy_page_lock()
Currently, ext4_mb_get_buddy_page_lock() uses blocks_per_page to calculate folio index and offset. However, when blocksize is larger than PAGE_SIZE, blocks_per_page becomes zero, leading to a potential division-by-zero bug. To support BS > PS, use bytes to compute folio index and offset within folio to get rid of blocks_per_page. Also, since ext4_mb_get_buddy_page_lock() already fully supports folio, rename it to ext4_mb_get_buddy_folio_lock(). Signed-off-by: Baokun Li <libaokun1@huawei.com> Reviewed-by: Zhang Yi <yi.zhang@huawei.com> Reviewed-by: Jan Kara <jack@suse.cz> Reviewed-by: Ojaswin Mujoo <ojaswin@linux.ibm.com> Message-ID: <20251121090654.631996-13-libaokun@huaweicloud.com> Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent 6117f18 commit 3938fc2

1 file changed

Lines changed: 22 additions & 20 deletions

File tree

fs/ext4/mballoc.c

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,50 +1527,52 @@ static int ext4_mb_init_cache(struct folio *folio, char *incore, gfp_t gfp)
15271527
}
15281528

15291529
/*
1530-
* Lock the buddy and bitmap pages. This make sure other parallel init_group
1531-
* on the same buddy page doesn't happen whild holding the buddy page lock.
1532-
* Return locked buddy and bitmap pages on e4b struct. If buddy and bitmap
1533-
* are on the same page e4b->bd_buddy_folio is NULL and return value is 0.
1530+
* Lock the buddy and bitmap folios. This makes sure other parallel init_group
1531+
* on the same buddy folio doesn't happen while holding the buddy folio lock.
1532+
* Return locked buddy and bitmap folios on e4b struct. If buddy and bitmap
1533+
* are on the same folio e4b->bd_buddy_folio is NULL and return value is 0.
15341534
*/
1535-
static int ext4_mb_get_buddy_page_lock(struct super_block *sb,
1535+
static int ext4_mb_get_buddy_folio_lock(struct super_block *sb,
15361536
ext4_group_t group, struct ext4_buddy *e4b, gfp_t gfp)
15371537
{
15381538
struct inode *inode = EXT4_SB(sb)->s_buddy_cache;
1539-
int block, pnum, poff;
1540-
int blocks_per_page;
1539+
int block, pnum;
15411540
struct folio *folio;
15421541

15431542
e4b->bd_buddy_folio = NULL;
15441543
e4b->bd_bitmap_folio = NULL;
15451544

1546-
blocks_per_page = PAGE_SIZE / sb->s_blocksize;
15471545
/*
15481546
* the buddy cache inode stores the block bitmap
15491547
* and buddy information in consecutive blocks.
15501548
* So for each group we need two blocks.
15511549
*/
15521550
block = group * 2;
1553-
pnum = block / blocks_per_page;
1554-
poff = block % blocks_per_page;
1551+
pnum = EXT4_LBLK_TO_PG(inode, block);
15551552
folio = __filemap_get_folio(inode->i_mapping, pnum,
15561553
FGP_LOCK | FGP_ACCESSED | FGP_CREAT, gfp);
15571554
if (IS_ERR(folio))
15581555
return PTR_ERR(folio);
15591556
BUG_ON(folio->mapping != inode->i_mapping);
1557+
WARN_ON_ONCE(folio_size(folio) < sb->s_blocksize);
15601558
e4b->bd_bitmap_folio = folio;
1561-
e4b->bd_bitmap = folio_address(folio) + (poff * sb->s_blocksize);
1559+
e4b->bd_bitmap = folio_address(folio) +
1560+
offset_in_folio(folio, EXT4_LBLK_TO_B(inode, block));
15621561

1563-
if (blocks_per_page >= 2) {
1564-
/* buddy and bitmap are on the same page */
1562+
block++;
1563+
pnum = EXT4_LBLK_TO_PG(inode, block);
1564+
if (folio_contains(folio, pnum)) {
1565+
/* buddy and bitmap are on the same folio */
15651566
return 0;
15661567
}
15671568

1568-
/* blocks_per_page == 1, hence we need another page for the buddy */
1569-
folio = __filemap_get_folio(inode->i_mapping, block + 1,
1569+
/* we need another folio for the buddy */
1570+
folio = __filemap_get_folio(inode->i_mapping, pnum,
15701571
FGP_LOCK | FGP_ACCESSED | FGP_CREAT, gfp);
15711572
if (IS_ERR(folio))
15721573
return PTR_ERR(folio);
15731574
BUG_ON(folio->mapping != inode->i_mapping);
1575+
WARN_ON_ONCE(folio_size(folio) < sb->s_blocksize);
15741576
e4b->bd_buddy_folio = folio;
15751577
return 0;
15761578
}
@@ -1609,14 +1611,14 @@ int ext4_mb_init_group(struct super_block *sb, ext4_group_t group, gfp_t gfp)
16091611

16101612
/*
16111613
* This ensures that we don't reinit the buddy cache
1612-
* page which map to the group from which we are already
1614+
* folio which map to the group from which we are already
16131615
* allocating. If we are looking at the buddy cache we would
16141616
* have taken a reference using ext4_mb_load_buddy and that
1615-
* would have pinned buddy page to page cache.
1616-
* The call to ext4_mb_get_buddy_page_lock will mark the
1617-
* page accessed.
1617+
* would have pinned buddy folio to page cache.
1618+
* The call to ext4_mb_get_buddy_folio_lock will mark the
1619+
* folio accessed.
16181620
*/
1619-
ret = ext4_mb_get_buddy_page_lock(sb, group, &e4b, gfp);
1621+
ret = ext4_mb_get_buddy_folio_lock(sb, group, &e4b, gfp);
16201622
if (ret || !EXT4_MB_GRP_NEED_INIT(this_grp)) {
16211623
/*
16221624
* somebody initialized the group

0 commit comments

Comments
 (0)