Skip to contentSkip to conversation

cxl: Introduce devm_cxl_probe_mem() and fix region deletion

PATCH In review Dan Williams 3 participants 23 messages May 19 – Jun 12 CXL

lore
Share & tools
lore

Dan Williams

author maintainer
#0 lore raw

cover cxl: Introduce devm_cxl_probe_mem() and fix region deletion

The devm_cxl_probe_mem() helper creates a memdev and retrieves a mapped HPA range for the device. At present the retrieved region information is expected to be established by platform firmware. A future update will auto-establish a region if one is not already present. This arranges for a simple CXL topology detach == Accelerator driver detach scheme.

Now, automatic region establishment on behalf of an accelerator driver also requires automatic region teardown. The existing scheme of triggering devm_release_action() of a cxl root device to destroy a region is racy [1]. There is no path to coordinate multiple devres actions, or conflicting devres actions, without using the devm_remove_action_nowarn() workaround.

Instead, move final region cleanup responsibility to the root decoder teardown path (devres action cxld_unregister()). Expand the root decoder @range_lock, renamed @regions_lock, to coordinate region creation, sysfs manageability, and deletion. The @regions_lock also prevents regions from disappearing mid-auto-assembly.

Changes from the initial RFC [2]:
* tested with Dave's type2 cxl_test
* fix region remove by endpoints (Dave, Alejandro)
* fix user triggered region remove (Sungwoo)
* fix lifetime of the @attach ops (Alison, sashiko)
* hide @attach details inside the CXL core

[1]: http://lore.kernel.org/[email protected]
[2]: http://lore.kernel.org/[email protected]

Dan Williams (5):
cxl/region: Block region delete during region creation cxl/region: Resolve region deletion races cxl/memdev: Pin parents for entire memdev lifetime cxl/memdev: Introduce cxl_class_memdev_type cxl/region: Introduce devm_cxl_probe_mem()

 drivers/cxl/core/core.h      |   2 +
 drivers/cxl/cxl.h            |   8 +-
 drivers/cxl/cxlmem.h         |  27 +++--
 include/cxl/cxl.h            |   3 +
 drivers/cxl/core/memdev.c    |  20 +++-
 drivers/cxl/core/port.c      |   7 +-
 drivers/cxl/core/region.c    | 194 ++++++++++++++++++++++++++++++-----
 drivers/cxl/mem.c            |  95 +++++++++++++----
 drivers/cxl/pci.c            |   2 +-
 tools/testing/cxl/test/mem.c |   2 +-
 10 files changed, 297 insertions(+), 63 deletions(-)

base-commit: 5d6919055dec134de3c40167a490f33c74c12581

Signature · 1 line
2.53.0
author maintainer
#1 lore raw

PATCH 1/5 cxl/region: Block region delete during region creation

Expand the range lock, rename it "regions_lock", to disable region deletion in the critical period between construct_region() and attach_target(), as well as the period between device_add() and registering the remove actions.

Otherwise, userspace can confuse the kernel. It can violate the assumption the region stays registered through the completion of cxl_add_to_region(). It can violate the assumption that devm_add_action_or_reset() is working with a live 'struct cxl_region'.

It is ok for the region to disappear outside of those windows as that mirrors device hotplug flows where the proper locks are held.

Fixes a32320b71f08 ("cxl/region: Add region autodiscovery")
---
 drivers/cxl/cxl.h         |  4 ++--
 drivers/cxl/core/port.c   |  2 +-
 drivers/cxl/core/region.c | 12 ++++++++++--
 3 files changed, 13 insertions(+), 5 deletions(-)
