Skip to content

Commit 0092908

Browse files
Christoph Hellwigjgunthorpe
Christoph Hellwig
authored andcommitted
mm: factor out a devm_request_free_mem_region helper
Keep the physical address allocation that hmm_add_device does with the rest of the resource code, and allow future reuse of it without the hmm wrapper. Signed-off-by: Christoph Hellwig <[email protected]> Reviewed-by: Jason Gunthorpe <[email protected]> Reviewed-by: John Hubbard <[email protected]> Reviewed-by: Dan Williams <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]>
1 parent 6926221 commit 0092908

File tree

3 files changed

+45
-29
lines changed

3 files changed

+45
-29
lines changed

include/linux/ioport.h

+2
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,8 @@ static inline bool resource_overlaps(struct resource *r1, struct resource *r2)
285285
return (r1->start <= r2->end && r1->end >= r2->start);
286286
}
287287

288+
struct resource *devm_request_free_mem_region(struct device *dev,
289+
struct resource *base, unsigned long size);
288290

289291
#endif /* __ASSEMBLY__ */
290292
#endif /* _LINUX_IOPORT_H */

kernel/resource.c

+39
Original file line numberDiff line numberDiff line change
@@ -1628,6 +1628,45 @@ void resource_list_free(struct list_head *head)
16281628
}
16291629
EXPORT_SYMBOL(resource_list_free);
16301630

1631+
#ifdef CONFIG_DEVICE_PRIVATE
1632+
/**
1633+
* devm_request_free_mem_region - find free region for device private memory
1634+
*
1635+
* @dev: device struct to bind the resource to
1636+
* @size: size in bytes of the device memory to add
1637+
* @base: resource tree to look in
1638+
*
1639+
* This function tries to find an empty range of physical address big enough to
1640+
* contain the new resource, so that it can later be hotplugged as ZONE_DEVICE
1641+
* memory, which in turn allocates struct pages.
1642+
*/
1643+
struct resource *devm_request_free_mem_region(struct device *dev,
1644+
struct resource *base, unsigned long size)
1645+
{
1646+
resource_size_t end, addr;
1647+
struct resource *res;
1648+
1649+
size = ALIGN(size, 1UL << PA_SECTION_SHIFT);
1650+
end = min_t(unsigned long, base->end, (1UL << MAX_PHYSMEM_BITS) - 1);
1651+
addr = end - size + 1UL;
1652+
1653+
for (; addr > size && addr >= base->start; addr -= size) {
1654+
if (region_intersects(addr, size, 0, IORES_DESC_NONE) !=
1655+
REGION_DISJOINT)
1656+
continue;
1657+
1658+
res = devm_request_mem_region(dev, addr, size, dev_name(dev));
1659+
if (!res)
1660+
return ERR_PTR(-ENOMEM);
1661+
res->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY;
1662+
return res;
1663+
}
1664+
1665+
return ERR_PTR(-ERANGE);
1666+
}
1667+
EXPORT_SYMBOL_GPL(devm_request_free_mem_region);
1668+
#endif /* CONFIG_DEVICE_PRIVATE */
1669+
16311670
static int __init strict_iomem(char *str)
16321671
{
16331672
if (strstr(str, "relaxed"))

mm/hmm.c

+4-29
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
#include <linux/mmu_notifier.h>
2626
#include <linux/memory_hotplug.h>
2727

28-
#define PA_SECTION_SIZE (1UL << PA_SECTION_SHIFT)
29-
3028
#if IS_ENABLED(CONFIG_HMM_MIRROR)
3129
static const struct mmu_notifier_ops hmm_mmu_notifier_ops;
3230

@@ -1408,7 +1406,6 @@ struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops,
14081406
unsigned long size)
14091407
{
14101408
struct hmm_devmem *devmem;
1411-
resource_size_t addr;
14121409
void *result;
14131410
int ret;
14141411

@@ -1430,32 +1427,10 @@ struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops,
14301427
if (ret)
14311428
return ERR_PTR(ret);
14321429

1433-
size = ALIGN(size, PA_SECTION_SIZE);
1434-
addr = min((unsigned long)iomem_resource.end,
1435-
(1UL << MAX_PHYSMEM_BITS) - 1);
1436-
addr = addr - size + 1UL;
1437-
1438-
/*
1439-
* FIXME add a new helper to quickly walk resource tree and find free
1440-
* range
1441-
*
1442-
* FIXME what about ioport_resource resource ?
1443-
*/
1444-
for (; addr > size && addr >= iomem_resource.start; addr -= size) {
1445-
ret = region_intersects(addr, size, 0, IORES_DESC_NONE);
1446-
if (ret != REGION_DISJOINT)
1447-
continue;
1448-
1449-
devmem->resource = devm_request_mem_region(device, addr, size,
1450-
dev_name(device));
1451-
if (!devmem->resource)
1452-
return ERR_PTR(-ENOMEM);
1453-
break;
1454-
}
1455-
if (!devmem->resource)
1456-
return ERR_PTR(-ERANGE);
1457-
1458-
devmem->resource->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY;
1430+
devmem->resource = devm_request_free_mem_region(device, &iomem_resource,
1431+
size);
1432+
if (IS_ERR(devmem->resource))
1433+
return ERR_CAST(devmem->resource);
14591434
devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
14601435
devmem->pfn_last = devmem->pfn_first +
14611436
(resource_size(devmem->resource) >> PAGE_SHIFT);

0 commit comments

Comments
 (0)