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