Skip to contentSkip to conversation

cxl/region: Add cxl_decoder_is_passthrough() helper

PATCH In review Richard Cheng 3 participants 3 messages Aug 5 – Aug 13 CXL

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 cxl/region: Add cxl_decoder_is_passthrough() helper

commit_decoder() open-codes the passthrough test.
A NULL ->commit() plus a switch decoder with at most one target. The call site is hard to read, and it suggests a NULL ->commit() alone identifies a passthrough decoder. It does not. DVSEC-emulated endpoint decoders also leave ->commit() NULL, and root decoders never set it. A future caller testing ->commit() alone would silently include them.

Move the test into cxl_decoder_is_passthrough() and document what each condition rules out. The helper runs the same tests in the same order, no functional change.

---
Changelog:

v1 -> v2:
- Dropped the passthrough F_ENABLE restore patch. The bug was already fixed.
- What remains is only the clarify helper function patch, so the patch is retitled accordingly

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

Best regards,
Richard Cheng.

---
 drivers/cxl/core/region.c | 28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)
drivers/cxl/core/region.c +22 -6
index 27e63e6dab7c..cd73684fbe97 100644
@@ -293,17 +293,33 @@ static void cxl_region_decode_reset(struct cxl_region *cxlr, int count)
 	clear_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags);
 }
 
-static int commit_decoder(struct cxl_decoder *cxld)
+/*
+ * A single-dport host-bridge need not publish an HDM decoder capability
+ * when passthrough decode can be assumed. The resulting decoder is a
+ * software-only construct with no registers to program, so it carries no
+ * ->commit() operation, see devm_cxl_add_passthrough_decoder().
+ *
+ * A NULL ->commit() alone does not identify one, it is also NULL for
+ * DVSEC-emulated endpoint decoders. Test the decoder type and target
+ * count as well.
+ */
+static bool cxl_decoder_is_passthrough(struct cxl_decoder *cxld)
 {
-	struct cxl_switch_decoder *cxlsd = NULL;
+	if (cxld->commit)
+		return false;
+
+	if (!is_switch_decoder(&cxld->dev))
+		return false;
+
+	return to_cxl_switch_decoder(&cxld->dev)->nr_targets <= 1;
+}
 
+static int commit_decoder(struct cxl_decoder *cxld)
+{
 	if (cxld->commit)
 		return cxld->commit(cxld);
 
-	if (is_switch_decoder(&cxld->dev))
-		cxlsd = to_cxl_switch_decoder(&cxld->dev);
-
-	if (dev_WARN_ONCE(&cxld->dev, !cxlsd || cxlsd->nr_targets > 1,
+	if (dev_WARN_ONCE(&cxld->dev, !cxl_decoder_is_passthrough(cxld),
 			  "->commit() is required\n"))
 		return -ENXIO;
 	return 0;

base-commit: 212e015fc34712c849653cdb3179cd643c915015

Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] `cxl_decoder_is_passthrough()` incorrectly identifies single-target root decoders as passthrough…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] cxl_decoder_is_passthrough() incorrectly identifies single-target root decoders as passthrough decoders, contradicting the commit message. --

--- Patch [1]: [PATCH v2] cxl/region: Add cxl_decoder_is_passthrough() helper --- commit 9b921fd6e2f183466d76ed78181b72b15a45105b
Author: Richard Cheng <[email protected]>
cxl/region: Add cxl_decoder_is_passthrough() helper

This commit abstracts the passthrough decoder check into a standalone helper function. The commit message explicitly notes this is necessary because checking only for a NULL ->commit() would incorrectly identify root decoders and DVSEC-emulated endpoint decoders as passthrough.

Quoted 3 lines
drivers/cxl/core/region.c +0 -0

[ ... ]

Quoted 9 lines
drivers/cxl/core/region.c +9 -0
+/*
+ * A single-dport host-bridge need not publish an HDM decoder capability
+ * when passthrough decode can be assumed. The resulting decoder is a
+ * software-only construct with no registers to program, so it carries no
+ * ->commit() operation, see devm_cxl_add_passthrough_decoder().
+ *
+ * A NULL ->commit() alone does not identify one, it is also NULL for
+ * DVSEC-emulated endpoint decoders. Test the decoder type and target
+ * count as well.

drivers/cxl/core/region.c static void cxl_region_decode_reset(struct cxl_region *cxlr, int count)

Quoted 10 lines
drivers/cxl/core/region.c +10 -0
+ */
+static bool cxl_decoder_is_passthrough(struct cxl_decoder *cxld)
+{
+	if (cxld->commit)
+		return false;
+
+	if (!is_switch_decoder(&cxld->dev))
+		return false;
+
+	return to_cxl_switch_decoder(&cxld->dev)->nr_targets <= 1;

[Severity: Medium]
Does this helper correctly exclude root decoders as intended?

The commit message states that root decoders leave ->commit() NULL and that a future caller testing ->commit() alone would silently include them. However, looking at is_switch_decoder() in drivers/cxl/core/port.c, it returns true for root decoders:

bool is_switch_decoder(struct device *dev)

{
        return is_root_decoder(dev) || dev->type == &cxl_decoder_switch_type;
}

Since is_switch_decoder() evaluates to true for root decoders, won't this function incorrectly return true for single-target root decoders? This appears to create a misleading abstraction that contradicts the commit message's claim about ruling out root decoders.

Quoted 1 line

+}

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Richard Cheng
Quoted 41 lines · Richard Cheng

commit_decoder() open-codes the passthrough test.
A NULL ->commit() plus a switch decoder with at most one target. The call site is hard to read, and it suggests a NULL ->commit() alone identifies a passthrough decoder. It does not. DVSEC-emulated endpoint decoders also leave ->commit() NULL, and root decoders never set it. A future caller testing ->commit() alone would silently include them.

Move the test into cxl_decoder_is_passthrough() and document what each condition rules out. The helper runs the same tests in the same order, no functional change.

---
Changelog:

v1 -> v2:
- Dropped the passthrough F_ENABLE restore patch. The bug was already fixed.
- What remains is only the clarify helper function patch, so the patch is retitled accordingly

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

Best regards,
Richard Cheng.

---
 drivers/cxl/core/region.c | 28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)
