mm/memory_hotplug: shrink zones when offlining memory

commit feee6b2989165631b17ac6d4ccdbf6759254e85a upstream.

-- snip --

- Missing arm64 hot(un)plug support
- Missing some vmem_altmap_offset() cleanups
- Missing sub-section hotadd support
- Missing unification of mm/hmm.c and kernel/memremap.c

-- snip --

We currently try to shrink a single zone when removing memory.  We use
the zone of the first page of the memory we are removing.  If that
memmap was never initialized (e.g., memory was never onlined), we will
read garbage and can trigger kernel BUGs (due to a stale pointer):

    BUG: unable to handle page fault for address: 000000000000353d
    #PF: supervisor write access in kernel mode
    #PF: error_code(0x0002) - not-present page
    PGD 0 P4D 0
    Oops: 0002 [#1] SMP PTI
    CPU: 1 PID: 7 Comm: kworker/u8:0 Not tainted 5.3.0-rc5-next-20190820+ #317
    Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.12.1-0-ga5cab58e9a3f-prebuilt.qemu.4
    Workqueue: kacpi_hotplug acpi_hotplug_work_fn
    RIP: 0010:clear_zone_contiguous+0x5/0x10
    Code: 48 89 c6 48 89 c3 e8 2a fe ff ff 48 85 c0 75 cf 5b 5d c3 c6 85 fd 05 00 00 01 5b 5d c3 0f 1f 840
    RSP: 0018:ffffad2400043c98 EFLAGS: 00010246
    RAX: 0000000000000000 RBX: 0000000200000000 RCX: 0000000000000000
    RDX: 0000000000200000 RSI: 0000000000140000 RDI: 0000000000002f40
    RBP: 0000000140000000 R08: 0000000000000000 R09: 0000000000000001
    R10: 0000000000000000 R11: 0000000000000000 R12: 0000000000140000
    R13: 0000000000140000 R14: 0000000000002f40 R15: ffff9e3e7aff3680
    FS:  0000000000000000(0000) GS:ffff9e3e7bb00000(0000) knlGS:0000000000000000
    CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
    CR2: 000000000000353d CR3: 0000000058610000 CR4: 00000000000006e0
    DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
    DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
    Call Trace:
     __remove_pages+0x4b/0x640
     arch_remove_memory+0x63/0x8d
     try_remove_memory+0xdb/0x130
     __remove_memory+0xa/0x11
     acpi_memory_device_remove+0x70/0x100
     acpi_bus_trim+0x55/0x90
     acpi_device_hotplug+0x227/0x3a0
     acpi_hotplug_work_fn+0x1a/0x30
     process_one_work+0x221/0x550
     worker_thread+0x50/0x3b0
     kthread+0x105/0x140
     ret_from_fork+0x3a/0x50
    Modules linked in:
    CR2: 000000000000353d

Instead, shrink the zones when offlining memory or when onlining failed.
Introduce and use remove_pfn_range_from_zone(() for that.  We now
properly shrink the zones, even if we have DIMMs whereby

 - Some memory blocks fall into no zone (never onlined)

 - Some memory blocks fall into multiple zones (offlined+re-onlined)

 - Multiple memory blocks that fall into different zones

Drop the zone parameter (with a potential dubious value) from
__remove_pages() and __remove_section().

Link: http://lkml.kernel.org/r/20191006085646.5768-6-david@redhat.com
Fixes: f1dd2cd13c ("mm, memory_hotplug: do not associate hotadded memory to zones until online")	[visible after d0dc12e86b]
Signed-off-by: David Hildenbrand <david@redhat.com>
Reviewed-by: Oscar Salvador <osalvador@suse.de>
Cc: Michal Hocko <mhocko@suse.com>
Cc: "Matthew Wilcox (Oracle)" <willy@infradead.org>
Cc: "Aneesh Kumar K.V" <aneesh.kumar@linux.ibm.com>
Cc: Pavel Tatashin <pasha.tatashin@soleen.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Dan Williams <dan.j.williams@intel.com>
Cc: Logan Gunthorpe <logang@deltatee.com>
Cc: <stable@vger.kernel.org>	[5.0+]
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: David Hildenbrand <david@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
David Hildenbrand 2020-01-28 10:50:21 +01:00 committed by Greg Kroah-Hartman
parent d98d053efa
commit 86834898d5
10 changed files with 27 additions and 51 deletions

View file

@ -666,9 +666,7 @@ void arch_remove_memory(int nid, u64 start, u64 size,
{ {
unsigned long start_pfn = start >> PAGE_SHIFT; unsigned long start_pfn = start >> PAGE_SHIFT;
unsigned long nr_pages = size >> PAGE_SHIFT; unsigned long nr_pages = size >> PAGE_SHIFT;
struct zone *zone;
zone = page_zone(pfn_to_page(start_pfn)); __remove_pages(start_pfn, nr_pages, altmap);
__remove_pages(zone, start_pfn, nr_pages, altmap);
} }
#endif #endif

View file

@ -144,18 +144,9 @@ void __ref arch_remove_memory(int nid, u64 start, u64 size,
{ {
unsigned long start_pfn = start >> PAGE_SHIFT; unsigned long start_pfn = start >> PAGE_SHIFT;
unsigned long nr_pages = size >> PAGE_SHIFT; unsigned long nr_pages = size >> PAGE_SHIFT;
struct page *page;
int ret; int ret;
/* __remove_pages(start_pfn, nr_pages, altmap);
* If we have an altmap then we need to skip over any reserved PFNs
* when querying the zone.
*/
page = pfn_to_page(start_pfn);
if (altmap)
page += vmem_altmap_offset(altmap);
__remove_pages(page_zone(page), start_pfn, nr_pages, altmap);
/* Remove htab bolted mappings for this section of memory */ /* Remove htab bolted mappings for this section of memory */
start = (unsigned long)__va(start); start = (unsigned long)__va(start);

View file

@ -244,10 +244,8 @@ void arch_remove_memory(int nid, u64 start, u64 size,
{ {
unsigned long start_pfn = start >> PAGE_SHIFT; unsigned long start_pfn = start >> PAGE_SHIFT;
unsigned long nr_pages = size >> PAGE_SHIFT; unsigned long nr_pages = size >> PAGE_SHIFT;
struct zone *zone;
zone = page_zone(pfn_to_page(start_pfn)); __remove_pages(start_pfn, nr_pages, altmap);
__remove_pages(zone, start_pfn, nr_pages, altmap);
vmem_remove_mapping(start, size); vmem_remove_mapping(start, size);
} }
#endif /* CONFIG_MEMORY_HOTPLUG */ #endif /* CONFIG_MEMORY_HOTPLUG */

View file

@ -448,9 +448,7 @@ void arch_remove_memory(int nid, u64 start, u64 size,
{ {
unsigned long start_pfn = PFN_DOWN(start); unsigned long start_pfn = PFN_DOWN(start);
unsigned long nr_pages = size >> PAGE_SHIFT; unsigned long nr_pages = size >> PAGE_SHIFT;
struct zone *zone;
zone = page_zone(pfn_to_page(start_pfn)); __remove_pages(start_pfn, nr_pages, altmap);
__remove_pages(zone, start_pfn, nr_pages, altmap);
} }
#endif /* CONFIG_MEMORY_HOTPLUG */ #endif /* CONFIG_MEMORY_HOTPLUG */

View file

@ -865,10 +865,8 @@ void arch_remove_memory(int nid, u64 start, u64 size,
{ {
unsigned long start_pfn = start >> PAGE_SHIFT; unsigned long start_pfn = start >> PAGE_SHIFT;
unsigned long nr_pages = size >> PAGE_SHIFT; unsigned long nr_pages = size >> PAGE_SHIFT;
struct zone *zone;
zone = page_zone(pfn_to_page(start_pfn)); __remove_pages(start_pfn, nr_pages, altmap);
__remove_pages(zone, start_pfn, nr_pages, altmap);
} }
#endif #endif

View file

@ -1146,14 +1146,8 @@ void __ref arch_remove_memory(int nid, u64 start, u64 size,
{ {
unsigned long start_pfn = start >> PAGE_SHIFT; unsigned long start_pfn = start >> PAGE_SHIFT;
unsigned long nr_pages = size >> PAGE_SHIFT; unsigned long nr_pages = size >> PAGE_SHIFT;
struct page *page = pfn_to_page(start_pfn);
struct zone *zone;
/* With altmap the first mapped page is offset from @start */ __remove_pages(start_pfn, nr_pages, altmap);
if (altmap)
page += vmem_altmap_offset(altmap);
zone = page_zone(page);
__remove_pages(zone, start_pfn, nr_pages, altmap);
kernel_physical_mapping_remove(start, start + size); kernel_physical_mapping_remove(start, start + size);
} }
#endif /* CONFIG_MEMORY_HOTPLUG */ #endif /* CONFIG_MEMORY_HOTPLUG */

View file

@ -110,8 +110,8 @@ static inline bool movable_node_is_enabled(void)
extern void arch_remove_memory(int nid, u64 start, u64 size, extern void arch_remove_memory(int nid, u64 start, u64 size,
struct vmem_altmap *altmap); struct vmem_altmap *altmap);
extern void __remove_pages(struct zone *zone, unsigned long start_pfn, extern void __remove_pages(unsigned long start_pfn, unsigned long nr_pages,
unsigned long nr_pages, struct vmem_altmap *altmap); struct vmem_altmap *altmap);
/* reasonably generic interface to expand the physical pages */ /* reasonably generic interface to expand the physical pages */
extern int __add_pages(int nid, unsigned long start_pfn, unsigned long nr_pages, extern int __add_pages(int nid, unsigned long start_pfn, unsigned long nr_pages,
@ -331,6 +331,9 @@ extern int arch_add_memory(int nid, u64 start, u64 size,
struct vmem_altmap *altmap, bool want_memblock); struct vmem_altmap *altmap, bool want_memblock);
extern void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn, extern void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
unsigned long nr_pages, struct vmem_altmap *altmap); unsigned long nr_pages, struct vmem_altmap *altmap);
extern void remove_pfn_range_from_zone(struct zone *zone,
unsigned long start_pfn,
unsigned long nr_pages);
extern int offline_pages(unsigned long start_pfn, unsigned long nr_pages); extern int offline_pages(unsigned long start_pfn, unsigned long nr_pages);
extern bool is_memblock_offlined(struct memory_block *mem); extern bool is_memblock_offlined(struct memory_block *mem);
extern int sparse_add_one_section(int nid, unsigned long start_pfn, extern int sparse_add_one_section(int nid, unsigned long start_pfn,

View file

@ -141,8 +141,7 @@ static void devm_memremap_pages_release(void *data)
mem_hotplug_begin(); mem_hotplug_begin();
if (pgmap->type == MEMORY_DEVICE_PRIVATE) { if (pgmap->type == MEMORY_DEVICE_PRIVATE) {
pfn = align_start >> PAGE_SHIFT; pfn = align_start >> PAGE_SHIFT;
__remove_pages(page_zone(first_page), pfn, __remove_pages(pfn, align_size >> PAGE_SHIFT, NULL);
align_size >> PAGE_SHIFT, NULL);
} else { } else {
arch_remove_memory(nid, align_start, align_size, arch_remove_memory(nid, align_start, align_size,
pgmap->altmap_valid ? &pgmap->altmap : NULL); pgmap->altmap_valid ? &pgmap->altmap : NULL);

View file

@ -997,7 +997,6 @@ static void hmm_devmem_release(void *data)
struct hmm_devmem *devmem = data; struct hmm_devmem *devmem = data;
struct resource *resource = devmem->resource; struct resource *resource = devmem->resource;
unsigned long start_pfn, npages; unsigned long start_pfn, npages;
struct zone *zone;
struct page *page; struct page *page;
int nid; int nid;
@ -1006,12 +1005,11 @@ static void hmm_devmem_release(void *data)
npages = ALIGN(resource_size(resource), PA_SECTION_SIZE) >> PAGE_SHIFT; npages = ALIGN(resource_size(resource), PA_SECTION_SIZE) >> PAGE_SHIFT;
page = pfn_to_page(start_pfn); page = pfn_to_page(start_pfn);
zone = page_zone(page);
nid = page_to_nid(page); nid = page_to_nid(page);
mem_hotplug_begin(); mem_hotplug_begin();
if (resource->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY) if (resource->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY)
__remove_pages(zone, start_pfn, npages, NULL); __remove_pages(start_pfn, npages, NULL);
else else
arch_remove_memory(nid, start_pfn << PAGE_SHIFT, arch_remove_memory(nid, start_pfn << PAGE_SHIFT,
npages << PAGE_SHIFT, NULL); npages << PAGE_SHIFT, NULL);

View file

@ -449,10 +449,11 @@ static void update_pgdat_span(struct pglist_data *pgdat)
pgdat->node_spanned_pages = node_end_pfn - node_start_pfn; pgdat->node_spanned_pages = node_end_pfn - node_start_pfn;
} }
static void __remove_zone(struct zone *zone, unsigned long start_pfn) void __ref remove_pfn_range_from_zone(struct zone *zone,
unsigned long start_pfn,
unsigned long nr_pages)
{ {
struct pglist_data *pgdat = zone->zone_pgdat; struct pglist_data *pgdat = zone->zone_pgdat;
int nr_pages = PAGES_PER_SECTION;
unsigned long flags; unsigned long flags;
#ifdef CONFIG_ZONE_DEVICE #ifdef CONFIG_ZONE_DEVICE
@ -465,14 +466,17 @@ static void __remove_zone(struct zone *zone, unsigned long start_pfn)
return; return;
#endif #endif
clear_zone_contiguous(zone);
pgdat_resize_lock(zone->zone_pgdat, &flags); pgdat_resize_lock(zone->zone_pgdat, &flags);
shrink_zone_span(zone, start_pfn, start_pfn + nr_pages); shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
update_pgdat_span(pgdat); update_pgdat_span(pgdat);
pgdat_resize_unlock(zone->zone_pgdat, &flags); pgdat_resize_unlock(zone->zone_pgdat, &flags);
set_zone_contiguous(zone);
} }
static void __remove_section(struct zone *zone, struct mem_section *ms, static void __remove_section(struct mem_section *ms, unsigned long map_offset,
unsigned long map_offset,
struct vmem_altmap *altmap) struct vmem_altmap *altmap)
{ {
unsigned long start_pfn; unsigned long start_pfn;
@ -483,14 +487,12 @@ static void __remove_section(struct zone *zone, struct mem_section *ms,
scn_nr = __section_nr(ms); scn_nr = __section_nr(ms);
start_pfn = section_nr_to_pfn((unsigned long)scn_nr); start_pfn = section_nr_to_pfn((unsigned long)scn_nr);
__remove_zone(zone, start_pfn);
sparse_remove_one_section(ms, map_offset, altmap); sparse_remove_one_section(ms, map_offset, altmap);
} }
/** /**
* __remove_pages() - remove sections of pages from a zone * __remove_pages() - remove sections of pages
* @zone: zone from which pages need to be removed
* @phys_start_pfn: starting pageframe (must be aligned to start of a section) * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
* @nr_pages: number of pages to remove (must be multiple of section size) * @nr_pages: number of pages to remove (must be multiple of section size)
* @altmap: alternative device page map or %NULL if default memmap is used * @altmap: alternative device page map or %NULL if default memmap is used
@ -500,8 +502,8 @@ static void __remove_section(struct zone *zone, struct mem_section *ms,
* sure that pages are marked reserved and zones are adjust properly by * sure that pages are marked reserved and zones are adjust properly by
* calling offline_pages(). * calling offline_pages().
*/ */
void __remove_pages(struct zone *zone, unsigned long phys_start_pfn, void __remove_pages(unsigned long phys_start_pfn, unsigned long nr_pages,
unsigned long nr_pages, struct vmem_altmap *altmap) struct vmem_altmap *altmap)
{ {
unsigned long i; unsigned long i;
unsigned long map_offset = 0; unsigned long map_offset = 0;
@ -510,8 +512,6 @@ void __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
if (altmap) if (altmap)
map_offset = vmem_altmap_offset(altmap); map_offset = vmem_altmap_offset(altmap);
clear_zone_contiguous(zone);
/* /*
* We can only remove entire sections * We can only remove entire sections
*/ */
@ -523,12 +523,9 @@ void __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION; unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
cond_resched(); cond_resched();
__remove_section(zone, __pfn_to_section(pfn), map_offset, __remove_section(__pfn_to_section(pfn), map_offset, altmap);
altmap);
map_offset = 0; map_offset = 0;
} }
set_zone_contiguous(zone);
} }
int set_online_page_callback(online_page_callback_t callback) int set_online_page_callback(online_page_callback_t callback)
@ -898,6 +895,7 @@ int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_typ
(unsigned long long) pfn << PAGE_SHIFT, (unsigned long long) pfn << PAGE_SHIFT,
(((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1); (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1);
memory_notify(MEM_CANCEL_ONLINE, &arg); memory_notify(MEM_CANCEL_ONLINE, &arg);
remove_pfn_range_from_zone(zone, pfn, nr_pages);
mem_hotplug_done(); mem_hotplug_done();
return ret; return ret;
} }
@ -1682,6 +1680,7 @@ static int __ref __offline_pages(unsigned long start_pfn,
writeback_set_ratelimit(); writeback_set_ratelimit();
memory_notify(MEM_OFFLINE, &arg); memory_notify(MEM_OFFLINE, &arg);
remove_pfn_range_from_zone(zone, start_pfn, nr_pages);
mem_hotplug_done(); mem_hotplug_done();
return 0; return 0;