Skip to content

Commit 8b1413d

Browse files
LiBaokun96gregkh
authored andcommitted
ext4: avoid online resizing failures due to oversized flex bg
[ Upstream commit 5d1935a ] When we online resize an ext4 filesystem with a oversized flexbg_size, mkfs.ext4 -F -G 67108864 $dev -b 4096 100M mount $dev $dir resize2fs $dev 16G the following WARN_ON is triggered: ================================================================== WARNING: CPU: 0 PID: 427 at mm/page_alloc.c:4402 __alloc_pages+0x411/0x550 Modules linked in: sg(E) CPU: 0 PID: 427 Comm: resize2fs Tainted: G E 6.6.0-rc5+ #314 RIP: 0010:__alloc_pages+0x411/0x550 Call Trace: <TASK> __kmalloc_large_node+0xa2/0x200 __kmalloc+0x16e/0x290 ext4_resize_fs+0x481/0xd80 __ext4_ioctl+0x1616/0x1d90 ext4_ioctl+0x12/0x20 __x64_sys_ioctl+0xf0/0x150 do_syscall_64+0x3b/0x90 ================================================================== This is because flexbg_size is too large and the size of the new_group_data array to be allocated exceeds MAX_ORDER. Currently, the minimum value of MAX_ORDER is 8, the minimum value of PAGE_SIZE is 4096, the corresponding maximum number of groups that can be allocated is: (PAGE_SIZE << MAX_ORDER) / sizeof(struct ext4_new_group_data) ≈ 21845 And the value that is down-aligned to the power of 2 is 16384. Therefore, this value is defined as MAX_RESIZE_BG, and the number of groups added each time does not exceed this value during resizing, and is added multiple times to complete the online resizing. The difference is that the metadata in a flex_bg may be more dispersed. Signed-off-by: Baokun Li <[email protected]> Reviewed-by: Jan Kara <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Theodore Ts'o <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 04cf95f commit 8b1413d

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

fs/ext4/resize.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,17 @@ struct ext4_new_flex_group_data {
231231
in the flex group */
232232
__u16 *bg_flags; /* block group flags of groups
233233
in @groups */
234+
ext4_group_t resize_bg; /* number of allocated
235+
new_group_data */
234236
ext4_group_t count; /* number of groups in @groups
235237
*/
236238
};
237239

240+
/*
241+
* Avoiding memory allocation failures due to too many groups added each time.
242+
*/
243+
#define MAX_RESIZE_BG 16384
244+
238245
/*
239246
* alloc_flex_gd() allocates a ext4_new_flex_group_data with size of
240247
* @flexbg_size.
@@ -249,14 +256,18 @@ static struct ext4_new_flex_group_data *alloc_flex_gd(unsigned int flexbg_size)
249256
if (flex_gd == NULL)
250257
goto out3;
251258

252-
flex_gd->count = flexbg_size;
253-
flex_gd->groups = kmalloc_array(flexbg_size,
259+
if (unlikely(flexbg_size > MAX_RESIZE_BG))
260+
flex_gd->resize_bg = MAX_RESIZE_BG;
261+
else
262+
flex_gd->resize_bg = flexbg_size;
263+
264+
flex_gd->groups = kmalloc_array(flex_gd->resize_bg,
254265
sizeof(struct ext4_new_group_data),
255266
GFP_NOFS);
256267
if (flex_gd->groups == NULL)
257268
goto out2;
258269

259-
flex_gd->bg_flags = kmalloc_array(flexbg_size, sizeof(__u16),
270+
flex_gd->bg_flags = kmalloc_array(flex_gd->resize_bg, sizeof(__u16),
260271
GFP_NOFS);
261272
if (flex_gd->bg_flags == NULL)
262273
goto out1;
@@ -1619,8 +1630,7 @@ static int ext4_flex_group_add(struct super_block *sb,
16191630

16201631
static int ext4_setup_next_flex_gd(struct super_block *sb,
16211632
struct ext4_new_flex_group_data *flex_gd,
1622-
ext4_fsblk_t n_blocks_count,
1623-
unsigned int flexbg_size)
1633+
ext4_fsblk_t n_blocks_count)
16241634
{
16251635
struct ext4_sb_info *sbi = EXT4_SB(sb);
16261636
struct ext4_super_block *es = sbi->s_es;
@@ -1644,7 +1654,7 @@ static int ext4_setup_next_flex_gd(struct super_block *sb,
16441654
BUG_ON(last);
16451655
ext4_get_group_no_and_offset(sb, n_blocks_count - 1, &n_group, &last);
16461656

1647-
last_group = group | (flexbg_size - 1);
1657+
last_group = group | (flex_gd->resize_bg - 1);
16481658
if (last_group > n_group)
16491659
last_group = n_group;
16501660

@@ -2147,8 +2157,7 @@ int ext4_resize_fs(struct super_block *sb, ext4_fsblk_t n_blocks_count)
21472157
/* Add flex groups. Note that a regular group is a
21482158
* flex group with 1 group.
21492159
*/
2150-
while (ext4_setup_next_flex_gd(sb, flex_gd, n_blocks_count,
2151-
flexbg_size)) {
2160+
while (ext4_setup_next_flex_gd(sb, flex_gd, n_blocks_count)) {
21522161
if (time_is_before_jiffies(last_update_time + HZ * 10)) {
21532162
if (last_update_time)
21542163
ext4_msg(sb, KERN_INFO,

0 commit comments

Comments
 (0)