mirror of
				https://github.com/torvalds/linux.git
				synced 2025-10-31 16:48:26 +02:00 
			
		
		
		
	kernel/resource: fix locking in request_free_mem_region
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.
Link: https://lkml.kernel.org/r/20210419070109.4780-3-apopple@nvidia.com
Signed-off-by: Alistair Popple <apopple@nvidia.com>
Reviewed-by: David Hildenbrand <david@redhat.com>
Cc: Balbir Singh <bsingharora@gmail.com>
Cc: Daniel Vetter <daniel.vetter@ffwll.ch>
Cc: Dan Williams <dan.j.williams@intel.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Jerome Glisse <jglisse@redhat.com>
Cc: John Hubbard <jhubbard@nvidia.com>
Cc: Muchun Song <smuchun@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
			
			
This commit is contained in:
		
							parent
							
								
									63cdafe0af
								
							
						
					
					
						commit
						56fd94919b
					
				
					 1 changed files with 38 additions and 7 deletions
				
			
		|  | @ -1780,25 +1780,56 @@ static struct resource *__request_free_mem_region(struct device *dev, | ||||||
| { | { | ||||||
| 	resource_size_t end, addr; | 	resource_size_t end, addr; | ||||||
| 	struct resource *res; | 	struct resource *res; | ||||||
|  | 	struct region_devres *dr = NULL; | ||||||
| 
 | 
 | ||||||
| 	size = ALIGN(size, 1UL << PA_SECTION_SHIFT); | 	size = ALIGN(size, 1UL << PA_SECTION_SHIFT); | ||||||
| 	end = min_t(unsigned long, base->end, (1UL << MAX_PHYSMEM_BITS) - 1); | 	end = min_t(unsigned long, base->end, (1UL << MAX_PHYSMEM_BITS) - 1); | ||||||
| 	addr = end - size + 1UL; | 	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) { | 	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) | 				REGION_DISJOINT) | ||||||
| 			continue; | 			continue; | ||||||
| 
 | 
 | ||||||
| 		if (dev) | 		if (!__request_region_locked(res, &iomem_resource, addr, size, | ||||||
| 			res = devm_request_mem_region(dev, addr, size, name); | 						name, 0)) | ||||||
| 		else | 			break; | ||||||
| 			res = request_mem_region(addr, size, name); | 
 | ||||||
| 		if (!res) | 		if (dev) { | ||||||
| 			return ERR_PTR(-ENOMEM); | 			dr->parent = &iomem_resource; | ||||||
|  | 			dr->start = addr; | ||||||
|  | 			dr->n = size; | ||||||
|  | 			devres_add(dev, dr); | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
| 		res->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY; | 		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; | 		return res; | ||||||
| 	} | 	} | ||||||
|  | 	write_unlock(&resource_lock); | ||||||
|  | 
 | ||||||
|  | 	free_resource(res); | ||||||
|  | 	if (dr) | ||||||
|  | 		devres_free(dr); | ||||||
| 
 | 
 | ||||||
| 	return ERR_PTR(-ERANGE); | 	return ERR_PTR(-ERANGE); | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue
	
	 Alistair Popple
						Alistair Popple