Linux CXL
 help / color / mirror / Atom feed
* [PATCH] cxl/port: Validate decoder nr_targets against supported ways
@ 2025-08-13 21:42 alison.schofield
  2025-08-14  0:57 ` dan.j.williams
  0 siblings, 1 reply; 5+ messages in thread
From: alison.schofield @ 2025-08-13 21:42 UTC (permalink / raw)
  To: Davidlohr Bueso, Jonathan Cameron, Dave Jiang, Alison Schofield,
	Vishal Verma, Ira Weiny, Dan Williams
  Cc: linux-cxl

From: Alison Schofield <alison.schofield@intel.com>

nr_targets is the number of downstream ports in a decoder's target
list. It is limited to the supported interleave ways: 1, 2, 3, 4, 6,
8, 12, 16. The existing check only enforces nr_targets <=
CXL_DECODER_MAX_INTERLEAVE (16), allowing invalid values, like 5, 7,
etc, to pass through.

Add is_nr_targets_valid() to check for supported interleave ways using
ways_to_eiw(). Use it before allocating a decoder to avoid alloc/free
churn when nr_targets is already known to be invalid.

This was found by inspection and hardens the code.

Signed-off-by: Alison Schofield <alison.schofield@intel.com>
---
 drivers/cxl/core/port.c | 23 ++++++++++-------------
 drivers/cxl/cxl.h       | 11 +++++++++++
 2 files changed, 21 insertions(+), 13 deletions(-)

diff --git a/drivers/cxl/core/port.c b/drivers/cxl/core/port.c
index 8f36ff413f5d..7d62797d1202 100644
--- a/drivers/cxl/core/port.c
+++ b/drivers/cxl/core/port.c
@@ -1773,17 +1773,6 @@ static int cxl_decoder_init(struct cxl_port *port, struct cxl_decoder *cxld)
 	return 0;
 }
 
-static int cxl_switch_decoder_init(struct cxl_port *port,
-				   struct cxl_switch_decoder *cxlsd,
-				   int nr_targets)
-{
-	if (nr_targets > CXL_DECODER_MAX_INTERLEAVE)
-		return -EINVAL;
-
-	cxlsd->nr_targets = nr_targets;
-	return cxl_decoder_init(port, &cxlsd->cxld);
-}
-
 /**
  * cxl_root_decoder_alloc - Allocate a root level decoder
  * @port: owning CXL root of this decoder
@@ -1805,13 +1794,17 @@ struct cxl_root_decoder *cxl_root_decoder_alloc(struct cxl_port *port,
 	if (!is_cxl_root(port))
 		return ERR_PTR(-EINVAL);
 
+	if (!is_nr_targets_valid(nr_targets))
+		return ERR_PTR(-EINVAL);
+
 	cxlrd = kzalloc(struct_size(cxlrd, cxlsd.target, nr_targets),
 			GFP_KERNEL);
 	if (!cxlrd)
 		return ERR_PTR(-ENOMEM);
 
 	cxlsd = &cxlrd->cxlsd;
-	rc = cxl_switch_decoder_init(port, cxlsd, nr_targets);
+	cxlsd->nr_targets = nr_targets;
+	rc = cxl_decoder_init(port, &cxlsd->cxld);
 	if (rc) {
 		kfree(cxlrd);
 		return ERR_PTR(rc);
@@ -1859,11 +1852,15 @@ struct cxl_switch_decoder *cxl_switch_decoder_alloc(struct cxl_port *port,
 	if (is_cxl_root(port) || is_cxl_endpoint(port))
 		return ERR_PTR(-EINVAL);
 
+	if (!is_nr_targets_valid(nr_targets))
+		return ERR_PTR(-EINVAL);
+
 	cxlsd = kzalloc(struct_size(cxlsd, target, nr_targets), GFP_KERNEL);
 	if (!cxlsd)
 		return ERR_PTR(-ENOMEM);
 
-	rc = cxl_switch_decoder_init(port, cxlsd, nr_targets);
+	cxlsd->nr_targets = nr_targets;
+	rc = cxl_decoder_init(port, &cxlsd->cxld);
 	if (rc) {
 		kfree(cxlsd);
 		return ERR_PTR(rc);
diff --git a/drivers/cxl/cxl.h b/drivers/cxl/cxl.h
index 4fe3df06f57a..02b526a16492 100644
--- a/drivers/cxl/cxl.h
+++ b/drivers/cxl/cxl.h
@@ -132,6 +132,17 @@ static inline int ways_to_eiw(unsigned int ways, u8 *eiw)
 	return 0;
 }
 
+/* nr_targets must match a supported number of interleave ways */
+static inline bool is_nr_targets_valid(int nr_targets)
+{
+	u8 unused_eiw;
+
+	if (nr_targets <= 0)
+		return false;
+
+	return ways_to_eiw(nr_targets, &unused_eiw) == 0;
+}
+
 /* RAS Registers CXL 2.0 8.2.5.9 CXL RAS Capability Structure */
 #define CXL_RAS_UNCORRECTABLE_STATUS_OFFSET 0x0
 #define   CXL_RAS_UNCORRECTABLE_STATUS_MASK (GENMASK(16, 14) | GENMASK(11, 0))

base-commit: d9412f08e25a5b66f9021739c090cc9b8f1089b1
-- 
2.37.3


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-08-21  0:38 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-13 21:42 [PATCH] cxl/port: Validate decoder nr_targets against supported ways alison.schofield
2025-08-14  0:57 ` dan.j.williams
2025-08-14  3:12   ` Alison Schofield
2025-08-14 17:36     ` dan.j.williams
2025-08-21  0:37       ` Alison Schofield

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox