Skip to content

Commit feee6b2

Browse files
davidhildenbrandtorvalds
authored andcommitted
mm/memory_hotplug: shrink zones when offlining memory
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+ torvalds#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/[email protected] Fixes: f1dd2cd ("mm, memory_hotplug: do not associate hotadded memory to zones until online") [visible after d0dc12e] Signed-off-by: David Hildenbrand <[email protected]> Reviewed-by: Oscar Salvador <[email protected]> Cc: Michal Hocko <[email protected]> Cc: "Matthew Wilcox (Oracle)" <[email protected]> Cc: "Aneesh Kumar K.V" <[email protected]> Cc: Pavel Tatashin <[email protected]> Cc: Greg Kroah-Hartman <[email protected]> Cc: Dan Williams <[email protected]> Cc: Logan Gunthorpe <[email protected]> Cc: <[email protected]> [5.0+] Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 5613970 commit feee6b2

File tree

10 files changed

+29
-38
lines changed

10 files changed

+29
-38
lines changed

arch/arm64/mm/mmu.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,6 @@ void arch_remove_memory(int nid, u64 start, u64 size,
10701070
{
10711071
unsigned long start_pfn = start >> PAGE_SHIFT;
10721072
unsigned long nr_pages = size >> PAGE_SHIFT;
1073-
struct zone *zone;
10741073

10751074
/*
10761075
* FIXME: Cleanup page tables (also in arch_add_memory() in case
@@ -1079,7 +1078,6 @@ void arch_remove_memory(int nid, u64 start, u64 size,
10791078
* unplug. ARCH_ENABLE_MEMORY_HOTREMOVE must not be
10801079
* unlocked yet.
10811080
*/
1082-
zone = page_zone(pfn_to_page(start_pfn));
1083-
__remove_pages(zone, start_pfn, nr_pages, altmap);
1081+
__remove_pages(start_pfn, nr_pages, altmap);
10841082
}
10851083
#endif

arch/ia64/mm/init.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -689,9 +689,7 @@ void arch_remove_memory(int nid, u64 start, u64 size,
689689
{
690690
unsigned long start_pfn = start >> PAGE_SHIFT;
691691
unsigned long nr_pages = size >> PAGE_SHIFT;
692-
struct zone *zone;
693692

694-
zone = page_zone(pfn_to_page(start_pfn));
695-
__remove_pages(zone, start_pfn, nr_pages, altmap);
693+
__remove_pages(start_pfn, nr_pages, altmap);
696694
}
697695
#endif

arch/powerpc/mm/mem.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,9 @@ void __ref arch_remove_memory(int nid, u64 start, u64 size,
151151
{
152152
unsigned long start_pfn = start >> PAGE_SHIFT;
153153
unsigned long nr_pages = size >> PAGE_SHIFT;
154-
struct page *page = pfn_to_page(start_pfn) + vmem_altmap_offset(altmap);
155154
int ret;
156155

157-
__remove_pages(page_zone(page), start_pfn, nr_pages, altmap);
156+
__remove_pages(start_pfn, nr_pages, altmap);
158157

159158
/* Remove htab bolted mappings for this section of memory */
160159
start = (unsigned long)__va(start);

arch/s390/mm/init.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,8 @@ void arch_remove_memory(int nid, u64 start, u64 size,
292292
{
293293
unsigned long start_pfn = start >> PAGE_SHIFT;
294294
unsigned long nr_pages = size >> PAGE_SHIFT;
295-
struct zone *zone;
296295

297-
zone = page_zone(pfn_to_page(start_pfn));
298-
__remove_pages(zone, start_pfn, nr_pages, altmap);
296+
__remove_pages(start_pfn, nr_pages, altmap);
299297
vmem_remove_mapping(start, size);
300298
}
301299
#endif /* CONFIG_MEMORY_HOTPLUG */

arch/sh/mm/init.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -434,9 +434,7 @@ void arch_remove_memory(int nid, u64 start, u64 size,
434434
{
435435
unsigned long start_pfn = PFN_DOWN(start);
436436
unsigned long nr_pages = size >> PAGE_SHIFT;
437-
struct zone *zone;
438437

439-
zone = page_zone(pfn_to_page(start_pfn));
440-
__remove_pages(zone, start_pfn, nr_pages, altmap);
438+
__remove_pages(start_pfn, nr_pages, altmap);
441439
}
442440
#endif /* CONFIG_MEMORY_HOTPLUG */

arch/x86/mm/init_32.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -865,10 +865,8 @@ void arch_remove_memory(int nid, u64 start, u64 size,
865865
{
866866
unsigned long start_pfn = start >> PAGE_SHIFT;
867867
unsigned long nr_pages = size >> PAGE_SHIFT;
868-
struct zone *zone;
869868

870-
zone = page_zone(pfn_to_page(start_pfn));
871-
__remove_pages(zone, start_pfn, nr_pages, altmap);
869+
__remove_pages(start_pfn, nr_pages, altmap);
872870
}
873871
#endif
874872

arch/x86/mm/init_64.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -1212,10 +1212,8 @@ void __ref arch_remove_memory(int nid, u64 start, u64 size,
12121212
{
12131213
unsigned long start_pfn = start >> PAGE_SHIFT;
12141214
unsigned long nr_pages = size >> PAGE_SHIFT;
1215-
struct page *page = pfn_to_page(start_pfn) + vmem_altmap_offset(altmap);
1216-
struct zone *zone = page_zone(page);
12171215

1218-
__remove_pages(zone, start_pfn, nr_pages, altmap);
1216+
__remove_pages(start_pfn, nr_pages, altmap);
12191217
kernel_physical_mapping_remove(start, start + size);
12201218
}
12211219
#endif /* CONFIG_MEMORY_HOTPLUG */

include/linux/memory_hotplug.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ static inline bool movable_node_is_enabled(void)
122122

123123
extern void arch_remove_memory(int nid, u64 start, u64 size,
124124
struct vmem_altmap *altmap);
125-
extern void __remove_pages(struct zone *zone, unsigned long start_pfn,
126-
unsigned long nr_pages, struct vmem_altmap *altmap);
125+
extern void __remove_pages(unsigned long start_pfn, unsigned long nr_pages,
126+
struct vmem_altmap *altmap);
127127

128128
/* reasonably generic interface to expand the physical pages */
129129
extern int __add_pages(int nid, unsigned long start_pfn, unsigned long nr_pages,
@@ -342,6 +342,9 @@ extern int add_memory(int nid, u64 start, u64 size);
342342
extern int add_memory_resource(int nid, struct resource *resource);
343343
extern void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
344344
unsigned long nr_pages, struct vmem_altmap *altmap);
345+
extern void remove_pfn_range_from_zone(struct zone *zone,
346+
unsigned long start_pfn,
347+
unsigned long nr_pages);
345348
extern bool is_memblock_offlined(struct memory_block *mem);
346349
extern int sparse_add_section(int nid, unsigned long pfn,
347350
unsigned long nr_pages, struct vmem_altmap *altmap);

mm/memory_hotplug.c

+16-15
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,9 @@ static void update_pgdat_span(struct pglist_data *pgdat)
483483
pgdat->node_spanned_pages = node_end_pfn - node_start_pfn;
484484
}
485485

486-
static void __remove_zone(struct zone *zone, unsigned long start_pfn,
487-
unsigned long nr_pages)
486+
void __ref remove_pfn_range_from_zone(struct zone *zone,
487+
unsigned long start_pfn,
488+
unsigned long nr_pages)
488489
{
489490
struct pglist_data *pgdat = zone->zone_pgdat;
490491
unsigned long flags;
@@ -499,28 +500,30 @@ static void __remove_zone(struct zone *zone, unsigned long start_pfn,
499500
return;
500501
#endif
501502

503+
clear_zone_contiguous(zone);
504+
502505
pgdat_resize_lock(zone->zone_pgdat, &flags);
503506
shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
504507
update_pgdat_span(pgdat);
505508
pgdat_resize_unlock(zone->zone_pgdat, &flags);
509+
510+
set_zone_contiguous(zone);
506511
}
507512

508-
static void __remove_section(struct zone *zone, unsigned long pfn,
509-
unsigned long nr_pages, unsigned long map_offset,
510-
struct vmem_altmap *altmap)
513+
static void __remove_section(unsigned long pfn, unsigned long nr_pages,
514+
unsigned long map_offset,
515+
struct vmem_altmap *altmap)
511516
{
512517
struct mem_section *ms = __nr_to_section(pfn_to_section_nr(pfn));
513518

514519
if (WARN_ON_ONCE(!valid_section(ms)))
515520
return;
516521

517-
__remove_zone(zone, pfn, nr_pages);
518522
sparse_remove_section(ms, pfn, nr_pages, map_offset, altmap);
519523
}
520524

521525
/**
522-
* __remove_pages() - remove sections of pages from a zone
523-
* @zone: zone from which pages need to be removed
526+
* __remove_pages() - remove sections of pages
524527
* @pfn: starting pageframe (must be aligned to start of a section)
525528
* @nr_pages: number of pages to remove (must be multiple of section size)
526529
* @altmap: alternative device page map or %NULL if default memmap is used
@@ -530,16 +533,14 @@ static void __remove_section(struct zone *zone, unsigned long pfn,
530533
* sure that pages are marked reserved and zones are adjust properly by
531534
* calling offline_pages().
532535
*/
533-
void __remove_pages(struct zone *zone, unsigned long pfn,
534-
unsigned long nr_pages, struct vmem_altmap *altmap)
536+
void __remove_pages(unsigned long pfn, unsigned long nr_pages,
537+
struct vmem_altmap *altmap)
535538
{
536539
unsigned long map_offset = 0;
537540
unsigned long nr, start_sec, end_sec;
538541

539542
map_offset = vmem_altmap_offset(altmap);
540543

541-
clear_zone_contiguous(zone);
542-
543544
if (check_pfn_span(pfn, nr_pages, "remove"))
544545
return;
545546

@@ -551,13 +552,11 @@ void __remove_pages(struct zone *zone, unsigned long pfn,
551552
cond_resched();
552553
pfns = min(nr_pages, PAGES_PER_SECTION
553554
- (pfn & ~PAGE_SECTION_MASK));
554-
__remove_section(zone, pfn, pfns, map_offset, altmap);
555+
__remove_section(pfn, pfns, map_offset, altmap);
555556
pfn += pfns;
556557
nr_pages -= pfns;
557558
map_offset = 0;
558559
}
559-
560-
set_zone_contiguous(zone);
561560
}
562561

563562
int set_online_page_callback(online_page_callback_t callback)
@@ -869,6 +868,7 @@ int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_typ
869868
(unsigned long long) pfn << PAGE_SHIFT,
870869
(((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1);
871870
memory_notify(MEM_CANCEL_ONLINE, &arg);
871+
remove_pfn_range_from_zone(zone, pfn, nr_pages);
872872
mem_hotplug_done();
873873
return ret;
874874
}
@@ -1628,6 +1628,7 @@ static int __ref __offline_pages(unsigned long start_pfn,
16281628
writeback_set_ratelimit();
16291629

16301630
memory_notify(MEM_OFFLINE, &arg);
1631+
remove_pfn_range_from_zone(zone, start_pfn, nr_pages);
16311632
mem_hotplug_done();
16321633
return 0;
16331634

mm/memremap.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ void memunmap_pages(struct dev_pagemap *pgmap)
120120

121121
mem_hotplug_begin();
122122
if (pgmap->type == MEMORY_DEVICE_PRIVATE) {
123-
__remove_pages(page_zone(first_page), PHYS_PFN(res->start),
123+
__remove_pages(PHYS_PFN(res->start),
124124
PHYS_PFN(resource_size(res)), NULL);
125125
} else {
126126
arch_remove_memory(nid, res->start, resource_size(res),

0 commit comments

Comments
 (0)