Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp3304504imw; Mon, 18 Jul 2022 05:56:54 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tjP3dYrwok3kZr0qKpslk4iBeD3wfe5r/HO11rJGw+ASpycUjpGXOZZpMMuJzaboaZt44V X-Received: by 2002:a05:6808:124a:b0:32c:45f0:5011 with SMTP id o10-20020a056808124a00b0032c45f05011mr13150175oiv.217.1658149014572; Mon, 18 Jul 2022 05:56:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658149014; cv=none; d=google.com; s=arc-20160816; b=mrJIfhjmlaC5jg0+CFho22e09CKJSWqrJvoa/qITrmmWCxkidSyoZlc3rt7H9q9oyJ 1tfJCqMsQXVw39xz66s0HCp20G2yDnIKIagwWfGk0U6F46wHY0dcX8SgDcR43AHvp3hM gny9OR//K1ziuaGYJSOYg+tSdFF7VoZslHXVxHVBIHso3Zv1opRlpdrYn8/RpvY5brlC KrZTMAQ4Edq0m9KGbCLwWMS20DwUZUrglXR2NQqfr4JaR3PRlO9aXsWoYml7Qi1yv+H3 wtEHRa1eCQE7FKs+xbu2L4safbhtTFe5v95OWqdjvB/6TyMAORdivRLXG7Ai0mRECWaU kUdQ== 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=LKCoD/KnufnkO8yyYrHjvsItoxsCe8VHk9OXki6/g20=; b=X2Le4crUUqS0yke04koNxcTzYosjPUAYBpS+3hjHb3SUcegoOGPqJnLTHjr0YQehQ3 CRxozLiOkBP8VMX/TfowsdKB6VrxhyZGHUZe9tC/WHuLv0P1zj/tq4cUWie9kqTUVXvb vC3UXK+p9UuqoM2qNuqOMTpzIhIr474ksvILrkxRQBnSGsNFY2Leh7LRA+7ZbOLkRfWY 2DslkPCnvDnLfb00oD8SUiKl1tyqxfmaTsEdsFqFCKgwpToBi4hM4o2rHYMz8EQHEl3d XJ8Q+mRy/WuXmMoSOP16ruxiTVR5i6K7kL5jlyuelTRQxF/6C6yU3qf7FFCzdrfuUwIh japw== 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 bs11-20020a056830398b00b0061c9ee91bc4si4299607otb.94.2022.07.18.05.56.41; Mon, 18 Jul 2022 05:56:54 -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 S234949AbiGRMj5 (ORCPT + 99 others); Mon, 18 Jul 2022 08:39:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58704 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234981AbiGRMjy (ORCPT ); Mon, 18 Jul 2022 08:39:54 -0400 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A91E7DF5A for ; Mon, 18 Jul 2022 05:39:52 -0700 (PDT) Received: from kwepemi500012.china.huawei.com (unknown [172.30.72.57]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4LmhN91ppjzFq8d; Mon, 18 Jul 2022 20:38:49 +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:39:49 +0800 From: Gaosheng Cui To: , , CC: , , Subject: [PATCH -next,v5] nvmem: core: Fix memleak in nvmem_register() Date: Mon, 18 Jul 2022 20:39:49 +0800 Message-ID: <20220718123949.3111223-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: dggems704-chm.china.huawei.com (10.3.19.181) 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 | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index 1e3c754efd0d..2164efd12ba9 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -829,21 +829,18 @@ 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