From patchwork Wed May 7 08:53:07 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Shevchenko X-Patchwork-Id: 4126581 X-Patchwork-Delegate: dan.j.williams@gmail.com Return-Path: X-Original-To: patchwork-dmaengine@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id E07C3BFF02 for ; Wed, 7 May 2014 08:53:33 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 18341202AE for ; Wed, 7 May 2014 08:53:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 20D7F20268 for ; Wed, 7 May 2014 08:53:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755497AbaEGIxb (ORCPT ); Wed, 7 May 2014 04:53:31 -0400 Received: from mga03.intel.com ([143.182.124.21]:61103 "EHLO mga03.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755104AbaEGIxa (ORCPT ); Wed, 7 May 2014 04:53:30 -0400 Received: from azsmga001.ch.intel.com ([10.2.17.19]) by azsmga101.ch.intel.com with ESMTP; 07 May 2014 01:53:11 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="4.97,1001,1389772800"; d="scan'208";a="428758923" Received: from unknown (HELO smile.fi.intel.com) ([10.237.72.73]) by azsmga001.ch.intel.com with ESMTP; 07 May 2014 01:53:10 -0700 Received: from andy by smile.fi.intel.com with local (Exim 4.82) (envelope-from ) id 1WhxbA-00035S-D9; Wed, 07 May 2014 11:53:08 +0300 From: Andy Shevchenko To: Dan Williams , dmaengine@vger.kernel.org Cc: Andy Shevchenko Subject: [PATCH v2, resend] dmatest: prevent memory leakage on error path in thread Date: Wed, 7 May 2014 11:53:07 +0300 Message-Id: <1399452787-11831-1-git-send-email-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 1.9.2 Sender: dmaengine-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: dmaengine@vger.kernel.org X-Spam-Status: No, score=-7.5 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 When we fail to allocate memory for thread->srcs or thread->dsts and src_cnt or dst_cnt great than 1 we leak memory on error path. This patch fixes the issue. As a side effect we allocate the exact number of soruce and destination buffers. Signed-off-by: Andy Shevchenko --- drivers/dma/dmatest.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/drivers/dma/dmatest.c b/drivers/dma/dmatest.c index 740013d..d490c0f 100644 --- a/drivers/dma/dmatest.c +++ b/drivers/dma/dmatest.c @@ -441,7 +441,7 @@ static int dmatest_func(void *data) src_cnt = min_odd(params->pq_sources | 1, dma_maxpq(dev, 0)); dst_cnt = 2; - pq_coefs = kmalloc(params->pq_sources+1, GFP_KERNEL); + pq_coefs = kmalloc(params->pq_sources + 1, GFP_KERNEL); if (!pq_coefs) goto err_thread_type; @@ -450,7 +450,7 @@ static int dmatest_func(void *data) } else goto err_thread_type; - thread->srcs = kcalloc(src_cnt+1, sizeof(u8 *), GFP_KERNEL); + thread->srcs = kcalloc(src_cnt, sizeof(u8 *), GFP_KERNEL); if (!thread->srcs) goto err_srcs; for (i = 0; i < src_cnt; i++) { @@ -458,9 +458,8 @@ static int dmatest_func(void *data) if (!thread->srcs[i]) goto err_srcbuf; } - thread->srcs[i] = NULL; - thread->dsts = kcalloc(dst_cnt+1, sizeof(u8 *), GFP_KERNEL); + thread->dsts = kcalloc(dst_cnt, sizeof(u8 *), GFP_KERNEL); if (!thread->dsts) goto err_dsts; for (i = 0; i < dst_cnt; i++) { @@ -468,7 +467,6 @@ static int dmatest_func(void *data) if (!thread->dsts[i]) goto err_dstbuf; } - thread->dsts[i] = NULL; set_user_nice(current, 10); @@ -751,14 +749,17 @@ static int dmatest_func(void *data) runtime = ktime_us_delta(ktime_get(), ktime); ret = 0; - for (i = 0; thread->dsts[i]; i++) - kfree(thread->dsts[i]); + + i = dst_cnt; err_dstbuf: + while (--i >= 0) + kfree(thread->dsts[i]); kfree(thread->dsts); err_dsts: - for (i = 0; thread->srcs[i]; i++) - kfree(thread->srcs[i]); + i = src_cnt; err_srcbuf: + while (--i >= 0) + kfree(thread->srcs[i]); kfree(thread->srcs); err_srcs: kfree(pq_coefs);