Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp3276764imw; Mon, 18 Jul 2022 05:25:57 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sKDVtU3urd9zOvxNS0RwcPWfPMbUJganb8YURIJD+MgnD8PUUrFgXLSprO21ii3IHFyaPu X-Received: by 2002:a4a:a4d1:0:b0:435:9b4d:6d23 with SMTP id c17-20020a4aa4d1000000b004359b4d6d23mr1629404oom.68.1658147157323; Mon, 18 Jul 2022 05:25:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658147157; cv=none; d=google.com; s=arc-20160816; b=tySlAV5gbbQu7SD5zGaNXfRO/jn2apg9NrvHIvp4Ppq09OGyypTPh6ZdHo/jk4sV13 0vF+NAMjAUlWdfWY2oZJMEC5+OftxVtBgvmpIJmDx9b0n6IaymUKmmEsFFOxa/JQ7JrK kDsJv3m5b5nkJ+Q75DBlN8cE+Z9sDW0EIIH6hFznuUfi0lBjiMpcTVZ1tl8Ruz4XUgT1 8JC+q5dALMgFRQUHZqGY/xz9PS2a1F8rj9aOweIi7zJ2aXhOSZiBl4CALVxz/Ekvog/D afjatzm+kwGEPXiAPxveX6SotiuxpHTP7eZAJEYkJJaiBimv6mCtQI0sEC+itm09a5wd QzIg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=d1OC2XyjfNuX2PdQDvmFdFHZUuDqhDOwsYVKridSRsw=; b=nA3QTw/RFc4BzqCPvN3sCXOpkLp5zEia5Z5+3QvFX6Apx/eni9G+5CK8wv5hUJzQi+ Bwygr/223JvegjePpKbc7tg181QHKGeMmuCfQFAjDa+9vgIKFhYU0WztxQLzDFORMx/l 3AQV+cE7dOfIiVlwwh4Q9+opX27FQJbxSTU22DL2ctbDS6m7UJxebbrCLe5GB2VjNADi nq+/In1r1JAPFHtrWz/Z99YVwWlriFcgviog23n24OtxJs/vd9V8HSLSMgYyA3/t4zdr AAjVZNCj1fHJECneWSYx8ebbfuKEDTMuat3sGhNk9MsBnm/iAJhlyJZoxoKIW8sX/gze tz5A== 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 d1-20020a05683025c100b00616cc0c301csi10181739otu.17.2022.07.18.05.25.39; Mon, 18 Jul 2022 05:25:57 -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 S235024AbiGRMV1 (ORCPT + 99 others); Mon, 18 Jul 2022 08:21:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42556 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235020AbiGRMVZ (ORCPT ); Mon, 18 Jul 2022 08:21:25 -0400 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 58DEC24F3F for ; Mon, 18 Jul 2022 05:21:23 -0700 (PDT) Received: from kwepemi500012.china.huawei.com (unknown [172.30.72.53]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4Lmgww0S7vz1M7xB; Mon, 18 Jul 2022 20:18:40 +0800 (CST) Received: from dggphis33418.huawei.com (10.244.148.83) 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:21:21 +0800 From: Gaosheng Cui To: , , CC: , , Subject: [PATCH -next,v4] nvmem: core: Fix memleak in nvmem_register() Date: Mon, 18 Jul 2022 20:21:20 +0800 Message-ID: <20220718122120.3103545-1-cuigaosheng1@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.244.148.83] 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,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 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 nvmem_validate_keepouts() after device_register() and let the device core deal with cleaning name in error cases. Fixes: de0534df9347 ("nvmem: core: fix error handling while validating keepout regions") Signed-off-by: Gaosheng Cui --- drivers/nvmem/core.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index 1e3c754efd0d..6067d3bbff5d 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -829,21 +829,19 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) nvmem->dev.groups = nvmem_dev_groups; #endif - if (nvmem->nkeepout) { - rval = nvmem_validate_keepouts(nvmem); - if (rval) { - ida_free(&nvmem_ida, nvmem->id); - kfree(nvmem); - return ERR_PTR(rval); - } - } - dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name); rval = device_register(&nvmem->dev); if (rval) goto err_put_device; + if (nvmem->nkeepout) { + rval = nvmem_validate_keepouts(nvmem); + if (rval) { + goto err_device_del; + } + } + if (config->compat) { rval = nvmem_sysfs_setup_compat(nvmem, config); if (rval) -- 2.25.1