linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v5 1/3] kernel/resource: Allow region_intersects users to hold resource_lock
@ 2021-04-19  7:01 Alistair Popple
  2021-04-19  7:01 ` [PATCH v5 2/3] kernel/resource: Refactor __request_region to allow external locking Alistair Popple
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Alistair Popple @ 2021-04-19  7:01 UTC (permalink / raw)
  To: akpm
  Cc: linux-mm, linux-kernel, david, daniel.vetter, dan.j.williams,
	gregkh, jhubbard, jglisse, bsingharora, smuchun, 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().

Signed-off-by: Alistair Popple <apopple@nvidia.com>
---
 kernel/resource.c | 52 ++++++++++++++++++++++++++++-------------------
 1 file changed, 31 insertions(+), 21 deletions(-)

diff --git a/kernel/resource.c b/kernel/resource.c
index 627e61b0c124..736768587d2d 100644
--- a/kernel/resource.c
+++ b/kernel/resource.c
@@ -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);
 
-- 
2.20.1



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v5 2/3] kernel/resource: Refactor __request_region to allow external locking
  2021-04-19  7:01 [PATCH v5 1/3] kernel/resource: Allow region_intersects users to hold resource_lock Alistair Popple
@ 2021-04-19  7:01 ` Alistair Popple
  2021-04-20 14:43   ` David Hildenbrand
  2021-04-19  7:01 ` [PATCH v5 3/3] kernel/resource: Fix locking in request_free_mem_region Alistair Popple
  2021-04-19 14:18 ` [PATCH v5 1/3] kernel/resource: Allow region_intersects users to hold resource_lock David Hildenbrand
  2 siblings, 1 reply; 6+ messages in thread
From: Alistair Popple @ 2021-04-19  7:01 UTC (permalink / raw)
  To: akpm
  Cc: linux-mm, linux-kernel, david, daniel.vetter, dan.j.williams,
	gregkh, jhubbard, jglisse, bsingharora, smuchun, 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.

Signed-off-by: Alistair Popple <apopple@nvidia.com>
---
 kernel/resource.c | 52 +++++++++++++++++++++++++++++------------------
 1 file changed, 32 insertions(+), 20 deletions(-)

diff --git a/kernel/resource.c b/kernel/resource.c
index 736768587d2d..75f8da722497 100644
--- a/kernel/resource.c
+++ b/kernel/resource.c
@@ -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;
-- 
2.20.1



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v5 3/3] kernel/resource: Fix locking in request_free_mem_region
  2021-04-19  7:01 [PATCH v5 1/3] kernel/resource: Allow region_intersects users to hold resource_lock Alistair Popple
  2021-04-19  7:01 ` [PATCH v5 2/3] kernel/resource: Refactor __request_region to allow external locking Alistair Popple
@ 2021-04-19  7:01 ` Alistair Popple
  2021-04-20 14:48   ` David Hildenbrand
  2021-04-19 14:18 ` [PATCH v5 1/3] kernel/resource: Allow region_intersects users to hold resource_lock David Hildenbrand
  2 siblings, 1 reply; 6+ messages in thread
From: Alistair Popple @ 2021-04-19  7:01 UTC (permalink / raw)
  To: akpm
  Cc: linux-mm, linux-kernel, david, daniel.vetter, dan.j.williams,
	gregkh, jhubbard, jglisse, bsingharora, smuchun, 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.

Signed-off-by: Alistair Popple <apopple@nvidia.com>
---
 kernel/resource.c | 45 ++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 38 insertions(+), 7 deletions(-)

diff --git a/kernel/resource.c b/kernel/resource.c
index 75f8da722497..e8468e867495 100644
--- a/kernel/resource.c
+++ b/kernel/resource.c
@@ -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);
 }
-- 
2.20.1



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v5 1/3] kernel/resource: Allow region_intersects users to hold resource_lock
  2021-04-19  7:01 [PATCH v5 1/3] kernel/resource: Allow region_intersects users to hold resource_lock Alistair Popple
  2021-04-19  7:01 ` [PATCH v5 2/3] kernel/resource: Refactor __request_region to allow external locking Alistair Popple
  2021-04-19  7:01 ` [PATCH v5 3/3] kernel/resource: Fix locking in request_free_mem_region Alistair Popple
@ 2021-04-19 14:18 ` David Hildenbrand
  2 siblings, 0 replies; 6+ messages in thread
