xref: /openbmc/linux/mm/memory_hotplug.c (revision 31bc3858ea3ebcc3157b3f5f0e624c5962f5a7a6)
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 	/* during initialize mem_map, align hot-added range to section */
516 	start_sec = pfn_to_section_nr(phys_start_pfn);
517 	end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
518 
519 	altmap = to_vmem_altmap((unsigned long) pfn_to_page(phys_start_pfn));
520 	if (altmap) {
521 		/*
522 		 * Validate altmap is within bounds of the total request
523 		 */
524 		if (altmap->base_pfn != phys_start_pfn
525 				|| vmem_altmap_offset(altmap) > nr_pages) {
526 			pr_warn_once("memory add fail, invalid altmap\n");
527 			return -EINVAL;
528 		}
529 		altmap->alloc = 0;
530 	}
531 
532 	for (i = start_sec; i <= end_sec; i++) {
533 		err = __add_section(nid, zone, section_nr_to_pfn(i));
534 
535 		/*
536 		 * EEXIST is finally dealt with by ioresource collision
537 		 * check. see add_memory() => register_memory_resource()
538 		 * Warning will be printed if there is collision.
539 		 */
540 		if (err && (err != -EEXIST))
541 			break;
542 		err = 0;
543 	}
544 	vmemmap_populate_print_last();
545 
546 	return err;
547 }
548 EXPORT_SYMBOL_GPL(__add_pages);
549 
550 #ifdef CONFIG_MEMORY_HOTREMOVE
551 /* find the smallest valid pfn in the range [start_pfn, end_pfn) */
552 static int find_smallest_section_pfn(int nid, struct zone *zone,
553 				     unsigned long start_pfn,
554 				     unsigned long end_pfn)
555 {
556 	struct mem_section *ms;
557 
558 	for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
559 		ms = __pfn_to_section(start_pfn);
560 
561 		if (unlikely(!valid_section(ms)))
562 			continue;
563 
564 		if (unlikely(pfn_to_nid(start_pfn) != nid))
565 			continue;
566 
567 		if (zone && zone != page_zone(pfn_to_page(start_pfn)))
568 			continue;
569 
570 		return start_pfn;
571 	}
572 
573 	return 0;
574 }
575 
576 /* find the biggest valid pfn in the range [start_pfn, end_pfn). */
577 static int find_biggest_section_pfn(int nid, struct zone *zone,
578 				    unsigned long start_pfn,
579 				    unsigned long end_pfn)
580 {
581 	struct mem_section *ms;
582 	unsigned long pfn;
583 
584 	/* pfn is the end pfn of a memory section. */
585 	pfn = end_pfn - 1;
586 	for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
587 		ms = __pfn_to_section(pfn);
588 
589 		if (unlikely(!valid_section(ms)))
590 			continue;
591 
592 		if (unlikely(pfn_to_nid(pfn) != nid))
593 			continue;
594 
595 		if (zone && zone != page_zone(pfn_to_page(pfn)))
596 			continue;
597 
598 		return pfn;
599 	}
600 
601 	return 0;
602 }
603 
604 static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
605 			     unsigned long end_pfn)
606 {
607 	unsigned long zone_start_pfn = zone->zone_start_pfn;
608 	unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
609 	unsigned long zone_end_pfn = z;
610 	unsigned long pfn;
611 	struct mem_section *ms;
612 	int nid = zone_to_nid(zone);
613 
614 	zone_span_writelock(zone);
615 	if (zone_start_pfn == start_pfn) {
616 		/*
617 		 * If the section is smallest section in the zone, it need
618 		 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
619 		 * In this case, we find second smallest valid mem_section
620 		 * for shrinking zone.
621 		 */
622 		pfn = find_smallest_section_pfn(nid, zone, end_pfn,
623 						zone_end_pfn);
624 		if (pfn) {
625 			zone->zone_start_pfn = pfn;
626 			zone->spanned_pages = zone_end_pfn - pfn;
627 		}
628 	} else if (zone_end_pfn == end_pfn) {
629 		/*
630 		 * If the section is biggest section in the zone, it need
631 		 * shrink zone->spanned_pages.
632 		 * In this case, we find second biggest valid mem_section for
633 		 * shrinking zone.
634 		 */
635 		pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
636 					       start_pfn);
637 		if (pfn)
638 			zone->spanned_pages = pfn - zone_start_pfn + 1;
639 	}
640 
641 	/*
642 	 * The section is not biggest or smallest mem_section in the zone, it
643 	 * only creates a hole in the zone. So in this case, we need not
644 	 * change the zone. But perhaps, the zone has only hole data. Thus
645 	 * it check the zone has only hole or not.
646 	 */
647 	pfn = zone_start_pfn;
648 	for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
649 		ms = __pfn_to_section(pfn);
650 
651 		if (unlikely(!valid_section(ms)))
652 			continue;
653 
654 		if (page_zone(pfn_to_page(pfn)) != zone)
655 			continue;
656 
657 		 /* If the section is current section, it continues the loop */
658 		if (start_pfn == pfn)
659 			continue;
660 
661 		/* If we find valid section, we have nothing to do */
662 		zone_span_writeunlock(zone);
663 		return;
664 	}
665 
666 	/* The zone has no valid section */
667 	zone->zone_start_pfn = 0;
668 	zone->spanned_pages = 0;
669 	zone_span_writeunlock(zone);
670 }
671 
672 static void shrink_pgdat_span(struct pglist_data *pgdat,
673 			      unsigned long start_pfn, unsigned long end_pfn)
674 {
675 	unsigned long pgdat_start_pfn = pgdat->node_start_pfn;
676 	unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */
677 	unsigned long pgdat_end_pfn = p;
678 	unsigned long pfn;
679 	struct mem_section *ms;
680 	int nid = pgdat->node_id;
681 
682 	if (pgdat_start_pfn == start_pfn) {
683 		/*
684 		 * If the section is smallest section in the pgdat, it need
685 		 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
686 		 * In this case, we find second smallest valid mem_section
687 		 * for shrinking zone.
688 		 */
689 		pfn = find_smallest_section_pfn(nid, NULL, end_pfn,
690 						pgdat_end_pfn);
691 		if (pfn) {
692 			pgdat->node_start_pfn = pfn;
693 			pgdat->node_spanned_pages = pgdat_end_pfn - pfn;
694 		}
695 	} else if (pgdat_end_pfn == end_pfn) {
696 		/*
697 		 * If the section is biggest section in the pgdat, it need
698 		 * shrink pgdat->node_spanned_pages.
699 		 * In this case, we find second biggest valid mem_section for
700 		 * shrinking zone.
701 		 */
702 		pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn,
703 					       start_pfn);
704 		if (pfn)
705 			pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1;
706 	}
707 
708 	/*
709 	 * If the section is not biggest or smallest mem_section in the pgdat,
710 	 * it only creates a hole in the pgdat. So in this case, we need not
711 	 * change the pgdat.
712 	 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
713 	 * has only hole or not.
714 	 */
715 	pfn = pgdat_start_pfn;
716 	for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) {
717 		ms = __pfn_to_section(pfn);
718 
719 		if (unlikely(!valid_section(ms)))
720 			continue;
721 
722 		if (pfn_to_nid(pfn) != nid)
723 			continue;
724 
725 		 /* If the section is current section, it continues the loop */
726 		if (start_pfn == pfn)
727 			continue;
728 
729 		/* If we find valid section, we have nothing to do */
730 		return;
731 	}
732 
733 	/* The pgdat has no valid section */
734 	pgdat->node_start_pfn = 0;
735 	pgdat->node_spanned_pages = 0;
736 }
737 
738 static void __remove_zone(struct zone *zone, unsigned long start_pfn)
739 {
740 	struct pglist_data *pgdat = zone->zone_pgdat;
741 	int nr_pages = PAGES_PER_SECTION;
742 	int zone_type;
743 	unsigned long flags;
744 
745 	zone_type = zone - pgdat->node_zones;
746 
747 	pgdat_resize_lock(zone->zone_pgdat, &flags);
748 	shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
749 	shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages);
750 	pgdat_resize_unlock(zone->zone_pgdat, &flags);
751 }
752 
753 static int __remove_section(struct zone *zone, struct mem_section *ms,
754 		unsigned long map_offset)
755 {
756 	unsigned long start_pfn;
757 	int scn_nr;
758 	int ret = -EINVAL;
759 
760 	if (!valid_section(ms))
761 		return ret;
762 
763 	ret = unregister_memory_section(ms);
764 	if (ret)
765 		return ret;
766 
767 	scn_nr = __section_nr(ms);
768 	start_pfn = section_nr_to_pfn(scn_nr);
769 	__remove_zone(zone, start_pfn);
770 
771 	sparse_remove_one_section(zone, ms, map_offset);
772 	return 0;
773 }
774 
775 /**
776  * __remove_pages() - remove sections of pages from a zone
777  * @zone: zone from which pages need to be removed
778  * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
779  * @nr_pages: number of pages to remove (must be multiple of section size)
780  *
781  * Generic helper function to remove section mappings and sysfs entries
782  * for the section of the memory we are removing. Caller needs to make
783  * sure that pages are marked reserved and zones are adjust properly by
784  * calling offline_pages().
785  */
786 int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
787 		 unsigned long nr_pages)
788 {
789 	unsigned long i;
790 	unsigned long map_offset = 0;
791 	int sections_to_remove, ret = 0;
792 
793 	/* In the ZONE_DEVICE case device driver owns the memory region */
794 	if (is_dev_zone(zone)) {
795 		struct page *page = pfn_to_page(phys_start_pfn);
796 		struct vmem_altmap *altmap;
797 
798 		altmap = to_vmem_altmap((unsigned long) page);
799 		if (altmap)
800 			map_offset = vmem_altmap_offset(altmap);
801 	} else {
802 		resource_size_t start, size;
803 
804 		start = phys_start_pfn << PAGE_SHIFT;
805 		size = nr_pages * PAGE_SIZE;
806 
807 		ret = release_mem_region_adjustable(&iomem_resource, start,
808 					size);
809 		if (ret) {
810 			resource_size_t endres = start + size - 1;
811 
812 			pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
813 					&start, &endres, ret);
814 		}
815 	}
816 
817 	/*
818 	 * We can only remove entire sections
819 	 */
820 	BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
821 	BUG_ON(nr_pages % PAGES_PER_SECTION);
822 
823 	sections_to_remove = nr_pages / PAGES_PER_SECTION;
824 	for (i = 0; i < sections_to_remove; i++) {
825 		unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
826 
827 		ret = __remove_section(zone, __pfn_to_section(pfn), map_offset);
828 		map_offset = 0;
829 		if (ret)
830 			break;
831 	}
832 	return ret;
833 }
834 EXPORT_SYMBOL_GPL(__remove_pages);
835 #endif /* CONFIG_MEMORY_HOTREMOVE */
836 
837 int set_online_page_callback(online_page_callback_t callback)
838 {
839 	int rc = -EINVAL;
840 
841 	get_online_mems();
842 	mutex_lock(&online_page_callback_lock);
843 
844 	if (online_page_callback == generic_online_page) {
845 		online_page_callback = callback;
846 		rc = 0;
847 	}
848 
849 	mutex_unlock(&online_page_callback_lock);
850 	put_online_mems();
851 
852 	return rc;
853 }
854 EXPORT_SYMBOL_GPL(set_online_page_callback);
855 
856 int restore_online_page_callback(online_page_callback_t callback)
857 {
858 	int rc = -EINVAL;
859 
860 	get_online_mems();
861 	mutex_lock(&online_page_callback_lock);
862 
863 	if (online_page_callback == callback) {
864 		online_page_callback = generic_online_page;
865 		rc = 0;
866 	}
867 
868 	mutex_unlock(&online_page_callback_lock);
869 	put_online_mems();
870 
871 	return rc;
872 }
873 EXPORT_SYMBOL_GPL(restore_online_page_callback);
874 
875 void __online_page_set_limits(struct page *page)
876 {
877 }
878 EXPORT_SYMBOL_GPL(__online_page_set_limits);
879 
880 void __online_page_increment_counters(struct page *page)
881 {
882 	adjust_managed_page_count(page, 1);
883 }
884 EXPORT_SYMBOL_GPL(__online_page_increment_counters);
885 
886 void __online_page_free(struct page *page)
887 {
888 	__free_reserved_page(page);
889 }
890 EXPORT_SYMBOL_GPL(__online_page_free);
891 
892 static void generic_online_page(struct page *page)
893 {
894 	__online_page_set_limits(page);
895 	__online_page_increment_counters(page);
896 	__online_page_free(page);
897 }
898 
899 static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
900 			void *arg)
901 {
902 	unsigned long i;
903 	unsigned long onlined_pages = *(unsigned long *)arg;
904 	struct page *page;
905 	if (PageReserved(pfn_to_page(start_pfn)))
906 		for (i = 0; i < nr_pages; i++) {
907 			page = pfn_to_page(start_pfn + i);
908 			(*online_page_callback)(page);
909 			onlined_pages++;
910 		}
911 	*(unsigned long *)arg = onlined_pages;
912 	return 0;
913 }
914 
915 #ifdef CONFIG_MOVABLE_NODE
916 /*
917  * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
918  * normal memory.
919  */
920 static bool can_online_high_movable(struct zone *zone)
921 {
922 	return true;
923 }
924 #else /* CONFIG_MOVABLE_NODE */
925 /* ensure every online node has NORMAL memory */
926 static bool can_online_high_movable(struct zone *zone)
927 {
928 	return node_state(zone_to_nid(zone), N_NORMAL_MEMORY);
929 }
930 #endif /* CONFIG_MOVABLE_NODE */
931 
932 /* check which state of node_states will be changed when online memory */
933 static void node_states_check_changes_online(unsigned long nr_pages,
934 	struct zone *zone, struct memory_notify *arg)
935 {
936 	int nid = zone_to_nid(zone);
937 	enum zone_type zone_last = ZONE_NORMAL;
938 
939 	/*
940 	 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
941 	 * contains nodes which have zones of 0...ZONE_NORMAL,
942 	 * set zone_last to ZONE_NORMAL.
943 	 *
944 	 * If we don't have HIGHMEM nor movable node,
945 	 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
946 	 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
947 	 */
948 	if (N_MEMORY == N_NORMAL_MEMORY)
949 		zone_last = ZONE_MOVABLE;
950 
951 	/*
952 	 * if the memory to be online is in a zone of 0...zone_last, and
953 	 * the zones of 0...zone_last don't have memory before online, we will
954 	 * need to set the node to node_states[N_NORMAL_MEMORY] after
955 	 * the memory is online.
956 	 */
957 	if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
958 		arg->status_change_nid_normal = nid;
959 	else
960 		arg->status_change_nid_normal = -1;
961 
962 #ifdef CONFIG_HIGHMEM
963 	/*
964 	 * If we have movable node, node_states[N_HIGH_MEMORY]
965 	 * contains nodes which have zones of 0...ZONE_HIGHMEM,
966 	 * set zone_last to ZONE_HIGHMEM.
967 	 *
968 	 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
969 	 * contains nodes which have zones of 0...ZONE_MOVABLE,
970 	 * set zone_last to ZONE_MOVABLE.
971 	 */
972 	zone_last = ZONE_HIGHMEM;
973 	if (N_MEMORY == N_HIGH_MEMORY)
974 		zone_last = ZONE_MOVABLE;
975 
976 	if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
977 		arg->status_change_nid_high = nid;
978 	else
979 		arg->status_change_nid_high = -1;
980 #else
981 	arg->status_change_nid_high = arg->status_change_nid_normal;
982 #endif
983 
984 	/*
985 	 * if the node don't have memory befor online, we will need to
986 	 * set the node to node_states[N_MEMORY] after the memory
987 	 * is online.
988 	 */
989 	if (!node_state(nid, N_MEMORY))
990 		arg->status_change_nid = nid;
991 	else
992 		arg->status_change_nid = -1;
993 }
994 
995 static void node_states_set_node(int node, struct memory_notify *arg)
996 {
997 	if (arg->status_change_nid_normal >= 0)
998 		node_set_state(node, N_NORMAL_MEMORY);
999 
1000 	if (arg->status_change_nid_high >= 0)
1001 		node_set_state(node, N_HIGH_MEMORY);
1002 
1003 	node_set_state(node, N_MEMORY);
1004 }
1005 
1006 
1007 /* Must be protected by mem_hotplug_begin() */
1008 int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
1009 {
1010 	unsigned long flags;
1011 	unsigned long onlined_pages = 0;
1012 	struct zone *zone;
1013 	int need_zonelists_rebuild = 0;
1014 	int nid;
1015 	int ret;
1016 	struct memory_notify arg;
1017 
1018 	/*
1019 	 * This doesn't need a lock to do pfn_to_page().
1020 	 * The section can't be removed here because of the
1021 	 * memory_block->state_mutex.
1022 	 */
1023 	zone = page_zone(pfn_to_page(pfn));
1024 
1025 	if ((zone_idx(zone) > ZONE_NORMAL ||
1026 	    online_type == MMOP_ONLINE_MOVABLE) &&
1027 	    !can_online_high_movable(zone))
1028 		return -EINVAL;
1029 
1030 	if (online_type == MMOP_ONLINE_KERNEL &&
1031 	    zone_idx(zone) == ZONE_MOVABLE) {
1032 		if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages))
1033 			return -EINVAL;
1034 	}
1035 	if (online_type == MMOP_ONLINE_MOVABLE &&
1036 	    zone_idx(zone) == ZONE_MOVABLE - 1) {
1037 		if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages))
1038 			return -EINVAL;
1039 	}
1040 
1041 	/* Previous code may changed the zone of the pfn range */
1042 	zone = page_zone(pfn_to_page(pfn));
1043 
1044 	arg.start_pfn = pfn;
1045 	arg.nr_pages = nr_pages;
1046 	node_states_check_changes_online(nr_pages, zone, &arg);
1047 
1048 	nid = pfn_to_nid(pfn);
1049 
1050 	ret = memory_notify(MEM_GOING_ONLINE, &arg);
1051 	ret = notifier_to_errno(ret);
1052 	if (ret) {
1053 		memory_notify(MEM_CANCEL_ONLINE, &arg);
1054 		return ret;
1055 	}
1056 	/*
1057 	 * If this zone is not populated, then it is not in zonelist.
1058 	 * This means the page allocator ignores this zone.
1059 	 * So, zonelist must be updated after online.
1060 	 */
1061 	mutex_lock(&zonelists_mutex);
1062 	if (!populated_zone(zone)) {
1063 		need_zonelists_rebuild = 1;
1064 		build_all_zonelists(NULL, zone);
1065 	}
1066 
1067 	ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
1068 		online_pages_range);
1069 	if (ret) {
1070 		if (need_zonelists_rebuild)
1071 			zone_pcp_reset(zone);
1072 		mutex_unlock(&zonelists_mutex);
1073 		printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n",
1074 		       (unsigned long long) pfn << PAGE_SHIFT,
1075 		       (((unsigned long long) pfn + nr_pages)
1076 			    << PAGE_SHIFT) - 1);
1077 		memory_notify(MEM_CANCEL_ONLINE, &arg);
1078 		return ret;
1079 	}
1080 
1081 	zone->present_pages += onlined_pages;
1082 
1083 	pgdat_resize_lock(zone->zone_pgdat, &flags);
1084 	zone->zone_pgdat->node_present_pages += onlined_pages;
1085 	pgdat_resize_unlock(zone->zone_pgdat, &flags);
1086 
1087 	if (onlined_pages) {
1088 		node_states_set_node(zone_to_nid(zone), &arg);
1089 		if (need_zonelists_rebuild)
1090 			build_all_zonelists(NULL, NULL);
1091 		else
1092 			zone_pcp_update(zone);
1093 	}
1094 
1095 	mutex_unlock(&zonelists_mutex);
1096 
1097 	init_per_zone_wmark_min();
1098 
1099 	if (onlined_pages)
1100 		kswapd_run(zone_to_nid(zone));
1101 
1102 	vm_total_pages = nr_free_pagecache_pages();
1103 
1104 	writeback_set_ratelimit();
1105 
1106 	if (onlined_pages)
1107 		memory_notify(MEM_ONLINE, &arg);
1108 	return 0;
1109 }
1110 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
1111 
1112 static void reset_node_present_pages(pg_data_t *pgdat)
1113 {
1114 	struct zone *z;
1115 
1116 	for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
1117 		z->present_pages = 0;
1118 
1119 	pgdat->node_present_pages = 0;
1120 }
1121 
1122 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1123 static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
1124 {
1125 	struct pglist_data *pgdat;
1126 	unsigned long zones_size[MAX_NR_ZONES] = {0};
1127 	unsigned long zholes_size[MAX_NR_ZONES] = {0};
1128 	unsigned long start_pfn = PFN_DOWN(start);
1129 
1130 	pgdat = NODE_DATA(nid);
1131 	if (!pgdat) {
1132 		pgdat = arch_alloc_nodedata(nid);
1133 		if (!pgdat)
1134 			return NULL;
1135 
1136 		arch_refresh_nodedata(nid, pgdat);
1137 	} else {
1138 		/* Reset the nr_zones and classzone_idx to 0 before reuse */
1139 		pgdat->nr_zones = 0;
1140 		pgdat->classzone_idx = 0;
1141 	}
1142 
1143 	/* we can use NODE_DATA(nid) from here */
1144 
1145 	/* init node's zones as empty zones, we don't have any present pages.*/
1146 	free_area_init_node(nid, zones_size, start_pfn, zholes_size);
1147 
1148 	/*
1149 	 * The node we allocated has no zone fallback lists. For avoiding
1150 	 * to access not-initialized zonelist, build here.
1151 	 */
1152 	mutex_lock(&zonelists_mutex);
1153 	build_all_zonelists(pgdat, NULL);
1154 	mutex_unlock(&zonelists_mutex);
1155 
1156 	/*
1157 	 * zone->managed_pages is set to an approximate value in
1158 	 * free_area_init_core(), which will cause
1159 	 * /sys/device/system/node/nodeX/meminfo has wrong data.
1160 	 * So reset it to 0 before any memory is onlined.
1161 	 */
1162 	reset_node_managed_pages(pgdat);
1163 
1164 	/*
1165 	 * When memory is hot-added, all the memory is in offline state. So
1166 	 * clear all zones' present_pages because they will be updated in
1167 	 * online_pages() and offline_pages().
1168 	 */
1169 	reset_node_present_pages(pgdat);
1170 
1171 	return pgdat;
1172 }
1173 
1174 static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
1175 {
1176 	arch_refresh_nodedata(nid, NULL);
1177 	arch_free_nodedata(pgdat);
1178 	return;
1179 }
1180 
1181 
1182 /**
1183  * try_online_node - online a node if offlined
1184  *
1185  * called by cpu_up() to online a node without onlined memory.
1186  */
1187 int try_online_node(int nid)
1188 {
1189 	pg_data_t	*pgdat;
1190 	int	ret;
1191 
1192 	if (node_online(nid))
1193 		return 0;
1194 
1195 	mem_hotplug_begin();
1196 	pgdat = hotadd_new_pgdat(nid, 0);
1197 	if (!pgdat) {
1198 		pr_err("Cannot online node %d due to NULL pgdat\n", nid);
1199 		ret = -ENOMEM;
1200 		goto out;
1201 	}
1202 	node_set_online(nid);
1203 	ret = register_one_node(nid);
1204 	BUG_ON(ret);
1205 
1206 	if (pgdat->node_zonelists->_zonerefs->zone == NULL) {
1207 		mutex_lock(&zonelists_mutex);
1208 		build_all_zonelists(NULL, NULL);
1209 		mutex_unlock(&zonelists_mutex);
1210 	}
1211 
1212 out:
1213 	mem_hotplug_done();
1214 	return ret;
1215 }
1216 
1217 static int check_hotplug_memory_range(u64 start, u64 size)
1218 {
1219 	u64 start_pfn = PFN_DOWN(start);
1220 	u64 nr_pages = size >> PAGE_SHIFT;
1221 
1222 	/* Memory range must be aligned with section */
1223 	if ((start_pfn & ~PAGE_SECTION_MASK) ||
1224 	    (nr_pages % PAGES_PER_SECTION) || (!nr_pages)) {
1225 		pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
1226 				(unsigned long long)start,
1227 				(unsigned long long)size);
1228 		return -EINVAL;
1229 	}
1230 
1231 	return 0;
1232 }
1233 
1234 /*
1235  * If movable zone has already been setup, newly added memory should be check.
1236  * If its address is higher than movable zone, it should be added as movable.
1237  * Without this check, movable zone may overlap with other zone.
1238  */
1239 static int should_add_memory_movable(int nid, u64 start, u64 size)
1240 {
1241 	unsigned long start_pfn = start >> PAGE_SHIFT;
1242 	pg_data_t *pgdat = NODE_DATA(nid);
1243 	struct zone *movable_zone = pgdat->node_zones + ZONE_MOVABLE;
1244 
1245 	if (zone_is_empty(movable_zone))
1246 		return 0;
1247 
1248 	if (movable_zone->zone_start_pfn <= start_pfn)
1249 		return 1;
1250 
1251 	return 0;
1252 }
1253 
1254 int zone_for_memory(int nid, u64 start, u64 size, int zone_default,
1255 		bool for_device)
1256 {
1257 #ifdef CONFIG_ZONE_DEVICE
1258 	if (for_device)
1259 		return ZONE_DEVICE;
1260 #endif
1261 	if (should_add_memory_movable(nid, start, size))
1262 		return ZONE_MOVABLE;
1263 
1264 	return zone_default;
1265 }
1266 
1267 static int online_memory_block(struct memory_block *mem, void *arg)
1268 {
1269 	return memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
1270 }
1271 
1272 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1273 int __ref add_memory_resource(int nid, struct resource *res, bool online)
1274 {
1275 	u64 start, size;
1276 	pg_data_t *pgdat = NULL;
1277 	bool new_pgdat;
1278 	bool new_node;
1279 	int ret;
1280 
1281 	start = res->start;
1282 	size = resource_size(res);
1283 
1284 	ret = check_hotplug_memory_range(start, size);
1285 	if (ret)
1286 		return ret;
1287 
1288 	{	/* Stupid hack to suppress address-never-null warning */
1289 		void *p = NODE_DATA(nid);
1290 		new_pgdat = !p;
1291 	}
1292 
1293 	mem_hotplug_begin();
1294 
1295 	/*
1296 	 * Add new range to memblock so that when hotadd_new_pgdat() is called
1297 	 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
1298 	 * this new range and calculate total pages correctly.  The range will
1299 	 * be removed at hot-remove time.
1300 	 */
1301 	memblock_add_node(start, size, nid);
1302 
1303 	new_node = !node_online(nid);
1304 	if (new_node) {
1305 		pgdat = hotadd_new_pgdat(nid, start);
1306 		ret = -ENOMEM;
1307 		if (!pgdat)
1308 			goto error;
1309 	}
1310 
1311 	/* call arch's memory hotadd */
1312 	ret = arch_add_memory(nid, start, size, false);
1313 
1314 	if (ret < 0)
1315 		goto error;
1316 
1317 	/* we online node here. we can't roll back from here. */
1318 	node_set_online(nid);
1319 
1320 	if (new_node) {
1321 		ret = register_one_node(nid);
1322 		/*
1323 		 * If sysfs file of new node can't create, cpu on the node
1324 		 * can't be hot-added. There is no rollback way now.
1325 		 * So, check by BUG_ON() to catch it reluctantly..
1326 		 */
1327 		BUG_ON(ret);
1328 	}
1329 
1330 	/* create new memmap entry */
1331 	firmware_map_add_hotplug(start, start + size, "System RAM");
1332 
1333 	/* online pages if requested */
1334 	if (online)
1335 		walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1),
1336 				  NULL, online_memory_block);
1337 
1338 	goto out;
1339 
1340 error:
1341 	/* rollback pgdat allocation and others */
1342 	if (new_pgdat)
1343 		rollback_node_hotadd(nid, pgdat);
1344 	memblock_remove(start, size);
1345 
1346 out:
1347 	mem_hotplug_done();
1348 	return ret;
1349 }
1350 EXPORT_SYMBOL_GPL(add_memory_resource);
1351 
1352 int __ref add_memory(int nid, u64 start, u64 size)
1353 {
1354 	struct resource *res;
1355 	int ret;
1356 
1357 	res = register_memory_resource(start, size);
1358 	if (IS_ERR(res))
1359 		return PTR_ERR(res);
1360 
1361 	ret = add_memory_resource(nid, res, memhp_auto_online);
1362 	if (ret < 0)
1363 		release_memory_resource(res);
1364 	return ret;
1365 }
1366 EXPORT_SYMBOL_GPL(add_memory);
1367 
1368 #ifdef CONFIG_MEMORY_HOTREMOVE
1369 /*
1370  * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1371  * set and the size of the free page is given by page_order(). Using this,
1372  * the function determines if the pageblock contains only free pages.
1373  * Due to buddy contraints, a free page at least the size of a pageblock will
1374  * be located at the start of the pageblock
1375  */
1376 static inline int pageblock_free(struct page *page)
1377 {
1378 	return PageBuddy(page) && page_order(page) >= pageblock_order;
1379 }
1380 
1381 /* Return the start of the next active pageblock after a given page */
1382 static struct page *next_active_pageblock(struct page *page)
1383 {
1384 	/* Ensure the starting page is pageblock-aligned */
1385 	BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
1386 
1387 	/* If the entire pageblock is free, move to the end of free page */
1388 	if (pageblock_free(page)) {
1389 		int order;
1390 		/* be careful. we don't have locks, page_order can be changed.*/
1391 		order = page_order(page);
1392 		if ((order < MAX_ORDER) && (order >= pageblock_order))
1393 			return page + (1 << order);
1394 	}
1395 
1396 	return page + pageblock_nr_pages;
1397 }
1398 
1399 /* Checks if this range of memory is likely to be hot-removable. */
1400 int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
1401 {
1402 	struct page *page = pfn_to_page(start_pfn);
1403 	struct page *end_page = page + nr_pages;
1404 
1405 	/* Check the starting page of each pageblock within the range */
1406 	for (; page < end_page; page = next_active_pageblock(page)) {
1407 		if (!is_pageblock_removable_nolock(page))
1408 			return 0;
1409 		cond_resched();
1410 	}
1411 
1412 	/* All pageblocks in the memory block are likely to be hot-removable */
1413 	return 1;
1414 }
1415 
1416 /*
1417  * Confirm all pages in a range [start, end) is belongs to the same zone.
1418  */
1419 int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
1420 {
1421 	unsigned long pfn, sec_end_pfn;
1422 	struct zone *zone = NULL;
1423 	struct page *page;
1424 	int i;
1425 	for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn);
1426 	     pfn < end_pfn;
1427 	     pfn = sec_end_pfn + 1, sec_end_pfn += PAGES_PER_SECTION) {
1428 		/* Make sure the memory section is present first */
1429 		if (!present_section_nr(pfn_to_section_nr(pfn)))
1430 			continue;
1431 		for (; pfn < sec_end_pfn && pfn < end_pfn;
1432 		     pfn += MAX_ORDER_NR_PAGES) {
1433 			i = 0;
1434 			/* This is just a CONFIG_HOLES_IN_ZONE check.*/
1435 			while ((i < MAX_ORDER_NR_PAGES) &&
1436 				!pfn_valid_within(pfn + i))
1437 				i++;
1438 			if (i == MAX_ORDER_NR_PAGES)
1439 				continue;
1440 			page = pfn_to_page(pfn + i);
1441 			if (zone && page_zone(page) != zone)
1442 				return 0;
1443 			zone = page_zone(page);
1444 		}
1445 	}
1446 	return 1;
1447 }
1448 
1449 /*
1450  * Scan pfn range [start,end) to find movable/migratable pages (LRU pages
1451  * and hugepages). We scan pfn because it's much easier than scanning over
1452  * linked list. This function returns the pfn of the first found movable
1453  * page if it's found, otherwise 0.
1454  */
1455 static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
1456 {
1457 	unsigned long pfn;
1458 	struct page *page;
1459 	for (pfn = start; pfn < end; pfn++) {
1460 		if (pfn_valid(pfn)) {
1461 			page = pfn_to_page(pfn);
1462 			if (PageLRU(page))
1463 				return pfn;
1464 			if (PageHuge(page)) {
1465 				if (page_huge_active(page))
1466 					return pfn;
1467 				else
1468 					pfn = round_up(pfn + 1,
1469 						1 << compound_order(page)) - 1;
1470 			}
1471 		}
1472 	}
1473 	return 0;
1474 }
1475 
1476 #define NR_OFFLINE_AT_ONCE_PAGES	(256)
1477 static int
1478 do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1479 {
1480 	unsigned long pfn;
1481 	struct page *page;
1482 	int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
1483 	int not_managed = 0;
1484 	int ret = 0;
1485 	LIST_HEAD(source);
1486 
1487 	for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
1488 		if (!pfn_valid(pfn))
1489 			continue;
1490 		page = pfn_to_page(pfn);
1491 
1492 		if (PageHuge(page)) {
1493 			struct page *head = compound_head(page);
1494 			pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
1495 			if (compound_order(head) > PFN_SECTION_SHIFT) {
1496 				ret = -EBUSY;
1497 				break;
1498 			}
1499 			if (isolate_huge_page(page, &source))
1500 				move_pages -= 1 << compound_order(head);
1501 			continue;
1502 		}
1503 
1504 		if (!get_page_unless_zero(page))
1505 			continue;
1506 		/*
1507 		 * We can skip free pages. And we can only deal with pages on
1508 		 * LRU.
1509 		 */
1510 		ret = isolate_lru_page(page);
1511 		if (!ret) { /* Success */
1512 			put_page(page);
1513 			list_add_tail(&page->lru, &source);
1514 			move_pages--;
1515 			inc_zone_page_state(page, NR_ISOLATED_ANON +
1516 					    page_is_file_cache(page));
1517 
1518 		} else {
1519 #ifdef CONFIG_DEBUG_VM
1520 			printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
1521 			       pfn);
1522 			dump_page(page, "failed to remove from LRU");
1523 #endif
1524 			put_page(page);
1525 			/* Because we don't have big zone->lock. we should
1526 			   check this again here. */
1527 			if (page_count(page)) {
1528 				not_managed++;
1529 				ret = -EBUSY;
1530 				break;
1531 			}
1532 		}
1533 	}
1534 	if (!list_empty(&source)) {
1535 		if (not_managed) {
1536 			putback_movable_pages(&source);
1537 			goto out;
1538 		}
1539 
1540 		/*
1541 		 * alloc_migrate_target should be improooooved!!
1542 		 * migrate_pages returns # of failed pages.
1543 		 */
1544 		ret = migrate_pages(&source, alloc_migrate_target, NULL, 0,
1545 					MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
1546 		if (ret)
1547 			putback_movable_pages(&source);
1548 	}
1549 out:
1550 	return ret;
1551 }
1552 
1553 /*
1554  * remove from free_area[] and mark all as Reserved.
1555  */
1556 static int
1557 offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1558 			void *data)
1559 {
1560 	__offline_isolated_pages(start, start + nr_pages);
1561 	return 0;
1562 }
1563 
1564 static void
1565 offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1566 {
1567 	walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
1568 				offline_isolated_pages_cb);
1569 }
1570 
1571 /*
1572  * Check all pages in range, recoreded as memory resource, are isolated.
1573  */
1574 static int
1575 check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1576 			void *data)
1577 {
1578 	int ret;
1579 	long offlined = *(long *)data;
1580 	ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
1581 	offlined = nr_pages;
1582 	if (!ret)
1583 		*(long *)data += offlined;
1584 	return ret;
1585 }
1586 
1587 static long
1588 check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1589 {
1590 	long offlined = 0;
1591 	int ret;
1592 
1593 	ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
1594 			check_pages_isolated_cb);
1595 	if (ret < 0)
1596 		offlined = (long)ret;
1597 	return offlined;
1598 }
1599 
1600 #ifdef CONFIG_MOVABLE_NODE
1601 /*
1602  * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
1603  * normal memory.
1604  */
1605 static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1606 {
1607 	return true;
1608 }
1609 #else /* CONFIG_MOVABLE_NODE */
1610 /* ensure the node has NORMAL memory if it is still online */
1611 static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1612 {
1613 	struct pglist_data *pgdat = zone->zone_pgdat;
1614 	unsigned long present_pages = 0;
1615 	enum zone_type zt;
1616 
1617 	for (zt = 0; zt <= ZONE_NORMAL; zt++)
1618 		present_pages += pgdat->node_zones[zt].present_pages;
1619 
1620 	if (present_pages > nr_pages)
1621 		return true;
1622 
1623 	present_pages = 0;
1624 	for (; zt <= ZONE_MOVABLE; zt++)
1625 		present_pages += pgdat->node_zones[zt].present_pages;
1626 
1627 	/*
1628 	 * we can't offline the last normal memory until all
1629 	 * higher memory is offlined.
1630 	 */
1631 	return present_pages == 0;
1632 }
1633 #endif /* CONFIG_MOVABLE_NODE */
1634 
1635 static int __init cmdline_parse_movable_node(char *p)
1636 {
1637 #ifdef CONFIG_MOVABLE_NODE
1638 	/*
1639 	 * Memory used by the kernel cannot be hot-removed because Linux
1640 	 * cannot migrate the kernel pages. When memory hotplug is
1641 	 * enabled, we should prevent memblock from allocating memory
1642 	 * for the kernel.
1643 	 *
1644 	 * ACPI SRAT records all hotpluggable memory ranges. But before
1645 	 * SRAT is parsed, we don't know about it.
1646 	 *
1647 	 * The kernel image is loaded into memory at very early time. We
1648 	 * cannot prevent this anyway. So on NUMA system, we set any
1649 	 * node the kernel resides in as un-hotpluggable.
1650 	 *
1651 	 * Since on modern servers, one node could have double-digit
1652 	 * gigabytes memory, we can assume the memory around the kernel
1653 	 * image is also un-hotpluggable. So before SRAT is parsed, just
1654 	 * allocate memory near the kernel image to try the best to keep
1655 	 * the kernel away from hotpluggable memory.
1656 	 */
1657 	memblock_set_bottom_up(true);
1658 	movable_node_enabled = true;
1659 #else
1660 	pr_warn("movable_node option not supported\n");
1661 #endif
1662 	return 0;
1663 }
1664 early_param("movable_node", cmdline_parse_movable_node);
1665 
1666 /* check which state of node_states will be changed when offline memory */
1667 static void node_states_check_changes_offline(unsigned long nr_pages,
1668 		struct zone *zone, struct memory_notify *arg)
1669 {
1670 	struct pglist_data *pgdat = zone->zone_pgdat;
1671 	unsigned long present_pages = 0;
1672 	enum zone_type zt, zone_last = ZONE_NORMAL;
1673 
1674 	/*
1675 	 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1676 	 * contains nodes which have zones of 0...ZONE_NORMAL,
1677 	 * set zone_last to ZONE_NORMAL.
1678 	 *
1679 	 * If we don't have HIGHMEM nor movable node,
1680 	 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1681 	 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
1682 	 */
1683 	if (N_MEMORY == N_NORMAL_MEMORY)
1684 		zone_last = ZONE_MOVABLE;
1685 
1686 	/*
1687 	 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1688 	 * If the memory to be offline is in a zone of 0...zone_last,
1689 	 * and it is the last present memory, 0...zone_last will
1690 	 * become empty after offline , thus we can determind we will
1691 	 * need to clear the node from node_states[N_NORMAL_MEMORY].
1692 	 */
1693 	for (zt = 0; zt <= zone_last; zt++)
1694 		present_pages += pgdat->node_zones[zt].present_pages;
1695 	if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1696 		arg->status_change_nid_normal = zone_to_nid(zone);
1697 	else
1698 		arg->status_change_nid_normal = -1;
1699 
1700 #ifdef CONFIG_HIGHMEM
1701 	/*
1702 	 * If we have movable node, node_states[N_HIGH_MEMORY]
1703 	 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1704 	 * set zone_last to ZONE_HIGHMEM.
1705 	 *
1706 	 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1707 	 * contains nodes which have zones of 0...ZONE_MOVABLE,
1708 	 * set zone_last to ZONE_MOVABLE.
1709 	 */
1710 	zone_last = ZONE_HIGHMEM;
1711 	if (N_MEMORY == N_HIGH_MEMORY)
1712 		zone_last = ZONE_MOVABLE;
1713 
1714 	for (; zt <= zone_last; zt++)
1715 		present_pages += pgdat->node_zones[zt].present_pages;
1716 	if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1717 		arg->status_change_nid_high = zone_to_nid(zone);
1718 	else
1719 		arg->status_change_nid_high = -1;
1720 #else
1721 	arg->status_change_nid_high = arg->status_change_nid_normal;
1722 #endif
1723 
1724 	/*
1725 	 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1726 	 */
1727 	zone_last = ZONE_MOVABLE;
1728 
1729 	/*
1730 	 * check whether node_states[N_HIGH_MEMORY] will be changed
1731 	 * If we try to offline the last present @nr_pages from the node,
1732 	 * we can determind we will need to clear the node from
1733 	 * node_states[N_HIGH_MEMORY].
1734 	 */
1735 	for (; zt <= zone_last; zt++)
1736 		present_pages += pgdat->node_zones[zt].present_pages;
1737 	if (nr_pages >= present_pages)
1738 		arg->status_change_nid = zone_to_nid(zone);
1739 	else
1740 		arg->status_change_nid = -1;
1741 }
1742 
1743 static void node_states_clear_node(int node, struct memory_notify *arg)
1744 {
1745 	if (arg->status_change_nid_normal >= 0)
1746 		node_clear_state(node, N_NORMAL_MEMORY);
1747 
1748 	if ((N_MEMORY != N_NORMAL_MEMORY) &&
1749 	    (arg->status_change_nid_high >= 0))
1750 		node_clear_state(node, N_HIGH_MEMORY);
1751 
1752 	if ((N_MEMORY != N_HIGH_MEMORY) &&
1753 	    (arg->status_change_nid >= 0))
1754 		node_clear_state(node, N_MEMORY);
1755 }
1756 
1757 static int __ref __offline_pages(unsigned long start_pfn,
1758 		  unsigned long end_pfn, unsigned long timeout)
1759 {
1760 	unsigned long pfn, nr_pages, expire;
1761 	long offlined_pages;
1762 	int ret, drain, retry_max, node;
1763 	unsigned long flags;
1764 	struct zone *zone;
1765 	struct memory_notify arg;
1766 
1767 	/* at least, alignment against pageblock is necessary */
1768 	if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1769 		return -EINVAL;
1770 	if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1771 		return -EINVAL;
1772 	/* This makes hotplug much easier...and readable.
1773 	   we assume this for now. .*/
1774 	if (!test_pages_in_a_zone(start_pfn, end_pfn))
1775 		return -EINVAL;
1776 
1777 	zone = page_zone(pfn_to_page(start_pfn));
1778 	node = zone_to_nid(zone);
1779 	nr_pages = end_pfn - start_pfn;
1780 
1781 	if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
1782 		return -EINVAL;
1783 
1784 	/* set above range as isolated */
1785 	ret = start_isolate_page_range(start_pfn, end_pfn,
1786 				       MIGRATE_MOVABLE, true);
1787 	if (ret)
1788 		return ret;
1789 
1790 	arg.start_pfn = start_pfn;
1791 	arg.nr_pages = nr_pages;
1792 	node_states_check_changes_offline(nr_pages, zone, &arg);
1793 
1794 	ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1795 	ret = notifier_to_errno(ret);
1796 	if (ret)
1797 		goto failed_removal;
1798 
1799 	pfn = start_pfn;
1800 	expire = jiffies + timeout;
1801 	drain = 0;
1802 	retry_max = 5;
1803 repeat:
1804 	/* start memory hot removal */
1805 	ret = -EAGAIN;
1806 	if (time_after(jiffies, expire))
1807 		goto failed_removal;
1808 	ret = -EINTR;
1809 	if (signal_pending(current))
1810 		goto failed_removal;
1811 	ret = 0;
1812 	if (drain) {
1813 		lru_add_drain_all();
1814 		cond_resched();
1815 		drain_all_pages(zone);
1816 	}
1817 
1818 	pfn = scan_movable_pages(start_pfn, end_pfn);
1819 	if (pfn) { /* We have movable pages */
1820 		ret = do_migrate_range(pfn, end_pfn);
1821 		if (!ret) {
1822 			drain = 1;
1823 			goto repeat;
1824 		} else {
1825 			if (ret < 0)
1826 				if (--retry_max == 0)
1827 					goto failed_removal;
1828 			yield();
1829 			drain = 1;
1830 			goto repeat;
1831 		}
1832 	}
1833 	/* drain all zone's lru pagevec, this is asynchronous... */
1834 	lru_add_drain_all();
1835 	yield();
1836 	/* drain pcp pages, this is synchronous. */
1837 	drain_all_pages(zone);
1838 	/*
1839 	 * dissolve free hugepages in the memory block before doing offlining
1840 	 * actually in order to make hugetlbfs's object counting consistent.
1841 	 */
1842 	dissolve_free_huge_pages(start_pfn, end_pfn);
1843 	/* check again */
1844 	offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1845 	if (offlined_pages < 0) {
1846 		ret = -EBUSY;
1847 		goto failed_removal;
1848 	}
1849 	printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
1850 	/* Ok, all of our target is isolated.
1851 	   We cannot do rollback at this point. */
1852 	offline_isolated_pages(start_pfn, end_pfn);
1853 	/* reset pagetype flags and makes migrate type to be MOVABLE */
1854 	undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1855 	/* removal success */
1856 	adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
1857 	zone->present_pages -= offlined_pages;
1858 
1859 	pgdat_resize_lock(zone->zone_pgdat, &flags);
1860 	zone->zone_pgdat->node_present_pages -= offlined_pages;
1861 	pgdat_resize_unlock(zone->zone_pgdat, &flags);
1862 
1863 	init_per_zone_wmark_min();
1864 
1865 	if (!populated_zone(zone)) {
1866 		zone_pcp_reset(zone);
1867 		mutex_lock(&zonelists_mutex);
1868 		build_all_zonelists(NULL, NULL);
1869 		mutex_unlock(&zonelists_mutex);
1870 	} else
1871 		zone_pcp_update(zone);
1872 
1873 	node_states_clear_node(node, &arg);
1874 	if (arg.status_change_nid >= 0)
1875 		kswapd_stop(node);
1876 
1877 	vm_total_pages = nr_free_pagecache_pages();
1878 	writeback_set_ratelimit();
1879 
1880 	memory_notify(MEM_OFFLINE, &arg);
1881 	return 0;
1882 
1883 failed_removal:
1884 	printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n",
1885 	       (unsigned long long) start_pfn << PAGE_SHIFT,
1886 	       ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
1887 	memory_notify(MEM_CANCEL_OFFLINE, &arg);
1888 	/* pushback to free area */
1889 	undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1890 	return ret;
1891 }
1892 
1893 /* Must be protected by mem_hotplug_begin() */
1894 int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1895 {
1896 	return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
1897 }
1898 #endif /* CONFIG_MEMORY_HOTREMOVE */
1899 
1900 /**
1901  * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1902  * @start_pfn: start pfn of the memory range
1903  * @end_pfn: end pfn of the memory range
1904  * @arg: argument passed to func
1905  * @func: callback for each memory section walked
1906  *
1907  * This function walks through all present mem sections in range
1908  * [start_pfn, end_pfn) and call func on each mem section.
1909  *
1910  * Returns the return value of func.
1911  */
1912 int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
1913 		void *arg, int (*func)(struct memory_block *, void *))
1914 {
1915 	struct memory_block *mem = NULL;
1916 	struct mem_section *section;
1917 	unsigned long pfn, section_nr;
1918 	int ret;
1919 
1920 	for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1921 		section_nr = pfn_to_section_nr(pfn);
1922 		if (!present_section_nr(section_nr))
1923 			continue;
1924 
1925 		section = __nr_to_section(section_nr);
1926 		/* same memblock? */
1927 		if (mem)
1928 			if ((section_nr >= mem->start_section_nr) &&
1929 			    (section_nr <= mem->end_section_nr))
1930 				continue;
1931 
1932 		mem = find_memory_block_hinted(section, mem);
1933 		if (!mem)
1934 			continue;
1935 
1936 		ret = func(mem, arg);
1937 		if (ret) {
1938 			kobject_put(&mem->dev.kobj);
1939 			return ret;
1940 		}
1941 	}
1942 
1943 	if (mem)
1944 		kobject_put(&mem->dev.kobj);
1945 
1946 	return 0;
1947 }
1948 
1949 #ifdef CONFIG_MEMORY_HOTREMOVE
1950 static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
1951 {
1952 	int ret = !is_memblock_offlined(mem);
1953 
1954 	if (unlikely(ret)) {
1955 		phys_addr_t beginpa, endpa;
1956 
1957 		beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
1958 		endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
1959 		pr_warn("removing memory fails, because memory "
1960 			"[%pa-%pa] is onlined\n",
1961 			&beginpa, &endpa);
1962 	}
1963 
1964 	return ret;
1965 }
1966 
1967 static int check_cpu_on_node(pg_data_t *pgdat)
1968 {
1969 	int cpu;
1970 
1971 	for_each_present_cpu(cpu) {
1972 		if (cpu_to_node(cpu) == pgdat->node_id)
1973 			/*
1974 			 * the cpu on this node isn't removed, and we can't
1975 			 * offline this node.
1976 			 */
1977 			return -EBUSY;
1978 	}
1979 
1980 	return 0;
1981 }
1982 
1983 static void unmap_cpu_on_node(pg_data_t *pgdat)
1984 {
1985 #ifdef CONFIG_ACPI_NUMA
1986 	int cpu;
1987 
1988 	for_each_possible_cpu(cpu)
1989 		if (cpu_to_node(cpu) == pgdat->node_id)
1990 			numa_clear_node(cpu);
1991 #endif
1992 }
1993 
1994 static int check_and_unmap_cpu_on_node(pg_data_t *pgdat)
1995 {
1996 	int ret;
1997 
1998 	ret = check_cpu_on_node(pgdat);
1999 	if (ret)
2000 		return ret;
2001 
2002 	/*
2003 	 * the node will be offlined when we come here, so we can clear
2004 	 * the cpu_to_node() now.
2005 	 */
2006 
2007 	unmap_cpu_on_node(pgdat);
2008 	return 0;
2009 }
2010 
2011 /**
2012  * try_offline_node
2013  *
2014  * Offline a node if all memory sections and cpus of the node are removed.
2015  *
2016  * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2017  * and online/offline operations before this call.
2018  */
2019 void try_offline_node(int nid)
2020 {
2021 	pg_data_t *pgdat = NODE_DATA(nid);
2022 	unsigned long start_pfn = pgdat->node_start_pfn;
2023 	unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
2024 	unsigned long pfn;
2025 	int i;
2026 
2027 	for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
2028 		unsigned long section_nr = pfn_to_section_nr(pfn);
2029 
2030 		if (!present_section_nr(section_nr))
2031 			continue;
2032 
2033 		if (pfn_to_nid(pfn) != nid)
2034 			continue;
2035 
2036 		/*
2037 		 * some memory sections of this node are not removed, and we
2038 		 * can't offline node now.
2039 		 */
2040 		return;
2041 	}
2042 
2043 	if (check_and_unmap_cpu_on_node(pgdat))
2044 		return;
2045 
2046 	/*
2047 	 * all memory/cpu of this node are removed, we can offline this
2048 	 * node now.
2049 	 */
2050 	node_set_offline(nid);
2051 	unregister_one_node(nid);
2052 
2053 	/* free waittable in each zone */
2054 	for (i = 0; i < MAX_NR_ZONES; i++) {
2055 		struct zone *zone = pgdat->node_zones + i;
2056 
2057 		/*
2058 		 * wait_table may be allocated from boot memory,
2059 		 * here only free if it's allocated by vmalloc.
2060 		 */
2061 		if (is_vmalloc_addr(zone->wait_table)) {
2062 			vfree(zone->wait_table);
2063 			zone->wait_table = NULL;
2064 		}
2065 	}
2066 }
2067 EXPORT_SYMBOL(try_offline_node);
2068 
2069 /**
2070  * remove_memory
2071  *
2072  * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2073  * and online/offline operations before this call, as required by
2074  * try_offline_node().
2075  */
2076 void __ref remove_memory(int nid, u64 start, u64 size)
2077 {
2078 	int ret;
2079 
2080 	BUG_ON(check_hotplug_memory_range(start, size));
2081 
2082 	mem_hotplug_begin();
2083 
2084 	/*
2085 	 * All memory blocks must be offlined before removing memory.  Check
2086 	 * whether all memory blocks in question are offline and trigger a BUG()
2087 	 * if this is not the case.
2088 	 */
2089 	ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
2090 				check_memblock_offlined_cb);
2091 	if (ret)
2092 		BUG();
2093 
2094 	/* remove memmap entry */
2095 	firmware_map_remove(start, start + size, "System RAM");
2096 	memblock_free(start, size);
2097 	memblock_remove(start, size);
2098 
2099 	arch_remove_memory(start, size);
2100 
2101 	try_offline_node(nid);
2102 
2103 	mem_hotplug_done();
2104 }
2105 EXPORT_SYMBOL_GPL(remove_memory);
2106 #endif /* CONFIG_MEMORY_HOTREMOVE */
2107