Skip to contentSkip to conversation

btrfs: volumes: Increase bioc pointer check

PATCH Superseded Li zeming 3 participants 5 messages Oct 25, 2022 Btrfs file system

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Li zeming

author
#0 lore raw

PATCH 1/1 btrfs: volumes: Increase bioc pointer check

If kzalloc fails to allocate the bioc pointer, NULL is returned directly.

---
 fs/btrfs/volumes.c | 2 ++
 1 file changed, 2 insertions(+)
fs/btrfs/volumes.c +2 -0
index 064ab2a79c80..f9cb815fe23d 100644
@@ -5892,6 +5892,8 @@ static struct btrfs_io_context *alloc_btrfs_io_context(struct btrfs_fs_info *fs_
 		 */
 		sizeof(u64) * (total_stripes),
 		GFP_NOFS|__GFP_NOFAIL);
+	if (!bioc)
+		return NULL;
 
 	atomic_set(&bioc->error, 0);
 	refcount_set(&bioc->refs, 1);
Signature · 1 line
2.18.2
#1 lore raw
Quoted 4 lines · Li zeming

If kzalloc fails to allocate the bioc pointer, NULL is returned directly.

This patch clearly shows you haven't really understood the code. As is evident there is __GFP_NOFAIL flag so as per the guarantees for this flag we either loop infinitely trying to allocate a bioc or simply allocated it. So this check can never be triggered.

NAK

Quoted 17 lines
---
  fs/btrfs/volumes.c | 2 ++
  1 file changed, 2 insertions(+)
fs/btrfs/volumes.c +2 -0
index 064ab2a79c80..f9cb815fe23d 100644
@@ -5892,6 +5892,8 @@ static struct btrfs_io_context *alloc_btrfs_io_context(struct btrfs_fs_info *fs_
  		 */
  		sizeof(u64) * (total_stripes),
  		GFP_NOFS|__GFP_NOFAIL);
+	if (!bioc)
+		return NULL;
 
  	atomic_set(&bioc->error, 0);
  	refcount_set(&bioc->refs, 1);
#2 lore raw
Quoted 6 lines · Nikolay Borisov
Quoted 3 lines · Li zeming

If kzalloc fails to allocate the bioc pointer, NULL is returned directly.

Quoted 6 lines
Quoted 1 line

This patch clearly shows you haven't really understood the code. As is evident there is __GFP_NOFAIL flag so as per the guarantees for this flag we either loop infinitely trying to allocate a bioc or simply allocated it. So this check can never be triggered.

I guess what he missed is just to also remove that NOFAIL flag.

NOFAIL will not 100% guarantee the allocation, and I don't see this location to be so important, especially when the only caller is already handing allocation failure.

Thanks,
Qu

Quoted 19 lines

NAK

Quoted 17 lines

---
  fs/btrfs/volumes.c | 2 ++

  1 file changed, 2 insertions(+)
fs/btrfs/volumes.c +2 -0
index 064ab2a79c80..f9cb815fe23d 100644
@@ -5892,6 +5892,8 @@ static struct btrfs_io_context
 *alloc_btrfs_io_context(struct btrfs_fs_info *fs_
            */
           sizeof(u64) * (total_stripes),
           GFP_NOFS|__GFP_NOFAIL);
+    if (!bioc)
+        return NULL;
       atomic_set(&bioc->error, 0);
       refcount_set(&bioc->refs, 1);
Reviewed-by on v1 1/1
#3 lore raw
In reply to Li zeming
Quoted 2 lines · Li zeming

If kzalloc fails to allocate the bioc pointer, NULL is returned directly.

s/is returned/should be returned/

Quoted 11 lines
---
  fs/btrfs/volumes.c | 2 ++
  1 file changed, 2 insertions(+)
fs/btrfs/volumes.c +0 -0
index 064ab2a79c80..f9cb815fe23d 100644

fs/btrfs/volumes.c static struct btrfs_io_context *alloc_btrfs_io_context(struct btrfs_fs_info *fs_ · line 5894

Quoted 3 lines
fs/btrfs/volumes.c +0 -0
@@ -5892,6 +5892,8 @@ static struct btrfs_io_context *alloc_btrfs_io_context(struct btrfs_fs_info *fs_
  		 */
  		sizeof(u64) * (total_stripes),
  		GFP_NOFS|__GFP_NOFAIL);

I think you can also remove the __GFP_NOFAIL flag.

Especially the only caller is properly handling the error.

With that __GFP_NOFAIL flag there, it should not fail, but we can not just rely on NOFAIL flag to save our asses.

Otherwise looks good to me.

With above two points fixed, you can add my tag:

Thanks,
Qu

Quoted 5 lines
fs/btrfs/volumes.c +2 -0
+	if (!bioc)
+		return NULL;
 
  	atomic_set(&bioc->error, 0);
  	refcount_set(&bioc->refs, 1);
author
#4 lore raw

very thankful. I have fixed these two problems and am ready to release v2 patches.