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