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