From nobody Fri Feb 13 09:31:11 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AE391E7E65A for ; Tue, 26 Sep 2023 18:45:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235585AbjIZSpy (ORCPT ); Tue, 26 Sep 2023 14:45:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235449AbjIZSpu (ORCPT ); Tue, 26 Sep 2023 14:45:50 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D7ED5EB; Tue, 26 Sep 2023 11:45:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1695753943; x=1727289943; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=LryE28J099aJFwLkOHr91UPQ19Da3H3VkYKesB9Y1vo=; b=XpPhiYMxUz2nJUcaeyDQ+/znyXVFw7Cu0vQXyPkQl1Ou5IG4CW1XU2RD OqdkkzPuWYK7Lsn2apQDeJJ20X7PyOl8QI/kDiZ6I00J4TP06Y4FSWSQ4 UCtvz1LMnab8NJZYTtE0EhBzfMmnfFJyPsETqv4CkL4Kzvz71zfAaayu6 63qMmvJSuCRuyZuxaqX5N+fTFf1DKhnTg0niME1lAbzDgtvH31XwJYJUA 6QY/tcQ/8uK/qxSaw57yhIwt1p+JPm/j7ECNClokQ3z7hp31CUuTfIXrT XUuyBu9zotCHHCqDZXlCEU65O9d5/08oASMNS5oyHkp4sWVie/FcqRHtN g==; X-IronPort-AV: E=McAfee;i="6600,9927,10845"; a="378920265" X-IronPort-AV: E=Sophos;i="6.03,178,1694761200"; d="scan'208";a="378920265" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Sep 2023 11:45:34 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10845"; a="752279638" X-IronPort-AV: E=Sophos;i="6.03,178,1694761200"; d="scan'208";a="752279638" Received: from powerlab.fi.intel.com ([10.237.71.25]) by fmsmga007-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Sep 2023 11:45:31 -0700 From: Michal Wilczynski To: linux-acpi@vger.kernel.org, linux-kernel@vger.kernel.org, nvdimm@lists.linux.dev Cc: rafael.j.wysocki@intel.com, andriy.shevchenko@intel.com, lenb@kernel.org, dan.j.williams@intel.com, vishal.l.verma@intel.com, ira.weiny@intel.com, Michal Wilczynski , Andy Shevchenko Subject: [PATCH v1 1/2] ACPI: NFIT: Fix memory leak, and local use of devm_*() Date: Tue, 26 Sep 2023 21:45:19 +0300 Message-ID: <20230926184520.2239723-2-michal.wilczynski@intel.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230926184520.2239723-1-michal.wilczynski@intel.com> References: <20230926184520.2239723-1-michal.wilczynski@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" devm_*() family of functions purpose is managing memory attached to a device. So in general it should only be used for allocations that should last for the whole lifecycle of the device. This is not the case for acpi_nfit_init_interleave_set(). There are two allocations that are only used locally in this function. What's more - if the function exits on error path memory is never freed. It's still attached to dev and would be freed on device detach, so this leak could be called a 'local leak'. Fix this by switching from devm_kcalloc() to kcalloc(), and adding proper rollback. Fixes: eaf961536e16 ("libnvdimm, nfit: add interleave-set state-tracking in= frastructure") Reported-by: Andy Shevchenko Signed-off-by: Michal Wilczynski Reviewed-by: Andy Shevchenko Reviewed-by: Dave Jiang --- drivers/acpi/nfit/core.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/drivers/acpi/nfit/core.c b/drivers/acpi/nfit/core.c index f0e6738ae3c9..78f0f855c4de 100644 --- a/drivers/acpi/nfit/core.c +++ b/drivers/acpi/nfit/core.c @@ -2262,6 +2262,7 @@ static int acpi_nfit_init_interleave_set(struct acpi_= nfit_desc *acpi_desc, u16 nr =3D ndr_desc->num_mappings; struct nfit_set_info2 *info2; struct nfit_set_info *info; + int err =3D 0; int i; =20 nd_set =3D devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL); @@ -2269,13 +2270,15 @@ static int acpi_nfit_init_interleave_set(struct acp= i_nfit_desc *acpi_desc, return -ENOMEM; import_guid(&nd_set->type_guid, spa->range_guid); =20 - info =3D devm_kcalloc(dev, nr, sizeof(*info), GFP_KERNEL); + info =3D kcalloc(nr, sizeof(*info), GFP_KERNEL); if (!info) return -ENOMEM; =20 - info2 =3D devm_kcalloc(dev, nr, sizeof(*info2), GFP_KERNEL); - if (!info2) - return -ENOMEM; + info2 =3D kcalloc(nr, sizeof(*info2), GFP_KERNEL); + if (!info2) { + err =3D -ENOMEM; + goto free_info; + } =20 for (i =3D 0; i < nr; i++) { struct nd_mapping_desc *mapping =3D &ndr_desc->mapping[i]; @@ -2289,7 +2292,8 @@ static int acpi_nfit_init_interleave_set(struct acpi_= nfit_desc *acpi_desc, =20 if (!memdev || !nfit_mem->dcr) { dev_err(dev, "%s: failed to find DCR\n", __func__); - return -ENODEV; + err =3D -ENODEV; + goto free_info2; } =20 map->region_offset =3D memdev->region_offset; @@ -2337,10 +2341,13 @@ static int acpi_nfit_init_interleave_set(struct acp= i_nfit_desc *acpi_desc, } =20 ndr_desc->nd_set =3D nd_set; - devm_kfree(dev, info); - devm_kfree(dev, info2); =20 - return 0; +free_info2: + kfree(info2); +free_info: + kfree(info); + + return err; } =20 static int ars_get_cap(struct acpi_nfit_desc *acpi_desc, --=20 2.41.0 From nobody Fri Feb 13 09:31:11 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3DAAAE7E657 for ; Tue, 26 Sep 2023 18:45:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235583AbjIZSp4 (ORCPT ); Tue, 26 Sep 2023 14:45:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46392 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235576AbjIZSpv (ORCPT ); Tue, 26 Sep 2023 14:45:51 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EB4B911F; Tue, 26 Sep 2023 11:45:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1695753943; x=1727289943; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=UsIV3Q3gcUAc21rNqcq+g99sj4f1HzfeIQJnfu00KFE=; b=aCdntd0tivFkxDS9b5DH2iVc0G0cRPyBa8PhfEFalxHJkRknJ5s0gvjx img5RTQhfHysyg8Y+wmEj9UyN6iHsiW0XpyfuSuoOvjBBp9mTdtcFzeb0 Ko182z3tkAMKVCsQsGYA5zFJBq/0Gv/VK/AHQzlU9d4mU2a71n+wV8Xcw I0cTdD8iQhtzR93tDdCbUqch1Nh61YXlgM+WclLFInaelnpXOwzWzvGbP HCYGD4OPy3zPOZlsnKnOwUpV9VkFpWHNfhMZEaji1MVAe6fyPLpNYs/B/ 1Xfzg0rsGxZJTO/PbU0AT78JBSqLfhXC67hu4qgXw+qiKj0RhtKauTabR w==; X-IronPort-AV: E=McAfee;i="6600,9927,10845"; a="378920271" X-IronPort-AV: E=Sophos;i="6.03,178,1694761200"; d="scan'208";a="378920271" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Sep 2023 11:45:37 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10845"; a="752279646" X-IronPort-AV: E=Sophos;i="6.03,178,1694761200"; d="scan'208";a="752279646" Received: from powerlab.fi.intel.com ([10.237.71.25]) by fmsmga007-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Sep 2023 11:45:34 -0700 From: Michal Wilczynski To: linux-acpi@vger.kernel.org, linux-kernel@vger.kernel.org, nvdimm@lists.linux.dev Cc: rafael.j.wysocki@intel.com, andriy.shevchenko@intel.com, lenb@kernel.org, dan.j.williams@intel.com, vishal.l.verma@intel.com, ira.weiny@intel.com, Michal Wilczynski , Dave Jiang , Andy Shevchenko Subject: [PATCH v1 2/2] ACPI: NFIT: Use modern scope based rollback Date: Tue, 26 Sep 2023 21:45:20 +0300 Message-ID: <20230926184520.2239723-3-michal.wilczynski@intel.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230926184520.2239723-1-michal.wilczynski@intel.com> References: <20230926184520.2239723-1-michal.wilczynski@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Change rollback in acpi_nfit_init_interleave_set() to use modern scope based attribute __free(). This is similar to C++ RAII and is a preferred way for handling local memory allocations. Suggested-by: Dave Jiang Suggested-by: Andy Shevchenko Signed-off-by: Michal Wilczynski Reviewed-by: Andy Shevchenko Reviewed-by: Dave Jiang --- drivers/acpi/nfit/core.c | 32 ++++++++++---------------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/drivers/acpi/nfit/core.c b/drivers/acpi/nfit/core.c index 78f0f855c4de..079bd663495f 100644 --- a/drivers/acpi/nfit/core.c +++ b/drivers/acpi/nfit/core.c @@ -2257,29 +2257,23 @@ static int acpi_nfit_init_interleave_set(struct acp= i_nfit_desc *acpi_desc, struct nd_region_desc *ndr_desc, struct acpi_nfit_system_address *spa) { + u16 nr =3D ndr_desc->num_mappings; + struct nfit_set_info2 *info2 __free(kfree) =3D + kcalloc(nr, sizeof(*info2), GFP_KERNEL); + struct nfit_set_info *info __free(kfree) =3D + kcalloc(nr, sizeof(*info), GFP_KERNEL); struct device *dev =3D acpi_desc->dev; struct nd_interleave_set *nd_set; - u16 nr =3D ndr_desc->num_mappings; - struct nfit_set_info2 *info2; - struct nfit_set_info *info; - int err =3D 0; int i; =20 + if (!info || !info2) + return -ENOMEM; + nd_set =3D devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL); if (!nd_set) return -ENOMEM; import_guid(&nd_set->type_guid, spa->range_guid); =20 - info =3D kcalloc(nr, sizeof(*info), GFP_KERNEL); - if (!info) - return -ENOMEM; - - info2 =3D kcalloc(nr, sizeof(*info2), GFP_KERNEL); - if (!info2) { - err =3D -ENOMEM; - goto free_info; - } - for (i =3D 0; i < nr; i++) { struct nd_mapping_desc *mapping =3D &ndr_desc->mapping[i]; struct nvdimm *nvdimm =3D mapping->nvdimm; @@ -2292,8 +2286,7 @@ static int acpi_nfit_init_interleave_set(struct acpi_= nfit_desc *acpi_desc, =20 if (!memdev || !nfit_mem->dcr) { dev_err(dev, "%s: failed to find DCR\n", __func__); - err =3D -ENODEV; - goto free_info2; + return -ENODEV; } =20 map->region_offset =3D memdev->region_offset; @@ -2342,12 +2335,7 @@ static int acpi_nfit_init_interleave_set(struct acpi= _nfit_desc *acpi_desc, =20 ndr_desc->nd_set =3D nd_set; =20 -free_info2: - kfree(info2); -free_info: - kfree(info); - - return err; + return 0; } =20 static int ars_get_cap(struct acpi_nfit_desc *acpi_desc, --=20 2.41.0