Skip to contentSkip to conversation

cxl/region: Fix null pointer dereference due to pass through decoder commit

PATCH In review Jonathan Cameron 3 participants 7 messages Aug 18, 2022 – Oct 11, 2022 CXL

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cxl/region: Fix null pointer dereference due to pass through decoder commit

Not all decoders have a commit callback.

The CXL specification allows a host bridge with a single root port to have no explicit HDM decoders. Currently we assumes there are none. As such we create a special pass through decoder instance without a commit callback.

Prior to this patch, the commit callback was called unconditionally. Thus a configuration with 1 Host Bridge, 1 Root Port, 1 switch with multiple downstream ports below which there are multiple CXL type 3 devices results in a situation where committing the region causes a null pointer dereference.

Fixes 176baefb2eb5 ("cxl/hdm: Commit decoder state to hardware")

---

We could fix this with a stub function perhaps as an alternative?

 drivers/cxl/core/region.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/cxl/core/region.c +2 -1
index 401148016978..c49d9a5f1091 100644
@@ -174,7 +174,8 @@ static int cxl_region_decode_commit(struct cxl_region *cxlr)
 		     iter = to_cxl_port(iter->dev.parent)) {
 			cxl_rr = cxl_rr_load(iter, cxlr);
 			cxld = cxl_rr->decoder;
-			rc = cxld->commit(cxld);
+			if (cxld->commit)
+				rc = cxld->commit(cxld);
 			if (rc)
 				break;
 		}
Signature · 1 line
2.32.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Jonathan Cameron

Not all decoders have a commit callback.

The CXL specification allows a host bridge with a single root port to have no explicit HDM decoders. Currently we assumes there are none.

s/assumes/assume/ or,
'the region driver' instead of 'we'?

Quoted 12 lines

As such we create a special pass through decoder instance without a commit callback.

Prior to this patch, the commit callback was called unconditionally. Thus a configuration with 1 Host Bridge, 1 Root Port, 1 switch with multiple downstream ports below which there are multiple CXL type 3 devices results in a situation where committing the region causes a null pointer dereference.

Fixes 176baefb2eb5 ("cxl/hdm: Commit decoder state to hardware")
Quoted 6 lines

---

We could fix this with a stub function perhaps as an alternative?

 drivers/cxl/core/region.c | 3 ++-

 1 file changed, 2 insertions(+), 1 deletion(-)

Other than that, looks good to me,

Quoted 16 lines
drivers/cxl/core/region.c +2 -1
index 401148016978..c49d9a5f1091 100644
@@ -174,7 +174,8 @@ static int cxl_region_decode_commit(struct
 cxl_region *cxlr)
                      iter = to_cxl_port(iter->dev.parent)) {
                         cxl_rr = cxl_rr_load(iter, cxlr);
                         cxld = cxl_rr->decoder;
-                       rc = cxld->commit(cxld);
+                       if (cxld->commit)
+                               rc = cxld->commit(cxld);
                         if (rc)
                                 break;

                }

#2 lore raw
Quoted 23 lines · Vishal L
Quoted 4 lines · Jonathan Cameron

Not all decoders have a commit callback.

The CXL specification allows a host bridge with a single root port to have no explicit HDM decoders. Currently we assumes there are none.

s/assumes/assume/ or,
'the region driver' instead of 'we'?

Quoted 14 lines

As such we create a special pass through decoder instance without a commit callback.

Prior to this patch, the commit callback was called unconditionally. Thus a configuration with 1 Host Bridge, 1 Root Port, 1 switch with multiple downstream ports below which there are multiple CXL type 3 devices results in a situation where committing the region causes a null pointer dereference.

Fixes 176baefb2eb5 ("cxl/hdm: Commit decoder state to hardware")

---

Quoted 8 lines
Quoted 4 lines

We could fix this with a stub function perhaps as an alternative?

 drivers/cxl/core/region.c | 3 ++-

 1 file changed, 2 insertions(+), 1 deletion(-)

Other than that, looks good to me,

Thanks, incorporated the changelog fixups.

#3 lore raw
In reply to Jonathan Cameron
Quoted 13 lines · Jonathan Cameron

Not all decoders have a commit callback.

The CXL specification allows a host bridge with a single root port to have no explicit HDM decoders. Currently we assumes there are none. As such we create a special pass through decoder instance without a commit callback.

Prior to this patch, the commit callback was called unconditionally. Thus a configuration with 1 Host Bridge, 1 Root Port, 1 switch with multiple downstream ports below which there are multiple CXL type 3 devices results in a situation where committing the region causes a null pointer dereference.

