Linux CXL
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: alucerop@amd.com, linux-cxl@vger.kernel.org
Subject: Re: [RFC 1/3] cxl: add tracepoints to the unwinding code path
Date: Wed, 18 Nov 2026 18:00:30 +0000	[thread overview]
Message-ID: <sashiko-outbox-221716@kernel.org> (raw)
In-Reply-To: <20261118191431.21781-2-alucerop@amd.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] cxl: use-after-free in unwinding tracepoints after kfree() and
  put_device()
- [Low] cxl: unmatched start tracepoint on early return in
  __cxl_decoder_detach()
- [Low] cxl: missing multi-read guards and convention defects in
  tracepoint headers
--

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

cxl: add tracepoints to the unwinding code path

This commit adds start and done tracepoints under the cxl_unwinding trace
system across CXL memdev, port, decoder, and region teardown paths.

> diff --git a/drivers/cxl/core/memdev.c b/drivers/cxl/core/memdev.c
> index 787988e92965a..7333fe7594bd9 100644
> --- a/drivers/cxl/core/memdev.c
> +++ b/drivers/cxl/core/memdev.c
[ ... ]
> @@ -27,9 +30,11 @@ static void cxl_memdev_release(struct device *dev)
>  	struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
>  	struct device *parent = dev->parent;
>  
> +	trace_cxl_memdev_release(cxlmd, "start");
>  	ida_free(&cxl_memdev_ida, cxlmd->id);
>  	kfree(cxlmd);
>  	put_device(parent);
> +	trace_cxl_memdev_release(cxlmd, "done");
>  }
>  
>  static char *cxl_memdev_devnode(const struct device *dev, umode_t *mode, kuid_t *uid,
> @@ -752,9 +757,11 @@ static void cxl_memdev_unregister(void *_cxlmd)
>  	struct cxl_memdev *cxlmd = _cxlmd;
>  	struct device *dev = &cxlmd->dev;
>  
> +	trace_cxl_memdev_unregister(cxlmd, "start");
>  	cdev_device_del(&cxlmd->cdev, dev);
>  	cxl_memdev_shutdown(dev);
>  	put_device(dev);
> +	trace_cxl_memdev_unregister(cxlmd, "done");
>  }
>  
>  static void detach_memdev(struct work_struct *work)
> @@ -763,6 +770,7 @@ static void detach_memdev(struct work_struct *work)
>  
>  	cxlmd = container_of(work, typeof(*cxlmd), detach_work);
>  
> +	trace_cxl_memdev_detach(cxlmd, "start");
>  	/*
>  	 * When the creator of @cxlmd sets ->attach it indicates CXL operation
>  	 * is required. In that case, @cxlmd detach escalates to parent device
> @@ -772,7 +780,9 @@ static void detach_memdev(struct work_struct *work)
>  		device_release_driver(cxlmd->dev.parent);
>  	else
>  		device_release_driver(&cxlmd->dev);
> +
>  	put_device(&cxlmd->dev);
> +	trace_cxl_memdev_detach(cxlmd, "done");
>  }
>  
>  static struct lock_class_key cxl_memdev_key;
> diff --git a/drivers/cxl/core/port.c b/drivers/cxl/core/port.c
> index f3705ea44db77..9ccb0d1859047 100644
> --- a/drivers/cxl/core/port.c
> +++ b/drivers/cxl/core/port.c
[ ... ]
> @@ -540,8 +544,10 @@ EXPORT_SYMBOL_NS_GPL(to_cxl_switch_decoder, "CXL");
>  
>  static void cxl_ep_release(struct cxl_ep *ep)
>  {
> +	trace_cxl_ep_release(ep->ep, "start");
>  	put_device(ep->ep);
>  	kfree(ep);
> +	trace_cxl_ep_release(ep->ep, "done");
>  }
>  
>  static void cxl_ep_remove(struct cxl_port *port, struct cxl_ep *ep)
> @@ -558,6 +564,7 @@ static void cxl_port_release(struct device *dev)
>  	unsigned long index;
>  	struct cxl_ep *ep;
>  
> +	trace_cxl_port_release(dev, "start");
>  	xa_for_each(&port->endpoints, index, ep)
>  		cxl_ep_remove(port, ep);
>  	xa_destroy(&port->endpoints);
> @@ -571,6 +578,7 @@ static void cxl_port_release(struct device *dev)
>  		put_device(dev->parent);
>  		kfree(port);
>  	}
> +	trace_cxl_port_release(dev, "done");
>  }
>  
>  static ssize_t decoders_committed_show(struct device *dev,
> @@ -1478,6 +1486,8 @@ static void delete_endpoint(void *data)
>  	struct cxl_port *endpoint = cxlmd->endpoint;
>  	struct device *host = port_to_host(endpoint);
>  
> +	trace_cxl_delete_endpoint(&endpoint->dev, "start");
> +
>  	scoped_guard(device, host) {
>  		if (host->driver && !endpoint->dead) {
>  			devm_release_action(host, cxl_unlink_parent_dport, endpoint);
> @@ -1488,6 +1498,7 @@ static void delete_endpoint(void *data)
>  	}
>  	put_device(&endpoint->dev);
>  	put_device(host);
> +	trace_cxl_delete_endpoint(&endpoint->dev, "done");
>  }
[ ... ]
> @@ -2283,6 +2296,7 @@ EXPORT_SYMBOL_NS_GPL(cxl_decoder_add, "CXL");
>  
>  static void cxld_unregister(void *dev)
>  {
> +	trace_cxld_unregister(dev, "start");
>  	if (is_endpoint_decoder(dev))
>  		cxl_decoder_detach(NULL, to_cxl_endpoint_decoder(dev), -1,
>  				   DETACH_INVALIDATE);
> @@ -2290,6 +2304,7 @@ static void cxld_unregister(void *dev)
>  		kill_regions(to_cxl_root_decoder(dev));
>  
>  	device_unregister(dev);
> +	trace_cxld_unregister(dev, "done");
>  }
>  
>  int cxl_decoder_autoremove(struct device *host, struct cxl_decoder *cxld)
[ ... ]
> diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
> index a048bd5c06cb6..efbd53a37405d 100644
> --- a/drivers/cxl/core/region.c
> +++ b/drivers/cxl/core/region.c
[ ... ]
> @@ -2275,6 +2278,7 @@ __cxl_decoder_detach(struct cxl_region *cxlr,
>  	lockdep_assert_held_write(&cxl_rwsem.region);
>  
>  	if (!cxled) {
> +		trace_cxl_decoder_detach(&cxlr->dev, "start");
>  		p = &cxlr->params;
>  
>  		if (pos >= p->interleave_ways) {

[Severity: Low]
Does emitting trace_cxl_decoder_detach(&cxlr->dev, "start") before
checking pos and p->targets[pos] leave an unmatched "start" trace event
when __cxl_decoder_detach() returns NULL early?

When unregister_region() loops over p->interleave_ways calling
detach_target(cxlr, i), any target already detached by cxld_unregister()
has p->targets[pos] == NULL and returns without a matching "done" event
(as does the dev_WARN_ONCE() return later in __cxl_decoder_detach()).

> @@ -2521,6 +2528,7 @@ static void cxl_region_release(struct device *dev)
>  	struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
>  	int id = atomic_read(&cxlrd->region_id);
>  
> +	trace_cxl_region_release(dev, "start");
>  	/*
>  	 * Try to reuse the recently idled id rather than the cached
>  	 * next id to prevent the region id space from increasing
> @@ -2536,6 +2544,7 @@ static void cxl_region_release(struct device *dev)
>  out:
>  	put_device(dev->parent);
>  	kfree(cxlr);
> +	trace_cxl_region_release(dev, "done");
>  }
>  
>  const struct device_type cxl_region_type = {
> @@ -2565,6 +2574,7 @@ static void unregister_region(struct cxl_region *cxlr)
>  	struct cxl_region_params *p = &cxlr->params;
>  	int i;
>  
> +	trace_cxl_unregister_region(&cxlr->dev, "start");
>  	xa_erase(&cxlrd->regions, cxlr->id);
>  	device_del(&cxlr->dev);
>  
> @@ -2580,6 +2590,7 @@ static void unregister_region(struct cxl_region *cxlr)
>  
>  	cxl_region_iomem_release(cxlr);
>  	put_device(&cxlr->dev);
> +	trace_cxl_unregister_region(&cxlr->dev, "done");
>  }
>  
>  static void endpoint_unregister_region(void *_cxlr)
> @@ -2587,10 +2598,12 @@ static void endpoint_unregister_region(void *_cxlr)
>  	struct cxl_region *cxlr = _cxlr;
>  	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
>  
> +	trace_cxl_endpoint_unregister_region(&cxlr->dev, "start");
>  	guard(mutex)(&cxlrd->regions_lock);
>  	if (xa_load(&cxlrd->regions, cxlr->id))
>  		unregister_region(cxlr);
>  	put_device(&cxlr->dev);
> +	trace_cxl_endpoint_unregister_region(&cxlr->dev, "done");
>  }

[Severity: High]
Do the "done" tracepoints in these release and unregister callbacks trigger
a use-after-free?

Each of these tracepoints reads dev->kobj.name (or cxlmd->dev.kobj.name),
but the "done" call runs after kfree(), put_device(), or
device_unregister() has already freed the object and its kobject name:

- cxl_ep_release() calls kfree(ep) after put_device(ep->ep), and then
  trace_cxl_ep_release(ep->ep, "done") evaluates ep->ep from the freed
  struct cxl_ep even when tracing is disabled.
- cxl_memdev_release(), cxl_port_release(), and cxl_region_release() call
  kfree() on the enclosing struct before the "done" tracepoint reads
  dev->kobj.name.
- cxl_memdev_unregister(), detach_memdev(), delete_endpoint(),
  cxld_unregister(), unregister_region(), and
  endpoint_unregister_region() drop the final device reference via
  put_device() or device_unregister() before calling the "done"
  tracepoint.

> diff --git a/drivers/cxl/cxl_memdev_tracepoints.h b/drivers/cxl/cxl_memdev_tracepoints.h
> new file mode 100644
> index 0000000000000..6e8206cf178a0
> --- /dev/null
> +++ b/drivers/cxl/cxl_memdev_tracepoints.h
> @@ -0,0 +1,58 @@
[ ... ]
> +#undef TRACE_SYSTEM
> +#define TRACE_SYSTEM cxl_unwinding
> +
> +#include <linux/tracepoint.h>
> +#include "cxlmem.h"
> +
> +TRACE_EVENT(cxl_memdev_release,
> +	    TP_PROTO(struct cxl_memdev *cxlmd, char *action),
> +	    TP_ARGS(cxlmd, action),
> +	    TP_STRUCT__entry(__string(devname, cxlmd->dev.kobj.name)
> +			     __string(action, action)
> +	    ),
> +	    TP_fast_assign(__assign_str(devname);
> +			   __assign_str(action);
> +	    ),
> +	    TP_printk("%s, %s", __get_str(devname), __get_str(action))
> +);
[ ... ]
> +#undef TRACE_INCLUDE_PATH
> +#define TRACE_INCLUDE_PATH .
> +#undef TRACE_INCLUDE_FILE
> +#define TRACE_INCLUDE_FILE cxl_memdev_tracepoints
> +
> +#include <trace/define_trace.h>
> diff --git a/drivers/cxl/cxl_port_tracepoints.h b/drivers/cxl/cxl_port_tracepoints.h
> new file mode 100644
> index 0000000000000..faefa418d9684
> --- /dev/null
> +++ b/drivers/cxl/cxl_port_tracepoints.h
> @@ -0,0 +1,91 @@
[ ... ]
> +TRACE_EVENT(schedule_cxl_memdev_detach,
> +	    TP_PROTO(struct cxl_memdev *cxlmd),
> +	    TP_ARGS(cxlmd),
> +	    TP_STRUCT__entry(__string(devname, cxlmd->dev.kobj.name)),
> +	    TP_fast_assign(__assign_str(devname);
> +	    ),
> +	    TP_printk("%s", __get_str(devname))
> +);
[ ... ]
> +TRACE_EVENT(cxld_unregister,
> +	    TP_PROTO(struct device *dev, char *action),
[ ... ]
> diff --git a/drivers/cxl/cxl_region_tracepoints.h b/drivers/cxl/cxl_region_tracepoints.h
> new file mode 100644
> index 0000000000000..977eb45b6aad8
> --- /dev/null
> +++ b/drivers/cxl/cxl_region_tracepoints.h
> @@ -0,0 +1,78 @@
[ ... ]
> +#undef TRACE_SYSTEM
> +#define TRACE_SYSTEM cxl_unwinding
> +
> +#include <linux/tracepoint.h>
> +
> +TRACE_EVENT(cxl_decoder_detach,
> +	    TP_PROTO(struct device *dev, char *action),

[Severity: Low]
Should these new tracepoint headers wrap their definitions in a
"#if !defined(...) || defined(TRACE_HEADER_MULTI_READ)" guard with #endif
placed before #include <trace/define_trace.h>, like
drivers/cxl/core/trace.h?

Also, schedule_cxl_memdev_detach and cxld_unregister do not start with the
cxl_ subsystem prefix, the events read dev->kobj.name directly instead of
using dev_name(dev) (and take char *action instead of const char *action),
and the twelve identical (dev, action) TRACE_EVENT definitions could share
a DECLARE_EVENT_CLASS in drivers/cxl/core/trace.h.

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

  reply	other threads:[~2026-11-18 18:00 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 [this message]
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
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-221716@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