From patchwork Mon Dec 21 12:26:29 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mauro Carvalho Chehab X-Patchwork-Id: 7895591 Return-Path: X-Original-To: patchwork-linux-media@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id AA7929F349 for ; Mon, 21 Dec 2015 12:27:02 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id C236D2047D for ; Mon, 21 Dec 2015 12:27:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id C4CD52045B for ; Mon, 21 Dec 2015 12:27:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751376AbbLUM06 (ORCPT ); Mon, 21 Dec 2015 07:26:58 -0500 Received: from bombadil.infradead.org ([198.137.202.9]:50504 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751145AbbLUM06 (ORCPT ); Mon, 21 Dec 2015 07:26:58 -0500 Received: from 177.17.209.187.dynamic.adsl.gvt.net.br ([177.17.209.187] helo=smtp.w2.samsung.com) by bombadil.infradead.org with esmtpsa (Exim 4.80.1 #2 (Red Hat Linux)) id 1aAzYH-00087A-04; Mon, 21 Dec 2015 12:26:57 +0000 Received: from mchehab by smtp.w2.samsung.com with local (Exim 4.86) (envelope-from ) id 1aAzXr-0004PA-IF; Mon, 21 Dec 2015 10:26:31 -0200 From: Mauro Carvalho Chehab Cc: Mauro Carvalho Chehab , Linux Media Mailing List , Mauro Carvalho Chehab , Krzysztof Kozlowski Subject: [PATCH] au8522: Avoid memory leak in case of errors Date: Mon, 21 Dec 2015 10:26:29 -0200 Message-Id: X-Mailer: git-send-email 2.5.0 To: unlisted-recipients:; (no To-header on input) Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP As reported by kmemleak: unreferenced object 0xffff880321e1da40 (size 32): comm "modprobe", pid 3309, jiffies 4295019569 (age 2359.636s) hex dump (first 32 bytes): 47 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 G............... 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ backtrace: [] kmemleak_alloc+0x4e/0xb0 [] kmem_cache_alloc_trace+0x1ec/0x280 [] au8522_probe+0x19a/0xa30 [au8522_decoder] [] i2c_device_probe+0x2b2/0x490 [] driver_probe_device+0x454/0xd90 [] __device_attach_driver+0x17b/0x230 [] bus_for_each_drv+0x11a/0x1b0 [] __device_attach+0x1cd/0x2c0 [] device_initial_probe+0x13/0x20 [] bus_probe_device+0x1af/0x250 [] device_add+0x943/0x13b0 [] device_register+0x1a/0x20 [] i2c_new_device+0x5d6/0x8f0 [] v4l2_i2c_new_subdev_board+0x1e4/0x250 [v4l2_common] [] v4l2_i2c_new_subdev+0xd7/0x110 [v4l2_common] [] au0828_card_analog_fe_setup+0x2e6/0x3f0 [au0828] Checking where the error happens: (gdb) list *au8522_probe+0x19a 0x99a is in au8522_probe (drivers/media/dvb-frontends/au8522_decoder.c:761). 756 printk(KERN_INFO "au8522_decoder attach existing instance.\n"); 757 break; 758 } 759 760 demod_config = kzalloc(sizeof(struct au8522_config), GFP_KERNEL); 761 if (demod_config == NULL) { 762 if (instance == 1) 763 kfree(state); 764 return -ENOMEM; 765 } Shows that the error pathc is not being handled properly. The issue here is that it should have been calling hybrid_tuner_release_state() function, by calling au8522_release_state(). Yet, it is easier to just reorder the allocation, with makes the error path simpler. Signed-off-by: Mauro Carvalho Chehab --- drivers/media/dvb-frontends/au8522_decoder.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/drivers/media/dvb-frontends/au8522_decoder.c b/drivers/media/dvb-frontends/au8522_decoder.c index 28d7dc2fee34..88fddbbf8491 100644 --- a/drivers/media/dvb-frontends/au8522_decoder.c +++ b/drivers/media/dvb-frontends/au8522_decoder.c @@ -738,11 +738,16 @@ static int au8522_probe(struct i2c_client *client, return -EIO; } + demod_config = kzalloc(sizeof(struct au8522_config), GFP_KERNEL); + if (!demod_config) + return -ENOMEM; + /* allocate memory for the internal state */ instance = au8522_get_state(&state, client->adapter, client->addr); switch (instance) { case 0: printk(KERN_ERR "au8522_decoder allocation failed\n"); + kfree(demod_config); return -EIO; case 1: /* new demod instance */ @@ -754,12 +759,6 @@ static int au8522_probe(struct i2c_client *client, break; } - demod_config = kzalloc(sizeof(struct au8522_config), GFP_KERNEL); - if (demod_config == NULL) { - if (instance == 1) - kfree(state); - return -ENOMEM; - } demod_config->demod_address = 0x8e >> 1; state->config = demod_config;