Skip to content

Commit b5ddcff

Browse files
asjkdave
authored andcommitted
btrfs: fix put of uninitialized kobject after seed device delete
The following test case leads to NULL kobject free error: mount seed /mnt add sprout to /mnt umount /mnt mount sprout to /mnt delete seed kobject: '(null)' (00000000dd2b87e4): is not initialized, yet kobject_put() is being called. WARNING: CPU: 1 PID: 15784 at lib/kobject.c:736 kobject_put+0x80/0x350 RIP: 0010:kobject_put+0x80/0x350 :: Call Trace: btrfs_sysfs_remove_devices_dir+0x6e/0x160 [btrfs] btrfs_rm_device.cold+0xa8/0x298 [btrfs] btrfs_ioctl+0x206c/0x22a0 [btrfs] ksys_ioctl+0xe2/0x140 __x64_sys_ioctl+0x1e/0x29 do_syscall_64+0x96/0x150 entry_SYSCALL_64_after_hwframe+0x44/0xa9 RIP: 0033:0x7f4047c6288b :: This is because, at the end of the seed device-delete, we try to remove the seed's devid sysfs entry. But for the seed devices under the sprout fs, we don't initialize the devid kobject yet. So add a kobject state check, which takes care of the bug. Fixes: 668e48a ("btrfs: sysfs, add devid/dev_state kobject and device attributes") CC: [email protected] # 5.6+ Signed-off-by: Anand Jain <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 35be885 commit b5ddcff

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

fs/btrfs/sysfs.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,10 +1170,12 @@ int btrfs_sysfs_remove_devices_dir(struct btrfs_fs_devices *fs_devices,
11701170
disk_kobj->name);
11711171
}
11721172

1173-
kobject_del(&one_device->devid_kobj);
1174-
kobject_put(&one_device->devid_kobj);
1173+
if (one_device->devid_kobj.state_initialized) {
1174+
kobject_del(&one_device->devid_kobj);
1175+
kobject_put(&one_device->devid_kobj);
11751176

1176-
wait_for_completion(&one_device->kobj_unregister);
1177+
wait_for_completion(&one_device->kobj_unregister);
1178+
}
11771179

11781180
return 0;
11791181
}
@@ -1186,10 +1188,12 @@ int btrfs_sysfs_remove_devices_dir(struct btrfs_fs_devices *fs_devices,
11861188
sysfs_remove_link(fs_devices->devices_kobj,
11871189
disk_kobj->name);
11881190
}
1189-
kobject_del(&one_device->devid_kobj);
1190-
kobject_put(&one_device->devid_kobj);
1191+
if (one_device->devid_kobj.state_initialized) {
1192+
kobject_del(&one_device->devid_kobj);
1193+
kobject_put(&one_device->devid_kobj);
11911194

1192-
wait_for_completion(&one_device->kobj_unregister);
1195+
wait_for_completion(&one_device->kobj_unregister);
1196+
}
11931197
}
11941198

11951199
return 0;

0 commit comments

Comments
 (0)