Skip to contentSkip to conversation

bcachefs: fix error path of __bch2_read_super()

PATCH In review Chao Yu 2 participants 3 messages Apr 12, 2024 – Apr 15, 2024

lore
Share & tools
lore

Chao Yu

author
#0 lore raw

PATCH v2 1/1 bcachefs: fix error path of __bch2_read_super()

In __bch2_read_super(), if kstrdup() fails, it needs to release memory in sb->holder, fix to call bch2_free_super() in the error path.

---
v2:
- add error message in error path pointed out by Hongbo Li.

 fs/bcachefs/super-io.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/bcachefs/super-io.c +5 -2
index ad28e370b640..cc80d7d30b8c 100644
@@ -698,8 +698,11 @@ static int __bch2_read_super(const char *path, struct bch_opts *opts,
 		return -ENOMEM;
 
 	sb->sb_name = kstrdup(path, GFP_KERNEL);
-	if (!sb->sb_name)
-		return -ENOMEM;
+	if (!sb->sb_name) {
+		ret = -ENOMEM;
+		prt_printf(&err, "error allocating memory for sb_name");
+		goto err;
+	}
 
 #ifndef __KERNEL__
 	if (opt_get(*opts, direct_io) == false)
Signature · 1 line
2.40.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 28 lines · Chao Yu

In __bch2_read_super(), if kstrdup() fails, it needs to release memory in sb->holder, fix to call bch2_free_super() in the error path.

---
v2:
- add error message in error path pointed out by Hongbo Li.

  fs/bcachefs/super-io.c | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)
fs/bcachefs/super-io.c +5 -2
index ad28e370b640..cc80d7d30b8c 100644
@@ -698,8 +698,11 @@ static int __bch2_read_super(const char *path, struct bch_opts *opts,
  		return -ENOMEM;
 
  	sb->sb_name = kstrdup(path, GFP_KERNEL);
-	if (!sb->sb_name)
-		return -ENOMEM;
+	if (!sb->sb_name) {
+		ret = -ENOMEM;
+		prt_printf(&err, "error allocating memory for sb_name");
+		goto err;
+	}
 
  #ifndef __KERNEL__
  	if (opt_get(*opts, direct_io) == false)
#2 lore raw

This patch fixes the issue with memory reclamation in certain case. May be we should use it! 😉

Quoted 31 lines · Hongbo Li
Quoted 27 lines · Chao Yu

In __bch2_read_super(), if kstrdup() fails, it needs to release memory in sb->holder, fix to call bch2_free_super() in the error path.

---
v2:
- add error message in error path pointed out by Hongbo Li. fs/bcachefs/super-io.c | 7 +++++--

  1 file changed, 5 insertions(+), 2 deletions(-)
fs/bcachefs/super-io.c +5 -2
index ad28e370b640..cc80d7d30b8c 100644
@@ -698,8 +698,11 @@ static int __bch2_read_super(const char *path, 
 struct bch_opts *opts,
           return -ENOMEM;
       sb->sb_name = kstrdup(path, GFP_KERNEL);
-    if (!sb->sb_name)
-        return -ENOMEM;
+    if (!sb->sb_name) {
+        ret = -ENOMEM;
+        prt_printf(&err, "error allocating memory for sb_name");
+        goto err;
+    }
   #ifndef __KERNEL__
       if (opt_get(*opts, direct_io) == false)