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