Skip to contentSkip to conversation

ext4: remove unnecessary checks for __GFP_NOFAIL allocation.

PATCH In review Julian Sun 4 participants 6 messages Feb 28, 2025 – Mar 1, 2025 ext4 file system

lore
Share & tools
lore

Julian Sun

author
#0 lore raw

PATCH 1/1 ext4: remove unnecessary checks for __GFP_NOFAIL allocation.

The __GFP_NOFAIL flag ensures that allocation will not fail. So remove the unnecessary checks.

---
 fs/ext4/extents.c | 4 ----
 1 file changed, 4 deletions(-)
fs/ext4/extents.c +0 -4
index a07a98a4b97a..95debd5d6506 100644
@@ -2940,10 +2940,6 @@ int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
 	} else {
 		path = kcalloc(depth + 1, sizeof(struct ext4_ext_path),
 			       GFP_NOFS | __GFP_NOFAIL);
-		if (path == NULL) {
-			ext4_journal_stop(handle);
-			return -ENOMEM;
-		}
 		path[0].p_maxdepth = path[0].p_depth = depth;
 		path[0].p_hdr = ext_inode_hdr(inode);
 		i = 0;
Signature · 1 line
2.39.5
#1 lore raw
Quoted 2 lines · Julian Sun

The __GFP_NOFAIL flag ensures that allocation will not fail. So remove the unnecessary checks.

Actually, even with __GFP_NOFAIL set, kcalloc() can still return NULL, such as when the input parameters overflow.

Baokun

Quoted 21 lines
---
  fs/ext4/extents.c | 4 ----
  1 file changed, 4 deletions(-)
fs/ext4/extents.c +0 -4
index a07a98a4b97a..95debd5d6506 100644
@@ -2940,10 +2940,6 @@ int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
  	} else {
  		path = kcalloc(depth + 1, sizeof(struct ext4_ext_path),
  			       GFP_NOFS | __GFP_NOFAIL);
-		if (path == NULL) {
-			ext4_journal_stop(handle);
-			return -ENOMEM;
-		}
  		path[0].p_maxdepth = path[0].p_depth = depth;
  		path[0].p_hdr = ext_inode_hdr(inode);
  		i = 0;
author
#2 lore raw

Hi, Baokun.
Thanks for your review and comments.

Quoted 7 lines · Baokun Li
Quoted 2 lines · Julian Sun

The __GFP_NOFAIL flag ensures that allocation will not fail. So remove the unnecessary checks.

Actually, even with __GFP_NOFAIL set, kcalloc() can still return NULL, such as when the input parameters overflow.

Yeah, agreed. But IMO an overflow shouldn’t happen in this situation.

If there's something I'm missing, please let me know.

Quoted 8 lines

Baokun

Quoted 6 lines
---
  fs/ext4/extents.c | 4 ----
  1 file changed, 4 deletions(-)
Quoted 17 lines
Quoted 5 lines
fs/ext4/extents.c +0 -0
index a07a98a4b97a..95debd5d6506 100644

fs/ext4/extents.c int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start, · line 2945

Quoted 10 lines
fs/ext4/extents.c +0 -4
@@ -2940,10 +2940,6 @@ int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
      } else {
              path = kcalloc(depth + 1, sizeof(struct ext4_ext_path),
                             GFP_NOFS | __GFP_NOFAIL);
-             if (path == NULL) {
-                     ext4_journal_stop(handle);
-                     return -ENOMEM;
-             }
              path[0].p_maxdepth = path[0].p_depth = depth;
              path[0].p_hdr = ext_inode_hdr(inode);
              i = 0;

Thanks,

Signature · 1 line
Julian Sun <[email protected]>
maintainer
#3 lore raw
Quoted 6 lines · Julian Sun
Quoted 3 lines

Actually, even with __GFP_NOFAIL set, kcalloc() can still return NULL, such as when the input parameters overflow.

Yeah, agreed. But IMO an overflow shouldn’t happen in this situation.

If there's something I'm missing, please let me know.

It's not a matter of missing something; or even Right vs Wrong. Different maintainers have different tastes about this sort of thing.

The mm folks have changed the meaning of __GFP_NOFAIL in the past (TL;DR: they *hate* that concept, and I wouldn't be surprised if they try to change its behavior in the future) and especially in large code bases such as the Linux Kernel, I'm a big believer in defensive programming.

As Linus has said in a different thread, when a compiler adds warnings because of what it thinks are "unnecessary" range checks, that's a bad warning. Adding extra range checks is never a bad thing, and compiler behaviour that whine about that sort of thing are.... unfortunate. Similarly, I'd much rather keep the extra check.

(Also, there exist static program checkers, such as Coverity, that don't know about the semantics of the GFP_* flags, and so removing the check would actually cause those tools to complain.)

Cheers,

- Ted

author
#4 lore raw

Hi, Ted.

Quoted 24 lines · Theodore Ts'o
Quoted 6 lines · Julian Sun
Quoted 3 lines

Actually, even with __GFP_NOFAIL set, kcalloc() can still return NULL, such as when the input parameters overflow.

Yeah, agreed. But IMO an overflow shouldn’t happen in this situation.

If there's something I'm missing, please let me know.

It's not a matter of missing something; or even Right vs Wrong. Different maintainers have different tastes about this sort of thing.

The mm folks have changed the meaning of __GFP_NOFAIL in the past (TL;DR: they *hate* that concept, and I wouldn't be surprised if they try to change its behavior in the future) and especially in large code bases such as the Linux Kernel, I'm a big believer in defensive programming.

As Linus has said in a different thread, when a compiler adds warnings because of what it thinks are "unnecessary" range checks, that's a bad warning. Adding extra range checks is never a bad thing, and compiler behaviour that whine about that sort of thing are.... unfortunate. Similarly, I'd much rather keep the extra check.

Quoted 4 lines

(Also, there exist static program checkers, such as Coverity, that don't know about the semantics of the GFP_* flags, and so removing the check would actually cause those tools to complain.)

Got it. Thanks for your detailed explanation, it makes sense to me. By the way, I know you're busy, and I’m not trying to rush you, but when you have some time, could you please take a look at these patches? Thank you!

https://lore.kernel.org/linux-ext4/[email protected]/

Quoted 3 lines

Cheers,

- Ted

Best Regards,

Signature · 1 line
Julian Sun <[email protected]>
Reviewed-by on v1 1/1
#5 lore raw
In reply to Julian Sun
Quoted 4 lines · Julian Sun

The __GFP_NOFAIL flag ensures that allocation will not fail. So remove the unnecessary checks.

Thanks for the cleanup, looks good to me.

Quoted 19 lines
---
 fs/ext4/extents.c | 4 ----
 1 file changed, 4 deletions(-)
fs/ext4/extents.c +0 -4
index a07a98a4b97a..95debd5d6506 100644
@@ -2940,10 +2940,6 @@ int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
 	} else {
 		path = kcalloc(depth + 1, sizeof(struct ext4_ext_path),
 			       GFP_NOFS | __GFP_NOFAIL);
-		if (path == NULL) {
-			ext4_journal_stop(handle);
-			return -ENOMEM;
-		}
 		path[0].p_maxdepth = path[0].p_depth = depth;
 		path[0].p_hdr = ext_inode_hdr(inode);
 		i = 0;