From: David Hildenbrand @ 2021-04-19 14:18 UTC (permalink / raw)
  To: Alistair Popple, akpm
  Cc: linux-mm, linux-kernel, daniel.vetter, dan.j.williams, gregkh,
	jhubbard, jglisse, bsingharora, smuchun

On 19.04.21 09:01, Alistair Popple wrote:
> 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().
> 
> Signed-off-by: Alistair Popple <apopple@nvidia.com>
> ---
>   kernel/resource.c | 52 ++++++++++++++++++++++++++++-------------------
>   1 file changed, 31 insertions(+), 21 deletions(-)
> 
> diff --git a/kernel/resource.c b/kernel/resource.c
> index 627e61b0c124..736768587d2d 100644
> --- a/kernel/resource.c
> +++ b/kernel/resource.c
> @@ -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;

Reviewed-by: David Hildenbrand <david@redhat.com>


-- 
Thanks,

David / dhildenb



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v5 2/3] kernel/resource: Refactor __request_region to allow external locking
  2021-04-19  7:01 ` [PATCH v5 2/3] kernel/resource: Refactor __request_region to allow external locking Alistair Popple
@ 2021-04-20 14:43   ` David Hildenbrand
  0 siblings, 0 replies; 6+ messages in thread
From: David Hildenbrand @ 2021-04-20 14:43 UTC (permalink / raw)
  To: Alistair Popple, akpm
  Cc: linux-mm, linux-kernel, daniel.vetter, dan.j.williams, gregkh,
	jhubbard, jglisse, bsingharora, smuchun

On 19.04.21 09:01, Alistair Popple wrote:
> Refactor the portion of __request_region() done whilst holding the
> resource_lock into a separate function to allow callers to hold the
> lock.
> 
> Signed-off-by: Alistair Popple <apopple@nvidia.com>
> ---
>   kernel/resource.c | 52 +++++++++++++++++++++++++++++------------------
>   1 file changed, 32 insertions(+), 20 deletions(-)
> 
> diff --git a/kernel/resource.c b/kernel/resource.c
> index 736768587d2d..75f8da722497 100644
> --- a/kernel/resource.c
> +++ b/kernel/resource.c
> @@ -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);

Reviewed-by: David Hildenbrand <david@redhat.com>

-- 
Thanks,

David / dhildenb



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v5 3/3] kernel/resource: Fix locking in request_free_mem_region
  2021-04-19  7:01 ` [PATCH v5 3/3] kernel/resource: Fix locking in request_free_mem_region Alistair Popple
@ 2021-04-20 14:48   ` David Hildenbrand
  0 siblings, 0 replies; 6+ messages in thread
From: David Hildenbrand @ 2021-04-20 14:48 UTC (permalink / raw)
  To: Alistair Popple, akpm
  Cc: linux-mm, linux-kernel, daniel.vetter, dan.j.williams, gregkh,
	jhubbard, jglisse, bsingharora, smuchun

On 19.04.21 09:01, Alistair Popple wrote:
> 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.
> 
> Signed-off-by: Alistair Popple <apopple@nvidia.com>
> ---
>   kernel/resource.c | 45 ++++++++++++++++++++++++++++++++++++++-------
>   1 file changed, 38 insertions(+), 7 deletions(-)
> 
> diff --git a/kernel/resource.c b/kernel/resource.c
> index 75f8da722497..e8468e867495 100644
> --- a/kernel/resource.c
> +++ b/kernel/resource.c
> @@ -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);
>   }
> 

LGTM

Reviewed-by: David Hildenbrand <david@redhat.com>

-- 
Thanks,

David / dhildenb



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-04-20 14:48 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-19  7:01 [PATCH v5 1/3] kernel/resource: Allow region_intersects users to hold resource_lock Alistair Popple
2021-04-19  7:01 ` [PATCH v5 2/3] kernel/resource: Refactor __request_region to allow external locking Alistair Popple
2021-04-20 14:43   ` David Hildenbrand
2021-04-19  7:01 ` [PATCH v5 3/3] kernel/resource: Fix locking in request_free_mem_region Alistair Popple
2021-04-20 14:48   ` David Hildenbrand
2021-04-19 14:18 ` [PATCH v5 1/3] kernel/resource: Allow region_intersects users to hold resource_lock David Hildenbrand

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).