From patchwork Mon Oct 24 07:42:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liu Shixin X-Patchwork-Id: 13016742 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 0B1CCC38A2D for ; Mon, 24 Oct 2022 06:54:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7A2528E0002; Mon, 24 Oct 2022 02:54:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7517D8E0001; Mon, 24 Oct 2022 02:54:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 640428E0002; Mon, 24 Oct 2022 02:54:06 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 520FA8E0001 for ; Mon, 24 Oct 2022 02:54:06 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 1FA9DC0977 for ; Mon, 24 Oct 2022 06:54:06 +0000 (UTC) X-FDA: 80054928492.17.B9BCA47 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by imf17.hostedemail.com (Postfix) with ESMTP id DFFB84001C for ; Mon, 24 Oct 2022 06:54:04 +0000 (UTC) Received: from dggpemm500023.china.huawei.com (unknown [172.30.72.54]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4MwlzV3FVnz15Lxv; Mon, 24 Oct 2022 14:49:10 +0800 (CST) Received: from dggpemm100009.china.huawei.com (7.185.36.113) by dggpemm500023.china.huawei.com (7.185.36.83) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Mon, 24 Oct 2022 14:54:00 +0800 Received: from huawei.com (10.175.113.32) by dggpemm100009.china.huawei.com (7.185.36.113) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Mon, 24 Oct 2022 14:53:59 +0800 From: Liu Shixin To: Christoph Lameter , Pekka Enberg , "David Rientjes" , Joonsoo Kim , "Andrew Morton" , Vlastimil Babka , "Roman Gushchin" , Hyeonggon Yoo <42.hyeyoo@gmail.com> CC: , , Liu Shixin Subject: [PATCH 1/4] mm/slab_common: Move cache_name to create_cache() Date: Mon, 24 Oct 2022 15:42:29 +0800 Message-ID: <20221024074232.151383-2-liushixin2@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221024074232.151383-1-liushixin2@huawei.com> References: <20221024074232.151383-1-liushixin2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.113.32] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemm100009.china.huawei.com (7.185.36.113) X-CFilter-Loop: Reflected ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1666594445; a=rsa-sha256; cv=none; b=Lq5Hj4AqbcuNs0k4Xf5SaNrirD3mympzRtAZRdRngB3X1t5NdjGyyKb8/P/sPpVNLvbWwA K3jgXmG+2QP9gqiKAFjXPlTt89E5m43eP5QfW67C9w1ASS3wCZuS+8sOe+9tc8CkfcbWYx Y9256JI1VFX7N0nlgNZ82sbebdTBWwo= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=none; spf=pass (imf17.hostedemail.com: domain of liushixin2@huawei.com designates 45.249.212.255 as permitted sender) smtp.mailfrom=liushixin2@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1666594445; 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=leyZkFm/H1Ug67EGoS9cgja/SPXbwRKk1/xB4yKJAtU=; b=GPJFX0XkLyOSr+vdHRpYY52LPi+E58AaQ5F0qlzvWNbwMFHq5e/UEC4VymFpC8L2lM4P60 jCmFrwkJA3GJ38+uQQwRQ44wUKUMkzqWbx8TJCpch6tL/blJBwDVhHg+tQG3J76ViikSw/ NzMbnmaxkDBYKspQ/m2uA9qKBGt1p0o= X-Stat-Signature: 989ioga69kd8x38cnw57jiiab6id67z8 X-Rspamd-Queue-Id: DFFB84001C Authentication-Results: imf17.hostedemail.com; dkim=none; spf=pass (imf17.hostedemail.com: domain of liushixin2@huawei.com designates 45.249.212.255 as permitted sender) smtp.mailfrom=liushixin2@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com X-Rspam-User: X-Rspamd-Server: rspam05 X-HE-Tag: 1666594444-336285 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: The string cache_name and its kmem_cache have same life cycle. The latter is allocated in create_cache() so move cache_name to create_cache() too for better error handing. Signed-off-by: Liu Shixin --- mm/slab_common.c | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/mm/slab_common.c b/mm/slab_common.c index 33b1886b06eb..e5f430a17d95 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -209,17 +209,21 @@ static struct kmem_cache *create_cache(const char *name, struct kmem_cache *root_cache) { struct kmem_cache *s; - int err; + const char *cache_name; + int err = -ENOMEM; if (WARN_ON(useroffset + usersize > object_size)) useroffset = usersize = 0; - err = -ENOMEM; s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL); if (!s) - goto out; + return ERR_PTR(err); - s->name = name; + cache_name = kstrdup_const(name, GFP_KERNEL); + if (!cache_name) + goto out_free_cache; + + s->name = cache_name; s->size = s->object_size = object_size; s->align = align; s->ctor = ctor; @@ -228,18 +232,17 @@ static struct kmem_cache *create_cache(const char *name, err = __kmem_cache_create(s, flags); if (err) - goto out_free_cache; + goto out_free_name; s->refcount = 1; list_add(&s->list, &slab_caches); -out: - if (err) - return ERR_PTR(err); return s; +out_free_name: + kfree_const(s->name); out_free_cache: kmem_cache_free(kmem_cache, s); - goto out; + return ERR_PTR(err); } /** @@ -278,7 +281,6 @@ kmem_cache_create_usercopy(const char *name, void (*ctor)(void *)) { struct kmem_cache *s = NULL; - const char *cache_name; int err; #ifdef CONFIG_SLUB_DEBUG @@ -326,19 +328,11 @@ kmem_cache_create_usercopy(const char *name, if (s) goto out_unlock; - cache_name = kstrdup_const(name, GFP_KERNEL); - if (!cache_name) { - err = -ENOMEM; - goto out_unlock; - } - - s = create_cache(cache_name, size, + s = create_cache(name, size, calculate_alignment(flags, align, size), flags, useroffset, usersize, ctor, NULL); - if (IS_ERR(s)) { + if (IS_ERR(s)) err = PTR_ERR(s); - kfree_const(cache_name); - } out_unlock: mutex_unlock(&slab_mutex); From patchwork Mon Oct 24 07:42:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liu Shixin X-Patchwork-Id: 13016744 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 87499FA373D for ; Mon, 24 Oct 2022 06:54:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4D75B8E0001; Mon, 24 Oct 2022 02:54:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 457C78E0006; Mon, 24 Oct 2022 02:54:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1D6168E0001; Mon, 24 Oct 2022 02:54:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id EECF88E0003 for ; Mon, 24 Oct 2022 02:54:06 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id B87E1C0AA4 for ; Mon, 24 Oct 2022 06:54:06 +0000 (UTC) X-FDA: 80054928492.13.4DC9E6E Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf13.hostedemail.com (Postfix) with ESMTP id E7FDC20002 for ; Mon, 24 Oct 2022 06:54:05 +0000 (UTC) Received: from dggpemm500024.china.huawei.com (unknown [172.30.72.56]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4Mwm4t0SWzzHv3D; Mon, 24 Oct 2022 14:53:50 +0800 (CST) Received: from dggpemm100009.china.huawei.com (7.185.36.113) by dggpemm500024.china.huawei.com (7.185.36.203) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Mon, 24 Oct 2022 14:54:01 +0800 Received: from huawei.com (10.175.113.32) by dggpemm100009.china.huawei.com (7.185.36.113) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Mon, 24 Oct 2022 14:54:00 +0800 From: Liu Shixin To: Christoph Lameter , Pekka Enberg , "David Rientjes" , Joonsoo Kim , "Andrew Morton" , Vlastimil Babka , "Roman Gushchin" , Hyeonggon Yoo <42.hyeyoo@gmail.com> CC: , , Liu Shixin Subject: [PATCH 2/4] mm/slub: rename kmem_cache_release to slab_release in slub.c Date: Mon, 24 Oct 2022 15:42:30 +0800 Message-ID: <20221024074232.151383-3-liushixin2@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221024074232.151383-1-liushixin2@huawei.com> References: <20221024074232.151383-1-liushixin2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.113.32] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemm100009.china.huawei.com (7.185.36.113) X-CFilter-Loop: Reflected ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1666594446; 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=WQBkH9lp0J5Wz/9XctF9SQTcwcyfIvX5R5NglwtmtO0=; b=ztAyk1e/HtcB3vE5YnWEi9Ei+oUnSs2yZOnsKIHycDiBT3WmN/SzztEWeZrA3cZ7P0w+OX sxyEirxEgu9u6FJ3rjZPLFoi7tNC8C6kooRbCewgoTm0kJtNzEJtCKMkxLdtXBPbV+K2FQ XZs6PFCn73HTUZ/GJkew3htR/wT7bKw= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=none; spf=pass (imf13.hostedemail.com: domain of liushixin2@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=liushixin2@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1666594446; a=rsa-sha256; cv=none; b=8pduBwJ4jHUcQCw6dKyl1WgAAcyNhsKfVMkA7dL2ypeN1NoeUUkc7wRKQs85qchA3gpygG Y82VIFqX82bm96fNC7EpjIDFnByDGtDrGis9v4Ua2gYNj0tg55etlqT934ydBMrXnDrXKw 6Pj64XQTVtPDMDO8Q0C0A8SDh5fkHKk= X-Stat-Signature: hqyu3zwy4icd4qon1thw1qnssf1c85bg X-Rspam-User: Authentication-Results: imf13.hostedemail.com; dkim=none; spf=pass (imf13.hostedemail.com: domain of liushixin2@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=liushixin2@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: E7FDC20002 X-HE-Tag: 1666594445-963503 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: The function name kmem_cache_release has been reused in slab_common.c and slub.c which is easy to misunderstand. Rename the kmem_cache_release to slab_release in slub.c to distinguish them. Signed-off-by: Liu Shixin --- mm/slub.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index 157527d7101b..ba94eb6fda78 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -5847,7 +5847,7 @@ static ssize_t slab_attr_store(struct kobject *kobj, return attribute->store(s, buf, len); } -static void kmem_cache_release(struct kobject *k) +static void slab_release(struct kobject *k) { slab_kmem_cache_release(to_slab(k)); } @@ -5859,7 +5859,7 @@ static const struct sysfs_ops slab_sysfs_ops = { static struct kobj_type slab_ktype = { .sysfs_ops = &slab_sysfs_ops, - .release = kmem_cache_release, + .release = slab_release, }; static struct kset *slab_kset; From patchwork Mon Oct 24 07:42:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liu Shixin X-Patchwork-Id: 13016746 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 C188EFA3740 for ; Mon, 24 Oct 2022 06:54:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C0F628E0007; Mon, 24 Oct 2022 02:54:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BBEC58E0003; Mon, 24 Oct 2022 02:54:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A5F298E0008; Mon, 24 Oct 2022 02:54:12 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 8F89F8E0007 for ; Mon, 24 Oct 2022 02:54:12 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 5F67B120AF4 for ; Mon, 24 Oct 2022 06:54:12 +0000 (UTC) X-FDA: 80054928744.01.0772F9B Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf25.hostedemail.com (Postfix) with ESMTP id 496B6A000E for ; Mon, 24 Oct 2022 06:54:10 +0000 (UTC) Received: from dggpemm500021.china.huawei.com (unknown [172.30.72.56]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4Mwm513dy3zHv6T; Mon, 24 Oct 2022 14:53:57 +0800 (CST) Received: from dggpemm100009.china.huawei.com (7.185.36.113) by dggpemm500021.china.huawei.com (7.185.36.109) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Mon, 24 Oct 2022 14:54:01 +0800 Received: from huawei.com (10.175.113.32) by dggpemm100009.china.huawei.com (7.185.36.113) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Mon, 24 Oct 2022 14:54:01 +0800 From: Liu Shixin To: Christoph Lameter , Pekka Enberg , "David Rientjes" , Joonsoo Kim , "Andrew Morton" , Vlastimil Babka , "Roman Gushchin" , Hyeonggon Yoo <42.hyeyoo@gmail.com> CC: , , Liu Shixin Subject: [PATCH 3/4] mm/slab_common: Separate sysfs_slab_add() and debugfs_slab_add() from __kmem_cache_create() Date: Mon, 24 Oct 2022 15:42:31 +0800 Message-ID: <20221024074232.151383-4-liushixin2@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221024074232.151383-1-liushixin2@huawei.com> References: <20221024074232.151383-1-liushixin2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.113.32] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemm100009.china.huawei.com (7.185.36.113) X-CFilter-Loop: Reflected ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1666594452; a=rsa-sha256; cv=none; b=g2RC88pJgelXT7lPf3MCgxotpdBIAITo4eFYLFdnTfpbFKBJ85cHx/T4WVD5TNIrSpNTPn tfjSH93slQVxfxfSXFufh3d7eARpSQTkWghTH0KqSsPjFCAHDtFueMmx9vbDpOEicgT+PR f1LqGpkKz2Dmf1xXiRFgNGX+WfILJGo= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf25.hostedemail.com: domain of liushixin2@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=liushixin2@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1666594452; 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=LPKumfy4xPUNGVq+lliJcA29VBfHIbiXWDbPJ5kUApU=; b=xc4ku6z8aeYe+VRIS8E0uE2c074ZFDPnz7ZJOQMfz5Y/sST3gIIiwfOlCAlL8PrCwcKoOh /ibO1Cp14rV7vRlTv1g7GfwbnktKCvR6Zkd0vFDIJrxOTtfOfzf5tlUY+uklRKNKZAhYvA C2/MPTSnutEi8U3fXVvd0EIcDBRr26Q= X-Stat-Signature: ryquj41wcj5au5kktgytsucmp7bafe7n X-Rspamd-Queue-Id: 496B6A000E Authentication-Results: imf25.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf25.hostedemail.com: domain of liushixin2@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=liushixin2@huawei.com X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1666594450-832530 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: Separate sysfs_slab_add() and debugfs_slab_add() from __kmem_cache_create() can help to fix a memory leak about kobject. After this patch, we can fix the memory leak naturally by calling kobject_put() to free kobject and associated kmem_cache when sysfs_slab_add() failed. Besides, after that, we can easy to provide sysfs and debugfs support for other allocators too. Signed-off-by: Liu Shixin --- include/linux/slub_def.h | 11 ++++++++++ mm/slab_common.c | 10 +++++++++ mm/slub.c | 44 +++++++--------------------------------- 3 files changed, 28 insertions(+), 37 deletions(-) diff --git a/include/linux/slub_def.h b/include/linux/slub_def.h index f9c68a9dac04..26d56c4c74d1 100644 --- a/include/linux/slub_def.h +++ b/include/linux/slub_def.h @@ -144,9 +144,14 @@ struct kmem_cache { #ifdef CONFIG_SYSFS #define SLAB_SUPPORTS_SYSFS +int sysfs_slab_add(struct kmem_cache *); 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) +{ + return 0; +} static inline void sysfs_slab_unlink(struct kmem_cache *s) { } @@ -155,6 +160,12 @@ static inline void sysfs_slab_release(struct kmem_cache *s) } #endif +#if defined(CONFIG_DEBUG_FS) && defined(CONFIG_SLUB_DEBUG) +void debugfs_slab_add(struct kmem_cache *); +#else +static inline void debugfs_slab_add(struct kmem_cache *s) { } +#endif + void *fixup_red_left(struct kmem_cache *s, void *p); static inline void *nearest_obj(struct kmem_cache *cache, const struct slab *slab, diff --git a/mm/slab_common.c b/mm/slab_common.c index e5f430a17d95..f146dea3f9de 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -234,6 +234,16 @@ static struct kmem_cache *create_cache(const char *name, if (err) goto out_free_name; + /* Mutex is not taken during early boot */ + if (slab_state >= FULL) { + err = sysfs_slab_add(s); + if (err) { + slab_kmem_cache_release(s); + return ERR_PTR(err); + } + debugfs_slab_add(s); + } + s->refcount = 1; list_add(&s->list, &slab_caches); return s; diff --git a/mm/slub.c b/mm/slub.c index ba94eb6fda78..a1ad759753ce 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -299,20 +299,12 @@ struct track { enum track_item { TRACK_ALLOC, TRACK_FREE }; #ifdef CONFIG_SYSFS -static int sysfs_slab_add(struct kmem_cache *); static int sysfs_slab_alias(struct kmem_cache *, const char *); #else -static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; } static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p) { return 0; } #endif -#if defined(CONFIG_DEBUG_FS) && defined(CONFIG_SLUB_DEBUG) -static void debugfs_slab_add(struct kmem_cache *); -#else -static inline void debugfs_slab_add(struct kmem_cache *s) { } -#endif - static inline void stat(const struct kmem_cache *s, enum stat_item si) { #ifdef CONFIG_SLUB_STATS @@ -4297,7 +4289,7 @@ static int calculate_sizes(struct kmem_cache *s) return !!oo_objects(s->oo); } -static int kmem_cache_open(struct kmem_cache *s, slab_flags_t flags) +int __kmem_cache_create(struct kmem_cache *s, slab_flags_t flags) { s->flags = kmem_cache_flags(s->size, flags, s->name); #ifdef CONFIG_SLAB_FREELIST_HARDENED @@ -4900,30 +4892,6 @@ __kmem_cache_alias(const char *name, unsigned int size, unsigned int align, return s; } -int __kmem_cache_create(struct kmem_cache *s, slab_flags_t flags) -{ - int err; - - err = kmem_cache_open(s, flags); - if (err) - return err; - - /* Mutex is not taken during early boot */ - if (slab_state <= UP) - return 0; - - err = sysfs_slab_add(s); - if (err) { - __kmem_cache_release(s); - return err; - } - - if (s->flags & SLAB_STORE_USER) - debugfs_slab_add(s); - - return 0; -} - #ifdef CONFIG_SYSFS static int count_inuse(struct slab *slab) { @@ -5913,7 +5881,7 @@ static char *create_unique_id(struct kmem_cache *s) return name; } -static int sysfs_slab_add(struct kmem_cache *s) +int sysfs_slab_add(struct kmem_cache *s) { int err; const char *name; @@ -6236,10 +6204,13 @@ static const struct file_operations slab_debugfs_fops = { .release = slab_debug_trace_release, }; -static void debugfs_slab_add(struct kmem_cache *s) +void debugfs_slab_add(struct kmem_cache *s) { struct dentry *slab_cache_dir; + if (!(s->flags & SLAB_STORE_USER)) + return; + if (unlikely(!slab_debugfs_root)) return; @@ -6264,8 +6235,7 @@ static int __init slab_debugfs_init(void) slab_debugfs_root = debugfs_create_dir("slab", NULL); list_for_each_entry(s, &slab_caches, list) - if (s->flags & SLAB_STORE_USER) - debugfs_slab_add(s); + debugfs_slab_add(s); return 0; From patchwork Mon Oct 24 07:42:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liu Shixin X-Patchwork-Id: 13016745 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 074A7C38A2D for ; Mon, 24 Oct 2022 06:54:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8DA1E8E0006; Mon, 24 Oct 2022 02:54:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 88A0F8E0003; Mon, 24 Oct 2022 02:54:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 752F68E0006; Mon, 24 Oct 2022 02:54:12 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 64C948E0003 for ; Mon, 24 Oct 2022 02:54:12 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 3DB09120AB1 for ; Mon, 24 Oct 2022 06:54:12 +0000 (UTC) X-FDA: 80054928744.01.062F594 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf21.hostedemail.com (Postfix) with ESMTP id DEDAF1C0006 for ; Mon, 24 Oct 2022 06:54:09 +0000 (UTC) Received: from dggpemm500020.china.huawei.com (unknown [172.30.72.53]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4Mwm1B6HgJzpVrh; Mon, 24 Oct 2022 14:50:38 +0800 (CST) Received: from dggpemm100009.china.huawei.com (7.185.36.113) by dggpemm500020.china.huawei.com (7.185.36.49) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Mon, 24 Oct 2022 14:54:02 +0800 Received: from huawei.com (10.175.113.32) by dggpemm100009.china.huawei.com (7.185.36.113) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Mon, 24 Oct 2022 14:54:01 +0800 From: Liu Shixin To: Christoph Lameter , Pekka Enberg , "David Rientjes" , Joonsoo Kim , "Andrew Morton" , Vlastimil Babka , "Roman Gushchin" , Hyeonggon Yoo <42.hyeyoo@gmail.com> CC: , , Liu Shixin Subject: [PATCH 4/4] mm/slab_common: Fix memory leak of kobj->name in sysfs_slab_add() Date: Mon, 24 Oct 2022 15:42:32 +0800 Message-ID: <20221024074232.151383-5-liushixin2@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221024074232.151383-1-liushixin2@huawei.com> References: <20221024074232.151383-1-liushixin2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.113.32] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemm100009.china.huawei.com (7.185.36.113) X-CFilter-Loop: Reflected ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=none; spf=pass (imf21.hostedemail.com: domain of liushixin2@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=liushixin2@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1666594450; a=rsa-sha256; cv=none; b=Guj84LXv8u/VsNRrPPL+gAZE8VoRzuy84JHfJdjjKR0Vzze7fR8BmnxI18uBB3AIxM58+t z0KYYknUDl5JS5tr23kAymuztSkEHyS8DeVVrOZJ8GSvzD8tEMAnCDrzIITS29oNXDkMQQ IjL8EgWJZEDfhfkBAI46ea0iWtJxqUM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1666594450; 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=EocatO6F99HY0yfMHcZySow4EF6M+vrS3xPsFF4oz/A=; b=42Zbo8Gr3oTEUIQPAaz/xhp2kZauJyaR36bk93Djrt5IvIV2crTzJWi71mhwxYvNmqf9Pw 5iqZfcdxxC92sySQm5d+RWZCPI0q1N4m6/xzYnwFdYDFnUn87Lcnf68oWDhHVawv+oUJ1F +4osfz0Wgm3QKTxeQ+3v8c1EP1fVec0= X-Stat-Signature: o6tkdnkmogazmjo7zeo4z6qoqo7smmbz X-Rspamd-Queue-Id: DEDAF1C0006 X-Rspam-User: Authentication-Results: imf21.hostedemail.com; dkim=none; spf=pass (imf21.hostedemail.com: domain of liushixin2@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=liushixin2@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com X-Rspamd-Server: rspam06 X-HE-Tag: 1666594449-39882 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: 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 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. Fixes: 80da026a8e5d ("mm/slub: fix slab double-free in case of duplicate sysfs filename") Signed-off-by: Liu Shixin --- mm/slab_common.c | 4 +--- mm/slub.c | 8 ++++++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/mm/slab_common.c b/mm/slab_common.c index f146dea3f9de..e60f873c1aaf 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -237,10 +237,8 @@ static struct kmem_cache *create_cache(const char *name, /* Mutex is not taken during early boot */ if (slab_state >= FULL) { err = sysfs_slab_add(s); - if (err) { - slab_kmem_cache_release(s); + if (err) return ERR_PTR(err); - } debugfs_slab_add(s); } diff --git a/mm/slub.c b/mm/slub.c index a1ad759753ce..f8883bc642b8 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -5911,14 +5911,16 @@ int sysfs_slab_add(struct kmem_cache *s) * for the symlinks. */ name = create_unique_id(s); - if (IS_ERR(name)) + if (IS_ERR(name)) { + 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 +5936,8 @@ int sysfs_slab_add(struct kmem_cache *s) return err; out_del_kobj: kobject_del(&s->kobj); +out_put_kobj: + kobject_put(&s->kobj); goto out; }