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 DDA1FC4332F for ; Wed, 2 Nov 2022 08:53:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 578098E0002; Wed, 2 Nov 2022 04:53:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 528548E0001; Wed, 2 Nov 2022 04:53:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3F0098E0002; Wed, 2 Nov 2022 04:53:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 2C3C88E0001 for ; Wed, 2 Nov 2022 04:53:17 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id EB6AA805BD for ; Wed, 2 Nov 2022 08:53:16 +0000 (UTC) X-FDA: 80087887992.15.F0697CD Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by imf06.hostedemail.com (Postfix) with ESMTP id BF81F180004 for ; Wed, 2 Nov 2022 08:53:15 +0000 (UTC) Received: from dggpemm500023.china.huawei.com (unknown [172.30.72.56]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4N2LF32w9qzJnLD; Wed, 2 Nov 2022 16:50:15 +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; Wed, 2 Nov 2022 16:53:09 +0800 Received: from [10.174.179.24] (10.174.179.24) 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; Wed, 2 Nov 2022 16:53:08 +0800 Subject: Re: [PATCH v2 0/3] Refactor __kmem_cache_create() and fix memory leak To: Hyeonggon Yoo <42.hyeyoo@gmail.com> References: <20221031134747.3049593-1-liushixin2@huawei.com> CC: Christoph Lameter , Pekka Enberg , "David Rientjes" , Joonsoo Kim , "Andrew Morton" , Vlastimil Babka , "Roman Gushchin" , , From: Liu Shixin Message-ID: Date: Wed, 2 Nov 2022 16:53:08 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.7.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="windows-1252" Content-Transfer-Encoding: 7bit X-Originating-IP: [10.174.179.24] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpemm100009.china.huawei.com (7.185.36.113) X-CFilter-Loop: Reflected ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1667379196; a=rsa-sha256; cv=none; b=RAecLBcjnkLpFAGOt84TdMqiI5Md4NrQ/8Sx+JlMV2xOWRBeUoLEPJMW83YdailxCo3YoX AQY0QcSRu/zba/Prwf/QhkU6QGicssOzw5ITyr1UkSrcG3gC+QP9Xho4veHQms9tew+uIa XKg5z3X4Iz8Dq+QU4QLnW/CzOErGcQ0= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf06.hostedemail.com: domain of liushixin2@huawei.com designates 45.249.212.189 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=1667379196; 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=RPhpO7Yzwi5vHHJmOK+iBoIINHJ9ArjLUz78Nyjp2gI=; b=DlFgt44Lpk+ZUD7OSIrwjMP9JsBsXSHRkvjqqYOh/EzH7o1beThGqhHDrZuib+lwy02X3X 9R9Cz9bOViMZ88c0KmhX8UPci0VuJ4awZhnqNU4MUjSZoL41Y1aF1+PEB+H6vs1Fbb+SYn MRnh0KqjNoa9bTnPiAzcK32A/5t9YHA= X-Stat-Signature: 6gbx1fyi9tzfme34uhefx1pp9j6a9fu8 X-Rspamd-Queue-Id: BF81F180004 Authentication-Results: imf06.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf06.hostedemail.com: domain of liushixin2@huawei.com designates 45.249.212.189 as permitted sender) smtp.mailfrom=liushixin2@huawei.com X-Rspam-User: X-Rspamd-Server: rspam07 X-HE-Tag: 1667379195-606881 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: On 2022/11/2 15:46, Hyeonggon Yoo wrote: > On Mon, Oct 31, 2022 at 09:47:44PM +0800, Liu Shixin wrote: >> 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(): > Thank you for reporting this! Indeed it seems tried to fix double free but > introduced a leak. > >> 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 what to do if a cache is created early and later sysfs_slab_add() failed? > (Which is unlikely on normal condition) > > With this series it introduces use-after-free if sysfs_slab_add() in > slab_sysfs_init() failed. Should we just call BUG() or something like that? Thanks for your discovery, what I missed. I prefer to panic directly, just as create_boot_cache() does. Of couse, if you want the system to continue booting, I think it's possible to distinguish them by slab_state. Looking forward to your advice. Thanks, > >> 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 . >> >> 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 | 52 ++++++++++------------------------------ >> 3 files changed, 48 insertions(+), 59 deletions(-) >> >> -- >> 2.25.1 >>