Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp3325481imw; Mon, 18 Jul 2022 06:15:21 -0700 (PDT) X-Google-Smtp-Source: AGRyM1t20eH0tuXcO4tQI+zT6pjV65tQzdSuX2Gvd+5kcSDAOX+/1zf2iZz2srNXnE5S/83rgs+s X-Received: by 2002:ae9:ec12:0:b0:6ac:665e:1dd8 with SMTP id h18-20020ae9ec12000000b006ac665e1dd8mr17186726qkg.636.1658150121574; Mon, 18 Jul 2022 06:15:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658150121; cv=none; d=google.com; s=arc-20160816; b=JLGkilnWi/hSo3Hmdkh+zS9cJBmSZDbDVTNmt2XXkXlPrK7xpQWWpMGgx+hoivWe7W etHIuFvXelykYGDzHLodmVpE52cf9PWg9b0QkM7kIXvheqGACT5FyhuDbKJbWE16XxEm 1tAe2GJzSeU2L05l47TStuAAL+eUYpWjWx/kv0jTNjsjUsxon0oBbMWJagikE9NWTJmv 5P3s2SV6XpbSR/ehcK0leEDdjBxs1CBLvvHSmBCOpU/iUx1aHPO4uHmepM116oaQvyTj z/rBPNr1BfC8JYNs+V5nWQPqdoW39lg9NglApDoN5V/2kv9A+5FM4ipx3w+3S4pLND/o QTag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:from:references:cc:to :subject; bh=f15DeR7aKVwXMYsF6Ly0+xSDhjl6UGTcF3Tf3FHFg/U=; b=1ChKrfuuxBiZ0dUzpoDSd482H21lsELilIgS7ad1ZvE/AH6GKCOch2CtOBXqCZVWnU oVUomxPJ6NbAWdwWx8+lm82VDHHZLg7KNkmnFKSddofEjGzDs7awPyDNNzgnTctc2dJq EBI6mDQAYP2LASVSUQyMr0pXCbSEq1K/kUxdr0E1z9FJl76tQ8Ks9bjRCvxk+fVjOpsy gDAXoWTWhMjjANWzBHTJJKJ1kQj/8kzCYV51Rj5jn2NYU3iYHtsBUgoVS8Xjyw+TR498 LYXlnCtllXatASnML3fWNxBmJKx0JG3GPF9+Gqk85ebpfpnVHBTEg2BLgaFCRIr7o1gF 0lCQ== 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 g12-20020ac8774c000000b0031d35f9961csi5037169qtu.210.2022.07.18.06.15.06; Mon, 18 Jul 2022 06:15:21 -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 S234697AbiGRM3X (ORCPT + 99 others); Mon, 18 Jul 2022 08:29:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51532 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234537AbiGRM3V (ORCPT ); Mon, 18 Jul 2022 08:29:21 -0400 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 94E95B66 for ; Mon, 18 Jul 2022 05:29:20 -0700 (PDT) Received: from kwepemi500012.china.huawei.com (unknown [172.30.72.54]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4Lmh645hLpz1M7xh; Mon, 18 Jul 2022 20:26:36 +0800 (CST) Received: from [10.67.110.176] (10.67.110.176) by kwepemi500012.china.huawei.com (7.221.188.12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 18 Jul 2022 20:29:17 +0800 Subject: Re: [PATCH -next,v2] nvmem: core: Fix memleak in nvmem_register() To: Srinivas Kandagatla , CC: , , References: <20220716095257.2752110-1-cuigaosheng1@huawei.com> <9029caa7-beea-34f6-7f9b-b2b230c3baeb@linaro.org> From: cuigaosheng Message-ID: <81b18902-fb45-74b2-ec58-84b468ee9bf1@huawei.com> Date: Mon, 18 Jul 2022 20:29:17 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.1 MIME-Version: 1.0 In-Reply-To: <9029caa7-beea-34f6-7f9b-b2b230c3baeb@linaro.org> Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 8bit X-Originating-IP: [10.67.110.176] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To kwepemi500012.china.huawei.com (7.221.188.12) 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 Thanks for your ideas, I have made another patch, moving nvmem_validate_keepouts() after device_register() and let the device core deal with cleaning name in error cases, it's a better option for the issue. Thanks, 在 2022/7/18 19:44, Srinivas Kandagatla 写道: > > > On 16/07/2022 10:52, Gaosheng Cui wrote: >> dev_set_name will alloc memory for nvmem->dev.kobj.name in >> nvmem_register, when nvmem_validate_keepouts failed, nvmem's >> memory will be freed and return, but nobody will free memory >> for nvmem->dev.kobj.name, there will be memleak, so moving >> dev_set_name after nvmem_validate_keepouts to fix it. >> >> Fixes: de0534df9347 ("nvmem: core: fix error handling while >> validating keepout regions") >> Signed-off-by: Gaosheng Cui >> --- >>   drivers/nvmem/core.c | 28 ++++++++++++++-------------- >>   1 file changed, 14 insertions(+), 14 deletions(-) >> >> diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c >> index 1e3c754efd0d..a5d75edf509d 100644 >> --- a/drivers/nvmem/core.c >> +++ b/drivers/nvmem/core.c >> @@ -808,20 +808,6 @@ struct nvmem_device *nvmem_register(const struct >> nvmem_config *config) >>       else if (!config->no_of_node) >>           nvmem->dev.of_node = config->dev->of_node; >>   -    switch (config->id) { >> -    case NVMEM_DEVID_NONE: >> -        dev_set_name(&nvmem->dev, "%s", config->name); >> -        break; >> -    case NVMEM_DEVID_AUTO: >> -        dev_set_name(&nvmem->dev, "%s%d", config->name, nvmem->id); >> -        break; >> -    default: >> -        dev_set_name(&nvmem->dev, "%s%d", >> -                 config->name ? : "nvmem", >> -                 config->name ? config->id : nvmem->id); >> -        break; >> -    } >> - >>       nvmem->read_only = device_property_present(config->dev, >> "read-only") || >>                  config->read_only || !nvmem->reg_write; >>   @@ -838,6 +824,20 @@ struct nvmem_device *nvmem_register(const >> struct nvmem_config *config) >>           } >>       } >>   +    switch (config->id) { >> +    case NVMEM_DEVID_NONE: >> +        dev_set_name(&nvmem->dev, "%s", config->name); >> +        break; >> +    case NVMEM_DEVID_AUTO: >> +        dev_set_name(&nvmem->dev, "%s%d", config->name, nvmem->id); >> +        break; >> +    default: >> +        dev_set_name(&nvmem->dev, "%s%d", >> +                 config->name ? : "nvmem", >> +                 config->name ? config->id : nvmem->id); >> +        break; >> +    } >> + > The issue with this approach is that dev_err messages in > nvmem_validate_keepouts() will show up without device name setup. > > I think better option is to move nvmem_validate_keepouts() after > device_register() and let the device core deal with cleaning name in > error cases. > > --srini > >>       dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", >> config->name); >>         rval = device_register(&nvmem->dev); > .