Skip to content

Commit 5b230da

Browse files
davejiangSasha Levin
authored andcommitted
cxl: Fix race of nvdimm_bus object when creating nvdimm objects
[ Upstream commit 96a1fd0 ] Found issue during running of cxl-translate.sh unit test. Adding a 3s sleep right before the test seems to make the issue reproduce fairly consistently. The cxl_translate module has dependency on cxl_acpi and causes orphaned nvdimm objects to reprobe after cxl_acpi is removed. The nvdimm_bus object is registered by the cxl_nvb object when cxl_acpi_probe() is called. With the nvdimm_bus object missing, __nd_device_register() will trigger NULL pointer dereference when accessing the dev->parent that points to &nvdimm_bus->dev. [ 192.884510] BUG: kernel NULL pointer dereference, address: 000000000000006c [ 192.895383] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS edk2-20250812-19.fc42 08/12/2025 [ 192.897721] Workqueue: cxl_port cxl_bus_rescan_queue [cxl_core] [ 192.899459] RIP: 0010:kobject_get+0xc/0x90 [ 192.924871] Call Trace: [ 192.925959] <TASK> [ 192.926976] ? pm_runtime_init+0xb9/0xe0 [ 192.929712] __nd_device_register.part.0+0x4d/0xc0 [libnvdimm] [ 192.933314] __nvdimm_create+0x206/0x290 [libnvdimm] [ 192.936662] cxl_nvdimm_probe+0x119/0x1d0 [cxl_pmem] [ 192.940245] cxl_bus_probe+0x1a/0x60 [cxl_core] [ 192.943349] really_probe+0xde/0x380 This patch also relies on the previous change where devm_cxl_add_nvdimm_bridge() is called from drivers/cxl/pmem.c instead of drivers/cxl/core.c to ensure the dependency of cxl_acpi on cxl_pmem. 1. Set probe_type of cxl_nvb to PROBE_FORCE_SYNCHRONOUS to ensure the driver is probed synchronously when add_device() is called. 2. Add a check in __devm_cxl_add_nvdimm_bridge() to ensure that the cxl_nvb driver is attached during cxl_acpi_probe(). 3. Take the cxl_root uport_dev lock and the cxl_nvb->dev lock in devm_cxl_add_nvdimm() before checking nvdimm_bus is valid. 4. Set cxl_nvdimm flag to CXL_NVD_F_INVALIDATED so cxl_nvdimm_probe() will exit with -EBUSY. The removal of cxl_nvdimm devices should prevent any orphaned devices from probing once the nvdimm_bus is gone. [ dj: Fixed 0-day reported kdoc issue. ] [ dj: Fix cxl_nvb reference leak on error. Gregory (kreview-0811365) ] Suggested-by: Dan Williams <dan.j.williams@intel.com> Fixes: 8fdcb17 ("cxl/pmem: Add initial infrastructure for pmem support") Tested-by: Alison Schofield <alison.schofield@intel.com> Reviewed-by: Alison Schofield <alison.schofield@intel.com?> Link: https://patch.msgid.link/20260205001633.1813643-3-dave.jiang@intel.com Signed-off-by: Dave Jiang <dave.jiang@intel.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 4cc3d2f commit 5b230da

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

drivers/cxl/core/pmem.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,15 @@ static void unregister_nvb(void *_cxl_nvb)
115115
device_unregister(&cxl_nvb->dev);
116116
}
117117

