Skip to contentSkip to conversation

erofs: reliably distinguish block based and fscache mode

PATCH In review Baokun Li 4 participants 12 messages Apr 19, 2024 – Apr 28, 2024 EROFS file system

lore
Share & tools
lore

Baokun Li

author
#0 lore raw

v3 cover erofs: reliably distinguish block based and fscache mode

Hi Christian,

As your original patch had some "DOS line endings", I failed at am it, so manually changed the relevant content when making v2, which caused Author to be modified, I just noticed this and corrected it, apologies for that.

Changes since v2:
Get rid of erofs_fs_context.

Changes since v1:
Allocate and initialise fc->s_fs_info in erofs_fc_get_tree() instead of modifying fc->sb_flags.

V1: https://lore.kernel.org/r/[email protected]/
V2: https://lore.kernel.org/r/[email protected]/

Baokun Li (1):
erofs: get rid of erofs_fs_context

Christian Brauner (1):
erofs: reliably distinguish block based and fscache mode

 fs/erofs/internal.h |   7 ---
 fs/erofs/super.c    | 124 ++++++++++++++++++++------------------------
 2 files changed, 55 insertions(+), 76 deletions(-)
Signature · 1 line
2.31.1
author
#1 lore raw

PATCH v3 1/2 erofs: get rid of erofs_fs_context

Instead of allocating the erofs_sb_info in fill_super() allocate it during erofs_init_fs_context() and ensure that erofs can always have the info available during erofs_kill_sb(). After this erofs_fs_context is no longer needed, replace ctx with sbi, no functional changes.

---
 fs/erofs/internal.h |   7 ---
 fs/erofs/super.c    | 116 ++++++++++++++++++++------------------------
 2 files changed, 53 insertions(+), 70 deletions(-)
fs/erofs/internal.h +0 -7
index 39c67119f43b..d28ccfc0352b 100644
@@ -84,13 +84,6 @@ struct erofs_dev_context {
 	bool flatdev;
 };
 
