Skip to contentSkip to conversation

cxl/region: Fix target accounting when detaching direct EP

PATCH In review Richard Cheng 2 participants 2 messages Nov 4 – Nov 20 CXL

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cxl/region: Fix target accounting when detaching direct EP

cxl_port_detach_region() avoids decrementing nr_targets if another endpoint in the region has the same ep->next. However, for directly attached endpoints, ep->next is NULL. Comparing these NULL pointers incorrectly treats EPs on different downstream ports as sharing a target. Removing one endpoint while another remains therefore leaves nr_targets stale.

E.g. 2 direct EP using a 2-target switch decoder, clearing one region targetN entry leaves nr_targets at 2 instead of 1. Refilling that entry then attempts to add a 3rd target and fails with -ENXIO, even though the requested config fits.

Decrement nr_targets when the removed endpoint has no next port, regardless of whether another endpoint also has a NULL next pointer.

---
Tested using tools/testing/cxl in an isolated arm64 QEMU/KVM guest.

Configure an 8-way PMEM region across the 8 memdevs under the mock two-target root decoder, reserving 256 MB per EP.
Run the following as root:

```
    set -eu
    sys=/sys/bus/cxl/devices
    region=region3
    r="$sys/$region"
    decoder=$(cat "$r/target0")

    test -n "$decoder"
    test ! -L "$r/driver"
    test "$(cat "$r/commit")" = 1

    # Clear only target0, leaving the other targets populated.
    printf '\n' > "$r/target0"
    test -z "$(cat "$r/target0")"
    test -z "$(cat "$sys/$decoder/region")"
    test "$(cat "$r/commit")" = 0

    # Restore the endpoint decoder to its original position.
    printf '%s\n' "$decoder" > "$r/target0"
    test "$(cat "$r/target0")" = "$decoder"
    test "$(cat "$sys/$decoder/region")" = "$region"

    # The restored region should be committable again.
    printf '1\n' > "$r/commit"
    test "$(cat "$r/commit")" = 1
```

Before the fix, clearing target0 succeeds but restoring its decoder fails with ENXIO. With dynamic debug enabled, the kernel reports:

... overflows targets: 2

With the fix, the clear, refill and recommit sequence succeeds.

Best regards,
Richard Cheng.

---
 drivers/cxl/core/region.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/cxl/core/region.c +2 -1
index a048bd5c06cb..30686059ec7b 100644
@@ -1344,7 +1344,8 @@ static void cxl_port_detach_region(struct cxl_port *port,
 				break;
 			}
 		}
-		if (!found)
+		/* Endpoints with no next port do not share a target. */
+		if (!found || !ep->next)
 			cxl_rr->nr_targets--;
 	}
 

base-commit: 05441d697f7f9a927264f4058cbbc6a03dfcac9d

Signature · 1 line
2.43.0
Tested-by on v1 1/1
#1 lore raw
Quoted 4 lines · Richard Cheng

E.g. 2 direct EP using a 2-target switch decoder, clearing one region targetN entry leaves nr_targets at 2 instead of 1. Refilling that entry then attempts to add a 3rd target and fails with -ENXIO, even though the requested config fits.

I reproduced this with cxl_test in an x86_64 guest, with the CXL modules built from cxl/next (32af7819352b) on a 7.4-rc3 kernel. On an unbound 2-way RAM region over the two endpoints below cxl_switch_uport.0, clearing target0 and writing the same decoder back fails with -ENXIO ("overflows targets: 2") without this patch. With it, the refill and the recommit both succeed.