Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753904AbdF1Dpo (ORCPT ); Tue, 27 Jun 2017 23:45:44 -0400 Received: from mail-pf0-f193.google.com ([209.85.192.193]:36542 "EHLO mail-pf0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753605AbdF1Dph (ORCPT ); Tue, 27 Jun 2017 23:45:37 -0400 From: Wei Yang To: akpm@linux-foundation.org, mhocko@suse.com Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Wei Yang Subject: [PATCH] mm/memory_hotplug: adjust zone/node size during __offline_pages() Date: Wed, 28 Jun 2017 11:45:31 +0800 Message-Id: <20170628034531.70940-1-richard.weiyang@gmail.com> X-Mailer: git-send-email 2.11.0 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3976 Lines: 119 After onlining a memory_block and then offline it, the valid_zones will not come back to the original state. For example: $cat memory4?/valid_zones Movable Normal Movable Normal Movable Normal $echo online > memory40/state $cat memory4?/valid_zones Movable Movable Movable $echo offline > memory40/state $cat memory4?/valid_zones Movable Movable Movable While the expected behavior is back to the original valid_zones. The reason is during __offline_pages(), zone/node related fields are not adjusted. This patch adjusts zone/node related fields in __offline_pages(). Signed-off-by: Wei Yang --- mm/memory_hotplug.c | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index 9b94ca67ab00..823939d57f9b 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -879,8 +879,8 @@ bool allow_online_pfn_range(int nid, unsigned long pfn, unsigned long nr_pages, return online_type == MMOP_ONLINE_KEEP; } -static void __meminit resize_zone_range(struct zone *zone, unsigned long start_pfn, - unsigned long nr_pages) +static void __meminit upsize_zone_range(struct zone *zone, + unsigned long start_pfn, unsigned long nr_pages) { unsigned long old_end_pfn = zone_end_pfn(zone); @@ -890,8 +890,21 @@ static void __meminit resize_zone_range(struct zone *zone, unsigned long start_p zone->spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - zone->zone_start_pfn; } -static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned long start_pfn, - unsigned long nr_pages) +static void __meminit downsize_zone_range(struct zone *zone, + unsigned long start_pfn, unsigned long nr_pages) +{ + unsigned long old_end_pfn = zone_end_pfn(zone); + + if (start_pfn == zone->zone_start_pfn + || old_end_pfn == (start_pfn + nr_pages)) + zone->spanned_pages -= nr_pages; + + if (start_pfn == zone->zone_start_pfn) + zone->zone_start_pfn += nr_pages; +} + +static void __meminit upsize_pgdat_range(struct pglist_data *pgdat, + unsigned long start_pfn, unsigned long nr_pages) { unsigned long old_end_pfn = pgdat_end_pfn(pgdat); @@ -901,6 +914,19 @@ static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon pgdat->node_spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - pgdat->node_start_pfn; } +static void __meminit downsize_pgdat_range(struct pglist_data *pgdat, + unsigned long start_pfn, unsigned long nr_pages) +{ + unsigned long old_end_pfn = pgdat_end_pfn(pgdat); + + if (pgdat->node_start_pfn == start_pfn) + pgdat->node_start_pfn = start_pfn; + + if (pgdat->node_start_pfn == start_pfn + || old_end_pfn == (start_pfn + nr_pages)) + pgdat->node_spanned_pages -= nr_pages; +} + void __ref move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn, unsigned long nr_pages) { @@ -916,9 +942,9 @@ void __ref move_pfn_range_to_zone(struct zone *zone, /* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */ pgdat_resize_lock(pgdat, &flags); zone_span_writelock(zone); - resize_zone_range(zone, start_pfn, nr_pages); + upsize_zone_range(zone, start_pfn, nr_pages); zone_span_writeunlock(zone); - resize_pgdat_range(pgdat, start_pfn, nr_pages); + upsize_pgdat_range(pgdat, start_pfn, nr_pages); pgdat_resize_unlock(pgdat, &flags); /* @@ -1809,7 +1835,11 @@ static int __ref __offline_pages(unsigned long start_pfn, zone->present_pages -= offlined_pages; pgdat_resize_lock(zone->zone_pgdat, &flags); + zone_span_writelock(zone); + downsize_zone_range(zone, start_pfn, nr_pages); + zone_span_writeunlock(zone); zone->zone_pgdat->node_present_pages -= offlined_pages; + downsize_pgdat_range(zone->zone_pgdat, start_pfn, nr_pages); pgdat_resize_unlock(zone->zone_pgdat, &flags); init_per_zone_wmark_min(); -- 2.11.0