Skip to content

Commit 9ca6551

Browse files
David Hildenbrandtorvalds
authored andcommitted
mm/memory_hotplug: MEMHP_MERGE_RESOURCE to specify merging of System RAM resources
Some add_memory*() users add memory in small, contiguous memory blocks. Examples include virtio-mem, hyper-v balloon, and the XEN balloon. This can quickly result in a lot of memory resources, whereby the actual resource boundaries are not of interest (e.g., it might be relevant for DIMMs, exposed via /proc/iomem to user space). We really want to merge added resources in this scenario where possible. Let's provide a flag (MEMHP_MERGE_RESOURCE) to specify that a resource either created within add_memory*() or passed via add_memory_resource() shall be marked mergeable and merged with applicable siblings. To implement that, we need a kernel/resource interface to mark selected System RAM resources mergeable (IORESOURCE_SYSRAM_MERGEABLE) and trigger merging. Note: We really want to merge after the whole operation succeeded, not directly when adding a resource to the resource tree (it would break add_memory_resource() and require splitting resources again when the operation failed - e.g., due to -ENOMEM). Signed-off-by: David Hildenbrand <david@redhat.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Reviewed-by: Pankaj Gupta <pankaj.gupta.linux@gmail.com> Cc: Michal Hocko <mhocko@suse.com> Cc: Dan Williams <dan.j.williams@intel.com> Cc: Jason Gunthorpe <jgg@ziepe.ca> Cc: Kees Cook <keescook@chromium.org> Cc: Ard Biesheuvel <ardb@kernel.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: "K. Y. Srinivasan" <kys@microsoft.com> Cc: Haiyang Zhang <haiyangz@microsoft.com> Cc: Stephen Hemminger <sthemmin@microsoft.com> Cc: Wei Liu <wei.liu@kernel.org> Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com> Cc: Juergen Gross <jgross@suse.com> Cc: Stefano Stabellini <sstabellini@kernel.org> Cc: Roger Pau Monné <roger.pau@citrix.com> Cc: Julien Grall <julien@xen.org> Cc: Baoquan He <bhe@redhat.com> Cc: Wei Yang <richardw.yang@linux.intel.com> Cc: Anton Blanchard <anton@ozlabs.org> Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org> Cc: Christian Borntraeger <borntraeger@de.ibm.com> Cc: Dave Jiang <dave.jiang@intel.com> Cc: Eric Biederman <ebiederm@xmission.com> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: Heiko Carstens <hca@linux.ibm.com> Cc: Jason Wang <jasowang@redhat.com> Cc: Len Brown <lenb@kernel.org> Cc: Leonardo Bras <leobras.c@gmail.com> Cc: Libor Pechacek <lpechacek@suse.cz> Cc: Michael Ellerman <mpe@ellerman.id.au> Cc: "Michael S. Tsirkin" <mst@redhat.com> Cc: Nathan Lynch <nathanl@linux.ibm.com> Cc: "Oliver O'Halloran" <oohall@gmail.com> Cc: Paul Mackerras <paulus@samba.org> Cc: Pingfan Liu <kernelfans@gmail.com> Cc: "Rafael J. Wysocki" <rjw@rjwysocki.net> Cc: Vasily Gorbik <gor@linux.ibm.com> Cc: Vishal Verma <vishal.l.verma@intel.com> Link: https://lkml.kernel.org/r/20200911103459.10306-6-david@redhat.com Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent b611719 commit 9ca6551

4 files changed

Lines changed: 78 additions & 0 deletions

File tree

