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 A381DC4332F for ; Thu, 3 Nov 2022 13:24:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1AD996B0071; Thu, 3 Nov 2022 09:24:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 15DA68E0001; Thu, 3 Nov 2022 09:24:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 024A56B0073; Thu, 3 Nov 2022 09:24:03 -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 E3E446B0071 for ; Thu, 3 Nov 2022 09:24:03 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 55143160F96 for ; Thu, 3 Nov 2022 13:24:03 +0000 (UTC) X-FDA: 80092199166.06.E4CBA02 Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.169]) by imf19.hostedemail.com (Postfix) with ESMTP id D7F401A0004 for ; Thu, 3 Nov 2022 13:24:02 +0000 (UTC) Received: by mail-pg1-f169.google.com with SMTP id v3so1649022pgh.4 for ; Thu, 03 Nov 2022 06:24:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=XrHkiVdh+qB5p2Sw2z7czeCm3PU3sfVYaV65IQbO0PU=; b=GckebTsM5Af16DqURlc/Nk6EqDA1yWwlUEZUnmFCufGlYsJq7EN5hk8iaWXuyYu87L a4pY5j+sghuYrvVbi+AE5Cey+7OBhp3ltyN0dzzgXBAe5uCkMOiuJ6/XCaT3u2McLQN9 zM49chGJm65xj1uyXFPxzKne2YcxbEb8qjpgmvthOf5EmFQwJU8HHKYUTrR8P20+wQUw FzbPw024UiRRdQd4R7yZEkRkCR3RTwI8NINJS2JgPYhoqObGl89eWapH0vKZPMX+fTNz v7qpnSZVc2L/Z1JZj7TrY8bPfNz82WQJgf+BJZgPrlwVhIcbEVC4mGWoUVxLaCOGMARK txFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=XrHkiVdh+qB5p2Sw2z7czeCm3PU3sfVYaV65IQbO0PU=; b=Xn81m8GlXfwZJx2pGFEGTLFi+nNi68NLsMkbS0QCZmMGXixHu29BIaMuw653EfJ2Uv f7KqpgmARQNN+A/yH49qEnOo/tzYJi/EKW9zfv0YojEO9R37oSLdz9d0oa5YW2qGtxEY 5erhlPhlY01Y7d1k5WBSeytN1qIO+ht3rGBZoTBxjQ0Q8gBk3yzrLCkap+KMUVDDZT+a HRi2t6Hkih3ML7WkxwwqBatOQFwkee7moJY9oR+lFzSUG3bVsoJG4QgbLJqBVAGh3982 TRWE1TFxKr/1h9WgfmqP8hMCrNVf1hIJ/Gu4INQsmoq5uTl0wZP4q7OIOBTopH3BY0Ef JiMw== X-Gm-Message-State: ACrzQf0wPvN2kCTaMjQoAeXHCtM7v2pJMT8WZ48btyqNYsmb1XJdW7Tz kolHE3DzE3T+OE6tihFa+0I= X-Google-Smtp-Source: AMsMyM79JtxqP7QPxDsd1kRRmNFm8mQSnZb7l9ngiFkoEpvsK/cLZZXbTypytMz3gUgO5X6xJqdqNQ== X-Received: by 2002:a62:a503:0:b0:56b:cb10:2d87 with SMTP id v3-20020a62a503000000b0056bcb102d87mr30453796pfm.79.1667481841750; Thu, 03 Nov 2022 06:24:01 -0700 (PDT) Received: from hyeyoo ([114.29.91.56]) by smtp.gmail.com with ESMTPSA id nm1-20020a17090b19c100b00209a12b3879sm949113pjb.37.2022.11.03.06.23.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Nov 2022 06:24:00 -0700 (PDT) Date: Thu, 3 Nov 2022 22:23:52 +0900 From: Hyeonggon Yoo <42.hyeyoo@gmail.com> To: Liu Shixin Cc: Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Vlastimil Babka , Roman Gushchin , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 0/3] Refactor __kmem_cache_create() and fix memory leak Message-ID: References: <20221031134747.3049593-1-liushixin2@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1667481842; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=XrHkiVdh+qB5p2Sw2z7czeCm3PU3sfVYaV65IQbO0PU=; b=NhKG/ok23BjFkgqXJiTZJz0JuYQ8sRVrXExVwMe8AHsnKmd+QE0wOrm0HXJAsNrwR1vhOT rxze9T1p/lQXzNd7UBDWc3yLbjOkpQ1Vy3EjfivFgzN6ErO2y44cIKDU3JxIkgDY2EYQII b5Zp3OFOwrH5LKa8nkzbZthpbd0Tp+Q= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=GckebTsM; spf=pass (imf19.hostedemail.com: domain of 42.hyeyoo@gmail.com designates 209.85.215.169 as permitted sender) smtp.mailfrom=42.hyeyoo@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1667481842; a=rsa-sha256; cv=none; b=rCoSZgv0mCN5YCjzN3lsLU/5bfT0Ud4KURJEOirFo3Lme087McyTPM5aLfF2e/6ByzKRTL YuRrWOAiFMxW+/Ua7IVokCjeclGmTqBQSkNswEMUrF49xJSDAJEq5lqwBCD/wwXscjgcxI IWL/SlHdGiu1rbeTX5tqgMJLKMvV0hs= X-Stat-Signature: efcm7aku6o7hrdmeg7xm1aibcj7c6o9a X-Rspamd-Queue-Id: D7F401A0004 X-Rspamd-Server: rspam06 X-Rspam-User: Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=GckebTsM; spf=pass (imf19.hostedemail.com: domain of 42.hyeyoo@gmail.com designates 209.85.215.169 as permitted sender) smtp.mailfrom=42.hyeyoo@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-HE-Tag: 1667481842-898409 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 Wed, Nov 02, 2022 at 04:53:08PM +0800, Liu Shixin wrote: > 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. You're welcome. > I prefer to panic directly, just as create_boot_cache() does. IMHO that should be nothing serious. but let's hear maintainers' opinion. > Of couse, if you want the system to continue booting, I think it's possible to distinguish them > by slab_state. I'm afraid to make it more complex :( > 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 > >> > -- Thanks, Hyeonggon