cxl/region: Remove the AUTO_STAGED endpoint decoder state
Alison Schofield
maintainerPATCH 1/1 cxl/region: Remove the AUTO_STAGED endpoint decoder state
Dan added a new decoder state, AUTO_STAGED, to trigger cleanup when an endpoint decoder was unregistered while waiting for its peers. The cleanup used cxled->pos to find the target entry.
Since commit cbda6a2c2bec ("cxl/region: Fix out-of-bounds access in cxl_cancel_auto_attach()"), the cleanup scans for the decoder pointer and no longer depends on cxled->pos.
Dave Jiang [1] reported a use-after-free during region teardown because AUTO_STAGED is lost when an upstream attach fails and unwinds. That prompted another look at the state. The scan already determines whether there is a target entry to remove, so AUTO_STAGED is redundant.
Drop the state and run the lookup when an AUTO decoder is unregistered with no cxld->region. This covers both decoders waiting for peers (Dan's case) and decoders left in the array after unwind (Dave's case).
[1] https://patch.msgid.link/[email protected]
--- drivers/cxl/core/region.c | 12 +----------- drivers/cxl/cxl.h | 6 ++---- 2 files changed, 3 insertions(+), 15 deletions(-)
drivers/cxl/core/region.c +1 -11
@@ -1105,14 +1105,6 @@ static int cxl_rr_ep_add(struct cxl_region_ref *cxl_rr,if (!cxld->region) { cxld->region = cxlr; - -/* -* Now that cxld->region is set the intermediate staging state -* can be cleared. -*/ -if (cxld == &cxled->cxld && - cxled->state ==CXL_DECODER_STATE_AUTO_STAGED ) - cxled->state =CXL_DECODER_STATE_AUTO ;get_device (&cxlr->dev); }
@@ -1876,7 +1868,6 @@ static int cxl_region_attach_auto(struct cxl_region *cxlr, p->targets[pos] = cxled; cxled->pos = pos; - cxled->state =CXL_DECODER_STATE_AUTO_STAGED ; p->nr_targets++;return 0 ;
@@ -2247,7 +2238,6 @@ static int cxl_region_remove_target(struct device *dev, void *data)for (i =0 ; i < p->interleave_ways; i++) {if (p->targets[i] == cxled) { p->nr_targets--; - cxled->state =CXL_DECODER_STATE_AUTO ; cxled->pos =-1 ; p->targets[i] =NULL ;
@@ -2264,7 +2254,7 @@ static int cxl_region_remove_target(struct device *dev, void *data)*/ static void cxl_cancel_auto_attach (struct cxl_endpoint_decoder *cxled) { -if (cxled->state !=CXL_DECODER_STATE_AUTO_STAGED ) +if (cxled->state !=CXL_DECODER_STATE_AUTO )return ;bus_for_each_dev (&cxl_bus_type,NULL , cxled, cxl_region_remove_target);
drivers/cxl/cxl.h +2 -4
@@ -314,14 +314,12 @@ struct cxl_decoder { };/* -* Track whether this decoder is free for userspace provisioning, reserved for -* region autodiscovery, whether it is started connecting (awaiting other -* peers), or has completed auto assembly. +* Track whether this decoder is reserved for region autodiscovery, or +* free for userspace provisioning. */ enum cxl_decoder_state {CXL_DECODER_STATE_MANUAL ,CXL_DECODER_STATE_AUTO , -CXL_DECODER_STATE_AUTO_STAGED , };/**
base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
Signature · 1 line
2.37.3
Quoted 13 lines · Alison Schofield
Dan added a new decoder state,
AUTO_STAGED, to trigger cleanup when an endpoint decoder was unregistered while waiting for its peers. The cleanup usedcxled->posto find the target entry.Since commit
cbda6a2c2bec("cxl/region: Fix out-of-bounds access incxl_cancel_auto_attach()"), the cleanup scans for the decoder pointer and no longer depends oncxled->pos.Dave Jiang [1] reported a use-after-free during region teardown because
AUTO_STAGEDis lost when an upstream attach fails and unwinds. That prompted another look at the state. The scan already determines whether there is a target entry to remove, soAUTO_STAGEDis redundant.
Quoted 8 lines
Drop the state and run the lookup when an AUTO decoder is unregistered with no
cxld->region. This covers both decoders waiting for peers (Dan's case) and decoders left in the array after unwind (Dave's case).[1] https://patch.msgid.link/[email protected]
Fixes 87805c32e6ad ("cxl/region: Fix use-after-free from auto assembly failure")Signed-off-by Alison Schofield <[email protected]>
Quoted 72 lines
--- drivers/cxl/core/region.c | 12 +----------- drivers/cxl/cxl.h | 6 ++---- 2 files changed, 3 insertions(+), 15 deletions(-)drivers/cxl/core/region.c +1
-11@@ -1105,14 +1105,6 @@ static int cxl_rr_ep_add(struct cxl_region_ref *cxl_rr,if (!cxld->region) { cxld->region = cxlr; - -/* -* Now that cxld->region is set the intermediate staging state -* can be cleared. -*/ -if (cxld == &cxled->cxld && - cxled->state ==CXL_DECODER_STATE_AUTO_STAGED ) - cxled->state =CXL_DECODER_STATE_AUTO ;get_device (&cxlr->dev); }@@ -1876,7 +1868,6 @@ static int cxl_region_attach_auto(struct cxl_region *cxlr, p->targets[pos] = cxled; cxled->pos = pos; - cxled->state =CXL_DECODER_STATE_AUTO_STAGED ; p->nr_targets++;return 0 ;@@ -2247,7 +2238,6 @@ static int cxl_region_remove_target(struct device *dev, void *data)for (i =0 ; i < p->interleave_ways; i++) {if (p->targets[i] == cxled) { p->nr_targets--; - cxled->state =CXL_DECODER_STATE_AUTO ; cxled->pos =-1 ; p->targets[i] =NULL ;@@ -2264,7 +2254,7 @@ static int cxl_region_remove_target(struct device *dev, void *data)*/ static void cxl_cancel_auto_attach (struct cxl_endpoint_decoder *cxled) { -if (cxled->state !=CXL_DECODER_STATE_AUTO_STAGED ) +if (cxled->state !=CXL_DECODER_STATE_AUTO )return ;bus_for_each_dev (&cxl_bus_type,NULL , cxled, cxl_region_remove_target);drivers/cxl/cxl.h +2
-4@@ -314,14 +314,12 @@ struct cxl_decoder { };/* -* Track whether this decoder is free for userspace provisioning, reserved for -* region autodiscovery, whether it is started connecting (awaiting other -* peers), or has completed auto assembly. +* Track whether this decoder is reserved for region autodiscovery, or +* free for userspace provisioning. */ enum cxl_decoder_state {CXL_DECODER_STATE_MANUAL ,CXL_DECODER_STATE_AUTO , -CXL_DECODER_STATE_AUTO_STAGED , };/** base-commit:
fe6ff8d5d675c01793b9f051d16055dd4aa75244