Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp1298701imw; Sat, 16 Jul 2022 03:33:53 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vDh9A8sYcLtU6JcB1GkNGXGDYjQZN51glK+xGpBfStWJSGEm2Y/ogzcq7yNhRJlD/PR0LS X-Received: by 2002:a17:902:ecc2:b0:16c:5b89:fd0b with SMTP id a2-20020a170902ecc200b0016c5b89fd0bmr18333594plh.122.1657967632717; Sat, 16 Jul 2022 03:33:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657967632; cv=none; d=google.com; s=arc-20160816; b=o/sbARnSf0fXAhbjvG/lK9VpGopCWiClNJes5/XwIqes1VwGfIPln2evD/hFITHd2l F16mRgAQG3+944UZXSRxhVC4bQRn0C59RHEp8oI39XKc2bYWMGqS03IelCstCdCF/2w9 PAmFcjDyl7/4RFT/2di0+2/aJcmQwUpsuKldyAC3ioDh7VIAHSSexhCww4yotT6STaft onV1otAm545elA9WivCDrSFQjO+jaYo8DTwR8zaCIjxAxhoBprjFgjbTGyszBmA+05VW /yOyDSdRTEw/Mwn9AklCBNzKMkDcaD8rgJKuGuepsLNyE6yo1KILnouOvOOIOFRkyBV0 40WQ== 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=jCSx15sXlfx9sXY61JP1dClm26KdPBGmu/e4F8vfTKc=; b=tnLROvaDroGUZjWOHIQMrliGLBALakP/euhZpchV99i8s4W5vIDrOK5lQ+uonI/d70 fUOo39iNWrhSzL71QT+pwSSV0xyBvL7lMnssILUoSwxzYAloktWdHGO/Tw6zn6oLqEG9 sGusaim4tdoXH3oP/TXa/igtk99OWPbaLs96Fp0YHKFjemvH5UvPMNe6jxtOu9yJ3zvJ G6IPGTOtFRSOTl/nRjxpG38892T5cUNZxtLenzpjL5oNfYVOb7iLWqD/UJltXvNZRaCL euCix1ievuD62hnPXicTk9Qqwe38D+rHvwf3f1AA0pBybWa4DmGWzRRGM6a16tCMVDYK ACPQ== 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 u63-20020a638542000000b0040de553ebecsi3905927pgd.616.2022.07.16.03.33.37; Sat, 16 Jul 2022 03:33:52 -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 S231311AbiGPJxC (ORCPT + 99 others); Sat, 16 Jul 2022 05:53:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43102 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229521AbiGPJxB (ORCPT ); Sat, 16 Jul 2022 05:53:01 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3C56D1EC72 for ; Sat, 16 Jul 2022 02:53:00 -0700 (PDT) Received: from kwepemi500012.china.huawei.com (unknown [172.30.72.54]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4LlNl55TYRzkWWq; Sat, 16 Jul 2022 17:50: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; Sat, 16 Jul 2022 17:52:57 +0800 From: Gaosheng Cui To: , , CC: , , Subject: [PATCH -next,v2] nvmem: core: Fix memleak in nvmem_register() Date: Sat, 16 Jul 2022 17:52:57 +0800 Message-ID: <20220716095257.2752110-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 | 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; + } + dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name); rval = device_register(&nvmem->dev); -- 2.25.1