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