Linux CXL
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: alucerop@amd.com, linux-cxl@vger.kernel.org
Subject: Re: [RFC 2/3] cxl: make explicit removal of endpoint and decoder for Type2
Date: Wed, 18 Nov 2026 18:09:51 +0000	[thread overview]
Message-ID: <sashiko-outbox-221725@kernel.org> (raw)
In-Reply-To: <20261118191431.21781-3-alucerop@amd.com>

Thank you for your contribution! Sashiko AI review found 8 potential
issues to consider:
- [High] cxl: double cxld_unregister() and use-after-free in
  delete_endpoint()
- [High] cxl: endpoint->dev reference leak in detach_memdev() after
  delete_endpoint()
- [High] cxl: invalid pointer dereference in delete_endpoint() on Type2
  probe failure
- [High] cxl: cxl_detach_ep() runs before delete_endpoint() on Type2
  memdev unbind
- [High] cxl: cxlmd->detach_scheduled is never reset and races across
  device locks
- [Medium] cxl: link failure on endpoint_unregister_region() when
  CONFIG_CXL_REGION=n
- [Medium] cxl: reference leak in cxl_memdev_attach_region() on devm
  registration error
- [Medium] cxl: use-after-free in unwinding tracepoints after
  put_device()
--

commit 5ea2db5ceee405003c55ceb45874754b74107942
Author: Alejandro Lucero <alucerop@amd.com>

cxl: make explicit removal of endpoint and decoder for Type2

This commit replaces devm-based endpoint and region teardown for Type 2 CXL
devices with explicit teardown in cxl_memdev_unregister() and
delete_endpoint(), while pinning the endpoint device and adding
schedule_detach actions in cxl_memdev_attach_region().

> diff --git a/drivers/cxl/core/memdev.c b/drivers/cxl/core/memdev.c
> index 7333fe7594bd9..c699c5561037e 100644
> --- a/drivers/cxl/core/memdev.c
> +++ b/drivers/cxl/core/memdev.c
> @@ -758,6 +758,14 @@ static void cxl_memdev_unregister(void *_cxlmd)
>  	struct device *dev = &cxlmd->dev;
>  
>  	trace_cxl_memdev_unregister(cxlmd, "start");
> +
> +	/*
> +	 * Type2 requires to explicitly remove endpoint while Type3 relies on
> +	 * devm mechanisms.
> +	 */
> +	if (cxlmd->attach)
> +		delete_endpoint(cxlmd);
> +

[Severity: High]
Can this dereference an invalid cxlmd->endpoint pointer when a Type 2
memdev fails to probe cxl_mem?

cxl_memdev_alloc() initializes cxlmd->endpoint to ERR_PTR(-ENXIO).  If
cxl_mem_probe() does not run or fails before registering the endpoint
(for example when !cxlds->media_ready returns -EBUSY),
__devm_cxl_add_memdev() -> cxl_memdev_autoremove() sees
cxl_memdev_attach_failed(cxlmd) and calls cxl_memdev_unregister(cxlmd).
Because cxlmd->attach is set, delete_endpoint(cxlmd) runs and immediately
dereferences cxlmd->endpoint in port_to_host(endpoint) and
trace_cxl_delete_endpoint().

>  	cdev_device_del(&cxlmd->cdev, dev);
>  	cxl_memdev_shutdown(dev);
>  	put_device(dev);
> @@ -776,10 +784,14 @@ static void detach_memdev(struct work_struct *work)
>  	 * is required. In that case, @cxlmd detach escalates to parent device
>  	 * detach.
>  	 */
> -	if (cxlmd->attach)
> +	if (cxlmd->attach) {
>  		device_release_driver(cxlmd->dev.parent);
> -	else
> +
> +		/* Type2 attach region did a get. */
> +		put_device(&cxlmd->endpoint->dev);
> +	} else {
>  		device_release_driver(&cxlmd->dev);
> +	}

[Severity: High]
Does put_device(&cxlmd->endpoint->dev) here leak the endpoint->dev
reference taken in cxl_memdev_attach_region()?

device_release_driver(cxlmd->dev.parent) synchronously releases devres on
the parent device, which runs cxl_memdev_unregister() ->
delete_endpoint(cxlmd).  delete_endpoint() sets cxlmd->endpoint = NULL and
only drops the reference taken in cxl_endpoint_autoremove().  When control
returns to detach_memdev(), cxlmd->endpoint is NULL (and since dev is at
offset 0 of struct cxl_port, put_device(NULL) is a no-op).  The extra
reference is also leaked if cxlmd->dev.parent is unbound directly without
going through detach_memdev().

>  
>  	put_device(&cxlmd->dev);
>  	trace_cxl_memdev_detach(cxlmd, "done");

[Severity: Medium]
Can trace_cxl_memdev_detach(cxlmd, "done") read freed memory after
put_device(&cxlmd->dev)?

