Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp3775090imm; Mon, 17 Sep 2018 03:03:35 -0700 (PDT) X-Google-Smtp-Source: ANB0Vdam4khbo+C4IqCyXMQ0sLyIsRxyk+wvv/QJ9rOR4DCXBRDFVmwBu828ikbYx4kmM9FhCYC1 X-Received: by 2002:a65:614a:: with SMTP id o10-v6mr22818066pgv.387.1537178614977; Mon, 17 Sep 2018 03:03:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537178614; cv=none; d=google.com; s=arc-20160816; b=uk5Vspk9K8CGRemKov0bXsXfDKBEjohbGu81MbNaB4zE87hv2S35agAGXEGU1stUCg rPLZsvtnK+hErLpk5r3j3cdhASoMGJp3nOQIfYsBkMVt0AmVEBjH5o+O45ESARf5bMcd /G1LDMbowyKKqTipE7T4CjJK/qtqnpj6Yu9I/bkFxIruQedjNONBV7mNAatEee5dbT8u UCcaKLVi35T/I2spx/xE4I9AmtolStoMtVmCuTaS6E0BahURE44pCJVrCFlK/FzuYNq6 29ZyyMY2oCMGkjQK1aybQv1ep9ZBn1XUCrrc8THPEI0bk1NWApouJ/zHrtDpVRQqKWHh j72w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject:cc :to:from; bh=//sv84nIlefvvQdx6a77MG9qLPNihoWq5i39mDUXFMU=; b=HTMr7pzLUjDePHJLvGJ4H1UHyxKDuaYguMXCqmnch+97eRkAeIWXDwlXsd9WVZh6Zy nqS3dOgj563Ui+VDM0+TEXxGCoXPZLfw2giQkzrCufC74jZCCDWPGBa7bLppOopiEH73 5ty+lpPl2K1Ac4E+jxcUY6Ln9FBbknVDrm+S6xgkBSj+Tq3djlrS9Dsf0Cqlvi3agV3p YXyDtgzYAXL7Aw4Do5R7C9G0IBtHiaELxzPWUIoXf/MW8k3uYRGqnXMFqJTHIEn5/PyA lmCWDP23mcURkTV78YPSZpdo3M3Rgan5QcJHSbCV0YFWsFJhEwE9GlAHwBoxm4IiJS3l 9/hg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a8-v6si14522957pgl.568.2018.09.17.03.03.19; Mon, 17 Sep 2018 03:03:34 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727179AbeIQP2Q (ORCPT + 99 others); Mon, 17 Sep 2018 11:28:16 -0400 Received: from szxga04-in.huawei.com ([45.249.212.190]:12590 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726169AbeIQP2P (ORCPT ); Mon, 17 Sep 2018 11:28:15 -0400 Received: from DGGEMS405-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id C4418AC64D4D4; Mon, 17 Sep 2018 18:01:31 +0800 (CST) Received: from linux-work.huawei.com (10.67.189.174) by DGGEMS405-HUB.china.huawei.com (10.3.19.205) with Microsoft SMTP Server id 14.3.399.0; Mon, 17 Sep 2018 18:01:26 +0800 From: nixiaoming To: , CC: , , Subject: [PATCH] fix memory leak in ramoops_init Date: Mon, 17 Sep 2018 17:15:31 +0800 Message-ID: <20180917091531.21356-1-nixiaoming@huawei.com> X-Mailer: git-send-email 2.10.1 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.67.189.174] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 1, memory leak in ramoops_register_dummy. dummy_data = kzalloc(sizeof(*dummy_data), GFP_KERNEL); but no free when platform_device_register_data return fail 2, if kzalloc(sizeof(*dummy_data), GFP_KERNEL) return NULL, but platform_driver_register(&ramoops_driver) return 0 kfree(NULL) in ramoops_exit so, add return val for ramoops_register_dummy, and check it in ramoops_init 3, memory leak in ramoops_init. miss platform_device_unregister(dummy) and kfree(dummy_data) when platform_driver_register(&ramoops_driver) return fail Signed-off-by: nixiaoming --- fs/pstore/ram.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/fs/pstore/ram.c b/fs/pstore/ram.c index bd9812e..331b600 100644 --- a/fs/pstore/ram.c +++ b/fs/pstore/ram.c @@ -604,17 +604,17 @@ static struct platform_driver ramoops_driver = { }, }; -static void ramoops_register_dummy(void) +static int ramoops_register_dummy(void) { if (!mem_size) - return; + return -EINVAL; pr_info("using module parameters\n"); dummy_data = kzalloc(sizeof(*dummy_data), GFP_KERNEL); if (!dummy_data) { pr_info("could not allocate pdata\n"); - return; + return -ENOMEM; } dummy_data->mem_size = mem_size; @@ -636,13 +636,25 @@ static void ramoops_register_dummy(void) if (IS_ERR(dummy)) { pr_info("could not create platform device: %ld\n", PTR_ERR(dummy)); + kfree(dummy_data); + return PTR_ERR(dummy); } + return 0; } static int __init ramoops_init(void) { - ramoops_register_dummy(); - return platform_driver_register(&ramoops_driver); + int ret = ramoops_register_dummy(); + + if (ret != 0) + return ret; + + ret = platform_driver_register(&ramoops_driver); + if (ret != 0) { + platform_device_unregister(dummy); + kfree(dummy_data); + } + return ret; } postcore_initcall(ramoops_init); -- 1.9.0