Skip to contentSkip to conversation

btrfs: handle kobject_init_and_add() failure correctly

PATCH In review Qu Wenruo 2 participants 2 messages Nov 18 – Nov 19 Btrfs file system

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

PATCH 1/1 btrfs: handle kobject_init_and_add() failure correctly

There is a bug report that if error is injected into kobject_init_and_add(), later error handling at btrfs_sysfs_del_qgroups() will trigger a warning during kobject_del(fs_info->qgroups_kobj).

The call trace looks like this:

btrfs_sysfs_add_qgroups()

  |-kobject_init_and_add()
  | \-kobject_add_varg()
  |   \-kobject_add_internal()
  |     \-create_dir()
  |       \-sysfs_create_dir_ns()
  |         Fails with -ENOMEM with error injection
  |         kobj->sd stays NULL
  |
  \-out: btrfs_sysfs_del_qgroups()
    \-kobject_del()
      \-__kobject_del()
        \-sysfs_remove_groups()
          \-sysfs_remove_group()
	    |-kn = parent
	    | Which is kobj->sd, and it's still NULL
	    |
            \-remove_files(kn, ..)
              \-kernfs_remove_by_name_ns(parent, ...)
	        Trigger the warning as @parent is NULL.

Fix it by handle the kobject_init_add_add() error manually by kobject_put(), without relying on btrfs_sysfs_del_qgroups().

---
 fs/btrfs/sysfs.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
fs/btrfs/sysfs.c +10 -2
index 89b0a4ef4063..f87f828aff99 100644
@@ -2487,8 +2487,16 @@ int btrfs_sysfs_add_qgroups(struct btrfs_fs_info *fs_info)
 
 	ret = kobject_init_and_add(fs_info->qgroups_kobj, &qgroups_ktype,
 				   fsid_kobj, "qgroups");
-	if (ret < 0)
-		goto out;
+	if (ret < 0) {
+		/*
+		 * The kobject is not added into sysfs, so we can only free it
+		 * manually. kobject_del() from btrfs_del_qgroups() will
+		 * trigger a warning.
+		 */
+		kobject_put(fs_info->qgroups_kobj);
+		fs_info->qgroups_kobj = NULL;
+		return ret;
+	}
 
 	rbtree_postorder_for_each_entry_safe(qgroup, next,
 					     &fs_info->qgroup_tree, node) {
Signature · 1 line
2.56.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 29 lines · Qu Wenruo

There is a bug report that if error is injected into kobject_init_and_add(), later error handling at btrfs_sysfs_del_qgroups() will trigger a warning during kobject_del(fs_info->qgroups_kobj).

The call trace looks like this:

btrfs_sysfs_add_qgroups()

  |-kobject_init_and_add()
  | \-kobject_add_varg()
  |   \-kobject_add_internal()
  |     \-create_dir()
  |       \-sysfs_create_dir_ns()
  |         Fails with -ENOMEM with error injection
  |         kobj->sd stays NULL
  |
  \-out: btrfs_sysfs_del_qgroups()
    \-kobject_del()
      \-__kobject_del()
        \-sysfs_remove_groups()
          \-sysfs_remove_group()
            |-kn = parent
            | Which is kobj->sd, and it's still NULL
            |
            \-remove_files(kn, ..)
              \-kernfs_remove_by_name_ns(parent, ...)
                Trigger the warning as @parent is NULL.
Quoted 7 lines

Fix it by handle the kobject_init_add_add() error manually by kobject_put(), without relying on btrfs_sysfs_del_qgroups().

Thanks.

Quoted 31 lines
---
 fs/btrfs/sysfs.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
fs/btrfs/sysfs.c +10 -3
index 89b0a4ef4063..f87f828aff99 100644
@@ -2487,8 +2487,16 @@ int btrfs_sysfs_add_qgroups(struct btrfs_fs_info *fs_info)
 
        ret = kobject_init_and_add(fs_info->qgroups_kobj, &qgroups_ktype,
                                   fsid_kobj, "qgroups");
-       if (ret < 0)
-               goto out;
+       if (ret < 0) {
+               /*
+                * The kobject is not added into sysfs, so we can only free it
+                * manually. kobject_del() from btrfs_del_qgroups() will
+                * trigger a warning.
+                */
+               kobject_put(fs_info->qgroups_kobj);
+               fs_info->qgroups_kobj = NULL;
+               return ret;
+       }
 
        rbtree_postorder_for_each_entry_safe(qgroup, next,
                                             &fs_info->qgroup_tree, node) {
--

2.56.0