118+
static bool cxl_nvdimm_bridge_failed_attach(struct cxl_nvdimm_bridge *cxl_nvb)
119+
{
120+
struct device *dev = &cxl_nvb->dev;
121+
122+
guard(device)(dev);
123+
/* If the device has no driver, then it failed to attach. */
124+
return dev->driver == NULL;
125+
}
126+
118127
struct cxl_nvdimm_bridge *__devm_cxl_add_nvdimm_bridge(struct device *host,
119128
struct cxl_port *port)
120129
{
@@ -138,6 +147,11 @@ struct cxl_nvdimm_bridge *__devm_cxl_add_nvdimm_bridge(struct device *host,
138147
if (rc)
139148
goto err;
140149

150+
if (cxl_nvdimm_bridge_failed_attach(cxl_nvb)) {
151+
unregister_nvb(cxl_nvb);
152+
return ERR_PTR(-ENODEV);
153+
}
154+
141155
rc = devm_add_action_or_reset(host, unregister_nvb, cxl_nvb);
142156
if (rc)
143157
return ERR_PTR(rc);
@@ -247,6 +261,21 @@ int devm_cxl_add_nvdimm(struct cxl_port *parent_port,
247261
if (!cxl_nvb)
248262
return -ENODEV;
249263

264+
/*
265+
* Take the uport_dev lock to guard against race of nvdimm_bus object.
266+
* cxl_acpi_probe() registers the nvdimm_bus and is done under the
267+
* root port uport_dev lock.
268+
*
269+
* Take the cxl_nvb device lock to ensure that cxl_nvb driver is in a
270+
* consistent state. And the driver registers nvdimm_bus.
271+
*/
272+
guard(device)(cxl_nvb->port->uport_dev);
273+
guard(device)(&cxl_nvb->dev);
274+
if (!cxl_nvb->nvdimm_bus) {
275+
rc = -ENODEV;
276+
goto err_alloc;
277+
}
278+
250279
cxl_nvd = cxl_nvdimm_alloc(cxl_nvb, cxlmd);
251280
if (IS_ERR(cxl_nvd)) {
252281
rc = PTR_ERR(cxl_nvd);

drivers/cxl/cxl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,11 +563,16 @@ struct cxl_nvdimm_bridge {
563563

564564
#define CXL_DEV_ID_LEN 19
565565

566+
enum {
567+
CXL_NVD_F_INVALIDATED = 0,
568+
};
569+
566570
struct cxl_nvdimm {
567571
struct device dev;
568572
struct cxl_memdev *cxlmd;
569573
u8 dev_id[CXL_DEV_ID_LEN]; /* for nvdimm, string of 'serial' */
570574
u64 dirty_shutdowns;
575+
unsigned long flags;
571576
};
572577

573578
struct cxl_pmem_region_mapping {

drivers/cxl/pmem.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
static __read_mostly DECLARE_BITMAP(exclusive_cmds, CXL_MEM_COMMAND_ID_MAX);
1515

1616
/**
17-
* __devm_cxl_add_nvdimm_bridge() - add the root of a LIBNVDIMM topology
17+
* devm_cxl_add_nvdimm_bridge() - add the root of a LIBNVDIMM topology
1818
* @host: platform firmware root device
1919
* @port: CXL port at the root of a CXL topology
2020
*
@@ -143,6 +143,9 @@ static int cxl_nvdimm_probe(struct device *dev)
143143
struct nvdimm *nvdimm;
144144
int rc;
145145

146+
if (test_bit(CXL_NVD_F_INVALIDATED, &cxl_nvd->flags))
147+
return -EBUSY;
148+
146149
set_exclusive_cxl_commands(mds, exclusive_cmds);
147150
rc = devm_add_action_or_reset(dev, clear_exclusive, mds);
148151
if (rc)
@@ -323,8 +326,10 @@ static int detach_nvdimm(struct device *dev, void *data)
323326
scoped_guard(device, dev) {
324327
if (dev->driver) {
325328
cxl_nvd = to_cxl_nvdimm(dev);
326-
if (cxl_nvd->cxlmd && cxl_nvd->cxlmd->cxl_nvb == data)
329+
if (cxl_nvd->cxlmd && cxl_nvd->cxlmd->cxl_nvb == data) {
327330
release = true;
331+
set_bit(CXL_NVD_F_INVALIDATED, &cxl_nvd->flags);
332+
}
328333
}
329334
}
330335
if (release)
@@ -367,6 +372,7 @@ static struct cxl_driver cxl_nvdimm_bridge_driver = {
367372
.probe = cxl_nvdimm_bridge_probe,
368373
.id = CXL_DEVICE_NVDIMM_BRIDGE,
369374
.drv = {
375+
.probe_type = PROBE_FORCE_SYNCHRONOUS,
370376
.suppress_bind_attrs = true,
371377
},
372378
};

0 commit comments

Comments
 (0)