Skip to contentSkip to conversation

cxl/region: Remove soft reserve resource at region construction

PATCH In review Nathan Fontenot 3 participants 6 messages Jun 12, 2024 – Jun 13, 2024 CXL

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cxl/region: Remove soft reserve resource at region construction

Systems with a BIOS that creates soft reserve iomem resources for memory ranges covered by CXL devices, the soft reserve resource needs to be removed when hotplugging a CXL device. Without doing so a new soft reserve for the memory range cannot be created during hotplug add.

The previous approach from Alison [1] was to fix up the soft reserve resource at region release so it longer covers the memory range for the region being removed.

Instead of waiting for region removal, remove the soft reserve resource when constructing a region. This is done by walking the children of the region's parent's resources, finding a soft reserve resource (if it exists) containing the memory range for the region and removing the soft reserve prior to inserting the resource for the region being constructed.

[1] https://lore.kernel.org/linux-cxl/[email protected]/

---
 drivers/cxl/core/region.c | 30 +++++++++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)
drivers/cxl/core/region.c +29 -1
index 00a9f0eef8dd..90e5c84230a5 100644
@@ -3096,6 +3096,34 @@ static int match_region_by_range(struct device *dev, void *data)
 	return rc;
 }
 
+static int cxl_insert_resource(struct device *dev, struct resource *parent,
+			       struct resource *res)
+{
+	struct resource *soft;
+	int rc;
+
+	/*
+	 * Find and attempt to remove the SOFT_RESERVED resource
+	 * (if it exists) that includes the range of the new
+	 * resource we're inserting.
+	 */
+	for (soft = parent->child; soft; soft = soft->sibling) {
+		if (soft->desc != IORES_DESC_SOFT_RESERVED)
+			continue;
+
+		if (soft->start <= res->start && soft->end >= res->end) {
+			rc = remove_resource(soft);
+			if (rc)
+				dev_warn(dev,
+					 "Couldn't remove %s resource %llx - %llx\n",
+					 soft->name, soft->start, soft->end);
+			break;
+		}
+	}
+
+	return insert_resource(parent, res);
+}
+
 /* Establish an empty region covering the given HPA range */
 static struct cxl_region *construct_region(struct cxl_root_decoder *cxlrd,
 					   struct cxl_endpoint_decoder *cxled)
