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