[RFC v2 04/15] cxl: introduce devm_cxl_del_memdev()

mhonap
2025-12-09T16:51:50Z

From: Zhi Wang <zhiw@nvidia.com>

The teardown path of kernel CXL core heavily leverages the device
resource manager. Thus, the lifecycle of many created resources are
tied to the refcount of parent object and the resourced are freed
when the parent object is freed.

However, this creates a gap when an external caller wants to swept the
resource but keep the parent object for a re-initialization sequence.
E.g. in vfio-cxl.

Introduce the devm_cxl_del_memdev() for an external caller to destroy
the CXL memdev.

Signed-off-by: Zhi Wang <zhiw@nvidia.com>
Signed-off-by: Manish Honap <mhonap@nvidia.com>
---
drivers/cxl/core/memdev.c | 6 ++++++
include/cxl/cxl.h | 1 +
2 files changed, 7 insertions(+)

diff --git a/drivers/cxl/core/memdev.c b/drivers/cxl/core/memdev.c
index 9de2ecb2abdc..d281843fb2f4 100644
--- a/drivers/cxl/core/memdev.c
+++ b/drivers/cxl/core/memdev.c
@@ -775,6 +775,12 @@ int devm_cxl_memdev_add_or_reset(struct device *host, struct cxl_memdev *cxlmd)
}
EXPORT_SYMBOL_NS_GPL(devm_cxl_memdev_add_or_reset, "CXL");

+void devm_cxl_del_memdev(struct device *host, struct cxl_memdev *cxlmd)
+{
+ devm_release_action(host, cxl_memdev_unregister, cxlmd);
+}
+EXPORT_SYMBOL_NS_GPL(devm_cxl_del_memdev, "CXL");
+
static long __cxl_memdev_ioctl(struct cxl_memdev *cxlmd, unsigned int cmd,
unsigned long arg)
{
diff --git a/include/cxl/cxl.h b/include/cxl/cxl.h
index 28a39bfd74bc..e3bf8cf0b6d6 100644
--- a/include/cxl/cxl.h
+++ b/include/cxl/cxl.h
@@ -267,6 +267,7 @@ int cxl_await_range_active(struct cxl_dev_state *cxlds);
struct cxl_memdev *devm_cxl_add_memdev(struct device *host,
struct cxl_dev_state *cxlds,
const struct cxl_memdev_ops *ops);
+void devm_cxl_del_memdev(struct device *host, struct cxl_memdev *cxlmd);
struct cxl_port;
struct cxl_root_decoder *cxl_get_hpa_freespace(struct cxl_memdev *cxlmd,
int interleave_ways,
--
2.25.1