Skip to content

Commit 6b8f079

Browse files
stellarhopperakpm00
authored andcommitted
mm/memory_hotplug: split memmap_on_memory requests across memblocks
The MHP_MEMMAP_ON_MEMORY flag for hotplugged memory is restricted to 'memblock_size' chunks of memory being added. Adding a larger span of memory precludes memmap_on_memory semantics. For users of hotplug such as kmem, large amounts of memory might get added from the CXL subsystem. In some cases, this amount may exceed the available 'main memory' to store the memmap for the memory being added. In this case, it is useful to have a way to place the memmap on the memory being added, even if it means splitting the addition into memblock-sized chunks. Change add_memory_resource() to loop over memblock-sized chunks of memory if caller requested memmap_on_memory, and if other conditions for it are met. Teach try_remove_memory() to also expect that a memory range being removed might have been split up into memblock sized chunks, and to loop through those as needed. This does preclude being able to use PUD mappings in the direct map; a proposal to how this could be optimized in the future is laid out here[1]. [1]: https://lore.kernel.org/linux-mm/b6753402-2de9-25b2-36e9-eacd49752b19@redhat.com/ Link: https://lkml.kernel.org/r/20231107-vv-kmem_memmap-v10-2-1253ec050ed0@intel.com Signed-off-by: Vishal Verma <vishal.l.verma@intel.com> Suggested-by: David Hildenbrand <david@redhat.com> Reviewed-by: Dan Williams <dan.j.williams@intel.com> Reviewed-by: "Huang, Ying" <ying.huang@intel.com> Acked-by: David Hildenbrand <david@redhat.com> Cc: Michal Hocko <mhocko@suse.com> Cc: Oscar Salvador <osalvador@suse.de> Cc: Dave Jiang <dave.jiang@intel.com> Cc: Dave Hansen <dave.hansen@linux.intel.com> Cc: Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com> Cc: Fan Ni <fan.ni@samsung.com> Cc: Jeff Moyer <jmoyer@redhat.com> Cc: Jonathan Cameron <Jonathan.Cameron@huawei.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 82b8a3b commit 6b8f079

File tree

1 file changed

+136
-76
lines changed

1 file changed

+136
-76
lines changed

mm/memory_hotplug.c

Lines changed: 136 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,85 @@ static bool mhp_supports_memmap_on_memory(unsigned long size)
13801380
return arch_supports_memmap_on_memory(vmemmap_size);
13811381
}
13821382

