From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id DA93DCD4F4E for ; Thu, 5 Sep 2024 03:41:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 60A5F6B043B; Wed, 4 Sep 2024 23:41:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5B9E26B043C; Wed, 4 Sep 2024 23:41:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 480DA6B043D; Wed, 4 Sep 2024 23:41:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 2BB1C6B043B for ; Wed, 4 Sep 2024 23:41:24 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 9D15E808A8 for ; Thu, 5 Sep 2024 03:41:23 +0000 (UTC) X-FDA: 82529284446.01.DA484C8 Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) by imf01.hostedemail.com (Postfix) with ESMTP id 6946940018 for ; Thu, 5 Sep 2024 03:41:20 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf01.hostedemail.com: domain of ruanjinjie@huawei.com designates 45.249.212.191 as permitted sender) smtp.mailfrom=ruanjinjie@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725507657; a=rsa-sha256; cv=none; b=LPiEeis3SZDVcmiAxQTIsiYl0iTp1AYvZpznnj9tC9T2cSIcQy72QciK4olRbPdoWaSrhh gSYB3XzXezR9mu+2gDMvEuqh+gBeT0nhR3h99FI85BUNs0IdNk3cG/zxHTRTn+B9zJZffl rwDVOpIR0sgAk953dlM8dgxeD0chv/Y= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf01.hostedemail.com: domain of ruanjinjie@huawei.com designates 45.249.212.191 as permitted sender) smtp.mailfrom=ruanjinjie@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725507657; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=05dDidfTeNJRR+hj7DO/504gzhw/TmOyGgBoLx6j010=; b=7YQ50dzhvYekBGQ9k8p2DMA+7jhlPfoPj1ODxz46oD2aTMtS1ru1j30aYzTxc/d58halJt uHayp21jp+TzSIUtfgA/+wsAOFwpPDTtukjdlkNuoYs8XcFV68vlAaQrJEmU6ZRklPqA/Z LPF8e4LKBIeh2bFir3QVItV++Lqm8s4= Received: from mail.maildlp.com (unknown [172.19.163.17]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4WzlWV1FW7z1j88L; Thu, 5 Sep 2024 11:40:54 +0800 (CST) Received: from kwepemh500013.china.huawei.com (unknown [7.202.181.146]) by mail.maildlp.com (Postfix) with ESMTPS id 3A3791A0188; Thu, 5 Sep 2024 11:41:15 +0800 (CST) Received: from [10.67.109.254] (10.67.109.254) by kwepemh500013.china.huawei.com (7.202.181.146) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Thu, 5 Sep 2024 11:41:14 +0800 Message-ID: <3780a622-03f2-4cfe-5705-0e9d0be61d57@huawei.com> Date: Thu, 5 Sep 2024 11:41:13 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.2.0 Subject: Re: [PATCH v4 3/3] mm/slub: Fix memory leak of kobj->name in sysfs_slab_add() Content-Language: en-US To: Liu Shixin , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Vlastimil Babka , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com> CC: , References: <20221112114602.1268989-1-liushixin2@huawei.com> <20221112114602.1268989-4-liushixin2@huawei.com> From: Jinjie Ruan In-Reply-To: <20221112114602.1268989-4-liushixin2@huawei.com> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-Originating-IP: [10.67.109.254] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To kwepemh500013.china.huawei.com (7.202.181.146) X-Rspam-User: X-Rspamd-Queue-Id: 6946940018 X-Rspamd-Server: rspam01 X-Stat-Signature: z5pi6rz3zokdqngdydu4tonuywxiadzr X-HE-Tag: 1725507680-431364 X-HE-Meta: U2FsdGVkX19NHjkV8GSRLlfi6cTFxUnnJ8j4BbE8f8faQ7P9YqQrfENdmRDrRo92MSY2EioMFt6SmqY8DdLyteDfyt+BUy+yf1xCrNHfKGwjh+oON8mG6Nqi8jKVUU2upcrvwxqa6UrJOT4YXF7nv7e3pcJjpKo4miIdULsVyi3hsUnA9Im08MjlFiPmpf2IRrOfrmFQPxXx6qMY8J8uGkkYPuygnzwubwEY/oqKFQIej3vXyiwtu42M6OYdl96RshEhkbMne/KozoYut54Bjs0do5zVwfUsak6ZwP/yD5OlDmT6tpkiaEiAjfEIl+gIakF1WD8nSHcs6C4APfgqouP1hsyLgL+zZC++E/78um4JvJCBrusW797pq6AqQlV3j+YEuweNxc0hoeewdiWBPiRNIcQx3wQdXUJvOSsq0VHlVZ49/4n3S0khWCDbzLRfvzauwHcTk55ynhN1zRsWg9GQkv8nD4W+N9GN5f8bTBTPCqTN73slbX+dpDy0YytmWva2POVVbYJHmqlufSyLPOPcY1h8RRHwOLkjqlZi4z1fdBB/IGmAcPEJ7B7SeDB5MFszI61elQ9fQ90llwy2i1S1WVAhoxyL6VjdKPYkw5VjjX3SCW+buURDSw/Y8GsEoQ1TXvRVcl8XIc1Hvf3GQSRYRw4TTlJ2nYWGh6TzrzqdIaYlX3AfZuXj0GN621a835I+g9tPOAgYhSD2mKrXXA2321WifiZpd5+i36R9To8RmsEdNpc/nIo3CUE4+acRkLTVSHR0fWkZ6iJMTOQaU+YATv9oFsiiN7HWEFByy2Zvw1isa6SE5ntIrtyj/JzQMI+e6kq/10excOGezKHCtZF8Jd44BgB+b3CLcGtWG/cKUzZELDmYFRHQiavuorPZtwHO6wE50gAFOyWgSa+8Un+IzvEVdI1lk6quBrE9A2oTqTM+sMk8ysA++sm9XET+4/Vd1h8trsg5jNxOgVB UtzOVCvr ASKO+AUws3cbyHIhJM/Lsoq3K7QfItoaVbxMMuPc6ojes3GvTKqWJ4FShJfNmsTL1Nxf/oHdaImynoyzQK+ZoUC2jznjU6iWV/d9pbgCfsf8PGoheeuCW/7jUd9xYWyNGM1BcIlwr6SxQFZVRLgBlXM5aI5oga49XptVs7t9kV763PGu4yhqTQlqrPzltkMjfEKPyzaOXjjOj7NfjBOmcurP4KoyvacScp16NUYbBKSvougBkS6v2p5+AvxA8vwxz4PfQLDxzIi2dJ6TxCUyjzfdf5re93G3+0WfMSRtTiDtZowo= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On 2022/11/12 19:46, Liu Shixin wrote: > There is a memory leak of kobj->name in sysfs_slab_add(): > > unreferenced object 0xffff88817e446440 (size 32): > comm "insmod", pid 4085, jiffies 4296564501 (age 126.272s) > hex dump (first 32 bytes): > 75 62 69 66 73 5f 69 6e 6f 64 65 5f 73 6c 61 62 ubifs_inode_slab > 00 65 44 7e 81 88 ff ff 00 00 00 00 00 00 00 00 .eD~............ > backtrace: > [<000000005b30fbbd>] __kmalloc_node_track_caller+0x4e/0x150 > [<000000002f70da0c>] kstrdup_const+0x4b/0x80 > [<00000000c6712c61>] kobject_set_name_vargs+0x2f/0xb0 > [<00000000b151218e>] kobject_init_and_add+0xb0/0x120 > [<00000000e56a4cf5>] sysfs_slab_add+0x17d/0x220 > [<000000009326fd57>] __kmem_cache_create+0x406/0x590 > [<00000000dde33cff>] kmem_cache_create_usercopy+0x1fc/0x300 > [<00000000fe90cedb>] kmem_cache_create+0x12/0x20 > [<000000007a6531c8>] 0xffffffffa02d802d > [<000000000e3b13c7>] do_one_initcall+0x87/0x2a0 > [<00000000995ecdcf>] do_init_module+0xdf/0x320 > [<000000008821941f>] load_module+0x2f98/0x3330 > [<00000000ef51efa4>] __do_sys_finit_module+0x113/0x1b0 > [<000000009339fbce>] do_syscall_64+0x35/0x80 > [<000000006b7f2033>] entry_SYSCALL_64_after_hwframe+0x46/0xb0 Hi,every one, I found the same problem and it solve this problem with the patch, is there any plan to update the patch and solve it. > > Following the rules stated in the comment for kobject_init_and_add(): > If this function returns an error, kobject_put() must be called to > properly clean up the memory associated with the object. > > kobject_put() is more appropriate for error handling after kobject_init(). > And we can use this function to solve this problem. > > For the cache created early, the related sysfs_slab_add() is called in > slab_sysfs_init(). Skip free these kmem_cache since they are important > for system. Keep them working without sysfs. > > Fixes: 80da026a8e5d ("mm/slub: fix slab double-free in case of duplicate sysfs filename") > Signed-off-by: Liu Shixin > --- > include/linux/slub_def.h | 4 ++-- > mm/slab_common.c | 6 ++---- > mm/slub.c | 21 +++++++++++++++++---- > 3 files changed, 21 insertions(+), 10 deletions(-) > > diff --git a/include/linux/slub_def.h b/include/linux/slub_def.h > index 26d56c4c74d1..90c3e06b77b1 100644 > --- a/include/linux/slub_def.h > +++ b/include/linux/slub_def.h > @@ -144,11 +144,11 @@ struct kmem_cache { > > #ifdef CONFIG_SYSFS > #define SLAB_SUPPORTS_SYSFS > -int sysfs_slab_add(struct kmem_cache *); > +int sysfs_slab_add(struct kmem_cache *, bool); > void sysfs_slab_unlink(struct kmem_cache *); > void sysfs_slab_release(struct kmem_cache *); > #else > -static inline int sysfs_slab_add(struct kmem_cache *s) > +static inline int sysfs_slab_add(struct kmem_cache *s, bool free_slab) > { > return 0; > } > diff --git a/mm/slab_common.c b/mm/slab_common.c > index 55e2cf064dfe..30808a1d1b32 100644 > --- a/mm/slab_common.c > +++ b/mm/slab_common.c > @@ -237,11 +237,9 @@ static struct kmem_cache *create_cache(const char *name, > #ifdef SLAB_SUPPORTS_SYSFS > /* Mutex is not taken during early boot */ > if (slab_state >= FULL) { > - err = sysfs_slab_add(s); > - if (err) { > - slab_kmem_cache_release(s); > + err = sysfs_slab_add(s, true); > + if (err) > return ERR_PTR(err); > - } > debugfs_slab_add(s); > } > #endif > diff --git a/mm/slub.c b/mm/slub.c > index a1ad759753ce..25575bce0c3c 100644 > --- a/mm/slub.c > +++ b/mm/slub.c > @@ -5881,7 +5881,7 @@ static char *create_unique_id(struct kmem_cache *s) > return name; > } > > -int sysfs_slab_add(struct kmem_cache *s) > +int sysfs_slab_add(struct kmem_cache *s, bool free_slab) > { > int err; > const char *name; > @@ -5911,14 +5911,17 @@ int sysfs_slab_add(struct kmem_cache *s) > * for the symlinks. > */ > name = create_unique_id(s); > - if (IS_ERR(name)) > + if (IS_ERR(name)) { > + if (free_slab) > + slab_kmem_cache_release(s); > return PTR_ERR(name); > + } > } > > s->kobj.kset = kset; > err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, "%s", name); > if (err) > - goto out; > + goto out_put_kobj; > > err = sysfs_create_group(&s->kobj, &slab_attr_group); > if (err) > @@ -5934,6 +5937,16 @@ int sysfs_slab_add(struct kmem_cache *s) > return err; > out_del_kobj: > kobject_del(&s->kobj); > +out_put_kobj: > + /* > + * Skip free kmem_cache that create early since they are important > + * for system. Keep them working without sysfs. Only free the name > + * for early allocated kmem_cache. > + */ > + if (free_slab) > + kobject_put(&s->kobj); > + else > + kfree_const(s->kobj.name); > goto out; > } > > @@ -6002,7 +6015,7 @@ static int __init slab_sysfs_init(void) > slab_state = FULL; > > list_for_each_entry(s, &slab_caches, list) { > - err = sysfs_slab_add(s); > + err = sysfs_slab_add(s, false); > if (err) > pr_err("SLUB: Unable to add boot slab %s to sysfs\n", > s->name);