From patchwork Tue Apr 5 14:55:31 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Matias_Bj=C3=B8rling?= X-Patchwork-Id: 8752791 Return-Path: X-Original-To: patchwork-linux-block@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 792E79F7C9 for ; Tue, 5 Apr 2016 14:56:45 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 8455A2037E for ; Tue, 5 Apr 2016 14:56:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 6B6D4203A0 for ; Tue, 5 Apr 2016 14:56:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758523AbcDEO4f (ORCPT ); Tue, 5 Apr 2016 10:56:35 -0400 Received: from mail-wm0-f45.google.com ([74.125.82.45]:33805 "EHLO mail-wm0-f45.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1758716AbcDEO4S (ORCPT ); Tue, 5 Apr 2016 10:56:18 -0400 Received: by mail-wm0-f45.google.com with SMTP id l6so27328979wml.1 for ; Tue, 05 Apr 2016 07:56:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bjorling.me; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=2g0fr71YSBADyyD2Ph4uHxGFB6p/xaXDI36OSTP/evY=; b=QPeL/xUJEza7D0Naty69+YwEvG88OXqbDUCXc0MQgMhMAI4fUnjtzubiPvLMqPzLxe lQZcLpqS6zfAZwTIZ0XPDNzX+43VmUZ6i5zSs6I48JcVLBs5GwZQyRHQzgED+JQESDnd pc/GkmXcYuFYgLPMMza/kW6oCpqsTSiF4mu3k= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2g0fr71YSBADyyD2Ph4uHxGFB6p/xaXDI36OSTP/evY=; b=bAnzDYM4xfmu8NTaybaU+wz2r1un/xyexBlFv5LK+tzpxyIGis/6VXdCpduBTFjs1u xDOvOjXlyuCAvJaTt6wWeIHFX73QiGTZsfiwGE2rz21VKdmAnu9YUF4f2rR75fzAWkoZ 2tNAmR0XIKgBGrJPJhHXARRVv32uvEfZnNy0ImmHpqS7oSu6lU/gVNXkmBVM40u2JgRx HdzbWuItINI0cEvytA+PyicuiiQgwKcZHfftJw8QXS0QZ7CkNnpIfRTDCPO3U7oNT8cE vaan/C7nOtDR4hnB/nwIzwrk95XDjlTQZOQ/MsYRyufz8hkVhXvGrCTgqEKZ9LjA6/pp 1fNg== X-Gm-Message-State: AD7BkJJIRPBOXQZFlnHP5XFIJ744QbzRSusEMuOLeJVzF/TyPdIRYR3/CiBOo0npHruxVw== X-Received: by 10.194.84.66 with SMTP id w2mr25069906wjy.6.1459868176920; Tue, 05 Apr 2016 07:56:16 -0700 (PDT) Received: from Macroninja.cnexlabs.com (6164198-cl69.boa.fiberby.dk. [193.106.164.198]) by smtp.gmail.com with ESMTPSA id av3sm35125573wjc.44.2016.04.05.07.56.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 05 Apr 2016 07:56:16 -0700 (PDT) From: =?UTF-8?q?Matias=20Bj=C3=B8rling?= To: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org Cc: =?UTF-8?q?Matias=20Bj=C3=B8rling?= Subject: [PATCH 5/5] lightnvm: avoid memory leak when lun_map kcalloc fails Date: Tue, 5 Apr 2016 16:55:31 +0200 Message-Id: <1459868131-15133-6-git-send-email-m@bjorling.me> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1459868131-15133-1-git-send-email-m@bjorling.me> References: <1459868131-15133-1-git-send-email-m@bjorling.me> MIME-Version: 1.0 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org X-Spam-Status: No, score=-7.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,RP_MATCHES_RCVD,T_DKIM_INVALID,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 A memory leak occurs if the lower page table is initialized and the following dev->lun_map fails on allocation. Rearrange the initialization of lower page table to allow dev->lun_map to fail gracefully without memory leak. Reported-by: Dan Carpenter Signed-off-by: Matias Bjørling Reviewed-by: Johannes Thumshirn --- drivers/lightnvm/core.c | 51 ++++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/drivers/lightnvm/core.c b/drivers/lightnvm/core.c index 4cadbe0..fd5a9f3 100644 --- a/drivers/lightnvm/core.c +++ b/drivers/lightnvm/core.c @@ -504,6 +504,7 @@ static int nvm_core_init(struct nvm_dev *dev) { struct nvm_id *id = &dev->identity; struct nvm_id_group *grp = &id->groups[0]; + int ret; /* device values */ dev->nr_chnls = grp->num_ch; @@ -522,33 +523,16 @@ static int nvm_core_init(struct nvm_dev *dev) dev->plane_mode = NVM_PLANE_SINGLE; dev->max_rq_size = dev->ops->max_phys_sect * dev->sec_size; - if (grp->mtype != 0) { - pr_err("nvm: memory type not supported\n"); - return -EINVAL; - } - - switch (grp->fmtype) { - case NVM_ID_FMTYPE_SLC: - if (nvm_init_slc_tbl(dev, grp)) - return -ENOMEM; - break; - case NVM_ID_FMTYPE_MLC: - if (nvm_init_mlc_tbl(dev, grp)) - return -ENOMEM; - break; - default: - pr_err("nvm: flash type not supported\n"); - return -EINVAL; - } - - if (!dev->lps_per_blk) - pr_info("nvm: lower page programming table missing\n"); - if (grp->mpos & 0x020202) dev->plane_mode = NVM_PLANE_DOUBLE; if (grp->mpos & 0x040404) dev->plane_mode = NVM_PLANE_QUAD; + if (grp->mtype != 0) { + pr_err("nvm: memory type not supported\n"); + return -EINVAL; + } + /* calculated values */ dev->sec_per_pl = dev->sec_per_pg * dev->nr_planes; dev->sec_per_blk = dev->sec_per_pl * dev->pgs_per_blk; @@ -560,11 +544,34 @@ static int nvm_core_init(struct nvm_dev *dev) sizeof(unsigned long), GFP_KERNEL); if (!dev->lun_map) return -ENOMEM; + + switch (grp->fmtype) { + case NVM_ID_FMTYPE_SLC: + if (nvm_init_slc_tbl(dev, grp)) { + ret = -ENOMEM; + goto err_fmtype; + } + break; + case NVM_ID_FMTYPE_MLC: + if (nvm_init_mlc_tbl(dev, grp)) { + ret = -ENOMEM; + goto err_fmtype; + } + break; + default: + pr_err("nvm: flash type not supported\n"); + ret = -EINVAL; + goto err_fmtype; + } + INIT_LIST_HEAD(&dev->online_targets); mutex_init(&dev->mlock); spin_lock_init(&dev->lock); return 0; +err_fmtype: + kfree(dev->lun_map); + return ret; } static void nvm_free(struct nvm_dev *dev)