Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp3690335rwb; Sun, 20 Nov 2022 20:03:36 -0800 (PST) X-Google-Smtp-Source: AA0mqf5qz7NgOYkSw3lWNiBPOudEBnzZMjuiE0EwQ9eYpV9MdkP0/wjm5C+h/IrN92kh3jAtGBGU X-Received: by 2002:a17:906:4a8a:b0:7ad:bc84:2f23 with SMTP id x10-20020a1709064a8a00b007adbc842f23mr14484927eju.262.1669003416728; Sun, 20 Nov 2022 20:03:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669003416; cv=none; d=google.com; s=arc-20160816; b=Duf72j192xPPk6pLZZE4rKiRanRjcFc326LP/d1ojRDO/KLHX/rqOSkWPtPrkhlLVq ON1PEpUfMAXuv48ssBWx7M3frgfcQ9hij7V7OQr1FtsESCu/8LFkmQxoDgOcv5DLbXmi ZWFgYYGnWu5B/HngAZMHQ7VHmSM6VIaAt6O5loiqNiWXCfzrRoCcdg+xgoyqQyoXrnIO xOpMbu+XNTAD3SfWhh0RPMUEmt5MBChQBnzDJbSkjZW/YQ+MFh+NZ//S1iRsBQfHCH9c m6PiL+1xfA1TDwoD7nVJ/xQt0YVyy8M+OKIFSd1oeFOeQnLF5wqZYBhHzkzmXEJrBWpR Z3YQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :to:subject; bh=F0v9sQw48WH0dOVWkTScKJAwKT+6aQneqKKxSforEJg=; b=WAeL6uQ3SfPf8M1wrFEw4HjsJFFOtgsgZHEEOzi280jzp/q/bMGCwc7xKpcFmPbcdg P2PWM7m/IIAC7bYz1ymiXxJ/P1vxxC6wnTFp+8CATlJcmmUBxKcvd7oaEyC7puzeJJf8 D9Gq0rT2kz90Hfdbf8wDMDKiiJsvagFAvzE7uNNFdR+M9JM3YBk0oovDNMNmdRtAOglP G7CeShVbIsEYQWVu77GC5TXjTq8FdQei3ZsyJR/07qFD3olSp/lpbTYBe9SEpYbuyWL0 x6PGxsdd5qT0QKTnvTGMgaWzoTLiddisIPN86KLYzUTzW38Ck/V8BZ3CTBHSXB88ifdC s1Pw== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id xb1-20020a170907070100b0078d44c5da0esi8193036ejb.667.2022.11.20.20.03.14; Sun, 20 Nov 2022 20:03:36 -0800 (PST) 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; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229533AbiKUDuD (ORCPT + 90 others); Sun, 20 Nov 2022 22:50:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41234 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229446AbiKUDuC (ORCPT ); Sun, 20 Nov 2022 22:50:02 -0500 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 183D51EEF0; Sun, 20 Nov 2022 19:50:00 -0800 (PST) Received: from dggpemm500020.china.huawei.com (unknown [172.30.72.53]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4NFtgF5wHNzmVxP; Mon, 21 Nov 2022 11:49:29 +0800 (CST) Received: from dggpemm500006.china.huawei.com (7.185.36.236) 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, 21 Nov 2022 11:49:58 +0800 Received: from [10.174.178.55] (10.174.178.55) by dggpemm500006.china.huawei.com (7.185.36.236) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Mon, 21 Nov 2022 11:49:57 +0800 Subject: Re: [PATCH] btrfs: fix a resource leak in btrfs_init_sysfs() To: Qu Wenruo , Chris Mason , Josef Bacik , David Sterba , , References: <20221119064348.1743-1-thunder.leizhen@huawei.com> From: "Leizhen (ThunderTown)" Message-ID: Date: Mon, 21 Nov 2022 11:49:56 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 8bit X-Originating-IP: [10.174.178.55] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500006.china.huawei.com (7.185.36.236) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS autolearn=ham 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 2022/11/20 11:29, Qu Wenruo wrote: > > > On 2022/11/20 11:11, Qu Wenruo wrote: >> >> >> On 2022/11/19 14:53, Qu Wenruo wrote: >>> >>> >>> On 2022/11/19 14:43, Zhen Lei wrote: >>>> When btrfs_debug_feature_attr_group fails to be created, >>>> btrfs_feature_attr_group is not removed. >>>> >>>> Signed-off-by: Zhen Lei >>> >>> Reviewed-by: Qu Wenruo >> >> Wait for a minute, should we call sysfs_unmerge_group() first before calling sysfs_remove_group()? >> >> As the sysfs_remove_group() will only remove the btrfs_feature_attr_group, and kset_unregister() will only free btrfs_kset, without removing the added btrfs_static_feature_attr_group. >> >> I haven't yet find a function that will remove all children attrs in just one go, or did I miss something? > > Oh, indeed I missed something. > > The following call chain would properly handle every child of a kobj: > > kset_unregister() > |- kobject_del() >    |- __kobject_del() >       |- sysfs_remove_dir() >          |- kernfs_remove() >             |- __kernfs_remove() >                |- do { kernfs_leftmost_descendant(); } while (pos != kn) > > The final do {} while () loop will unlink each child kernel_node and free it. > > This means, as long as we call kset_unregister(), we should be able to free every child of it. OK, thanks. > > > So with or without this patch, the error handling should be fine. > > Although personally speaking, I'd prefer a more explicit cleanup (with extra sysfs_unmerge_group() call) as going through the above call chain is not that straightforward. Yes, with sysfs_unmerge_group(), the code logic is clearer. > > Thanks, > Qu > >> >> Thanks, >> Qu >>> >>> Thanks, >>> Qu >>>> --- >>>>   fs/btrfs/sysfs.c | 2 +- >>>>   1 file changed, 1 insertion(+), 1 deletion(-) >>>> >>>> diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c >>>> index 699b54b3acaae0b..947125f2ceaaf96 100644 >>>> --- a/fs/btrfs/sysfs.c >>>> +++ b/fs/btrfs/sysfs.c >>>> @@ -2322,7 +2322,7 @@ int __init btrfs_init_sysfs(void) >>>>   #ifdef CONFIG_BTRFS_DEBUG >>>>       ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group); >>>>       if (ret) >>>> -        goto out2; >>>> +        goto out_remove_group; >>>>   #endif >>>>       return 0; > . > -- Regards, Zhen Lei