-struct erofs_fs_context {
-	struct erofs_mount_opts opt;
-	struct erofs_dev_context *devs;
-	char *fsid;
-	char *domain_id;
-};
-
 /* all filesystem-wide lz4 configurations */
 struct erofs_sb_lz4_info {
 	/* # of pages needed for EROFS lz4 rolling decompression */
fs/erofs/super.c +53 -63

234 lines not on this page · raw message

Signature · 1 line
2.31.1
#2 lore raw

Re: [PATCH -next v3 1/2] erofs: get rid of erofs_fs_context

fs/erofs/super.c static void erofs_free_dev_context(struct erofs_dev_context *devs) · line 753

Quoted 9 lines · Baokun Li
fs/erofs/super.c +4 -1
@@ -761,12 +747,15 @@ static void erofs_free_dev_context(struct erofs_dev_context *devs)
 
 static void erofs_fc_free(struct fs_context *fc)
 {
-	struct erofs_fs_context *ctx = fc->fs_private;
+	struct erofs_sb_info *sbi = fc->s_fs_info;
+
+	if (!sbi)
+		return;

This is the only difference comparing to the original code literally. Is there any chance that fc->s_fs_info can be NULL when erofs_fc_free() is called?

Otherwise looks good to me.

Signature · 2 lines
Thanks,
Jingbo
author
#3 lore raw

Re: [PATCH -next v3 1/2] erofs: get rid of erofs_fs_context

Hi Jingbo,

Quoted 17 lines · Jingbo Xu

fs/erofs/super.c static void erofs_free_dev_context(struct erofs_dev_context *devs) · line 753

Quoted 9 lines · Baokun Li
fs/erofs/super.c +4 -1
@@ -761,12 +747,15 @@ static void erofs_free_dev_context(struct erofs_dev_context *devs)
 
  static void erofs_fc_free(struct fs_context *fc)
  {
-	struct erofs_fs_context *ctx = fc->fs_private;
+	struct erofs_sb_info *sbi = fc->s_fs_info;
+
+	if (!sbi)
+		return;

This is the only difference comparing to the original code literally. Is there any chance that fc->s_fs_info can be NULL when erofs_fc_free() is called?

Quoted 2 lines

Otherwise looks good to me.

When sget_fc() executes successfully, fc->s_fs_info is set to NULL, so the following NULL pointer dereference may occur:

do_new_mount
  vfs_get_tree
    erofs_fc_get_tree
      get_tree_bdev
        sget_dev
          sget_fc
            s = alloc_super
            s->s_fs_info = fc->s_fs_info;
            fc->s_fs_info = NULL;
        fill_super
        // return error
        deactivate_locked_super
          kfree(sbi);
  put_fs_context
    sbi = fc->s_fs_info
    kfree(sbi->fsid)

Thank you very much for the review!

Signature · 2 lines
With Best Regards,
Baokun Li
Reviewed-by
#4 lore raw

Re: [PATCH -next v3 1/2] erofs: get rid of erofs_fs_context

Quoted 25 lines · Baokun Li

Hi Jingbo,

Quoted 17 lines · Jingbo Xu

fs/erofs/super.c static void erofs_free_dev_context(struct · line 753

Quoted 9 lines · Baokun Li
fs/erofs/super.c +4 -1
@@ -761,12 +747,15 @@ static void erofs_free_dev_context(struct
 erofs_dev_context *devs)
     static void erofs_fc_free(struct fs_context *fc)
   {
-    struct erofs_fs_context *ctx = fc->fs_private;
+    struct erofs_sb_info *sbi = fc->s_fs_info;
+
+    if (!sbi)
+        return;

This is the only difference comparing to the original code literally. Is there any chance that fc->s_fs_info can be NULL when erofs_fc_free() is called?

Quoted 2 lines

Otherwise looks good to me.

When sget_fc() executes successfully, fc->s_fs_info is set to NULL, so the following NULL pointer dereference may occur:

Quoted 17 lines

do_new_mount
  vfs_get_tree
    erofs_fc_get_tree
      get_tree_bdev
        sget_dev
          sget_fc
            s = alloc_super
            s->s_fs_info = fc->s_fs_info;
            fc->s_fs_info = NULL;
        fill_super
        // return error
        deactivate_locked_super
          kfree(sbi);
  put_fs_context
    sbi = fc->s_fs_info
    kfree(sbi->fsid)

Alright, fc->s_fs_info is transferred to s->s_fs_info and set to NULL.

Feel free to add:

Signature · 2 lines
Thanks,
Jingbo
#5 lore raw

Re: [PATCH -next v3 1/2] erofs: get rid of erofs_fs_context

In reply to Baokun Li
Quoted 7 lines · Baokun Li

Instead of allocating the erofs_sb_info in fill_super() allocate it during erofs_init_fs_context() and ensure that erofs can always have the info available during erofs_kill_sb(). After this erofs_fs_context is no longer needed, replace ctx with sbi, no functional changes.

Thanks, it looks good to me, let's see how it behaves after applying to -next.

Thanks,
Gao Xiang

Reviewed-by on v3 1/2
#6 lore raw

Re: [PATCH -next v3 1/2] erofs: get rid of erofs_fs_context

In reply to Baokun Li
Quoted 7 lines · Baokun Li

Instead of allocating the erofs_sb_info in fill_super() allocate it during erofs_init_fs_context() and ensure that erofs can always have the info available during erofs_kill_sb(). After this erofs_fs_context is no longer needed, replace ctx with sbi, no functional changes.

Thanks,
Gao Xiang

maintainer Reviewed-by on v3 1/2
#7 lore raw

Re: [PATCH -next v3 1/2] erofs: get rid of erofs_fs_context

In reply to Baokun Li
Quoted 7 lines · Baokun Li

Instead of allocating the erofs_sb_info in fill_super() allocate it during erofs_init_fs_context() and ensure that erofs can always have the info available during erofs_kill_sb(). After this erofs_fs_context is no longer needed, replace ctx with sbi, no functional changes.

Thanks,

author
#8 lore raw

PATCH v3 2/2 erofs: reliably distinguish block based and fscache mode

In reply to Baokun Li

From: Christian Brauner <[email protected]>

When erofs_kill_sb() is called in block dev based mode, s_bdev may not have been initialised yet, and if CONFIG_EROFS_FS_ONDEMAND is enabled, it will be mistaken for fscache mode, and then attempt to free an anon_dev that has never been allocated, triggering the following warning:

============================================
ida_free called for id=0 which is not allocated.

WARNING: CPU: 14 PID: 926 at lib/idr.c:525 ida_free+0x134/0x140
Modules linked in:
CPU: 14 PID: 926 Comm: mount Not tainted 6.9.0-rc3-dirty #630
RIP: 0010:ida_free+0x134/0x140
Call Trace:
 <TASK>
 erofs_kill_sb+0x81/0x90
 deactivate_locked_super+0x35/0x80
 get_tree_bdev+0x136/0x1e0
 vfs_get_tree+0x2c/0xf0
 do_new_mount+0x190/0x2f0
 [...]
============================================

Now when erofs_kill_sb() is called, erofs_sb_info must have been initialised, so use sbi->fsid to distinguish between the two modes.

---
 fs/erofs/super.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
fs/erofs/super.c +2 -6
index 21faa49bc970..30b49b2eee53 100644
@@ -789,17 +789,13 @@ static int erofs_init_fs_context(struct fs_context *fc)
 
 static void erofs_kill_sb(struct super_block *sb)
 {
-	struct erofs_sb_info *sbi;
+	struct erofs_sb_info *sbi = EROFS_SB(sb);
 
-	if (erofs_is_fscache_mode(sb))
+	if (IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && sbi->fsid)
 		kill_anon_super(sb);
 	else
 		kill_block_super(sb);
 
-	sbi = EROFS_SB(sb);
-	if (!sbi)
-		return;
-
 	erofs_free_dev_context(sbi->devs);
 	fs_put_dax(sbi->dax_dev, NULL);
 	erofs_fscache_unregister_fs(sb);
Signature · 1 line
2.31.1
Reviewed-by on v3 2/2
#9 lore raw
Quoted 23 lines · Baokun Li

From: Christian Brauner <[email protected]>

When erofs_kill_sb() is called in block dev based mode, s_bdev may not have been initialised yet, and if CONFIG_EROFS_FS_ONDEMAND is enabled, it will be mistaken for fscache mode, and then attempt to free an anon_dev that has never been allocated, triggering the following warning:

============================================
ida_free called for id=0 which is not allocated.

WARNING: CPU: 14 PID: 926 at lib/idr.c:525 ida_free+0x134/0x140
Modules linked in:
CPU: 14 PID: 926 Comm: mount Not tainted 6.9.0-rc3-dirty #630
RIP: 0010:ida_free+0x134/0x140
Call Trace:
 <TASK>
 erofs_kill_sb+0x81/0x90
 deactivate_locked_super+0x35/0x80
 get_tree_bdev+0x136/0x1e0
 vfs_get_tree+0x2c/0xf0
 do_new_mount+0x190/0x2f0
 [...]
============================================
Quoted 5 lines

Now when erofs_kill_sb() is called, erofs_sb_info must have been initialised, so use sbi->fsid to distinguish between the two modes.

Quoted 28 lines
---
 fs/erofs/super.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
fs/erofs/super.c +2 -6
index 21faa49bc970..30b49b2eee53 100644
@@ -789,17 +789,13 @@ static int erofs_init_fs_context(struct fs_context *fc)
 
 static void erofs_kill_sb(struct super_block *sb)
 {
-	struct erofs_sb_info *sbi;
+	struct erofs_sb_info *sbi = EROFS_SB(sb);
 
-	if (erofs_is_fscache_mode(sb))
+	if (IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && sbi->fsid)
 		kill_anon_super(sb);
 	else
 		kill_block_super(sb);
 
-	sbi = EROFS_SB(sb);
-	if (!sbi)
-		return;
-
 	erofs_free_dev_context(sbi->devs);
 	fs_put_dax(sbi->dax_dev, NULL);
 	erofs_fscache_unregister_fs(sb);
Signature · 2 lines
Thanks,
Jingbo
Reviewed-by on v3 2/2
#10 lore raw
In reply to Baokun Li
Quoted 23 lines · Baokun Li

From: Christian Brauner <[email protected]>

When erofs_kill_sb() is called in block dev based mode, s_bdev may not have been initialised yet, and if CONFIG_EROFS_FS_ONDEMAND is enabled, it will be mistaken for fscache mode, and then attempt to free an anon_dev that has never been allocated, triggering the following warning:

============================================
ida_free called for id=0 which is not allocated.

WARNING: CPU: 14 PID: 926 at lib/idr.c:525 ida_free+0x134/0x140
Modules linked in:
CPU: 14 PID: 926 Comm: mount Not tainted 6.9.0-rc3-dirty #630
RIP: 0010:ida_free+0x134/0x140
Call Trace:
  <TASK>
  erofs_kill_sb+0x81/0x90
  deactivate_locked_super+0x35/0x80
  get_tree_bdev+0x136/0x1e0
  vfs_get_tree+0x2c/0xf0
  do_new_mount+0x190/0x2f0
  [...]
============================================
Quoted 5 lines

Now when erofs_kill_sb() is called, erofs_sb_info must have been initialised, so use sbi->fsid to distinguish between the two modes.

Thanks,
Gao Xiang

maintainer Reviewed-by on v3 2/2
#11 lore raw
In reply to Baokun Li
Quoted 23 lines · Baokun Li

From: Christian Brauner <[email protected]>

When erofs_kill_sb() is called in block dev based mode, s_bdev may not have been initialised yet, and if CONFIG_EROFS_FS_ONDEMAND is enabled, it will be mistaken for fscache mode, and then attempt to free an anon_dev that has never been allocated, triggering the following warning:

============================================
ida_free called for id=0 which is not allocated.

WARNING: CPU: 14 PID: 926 at lib/idr.c:525 ida_free+0x134/0x140
Modules linked in:
CPU: 14 PID: 926 Comm: mount Not tainted 6.9.0-rc3-dirty #630
RIP: 0010:ida_free+0x134/0x140
Call Trace:
  <TASK>
  erofs_kill_sb+0x81/0x90
  deactivate_locked_super+0x35/0x80
  get_tree_bdev+0x136/0x1e0
  vfs_get_tree+0x2c/0xf0
  do_new_mount+0x190/0x2f0
  [...]
============================================
Quoted 5 lines

Now when erofs_kill_sb() is called, erofs_sb_info must have been initialised, so use sbi->fsid to distinguish between the two modes.

Thanks,