drivers/cxl/core/region.c +1 -1
index 27e63e6dab7c..cd73684fbe97 100644
@@ -293,17 +293,33 @@ static void cxl_region_decode_reset(struct cxl_region *cxlr, int count)
 	clear_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags);
 }
 
-static int commit_decoder(struct cxl_decoder *cxld)
+/*

drivers/cxl/core/region.c static void cxl_region_decode_reset(struct cxl_region *cxlr, int count) · line 306

Quoted 10 lines
drivers/cxl/core/region.c +10 -0
+ * A single-dport host-bridge need not publish an HDM decoder capability
+ * when passthrough decode can be assumed. The resulting decoder is a
+ * software-only construct with no registers to program, so it carries no
+ * ->commit() operation, see devm_cxl_add_passthrough_decoder().
+ *
+ * A NULL ->commit() alone does not identify one, it is also NULL for
+ * DVSEC-emulated endpoint decoders. Test the decoder type and target
+ * count as well.
+ */
+static bool cxl_decoder_is_passthrough(struct cxl_decoder *cxld)

This is much noise for just checking if ->commit() is required in some cases and generate an error if not.

drivers/cxl/core/region.c static void cxl_region_decode_reset(struct cxl_region *cxlr, int count) · line 309

Quoted 4 lines
drivers/cxl/core/region.c +2 -1
 {
-	struct cxl_switch_decoder *cxlsd = NULL;
+	if (cxld->commit)
+		return false;

That duplicates the test and the code is never executed.

drivers/cxl/core/region.c static void cxl_region_decode_reset(struct cxl_region *cxlr, int count) · line 314

Quoted 5 lines
drivers/cxl/core/region.c +5 -0
+
+	if (!is_switch_decoder(&cxld->dev))
+		return false;
+
+	return to_cxl_switch_decoder(&cxld->dev)->nr_targets <= 1;

cxlsd->nr_targets is easier to read and explains that nr_targets is in struct cxl_switch_decoder, which wouln't be obious else. So the change does not improve readability and instead does multiple things in a single line.

drivers/cxl/core/region.c static void cxl_region_decode_reset(struct cxl_region *cxlr, int count) · line 323

Quoted 13 lines
drivers/cxl/core/region.c +4 -4
+}
 
+static int commit_decoder(struct cxl_decoder *cxld)
+{
 	if (cxld->commit)
 		return cxld->commit(cxld);
 
-	if (is_switch_decoder(&cxld->dev))
-		cxlsd = to_cxl_switch_decoder(&cxld->dev);
-
-	if (dev_WARN_ONCE(&cxld->dev, !cxlsd || cxlsd->nr_targets > 1,
+	if (dev_WARN_ONCE(&cxld->dev, !cxl_decoder_is_passthrough(cxld),
 			  "->commit() is required\n"))

The original was pretty straight forward: if it is a switch decoder with multiple targets, raise an error. It looks like an assert here as !cxld->commit is only created in devm_cxl_add_passthrough_decoder() which sets the number of targets to one (not looking into test code here, but if that causes an issue, fix it there).

So I don't see much sense in this patch.

Thanks,

-Robert

Quoted 7 lines
drivers/cxl/core/region.c +0 -0
 		return -ENXIO;
 	return 0;

base-commit: 212e015fc34712c849653cdb3179cd643c915015

Signature · 1 line
2.43.0