Received: by 2002:a05:622a:1442:b0:3a5:28ea:c4b9 with SMTP id v2csp553959qtx; Thu, 3 Nov 2022 06:48:46 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5Xmk3URlTsJpHXThzyvs8rliCu0FtYxPb2MXF08v1MgmyYu0ut5pRddknR39gap/iWm+Jx X-Received: by 2002:a05:6402:194d:b0:461:8982:52b8 with SMTP id f13-20020a056402194d00b00461898252b8mr30056369edz.49.1667483326652; Thu, 03 Nov 2022 06:48:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667483326; cv=none; d=google.com; s=arc-20160816; b=dAz7Ezp6fb5XytPRsoSbSmAfJVmUBl/nFBNXWx5nOIhTxt+0eK82cP286Bi3Ov+Kf6 O+QU6utg8qm1XSRAaCviXtXxX/H7WtUQz2ndfQfXLnU1tO0QXvOFZtO/gbdLT+gY+CrX 2EIq7ND1gqSSJ4PhUfQ7Sg7cSEE4/z7ADTHTN9QCXgOFnljBaG2SIU/L3Fs863cdWfV0 bPWwX6oYD17/uNRmL7taytj0ljDThexS3c0sWcnCXOLdhkeYfGaPL/zIM+NanGr6S2Ll L3RhIMpz9pD0HaI9hA6JuLq18nfUftKil23mSUtTWeagh0dh50Wqgrn89tVSJXj9b47H 3oRQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=XrHkiVdh+qB5p2Sw2z7czeCm3PU3sfVYaV65IQbO0PU=; b=BE/jIwYlreMDC3tL6mLpF3/GzMAT9w5FdUxkcRqPe6CLMrZhW1cIsg/uExP0zrdeG1 23p8KY9TfY9/bAvTqxPv6hjcKasSo4+f8IsQEm5EJf8VwjxVmspVpx+vqXdMPs5EV8Ta UWWW2/mYir9i4rhPALoFRGGklUJSCSsVUzq4MS1TGBn/PLmG6ZlIuIEYkdOts5DxBFfE d3wDfJtnfnnb+s9n3ac3LCUYxuKcP5BpYRwFQPg4+Nz6qb81dP5T4tbc9I3M0jArWzKi Kvt7/6i7cRZavT4IDK220T6FpS2BOooAtVrCtElwV0dHNHiMWnp8HWG09vcgSfM8ltP6 BXwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=GckebTsM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g12-20020a170906538c00b0078d288ddfc9si1011272ejo.143.2022.11.03.06.48.21; Thu, 03 Nov 2022 06:48:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=GckebTsM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231497AbiKCNYF (ORCPT + 97 others); Thu, 3 Nov 2022 09:24:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60258 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231470AbiKCNYD (ORCPT ); Thu, 3 Nov 2022 09:24:03 -0400 Received: from mail-pf1-x433.google.com (mail-pf1-x433.google.com [IPv6:2607:f8b0:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3C3C910B9 for ; Thu, 3 Nov 2022 06:24:02 -0700 (PDT) Received: by mail-pf1-x433.google.com with SMTP id m6so1622367pfb.0 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=BM8w6jQ7VCumG5wq7X5CCC3BCgjY/xyIht8qMbGy2bnvz8TH5dYl9NS1rf6q5KQT4N UgXVvG5XvUWSbWn1SOOWMNODAAUl52K1/XdZg01vsrUI6pzDnVrjvT0w7cgq7t3ZvLyB rfLLgvcMSGx9BTUSAlsY8DiAdDrZJxSmMkQ1JpAz8Hq2uEJnKFuDNq98KVhlkxuvuT1F FKKEwerVuzIKGQmw0YPC6P1vrNStj2h+a0aXz5tjUM9/zJpEOGcGSp5F2M9HWzhoWvxL kxTOdiOKaELFoEueW4bxI4I1ZHf/JHeVJfcz3FBqjuqETuwjvisSvBJ2I4dXBunmL+uc 6Kjg== X-Gm-Message-State: ACrzQf2xfK7iETnAHwC10MGpkk6rqPkF0Y5bMdcnYN3sJ4p3slBNxHjp sytGsbNpARkNMfHykm9r1RY= 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: X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HK_RANDOM_ENVFROM, HK_RANDOM_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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