1383+
static void __ref remove_memory_blocks_and_altmaps(u64 start, u64 size)
1384+
{
1385+
unsigned long memblock_size = memory_block_size_bytes();
1386+
u64 cur_start;
1387+
1388+
/*
1389+
* For memmap_on_memory, the altmaps were added on a per-memblock
1390+
* basis; we have to process each individual memory block.
1391+
*/
1392+
for (cur_start = start; cur_start < start + size;
1393+
cur_start += memblock_size) {
1394+
struct vmem_altmap *altmap = NULL;
1395+
struct memory_block *mem;
1396+
1397+
mem = find_memory_block(pfn_to_section_nr(PFN_DOWN(cur_start)));
1398+
if (WARN_ON_ONCE(!mem))
1399+
continue;
1400+
1401+
altmap = mem->altmap;
1402+
mem->altmap = NULL;
1403+
1404+
remove_memory_block_devices(cur_start, memblock_size);
1405+
1406+
arch_remove_memory(cur_start, memblock_size, altmap);
1407+
1408+
/* Verify that all vmemmap pages have actually been freed. */
1409+
WARN(altmap->alloc, "Altmap not fully unmapped");
1410+
kfree(altmap);
1411+
}
1412+
}
1413+
1414+
static int create_altmaps_and_memory_blocks(int nid, struct memory_group *group,
1415+
u64 start, u64 size)
1416+
{
1417+
unsigned long memblock_size = memory_block_size_bytes();
1418+
u64 cur_start;
1419+
int ret;
1420+
1421+
for (cur_start = start; cur_start < start + size;
1422+
cur_start += memblock_size) {
1423+
struct mhp_params params = { .pgprot =
1424+
pgprot_mhp(PAGE_KERNEL) };
1425+
struct vmem_altmap mhp_altmap = {
1426+
.base_pfn = PHYS_PFN(cur_start),
1427+
.end_pfn = PHYS_PFN(cur_start + memblock_size - 1),
1428+
};
1429+
1430+
mhp_altmap.free = memory_block_memmap_on_memory_pages();
1431+
params.altmap = kmemdup(&mhp_altmap, sizeof(struct vmem_altmap),
1432+
GFP_KERNEL);
1433+
if (!params.altmap) {
1434+
ret = -ENOMEM;
1435+
goto out;
1436+
}
1437+
1438+
/* call arch's memory hotadd */
1439+
ret = arch_add_memory(nid, cur_start, memblock_size, &params);
1440+
if (ret < 0) {
1441+
kfree(params.altmap);
1442+
goto out;
1443+
}
1444+
1445+
/* create memory block devices after memory was added */
1446+
ret = create_memory_block_devices(cur_start, memblock_size,
1447+
params.altmap, group);
1448+
if (ret) {
1449+
arch_remove_memory(cur_start, memblock_size, NULL);
1450+
kfree(params.altmap);
1451+
goto out;
1452+
}
1453+
}
1454+
1455+
return 0;
1456+
out:
1457+
if (ret && cur_start != start)
1458+
remove_memory_blocks_and_altmaps(start, cur_start - start);
1459+
return ret;
1460+
}
1461+
13831462
/*
13841463
* NOTE: The caller must call lock_device_hotplug() to serialize hotplug
13851464
* and online/offline operations (triggered e.g. by sysfs).
@@ -1390,10 +1469,6 @@ int __ref add_memory_resource(int nid, struct resource *res, mhp_t mhp_flags)
13901469
{
13911470
struct mhp_params params = { .pgprot = pgprot_mhp(PAGE_KERNEL) };
13921471
enum memblock_flags memblock_flags = MEMBLOCK_NONE;
1393-
struct vmem_altmap mhp_altmap = {
1394-
.base_pfn = PHYS_PFN(res->start),
1395-
.end_pfn = PHYS_PFN(res->end),
1396-
};
13971472
struct memory_group *group = NULL;
13981473
u64 start, size;
13991474
bool new_node = false;
@@ -1436,30 +1511,22 @@ int __ref add_memory_resource(int nid, struct resource *res, mhp_t mhp_flags)
14361511
/*
14371512
* Self hosted memmap array
14381513
*/
1439-
if (mhp_flags & MHP_MEMMAP_ON_MEMORY) {
1440-
if (mhp_supports_memmap_on_memory(size)) {
1441-
mhp_altmap.free = memory_block_memmap_on_memory_pages();
1442-
params.altmap = kmemdup(&mhp_altmap,
1443-
sizeof(struct vmem_altmap),
1444-
GFP_KERNEL);
1445-
if (!params.altmap) {
1446-
ret = -ENOMEM;
1447-
goto error;
1448-
}
1449-
}
1450-
/* fallback to not using altmap */
1451-
}
1452-
1453-
/* call arch's memory hotadd */
1454-
ret = arch_add_memory(nid, start, size, &params);
1455-
if (ret < 0)
1456-
goto error_free;
1514+
if ((mhp_flags & MHP_MEMMAP_ON_MEMORY) &&
1515+
mhp_supports_memmap_on_memory(memory_block_size_bytes())) {
1516+
ret = create_altmaps_and_memory_blocks(nid, group, start, size);
1517+
if (ret)
1518+
goto error;
1519+
} else {
1520+
ret = arch_add_memory(nid, start, size, &params);
1521+
if (ret < 0)
1522+
goto error;
14571523

1458-
/* create memory block devices after memory was added */
1459-
ret = create_memory_block_devices(start, size, params.altmap, group);
1460-
if (ret) {
1461-
arch_remove_memory(start, size, params.altmap);
1462-
goto error_free;
1524+
/* create memory block devices after memory was added */
1525+
ret = create_memory_block_devices(start, size, NULL, group);
1526+
if (ret) {
1527+
arch_remove_memory(start, size, params.altmap);
1528+
goto error;
1529+
}
14631530
}
14641531

14651532
if (new_node) {
@@ -1496,8 +1563,6 @@ int __ref add_memory_resource(int nid, struct resource *res, mhp_t mhp_flags)
14961563
walk_memory_blocks(start, size, NULL, online_memory_block);
14971564

14981565
return ret;
1499-
error_free:
1500-
kfree(params.altmap);
15011566
error:
15021567
if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK))
15031568
memblock_remove(start, size);
@@ -2067,17 +2132,13 @@ static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
20672132
return 0;
20682133
}
20692134

