Skip to contentSkip to conversation

ext4: fix allocation failure in ext4_mb_load_buddy_gfp

PATCH In review Deepanshu Kartikey 2 participants 2 messages Sep 24, 2025 ext4 file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ext4: fix allocation failure in ext4_mb_load_buddy_gfp

Fix WARNING in __alloc_pages_slowpath() when ext4_discard_preallocations() is called during memory pressure.

The issue occurs when __GFP_NOFAIL is used during memory reclaim context, which can lead to allocation warnings. Avoid using __GFP_NOFAIL when the current process is already in memory allocation context to prevent potential deadlocks and warnings.

---
 fs/ext4/mballoc.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/ext4/mballoc.c +4 -2
index 5898d92ba19f..61ee009717f1 100644
@@ -5656,9 +5656,11 @@ void ext4_discard_preallocations(struct inode *inode)
 	list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
 		BUG_ON(pa->pa_type != MB_INODE_PA);
 		group = ext4_get_group_number(sb, pa->pa_pstart);
+		gfp_t flags = GFP_NOFS;
+		if (!(current->flags & PF_MEMALLOC))
+			flags |= __GFP_NOFAIL;
 
-		err = ext4_mb_load_buddy_gfp(sb, group, &e4b,
-					     GFP_NOFS|__GFP_NOFAIL);
+		err = ext4_mb_load_buddy_gfp(sb, group, &e4b, flags);
 		if (err) {
 			ext4_error_err(sb, -err, "Error %d loading buddy information for %u",
 				       err, group);
Signature · 1 line
2.43.0
#1 lore raw
Quoted 8 lines · Deepanshu Kartikey

Fix WARNING in __alloc_pages_slowpath() when ext4_discard_preallocations() is called during memory pressure.

The issue occurs when __GFP_NOFAIL is used during memory reclaim context, which can lead to allocation warnings. Avoid using __GFP_NOFAIL when the current process is already in memory allocation context to prevent potential deadlocks and warnings.

This quiets the memory allocation warning, but will result in a filesystem error being generated (read-only or panic) if the allocation fails, if you follow the code a few lines further down. That is not good error handling for a memory allocation failure during cache cleanup.

When __GFP_NOFAIL was *always* passed, then the error could never be hit, which is why it was put there in the first place.

It looks like this function can return an error and the caller will retry, so that would be preferable to causing the filesystem to abort in this case.

Cheers, Andreas

Quoted 27 lines

---
fs/ext4/mballoc.c | 6 ++++--

1 file changed, 4 insertions(+), 2 deletions(-)
fs/ext4/mballoc.c +4 -3
index 5898d92ba19f..61ee009717f1 100644
@@ -5656,9 +5656,11 @@ void ext4_discard_preallocations(struct inode *inode)
 	list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
 		BUG_ON(pa->pa_type != MB_INODE_PA);
 		group = ext4_get_group_number(sb, pa->pa_pstart);
+		gfp_t flags = GFP_NOFS;
+		if (!(current->flags & PF_MEMALLOC))
+			flags |= __GFP_NOFAIL;
 
-		err = ext4_mb_load_buddy_gfp(sb, group, &e4b,
-					     GFP_NOFS|__GFP_NOFAIL);
+		err = ext4_mb_load_buddy_gfp(sb, group, &e4b, flags);
 		if (err) {
 			ext4_error_err(sb, -err, "Error %d loading buddy information for %u",
 				       err, group);
--
Quoted 2 lines

2.43.0

Cheers, Andreas