drivers/cxl/cxl.h +2 -2
index 1297594beaec..3900a0778571 100644
@@ -359,7 +359,7 @@ struct cxl_rd_ops {
  * @cache_size: extended linear cache size if exists, otherwise zero.
  * @region_id: region id for next region provisioning event
  * @platform_data: platform specific configuration data
- * @range_lock: sync region autodiscovery by address range
+ * @regions_lock: sync region discovery, construction, and deletion
  * @qos_class: QoS performance class cookie
  * @ops: CXL root decoder operations
  * @cxlsd: base cxl switch decoder
@@ -369,7 +369,7 @@ struct cxl_root_decoder {
 	resource_size_t cache_size;
 	atomic_t region_id;
 	void *platform_data;
-	struct mutex range_lock;
+	struct mutex regions_lock;
 	int qos_class;
 	struct cxl_rd_ops ops;
 	struct cxl_switch_decoder cxlsd;
drivers/cxl/core/port.c +1 -1
index c5aacd7054f1..6e7a70d51cfe 100644
@@ -2016,7 +2016,7 @@ struct cxl_root_decoder *cxl_root_decoder_alloc(struct cxl_port *port,
 		return ERR_PTR(rc);
 	}
 
-	mutex_init(&cxlrd->range_lock);
+	mutex_init(&cxlrd->regions_lock);
 
 	cxld = &cxlsd->cxld;
 	cxld->dev.type = &cxl_decoder_root_type;
drivers/cxl/core/region.c +10 -2
index e50dc716d4e8..b5601e89e302 100644
@@ -2779,6 +2779,10 @@ static ssize_t create_region_store(struct device *dev, const char *buf,
 	if (rc != 1)
 		return -EINVAL;
 
+	ACQUIRE(mutex_intr, regions_lock)(&cxlrd->regions_lock);
+	if ((rc = ACQUIRE_ERR(mutex_intr, &regions_lock)))
+		return rc;
+
 	cxlr = __create_region(cxlrd, mode, id, CXL_DECODER_HOSTONLYMEM);
 	if (IS_ERR(cxlr))
 		return PTR_ERR(cxlr);
@@ -2838,6 +2842,11 @@ static ssize_t delete_region_store(struct device *dev,
 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
 	struct cxl_port *port = to_cxl_port(dev->parent);
 	struct cxl_region *cxlr;
+	int rc;
+
+	ACQUIRE(mutex_intr, regions_lock)(&cxlrd->regions_lock);
+	if ((rc = ACQUIRE_ERR(mutex_intr, &regions_lock)))
+		return rc;
 
 	cxlr = cxl_find_region_by_name(cxlrd, buf);
 	if (IS_ERR(cxlr))
@@ -3776,12 +3785,11 @@ int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
 	 * for the HPA range, one does the construction and the others
 	 * add to that.
 	 */
-	mutex_lock(&cxlrd->range_lock);
+	guard(mutex)(&cxlrd->regions_lock);
 	struct cxl_region *cxlr __free(put_cxl_region) =
 		cxl_find_region_by_range(cxlrd, &ctx.hpa_range);
 	if (!cxlr)
 		cxlr = construct_region(cxlrd, &ctx);
-	mutex_unlock(&cxlrd->range_lock);
 
 	rc = PTR_ERR_OR_ZERO(cxlr);
 	if (rc)
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] cxl/region: Block region delete during region creation

Quoted 9 lines · Dan Williams

Expand the range lock, rename it "regions_lock", to disable region deletion in the critical period between construct_region() and attach_target(), as well as the period between device_add() and registering the remove actions.

Otherwise, userspace can confuse the kernel. It can violate the assumption the region stays registered through the completion of cxl_add_to_region(). It can violate the assumption that devm_add_action_or_reset() is working with a live 'struct cxl_region'.

Quoted 5 lines

It is ok for the region to disappear outside of those windows as that mirrors device hotplug flows where the proper locks are held.

Fixes a32320b71f08 ("cxl/region: Add region autodiscovery")
Quoted 82 lines
---
 drivers/cxl/cxl.h         |  4 ++--
 drivers/cxl/core/port.c   |  2 +-
 drivers/cxl/core/region.c | 12 ++++++++++--
 3 files changed, 13 insertions(+), 5 deletions(-)
drivers/cxl/cxl.h +2 -2
index 1297594beaec..3900a0778571 100644
@@ -359,7 +359,7 @@ struct cxl_rd_ops {
  * @cache_size: extended linear cache size if exists, otherwise zero.
  * @region_id: region id for next region provisioning event
  * @platform_data: platform specific configuration data
- * @range_lock: sync region autodiscovery by address range
+ * @regions_lock: sync region discovery, construction, and deletion
  * @qos_class: QoS performance class cookie
  * @ops: CXL root decoder operations
  * @cxlsd: base cxl switch decoder
@@ -369,7 +369,7 @@ struct cxl_root_decoder {
 	resource_size_t cache_size;
 	atomic_t region_id;
 	void *platform_data;
-	struct mutex range_lock;
+	struct mutex regions_lock;
 	int qos_class;
 	struct cxl_rd_ops ops;
 	struct cxl_switch_decoder cxlsd;
drivers/cxl/core/port.c +1 -1
index c5aacd7054f1..6e7a70d51cfe 100644
@@ -2016,7 +2016,7 @@ struct cxl_root_decoder *cxl_root_decoder_alloc(struct cxl_port *port,
 		return ERR_PTR(rc);
 	}
 
-	mutex_init(&cxlrd->range_lock);
+	mutex_init(&cxlrd->regions_lock);
 
 	cxld = &cxlsd->cxld;
 	cxld->dev.type = &cxl_decoder_root_type;
drivers/cxl/core/region.c +10 -2
index e50dc716d4e8..b5601e89e302 100644
@@ -2779,6 +2779,10 @@ static ssize_t create_region_store(struct device *dev, const char *buf,
 	if (rc != 1)
 		return -EINVAL;
 
+	ACQUIRE(mutex_intr, regions_lock)(&cxlrd->regions_lock);
+	if ((rc = ACQUIRE_ERR(mutex_intr, &regions_lock)))
+		return rc;
+
 	cxlr = __create_region(cxlrd, mode, id, CXL_DECODER_HOSTONLYMEM);
 	if (IS_ERR(cxlr))
 		return PTR_ERR(cxlr);
@@ -2838,6 +2842,11 @@ static ssize_t delete_region_store(struct device *dev,
 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
 	struct cxl_port *port = to_cxl_port(dev->parent);
 	struct cxl_region *cxlr;
+	int rc;
+
+	ACQUIRE(mutex_intr, regions_lock)(&cxlrd->regions_lock);
+	if ((rc = ACQUIRE_ERR(mutex_intr, &regions_lock)))
+		return rc;
 
 	cxlr = cxl_find_region_by_name(cxlrd, buf);
 	if (IS_ERR(cxlr))
@@ -3776,12 +3785,11 @@ int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
 	 * for the HPA range, one does the construction and the others
 	 * add to that.
 	 */
-	mutex_lock(&cxlrd->range_lock);
+	guard(mutex)(&cxlrd->regions_lock);
 	struct cxl_region *cxlr __free(put_cxl_region) =
 		cxl_find_region_by_range(cxlrd, &ctx.hpa_range);
 	if (!cxlr)
 		cxlr = construct_region(cxlrd, &ctx);
-	mutex_unlock(&cxlrd->range_lock);
 
 	rc = PTR_ERR_OR_ZERO(cxlr);
 	if (rc)
Reviewed-by Tested-by on v1 1/5
#3 lore raw

Re: [PATCH 1/5] cxl/region: Block region delete during region creation

In reply to Dan Williams
Quoted 9 lines · Dan Williams

Expand the range lock, rename it "regions_lock", to disable region deletion in the critical period between construct_region() and attach_target(), as well as the period between device_add() and registering the remove actions.

Otherwise, userspace can confuse the kernel. It can violate the assumption the region stays registered through the completion of cxl_add_to_region(). It can violate the assumption that devm_add_action_or_reset() is working with a live 'struct cxl_region'.

Quoted 5 lines

It is ok for the region to disappear outside of those windows as that mirrors device hotplug flows where the proper locks are held.

Fixes a32320b71f08 ("cxl/region: Add region autodiscovery")
Quoted 82 lines
---
  drivers/cxl/cxl.h         |  4 ++--
  drivers/cxl/core/port.c   |  2 +-
  drivers/cxl/core/region.c | 12 ++++++++++--
  3 files changed, 13 insertions(+), 5 deletions(-)
drivers/cxl/cxl.h +2 -2
index 1297594beaec..3900a0778571 100644
@@ -359,7 +359,7 @@ struct cxl_rd_ops {
   * @cache_size: extended linear cache size if exists, otherwise zero.
   * @region_id: region id for next region provisioning event
   * @platform_data: platform specific configuration data
- * @range_lock: sync region autodiscovery by address range
+ * @regions_lock: sync region discovery, construction, and deletion
   * @qos_class: QoS performance class cookie
   * @ops: CXL root decoder operations
   * @cxlsd: base cxl switch decoder
@@ -369,7 +369,7 @@ struct cxl_root_decoder {
  	resource_size_t cache_size;
  	atomic_t region_id;
  	void *platform_data;
-	struct mutex range_lock;
+	struct mutex regions_lock;
  	int qos_class;
  	struct cxl_rd_ops ops;
  	struct cxl_switch_decoder cxlsd;
drivers/cxl/core/port.c +1 -1
index c5aacd7054f1..6e7a70d51cfe 100644
@@ -2016,7 +2016,7 @@ struct cxl_root_decoder *cxl_root_decoder_alloc(struct cxl_port *port,
  		return ERR_PTR(rc);
  	}
 
-	mutex_init(&cxlrd->range_lock);
+	mutex_init(&cxlrd->regions_lock);
 
  	cxld = &cxlsd->cxld;
  	cxld->dev.type = &cxl_decoder_root_type;
drivers/cxl/core/region.c +10 -2
index e50dc716d4e8..b5601e89e302 100644
@@ -2779,6 +2779,10 @@ static ssize_t create_region_store(struct device *dev, const char *buf,
  	if (rc != 1)
  		return -EINVAL;
 
+	ACQUIRE(mutex_intr, regions_lock)(&cxlrd->regions_lock);
+	if ((rc = ACQUIRE_ERR(mutex_intr, &regions_lock)))
+		return rc;
+
  	cxlr = __create_region(cxlrd, mode, id, CXL_DECODER_HOSTONLYMEM);
  	if (IS_ERR(cxlr))
  		return PTR_ERR(cxlr);
@@ -2838,6 +2842,11 @@ static ssize_t delete_region_store(struct device *dev,
  	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
  	struct cxl_port *port = to_cxl_port(dev->parent);
  	struct cxl_region *cxlr;
+	int rc;
+
+	ACQUIRE(mutex_intr, regions_lock)(&cxlrd->regions_lock);
+	if ((rc = ACQUIRE_ERR(mutex_intr, &regions_lock)))
+		return rc;
 
  	cxlr = cxl_find_region_by_name(cxlrd, buf);
  	if (IS_ERR(cxlr))
@@ -3776,12 +3785,11 @@ int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
  	 * for the HPA range, one does the construction and the others
  	 * add to that.
  	 */
-	mutex_lock(&cxlrd->range_lock);
+	guard(mutex)(&cxlrd->regions_lock);
  	struct cxl_region *cxlr __free(put_cxl_region) =
  		cxl_find_region_by_range(cxlrd, &ctx.hpa_range);
  	if (!cxlr)
  		cxlr = construct_region(cxlrd, &ctx);
-	mutex_unlock(&cxlrd->range_lock);
 
  	rc = PTR_ERR_OR_ZERO(cxlr);
  	if (rc)
author maintainer
#4 lore raw

PATCH 2/5 cxl/region: Resolve region deletion races

In reply to Dan Williams

Sungwoo noticed that the sysfs trigger to delete a region may try to delete a region multiple times. It also has no exclusion relative to the kernel releasing the region via CXL root device teardown.

Instead of installing new cxl root devres actions per region, use the existing root decoder unregistration event to remove all remaining regions. An xarray of regions replaces a devres list of regions.

This handles 3 separate issues with the old approach:

1/ sysfs users racing to delete the same region: no longer possible now
that the regions_lock is held over the lookup and deletion.

2/ multiple actions triggering deletion of the same region: solved by
erasing regions while holding @regions_lock, and only proceeding on successful erasure.

3/ userspace racing devres_release_all() to trigger the devres not found
warning: solved by sysfs unregistration not requiring a release action

Fixes 779dd20cfb56 ("cxl/region: Add region creation support")
---
 drivers/cxl/core/core.h   |  2 ++
 drivers/cxl/cxl.h         |  4 +++
 drivers/cxl/core/port.c   |  5 ++++
 drivers/cxl/core/region.c | 60 +++++++++++++++++++++------------------
 4 files changed, 44 insertions(+), 27 deletions(-)
drivers/cxl/core/core.h +2 -0
index 82ca3a476708..07555ae63859 100644
@@ -52,6 +52,7 @@ u64 cxl_dpa_to_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd,
 		   u64 dpa);
 int devm_cxl_add_dax_region(struct cxl_region *cxlr);
 int devm_cxl_add_pmem_region(struct cxl_region *cxlr);
+void kill_regions(struct cxl_root_decoder *cxlrd);
 
 #else
 static inline u64 cxl_dpa_to_hpa(struct cxl_region *cxlr,
@@ -81,6 +82,7 @@ static inline int cxl_region_init(void)
 static inline void cxl_region_exit(void)
 {
 }
+static inline void kill_regions(struct cxl_root_decoder *cxlrd) { };
 #define CXL_REGION_ATTR(x) NULL
 #define CXL_REGION_TYPE(x) NULL
 #define SET_CXL_REGION_ATTR(x)
drivers/cxl/cxl.h +4 -0
index 3900a0778571..f43abd1903ce 100644
@@ -360,6 +360,8 @@ struct cxl_rd_ops {
  * @region_id: region id for next region provisioning event
  * @platform_data: platform specific configuration data
  * @regions_lock: sync region discovery, construction, and deletion
+ * @regions: regions to remove at root decoder destruct time
+ * @dead: root decoder dead to region creation
  * @qos_class: QoS performance class cookie
  * @ops: CXL root decoder operations
  * @cxlsd: base cxl switch decoder
@@ -370,6 +372,8 @@ struct cxl_root_decoder {
 	atomic_t region_id;
 	void *platform_data;
 	struct mutex regions_lock;
+	struct xarray regions;
+	bool dead;
 	int qos_class;
 	struct cxl_rd_ops ops;
 	struct cxl_switch_decoder cxlsd;
drivers/cxl/core/port.c +5 -0
index 6e7a70d51cfe..1215ee4f4035 100644
@@ -458,6 +458,8 @@ static void cxl_root_decoder_release(struct device *dev)
 
 	if (atomic_read(&cxlrd->region_id) >= 0)
 		memregion_free(atomic_read(&cxlrd->region_id));
+	mutex_destroy(&cxlrd->regions_lock);
+	xa_destroy(&cxlrd->regions);
 	__cxl_decoder_release(&cxlrd->cxlsd.cxld);
 	kfree(cxlrd);
 }
@@ -2017,6 +2019,7 @@ struct cxl_root_decoder *cxl_root_decoder_alloc(struct cxl_port *port,
 	}
 
 	mutex_init(&cxlrd->regions_lock);
+	xa_init(&cxlrd->regions);
 
 	cxld = &cxlsd->cxld;
 	cxld->dev.type = &cxl_decoder_root_type;
@@ -2192,6 +2195,8 @@ static void cxld_unregister(void *dev)
 	if (is_endpoint_decoder(dev))
 		cxl_decoder_detach(NULL, to_cxl_endpoint_decoder(dev), -1,
 				   DETACH_INVALIDATE);
+	if (is_root_decoder(dev))
+		kill_regions(to_cxl_root_decoder(dev));
 
 	device_unregister(dev);
 }
drivers/cxl/core/region.c +33 -27

123 lines not on this page · raw message

Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 2/5
#5 lore raw

Re: [PATCH 2/5] cxl/region: Resolve region deletion races

Quoted 17 lines · Dan Williams

Sungwoo noticed that the sysfs trigger to delete a region may try to delete a region multiple times. It also has no exclusion relative to the kernel releasing the region via CXL root device teardown.

Instead of installing new cxl root devres actions per region, use the existing root decoder unregistration event to remove all remaining regions. An xarray of regions replaces a devres list of regions.

This handles 3 separate issues with the old approach:

1/ sysfs users racing to delete the same region: no longer possible now
that the regions_lock is held over the lookup and deletion.

2/ multiple actions triggering deletion of the same region: solved by
erasing regions while holding @regions_lock, and only proceeding on successful erasure.

Quoted 7 lines

3/ userspace racing devres_release_all() to trigger the devres not found
warning: solved by sysfs unregistration not requiring a release action

Fixes 779dd20cfb56 ("cxl/region: Add region creation support")

LGTM but with a nit below.

Quoted 175 lines
---
 drivers/cxl/core/core.h   |  2 ++
 drivers/cxl/cxl.h         |  4 +++
 drivers/cxl/core/port.c   |  5 ++++
 drivers/cxl/core/region.c | 60 +++++++++++++++++++++------------------
 4 files changed, 44 insertions(+), 27 deletions(-)
drivers/cxl/core/core.h +2 -0
index 82ca3a476708..07555ae63859 100644
@@ -52,6 +52,7 @@ u64 cxl_dpa_to_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd,
 		   u64 dpa);
 int devm_cxl_add_dax_region(struct cxl_region *cxlr);
 int devm_cxl_add_pmem_region(struct cxl_region *cxlr);
+void kill_regions(struct cxl_root_decoder *cxlrd);
 
 #else
 static inline u64 cxl_dpa_to_hpa(struct cxl_region *cxlr,
@@ -81,6 +82,7 @@ static inline int cxl_region_init(void)
 static inline void cxl_region_exit(void)
 {
 }
+static inline void kill_regions(struct cxl_root_decoder *cxlrd) { };
 #define CXL_REGION_ATTR(x) NULL
 #define CXL_REGION_TYPE(x) NULL
 #define SET_CXL_REGION_ATTR(x)
drivers/cxl/cxl.h +4 -0
index 3900a0778571..f43abd1903ce 100644
@@ -360,6 +360,8 @@ struct cxl_rd_ops {
  * @region_id: region id for next region provisioning event
  * @platform_data: platform specific configuration data
  * @regions_lock: sync region discovery, construction, and deletion
+ * @regions: regions to remove at root decoder destruct time
+ * @dead: root decoder dead to region creation
  * @qos_class: QoS performance class cookie
  * @ops: CXL root decoder operations
  * @cxlsd: base cxl switch decoder
@@ -370,6 +372,8 @@ struct cxl_root_decoder {
 	atomic_t region_id;
 	void *platform_data;
 	struct mutex regions_lock;
+	struct xarray regions;
+	bool dead;
 	int qos_class;
 	struct cxl_rd_ops ops;
 	struct cxl_switch_decoder cxlsd;
drivers/cxl/core/port.c +5 -0
index 6e7a70d51cfe..1215ee4f4035 100644
@@ -458,6 +458,8 @@ static void cxl_root_decoder_release(struct device *dev)
 
 	if (atomic_read(&cxlrd->region_id) >= 0)
 		memregion_free(atomic_read(&cxlrd->region_id));
+	mutex_destroy(&cxlrd->regions_lock);
+	xa_destroy(&cxlrd->regions);
 	__cxl_decoder_release(&cxlrd->cxlsd.cxld);
 	kfree(cxlrd);
 }
@@ -2017,6 +2019,7 @@ struct cxl_root_decoder *cxl_root_decoder_alloc(struct cxl_port *port,
 	}
 
 	mutex_init(&cxlrd->regions_lock);
+	xa_init(&cxlrd->regions);
 
 	cxld = &cxlsd->cxld;
 	cxld->dev.type = &cxl_decoder_root_type;
@@ -2192,6 +2195,8 @@ static void cxld_unregister(void *dev)
 	if (is_endpoint_decoder(dev))
 		cxl_decoder_detach(NULL, to_cxl_endpoint_decoder(dev), -1,
 				   DETACH_INVALIDATE);
+	if (is_root_decoder(dev))
+		kill_regions(to_cxl_root_decoder(dev));
 
 	device_unregister(dev);
 }
drivers/cxl/core/region.c +24 -20
index b5601e89e302..faf9785c0509 100644
@@ -2537,12 +2537,13 @@ static struct cxl_region *to_cxl_region(struct device *dev)
 	return container_of(dev, struct cxl_region, dev);
 }
 
-static void unregister_region(void *_cxlr)
+static void unregister_region(struct cxl_region *cxlr)
 {
-	struct cxl_region *cxlr = _cxlr;
+	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
 	struct cxl_region_params *p = &cxlr->params;
 	int i;
 
+	xa_erase(&cxlrd->regions, cxlr->id);
 	device_del(&cxlr->dev);
 
 	/*
@@ -2673,6 +2674,19 @@ static int cxl_region_calculate_adistance(struct notifier_block *nb,
 	return NOTIFY_STOP;
 }
 
+/* unwind all remaining regions */
+void kill_regions(struct cxl_root_decoder *cxlrd)
+{
+	unsigned long index;
+	struct cxl_region *cxlr;
+
+	guard(mutex)(&cxlrd->regions_lock);
+	/* no more region creation */
+	cxlrd->dead = true;
+	xa_for_each(&cxlrd->regions, index, cxlr)
+		unregister_region(cxlr);
+}
+
 /**
  * devm_cxl_add_region - Adds a region to a decoder
  * @cxlrd: root decoder
@@ -2711,14 +2725,15 @@ static struct cxl_region *devm_cxl_add_region(struct cxl_root_decoder *cxlrd,
 	if (rc)
 		goto err;
 
-	rc = devm_add_action_or_reset(port->uport_dev, unregister_region, cxlr);
-	if (rc)
+	rc = xa_insert(&cxlrd->regions, cxlr->id, cxlr, GFP_KERNEL);
+	if (rc) {
+		unregister_region(cxlr);
 		return ERR_PTR(rc);
+	}
 
 	dev_dbg(port->uport_dev, "%s: created %s\n",
 		dev_name(&cxlrd->cxlsd.cxld.dev), dev_name(dev));
 	return cxlr;
-
 err:
 	put_device(dev);
 	return ERR_PTR(rc);
@@ -2747,6 +2762,9 @@ static struct cxl_region *__create_region(struct cxl_root_decoder *cxlrd,
 {
 	int rc;
 
+	if (cxlrd->dead)
+		return ERR_PTR(-ENXIO);
+
 	switch (mode) {
 	case CXL_PARTMODE_RAM:
 	case CXL_PARTMODE_PMEM:
@@ -2822,38 +2840,27 @@ static ssize_t region_show(struct device *dev, struct device_attribute *attr,
 }
 DEVICE_ATTR_RO(region);
 
-static struct cxl_region *
-cxl_find_region_by_name(struct cxl_root_decoder *cxlrd, const char *name)
-{
-	struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
-	struct device *region_dev;
-
-	region_dev = device_find_child_by_name(&cxld->dev, name);
-	if (!region_dev)
-		return ERR_PTR(-ENODEV);
-
-	return to_cxl_region(region_dev);
-}
-
 static ssize_t delete_region_store(struct device *dev,
 				   struct device_attribute *attr,
 				   const char *buf, size_t len)
 {
 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
-	struct cxl_port *port = to_cxl_port(dev->parent);
 	struct cxl_region *cxlr;
-	int rc;
+	int rc, id;
 

drivers/cxl/core/region.c static ssize_t region_show(struct device *dev, struct device_attribute *attr, · line 2857

Quoted 10 lines
drivers/cxl/core/region.c +3 -3
 	ACQUIRE(mutex_intr, regions_lock)(&cxlrd->regions_lock);
 	if ((rc = ACQUIRE_ERR(mutex_intr, &regions_lock)))
 		return rc;
 
-	cxlr = cxl_find_region_by_name(cxlrd, buf);
-	if (IS_ERR(cxlr))
-		return PTR_ERR(cxlr);
+	rc = sscanf(buf, "region%d\n", &id);
+	if (rc != 1)
+		return -EINVAL;

Maybe add a check for (id >= 0) just to sanitize user input?

DJ

Quoted 28 lines
drivers/cxl/core/region.c +6 -4
-	devm_release_action(port->uport_dev, unregister_region, cxlr);
-	put_device(&cxlr->dev);
+	cxlr = xa_load(&cxlrd->regions, id);
+	if (!cxlr || !sysfs_streq(buf, dev_name(&cxlr->dev)))
+		return -ENODEV;
+
+	unregister_region(cxlr);
 
 	return len;
 }
@@ -3718,7 +3725,6 @@ static struct cxl_region *construct_region(struct cxl_root_decoder *cxlrd,
 {
 	struct cxl_endpoint_decoder *cxled = ctx->cxled;
 	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
-	struct cxl_port *port = cxlrd_to_port(cxlrd);
 	struct cxl_dev_state *cxlds = cxlmd->cxlds;
 	int rc, part = READ_ONCE(cxled->part);
 	struct cxl_region *cxlr;
@@ -3739,7 +3745,7 @@ static struct cxl_region *construct_region(struct cxl_root_decoder *cxlrd,
 
 	rc = __construct_region(cxlr, ctx);
 	if (rc) {
-		devm_release_action(port->uport_dev, unregister_region, cxlr);
+		unregister_region(cxlr);
 		return ERR_PTR(rc);
 	}
 
Reviewed-by Tested-by on v1 2/5
#6 lore raw

Re: [PATCH 2/5] cxl/region: Resolve region deletion races

In reply to Dan Williams
Quoted 17 lines · Dan Williams

Sungwoo noticed that the sysfs trigger to delete a region may try to delete a region multiple times. It also has no exclusion relative to the kernel releasing the region via CXL root device teardown.

Instead of installing new cxl root devres actions per region, use the existing root decoder unregistration event to remove all remaining regions. An xarray of regions replaces a devres list of regions.

This handles 3 separate issues with the old approach:

1/ sysfs users racing to delete the same region: no longer possible now
that the regions_lock is held over the lookup and deletion.

2/ multiple actions triggering deletion of the same region: solved by
erasing regions while holding @regions_lock, and only proceeding on successful erasure.

Quoted 7 lines

3/ userspace racing devres_release_all() to trigger the devres not found
warning: solved by sysfs unregistration not requiring a release action

Fixes 779dd20cfb56 ("cxl/region: Add region creation support")
Quoted 213 lines
---
  drivers/cxl/core/core.h   |  2 ++
  drivers/cxl/cxl.h         |  4 +++
  drivers/cxl/core/port.c   |  5 ++++
  drivers/cxl/core/region.c | 60 +++++++++++++++++++++------------------
  4 files changed, 44 insertions(+), 27 deletions(-)
drivers/cxl/core/core.h +2 -0
index 82ca3a476708..07555ae63859 100644
@@ -52,6 +52,7 @@ u64 cxl_dpa_to_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd,
  		   u64 dpa);
  int devm_cxl_add_dax_region(struct cxl_region *cxlr);
  int devm_cxl_add_pmem_region(struct cxl_region *cxlr);
+void kill_regions(struct cxl_root_decoder *cxlrd);
 
  #else
  static inline u64 cxl_dpa_to_hpa(struct cxl_region *cxlr,
@@ -81,6 +82,7 @@ static inline int cxl_region_init(void)
  static inline void cxl_region_exit(void)
  {
  }
+static inline void kill_regions(struct cxl_root_decoder *cxlrd) { };
  #define CXL_REGION_ATTR(x) NULL
  #define CXL_REGION_TYPE(x) NULL
  #define SET_CXL_REGION_ATTR(x)
drivers/cxl/cxl.h +4 -0
index 3900a0778571..f43abd1903ce 100644
@@ -360,6 +360,8 @@ struct cxl_rd_ops {
   * @region_id: region id for next region provisioning event
   * @platform_data: platform specific configuration data
   * @regions_lock: sync region discovery, construction, and deletion
+ * @regions: regions to remove at root decoder destruct time
+ * @dead: root decoder dead to region creation
   * @qos_class: QoS performance class cookie
   * @ops: CXL root decoder operations
   * @cxlsd: base cxl switch decoder
@@ -370,6 +372,8 @@ struct cxl_root_decoder {
  	atomic_t region_id;
  	void *platform_data;
  	struct mutex regions_lock;
+	struct xarray regions;
+	bool dead;
  	int qos_class;
  	struct cxl_rd_ops ops;
  	struct cxl_switch_decoder cxlsd;
drivers/cxl/core/port.c +5 -0
index 6e7a70d51cfe..1215ee4f4035 100644
@@ -458,6 +458,8 @@ static void cxl_root_decoder_release(struct device *dev)
 
  	if (atomic_read(&cxlrd->region_id) >= 0)
  		memregion_free(atomic_read(&cxlrd->region_id));
+	mutex_destroy(&cxlrd->regions_lock);
+	xa_destroy(&cxlrd->regions);
  	__cxl_decoder_release(&cxlrd->cxlsd.cxld);
  	kfree(cxlrd);
  }
@@ -2017,6 +2019,7 @@ struct cxl_root_decoder *cxl_root_decoder_alloc(struct cxl_port *port,
  	}
 
  	mutex_init(&cxlrd->regions_lock);
+	xa_init(&cxlrd->regions);
 
  	cxld = &cxlsd->cxld;
  	cxld->dev.type = &cxl_decoder_root_type;
@@ -2192,6 +2195,8 @@ static void cxld_unregister(void *dev)
  	if (is_endpoint_decoder(dev))
  		cxl_decoder_detach(NULL, to_cxl_endpoint_decoder(dev), -1,
  				   DETACH_INVALIDATE);
+	if (is_root_decoder(dev))
+		kill_regions(to_cxl_root_decoder(dev));
 
  	device_unregister(dev);
  }
drivers/cxl/core/region.c +33 -27
index b5601e89e302..faf9785c0509 100644
@@ -2537,12 +2537,13 @@ static struct cxl_region *to_cxl_region(struct device *dev)
  	return container_of(dev, struct cxl_region, dev);
  }
 
-static void unregister_region(void *_cxlr)
+static void unregister_region(struct cxl_region *cxlr)
  {
-	struct cxl_region *cxlr = _cxlr;
+	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
  	struct cxl_region_params *p = &cxlr->params;
  	int i;
 
+	xa_erase(&cxlrd->regions, cxlr->id);
  	device_del(&cxlr->dev);
 
  	/*
@@ -2673,6 +2674,19 @@ static int cxl_region_calculate_adistance(struct notifier_block *nb,
  	return NOTIFY_STOP;
  }
 
+/* unwind all remaining regions */
+void kill_regions(struct cxl_root_decoder *cxlrd)
+{
+	unsigned long index;
+	struct cxl_region *cxlr;
+
+	guard(mutex)(&cxlrd->regions_lock);
+	/* no more region creation */
+	cxlrd->dead = true;
+	xa_for_each(&cxlrd->regions, index, cxlr)
+		unregister_region(cxlr);
+}
+
  /**
   * devm_cxl_add_region - Adds a region to a decoder
   * @cxlrd: root decoder
@@ -2711,14 +2725,15 @@ static struct cxl_region *devm_cxl_add_region(struct cxl_root_decoder *cxlrd,
  	if (rc)
  		goto err;
 
-	rc = devm_add_action_or_reset(port->uport_dev, unregister_region, cxlr);
-	if (rc)
+	rc = xa_insert(&cxlrd->regions, cxlr->id, cxlr, GFP_KERNEL);
+	if (rc) {
+		unregister_region(cxlr);
  		return ERR_PTR(rc);
+	}
 
  	dev_dbg(port->uport_dev, "%s: created %s\n",
  		dev_name(&cxlrd->cxlsd.cxld.dev), dev_name(dev));
  	return cxlr;
-
  err:
  	put_device(dev);
  	return ERR_PTR(rc);
@@ -2747,6 +2762,9 @@ static struct cxl_region *__create_region(struct cxl_root_decoder *cxlrd,
  {
  	int rc;
 
+	if (cxlrd->dead)
+		return ERR_PTR(-ENXIO);
+
  	switch (mode) {
  	case CXL_PARTMODE_RAM:
  	case CXL_PARTMODE_PMEM:
@@ -2822,38 +2840,27 @@ static ssize_t region_show(struct device *dev, struct device_attribute *attr,
  }
  DEVICE_ATTR_RO(region);
 
-static struct cxl_region *
-cxl_find_region_by_name(struct cxl_root_decoder *cxlrd, const char *name)
-{
-	struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
-	struct device *region_dev;
-
-	region_dev = device_find_child_by_name(&cxld->dev, name);
-	if (!region_dev)
-		return ERR_PTR(-ENODEV);
-
-	return to_cxl_region(region_dev);
-}
-
  static ssize_t delete_region_store(struct device *dev,
  				   struct device_attribute *attr,
  				   const char *buf, size_t len)
  {
  	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
-	struct cxl_port *port = to_cxl_port(dev->parent);
  	struct cxl_region *cxlr;
-	int rc;
+	int rc, id;
 
  	ACQUIRE(mutex_intr, regions_lock)(&cxlrd->regions_lock);
  	if ((rc = ACQUIRE_ERR(mutex_intr, &regions_lock)))
  		return rc;
 
-	cxlr = cxl_find_region_by_name(cxlrd, buf);
-	if (IS_ERR(cxlr))
-		return PTR_ERR(cxlr);
+	rc = sscanf(buf, "region%d\n", &id);
+	if (rc != 1)
+		return -EINVAL;
 
-	devm_release_action(port->uport_dev, unregister_region, cxlr);
-	put_device(&cxlr->dev);
+	cxlr = xa_load(&cxlrd->regions, id);
+	if (!cxlr || !sysfs_streq(buf, dev_name(&cxlr->dev)))
+		return -ENODEV;
+
+	unregister_region(cxlr);
 
  	return len;
  }
@@ -3718,7 +3725,6 @@ static struct cxl_region *construct_region(struct cxl_root_decoder *cxlrd,
  {
  	struct cxl_endpoint_decoder *cxled = ctx->cxled;
  	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
-	struct cxl_port *port = cxlrd_to_port(cxlrd);
  	struct cxl_dev_state *cxlds = cxlmd->cxlds;
  	int rc, part = READ_ONCE(cxled->part);
  	struct cxl_region *cxlr;
@@ -3739,7 +3745,7 @@ static struct cxl_region *construct_region(struct cxl_root_decoder *cxlrd,
 
  	rc = __construct_region(cxlr, ctx);
  	if (rc) {
-		devm_release_action(port->uport_dev, unregister_region, cxlr);
+		unregister_region(cxlr);
  		return ERR_PTR(rc);
  	}
 
author maintainer
#7 lore raw

PATCH 3/5 cxl/memdev: Pin parents for entire memdev lifetime

In reply to Dan Williams

In order to be able to manage the driver that uses a memdev attach mechanism the parent needs to stick around for the device_release_driver(cxlmd->dev.parent) event.

Fixes 29317f8dc6ed ("cxl/mem: Introduce cxl_memdev_attach for CXL-dependent operation")
---
 drivers/cxl/core/memdev.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/cxl/core/memdev.c +3 -1
index 80e65690eb77..91c99eeea92c 100644
@@ -25,9 +25,11 @@ static DEFINE_IDA(cxl_memdev_ida);
 static void cxl_memdev_release(struct device *dev)
 {
 	struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
+	struct device *parent = dev->parent;
 
 	ida_free(&cxl_memdev_ida, cxlmd->id);
 	kfree(cxlmd);
+	put_device(parent);
 }
 
 static char *cxl_memdev_devnode(const struct device *dev, umode_t *mode, kuid_t *uid,
@@ -707,7 +709,7 @@ static struct cxl_memdev *cxl_memdev_alloc(struct cxl_dev_state *cxlds,
 	dev = &cxlmd->dev;
 	device_initialize(dev);
 	lockdep_set_class(&dev->mutex, &cxl_memdev_key);
-	dev->parent = cxlds->dev;
+	dev->parent = get_device(cxlds->dev);
 	dev->bus = &cxl_bus_type;
 	dev->devt = MKDEV(cxl_mem_major, cxlmd->id);
 	dev->type = &cxl_memdev_type;
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 3/5
#8 lore raw

Re: [PATCH 3/5] cxl/memdev: Pin parents for entire memdev lifetime

Quoted 6 lines · Dan Williams

In order to be able to manage the driver that uses a memdev attach mechanism the parent needs to stick around for the device_release_driver(cxlmd->dev.parent) event.

Fixes 29317f8dc6ed ("cxl/mem: Introduce cxl_memdev_attach for CXL-dependent operation")

Maybe a comment above get_device() to make note of why we are taking the reference.

Quoted 29 lines
---
 drivers/cxl/core/memdev.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/cxl/core/memdev.c +3 -1
index 80e65690eb77..91c99eeea92c 100644
@@ -25,9 +25,11 @@ static DEFINE_IDA(cxl_memdev_ida);
 static void cxl_memdev_release(struct device *dev)
 {
 	struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
+	struct device *parent = dev->parent;
 
 	ida_free(&cxl_memdev_ida, cxlmd->id);
 	kfree(cxlmd);
+	put_device(parent);
 }
 
 static char *cxl_memdev_devnode(const struct device *dev, umode_t *mode, kuid_t *uid,
@@ -707,7 +709,7 @@ static struct cxl_memdev *cxl_memdev_alloc(struct cxl_dev_state *cxlds,
 	dev = &cxlmd->dev;
 	device_initialize(dev);
 	lockdep_set_class(&dev->mutex, &cxl_memdev_key);
-	dev->parent = cxlds->dev;
+	dev->parent = get_device(cxlds->dev);
 	dev->bus = &cxl_bus_type;
 	dev->devt = MKDEV(cxl_mem_major, cxlmd->id);
 	dev->type = &cxl_memdev_type;
Reviewed-by Tested-by on v1 3/5
#9 lore raw

Re: [PATCH 3/5] cxl/memdev: Pin parents for entire memdev lifetime

In reply to Dan Williams
Quoted 6 lines · Dan Williams

In order to be able to manage the driver that uses a memdev attach mechanism the parent needs to stick around for the device_release_driver(cxlmd->dev.parent) event.

Fixes 29317f8dc6ed ("cxl/mem: Introduce cxl_memdev_attach for CXL-dependent operation")
Quoted 29 lines
---
  drivers/cxl/core/memdev.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
drivers/cxl/core/memdev.c +3 -1
index 80e65690eb77..91c99eeea92c 100644
@@ -25,9 +25,11 @@ static DEFINE_IDA(cxl_memdev_ida);
  static void cxl_memdev_release(struct device *dev)
  {
  	struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
+	struct device *parent = dev->parent;
 
  	ida_free(&cxl_memdev_ida, cxlmd->id);
  	kfree(cxlmd);
+	put_device(parent);
  }
 
  static char *cxl_memdev_devnode(const struct device *dev, umode_t *mode, kuid_t *uid,
@@ -707,7 +709,7 @@ static struct cxl_memdev *cxl_memdev_alloc(struct cxl_dev_state *cxlds,
  	dev = &cxlmd->dev;
  	device_initialize(dev);
  	lockdep_set_class(&dev->mutex, &cxl_memdev_key);
-	dev->parent = cxlds->dev;
+	dev->parent = get_device(cxlds->dev);
  	dev->bus = &cxl_bus_type;
  	dev->devt = MKDEV(cxl_mem_major, cxlmd->id);
  	dev->type = &cxl_memdev_type;
author maintainer
#10 lore raw

PATCH 4/5 cxl/memdev: Introduce cxl_class_memdev_type

In reply to Dan Williams

In preparation for memdev's without mailbox related infrastructure, introduce cxl_class_memdev_type as a superset of a cxl_memdev_type. Effectively the only difference is that cxl_class_memdev_type exports common sysfs attributes where cxl_memdev_type has none.

Related to this is all the cxl_mem_probe() paths that assume the presence of a class device mailbox are updated to skip that requirement.

---
 drivers/cxl/core/memdev.c | 16 +++++++++++---
 drivers/cxl/mem.c         | 45 +++++++++++++++++++++++++++++----------
 2 files changed, 47 insertions(+), 14 deletions(-)
drivers/cxl/core/memdev.c +13 -3
index 91c99eeea92c..33a3d2e7b13a 100644
@@ -574,16 +574,23 @@ void cxl_memdev_update_perf(struct cxl_memdev *cxlmd)
 }
 EXPORT_SYMBOL_NS_GPL(cxl_memdev_update_perf, "CXL");
 
-static const struct device_type cxl_memdev_type = {
+static const struct device_type cxl_class_memdev_type = {
 	.name = "cxl_memdev",
 	.release = cxl_memdev_release,
 	.devnode = cxl_memdev_devnode,
 	.groups = cxl_memdev_attribute_groups,
 };
 
+static const struct device_type cxl_memdev_type = {
+	.name = "cxl_memdev",
+	.release = cxl_memdev_release,
+	.devnode = cxl_memdev_devnode,
+};
+
 bool is_cxl_memdev(const struct device *dev)
 {
-	return dev->type == &cxl_memdev_type;
+	return (dev->type == &cxl_class_memdev_type ||
+		dev->type == &cxl_memdev_type);
 }
 EXPORT_SYMBOL_NS_GPL(is_cxl_memdev, "CXL");
 
@@ -712,7 +719,10 @@ static struct cxl_memdev *cxl_memdev_alloc(struct cxl_dev_state *cxlds,
 	dev->parent = get_device(cxlds->dev);
 	dev->bus = &cxl_bus_type;
 	dev->devt = MKDEV(cxl_mem_major, cxlmd->id);
-	dev->type = &cxl_memdev_type;
+	if (cxlds->type == CXL_DEVTYPE_DEVMEM)
+		dev->type = &cxl_memdev_type;
+	else
+		dev->type = &cxl_class_memdev_type;
 	device_set_pm_not_required(dev);
 	INIT_WORK(&cxlmd->detach_work, detach_memdev);
 
drivers/cxl/mem.c +34 -11

68 lines not on this page · raw message

Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 4/5
#11 lore raw

Re: [PATCH 4/5] cxl/memdev: Introduce cxl_class_memdev_type

Quoted 11 lines · Dan Williams

In preparation for memdev's without mailbox related infrastructure, introduce cxl_class_memdev_type as a superset of a cxl_memdev_type. Effectively the only difference is that cxl_class_memdev_type exports common sysfs attributes where cxl_memdev_type has none.

Related to this is all the cxl_mem_probe() paths that assume the presence of a class device mailbox are updated to skip that requirement.

Quoted 122 lines
---
 drivers/cxl/core/memdev.c | 16 +++++++++++---
 drivers/cxl/mem.c         | 45 +++++++++++++++++++++++++++++----------
 2 files changed, 47 insertions(+), 14 deletions(-)
drivers/cxl/core/memdev.c +13 -3
index 91c99eeea92c..33a3d2e7b13a 100644
@@ -574,16 +574,23 @@ void cxl_memdev_update_perf(struct cxl_memdev *cxlmd)
 }
 EXPORT_SYMBOL_NS_GPL(cxl_memdev_update_perf, "CXL");
 
-static const struct device_type cxl_memdev_type = {
+static const struct device_type cxl_class_memdev_type = {
 	.name = "cxl_memdev",
 	.release = cxl_memdev_release,
 	.devnode = cxl_memdev_devnode,
 	.groups = cxl_memdev_attribute_groups,
 };
 
+static const struct device_type cxl_memdev_type = {
+	.name = "cxl_memdev",
+	.release = cxl_memdev_release,
+	.devnode = cxl_memdev_devnode,
+};
+
 bool is_cxl_memdev(const struct device *dev)
 {
-	return dev->type == &cxl_memdev_type;
+	return (dev->type == &cxl_class_memdev_type ||
+		dev->type == &cxl_memdev_type);
 }
 EXPORT_SYMBOL_NS_GPL(is_cxl_memdev, "CXL");
 
@@ -712,7 +719,10 @@ static struct cxl_memdev *cxl_memdev_alloc(struct cxl_dev_state *cxlds,
 	dev->parent = get_device(cxlds->dev);
 	dev->bus = &cxl_bus_type;
 	dev->devt = MKDEV(cxl_mem_major, cxlmd->id);
-	dev->type = &cxl_memdev_type;
+	if (cxlds->type == CXL_DEVTYPE_DEVMEM)
+		dev->type = &cxl_memdev_type;
+	else
+		dev->type = &cxl_class_memdev_type;
 	device_set_pm_not_required(dev);
 	INIT_WORK(&cxlmd->detach_work, detach_memdev);
 
drivers/cxl/mem.c +34 -11
index fcffe24dcb42..ff858318091f 100644
@@ -65,6 +65,26 @@ static int cxl_debugfs_poison_clear(void *data, u64 dpa)
 DEFINE_DEBUGFS_ATTRIBUTE(cxl_poison_clear_fops, NULL,
 			 cxl_debugfs_poison_clear, "%llx\n");
 
+static void cxl_memdev_poison_enable(struct cxl_memdev_state *mds,
+				     struct cxl_memdev *cxlmd,
+				     struct dentry *dentry)
+{
+	/*
+	 * Avoid poison debugfs for DEVMEM aka accelerators as they rely on
+	 * cxl_memdev_state.
+	 */
+	if (!mds)
+		return;
+
+	if (test_bit(CXL_POISON_ENABLED_INJECT, mds->poison.enabled_cmds))
+		debugfs_create_file("inject_poison", 0200, dentry, cxlmd,
+				    &cxl_poison_inject_fops);
+
+	if (test_bit(CXL_POISON_ENABLED_CLEAR, mds->poison.enabled_cmds))
+		debugfs_create_file("clear_poison", 0200, dentry, cxlmd,
+				    &cxl_poison_clear_fops);
+}
+
 static int cxl_mem_probe(struct device *dev)
 {
 	struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
@@ -92,12 +112,7 @@ static int cxl_mem_probe(struct device *dev)
 	dentry = cxl_debugfs_create_dir(dev_name(dev));
 	debugfs_create_devm_seqfile(dev, "dpamem", dentry, cxl_mem_dpa_show);
 
-	if (test_bit(CXL_POISON_ENABLED_INJECT, mds->poison.enabled_cmds))
-		debugfs_create_file("inject_poison", 0200, dentry, cxlmd,
-				    &cxl_poison_inject_fops);
-	if (test_bit(CXL_POISON_ENABLED_CLEAR, mds->poison.enabled_cmds))
-		debugfs_create_file("clear_poison", 0200, dentry, cxlmd,
-				    &cxl_poison_clear_fops);
+	cxl_memdev_poison_enable(mds, cxlmd, dentry);
 
 	rc = devm_add_action_or_reset(dev, remove_debugfs, dentry);
 	if (rc)
@@ -206,16 +221,24 @@ static ssize_t trigger_poison_list_store(struct device *dev,
 }
 static DEVICE_ATTR_WO(trigger_poison_list);
 
-static umode_t cxl_mem_visible(struct kobject *kobj, struct attribute *a, int n)
+static bool cxl_poison_attr_visible(struct kobject *kobj, struct attribute *a)
 {
 	struct device *dev = kobj_to_dev(kobj);
 	struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
 	struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlmd->cxlds);
 
-	if (a == &dev_attr_trigger_poison_list.attr)
-		if (!test_bit(CXL_POISON_ENABLED_LIST,
-			      mds->poison.enabled_cmds))
-			return 0;
+	if (!mds ||
+	    !test_bit(CXL_POISON_ENABLED_LIST, mds->poison.enabled_cmds))
+		return false;
+
+	return true;
+}
+
+static umode_t cxl_mem_visible(struct kobject *kobj, struct attribute *a, int n)
+{
+	if (a == &dev_attr_trigger_poison_list.attr &&
+	    !cxl_poison_attr_visible(kobj, a))
+		return 0;
 
 	return a->mode;
 }
Tested-by on v1 4/5
#12 lore raw

Re: [PATCH 4/5] cxl/memdev: Introduce cxl_class_memdev_type

In reply to Dan Williams
Quoted 11 lines · Dan Williams

In preparation for memdev's without mailbox related infrastructure, introduce cxl_class_memdev_type as a superset of a cxl_memdev_type. Effectively the only difference is that cxl_class_memdev_type exports common sysfs attributes where cxl_memdev_type has none.

Related to this is all the cxl_mem_probe() paths that assume the presence of a class device mailbox are updated to skip that requirement.

Quoted 122 lines
---
  drivers/cxl/core/memdev.c | 16 +++++++++++---
  drivers/cxl/mem.c         | 45 +++++++++++++++++++++++++++++----------
  2 files changed, 47 insertions(+), 14 deletions(-)
drivers/cxl/core/memdev.c +13 -3
index 91c99eeea92c..33a3d2e7b13a 100644
@@ -574,16 +574,23 @@ void cxl_memdev_update_perf(struct cxl_memdev *cxlmd)
  }
  EXPORT_SYMBOL_NS_GPL(cxl_memdev_update_perf, "CXL");
 
-static const struct device_type cxl_memdev_type = {
+static const struct device_type cxl_class_memdev_type = {
  	.name = "cxl_memdev",
  	.release = cxl_memdev_release,
  	.devnode = cxl_memdev_devnode,
  	.groups = cxl_memdev_attribute_groups,
  };
 
+static const struct device_type cxl_memdev_type = {
+	.name = "cxl_memdev",
+	.release = cxl_memdev_release,
+	.devnode = cxl_memdev_devnode,
+};
+
  bool is_cxl_memdev(const struct device *dev)
  {
-	return dev->type == &cxl_memdev_type;
+	return (dev->type == &cxl_class_memdev_type ||
+		dev->type == &cxl_memdev_type);
  }
  EXPORT_SYMBOL_NS_GPL(is_cxl_memdev, "CXL");
 
@@ -712,7 +719,10 @@ static struct cxl_memdev *cxl_memdev_alloc(struct cxl_dev_state *cxlds,
  	dev->parent = get_device(cxlds->dev);
  	dev->bus = &cxl_bus_type;
  	dev->devt = MKDEV(cxl_mem_major, cxlmd->id);
-	dev->type = &cxl_memdev_type;
+	if (cxlds->type == CXL_DEVTYPE_DEVMEM)
+		dev->type = &cxl_memdev_type;
+	else
+		dev->type = &cxl_class_memdev_type;
  	device_set_pm_not_required(dev);
  	INIT_WORK(&cxlmd->detach_work, detach_memdev);
 
drivers/cxl/mem.c +34 -11
index fcffe24dcb42..ff858318091f 100644
@@ -65,6 +65,26 @@ static int cxl_debugfs_poison_clear(void *data, u64 dpa)
  DEFINE_DEBUGFS_ATTRIBUTE(cxl_poison_clear_fops, NULL,
  			 cxl_debugfs_poison_clear, "%llx\n");
 
+static void cxl_memdev_poison_enable(struct cxl_memdev_state *mds,
+				     struct cxl_memdev *cxlmd,
+				     struct dentry *dentry)
+{
+	/*
+	 * Avoid poison debugfs for DEVMEM aka accelerators as they rely on
+	 * cxl_memdev_state.
+	 */
+	if (!mds)
+		return;
+
+	if (test_bit(CXL_POISON_ENABLED_INJECT, mds->poison.enabled_cmds))
+		debugfs_create_file("inject_poison", 0200, dentry, cxlmd,
+				    &cxl_poison_inject_fops);
+
+	if (test_bit(CXL_POISON_ENABLED_CLEAR, mds->poison.enabled_cmds))
+		debugfs_create_file("clear_poison", 0200, dentry, cxlmd,
+				    &cxl_poison_clear_fops);
+}
+
  static int cxl_mem_probe(struct device *dev)
  {
  	struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
@@ -92,12 +112,7 @@ static int cxl_mem_probe(struct device *dev)
  	dentry = cxl_debugfs_create_dir(dev_name(dev));
  	debugfs_create_devm_seqfile(dev, "dpamem", dentry, cxl_mem_dpa_show);
 
-	if (test_bit(CXL_POISON_ENABLED_INJECT, mds->poison.enabled_cmds))
-		debugfs_create_file("inject_poison", 0200, dentry, cxlmd,
-				    &cxl_poison_inject_fops);
-	if (test_bit(CXL_POISON_ENABLED_CLEAR, mds->poison.enabled_cmds))
-		debugfs_create_file("clear_poison", 0200, dentry, cxlmd,
-				    &cxl_poison_clear_fops);
+	cxl_memdev_poison_enable(mds, cxlmd, dentry);
 
  	rc = devm_add_action_or_reset(dev, remove_debugfs, dentry);
  	if (rc)
@@ -206,16 +221,24 @@ static ssize_t trigger_poison_list_store(struct device *dev,
  }
  static DEVICE_ATTR_WO(trigger_poison_list);
 
-static umode_t cxl_mem_visible(struct kobject *kobj, struct attribute *a, int n)
+static bool cxl_poison_attr_visible(struct kobject *kobj, struct attribute *a)
  {
  	struct device *dev = kobj_to_dev(kobj);
  	struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
  	struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlmd->cxlds);
 
-	if (a == &dev_attr_trigger_poison_list.attr)
-		if (!test_bit(CXL_POISON_ENABLED_LIST,
-			      mds->poison.enabled_cmds))
-			return 0;
+	if (!mds ||
+	    !test_bit(CXL_POISON_ENABLED_LIST, mds->poison.enabled_cmds))
+		return false;
+
+	return true;
+}
+
+static umode_t cxl_mem_visible(struct kobject *kobj, struct attribute *a, int n)
+{
+	if (a == &dev_attr_trigger_poison_list.attr &&
+	    !cxl_poison_attr_visible(kobj, a))
+		return 0;
 
  	return a->mode;
  }
author maintainer
#13 lore raw

PATCH 5/5 cxl/region: Introduce devm_cxl_probe_mem()

In reply to Dan Williams

To date, platform firmware maps accelerator memory and accelerator drivers simply want an address range that they can map themselves. This typically results in a single region being auto-assembled upon registration of a memory device. Use the @attach mechanism of devm_cxl_add_memdev() parameter to retrieve that region while also adhering to CXL subsystem locking and lifetime rules. As part of adhering to current object lifetime rules, if the region or the CXL port topology is invalidated, the CXL core arranges for the accelertor driver to be detached as well.

The locking and lifetime rules were validated with Dave's work-in-progress cxl-type-2 support for cxl_test.

devm_cxl_add_classdev() supports the general memory expansion flow where region assembly is optional, dynamic, and user controlled.

Cc Alejandro Lucero <[email protected]>
---
 drivers/cxl/cxlmem.h         |  27 ++++++--
 include/cxl/cxl.h            |   3 +
 drivers/cxl/core/region.c    | 124 +++++++++++++++++++++++++++++++++++
 drivers/cxl/mem.c            |  50 +++++++++++---
 drivers/cxl/pci.c            |   2 +-
 tools/testing/cxl/test/mem.c |   2 +-
 6 files changed, 191 insertions(+), 17 deletions(-)
drivers/cxl/cxlmem.h +21 -6
index 776c50d1db51..d3bdd00f94b3 100644
@@ -34,10 +34,6 @@
 	(FIELD_GET(CXLMDEV_RESET_NEEDED_MASK, status) !=                       \
 	 CXLMDEV_RESET_NEEDED_NOT)
 
-struct cxl_memdev_attach {
-	int (*probe)(struct cxl_memdev *cxlmd);
-};
-
 /**
  * struct cxl_memdev - CXL bus object representing a Type-3 Memory Device
  * @dev: driver core device object
@@ -101,10 +97,29 @@ static inline bool is_cxl_endpoint(struct cxl_port *port)
 	return is_cxl_memdev(port->uport_dev);
 }
 
+struct cxl_memdev_attach {
+	int (*probe)(struct cxl_memdev *cxlmd);
+};
+
+/**
+ * struct cxl_attach_region - coordinate mapping a region at memdev registration
+ * @attach: common core attachment descriptor
+ * @hpa_range: physical address range of the region
+ *
+ * For the common simple case of a CXL device with private (non-general purpose
+ * / "accelerator") memory, enumerate firmware instantiated region, or
+ * instantiate a region for the device's capacity. Destroy the region on detach.
+ */
+struct cxl_attach_region {
+	struct cxl_memdev_attach attach;
+	struct range hpa_range;
+};
+
+int cxl_memdev_attach_region(struct cxl_memdev *cxlmd);
+
+struct cxl_memdev *devm_cxl_add_classdev(struct cxl_dev_state *cxlds);
 struct cxl_memdev *__devm_cxl_add_memdev(struct cxl_dev_state *cxlds,
 					 const struct cxl_memdev_attach *attach);
-struct cxl_memdev *devm_cxl_add_memdev(struct cxl_dev_state *cxlds,
-				       const struct cxl_memdev_attach *attach);
 int devm_cxl_sanitize_setup_notifier(struct device *host,
 				     struct cxl_memdev *cxlmd);
 struct cxl_memdev_state;
include/cxl/cxl.h +3 -0
index fa7269154620..016c74fb747c 100644
@@ -223,4 +223,7 @@ struct cxl_dev_state *_devm_cxl_dev_state_create(struct device *dev,
 		(drv_struct *)_devm_cxl_dev_state_create(parent, type, serial, dvsec,	\
 						      sizeof(drv_struct), mbox);	\
 	})
+
+struct cxl_memdev *devm_cxl_probe_mem(struct cxl_dev_state *cxlds,
+				      struct range *range);
 #endif /* __CXL_CXL_H__ */
drivers/cxl/core/region.c +124 -0

148 lines not on this page · raw message

drivers/cxl/mem.c +41 -9

68 lines not on this page · raw message

drivers/cxl/pci.c +1 -1
index bace662dc988..267c679b0b3c 100644
@@ -878,7 +878,7 @@ static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	if (rc)
 		dev_dbg(&pdev->dev, "No CXL Features discovered\n");
 
-	cxlmd = devm_cxl_add_memdev(cxlds, NULL);
+	cxlmd = devm_cxl_add_classdev(cxlds);
 	if (IS_ERR(cxlmd))
 		return PTR_ERR(cxlmd);
 
tools/testing/cxl/test/mem.c +1 -1
index 271c7ad8cc32..095ca544ac02 100644
@@ -1769,7 +1769,7 @@ static int cxl_mock_mem_probe(struct platform_device *pdev)
 
 	cxl_mock_add_event_logs(&mdata->mes);
 
-	cxlmd = devm_cxl_add_memdev(cxlds, NULL);
+	cxlmd = devm_cxl_add_classdev(cxlds);
 	if (IS_ERR(cxlmd))
 		return PTR_ERR(cxlmd);
 
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 5/5
#14 lore raw

Re: [PATCH 5/5] cxl/region: Introduce devm_cxl_probe_mem()

Quoted 9 lines · Dan Williams

To date, platform firmware maps accelerator memory and accelerator drivers simply want an address range that they can map themselves. This typically results in a single region being auto-assembled upon registration of a memory device. Use the @attach mechanism of devm_cxl_add_memdev() parameter to retrieve that region while also adhering to CXL subsystem locking and lifetime rules. As part of adhering to current object lifetime rules, if the region or the CXL port topology is invalidated, the CXL core arranges for the accelertor driver to be detached as well.

s/accelertor/accelerator/

Quoted 8 lines

The locking and lifetime rules were validated with Dave's work-in-progress cxl-type-2 support for cxl_test.

devm_cxl_add_classdev() supports the general memory expansion flow where region assembly is optional, dynamic, and user controlled.

Cc Alejandro Lucero <[email protected]>
Quoted 323 lines
---
 drivers/cxl/cxlmem.h         |  27 ++++++--
 include/cxl/cxl.h            |   3 +
 drivers/cxl/core/region.c    | 124 +++++++++++++++++++++++++++++++++++
 drivers/cxl/mem.c            |  50 +++++++++++---
 drivers/cxl/pci.c            |   2 +-
 tools/testing/cxl/test/mem.c |   2 +-
 6 files changed, 191 insertions(+), 17 deletions(-)
drivers/cxl/cxlmem.h +21 -6
index 776c50d1db51..d3bdd00f94b3 100644
@@ -34,10 +34,6 @@
 	(FIELD_GET(CXLMDEV_RESET_NEEDED_MASK, status) !=                       \
 	 CXLMDEV_RESET_NEEDED_NOT)
 
-struct cxl_memdev_attach {
-	int (*probe)(struct cxl_memdev *cxlmd);
-};
-
 /**
  * struct cxl_memdev - CXL bus object representing a Type-3 Memory Device
  * @dev: driver core device object
@@ -101,10 +97,29 @@ static inline bool is_cxl_endpoint(struct cxl_port *port)
 	return is_cxl_memdev(port->uport_dev);
 }
 
+struct cxl_memdev_attach {
+	int (*probe)(struct cxl_memdev *cxlmd);
+};
+
+/**
+ * struct cxl_attach_region - coordinate mapping a region at memdev registration
+ * @attach: common core attachment descriptor
+ * @hpa_range: physical address range of the region
+ *
+ * For the common simple case of a CXL device with private (non-general purpose
+ * / "accelerator") memory, enumerate firmware instantiated region, or
+ * instantiate a region for the device's capacity. Destroy the region on detach.
+ */
+struct cxl_attach_region {
+	struct cxl_memdev_attach attach;
+	struct range hpa_range;
+};
+
+int cxl_memdev_attach_region(struct cxl_memdev *cxlmd);
+
+struct cxl_memdev *devm_cxl_add_classdev(struct cxl_dev_state *cxlds);
 struct cxl_memdev *__devm_cxl_add_memdev(struct cxl_dev_state *cxlds,
 					 const struct cxl_memdev_attach *attach);
-struct cxl_memdev *devm_cxl_add_memdev(struct cxl_dev_state *cxlds,
-				       const struct cxl_memdev_attach *attach);
 int devm_cxl_sanitize_setup_notifier(struct device *host,
 				     struct cxl_memdev *cxlmd);
 struct cxl_memdev_state;
include/cxl/cxl.h +3 -0
index fa7269154620..016c74fb747c 100644
@@ -223,4 +223,7 @@ struct cxl_dev_state *_devm_cxl_dev_state_create(struct device *dev,
 		(drv_struct *)_devm_cxl_dev_state_create(parent, type, serial, dvsec,	\
 						      sizeof(drv_struct), mbox);	\
 	})
+
+struct cxl_memdev *devm_cxl_probe_mem(struct cxl_dev_state *cxlds,
+				      struct range *range);
 #endif /* __CXL_CXL_H__ */
drivers/cxl/core/region.c +124 -0
index faf9785c0509..ce99f0650764 100644
@@ -1148,6 +1148,19 @@ static int cxl_rr_assign_decoder(struct cxl_port *port, struct cxl_region *cxlr,
 static void cxl_region_setup_flags(struct cxl_region *cxlr,
 				   struct cxl_decoder *cxld)
 {
+	if (is_endpoint_decoder(&cxld->dev)) {
+		struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(&cxld->dev);
+		struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
+
+		/*
+		 * When a region's memdevs specify an @attach method the attach
+		 * provider is responsible for dispositioning the region for
+		 * both probe and userspace management
+		 */
+		if (cxlmd->attach)
+			set_bit(CXL_REGION_F_LOCK, &cxlr->flags);
+	}
+
 	if (cxld->flags & CXL_DECODER_F_LOCK) {
 		set_bit(CXL_REGION_F_LOCK, &cxlr->flags);
 		clear_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags);
@@ -2560,6 +2573,17 @@ static void unregister_region(struct cxl_region *cxlr)
 	put_device(&cxlr->dev);
 }
 
+static void endpoint_unregister_region(void *_cxlr)
+{
+	struct cxl_region *cxlr = _cxlr;
+	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
+
+	guard(mutex)(&cxlrd->regions_lock);
+	if (xa_load(&cxlrd->regions, cxlr->id))
+		unregister_region(cxlr);
+	put_device(&cxlr->dev);
+}
+
 static struct lock_class_key cxl_region_key;
 
 static struct cxl_region *cxl_region_alloc(struct cxl_root_decoder *cxlrd, int id)
@@ -4057,6 +4081,103 @@ static int cxl_region_can_probe(struct cxl_region *cxlr)
 	return 0;
 }
 
+static int first_mapped_decoder(struct device *dev, const void *data)
+{
+	struct cxl_endpoint_decoder *cxled;
+
+	if (!is_endpoint_decoder(dev))
+		return 0;
+
+	cxled = to_cxl_endpoint_decoder(dev);
+	if (cxled->cxld.region)
+		return 1;
+
+	return 0;
+}
+
+/*
+ * Runs in cxl_mem_probe context after successful endpoint probe, assumes the
+ * simple case of single mapped decoder per memdev.
+ */
+int cxl_memdev_attach_region(struct cxl_memdev *cxlmd)
+{
+	struct cxl_attach_region *attach =
+		container_of(cxlmd->attach, typeof(*attach), attach);
+	struct cxl_port *endpoint = cxlmd->endpoint;
+	struct cxl_endpoint_decoder *cxled;
+	struct cxl_region *cxlr;
+	int rc;
+
+	/* hold endpoint lock to setup autoremove of the region */
+	guard(device)(&endpoint->dev);
+	if (!endpoint->dev.driver)
+		return -ENXIO;
+	guard(rwsem_read)(&cxl_rwsem.region);
+	guard(rwsem_read)(&cxl_rwsem.dpa);
+
+	/*
+	 * TODO auto-instantiate a region, for now assume this will find an
+	 * auto-region
+	 */
+	struct device *dev __free(put_device) =
+		device_find_child(&endpoint->dev, NULL, first_mapped_decoder);
+
+	if (!dev) {
+		dev_dbg(cxlmd->cxlds->dev, "no region found for memdev %s\n",
+			dev_name(&cxlmd->dev));
+		return -ENXIO;
+	}
+
+	cxled = to_cxl_endpoint_decoder(dev);
+	cxlr = cxled->cxld.region;
+
+	if (cxlr->params.state < CXL_CONFIG_COMMIT) {
+		dev_dbg(cxlmd->cxlds->dev,
+			"region %s not committed for memdev %s\n",
+			dev_name(&cxlr->dev), dev_name(&cxlmd->dev));
+		return -ENXIO;
+	}
+
+	if (cxlr->params.nr_targets > 1) {
+		dev_dbg(cxlmd->cxlds->dev,
+			"Only attach to local non-interleaved region\n");
+		return -ENXIO;
+	}
+
+	/* 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);
+	if (rc)
+		return rc;
+
+	attach->hpa_range = (struct range) {
+		.start = cxlr->params.res->start,
+		.end = cxlr->params.res->end,
+	};
+	return 0;
+}
+EXPORT_SYMBOL_FOR_MODULES(cxl_memdev_attach_region, "cxl_mem");
+
+/*
+ * The presence of an attach method indicates that the region is designated for
+ * a purpose outside of CXL core memory expansion defaults.
+ */
+static bool cxl_region_has_memdev_attach(struct cxl_region *cxlr)
+{
+	struct cxl_region_params *p = &cxlr->params;
+
+	for (int i = 0; i < p->nr_targets; i++) {
+		struct cxl_endpoint_decoder *cxled = p->targets[i];
+		struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
+
+		if (cxlmd->attach)
+			return true;
+	}
+
+	return false;
+}
+
 static int cxl_region_probe(struct device *dev)
 {
 	struct cxl_region *cxlr = to_cxl_region(dev);
@@ -4088,6 +4209,9 @@ static int cxl_region_probe(struct device *dev)
 	if (rc)
 		return rc;
 
+	if (cxl_region_has_memdev_attach(cxlr))
+		return 0;
+
 	switch (cxlr->mode) {
 	case CXL_PARTMODE_PMEM:
 		rc = devm_cxl_region_edac_register(cxlr);
drivers/cxl/mem.c +41 -9
index ff858318091f..67d31482f06b 100644
@@ -183,27 +183,59 @@ static int cxl_mem_probe(struct device *dev)
 }
 
 /**
- * devm_cxl_add_memdev - Add a CXL memory device
+ * devm_cxl_add_classdev - Add a CXL memory class-code device
  * @cxlds: CXL device state to associate with the memdev
- * @attach: Caller depends on CXL topology attachment
  *
  * Upon return the device will have had a chance to attach to the
  * cxl_mem driver, but may fail to attach if the CXL topology is not ready
  * (hardware CXL link down, or software platform CXL root not attached).
  *
- * When @attach is NULL it indicates the caller wants the memdev to remain
- * registered even if it does not immediately attach to the CXL hierarchy. When
- * @attach is provided a cxl_mem_probe() failure leads to failure of this routine.
+ * The parent of the resulting device and the devm context for allocations is
+ * @cxlds->dev.
+ */
+struct cxl_memdev *devm_cxl_add_classdev(struct cxl_dev_state *cxlds)
+{
+	return __devm_cxl_add_memdev(cxlds, NULL);
+}
+EXPORT_SYMBOL_NS_GPL(devm_cxl_add_classdev, "CXL");
+
+/**
+ * devm_cxl_probe_mem - Add a CXL memory device and probe its region
+ * @cxlds: CXL device state to associate with the memdev
+ * @hpa_range: CXL.mem physical address range result
+ *
+ * Upon return the device will have had a chance to attach to the
+ * cxl_mem driver, but may fail to attach if the CXL topology is not ready
+ * (hardware CXL link down, or software platform CXL root not attached).
+ *
+ * Failure to probe the memdev and/or setup a region for the memdev
+ * results in this function failing.
  *
  * The parent of the resulting device and the devm context for allocations is
  * @cxlds->dev.
  */
-struct cxl_memdev *devm_cxl_add_memdev(struct cxl_dev_state *cxlds,
-				       const struct cxl_memdev_attach *attach)
+struct cxl_memdev *devm_cxl_probe_mem(struct cxl_dev_state *cxlds,
+				      struct range *hpa_range)
 {
-	return __devm_cxl_add_memdev(cxlds, attach);
+	struct cxl_attach_region *attach =
+		devm_kmalloc(cxlds->dev, sizeof(*attach), GFP_KERNEL);
+	struct cxl_memdev *cxlmd;
+
+	if (!attach)
+		return ERR_PTR(-ENOMEM);
+
+	*attach = (struct cxl_attach_region) {
+		.attach = {
+			   .probe = cxl_memdev_attach_region,
+		},
+		.hpa_range = { 0, -1 },
+	};
+
+	cxlmd = __devm_cxl_add_memdev(cxlds, &attach->attach);
+	*hpa_range = attach->hpa_range;
+	return cxlmd;
 }
-EXPORT_SYMBOL_NS_GPL(devm_cxl_add_memdev, "CXL");
+EXPORT_SYMBOL_NS_GPL(devm_cxl_probe_mem, "CXL");
 
 static ssize_t trigger_poison_list_store(struct device *dev,
 					 struct device_attribute *attr,
drivers/cxl/pci.c +1 -1
index bace662dc988..267c679b0b3c 100644
@@ -878,7 +878,7 @@ static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	if (rc)
 		dev_dbg(&pdev->dev, "No CXL Features discovered\n");
 
-	cxlmd = devm_cxl_add_memdev(cxlds, NULL);
+	cxlmd = devm_cxl_add_classdev(cxlds);
 	if (IS_ERR(cxlmd))
 		return PTR_ERR(cxlmd);
 
tools/testing/cxl/test/mem.c +1 -1
index 271c7ad8cc32..095ca544ac02 100644
@@ -1769,7 +1769,7 @@ static int cxl_mock_mem_probe(struct platform_device *pdev)
 
 	cxl_mock_add_event_logs(&mdata->mes);
 
-	cxlmd = devm_cxl_add_memdev(cxlds, NULL);
+	cxlmd = devm_cxl_add_classdev(cxlds);
 	if (IS_ERR(cxlmd))
 		return PTR_ERR(cxlmd);
 
maintainer
#15 lore raw

Re: [PATCH 5/5] cxl/region: Introduce devm_cxl_probe_mem()

In reply to Dan Williams
Quoted 112 lines · Dan Williams

To date, platform firmware maps accelerator memory and accelerator drivers simply want an address range that they can map themselves. This typically results in a single region being auto-assembled upon registration of a memory device. Use the @attach mechanism of devm_cxl_add_memdev() parameter to retrieve that region while also adhering to CXL subsystem locking and lifetime rules. As part of adhering to current object lifetime rules, if the region or the CXL port topology is invalidated, the CXL core arranges for the accelertor driver to be detached as well.

The locking and lifetime rules were validated with Dave's work-in-progress cxl-type-2 support for cxl_test.

devm_cxl_add_classdev() supports the general memory expansion flow where region assembly is optional, dynamic, and user controlled.

Cc Alejandro Lucero <[email protected]>
---
 drivers/cxl/cxlmem.h         |  27 ++++++--
 include/cxl/cxl.h            |   3 +
 drivers/cxl/core/region.c    | 124 +++++++++++++++++++++++++++++++++++
 drivers/cxl/mem.c            |  50 +++++++++++---
 drivers/cxl/pci.c            |   2 +-
 tools/testing/cxl/test/mem.c |   2 +-
 6 files changed, 191 insertions(+), 17 deletions(-)
drivers/cxl/cxlmem.h +21 -6
index 776c50d1db51..d3bdd00f94b3 100644
@@ -34,10 +34,6 @@
 	(FIELD_GET(CXLMDEV_RESET_NEEDED_MASK, status) !=                       \
 	 CXLMDEV_RESET_NEEDED_NOT)
 
-struct cxl_memdev_attach {
-	int (*probe)(struct cxl_memdev *cxlmd);
-};
-
 /**
  * struct cxl_memdev - CXL bus object representing a Type-3 Memory Device
  * @dev: driver core device object
@@ -101,10 +97,29 @@ static inline bool is_cxl_endpoint(struct cxl_port *port)
 	return is_cxl_memdev(port->uport_dev);
 }
 
+struct cxl_memdev_attach {
+	int (*probe)(struct cxl_memdev *cxlmd);
+};
+
+/**
+ * struct cxl_attach_region - coordinate mapping a region at memdev registration
+ * @attach: common core attachment descriptor
+ * @hpa_range: physical address range of the region
+ *
+ * For the common simple case of a CXL device with private (non-general purpose
+ * / "accelerator") memory, enumerate firmware instantiated region, or
+ * instantiate a region for the device's capacity. Destroy the region on detach.
+ */
+struct cxl_attach_region {
+	struct cxl_memdev_attach attach;
+	struct range hpa_range;
+};
+
+int cxl_memdev_attach_region(struct cxl_memdev *cxlmd);
+
+struct cxl_memdev *devm_cxl_add_classdev(struct cxl_dev_state *cxlds);
 struct cxl_memdev *__devm_cxl_add_memdev(struct cxl_dev_state *cxlds,
 					 const struct cxl_memdev_attach *attach);
-struct cxl_memdev *devm_cxl_add_memdev(struct cxl_dev_state *cxlds,
-				       const struct cxl_memdev_attach *attach);
 int devm_cxl_sanitize_setup_notifier(struct device *host,
 				     struct cxl_memdev *cxlmd);
 struct cxl_memdev_state;
include/cxl/cxl.h +3 -0
index fa7269154620..016c74fb747c 100644
@@ -223,4 +223,7 @@ struct cxl_dev_state *_devm_cxl_dev_state_create(struct device *dev,
 		(drv_struct *)_devm_cxl_dev_state_create(parent, type, serial, dvsec,	\
 						      sizeof(drv_struct), mbox);	\
 	})
+
+struct cxl_memdev *devm_cxl_probe_mem(struct cxl_dev_state *cxlds,
+				      struct range *range);
 #endif /* __CXL_CXL_H__ */
drivers/cxl/core/region.c +13 -0
index faf9785c0509..ce99f0650764 100644
@@ -1148,6 +1148,19 @@ static int cxl_rr_assign_decoder(struct cxl_port *port, struct cxl_region *cxlr,
 static void cxl_region_setup_flags(struct cxl_region *cxlr,
 				   struct cxl_decoder *cxld)
 {
+	if (is_endpoint_decoder(&cxld->dev)) {
+		struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(&cxld->dev);
+		struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
+
+		/*
+		 * When a region's memdevs specify an @attach method the attach
+		 * provider is responsible for dispositioning the region for
+		 * both probe and userspace management
+		 */
+		if (cxlmd->attach)
+			set_bit(CXL_REGION_F_LOCK, &cxlr->flags);
+	}
+
 	if (cxld->flags & CXL_DECODER_F_LOCK) {
 		set_bit(CXL_REGION_F_LOCK, &cxlr->flags);
 		clear_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags);
@@ -2560,6 +2573,17 @@ static void unregister_region(struct cxl_region *cxlr)
 	put_device(&cxlr->dev);
 }

drivers/cxl/core/region.c static void unregister_region(struct cxl_region *cxlr) · line 2584

Quoted 10 lines
drivers/cxl/core/region.c +9 -0
 
+static void endpoint_unregister_region(void *_cxlr)
+{
+	struct cxl_region *cxlr = _cxlr;
+	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
+
+	guard(mutex)(&cxlrd->regions_lock);
+	if (xa_load(&cxlrd->regions, cxlr->id))
+		unregister_region(cxlr);
+	put_device(&cxlr->dev);

Claude mentioned that there may be a refcount underflow happening here when xa_load() succeeds, where unregister_region() calls put_device(&cxlr->dev) and then another put_device(&cxlr->dev) in endpoint_unregister_region().

DJ

Quoted 218 lines
drivers/cxl/core/region.c +102 -0
+}
+
 static struct lock_class_key cxl_region_key;
 
 static struct cxl_region *cxl_region_alloc(struct cxl_root_decoder *cxlrd, int id)
@@ -4057,6 +4081,103 @@ static int cxl_region_can_probe(struct cxl_region *cxlr)
 	return 0;
 }
 
+static int first_mapped_decoder(struct device *dev, const void *data)
+{
+	struct cxl_endpoint_decoder *cxled;
+
+	if (!is_endpoint_decoder(dev))
+		return 0;
+
+	cxled = to_cxl_endpoint_decoder(dev);
+	if (cxled->cxld.region)
+		return 1;
+
+	return 0;
+}
+
+/*
+ * Runs in cxl_mem_probe context after successful endpoint probe, assumes the
+ * simple case of single mapped decoder per memdev.
+ */
+int cxl_memdev_attach_region(struct cxl_memdev *cxlmd)
+{
+	struct cxl_attach_region *attach =
+		container_of(cxlmd->attach, typeof(*attach), attach);
+	struct cxl_port *endpoint = cxlmd->endpoint;
+	struct cxl_endpoint_decoder *cxled;
+	struct cxl_region *cxlr;
+	int rc;
+
+	/* hold endpoint lock to setup autoremove of the region */
+	guard(device)(&endpoint->dev);
+	if (!endpoint->dev.driver)
+		return -ENXIO;
+	guard(rwsem_read)(&cxl_rwsem.region);
+	guard(rwsem_read)(&cxl_rwsem.dpa);
+
+	/*
+	 * TODO auto-instantiate a region, for now assume this will find an
+	 * auto-region
+	 */
+	struct device *dev __free(put_device) =
+		device_find_child(&endpoint->dev, NULL, first_mapped_decoder);
+
+	if (!dev) {
+		dev_dbg(cxlmd->cxlds->dev, "no region found for memdev %s\n",
+			dev_name(&cxlmd->dev));
+		return -ENXIO;
+	}
+
+	cxled = to_cxl_endpoint_decoder(dev);
+	cxlr = cxled->cxld.region;
+
+	if (cxlr->params.state < CXL_CONFIG_COMMIT) {
+		dev_dbg(cxlmd->cxlds->dev,
+			"region %s not committed for memdev %s\n",
+			dev_name(&cxlr->dev), dev_name(&cxlmd->dev));
+		return -ENXIO;
+	}
+
+	if (cxlr->params.nr_targets > 1) {
+		dev_dbg(cxlmd->cxlds->dev,
+			"Only attach to local non-interleaved region\n");
+		return -ENXIO;
+	}
+
+	/* 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);
+	if (rc)
+		return rc;
+
+	attach->hpa_range = (struct range) {
+		.start = cxlr->params.res->start,
+		.end = cxlr->params.res->end,
+	};
+	return 0;
+}
+EXPORT_SYMBOL_FOR_MODULES(cxl_memdev_attach_region, "cxl_mem");
+
+/*
+ * The presence of an attach method indicates that the region is designated for
+ * a purpose outside of CXL core memory expansion defaults.
+ */
+static bool cxl_region_has_memdev_attach(struct cxl_region *cxlr)
+{
+	struct cxl_region_params *p = &cxlr->params;
+
+	for (int i = 0; i < p->nr_targets; i++) {
+		struct cxl_endpoint_decoder *cxled = p->targets[i];
+		struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
+
+		if (cxlmd->attach)
+			return true;
+	}
+
+	return false;
+}
+
 static int cxl_region_probe(struct device *dev)
 {
 	struct cxl_region *cxlr = to_cxl_region(dev);
@@ -4088,6 +4209,9 @@ static int cxl_region_probe(struct device *dev)
 	if (rc)
 		return rc;
 
+	if (cxl_region_has_memdev_attach(cxlr))
+		return 0;
+
 	switch (cxlr->mode) {
 	case CXL_PARTMODE_PMEM:
 		rc = devm_cxl_region_edac_register(cxlr);
drivers/cxl/mem.c +41 -9
index ff858318091f..67d31482f06b 100644
@@ -183,27 +183,59 @@ static int cxl_mem_probe(struct device *dev)
 }
 
 /**
- * devm_cxl_add_memdev - Add a CXL memory device
+ * devm_cxl_add_classdev - Add a CXL memory class-code device
  * @cxlds: CXL device state to associate with the memdev
- * @attach: Caller depends on CXL topology attachment
  *
  * Upon return the device will have had a chance to attach to the
  * cxl_mem driver, but may fail to attach if the CXL topology is not ready
  * (hardware CXL link down, or software platform CXL root not attached).
  *
- * When @attach is NULL it indicates the caller wants the memdev to remain
- * registered even if it does not immediately attach to the CXL hierarchy. When
- * @attach is provided a cxl_mem_probe() failure leads to failure of this routine.
+ * The parent of the resulting device and the devm context for allocations is
+ * @cxlds->dev.
+ */
+struct cxl_memdev *devm_cxl_add_classdev(struct cxl_dev_state *cxlds)
+{
+	return __devm_cxl_add_memdev(cxlds, NULL);
+}
+EXPORT_SYMBOL_NS_GPL(devm_cxl_add_classdev, "CXL");
+
+/**
+ * devm_cxl_probe_mem - Add a CXL memory device and probe its region
+ * @cxlds: CXL device state to associate with the memdev
+ * @hpa_range: CXL.mem physical address range result
+ *
+ * Upon return the device will have had a chance to attach to the
+ * cxl_mem driver, but may fail to attach if the CXL topology is not ready
+ * (hardware CXL link down, or software platform CXL root not attached).
+ *
+ * Failure to probe the memdev and/or setup a region for the memdev
+ * results in this function failing.
  *
  * The parent of the resulting device and the devm context for allocations is
  * @cxlds->dev.
  */
-struct cxl_memdev *devm_cxl_add_memdev(struct cxl_dev_state *cxlds,
-				       const struct cxl_memdev_attach *attach)
+struct cxl_memdev *devm_cxl_probe_mem(struct cxl_dev_state *cxlds,
+				      struct range *hpa_range)
 {
-	return __devm_cxl_add_memdev(cxlds, attach);
+	struct cxl_attach_region *attach =
+		devm_kmalloc(cxlds->dev, sizeof(*attach), GFP_KERNEL);
+	struct cxl_memdev *cxlmd;
+
+	if (!attach)
+		return ERR_PTR(-ENOMEM);
+
+	*attach = (struct cxl_attach_region) {
+		.attach = {
+			   .probe = cxl_memdev_attach_region,
+		},
+		.hpa_range = { 0, -1 },
+	};
+
+	cxlmd = __devm_cxl_add_memdev(cxlds, &attach->attach);
+	*hpa_range = attach->hpa_range;
+	return cxlmd;
 }
-EXPORT_SYMBOL_NS_GPL(devm_cxl_add_memdev, "CXL");
+EXPORT_SYMBOL_NS_GPL(devm_cxl_probe_mem, "CXL");
 
 static ssize_t trigger_poison_list_store(struct device *dev,
 					 struct device_attribute *attr,
drivers/cxl/pci.c +1 -1
index bace662dc988..267c679b0b3c 100644
@@ -878,7 +878,7 @@ static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	if (rc)
 		dev_dbg(&pdev->dev, "No CXL Features discovered\n");
 
-	cxlmd = devm_cxl_add_memdev(cxlds, NULL);
+	cxlmd = devm_cxl_add_classdev(cxlds);
 	if (IS_ERR(cxlmd))
 		return PTR_ERR(cxlmd);
 
tools/testing/cxl/test/mem.c +1 -1
index 271c7ad8cc32..095ca544ac02 100644
@@ -1769,7 +1769,7 @@ static int cxl_mock_mem_probe(struct platform_device *pdev)
 
 	cxl_mock_add_event_logs(&mdata->mes);
 
-	cxlmd = devm_cxl_add_memdev(cxlds, NULL);
+	cxlmd = devm_cxl_add_classdev(cxlds);
 	if (IS_ERR(cxlmd))
 		return PTR_ERR(cxlmd);
 
Reviewed-by Tested-by on v1 5/5
#16 lore raw

Re: [PATCH 5/5] cxl/region: Introduce devm_cxl_probe_mem()

In reply to Dan Williams
Quoted 9 lines · Dan Williams

To date, platform firmware maps accelerator memory and accelerator drivers simply want an address range that they can map themselves. This typically results in a single region being auto-assembled upon registration of a memory device. Use the @attach mechanism of devm_cxl_add_memdev() parameter to retrieve that region while also adhering to CXL subsystem locking and lifetime rules. As part of adhering to current object lifetime rules, if the region or the CXL port topology is invalidated, the CXL core arranges for the accelertor driver to be detached as well.

Quoted 8 lines

The locking and lifetime rules were validated with Dave's work-in-progress cxl-type-2 support for cxl_test.

devm_cxl_add_classdev() supports the general memory expansion flow where region assembly is optional, dynamic, and user controlled.

Cc Alejandro Lucero <[email protected]>

I think this is all good enough as long as we only support:

    1) the accelerator driver removal if cxl_acpi or memdev unbound

    2) autodiscovered regions

what is what we need now as a priority. So I'm happy with it. Let's deal with the complexity for other cases at its due time.

My other concern with the driver removal was a potential race with driver probing ... which I do not think it can happen as the device will be locked during probing. So apart from a minor suggestion below:

Quoted 262 lines
---
  drivers/cxl/cxlmem.h         |  27 ++++++--
  include/cxl/cxl.h            |   3 +
  drivers/cxl/core/region.c    | 124 +++++++++++++++++++++++++++++++++++
  drivers/cxl/mem.c            |  50 +++++++++++---
  drivers/cxl/pci.c            |   2 +-
  tools/testing/cxl/test/mem.c |   2 +-
  6 files changed, 191 insertions(+), 17 deletions(-)
drivers/cxl/cxlmem.h +21 -6
index 776c50d1db51..d3bdd00f94b3 100644
@@ -34,10 +34,6 @@
  	(FIELD_GET(CXLMDEV_RESET_NEEDED_MASK, status) !=                       \
  	 CXLMDEV_RESET_NEEDED_NOT)
 
-struct cxl_memdev_attach {
-	int (*probe)(struct cxl_memdev *cxlmd);
-};
-
  /**
   * struct cxl_memdev - CXL bus object representing a Type-3 Memory Device
   * @dev: driver core device object
@@ -101,10 +97,29 @@ static inline bool is_cxl_endpoint(struct cxl_port *port)
  	return is_cxl_memdev(port->uport_dev);
  }
 
+struct cxl_memdev_attach {
+	int (*probe)(struct cxl_memdev *cxlmd);
+};
+
+/**
+ * struct cxl_attach_region - coordinate mapping a region at memdev registration
+ * @attach: common core attachment descriptor
+ * @hpa_range: physical address range of the region
+ *
+ * For the common simple case of a CXL device with private (non-general purpose
+ * / "accelerator") memory, enumerate firmware instantiated region, or
+ * instantiate a region for the device's capacity. Destroy the region on detach.
+ */
+struct cxl_attach_region {
+	struct cxl_memdev_attach attach;
+	struct range hpa_range;
+};
+
+int cxl_memdev_attach_region(struct cxl_memdev *cxlmd);
+
+struct cxl_memdev *devm_cxl_add_classdev(struct cxl_dev_state *cxlds);
  struct cxl_memdev *__devm_cxl_add_memdev(struct cxl_dev_state *cxlds,
  					 const struct cxl_memdev_attach *attach);
-struct cxl_memdev *devm_cxl_add_memdev(struct cxl_dev_state *cxlds,
-				       const struct cxl_memdev_attach *attach);
  int devm_cxl_sanitize_setup_notifier(struct device *host,
  				     struct cxl_memdev *cxlmd);
  struct cxl_memdev_state;
include/cxl/cxl.h +3 -0
index fa7269154620..016c74fb747c 100644
@@ -223,4 +223,7 @@ struct cxl_dev_state *_devm_cxl_dev_state_create(struct device *dev,
  		(drv_struct *)_devm_cxl_dev_state_create(parent, type, serial, dvsec,	\
  						      sizeof(drv_struct), mbox);	\
  	})
+
+struct cxl_memdev *devm_cxl_probe_mem(struct cxl_dev_state *cxlds,
+				      struct range *range);
  #endif /* __CXL_CXL_H__ */
drivers/cxl/core/region.c +124 -0
index faf9785c0509..ce99f0650764 100644
@@ -1148,6 +1148,19 @@ static int cxl_rr_assign_decoder(struct cxl_port *port, struct cxl_region *cxlr,
  static void cxl_region_setup_flags(struct cxl_region *cxlr,
  				   struct cxl_decoder *cxld)
  {
+	if (is_endpoint_decoder(&cxld->dev)) {
+		struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(&cxld->dev);
+		struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
+
+		/*
+		 * When a region's memdevs specify an @attach method the attach
+		 * provider is responsible for dispositioning the region for
+		 * both probe and userspace management
+		 */
+		if (cxlmd->attach)
+			set_bit(CXL_REGION_F_LOCK, &cxlr->flags);
+	}
+
  	if (cxld->flags & CXL_DECODER_F_LOCK) {
  		set_bit(CXL_REGION_F_LOCK, &cxlr->flags);
  		clear_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags);
@@ -2560,6 +2573,17 @@ static void unregister_region(struct cxl_region *cxlr)
  	put_device(&cxlr->dev);
  }
 
+static void endpoint_unregister_region(void *_cxlr)
+{
+	struct cxl_region *cxlr = _cxlr;
+	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
+
+	guard(mutex)(&cxlrd->regions_lock);
+	if (xa_load(&cxlrd->regions, cxlr->id))
+		unregister_region(cxlr);
+	put_device(&cxlr->dev);
+}
+
  static struct lock_class_key cxl_region_key;
 
  static struct cxl_region *cxl_region_alloc(struct cxl_root_decoder *cxlrd, int id)
@@ -4057,6 +4081,103 @@ static int cxl_region_can_probe(struct cxl_region *cxlr)
  	return 0;
  }
 
+static int first_mapped_decoder(struct device *dev, const void *data)
+{
+	struct cxl_endpoint_decoder *cxled;
+
+	if (!is_endpoint_decoder(dev))
+		return 0;
+
+	cxled = to_cxl_endpoint_decoder(dev);
+	if (cxled->cxld.region)
+		return 1;
+
+	return 0;
+}
+
+/*
+ * Runs in cxl_mem_probe context after successful endpoint probe, assumes the
+ * simple case of single mapped decoder per memdev.
+ */
+int cxl_memdev_attach_region(struct cxl_memdev *cxlmd)
+{
+	struct cxl_attach_region *attach =
+		container_of(cxlmd->attach, typeof(*attach), attach);
+	struct cxl_port *endpoint = cxlmd->endpoint;
+	struct cxl_endpoint_decoder *cxled;
+	struct cxl_region *cxlr;
+	int rc;
+
+	/* hold endpoint lock to setup autoremove of the region */
+	guard(device)(&endpoint->dev);
+	if (!endpoint->dev.driver)
+		return -ENXIO;
+	guard(rwsem_read)(&cxl_rwsem.region);
+	guard(rwsem_read)(&cxl_rwsem.dpa);
+
+	/*
+	 * TODO auto-instantiate a region, for now assume this will find an
+	 * auto-region
+	 */
+	struct device *dev __free(put_device) =
+		device_find_child(&endpoint->dev, NULL, first_mapped_decoder);
+
+	if (!dev) {
+		dev_dbg(cxlmd->cxlds->dev, "no region found for memdev %s\n",
+			dev_name(&cxlmd->dev));
+		return -ENXIO;
+	}
+
+	cxled = to_cxl_endpoint_decoder(dev);
+	cxlr = cxled->cxld.region;
+
+	if (cxlr->params.state < CXL_CONFIG_COMMIT) {
+		dev_dbg(cxlmd->cxlds->dev,
+			"region %s not committed for memdev %s\n",
+			dev_name(&cxlr->dev), dev_name(&cxlmd->dev));
+		return -ENXIO;
+	}
+
+	if (cxlr->params.nr_targets > 1) {
+		dev_dbg(cxlmd->cxlds->dev,
+			"Only attach to local non-interleaved region\n");
+		return -ENXIO;
+	}
+
+	/* 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);
+	if (rc)
+		return rc;
+
+	attach->hpa_range = (struct range) {
+		.start = cxlr->params.res->start,
+		.end = cxlr->params.res->end,
+	};
+	return 0;
+}
+EXPORT_SYMBOL_FOR_MODULES(cxl_memdev_attach_region, "cxl_mem");
+
+/*
+ * The presence of an attach method indicates that the region is designated for
+ * a purpose outside of CXL core memory expansion defaults.
+ */
+static bool cxl_region_has_memdev_attach(struct cxl_region *cxlr)
+{
+	struct cxl_region_params *p = &cxlr->params;
+
+	for (int i = 0; i < p->nr_targets; i++) {
+		struct cxl_endpoint_decoder *cxled = p->targets[i];
+		struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
+
+		if (cxlmd->attach)
+			return true;
+	}
+
+	return false;
+}
+
  static int cxl_region_probe(struct device *dev)
  {
  	struct cxl_region *cxlr = to_cxl_region(dev);
@@ -4088,6 +4209,9 @@ static int cxl_region_probe(struct device *dev)
  	if (rc)
  		return rc;
 
+	if (cxl_region_has_memdev_attach(cxlr))
+		return 0;
+
  	switch (cxlr->mode) {
  	case CXL_PARTMODE_PMEM:
  		rc = devm_cxl_region_edac_register(cxlr);
drivers/cxl/mem.c +19 -5
index ff858318091f..67d31482f06b 100644
@@ -183,27 +183,59 @@ static int cxl_mem_probe(struct device *dev)
  }
 
  /**
- * devm_cxl_add_memdev - Add a CXL memory device
+ * devm_cxl_add_classdev - Add a CXL memory class-code device
   * @cxlds: CXL device state to associate with the memdev
- * @attach: Caller depends on CXL topology attachment
   *
   * Upon return the device will have had a chance to attach to the
   * cxl_mem driver, but may fail to attach if the CXL topology is not ready
   * (hardware CXL link down, or software platform CXL root not attached).
   *
- * When @attach is NULL it indicates the caller wants the memdev to remain
- * registered even if it does not immediately attach to the CXL hierarchy. When
- * @attach is provided a cxl_mem_probe() failure leads to failure of this routine.
+ * The parent of the resulting device and the devm context for allocations is
+ * @cxlds->dev.
+ */
+struct cxl_memdev *devm_cxl_add_classdev(struct cxl_dev_state *cxlds)
+{
+	return __devm_cxl_add_memdev(cxlds, NULL);
+}
+EXPORT_SYMBOL_NS_GPL(devm_cxl_add_classdev, "CXL");
+
+/**
+ * devm_cxl_probe_mem - Add a CXL memory device and probe its region
+ * @cxlds: CXL device state to associate with the memdev
+ * @hpa_range: CXL.mem physical address range result
+ *
+ * Upon return the device will have had a chance to attach to the
+ * cxl_mem driver, but may fail to attach if the CXL topology is not ready
+ * (hardware CXL link down, or software platform CXL root not attached).
+ *

drivers/cxl/mem.c static int cxl_mem_probe(struct device *dev) · line 218

Quoted 10 lines
drivers/cxl/mem.c +4 -2
+ * Failure to probe the memdev and/or setup a region for the memdev
+ * results in this function failing.
   *
   * The parent of the resulting device and the devm context for allocations is
   * @cxlds->dev.
   */
-struct cxl_memdev *devm_cxl_add_memdev(struct cxl_dev_state *cxlds,
-				       const struct cxl_memdev_attach *attach)
+struct cxl_memdev *devm_cxl_probe_mem(struct cxl_dev_state *cxlds,
+				      struct range *hpa_range)

The accelerator driver does not need the cxl_memdev reference, just the range to work with.

I would suggest to change the interface to

struct range *devm_cxl_mem_probe(struct cxl_dev_state *cxlds)

then return pointer to attach->hpa_range;

Quoted 51 lines
drivers/cxl/mem.c +18 -2
  {
-	return __devm_cxl_add_memdev(cxlds, attach);
+	struct cxl_attach_region *attach =
+		devm_kmalloc(cxlds->dev, sizeof(*attach), GFP_KERNEL);
+	struct cxl_memdev *cxlmd;
+
+	if (!attach)
+		return ERR_PTR(-ENOMEM);
+
+	*attach = (struct cxl_attach_region) {
+		.attach = {
+			   .probe = cxl_memdev_attach_region,
+		},
+		.hpa_range = { 0, -1 },
+	};
+
+	cxlmd = __devm_cxl_add_memdev(cxlds, &attach->attach);
+	*hpa_range = attach->hpa_range;
+	return cxlmd;
  }
-EXPORT_SYMBOL_NS_GPL(devm_cxl_add_memdev, "CXL");
+EXPORT_SYMBOL_NS_GPL(devm_cxl_probe_mem, "CXL");
 
  static ssize_t trigger_poison_list_store(struct device *dev,
  					 struct device_attribute *attr,
drivers/cxl/pci.c +1 -1
index bace662dc988..267c679b0b3c 100644
@@ -878,7 +878,7 @@ static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  	if (rc)
  		dev_dbg(&pdev->dev, "No CXL Features discovered\n");
 
-	cxlmd = devm_cxl_add_memdev(cxlds, NULL);
+	cxlmd = devm_cxl_add_classdev(cxlds);
  	if (IS_ERR(cxlmd))
  		return PTR_ERR(cxlmd);
 
tools/testing/cxl/test/mem.c +1 -1
index 271c7ad8cc32..095ca544ac02 100644
@@ -1769,7 +1769,7 @@ static int cxl_mock_mem_probe(struct platform_device *pdev)
 
  	cxl_mock_add_event_logs(&mdata->mes);
 
-	cxlmd = devm_cxl_add_memdev(cxlds, NULL);
+	cxlmd = devm_cxl_add_classdev(cxlds);
  	if (IS_ERR(cxlmd))
  		return PTR_ERR(cxlmd);
 
#17 lore raw
In reply to Dan Williams

Hi Dan,

Quoted 5 lines · Dan Williams

The devm_cxl_probe_mem() helper creates a memdev and retrieves a mapped HPA range for the device. At present the retrieved region information is expected to be established by platform firmware. A future update will auto-establish a region if one is not already present. This arranges for a simple CXL topology detach == Accelerator driver detach scheme.

I have successfully  tested this with sfc and it indeed simplifies the accelerator driver. Removing cxl_acpi or unbinding the related memdev works as expected removing the sfc driver as well.

I'm quite happy with this approach and I hope this is the last building block for the simplified (only with autodiscovered regions) type2 series.

Just a minor comment in patch 5 and a suggestion for maybe changing the devm_cxl_probe_mem interface call.

Cheers

Quoted 44 lines

Now, automatic region establishment on behalf of an accelerator driver also requires automatic region teardown. The existing scheme of triggering devm_release_action() of a cxl root device to destroy a region is racy [1]. There is no path to coordinate multiple devres actions, or conflicting devres actions, without using the devm_remove_action_nowarn() workaround.

Instead, move final region cleanup responsibility to the root decoder teardown path (devres action cxld_unregister()). Expand the root decoder @range_lock, renamed @regions_lock, to coordinate region creation, sysfs manageability, and deletion. The @regions_lock also prevents regions from disappearing mid-auto-assembly.

Changes from the initial RFC [2]:
* tested with Dave's type2 cxl_test
* fix region remove by endpoints (Dave, Alejandro)
* fix user triggered region remove (Sungwoo)
* fix lifetime of the @attach ops (Alison, sashiko)
* hide @attach details inside the CXL core

[1]: http://lore.kernel.org/[email protected]
[2]: http://lore.kernel.org/[email protected]

Dan Williams (5):
cxl/region: Block region delete during region creation cxl/region: Resolve region deletion races cxl/memdev: Pin parents for entire memdev lifetime cxl/memdev: Introduce cxl_class_memdev_type cxl/region: Introduce devm_cxl_probe_mem()

  drivers/cxl/core/core.h      |   2 +
  drivers/cxl/cxl.h            |   8 +-
  drivers/cxl/cxlmem.h         |  27 +++--
  include/cxl/cxl.h            |   3 +
  drivers/cxl/core/memdev.c    |  20 +++-
  drivers/cxl/core/port.c      |   7 +-
  drivers/cxl/core/region.c    | 194 ++++++++++++++++++++++++++++++-----
  drivers/cxl/mem.c            |  95 +++++++++++++----
  drivers/cxl/pci.c            |   2 +-
  tools/testing/cxl/test/mem.c |   2 +-
  10 files changed, 297 insertions(+), 63 deletions(-)

base-commit: 5d6919055dec134de3c40167a490f33c74c12581

#18 lore raw
Quoted 10 lines · Alejandro Lucero Palau

Hi Dan,

Quoted 5 lines · Dan Williams

The devm_cxl_probe_mem() helper creates a memdev and retrieves a mapped HPA range for the device. At present the retrieved region information is expected to be established by platform firmware. A future update will auto-establish a region if one is not already present. This arranges for a simple CXL topology detach == Accelerator driver detach scheme.

Quoted 6 lines

I have successfully  tested this with sfc and it indeed simplifies the accelerator driver. Removing cxl_acpi or unbinding the related memdev works as expected removing the sfc driver as well.

I'm quite happy with this approach and I hope this is the last building block for the simplified (only with autodiscovered regions) type2 series.

No updates or comment for two weeks. I wonder if this is being superseded somehow and I did not notice.

Quoted 52 lines

Just a minor comment in patch 5 and a suggestion for maybe changing the devm_cxl_probe_mem interface call.

Cheers

Quoted 44 lines

Now, automatic region establishment on behalf of an accelerator driver also requires automatic region teardown. The existing scheme of triggering devm_release_action() of a cxl root device to destroy a region is racy [1].  There is no path to coordinate multiple devres actions, or conflicting devres actions, without using the devm_remove_action_nowarn() workaround.

Instead, move final region cleanup responsibility to the root decoder teardown path (devres action cxld_unregister()). Expand the root decoder @range_lock, renamed @regions_lock, to coordinate region creation, sysfs manageability, and deletion. The @regions_lock also prevents regions from disappearing mid-auto-assembly.

Changes from the initial RFC [2]:
* tested with Dave's type2 cxl_test
* fix region remove by endpoints (Dave, Alejandro)
* fix user triggered region remove (Sungwoo)
* fix lifetime of the @attach ops (Alison, sashiko)
* hide @attach details inside the CXL core

[1]: http://lore.kernel.org/[email protected]
[2]: http://lore.kernel.org/[email protected]

Dan Williams (5):
   cxl/region: Block region delete during region creation cxl/region: Resolve region deletion races cxl/memdev: Pin parents for entire memdev lifetime cxl/memdev: Introduce cxl_class_memdev_type cxl/region: Introduce devm_cxl_probe_mem()

  drivers/cxl/core/core.h      |   2 +
  drivers/cxl/cxl.h            |   8 +-
  drivers/cxl/cxlmem.h         |  27 +++--
  include/cxl/cxl.h            |   3 + drivers/cxl/core/memdev.c    |  20 +++- drivers/cxl/core/port.c      |   7 +- drivers/cxl/core/region.c    | 194 ++++++++++++++++++++++++++++++----- drivers/cxl/mem.c            |  95 +++++++++++++---- drivers/cxl/pci.c            |   2 +- tools/testing/cxl/test/mem.c |   2 +-

  10 files changed, 297 insertions(+), 63 deletions(-)

base-commit: 5d6919055dec134de3c40167a490f33c74c12581

maintainer
#19 lore raw
Quoted 12 lines · Alejandro Lucero Palau
Quoted 10 lines · Alejandro Lucero Palau

Hi Dan,

Quoted 5 lines · Dan Williams

The devm_cxl_probe_mem() helper creates a memdev and retrieves a mapped HPA range for the device. At present the retrieved region information is expected to be established by platform firmware. A future update will auto-establish a region if one is not already present. This arranges for a simple CXL topology detach == Accelerator driver detach scheme.

Quoted 8 lines
Quoted 6 lines

I have successfully  tested this with sfc and it indeed simplifies the accelerator driver. Removing cxl_acpi or unbinding the related memdev works as expected removing the sfc driver as well.

I'm quite happy with this approach and I hope this is the last building block for the simplified (only with autodiscovered regions) type2 series.

No updates or comment for two weeks. I wonder if this is being superseded somehow and I did not notice.

Are you not integrating this series with yours? At least that's my understanding given you are happy with the approach.

DJ

Quoted 54 lines
Quoted 54 lines

Just a minor comment in patch 5 and a suggestion for maybe changing the devm_cxl_probe_mem interface call.

Cheers

Quoted 44 lines

Now, automatic region establishment on behalf of an accelerator driver also requires automatic region teardown. The existing scheme of triggering devm_release_action() of a cxl root device to destroy a region is racy [1].  There is no path to coordinate multiple devres actions, or conflicting devres actions, without using the devm_remove_action_nowarn() workaround.

Instead, move final region cleanup responsibility to the root decoder teardown path (devres action cxld_unregister()). Expand the root decoder @range_lock, renamed @regions_lock, to coordinate region creation, sysfs manageability, and deletion. The @regions_lock also prevents regions from disappearing mid-auto-assembly.

Changes from the initial RFC [2]:
* tested with Dave's type2 cxl_test
* fix region remove by endpoints (Dave, Alejandro)
* fix user triggered region remove (Sungwoo)
* fix lifetime of the @attach ops (Alison, sashiko)
* hide @attach details inside the CXL core

[1]: http://lore.kernel.org/[email protected]
[2]: http://lore.kernel.org/[email protected]

Dan Williams (5):
   cxl/region: Block region delete during region creation cxl/region: Resolve region deletion races cxl/memdev: Pin parents for entire memdev lifetime cxl/memdev: Introduce cxl_class_memdev_type cxl/region: Introduce devm_cxl_probe_mem()

  drivers/cxl/core/core.h      |   2 +
  drivers/cxl/cxl.h            |   8 +-
  drivers/cxl/cxlmem.h         |  27 +++--
  include/cxl/cxl.h            |   3 + drivers/cxl/core/memdev.c    |  20 +++- drivers/cxl/core/port.c      |   7 +- drivers/cxl/core/region.c    | 194 ++++++++++++++++++++++++++++++----- drivers/cxl/mem.c            |  95 +++++++++++++---- drivers/cxl/pci.c            |   2 +- tools/testing/cxl/test/mem.c |   2 +-

  10 files changed, 297 insertions(+), 63 deletions(-)

base-commit: 5d6919055dec134de3c40167a490f33c74c12581

#20 lore raw
Quoted 15 lines · Dave Jiang
Quoted 13 lines · Alejandro Lucero Palau
Quoted 12 lines · Alejandro Lucero Palau

Hi Dan,

Quoted 5 lines · Dan Williams

The devm_cxl_probe_mem() helper creates a memdev and retrieves a mapped HPA range for the device. At present the retrieved region information is expected to be established by platform firmware. A future update will auto-establish a region if one is not already present. This arranges for a simple CXL topology detach == Accelerator driver detach scheme.

I have successfully  tested this with sfc and it indeed simplifies the accelerator driver. Removing cxl_acpi or unbinding the related memdev works as expected removing the sfc driver as well.

Quoted 7 lines
Quoted 4 lines
Quoted 3 lines

I'm quite happy with this approach and I hope this is the last building block for the simplified (only with autodiscovered regions) type2 series.

No updates or comment for two weeks. I wonder if this is being superseded somehow and I did not notice.

Are you not integrating this series with yours? At least that's my understanding given you are happy with the approach.

DJ

Hi Dave,

This series got your review tags plus my tested and review ones. It has not been merged and and not received further comments either.

If Dan wants me to integrate this with type2, fine, but I think it should go first. The final type2 bits along sfc using this new type2 support should keep simple and they will be even simpler after this patchset from Dan.

After 26 versions, I do not want to send new versions as soon as there exists a new possibility ... As I said, this series are promising and I hope they are the final step setting the way for my type2, but I guess you understand my precautions approach ...

In any case, if Dan or you want me to integrate this with my type2 series, please tell me.

Thank you,

Alejandro

Quoted 53 lines
Quoted 53 lines
Quoted 53 lines

Just a minor comment in patch 5 and a suggestion for maybe changing the devm_cxl_probe_mem interface call.

Cheers

Quoted 44 lines

Now, automatic region establishment on behalf of an accelerator driver also requires automatic region teardown. The existing scheme of triggering devm_release_action() of a cxl root device to destroy a region is racy [1].  There is no path to coordinate multiple devres actions, or conflicting devres actions, without using the devm_remove_action_nowarn() workaround.

Instead, move final region cleanup responsibility to the root decoder teardown path (devres action cxld_unregister()). Expand the root decoder @range_lock, renamed @regions_lock, to coordinate region creation, sysfs manageability, and deletion. The @regions_lock also prevents regions from disappearing mid-auto-assembly.

Changes from the initial RFC [2]:
* tested with Dave's type2 cxl_test
* fix region remove by endpoints (Dave, Alejandro)
* fix user triggered region remove (Sungwoo)
* fix lifetime of the @attach ops (Alison, sashiko)
* hide @attach details inside the CXL core

[1]: http://lore.kernel.org/[email protected]
[2]: http://lore.kernel.org/[email protected]

Dan Williams (5):
   cxl/region: Block region delete during region creation cxl/region: Resolve region deletion races cxl/memdev: Pin parents for entire memdev lifetime cxl/memdev: Introduce cxl_class_memdev_type cxl/region: Introduce devm_cxl_probe_mem()

   drivers/cxl/core/core.h      |   2 +
   drivers/cxl/cxl.h            |   8 +-
   drivers/cxl/cxlmem.h         |  27 +++--
   include/cxl/cxl.h            |   3 +
   drivers/cxl/core/memdev.c    |  20 +++-
   drivers/cxl/core/port.c      |   7 +-
   drivers/cxl/core/region.c    | 194 ++++++++++++++++++++++++++++++-----
   drivers/cxl/mem.c            |  95 +++++++++++++----
   drivers/cxl/pci.c            |   2 +-
   tools/testing/cxl/test/mem.c |   2 +-
   10 files changed, 297 insertions(+), 63 deletions(-)

base-commit: 5d6919055dec134de3c40167a490f33c74c12581

maintainer
#21 lore raw
Quoted 32 lines · Alejandro Lucero Palau
Quoted 15 lines · Dave Jiang
Quoted 13 lines · Alejandro Lucero Palau
Quoted 12 lines · Alejandro Lucero Palau

Hi Dan,

Quoted 5 lines · Dan Williams

The devm_cxl_probe_mem() helper creates a memdev and retrieves a mapped HPA range for the device. At present the retrieved region information is expected to be established by platform firmware. A future update will auto-establish a region if one is not already present. This arranges for a simple CXL topology detach == Accelerator driver detach scheme.

I have successfully  tested this with sfc and it indeed simplifies the accelerator driver. Removing cxl_acpi or unbinding the related memdev works as expected removing the sfc driver as well.

Quoted 7 lines
Quoted 4 lines
Quoted 3 lines

I'm quite happy with this approach and I hope this is the last building block for the simplified (only with autodiscovered regions) type2 series.

No updates or comment for two weeks. I wonder if this is being superseded somehow and I did not notice.

Are you not integrating this series with yours? At least that's my understanding given you are happy with the approach.

DJ

Hi Dave,

This series got your review tags plus my tested and review ones. It has not been merged and and not received further comments either.

Quoted 7 lines

If Dan wants me to integrate this with type2, fine, but I think it should go first. The final type2 bits along sfc using this new type2 support should keep simple and they will be even simpler after this patchset from Dan.

After 26 versions, I do not want to send new versions as soon as there exists a new possibility ... As I said, this series are promising and I hope they are the final step setting the way for my type2, but I guess you understand my precautions approach ...

In any case, if Dan or you want me to integrate this with my type2 series, please tell me.

I pushed the series from Dan to a branch [1]. Rebase your current type2 enabling code on top of that and post and we'll go from there.

[1]: https://git.kernel.org/pub/scm/linux/kernel/git/cxl/cxl.git/log/?h=for-7.3/cxl-type2-enabling

Quoted 61 lines

Thank you,

Alejandro

Quoted 53 lines
Quoted 53 lines
Quoted 53 lines

Just a minor comment in patch 5 and a suggestion for maybe changing the devm_cxl_probe_mem interface call.

Cheers

Quoted 44 lines

Now, automatic region establishment on behalf of an accelerator driver also requires automatic region teardown. The existing scheme of triggering devm_release_action() of a cxl root device to destroy a region is racy [1].  There is no path to coordinate multiple devres actions, or conflicting devres actions, without using the devm_remove_action_nowarn() workaround.

Instead, move final region cleanup responsibility to the root decoder teardown path (devres action cxld_unregister()). Expand the root decoder @range_lock, renamed @regions_lock, to coordinate region creation, sysfs manageability, and deletion. The @regions_lock also prevents regions from disappearing mid-auto-assembly.

Changes from the initial RFC [2]:
* tested with Dave's type2 cxl_test
* fix region remove by endpoints (Dave, Alejandro)
* fix user triggered region remove (Sungwoo)
* fix lifetime of the @attach ops (Alison, sashiko)
* hide @attach details inside the CXL core

[1]: http://lore.kernel.org/[email protected]
[2]: http://lore.kernel.org/[email protected]

Dan Williams (5):
    cxl/region: Block region delete during region creation cxl/region: Resolve region deletion races cxl/memdev: Pin parents for entire memdev lifetime cxl/memdev: Introduce cxl_class_memdev_type cxl/region: Introduce devm_cxl_probe_mem()

   drivers/cxl/core/core.h      |   2 +
   drivers/cxl/cxl.h            |   8 +-
   drivers/cxl/cxlmem.h         |  27 +++--
   include/cxl/cxl.h            |   3 + drivers/cxl/core/memdev.c    |  20 +++- drivers/cxl/core/port.c      |   7 +- drivers/cxl/core/region.c    | 194 ++++++++++++++++++++++++++++++----- drivers/cxl/mem.c            |  95 +++++++++++++---- drivers/cxl/pci.c            |   2 +- tools/testing/cxl/test/mem.c |   2 +-

   10 files changed, 297 insertions(+), 63 deletions(-)

base-commit: 5d6919055dec134de3c40167a490f33c74c12581

maintainer
#22 lore raw
In reply to Dan Williams
Quoted 49 lines · Dan Williams

The devm_cxl_probe_mem() helper creates a memdev and retrieves a mapped HPA range for the device. At present the retrieved region information is expected to be established by platform firmware. A future update will auto-establish a region if one is not already present. This arranges for a simple CXL topology detach == Accelerator driver detach scheme.

Now, automatic region establishment on behalf of an accelerator driver also requires automatic region teardown. The existing scheme of triggering devm_release_action() of a cxl root device to destroy a region is racy [1]. There is no path to coordinate multiple devres actions, or conflicting devres actions, without using the devm_remove_action_nowarn() workaround.

Instead, move final region cleanup responsibility to the root decoder teardown path (devres action cxld_unregister()). Expand the root decoder @range_lock, renamed @regions_lock, to coordinate region creation, sysfs manageability, and deletion. The @regions_lock also prevents regions from disappearing mid-auto-assembly.

Changes from the initial RFC [2]:
* tested with Dave's type2 cxl_test
* fix region remove by endpoints (Dave, Alejandro)
* fix user triggered region remove (Sungwoo)
* fix lifetime of the @attach ops (Alison, sashiko)
* hide @attach details inside the CXL core

[1]: http://lore.kernel.org/[email protected]
[2]: http://lore.kernel.org/[email protected]

Dan Williams (5):
cxl/region: Block region delete during region creation cxl/region: Resolve region deletion races cxl/memdev: Pin parents for entire memdev lifetime cxl/memdev: Introduce cxl_class_memdev_type cxl/region: Introduce devm_cxl_probe_mem()

 drivers/cxl/core/core.h      |   2 +
 drivers/cxl/cxl.h            |   8 +-
 drivers/cxl/cxlmem.h         |  27 +++--
 include/cxl/cxl.h            |   3 +
 drivers/cxl/core/memdev.c    |  20 +++-
 drivers/cxl/core/port.c      |   7 +-
 drivers/cxl/core/region.c    | 194 ++++++++++++++++++++++++++++++-----
 drivers/cxl/mem.c            |  95 +++++++++++++----
 drivers/cxl/pci.c            |   2 +-
 tools/testing/cxl/test/mem.c |   2 +-
 10 files changed, 297 insertions(+), 63 deletions(-)
Quoted 1 line

base-commit: 5d6919055dec134de3c40167a490f33c74c12581

Applied to cxl/next
d8dcb0b74b04 cxl/region: Introduce devm_cxl_probe_mem() 2ed519c21bb4 cxl/memdev: Introduce cxl_class_memdev_type bd3a6ff4b84e cxl/memdev: Pin parents for entire memdev lifetime 4dd86ca99ffc cxl/region: Resolve region deletion races d91feb88692e cxl/region: Block region delete during region creation