2070-
static int test_has_altmap_cb(struct memory_block *mem, void *arg)
2135+
static int count_memory_range_altmaps_cb(struct memory_block *mem, void *arg)
20712136
{
2072-
struct memory_block **mem_ptr = (struct memory_block **)arg;
2073-
/*
2074-
* return the memblock if we have altmap
2075-
* and break callback.
2076-
*/
2077-
if (mem->altmap) {
2078-
*mem_ptr = mem;
2079-
return 1;
2080-
}
2137+
u64 *num_altmaps = (u64 *)arg;
2138+
2139+
if (mem->altmap)
2140+
*num_altmaps += 1;
2141+
20812142
return 0;
20822143
}
20832144

@@ -2151,11 +2212,29 @@ void try_offline_node(int nid)
21512212
}
21522213
EXPORT_SYMBOL(try_offline_node);
21532214

2215+
static int memory_blocks_have_altmaps(u64 start, u64 size)
2216+
{
2217+
u64 num_memblocks = size / memory_block_size_bytes();
2218+
u64 num_altmaps = 0;
2219+
2220+
if (!mhp_memmap_on_memory())
2221+
return 0;
2222+
2223+
walk_memory_blocks(start, size, &num_altmaps,
2224+
count_memory_range_altmaps_cb);
2225+
2226+
if (num_altmaps == 0)
2227+
return 0;
2228+
2229+
if (WARN_ON_ONCE(num_memblocks != num_altmaps))
2230+
return -EINVAL;
2231+
2232+
return 1;
2233+
}
2234+
21542235
static int __ref try_remove_memory(u64 start, u64 size)
21552236
{
2156-
struct memory_block *mem;
2157-
int rc = 0, nid = NUMA_NO_NODE;
2158-
struct vmem_altmap *altmap = NULL;
2237+
int rc, nid = NUMA_NO_NODE;
21592238

21602239
BUG_ON(check_hotplug_memory_range(start, size));
21612240

@@ -2172,45 +2251,26 @@ static int __ref try_remove_memory(u64 start, u64 size)
21722251
if (rc)
21732252
return rc;
21742253

2175-
/*
2176-
* We only support removing memory added with MHP_MEMMAP_ON_MEMORY in
2177-
* the same granularity it was added - a single memory block.
2178-
*/
2179-
if (mhp_memmap_on_memory()) {
2180-
rc = walk_memory_blocks(start, size, &mem, test_has_altmap_cb);
2181-
if (rc) {
2182-
if (size != memory_block_size_bytes()) {
2183-
pr_warn("Refuse to remove %#llx - %#llx,"
2184-
"wrong granularity\n",
2185-
start, start + size);
2186-
return -EINVAL;
2187-
}
2188-
altmap = mem->altmap;
2189-
/*
2190-
* Mark altmap NULL so that we can add a debug
2191-
* check on memblock free.
2192-
*/
2193-
mem->altmap = NULL;
2194-
}
2195-
}
2196-
21972254
/* remove memmap entry */
21982255
firmware_map_remove(start, start + size, "System RAM");
21992256

2200-
/*
2201-
* Memory block device removal under the device_hotplug_lock is
2202-
* a barrier against racing online attempts.
2203-
*/
2204-
remove_memory_block_devices(start, size);
2205-
22062257
mem_hotplug_begin();
22072258

2208-
arch_remove_memory(start, size, altmap);
2209-
2210-
/* Verify that all vmemmap pages have actually been freed. */
2211-
if (altmap) {
2212-
WARN(altmap->alloc, "Altmap not fully unmapped");
2213-
kfree(altmap);
2259+
rc = memory_blocks_have_altmaps(start, size);
2260+
if (rc < 0) {
2261+
mem_hotplug_done();
2262+
return rc;
2263+
} else if (!rc) {
2264+
/*
2265+
* Memory block device removal under the device_hotplug_lock is
2266+
* a barrier against racing online attempts.
2267+
* No altmaps present, do the removal directly
2268+
*/
2269+
remove_memory_block_devices(start, size);
2270+
arch_remove_memory(start, size, NULL);
2271+
} else {
2272+
/* all memblocks in the range have altmaps */
2273+
remove_memory_blocks_and_altmaps(start, size);
22142274
}
22152275

22162276
if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK)) {

0 commit comments

Comments
 (0)