From patchwork Sat Nov 12 11:45:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liu Shixin X-Patchwork-Id: 13041180 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 519E9C43217 for ; Sat, 12 Nov 2022 10:58:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D92A38E0003; Sat, 12 Nov 2022 05:58:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D44A28E0002; Sat, 12 Nov 2022 05:58:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BBCD88E0003; Sat, 12 Nov 2022 05:58:29 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id AEB468E0002 for ; Sat, 12 Nov 2022 05:58:29 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 785551A0E1C for ; Sat, 12 Nov 2022 10:58:29 +0000 (UTC) X-FDA: 80124491538.19.776059F Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf22.hostedemail.com (Postfix) with ESMTP id B9FDBC0002 for ; Sat, 12 Nov 2022 10:58:28 +0000 (UTC) Received: from dggpemm500024.china.huawei.com (unknown [172.30.72.53]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4N8Xbh2V3wzHvJN; Sat, 12 Nov 2022 18:57:52 +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; Sat, 12 Nov 2022 18:58:19 +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; Sat, 12 Nov 2022 18:58:18 +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 v4 0/3] Refactor __kmem_cache_create() and fix memory leak Date: Sat, 12 Nov 2022 19:45:59 +0800 Message-ID: <20221112114602.1268989-1-liushixin2@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Originating-IP: [10.175.113.32] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm100009.china.huawei.com (7.185.36.113) X-CFilter-Loop: Reflected ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1668250709; a=rsa-sha256; cv=none; b=eK6Ch/P53eHsg+UTkhcOeS5/x4uVp9zxkAtbjBZmeXiZTJfrub39UNB5N0GLmCt0EF0Itt K1lhrVCqlR54xEt8LPBTXJBhIaDGHs9cVFEnJ4q0nchiDtMP3BDDirmVHDmyuyoupPBbgF UxfIK1hl/Q4tQ589/SkKxdzcQlRdOxY= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf22.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=1668250709; 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: references; bh=RpNAtI9nax40A72acJzj9roGOnyjQdlvfMi+9uIOGw4=; b=iZp2KRpvi+0aet5bkfV6IA4lwANNxrs61H32HDyG6XMW7uT07qv3TYE9/pxH0DcrSRLVBc 0g5wLYHsrPc3Y2PKbk4KcI5cV2p09BrIWkjE83YbQfIlLQAHffC3ONIOZHsh4XA6OYd9tZ vDZcwHaUgsVY2f7DQHe5jqJ05jmXFo4= X-Rspam-User: X-Stat-Signature: 9rdqgbhup89faq3ha5hpg1qh5xbeeuwt X-Rspamd-Queue-Id: B9FDBC0002 Authentication-Results: imf22.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf22.hostedemail.com: domain of liushixin2@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=liushixin2@huawei.com X-Rspamd-Server: rspam03 X-HE-Tag: 1668250708-112049 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: I found a memory leak of kobj->name in sysfs_slab_add() which is introduced by 80da026a8e5d ("mm/slub: fix slab double-free in case of duplicate sysfs filename"). 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. We should use kobject_put() to free kobject. But we can't simply add kobject_put() since it will free kmem_cache too. If we use kobject_put(), we need to skip other release functions. In this series, We refactor the code to separate sysfs_slab_add() and debugfs_slab_add() from __kmem_cache_create(), and then use kobject_put() to free kobject in sysfs_slab_add(). This can fix the memory leak of kobject->name. v1->v2: Fix build error reported by kernel test robot . v2->v3: Don't free kmem_cache that create early. v3->v4: Fix error in kfree_const of kobject's name. Liu Shixin (3): mm/slab_common: Move cache_name to create_cache() mm/slub: Refactor __kmem_cache_create() mm/slub: Fix memory leak of kobj->name in sysfs_slab_add() include/linux/slub_def.h | 11 +++++++ mm/slab_common.c | 44 +++++++++++++++------------- mm/slub.c | 63 +++++++++++++++------------------------- 3 files changed, 58 insertions(+), 60 deletions(-) --- 2.25.1