Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp7721340rwi; Mon, 24 Oct 2022 19:36:28 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4uX8o6nsXcdG0PK7fjKnVwCsMMqGIZVjVlGLvqsxr119Idi08p2Uc7IkNUHFaB9rBbdRHJ X-Received: by 2002:a17:907:9807:b0:797:a5ba:1327 with SMTP id ji7-20020a170907980700b00797a5ba1327mr22972964ejc.274.1666665388134; Mon, 24 Oct 2022 19:36:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666665388; cv=none; d=google.com; s=arc-20160816; b=UKxr7nH2lRaxmeKBAqmaqibZRiV4lmwFyCkJNl5nVGAq0RyDbZwLaIln21PHU1VRFp 7hUJ2XzRyBL5ACRuo9VCUVbMYU1BgBpVNBbCZUj4omD5KM0F1sUfoA8P2SmYeIw72xBU LgBjBq8KUVkM+rcuGIcD8iSy2ls7f2N1UYlvpTznL58417ggNd/v5cRLzB8kxNql+Gnc 1FPH5ZNo/Kjxk3RPBhHUvPNjGJYZFuwibqcLHbEf4uVXirgyTao0sbz0DdAesCZOZ2z8 d8e7dQqwkGKIh/XjoHrQodxp2UfQHNbjuqWfB1OQLI9czjcqZrb5jiru3v7JY7r65tFe Om8Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-language:content-transfer-encoding :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=mBUKucCUqprBC2v/NT4b0TWz2/krmN41tzFEgb3bKRM=; b=hn3T9aTLJZkGCyydZ7XlcQ4LOUr/tJ4VuK+llrllvv4BCSqQUhOH5YrN9F/bGYrhDp VgKZJpSjw2S7P3MoYOuxxOC0GX/HZ8/be13hsRcQGuI4wWbiBfMEwIgJ5KX8m005es9A tDis30ePHftB6jc6n3rGFh++mUbb7qV4BVZewd7AoBK68y6jYLVsqvvI7RAHLzroBGCN n/HRdJKTUFz8ZO7lANJA047z0iVwlNC9Km7ZvvO+7F5l1vFC/d/5dXY4Rc1ee2ES9/Nw SFoCo3YsvvN+Ne5bZfEOCcukCoroi7bEpK3hBJctusajgoYjDUgsDXnCKjoo5J5WUfOg Zuww== 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 hb19-20020a170907161300b0078def5c29e3si1815794ejc.596.2022.10.24.19.36.02; Mon, 24 Oct 2022 19:36:28 -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; 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 S230490AbiJYCQr (ORCPT + 99 others); Mon, 24 Oct 2022 22:16:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55264 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230147AbiJYCQk (ORCPT ); Mon, 24 Oct 2022 22:16:40 -0400 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2424ABCD for ; Mon, 24 Oct 2022 19:16:37 -0700 (PDT) Received: from dggpemm500024.china.huawei.com (unknown [172.30.72.56]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4MxFmv6qXjz15M3T; Tue, 25 Oct 2022 10:11:43 +0800 (CST) Received: from dggpemm500007.china.huawei.com (7.185.36.183) 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; Tue, 25 Oct 2022 10:16:35 +0800 Received: from [10.174.178.174] (10.174.178.174) by dggpemm500007.china.huawei.com (7.185.36.183) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Tue, 25 Oct 2022 10:16:33 +0800 Subject: Re: [PATCH v2] kset: fix memory leak when kset_register() returns error To: Luben Tuikov , , , , , , , CC: , , , , , , , , , , , , , , References: <20221024121910.1169801-1-yangyingliang@huawei.com> <176ae1a1-9240-eef8-04e9-000d47646f4a@amd.com> From: Yang Yingliang Message-ID: <26c8c125-453c-af32-a66c-2a37e964ce19@huawei.com> Date: Tue, 25 Oct 2022 10:16:33 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US X-Originating-IP: [10.174.178.174] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpemm500007.china.huawei.com (7.185.36.183) 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,URIBL_BLOCKED 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 Hi, On 2022/10/25 5:25, Luben Tuikov wrote: > On 2022-10-24 17:06, Luben Tuikov wrote: >> On 2022-10-24 08:19, Yang Yingliang wrote: >>> Inject fault while loading module, kset_register() may fail. >>> If it fails, the name allocated by kobject_set_name() which >>> is called before kset_register() is leaked, because refcount >>> of kobject is hold in kset_init(). >> "is hold" --> "was set". >> >> Also, I'd say "which must be called" instead of "is", since >> we cannot register kobj/kset without a name--the kobj code crashes, >> and we want to make this clear. IOW, a novice user may wonder >> where "is" it called, as opposed to learning that they "must" >> call it to allocate/set a name, before calling kset_register(). >> >> So, I'd say this: >> >> "If it fails, the name allocated by kobject_set_name() which must >> be called before a call to kset_regsiter() is leaked, since >> refcount of kobj was set in kset_init()." > Actually, to be a bit more clear: > > "If kset_register() fails, the name allocated by kobject_set_name(), > namely kset.kobj.name, which must be called before a call to kset_register(), > may be leaked, if the caller doesn't explicitly free it, say by calling kset_put(). > > To mitigate this, we free the name in kset_register() when an error is encountered, > i.e. when kset_register() returns an error." Thanks for you suggestion. > >>> As a kset may be embedded in a larger structure which needs >>> be freed in release() function or error path in callers, we >> Drop "As", start with "A kset". "which needs _to_ be". >> Also please specify that the release is part of the ktype, >> like this: >> >> "A kset may be embedded in a larger structure which needs to be >> freed in ktype.release() or error path in callers, we ..." >> >>> can not call kset_put() in kset_register(), or it will cause >>> double free, so just call kfree_const() to free the name and >>> set it to NULL. >>> >>> With this fix, the callers don't need to care about the name >>> freeing and call an extra kset_put() if kset_register() fails. >> This is unclear because you're *missing* a verb: >> "and call an extra kset_put()". >> Please add the proper verb _between_ "and call", something like, >> >> "With this fix, the callers don't need to care about freeing >> the name of the kset, and _can_ call kset_put() if kset_register() fails." I was mean the callers don't need to care about freeing the name of the kset and the callers don't need to care about calling kset_put() Thanks, Yang >> >> Choose a proper verb here: can, should, cannot, should not, etc. >> >> We can do this because you set "kset.kobj.name to NULL, and this >> is checked for in kobject_cleanup(). We just need to stipulate >> whether they should/shouldn't have to call kset_put(), or can free the kset >> and/or the embedding object themselves. This really depends >> on how we want kset_register() to behave in the future, and on >> user's own ktype.release implementation... > Forgot "may", "may not". > > So, do we want to say "may call kset_put()", like: > > "With this fix, the callers need not care about freeing > the name of the kset, and _may_ call kset_put() if kset_register() fails." > > Or do we want to say "should" or even "must"--it really depends on > what else is (would be) going on in kobj registration. > > Although, the user may have additional work to be done in the ktype.release() > callback for the embedding object. It would be good to give them the freedom, > i.e. "may", to call kset_put(). If that's not the case, this must be explicitly > stipulated with the proper verb. > > Regards, > Luben > > .