Skip to content

Commit 3668a22

Browse files
committed
mm/memory_hotplug: split memmap_on_memory requests across memblocks
JIRA: https://issues.redhat.com/browse/RHEL-23824 commit 6b8f079 Author: Vishal Verma <vishal.l.verma@intel.com> Date: Tue Nov 7 00:22:42 2023 -0700 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> Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
1 parent 99cb851 commit 3668a22

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
@@ -1416,6 +1416,85 @@ static bool mhp_supports_memmap_on_memory(unsigned long size)
14161416
return arch_supports_memmap_on_memory(vmemmap_size);
14171417
}
14181418

1419+
static void __ref remove_memory_blocks_and_altmaps(u64 start, u64 size)
1420+
{
1421+
unsigned long memblock_size = memory_block_size_bytes();
1422+
u64 cur_start;
1423+
1424+
/*
1425+
* For memmap_on_memory, the altmaps were added on a per-memblock
1426+
* basis; we have to process each individual memory block.
1427+
*/
1428+
for (cur_start = start; cur_start < start + size;
1429+
cur_start += memblock_size) {
1430+
struct vmem_altmap *altmap = NULL;
1431+
struct memory_block *mem;
1432+
1433+
mem = find_memory_block(pfn_to_section_nr(PFN_DOWN(cur_start)));
1434+
if (WARN_ON_ONCE(!mem))
1435+
continue;
1436+
1437+
altmap = mem->altmap;
1438+
mem->altmap = NULL;
1439+
1440+
remove_memory_block_devices(cur_start, memblock_size);
1441+
1442+
arch_remove_memory(cur_start, memblock_size, altmap);
1443+
1444+
/* Verify that all vmemmap pages have actually been freed. */
1445+
WARN(altmap->alloc, "Altmap not fully unmapped");
1446+
kfree(altmap);
1447+
}
1448+
}
1449+
1450+
static int create_altmaps_and_memory_blocks(int nid, struct memory_group *group,
1451+
u64 start, u64 size)
1452+
{
1453+
unsigned long memblock_size = memory_block_size_bytes();
1454+
u64 cur_start;
1455+
int ret;
1456+
1457+
for (cur_start = start; cur_start < start + size;
1458+
cur_start += memblock_size) {
1459+
struct mhp_params params = { .pgprot =
1460+
pgprot_mhp(PAGE_KERNEL) };
1461+
struct vmem_altmap mhp_altmap = {
1462+
.base_pfn = PHYS_PFN(cur_start),
1463+
.end_pfn = PHYS_PFN(cur_start + memblock_size - 1),
1464+
};
1465+
1466+
mhp_altmap.free = memory_block_memmap_on_memory_pages();
1467+
params.altmap = kmemdup(&mhp_altmap, sizeof(struct vmem_altmap),
1468+
GFP_KERNEL);
1469+
if (!params.altmap) {
1470+
ret = -ENOMEM;
1471+
goto out;
1472+
}
1473+
1474+
/* call arch's memory hotadd */
1475+
ret = arch_add_memory(nid, cur_start, memblock_size, &params);
1476+
if (ret < 0) {
1477+
kfree(params.altmap);
1478+
goto out;
1479+
}
1480+
1481+
/* create memory block devices after memory was added */
1482+
ret = create_memory_block_devices(cur_start, memblock_size,
1483+
params.altmap, group);
1484+
if (ret) {
1485+
arch_remove_memory(cur_start, memblock_size, NULL);
1486+
kfree(params.altmap);
1487+
goto out;
1488+
}
1489+
}
1490+
1491+
return 0;
1492+
out:
1493+
if (ret && cur_start != start)
1494+
remove_memory_blocks_and_altmaps(start, cur_start - start);
1495+
return ret;
1496+
}
1497+
14191498
/*
14201499
* NOTE: The caller must call lock_device_hotplug() to serialize hotplug
14211500
* and online/offline operations (triggered e.g. by sysfs).
@@ -1426,10 +1505,6 @@ int __ref add_memory_resource(int nid, struct resource *res, mhp_t mhp_flags)
14261505
{
14271506
struct mhp_params params = { .pgprot = pgprot_mhp(PAGE_KERNEL) };
14281507
enum memblock_flags memblock_flags = MEMBLOCK_NONE;
1429-
struct vmem_altmap mhp_altmap = {
1430-
.base_pfn = PHYS_PFN(res->start),
1431-
.end_pfn = PHYS_PFN(res->end),
1432-
};
14331508
struct memory_group *group = NULL;
14341509
u64 start, size;
14351510
bool new_node = false;
@@ -1472,30 +1547,22 @@ int __ref add_memory_resource(int nid, struct resource *res, mhp_t mhp_flags)
14721547
/*
14731548
* Self hosted memmap array
14741549
*/
1475-
if (mhp_flags & MHP_MEMMAP_ON_MEMORY) {
1476-
if (mhp_supports_memmap_on_memory(size)) {
1477-
mhp_altmap.free = memory_block_memmap_on_memory_pages();
1478-
params.altmap = kmemdup(&mhp_altmap,
1479-
sizeof(struct vmem_altmap),
1480-
GFP_KERNEL);
1481-
if (!params.altmap) {
1482-
ret = -ENOMEM;
1483-
goto error;
1484-
}
1485-
}
1486-
/* fallback to not using altmap */
1487-
}
1488-
1489-
/* call arch's memory hotadd */
1490-
ret = arch_add_memory(nid, start, size, &params);
1491-
if (ret < 0)
1492-
goto error_free;
1550+
if ((mhp_flags & MHP_MEMMAP_ON_MEMORY) &&
1551+
mhp_supports_memmap_on_memory(memory_block_size_bytes())) {
1552+
ret = create_altmaps_and_memory_blocks(nid, group, start, size);
1553+
if (ret)
1554+
goto error;
1555+
} else {
1556+
ret = arch_add_memory(nid, start, size, &params);
1557+
if (ret < 0)
1558+
goto error;
14931559

1494-
/* create memory block devices after memory was added */
1495-
ret = create_memory_block_devices(start, size, params.altmap, group);
1496-
if (ret) {
1497-
arch_remove_memory(start, size, params.altmap);
1498-
goto error_free;
1560+
/* create memory block devices after memory was added */
1561+
ret = create_memory_block_devices(start, size, NULL, group);
1562+
if (ret) {
1563+
arch_remove_memory(start, size, params.altmap);
1564+
goto error;
1565+
}
14991566
}
15001567

15011568
if (new_node) {
@@ -1532,8 +1599,6 @@ int __ref add_memory_resource(int nid, struct resource *res, mhp_t mhp_flags)
15321599
walk_memory_blocks(start, size, NULL, online_memory_block);
15331600

15341601
return ret;
1535-
error_free:
1536-
kfree(params.altmap);
15371602
error:
15381603
if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK))
15391604
memblock_remove(start, size);
@@ -2094,17 +2159,13 @@ static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
20942159
return 0;
20952160
}
20962161