@@ -3142,7 +3170,7 @@ static struct cxl_region *construct_region(struct cxl_root_decoder *cxlrd,
 
 	*res = DEFINE_RES_MEM_NAMED(hpa->start, range_len(hpa),
 				    dev_name(&cxlr->dev));
-	rc = insert_resource(cxlrd->res, res);
+	rc = cxl_insert_resource(cxlmd->dev.parent, cxlrd->res, res);
 	if (rc) {
 		/*
 		 * Platform-firmware may not have split resources like "System
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 5 lines · Nathan Fontenot

Systems with a BIOS that creates soft reserve iomem resources for memory ranges covered by CXL devices, the soft reserve resource needs to be removed when hotplugging a CXL device. Without doing so a new soft reserve for the memory range cannot be created during hotplug add.

Is a new soft reserved ever created? I think the reason is to free up the memory for the new region creation, period. If the SR exists, userspace cannot 'replace' the region.

Quoted 11 lines

The previous approach from Alison [1] was to fix up the soft reserve resource at region release so it longer covers the memory range for the region being removed.

Instead of waiting for region removal, remove the soft reserve resource when constructing a region. This is done by walking the children of the region's parent's resources, finding a soft reserve resource (if it exists) containing the memory range for the region and removing the soft reserve prior to inserting the resource for the region being constructed.

Is it OK to remove the entire Soft Reserved when that first region is constructed? ie. in this setup that removes the soft reserved before region1 is constructed.

100-600 : CXL Window 0
100-600 : Soft Reserved

    100-200 : region0
    200-300 : region1

one bit below...

Quoted 26 lines

[1] https://lore.kernel.org/linux-cxl/[email protected]/

---
 drivers/cxl/core/region.c | 30 +++++++++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)
drivers/cxl/core/region.c +10 -0
index 00a9f0eef8dd..90e5c84230a5 100644
@@ -3096,6 +3096,34 @@ static int match_region_by_range(struct device *dev, void *data)
 	return rc;
 }
 
+static int cxl_insert_resource(struct device *dev, struct resource *parent,
+			       struct resource *res)
+{
+	struct resource *soft;
+	int rc;
+
+	/*
+	 * Find and attempt to remove the SOFT_RESERVED resource
+	 * (if it exists) that includes the range of the new
+	 * resource we're inserting.

drivers/cxl/core/region.c static int match_region_by_range(struct device *dev, void *data) · line 3118

Quoted 10 lines
drivers/cxl/core/region.c +10 -0
+	 */
+	for (soft = parent->child; soft; soft = soft->sibling) {
+		if (soft->desc != IORES_DESC_SOFT_RESERVED)
+			continue;
+
+		if (soft->start <= res->start && soft->end >= res->end) {
+			rc = remove_resource(soft);
+			if (rc)
+				dev_warn(dev,
+					 "Couldn't remove %s resource %llx - %llx\n",

Use %pr format for struct resource.

Quoted 23 lines
drivers/cxl/core/region.c +9 -1
+					 soft->name, soft->start, soft->end);
+			break;
+		}
+	}
+
+	return insert_resource(parent, res);
+}
+
 /* Establish an empty region covering the given HPA range */
 static struct cxl_region *construct_region(struct cxl_root_decoder *cxlrd,
 					   struct cxl_endpoint_decoder *cxled)
@@ -3142,7 +3170,7 @@ static struct cxl_region *construct_region(struct cxl_root_decoder *cxlrd,
 
 	*res = DEFINE_RES_MEM_NAMED(hpa->start, range_len(hpa),
 				    dev_name(&cxlr->dev));
-	rc = insert_resource(cxlrd->res, res);
+	rc = cxl_insert_resource(cxlmd->dev.parent, cxlrd->res, res);
 	if (rc) {
 		/*
 		 * Platform-firmware may not have split resources like "System
Signature · 1 line
2.34.1
#2 lore raw
Quoted 10 lines · Alison Schofield
Quoted 5 lines · Nathan Fontenot

Systems with a BIOS that creates soft reserve iomem resources for memory ranges covered by CXL devices, the soft reserve resource needs to be removed when hotplugging a CXL device. Without doing so a new soft reserve for the memory range cannot be created during hotplug add.

Is a new soft reserved ever created? I think the reason is to free up the memory for the new region creation, period. If the SR exists, userspace cannot 'replace' the region.

You're correct. I had started from an bad assumption, there is no new soft reserve created. If I now understand correctly the issue is in alloc_hpa() when a user tries to set the size for the region. The call to alloc_free_mem_region() ensures the address space is not claimed, which in this case it is by the soft reserve.

Quoted 13 lines
Quoted 6 lines

The previous approach from Alison [1] was to fix up the soft reserve resource at region release so it longer covers the memory range for the region being removed.

Quoted 6 lines

Instead of waiting for region removal, remove the soft reserve resource when constructing a region. This is done by walking the children of the region's parent's resources, finding a soft reserve resource (if it exists) containing the memory range for the region and removing the soft reserve prior to inserting the resource for the region being constructed.

Quoted 8 lines

Is it OK to remove the entire Soft Reserved when that first region is constructed? ie. in this setup that removes the soft reserved before region1 is constructed.

100-600 : CXL Window 0
100-600 : Soft Reserved

    100-200 : region0
    200-300 : region1

My approach was thinking that it is ok to go ahead and remove the entire soft reserve. I couldn't find a reason we needed to keep the soft reserve after this point. I could be wrong on this.

One different approach I've been looking at is if it's possible to add the CFMWS resource as a child of the soft reserve. Instead of starting with

100 - 600 " CXL Window 0
100 - 600 : Soft Reserved

We would have

100 - 600 : Soft Reserved
100 - 600 : CXL Window 0

My thinking is that with the soft reserve above the CXL Window we no longer need to manipulate the soft reserve range on hotplug remove. The alloc_hpa() call for user set up uses the CXL Window as the base for alloc_free_mem_region() and the soft reserve wouldn't come into play.

I haven't looked at acpi:add_cxl_resources() enough to know if this is something that actually can be done safely.

Quoted 35 lines

one bit below...

Quoted 30 lines

[1] https://lore.kernel.org/linux-cxl/[email protected]/

---
 drivers/cxl/core/region.c | 30 +++++++++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)
drivers/cxl/core/region.c +14 -0
index 00a9f0eef8dd..90e5c84230a5 100644
@@ -3096,6 +3096,34 @@ static int match_region_by_range(struct device *dev, void *data)
 	return rc;
 }
 
+static int cxl_insert_resource(struct device *dev, struct resource *parent,
+			       struct resource *res)
+{
+	struct resource *soft;
+	int rc;
+
+	/*
+	 * Find and attempt to remove the SOFT_RESERVED resource
+	 * (if it exists) that includes the range of the new
+	 * resource we're inserting.
+	 */
+	for (soft = parent->child; soft; soft = soft->sibling) {
+		if (soft->desc != IORES_DESC_SOFT_RESERVED)
+			continue;
Quoted 8 lines

drivers/cxl/core/region.c static int match_region_by_range(struct device *dev, void *data) · line 3118

Quoted 6 lines
drivers/cxl/core/region.c +6 -0
+
+		if (soft->start <= res->start && soft->end >= res->end) {
+			rc = remove_resource(soft);
+			if (rc)
+				dev_warn(dev,
+					 "Couldn't remove %s resource %llx - %llx\n",

Use %pr format for struct resource.

good catch, I'll update this locally but Dan's response let me know the resource walk is not something I should be doing.

-Nathan

Quoted 24 lines
Quoted 24 lines
drivers/cxl/core/region.c +9 -1
+					 soft->name, soft->start, soft->end);
+			break;
+		}
+	}
+
+	return insert_resource(parent, res);
+}
+
 /* Establish an empty region covering the given HPA range */
 static struct cxl_region *construct_region(struct cxl_root_decoder *cxlrd,
 					   struct cxl_endpoint_decoder *cxled)
@@ -3142,7 +3170,7 @@ static struct cxl_region *construct_region(struct cxl_root_decoder *cxlrd,
 
 	*res = DEFINE_RES_MEM_NAMED(hpa->start, range_len(hpa),
 				    dev_name(&cxlr->dev));
-	rc = insert_resource(cxlrd->res, res);
+	rc = cxl_insert_resource(cxlmd->dev.parent, cxlrd->res, res);
 	if (rc) {
 		/*
 		 * Platform-firmware may not have split resources like "System
Signature · 1 line
2.34.1
#3 lore raw

Nathan Fontenot wrote:
[..]

Quoted 25 lines
Quoted 9 lines

Is it OK to remove the entire Soft Reserved when that first region is constructed? ie. in this setup that removes the soft reserved before region1 is constructed.

100-600 : CXL Window 0
100-600 : Soft Reserved

    100-200 : region0
    200-300 : region1

My approach was thinking that it is ok to go ahead and remove the entire soft reserve. I couldn't find a reason we needed to keep the soft reserve after this point. I could be wrong on this.

One different approach I've been looking at is if it's possible to add the CFMWS resource as a child of the soft reserve. Instead of starting with

100 - 600 " CXL Window 0
100 - 600 : Soft Reserved

We would have

100 - 600 : Soft Reserved
100 - 600 : CXL Window 0

Quoted 7 lines

My thinking is that with the soft reserve above the CXL Window we no longer need to manipulate the soft reserve range on hotplug remove. The alloc_hpa() call for user set up uses the CXL Window as the base for alloc_free_mem_region() and the soft reserve wouldn't come into play.

I haven't looked at acpi:add_cxl_resources() enough to know if this is something that actually can be done safely.

Could you please stop trying to invent new approaches and respond to the one on the table?

#4 lore raw
Quoted 32 lines · Dan Williams

Nathan Fontenot wrote:
[..]

Quoted 30 lines
Quoted 9 lines

Is it OK to remove the entire Soft Reserved when that first region is constructed? ie. in this setup that removes the soft reserved before region1 is constructed.

100-600 : CXL Window 0
100-600 : Soft Reserved

    100-200 : region0
    200-300 : region1

My approach was thinking that it is ok to go ahead and remove the entire soft reserve. I couldn't find a reason we needed to keep the soft reserve after this point. I could be wrong on this.

One different approach I've been looking at is if it's possible to add the CFMWS resource as a child of the soft reserve. Instead of starting with

100 - 600 " CXL Window 0
100 - 600 : Soft Reserved

We would have

100 - 600 : Soft Reserved
100 - 600 : CXL Window 0

My thinking is that with the soft reserve above the CXL Window we no longer need to manipulate the soft reserve range on hotplug remove. The alloc_hpa() call for user set up uses the CXL Window as the base for alloc_free_mem_region() and the soft reserve wouldn't come into play.

Quoted 5 lines
Quoted 2 lines

I haven't looked at acpi:add_cxl_resources() enough to know if this is something that actually can be done safely.

Could you please stop trying to invent new approaches and respond to the one on the table?

Going back through the previous conversation, I was just trying to explore different options.

-Nathan

#5 lore raw
In reply to Nathan Fontenot
Quoted 10 lines · Nathan Fontenot

Systems with a BIOS that creates soft reserve iomem resources for memory ranges covered by CXL devices, the soft reserve resource needs to be removed when hotplugging a CXL device. Without doing so a new soft reserve for the memory range cannot be created during hotplug add.

The previous approach from Alison [1] was to fix up the soft reserve resource at region release so it longer covers the memory range for the region being removed.

Quoted 8 lines

Instead of waiting for region removal, remove the soft reserve resource when constructing a region. This is done by walking the children of the region's parent's resources, finding a soft reserve resource (if it exists) containing the memory range for the region and removing the soft reserve prior to inserting the resource for the region being constructed.

[1] https://lore.kernel.org/linux-cxl/[email protected]/

Somewhat disappointing that this changelog did not address anything from this write-up:

http://lore.kernel.org/r/[email protected]

Quoted 15 lines
---
 drivers/cxl/core/region.c | 30 +++++++++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)
drivers/cxl/core/region.c +1 -0
index 00a9f0eef8dd..90e5c84230a5 100644
@@ -3096,6 +3096,34 @@ static int match_region_by_range(struct device *dev, void *data)
 	return rc;
 }
 
+static int cxl_insert_resource(struct device *dev, struct resource *parent,

drivers/cxl/core/region.c static int match_region_by_range(struct device *dev, void *data) · line 3109

Quoted 10 lines
drivers/cxl/core/region.c +10 -0
+			       struct resource *res)
+{
+	struct resource *soft;
+	int rc;
+
+	/*
+	 * Find and attempt to remove the SOFT_RESERVED resource
+	 * (if it exists) that includes the range of the new
+	 * resource we're inserting.
+	 */

I wish it were this simple:

drivers/cxl/core/region.c static int match_region_by_range(struct device *dev, void *data) · line 3110

Quoted 1 line
drivers/cxl/core/region.c +1 -0
+	for (soft = parent->child; soft; soft = soft->sibling) {

This list is protected by resource_lock, unlocked walks are racy.

You might say, "what about walk_iomem_res_desc()", but that does not allow the callback routine to manipulate the resource tree.

drivers/cxl/core/region.c static int match_region_by_range(struct device *dev, void *data) · line 3115

Quoted 5 lines
drivers/cxl/core/region.c +5 -0
+		if (soft->desc != IORES_DESC_SOFT_RESERVED)
+			continue;
+
+		if (soft->start <= res->start && soft->end >= res->end) {
+			rc = remove_resource(soft);

The concern here is what happens if CXL region assembly eventually fails? There is no fallback possible. Instead the approach I outlined in [email protected] is where I would like to see this effort go.