Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp3276507imw; Mon, 18 Jul 2022 05:25:35 -0700 (PDT) X-Google-Smtp-Source: AGRyM1s29+8GebXVHTZJCnHmkcTzHbWl0cstu4a1FSlYYHx17BXMsd5craWZWr6YjBcMw96zaK8G X-Received: by 2002:a17:902:b785:b0:16b:d978:3899 with SMTP id e5-20020a170902b78500b0016bd9783899mr27617248pls.109.1658147135695; Mon, 18 Jul 2022 05:25:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658147135; cv=none; d=google.com; s=arc-20160816; b=U1F7f2FHs+1jGkY74hUbY1Bkg0CFtfmxNizUy8GSoq3IUzUyKVlnSL2RX88AsELZmU G0TBY5prdBYnwa5DwuWcEi5DJpH2ej+cYiB1rfdznFaNkJQ22PK+WOu5LmbQO9vxBhoc 5CNogrUkIAC7JYFpb0dkQ+C7tay9e0pUlHK0ohQmRj2mKQfk9Uan/DjzwzuxrRUx1j8f 9j4znwXu6WZLCnBNdym2XJA7VUaKy2MMewXs+K1a1MuhnH7zJP3L8YJq0zG7+GLMhflB A54QddW2dAZ7vo3CTNP6CDsdxzBTSxxZB/q7zUn6r9iAAKCTAjfEgRCdIApAo9x9zIkd kZlQ== 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=7kdMo9KKYe3vHk/64JyXKrptHqpv1fNtYPRRuMwDBwI=; b=xHMQfVcPnZd8KjdsPFYhyFGlB2g2sALEcEMk36xRaf1gCwDp32DZMY75fUR6duJWQq Xk+t94nx5AGa40I1Sv5/4E58odHz3OtyOsUCl59zJO0YnAZFRPDKMrvZFIAuoMgbBmuj sPTRWwEjL7CBni9PjmCKDaE5JlECXM4KjMl6qeqbCp7OqjSsi7zB8exoKK2wJB/ApBGZ V5wL6ccohelDHZW4Hi3dSZG8X1TaPFNlkKMFbrus9isIRrjatZInJs/AjlA/zVQ2i6uT 1o5fjeNiQFIGU4T6OQBcHkfhTjDWIh7nrhyQK6rULVlwnQJAT3YciBjbwo+iw2klY85/ 1D6Q== 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 h14-20020a056a00170e00b0052aea37f332si17174880pfc.112.2022.07.18.05.25.21; Mon, 18 Jul 2022 05:25:35 -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 S234911AbiGRMMg (ORCPT + 99 others); Mon, 18 Jul 2022 08:12:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60676 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234915AbiGRMM0 (ORCPT ); Mon, 18 Jul 2022 08:12:26 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EBA2124F00 for ; Mon, 18 Jul 2022 05:12:23 -0700 (PDT) Received: from kwepemi500012.china.huawei.com (unknown [172.30.72.53]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4Lmglj6WrPzlW4X; Mon, 18 Jul 2022 20:10:41 +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:12:21 +0800 From: Gaosheng Cui To: , , CC: , , Subject: [PATCH -next,v3] nvmem: core: Fix memleak in nvmem_register() Date: Mon, 18 Jul 2022 20:12:21 +0800 Message-ID: <20220718121221.3099515-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: dggems706-chm.china.huawei.com (10.3.19.183) 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 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 | 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