2097-
static int test_has_altmap_cb(struct memory_block *mem, void *arg)
2162+
static int count_memory_range_altmaps_cb(struct memory_block *mem, void *arg)
20982163
{
2099-
struct memory_block **mem_ptr = (struct memory_block **)arg;
2100-
/*
2101-
* return the memblock if we have altmap
2102-
* and break callback.
2103-
*/
2104-
if (mem->altmap) {
2105-
*mem_ptr = mem;
2106-
return 1;
2107-
}
2164+
u64 *num_altmaps = (u64 *)arg;
2165+
2166+
if (mem->altmap)
2167+
*num_altmaps += 1;
2168+
21082169
return 0;
21092170
}
21102171

@@ -2178,11 +2239,29 @@ void try_offline_node(int nid)
21782239
}
21792240
EXPORT_SYMBOL(try_offline_node);
21802241

2242+
static int memory_blocks_have_altmaps(u64 start, u64 size)
2243+
{
2244+
u64 num_memblocks = size / memory_block_size_bytes();
2245+
u64 num_altmaps = 0;
2246+
2247+
if (!mhp_memmap_on_memory())
2248+
return 0;
2249+
2250+
walk_memory_blocks(start, size, &num_altmaps,
2251+
count_memory_range_altmaps_cb);
2252+
2253+
if (num_altmaps == 0)
2254+
return 0;
2255+
2256+
if (WARN_ON_ONCE(num_memblocks != num_altmaps))
2257+
return -EINVAL;
2258+
2259+
return 1;
2260+
}
2261+
21812262
static int __ref try_remove_memory(u64 start, u64 size)
21822263
{
2183-
struct memory_block *mem;
2184-
int rc = 0, nid = NUMA_NO_NODE;
2185-
struct vmem_altmap *altmap = NULL;
2264+
int rc, nid = NUMA_NO_NODE;
21862265

21872266
BUG_ON(check_hotplug_memory_range(start, size));
21882267

@@ -2199,45 +2278,26 @@ static int __ref try_remove_memory(u64 start, u64 size)
21992278
if (rc)
22002279
return rc;
22012280

2202-
/*
2203-
* We only support removing memory added with MHP_MEMMAP_ON_MEMORY in
2204-
* the same granularity it was added - a single memory block.
2205-
*/
2206-
if (mhp_memmap_on_memory()) {
2207-
rc = walk_memory_blocks(start, size, &mem, test_has_altmap_cb);
2208-
if (rc) {
2209-
if (size != memory_block_size_bytes()) {
2210-
pr_warn("Refuse to remove %#llx - %#llx,"
2211-
"wrong granularity\n",
2212-
start, start + size);
2213-
return -EINVAL;
2214-
}
2215-
altmap = mem->altmap;
2216-
/*
2217-
* Mark altmap NULL so that we can add a debug
2218-
* check on memblock free.
2219-
*/
2220-
mem->altmap = NULL;
2221-
}
2222-
}
2223-
22242281
/* remove memmap entry */
22252282
firmware_map_remove(start, start + size, "System RAM");
22262283

2227-
/*
2228-
* Memory block device removal under the device_hotplug_lock is
2229-
* a barrier against racing online attempts.
2230-
*/
2231-
remove_memory_block_devices(start, size);
2232-
22332284
mem_hotplug_begin();
22342285

2235-
arch_remove_memory(start, size, altmap);
2236-
2237-
/* Verify that all vmemmap pages have actually been freed. */
2238-
if (altmap) {
2239-
WARN(altmap->alloc, "Altmap not fully unmapped");
2240-
kfree(altmap);
2286+
rc = memory_blocks_have_altmaps(start, size);
2287+
if (rc < 0) {
2288+
mem_hotplug_done();
2289+
return rc;
2290+
} else if (!rc) {
2291+
/*
2292+
* Memory block device removal under the device_hotplug_lock is
2293+
* a barrier against racing online attempts.
2294+
* No altmaps present, do the removal directly
2295+
*/
2296+
remove_memory_block_devices(start, size);
2297+
arch_remove_memory(start, size, NULL);
2298+
} else {
2299+
/* all memblocks in the range have altmaps */
2300+
remove_memory_blocks_and_altmaps(start, size);
22412301
}
22422302

22432303
if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK)) {

0 commit comments

Comments
 (0)