Quoted 6 lines
Fixes 176baefb2eb5 ("cxl/hdm: Commit decoder state to hardware")

---

We could fix this with a stub function perhaps as an alternative?

How about keep the callback mandatory except for the passthrough switch case?

Something like this (untested):

drivers/cxl/core/region.c +9 -3
index 401148016978..b1137f6dc2d1 100644
@@ -172,11 +172,17 @@ static int cxl_region_decode_commit(struct cxl_region *cxlr)
                /* commit bottom up */
                for (iter = cxled_to_port(cxled); !is_cxl_root(iter);
                     iter = to_cxl_port(iter->dev.parent)) {
+                       struct cxl_switch_decoder *cxlsd = NULL;
+
                        cxl_rr = cxl_rr_load(iter, cxlr);
                        cxld = cxl_rr->decoder;
-                       rc = cxld->commit(cxld);
-                       if (rc)
-                               break;
+                       if (is_switch_decoder(&cxld->dev))
+                               cxlsd = to_cxl_switch_decoder(&cxld->dev);
+                       if (cxld->commit || cxlsd && cxlsd->nr_targets > 1) {
+                               rc = cxld->commit(cxld);
+                               if (rc)
+                                       break;
+                       }
                }
 
                if (rc) {
#4 lore raw
Quoted 19 lines · On Thu, 18 Aug 2022 17:37:22 -0700 Dan Williams <[email protected]>…
Quoted 18 lines · Jonathan Cameron

Not all decoders have a commit callback.

The CXL specification allows a host bridge with a single root port to have no explicit HDM decoders. Currently we assumes there are none. As such we create a special pass through decoder instance without a commit callback.

Prior to this patch, the commit callback was called unconditionally. Thus a configuration with 1 Host Bridge, 1 Root Port, 1 switch with multiple downstream ports below which there are multiple CXL type 3 devices results in a situation where committing the region causes a null pointer dereference.

Fixes 176baefb2eb5 ("cxl/hdm: Commit decoder state to hardware")

---

Quoted 4 lines
Quoted 1 line

We could fix this with a stub function perhaps as an alternative?

How about keep the callback mandatory except for the passthrough switch case?

Should work, but makes it messier if we do add support for HDM decoders in the pass through case (which I'm 90% + sure we will have once actual platforms appear - we regularly have PCIe host bridges that on a particular platform have one RP and on other platforms expose multiple - it's just a wiring question - I doubt we'll bother hiding the HDMs given the spec allows for them to be there in this case and it adds complexity to the design to hide them).

Keeping the cxlsd->nr_targets > 1 check in one place would be my preference or adding a wrapper. e.g.
is_passthrough_decoder()
that we can change easily later.

Either way, focus now is getting fix in, can change how it's done later. So I'm fine with either fix.

Jonathan

Quoted 29 lines

Something like this (untested):

drivers/cxl/core/region.c +9 -3
index 401148016978..b1137f6dc2d1 100644
@@ -172,11 +172,17 @@ static int cxl_region_decode_commit(struct cxl_region *cxlr)
                /* commit bottom up */
                for (iter = cxled_to_port(cxled); !is_cxl_root(iter);
                     iter = to_cxl_port(iter->dev.parent)) {
+                       struct cxl_switch_decoder *cxlsd = NULL;
+
                        cxl_rr = cxl_rr_load(iter, cxlr);
                        cxld = cxl_rr->decoder;
-                       rc = cxld->commit(cxld);
-                       if (rc)
-                               break;
+                       if (is_switch_decoder(&cxld->dev))
+                               cxlsd = to_cxl_switch_decoder(&cxld->dev);
+                       if (cxld->commit || cxlsd && cxlsd->nr_targets > 1) {
+                               rc = cxld->commit(cxld);
+                               if (rc)
+                                       break;
+                       }
                }
 
                if (rc) {
#5 lore raw
Quoted 40 lines · On Fri, 19 Aug 2022 09:31:33 +0100 Jonathan Cameron…
Quoted 19 lines · On Thu, 18 Aug 2022 17:37:22 -0700 Dan Williams <[email protected]>…
Quoted 18 lines · Jonathan Cameron

Not all decoders have a commit callback.

The CXL specification allows a host bridge with a single root port to have no explicit HDM decoders. Currently we assumes there are none. As such we create a special pass through decoder instance without a commit callback.

Prior to this patch, the commit callback was called unconditionally. Thus a configuration with 1 Host Bridge, 1 Root Port, 1 switch with multiple downstream ports below which there are multiple CXL type 3 devices results in a situation where committing the region causes a null pointer dereference.

Fixes 176baefb2eb5 ("cxl/hdm: Commit decoder state to hardware")

---

Quoted 4 lines
Quoted 1 line

We could fix this with a stub function perhaps as an alternative?

How about keep the callback mandatory except for the passthrough switch case?

Should work, but makes it messier if we do add support for HDM decoders in the pass through case (which I'm 90% + sure we will have once actual platforms appear - we regularly have PCIe host bridges that on a particular platform have one RP and on other platforms expose multiple - it's just a wiring question - I doubt we'll bother hiding the HDMs given the spec allows for them to be there in this case and it adds complexity to the design to hide them).

Keeping the cxlsd->nr_targets > 1 check in one place would be my preference or adding a wrapper. e.g.
is_passthrough_decoder()
that we can change easily later.

Quoted 6 lines

Either way, focus now is getting fix in, can change how it's done later. So I'm fine with either fix.

Jonathan

Hi Dan,

I forgot to test your suggestion. Now done and it works as expected.

One comment below.

Given this is now your code, do you want to send the patch or is it easier if I do so?

Quoted 15 lines
Quoted 15 lines

Something like this (untested):

drivers/cxl/core/region.c +2 -0
index 401148016978..b1137f6dc2d1 100644
@@ -172,11 +172,17 @@ static int cxl_region_decode_commit(struct cxl_region *cxlr)
                /* commit bottom up */
                for (iter = cxled_to_port(cxled); !is_cxl_root(iter);
                     iter = to_cxl_port(iter->dev.parent)) {
+                       struct cxl_switch_decoder *cxlsd = NULL;
+
Quoted 8 lines

drivers/cxl/core/region.c static int cxl_region_decode_commit(struct cxl_region *cxlr) · line 181

Quoted 8 lines
drivers/cxl/core/region.c +3 -3
                        cxl_rr = cxl_rr_load(iter, cxlr);
                        cxld = cxl_rr->decoder;
-                       rc = cxld->commit(cxld);
-                       if (rc)
-                               break;
+                       if (is_switch_decoder(&cxld->dev))
+                               cxlsd = to_cxl_switch_decoder(&cxld->dev);
+                       if (cxld->commit || cxlsd && cxlsd->nr_targets > 1) {

I'd add brackets around the (cxlsd && cxlsd->nr_targets > 1)
just to stop forgetful people like me having to check the precedence ordering..

Quoted 9 lines
Quoted 8 lines
drivers/cxl/core/region.c +4 -0
+                               rc = cxld->commit(cxld);
+                               if (rc)
+                                       break;
+                       }
                }
 
                if (rc) {
#6 lore raw
Quoted 50 lines · Jonathan Cameron
Quoted 40 lines · On Fri, 19 Aug 2022 09:31:33 +0100 Jonathan Cameron…
Quoted 19 lines · On Thu, 18 Aug 2022 17:37:22 -0700 Dan Williams <[email protected]>…
Quoted 18 lines · Jonathan Cameron

Not all decoders have a commit callback.

The CXL specification allows a host bridge with a single root port to have no explicit HDM decoders. Currently we assumes there are none. As such we create a special pass through decoder instance without a commit callback.

Prior to this patch, the commit callback was called unconditionally. Thus a configuration with 1 Host Bridge, 1 Root Port, 1 switch with multiple downstream ports below which there are multiple CXL type 3 devices results in a situation where committing the region causes a null pointer dereference.

Fixes 176baefb2eb5 ("cxl/hdm: Commit decoder state to hardware")

---

Quoted 4 lines
Quoted 1 line

We could fix this with a stub function perhaps as an alternative?

How about keep the callback mandatory except for the passthrough switch case?

Should work, but makes it messier if we do add support for HDM decoders in the pass through case (which I'm 90% + sure we will have once actual platforms appear - we regularly have PCIe host bridges that on a particular platform have one RP and on other platforms expose multiple - it's just a wiring question - I doubt we'll bother hiding the HDMs given the spec allows for them to be there in this case and it adds complexity to the design to hide them).

Keeping the cxlsd->nr_targets > 1 check in one place would be my preference or adding a wrapper. e.g.
is_passthrough_decoder()
that we can change easily later.

Quoted 6 lines

Either way, focus now is getting fix in, can change how it's done later. So I'm fine with either fix.

Jonathan

Quoted 8 lines

Hi Dan,

I forgot to test your suggestion. Now done and it works as expected.

One comment below.

Given this is now your code, do you want to send the patch or is it easier if I do so?

I will take your fix since it is easier to backport, and then follow-up with a helper to make the expectations clearer for multi-target decoders.