For a Type 2 device, device_release_driver(cxlmd->dev.parent) has already
run cxl_memdev_unregister() and dropped the initial reference on
cxlmd->dev, so put_device(&cxlmd->dev) here drops the final reference and
frees cxlmd before trace_cxl_memdev_detach() reads cxlmd->dev.kobj.name.
The same issue exists in endpoint_unregister_region(), cxld_unregister(),
and delete_endpoint(), where the "done" tracepoints read dev->kobj.name
after put_device() or device_unregister().

> diff --git a/drivers/cxl/core/port.c b/drivers/cxl/core/port.c
> index 9ccb0d1859047..de109fb600386 100644
> --- a/drivers/cxl/core/port.c
> +++ b/drivers/cxl/core/port.c
> @@ -1480,14 +1480,24 @@ static struct device *grandparent(struct device *dev)
>  	return NULL;
>  }
>  
> -static void delete_endpoint(void *data)
> +static void cxld_unregister(void *dev);
> +
> +void delete_endpoint(void *data)
>  {
>  	struct cxl_memdev *cxlmd = data;
>  	struct cxl_port *endpoint = cxlmd->endpoint;
>  	struct device *host = port_to_host(endpoint);
> +	struct cxl_attach_region *attach =
> +		container_of(cxlmd->attach, typeof(*attach), attach);
>  
>  	trace_cxl_delete_endpoint(&endpoint->dev, "start");
>  
> +	/* Type2 requires to explicitly remove region and decoder */
> +	if (attach && attach->cxlr) {
> +		endpoint_unregister_region(attach->cxlr);
> +		cxld_unregister(&attach->cxled->cxld.dev);
> +	}

[Severity: High]
Won't calling cxld_unregister(&attach->cxled->cxld.dev) directly here
cause a double unregister and use-after-free of attach->cxled?

add_hdm_decoder() still registers cxld_unregister as a devm action on
&endpoint->dev via cxl_decoder_autoremove() for both Type 2 and Type 3
devices, and cxl_memdev_attach_region() drops the device_find_child()
reference on &cxled->cxld.dev when dev goes out of scope.  Calling
cxld_unregister() directly in delete_endpoint() unregisters the decoder
while leaving the devm action queued on &endpoint->dev; a few lines below,
devm_release_action(host, unregister_port, endpoint) unregisters
&endpoint->dev and runs devres_release_all(&endpoint->dev), which calls
cxld_unregister() a second time and then runs cxl_dpa_release() on the
freed decoder.

Conversely, if &endpoint->dev is unbound via sysfs first,
__device_release_driver(&endpoint->dev) runs devres_release_all() and
frees attach->cxled immediately (get_device(&endpoint->dev) only prevents
device_release(), not driver unbind devres cleanup), after which
delete_endpoint() calls cxld_unregister() on the already-freed
attach->cxled.

> +
>  	scoped_guard(device, host) {
>  		if (host->driver && !endpoint->dead) {
>  			devm_release_action(host, cxl_unlink_parent_dport, endpoint);
> @@ -1509,7 +1519,10 @@ int cxl_endpoint_autoremove(struct cxl_memdev *cxlmd, struct cxl_port *endpoint)
>  	get_device(host);
>  	get_device(&endpoint->dev);
>  	cxlmd->depth = endpoint->depth;
> -	return devm_add_action_or_reset(dev, delete_endpoint, cxlmd);
> +	/* Only Type3 relies on devm for decoder unwinding. */
> +	if (cxlmd->cxlds->type == CXL_DEVTYPE_CLASSMEM)
> +		return devm_add_action_or_reset(dev, delete_endpoint, cxlmd);
> +	return 0;
>  }
>  EXPORT_SYMBOL_NS_GPL(cxl_endpoint_autoremove, "CXL");

[Severity: High]
When cxlmd->dev is unbound on a Type 2 device, doesn't skipping the
devm action here cause cxl_detach_ep() to run before delete_endpoint()?

devm_cxl_enumerate_ports() registers cxl_detach_ep on &cxlmd->dev.
Previously delete_endpoint was registered on &cxlmd->dev after
cxl_detach_ep, so devres ran delete_endpoint() first.  Now, when
&cxlmd->dev unbinds (or fails in cxl_memdev_attach_region() after
devm_cxl_add_endpoint()), devres on &cxlmd->dev runs cxl_detach_ep()
before cxl_memdev_unregister() calls delete_endpoint().  cxl_detach_ep()
frees the struct cxl_ep entries in ancestor ports and deletes empty switch
ports while the endpoint port and region are still attached, causing
delete_endpoint() to hit dangling cxl_ep pointers (or NULL ep in
cxl_region_teardown_targets() / cxl_region_decode_reset()) or skip
unregister_port(endpoint) if a parent switch port's driver was detached.

> @@ -2413,6 +2426,12 @@ EXPORT_SYMBOL_NS_GPL(cxl_bus_drain, "CXL");
>  bool schedule_cxl_memdev_detach(struct cxl_memdev *cxlmd)
>  {
>  	trace_schedule_cxl_memdev_detach(cxlmd);
> +
> +	if (cxlmd->detach_scheduled)
> +		return 0;
> +
> +	cxlmd->detach_scheduled = true;
> +
>  	return queue_work(cxl_bus_wq, &cxlmd->detach_work);
>  }
>  EXPORT_SYMBOL_NS_GPL(schedule_cxl_memdev_detach, "CXL");

[Severity: High]
Since cxlmd->detach_scheduled is never cleared in detach_memdev(), won't
a Type 3 cxl_memdev that is detached and later re-probed fail to detach on
any subsequent port removal and leak the cxlmd->dev reference taken by
cxl_endpoint_port_probe()?

In addition, cxlmd->detach_scheduled is a plain bool accessed without
synchronization from schedule_detach actions on three different devices
(&cxlr->dev, &endpoint->dev, and &cxlmd->dev), and if a Type 2 parent
driver is unbound directly while cxlmd->detach_scheduled is still false,
cxl_memdev_unregister() will trigger those schedule_detach actions and
queue detach_work while cxlmd is already being unregistered.

> diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
> index efbd53a37405d..6a188041791c2 100644
> --- a/drivers/cxl/core/region.c
> +++ b/drivers/cxl/core/region.c

[ ... ]

> @@ -4177,11 +4177,33 @@ int cxl_memdev_attach_region(struct cxl_memdev *cxlmd)
>  	/* Only teardown regions that pass validation, ignore the rest */
>  	get_device(&cxlr->dev);
>  
> -	rc = devm_add_action_or_reset(&endpoint->dev,
> -				      endpoint_unregister_region, cxlr);
> +	/*
> +	 * While region in use by the Type2 device driver, no unbinding can
> +	 * happen. Keeping a reference to the endpoint device avoids release.
> +	 * The endpoint can be released after unwinding from the memdev forced
> +	 * with the below actions.
> +	 */
> +	get_device(&endpoint->dev);
> +
> +	/*
> +	 * All these next devm actions are necessary for dealing with unbind
> +	 * triggered through sysfs with the CXL objects linked to a Type2.
> +	 */
> +	rc = devm_add_action_or_reset(&cxlr->dev, schedule_detach, cxlmd);
> +	if (rc)
> +		return rc;
> +
> +	rc = devm_add_action_or_reset(&endpoint->dev, schedule_detach, cxlmd);
> +
> +	if (rc)
> +		return rc;
> +
> +	rc = devm_add_action_or_reset(&cxlmd->dev, schedule_detach, cxlmd);
>  	if (rc)
>  		return rc;
>  
> +	attach->cxlr = cxlr;
> +	attach->cxled = cxled;

[Severity: Medium]
If any of the devm_add_action_or_reset() calls above fails, aren't the
cxlr->dev and endpoint->dev references leaked?

attach->cxlr is only assigned after all three devm_add_action_or_reset()
calls succeed, so on early return delete_endpoint() sees
attach->cxlr == NULL and skips endpoint_unregister_region(attach->cxlr).

> diff --git a/drivers/cxl/cxl.h b/drivers/cxl/cxl.h
> index baa6df2d362a4..eaa663f25699a 100644
> --- a/drivers/cxl/cxl.h
> +++ b/drivers/cxl/cxl.h
> @@ -942,6 +942,9 @@ void cxl_coordinates_combine(struct access_coordinate *out,
>  			     struct access_coordinate *c1,
>  			     struct access_coordinate *c2);
>  
> +void endpoint_unregister_region(void *_cxlr);
> +void delete_endpoint(void *data);
> +

[Severity: Medium]
Does building with CONFIG_CXL_BUS enabled and CONFIG_CXL_REGION=n fail to
link on endpoint_unregister_region()?

drivers/cxl/core/port.o is built unconditionally under CONFIG_CXL_BUS and
calls endpoint_unregister_region() from delete_endpoint(), while
endpoint_unregister_region() in drivers/cxl/core/region.c is only built
when CONFIG_CXL_REGION is enabled and has no !CONFIG_CXL_REGION stub in
drivers/cxl/cxl.h.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261118191431.21781-2-alucerop@amd.com?part=2

  reply	other threads:[~2026-11-18 18:09 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 19:14 [RFC 0/3] Type2 unwinding ordering change alucerop
2026-11-18 19:14 ` [RFC 1/3] cxl: add tracepoints to the unwinding code path alucerop
2026-11-18 18:00   ` sashiko-bot
2026-11-18 19:14 ` [RFC 2/3] cxl: make explicit removal of endpoint and decoder for Type2 alucerop
2026-11-18 18:09   ` sashiko-bot [this message]
2026-11-18 19:14 ` [RFC 3/3] Documentation/cxl: add document about Type2 binding and unbinding alucerop
2026-11-18 18:04   ` sashiko-bot
2026-11-18 23:39 ` [RFC 0/3] Type2 unwinding ordering change Dave Jiang
2026-11-19  8:13   ` Lucero Palau, Alejandro
2026-11-19 10:00   ` Lucero Palau, Alejandro

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=sashiko-outbox-221725@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=alucerop@amd.com \
    --cc=linux-cxl@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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