Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp1529076imm; Wed, 8 Aug 2018 19:55:37 -0700 (PDT) X-Google-Smtp-Source: AA+uWPy1oQ47XGfqyZc9dgcTkKsdo999Gwq0v7eIC0i6f7XQMn3VVzkJwEsu5Q3Ns4UQn9amnREP X-Received: by 2002:a63:5b0d:: with SMTP id p13-v6mr319307pgb.202.1533783337939; Wed, 08 Aug 2018 19:55:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533783337; cv=none; d=google.com; s=arc-20160816; b=maoKQzO8Jthyxa0EUSrVkvxyX1pq8sjlmBuJwAfhNrmF6tEfYSfBlAeE8aL7iDLYoO 4KKNBX2qE9zo5fo9j84smxLbWs89uo1Zho4OpSi5V1HLXacOuBv079hbOPeGNsscB07I xJ6M7Uxu6uLKVwfifIjw1EcrNB0/zpHSBwUwpwVUVD1O2AoHQKavwydHCb5Bf3S3BN5N IOs4cAwV7TN8dt4ds/+sAqgHn7XhPNh1K/87brQ6R8qNQRSxYCjb4+SIHKe0zs1LIkWm aAuwvrqA63DeDU89JXf1gMwpRWPgksfgB8kbwcVQ4v0rYf6THGgucqEKg889nHEPYJ9c Vlww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=tFwl8V00znn4BRd+60VMf7GTR2h8kaA8lcYmqGSB/Rw=; b=uca9aFS76dsxgHIOMU5PPRcfEstsR0gpmk3YWJk52u8p0xC6nM3KNTxkQPsamUJ0u6 LqlSzAPj0J2lm3bRRsJjy5pYSj+uXEUxAbzdlYJnkkWu4Sq93+OcZPq+e/fgJCd9+MY7 oAsbwgoxaNKwVj9ca3QHIwq7z8ZuUfWDh4w1fdJN391lOTo7In6e4+SWhCpI6Q/tW6ZB OpOPwQGbBW/vmPigOw0ftKY5qQu/AlyAXj0h1FS5ZNwOqPKycYVqZE+FZdDke53TFRm6 Ds0a+4V8pA0oBvNZCAz+lX8pC+66Vys51PEkZcCO7McYjzlP1RRVGERT3EkVD7KBpikL UMNA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=qU7CS+0f; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ba6-v6si4679257plb.364.2018.08.08.19.55.23; Wed, 08 Aug 2018 19:55:37 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=qU7CS+0f; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727590AbeHIFRI (ORCPT + 99 others); Thu, 9 Aug 2018 01:17:08 -0400 Received: from mail-pf1-f196.google.com ([209.85.210.196]:39773 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726926AbeHIFRI (ORCPT ); Thu, 9 Aug 2018 01:17:08 -0400 Received: by mail-pf1-f196.google.com with SMTP id j8-v6so2101466pff.6 for ; Wed, 08 Aug 2018 19:54:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=tFwl8V00znn4BRd+60VMf7GTR2h8kaA8lcYmqGSB/Rw=; b=qU7CS+0fAa1sGX3ya47y9enOICbUYETce6ITJO+RX+Cp8QYa4h3+YX6ahK751u2xte 8CXCOrxApwKdVKia3JGvuIJfGjxUDv3+wvL3+4fHjBF6WDbqTFkzajnBYsO588yp0fJd dd72Osgzr7TXdedniMWMMG4krjNiXAwlJz3GUt1zjF8iccRh/XjEi31Tn6Ctk6nXx3aD xs9L7J+1V32O39a45Sqi3dYaOg4Z0ZwK4qXU75y15tyW0y8XBiIOM35AiFQVYlC6gVzI CKHpsyKX/xjU1wZhqN7GhLvKaFXIcQ9iV8D41CRxFFCKwxWhR/FPkUO7BqSqAah6iCqn /rSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=tFwl8V00znn4BRd+60VMf7GTR2h8kaA8lcYmqGSB/Rw=; b=F7bDDVW9iaEgdcyDC70HS6BeqQLhckg23VOHJ36WZ7ZkX13luISy6qp5isbvX//8JY gKDoyN2St/3RiLU8zuRLgiDaKKj28VOvM8k9mQ6Zzundh2m5fDBXrwUD5x0/rQQqxbfl oD0lTcMQJFXyyLdc5tRO/WsTBy3ivyHAPdInps/ujZ+sdyFXqsYKH86CSV2PbXebO2TE PXpKHLzby5uait19X6lsGLLzBb1urKMiiWtPMx8nrchAeE+WpkDz2jQcy3Z5lM3LdBmv cBLNjca2YCySJsfgI+Rma1TX5v74PRReFFpxih92nQLwrgjnrwii9cGC665ofSlBDUsy Ne7g== X-Gm-Message-State: AOUpUlE2hbP6vpyAQrmJP9mnUIgY2ThbhTueDVMnk2lPwz0H+CLWROmh gp+n8/pwK821uULklVsAMTk= X-Received: by 2002:a62:ce05:: with SMTP id y5-v6mr320390pfg.95.1533783274295; Wed, 08 Aug 2018 19:54:34 -0700 (PDT) Received: from rashmica.ozlabs.ibm.com ([122.99.82.10]) by smtp.gmail.com with ESMTPSA id o10-v6sm9796705pfk.76.2018.08.08.19.54.27 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 08 Aug 2018 19:54:33 -0700 (PDT) From: Rashmica Gupta To: toshi.kani@hpe.com, tglx@linutronix.de, akpm@linux-foundation.org, bp@suse.de, brijesh.singh@amd.com, thomas.lendacky@amd.com, jglisse@redhat.com, gregkh@linuxfoundation.org, baiyaowei@cmss.chinamobile.com, dan.j.williams@intel.com, mhocko@suse.com, iamjoonsoo.kim@lge.com, vbabka@suse.cz, malat@debian.org, bhelgaas@google.com, osalvador@techadventures.net, yasu.isimatu@gmail.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, rppt@linux.vnet.ibm.com Cc: Rashmica Gupta Subject: [PATCH v3] resource: Merge resources on a node when hot-adding memory Date: Thu, 9 Aug 2018 12:54:09 +1000 Message-Id: <20180809025409.31552-1-rashmica.g@gmail.com> X-Mailer: git-send-email 2.14.4 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When hot-removing memory release_mem_region_adjustable() splits iomem resources if they are not the exact size of the memory being hot-deleted. Adding this memory back to the kernel adds a new resource. Eg a node has memory 0x0 - 0xfffffffff. Offlining and hot-removing 1GB from 0xf40000000 results in the single resource 0x0-0xfffffffff being split into two resources: 0x0-0xf3fffffff and 0xf80000000-0xfffffffff. When we hot-add the memory back we now have three resources: 0x0-0xf3fffffff, 0xf40000000-0xf7fffffff, and 0xf80000000-0xfffffffff. Now if we try to remove some memory that overlaps these resources, like 2GB from 0xf40000000, release_mem_region_adjustable() fails as it expects the chunk of memory to be within the boundaries of a single resource. This patch adds a function request_resource_and_merge(). This is called instead of request_resource_conflict() when registering a resource in add_memory(). It calls request_resource_conflict() and if hot-removing is enabled (if it isn't we won't get resource fragmentation) we attempt to merge contiguous resources on the node. Signed-off-by: Rashmica Gupta --- v2->v3: Update Xen balloon, make the commit msg and a comment clearer, and changed '>' to '>=' when comparing the end of a resource and the end of a node. v1->v2: Only attempt to merge resources if hot-remove is enabled. drivers/xen/balloon.c | 3 +- include/linux/ioport.h | 2 + include/linux/memory_hotplug.h | 2 +- kernel/resource.c | 120 +++++++++++++++++++++++++++++++++++++++++ mm/memory_hotplug.c | 22 ++++---- 5 files changed, 136 insertions(+), 13 deletions(-) diff --git a/drivers/xen/balloon.c b/drivers/xen/balloon.c index 065f0b607373..9b972b37b0da 100644 --- a/drivers/xen/balloon.c +++ b/drivers/xen/balloon.c @@ -401,7 +401,8 @@ static enum bp_state reserve_additional_memory(void) * callers drop the mutex before trying again. */ mutex_unlock(&balloon_mutex); - rc = add_memory_resource(nid, resource, memhp_auto_online); + rc = add_memory_resource(nid, resource->start, resource_size(resource), + memhp_auto_online); mutex_lock(&balloon_mutex); if (rc) { diff --git a/include/linux/ioport.h b/include/linux/ioport.h index da0ebaec25f0..f5b93a711e86 100644 --- a/include/linux/ioport.h +++ b/include/linux/ioport.h @@ -189,6 +189,8 @@ extern int allocate_resource(struct resource *root, struct resource *new, resource_size_t, resource_size_t), void *alignf_data); +extern struct resource *request_resource_and_merge(struct resource *parent, + struct resource *new, int nid); struct resource *lookup_resource(struct resource *root, resource_size_t start); int adjust_resource(struct resource *res, resource_size_t start, resource_size_t size); diff --git a/include/linux/memory_hotplug.h b/include/linux/memory_hotplug.h index 4e9828cda7a2..9c00f97c8cc6 100644 --- a/include/linux/memory_hotplug.h +++ b/include/linux/memory_hotplug.h @@ -322,7 +322,7 @@ static inline void remove_memory(int nid, u64 start, u64 size) {} extern int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn, void *arg, int (*func)(struct memory_block *, void *)); extern int add_memory(int nid, u64 start, u64 size); -extern int add_memory_resource(int nid, struct resource *resource, bool online); +extern int add_memory_resource(int nid, u64 start, u64 size, bool online); extern int arch_add_memory(int nid, u64 start, u64 size, struct vmem_altmap *altmap, bool want_memblock); extern void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn, diff --git a/kernel/resource.c b/kernel/resource.c index 30e1bc68503b..a31d3f5bccb7 100644 --- a/kernel/resource.c +++ b/kernel/resource.c @@ -1621,3 +1621,123 @@ static int __init strict_iomem(char *str) } __setup("iomem=", strict_iomem); + +#ifdef CONFIG_MEMORY_HOTPLUG +#ifdef CONFIG_MEMORY_HOTREMOVE +/* + * Attempt to merge resource and it's sibling + */ +static int merge_resources(struct resource *res) +{ + struct resource *next; + struct resource *tmp; + uint64_t size; + int ret = -EINVAL; + + next = res->sibling; + + /* + * Not sure how to handle two different children. So only attempt + * to merge two resources if neither have children, only one has a + * child or if both have the same child. + */ + if ((res->child && next->child) && (res->child != next->child)) + return ret; + + if (res->end + 1 != next->start) + return ret; + + if (res->flags != next->flags) + return ret; + + /* Update sibling and child of resource */ + res->sibling = next->sibling; + tmp = res->child; + if (!res->child) + res->child = next->child; + + size = next->end - res->start + 1; + ret = __adjust_resource(res, res->start, size); + if (ret) { + /* Failed so restore resource to original state */ + res->sibling = next; + res->child = tmp; + return ret; + } + + free_resource(next); + + return ret; +} + +/* + * Attempt to merge resources on the node + */ +static void merge_node_resources(int nid, struct resource *parent) +{ + struct resource *res; + uint64_t start_addr; + uint64_t end_addr; + int ret; + + start_addr = node_start_pfn(nid) << PAGE_SHIFT; + end_addr = node_end_pfn(nid) << PAGE_SHIFT; + + write_lock(&resource_lock); + + /* Get the first resource */ + res = parent->child; + + while (res) { + /* Check that the resource is within the node */ + if (res->start < start_addr) { + res = res->sibling; + continue; + } + /* Exit if sibling resource is past end of node */ + if (res->sibling->end >= end_addr) + break; + + ret = merge_resources(res); + if (!ret) + continue; + res = res->sibling; + } + write_unlock(&resource_lock); +} +#endif /* CONFIG_MEMORY_HOTREMOVE */ + +/** + * request_resource_and_merge() - request an I/O or memory resource for hot-add + * @parent: parent resource descriptor + * @new: resource descriptor desired by caller + * @nid: node id of the node we want the resource on + * + * If no conflict resource then attempt to merge resources on the node. + * + * This is intended to cleanup the fragmentation of resources that occurs when + * hot-removing memory (see release_mem_region_adjustable). If hot-removing is + * not enabled then there is no point trying to merge resources. + * + * Note that the inability to merge resources is not an error. + * + * Return: NULL for successful request of resource and conflict resource if + * there was a conflict. + */ +struct resource *request_resource_and_merge(struct resource *parent, + struct resource *new, int nid) +{ + struct resource *conflict; + + conflict = request_resource_conflict(parent, new); + + if (conflict) + return conflict; + +#ifdef CONFIG_MEMORY_HOTREMOVE + merge_node_resources(nid, parent); +#endif /* CONFIG_MEMORY_HOTREMOVE */ + + return NULL; +} +#endif /* CONFIG_MEMORY_HOTPLUG */ diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index 7deb49f69e27..2e342f5ce322 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -97,7 +97,7 @@ void mem_hotplug_done(void) } /* add this memory to iomem resource */ -static struct resource *register_memory_resource(u64 start, u64 size) +static struct resource *register_memory_resource(int nid, u64 start, u64 size) { struct resource *res, *conflict; res = kzalloc(sizeof(struct resource), GFP_KERNEL); @@ -108,7 +108,7 @@ static struct resource *register_memory_resource(u64 start, u64 size) res->start = start; res->end = start + size - 1; res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY; - conflict = request_resource_conflict(&iomem_resource, res); + conflict = request_resource_and_merge(&iomem_resource, res, nid); if (conflict) { if (conflict->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY) { pr_debug("Device unaddressable memory block " @@ -122,11 +122,15 @@ static struct resource *register_memory_resource(u64 start, u64 size) return res; } -static void release_memory_resource(struct resource *res) +static void release_memory_resource(struct resource *res, u64 start, u64 size) { if (!res) return; +#ifdef CONFIG_MEMORY_HOTREMOVE + release_mem_region_adjustable(&iomem_resource, start, size); +#else release_resource(res); +#endif kfree(res); return; } @@ -1096,17 +1100,13 @@ static int online_memory_block(struct memory_block *mem, void *arg) } /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */ -int __ref add_memory_resource(int nid, struct resource *res, bool online) +int __ref add_memory_resource(int nid, u64 start, u64 size, bool online) { - u64 start, size; pg_data_t *pgdat = NULL; bool new_pgdat; bool new_node; int ret; - start = res->start; - size = resource_size(res); - ret = check_hotplug_memory_range(start, size); if (ret) return ret; @@ -1195,13 +1195,13 @@ int __ref add_memory(int nid, u64 start, u64 size) struct resource *res; int ret; - res = register_memory_resource(start, size); + res = register_memory_resource(nid, start, size); if (IS_ERR(res)) return PTR_ERR(res); - ret = add_memory_resource(nid, res, memhp_auto_online); + ret = add_memory_resource(nid, start, size, memhp_auto_online); if (ret < 0) - release_memory_resource(res); + release_memory_resource(res, start, size); return ret; } EXPORT_SYMBOL_GPL(add_memory); -- 2.14.4