Yang Yingliang
2022-Oct-21 02:20 UTC
[Ocfs2-devel] [PATCH 00/11] fix memory leak while kset_register() fails
The previous discussion link: https://lore.kernel.org/lkml/0db486eb-6927-927e-3629-958f8f211194 at huawei.com/T/ kset_register() is currently used in some places without calling kset_put() in error path, because the callers think it should be kset internal thing to do, but the driver core can not know what caller doing with that memory at times. The memory could be freed both in kset_put() and error path of caller, if it is called in kset_register(). So make the function documentation more explicit about calling kset_put() in the error path of caller first, so that people have a chance to know what to do here, then fixes this leaks by calling kset_put() from callers. Liu Shixin (1): ubifs: Fix memory leak in ubifs_sysfs_init() Yang Yingliang (10): kset: fix documentation for kset_register() kset: add null pointer check in kset_put() bus: fix possible memory leak in bus_register() kobject: fix possible memory leak in kset_create_and_add() class: fix possible memory leak in __class_register() firmware: qemu_fw_cfg: fix possible memory leak in fw_cfg_build_symlink() f2fs: fix possible memory leak in f2fs_init_sysfs() erofs: fix possible memory leak in erofs_init_sysfs() ocfs2: possible memory leak in mlog_sys_init() drm/amdgpu/discovery: fix possible memory leak drivers/base/bus.c | 4 +++- drivers/base/class.c | 6 ++++++ drivers/firmware/qemu_fw_cfg.c | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 5 +++-- fs/erofs/sysfs.c | 4 +++- fs/f2fs/sysfs.c | 4 +++- fs/ocfs2/cluster/masklog.c | 7 ++++++- fs/ubifs/sysfs.c | 2 ++ include/linux/kobject.h | 3 ++- lib/kobject.c | 5 ++++- 10 files changed, 33 insertions(+), 9 deletions(-) -- 2.25.1
Yang Yingliang
2022-Oct-21 02:20 UTC
[Ocfs2-devel] [PATCH 01/11] kset: fix documentation for kset_register()
kset_register() is currently used in some places without calling kset_put() in error path, because the callers think it should be kset internal thing to do, but the driver core can not know what caller doing with that memory at times. The memory could be freed both in kset_put() and error path of caller, if it is called in kset_register(). So make the function documentation more explicit about calling kset_put() in the error path of caller. Signed-off-by: Yang Yingliang <yangyingliang at huawei.com> --- lib/kobject.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/kobject.c b/lib/kobject.c index a0b2dbfcfa23..6da04353d974 100644 --- a/lib/kobject.c +++ b/lib/kobject.c @@ -834,6 +834,9 @@ EXPORT_SYMBOL_GPL(kobj_sysfs_ops); /** * kset_register() - Initialize and add a kset. * @k: kset. + * + * If this function returns an error, kset_put() must be called to + * properly clean up the memory associated with the object. */ int kset_register(struct kset *k) { -- 2.25.1
Yang Yingliang
2022-Oct-21 02:20 UTC
[Ocfs2-devel] [PATCH 02/11] kset: add null pointer check in kset_put()
kset_put() can be called from drivers, add null pointer check to make it more robust. Signed-off-by: Yang Yingliang <yangyingliang at huawei.com> --- include/linux/kobject.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/linux/kobject.h b/include/linux/kobject.h index 57fb972fea05..e81de8ba41a2 100644 --- a/include/linux/kobject.h +++ b/include/linux/kobject.h @@ -195,7 +195,8 @@ static inline struct kset *kset_get(struct kset *k) static inline void kset_put(struct kset *k) { - kobject_put(&k->kobj); + if (k) + kobject_put(&k->kobj); } static inline const struct kobj_type *get_ktype(struct kobject *kobj) -- 2.25.1
Yang Yingliang
2022-Oct-21 02:20 UTC
[Ocfs2-devel] [PATCH 03/11] bus: fix possible memory leak in bus_register()
Inject fault while loading module (e.g. edac_core.ko), kset_register() may fail in bus_register(), if it fails, but the refcount of kobject is not decreased to 0, the name allocated in kobject_set_name() is leaked. To fix this by calling kset_put(), so that name can be freed in callback function kobject_cleanup(). unreferenced object 0xffff888103bddb68 (size 8): comm "systemd-udevd", pid 341, jiffies 4294903262 (age 42.212s) hex dump (first 8 bytes): 65 64 61 63 00 00 00 00 edac.... backtrace: [<000000009e31d566>] __kmalloc_track_caller+0x1ae/0x320 [<00000000e4cfd8de>] kstrdup+0x3a/0x70 [<000000003d0ec369>] kstrdup_const+0x68/0x80 [<000000008e5c3b20>] kvasprintf_const+0x10b/0x190 [<00000000b9a945aa>] kobject_set_name_vargs+0x56/0x150 [<000000000df9278c>] kobject_set_name+0xab/0xe0 [<00000000f51dc49f>] bus_register+0x132/0x350 [<000000007d91c2e5>] subsys_register+0x23/0x220 Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Yang Yingliang <yangyingliang at huawei.com> --- drivers/base/bus.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/drivers/base/bus.c b/drivers/base/bus.c index 7ca47e5b3c1f..301b5330f9d8 100644 --- a/drivers/base/bus.c +++ b/drivers/base/bus.c @@ -804,8 +804,10 @@ int bus_register(struct bus_type *bus) priv->drivers_autoprobe = 1; retval = kset_register(&priv->subsys); - if (retval) + if (retval) { + kset_put(&priv->subsys); goto out; + } retval = bus_create_file(bus, &bus_attr_uevent); if (retval) -- 2.25.1
Yang Yingliang
2022-Oct-21 02:20 UTC
[Ocfs2-devel] [PATCH 04/11] kobject: fix possible memory leak in kset_create_and_add()
Inject fault while loading module (e.g. qemu_fw_cfg.ko), kset_register() may fail in kset_create_and_add(), if it fails, but the refcount of kobject is not decreased to 0, the name allocated in kset_create() is leaked. To fix this by calling kset_put(), so that name can be freed in callback function kobject_cleanup() and kset can be freed in kset_release(). unreferenced object 0xffff888103cc8c08 (size 8): comm "modprobe", pid 508, jiffies 4294915182 (age 120.020s) hex dump (first 8 bytes): 62 79 5f 6e 61 6d 65 00 by_name. backtrace: [<00000000572f97f9>] __kmalloc_track_caller+0x1ae/0x320 [<00000000a167a5cc>] kstrdup+0x3a/0x70 [<000000001cd0d05e>] kstrdup_const+0x68/0x80 [<00000000b9101e6d>] kvasprintf_const+0x10b/0x190 [<0000000088f2b8df>] kobject_set_name_vargs+0x56/0x150 [<000000003f8aca68>] kobject_set_name+0xab/0xe0 [<00000000249f7816>] kset_create_and_add+0x72/0x200 Fixes: b727c702896f ("kset: add kset_create_and_add function") Signed-off-by: Yang Yingliang <yangyingliang at huawei.com> --- lib/kobject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/kobject.c b/lib/kobject.c index 6da04353d974..e77f37200876 100644 --- a/lib/kobject.c +++ b/lib/kobject.c @@ -985,7 +985,7 @@ struct kset *kset_create_and_add(const char *name, return NULL; error = kset_register(kset); if (error) { - kfree(kset); + kset_put(kset); return NULL; } return kset; -- 2.25.1
Yang Yingliang
2022-Oct-21 02:20 UTC
[Ocfs2-devel] [PATCH 05/11] class: fix possible memory leak in __class_register()
Inject fault while loading module (e.g. pktcdvd.ko), kset_register() may fail in __class_register(), if it fails, but the refcount of kobject is not decreased to 0, the name allocated in kobject_set_name() is leaked. To fix this by calling kfree_const(). unreferenced object 0xffff888102fa8190 (size 8): comm "modprobe", pid 502, jiffies 4294906074 (age 49.296s) hex dump (first 8 bytes): 70 6b 74 63 64 76 64 00 pktcdvd. backtrace: [<00000000e7c7703d>] __kmalloc_track_caller+0x1ae/0x320 [<000000005e4d70bc>] kstrdup+0x3a/0x70 [<00000000c2e5e85a>] kstrdup_const+0x68/0x80 [<000000000049a8c7>] kvasprintf_const+0x10b/0x190 [<0000000029123163>] kobject_set_name_vargs+0x56/0x150 [<00000000747219c9>] kobject_set_name+0xab/0xe0 [<0000000005f1ea4e>] __class_register+0x15c/0x49a If class_add_groups() fails, it need delete kobject and free its name, besides, subsys_private also need be freed. unreferenced object 0xffff888037274000 (size 1024): comm "modprobe", pid 502, jiffies 4294906074 (age 49.296s) hex dump (first 32 bytes): 00 40 27 37 80 88 ff ff 00 40 27 37 80 88 ff ff .@'7.....@'7.... 00 00 00 00 ad 4e ad de ff ff ff ff 00 00 00 00 .....N.......... backtrace: [<00000000151f9600>] kmem_cache_alloc_trace+0x17c/0x2f0 [<00000000ecf3dd95>] __class_register+0x86/0x49a It can not call kset_put() or kset_unregister() in error path, because the 'cls' will be freed in callback function class_release() and it also freed in error path, it will cause double free. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Yang Yingliang <yangyingliang at huawei.com> --- drivers/base/class.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/drivers/base/class.c b/drivers/base/class.c index 64f7b9a0970f..87de0a04ee9b 100644 --- a/drivers/base/class.c +++ b/drivers/base/class.c @@ -187,11 +187,17 @@ int __class_register(struct class *cls, struct lock_class_key *key) error = kset_register(&cp->subsys); if (error) { + kfree_const(cp->subsys.kobj.name); kfree(cp); return error; } error = class_add_groups(class_get(cls), cls->class_groups); class_put(cls); + if (error) { + kobject_del(&cp->subsys.kobj); + kfree_const(cp->subsys.kobj.name); + kfree(cp); + } return error; } EXPORT_SYMBOL_GPL(__class_register); -- 2.25.1
Yang Yingliang
2022-Oct-21 02:20 UTC
[Ocfs2-devel] [PATCH 06/11] firmware: qemu_fw_cfg: fix possible memory leak in fw_cfg_build_symlink()
Inject fault while loading module, kset_register() may fail, if it fails, but the refcount of kobject is not decreased to 0, the name allocated in kobject_set_name() is leaked. To fix this by calling kset_put(), so that name can be freed in callback function kobject_cleanup() and 'subdir' is freed in kset_release(). unreferenced object 0xffff88810ad69050 (size 8): comm "swapper/0", pid 1, jiffies 4294677178 (age 38.812s) hex dump (first 8 bytes): 65 74 63 00 81 88 ff ff etc..... backtrace: [<00000000a80c7bf1>] __kmalloc_node_track_caller+0x44/0x1b0 [<000000003f0167c7>] kstrdup+0x3a/0x70 [<0000000049336709>] kstrdup_const+0x41/0x60 [<00000000175616e4>] kvasprintf_const+0xf5/0x180 [<000000004bcc30f7>] kobject_set_name_vargs+0x56/0x150 [<000000004b0251bd>] kobject_set_name+0xab/0xe0 [<00000000700151fb>] fw_cfg_sysfs_probe+0xa5b/0x1320 Fixes: 246c46ebaeae ("firmware: create directory hierarchy for sysfs fw_cfg entries") Signed-off-by: Yang Yingliang <yangyingliang at huawei.com> --- drivers/firmware/qemu_fw_cfg.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/firmware/qemu_fw_cfg.c b/drivers/firmware/qemu_fw_cfg.c index a69399a6b7c0..d036e69cabbb 100644 --- a/drivers/firmware/qemu_fw_cfg.c +++ b/drivers/firmware/qemu_fw_cfg.c @@ -544,7 +544,7 @@ static int fw_cfg_build_symlink(struct kset *dir, } ret = kset_register(subdir); if (ret) { - kfree(subdir); + kset_put(subdir); break; } -- 2.25.1
Yang Yingliang
2022-Oct-21 02:20 UTC
[Ocfs2-devel] [PATCH 07/11] f2fs: fix possible memory leak in f2fs_init_sysfs()
Inject fault while loading module, kset_register() may fail, if it fails, but the refcount of kobject is not decreased to 0, the name allocated in kobject_set_name() is leaked. Fix this by calling kset_put(), so that name can be freed in callback function kobject_cleanup(). unreferenced object 0xffff888101b7cc80 (size 8): comm "modprobe", pid 252, jiffies 4294691378 (age 31.760s) hex dump (first 8 bytes): 66 32 66 73 00 88 ff ff f2fs.... backtrace: [<000000001db5b408>] __kmalloc_node_track_caller+0x44/0x1b0 [<000000002783a073>] kstrdup+0x3a/0x70 [<00000000ead2b281>] kstrdup_const+0x63/0x80 [<000000003e5cf8f7>] kvasprintf_const+0x149/0x180 [<00000000c4d949ff>] kobject_set_name_vargs+0x56/0x150 [<0000000044611660>] kobject_set_name+0xab/0xe0 Fixes: bf9e697ecd42 ("f2fs: expose features to sysfs entry") Signed-off-by: Yang Yingliang <yangyingliang at huawei.com> Reviewed-by: Chao Yu <chao at kernel.org> --- fs/f2fs/sysfs.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/fs/f2fs/sysfs.c b/fs/f2fs/sysfs.c index df27afd71ef4..2ef7a48967be 100644 --- a/fs/f2fs/sysfs.c +++ b/fs/f2fs/sysfs.c @@ -1250,8 +1250,10 @@ int __init f2fs_init_sysfs(void) kobject_set_name(&f2fs_kset.kobj, "f2fs"); f2fs_kset.kobj.parent = fs_kobj; ret = kset_register(&f2fs_kset); - if (ret) + if (ret) { + kset_put(&f2fs_kset); return ret; + } ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype, NULL, "features"); -- 2.25.1
Yang Yingliang
2022-Oct-21 02:20 UTC
[Ocfs2-devel] [PATCH 08/11] erofs: fix possible memory leak in erofs_init_sysfs()
Inject fault while loading module, kset_register() may fail, if it fails, but the refcount of kobject is not decreased to 0, the name allocated in kobject_set_name() is leaked. Fix this by calling kset_put(), so that name can be freed in callback function kobject_cleanup(). unreferenced object 0xffff888101d228c0 (size 8): comm "modprobe", pid 276, jiffies 4294722700 (age 13.151s) hex dump (first 8 bytes): 65 72 6f 66 73 00 ff ff erofs... backtrace: [<00000000e2a9a4a6>] __kmalloc_node_track_caller+0x44/0x1b0 [<00000000b8ce02de>] kstrdup+0x3a/0x70 [<000000004a0e01d2>] kstrdup_const+0x63/0x80 [<00000000051b6cda>] kvasprintf_const+0x149/0x180 [<000000004dc51dad>] kobject_set_name_vargs+0x56/0x150 [<00000000b30f0bad>] kobject_set_name+0xab/0xe0 Fixes: 168e9a76200c ("erofs: add sysfs interface") Signed-off-by: Yang Yingliang <yangyingliang at huawei.com> --- fs/erofs/sysfs.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/fs/erofs/sysfs.c b/fs/erofs/sysfs.c index 783bb7b21b51..653b35001bc5 100644 --- a/fs/erofs/sysfs.c +++ b/fs/erofs/sysfs.c @@ -254,8 +254,10 @@ int __init erofs_init_sysfs(void) kobject_set_name(&erofs_root.kobj, "erofs"); erofs_root.kobj.parent = fs_kobj; ret = kset_register(&erofs_root); - if (ret) + if (ret) { + kset_put(&erofs_root); goto root_err; + } ret = kobject_init_and_add(&erofs_feat, &erofs_feat_ktype, NULL, "features"); -- 2.25.1
Yang Yingliang
2022-Oct-21 02:21 UTC
[Ocfs2-devel] [PATCH 09/11] ocfs2: possible memory leak in mlog_sys_init()
Inject fault while loading module, kset_register() may fail, if it fails, but the refcount of kobject is not decreased to 0, the name allocated in kobject_set_name() is leaked. Fix this by calling kset_put(), so that name can be freed in callback function kobject_cleanup(). unreferenced object 0xffff888100da9348 (size 8): comm "modprobe", pid 257, jiffies 4294701096 (age 33.334s) hex dump (first 8 bytes): 6c 6f 67 6d 61 73 6b 00 logmask. backtrace: [<00000000306e441c>] __kmalloc_node_track_caller+0x44/0x1b0 [<000000007c491a9e>] kstrdup+0x3a/0x70 [<0000000015719a3b>] kstrdup_const+0x63/0x80 [<0000000084e458ea>] kvasprintf_const+0x149/0x180 [<0000000091302b42>] kobject_set_name_vargs+0x56/0x150 [<000000005f48eeac>] kobject_set_name+0xab/0xe0 Fixes: 34980ca8faeb ("Drivers: clean up direct setting of the name of a kset") Signed-off-by: Yang Yingliang <yangyingliang at huawei.com> --- fs/ocfs2/cluster/masklog.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/fs/ocfs2/cluster/masklog.c b/fs/ocfs2/cluster/masklog.c index 563881ddbf00..7f9ba816d955 100644 --- a/fs/ocfs2/cluster/masklog.c +++ b/fs/ocfs2/cluster/masklog.c @@ -156,6 +156,7 @@ static struct kset mlog_kset = { int mlog_sys_init(struct kset *o2cb_kset) { int i = 0; + int ret; while (mlog_attrs[i].attr.mode) { mlog_default_attrs[i] = &mlog_attrs[i].attr; @@ -165,7 +166,11 @@ int mlog_sys_init(struct kset *o2cb_kset) kobject_set_name(&mlog_kset.kobj, "logmask"); mlog_kset.kobj.kset = o2cb_kset; - return kset_register(&mlog_kset); + ret = kset_register(&mlog_kset); + if (ret) + kset_put(&mlog_kset); + + return ret; } void mlog_sys_shutdown(void) -- 2.25.1
Yang Yingliang
2022-Oct-21 02:21 UTC
[Ocfs2-devel] [PATCH 10/11] drm/amdgpu/discovery: fix possible memory leak
If kset_register() fails, the refcount of kobject is not 0, the name allocated in kobject_set_name(&kset.kobj, ...) is leaked. Fix this by calling kset_put(), so that it will be freed in callback function kobject_cleanup(). Cc: stable at vger.kernel.org Fixes: a6c40b178092 ("drm/amdgpu: Show IP discovery in sysfs") Signed-off-by: Yang Yingliang <yangyingliang at huawei.com> Reviewed-by: Luben Tuikov <luben.tuikov at amd.com> --- drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c index 3993e6134914..638edcf70227 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c @@ -863,7 +863,7 @@ static int amdgpu_discovery_sysfs_ips(struct amdgpu_device *adev, res = kset_register(&ip_hw_id->hw_id_kset); if (res) { DRM_ERROR("Couldn't register ip_hw_id kset"); - kfree(ip_hw_id); + kset_put(&ip_hw_id->hw_id_kset); return res; } if (hw_id_names[ii]) { @@ -954,7 +954,7 @@ static int amdgpu_discovery_sysfs_recurse(struct amdgpu_device *adev) res = kset_register(&ip_die_entry->ip_kset); if (res) { DRM_ERROR("Couldn't register ip_die_entry kset"); - kfree(ip_die_entry); + kset_put(&ip_die_entry->ip_kset); return res; } @@ -989,6 +989,7 @@ static int amdgpu_discovery_sysfs_init(struct amdgpu_device *adev) res = kset_register(&adev->ip_top->die_kset); if (res) { DRM_ERROR("Couldn't register die_kset"); + kset_put(&adev->ip_top->die_kset); goto Err; } -- 2.25.1
Yang Yingliang
2022-Oct-21 02:21 UTC
[Ocfs2-devel] [PATCH 11/11] ubifs: Fix memory leak in ubifs_sysfs_init()
From: Liu Shixin <liushixin2 at huawei.com> When insmod ubifs.ko, a kmemleak reported as below: unreferenced object 0xffff88817fb1a780 (size 8): comm "insmod", pid 25265, jiffies 4295239702 (age 100.130s) hex dump (first 8 bytes): 75 62 69 66 73 00 ff ff ubifs... backtrace: [<ffffffff81b3fc4c>] slab_post_alloc_hook+0x9c/0x3c0 [<ffffffff81b44bf3>] __kmalloc_track_caller+0x183/0x410 [<ffffffff8198d3da>] kstrdup+0x3a/0x80 [<ffffffff8198d486>] kstrdup_const+0x66/0x80 [<ffffffff83989325>] kvasprintf_const+0x155/0x190 [<ffffffff83bf55bb>] kobject_set_name_vargs+0x5b/0x150 [<ffffffff83bf576b>] kobject_set_name+0xbb/0xf0 [<ffffffff8100204c>] do_one_initcall+0x14c/0x5a0 [<ffffffff8157e380>] do_init_module+0x1f0/0x660 [<ffffffff815857be>] load_module+0x6d7e/0x7590 [<ffffffff8158644f>] __do_sys_finit_module+0x19f/0x230 [<ffffffff815866b3>] __x64_sys_finit_module+0x73/0xb0 [<ffffffff88c98e85>] do_syscall_64+0x35/0x80 [<ffffffff88e00087>] entry_SYSCALL_64_after_hwframe+0x63/0xcd When kset_register() failed, we should call kset_put to cleanup it. Fixes: 2e3cbf425804 ("ubifs: Export filesystem error counters") Signed-off-by: Liu Shixin <liushixin2 at huawei.com> Signed-off-by: Yang Yingliang <yangyingliang at huawei.com> --- fs/ubifs/sysfs.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fs/ubifs/sysfs.c b/fs/ubifs/sysfs.c index 06ad8fa1fcfb..54270ad36321 100644 --- a/fs/ubifs/sysfs.c +++ b/fs/ubifs/sysfs.c @@ -144,6 +144,8 @@ int __init ubifs_sysfs_init(void) kobject_set_name(&ubifs_kset.kobj, "ubifs"); ubifs_kset.kobj.parent = fs_kobj; ret = kset_register(&ubifs_kset); + if (ret) + kset_put(&ubifs_kset); return ret; } -- 2.25.1
Luben Tuikov
2022-Oct-21 05:29 UTC
[Ocfs2-devel] [PATCH 00/11] fix memory leak while kset_register() fails
On 2022-10-20 22:20, Yang Yingliang wrote:> The previous discussion link: > https://lore.kernel.org/lkml/0db486eb-6927-927e-3629-958f8f211194 at huawei.com/T/The very first discussion on this was here: https://www.spinics.net/lists/dri-devel/msg368077.html Please use this link, and not the that one up there you which quoted above, and whose commit description is taken verbatim from the this link.> > kset_register() is currently used in some places without calling > kset_put() in error path, because the callers think it should be > kset internal thing to do, but the driver core can not know what > caller doing with that memory at times. The memory could be freed > both in kset_put() and error path of caller, if it is called in > kset_register().As I explained in the link above, the reason there's a memory leak is that one cannot call kset_register() without the kset->kobj.name being set--kobj_add_internal() returns -EINVAL, in this case, i.e. kset_register() fails with -EINVAL. Thus, the most common usage is something like this: kobj_set_name(&kset->kobj, format, ...); kset->kobj.kset = parent_kset; kset->kobj.ktype = ktype; res = kset_register(kset); So, what is being leaked, is the memory allocated in kobj_set_name(), by the common idiom shown above. This needs to be mentioned in the documentation, at least, in case, in the future this is absolved in kset_register() redesign, etc. Regards, Luben> > So make the function documentation more explicit about calling > kset_put() in the error path of caller first, so that people > have a chance to know what to do here, then fixes this leaks > by calling kset_put() from callers. > > Liu Shixin (1): > ubifs: Fix memory leak in ubifs_sysfs_init() > > Yang Yingliang (10): > kset: fix documentation for kset_register() > kset: add null pointer check in kset_put() > bus: fix possible memory leak in bus_register() > kobject: fix possible memory leak in kset_create_and_add() > class: fix possible memory leak in __class_register() > firmware: qemu_fw_cfg: fix possible memory leak in > fw_cfg_build_symlink() > f2fs: fix possible memory leak in f2fs_init_sysfs() > erofs: fix possible memory leak in erofs_init_sysfs() > ocfs2: possible memory leak in mlog_sys_init() > drm/amdgpu/discovery: fix possible memory leak > > drivers/base/bus.c | 4 +++- > drivers/base/class.c | 6 ++++++ > drivers/firmware/qemu_fw_cfg.c | 2 +- > drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 5 +++-- > fs/erofs/sysfs.c | 4 +++- > fs/f2fs/sysfs.c | 4 +++- > fs/ocfs2/cluster/masklog.c | 7 ++++++- > fs/ubifs/sysfs.c | 2 ++ > include/linux/kobject.h | 3 ++- > lib/kobject.c | 5 ++++- > 10 files changed, 33 insertions(+), 9 deletions(-) >
Greg KH
2022-Oct-21 05:37 UTC
[Ocfs2-devel] [PATCH 00/11] fix memory leak while kset_register() fails
On Fri, Oct 21, 2022 at 01:29:31AM -0400, Luben Tuikov wrote:> On 2022-10-20 22:20, Yang Yingliang wrote: > > The previous discussion link: > > https://lore.kernel.org/lkml/0db486eb-6927-927e-3629-958f8f211194 at huawei.com/T/ > > The very first discussion on this was here: > > https://www.spinics.net/lists/dri-devel/msg368077.html > > Please use this link, and not the that one up there you which quoted above, > and whose commit description is taken verbatim from the this link. > > > > > kset_register() is currently used in some places without calling > > kset_put() in error path, because the callers think it should be > > kset internal thing to do, but the driver core can not know what > > caller doing with that memory at times. The memory could be freed > > both in kset_put() and error path of caller, if it is called in > > kset_register(). > > As I explained in the link above, the reason there's > a memory leak is that one cannot call kset_register() without > the kset->kobj.name being set--kobj_add_internal() returns -EINVAL, > in this case, i.e. kset_register() fails with -EINVAL. > > Thus, the most common usage is something like this: > > kobj_set_name(&kset->kobj, format, ...); > kset->kobj.kset = parent_kset; > kset->kobj.ktype = ktype; > res = kset_register(kset); > > So, what is being leaked, is the memory allocated in kobj_set_name(), > by the common idiom shown above. This needs to be mentioned in > the documentation, at least, in case, in the future this is absolved > in kset_register() redesign, etc.Based on this, can kset_register() just clean up from itself when an error happens? Ideally that would be the case, as the odds of a kset being embedded in a larger structure is probably slim, but we would have to search the tree to make sure. thanks, greg k-h
Yang Yingliang
2022-Oct-21 07:25 UTC
[Ocfs2-devel] [PATCH 00/11] fix memory leak while kset_register() fails
Hi, On 2022/10/21 13:29, Luben Tuikov wrote:> On 2022-10-20 22:20, Yang Yingliang wrote: >> The previous discussion link: >> https://lore.kernel.org/lkml/0db486eb-6927-927e-3629-958f8f211194 at huawei.com/T/ > The very first discussion on this was here: > > https://www.spinics.net/lists/dri-devel/msg368077.html > > Please use this link, and not the that one up there you which quoted above, > and whose commit description is taken verbatim from the this link.I found this leaks in bus_register()/class_register()/kset_create_and_add() at first, and describe the reason in these patches which is using kobject_set_name() description, here is the patches: https://lore.kernel.org/lkml/20221017014957.156645-1-yangyingliang at huawei.com/T/ https://lore.kernel.org/lkml/20221017031335.1845383-1-yangyingliang at huawei.com/ https://lore.kernel.org/lkml/Y0zfPKAgQSrYZg5o at kroah.com/T/ And then I found other subsystem also have this problem, so posted the fix patches for them (including qemu_fw_cfg/f2fs/erofs/ocfs2/amdgpu_discovery): https://www.mail-archive.com/qemu-devel at nongnu.org/msg915553.html https://lore.kernel.org/linux-f2fs-devel/7908686b-9a7c-b754-d312-d689fc28366e at kernel.org/T/#t https://lore.kernel.org/linux-erofs/20221018073947.693206-1-yangyingliang at huawei.com/ https://lore.kernel.org/lkml/0db486eb-6927-927e-3629-958f8f211194 at huawei.com/T/ https://www.spinics.net/lists/dri-devel/msg368092.html In the amdgpu_discovery patch, I sent a old one which using wrong description and you pointer out, and then I send a v2. And then the maintainer of ocfs2 has different thought about this, so we had a discussion in the link that I gave out, and Greg suggested me to update kset_register() documentation and then put the fix patches together in one series, so I sent this patchset and use the link. Thanks, Yang> >> kset_register() is currently used in some places without calling >> kset_put() in error path, because the callers think it should be >> kset internal thing to do, but the driver core can not know what >> caller doing with that memory at times. The memory could be freed >> both in kset_put() and error path of caller, if it is called in >> kset_register(). > As I explained in the link above, the reason there's > a memory leak is that one cannot call kset_register() without > the kset->kobj.name being set--kobj_add_internal() returns -EINVAL, > in this case, i.e. kset_register() fails with -EINVAL. > > Thus, the most common usage is something like this: > > kobj_set_name(&kset->kobj, format, ...); > kset->kobj.kset = parent_kset; > kset->kobj.ktype = ktype; > res = kset_register(kset); > > So, what is being leaked, is the memory allocated in kobj_set_name(), > by the common idiom shown above. This needs to be mentioned in > the documentation, at least, in case, in the future this is absolved > in kset_register() redesign, etc. > > Regards, > Luben > >> So make the function documentation more explicit about calling >> kset_put() in the error path of caller first, so that people >> have a chance to know what to do here, then fixes this leaks >> by calling kset_put() from callers. >> >> Liu Shixin (1): >> ubifs: Fix memory leak in ubifs_sysfs_init() >> >> Yang Yingliang (10): >> kset: fix documentation for kset_register() >> kset: add null pointer check in kset_put() >> bus: fix possible memory leak in bus_register() >> kobject: fix possible memory leak in kset_create_and_add() >> class: fix possible memory leak in __class_register() >> firmware: qemu_fw_cfg: fix possible memory leak in >> fw_cfg_build_symlink() >> f2fs: fix possible memory leak in f2fs_init_sysfs() >> erofs: fix possible memory leak in erofs_init_sysfs() >> ocfs2: possible memory leak in mlog_sys_init() >> drm/amdgpu/discovery: fix possible memory leak >> >> drivers/base/bus.c | 4 +++- >> drivers/base/class.c | 6 ++++++ >> drivers/firmware/qemu_fw_cfg.c | 2 +- >> drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 5 +++-- >> fs/erofs/sysfs.c | 4 +++- >> fs/f2fs/sysfs.c | 4 +++- >> fs/ocfs2/cluster/masklog.c | 7 ++++++- >> fs/ubifs/sysfs.c | 2 ++ >> include/linux/kobject.h | 3 ++- >> lib/kobject.c | 5 ++++- >> 10 files changed, 33 insertions(+), 9 deletions(-) >> > .