mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] cxl/region: Reject freeing the HPA of a region with attached targets
@ 2026-12-03  8:31 Junho Cha via B4 Relay
  2026-12-03 18:36 ` Alison Schofield
  0 siblings, 1 reply; 2+ messages in thread
From: Junho Cha via B4 Relay @ 2026-12-03  8:31 UTC (permalink / raw)
  To: Dave Jiang, linux-cxl
  Cc: Ira Weiny, Vishal Verma, Jonathan Cameron, Davidlohr Bueso,
	linux-kernel, Ben Cheatham, Alison Schofield, Li Ming,
	Richard Cheng

From: Junho Cha <junho.cha@xcena.com>

free_hpa() only refuses once the region is CXL_CONFIG_ACTIVE. With
some targets attached, root can write 0 to size, which returns the
region to CXL_CONFIG_IDLE and lets interleave_ways,
interleave_granularity and size change while the attached endpoint
decoders keep their old settings.

The region can then be committed with endpoint decoders that do not
match it. If interleave_ways shrinks so that an attached target's
position is out of range, attaching targets until nr_targets reaches
the new interleave_ways can lead to a NULL dereference in
cxl_region_setup_targets(). Deleting the region and later unbinding
that target's memdev can lead to a use-after-free in
__cxl_decoder_detach(). All three were reproduced with cxl_test.

Refuse to free the HPA while any target is attached, and drop the
CXL_CONFIG_ACTIVE check, since a region reaches that state only once
all of its targets are attached. libcxl never writes 0 to size, so
cxl-cli is not affected.

Fixes: 384e624bb211 ("cxl/region: Attach endpoint decoders")
Cc: stable@vger.kernel.org
Signed-off-by: Junho Cha <junho.cha@xcena.com>
---
Tested with cxl_test on cxl/next. Writing 0 to size after detaching all
targets still works. Before commit b3a88225519c ("cxl/region: Consolidate
cxl_decoder_kill_region() and cxl_region_detach()"), the use-after-free
case only triggers a warning.
---
Changes in v2:
- Drop the CXL_CONFIG_ACTIVE check, which the new check makes dead code
  (Alison).
- Link to v1: https://patch.msgid.link/20261124-junhomax-fix-cxl-region-free-hpa-targets-v1-1-9229ae407595@xcena.com
---
 drivers/cxl/core/region.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
index 237172b33f55..8b5a69490c7a 100644
--- a/drivers/cxl/core/region.c
+++ b/drivers/cxl/core/region.c
@@ -772,7 +772,8 @@ static int free_hpa(struct cxl_region *cxlr)
 	if (!p->res)
 		return 0;
 
-	if (p->state >= CXL_CONFIG_ACTIVE)
+	/* Attached targets hold the current geometry, detach them first */
+	if (p->nr_targets)
 		return -EBUSY;
 
 	cxlr->hpa_range = DEFINE_RANGE(0, -1);

---
base-commit: 32af7819352bae9c690ebe277a44eceb501d5c68
change-id: 20261124-junhomax-fix-cxl-region-free-hpa-targets-0838a7a4e93d

Best regards,
--  
Junho Cha <junho.cha@xcena.com>



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

* Re: [PATCH v2] cxl/region: Reject freeing the HPA of a region with attached targets
  2026-12-03  8:31 [PATCH v2] cxl/region: Reject freeing the HPA of a region with attached targets Junho Cha via B4 Relay
@ 2026-12-03 18:36 ` Alison Schofield
  0 siblings, 0 replies; 2+ messages in thread
From: Alison Schofield @ 2026-12-03 18:36 UTC (permalink / raw)
  To: junho.cha
  Cc: Dave Jiang, linux-cxl, Ira Weiny, Vishal Verma, Jonathan Cameron,
	Davidlohr Bueso, linux-kernel, Ben Cheatham, Li Ming,
	Richard Cheng

On Thu, Dec 03, 2026 at 05:31:09PM +0900, Junho Cha via B4 Relay wrote:
> From: Junho Cha <junho.cha@xcena.com>
> 
> free_hpa() only refuses once the region is CXL_CONFIG_ACTIVE. With
> some targets attached, root can write 0 to size, which returns the
> region to CXL_CONFIG_IDLE and lets interleave_ways,
> interleave_granularity and size change while the attached endpoint
> decoders keep their old settings.
> 
> The region can then be committed with endpoint decoders that do not
> match it. If interleave_ways shrinks so that an attached target's
> position is out of range, attaching targets until nr_targets reaches
> the new interleave_ways can lead to a NULL dereference in
> cxl_region_setup_targets(). Deleting the region and later unbinding
> that target's memdev can lead to a use-after-free in
> __cxl_decoder_detach(). All three were reproduced with cxl_test.
> 
> Refuse to free the HPA while any target is attached, and drop the
> CXL_CONFIG_ACTIVE check, since a region reaches that state only once
> all of its targets are attached. libcxl never writes 0 to size, so
> cxl-cli is not affected.


Reviewed-by: Alison Schofield <alison.schofield@intel.com>


> 
> Fixes: 384e624bb211 ("cxl/region: Attach endpoint decoders")
> Cc: stable@vger.kernel.org
> Signed-off-by: Junho Cha <junho.cha@xcena.com>
> ---
> Tested with cxl_test on cxl/next. Writing 0 to size after detaching all
> targets still works. Before commit b3a88225519c ("cxl/region: Consolidate
> cxl_decoder_kill_region() and cxl_region_detach()"), the use-after-free
> case only triggers a warning.
> ---
> Changes in v2:
> - Drop the CXL_CONFIG_ACTIVE check, which the new check makes dead code
>   (Alison).
> - Link to v1: https://patch.msgid.link/20261124-junhomax-fix-cxl-region-free-hpa-targets-v1-1-9229ae407595@xcena.com
> ---
>  drivers/cxl/core/region.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
> index 237172b33f55..8b5a69490c7a 100644
> --- a/drivers/cxl/core/region.c
> +++ b/drivers/cxl/core/region.c
> @@ -772,7 +772,8 @@ static int free_hpa(struct cxl_region *cxlr)
>  	if (!p->res)
>  		return 0;
>  
> -	if (p->state >= CXL_CONFIG_ACTIVE)
> +	/* Attached targets hold the current geometry, detach them first */
> +	if (p->nr_targets)
>  		return -EBUSY;
>  
>  	cxlr->hpa_range = DEFINE_RANGE(0, -1);
> 
> ---
> base-commit: 32af7819352bae9c690ebe277a44eceb501d5c68
> change-id: 20261124-junhomax-fix-cxl-region-free-hpa-targets-0838a7a4e93d
> 
> Best regards,
> --  
> Junho Cha <junho.cha@xcena.com>
> 
> 
> 

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

end of thread, other threads:[~2026-12-03 18:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-03  8:31 [PATCH v2] cxl/region: Reject freeing the HPA of a region with attached targets Junho Cha via B4 Relay
2026-12-03 18:36 ` Alison Schofield

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

all inboxes | Powered by JetHome®