Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp3775572imm; Mon, 17 Sep 2018 03:04:03 -0700 (PDT) X-Google-Smtp-Source: ANB0Vdb64qLOgfilR74CmLs63QKorC9Ax4uoQdEarCs3MetD/IUpmVyjkz1pGoHj0f2BrrTog4Mm X-Received: by 2002:a63:28c7:: with SMTP id o190-v6mr22769971pgo.84.1537178643724; Mon, 17 Sep 2018 03:04:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537178643; cv=none; d=google.com; s=arc-20160816; b=jLbmAD3VyiW7Jk4IoPu5ooMi0+1BtmkRD9D6jyqzxUtMHKTKVBL8ukgYRpCJ2XKWzd 6pdFsCDPwrHjpNSNZ3cGKsFKiL2MI/6k+mDIZesKCeaWVB2kiHHpw0oRnlHZrEW/O54T qqAWQRGGoC3NrFSFjc6aFCkvD6pMVqolwXwvw6ad1tJSif3ZeqKQZuaWTQ8/OTQjYUXH 9VHLNIbOGvBG98C/qGd2kzIhlJApvFDgQFI5XkopejA0/Gf5fK85ZGEC8NQyZpYGrZy7 O/cMO0tO1vkEM122qWb9cKpKaXsm9RaOdIa1B75Kc9a1YsBoobcSmrSWOW4oVGfwdp3D lJCw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :content-language:accept-language:message-id:date:thread-index :thread-topic:subject:cc:to:from; bh=h4yJQV1K3hBge40lL1fDltTvNX2thv8LsjVvWPSSmIQ=; b=mR1lanYrGLLy24HUQv/xeq/3mmZrNFlEE4JojFjpYotXHUPStO4BuVvToZSSsjZiLv HUsY/8VFHb2+vQecY2NQ5sJOifg78Swv+BeNAGH8KF3G4NGa9m+0CkVCJ1dozt6qu9mk sNKyTCnCVK1hpok0G5ii6fhpcx4DdCUqQOYFpchknyg8Ay3vWrPnxPYh9f4R6bg0Vhii BzYNuvn+MiQxQXARfPP6598PcOmY9KrkCXxXln1nATcQ8x8ATdIzqbhJ+rB79x4raEnX Q5Jo8TBpDeF5Wrexox/GyKdIOcBAhZNpp3gnUnBXk3mZGEkD2r4gLZDcpHkQR7UVG8VQ XEWQ== 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 ca2-v6si17047009plb.305.2018.09.17.03.03.48; Mon, 17 Sep 2018 03:04:03 -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 S1727979AbeIQPaC convert rfc822-to-8bit (ORCPT + 99 others); Mon, 17 Sep 2018 11:30:02 -0400 Received: from szxga02-in.huawei.com ([45.249.212.188]:6456 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726169AbeIQPaC (ORCPT ); Mon, 17 Sep 2018 11:30:02 -0400 Received: from DGGEMM405-HUB.china.huawei.com (unknown [172.30.72.53]) by Forcepoint Email with ESMTP id 9C48015EE1C6E; Mon, 17 Sep 2018 18:03:18 +0800 (CST) Received: from DGGEMM422-HUB.china.huawei.com (10.1.198.39) by DGGEMM405-HUB.china.huawei.com (10.3.20.213) with Microsoft SMTP Server (TLS) id 14.3.399.0; Mon, 17 Sep 2018 18:03:19 +0800 Received: from DGGEMM527-MBS.china.huawei.com ([169.254.5.129]) by dggemm422-hub.china.huawei.com ([10.1.198.39]) with mapi id 14.03.0399.000; Mon, 17 Sep 2018 18:03:15 +0800 From: Nixiaoming To: "jack@suse.cz" , "amir73il@gmail.com" CC: "linux-fsdevel@vger.kernel.org" , "linux-kernel@vger.kernel.org" Subject: //RE: [PATCH] fix memory leak in ramoops_init Thread-Topic: //RE: [PATCH] fix memory leak in ramoops_init Thread-Index: AdRObaWZ83mG4rZTR1iKR4up27fUnQ== Date: Mon, 17 Sep 2018 10:03:14 +0000 Message-ID: Accept-Language: zh-CN, en-US Content-Language: zh-CN X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.57.88.168] Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 8BIT MIME-Version: 1.0 X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org I am very sorry, I sent the wrong email, please ignore the patch just now. -----Original Message----- From: Nixiaoming Sent: Monday, September 17, 2018 5:16 PM To: jack@suse.cz; amir73il@gmail.com Cc: Nixiaoming ; linux-fsdevel@vger.kernel.org; linux-kernel@vger.kernel.org Subject: [PATCH] fix memory leak in ramoops_init 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