‎include/linux/ioport.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ struct resource {
6060

6161
/* IORESOURCE_SYSRAM specific bits. */
6262
#define IORESOURCE_SYSRAM_DRIVER_MANAGED 0x02000000 /* Always detected via a driver. */
63+
#define IORESOURCE_SYSRAM_MERGEABLE 0x04000000 /* Resource can be merged. */
6364

6465
#define IORESOURCE_EXCLUSIVE 0x08000000 /* Userland may not map this resource */
6566

@@ -253,6 +254,9 @@ extern void __release_region(struct resource *, resource_size_t,
253254
extern void release_mem_region_adjustable(struct resource *, resource_size_t,
254255
resource_size_t);
255256
#endif
257+
#ifdef CONFIG_MEMORY_HOTPLUG
258+
extern void merge_system_ram_resource(struct resource *res);
259+
#endif
256260

257261
/* Wrappers for managed devices */
258262
struct device;

‎include/linux/memory_hotplug.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ typedef int __bitwise mhp_t;
6262

6363
/* No special request */
6464
#define MHP_NONE ((__force mhp_t)0)
65+
/*
66+
* Allow merging of the added System RAM resource with adjacent,
67+
* mergeable resources. After a successful call to add_memory_resource()
68+
* with this flag set, the resource pointer must no longer be used as it
69+
* might be stale, or the resource might have changed.
70+
*/
71+
#define MEMHP_MERGE_RESOURCE ((__force mhp_t)BIT(0))
6572

6673
/*
6774
* Extended parameters for memory hotplug:

‎kernel/resource.c‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,6 +1363,66 @@ void release_mem_region_adjustable(struct resource *parent,
13631363
}
13641364
#endif /* CONFIG_MEMORY_HOTREMOVE */
13651365

1366+
#ifdef CONFIG_MEMORY_HOTPLUG
1367+
static bool system_ram_resources_mergeable(struct resource *r1,
1368+
struct resource *r2)
1369+
{
1370+
/* We assume either r1 or r2 is IORESOURCE_SYSRAM_MERGEABLE. */
1371+
return r1->flags == r2->flags && r1->end + 1 == r2->start &&
1372+
r1->name == r2->name && r1->desc == r2->desc &&
1373+
!r1->child && !r2->child;
1374+
}
1375+
1376+
/*
1377+
* merge_system_ram_resource - mark the System RAM resource mergeable and try to
1378+
* merge it with adjacent, mergeable resources
1379+
* @res: resource descriptor
1380+
*
1381+
* This interface is intended for memory hotplug, whereby lots of contiguous
1382+
* system ram resources are added (e.g., via add_memory*()) by a driver, and
1383+
* the actual resource boundaries are not of interest (e.g., it might be
1384+
* relevant for DIMMs). Only resources that are marked mergeable, that have the
1385+
* same parent, and that don't have any children are considered. All mergeable
1386+
* resources must be immutable during the request.
1387+
*
1388+
* Note:
1389+
* - The caller has to make sure that no pointers to resources that are
1390+
* marked mergeable are used anymore after this call - the resource might
1391+
* be freed and the pointer might be stale!
1392+
* - release_mem_region_adjustable() will split on demand on memory hotunplug
1393+
*/
1394+
void merge_system_ram_resource(struct resource *res)
1395+
{
1396+
const unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
1397+
struct resource *cur;
1398+
1399+
if (WARN_ON_ONCE((res->flags & flags) != flags))
1400+
return;
1401+
1402+
write_lock(&resource_lock);
1403+
res->flags |= IORESOURCE_SYSRAM_MERGEABLE;
1404+
1405+
/* Try to merge with next item in the list. */
1406+
cur = res->sibling;
1407+
if (cur && system_ram_resources_mergeable(res, cur)) {
1408+
res->end = cur->end;
1409+
res->sibling = cur->sibling;
1410+
free_resource(cur);
1411+
}
1412+
1413+
/* Try to merge with previous item in the list. */
1414+
cur = res->parent->child;
1415+
while (cur && cur->sibling != res)
1416+
cur = cur->sibling;
1417+
if (cur && system_ram_resources_mergeable(cur, res)) {
1418+
cur->end = res->end;
1419+
cur->sibling = res->sibling;
1420+
free_resource(res);
1421+
}
1422+
write_unlock(&resource_lock);
1423+
}
1424+
#endif /* CONFIG_MEMORY_HOTPLUG */
1425+
13661426
/*
13671427
* Managed region resource
13681428
*/

‎mm/memory_hotplug.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,13 @@ int __ref add_memory_resource(int nid, struct resource *res, mhp_t mhp_flags)
11031103
/* device_online() will take the lock when calling online_pages() */
11041104
mem_hotplug_done();
11051105

1106+
/*
1107+
* In case we're allowed to merge the resource, flag it and trigger
1108+
* merging now that adding succeeded.
1109+
*/
1110+
if (mhp_flags & MEMHP_MERGE_RESOURCE)
1111+
merge_system_ram_resource(res);
1112+
11061113
/* online pages if requested */
11071114
if (memhp_default_online_type != MMOP_OFFLINE)
11081115
walk_memory_blocks(start, size, NULL, online_memory_block);

0 commit comments

Comments
 (0)