]>
Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
3947be19 DH |
2 | /* |
3 | * linux/mm/memory_hotplug.c | |
4 | * | |
5 | * Copyright (C) | |
6 | */ | |
7 | ||
3947be19 DH |
8 | #include <linux/stddef.h> |
9 | #include <linux/mm.h> | |
174cd4b1 | 10 | #include <linux/sched/signal.h> |
3947be19 DH |
11 | #include <linux/swap.h> |
12 | #include <linux/interrupt.h> | |
13 | #include <linux/pagemap.h> | |
3947be19 | 14 | #include <linux/compiler.h> |
b95f1b31 | 15 | #include <linux/export.h> |
3947be19 | 16 | #include <linux/pagevec.h> |
2d1d43f6 | 17 | #include <linux/writeback.h> |
3947be19 DH |
18 | #include <linux/slab.h> |
19 | #include <linux/sysctl.h> | |
20 | #include <linux/cpu.h> | |
21 | #include <linux/memory.h> | |
4b94ffdc | 22 | #include <linux/memremap.h> |
3947be19 DH |
23 | #include <linux/memory_hotplug.h> |
24 | #include <linux/highmem.h> | |
25 | #include <linux/vmalloc.h> | |
0a547039 | 26 | #include <linux/ioport.h> |
0c0e6195 KH |
27 | #include <linux/delay.h> |
28 | #include <linux/migrate.h> | |
29 | #include <linux/page-isolation.h> | |
71088785 | 30 | #include <linux/pfn.h> |
6ad696d2 | 31 | #include <linux/suspend.h> |
6d9c285a | 32 | #include <linux/mm_inline.h> |
d96ae530 | 33 | #include <linux/firmware-map.h> |
60a5a19e | 34 | #include <linux/stop_machine.h> |
c8721bbb | 35 | #include <linux/hugetlb.h> |
c5320926 | 36 | #include <linux/memblock.h> |
698b1b30 | 37 | #include <linux/compaction.h> |
b15c8726 | 38 | #include <linux/rmap.h> |
3947be19 DH |
39 | |
40 | #include <asm/tlbflush.h> | |
41 | ||
1e5ad9a3 | 42 | #include "internal.h" |
e900a918 | 43 | #include "shuffle.h" |
1e5ad9a3 | 44 | |
9d0ad8ca DK |
45 | /* |
46 | * online_page_callback contains pointer to current page onlining function. | |
47 | * Initially it is generic_online_page(). If it is required it could be | |
48 | * changed by calling set_online_page_callback() for callback registration | |
49 | * and restore_online_page_callback() for generic callback restore. | |
50 | */ | |
51 | ||
a9cd410a | 52 | static void generic_online_page(struct page *page, unsigned int order); |
9d0ad8ca DK |
53 | |
54 | static online_page_callback_t online_page_callback = generic_online_page; | |
bfc8c901 | 55 | static DEFINE_MUTEX(online_page_callback_lock); |
9d0ad8ca | 56 | |
3f906ba2 | 57 | DEFINE_STATIC_PERCPU_RWSEM(mem_hotplug_lock); |
bfc8c901 | 58 | |
3ce42ff4 SF |
59 | static int default_kernel_zone = ZONE_NORMAL; |
60 | ||
3f906ba2 TG |
61 | void get_online_mems(void) |
62 | { | |
63 | percpu_down_read(&mem_hotplug_lock); | |
64 | } | |
bfc8c901 | 65 | |
3f906ba2 TG |
66 | void put_online_mems(void) |
67 | { | |
68 | percpu_up_read(&mem_hotplug_lock); | |
69 | } | |
bfc8c901 | 70 | |
4932381e MH |
71 | bool movable_node_enabled = false; |
72 | ||
8604d9e5 | 73 | #ifndef CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE |
31bc3858 | 74 | bool memhp_auto_online; |
8604d9e5 VK |
75 | #else |
76 | bool memhp_auto_online = true; | |
77 | #endif | |
31bc3858 VK |
78 | EXPORT_SYMBOL_GPL(memhp_auto_online); |
79 | ||
86dd995d VK |
80 | static int __init setup_memhp_default_state(char *str) |
81 | { | |
82 | if (!strcmp(str, "online")) | |
83 | memhp_auto_online = true; | |
84 | else if (!strcmp(str, "offline")) | |
85 | memhp_auto_online = false; | |
86 | ||
87 | return 1; | |
88 | } | |
89 | __setup("memhp_default_state=", setup_memhp_default_state); | |
90 | ||
30467e0b | 91 | void mem_hotplug_begin(void) |
20d6c96b | 92 | { |
3f906ba2 TG |
93 | cpus_read_lock(); |
94 | percpu_down_write(&mem_hotplug_lock); | |
20d6c96b KM |
95 | } |
96 | ||
30467e0b | 97 | void mem_hotplug_done(void) |
bfc8c901 | 98 | { |
3f906ba2 TG |
99 | percpu_up_write(&mem_hotplug_lock); |
100 | cpus_read_unlock(); | |
bfc8c901 | 101 | } |
20d6c96b | 102 | |
357b4da5 JG |
103 | u64 max_mem_size = U64_MAX; |
104 | ||
45e0b78b KM |
105 | /* add this memory to iomem resource */ |
106 | static struct resource *register_memory_resource(u64 start, u64 size) | |
107 | { | |
2794129e DH |
108 | struct resource *res; |
109 | unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY; | |
110 | char *resource_name = "System RAM"; | |
357b4da5 JG |
111 | |
112 | if (start + size > max_mem_size) | |
113 | return ERR_PTR(-E2BIG); | |
114 | ||
2794129e DH |
115 | /* |
116 | * Request ownership of the new memory range. This might be | |
117 | * a child of an existing resource that was present but | |
118 | * not marked as busy. | |
119 | */ | |
120 | res = __request_region(&iomem_resource, start, size, | |
121 | resource_name, flags); | |
122 | ||
123 | if (!res) { | |
124 | pr_debug("Unable to reserve System RAM region: %016llx->%016llx\n", | |
125 | start, start + size); | |
6f754ba4 | 126 | return ERR_PTR(-EEXIST); |
45e0b78b KM |
127 | } |
128 | return res; | |
129 | } | |
130 | ||
131 | static void release_memory_resource(struct resource *res) | |
132 | { | |
133 | if (!res) | |
134 | return; | |
135 | release_resource(res); | |
136 | kfree(res); | |
45e0b78b KM |
137 | } |
138 | ||
53947027 | 139 | #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE |
46723bfa YI |
140 | void get_page_bootmem(unsigned long info, struct page *page, |
141 | unsigned long type) | |
04753278 | 142 | { |
ddffe98d | 143 | page->freelist = (void *)type; |
04753278 YG |
144 | SetPagePrivate(page); |
145 | set_page_private(page, info); | |
fe896d18 | 146 | page_ref_inc(page); |
04753278 YG |
147 | } |
148 | ||
170a5a7e | 149 | void put_page_bootmem(struct page *page) |
04753278 | 150 | { |
5f24ce5f | 151 | unsigned long type; |
04753278 | 152 | |
ddffe98d | 153 | type = (unsigned long) page->freelist; |
5f24ce5f AA |
154 | BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE || |
155 | type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE); | |
04753278 | 156 | |
fe896d18 | 157 | if (page_ref_dec_return(page) == 1) { |
ddffe98d | 158 | page->freelist = NULL; |
04753278 YG |
159 | ClearPagePrivate(page); |
160 | set_page_private(page, 0); | |
5f24ce5f | 161 | INIT_LIST_HEAD(&page->lru); |
170a5a7e | 162 | free_reserved_page(page); |
04753278 | 163 | } |
04753278 YG |
164 | } |
165 | ||
46723bfa YI |
166 | #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE |
167 | #ifndef CONFIG_SPARSEMEM_VMEMMAP | |
d92bc318 | 168 | static void register_page_bootmem_info_section(unsigned long start_pfn) |
04753278 | 169 | { |
f1eca35a | 170 | unsigned long mapsize, section_nr, i; |
04753278 YG |
171 | struct mem_section *ms; |
172 | struct page *page, *memmap; | |
f1eca35a | 173 | struct mem_section_usage *usage; |
04753278 | 174 | |
04753278 YG |
175 | section_nr = pfn_to_section_nr(start_pfn); |
176 | ms = __nr_to_section(section_nr); | |
177 | ||
178 | /* Get section's memmap address */ | |
179 | memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr); | |
180 | ||
181 | /* | |
182 | * Get page for the memmap's phys address | |
183 | * XXX: need more consideration for sparse_vmemmap... | |
184 | */ | |
185 | page = virt_to_page(memmap); | |
186 | mapsize = sizeof(struct page) * PAGES_PER_SECTION; | |
187 | mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT; | |
188 | ||
189 | /* remember memmap's page */ | |
190 | for (i = 0; i < mapsize; i++, page++) | |
191 | get_page_bootmem(section_nr, page, SECTION_INFO); | |
192 | ||
f1eca35a DW |
193 | usage = ms->usage; |
194 | page = virt_to_page(usage); | |
04753278 | 195 | |
f1eca35a | 196 | mapsize = PAGE_ALIGN(mem_section_usage_size()) >> PAGE_SHIFT; |
04753278 YG |
197 | |
198 | for (i = 0; i < mapsize; i++, page++) | |
af370fb8 | 199 | get_page_bootmem(section_nr, page, MIX_SECTION_INFO); |
04753278 YG |
200 | |
201 | } | |
46723bfa YI |
202 | #else /* CONFIG_SPARSEMEM_VMEMMAP */ |
203 | static void register_page_bootmem_info_section(unsigned long start_pfn) | |
204 | { | |
f1eca35a | 205 | unsigned long mapsize, section_nr, i; |
46723bfa YI |
206 | struct mem_section *ms; |
207 | struct page *page, *memmap; | |
f1eca35a | 208 | struct mem_section_usage *usage; |
46723bfa | 209 | |
46723bfa YI |
210 | section_nr = pfn_to_section_nr(start_pfn); |
211 | ms = __nr_to_section(section_nr); | |
212 | ||
213 | memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr); | |
214 | ||
215 | register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION); | |
216 | ||
f1eca35a DW |
217 | usage = ms->usage; |
218 | page = virt_to_page(usage); | |
46723bfa | 219 | |
f1eca35a | 220 | mapsize = PAGE_ALIGN(mem_section_usage_size()) >> PAGE_SHIFT; |
46723bfa YI |
221 | |
222 | for (i = 0; i < mapsize; i++, page++) | |
223 | get_page_bootmem(section_nr, page, MIX_SECTION_INFO); | |
224 | } | |
225 | #endif /* !CONFIG_SPARSEMEM_VMEMMAP */ | |
04753278 | 226 | |
7ded384a | 227 | void __init register_page_bootmem_info_node(struct pglist_data *pgdat) |
04753278 YG |
228 | { |
229 | unsigned long i, pfn, end_pfn, nr_pages; | |
230 | int node = pgdat->node_id; | |
231 | struct page *page; | |
04753278 YG |
232 | |
233 | nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT; | |
234 | page = virt_to_page(pgdat); | |
235 | ||
236 | for (i = 0; i < nr_pages; i++, page++) | |
237 | get_page_bootmem(node, page, NODE_INFO); | |
238 | ||
04753278 | 239 | pfn = pgdat->node_start_pfn; |
c1f19495 | 240 | end_pfn = pgdat_end_pfn(pgdat); |
04753278 | 241 | |
7e9f5eb0 | 242 | /* register section info */ |
f14851af | 243 | for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) { |
244 | /* | |
245 | * Some platforms can assign the same pfn to multiple nodes - on | |
246 | * node0 as well as nodeN. To avoid registering a pfn against | |
247 | * multiple nodes we check that this pfn does not already | |
7e9f5eb0 | 248 | * reside in some other nodes. |
f14851af | 249 | */ |
f65e91df | 250 | if (pfn_valid(pfn) && (early_pfn_to_nid(pfn) == node)) |
f14851af | 251 | register_page_bootmem_info_section(pfn); |
252 | } | |
04753278 | 253 | } |
46723bfa | 254 | #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */ |
04753278 | 255 | |
7ea62160 DW |
256 | static int check_pfn_span(unsigned long pfn, unsigned long nr_pages, |
257 | const char *reason) | |
258 | { | |
259 | /* | |
260 | * Disallow all operations smaller than a sub-section and only | |
261 | * allow operations smaller than a section for | |
262 | * SPARSEMEM_VMEMMAP. Note that check_hotplug_memory_range() | |
263 | * enforces a larger memory_block_size_bytes() granularity for | |
264 | * memory that will be marked online, so this check should only | |
265 | * fire for direct arch_{add,remove}_memory() users outside of | |
266 | * add_memory_resource(). | |
267 | */ | |
268 | unsigned long min_align; | |
269 | ||
270 | if (IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP)) | |
271 | min_align = PAGES_PER_SUBSECTION; | |
272 | else | |
273 | min_align = PAGES_PER_SECTION; | |
274 | if (!IS_ALIGNED(pfn, min_align) | |
275 | || !IS_ALIGNED(nr_pages, min_align)) { | |
276 | WARN(1, "Misaligned __%s_pages start: %#lx end: #%lx\n", | |
277 | reason, pfn, pfn + nr_pages - 1); | |
278 | return -EINVAL; | |
279 | } | |
280 | return 0; | |
281 | } | |
282 | ||
4edd7cef DR |
283 | /* |
284 | * Reasonably generic function for adding memory. It is | |
285 | * expected that archs that support memory hotplug will | |
286 | * call this function after deciding the zone to which to | |
287 | * add the new pages. | |
288 | */ | |
7ea62160 DW |
289 | int __ref __add_pages(int nid, unsigned long pfn, unsigned long nr_pages, |
290 | struct mhp_restrictions *restrictions) | |
4edd7cef | 291 | { |
9a845030 DW |
292 | int err; |
293 | unsigned long nr, start_sec, end_sec; | |
940519f0 | 294 | struct vmem_altmap *altmap = restrictions->altmap; |
4b94ffdc | 295 | |
4b94ffdc DW |
296 | if (altmap) { |
297 | /* | |
298 | * Validate altmap is within bounds of the total request | |
299 | */ | |
7ea62160 | 300 | if (altmap->base_pfn != pfn |
4b94ffdc DW |
301 | || vmem_altmap_offset(altmap) > nr_pages) { |
302 | pr_warn_once("memory add fail, invalid altmap\n"); | |
7ea62160 | 303 | return -EINVAL; |
4b94ffdc DW |
304 | } |
305 | altmap->alloc = 0; | |
306 | } | |
307 | ||
7ea62160 DW |
308 | err = check_pfn_span(pfn, nr_pages, "add"); |
309 | if (err) | |
310 | return err; | |
311 | ||
312 | start_sec = pfn_to_section_nr(pfn); | |
313 | end_sec = pfn_to_section_nr(pfn + nr_pages - 1); | |
9a845030 | 314 | for (nr = start_sec; nr <= end_sec; nr++) { |
7ea62160 DW |
315 | unsigned long pfns; |
316 | ||
317 | pfns = min(nr_pages, PAGES_PER_SECTION | |
318 | - (pfn & ~PAGE_SECTION_MASK)); | |
ba72b4c8 DW |
319 | err = sparse_add_section(nid, pfn, pfns, altmap); |
320 | if (err) | |
321 | break; | |
7ea62160 DW |
322 | pfn += pfns; |
323 | nr_pages -= pfns; | |
f64ac5e6 | 324 | cond_resched(); |
4edd7cef | 325 | } |
c435a390 | 326 | vmemmap_populate_print_last(); |
4edd7cef DR |
327 | return err; |
328 | } | |
4edd7cef | 329 | |
815121d2 | 330 | /* find the smallest valid pfn in the range [start_pfn, end_pfn) */ |
d09b0137 | 331 | static unsigned long find_smallest_section_pfn(int nid, struct zone *zone, |
815121d2 YI |
332 | unsigned long start_pfn, |
333 | unsigned long end_pfn) | |
334 | { | |
49ba3c6b | 335 | for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SUBSECTION) { |
7ce700bf | 336 | if (unlikely(!pfn_to_online_page(start_pfn))) |
815121d2 YI |
337 | continue; |
338 | ||
339 | if (unlikely(pfn_to_nid(start_pfn) != nid)) | |
340 | continue; | |
341 | ||
342 | if (zone && zone != page_zone(pfn_to_page(start_pfn))) | |
343 | continue; | |
344 | ||
345 | return start_pfn; | |
346 | } | |
347 | ||
348 | return 0; | |
349 | } | |
350 | ||
351 | /* find the biggest valid pfn in the range [start_pfn, end_pfn). */ | |
d09b0137 | 352 | static unsigned long find_biggest_section_pfn(int nid, struct zone *zone, |
815121d2 YI |
353 | unsigned long start_pfn, |
354 | unsigned long end_pfn) | |
355 | { | |
815121d2 YI |
356 | unsigned long pfn; |
357 | ||
358 | /* pfn is the end pfn of a memory section. */ | |
359 | pfn = end_pfn - 1; | |
49ba3c6b | 360 | for (; pfn >= start_pfn; pfn -= PAGES_PER_SUBSECTION) { |
7ce700bf | 361 | if (unlikely(!pfn_to_online_page(pfn))) |
815121d2 YI |
362 | continue; |
363 | ||
364 | if (unlikely(pfn_to_nid(pfn) != nid)) | |
365 | continue; | |
366 | ||
367 | if (zone && zone != page_zone(pfn_to_page(pfn))) | |
368 | continue; | |
369 | ||
370 | return pfn; | |
371 | } | |
372 | ||
373 | return 0; | |
374 | } | |
375 | ||
376 | static void shrink_zone_span(struct zone *zone, unsigned long start_pfn, | |
377 | unsigned long end_pfn) | |
378 | { | |
c33bc315 XQ |
379 | unsigned long zone_start_pfn = zone->zone_start_pfn; |
380 | unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */ | |
381 | unsigned long zone_end_pfn = z; | |
815121d2 | 382 | unsigned long pfn; |
815121d2 YI |
383 | int nid = zone_to_nid(zone); |
384 | ||
385 | zone_span_writelock(zone); | |
386 | if (zone_start_pfn == start_pfn) { | |
387 | /* | |
388 | * If the section is smallest section in the zone, it need | |
389 | * shrink zone->zone_start_pfn and zone->zone_spanned_pages. | |
390 | * In this case, we find second smallest valid mem_section | |
391 | * for shrinking zone. | |
392 | */ | |
393 | pfn = find_smallest_section_pfn(nid, zone, end_pfn, | |
394 | zone_end_pfn); | |
395 | if (pfn) { | |
396 | zone->zone_start_pfn = pfn; | |
397 | zone->spanned_pages = zone_end_pfn - pfn; | |
398 | } | |
399 | } else if (zone_end_pfn == end_pfn) { | |
400 | /* | |
401 | * If the section is biggest section in the zone, it need | |
402 | * shrink zone->spanned_pages. | |
403 | * In this case, we find second biggest valid mem_section for | |
404 | * shrinking zone. | |
405 | */ | |
406 | pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn, | |
407 | start_pfn); | |
408 | if (pfn) | |
409 | zone->spanned_pages = pfn - zone_start_pfn + 1; | |
410 | } | |
411 | ||
412 | /* | |
413 | * The section is not biggest or smallest mem_section in the zone, it | |
414 | * only creates a hole in the zone. So in this case, we need not | |
415 | * change the zone. But perhaps, the zone has only hole data. Thus | |
416 | * it check the zone has only hole or not. | |
417 | */ | |
418 | pfn = zone_start_pfn; | |
49ba3c6b | 419 | for (; pfn < zone_end_pfn; pfn += PAGES_PER_SUBSECTION) { |
7ce700bf | 420 | if (unlikely(!pfn_to_online_page(pfn))) |
815121d2 YI |
421 | continue; |
422 | ||
423 | if (page_zone(pfn_to_page(pfn)) != zone) | |
424 | continue; | |
425 | ||
49ba3c6b DW |
426 | /* Skip range to be removed */ |
427 | if (pfn >= start_pfn && pfn < end_pfn) | |
815121d2 YI |
428 | continue; |
429 | ||
430 | /* If we find valid section, we have nothing to do */ | |
431 | zone_span_writeunlock(zone); | |
432 | return; | |
433 | } | |
434 | ||
435 | /* The zone has no valid section */ | |
436 | zone->zone_start_pfn = 0; | |
437 | zone->spanned_pages = 0; | |
438 | zone_span_writeunlock(zone); | |
439 | } | |
440 | ||
00d6c019 | 441 | static void update_pgdat_span(struct pglist_data *pgdat) |
815121d2 | 442 | { |
00d6c019 DH |
443 | unsigned long node_start_pfn = 0, node_end_pfn = 0; |
444 | struct zone *zone; | |
445 | ||
446 | for (zone = pgdat->node_zones; | |
447 | zone < pgdat->node_zones + MAX_NR_ZONES; zone++) { | |
448 | unsigned long zone_end_pfn = zone->zone_start_pfn + | |
449 | zone->spanned_pages; | |
450 | ||
451 | /* No need to lock the zones, they can't change. */ | |
656d5711 DH |
452 | if (!zone->spanned_pages) |
453 | continue; | |
454 | if (!node_end_pfn) { | |
455 | node_start_pfn = zone->zone_start_pfn; | |
456 | node_end_pfn = zone_end_pfn; | |
457 | continue; | |
458 | } | |
459 | ||
00d6c019 DH |
460 | if (zone_end_pfn > node_end_pfn) |
461 | node_end_pfn = zone_end_pfn; | |
462 | if (zone->zone_start_pfn < node_start_pfn) | |
463 | node_start_pfn = zone->zone_start_pfn; | |
815121d2 YI |
464 | } |
465 | ||
00d6c019 DH |
466 | pgdat->node_start_pfn = node_start_pfn; |
467 | pgdat->node_spanned_pages = node_end_pfn - node_start_pfn; | |
815121d2 YI |
468 | } |
469 | ||
3b14ccf2 DH |
470 | void __ref remove_pfn_range_from_zone(struct zone *zone, |
471 | unsigned long start_pfn, | |
472 | unsigned long nr_pages) | |
815121d2 YI |
473 | { |
474 | struct pglist_data *pgdat = zone->zone_pgdat; | |
815121d2 YI |
475 | unsigned long flags; |
476 | ||
7ce700bf DH |
477 | #ifdef CONFIG_ZONE_DEVICE |
478 | /* | |
479 | * Zone shrinking code cannot properly deal with ZONE_DEVICE. So | |
480 | * we will not try to shrink the zones - which is okay as | |
481 | * set_zone_contiguous() cannot deal with ZONE_DEVICE either way. | |
482 | */ | |
483 | if (zone_idx(zone) == ZONE_DEVICE) | |
484 | return; | |
485 | #endif | |
486 | ||
3b14ccf2 DH |
487 | clear_zone_contiguous(zone); |
488 | ||
815121d2 YI |
489 | pgdat_resize_lock(zone->zone_pgdat, &flags); |
490 | shrink_zone_span(zone, start_pfn, start_pfn + nr_pages); | |
00d6c019 | 491 | update_pgdat_span(pgdat); |
815121d2 | 492 | pgdat_resize_unlock(zone->zone_pgdat, &flags); |
3b14ccf2 DH |
493 | |
494 | set_zone_contiguous(zone); | |
815121d2 YI |
495 | } |
496 | ||
3b14ccf2 DH |
497 | static void __remove_section(unsigned long pfn, unsigned long nr_pages, |
498 | unsigned long map_offset, | |
499 | struct vmem_altmap *altmap) | |
ea01ea93 | 500 | { |
7ea62160 | 501 | struct mem_section *ms = __nr_to_section(pfn_to_section_nr(pfn)); |
ea01ea93 | 502 | |
9d1d887d DH |
503 | if (WARN_ON_ONCE(!valid_section(ms))) |
504 | return; | |
ea01ea93 | 505 | |
ba72b4c8 | 506 | sparse_remove_section(ms, pfn, nr_pages, map_offset, altmap); |
ea01ea93 BP |
507 | } |
508 | ||
ea01ea93 | 509 | /** |
3b14ccf2 | 510 | * __remove_pages() - remove sections of pages |
7ea62160 | 511 | * @pfn: starting pageframe (must be aligned to start of a section) |
ea01ea93 | 512 | * @nr_pages: number of pages to remove (must be multiple of section size) |
e8b098fc | 513 | * @altmap: alternative device page map or %NULL if default memmap is used |
ea01ea93 BP |
514 | * |
515 | * Generic helper function to remove section mappings and sysfs entries | |
516 | * for the section of the memory we are removing. Caller needs to make | |
517 | * sure that pages are marked reserved and zones are adjust properly by | |
518 | * calling offline_pages(). | |
519 | */ | |
3b14ccf2 DH |
520 | void __remove_pages(unsigned long pfn, unsigned long nr_pages, |
521 | struct vmem_altmap *altmap) | |
ea01ea93 | 522 | { |
4b94ffdc | 523 | unsigned long map_offset = 0; |
9a845030 | 524 | unsigned long nr, start_sec, end_sec; |
4b94ffdc | 525 | |
96da4350 | 526 | map_offset = vmem_altmap_offset(altmap); |
ea01ea93 | 527 | |
7ea62160 DW |
528 | if (check_pfn_span(pfn, nr_pages, "remove")) |
529 | return; | |
ea01ea93 | 530 | |
7ea62160 DW |
531 | start_sec = pfn_to_section_nr(pfn); |
532 | end_sec = pfn_to_section_nr(pfn + nr_pages - 1); | |
9a845030 | 533 | for (nr = start_sec; nr <= end_sec; nr++) { |
7ea62160 | 534 | unsigned long pfns; |
4b94ffdc | 535 | |
dd33ad7b | 536 | cond_resched(); |
7ea62160 DW |
537 | pfns = min(nr_pages, PAGES_PER_SECTION |
538 | - (pfn & ~PAGE_SECTION_MASK)); | |
3b14ccf2 | 539 | __remove_section(pfn, pfns, map_offset, altmap); |
7ea62160 DW |
540 | pfn += pfns; |
541 | nr_pages -= pfns; | |
4b94ffdc | 542 | map_offset = 0; |
ea01ea93 | 543 | } |
ea01ea93 | 544 | } |
ea01ea93 | 545 | |
9d0ad8ca DK |
546 | int set_online_page_callback(online_page_callback_t callback) |
547 | { | |
548 | int rc = -EINVAL; | |
549 | ||
bfc8c901 VD |
550 | get_online_mems(); |
551 | mutex_lock(&online_page_callback_lock); | |
9d0ad8ca DK |
552 | |
553 | if (online_page_callback == generic_online_page) { | |
554 | online_page_callback = callback; | |
555 | rc = 0; | |
556 | } | |
557 | ||
bfc8c901 VD |
558 | mutex_unlock(&online_page_callback_lock); |
559 | put_online_mems(); | |
9d0ad8ca DK |
560 | |
561 | return rc; | |
562 | } | |
563 | EXPORT_SYMBOL_GPL(set_online_page_callback); | |
564 | ||
565 | int restore_online_page_callback(online_page_callback_t callback) | |
566 | { | |
567 | int rc = -EINVAL; | |
568 | ||
bfc8c901 VD |
569 | get_online_mems(); |
570 | mutex_lock(&online_page_callback_lock); | |
9d0ad8ca DK |
571 | |
572 | if (online_page_callback == callback) { | |
573 | online_page_callback = generic_online_page; | |
574 | rc = 0; | |
575 | } | |
576 | ||
bfc8c901 VD |
577 | mutex_unlock(&online_page_callback_lock); |
578 | put_online_mems(); | |
9d0ad8ca DK |
579 | |
580 | return rc; | |
581 | } | |
582 | EXPORT_SYMBOL_GPL(restore_online_page_callback); | |
583 | ||
584 | void __online_page_set_limits(struct page *page) | |
180c06ef | 585 | { |
9d0ad8ca DK |
586 | } |
587 | EXPORT_SYMBOL_GPL(__online_page_set_limits); | |
588 | ||
589 | void __online_page_increment_counters(struct page *page) | |
590 | { | |
3dcc0571 | 591 | adjust_managed_page_count(page, 1); |
9d0ad8ca DK |
592 | } |
593 | EXPORT_SYMBOL_GPL(__online_page_increment_counters); | |
180c06ef | 594 | |
9d0ad8ca DK |
595 | void __online_page_free(struct page *page) |
596 | { | |
3dcc0571 | 597 | __free_reserved_page(page); |
180c06ef | 598 | } |
9d0ad8ca DK |
599 | EXPORT_SYMBOL_GPL(__online_page_free); |
600 | ||
a9cd410a | 601 | static void generic_online_page(struct page *page, unsigned int order) |
9d0ad8ca | 602 | { |
c4367f32 VB |
603 | /* |
604 | * Freeing the page with debug_pagealloc enabled will try to unmap it, | |
605 | * so we should map it first. This is better than introducing a special | |
606 | * case in page freeing fast path. | |
607 | */ | |
608 | if (debug_pagealloc_enabled_static()) | |
609 | kernel_map_pages(page, 1 << order, 1); | |
a9cd410a AK |
610 | __free_pages_core(page, order); |
611 | totalram_pages_add(1UL << order); | |
612 | #ifdef CONFIG_HIGHMEM | |
613 | if (PageHighMem(page)) | |
614 | totalhigh_pages_add(1UL << order); | |
615 | #endif | |
616 | } | |
617 | ||
75884fb1 KH |
618 | static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages, |
619 | void *arg) | |
3947be19 | 620 | { |
b2c2ab20 DH |
621 | const unsigned long end_pfn = start_pfn + nr_pages; |
622 | unsigned long pfn; | |
623 | int order; | |
624 | ||
625 | /* | |
626 | * Online the pages. The callback might decide to keep some pages | |
627 | * PG_reserved (to add them to the buddy later), but we still account | |
628 | * them as being online/belonging to this zone ("present"). | |
629 | */ | |
630 | for (pfn = start_pfn; pfn < end_pfn; pfn += 1ul << order) { | |
631 | order = min(MAX_ORDER - 1, get_order(PFN_PHYS(end_pfn - pfn))); | |
bd02cc01 DH |
632 | /* __free_pages_core() wants pfns to be aligned to the order */ |
633 | if (WARN_ON_ONCE(!IS_ALIGNED(pfn, 1ul << order))) | |
634 | order = 0; | |
b2c2ab20 DH |
635 | (*online_page_callback)(pfn_to_page(pfn), order); |
636 | } | |
2d070eab | 637 | |
b2c2ab20 DH |
638 | /* mark all involved sections as online */ |
639 | online_mem_sections(start_pfn, end_pfn); | |
2d070eab | 640 | |
b2c2ab20 | 641 | *(unsigned long *)arg += nr_pages; |
75884fb1 KH |
642 | return 0; |
643 | } | |
644 | ||
d9713679 LJ |
645 | /* check which state of node_states will be changed when online memory */ |
646 | static void node_states_check_changes_online(unsigned long nr_pages, | |
647 | struct zone *zone, struct memory_notify *arg) | |
648 | { | |
649 | int nid = zone_to_nid(zone); | |
d9713679 | 650 | |
98fa15f3 AK |
651 | arg->status_change_nid = NUMA_NO_NODE; |
652 | arg->status_change_nid_normal = NUMA_NO_NODE; | |
653 | arg->status_change_nid_high = NUMA_NO_NODE; | |
d9713679 | 654 | |
8efe33f4 OS |
655 | if (!node_state(nid, N_MEMORY)) |
656 | arg->status_change_nid = nid; | |
657 | if (zone_idx(zone) <= ZONE_NORMAL && !node_state(nid, N_NORMAL_MEMORY)) | |
d9713679 | 658 | arg->status_change_nid_normal = nid; |
6715ddf9 | 659 | #ifdef CONFIG_HIGHMEM |
d3ba3ae1 | 660 | if (zone_idx(zone) <= ZONE_HIGHMEM && !node_state(nid, N_HIGH_MEMORY)) |
6715ddf9 | 661 | arg->status_change_nid_high = nid; |
6715ddf9 | 662 | #endif |
d9713679 LJ |
663 | } |
664 | ||
665 | static void node_states_set_node(int node, struct memory_notify *arg) | |
666 | { | |
667 | if (arg->status_change_nid_normal >= 0) | |
668 | node_set_state(node, N_NORMAL_MEMORY); | |
669 | ||
6715ddf9 LJ |
670 | if (arg->status_change_nid_high >= 0) |
671 | node_set_state(node, N_HIGH_MEMORY); | |
672 | ||
83d83612 OS |
673 | if (arg->status_change_nid >= 0) |
674 | node_set_state(node, N_MEMORY); | |
d9713679 LJ |
675 | } |
676 | ||
f1dd2cd1 MH |
677 | static void __meminit resize_zone_range(struct zone *zone, unsigned long start_pfn, |
678 | unsigned long nr_pages) | |
679 | { | |
680 | unsigned long old_end_pfn = zone_end_pfn(zone); | |
681 | ||
682 | if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn) | |
683 | zone->zone_start_pfn = start_pfn; | |
684 | ||
685 | zone->spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - zone->zone_start_pfn; | |
686 | } | |
687 | ||
688 | static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned long start_pfn, | |
689 | unsigned long nr_pages) | |
690 | { | |
691 | unsigned long old_end_pfn = pgdat_end_pfn(pgdat); | |
692 | ||
693 | if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn) | |
694 | pgdat->node_start_pfn = start_pfn; | |
695 | ||
696 | pgdat->node_spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - pgdat->node_start_pfn; | |
f1dd2cd1 | 697 | |
3fccb74c DH |
698 | } |
699 | /* | |
700 | * Associate the pfn range with the given zone, initializing the memmaps | |
701 | * and resizing the pgdat/zone data to span the added pages. After this | |
702 | * call, all affected pages are PG_reserved. | |
703 | */ | |
a99583e7 CH |
704 | void __ref move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn, |
705 | unsigned long nr_pages, struct vmem_altmap *altmap) | |
f1dd2cd1 MH |
706 | { |
707 | struct pglist_data *pgdat = zone->zone_pgdat; | |
708 | int nid = pgdat->node_id; | |
709 | unsigned long flags; | |
df429ac0 | 710 | |
f1dd2cd1 MH |
711 | clear_zone_contiguous(zone); |
712 | ||
713 | /* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */ | |
714 | pgdat_resize_lock(pgdat, &flags); | |
715 | zone_span_writelock(zone); | |
fa004ab7 WY |
716 | if (zone_is_empty(zone)) |
717 | init_currently_empty_zone(zone, start_pfn, nr_pages); | |
f1dd2cd1 MH |
718 | resize_zone_range(zone, start_pfn, nr_pages); |
719 | zone_span_writeunlock(zone); | |
720 | resize_pgdat_range(pgdat, start_pfn, nr_pages); | |
721 | pgdat_resize_unlock(pgdat, &flags); | |
722 | ||
723 | /* | |
724 | * TODO now we have a visible range of pages which are not associated | |
725 | * with their zone properly. Not nice but set_pfnblock_flags_mask | |
726 | * expects the zone spans the pfn range. All the pages in the range | |
727 | * are reserved so nobody should be touching them so we should be safe | |
728 | */ | |
a99583e7 | 729 | memmap_init_zone(nr_pages, nid, zone_idx(zone), start_pfn, |
67b3c2d3 | 730 | MEMINIT_HOTPLUG, altmap); |
f1dd2cd1 MH |
731 | |
732 | set_zone_contiguous(zone); | |
733 | } | |
734 | ||
3ce42ff4 SF |
735 | void set_default_mem_hotplug_zone(enum zone_type zone) |
736 | { | |
737 | default_kernel_zone = zone; | |
738 | } | |
739 | ||
740 | #ifdef CONFIG_HIGHMEM | |
741 | #define MAX_KERNEL_ZONE ZONE_HIGHMEM | |
742 | #else | |
743 | #define MAX_KERNEL_ZONE ZONE_NORMAL | |
744 | #endif | |
745 | ||
c246a213 MH |
746 | /* |
747 | * Returns a default kernel memory zone for the given pfn range. | |
748 | * If no kernel zone covers this pfn range it will automatically go | |
3ce42ff4 | 749 | * to the MAX_KERNEL_ZONE. |
c246a213 | 750 | */ |
c6f03e29 | 751 | static struct zone *default_kernel_zone_for_pfn(int nid, unsigned long start_pfn, |
c246a213 MH |
752 | unsigned long nr_pages) |
753 | { | |
754 | struct pglist_data *pgdat = NODE_DATA(nid); | |
755 | int zid; | |
756 | ||
3ce42ff4 | 757 | for (zid = 0; zid <= MAX_KERNEL_ZONE; zid++) { |
c246a213 MH |
758 | struct zone *zone = &pgdat->node_zones[zid]; |
759 | ||
760 | if (zone_intersects(zone, start_pfn, nr_pages)) | |
761 | return zone; | |
762 | } | |
763 | ||
3ce42ff4 | 764 | return &pgdat->node_zones[default_kernel_zone]; |
c246a213 MH |
765 | } |
766 | ||
c6f03e29 MH |
767 | static inline struct zone *default_zone_for_pfn(int nid, unsigned long start_pfn, |
768 | unsigned long nr_pages) | |
e5e68930 | 769 | { |
c6f03e29 MH |
770 | struct zone *kernel_zone = default_kernel_zone_for_pfn(nid, start_pfn, |
771 | nr_pages); | |
772 | struct zone *movable_zone = &NODE_DATA(nid)->node_zones[ZONE_MOVABLE]; | |
773 | bool in_kernel = zone_intersects(kernel_zone, start_pfn, nr_pages); | |
774 | bool in_movable = zone_intersects(movable_zone, start_pfn, nr_pages); | |
e5e68930 MH |
775 | |
776 | /* | |
c6f03e29 MH |
777 | * We inherit the existing zone in a simple case where zones do not |
778 | * overlap in the given range | |
e5e68930 | 779 | */ |
c6f03e29 MH |
780 | if (in_kernel ^ in_movable) |
781 | return (in_kernel) ? kernel_zone : movable_zone; | |
9f123ab5 | 782 | |
c6f03e29 MH |
783 | /* |
784 | * If the range doesn't belong to any zone or two zones overlap in the | |
785 | * given range then we use movable zone only if movable_node is | |
786 | * enabled because we always online to a kernel zone by default. | |
787 | */ | |
788 | return movable_node_enabled ? movable_zone : kernel_zone; | |
9f123ab5 MH |
789 | } |
790 | ||
6135dfcb DH |
791 | struct zone *zone_for_pfn_range(int online_type, int nid, |
792 | unsigned long start_pfn, unsigned long nr_pages) | |
f1dd2cd1 | 793 | { |
c6f03e29 MH |
794 | if (online_type == MMOP_ONLINE_KERNEL) |
795 | return default_kernel_zone_for_pfn(nid, start_pfn, nr_pages); | |
f1dd2cd1 | 796 | |
c6f03e29 MH |
797 | if (online_type == MMOP_ONLINE_MOVABLE) |
798 | return &NODE_DATA(nid)->node_zones[ZONE_MOVABLE]; | |
df429ac0 | 799 | |
c6f03e29 | 800 | return default_zone_for_pfn(nid, start_pfn, nr_pages); |
e5e68930 MH |
801 | } |
802 | ||
511c2aba | 803 | int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type) |
75884fb1 | 804 | { |
aa47228a | 805 | unsigned long flags; |
3947be19 DH |
806 | unsigned long onlined_pages = 0; |
807 | struct zone *zone; | |
6811378e | 808 | int need_zonelists_rebuild = 0; |
7b78d335 YG |
809 | int nid; |
810 | int ret; | |
811 | struct memory_notify arg; | |
d0dc12e8 PT |
812 | struct memory_block *mem; |
813 | ||
381eab4a DH |
814 | mem_hotplug_begin(); |
815 | ||
d0dc12e8 PT |
816 | /* |
817 | * We can't use pfn_to_nid() because nid might be stored in struct page | |
818 | * which is not yet initialized. Instead, we find nid from memory block. | |
819 | */ | |
820 | mem = find_memory_block(__pfn_to_section(pfn)); | |
821 | nid = mem->nid; | |
89c02e69 | 822 | put_device(&mem->dev); |
7b78d335 | 823 | |
f1dd2cd1 | 824 | /* associate pfn range with the zone */ |
3fccb74c DH |
825 | zone = zone_for_pfn_range(online_type, nid, pfn, nr_pages); |
826 | move_pfn_range_to_zone(zone, pfn, nr_pages, NULL); | |
f1dd2cd1 | 827 | |
7b78d335 YG |
828 | arg.start_pfn = pfn; |
829 | arg.nr_pages = nr_pages; | |
d9713679 | 830 | node_states_check_changes_online(nr_pages, zone, &arg); |
7b78d335 | 831 | |
7b78d335 YG |
832 | ret = memory_notify(MEM_GOING_ONLINE, &arg); |
833 | ret = notifier_to_errno(ret); | |
e33e33b4 CY |
834 | if (ret) |
835 | goto failed_addition; | |
836 | ||
6811378e YG |
837 | /* |
838 | * If this zone is not populated, then it is not in zonelist. | |
839 | * This means the page allocator ignores this zone. | |
840 | * So, zonelist must be updated after online. | |
841 | */ | |
6dcd73d7 | 842 | if (!populated_zone(zone)) { |
6811378e | 843 | need_zonelists_rebuild = 1; |
72675e13 | 844 | setup_zone_pageset(zone); |
6dcd73d7 | 845 | } |
6811378e | 846 | |
908eedc6 | 847 | ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages, |
75884fb1 | 848 | online_pages_range); |
fd8a4221 | 849 | if (ret) { |
ca9a46f8 | 850 | /* not a single memory resource was applicable */ |
6dcd73d7 WC |
851 | if (need_zonelists_rebuild) |
852 | zone_pcp_reset(zone); | |
e33e33b4 | 853 | goto failed_addition; |
fd8a4221 GL |
854 | } |
855 | ||
3947be19 | 856 | zone->present_pages += onlined_pages; |
aa47228a CS |
857 | |
858 | pgdat_resize_lock(zone->zone_pgdat, &flags); | |
f2937be5 | 859 | zone->zone_pgdat->node_present_pages += onlined_pages; |
aa47228a CS |
860 | pgdat_resize_unlock(zone->zone_pgdat, &flags); |
861 | ||
e900a918 DW |
862 | shuffle_zone(zone); |
863 | ||
ca9a46f8 DH |
864 | node_states_set_node(nid, &arg); |
865 | if (need_zonelists_rebuild) | |
866 | build_all_zonelists(NULL); | |
867 | else | |
868 | zone_pcp_update(zone); | |
3947be19 | 869 | |
1b79acc9 KM |
870 | init_per_zone_wmark_min(); |
871 | ||
ca9a46f8 DH |
872 | kswapd_run(nid); |
873 | kcompactd_run(nid); | |
61b13993 | 874 | |
1f522509 | 875 | vm_total_pages = nr_free_pagecache_pages(); |
2f7f24ec | 876 | |
2d1d43f6 | 877 | writeback_set_ratelimit(); |
7b78d335 | 878 | |
ca9a46f8 | 879 | memory_notify(MEM_ONLINE, &arg); |
381eab4a | 880 | mem_hotplug_done(); |
30467e0b | 881 | return 0; |
e33e33b4 CY |
882 | |
883 | failed_addition: | |
884 | pr_debug("online_pages [mem %#010llx-%#010llx] failed\n", | |
885 | (unsigned long long) pfn << PAGE_SHIFT, | |
886 | (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1); | |
887 | memory_notify(MEM_CANCEL_ONLINE, &arg); | |
3b14ccf2 | 888 | remove_pfn_range_from_zone(zone, pfn, nr_pages); |
381eab4a | 889 | mem_hotplug_done(); |
e33e33b4 | 890 | return ret; |
3947be19 | 891 | } |
53947027 | 892 | #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */ |
bc02af93 | 893 | |
0bd85420 TC |
894 | static void reset_node_present_pages(pg_data_t *pgdat) |
895 | { | |
896 | struct zone *z; | |
897 | ||
898 | for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++) | |
899 | z->present_pages = 0; | |
900 | ||
901 | pgdat->node_present_pages = 0; | |
902 | } | |
903 | ||
e1319331 HS |
904 | /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */ |
905 | static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start) | |
9af3c2de YG |
906 | { |
907 | struct pglist_data *pgdat; | |
c8e861a5 | 908 | unsigned long start_pfn = PFN_DOWN(start); |
9af3c2de | 909 | |
a1e565aa TC |
910 | pgdat = NODE_DATA(nid); |
911 | if (!pgdat) { | |
912 | pgdat = arch_alloc_nodedata(nid); | |
913 | if (!pgdat) | |
914 | return NULL; | |
9af3c2de | 915 | |
33fce011 WY |
916 | pgdat->per_cpu_nodestats = |
917 | alloc_percpu(struct per_cpu_nodestat); | |
a1e565aa | 918 | arch_refresh_nodedata(nid, pgdat); |
b0dc3a34 | 919 | } else { |
33fce011 | 920 | int cpu; |
e716f2eb MG |
921 | /* |
922 | * Reset the nr_zones, order and classzone_idx before reuse. | |
923 | * Note that kswapd will init kswapd_classzone_idx properly | |
924 | * when it starts in the near future. | |
925 | */ | |
b0dc3a34 | 926 | pgdat->nr_zones = 0; |
38087d9b MG |
927 | pgdat->kswapd_order = 0; |
928 | pgdat->kswapd_classzone_idx = 0; | |
33fce011 WY |
929 | for_each_online_cpu(cpu) { |
930 | struct per_cpu_nodestat *p; | |
931 | ||
932 | p = per_cpu_ptr(pgdat->per_cpu_nodestats, cpu); | |
933 | memset(p, 0, sizeof(*p)); | |
934 | } | |
a1e565aa | 935 | } |
9af3c2de YG |
936 | |
937 | /* we can use NODE_DATA(nid) from here */ | |
938 | ||
03e85f9d OS |
939 | pgdat->node_id = nid; |
940 | pgdat->node_start_pfn = start_pfn; | |
941 | ||
9af3c2de | 942 | /* init node's zones as empty zones, we don't have any present pages.*/ |
03e85f9d | 943 | free_area_init_core_hotplug(nid); |
9af3c2de | 944 | |
959ecc48 KH |
945 | /* |
946 | * The node we allocated has no zone fallback lists. For avoiding | |
947 | * to access not-initialized zonelist, build here. | |
948 | */ | |
72675e13 | 949 | build_all_zonelists(pgdat); |
959ecc48 | 950 | |
0bd85420 TC |
951 | /* |
952 | * When memory is hot-added, all the memory is in offline state. So | |
953 | * clear all zones' present_pages because they will be updated in | |
954 | * online_pages() and offline_pages(). | |
955 | */ | |
03e85f9d | 956 | reset_node_managed_pages(pgdat); |
0bd85420 TC |
957 | reset_node_present_pages(pgdat); |
958 | ||
9af3c2de YG |
959 | return pgdat; |
960 | } | |
961 | ||
b9ff0360 | 962 | static void rollback_node_hotadd(int nid) |
9af3c2de | 963 | { |
b9ff0360 OS |
964 | pg_data_t *pgdat = NODE_DATA(nid); |
965 | ||
9af3c2de | 966 | arch_refresh_nodedata(nid, NULL); |
5830169f | 967 | free_percpu(pgdat->per_cpu_nodestats); |
9af3c2de | 968 | arch_free_nodedata(pgdat); |
9af3c2de YG |
969 | } |
970 | ||
0a547039 | 971 | |
01b0f197 TK |
972 | /** |
973 | * try_online_node - online a node if offlined | |
e8b098fc | 974 | * @nid: the node ID |
b9ff0360 OS |
975 | * @start: start addr of the node |
976 | * @set_node_online: Whether we want to online the node | |
cf23422b | 977 | * called by cpu_up() to online a node without onlined memory. |
b9ff0360 OS |
978 | * |
979 | * Returns: | |
980 | * 1 -> a new node has been allocated | |
981 | * 0 -> the node is already online | |
982 | * -ENOMEM -> the node could not be allocated | |
cf23422b | 983 | */ |
b9ff0360 | 984 | static int __try_online_node(int nid, u64 start, bool set_node_online) |
cf23422b | 985 | { |
b9ff0360 OS |
986 | pg_data_t *pgdat; |
987 | int ret = 1; | |
cf23422b | 988 | |
01b0f197 TK |
989 | if (node_online(nid)) |
990 | return 0; | |
991 | ||
b9ff0360 | 992 | pgdat = hotadd_new_pgdat(nid, start); |
7553e8f2 | 993 | if (!pgdat) { |
01b0f197 | 994 | pr_err("Cannot online node %d due to NULL pgdat\n", nid); |
cf23422b | 995 | ret = -ENOMEM; |
996 | goto out; | |
997 | } | |
b9ff0360 OS |
998 | |
999 | if (set_node_online) { | |
1000 | node_set_online(nid); | |
1001 | ret = register_one_node(nid); | |
1002 | BUG_ON(ret); | |
1003 | } | |
cf23422b | 1004 | out: |
b9ff0360 OS |
1005 | return ret; |
1006 | } | |
1007 | ||
1008 | /* | |
1009 | * Users of this function always want to online/register the node | |
1010 | */ | |
1011 | int try_online_node(int nid) | |
1012 | { | |
1013 | int ret; | |
1014 | ||
1015 | mem_hotplug_begin(); | |
1016 | ret = __try_online_node(nid, 0, true); | |
bfc8c901 | 1017 | mem_hotplug_done(); |
cf23422b | 1018 | return ret; |
1019 | } | |
1020 | ||
27356f54 TK |
1021 | static int check_hotplug_memory_range(u64 start, u64 size) |
1022 | { | |
ba325585 | 1023 | /* memory range must be block size aligned */ |
cec3ebd0 DH |
1024 | if (!size || !IS_ALIGNED(start, memory_block_size_bytes()) || |
1025 | !IS_ALIGNED(size, memory_block_size_bytes())) { | |
ba325585 | 1026 | pr_err("Block size [%#lx] unaligned hotplug range: start %#llx, size %#llx", |
cec3ebd0 | 1027 | memory_block_size_bytes(), start, size); |
27356f54 TK |
1028 | return -EINVAL; |
1029 | } | |
1030 | ||
1031 | return 0; | |
1032 | } | |
1033 | ||
31bc3858 VK |
1034 | static int online_memory_block(struct memory_block *mem, void *arg) |
1035 | { | |
dc18d706 | 1036 | return device_online(&mem->dev); |
31bc3858 VK |
1037 | } |
1038 | ||
8df1d0e4 DH |
1039 | /* |
1040 | * NOTE: The caller must call lock_device_hotplug() to serialize hotplug | |
1041 | * and online/offline operations (triggered e.g. by sysfs). | |
1042 | * | |
1043 | * we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG | |
1044 | */ | |
f29d8e9c | 1045 | int __ref add_memory_resource(int nid, struct resource *res) |
bc02af93 | 1046 | { |
05f800a0 | 1047 | struct mhp_restrictions restrictions = {}; |
62cedb9f | 1048 | u64 start, size; |
b9ff0360 | 1049 | bool new_node = false; |
bc02af93 YG |
1050 | int ret; |
1051 | ||
62cedb9f DV |
1052 | start = res->start; |
1053 | size = resource_size(res); | |
1054 | ||
27356f54 TK |
1055 | ret = check_hotplug_memory_range(start, size); |
1056 | if (ret) | |
1057 | return ret; | |
1058 | ||
bfc8c901 | 1059 | mem_hotplug_begin(); |
ac13c462 | 1060 | |
7f36e3e5 TC |
1061 | /* |
1062 | * Add new range to memblock so that when hotadd_new_pgdat() is called | |
1063 | * to allocate new pgdat, get_pfn_range_for_nid() will be able to find | |
1064 | * this new range and calculate total pages correctly. The range will | |
1065 | * be removed at hot-remove time. | |
1066 | */ | |
1067 | memblock_add_node(start, size, nid); | |
1068 | ||
b9ff0360 OS |
1069 | ret = __try_online_node(nid, start, false); |
1070 | if (ret < 0) | |
1071 | goto error; | |
1072 | new_node = ret; | |
9af3c2de | 1073 | |
bc02af93 | 1074 | /* call arch's memory hotadd */ |
940519f0 | 1075 | ret = arch_add_memory(nid, start, size, &restrictions); |
9af3c2de YG |
1076 | if (ret < 0) |
1077 | goto error; | |
1078 | ||
db051a0d DH |
1079 | /* create memory block devices after memory was added */ |
1080 | ret = create_memory_block_devices(start, size); | |
1081 | if (ret) { | |
1082 | arch_remove_memory(nid, start, size, NULL); | |
1083 | goto error; | |
1084 | } | |
1085 | ||
a1e565aa | 1086 | if (new_node) { |
d5b6f6a3 | 1087 | /* If sysfs file of new node can't be created, cpu on the node |
0fc44159 YG |
1088 | * can't be hot-added. There is no rollback way now. |
1089 | * So, check by BUG_ON() to catch it reluctantly.. | |
d5b6f6a3 | 1090 | * We online node here. We can't roll back from here. |
0fc44159 | 1091 | */ |
d5b6f6a3 OS |
1092 | node_set_online(nid); |
1093 | ret = __register_one_node(nid); | |
0fc44159 YG |
1094 | BUG_ON(ret); |
1095 | } | |
1096 | ||
d5b6f6a3 | 1097 | /* link memory sections under this node.*/ |
dfaf159e LD |
1098 | ret = link_mem_sections(nid, PFN_DOWN(start), PFN_UP(start + size - 1), |
1099 | MEMINIT_HOTPLUG); | |
d5b6f6a3 OS |
1100 | BUG_ON(ret); |
1101 | ||
d96ae530 AM |
1102 | /* create new memmap entry */ |
1103 | firmware_map_add_hotplug(start, start + size, "System RAM"); | |
1104 | ||
381eab4a DH |
1105 | /* device_online() will take the lock when calling online_pages() */ |
1106 | mem_hotplug_done(); | |
1107 | ||
31bc3858 | 1108 | /* online pages if requested */ |
f29d8e9c | 1109 | if (memhp_auto_online) |
fbcf73ce | 1110 | walk_memory_blocks(start, size, NULL, online_memory_block); |
31bc3858 | 1111 | |
381eab4a | 1112 | return ret; |
9af3c2de YG |
1113 | error: |
1114 | /* rollback pgdat allocation and others */ | |
b9ff0360 OS |
1115 | if (new_node) |
1116 | rollback_node_hotadd(nid); | |
7f36e3e5 | 1117 | memblock_remove(start, size); |
bfc8c901 | 1118 | mem_hotplug_done(); |
bc02af93 YG |
1119 | return ret; |
1120 | } | |
62cedb9f | 1121 | |
8df1d0e4 DH |
1122 | /* requires device_hotplug_lock, see add_memory_resource() */ |
1123 | int __ref __add_memory(int nid, u64 start, u64 size) | |
62cedb9f DV |
1124 | { |
1125 | struct resource *res; | |
1126 | int ret; | |
1127 | ||
1128 | res = register_memory_resource(start, size); | |
6f754ba4 VK |
1129 | if (IS_ERR(res)) |
1130 | return PTR_ERR(res); | |
62cedb9f | 1131 | |
f29d8e9c | 1132 | ret = add_memory_resource(nid, res); |
62cedb9f DV |
1133 | if (ret < 0) |
1134 | release_memory_resource(res); | |
1135 | return ret; | |
1136 | } | |
8df1d0e4 DH |
1137 | |
1138 | int add_memory(int nid, u64 start, u64 size) | |
1139 | { | |
1140 | int rc; | |
1141 | ||
1142 | lock_device_hotplug(); | |
1143 | rc = __add_memory(nid, start, size); | |
1144 | unlock_device_hotplug(); | |
1145 | ||
1146 | return rc; | |
1147 | } | |
bc02af93 | 1148 | EXPORT_SYMBOL_GPL(add_memory); |
0c0e6195 KH |
1149 | |
1150 | #ifdef CONFIG_MEMORY_HOTREMOVE | |
5c755e9f BP |
1151 | /* |
1152 | * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy | |
1153 | * set and the size of the free page is given by page_order(). Using this, | |
1154 | * the function determines if the pageblock contains only free pages. | |
1155 | * Due to buddy contraints, a free page at least the size of a pageblock will | |
1156 | * be located at the start of the pageblock | |
1157 | */ | |
1158 | static inline int pageblock_free(struct page *page) | |
1159 | { | |
1160 | return PageBuddy(page) && page_order(page) >= pageblock_order; | |
1161 | } | |
1162 | ||
891cb2a7 MH |
1163 | /* Return the pfn of the start of the next active pageblock after a given pfn */ |
1164 | static unsigned long next_active_pageblock(unsigned long pfn) | |
5c755e9f | 1165 | { |
891cb2a7 MH |
1166 | struct page *page = pfn_to_page(pfn); |
1167 | ||
5c755e9f | 1168 | /* Ensure the starting page is pageblock-aligned */ |
891cb2a7 | 1169 | BUG_ON(pfn & (pageblock_nr_pages - 1)); |
5c755e9f | 1170 | |
5c755e9f | 1171 | /* If the entire pageblock is free, move to the end of free page */ |
0dcc48c1 KH |
1172 | if (pageblock_free(page)) { |
1173 | int order; | |
1174 | /* be careful. we don't have locks, page_order can be changed.*/ | |
1175 | order = page_order(page); | |
1176 | if ((order < MAX_ORDER) && (order >= pageblock_order)) | |
891cb2a7 | 1177 | return pfn + (1 << order); |
0dcc48c1 | 1178 | } |
5c755e9f | 1179 | |
891cb2a7 | 1180 | return pfn + pageblock_nr_pages; |
5c755e9f BP |
1181 | } |
1182 | ||
891cb2a7 | 1183 | static bool is_pageblock_removable_nolock(unsigned long pfn) |
fb52bbae | 1184 | { |
891cb2a7 | 1185 | struct page *page = pfn_to_page(pfn); |
fb52bbae | 1186 | struct zone *zone; |
fb52bbae MM |
1187 | |
1188 | /* | |
1189 | * We have to be careful here because we are iterating over memory | |
1190 | * sections which are not zone aware so we might end up outside of | |
1191 | * the zone but still within the section. | |
1192 | * We have to take care about the node as well. If the node is offline | |
1193 | * its NODE_DATA will be NULL - see page_zone. | |
1194 | */ | |
1195 | if (!node_online(page_to_nid(page))) | |
1196 | return false; | |
1197 | ||
1198 | zone = page_zone(page); | |
1199 | pfn = page_to_pfn(page); | |
1200 | if (!zone_spans_pfn(zone, pfn)) | |
1201 | return false; | |
1202 | ||
d381c547 | 1203 | return !has_unmovable_pages(zone, page, 0, MIGRATE_MOVABLE, SKIP_HWPOISON); |
fb52bbae MM |
1204 | } |
1205 | ||
5c755e9f | 1206 | /* Checks if this range of memory is likely to be hot-removable. */ |
c98940f6 | 1207 | bool is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages) |
5c755e9f | 1208 | { |
891cb2a7 MH |
1209 | unsigned long end_pfn, pfn; |
1210 | ||
1211 | end_pfn = min(start_pfn + nr_pages, | |
1212 | zone_end_pfn(page_zone(pfn_to_page(start_pfn)))); | |
5c755e9f BP |
1213 | |
1214 | /* Check the starting page of each pageblock within the range */ | |
891cb2a7 MH |
1215 | for (pfn = start_pfn; pfn < end_pfn; pfn = next_active_pageblock(pfn)) { |
1216 | if (!is_pageblock_removable_nolock(pfn)) | |
c98940f6 | 1217 | return false; |
49ac8255 | 1218 | cond_resched(); |
5c755e9f BP |
1219 | } |
1220 | ||
1221 | /* All pageblocks in the memory block are likely to be hot-removable */ | |
c98940f6 | 1222 | return true; |
5c755e9f BP |
1223 | } |
1224 | ||
0c0e6195 | 1225 | /* |
deb88a2a | 1226 | * Confirm all pages in a range [start, end) belong to the same zone. |
a96dfddb | 1227 | * When true, return its valid [start, end). |
0c0e6195 | 1228 | */ |
a96dfddb TK |
1229 | int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn, |
1230 | unsigned long *valid_start, unsigned long *valid_end) | |
0c0e6195 | 1231 | { |
5f0f2887 | 1232 | unsigned long pfn, sec_end_pfn; |
a96dfddb | 1233 | unsigned long start, end; |
0c0e6195 KH |
1234 | struct zone *zone = NULL; |
1235 | struct page *page; | |
1236 | int i; | |
deb88a2a | 1237 | for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn + 1); |
0c0e6195 | 1238 | pfn < end_pfn; |
deb88a2a | 1239 | pfn = sec_end_pfn, sec_end_pfn += PAGES_PER_SECTION) { |
5f0f2887 AB |
1240 | /* Make sure the memory section is present first */ |
1241 | if (!present_section_nr(pfn_to_section_nr(pfn))) | |
0c0e6195 | 1242 | continue; |
5f0f2887 AB |
1243 | for (; pfn < sec_end_pfn && pfn < end_pfn; |
1244 | pfn += MAX_ORDER_NR_PAGES) { | |
1245 | i = 0; | |
1246 | /* This is just a CONFIG_HOLES_IN_ZONE check.*/ | |
1247 | while ((i < MAX_ORDER_NR_PAGES) && | |
1248 | !pfn_valid_within(pfn + i)) | |
1249 | i++; | |
d6d8c8a4 | 1250 | if (i == MAX_ORDER_NR_PAGES || pfn + i >= end_pfn) |
5f0f2887 | 1251 | continue; |
24feb47c MZ |
1252 | /* Check if we got outside of the zone */ |
1253 | if (zone && !zone_spans_pfn(zone, pfn + i)) | |
1254 | return 0; | |
5f0f2887 AB |
1255 | page = pfn_to_page(pfn + i); |
1256 | if (zone && page_zone(page) != zone) | |
1257 | return 0; | |
a96dfddb TK |
1258 | if (!zone) |
1259 | start = pfn + i; | |
5f0f2887 | 1260 | zone = page_zone(page); |
a96dfddb | 1261 | end = pfn + MAX_ORDER_NR_PAGES; |
5f0f2887 | 1262 | } |
0c0e6195 | 1263 | } |
deb88a2a | 1264 | |
a96dfddb TK |
1265 | if (zone) { |
1266 | *valid_start = start; | |
d6d8c8a4 | 1267 | *valid_end = min(end, end_pfn); |
deb88a2a | 1268 | return 1; |
a96dfddb | 1269 | } else { |
deb88a2a | 1270 | return 0; |
a96dfddb | 1271 | } |
0c0e6195 KH |
1272 | } |
1273 | ||
1274 | /* | |
0efadf48 YX |
1275 | * Scan pfn range [start,end) to find movable/migratable pages (LRU pages, |
1276 | * non-lru movable pages and hugepages). We scan pfn because it's much | |
1277 | * easier than scanning over linked list. This function returns the pfn | |
1278 | * of the first found movable page if it's found, otherwise 0. | |
0c0e6195 | 1279 | */ |
c8721bbb | 1280 | static unsigned long scan_movable_pages(unsigned long start, unsigned long end) |
0c0e6195 KH |
1281 | { |
1282 | unsigned long pfn; | |
eeb0efd0 | 1283 | |
0c0e6195 | 1284 | for (pfn = start; pfn < end; pfn++) { |
eeb0efd0 OS |
1285 | struct page *page, *head; |
1286 | unsigned long skip; | |
1287 | ||
1288 | if (!pfn_valid(pfn)) | |
1289 | continue; | |
1290 | page = pfn_to_page(pfn); | |
1291 | if (PageLRU(page)) | |
1292 | return pfn; | |
1293 | if (__PageMovable(page)) | |
1294 | return pfn; | |
1295 | ||
1296 | if (!PageHuge(page)) | |
1297 | continue; | |
1298 | head = compound_head(page); | |
39186cbe | 1299 | if (page_huge_active(head)) |
eeb0efd0 | 1300 | return pfn; |
d8c6546b | 1301 | skip = compound_nr(head) - (page - head); |
eeb0efd0 | 1302 | pfn += skip - 1; |
0c0e6195 KH |
1303 | } |
1304 | return 0; | |
1305 | } | |
1306 | ||
666feb21 | 1307 | static struct page *new_node_page(struct page *page, unsigned long private) |
394e31d2 | 1308 | { |
394e31d2 | 1309 | int nid = page_to_nid(page); |
231e97e2 | 1310 | nodemask_t nmask = node_states[N_MEMORY]; |
7f252f27 MH |
1311 | |
1312 | /* | |
1313 | * try to allocate from a different node but reuse this node if there | |
1314 | * are no other online nodes to be used (e.g. we are offlining a part | |
1315 | * of the only existing node) | |
1316 | */ | |
1317 | node_clear(nid, nmask); | |
1318 | if (nodes_empty(nmask)) | |
1319 | node_set(nid, nmask); | |
394e31d2 | 1320 | |
8b913238 | 1321 | return new_page_nodemask(page, nid, &nmask); |
394e31d2 XQ |
1322 | } |
1323 | ||
0c0e6195 KH |
1324 | static int |
1325 | do_migrate_range(unsigned long start_pfn, unsigned long end_pfn) | |
1326 | { | |
1327 | unsigned long pfn; | |
1328 | struct page *page; | |
0c0e6195 KH |
1329 | int ret = 0; |
1330 | LIST_HEAD(source); | |
1331 | ||
a85009c3 | 1332 | for (pfn = start_pfn; pfn < end_pfn; pfn++) { |
0c0e6195 KH |
1333 | if (!pfn_valid(pfn)) |
1334 | continue; | |
1335 | page = pfn_to_page(pfn); | |
c8721bbb NH |
1336 | |
1337 | if (PageHuge(page)) { | |
1338 | struct page *head = compound_head(page); | |
d8c6546b | 1339 | pfn = page_to_pfn(head) + compound_nr(head) - 1; |
daf3538a | 1340 | isolate_huge_page(head, &source); |
c8721bbb | 1341 | continue; |
94723aaf | 1342 | } else if (PageTransHuge(page)) |
8135d892 NH |
1343 | pfn = page_to_pfn(compound_head(page)) |
1344 | + hpage_nr_pages(page) - 1; | |
c8721bbb | 1345 | |
b15c8726 MH |
1346 | /* |
1347 | * HWPoison pages have elevated reference counts so the migration would | |
1348 | * fail on them. It also doesn't make any sense to migrate them in the | |
1349 | * first place. Still try to unmap such a page in case it is still mapped | |
1350 | * (e.g. current hwpoison implementation doesn't unmap KSM pages but keep | |
1351 | * the unmap as the catch all safety net). | |
1352 | */ | |
1353 | if (PageHWPoison(page)) { | |
1354 | if (WARN_ON(PageLRU(page))) | |
1355 | isolate_lru_page(page); | |
1356 | if (page_mapped(page)) | |
1357 | try_to_unmap(page, TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS); | |
1358 | continue; | |
1359 | } | |
1360 | ||
700c2a46 | 1361 | if (!get_page_unless_zero(page)) |
0c0e6195 KH |
1362 | continue; |
1363 | /* | |
0efadf48 YX |
1364 | * We can skip free pages. And we can deal with pages on |
1365 | * LRU and non-lru movable pages. | |
0c0e6195 | 1366 | */ |
0efadf48 YX |
1367 | if (PageLRU(page)) |
1368 | ret = isolate_lru_page(page); | |
1369 | else | |
1370 | ret = isolate_movable_page(page, ISOLATE_UNEVICTABLE); | |
0c0e6195 | 1371 | if (!ret) { /* Success */ |
62695a84 | 1372 | list_add_tail(&page->lru, &source); |
0efadf48 YX |
1373 | if (!__PageMovable(page)) |
1374 | inc_node_page_state(page, NR_ISOLATED_ANON + | |
1375 | page_is_file_cache(page)); | |
6d9c285a | 1376 | |
0c0e6195 | 1377 | } else { |
2932c8b0 | 1378 | pr_warn("failed to isolate pfn %lx\n", pfn); |
0efadf48 | 1379 | dump_page(page, "isolation failed"); |
0c0e6195 | 1380 | } |
1723058e | 1381 | put_page(page); |
0c0e6195 | 1382 | } |
f3ab2636 | 1383 | if (!list_empty(&source)) { |
394e31d2 XQ |
1384 | /* Allocate a new page from the nearest neighbor node */ |
1385 | ret = migrate_pages(&source, new_node_page, NULL, 0, | |
9c620e2b | 1386 | MIGRATE_SYNC, MR_MEMORY_HOTPLUG); |
2932c8b0 MH |
1387 | if (ret) { |
1388 | list_for_each_entry(page, &source, lru) { | |
1389 | pr_warn("migrating pfn %lx failed ret:%d ", | |
1390 | page_to_pfn(page), ret); | |
1391 | dump_page(page, "migration failure"); | |
1392 | } | |
c8721bbb | 1393 | putback_movable_pages(&source); |
2932c8b0 | 1394 | } |
0c0e6195 | 1395 | } |
1723058e | 1396 | |
0c0e6195 KH |
1397 | return ret; |
1398 | } | |
1399 | ||
1400 | /* | |
1401 | * remove from free_area[] and mark all as Reserved. | |
1402 | */ | |
1403 | static int | |
1404 | offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages, | |
1405 | void *data) | |
1406 | { | |
5557c766 | 1407 | unsigned long *offlined_pages = (unsigned long *)data; |
0c0e6195 | 1408 | |
5557c766 MH |
1409 | *offlined_pages += __offline_isolated_pages(start, start + nr_pages); |
1410 | return 0; | |
0c0e6195 KH |
1411 | } |
1412 | ||
1413 | /* | |
1414 | * Check all pages in range, recoreded as memory resource, are isolated. | |
1415 | */ | |
1416 | static int | |
1417 | check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages, | |
1418 | void *data) | |
1419 | { | |
5557c766 | 1420 | return test_pages_isolated(start_pfn, start_pfn + nr_pages, true); |
0c0e6195 KH |
1421 | } |
1422 | ||
c5320926 TC |
1423 | static int __init cmdline_parse_movable_node(char *p) |
1424 | { | |
4932381e | 1425 | #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP |
55ac590c | 1426 | movable_node_enabled = true; |
4932381e MH |
1427 | #else |
1428 | pr_warn("movable_node parameter depends on CONFIG_HAVE_MEMBLOCK_NODE_MAP to work properly\n"); | |
1429 | #endif | |
c5320926 TC |
1430 | return 0; |
1431 | } | |
1432 | early_param("movable_node", cmdline_parse_movable_node); | |
1433 | ||
d9713679 LJ |
1434 | /* check which state of node_states will be changed when offline memory */ |
1435 | static void node_states_check_changes_offline(unsigned long nr_pages, | |
1436 | struct zone *zone, struct memory_notify *arg) | |
1437 | { | |
1438 | struct pglist_data *pgdat = zone->zone_pgdat; | |
1439 | unsigned long present_pages = 0; | |
86b27bea | 1440 | enum zone_type zt; |
d9713679 | 1441 | |
98fa15f3 AK |
1442 | arg->status_change_nid = NUMA_NO_NODE; |
1443 | arg->status_change_nid_normal = NUMA_NO_NODE; | |
1444 | arg->status_change_nid_high = NUMA_NO_NODE; | |
d9713679 LJ |
1445 | |
1446 | /* | |
86b27bea OS |
1447 | * Check whether node_states[N_NORMAL_MEMORY] will be changed. |
1448 | * If the memory to be offline is within the range | |
1449 | * [0..ZONE_NORMAL], and it is the last present memory there, | |
1450 | * the zones in that range will become empty after the offlining, | |
1451 | * thus we can determine that we need to clear the node from | |
1452 | * node_states[N_NORMAL_MEMORY]. | |
d9713679 | 1453 | */ |
86b27bea | 1454 | for (zt = 0; zt <= ZONE_NORMAL; zt++) |
d9713679 | 1455 | present_pages += pgdat->node_zones[zt].present_pages; |
86b27bea | 1456 | if (zone_idx(zone) <= ZONE_NORMAL && nr_pages >= present_pages) |
d9713679 | 1457 | arg->status_change_nid_normal = zone_to_nid(zone); |
d9713679 | 1458 | |
6715ddf9 LJ |
1459 | #ifdef CONFIG_HIGHMEM |
1460 | /* | |
86b27bea OS |
1461 | * node_states[N_HIGH_MEMORY] contains nodes which |
1462 | * have normal memory or high memory. | |
1463 | * Here we add the present_pages belonging to ZONE_HIGHMEM. | |
1464 | * If the zone is within the range of [0..ZONE_HIGHMEM), and | |
1465 | * we determine that the zones in that range become empty, | |
1466 | * we need to clear the node for N_HIGH_MEMORY. | |
6715ddf9 | 1467 | */ |
86b27bea OS |
1468 | present_pages += pgdat->node_zones[ZONE_HIGHMEM].present_pages; |
1469 | if (zone_idx(zone) <= ZONE_HIGHMEM && nr_pages >= present_pages) | |
6715ddf9 | 1470 | arg->status_change_nid_high = zone_to_nid(zone); |
6715ddf9 LJ |
1471 | #endif |
1472 | ||
d9713679 | 1473 | /* |
86b27bea OS |
1474 | * We have accounted the pages from [0..ZONE_NORMAL), and |
1475 | * in case of CONFIG_HIGHMEM the pages from ZONE_HIGHMEM | |
1476 | * as well. | |
1477 | * Here we count the possible pages from ZONE_MOVABLE. | |
1478 | * If after having accounted all the pages, we see that the nr_pages | |
1479 | * to be offlined is over or equal to the accounted pages, | |
1480 | * we know that the node will become empty, and so, we can clear | |
1481 | * it for N_MEMORY as well. | |
d9713679 | 1482 | */ |
86b27bea | 1483 | present_pages += pgdat->node_zones[ZONE_MOVABLE].present_pages; |
d9713679 | 1484 | |
d9713679 LJ |
1485 | if (nr_pages >= present_pages) |
1486 | arg->status_change_nid = zone_to_nid(zone); | |
d9713679 LJ |
1487 | } |
1488 | ||
1489 | static void node_states_clear_node(int node, struct memory_notify *arg) | |
1490 | { | |
1491 | if (arg->status_change_nid_normal >= 0) | |
1492 | node_clear_state(node, N_NORMAL_MEMORY); | |
1493 | ||
cf01f6f5 | 1494 | if (arg->status_change_nid_high >= 0) |
d9713679 | 1495 | node_clear_state(node, N_HIGH_MEMORY); |
6715ddf9 | 1496 | |
cf01f6f5 | 1497 | if (arg->status_change_nid >= 0) |
6715ddf9 | 1498 | node_clear_state(node, N_MEMORY); |
d9713679 LJ |
1499 | } |
1500 | ||
a16cee10 | 1501 | static int __ref __offline_pages(unsigned long start_pfn, |
ecde0f3e | 1502 | unsigned long end_pfn) |
0c0e6195 | 1503 | { |
ecde0f3e | 1504 | unsigned long pfn, nr_pages; |
5557c766 | 1505 | unsigned long offlined_pages = 0; |
9b7ea46a | 1506 | int ret, node, nr_isolate_pageblock; |
d702909f | 1507 | unsigned long flags; |
a96dfddb | 1508 | unsigned long valid_start, valid_end; |
0c0e6195 | 1509 | struct zone *zone; |
7b78d335 | 1510 | struct memory_notify arg; |
79605093 | 1511 | char *reason; |
0c0e6195 | 1512 | |
381eab4a DH |
1513 | mem_hotplug_begin(); |
1514 | ||
0c0e6195 KH |
1515 | /* This makes hotplug much easier...and readable. |
1516 | we assume this for now. .*/ | |
381eab4a DH |
1517 | if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start, |
1518 | &valid_end)) { | |
79605093 MH |
1519 | ret = -EINVAL; |
1520 | reason = "multizone range"; | |
1521 | goto failed_removal; | |
381eab4a | 1522 | } |
7b78d335 | 1523 | |
a96dfddb | 1524 | zone = page_zone(pfn_to_page(valid_start)); |
7b78d335 YG |
1525 | node = zone_to_nid(zone); |
1526 | nr_pages = end_pfn - start_pfn; | |
1527 | ||
0c0e6195 | 1528 | /* set above range as isolated */ |
b023f468 | 1529 | ret = start_isolate_page_range(start_pfn, end_pfn, |
d381c547 MH |
1530 | MIGRATE_MOVABLE, |
1531 | SKIP_HWPOISON | REPORT_FAILURE); | |
9b7ea46a | 1532 | if (ret < 0) { |
79605093 MH |
1533 | reason = "failure to isolate range"; |
1534 | goto failed_removal; | |
381eab4a | 1535 | } |
9b7ea46a | 1536 | nr_isolate_pageblock = ret; |
7b78d335 YG |
1537 | |
1538 | arg.start_pfn = start_pfn; | |
1539 | arg.nr_pages = nr_pages; | |
d9713679 | 1540 | node_states_check_changes_offline(nr_pages, zone, &arg); |
7b78d335 YG |
1541 | |
1542 | ret = memory_notify(MEM_GOING_OFFLINE, &arg); | |
1543 | ret = notifier_to_errno(ret); | |
79605093 MH |
1544 | if (ret) { |
1545 | reason = "notifier failure"; | |
1546 | goto failed_removal_isolated; | |
1547 | } | |
7b78d335 | 1548 | |
bb8965bd MH |
1549 | do { |
1550 | for (pfn = start_pfn; pfn;) { | |
1551 | if (signal_pending(current)) { | |
1552 | ret = -EINTR; | |
1553 | reason = "signal backoff"; | |
1554 | goto failed_removal_isolated; | |
1555 | } | |
72b39cfc | 1556 | |
bb8965bd MH |
1557 | cond_resched(); |
1558 | lru_add_drain_all(); | |
bb8965bd MH |
1559 | |
1560 | pfn = scan_movable_pages(pfn, end_pfn); | |
1561 | if (pfn) { | |
1562 | /* | |
1563 | * TODO: fatal migration failures should bail | |
1564 | * out | |
1565 | */ | |
1566 | do_migrate_range(pfn, end_pfn); | |
1567 | } | |
1568 | } | |
0c0e6195 | 1569 | |
bb8965bd MH |
1570 | /* |
1571 | * Dissolve free hugepages in the memory block before doing | |
1572 | * offlining actually in order to make hugetlbfs's object | |
1573 | * counting consistent. | |
1574 | */ | |
1575 | ret = dissolve_free_huge_pages(start_pfn, end_pfn); | |
1576 | if (ret) { | |
1577 | reason = "failure to dissolve huge pages"; | |
1578 | goto failed_removal_isolated; | |
1579 | } | |
1580 | /* check again */ | |
5557c766 MH |
1581 | ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, |
1582 | NULL, check_pages_isolated_cb); | |
fe345324 PT |
1583 | /* |
1584 | * per-cpu pages are drained in start_isolate_page_range, but if | |
1585 | * there are still pages that are not free, make sure that we | |
1586 | * drain again, because when we isolated range we might | |
1587 | * have raced with another thread that was adding pages to pcp | |
1588 | * list. | |
1589 | * | |
1590 | * Forward progress should be still guaranteed because | |
1591 | * pages on the pcp list can only belong to MOVABLE_ZONE | |
1592 | * because has_unmovable_pages explicitly checks for | |
1593 | * PageBuddy on freed pages on other zones. | |
1594 | */ | |
1595 | if (ret) | |
1596 | drain_all_pages(zone); | |
5557c766 | 1597 | } while (ret); |
72b39cfc | 1598 | |
b3834be5 | 1599 | /* Ok, all of our target is isolated. |
0c0e6195 | 1600 | We cannot do rollback at this point. */ |
5557c766 MH |
1601 | walk_system_ram_range(start_pfn, end_pfn - start_pfn, |
1602 | &offlined_pages, offline_isolated_pages_cb); | |
1603 | pr_info("Offlined Pages %ld\n", offlined_pages); | |
9b7ea46a QC |
1604 | /* |
1605 | * Onlining will reset pagetype flags and makes migrate type | |
1606 | * MOVABLE, so just need to decrease the number of isolated | |
1607 | * pageblocks zone counter here. | |
1608 | */ | |
1609 | spin_lock_irqsave(&zone->lock, flags); | |
1610 | zone->nr_isolate_pageblock -= nr_isolate_pageblock; | |
1611 | spin_unlock_irqrestore(&zone->lock, flags); | |
1612 | ||
0c0e6195 | 1613 | /* removal success */ |
3dcc0571 | 1614 | adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages); |
0c0e6195 | 1615 | zone->present_pages -= offlined_pages; |
d702909f CS |
1616 | |
1617 | pgdat_resize_lock(zone->zone_pgdat, &flags); | |
0c0e6195 | 1618 | zone->zone_pgdat->node_present_pages -= offlined_pages; |
d702909f | 1619 | pgdat_resize_unlock(zone->zone_pgdat, &flags); |
7b78d335 | 1620 | |
1b79acc9 KM |
1621 | init_per_zone_wmark_min(); |
1622 | ||
1e8537ba | 1623 | if (!populated_zone(zone)) { |
340175b7 | 1624 | zone_pcp_reset(zone); |
72675e13 | 1625 | build_all_zonelists(NULL); |
1e8537ba XQ |
1626 | } else |
1627 | zone_pcp_update(zone); | |
340175b7 | 1628 | |
d9713679 | 1629 | node_states_clear_node(node, &arg); |
698b1b30 | 1630 | if (arg.status_change_nid >= 0) { |
8fe23e05 | 1631 | kswapd_stop(node); |
698b1b30 VB |
1632 | kcompactd_stop(node); |
1633 | } | |
bce7394a | 1634 | |
0c0e6195 KH |
1635 | vm_total_pages = nr_free_pagecache_pages(); |
1636 | writeback_set_ratelimit(); | |
7b78d335 YG |
1637 | |
1638 | memory_notify(MEM_OFFLINE, &arg); | |
3b14ccf2 | 1639 | remove_pfn_range_from_zone(zone, start_pfn, nr_pages); |
381eab4a | 1640 | mem_hotplug_done(); |
0c0e6195 KH |
1641 | return 0; |
1642 | ||
79605093 MH |
1643 | failed_removal_isolated: |
1644 | undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE); | |
c4efe484 | 1645 | memory_notify(MEM_CANCEL_OFFLINE, &arg); |
0c0e6195 | 1646 | failed_removal: |
79605093 | 1647 | pr_debug("memory offlining [mem %#010llx-%#010llx] failed due to %s\n", |
e33e33b4 | 1648 | (unsigned long long) start_pfn << PAGE_SHIFT, |
79605093 MH |
1649 | ((unsigned long long) end_pfn << PAGE_SHIFT) - 1, |
1650 | reason); | |
0c0e6195 | 1651 | /* pushback to free area */ |
381eab4a | 1652 | mem_hotplug_done(); |
0c0e6195 KH |
1653 | return ret; |
1654 | } | |
71088785 | 1655 | |
a16cee10 WC |
1656 | int offline_pages(unsigned long start_pfn, unsigned long nr_pages) |
1657 | { | |
ecde0f3e | 1658 | return __offline_pages(start_pfn, start_pfn + nr_pages); |
a16cee10 WC |
1659 | } |
1660 | ||
d6de9d53 | 1661 | static int check_memblock_offlined_cb(struct memory_block *mem, void *arg) |
bbc76be6 WC |
1662 | { |
1663 | int ret = !is_memblock_offlined(mem); | |
1664 | ||
349daa0f RD |
1665 | if (unlikely(ret)) { |
1666 | phys_addr_t beginpa, endpa; | |
1667 | ||
1668 | beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr)); | |
b6c88d3b | 1669 | endpa = beginpa + memory_block_size_bytes() - 1; |
756a025f | 1670 | pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n", |
349daa0f | 1671 | &beginpa, &endpa); |
bbc76be6 | 1672 | |
eca499ab PT |
1673 | return -EBUSY; |
1674 | } | |
1675 | return 0; | |
bbc76be6 WC |
1676 | } |
1677 | ||
0f1cfe9d | 1678 | static int check_cpu_on_node(pg_data_t *pgdat) |
60a5a19e | 1679 | { |
60a5a19e TC |
1680 | int cpu; |
1681 | ||
1682 | for_each_present_cpu(cpu) { | |
1683 | if (cpu_to_node(cpu) == pgdat->node_id) | |
1684 | /* | |
1685 | * the cpu on this node isn't removed, and we can't | |
1686 | * offline this node. | |
1687 | */ | |
1688 | return -EBUSY; | |
1689 | } | |
1690 | ||
1691 | return 0; | |
1692 | } | |
1693 | ||
2c91f8fc DH |
1694 | static int check_no_memblock_for_node_cb(struct memory_block *mem, void *arg) |
1695 | { | |
1696 | int nid = *(int *)arg; | |
1697 | ||
1698 | /* | |
1699 | * If a memory block belongs to multiple nodes, the stored nid is not | |
1700 | * reliable. However, such blocks are always online (e.g., cannot get | |
1701 | * offlined) and, therefore, are still spanned by the node. | |
1702 | */ | |
1703 | return mem->nid == nid ? -EEXIST : 0; | |
1704 | } | |
1705 | ||
0f1cfe9d TK |
1706 | /** |
1707 | * try_offline_node | |
e8b098fc | 1708 | * @nid: the node ID |
0f1cfe9d TK |
1709 | * |
1710 | * Offline a node if all memory sections and cpus of the node are removed. | |
1711 | * | |
1712 | * NOTE: The caller must call lock_device_hotplug() to serialize hotplug | |
1713 | * and online/offline operations before this call. | |
1714 | */ | |
90b30cdc | 1715 | void try_offline_node(int nid) |
60a5a19e | 1716 | { |
d822b86a | 1717 | pg_data_t *pgdat = NODE_DATA(nid); |
2c91f8fc | 1718 | int rc; |
60a5a19e | 1719 | |
2c91f8fc DH |
1720 | /* |
1721 | * If the node still spans pages (especially ZONE_DEVICE), don't | |
1722 | * offline it. A node spans memory after move_pfn_range_to_zone(), | |
1723 | * e.g., after the memory block was onlined. | |
1724 | */ | |
1725 | if (pgdat->node_spanned_pages) | |
1726 | return; | |
60a5a19e | 1727 | |
2c91f8fc DH |
1728 | /* |
1729 | * Especially offline memory blocks might not be spanned by the | |
1730 | * node. They will get spanned by the node once they get onlined. | |
1731 | * However, they link to the node in sysfs and can get onlined later. | |
1732 | */ | |
1733 | rc = for_each_memory_block(&nid, check_no_memblock_for_node_cb); | |
1734 | if (rc) | |
60a5a19e | 1735 | return; |
60a5a19e | 1736 | |
46a3679b | 1737 | if (check_cpu_on_node(pgdat)) |
60a5a19e TC |
1738 | return; |
1739 | ||
1740 | /* | |
1741 | * all memory/cpu of this node are removed, we can offline this | |
1742 | * node now. | |
1743 | */ | |
1744 | node_set_offline(nid); | |
1745 | unregister_one_node(nid); | |
1746 | } | |
90b30cdc | 1747 | EXPORT_SYMBOL(try_offline_node); |
60a5a19e | 1748 | |
d9eb1417 DH |
1749 | static void __release_memory_resource(resource_size_t start, |
1750 | resource_size_t size) | |
1751 | { | |
1752 | int ret; | |
1753 | ||
1754 | /* | |
1755 | * When removing memory in the same granularity as it was added, | |
1756 | * this function never fails. It might only fail if resources | |
1757 | * have to be adjusted or split. We'll ignore the error, as | |
1758 | * removing of memory cannot fail. | |
1759 | */ | |
1760 | ret = release_mem_region_adjustable(&iomem_resource, start, size); | |
1761 | if (ret) { | |
1762 | resource_size_t endres = start + size - 1; | |
1763 | ||
1764 | pr_warn("Unable to release resource <%pa-%pa> (%d)\n", | |
1765 | &start, &endres, ret); | |
1766 | } | |
1767 | } | |
1768 | ||
eca499ab | 1769 | static int __ref try_remove_memory(int nid, u64 start, u64 size) |
bbc76be6 | 1770 | { |
eca499ab | 1771 | int rc = 0; |
993c1aad | 1772 | |
27356f54 TK |
1773 | BUG_ON(check_hotplug_memory_range(start, size)); |
1774 | ||
6677e3ea | 1775 | /* |
242831eb | 1776 | * All memory blocks must be offlined before removing memory. Check |
eca499ab | 1777 | * whether all memory blocks in question are offline and return error |
242831eb | 1778 | * if this is not the case. |
6677e3ea | 1779 | */ |
fbcf73ce | 1780 | rc = walk_memory_blocks(start, size, NULL, check_memblock_offlined_cb); |
eca499ab | 1781 | if (rc) |
7ddc0918 | 1782 | return rc; |
6677e3ea | 1783 | |
46c66c4b YI |
1784 | /* remove memmap entry */ |
1785 | firmware_map_remove(start, start + size, "System RAM"); | |
f9126ab9 XQ |
1786 | memblock_free(start, size); |
1787 | memblock_remove(start, size); | |
4c4b7f9b | 1788 | |
54714d39 DW |
1789 | /* |
1790 | * Memory block device removal under the device_hotplug_lock is | |
1791 | * a barrier against racing online attempts. | |
1792 | */ | |
4c4b7f9b | 1793 | remove_memory_block_devices(start, size); |
46c66c4b | 1794 | |
54714d39 DW |
1795 | mem_hotplug_begin(); |
1796 | ||
2c2a5af6 | 1797 | arch_remove_memory(nid, start, size, NULL); |
d9eb1417 | 1798 | __release_memory_resource(start, size); |
24d335ca | 1799 | |
60a5a19e TC |
1800 | try_offline_node(nid); |
1801 | ||
bfc8c901 | 1802 | mem_hotplug_done(); |
7ddc0918 | 1803 | return 0; |
71088785 | 1804 | } |
d15e5926 | 1805 | |
eca499ab PT |
1806 | /** |
1807 | * remove_memory | |
1808 | * @nid: the node ID | |
1809 | * @start: physical address of the region to remove | |
1810 | * @size: size of the region to remove | |
1811 | * | |
1812 | * NOTE: The caller must call lock_device_hotplug() to serialize hotplug | |
1813 | * and online/offline operations before this call, as required by | |
1814 | * try_offline_node(). | |
1815 | */ | |
1816 | void __remove_memory(int nid, u64 start, u64 size) | |
1817 | { | |
1818 | ||
1819 | /* | |
29a90db9 | 1820 | * trigger BUG() if some memory is not offlined prior to calling this |
eca499ab PT |
1821 | * function |
1822 | */ | |
1823 | if (try_remove_memory(nid, start, size)) | |
1824 | BUG(); | |
1825 | } | |
1826 | ||
1827 | /* | |
1828 | * Remove memory if every memory block is offline, otherwise return -EBUSY is | |
1829 | * some memory is not offline | |
1830 | */ | |
1831 | int remove_memory(int nid, u64 start, u64 size) | |
d15e5926 | 1832 | { |
eca499ab PT |
1833 | int rc; |
1834 | ||
d15e5926 | 1835 | lock_device_hotplug(); |
eca499ab | 1836 | rc = try_remove_memory(nid, start, size); |
d15e5926 | 1837 | unlock_device_hotplug(); |
eca499ab PT |
1838 | |
1839 | return rc; | |
d15e5926 | 1840 | } |
71088785 | 1841 | EXPORT_SYMBOL_GPL(remove_memory); |
aba6efc4 | 1842 | #endif /* CONFIG_MEMORY_HOTREMOVE */ |