• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/mm/memory_hotplug.c
4  *
5  *  Copyright (C)
6  */
7 
8 #include <linux/stddef.h>
9 #include <linux/mm.h>
10 #include <linux/sched/signal.h>
11 #include <linux/swap.h>
12 #include <linux/interrupt.h>
13 #include <linux/pagemap.h>
14 #include <linux/compiler.h>
15 #include <linux/export.h>
16 #include <linux/pagevec.h>
17 #include <linux/writeback.h>
18 #include <linux/slab.h>
19 #include <linux/sysctl.h>
20 #include <linux/cpu.h>
21 #include <linux/memory.h>
22 #include <linux/memremap.h>
23 #include <linux/memory_hotplug.h>
24 #include <linux/highmem.h>
25 #include <linux/vmalloc.h>
26 #include <linux/ioport.h>
27 #include <linux/delay.h>
28 #include <linux/migrate.h>
29 #include <linux/page-isolation.h>
30 #include <linux/pfn.h>
31 #include <linux/suspend.h>
32 #include <linux/mm_inline.h>
33 #include <linux/firmware-map.h>
34 #include <linux/stop_machine.h>
35 #include <linux/hugetlb.h>
36 #include <linux/memblock.h>
37 #include <linux/compaction.h>
38 #include <linux/rmap.h>
39 #include <linux/zswapd.h>
40 
41 #include <asm/tlbflush.h>
42 
43 #include "internal.h"
44 #include "shuffle.h"
45 
46 /*
47  * online_page_callback contains pointer to current page onlining function.
48  * Initially it is generic_online_page(). If it is required it could be
49  * changed by calling set_online_page_callback() for callback registration
50  * and restore_online_page_callback() for generic callback restore.
51  */
52 
53 static online_page_callback_t online_page_callback = generic_online_page;
54 static DEFINE_MUTEX(online_page_callback_lock);
55 
56 DEFINE_STATIC_PERCPU_RWSEM(mem_hotplug_lock);
57 
get_online_mems(void)58 void get_online_mems(void)
59 {
60 	percpu_down_read(&mem_hotplug_lock);
61 }
62 
put_online_mems(void)63 void put_online_mems(void)
64 {
65 	percpu_up_read(&mem_hotplug_lock);
66 }
67 
68 bool movable_node_enabled = false;
69 
70 #ifndef CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE
71 int memhp_default_online_type = MMOP_OFFLINE;
72 #else
73 int memhp_default_online_type = MMOP_ONLINE;
74 #endif
75 
setup_memhp_default_state(char * str)76 static int __init setup_memhp_default_state(char *str)
77 {
78 	const int online_type = memhp_online_type_from_str(str);
79 
80 	if (online_type >= 0)
81 		memhp_default_online_type = online_type;
82 
83 	return 1;
84 }
85 __setup("memhp_default_state=", setup_memhp_default_state);
86 
mem_hotplug_begin(void)87 void mem_hotplug_begin(void)
88 {
89 	cpus_read_lock();
90 	percpu_down_write(&mem_hotplug_lock);
91 }
92 
mem_hotplug_done(void)93 void mem_hotplug_done(void)
94 {
95 	percpu_up_write(&mem_hotplug_lock);
96 	cpus_read_unlock();
97 }
98 
99 u64 max_mem_size = U64_MAX;
100 
101 /* add this memory to iomem resource */
register_memory_resource(u64 start,u64 size,const char * resource_name)102 static struct resource *register_memory_resource(u64 start, u64 size,
103 						 const char *resource_name)
104 {
105 	struct resource *res;
106 	unsigned long flags =  IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
107 
108 	if (strcmp(resource_name, "System RAM"))
109 		flags |= IORESOURCE_SYSRAM_DRIVER_MANAGED;
110 
111 	/*
112 	 * Make sure value parsed from 'mem=' only restricts memory adding
113 	 * while booting, so that memory hotplug won't be impacted. Please
114 	 * refer to document of 'mem=' in kernel-parameters.txt for more
115 	 * details.
116 	 */
117 	if (start + size > max_mem_size && system_state < SYSTEM_RUNNING)
118 		return ERR_PTR(-E2BIG);
119 
120 	/*
121 	 * Request ownership of the new memory range.  This might be
122 	 * a child of an existing resource that was present but
123 	 * not marked as busy.
124 	 */
125 	res = __request_region(&iomem_resource, start, size,
126 			       resource_name, flags);
127 
128 	if (!res) {
129 		pr_debug("Unable to reserve System RAM region: %016llx->%016llx\n",
130 				start, start + size);
131 		return ERR_PTR(-EEXIST);
132 	}
133 	return res;
134 }
135 
release_memory_resource(struct resource * res)136 static void release_memory_resource(struct resource *res)
137 {
138 	if (!res)
139 		return;
140 	release_resource(res);
141 	kfree(res);
142 }
143 
144 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
get_page_bootmem(unsigned long info,struct page * page,unsigned long type)145 void get_page_bootmem(unsigned long info,  struct page *page,
146 		      unsigned long type)
147 {
148 	page->freelist = (void *)type;
149 	SetPagePrivate(page);
150 	set_page_private(page, info);
151 	page_ref_inc(page);
152 }
153 
put_page_bootmem(struct page * page)154 void put_page_bootmem(struct page *page)
155 {
156 	unsigned long type;
157 
158 	type = (unsigned long) page->freelist;
159 	BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
160 	       type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
161 
162 	if (page_ref_dec_return(page) == 1) {
163 		page->freelist = NULL;
164 		ClearPagePrivate(page);
165 		set_page_private(page, 0);
166 		INIT_LIST_HEAD(&page->lru);
167 		free_reserved_page(page);
168 	}
169 }
170 
171 #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
172 #ifndef CONFIG_SPARSEMEM_VMEMMAP
register_page_bootmem_info_section(unsigned long start_pfn)173 static void register_page_bootmem_info_section(unsigned long start_pfn)
174 {
175 	unsigned long mapsize, section_nr, i;
176 	struct mem_section *ms;
177 	struct page *page, *memmap;
178 	struct mem_section_usage *usage;
179 
180 	section_nr = pfn_to_section_nr(start_pfn);
181 	ms = __nr_to_section(section_nr);
182 
183 	/* Get section's memmap address */
184 	memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
185 
186 	/*
187 	 * Get page for the memmap's phys address
188 	 * XXX: need more consideration for sparse_vmemmap...
189 	 */
190 	page = virt_to_page(memmap);
191 	mapsize = sizeof(struct page) * PAGES_PER_SECTION;
192 	mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
193 
194 	/* remember memmap's page */
195 	for (i = 0; i < mapsize; i++, page++)
196 		get_page_bootmem(section_nr, page, SECTION_INFO);
197 
198 	usage = ms->usage;
199 	page = virt_to_page(usage);
200 
201 	mapsize = PAGE_ALIGN(mem_section_usage_size()) >> PAGE_SHIFT;
202 
203 	for (i = 0; i < mapsize; i++, page++)
204 		get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
205 
206 }
207 #else /* CONFIG_SPARSEMEM_VMEMMAP */
register_page_bootmem_info_section(unsigned long start_pfn)208 static void register_page_bootmem_info_section(unsigned long start_pfn)
209 {
210 	unsigned long mapsize, section_nr, i;
211 	struct mem_section *ms;
212 	struct page *page, *memmap;
213 	struct mem_section_usage *usage;
214 
215 	section_nr = pfn_to_section_nr(start_pfn);
216 	ms = __nr_to_section(section_nr);
217 
218 	memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
219 
220 	register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
221 
222 	usage = ms->usage;
223 	page = virt_to_page(usage);
224 
225 	mapsize = PAGE_ALIGN(mem_section_usage_size()) >> PAGE_SHIFT;
226 
227 	for (i = 0; i < mapsize; i++, page++)
228 		get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
229 }
230 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
231 
register_page_bootmem_info_node(struct pglist_data * pgdat)232 void __init register_page_bootmem_info_node(struct pglist_data *pgdat)
233 {
234 	unsigned long i, pfn, end_pfn, nr_pages;
235 	int node = pgdat->node_id;
236 	struct page *page;
237 
238 	nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
239 	page = virt_to_page(pgdat);
240 
241 	for (i = 0; i < nr_pages; i++, page++)
242 		get_page_bootmem(node, page, NODE_INFO);
243 
244 	pfn = pgdat->node_start_pfn;
245 	end_pfn = pgdat_end_pfn(pgdat);
246 
247 	/* register section info */
248 	for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
249 		/*
250 		 * Some platforms can assign the same pfn to multiple nodes - on
251 		 * node0 as well as nodeN.  To avoid registering a pfn against
252 		 * multiple nodes we check that this pfn does not already
253 		 * reside in some other nodes.
254 		 */
255 		if (pfn_valid(pfn) && (early_pfn_to_nid(pfn) == node))
256 			register_page_bootmem_info_section(pfn);
257 	}
258 }
259 #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
260 
check_pfn_span(unsigned long pfn,unsigned long nr_pages,const char * reason)261 static int check_pfn_span(unsigned long pfn, unsigned long nr_pages,
262 		const char *reason)
263 {
264 	/*
265 	 * Disallow all operations smaller than a sub-section and only
266 	 * allow operations smaller than a section for
267 	 * SPARSEMEM_VMEMMAP. Note that check_hotplug_memory_range()
268 	 * enforces a larger memory_block_size_bytes() granularity for
269 	 * memory that will be marked online, so this check should only
270 	 * fire for direct arch_{add,remove}_memory() users outside of
271 	 * add_memory_resource().
272 	 */
273 	unsigned long min_align;
274 
275 	if (IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP))
276 		min_align = PAGES_PER_SUBSECTION;
277 	else
278 		min_align = PAGES_PER_SECTION;
279 	if (!IS_ALIGNED(pfn, min_align)
280 			|| !IS_ALIGNED(nr_pages, min_align)) {
281 		WARN(1, "Misaligned __%s_pages start: %#lx end: #%lx\n",
282 				reason, pfn, pfn + nr_pages - 1);
283 		return -EINVAL;
284 	}
285 	return 0;
286 }
287 
check_hotplug_memory_addressable(unsigned long pfn,unsigned long nr_pages)288 static int check_hotplug_memory_addressable(unsigned long pfn,
289 					    unsigned long nr_pages)
290 {
291 	const u64 max_addr = PFN_PHYS(pfn + nr_pages) - 1;
292 
293 	if (max_addr >> MAX_PHYSMEM_BITS) {
294 		const u64 max_allowed = (1ull << (MAX_PHYSMEM_BITS + 1)) - 1;
295 		WARN(1,
296 		     "Hotplugged memory exceeds maximum addressable address, range=%#llx-%#llx, maximum=%#llx\n",
297 		     (u64)PFN_PHYS(pfn), max_addr, max_allowed);
298 		return -E2BIG;
299 	}
300 
301 	return 0;
302 }
303 
304 /*
305  * Reasonably generic function for adding memory.  It is
306  * expected that archs that support memory hotplug will
307  * call this function after deciding the zone to which to
308  * add the new pages.
309  */
__add_pages(int nid,unsigned long pfn,unsigned long nr_pages,struct mhp_params * params)310 int __ref __add_pages(int nid, unsigned long pfn, unsigned long nr_pages,
311 		struct mhp_params *params)
312 {
313 	const unsigned long end_pfn = pfn + nr_pages;
314 	unsigned long cur_nr_pages;
315 	int err;
316 	struct vmem_altmap *altmap = params->altmap;
317 
318 	if (WARN_ON_ONCE(!params->pgprot.pgprot))
319 		return -EINVAL;
320 
321 	err = check_hotplug_memory_addressable(pfn, nr_pages);
322 	if (err)
323 		return err;
324 
325 	if (altmap) {
326 		/*
327 		 * Validate altmap is within bounds of the total request
328 		 */
329 		if (altmap->base_pfn != pfn
330 				|| vmem_altmap_offset(altmap) > nr_pages) {
331 			pr_warn_once("memory add fail, invalid altmap\n");
332 			return -EINVAL;
333 		}
334 		altmap->alloc = 0;
335 	}
336 
337 	err = check_pfn_span(pfn, nr_pages, "add");
338 	if (err)
339 		return err;
340 
341 	for (; pfn < end_pfn; pfn += cur_nr_pages) {
342 		/* Select all remaining pages up to the next section boundary */
343 		cur_nr_pages = min(end_pfn - pfn,
344 				   SECTION_ALIGN_UP(pfn + 1) - pfn);
345 		err = sparse_add_section(nid, pfn, cur_nr_pages, altmap);
346 		if (err)
347 			break;
348 		cond_resched();
349 	}
350 	vmemmap_populate_print_last();
351 	return err;
352 }
353 
354 /* 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)355 static unsigned long find_smallest_section_pfn(int nid, struct zone *zone,
356 				     unsigned long start_pfn,
357 				     unsigned long end_pfn)
358 {
359 	for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SUBSECTION) {
360 		if (unlikely(!pfn_to_online_page(start_pfn)))
361 			continue;
362 
363 		if (unlikely(pfn_to_nid(start_pfn) != nid))
364 			continue;
365 
366 		if (zone != page_zone(pfn_to_page(start_pfn)))
367 			continue;
368 
369 		return start_pfn;
370 	}
371 
372 	return 0;
373 }
374 
375 /* 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)376 static unsigned long find_biggest_section_pfn(int nid, struct zone *zone,
377 				    unsigned long start_pfn,
378 				    unsigned long end_pfn)
379 {
380 	unsigned long pfn;
381 
382 	/* pfn is the end pfn of a memory section. */
383 	pfn = end_pfn - 1;
384 	for (; pfn >= start_pfn; pfn -= PAGES_PER_SUBSECTION) {
385 		if (unlikely(!pfn_to_online_page(pfn)))
386 			continue;
387 
388 		if (unlikely(pfn_to_nid(pfn) != nid))
389 			continue;
390 
391 		if (zone != page_zone(pfn_to_page(pfn)))
392 			continue;
393 
394 		return pfn;
395 	}
396 
397 	return 0;
398 }
399 
shrink_zone_span(struct zone * zone,unsigned long start_pfn,unsigned long end_pfn)400 static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
401 			     unsigned long end_pfn)
402 {
403 	unsigned long pfn;
404 	int nid = zone_to_nid(zone);
405 
406 	zone_span_writelock(zone);
407 	if (zone->zone_start_pfn == start_pfn) {
408 		/*
409 		 * If the section is smallest section in the zone, it need
410 		 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
411 		 * In this case, we find second smallest valid mem_section
412 		 * for shrinking zone.
413 		 */
414 		pfn = find_smallest_section_pfn(nid, zone, end_pfn,
415 						zone_end_pfn(zone));
416 		if (pfn) {
417 			zone->spanned_pages = zone_end_pfn(zone) - pfn;
418 			zone->zone_start_pfn = pfn;
419 		} else {
420 			zone->zone_start_pfn = 0;
421 			zone->spanned_pages = 0;
422 		}
423 	} else if (zone_end_pfn(zone) == end_pfn) {
424 		/*
425 		 * If the section is biggest section in the zone, it need
426 		 * shrink zone->spanned_pages.
427 		 * In this case, we find second biggest valid mem_section for
428 		 * shrinking zone.
429 		 */
430 		pfn = find_biggest_section_pfn(nid, zone, zone->zone_start_pfn,
431 					       start_pfn);
432 		if (pfn)
433 			zone->spanned_pages = pfn - zone->zone_start_pfn + 1;
434 		else {
435 			zone->zone_start_pfn = 0;
436 			zone->spanned_pages = 0;
437 		}
438 	}
439 	zone_span_writeunlock(zone);
440 }
441 
update_pgdat_span(struct pglist_data * pgdat)442 static void update_pgdat_span(struct pglist_data *pgdat)
443 {
444 	unsigned long node_start_pfn = 0, node_end_pfn = 0;
445 	struct zone *zone;
446 
447 	for (zone = pgdat->node_zones;
448 	     zone < pgdat->node_zones + MAX_NR_ZONES; zone++) {
449 		unsigned long zone_end_pfn = zone->zone_start_pfn +
450 					     zone->spanned_pages;
451 
452 		/* No need to lock the zones, they can't change. */
453 		if (!zone->spanned_pages)
454 			continue;
455 		if (!node_end_pfn) {
456 			node_start_pfn = zone->zone_start_pfn;
457 			node_end_pfn = zone_end_pfn;
458 			continue;
459 		}
460 
461 		if (zone_end_pfn > node_end_pfn)
462 			node_end_pfn = zone_end_pfn;
463 		if (zone->zone_start_pfn < node_start_pfn)
464 			node_start_pfn = zone->zone_start_pfn;
465 	}
466 
467 	pgdat->node_start_pfn = node_start_pfn;
468 	pgdat->node_spanned_pages = node_end_pfn - node_start_pfn;
469 }
470 
remove_pfn_range_from_zone(struct zone * zone,unsigned long start_pfn,unsigned long nr_pages)471 void __ref remove_pfn_range_from_zone(struct zone *zone,
472 				      unsigned long start_pfn,
473 				      unsigned long nr_pages)
474 {
475 	const unsigned long end_pfn = start_pfn + nr_pages;
476 	struct pglist_data *pgdat = zone->zone_pgdat;
477 	unsigned long pfn, cur_nr_pages, flags;
478 
479 	/* Poison struct pages because they are now uninitialized again. */
480 	for (pfn = start_pfn; pfn < end_pfn; pfn += cur_nr_pages) {
481 		cond_resched();
482 
483 		/* Select all remaining pages up to the next section boundary */
484 		cur_nr_pages =
485 			min(end_pfn - pfn, SECTION_ALIGN_UP(pfn + 1) - pfn);
486 		page_init_poison(pfn_to_page(pfn),
487 				 sizeof(struct page) * cur_nr_pages);
488 	}
489 
490 #ifdef CONFIG_ZONE_DEVICE
491 	/*
492 	 * Zone shrinking code cannot properly deal with ZONE_DEVICE. So
493 	 * we will not try to shrink the zones - which is okay as
494 	 * set_zone_contiguous() cannot deal with ZONE_DEVICE either way.
495 	 */
496 	if (zone_idx(zone) == ZONE_DEVICE)
497 		return;
498 #endif
499 
500 	clear_zone_contiguous(zone);
501 
502 	pgdat_resize_lock(zone->zone_pgdat, &flags);
503 	shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
504 	update_pgdat_span(pgdat);
505 	pgdat_resize_unlock(zone->zone_pgdat, &flags);
506 
507 	set_zone_contiguous(zone);
508 }
509 
__remove_section(unsigned long pfn,unsigned long nr_pages,unsigned long map_offset,struct vmem_altmap * altmap)510 static void __remove_section(unsigned long pfn, unsigned long nr_pages,
511 			     unsigned long map_offset,
512 			     struct vmem_altmap *altmap)
513 {
514 	struct mem_section *ms = __pfn_to_section(pfn);
515 
516 	if (WARN_ON_ONCE(!valid_section(ms)))
517 		return;
518 
519 	sparse_remove_section(ms, pfn, nr_pages, map_offset, altmap);
520 }
521 
522 /**
523  * __remove_pages() - remove sections of pages
524  * @pfn: starting pageframe (must be aligned to start of a section)
525  * @nr_pages: number of pages to remove (must be multiple of section size)
526  * @altmap: alternative device page map or %NULL if default memmap is used
527  *
528  * Generic helper function to remove section mappings and sysfs entries
529  * for the section of the memory we are removing. Caller needs to make
530  * sure that pages are marked reserved and zones are adjust properly by
531  * calling offline_pages().
532  */
__remove_pages(unsigned long pfn,unsigned long nr_pages,struct vmem_altmap * altmap)533 void __remove_pages(unsigned long pfn, unsigned long nr_pages,
534 		    struct vmem_altmap *altmap)
535 {
536 	const unsigned long end_pfn = pfn + nr_pages;
537 	unsigned long cur_nr_pages;
538 	unsigned long map_offset = 0;
539 
540 	map_offset = vmem_altmap_offset(altmap);
541 
542 	if (check_pfn_span(pfn, nr_pages, "remove"))
543 		return;
544 
545 	for (; pfn < end_pfn; pfn += cur_nr_pages) {
546 		cond_resched();
547 		/* Select all remaining pages up to the next section boundary */
548 		cur_nr_pages = min(end_pfn - pfn,
549 				   SECTION_ALIGN_UP(pfn + 1) - pfn);
550 		__remove_section(pfn, cur_nr_pages, map_offset, altmap);
551 		map_offset = 0;
552 	}
553 }
554 
set_online_page_callback(online_page_callback_t callback)555 int set_online_page_callback(online_page_callback_t callback)
556 {
557 	int rc = -EINVAL;
558 
559 	get_online_mems();
560 	mutex_lock(&online_page_callback_lock);
561 
562 	if (online_page_callback == generic_online_page) {
563 		online_page_callback = callback;
564 		rc = 0;
565 	}
566 
567 	mutex_unlock(&online_page_callback_lock);
568 	put_online_mems();
569 
570 	return rc;
571 }
572 EXPORT_SYMBOL_GPL(set_online_page_callback);
573 
restore_online_page_callback(online_page_callback_t callback)574 int restore_online_page_callback(online_page_callback_t callback)
575 {
576 	int rc = -EINVAL;
577 
578 	get_online_mems();
579 	mutex_lock(&online_page_callback_lock);
580 
581 	if (online_page_callback == callback) {
582 		online_page_callback = generic_online_page;
583 		rc = 0;
584 	}
585 
586 	mutex_unlock(&online_page_callback_lock);
587 	put_online_mems();
588 
589 	return rc;
590 }
591 EXPORT_SYMBOL_GPL(restore_online_page_callback);
592 
generic_online_page(struct page * page,unsigned int order)593 void generic_online_page(struct page *page, unsigned int order)
594 {
595 	/*
596 	 * Freeing the page with debug_pagealloc enabled will try to unmap it,
597 	 * so we should map it first. This is better than introducing a special
598 	 * case in page freeing fast path.
599 	 */
600 	if (debug_pagealloc_enabled_static())
601 		kernel_map_pages(page, 1 << order, 1);
602 	__free_pages_core(page, order);
603 	totalram_pages_add(1UL << order);
604 #ifdef CONFIG_HIGHMEM
605 	if (PageHighMem(page))
606 		totalhigh_pages_add(1UL << order);
607 #endif
608 }
609 EXPORT_SYMBOL_GPL(generic_online_page);
610 
online_pages_range(unsigned long start_pfn,unsigned long nr_pages)611 static void online_pages_range(unsigned long start_pfn, unsigned long nr_pages)
612 {
613 	const unsigned long end_pfn = start_pfn + nr_pages;
614 	unsigned long pfn;
615 
616 	/*
617 	 * Online the pages in MAX_ORDER - 1 aligned chunks. The callback might
618 	 * decide to not expose all pages to the buddy (e.g., expose them
619 	 * later). We account all pages as being online and belonging to this
620 	 * zone ("present").
621 	 */
622 	for (pfn = start_pfn; pfn < end_pfn; pfn += MAX_ORDER_NR_PAGES)
623 		(*online_page_callback)(pfn_to_page(pfn), MAX_ORDER - 1);
624 
625 	/* mark all involved sections as online */
626 	online_mem_sections(start_pfn, end_pfn);
627 }
628 
629 /* 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)630 static void node_states_check_changes_online(unsigned long nr_pages,
631 	struct zone *zone, struct memory_notify *arg)
632 {
633 	int nid = zone_to_nid(zone);
634 
635 	arg->status_change_nid = NUMA_NO_NODE;
636 	arg->status_change_nid_normal = NUMA_NO_NODE;
637 	arg->status_change_nid_high = NUMA_NO_NODE;
638 
639 	if (!node_state(nid, N_MEMORY))
640 		arg->status_change_nid = nid;
641 	if (zone_idx(zone) <= ZONE_NORMAL && !node_state(nid, N_NORMAL_MEMORY))
642 		arg->status_change_nid_normal = nid;
643 #ifdef CONFIG_HIGHMEM
644 	if (zone_idx(zone) <= ZONE_HIGHMEM && !node_state(nid, N_HIGH_MEMORY))
645 		arg->status_change_nid_high = nid;
646 #endif
647 }
648 
node_states_set_node(int node,struct memory_notify * arg)649 static void node_states_set_node(int node, struct memory_notify *arg)
650 {
651 	if (arg->status_change_nid_normal >= 0)
652 		node_set_state(node, N_NORMAL_MEMORY);
653 
654 	if (arg->status_change_nid_high >= 0)
655 		node_set_state(node, N_HIGH_MEMORY);
656 
657 	if (arg->status_change_nid >= 0)
658 		node_set_state(node, N_MEMORY);
659 }
660 
resize_zone_range(struct zone * zone,unsigned long start_pfn,unsigned long nr_pages)661 static void __meminit resize_zone_range(struct zone *zone, unsigned long start_pfn,
662 		unsigned long nr_pages)
663 {
664 	unsigned long old_end_pfn = zone_end_pfn(zone);
665 
666 	if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
667 		zone->zone_start_pfn = start_pfn;
668 
669 	zone->spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - zone->zone_start_pfn;
670 }
671 
resize_pgdat_range(struct pglist_data * pgdat,unsigned long start_pfn,unsigned long nr_pages)672 static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned long start_pfn,
673                                      unsigned long nr_pages)
674 {
675 	unsigned long old_end_pfn = pgdat_end_pfn(pgdat);
676 
677 	if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
678 		pgdat->node_start_pfn = start_pfn;
679 
680 	pgdat->node_spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - pgdat->node_start_pfn;
681 
682 }
683 /*
684  * Associate the pfn range with the given zone, initializing the memmaps
685  * and resizing the pgdat/zone data to span the added pages. After this
686  * call, all affected pages are PG_reserved.
687  *
688  * All aligned pageblocks are initialized to the specified migratetype
689  * (usually MIGRATE_MOVABLE). Besides setting the migratetype, no related
690  * zone stats (e.g., nr_isolate_pageblock) are touched.
691  */
move_pfn_range_to_zone(struct zone * zone,unsigned long start_pfn,unsigned long nr_pages,struct vmem_altmap * altmap,int migratetype)692 void __ref move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
693 				  unsigned long nr_pages,
694 				  struct vmem_altmap *altmap, int migratetype)
695 {
696 	struct pglist_data *pgdat = zone->zone_pgdat;
697 	int nid = pgdat->node_id;
698 	unsigned long flags;
699 
700 	clear_zone_contiguous(zone);
701 
702 	/* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */
703 	pgdat_resize_lock(pgdat, &flags);
704 	zone_span_writelock(zone);
705 	if (zone_is_empty(zone))
706 		init_currently_empty_zone(zone, start_pfn, nr_pages);
707 	resize_zone_range(zone, start_pfn, nr_pages);
708 	zone_span_writeunlock(zone);
709 	resize_pgdat_range(pgdat, start_pfn, nr_pages);
710 	pgdat_resize_unlock(pgdat, &flags);
711 
712 	/*
713 	 * TODO now we have a visible range of pages which are not associated
714 	 * with their zone properly. Not nice but set_pfnblock_flags_mask
715 	 * expects the zone spans the pfn range. All the pages in the range
716 	 * are reserved so nobody should be touching them so we should be safe
717 	 */
718 	memmap_init_zone(nr_pages, nid, zone_idx(zone), start_pfn, 0,
719 			 MEMINIT_HOTPLUG, altmap, migratetype);
720 
721 	set_zone_contiguous(zone);
722 }
723 
724 /*
725  * Returns a default kernel memory zone for the given pfn range.
726  * If no kernel zone covers this pfn range it will automatically go
727  * to the ZONE_NORMAL.
728  */
default_kernel_zone_for_pfn(int nid,unsigned long start_pfn,unsigned long nr_pages)729 static struct zone *default_kernel_zone_for_pfn(int nid, unsigned long start_pfn,
730 		unsigned long nr_pages)
731 {
732 	struct pglist_data *pgdat = NODE_DATA(nid);
733 	int zid;
734 
735 	for (zid = 0; zid <= ZONE_NORMAL; zid++) {
736 		struct zone *zone = &pgdat->node_zones[zid];
737 
738 		if (zone_intersects(zone, start_pfn, nr_pages))
739 			return zone;
740 	}
741 
742 	return &pgdat->node_zones[ZONE_NORMAL];
743 }
744 
default_zone_for_pfn(int nid,unsigned long start_pfn,unsigned long nr_pages)745 static inline struct zone *default_zone_for_pfn(int nid, unsigned long start_pfn,
746 		unsigned long nr_pages)
747 {
748 	struct zone *kernel_zone = default_kernel_zone_for_pfn(nid, start_pfn,
749 			nr_pages);
750 	struct zone *movable_zone = &NODE_DATA(nid)->node_zones[ZONE_MOVABLE];
751 	bool in_kernel = zone_intersects(kernel_zone, start_pfn, nr_pages);
752 	bool in_movable = zone_intersects(movable_zone, start_pfn, nr_pages);
753 
754 	/*
755 	 * We inherit the existing zone in a simple case where zones do not
756 	 * overlap in the given range
757 	 */
758 	if (in_kernel ^ in_movable)
759 		return (in_kernel) ? kernel_zone : movable_zone;
760 
761 	/*
762 	 * If the range doesn't belong to any zone or two zones overlap in the
763 	 * given range then we use movable zone only if movable_node is
764 	 * enabled because we always online to a kernel zone by default.
765 	 */
766 	return movable_node_enabled ? movable_zone : kernel_zone;
767 }
768 
zone_for_pfn_range(int online_type,int nid,unsigned long start_pfn,unsigned long nr_pages)769 struct zone *zone_for_pfn_range(int online_type, int nid,
770 		unsigned long start_pfn, unsigned long nr_pages)
771 {
772 	if (online_type == MMOP_ONLINE_KERNEL)
773 		return default_kernel_zone_for_pfn(nid, start_pfn, nr_pages);
774 
775 	if (online_type == MMOP_ONLINE_MOVABLE)
776 		return &NODE_DATA(nid)->node_zones[ZONE_MOVABLE];
777 
778 	return default_zone_for_pfn(nid, start_pfn, nr_pages);
779 }
780 
online_pages(unsigned long pfn,unsigned long nr_pages,int online_type,int nid)781 int __ref online_pages(unsigned long pfn, unsigned long nr_pages,
782 		       int online_type, int nid)
783 {
784 	unsigned long flags;
785 	struct zone *zone;
786 	int need_zonelists_rebuild = 0;
787 	int ret;
788 	struct memory_notify arg;
789 
790 	/* We can only online full sections (e.g., SECTION_IS_ONLINE) */
791 	if (WARN_ON_ONCE(!nr_pages ||
792 			 !IS_ALIGNED(pfn | nr_pages, PAGES_PER_SECTION)))
793 		return -EINVAL;
794 
795 	mem_hotplug_begin();
796 
797 	/* associate pfn range with the zone */
798 	zone = zone_for_pfn_range(online_type, nid, pfn, nr_pages);
799 	move_pfn_range_to_zone(zone, pfn, nr_pages, NULL, MIGRATE_ISOLATE);
800 
801 	arg.start_pfn = pfn;
802 	arg.nr_pages = nr_pages;
803 	node_states_check_changes_online(nr_pages, zone, &arg);
804 
805 	ret = memory_notify(MEM_GOING_ONLINE, &arg);
806 	ret = notifier_to_errno(ret);
807 	if (ret)
808 		goto failed_addition;
809 
810 	/*
811 	 * Fixup the number of isolated pageblocks before marking the sections
812 	 * onlining, such that undo_isolate_page_range() works correctly.
813 	 */
814 	spin_lock_irqsave(&zone->lock, flags);
815 	zone->nr_isolate_pageblock += nr_pages / pageblock_nr_pages;
816 	spin_unlock_irqrestore(&zone->lock, flags);
817 
818 	/*
819 	 * If this zone is not populated, then it is not in zonelist.
820 	 * This means the page allocator ignores this zone.
821 	 * So, zonelist must be updated after online.
822 	 */
823 	if (!populated_zone(zone)) {
824 		need_zonelists_rebuild = 1;
825 		setup_zone_pageset(zone);
826 	}
827 
828 	online_pages_range(pfn, nr_pages);
829 	zone->present_pages += nr_pages;
830 
831 	pgdat_resize_lock(zone->zone_pgdat, &flags);
832 	zone->zone_pgdat->node_present_pages += nr_pages;
833 	pgdat_resize_unlock(zone->zone_pgdat, &flags);
834 
835 	node_states_set_node(nid, &arg);
836 	if (need_zonelists_rebuild)
837 		build_all_zonelists(NULL);
838 	zone_pcp_update(zone);
839 
840 	/* Basic onlining is complete, allow allocation of onlined pages. */
841 	undo_isolate_page_range(pfn, pfn + nr_pages, MIGRATE_MOVABLE);
842 
843 	/*
844 	 * Freshly onlined pages aren't shuffled (e.g., all pages are placed to
845 	 * the tail of the freelist when undoing isolation). Shuffle the whole
846 	 * zone to make sure the just onlined pages are properly distributed
847 	 * across the whole freelist - to create an initial shuffle.
848 	 */
849 	shuffle_zone(zone);
850 
851 	init_per_zone_wmark_min();
852 
853 	kswapd_run(nid);
854 	kcompactd_run(nid);
855 #ifdef CONFIG_HYPERHOLD_ZSWAPD
856 	zswapd_run(nid);
857 #endif
858 
859 	writeback_set_ratelimit();
860 
861 	memory_notify(MEM_ONLINE, &arg);
862 	mem_hotplug_done();
863 	return 0;
864 
865 failed_addition:
866 	pr_debug("online_pages [mem %#010llx-%#010llx] failed\n",
867 		 (unsigned long long) pfn << PAGE_SHIFT,
868 		 (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1);
869 	memory_notify(MEM_CANCEL_ONLINE, &arg);
870 	remove_pfn_range_from_zone(zone, pfn, nr_pages);
871 	mem_hotplug_done();
872 	return ret;
873 }
874 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
875 
reset_node_present_pages(pg_data_t * pgdat)876 static void reset_node_present_pages(pg_data_t *pgdat)
877 {
878 	struct zone *z;
879 
880 	for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
881 		z->present_pages = 0;
882 
883 	pgdat->node_present_pages = 0;
884 }
885 
886 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
hotadd_new_pgdat(int nid)887 static pg_data_t __ref *hotadd_new_pgdat(int nid)
888 {
889 	struct pglist_data *pgdat;
890 
891 	pgdat = NODE_DATA(nid);
892 	if (!pgdat) {
893 		pgdat = arch_alloc_nodedata(nid);
894 		if (!pgdat)
895 			return NULL;
896 
897 		pgdat->per_cpu_nodestats =
898 			alloc_percpu(struct per_cpu_nodestat);
899 		arch_refresh_nodedata(nid, pgdat);
900 	} else {
901 		int cpu;
902 		/*
903 		 * Reset the nr_zones, order and highest_zoneidx before reuse.
904 		 * Note that kswapd will init kswapd_highest_zoneidx properly
905 		 * when it starts in the near future.
906 		 */
907 		pgdat->nr_zones = 0;
908 		pgdat->kswapd_order = 0;
909 		pgdat->kswapd_highest_zoneidx = 0;
910 		for_each_online_cpu(cpu) {
911 			struct per_cpu_nodestat *p;
912 
913 			p = per_cpu_ptr(pgdat->per_cpu_nodestats, cpu);
914 			memset(p, 0, sizeof(*p));
915 		}
916 	}
917 
918 	/* we can use NODE_DATA(nid) from here */
919 	pgdat->node_id = nid;
920 	pgdat->node_start_pfn = 0;
921 
922 	/* init node's zones as empty zones, we don't have any present pages.*/
923 	free_area_init_core_hotplug(nid);
924 
925 	/*
926 	 * The node we allocated has no zone fallback lists. For avoiding
927 	 * to access not-initialized zonelist, build here.
928 	 */
929 	build_all_zonelists(pgdat);
930 
931 	/*
932 	 * When memory is hot-added, all the memory is in offline state. So
933 	 * clear all zones' present_pages because they will be updated in
934 	 * online_pages() and offline_pages().
935 	 */
936 	reset_node_managed_pages(pgdat);
937 	reset_node_present_pages(pgdat);
938 
939 	return pgdat;
940 }
941 
rollback_node_hotadd(int nid)942 static void rollback_node_hotadd(int nid)
943 {
944 	pg_data_t *pgdat = NODE_DATA(nid);
945 
946 	arch_refresh_nodedata(nid, NULL);
947 	free_percpu(pgdat->per_cpu_nodestats);
948 	arch_free_nodedata(pgdat);
949 }
950 
951 
952 /**
953  * try_online_node - online a node if offlined
954  * @nid: the node ID
955  * @set_node_online: Whether we want to online the node
956  * called by cpu_up() to online a node without onlined memory.
957  *
958  * Returns:
959  * 1 -> a new node has been allocated
960  * 0 -> the node is already online
961  * -ENOMEM -> the node could not be allocated
962  */
__try_online_node(int nid,bool set_node_online)963 static int __try_online_node(int nid, bool set_node_online)
964 {
965 	pg_data_t *pgdat;
966 	int ret = 1;
967 
968 	if (node_online(nid))
969 		return 0;
970 
971 	pgdat = hotadd_new_pgdat(nid);
972 	if (!pgdat) {
973 		pr_err("Cannot online node %d due to NULL pgdat\n", nid);
974 		ret = -ENOMEM;
975 		goto out;
976 	}
977 
978 	if (set_node_online) {
979 		node_set_online(nid);
980 		ret = register_one_node(nid);
981 		BUG_ON(ret);
982 	}
983 out:
984 	return ret;
985 }
986 
987 /*
988  * Users of this function always want to online/register the node
989  */
try_online_node(int nid)990 int try_online_node(int nid)
991 {
992 	int ret;
993 
994 	mem_hotplug_begin();
995 	ret =  __try_online_node(nid, true);
996 	mem_hotplug_done();
997 	return ret;
998 }
999 
check_hotplug_memory_range(u64 start,u64 size)1000 static int check_hotplug_memory_range(u64 start, u64 size)
1001 {
1002 	/* memory range must be block size aligned */
1003 	if (!size || !IS_ALIGNED(start, memory_block_size_bytes()) ||
1004 	    !IS_ALIGNED(size, memory_block_size_bytes())) {
1005 		pr_err("Block size [%#lx] unaligned hotplug range: start %#llx, size %#llx",
1006 		       memory_block_size_bytes(), start, size);
1007 		return -EINVAL;
1008 	}
1009 
1010 	return 0;
1011 }
1012 
online_memory_block(struct memory_block * mem,void * arg)1013 static int online_memory_block(struct memory_block *mem, void *arg)
1014 {
1015 	mem->online_type = memhp_default_online_type;
1016 	return device_online(&mem->dev);
1017 }
1018 
1019 /*
1020  * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1021  * and online/offline operations (triggered e.g. by sysfs).
1022  *
1023  * we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG
1024  */
add_memory_resource(int nid,struct resource * res,mhp_t mhp_flags)1025 int __ref add_memory_resource(int nid, struct resource *res, mhp_t mhp_flags)
1026 {
1027 	struct mhp_params params = { .pgprot = pgprot_mhp(PAGE_KERNEL) };
1028 	u64 start, size;
1029 	bool new_node = false;
1030 	int ret;
1031 
1032 	start = res->start;
1033 	size = resource_size(res);
1034 
1035 	ret = check_hotplug_memory_range(start, size);
1036 	if (ret)
1037 		return ret;
1038 
1039 	if (!node_possible(nid)) {
1040 		WARN(1, "node %d was absent from the node_possible_map\n", nid);
1041 		return -EINVAL;
1042 	}
1043 
1044 	mem_hotplug_begin();
1045 
1046 	if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK))
1047 		memblock_add_node(start, size, nid);
1048 
1049 	ret = __try_online_node(nid, false);
1050 	if (ret < 0)
1051 		goto error;
1052 	new_node = ret;
1053 
1054 	/* call arch's memory hotadd */
1055 	ret = arch_add_memory(nid, start, size, &params);
1056 	if (ret < 0)
1057 		goto error;
1058 
1059 	/* create memory block devices after memory was added */
1060 	ret = create_memory_block_devices(start, size);
1061 	if (ret) {
1062 		arch_remove_memory(nid, start, size, NULL);
1063 		goto error;
1064 	}
1065 
1066 	if (new_node) {
1067 		/* If sysfs file of new node can't be created, cpu on the node
1068 		 * can't be hot-added. There is no rollback way now.
1069 		 * So, check by BUG_ON() to catch it reluctantly..
1070 		 * We online node here. We can't roll back from here.
1071 		 */
1072 		node_set_online(nid);
1073 		ret = __register_one_node(nid);
1074 		BUG_ON(ret);
1075 	}
1076 
1077 	/* link memory sections under this node.*/
1078 	link_mem_sections(nid, PFN_DOWN(start), PFN_UP(start + size - 1),
1079 			  MEMINIT_HOTPLUG);
1080 
1081 	/* create new memmap entry */
1082 	if (!strcmp(res->name, "System RAM"))
1083 		firmware_map_add_hotplug(start, start + size, "System RAM");
1084 
1085 	/* device_online() will take the lock when calling online_pages() */
1086 	mem_hotplug_done();
1087 
1088 	/*
1089 	 * In case we're allowed to merge the resource, flag it and trigger
1090 	 * merging now that adding succeeded.
1091 	 */
1092 	if (mhp_flags & MEMHP_MERGE_RESOURCE)
1093 		merge_system_ram_resource(res);
1094 
1095 	/* online pages if requested */
1096 	if (memhp_default_online_type != MMOP_OFFLINE)
1097 		walk_memory_blocks(start, size, NULL, online_memory_block);
1098 
1099 	return ret;
1100 error:
1101 	/* rollback pgdat allocation and others */
1102 	if (new_node)
1103 		rollback_node_hotadd(nid);
1104 	if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK))
1105 		memblock_remove(start, size);
1106 	mem_hotplug_done();
1107 	return ret;
1108 }
1109 
1110 /* requires device_hotplug_lock, see add_memory_resource() */
__add_memory(int nid,u64 start,u64 size,mhp_t mhp_flags)1111 int __ref __add_memory(int nid, u64 start, u64 size, mhp_t mhp_flags)
1112 {
1113 	struct resource *res;
1114 	int ret;
1115 
1116 	res = register_memory_resource(start, size, "System RAM");
1117 	if (IS_ERR(res))
1118 		return PTR_ERR(res);
1119 
1120 	ret = add_memory_resource(nid, res, mhp_flags);
1121 	if (ret < 0)
1122 		release_memory_resource(res);
1123 	return ret;
1124 }
1125 
add_memory(int nid,u64 start,u64 size,mhp_t mhp_flags)1126 int add_memory(int nid, u64 start, u64 size, mhp_t mhp_flags)
1127 {
1128 	int rc;
1129 
1130 	lock_device_hotplug();
1131 	rc = __add_memory(nid, start, size, mhp_flags);
1132 	unlock_device_hotplug();
1133 
1134 	return rc;
1135 }
1136 EXPORT_SYMBOL_GPL(add_memory);
1137 
1138 /*
1139  * Add special, driver-managed memory to the system as system RAM. Such
1140  * memory is not exposed via the raw firmware-provided memmap as system
1141  * RAM, instead, it is detected and added by a driver - during cold boot,
1142  * after a reboot, and after kexec.
1143  *
1144  * Reasons why this memory should not be used for the initial memmap of a
1145  * kexec kernel or for placing kexec images:
1146  * - The booting kernel is in charge of determining how this memory will be
1147  *   used (e.g., use persistent memory as system RAM)
1148  * - Coordination with a hypervisor is required before this memory
1149  *   can be used (e.g., inaccessible parts).
1150  *
1151  * For this memory, no entries in /sys/firmware/memmap ("raw firmware-provided
1152  * memory map") are created. Also, the created memory resource is flagged
1153  * with IORESOURCE_SYSRAM_DRIVER_MANAGED, so in-kernel users can special-case
1154  * this memory as well (esp., not place kexec images onto it).
1155  *
1156  * The resource_name (visible via /proc/iomem) has to have the format
1157  * "System RAM ($DRIVER)".
1158  */
add_memory_driver_managed(int nid,u64 start,u64 size,const char * resource_name,mhp_t mhp_flags)1159 int add_memory_driver_managed(int nid, u64 start, u64 size,
1160 			      const char *resource_name, mhp_t mhp_flags)
1161 {
1162 	struct resource *res;
1163 	int rc;
1164 
1165 	if (!resource_name ||
1166 	    strstr(resource_name, "System RAM (") != resource_name ||
1167 	    resource_name[strlen(resource_name) - 1] != ')')
1168 		return -EINVAL;
1169 
1170 	lock_device_hotplug();
1171 
1172 	res = register_memory_resource(start, size, resource_name);
1173 	if (IS_ERR(res)) {
1174 		rc = PTR_ERR(res);
1175 		goto out_unlock;
1176 	}
1177 
1178 	rc = add_memory_resource(nid, res, mhp_flags);
1179 	if (rc < 0)
1180 		release_memory_resource(res);
1181 
1182 out_unlock:
1183 	unlock_device_hotplug();
1184 	return rc;
1185 }
1186 EXPORT_SYMBOL_GPL(add_memory_driver_managed);
1187 
1188 #ifdef CONFIG_MEMORY_HOTREMOVE
1189 /*
1190  * Confirm all pages in a range [start, end) belong to the same zone (skipping
1191  * memory holes). When true, return the zone.
1192  */
test_pages_in_a_zone(unsigned long start_pfn,unsigned long end_pfn)1193 struct zone *test_pages_in_a_zone(unsigned long start_pfn,
1194 				  unsigned long end_pfn)
1195 {
1196 	unsigned long pfn, sec_end_pfn;
1197 	struct zone *zone = NULL;
1198 	struct page *page;
1199 	int i;
1200 	for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn + 1);
1201 	     pfn < end_pfn;
1202 	     pfn = sec_end_pfn, sec_end_pfn += PAGES_PER_SECTION) {
1203 		/* Make sure the memory section is present first */
1204 		if (!present_section_nr(pfn_to_section_nr(pfn)))
1205 			continue;
1206 		for (; pfn < sec_end_pfn && pfn < end_pfn;
1207 		     pfn += MAX_ORDER_NR_PAGES) {
1208 			i = 0;
1209 			/* This is just a CONFIG_HOLES_IN_ZONE check.*/
1210 			while ((i < MAX_ORDER_NR_PAGES) &&
1211 				!pfn_valid_within(pfn + i))
1212 				i++;
1213 			if (i == MAX_ORDER_NR_PAGES || pfn + i >= end_pfn)
1214 				continue;
1215 			/* Check if we got outside of the zone */
1216 			if (zone && !zone_spans_pfn(zone, pfn + i))
1217 				return NULL;
1218 			page = pfn_to_page(pfn + i);
1219 			if (zone && page_zone(page) != zone)
1220 				return NULL;
1221 			zone = page_zone(page);
1222 		}
1223 	}
1224 
1225 	return zone;
1226 }
1227 
1228 /*
1229  * Scan pfn range [start,end) to find movable/migratable pages (LRU pages,
1230  * non-lru movable pages and hugepages). Will skip over most unmovable
1231  * pages (esp., pages that can be skipped when offlining), but bail out on
1232  * definitely unmovable pages.
1233  *
1234  * Returns:
1235  *	0 in case a movable page is found and movable_pfn was updated.
1236  *	-ENOENT in case no movable page was found.
1237  *	-EBUSY in case a definitely unmovable page was found.
1238  */
scan_movable_pages(unsigned long start,unsigned long end,unsigned long * movable_pfn)1239 static int scan_movable_pages(unsigned long start, unsigned long end,
1240 			      unsigned long *movable_pfn)
1241 {
1242 	unsigned long pfn;
1243 
1244 	for (pfn = start; pfn < end; pfn++) {
1245 		struct page *page, *head;
1246 		unsigned long skip;
1247 
1248 		if (!pfn_valid(pfn))
1249 			continue;
1250 		page = pfn_to_page(pfn);
1251 		if (PageLRU(page))
1252 			goto found;
1253 		if (__PageMovable(page))
1254 			goto found;
1255 
1256 		/*
1257 		 * PageOffline() pages that are not marked __PageMovable() and
1258 		 * have a reference count > 0 (after MEM_GOING_OFFLINE) are
1259 		 * definitely unmovable. If their reference count would be 0,
1260 		 * they could at least be skipped when offlining memory.
1261 		 */
1262 		if (PageOffline(page) && page_count(page))
1263 			return -EBUSY;
1264 
1265 		if (!PageHuge(page))
1266 			continue;
1267 		head = compound_head(page);
1268 		if (page_huge_active(head))
1269 			goto found;
1270 		skip = compound_nr(head) - (page - head);
1271 		pfn += skip - 1;
1272 	}
1273 	return -ENOENT;
1274 found:
1275 	*movable_pfn = pfn;
1276 	return 0;
1277 }
1278 
1279 static int
do_migrate_range(unsigned long start_pfn,unsigned long end_pfn)1280 do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1281 {
1282 	unsigned long pfn;
1283 	struct page *page, *head;
1284 	int ret = 0;
1285 	LIST_HEAD(source);
1286 
1287 	for (pfn = start_pfn; pfn < end_pfn; pfn++) {
1288 		if (!pfn_valid(pfn))
1289 			continue;
1290 		page = pfn_to_page(pfn);
1291 		head = compound_head(page);
1292 
1293 		if (PageHuge(page)) {
1294 			pfn = page_to_pfn(head) + compound_nr(head) - 1;
1295 			isolate_huge_page(head, &source);
1296 			continue;
1297 		} else if (PageTransHuge(page))
1298 			pfn = page_to_pfn(head) + thp_nr_pages(page) - 1;
1299 
1300 		/*
1301 		 * HWPoison pages have elevated reference counts so the migration would
1302 		 * fail on them. It also doesn't make any sense to migrate them in the
1303 		 * first place. Still try to unmap such a page in case it is still mapped
1304 		 * (e.g. current hwpoison implementation doesn't unmap KSM pages but keep
1305 		 * the unmap as the catch all safety net).
1306 		 */
1307 		if (PageHWPoison(page)) {
1308 			if (WARN_ON(PageLRU(page)))
1309 				isolate_lru_page(page);
1310 			if (page_mapped(page))
1311 				try_to_unmap(page, TTU_IGNORE_MLOCK);
1312 			continue;
1313 		}
1314 
1315 		if (!get_page_unless_zero(page))
1316 			continue;
1317 		/*
1318 		 * We can skip free pages. And we can deal with pages on
1319 		 * LRU and non-lru movable pages.
1320 		 */
1321 		if (PageLRU(page))
1322 			ret = isolate_lru_page(page);
1323 		else
1324 			ret = isolate_movable_page(page, ISOLATE_UNEVICTABLE);
1325 		if (!ret) { /* Success */
1326 			list_add_tail(&page->lru, &source);
1327 			if (!__PageMovable(page))
1328 				inc_node_page_state(page, NR_ISOLATED_ANON +
1329 						    page_is_file_lru(page));
1330 
1331 		} else {
1332 			pr_warn("failed to isolate pfn %lx\n", pfn);
1333 			dump_page(page, "isolation failed");
1334 		}
1335 		put_page(page);
1336 	}
1337 	if (!list_empty(&source)) {
1338 		nodemask_t nmask = node_states[N_MEMORY];
1339 		struct migration_target_control mtc = {
1340 			.nmask = &nmask,
1341 			.gfp_mask = GFP_USER | __GFP_MOVABLE | __GFP_RETRY_MAYFAIL,
1342 		};
1343 
1344 		/*
1345 		 * We have checked that migration range is on a single zone so
1346 		 * we can use the nid of the first page to all the others.
1347 		 */
1348 		mtc.nid = page_to_nid(list_first_entry(&source, struct page, lru));
1349 
1350 		/*
1351 		 * try to allocate from a different node but reuse this node
1352 		 * if there are no other online nodes to be used (e.g. we are
1353 		 * offlining a part of the only existing node)
1354 		 */
1355 		node_clear(mtc.nid, nmask);
1356 		if (nodes_empty(nmask))
1357 			node_set(mtc.nid, nmask);
1358 		ret = migrate_pages(&source, alloc_migration_target, NULL,
1359 			(unsigned long)&mtc, MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
1360 		if (ret) {
1361 			list_for_each_entry(page, &source, lru) {
1362 				pr_warn("migrating pfn %lx failed ret:%d ",
1363 				       page_to_pfn(page), ret);
1364 				dump_page(page, "migration failure");
1365 			}
1366 			putback_movable_pages(&source);
1367 		}
1368 	}
1369 
1370 	return ret;
1371 }
1372 
cmdline_parse_movable_node(char * p)1373 static int __init cmdline_parse_movable_node(char *p)
1374 {
1375 	movable_node_enabled = true;
1376 	return 0;
1377 }
1378 early_param("movable_node", cmdline_parse_movable_node);
1379 
1380 /* 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)1381 static void node_states_check_changes_offline(unsigned long nr_pages,
1382 		struct zone *zone, struct memory_notify *arg)
1383 {
1384 	struct pglist_data *pgdat = zone->zone_pgdat;
1385 	unsigned long present_pages = 0;
1386 	enum zone_type zt;
1387 
1388 	arg->status_change_nid = NUMA_NO_NODE;
1389 	arg->status_change_nid_normal = NUMA_NO_NODE;
1390 	arg->status_change_nid_high = NUMA_NO_NODE;
1391 
1392 	/*
1393 	 * Check whether node_states[N_NORMAL_MEMORY] will be changed.
1394 	 * If the memory to be offline is within the range
1395 	 * [0..ZONE_NORMAL], and it is the last present memory there,
1396 	 * the zones in that range will become empty after the offlining,
1397 	 * thus we can determine that we need to clear the node from
1398 	 * node_states[N_NORMAL_MEMORY].
1399 	 */
1400 	for (zt = 0; zt <= ZONE_NORMAL; zt++)
1401 		present_pages += pgdat->node_zones[zt].present_pages;
1402 	if (zone_idx(zone) <= ZONE_NORMAL && nr_pages >= present_pages)
1403 		arg->status_change_nid_normal = zone_to_nid(zone);
1404 
1405 #ifdef CONFIG_HIGHMEM
1406 	/*
1407 	 * node_states[N_HIGH_MEMORY] contains nodes which
1408 	 * have normal memory or high memory.
1409 	 * Here we add the present_pages belonging to ZONE_HIGHMEM.
1410 	 * If the zone is within the range of [0..ZONE_HIGHMEM), and
1411 	 * we determine that the zones in that range become empty,
1412 	 * we need to clear the node for N_HIGH_MEMORY.
1413 	 */
1414 	present_pages += pgdat->node_zones[ZONE_HIGHMEM].present_pages;
1415 	if (zone_idx(zone) <= ZONE_HIGHMEM && nr_pages >= present_pages)
1416 		arg->status_change_nid_high = zone_to_nid(zone);
1417 #endif
1418 
1419 	/*
1420 	 * We have accounted the pages from [0..ZONE_NORMAL), and
1421 	 * in case of CONFIG_HIGHMEM the pages from ZONE_HIGHMEM
1422 	 * as well.
1423 	 * Here we count the possible pages from ZONE_MOVABLE.
1424 	 * If after having accounted all the pages, we see that the nr_pages
1425 	 * to be offlined is over or equal to the accounted pages,
1426 	 * we know that the node will become empty, and so, we can clear
1427 	 * it for N_MEMORY as well.
1428 	 */
1429 	present_pages += pgdat->node_zones[ZONE_MOVABLE].present_pages;
1430 
1431 	if (nr_pages >= present_pages)
1432 		arg->status_change_nid = zone_to_nid(zone);
1433 }
1434 
node_states_clear_node(int node,struct memory_notify * arg)1435 static void node_states_clear_node(int node, struct memory_notify *arg)
1436 {
1437 	if (arg->status_change_nid_normal >= 0)
1438 		node_clear_state(node, N_NORMAL_MEMORY);
1439 
1440 	if (arg->status_change_nid_high >= 0)
1441 		node_clear_state(node, N_HIGH_MEMORY);
1442 
1443 	if (arg->status_change_nid >= 0)
1444 		node_clear_state(node, N_MEMORY);
1445 }
1446 
count_system_ram_pages_cb(unsigned long start_pfn,unsigned long nr_pages,void * data)1447 static int count_system_ram_pages_cb(unsigned long start_pfn,
1448 				     unsigned long nr_pages, void *data)
1449 {
1450 	unsigned long *nr_system_ram_pages = data;
1451 
1452 	*nr_system_ram_pages += nr_pages;
1453 	return 0;
1454 }
1455 
offline_pages(unsigned long start_pfn,unsigned long nr_pages)1456 int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1457 {
1458 	const unsigned long end_pfn = start_pfn + nr_pages;
1459 	unsigned long pfn, system_ram_pages = 0;
1460 	unsigned long flags;
1461 	struct zone *zone;
1462 	struct memory_notify arg;
1463 	int ret, node;
1464 	char *reason;
1465 
1466 	/* We can only offline full sections (e.g., SECTION_IS_ONLINE) */
1467 	if (WARN_ON_ONCE(!nr_pages ||
1468 			 !IS_ALIGNED(start_pfn | nr_pages, PAGES_PER_SECTION)))
1469 		return -EINVAL;
1470 
1471 	mem_hotplug_begin();
1472 
1473 	/*
1474 	 * Don't allow to offline memory blocks that contain holes.
1475 	 * Consequently, memory blocks with holes can never get onlined
1476 	 * via the hotplug path - online_pages() - as hotplugged memory has
1477 	 * no holes. This way, we e.g., don't have to worry about marking
1478 	 * memory holes PG_reserved, don't need pfn_valid() checks, and can
1479 	 * avoid using walk_system_ram_range() later.
1480 	 */
1481 	walk_system_ram_range(start_pfn, nr_pages, &system_ram_pages,
1482 			      count_system_ram_pages_cb);
1483 	if (system_ram_pages != nr_pages) {
1484 		ret = -EINVAL;
1485 		reason = "memory holes";
1486 		goto failed_removal;
1487 	}
1488 
1489 	/* This makes hotplug much easier...and readable.
1490 	   we assume this for now. .*/
1491 	zone = test_pages_in_a_zone(start_pfn, end_pfn);
1492 	if (!zone) {
1493 		ret = -EINVAL;
1494 		reason = "multizone range";
1495 		goto failed_removal;
1496 	}
1497 	node = zone_to_nid(zone);
1498 
1499 	/* set above range as isolated */
1500 	ret = start_isolate_page_range(start_pfn, end_pfn,
1501 				       MIGRATE_MOVABLE,
1502 				       MEMORY_OFFLINE | REPORT_FAILURE);
1503 	if (ret) {
1504 		reason = "failure to isolate range";
1505 		goto failed_removal;
1506 	}
1507 
1508 	arg.start_pfn = start_pfn;
1509 	arg.nr_pages = nr_pages;
1510 	node_states_check_changes_offline(nr_pages, zone, &arg);
1511 
1512 	ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1513 	ret = notifier_to_errno(ret);
1514 	if (ret) {
1515 		reason = "notifier failure";
1516 		goto failed_removal_isolated;
1517 	}
1518 
1519 	do {
1520 		pfn = start_pfn;
1521 		do {
1522 			if (signal_pending(current)) {
1523 				ret = -EINTR;
1524 				reason = "signal backoff";
1525 				goto failed_removal_isolated;
1526 			}
1527 
1528 			cond_resched();
1529 			lru_add_drain_all();
1530 
1531 			ret = scan_movable_pages(pfn, end_pfn, &pfn);
1532 			if (!ret) {
1533 				/*
1534 				 * TODO: fatal migration failures should bail
1535 				 * out
1536 				 */
1537 				do_migrate_range(pfn, end_pfn);
1538 			}
1539 		} while (!ret);
1540 
1541 		if (ret != -ENOENT) {
1542 			reason = "unmovable page";
1543 			goto failed_removal_isolated;
1544 		}
1545 
1546 		/*
1547 		 * Dissolve free hugepages in the memory block before doing
1548 		 * offlining actually in order to make hugetlbfs's object
1549 		 * counting consistent.
1550 		 */
1551 		ret = dissolve_free_huge_pages(start_pfn, end_pfn);
1552 		if (ret) {
1553 			reason = "failure to dissolve huge pages";
1554 			goto failed_removal_isolated;
1555 		}
1556 
1557 		/*
1558 		 * per-cpu pages are drained in start_isolate_page_range, but if
1559 		 * there are still pages that are not free, make sure that we
1560 		 * drain again, because when we isolated range we might
1561 		 * have raced with another thread that was adding pages to pcp
1562 		 * list.
1563 		 *
1564 		 * Forward progress should be still guaranteed because
1565 		 * pages on the pcp list can only belong to MOVABLE_ZONE
1566 		 * because has_unmovable_pages explicitly checks for
1567 		 * PageBuddy on freed pages on other zones.
1568 		 */
1569 		ret = test_pages_isolated(start_pfn, end_pfn, MEMORY_OFFLINE);
1570 		if (ret)
1571 			drain_all_pages(zone);
1572 	} while (ret);
1573 
1574 	/* Mark all sections offline and remove free pages from the buddy. */
1575 	__offline_isolated_pages(start_pfn, end_pfn);
1576 	pr_info("Offlined Pages %ld\n", nr_pages);
1577 
1578 	/*
1579 	 * The memory sections are marked offline, and the pageblock flags
1580 	 * effectively stale; nobody should be touching them. Fixup the number
1581 	 * of isolated pageblocks, memory onlining will properly revert this.
1582 	 */
1583 	spin_lock_irqsave(&zone->lock, flags);
1584 	zone->nr_isolate_pageblock -= nr_pages / pageblock_nr_pages;
1585 	spin_unlock_irqrestore(&zone->lock, flags);
1586 
1587 	/* removal success */
1588 	adjust_managed_page_count(pfn_to_page(start_pfn), -nr_pages);
1589 	zone->present_pages -= nr_pages;
1590 
1591 	pgdat_resize_lock(zone->zone_pgdat, &flags);
1592 	zone->zone_pgdat->node_present_pages -= nr_pages;
1593 	pgdat_resize_unlock(zone->zone_pgdat, &flags);
1594 
1595 	init_per_zone_wmark_min();
1596 
1597 	if (!populated_zone(zone)) {
1598 		zone_pcp_reset(zone);
1599 		build_all_zonelists(NULL);
1600 	} else
1601 		zone_pcp_update(zone);
1602 
1603 	node_states_clear_node(node, &arg);
1604 	if (arg.status_change_nid >= 0) {
1605 		kswapd_stop(node);
1606 		kcompactd_stop(node);
1607 #ifdef CONFIG_HYPERHOLD_ZSWAPD
1608 		zswapd_stop(node);
1609 #endif
1610 	}
1611 
1612 	writeback_set_ratelimit();
1613 
1614 	memory_notify(MEM_OFFLINE, &arg);
1615 	remove_pfn_range_from_zone(zone, start_pfn, nr_pages);
1616 	mem_hotplug_done();
1617 	return 0;
1618 
1619 failed_removal_isolated:
1620 	undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1621 	memory_notify(MEM_CANCEL_OFFLINE, &arg);
1622 failed_removal:
1623 	pr_debug("memory offlining [mem %#010llx-%#010llx] failed due to %s\n",
1624 		 (unsigned long long) start_pfn << PAGE_SHIFT,
1625 		 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1,
1626 		 reason);
1627 	/* pushback to free area */
1628 	mem_hotplug_done();
1629 	return ret;
1630 }
1631 
check_memblock_offlined_cb(struct memory_block * mem,void * arg)1632 static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
1633 {
1634 	int ret = !is_memblock_offlined(mem);
1635 
1636 	if (unlikely(ret)) {
1637 		phys_addr_t beginpa, endpa;
1638 
1639 		beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
1640 		endpa = beginpa + memory_block_size_bytes() - 1;
1641 		pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n",
1642 			&beginpa, &endpa);
1643 
1644 		return -EBUSY;
1645 	}
1646 	return 0;
1647 }
1648 
check_cpu_on_node(pg_data_t * pgdat)1649 static int check_cpu_on_node(pg_data_t *pgdat)
1650 {
1651 	int cpu;
1652 
1653 	for_each_present_cpu(cpu) {
1654 		if (cpu_to_node(cpu) == pgdat->node_id)
1655 			/*
1656 			 * the cpu on this node isn't removed, and we can't
1657 			 * offline this node.
1658 			 */
1659 			return -EBUSY;
1660 	}
1661 
1662 	return 0;
1663 }
1664 
check_no_memblock_for_node_cb(struct memory_block * mem,void * arg)1665 static int check_no_memblock_for_node_cb(struct memory_block *mem, void *arg)
1666 {
1667 	int nid = *(int *)arg;
1668 
1669 	/*
1670 	 * If a memory block belongs to multiple nodes, the stored nid is not
1671 	 * reliable. However, such blocks are always online (e.g., cannot get
1672 	 * offlined) and, therefore, are still spanned by the node.
1673 	 */
1674 	return mem->nid == nid ? -EEXIST : 0;
1675 }
1676 
1677 /**
1678  * try_offline_node
1679  * @nid: the node ID
1680  *
1681  * Offline a node if all memory sections and cpus of the node are removed.
1682  *
1683  * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1684  * and online/offline operations before this call.
1685  */
try_offline_node(int nid)1686 void try_offline_node(int nid)
1687 {
1688 	pg_data_t *pgdat = NODE_DATA(nid);
1689 	int rc;
1690 
1691 	/*
1692 	 * If the node still spans pages (especially ZONE_DEVICE), don't
1693 	 * offline it. A node spans memory after move_pfn_range_to_zone(),
1694 	 * e.g., after the memory block was onlined.
1695 	 */
1696 	if (pgdat->node_spanned_pages)
1697 		return;
1698 
1699 	/*
1700 	 * Especially offline memory blocks might not be spanned by the
1701 	 * node. They will get spanned by the node once they get onlined.
1702 	 * However, they link to the node in sysfs and can get onlined later.
1703 	 */
1704 	rc = for_each_memory_block(&nid, check_no_memblock_for_node_cb);
1705 	if (rc)
1706 		return;
1707 
1708 	if (check_cpu_on_node(pgdat))
1709 		return;
1710 
1711 	/*
1712 	 * all memory/cpu of this node are removed, we can offline this
1713 	 * node now.
1714 	 */
1715 	node_set_offline(nid);
1716 	unregister_one_node(nid);
1717 }
1718 EXPORT_SYMBOL(try_offline_node);
1719 
try_remove_memory(int nid,u64 start,u64 size)1720 static int __ref try_remove_memory(int nid, u64 start, u64 size)
1721 {
1722 	int rc = 0;
1723 
1724 	BUG_ON(check_hotplug_memory_range(start, size));
1725 
1726 	/*
1727 	 * All memory blocks must be offlined before removing memory.  Check
1728 	 * whether all memory blocks in question are offline and return error
1729 	 * if this is not the case.
1730 	 */
1731 	rc = walk_memory_blocks(start, size, NULL, check_memblock_offlined_cb);
1732 	if (rc)
1733 		return rc;
1734 
1735 	/* remove memmap entry */
1736 	firmware_map_remove(start, start + size, "System RAM");
1737 
1738 	/*
1739 	 * Memory block device removal under the device_hotplug_lock is
1740 	 * a barrier against racing online attempts.
1741 	 */
1742 	remove_memory_block_devices(start, size);
1743 
1744 	mem_hotplug_begin();
1745 
1746 	arch_remove_memory(nid, start, size, NULL);
1747 
1748 	if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK)) {
1749 		memblock_free(start, size);
1750 		memblock_remove(start, size);
1751 	}
1752 
1753 	release_mem_region_adjustable(start, size);
1754 
1755 	try_offline_node(nid);
1756 
1757 	mem_hotplug_done();
1758 	return 0;
1759 }
1760 
1761 /**
1762  * remove_memory
1763  * @nid: the node ID
1764  * @start: physical address of the region to remove
1765  * @size: size of the region to remove
1766  *
1767  * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1768  * and online/offline operations before this call, as required by
1769  * try_offline_node().
1770  */
__remove_memory(int nid,u64 start,u64 size)1771 void __remove_memory(int nid, u64 start, u64 size)
1772 {
1773 
1774 	/*
1775 	 * trigger BUG() if some memory is not offlined prior to calling this
1776 	 * function
1777 	 */
1778 	if (try_remove_memory(nid, start, size))
1779 		BUG();
1780 }
1781 
1782 /*
1783  * Remove memory if every memory block is offline, otherwise return -EBUSY is
1784  * some memory is not offline
1785  */
remove_memory(int nid,u64 start,u64 size)1786 int remove_memory(int nid, u64 start, u64 size)
1787 {
1788 	int rc;
1789 
1790 	lock_device_hotplug();
1791 	rc  = try_remove_memory(nid, start, size);
1792 	unlock_device_hotplug();
1793 
1794 	return rc;
1795 }
1796 EXPORT_SYMBOL_GPL(remove_memory);
1797 
1798 /*
1799  * Try to offline and remove a memory block. Might take a long time to
1800  * finish in case memory is still in use. Primarily useful for memory devices
1801  * that logically unplugged all memory (so it's no longer in use) and want to
1802  * offline + remove the memory block.
1803  */
offline_and_remove_memory(int nid,u64 start,u64 size)1804 int offline_and_remove_memory(int nid, u64 start, u64 size)
1805 {
1806 	struct memory_block *mem;
1807 	int rc = -EINVAL;
1808 
1809 	if (!IS_ALIGNED(start, memory_block_size_bytes()) ||
1810 	    size != memory_block_size_bytes())
1811 		return rc;
1812 
1813 	lock_device_hotplug();
1814 	mem = find_memory_block(__pfn_to_section(PFN_DOWN(start)));
1815 	if (mem)
1816 		rc = device_offline(&mem->dev);
1817 	/* Ignore if the device is already offline. */
1818 	if (rc > 0)
1819 		rc = 0;
1820 
1821 	/*
1822 	 * In case we succeeded to offline the memory block, remove it.
1823 	 * This cannot fail as it cannot get onlined in the meantime.
1824 	 */
1825 	if (!rc) {
1826 		rc = try_remove_memory(nid, start, size);
1827 		WARN_ON_ONCE(rc);
1828 	}
1829 	unlock_device_hotplug();
1830 
1831 	return rc;
1832 }
1833 EXPORT_SYMBOL_GPL(offline_and_remove_memory);
1834 #endif /* CONFIG_MEMORY_HOTREMOVE */
1835