Skip to contentSkip to conversation

kernel/resource: Allow region_intersects users to hold resource_lock

PATCH Applied Alistair Popple 2 participants 6 messages Apr 19, 2021 – Apr 20, 2021 Memory management

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/3 kernel/resource: Allow region_intersects users to hold resource_lock

Introduce a version of region_intersects() that can be called with the resource_lock already held. This is used in a future fix to __request_free_mem_region().

---
 kernel/resource.c | 52 ++++++++++++++++++++++++++++-------------------
 1 file changed, 31 insertions(+), 21 deletions(-)
kernel/resource.c +31 -21
index 627e61b0c124..736768587d2d 100644
@@ -523,6 +523,34 @@ int __weak page_is_ram(unsigned long pfn)
 }
 EXPORT_SYMBOL_GPL(page_is_ram);
 
+int __region_intersects(resource_size_t start, size_t size, unsigned long flags,
+			unsigned long desc)
+{
+	struct resource res;
+	int type = 0; int other = 0;
+	struct resource *p;
+
+	res.start = start;
+	res.end = start + size - 1;
+
+	for (p = iomem_resource.child; p ; p = p->sibling) {
+		bool is_type = (((p->flags & flags) == flags) &&
+				((desc == IORES_DESC_NONE) ||
+				 (desc == p->desc)));
+
+		if (resource_overlaps(p, &res))
+			is_type ? type++ : other++;
+	}
+
+	if (type == 0)
+		return REGION_DISJOINT;
+
+	if (other == 0)
+		return REGION_INTERSECTS;
+
+	return REGION_MIXED;
+}
+
 /**
  * region_intersects() - determine intersection of region with known resources
  * @start: region start address
@@ -546,31 +574,13 @@ EXPORT_SYMBOL_GPL(page_is_ram);
 int region_intersects(resource_size_t start, size_t size, unsigned long flags,
 		      unsigned long desc)
 {
-	struct resource res;
-	int type = 0; int other = 0;
-	struct resource *p;
-
-	res.start = start;
-	res.end = start + size - 1;
+	int ret;
 
 	read_lock(&resource_lock);
-	for (p = iomem_resource.child; p ; p = p->sibling) {
-		bool is_type = (((p->flags & flags) == flags) &&
-				((desc == IORES_DESC_NONE) ||
-				 (desc == p->desc)));
-
-		if (resource_overlaps(p, &res))
-			is_type ? type++ : other++;
-	}
+	ret = __region_intersects(start, size, flags, desc);
 	read_unlock(&resource_lock);
 
-	if (type == 0)
-		return REGION_DISJOINT;
-
-	if (other == 0)
-		return REGION_INTERSECTS;
-
-	return REGION_MIXED;
+	return ret;
 }
 EXPORT_SYMBOL_GPL(region_intersects);
 
Signature · 1 line
2.20.1
#1 lore raw

PATCH v5 2/3 kernel/resource: Refactor __request_region to allow external locking

Refactor the portion of __request_region() done whilst holding the resource_lock into a separate function to allow callers to hold the lock.

---
 kernel/resource.c | 52 +++++++++++++++++++++++++++++------------------
 1 file changed, 32 insertions(+), 20 deletions(-)
kernel/resource.c +32 -20
index 736768587d2d..75f8da722497 100644
@@ -1181,31 +1181,16 @@ struct address_space *iomem_get_mapping(void)
 	return smp_load_acquire(&iomem_inode)->i_mapping;
 }
 
-/**
- * __request_region - create a new busy resource region
- * @parent: parent resource descriptor
- * @start: resource start address
- * @n: resource region size
- * @name: reserving caller's ID string
- * @flags: IO resource flags
- */
-struct resource * __request_region(struct resource *parent,
+static int __request_region_locked(struct resource *res, struct resource *parent,
 				   resource_size_t start, resource_size_t n,
 				   const char *name, int flags)
 {
 	DECLARE_WAITQUEUE(wait, current);
-	struct resource *res = alloc_resource(GFP_KERNEL);
-	struct resource *orig_parent = parent;
-
-	if (!res)
-		return NULL;
 
 	res->name = name;
 	res->start = start;
 	res->end = start + n - 1;
 
-	write_lock(&resource_lock);
-
 	for (;;) {
 		struct resource *conflict;
 
@@ -1241,13 +1226,40 @@ struct resource * __request_region(struct resource *parent,
 			continue;
 		}
 		/* Uhhuh, that didn't work out.. */
-		free_resource(res);
-		res = NULL;
-		break;
+		return -EBUSY;
 	}
+
+	return 0;
+}
+
+/**
+ * __request_region - create a new busy resource region
+ * @parent: parent resource descriptor
+ * @start: resource start address
+ * @n: resource region size
+ * @name: reserving caller's ID string
+ * @flags: IO resource flags
+ */
+struct resource *__request_region(struct resource *parent,
+				  resource_size_t start, resource_size_t n,
+				  const char *name, int flags)
+{
+	struct resource *res = alloc_resource(GFP_KERNEL);
+	int ret;
+
+	if (!res)
+		return NULL;
+
+	write_lock(&resource_lock);
+	ret = __request_region_locked(res, parent, start, n, name, flags);
 	write_unlock(&resource_lock);
 
-	if (res && orig_parent == &iomem_resource)
+	if (ret) {
+		free_resource(res);
+		return NULL;
+	}
+
+	if (parent == &iomem_resource)
 		revoke_iomem(res);
 
 	return res;
Signature · 1 line
2.20.1
Reviewed-by on v5 2/3
#2 lore raw

Re: [PATCH v5 2/3] kernel/resource: Refactor __request_region to allow external locking

Quoted 79 lines · Alistair Popple

Refactor the portion of __request_region() done whilst holding the resource_lock into a separate function to allow callers to hold the lock.

---
  kernel/resource.c | 52 +++++++++++++++++++++++++++++------------------
  1 file changed, 32 insertions(+), 20 deletions(-)
kernel/resource.c +26 -19
index 736768587d2d..75f8da722497 100644
@@ -1181,31 +1181,16 @@ struct address_space *iomem_get_mapping(void)
  	return smp_load_acquire(&iomem_inode)->i_mapping;
  }
 
-/**
- * __request_region - create a new busy resource region
- * @parent: parent resource descriptor
- * @start: resource start address
- * @n: resource region size
- * @name: reserving caller's ID string
- * @flags: IO resource flags
- */
-struct resource * __request_region(struct resource *parent,
+static int __request_region_locked(struct resource *res, struct resource *parent,
  				   resource_size_t start, resource_size_t n,
  				   const char *name, int flags)
  {
  	DECLARE_WAITQUEUE(wait, current);
-	struct resource *res = alloc_resource(GFP_KERNEL);
-	struct resource *orig_parent = parent;
-
-	if (!res)
-		return NULL;
 
  	res->name = name;
  	res->start = start;
  	res->end = start + n - 1;
 
-	write_lock(&resource_lock);
-
  	for (;;) {
  		struct resource *conflict;
 
@@ -1241,13 +1226,40 @@ struct resource * __request_region(struct resource *parent,
  			continue;
  		}
  		/* Uhhuh, that didn't work out.. */
-		free_resource(res);
-		res = NULL;
-		break;
+		return -EBUSY;
  	}
+
+	return 0;
+}
+
+/**
+ * __request_region - create a new busy resource region
+ * @parent: parent resource descriptor
+ * @start: resource start address
+ * @n: resource region size
+ * @name: reserving caller's ID string
+ * @flags: IO resource flags
+ */
+struct resource *__request_region(struct resource *parent,
+				  resource_size_t start, resource_size_t n,
+				  const char *name, int flags)
+{
+	struct resource *res = alloc_resource(GFP_KERNEL);
+	int ret;
+
+	if (!res)
+		return NULL;
+
+	write_lock(&resource_lock);
+	ret = __request_region_locked(res, parent, start, n, name, flags);

kernel/resource.c struct resource * __request_region(struct resource *parent, · line 1263

Quoted 10 lines
kernel/resource.c +6 -1
  	write_unlock(&resource_lock);
 
-	if (res && orig_parent == &iomem_resource)
+	if (ret) {
+		free_resource(res);
+		return NULL;
+	}
+
+	if (parent == &iomem_resource)
  		revoke_iomem(res);
Signature · 3 lines
Thanks,

David / dhildenb
#3 lore raw

PATCH v5 3/3 kernel/resource: Fix locking in request_free_mem_region

In reply to Alistair Popple

request_free_mem_region() is used to find an empty range of physical addresses for hotplugging ZONE_DEVICE memory. It does this by iterating over the range of possible addresses using region_intersects() to see if the range is free before calling request_mem_region() to allocate the region.

However the resource_lock is dropped between these two calls meaning by the time request_mem_region() is called in request_free_mem_region() another thread may have already reserved the requested region. This results in unexpected failures and a message in the kernel log from hitting this condition:

/*
* mm/hmm.c reserves physical addresses which then
* become unavailable to other users. Conflicts are
* not expected. Warn to aid debugging if encountered. */
if (conflict->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY) {
pr_warn("Unaddressable device %s %pR conflicts with %pR",
conflict->name, conflict, res);

These unexpected failures can be corrected by holding resource_lock across the two calls. This also requires memory allocation to be performed prior to taking the lock.

---
 kernel/resource.c | 45 ++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 38 insertions(+), 7 deletions(-)
kernel/resource.c +38 -7
index 75f8da722497..e8468e867495 100644
@@ -1801,25 +1801,56 @@ static struct resource *__request_free_mem_region(struct device *dev,
 {
 	resource_size_t end, addr;
 	struct resource *res;
+	struct region_devres *dr = NULL;
 
 	size = ALIGN(size, 1UL << PA_SECTION_SHIFT);
 	end = min_t(unsigned long, base->end, (1UL << MAX_PHYSMEM_BITS) - 1);
 	addr = end - size + 1UL;
 
+	res = alloc_resource(GFP_KERNEL);
+	if (!res)
+		return ERR_PTR(-ENOMEM);
+
+	if (dev) {
+		dr = devres_alloc(devm_region_release,
+				sizeof(struct region_devres), GFP_KERNEL);
+		if (!dr) {
+			free_resource(res);
+			return ERR_PTR(-ENOMEM);
+		}
+	}
+
+	write_lock(&resource_lock);
 	for (; addr > size && addr >= base->start; addr -= size) {
-		if (region_intersects(addr, size, 0, IORES_DESC_NONE) !=
+		if (__region_intersects(addr, size, 0, IORES_DESC_NONE) !=
 				REGION_DISJOINT)
 			continue;
 
-		if (dev)
-			res = devm_request_mem_region(dev, addr, size, name);
-		else
-			res = request_mem_region(addr, size, name);
-		if (!res)
-			return ERR_PTR(-ENOMEM);
+		if (!__request_region_locked(res, &iomem_resource, addr, size,
+						name, 0))
+			break;
+
+		if (dev) {
+			dr->parent = &iomem_resource;
+			dr->start = addr;
+			dr->n = size;
+			devres_add(dev, dr);
+		}
+
 		res->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY;
+		write_unlock(&resource_lock);
+
+		/*
+		 * A driver is claiming this region so revoke any mappings.
+		 */
+		revoke_iomem(res);
 		return res;
 	}
+	write_unlock(&resource_lock);
+
+	free_resource(res);
+	if (dr)
+		devres_free(dr);
 
 	return ERR_PTR(-ERANGE);
 }
Signature · 1 line
2.20.1
Reviewed-by on v5 3/3
#4 lore raw

Re: [PATCH v5 3/3] kernel/resource: Fix locking in request_free_mem_region

Quoted 89 lines · Alistair Popple

request_free_mem_region() is used to find an empty range of physical addresses for hotplugging ZONE_DEVICE memory. It does this by iterating over the range of possible addresses using region_intersects() to see if the range is free before calling request_mem_region() to allocate the region.

However the resource_lock is dropped between these two calls meaning by the time request_mem_region() is called in request_free_mem_region() another thread may have already reserved the requested region. This results in unexpected failures and a message in the kernel log from hitting this condition:

/*
* mm/hmm.c reserves physical addresses which then
* become unavailable to other users. Conflicts are
* not expected. Warn to aid debugging if encountered. */
if (conflict->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY) {
pr_warn("Unaddressable device %s %pR conflicts with %pR",
conflict->name, conflict, res);

These unexpected failures can be corrected by holding resource_lock across the two calls. This also requires memory allocation to be performed prior to taking the lock.

---
  kernel/resource.c | 45 ++++++++++++++++++++++++++++++++++++++-------
  1 file changed, 38 insertions(+), 7 deletions(-)
kernel/resource.c +33 -7
index 75f8da722497..e8468e867495 100644
@@ -1801,25 +1801,56 @@ static struct resource *__request_free_mem_region(struct device *dev,
  {
  	resource_size_t end, addr;
  	struct resource *res;
+	struct region_devres *dr = NULL;
 
  	size = ALIGN(size, 1UL << PA_SECTION_SHIFT);
  	end = min_t(unsigned long, base->end, (1UL << MAX_PHYSMEM_BITS) - 1);
  	addr = end - size + 1UL;
 
+	res = alloc_resource(GFP_KERNEL);
+	if (!res)
+		return ERR_PTR(-ENOMEM);
+
+	if (dev) {
+		dr = devres_alloc(devm_region_release,
+				sizeof(struct region_devres), GFP_KERNEL);
+		if (!dr) {
+			free_resource(res);
+			return ERR_PTR(-ENOMEM);
+		}
+	}
+
+	write_lock(&resource_lock);
  	for (; addr > size && addr >= base->start; addr -= size) {
-		if (region_intersects(addr, size, 0, IORES_DESC_NONE) !=
+		if (__region_intersects(addr, size, 0, IORES_DESC_NONE) !=
  				REGION_DISJOINT)
  			continue;
 
-		if (dev)
-			res = devm_request_mem_region(dev, addr, size, name);
-		else
-			res = request_mem_region(addr, size, name);
-		if (!res)
-			return ERR_PTR(-ENOMEM);
+		if (!__request_region_locked(res, &iomem_resource, addr, size,
+						name, 0))
+			break;
+
+		if (dev) {
+			dr->parent = &iomem_resource;
+			dr->start = addr;
+			dr->n = size;
+			devres_add(dev, dr);
+		}
+
  		res->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY;
+		write_unlock(&resource_lock);
+
+		/*
+		 * A driver is claiming this region so revoke any mappings.
+		 */
+		revoke_iomem(res);

kernel/resource.c static struct resource *__request_free_mem_region(struct device *dev, · line 1856

Quoted 10 lines
kernel/resource.c +5 -0
  		return res;
  	}
+	write_unlock(&resource_lock);
+
+	free_resource(res);
+	if (dr)
+		devres_free(dr);
 
  	return ERR_PTR(-ERANGE);
  }

LGTM

Signature · 3 lines
Thanks,

David / dhildenb
Reviewed-by on v5 1/3
#5 lore raw
In reply to Alistair Popple
Quoted 14 lines · Alistair Popple

Introduce a version of region_intersects() that can be called with the resource_lock already held. This is used in a future fix to __request_free_mem_region().

---
  kernel/resource.c | 52 ++++++++++++++++++++++++++++-------------------
  1 file changed, 31 insertions(+), 21 deletions(-)
kernel/resource.c +0 -0
index 627e61b0c124..736768587d2d 100644

kernel/resource.c int __weak page_is_ram(unsigned long pfn) · line 529

Quoted 7 lines
kernel/resource.c +4 -0
@@ -523,6 +523,34 @@ int __weak page_is_ram(unsigned long pfn)
  }
  EXPORT_SYMBOL_GPL(page_is_ram);
 
+int __region_intersects(resource_size_t start, size_t size, unsigned long flags,
+			unsigned long desc)
+{
+	struct resource res;

I'd do

struct resource res, *p;

Signature · 3 lines
Thanks,

David / dhildenb