Fix port enumeration failure
Li Ming
reviewerv3 cover Fix port enumeration failure
I ran CXL mock testing with next branch, I usually hit the following call trace.
Oops: general protection fault, probably for non-canonical address 0xdffffc0000000092: 0000 [#1] SMP KASAN NOPTI KASAN: null-ptr-deref in range [0x0000000000000490-0x0000000000000497] CPU: 3 UID: 0 PID: 42 Comm: kworker/u16:1 Tainted: G O J 6.19.0-rc5-cxl+ #4 PREEMPT(voluntary) Tainted: [O]=OOT_MODULE, [J]=FWCTL Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS rel-1.17.0-0-gb52ca86e094d-prebuilt.qemu.org 04/01/2014 Workqueue: async async_run_entry_fn RIP: 0010:cxl_dpa_to_region+0x105/0x1f0 [cxl_core] Call Trace: <TASK> cxl_event_trace_record+0xd1/0xa70 [cxl_core] __cxl_event_trace_record+0x12f/0x1e0 [cxl_core] cxl_mem_get_records_log+0x261/0x500 [cxl_core] cxl_mem_get_event_records+0x7c/0xc0 [cxl_core] cxl_mock_mem_probe+0xd38/0x1c60 [cxl_mock_mem] platform_probe+0x9d/0x130 really_probe+0x1c8/0x960 driver_probe_device+0x45/0x120 __device_attach_driver+0x15d/0x280 bus_for_each_drv+0x100/0x180 __device_attach_async_helper+0x199/0x250 async_run_entry_fn+0x95/0x430 process_one_work+0x7db/0x1940
After detailed debugging, I identified adding dport failure leads to the problem.
What I observed is when two memdev were trying to enumerate a same port, the first memdev was responsible for port creation and bind it to the cxl port driver. However, there is a small window between the point where the new port becomes visible(after being added to the device list of cxl bus) and when it is bound to the port driver. During this window, the second memdev may discover the port and acquire its lock while attempting to add its dport, which blocks bus_probe_device() inside device_add(). As a result, the second memdev observes the port as unbound and fails to add its dport. The second memdev->endpoint would not be updated because of that, then trigger above trace.
The solution is to fix this race by holding the host lock of the target port during dport addition, preventing premature access before driver binding completed.
base-commit: 63fbf275fa9f18f7020fb8acf54fa107e51d0f23 cxl/next
Changes from V2:
- Split to_port_host() implementation to a lead-in patch. (Dan)
- Use to_port_host() instead of open coded. (Dan)
- Rename to_port_host() to port_to_host() to align with dport_to_host().
Changes from V1:
- Remove the patch of initializing memdev->endpoint to NULL. (Dan)
- Fixes typo errors. (Jonathan)
- Introduce a helper called to_port_host().
- unregister_port() cleanup.
---
Li Ming (3):
cxl/port: Introduce port_to_host() helper cxl/port: Hold port host lock during dport adding. cxl/port: Use port_to_host() to get port host
drivers/cxl/core/core.h | 18 +++++++++++++++++ drivers/cxl/core/port.c | 52 +++++++++++++++++-------------------------------- 2 files changed, 36 insertions(+), 34 deletions(-)
---
base-commit: 63fbf275fa9f18f7020fb8acf54fa107e51d0f23
change-id: 20260208-fix-port-enumeration-failure-34e1f4953f02
Best regards,
Signature · 1 line
Li Ming <[email protected]>
Li Ming
reviewerPATCH v3 1/3 cxl/port: Introduce port_to_host() helper
In CXL subsystem, a port has its own host device for the port creation and removal. The host of CXL root and all the first level ports is the platform firmware device, the host of other ports is their parent port's device. Create this new helper to much easier to get the host of a cxl port.
A dport_to_host() helper has been introduced, use port_to_host() as the name to align with this naming convention.
--- drivers/cxl/core/core.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
drivers/cxl/core/core.h +18 -0
@@ -152,6 +152,24 @@ int cxl_pci_get_bandwidth(struct pci_dev *pdev, struct access_coordinate *c);int cxl_port_get_switch_dport_bandwidth (struct cxl_port *port,struct access_coordinate *c); +static inline struct device *port_to_host (struct cxl_port *port) +{ +struct cxl_port *parent =is_cxl_root (port) ?NULL : +to_cxl_port (port->dev.parent); + +/* +* The host of CXL root port and the first level of ports is +* the platform firmware device, the host of all other ports +* is their parent port. +*/ +if (!parent) +return port->uport_dev; +else if (is_cxl_root (parent)) +return parent->uport_dev; +else +return &parent->dev; +} +static inline struct device *dport_to_host (struct cxl_dport *dport) {struct cxl_port *port = dport->port;
Signature · 1 line
2.43.0
Li Ming
reviewerPATCH v3 2/3 cxl/port: Hold port host lock during dport adding.
In reply to Li MingCXL testing environment can trigger following trace
Oops: general protection fault, probably for non-canonical address 0xdffffc0000000092: 0000 [#1] SMP KASAN NOPTI KASAN: null-ptr-deref in range [0x0000000000000490-0x0000000000000497] RIP: 0010:cxl_dpa_to_region+0x105/0x1f0 [cxl_core] Call Trace: <TASK> cxl_event_trace_record+0xd1/0xa70 [cxl_core] __cxl_event_trace_record+0x12f/0x1e0 [cxl_core] cxl_mem_get_records_log+0x261/0x500 [cxl_core] cxl_mem_get_event_records+0x7c/0xc0 [cxl_core] cxl_mock_mem_probe+0xd38/0x1c60 [cxl_mock_mem] platform_probe+0x9d/0x130 really_probe+0x1c8/0x960 __driver_probe_device+0x187/0x3e0 driver_probe_device+0x45/0x120 __device_attach_driver+0x15d/0x280
When CXL subsystem adds a cxl port to a hierarchy, there is a small window where the new port becomes visible before it is bound to a driver. This happens because device_add() adds a device to bus device list before bus_probe_device() binds it to a driver.
So if two cxl memdevs are trying to add a dport to a same port via devm_cxl_enumerate_ports(), the second cxl memdev may observe the port and attempt to add a dport, but fails because the port has not yet been attached to cxl port driver. That causes the memdev->endpoint can not be updated.
The sequence is like:
CPU 0 CPU 1devm_cxl_enumerate_ports()
# port not found, add it
add_port_attach_ep()
# hold the parent port lock
# to add the new port
devm_cxl_create_port()
device_add()
# Add dev to bus devs list
bus_add_device()
devm_cxl_enumerate_ports()
# found the port
find_cxl_port_by_uport()
# hold port lock to add a dport
device_lock(the port)
find_or_add_dport()
cxl_port_add_dport()
return -ENXIO because port->dev.driver is NULL
device_unlock(the port)
bus_probe_device()
# hold the port lock
# for attaching
device_lock(the port)
attaching the new port
device_unlock(the port)To fix this race, require that dport addition holds the host lock of the target port(the host of CXL root and all cxl host bridge ports is the platform firmware device, the host of all other ports is their parent port). The CXL subsystem already requires holding the host lock while attaching a new port. Therefore, successfully acquiring the host lock guarantees that port attaching has completed.
--- drivers/cxl/core/port.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-)
drivers/cxl/core/port.c +15 -8
@@ -1790,7 +1790,16 @@ static struct cxl_dport *find_or_add_dport(struct cxl_port *port, {struct cxl_dport *dport; -device_lock_assert (&port->dev); +/* +* The port is already visible in CXL hierarchy, but it may still +* be in the process of binding to the CXL port driver at this point. +* +* port creation and driver binding are protected by the port's host +* lock, so acquire the host lock here to ensure the port has completed +* driver binding before proceeding with dport addition. +*/ +guard (device)(port_to_host (port)); +guard (device)(&port->dev); dport =cxl_find_dport_by_dev (port, dport_dev);if (!dport) { dport =probe_dport (port, dport_dev);
@@ -1857,13 +1866,11 @@ int devm_cxl_enumerate_ports(struct cxl_memdev *cxlmd)* RP port enumerated by cxl_acpi without dport will * have the dport added here. */ - scoped_guard(device, &port->dev) { - dport =find_or_add_dport (port, dport_dev); -if (IS_ERR (dport)) { -if (PTR_ERR (dport) == -EAGAIN ) - goto retry; -return PTR_ERR (dport); - } + dport =find_or_add_dport (port, dport_dev); +if (IS_ERR (dport)) { +if (PTR_ERR (dport) == -EAGAIN ) + goto retry; +return PTR_ERR (dport); } rc =cxl_add_ep (dport, &cxlmd->dev);
Signature · 1 line
2.43.0
Li Ming
reviewerPATCH v3 3/3 cxl/port: Use port_to_host() to get port host
In reply to Li Mingport_to_host() has been introduced, use it to replace all places where using open coded to get the host of a port.
Remove endpoint_host() as its functionality can be replaced by port_to_host().
--- drivers/cxl/core/port.c | 29 +++-------------------------- 1 file changed, 3 insertions(+), 26 deletions(-)
drivers/cxl/core/port.c +3 -26
@@ -615,22 +615,8 @@ struct cxl_port *parent_port_of(struct cxl_port *port)static void unregister_port (void *_port) {struct cxl_port *port = _port; -struct cxl_port *parent =parent_port_of (port); -struct device *lock_dev; -/* -* CXL root port's and the first level of ports are unregistered -* under the platform firmware device lock, all other ports are -* unregistered while holding their parent port lock. -*/ -if (!parent) - lock_dev = port->uport_dev; -else if (is_cxl_root (parent)) - lock_dev = parent->uport_dev; -else - lock_dev = &parent->dev; - -device_lock_assert (lock_dev); +device_lock_assert (port_to_host (port)); port->dead = true;device_unregister (&port->dev); }
@@ -1427,20 +1413,11 @@ static struct device *grandparent(struct device *dev)return NULL ; } -static struct device *endpoint_host (struct cxl_port *endpoint) -{ -struct cxl_port *port =to_cxl_port (endpoint->dev.parent); - -if (is_cxl_root (port)) -return port->uport_dev; -return &port->dev; -} -static void delete_endpoint (void *data) {struct cxl_memdev *cxlmd = data;struct cxl_port *endpoint = cxlmd->endpoint; -struct device *host =endpoint_host (endpoint); +struct device *host =port_to_host (endpoint); scoped_guard(device, host) {if (host->driver && !endpoint->dead) {
@@ -1456,7 +1433,7 @@ static void delete_endpoint(void *data)int cxl_endpoint_autoremove (struct cxl_memdev *cxlmd,struct cxl_port *endpoint) { -struct device *host =endpoint_host (endpoint); +struct device *host =port_to_host (endpoint);struct device *dev = &cxlmd->dev;get_device (host);
Signature · 1 line
2.43.0
Quoted 2 lines · Li Ming
I ran CXL mock testing with next branch, I usually hit the following call trace.
[..]
Quoted 18 lines
Changes from V2:
- Splitto_port_host()implementation to a lead-in patch. (Dan)
- Useto_port_host()instead of open coded. (Dan)
- Renameto_port_host()toport_to_host()to align withdport_to_host().Changes from V1:
- Remove the patch of initializingmemdev->endpointto NULL. (Dan)
- Fixes typo errors. (Jonathan)
- Introduce a helper calledto_port_host().
-unregister_port()cleanup.Signed-off-by Li Ming <[email protected]>---
Li Ming (3):
cxl/port: Introduceport_to_host()helper cxl/port: Hold port host lock during dport adding. cxl/port: Useport_to_host()to get port host
Quoted 6 lines
drivers/cxl/core/core.h | 18 +++++++++++++++++ drivers/cxl/core/port.c | 52 +++++++++++++++++-------------------------------- 2 files changed, 36 insertions(+), 34 deletions(-)---
base-commit:63fbf275fa9f18f7020fb8acf54fa107e51d0f23
change-id: 20260208-fix-port-enumeration-failure-34e1f4953f02
For the series:
...but I was expecting patch1 and patch3 to be squashed. Dave can do that on applying, or leave it as is.
Quoted 2 lines · Li Ming
I ran CXL mock testing with next branch, I usually hit the following call trace.
Hi Ming,
Without a crisp reproducer, I just beat this up and it ran beyond what it had 'before this patch'. It survived 100 --suite=cxl and 500 module reloads.
For the series:
snip
Quoted 2 lines · Li Ming
I ran CXL mock testing with next branch, I usually hit the following call trace.
Applied series to cxl/fixes. I squashed patch 1 and 3 together. 0066688dbcdc cxl/port: Hold port host lock during dport adding. 822655e6751d cxl/port: Introduce port_to_host() helper
Quoted 69 lines
Oops: general protection fault, probably for non-canonical address 0xdffffc0000000092: 0000 [#1] SMP KASAN NOPTI KASAN: null-ptr-deref in range [0x0000000000000490-0x0000000000000497] CPU: 3 UID: 0 PID: 42 Comm: kworker/u16:1 Tainted: G O J 6.19.0-rc5-cxl+ #4 PREEMPT(voluntary) Tainted: [O]=OOT_MODULE, [J]=FWCTL Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS rel-1.17.0-0-gb52ca86e094d-prebuilt.qemu.org 04/01/2014 Workqueue: async async_run_entry_fn RIP: 0010:cxl_dpa_to_region+0x105/0x1f0 [cxl_core] Call Trace: <TASK> cxl_event_trace_record+0xd1/0xa70 [cxl_core] __cxl_event_trace_record+0x12f/0x1e0 [cxl_core] cxl_mem_get_records_log+0x261/0x500 [cxl_core] cxl_mem_get_event_records+0x7c/0xc0 [cxl_core] cxl_mock_mem_probe+0xd38/0x1c60 [cxl_mock_mem] platform_probe+0x9d/0x130 really_probe+0x1c8/0x960 driver_probe_device+0x45/0x120 __device_attach_driver+0x15d/0x280 bus_for_each_drv+0x100/0x180 __device_attach_async_helper+0x199/0x250 async_run_entry_fn+0x95/0x430 process_one_work+0x7db/0x1940After detailed debugging, I identified adding dport failure leads to the problem.
What I observed is when two memdev were trying to enumerate a same port, the first memdev was responsible for port creation and bind it to the cxl port driver. However, there is a small window between the point where the new port becomes visible(after being added to the device list of cxl bus) and when it is bound to the port driver. During this window, the second memdev may discover the port and acquire its lock while attempting to add its dport, which blocksbus_probe_device()insidedevice_add(). As a result, the second memdev observes the port as unbound and fails to add its dport. The secondmemdev->endpointwould not be updated because of that, then trigger above trace.The solution is to fix this race by holding the host lock of the target port during dport addition, preventing premature access before driver binding completed.
base-commit:
63fbf275fa9f18f7020fb8acf54fa107e51d0f23cxl/nextChanges from V2:
- Splitto_port_host()implementation to a lead-in patch. (Dan)
- Useto_port_host()instead of open coded. (Dan)
- Renameto_port_host()toport_to_host()to align withdport_to_host().Changes from V1:
- Remove the patch of initializingmemdev->endpointto NULL. (Dan)
- Fixes typo errors. (Jonathan)
- Introduce a helper calledto_port_host().
-unregister_port()cleanup.Signed-off-by Li Ming <[email protected]>---
Li Ming (3):
cxl/port: Introduceport_to_host()helper cxl/port: Hold port host lock during dport adding. cxl/port: Useport_to_host()to get port hostdrivers/cxl/core/core.h | 18 +++++++++++++++++ drivers/cxl/core/port.c | 52 +++++++++++++++++-------------------------------- 2 files changed, 36 insertions(+), 34 deletions(-)---
base-commit:63fbf275fa9f18f7020fb8acf54fa107e51d0f23
change-id: 20260208-fix-port-enumeration-failure-34e1f4953f02Best regards,