Linux CXL
 help / color / mirror / Atom feed
From: alison.schofield@intel.com
To: Davidlohr Bueso <dave@stgolabs.net>,
	Jonathan Cameron <jonathan.cameron@huawei.com>,
	Dave Jiang <dave.jiang@intel.com>,
	Alison Schofield <alison.schofield@intel.com>,
	Vishal Verma <vishal.l.verma@intel.com>,
	Ira Weiny <ira.weiny@intel.com>,
	Dan Williams <dan.j.williams@intel.com>
Cc: linux-cxl@vger.kernel.org
Subject: [PATCH] cxl/port: Validate decoder nr_targets against supported ways
Date: Wed, 13 Aug 2025 14:42:16 -0700	[thread overview]
Message-ID: <20250813214218.2917555-1-alison.schofield@intel.com> (raw)

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


             reply	other threads:[~2025-08-13 21:42 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-13 21:42 alison.schofield [this message]
2025-08-14  0:57 ` [PATCH] cxl/port: Validate decoder nr_targets against supported ways 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250813214218.2917555-1-alison.schofield@intel.com \
    --to=alison.schofield@intel.com \
    --cc=dan.j.williams@intel.com \
    --cc=dave.jiang@intel.com \
    --cc=dave@stgolabs.net \
    --cc=ira.weiny@intel.com \
    --cc=jonathan.cameron@huawei.com \
    --cc=linux-cxl@vger.kernel.org \
    --cc=vishal.l.verma@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox