All of lore.kernel.org
 help / color / mirror / Atom feed
From: Suseendran Bhaskaran <suseendranb@google.com>
To: Andrew Morton <akpm@linux-foundation.org>,
	Mike Rapoport <rppt@kernel.org>,  Dan Williams <djbw@kernel.org>,
	David Hildenbrand <david@kernel.org>
Cc: Alexander Graf <graf@amazon.com>,
	Alison Schofield <alison.schofield@intel.com>,
	 Andy Lutomirski <luto@kernel.org>,
	Borislav Petkov <bp@alien8.de>,
	 Catalin Marinas <catalin.marinas@arm.com>,
	Changyuan Lyu <changyuanl@google.com>,
	 Dave Hansen <dave.hansen@linux.intel.com>,
	Dave Jiang <dave.jiang@intel.com>,
	 David Rientjes <rientjes@google.com>,
	Frank van der Linden <fvdl@google.com>,
	"H. Peter Anvin" <hpa@zytor.com>,  Ingo Molnar <mingo@redhat.com>,
	Ira Weiny <iweiny@kernel.org>, Jonathan Corbet <corbet@lwn.net>,
	 "Liam R. Howlett" <liam@infradead.org>,
	Mark Rutland <mark.rutland@arm.com>,
	 Michal Hocko <mhocko@suse.com>,
	Oscar Salvador <osalvador@suse.de>,
	 Pasha Tatashin <pasha.tatashin@soleen.com>,
	Peter Zijlstra <peterz@infradead.org>,
	 Pratyush Yadav <pratyush@kernel.org>,
	Randy Dunlap <rdunlap@infradead.org>,
	 Shuah Khan <skhan@linuxfoundation.org>,
	Thomas Gleixner <tglx@kernel.org>,
	 Vishal Verma <vishal.l.verma@intel.com>,
	Vlastimil Babka <vbabka@kernel.org>,
	 Will Deacon <will@kernel.org>,
	kexec@lists.infradead.org,  linux-arm-kernel@lists.infradead.org,
	linux-cxl@vger.kernel.org,  linux-doc@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-mm@kvack.org,
	 nvdimm@lists.linux.dev, x86@kernel.org,
	 Suseendran Bhaskaran <suseendranb@google.com>
Subject: [RFC PATCH 07/11] nvdimm/ramdax: manage bus and DIMM allocations with devres
Date: Wed,  2 Dec 2026 00:19:42 +0000	[thread overview]
Message-ID: <20261202001946.1226273-8-suseendranb@google.com> (raw)
In-Reply-To: <20261202001946.1226273-1-suseendranb@google.com>

ramdax_probe() inherited a static nvdimm_bus_descriptor from e820_pmem,
sharing a single descriptor across all probed pmem-region platform
devices (and omitting nd_desc->of_node). In addition,
ramdax_register_dimm() allocates dimm, nd_set, and the memremap()'d
label area without freeing or unmapping them in ramdax_remove() or when
a multi-resource probe fails partway through.

Allocate nd_desc, dimm, and nd_set with devm_kzalloc_obj() and map
dimm->label_area with devm_memremap() so each device owns its bus
descriptor and devres releases DIMM and region state after
nvdimm_bus_unregister() on unbind or failed probe.

Fixes: 43bc0aa19a21 ("nvdimm: allow exposing RAM carveouts as NVDIMM DIMM devices")
Signed-off-by: Suseendran Bhaskaran <suseendranb@google.com>
---
 drivers/nvdimm/ramdax.c | 123 ++++++++++++++--------------------------
 1 file changed, 44 insertions(+), 79 deletions(-)

diff --git a/drivers/nvdimm/ramdax.c b/drivers/nvdimm/ramdax.c
index e76fca736d76..cca470d071eb 100644
--- a/drivers/nvdimm/ramdax.c
+++ b/drivers/nvdimm/ramdax.c
@@ -11,7 +11,7 @@
 #include <linux/libnvdimm.h>
 #include <linux/module.h>
 #include <linux/numa.h>
-#include <linux/slab.h>
+#include <linux/err.h>
 #include <linux/io.h>
 #include <linux/of.h>
 
@@ -31,16 +31,16 @@ static void ramdax_remove(struct platform_device *pdev)
 	nvdimm_bus_unregister(nvdimm_bus);
 }
 
-static int ramdax_register_region(struct resource *res,
-		struct nvdimm *nvdimm,
-		struct nvdimm_bus *nvdimm_bus)
+static int ramdax_register_region(struct device *dev, struct resource *res,
+				  struct nvdimm *nvdimm,
+				  struct nvdimm_bus *nvdimm_bus)
 {
 	struct nd_mapping_desc mapping;
 	struct nd_region_desc ndr_desc;
 	struct nd_interleave_set *nd_set;
 	int nid = phys_to_target_node(res->start);
 
-	nd_set = kzalloc_obj(*nd_set);
+	nd_set = devm_kzalloc_obj(dev, *nd_set);
 	if (!nd_set)
 		return -ENOMEM;
 
@@ -62,34 +62,28 @@ static int ramdax_register_region(struct resource *res,
 	ndr_desc.nd_set = nd_set;
 
 	if (!nvdimm_pmem_region_create(nvdimm_bus, &ndr_desc))
-		goto err_free_nd_set;
+		return -ENXIO;
 
 	return 0;
-
-err_free_nd_set:
-	kfree(nd_set);
-	return -ENXIO;
 }
 
 static int ramdax_register_dimm(struct resource *res, void *data)
 {
+	struct device *dev = data;
+	struct nvdimm_bus *nvdimm_bus = dev_get_drvdata(dev);
 	resource_size_t start = res->start;
 	resource_size_t size = resource_size(res);
 	unsigned long flags = 0, cmd_mask = 0;
-	struct nvdimm_bus *nvdimm_bus = data;
 	struct ramdax_dimm *dimm;
-	int err;
 
-	dimm = kzalloc_obj(*dimm);
+	dimm = devm_kzalloc_obj(dev, *dimm);
 	if (!dimm)
 		return -ENOMEM;
 
-	dimm->label_area = memremap(start + size - LABEL_AREA_SIZE,
-				    LABEL_AREA_SIZE, MEMREMAP_WB);
-	if (!dimm->label_area) {
-		err = -ENOMEM;
-		goto err_free_dimm;
-	}
+	dimm->label_area = devm_memremap(dev, start + size - LABEL_AREA_SIZE,
+					 LABEL_AREA_SIZE, MEMREMAP_WB);
+	if (IS_ERR(dimm->label_area))
+		return PTR_ERR(dimm->label_area);
 
 	set_bit(NDD_LABELING, &flags);
 	set_bit(NDD_REGISTER_SYNC, &flags);
@@ -99,24 +93,10 @@ static int ramdax_register_dimm(struct resource *res, void *data)
 	dimm->nvdimm = nvdimm_create(nvdimm_bus, dimm,
 				     /* dimm_attribute_groups */ NULL,
 				     flags, cmd_mask, 0, NULL);
-	if (!dimm->nvdimm) {
-		err = -ENOMEM;
-		goto err_unmap_label;
-	}
-
-	err = ramdax_register_region(res, dimm->nvdimm, nvdimm_bus);
-	if (err)
-		goto err_remove_nvdimm;
-
-	return 0;
+	if (!dimm->nvdimm)
+		return -ENOMEM;
 
-err_remove_nvdimm:
-	nvdimm_delete(dimm->nvdimm);
-err_unmap_label:
-	memunmap(dimm->label_area);
-err_free_dimm:
-	kfree(dimm);
-	return err;
+	return ramdax_register_region(dev, res, dimm->nvdimm, nvdimm_bus);
 }
 
 static int ramdax_get_config_size(struct nvdimm *nvdimm, int buf_len,
@@ -210,60 +190,45 @@ static const struct of_device_id ramdax_of_matches[] = {
 };
 #endif
 
-static int ramdax_probe_of(struct platform_device *pdev,
-		struct nvdimm_bus *bus, struct device_node *np)
-{
-	int err;
-
-	if (!of_match_node(ramdax_of_matches, np))
-		return -ENODEV;
-
-	for (int i = 0; i < pdev->num_resources; i++) {
-		err = ramdax_register_dimm(&pdev->resource[i], bus);
-		if (err)
-			goto err_unregister;
-	}
-
-	return 0;
-
-err_unregister:
-	/*
-	 * FIXME: should we unregister the dimms that were registered
-	 * successfully
-	 */
-	return err;
-}
-
 static int ramdax_probe(struct platform_device *pdev)
 {
-	static struct nvdimm_bus_descriptor nd_desc;
 	struct device *dev = &pdev->dev;
+	struct device_node *np = dev_of_node(dev);
+	struct nvdimm_bus_descriptor *nd_desc;
 	struct nvdimm_bus *nvdimm_bus;
-	struct device_node *np;
-	int rc = -ENXIO;
+	int rc = -ENODEV;
+
+	if (np && !of_match_node(ramdax_of_matches, np))
+		return -ENODEV;
+
+	nd_desc = devm_kzalloc_obj(dev, *nd_desc);
+	if (!nd_desc)
+		return -ENOMEM;
 
-	nd_desc.provider_name = "ramdax";
-	nd_desc.module = THIS_MODULE;
-	nd_desc.ndctl = ramdax_ctl;
-	nvdimm_bus = nvdimm_bus_register(dev, &nd_desc);
+	nd_desc->provider_name = "ramdax";
+	nd_desc->module = THIS_MODULE;
+	nd_desc->of_node = np;
+	nd_desc->ndctl = ramdax_ctl;
+	nvdimm_bus = nvdimm_bus_register(dev, nd_desc);
 	if (!nvdimm_bus)
-		goto err;
+		return -ENXIO;
 
-	np = dev_of_node(&pdev->dev);
-	if (np)
-		rc = ramdax_probe_of(pdev, nvdimm_bus, np);
-	else
+	platform_set_drvdata(pdev, nvdimm_bus);
+
+	if (np) {
+		for (unsigned int i = 0; i < pdev->num_resources; i++) {
+			rc = ramdax_register_dimm(&pdev->resource[i], dev);
+			if (rc)
+				break;
+		}
+	} else {
 		rc = walk_iomem_res_desc(IORES_DESC_PERSISTENT_MEMORY_LEGACY,
-					 IORESOURCE_MEM, 0, -1, nvdimm_bus,
+					 IORESOURCE_MEM, 0, -1, dev,
 					 ramdax_register_dimm);
+	}
 	if (rc)
-		goto err;
+		nvdimm_bus_unregister(nvdimm_bus);
 
-	platform_set_drvdata(pdev, nvdimm_bus);
-
-	return 0;
-err:
-	nvdimm_bus_unregister(nvdimm_bus);
 	return rc;
 }
 

  parent reply	other threads:[~2026-12-02  0:20 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  0:19 [RFC PATCH 00/11] mm: NUMA-aware boot memory carving (extmem) Suseendran Bhaskaran
2026-12-02  0:19 ` [RFC PATCH 01/11] mm/cma: clamp multi-range search to 4G and honor memblock flags Suseendran Bhaskaran
2026-12-02  0:19 ` [RFC PATCH 02/11] mm/memblock: factor out multi-range contiguous reservation helper from CMA Suseendran Bhaskaran
2026-12-02  0:19 ` [RFC PATCH 03/11] x86/mm: add early direct-map unmap helpers for boot carveouts Suseendran Bhaskaran
2026-12-02  0:19 ` [RFC PATCH 04/11] arm64/mm: support unmapping boot carveouts from the linear map Suseendran Bhaskaran
2026-12-02  0:19 ` [RFC PATCH 05/11] mm/extmem: add architecture-neutral external memory subsystem Suseendran Bhaskaran
2026-12-02  0:19 ` [RFC PATCH 06/11] dax/hmem: bind extmem platform devices with per-region alignment Suseendran Bhaskaran
2026-12-02  0:19 ` Suseendran Bhaskaran [this message]
2026-12-02  0:19 ` [RFC PATCH 08/11] nvdimm/ramdax: harden probe checks and select NUMA_KEEP_MEMINFO Suseendran Bhaskaran
2026-12-02  0:19 ` [RFC PATCH 09/11] nvdimm/ramdax: support binding extmem platform devices Suseendran Bhaskaran
2026-12-02  0:19 ` [RFC PATCH 10/11] x86/kaslr: avoid placing kernel in carvable high memory when extmem= is set Suseendran Bhaskaran
2026-12-02  0:19 ` [RFC PATCH 11/11] mm/extmem: preserve carved regions across KHO kexec Suseendran Bhaskaran
2026-12-02 14:45 ` [RFC PATCH 00/11] mm: NUMA-aware boot memory carving (extmem) Mike Rapoport
2026-12-02 15:10   ` David Hildenbrand (Arm)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261202001946.1226273-8-suseendranb@google.com \
    --to=suseendranb@google.com \
    --cc=akpm@linux-foundation.org \
    --cc=alison.schofield@intel.com \
    --cc=bp@alien8.de \
    --cc=catalin.marinas@arm.com \
    --cc=changyuanl@google.com \
    --cc=corbet@lwn.net \
    --cc=dave.hansen@linux.intel.com \
    --cc=dave.jiang@intel.com \
    --cc=david@kernel.org \
    --cc=djbw@kernel.org \
    --cc=fvdl@google.com \
    --cc=graf@amazon.com \
    --cc=hpa@zytor.com \
    --cc=iweiny@kernel.org \
    --cc=kexec@lists.infradead.org \
    --cc=liam@infradead.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-cxl@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=luto@kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mhocko@suse.com \
    --cc=mingo@redhat.com \
    --cc=nvdimm@lists.linux.dev \
    --cc=osalvador@suse.de \
    --cc=pasha.tatashin@soleen.com \
    --cc=peterz@infradead.org \
    --cc=pratyush@kernel.org \
    --cc=rdunlap@infradead.org \
    --cc=rientjes@google.com \
    --cc=rppt@kernel.org \
    --cc=skhan@linuxfoundation.org \
    --cc=tglx@kernel.org \
    --cc=vbabka@kernel.org \
    --cc=vishal.l.verma@intel.com \
    --cc=will@kernel.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.