Skip to contentSkip to conversation

cxl/region: Calculate peer distance only for reused targets

RFC PATCH In review Richard Cheng 3 participants 3 messages Oct 15 – Oct 17 CXL

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 cxl/region: Calculate peer distance only for reused targets

This patch depends on Alison Schofield's v3 "cxl/region: Refactor cxl_port_setup_targets()" series [1], this is inspired from that serie as well, since her serie contains no functional change, I pull this one out as a standalone patch.

cxl_switch_port_setup_targets() calculates peer distance before searching the existing targets. When the endpoint needs a new target, the calculated distance is never used.

Move cxl_port_peer_distance() into the matching-target branch, immediately before check_last_peer(). This avoids unnecessary ancestor walks and per-port region lookups when adding distinct targets.

[1]:
https://lore.kernel.org/linux-cxl/[email protected]/

---
I don't have available HW for multi-way interleaving setup, so I can't exercise the case optimized here. I would appreciate if someone is williing to validate the actual benefit of this.

Best regards,
Richard Cheng.

---
 drivers/cxl/core/region.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/cxl/core/region.c +2 -2
index 50f4830c225d..efd173c57dad 100644
@@ -1666,10 +1666,10 @@ static int cxl_switch_port_setup_targets(struct cxl_port *port, struct cxl_regio
 
 	cxlsd = to_cxl_switch_decoder(&cxld->dev);
 	if (cxl_rr->nr_targets_set) {
-		int distance = cxl_port_peer_distance(port, cxlr);
-
 		for (int i = 0; i < cxl_rr->nr_targets_set; i++)
 			if (ep->dport == cxlsd->target[i]) {
+				int distance = cxl_port_peer_distance(port, cxlr);
+
 				rc = check_last_peer(cxled, ep, cxl_rr,
 						     distance);
 				if (rc)
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 2 lines · On Thu, 15 Oct 2026 17:40:59 +0800 Richard Cheng <[email protected]> wrote:

This patch depends on Alison Schofield's v3 "cxl/region: Refactor cxl_port_setup_targets()" series [1], this is inspired from that serie

series is a an odd word in English. Singular and plural are the same.

Quoted 7 lines

as well, since her serie contains no functional change, I pull this one out as a standalone patch.

cxl_switch_port_setup_targets() calculates peer distance before searching the existing targets. When the endpoint needs a new target, the calculated distance is never used.

Quoted 6 lines

Move cxl_port_peer_distance() into the matching-target branch, immediately before check_last_peer(). This avoids unnecessary ancestor walks and per-port region lookups when adding distinct targets.

[1]:
https://lore.kernel.org/linux-cxl/[email protected]/

Use a link tag.

Quoted 5 lines

---
I don't have available HW for multi-way interleaving setup, so I can't exercise the case optimized here. I would appreciate if someone is williing to validate the actual benefit of this.

Maybe give it a spin on qemu. But it looks like it's just a code reorder effectively so It would be very odd if it is a problem.

I'd just put this as a comment on Alison's series as I don't think that has merged yet.

Seems like a nice little optimization.

Quoted 24 lines

Best regards,
Richard Cheng.

---
 drivers/cxl/core/region.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/cxl/core/region.c +2 -2
index 50f4830c225d..efd173c57dad 100644
@@ -1666,10 +1666,10 @@ static int cxl_switch_port_setup_targets(struct cxl_port *port, struct cxl_regio
 
 	cxlsd = to_cxl_switch_decoder(&cxld->dev);
 	if (cxl_rr->nr_targets_set) {
-		int distance = cxl_port_peer_distance(port, cxlr);
-
 		for (int i = 0; i < cxl_rr->nr_targets_set; i++)
 			if (ep->dport == cxlsd->target[i]) {
+				int distance = cxl_port_peer_distance(port, cxlr);
+
 				rc = check_last_peer(cxled, ep, cxl_rr,
 						     distance);
 				if (rc)
maintainer
#2 lore raw
In reply to Richard Cheng
Quoted 13 lines · Richard Cheng

This patch depends on Alison Schofield's v3 "cxl/region: Refactor cxl_port_setup_targets()" series [1], this is inspired from that serie as well, since her serie contains no functional change, I pull this one out as a standalone patch.

cxl_switch_port_setup_targets() calculates peer distance before searching the existing targets. When the endpoint needs a new target, the calculated distance is never used.

Move cxl_port_peer_distance() into the matching-target branch, immediately before check_last_peer(). This avoids unnecessary ancestor walks and per-port region lookups when adding distinct targets.

Quoted 7 lines

[1]:
https://lore.kernel.org/linux-cxl/[email protected]/

---
I don't have available HW for multi-way interleaving setup, so I can't exercise the case optimized here. I would appreciate if someone is williing to validate the actual benefit of this.

Nice patch! I've picked it up into v4 of the refactor set.

I tested it with using the cxl unit test cxl-region-sysfs.sh and this at the end of cxl_port_peer_distance():

dev_dbg(&cxlr->dev, "%s:%s: peer distance %d\n",

        dev_name(port->uport_dev), dev_name(&port->dev), distance);

The x8 region went from 10 calls to 4. The eliminated calls were for new downstream targets and the remaining calls were for reused targets where check_last_peer() needs the distance.

The x2 auto region also went from 2 calls to 1.

I tested the peer validation path by swapping endpoints at positions 0 and 2 in cxl-region-sysfs.sh. The mismatched peer was detected and the region rejected.

-- Alison

Quoted 27 lines

Best regards,
Richard Cheng.

---
 drivers/cxl/core/region.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/cxl/core/region.c +2 -2
index 50f4830c225d..efd173c57dad 100644
@@ -1666,10 +1666,10 @@ static int cxl_switch_port_setup_targets(struct cxl_port *port, struct cxl_regio
 
 	cxlsd = to_cxl_switch_decoder(&cxld->dev);
 	if (cxl_rr->nr_targets_set) {
-		int distance = cxl_port_peer_distance(port, cxlr);
-
 		for (int i = 0; i < cxl_rr->nr_targets_set; i++)
 			if (ep->dport == cxlsd->target[i]) {
+				int distance = cxl_port_peer_distance(port, cxlr);
+
 				rc = check_last_peer(cxled, ep, cxl_rr,
 						     distance);
 				if (rc)
Signature · 1 line
2.43.0