]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - mm/memory_hotplug.c
mm/memory_hotplug: make unregister_memory_section() never fail
[thirdparty/kernel/stable.git] / mm / memory_hotplug.c
CommitLineData
3947be19
DH
1/*
2 * linux/mm/memory_hotplug.c
3 *
4 * Copyright (C)
5 */
6
3947be19
DH
7#include <linux/stddef.h>
8#include <linux/mm.h>
174cd4b1 9#include <linux/sched/signal.h>
3947be19
DH
10#include <linux/swap.h>
11#include <linux/interrupt.h>
12#include <linux/pagemap.h>
3947be19 13#include <linux/compiler.h>
b95f1b31 14#include <linux/export.h>
3947be19 15#include <linux/pagevec.h>
2d1d43f6 16#include <linux/writeback.h>
3947be19
DH
17#include <linux/slab.h>
18#include <linux/sysctl.h>
19#include <linux/cpu.h>
20#include <linux/memory.h>
4b94ffdc 21#include <linux/memremap.h>
3947be19
DH
22#include <linux/memory_hotplug.h>
23#include <linux/highmem.h>
24#include <linux/vmalloc.h>
0a547039 25#include <linux/ioport.h>
0c0e6195
KH
26#include <linux/delay.h>
27#include <linux/migrate.h>
28#include <linux/page-isolation.h>
71088785 29#include <linux/pfn.h>
6ad696d2 30#include <linux/suspend.h>
6d9c285a 31#include <linux/mm_inline.h>
d96ae530 32#include <linux/firmware-map.h>
60a5a19e 33#include <linux/stop_machine.h>
c8721bbb 34#include <linux/hugetlb.h>
c5320926 35#include <linux/memblock.h>
698b1b30 36#include <linux/compaction.h>
b15c8726 37#include <linux/rmap.h>
3947be19
DH
38
39#include <asm/tlbflush.h>
40
1e5ad9a3
AB
41#include "internal.h"
42
9d0ad8ca
DK
43/*
44 * online_page_callback contains pointer to current page onlining function.
45 * Initially it is generic_online_page(). If it is required it could be
46 * changed by calling set_online_page_callback() for callback registration
47 * and restore_online_page_callback() for generic callback restore.
48 */
49
a9cd410a 50static void generic_online_page(struct page *page, unsigned int order);
9d0ad8ca
DK
51
52static online_page_callback_t online_page_callback = generic_online_page;
bfc8c901 53static DEFINE_MUTEX(online_page_callback_lock);
9d0ad8ca 54
3f906ba2 55DEFINE_STATIC_PERCPU_RWSEM(mem_hotplug_lock);
bfc8c901 56
3f906ba2
TG
57void get_online_mems(void)
58{
59 percpu_down_read(&mem_hotplug_lock);
60}
bfc8c901 61
3f906ba2
TG
62void put_online_mems(void)
63{
64 percpu_up_read(&mem_hotplug_lock);
65}
bfc8c901 66
4932381e
MH
67bool movable_node_enabled = false;
68
8604d9e5 69#ifndef CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE
31bc3858 70bool memhp_auto_online;
8604d9e5
VK
71#else
72bool memhp_auto_online = true;
73#endif
31bc3858
VK
74EXPORT_SYMBOL_GPL(memhp_auto_online);
75
86dd995d
VK
76static int __init setup_memhp_default_state(char *str)
77{
78 if (!strcmp(str, "online"))
79 memhp_auto_online = true;
80 else if (!strcmp(str, "offline"))
81 memhp_auto_online = false;
82
83 return 1;
84}
85__setup("memhp_default_state=", setup_memhp_default_state);
86
30467e0b 87void mem_hotplug_begin(void)
20d6c96b 88{
3f906ba2
TG
89 cpus_read_lock();
90 percpu_down_write(&mem_hotplug_lock);
20d6c96b
KM
91}
92
30467e0b 93void mem_hotplug_done(void)
bfc8c901 94{
3f906ba2
TG
95 percpu_up_write(&mem_hotplug_lock);
96 cpus_read_unlock();
bfc8c901 97}
20d6c96b 98
357b4da5
JG
99u64 max_mem_size = U64_MAX;
100
45e0b78b
KM
101/* add this memory to iomem resource */
102static struct resource *register_memory_resource(u64 start, u64 size)
103{
2794129e
DH
104 struct resource *res;
105 unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
106 char *resource_name = "System RAM";
357b4da5
JG
107
108 if (start + size > max_mem_size)
109 return ERR_PTR(-E2BIG);
110
2794129e
DH
111 /*
112 * Request ownership of the new memory range. This might be
113 * a child of an existing resource that was present but
114 * not marked as busy.
115 */
116 res = __request_region(&iomem_resource, start, size,
117 resource_name, flags);
118
119 if (!res) {
120 pr_debug("Unable to reserve System RAM region: %016llx->%016llx\n",
121 start, start + size);
6f754ba4 122 return ERR_PTR(-EEXIST);
45e0b78b
KM
123 }
124 return res;
125}
126
127static void release_memory_resource(struct resource *res)
128{
129 if (!res)
130 return;
131 release_resource(res);
132 kfree(res);
133 return;
134}
135
53947027 136#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
46723bfa
YI
137void get_page_bootmem(unsigned long info, struct page *page,
138 unsigned long type)
04753278 139{
ddffe98d 140 page->freelist = (void *)type;
04753278
YG
141 SetPagePrivate(page);
142 set_page_private(page, info);
fe896d18 143 page_ref_inc(page);
04753278
YG
144}
145
170a5a7e 146void put_page_bootmem(struct page *page)
04753278 147{
5f24ce5f 148 unsigned long type;
04753278 149
ddffe98d 150 type = (unsigned long) page->freelist;
5f24ce5f
AA
151 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
152 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
04753278 153
fe896d18 154 if (page_ref_dec_return(page) == 1) {
ddffe98d 155 page->freelist = NULL;
04753278
YG
156 ClearPagePrivate(page);
157 set_page_private(page, 0);
5f24ce5f 158 INIT_LIST_HEAD(&page->lru);
170a5a7e 159 free_reserved_page(page);
04753278 160 }
04753278
YG
161}
162
46723bfa
YI
163#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
164#ifndef CONFIG_SPARSEMEM_VMEMMAP
d92bc318 165static void register_page_bootmem_info_section(unsigned long start_pfn)
04753278
YG
166{
167 unsigned long *usemap, mapsize, section_nr, i;
168 struct mem_section *ms;
169 struct page *page, *memmap;
170
04753278
YG
171 section_nr = pfn_to_section_nr(start_pfn);
172 ms = __nr_to_section(section_nr);
173
174 /* Get section's memmap address */
175 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
176
177 /*
178 * Get page for the memmap's phys address
179 * XXX: need more consideration for sparse_vmemmap...
180 */
181 page = virt_to_page(memmap);
182 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
183 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
184
185 /* remember memmap's page */
186 for (i = 0; i < mapsize; i++, page++)
187 get_page_bootmem(section_nr, page, SECTION_INFO);
188
9ac9322d 189 usemap = ms->pageblock_flags;
04753278
YG
190 page = virt_to_page(usemap);
191
192 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
193
194 for (i = 0; i < mapsize; i++, page++)
af370fb8 195 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
04753278
YG
196
197}
46723bfa
YI
198#else /* CONFIG_SPARSEMEM_VMEMMAP */
199static void register_page_bootmem_info_section(unsigned long start_pfn)
200{
201 unsigned long *usemap, mapsize, section_nr, i;
202 struct mem_section *ms;
203 struct page *page, *memmap;
204
46723bfa
YI
205 section_nr = pfn_to_section_nr(start_pfn);
206 ms = __nr_to_section(section_nr);
207
208 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
209
210 register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
211
9ac9322d 212 usemap = ms->pageblock_flags;
46723bfa
YI
213 page = virt_to_page(usemap);
214
215 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
216
217 for (i = 0; i < mapsize; i++, page++)
218 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
219}
220#endif /* !CONFIG_SPARSEMEM_VMEMMAP */
04753278 221
7ded384a 222void __init register_page_bootmem_info_node(struct pglist_data *pgdat)
04753278
YG
223{
224 unsigned long i, pfn, end_pfn, nr_pages;
225 int node = pgdat->node_id;
226 struct page *page;
04753278
YG
227
228 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
229 page = virt_to_page(pgdat);
230
231 for (i = 0; i < nr_pages; i++, page++)
232 get_page_bootmem(node, page, NODE_INFO);
233
04753278 234 pfn = pgdat->node_start_pfn;
c1f19495 235 end_pfn = pgdat_end_pfn(pgdat);
04753278 236
7e9f5eb0 237 /* register section info */
f14851af 238 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
239 /*
240 * Some platforms can assign the same pfn to multiple nodes - on
241 * node0 as well as nodeN. To avoid registering a pfn against
242 * multiple nodes we check that this pfn does not already
7e9f5eb0 243 * reside in some other nodes.
f14851af 244 */
f65e91df 245 if (pfn_valid(pfn) && (early_pfn_to_nid(pfn) == node))
f14851af 246 register_page_bootmem_info_section(pfn);
247 }
04753278 248}
46723bfa 249#endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
04753278 250
f1dd2cd1 251static int __meminit __add_section(int nid, unsigned long phys_start_pfn,
7b73d978 252 struct vmem_altmap *altmap, bool want_memblock)
3947be19 253{
3947be19
DH
254 int ret;
255
ebd15302
KH
256 if (pfn_valid(phys_start_pfn))
257 return -EEXIST;
258
4e0d2e7e 259 ret = sparse_add_one_section(nid, phys_start_pfn, altmap);
3947be19
DH
260 if (ret < 0)
261 return ret;
262
1b862aec
MH
263 if (!want_memblock)
264 return 0;
265
fc44f7f9 266 return hotplug_memory_register(nid, __pfn_to_section(phys_start_pfn));
3947be19
DH
267}
268
4edd7cef
DR
269/*
270 * Reasonably generic function for adding memory. It is
271 * expected that archs that support memory hotplug will
272 * call this function after deciding the zone to which to
273 * add the new pages.
274 */
f1dd2cd1 275int __ref __add_pages(int nid, unsigned long phys_start_pfn,
940519f0 276 unsigned long nr_pages, struct mhp_restrictions *restrictions)
4edd7cef
DR
277{
278 unsigned long i;
279 int err = 0;
280 int start_sec, end_sec;
940519f0 281 struct vmem_altmap *altmap = restrictions->altmap;
4b94ffdc 282
4edd7cef
DR
283 /* during initialize mem_map, align hot-added range to section */
284 start_sec = pfn_to_section_nr(phys_start_pfn);
285 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
286
4b94ffdc
DW
287 if (altmap) {
288 /*
289 * Validate altmap is within bounds of the total request
290 */
291 if (altmap->base_pfn != phys_start_pfn
292 || vmem_altmap_offset(altmap) > nr_pages) {
293 pr_warn_once("memory add fail, invalid altmap\n");
7cf91a98
JK
294 err = -EINVAL;
295 goto out;
4b94ffdc
DW
296 }
297 altmap->alloc = 0;
298 }
299
4edd7cef 300 for (i = start_sec; i <= end_sec; i++) {
7b73d978 301 err = __add_section(nid, section_nr_to_pfn(i), altmap,
940519f0 302 restrictions->flags & MHP_MEMBLOCK_API);
4edd7cef
DR
303
304 /*
305 * EEXIST is finally dealt with by ioresource collision
306 * check. see add_memory() => register_memory_resource()
307 * Warning will be printed if there is collision.
308 */
309 if (err && (err != -EEXIST))
310 break;
311 err = 0;
f64ac5e6 312 cond_resched();
4edd7cef 313 }
c435a390 314 vmemmap_populate_print_last();
7cf91a98 315out:
4edd7cef
DR
316 return err;
317}
4edd7cef
DR
318
319#ifdef CONFIG_MEMORY_HOTREMOVE
815121d2 320/* find the smallest valid pfn in the range [start_pfn, end_pfn) */
d09b0137 321static unsigned long find_smallest_section_pfn(int nid, struct zone *zone,
815121d2
YI
322 unsigned long start_pfn,
323 unsigned long end_pfn)
324{
325 struct mem_section *ms;
326
327 for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
328 ms = __pfn_to_section(start_pfn);
329
330 if (unlikely(!valid_section(ms)))
331 continue;
332
333 if (unlikely(pfn_to_nid(start_pfn) != nid))
334 continue;
335
336 if (zone && zone != page_zone(pfn_to_page(start_pfn)))
337 continue;
338
339 return start_pfn;
340 }
341
342 return 0;
343}
344
345/* find the biggest valid pfn in the range [start_pfn, end_pfn). */
d09b0137 346static unsigned long find_biggest_section_pfn(int nid, struct zone *zone,
815121d2
YI
347 unsigned long start_pfn,
348 unsigned long end_pfn)
349{
350 struct mem_section *ms;
351 unsigned long pfn;
352
353 /* pfn is the end pfn of a memory section. */
354 pfn = end_pfn - 1;
355 for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
356 ms = __pfn_to_section(pfn);
357
358 if (unlikely(!valid_section(ms)))
359 continue;
360
361 if (unlikely(pfn_to_nid(pfn) != nid))
362 continue;
363
364 if (zone && zone != page_zone(pfn_to_page(pfn)))
365 continue;
366
367 return pfn;
368 }
369
370 return 0;
371}
372
373static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
374 unsigned long end_pfn)
375{
c33bc315
XQ
376 unsigned long zone_start_pfn = zone->zone_start_pfn;
377 unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
378 unsigned long zone_end_pfn = z;
815121d2
YI
379 unsigned long pfn;
380 struct mem_section *ms;
381 int nid = zone_to_nid(zone);
382
383 zone_span_writelock(zone);
384 if (zone_start_pfn == start_pfn) {
385 /*
386 * If the section is smallest section in the zone, it need
387 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
388 * In this case, we find second smallest valid mem_section
389 * for shrinking zone.
390 */
391 pfn = find_smallest_section_pfn(nid, zone, end_pfn,
392 zone_end_pfn);
393 if (pfn) {
394 zone->zone_start_pfn = pfn;
395 zone->spanned_pages = zone_end_pfn - pfn;
396 }
397 } else if (zone_end_pfn == end_pfn) {
398 /*
399 * If the section is biggest section in the zone, it need
400 * shrink zone->spanned_pages.
401 * In this case, we find second biggest valid mem_section for
402 * shrinking zone.
403 */
404 pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
405 start_pfn);
406 if (pfn)
407 zone->spanned_pages = pfn - zone_start_pfn + 1;
408 }
409
410 /*
411 * The section is not biggest or smallest mem_section in the zone, it
412 * only creates a hole in the zone. So in this case, we need not
413 * change the zone. But perhaps, the zone has only hole data. Thus
414 * it check the zone has only hole or not.
415 */
416 pfn = zone_start_pfn;
417 for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
418 ms = __pfn_to_section(pfn);
419
420 if (unlikely(!valid_section(ms)))
421 continue;
422
423 if (page_zone(pfn_to_page(pfn)) != zone)
424 continue;
425
426 /* If the section is current section, it continues the loop */
427 if (start_pfn == pfn)
428 continue;
429
430 /* If we find valid section, we have nothing to do */
431 zone_span_writeunlock(zone);
432 return;
433 }
434
435 /* The zone has no valid section */
436 zone->zone_start_pfn = 0;
437 zone->spanned_pages = 0;
438 zone_span_writeunlock(zone);
439}
440
441static void shrink_pgdat_span(struct pglist_data *pgdat,
442 unsigned long start_pfn, unsigned long end_pfn)
443{
83285c72
XQ
444 unsigned long pgdat_start_pfn = pgdat->node_start_pfn;
445 unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */
446 unsigned long pgdat_end_pfn = p;
815121d2
YI
447 unsigned long pfn;
448 struct mem_section *ms;
449 int nid = pgdat->node_id;
450
451 if (pgdat_start_pfn == start_pfn) {
452 /*
453 * If the section is smallest section in the pgdat, it need
454 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
455 * In this case, we find second smallest valid mem_section
456 * for shrinking zone.
457 */
458 pfn = find_smallest_section_pfn(nid, NULL, end_pfn,
459 pgdat_end_pfn);
460 if (pfn) {
461 pgdat->node_start_pfn = pfn;
462 pgdat->node_spanned_pages = pgdat_end_pfn - pfn;
463 }
464 } else if (pgdat_end_pfn == end_pfn) {
465 /*
466 * If the section is biggest section in the pgdat, it need
467 * shrink pgdat->node_spanned_pages.
468 * In this case, we find second biggest valid mem_section for
469 * shrinking zone.
470 */
471 pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn,
472 start_pfn);
473 if (pfn)
474 pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1;
475 }
476
477 /*
478 * If the section is not biggest or smallest mem_section in the pgdat,
479 * it only creates a hole in the pgdat. So in this case, we need not
480 * change the pgdat.
481 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
482 * has only hole or not.
483 */
484 pfn = pgdat_start_pfn;
485 for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) {
486 ms = __pfn_to_section(pfn);
487
488 if (unlikely(!valid_section(ms)))
489 continue;
490
491 if (pfn_to_nid(pfn) != nid)
492 continue;
493
494 /* If the section is current section, it continues the loop */
495 if (start_pfn == pfn)
496 continue;
497
498 /* If we find valid section, we have nothing to do */
499 return;
500 }
501
502 /* The pgdat has no valid section */
503 pgdat->node_start_pfn = 0;
504 pgdat->node_spanned_pages = 0;
505}
506
507static void __remove_zone(struct zone *zone, unsigned long start_pfn)
508{
509 struct pglist_data *pgdat = zone->zone_pgdat;
510 int nr_pages = PAGES_PER_SECTION;
815121d2
YI
511 unsigned long flags;
512
815121d2
YI
513 pgdat_resize_lock(zone->zone_pgdat, &flags);
514 shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
515 shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages);
516 pgdat_resize_unlock(zone->zone_pgdat, &flags);
517}
518
4b94ffdc 519static int __remove_section(struct zone *zone, struct mem_section *ms,
24b6d416 520 unsigned long map_offset, struct vmem_altmap *altmap)
ea01ea93 521{
815121d2
YI
522 unsigned long start_pfn;
523 int scn_nr;
ea01ea93
BP
524 int ret = -EINVAL;
525
526 if (!valid_section(ms))
527 return ret;
528
cb7b3a36 529 unregister_memory_section(ms);
ea01ea93 530
815121d2 531 scn_nr = __section_nr(ms);
1dd2bfc8 532 start_pfn = section_nr_to_pfn((unsigned long)scn_nr);
815121d2
YI
533 __remove_zone(zone, start_pfn);
534
24b6d416 535 sparse_remove_one_section(zone, ms, map_offset, altmap);
ea01ea93
BP
536 return 0;
537}
538
ea01ea93
BP
539/**
540 * __remove_pages() - remove sections of pages from a zone
541 * @zone: zone from which pages need to be removed
542 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
543 * @nr_pages: number of pages to remove (must be multiple of section size)
e8b098fc 544 * @altmap: alternative device page map or %NULL if default memmap is used
ea01ea93
BP
545 *
546 * Generic helper function to remove section mappings and sysfs entries
547 * for the section of the memory we are removing. Caller needs to make
548 * sure that pages are marked reserved and zones are adjust properly by
549 * calling offline_pages().
550 */
551int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
da024512 552 unsigned long nr_pages, struct vmem_altmap *altmap)
ea01ea93 553{
fe74ebb1 554 unsigned long i;
4b94ffdc
DW
555 unsigned long map_offset = 0;
556 int sections_to_remove, ret = 0;
557
558 /* In the ZONE_DEVICE case device driver owns the memory region */
559 if (is_dev_zone(zone)) {
4b94ffdc
DW
560 if (altmap)
561 map_offset = vmem_altmap_offset(altmap);
4b94ffdc 562 }
ea01ea93 563
7cf91a98
JK
564 clear_zone_contiguous(zone);
565
ea01ea93
BP
566 /*
567 * We can only remove entire sections
568 */
569 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
570 BUG_ON(nr_pages % PAGES_PER_SECTION);
571
ea01ea93
BP
572 sections_to_remove = nr_pages / PAGES_PER_SECTION;
573 for (i = 0; i < sections_to_remove; i++) {
574 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
4b94ffdc 575
dd33ad7b 576 cond_resched();
24b6d416
CH
577 ret = __remove_section(zone, __pfn_to_section(pfn), map_offset,
578 altmap);
4b94ffdc 579 map_offset = 0;
ea01ea93
BP
580 if (ret)
581 break;
582 }
7cf91a98
JK
583
584 set_zone_contiguous(zone);
585
ea01ea93
BP
586 return ret;
587}
4edd7cef 588#endif /* CONFIG_MEMORY_HOTREMOVE */
ea01ea93 589
9d0ad8ca
DK
590int set_online_page_callback(online_page_callback_t callback)
591{
592 int rc = -EINVAL;
593
bfc8c901
VD
594 get_online_mems();
595 mutex_lock(&online_page_callback_lock);
9d0ad8ca
DK
596
597 if (online_page_callback == generic_online_page) {
598 online_page_callback = callback;
599 rc = 0;
600 }
601
bfc8c901
VD
602 mutex_unlock(&online_page_callback_lock);
603 put_online_mems();
9d0ad8ca
DK
604
605 return rc;
606}
607EXPORT_SYMBOL_GPL(set_online_page_callback);
608
609int restore_online_page_callback(online_page_callback_t callback)
610{
611 int rc = -EINVAL;
612
bfc8c901
VD
613 get_online_mems();
614 mutex_lock(&online_page_callback_lock);
9d0ad8ca
DK
615
616 if (online_page_callback == callback) {
617 online_page_callback = generic_online_page;
618 rc = 0;
619 }
620
bfc8c901
VD
621 mutex_unlock(&online_page_callback_lock);
622 put_online_mems();
9d0ad8ca
DK
623
624 return rc;
625}
626EXPORT_SYMBOL_GPL(restore_online_page_callback);
627
628void __online_page_set_limits(struct page *page)
180c06ef 629{
9d0ad8ca
DK
630}
631EXPORT_SYMBOL_GPL(__online_page_set_limits);
632
633void __online_page_increment_counters(struct page *page)
634{
3dcc0571 635 adjust_managed_page_count(page, 1);
9d0ad8ca
DK
636}
637EXPORT_SYMBOL_GPL(__online_page_increment_counters);
180c06ef 638
9d0ad8ca
DK
639void __online_page_free(struct page *page)
640{
3dcc0571 641 __free_reserved_page(page);
180c06ef 642}
9d0ad8ca
DK
643EXPORT_SYMBOL_GPL(__online_page_free);
644
a9cd410a 645static void generic_online_page(struct page *page, unsigned int order)
9d0ad8ca 646{
cd02cf1a 647 kernel_map_pages(page, 1 << order, 1);
a9cd410a
AK
648 __free_pages_core(page, order);
649 totalram_pages_add(1UL << order);
650#ifdef CONFIG_HIGHMEM
651 if (PageHighMem(page))
652 totalhigh_pages_add(1UL << order);
653#endif
654}
655
656static int online_pages_blocks(unsigned long start, unsigned long nr_pages)
657{
658 unsigned long end = start + nr_pages;
659 int order, onlined_pages = 0;
660
661 while (start < end) {
662 order = min(MAX_ORDER - 1,
663 get_order(PFN_PHYS(end) - PFN_PHYS(start)));
664 (*online_page_callback)(pfn_to_page(start), order);
665
666 onlined_pages += (1UL << order);
667 start += (1UL << order);
668 }
669 return onlined_pages;
9d0ad8ca 670}
180c06ef 671
75884fb1
KH
672static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
673 void *arg)
3947be19 674{
75884fb1 675 unsigned long onlined_pages = *(unsigned long *)arg;
2d070eab 676
75884fb1 677 if (PageReserved(pfn_to_page(start_pfn)))
a9cd410a 678 onlined_pages += online_pages_blocks(start_pfn, nr_pages);
2d070eab
MH
679
680 online_mem_sections(start_pfn, start_pfn + nr_pages);
681
75884fb1
KH
682 *(unsigned long *)arg = onlined_pages;
683 return 0;
684}
685
d9713679
LJ
686/* check which state of node_states will be changed when online memory */
687static void node_states_check_changes_online(unsigned long nr_pages,
688 struct zone *zone, struct memory_notify *arg)
689{
690 int nid = zone_to_nid(zone);
d9713679 691
98fa15f3
AK
692 arg->status_change_nid = NUMA_NO_NODE;
693 arg->status_change_nid_normal = NUMA_NO_NODE;
694 arg->status_change_nid_high = NUMA_NO_NODE;
d9713679 695
8efe33f4
OS
696 if (!node_state(nid, N_MEMORY))
697 arg->status_change_nid = nid;
698 if (zone_idx(zone) <= ZONE_NORMAL && !node_state(nid, N_NORMAL_MEMORY))
d9713679 699 arg->status_change_nid_normal = nid;
6715ddf9 700#ifdef CONFIG_HIGHMEM
d3ba3ae1 701 if (zone_idx(zone) <= ZONE_HIGHMEM && !node_state(nid, N_HIGH_MEMORY))
6715ddf9 702 arg->status_change_nid_high = nid;
6715ddf9 703#endif
d9713679
LJ
704}
705
706static void node_states_set_node(int node, struct memory_notify *arg)
707{
708 if (arg->status_change_nid_normal >= 0)
709 node_set_state(node, N_NORMAL_MEMORY);
710
6715ddf9
LJ
711 if (arg->status_change_nid_high >= 0)
712 node_set_state(node, N_HIGH_MEMORY);
713
83d83612
OS
714 if (arg->status_change_nid >= 0)
715 node_set_state(node, N_MEMORY);
d9713679
LJ
716}
717
f1dd2cd1
MH
718static void __meminit resize_zone_range(struct zone *zone, unsigned long start_pfn,
719 unsigned long nr_pages)
720{
721 unsigned long old_end_pfn = zone_end_pfn(zone);
722
723 if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
724 zone->zone_start_pfn = start_pfn;
725
726 zone->spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - zone->zone_start_pfn;
727}
728
729static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned long start_pfn,
730 unsigned long nr_pages)
731{
732 unsigned long old_end_pfn = pgdat_end_pfn(pgdat);
733
734 if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
735 pgdat->node_start_pfn = start_pfn;
736
737 pgdat->node_spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - pgdat->node_start_pfn;
738}
739
a99583e7
CH
740void __ref move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
741 unsigned long nr_pages, struct vmem_altmap *altmap)
f1dd2cd1
MH
742{
743 struct pglist_data *pgdat = zone->zone_pgdat;
744 int nid = pgdat->node_id;
745 unsigned long flags;
df429ac0 746
f1dd2cd1
MH
747 clear_zone_contiguous(zone);
748
749 /* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */
750 pgdat_resize_lock(pgdat, &flags);
751 zone_span_writelock(zone);
fa004ab7
WY
752 if (zone_is_empty(zone))
753 init_currently_empty_zone(zone, start_pfn, nr_pages);
f1dd2cd1
MH
754 resize_zone_range(zone, start_pfn, nr_pages);
755 zone_span_writeunlock(zone);
756 resize_pgdat_range(pgdat, start_pfn, nr_pages);
757 pgdat_resize_unlock(pgdat, &flags);
758
759 /*
760 * TODO now we have a visible range of pages which are not associated
761 * with their zone properly. Not nice but set_pfnblock_flags_mask
762 * expects the zone spans the pfn range. All the pages in the range
763 * are reserved so nobody should be touching them so we should be safe
764 */
a99583e7
CH
765 memmap_init_zone(nr_pages, nid, zone_idx(zone), start_pfn,
766 MEMMAP_HOTPLUG, altmap);
f1dd2cd1
MH
767
768 set_zone_contiguous(zone);
769}
770
c246a213
MH
771/*
772 * Returns a default kernel memory zone for the given pfn range.
773 * If no kernel zone covers this pfn range it will automatically go
774 * to the ZONE_NORMAL.
775 */
c6f03e29 776static struct zone *default_kernel_zone_for_pfn(int nid, unsigned long start_pfn,
c246a213
MH
777 unsigned long nr_pages)
778{
779 struct pglist_data *pgdat = NODE_DATA(nid);
780 int zid;
781
782 for (zid = 0; zid <= ZONE_NORMAL; zid++) {
783 struct zone *zone = &pgdat->node_zones[zid];
784
785 if (zone_intersects(zone, start_pfn, nr_pages))
786 return zone;
787 }
788
789 return &pgdat->node_zones[ZONE_NORMAL];
790}
791
c6f03e29
MH
792static inline struct zone *default_zone_for_pfn(int nid, unsigned long start_pfn,
793 unsigned long nr_pages)
e5e68930 794{
c6f03e29
MH
795 struct zone *kernel_zone = default_kernel_zone_for_pfn(nid, start_pfn,
796 nr_pages);
797 struct zone *movable_zone = &NODE_DATA(nid)->node_zones[ZONE_MOVABLE];
798 bool in_kernel = zone_intersects(kernel_zone, start_pfn, nr_pages);
799 bool in_movable = zone_intersects(movable_zone, start_pfn, nr_pages);
e5e68930
MH
800
801 /*
c6f03e29
MH
802 * We inherit the existing zone in a simple case where zones do not
803 * overlap in the given range
e5e68930 804 */
c6f03e29
MH
805 if (in_kernel ^ in_movable)
806 return (in_kernel) ? kernel_zone : movable_zone;
9f123ab5 807
c6f03e29
MH
808 /*
809 * If the range doesn't belong to any zone or two zones overlap in the
810 * given range then we use movable zone only if movable_node is
811 * enabled because we always online to a kernel zone by default.
812 */
813 return movable_node_enabled ? movable_zone : kernel_zone;
9f123ab5
MH
814}
815
e5e68930
MH
816struct zone * zone_for_pfn_range(int online_type, int nid, unsigned start_pfn,
817 unsigned long nr_pages)
f1dd2cd1 818{
c6f03e29
MH
819 if (online_type == MMOP_ONLINE_KERNEL)
820 return default_kernel_zone_for_pfn(nid, start_pfn, nr_pages);
f1dd2cd1 821
c6f03e29
MH
822 if (online_type == MMOP_ONLINE_MOVABLE)
823 return &NODE_DATA(nid)->node_zones[ZONE_MOVABLE];
df429ac0 824
c6f03e29 825 return default_zone_for_pfn(nid, start_pfn, nr_pages);
e5e68930
MH
826}
827
828/*
829 * Associates the given pfn range with the given node and the zone appropriate
830 * for the given online type.
831 */
832static struct zone * __meminit move_pfn_range(int online_type, int nid,
833 unsigned long start_pfn, unsigned long nr_pages)
834{
835 struct zone *zone;
836
837 zone = zone_for_pfn_range(online_type, nid, start_pfn, nr_pages);
a99583e7 838 move_pfn_range_to_zone(zone, start_pfn, nr_pages, NULL);
f1dd2cd1 839 return zone;
df429ac0 840}
75884fb1 841
511c2aba 842int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
75884fb1 843{
aa47228a 844 unsigned long flags;
3947be19
DH
845 unsigned long onlined_pages = 0;
846 struct zone *zone;
6811378e 847 int need_zonelists_rebuild = 0;
7b78d335
YG
848 int nid;
849 int ret;
850 struct memory_notify arg;
d0dc12e8
PT
851 struct memory_block *mem;
852
381eab4a
DH
853 mem_hotplug_begin();
854
d0dc12e8
PT
855 /*
856 * We can't use pfn_to_nid() because nid might be stored in struct page
857 * which is not yet initialized. Instead, we find nid from memory block.
858 */
859 mem = find_memory_block(__pfn_to_section(pfn));
860 nid = mem->nid;
89c02e69 861 put_device(&mem->dev);
7b78d335 862
f1dd2cd1
MH
863 /* associate pfn range with the zone */
864 zone = move_pfn_range(online_type, nid, pfn, nr_pages);
865
7b78d335
YG
866 arg.start_pfn = pfn;
867 arg.nr_pages = nr_pages;
d9713679 868 node_states_check_changes_online(nr_pages, zone, &arg);
7b78d335 869
7b78d335
YG
870 ret = memory_notify(MEM_GOING_ONLINE, &arg);
871 ret = notifier_to_errno(ret);
e33e33b4
CY
872 if (ret)
873 goto failed_addition;
874
6811378e
YG
875 /*
876 * If this zone is not populated, then it is not in zonelist.
877 * This means the page allocator ignores this zone.
878 * So, zonelist must be updated after online.
879 */
6dcd73d7 880 if (!populated_zone(zone)) {
6811378e 881 need_zonelists_rebuild = 1;
72675e13 882 setup_zone_pageset(zone);
6dcd73d7 883 }
6811378e 884
908eedc6 885 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
75884fb1 886 online_pages_range);
fd8a4221 887 if (ret) {
6dcd73d7
WC
888 if (need_zonelists_rebuild)
889 zone_pcp_reset(zone);
e33e33b4 890 goto failed_addition;
fd8a4221
GL
891 }
892
3947be19 893 zone->present_pages += onlined_pages;
aa47228a
CS
894
895 pgdat_resize_lock(zone->zone_pgdat, &flags);
f2937be5 896 zone->zone_pgdat->node_present_pages += onlined_pages;
aa47228a
CS
897 pgdat_resize_unlock(zone->zone_pgdat, &flags);
898
08dff7b7 899 if (onlined_pages) {
e888ca35 900 node_states_set_node(nid, &arg);
08dff7b7 901 if (need_zonelists_rebuild)
72675e13 902 build_all_zonelists(NULL);
08dff7b7
JL
903 else
904 zone_pcp_update(zone);
905 }
3947be19 906
1b79acc9
KM
907 init_per_zone_wmark_min();
908
698b1b30 909 if (onlined_pages) {
e888ca35 910 kswapd_run(nid);
698b1b30
VB
911 kcompactd_run(nid);
912 }
61b13993 913
1f522509 914 vm_total_pages = nr_free_pagecache_pages();
2f7f24ec 915
2d1d43f6 916 writeback_set_ratelimit();
7b78d335
YG
917
918 if (onlined_pages)
919 memory_notify(MEM_ONLINE, &arg);
381eab4a 920 mem_hotplug_done();
30467e0b 921 return 0;
e33e33b4
CY
922
923failed_addition:
924 pr_debug("online_pages [mem %#010llx-%#010llx] failed\n",
925 (unsigned long long) pfn << PAGE_SHIFT,
926 (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1);
927 memory_notify(MEM_CANCEL_ONLINE, &arg);
381eab4a 928 mem_hotplug_done();
e33e33b4 929 return ret;
3947be19 930}
53947027 931#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
bc02af93 932
0bd85420
TC
933static void reset_node_present_pages(pg_data_t *pgdat)
934{
935 struct zone *z;
936
937 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
938 z->present_pages = 0;
939
940 pgdat->node_present_pages = 0;
941}
942
e1319331
HS
943/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
944static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
9af3c2de
YG
945{
946 struct pglist_data *pgdat;
c8e861a5 947 unsigned long start_pfn = PFN_DOWN(start);
9af3c2de 948
a1e565aa
TC
949 pgdat = NODE_DATA(nid);
950 if (!pgdat) {
951 pgdat = arch_alloc_nodedata(nid);
952 if (!pgdat)
953 return NULL;
9af3c2de 954
a1e565aa 955 arch_refresh_nodedata(nid, pgdat);
b0dc3a34 956 } else {
e716f2eb
MG
957 /*
958 * Reset the nr_zones, order and classzone_idx before reuse.
959 * Note that kswapd will init kswapd_classzone_idx properly
960 * when it starts in the near future.
961 */
b0dc3a34 962 pgdat->nr_zones = 0;
38087d9b
MG
963 pgdat->kswapd_order = 0;
964 pgdat->kswapd_classzone_idx = 0;
a1e565aa 965 }
9af3c2de
YG
966
967 /* we can use NODE_DATA(nid) from here */
968
03e85f9d
OS
969 pgdat->node_id = nid;
970 pgdat->node_start_pfn = start_pfn;
971
9af3c2de 972 /* init node's zones as empty zones, we don't have any present pages.*/
03e85f9d 973 free_area_init_core_hotplug(nid);
5830169f 974 pgdat->per_cpu_nodestats = alloc_percpu(struct per_cpu_nodestat);
9af3c2de 975
959ecc48
KH
976 /*
977 * The node we allocated has no zone fallback lists. For avoiding
978 * to access not-initialized zonelist, build here.
979 */
72675e13 980 build_all_zonelists(pgdat);
959ecc48 981
0bd85420
TC
982 /*
983 * When memory is hot-added, all the memory is in offline state. So
984 * clear all zones' present_pages because they will be updated in
985 * online_pages() and offline_pages().
986 */
03e85f9d 987 reset_node_managed_pages(pgdat);
0bd85420
TC
988 reset_node_present_pages(pgdat);
989
9af3c2de
YG
990 return pgdat;
991}
992
b9ff0360 993static void rollback_node_hotadd(int nid)
9af3c2de 994{
b9ff0360
OS
995 pg_data_t *pgdat = NODE_DATA(nid);
996
9af3c2de 997 arch_refresh_nodedata(nid, NULL);
5830169f 998 free_percpu(pgdat->per_cpu_nodestats);
9af3c2de
YG
999 arch_free_nodedata(pgdat);
1000 return;
1001}
1002
0a547039 1003
01b0f197
TK
1004/**
1005 * try_online_node - online a node if offlined
e8b098fc 1006 * @nid: the node ID
b9ff0360
OS
1007 * @start: start addr of the node
1008 * @set_node_online: Whether we want to online the node
cf23422b 1009 * called by cpu_up() to online a node without onlined memory.
b9ff0360
OS
1010 *
1011 * Returns:
1012 * 1 -> a new node has been allocated
1013 * 0 -> the node is already online
1014 * -ENOMEM -> the node could not be allocated
cf23422b 1015 */
b9ff0360 1016static int __try_online_node(int nid, u64 start, bool set_node_online)
cf23422b 1017{
b9ff0360
OS
1018 pg_data_t *pgdat;
1019 int ret = 1;
cf23422b 1020
01b0f197
TK
1021 if (node_online(nid))
1022 return 0;
1023
b9ff0360 1024 pgdat = hotadd_new_pgdat(nid, start);
7553e8f2 1025 if (!pgdat) {
01b0f197 1026 pr_err("Cannot online node %d due to NULL pgdat\n", nid);
cf23422b 1027 ret = -ENOMEM;
1028 goto out;
1029 }
b9ff0360
OS
1030
1031 if (set_node_online) {
1032 node_set_online(nid);
1033 ret = register_one_node(nid);
1034 BUG_ON(ret);
1035 }
cf23422b 1036out:
b9ff0360
OS
1037 return ret;
1038}
1039
1040/*
1041 * Users of this function always want to online/register the node
1042 */
1043int try_online_node(int nid)
1044{
1045 int ret;
1046
1047 mem_hotplug_begin();
1048 ret = __try_online_node(nid, 0, true);
bfc8c901 1049 mem_hotplug_done();
cf23422b 1050 return ret;
1051}
1052
27356f54
TK
1053static int check_hotplug_memory_range(u64 start, u64 size)
1054{
ba325585
PT
1055 unsigned long block_sz = memory_block_size_bytes();
1056 u64 block_nr_pages = block_sz >> PAGE_SHIFT;
27356f54 1057 u64 nr_pages = size >> PAGE_SHIFT;
ba325585 1058 u64 start_pfn = PFN_DOWN(start);
27356f54 1059
ba325585
PT
1060 /* memory range must be block size aligned */
1061 if (!nr_pages || !IS_ALIGNED(start_pfn, block_nr_pages) ||
1062 !IS_ALIGNED(nr_pages, block_nr_pages)) {
1063 pr_err("Block size [%#lx] unaligned hotplug range: start %#llx, size %#llx",
1064 block_sz, start, size);
27356f54
TK
1065 return -EINVAL;
1066 }
1067
1068 return 0;
1069}
1070
31bc3858
VK
1071static int online_memory_block(struct memory_block *mem, void *arg)
1072{
dc18d706 1073 return device_online(&mem->dev);
31bc3858
VK
1074}
1075
8df1d0e4
DH
1076/*
1077 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1078 * and online/offline operations (triggered e.g. by sysfs).
1079 *
1080 * we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG
1081 */
f29d8e9c 1082int __ref add_memory_resource(int nid, struct resource *res)
bc02af93 1083{
940519f0
MH
1084 struct mhp_restrictions restrictions = {
1085 .flags = MHP_MEMBLOCK_API,
1086 };
62cedb9f 1087 u64 start, size;
b9ff0360 1088 bool new_node = false;
bc02af93
YG
1089 int ret;
1090
62cedb9f
DV
1091 start = res->start;
1092 size = resource_size(res);
1093
27356f54
TK
1094 ret = check_hotplug_memory_range(start, size);
1095 if (ret)
1096 return ret;
1097
bfc8c901 1098 mem_hotplug_begin();
ac13c462 1099
7f36e3e5
TC
1100 /*
1101 * Add new range to memblock so that when hotadd_new_pgdat() is called
1102 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
1103 * this new range and calculate total pages correctly. The range will
1104 * be removed at hot-remove time.
1105 */
1106 memblock_add_node(start, size, nid);
1107
b9ff0360
OS
1108 ret = __try_online_node(nid, start, false);
1109 if (ret < 0)
1110 goto error;
1111 new_node = ret;
9af3c2de 1112
bc02af93 1113 /* call arch's memory hotadd */
940519f0 1114 ret = arch_add_memory(nid, start, size, &restrictions);
9af3c2de
YG
1115 if (ret < 0)
1116 goto error;
1117
a1e565aa 1118 if (new_node) {
d5b6f6a3 1119 /* If sysfs file of new node can't be created, cpu on the node
0fc44159
YG
1120 * can't be hot-added. There is no rollback way now.
1121 * So, check by BUG_ON() to catch it reluctantly..
d5b6f6a3 1122 * We online node here. We can't roll back from here.
0fc44159 1123 */
d5b6f6a3
OS
1124 node_set_online(nid);
1125 ret = __register_one_node(nid);
0fc44159
YG
1126 BUG_ON(ret);
1127 }
1128
d5b6f6a3 1129 /* link memory sections under this node.*/
4fbce633 1130 ret = link_mem_sections(nid, PFN_DOWN(start), PFN_UP(start + size - 1));
d5b6f6a3
OS
1131 BUG_ON(ret);
1132
d96ae530
AM
1133 /* create new memmap entry */
1134 firmware_map_add_hotplug(start, start + size, "System RAM");
1135
381eab4a
DH
1136 /* device_online() will take the lock when calling online_pages() */
1137 mem_hotplug_done();
1138
31bc3858 1139 /* online pages if requested */
f29d8e9c 1140 if (memhp_auto_online)
31bc3858
VK
1141 walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1),
1142 NULL, online_memory_block);
1143
381eab4a 1144 return ret;
9af3c2de
YG
1145error:
1146 /* rollback pgdat allocation and others */
b9ff0360
OS
1147 if (new_node)
1148 rollback_node_hotadd(nid);
7f36e3e5 1149 memblock_remove(start, size);
bfc8c901 1150 mem_hotplug_done();
bc02af93
YG
1151 return ret;
1152}
62cedb9f 1153
8df1d0e4
DH
1154/* requires device_hotplug_lock, see add_memory_resource() */
1155int __ref __add_memory(int nid, u64 start, u64 size)
62cedb9f
DV
1156{
1157 struct resource *res;
1158 int ret;
1159
1160 res = register_memory_resource(start, size);
6f754ba4
VK
1161 if (IS_ERR(res))
1162 return PTR_ERR(res);
62cedb9f 1163
f29d8e9c 1164 ret = add_memory_resource(nid, res);
62cedb9f
DV
1165 if (ret < 0)
1166 release_memory_resource(res);
1167 return ret;
1168}
8df1d0e4
DH
1169
1170int add_memory(int nid, u64 start, u64 size)
1171{
1172 int rc;
1173
1174 lock_device_hotplug();
1175 rc = __add_memory(nid, start, size);
1176 unlock_device_hotplug();
1177
1178 return rc;
1179}
bc02af93 1180EXPORT_SYMBOL_GPL(add_memory);
0c0e6195
KH
1181
1182#ifdef CONFIG_MEMORY_HOTREMOVE
5c755e9f
BP
1183/*
1184 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1185 * set and the size of the free page is given by page_order(). Using this,
1186 * the function determines if the pageblock contains only free pages.
1187 * Due to buddy contraints, a free page at least the size of a pageblock will
1188 * be located at the start of the pageblock
1189 */
1190static inline int pageblock_free(struct page *page)
1191{
1192 return PageBuddy(page) && page_order(page) >= pageblock_order;
1193}
1194
891cb2a7
MH
1195/* Return the pfn of the start of the next active pageblock after a given pfn */
1196static unsigned long next_active_pageblock(unsigned long pfn)
5c755e9f 1197{
891cb2a7
MH
1198 struct page *page = pfn_to_page(pfn);
1199
5c755e9f 1200 /* Ensure the starting page is pageblock-aligned */
891cb2a7 1201 BUG_ON(pfn & (pageblock_nr_pages - 1));
5c755e9f 1202
5c755e9f 1203 /* If the entire pageblock is free, move to the end of free page */
0dcc48c1
KH
1204 if (pageblock_free(page)) {
1205 int order;
1206 /* be careful. we don't have locks, page_order can be changed.*/
1207 order = page_order(page);
1208 if ((order < MAX_ORDER) && (order >= pageblock_order))
891cb2a7 1209 return pfn + (1 << order);
0dcc48c1 1210 }
5c755e9f 1211
891cb2a7 1212 return pfn + pageblock_nr_pages;
5c755e9f
BP
1213}
1214
891cb2a7 1215static bool is_pageblock_removable_nolock(unsigned long pfn)
fb52bbae 1216{
891cb2a7 1217 struct page *page = pfn_to_page(pfn);
fb52bbae 1218 struct zone *zone;
fb52bbae
MM
1219
1220 /*
1221 * We have to be careful here because we are iterating over memory
1222 * sections which are not zone aware so we might end up outside of
1223 * the zone but still within the section.
1224 * We have to take care about the node as well. If the node is offline
1225 * its NODE_DATA will be NULL - see page_zone.
1226 */
1227 if (!node_online(page_to_nid(page)))
1228 return false;
1229
1230 zone = page_zone(page);
1231 pfn = page_to_pfn(page);
1232 if (!zone_spans_pfn(zone, pfn))
1233 return false;
1234
d381c547 1235 return !has_unmovable_pages(zone, page, 0, MIGRATE_MOVABLE, SKIP_HWPOISON);
fb52bbae
MM
1236}
1237
5c755e9f 1238/* Checks if this range of memory is likely to be hot-removable. */
c98940f6 1239bool is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
5c755e9f 1240{
891cb2a7
MH
1241 unsigned long end_pfn, pfn;
1242
1243 end_pfn = min(start_pfn + nr_pages,
1244 zone_end_pfn(page_zone(pfn_to_page(start_pfn))));
5c755e9f
BP
1245
1246 /* Check the starting page of each pageblock within the range */
891cb2a7
MH
1247 for (pfn = start_pfn; pfn < end_pfn; pfn = next_active_pageblock(pfn)) {
1248 if (!is_pageblock_removable_nolock(pfn))
c98940f6 1249 return false;
49ac8255 1250 cond_resched();
5c755e9f
BP
1251 }
1252
1253 /* All pageblocks in the memory block are likely to be hot-removable */
c98940f6 1254 return true;
5c755e9f
BP
1255}
1256
0c0e6195 1257/*
deb88a2a 1258 * Confirm all pages in a range [start, end) belong to the same zone.
a96dfddb 1259 * When true, return its valid [start, end).
0c0e6195 1260 */
a96dfddb
TK
1261int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn,
1262 unsigned long *valid_start, unsigned long *valid_end)
0c0e6195 1263{
5f0f2887 1264 unsigned long pfn, sec_end_pfn;
a96dfddb 1265 unsigned long start, end;
0c0e6195
KH
1266 struct zone *zone = NULL;
1267 struct page *page;
1268 int i;
deb88a2a 1269 for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn + 1);
0c0e6195 1270 pfn < end_pfn;
deb88a2a 1271 pfn = sec_end_pfn, sec_end_pfn += PAGES_PER_SECTION) {
5f0f2887
AB
1272 /* Make sure the memory section is present first */
1273 if (!present_section_nr(pfn_to_section_nr(pfn)))
0c0e6195 1274 continue;
5f0f2887
AB
1275 for (; pfn < sec_end_pfn && pfn < end_pfn;
1276 pfn += MAX_ORDER_NR_PAGES) {
1277 i = 0;
1278 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1279 while ((i < MAX_ORDER_NR_PAGES) &&
1280 !pfn_valid_within(pfn + i))
1281 i++;
d6d8c8a4 1282 if (i == MAX_ORDER_NR_PAGES || pfn + i >= end_pfn)
5f0f2887 1283 continue;
24feb47c
MZ
1284 /* Check if we got outside of the zone */
1285 if (zone && !zone_spans_pfn(zone, pfn + i))
1286 return 0;
5f0f2887
AB
1287 page = pfn_to_page(pfn + i);
1288 if (zone && page_zone(page) != zone)
1289 return 0;
a96dfddb
TK
1290 if (!zone)
1291 start = pfn + i;
5f0f2887 1292 zone = page_zone(page);
a96dfddb 1293 end = pfn + MAX_ORDER_NR_PAGES;
5f0f2887 1294 }
0c0e6195 1295 }
deb88a2a 1296
a96dfddb
TK
1297 if (zone) {
1298 *valid_start = start;
d6d8c8a4 1299 *valid_end = min(end, end_pfn);
deb88a2a 1300 return 1;
a96dfddb 1301 } else {
deb88a2a 1302 return 0;
a96dfddb 1303 }
0c0e6195
KH
1304}
1305
1306/*
0efadf48
YX
1307 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages,
1308 * non-lru movable pages and hugepages). We scan pfn because it's much
1309 * easier than scanning over linked list. This function returns the pfn
1310 * of the first found movable page if it's found, otherwise 0.
0c0e6195 1311 */
c8721bbb 1312static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
0c0e6195
KH
1313{
1314 unsigned long pfn;
eeb0efd0 1315
0c0e6195 1316 for (pfn = start; pfn < end; pfn++) {
eeb0efd0
OS
1317 struct page *page, *head;
1318 unsigned long skip;
1319
1320 if (!pfn_valid(pfn))
1321 continue;
1322 page = pfn_to_page(pfn);
1323 if (PageLRU(page))
1324 return pfn;
1325 if (__PageMovable(page))
1326 return pfn;
1327
1328 if (!PageHuge(page))
1329 continue;
1330 head = compound_head(page);
39186cbe 1331 if (page_huge_active(head))
eeb0efd0
OS
1332 return pfn;
1333 skip = (1 << compound_order(head)) - (page - head);
1334 pfn += skip - 1;
0c0e6195
KH
1335 }
1336 return 0;
1337}
1338
666feb21 1339static struct page *new_node_page(struct page *page, unsigned long private)
394e31d2 1340{
394e31d2 1341 int nid = page_to_nid(page);
231e97e2 1342 nodemask_t nmask = node_states[N_MEMORY];
7f252f27
MH
1343
1344 /*
1345 * try to allocate from a different node but reuse this node if there
1346 * are no other online nodes to be used (e.g. we are offlining a part
1347 * of the only existing node)
1348 */
1349 node_clear(nid, nmask);
1350 if (nodes_empty(nmask))
1351 node_set(nid, nmask);
394e31d2 1352
8b913238 1353 return new_page_nodemask(page, nid, &nmask);
394e31d2
XQ
1354}
1355
0c0e6195
KH
1356static int
1357do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1358{
1359 unsigned long pfn;
1360 struct page *page;
0c0e6195
KH
1361 int ret = 0;
1362 LIST_HEAD(source);
1363
a85009c3 1364 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
0c0e6195
KH
1365 if (!pfn_valid(pfn))
1366 continue;
1367 page = pfn_to_page(pfn);
c8721bbb
NH
1368
1369 if (PageHuge(page)) {
1370 struct page *head = compound_head(page);
daf3538a
OS
1371 pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
1372 isolate_huge_page(head, &source);
c8721bbb 1373 continue;
94723aaf 1374 } else if (PageTransHuge(page))
8135d892
NH
1375 pfn = page_to_pfn(compound_head(page))
1376 + hpage_nr_pages(page) - 1;
c8721bbb 1377
b15c8726
MH
1378 /*
1379 * HWPoison pages have elevated reference counts so the migration would
1380 * fail on them. It also doesn't make any sense to migrate them in the
1381 * first place. Still try to unmap such a page in case it is still mapped
1382 * (e.g. current hwpoison implementation doesn't unmap KSM pages but keep
1383 * the unmap as the catch all safety net).
1384 */
1385 if (PageHWPoison(page)) {
1386 if (WARN_ON(PageLRU(page)))
1387 isolate_lru_page(page);
1388 if (page_mapped(page))
1389 try_to_unmap(page, TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS);
1390 continue;
1391 }
1392
700c2a46 1393 if (!get_page_unless_zero(page))
0c0e6195
KH
1394 continue;
1395 /*
0efadf48
YX
1396 * We can skip free pages. And we can deal with pages on
1397 * LRU and non-lru movable pages.
0c0e6195 1398 */
0efadf48
YX
1399 if (PageLRU(page))
1400 ret = isolate_lru_page(page);
1401 else
1402 ret = isolate_movable_page(page, ISOLATE_UNEVICTABLE);
0c0e6195 1403 if (!ret) { /* Success */
62695a84 1404 list_add_tail(&page->lru, &source);
0efadf48
YX
1405 if (!__PageMovable(page))
1406 inc_node_page_state(page, NR_ISOLATED_ANON +
1407 page_is_file_cache(page));
6d9c285a 1408
0c0e6195 1409 } else {
2932c8b0 1410 pr_warn("failed to isolate pfn %lx\n", pfn);
0efadf48 1411 dump_page(page, "isolation failed");
0c0e6195 1412 }
1723058e 1413 put_page(page);
0c0e6195 1414 }
f3ab2636 1415 if (!list_empty(&source)) {
394e31d2
XQ
1416 /* Allocate a new page from the nearest neighbor node */
1417 ret = migrate_pages(&source, new_node_page, NULL, 0,
9c620e2b 1418 MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
2932c8b0
MH
1419 if (ret) {
1420 list_for_each_entry(page, &source, lru) {
1421 pr_warn("migrating pfn %lx failed ret:%d ",
1422 page_to_pfn(page), ret);
1423 dump_page(page, "migration failure");
1424 }
c8721bbb 1425 putback_movable_pages(&source);
2932c8b0 1426 }
0c0e6195 1427 }
1723058e 1428
0c0e6195
KH
1429 return ret;
1430}
1431
1432/*
1433 * remove from free_area[] and mark all as Reserved.
1434 */
1435static int
1436offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1437 void *data)
1438{
5557c766 1439 unsigned long *offlined_pages = (unsigned long *)data;
0c0e6195 1440
5557c766
MH
1441 *offlined_pages += __offline_isolated_pages(start, start + nr_pages);
1442 return 0;
0c0e6195
KH
1443}
1444
1445/*
1446 * Check all pages in range, recoreded as memory resource, are isolated.
1447 */
1448static int
1449check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1450 void *data)
1451{
5557c766 1452 return test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
0c0e6195
KH
1453}
1454
c5320926
TC
1455static int __init cmdline_parse_movable_node(char *p)
1456{
4932381e 1457#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
55ac590c 1458 movable_node_enabled = true;
4932381e
MH
1459#else
1460 pr_warn("movable_node parameter depends on CONFIG_HAVE_MEMBLOCK_NODE_MAP to work properly\n");
1461#endif
c5320926
TC
1462 return 0;
1463}
1464early_param("movable_node", cmdline_parse_movable_node);
1465
d9713679
LJ
1466/* check which state of node_states will be changed when offline memory */
1467static void node_states_check_changes_offline(unsigned long nr_pages,
1468 struct zone *zone, struct memory_notify *arg)
1469{
1470 struct pglist_data *pgdat = zone->zone_pgdat;
1471 unsigned long present_pages = 0;
86b27bea 1472 enum zone_type zt;
d9713679 1473
98fa15f3
AK
1474 arg->status_change_nid = NUMA_NO_NODE;
1475 arg->status_change_nid_normal = NUMA_NO_NODE;
1476 arg->status_change_nid_high = NUMA_NO_NODE;
d9713679
LJ
1477
1478 /*
86b27bea
OS
1479 * Check whether node_states[N_NORMAL_MEMORY] will be changed.
1480 * If the memory to be offline is within the range
1481 * [0..ZONE_NORMAL], and it is the last present memory there,
1482 * the zones in that range will become empty after the offlining,
1483 * thus we can determine that we need to clear the node from
1484 * node_states[N_NORMAL_MEMORY].
d9713679 1485 */
86b27bea 1486 for (zt = 0; zt <= ZONE_NORMAL; zt++)
d9713679 1487 present_pages += pgdat->node_zones[zt].present_pages;
86b27bea 1488 if (zone_idx(zone) <= ZONE_NORMAL && nr_pages >= present_pages)
d9713679 1489 arg->status_change_nid_normal = zone_to_nid(zone);
d9713679 1490
6715ddf9
LJ
1491#ifdef CONFIG_HIGHMEM
1492 /*
86b27bea
OS
1493 * node_states[N_HIGH_MEMORY] contains nodes which
1494 * have normal memory or high memory.
1495 * Here we add the present_pages belonging to ZONE_HIGHMEM.
1496 * If the zone is within the range of [0..ZONE_HIGHMEM), and
1497 * we determine that the zones in that range become empty,
1498 * we need to clear the node for N_HIGH_MEMORY.
6715ddf9 1499 */
86b27bea
OS
1500 present_pages += pgdat->node_zones[ZONE_HIGHMEM].present_pages;
1501 if (zone_idx(zone) <= ZONE_HIGHMEM && nr_pages >= present_pages)
6715ddf9 1502 arg->status_change_nid_high = zone_to_nid(zone);
6715ddf9
LJ
1503#endif
1504
d9713679 1505 /*
86b27bea
OS
1506 * We have accounted the pages from [0..ZONE_NORMAL), and
1507 * in case of CONFIG_HIGHMEM the pages from ZONE_HIGHMEM
1508 * as well.
1509 * Here we count the possible pages from ZONE_MOVABLE.
1510 * If after having accounted all the pages, we see that the nr_pages
1511 * to be offlined is over or equal to the accounted pages,
1512 * we know that the node will become empty, and so, we can clear
1513 * it for N_MEMORY as well.
d9713679 1514 */
86b27bea 1515 present_pages += pgdat->node_zones[ZONE_MOVABLE].present_pages;
d9713679 1516
d9713679
LJ
1517 if (nr_pages >= present_pages)
1518 arg->status_change_nid = zone_to_nid(zone);
d9713679
LJ
1519}
1520
1521static void node_states_clear_node(int node, struct memory_notify *arg)
1522{
1523 if (arg->status_change_nid_normal >= 0)
1524 node_clear_state(node, N_NORMAL_MEMORY);
1525
cf01f6f5 1526 if (arg->status_change_nid_high >= 0)
d9713679 1527 node_clear_state(node, N_HIGH_MEMORY);
6715ddf9 1528
cf01f6f5 1529 if (arg->status_change_nid >= 0)
6715ddf9 1530 node_clear_state(node, N_MEMORY);
d9713679
LJ
1531}
1532
a16cee10 1533static int __ref __offline_pages(unsigned long start_pfn,
ecde0f3e 1534 unsigned long end_pfn)
0c0e6195 1535{
ecde0f3e 1536 unsigned long pfn, nr_pages;
5557c766 1537 unsigned long offlined_pages = 0;
9b7ea46a 1538 int ret, node, nr_isolate_pageblock;
d702909f 1539 unsigned long flags;
a96dfddb 1540 unsigned long valid_start, valid_end;
0c0e6195 1541 struct zone *zone;
7b78d335 1542 struct memory_notify arg;
79605093 1543 char *reason;
0c0e6195 1544
381eab4a
DH
1545 mem_hotplug_begin();
1546
0c0e6195
KH
1547 /* This makes hotplug much easier...and readable.
1548 we assume this for now. .*/
381eab4a
DH
1549 if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start,
1550 &valid_end)) {
79605093
MH
1551 ret = -EINVAL;
1552 reason = "multizone range";
1553 goto failed_removal;
381eab4a 1554 }
7b78d335 1555
a96dfddb 1556 zone = page_zone(pfn_to_page(valid_start));
7b78d335
YG
1557 node = zone_to_nid(zone);
1558 nr_pages = end_pfn - start_pfn;
1559
0c0e6195 1560 /* set above range as isolated */
b023f468 1561 ret = start_isolate_page_range(start_pfn, end_pfn,
d381c547
MH
1562 MIGRATE_MOVABLE,
1563 SKIP_HWPOISON | REPORT_FAILURE);
9b7ea46a 1564 if (ret < 0) {
79605093
MH
1565 reason = "failure to isolate range";
1566 goto failed_removal;
381eab4a 1567 }
9b7ea46a 1568 nr_isolate_pageblock = ret;
7b78d335
YG
1569
1570 arg.start_pfn = start_pfn;
1571 arg.nr_pages = nr_pages;
d9713679 1572 node_states_check_changes_offline(nr_pages, zone, &arg);
7b78d335
YG
1573
1574 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1575 ret = notifier_to_errno(ret);
79605093
MH
1576 if (ret) {
1577 reason = "notifier failure";
1578 goto failed_removal_isolated;
1579 }
7b78d335 1580
bb8965bd
MH
1581 do {
1582 for (pfn = start_pfn; pfn;) {
1583 if (signal_pending(current)) {
1584 ret = -EINTR;
1585 reason = "signal backoff";
1586 goto failed_removal_isolated;
1587 }
72b39cfc 1588
bb8965bd
MH
1589 cond_resched();
1590 lru_add_drain_all();
bb8965bd
MH
1591
1592 pfn = scan_movable_pages(pfn, end_pfn);
1593 if (pfn) {
1594 /*
1595 * TODO: fatal migration failures should bail
1596 * out
1597 */
1598 do_migrate_range(pfn, end_pfn);
1599 }
1600 }
0c0e6195 1601
bb8965bd
MH
1602 /*
1603 * Dissolve free hugepages in the memory block before doing
1604 * offlining actually in order to make hugetlbfs's object
1605 * counting consistent.
1606 */
1607 ret = dissolve_free_huge_pages(start_pfn, end_pfn);
1608 if (ret) {
1609 reason = "failure to dissolve huge pages";
1610 goto failed_removal_isolated;
1611 }
1612 /* check again */
5557c766
MH
1613 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn,
1614 NULL, check_pages_isolated_cb);
1615 } while (ret);
72b39cfc 1616
b3834be5 1617 /* Ok, all of our target is isolated.
0c0e6195 1618 We cannot do rollback at this point. */
5557c766
MH
1619 walk_system_ram_range(start_pfn, end_pfn - start_pfn,
1620 &offlined_pages, offline_isolated_pages_cb);
1621 pr_info("Offlined Pages %ld\n", offlined_pages);
9b7ea46a
QC
1622 /*
1623 * Onlining will reset pagetype flags and makes migrate type
1624 * MOVABLE, so just need to decrease the number of isolated
1625 * pageblocks zone counter here.
1626 */
1627 spin_lock_irqsave(&zone->lock, flags);
1628 zone->nr_isolate_pageblock -= nr_isolate_pageblock;
1629 spin_unlock_irqrestore(&zone->lock, flags);
1630
0c0e6195 1631 /* removal success */
3dcc0571 1632 adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
0c0e6195 1633 zone->present_pages -= offlined_pages;
d702909f
CS
1634
1635 pgdat_resize_lock(zone->zone_pgdat, &flags);
0c0e6195 1636 zone->zone_pgdat->node_present_pages -= offlined_pages;
d702909f 1637 pgdat_resize_unlock(zone->zone_pgdat, &flags);
7b78d335 1638
1b79acc9
KM
1639 init_per_zone_wmark_min();
1640
1e8537ba 1641 if (!populated_zone(zone)) {
340175b7 1642 zone_pcp_reset(zone);
72675e13 1643 build_all_zonelists(NULL);
1e8537ba
XQ
1644 } else
1645 zone_pcp_update(zone);
340175b7 1646
d9713679 1647 node_states_clear_node(node, &arg);
698b1b30 1648 if (arg.status_change_nid >= 0) {
8fe23e05 1649 kswapd_stop(node);
698b1b30
VB
1650 kcompactd_stop(node);
1651 }
bce7394a 1652
0c0e6195
KH
1653 vm_total_pages = nr_free_pagecache_pages();
1654 writeback_set_ratelimit();
7b78d335
YG
1655
1656 memory_notify(MEM_OFFLINE, &arg);
381eab4a 1657 mem_hotplug_done();
0c0e6195
KH
1658 return 0;
1659
79605093
MH
1660failed_removal_isolated:
1661 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
c4efe484 1662 memory_notify(MEM_CANCEL_OFFLINE, &arg);
0c0e6195 1663failed_removal:
79605093 1664 pr_debug("memory offlining [mem %#010llx-%#010llx] failed due to %s\n",
e33e33b4 1665 (unsigned long long) start_pfn << PAGE_SHIFT,
79605093
MH
1666 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1,
1667 reason);
0c0e6195 1668 /* pushback to free area */
381eab4a 1669 mem_hotplug_done();
0c0e6195
KH
1670 return ret;
1671}
71088785 1672
a16cee10
WC
1673int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1674{
ecde0f3e 1675 return __offline_pages(start_pfn, start_pfn + nr_pages);
a16cee10 1676}
e2ff3940 1677#endif /* CONFIG_MEMORY_HOTREMOVE */
a16cee10 1678
bbc76be6
WC
1679/**
1680 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1681 * @start_pfn: start pfn of the memory range
e05c4bbf 1682 * @end_pfn: end pfn of the memory range
bbc76be6
WC
1683 * @arg: argument passed to func
1684 * @func: callback for each memory section walked
1685 *
1686 * This function walks through all present mem sections in range
1687 * [start_pfn, end_pfn) and call func on each mem section.
1688 *
1689 * Returns the return value of func.
1690 */
e2ff3940 1691int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
bbc76be6 1692 void *arg, int (*func)(struct memory_block *, void *))
71088785 1693{
e90bdb7f
WC
1694 struct memory_block *mem = NULL;
1695 struct mem_section *section;
e90bdb7f
WC
1696 unsigned long pfn, section_nr;
1697 int ret;
e90bdb7f
WC
1698
1699 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1700 section_nr = pfn_to_section_nr(pfn);
1701 if (!present_section_nr(section_nr))
1702 continue;
1703
1704 section = __nr_to_section(section_nr);
1705 /* same memblock? */
1706 if (mem)
1707 if ((section_nr >= mem->start_section_nr) &&
1708 (section_nr <= mem->end_section_nr))
1709 continue;
1710
1711 mem = find_memory_block_hinted(section, mem);
1712 if (!mem)
1713 continue;
1714
bbc76be6 1715 ret = func(mem, arg);
e90bdb7f 1716 if (ret) {
bbc76be6
WC
1717 kobject_put(&mem->dev.kobj);
1718 return ret;
e90bdb7f
WC
1719 }
1720 }
1721
1722 if (mem)
1723 kobject_put(&mem->dev.kobj);
1724
bbc76be6
WC
1725 return 0;
1726}
1727
e2ff3940 1728#ifdef CONFIG_MEMORY_HOTREMOVE
d6de9d53 1729static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
bbc76be6
WC
1730{
1731 int ret = !is_memblock_offlined(mem);
1732
349daa0f
RD
1733 if (unlikely(ret)) {
1734 phys_addr_t beginpa, endpa;
1735
1736 beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
1737 endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
756a025f 1738 pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n",
349daa0f
RD
1739 &beginpa, &endpa);
1740 }
bbc76be6
WC
1741
1742 return ret;
1743}
1744
0f1cfe9d 1745static int check_cpu_on_node(pg_data_t *pgdat)
60a5a19e 1746{
60a5a19e
TC
1747 int cpu;
1748
1749 for_each_present_cpu(cpu) {
1750 if (cpu_to_node(cpu) == pgdat->node_id)
1751 /*
1752 * the cpu on this node isn't removed, and we can't
1753 * offline this node.
1754 */
1755 return -EBUSY;
1756 }
1757
1758 return 0;
1759}
1760
0f1cfe9d
TK
1761/**
1762 * try_offline_node
e8b098fc 1763 * @nid: the node ID
0f1cfe9d
TK
1764 *
1765 * Offline a node if all memory sections and cpus of the node are removed.
1766 *
1767 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1768 * and online/offline operations before this call.
1769 */
90b30cdc 1770void try_offline_node(int nid)
60a5a19e 1771{
d822b86a
WC
1772 pg_data_t *pgdat = NODE_DATA(nid);
1773 unsigned long start_pfn = pgdat->node_start_pfn;
1774 unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
60a5a19e
TC
1775 unsigned long pfn;
1776
1777 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1778 unsigned long section_nr = pfn_to_section_nr(pfn);
1779
1780 if (!present_section_nr(section_nr))
1781 continue;
1782
1783 if (pfn_to_nid(pfn) != nid)
1784 continue;
1785
1786 /*
1787 * some memory sections of this node are not removed, and we
1788 * can't offline node now.
1789 */
1790 return;
1791 }
1792
46a3679b 1793 if (check_cpu_on_node(pgdat))
60a5a19e
TC
1794 return;
1795
1796 /*
1797 * all memory/cpu of this node are removed, we can offline this
1798 * node now.
1799 */
1800 node_set_offline(nid);
1801 unregister_one_node(nid);
1802}
90b30cdc 1803EXPORT_SYMBOL(try_offline_node);
60a5a19e 1804
d9eb1417
DH
1805static void __release_memory_resource(resource_size_t start,
1806 resource_size_t size)
1807{
1808 int ret;
1809
1810 /*
1811 * When removing memory in the same granularity as it was added,
1812 * this function never fails. It might only fail if resources
1813 * have to be adjusted or split. We'll ignore the error, as
1814 * removing of memory cannot fail.
1815 */
1816 ret = release_mem_region_adjustable(&iomem_resource, start, size);
1817 if (ret) {
1818 resource_size_t endres = start + size - 1;
1819
1820 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
1821 &start, &endres, ret);
1822 }
1823}
1824
0f1cfe9d
TK
1825/**
1826 * remove_memory
e8b098fc
MR
1827 * @nid: the node ID
1828 * @start: physical address of the region to remove
1829 * @size: size of the region to remove
0f1cfe9d
TK
1830 *
1831 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1832 * and online/offline operations before this call, as required by
1833 * try_offline_node().
1834 */
d15e5926 1835void __ref __remove_memory(int nid, u64 start, u64 size)
bbc76be6 1836{
242831eb 1837 int ret;
993c1aad 1838
27356f54
TK
1839 BUG_ON(check_hotplug_memory_range(start, size));
1840
bfc8c901 1841 mem_hotplug_begin();
6677e3ea
YI
1842
1843 /*
242831eb
RW
1844 * All memory blocks must be offlined before removing memory. Check
1845 * whether all memory blocks in question are offline and trigger a BUG()
1846 * if this is not the case.
6677e3ea 1847 */
242831eb 1848 ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
d6de9d53 1849 check_memblock_offlined_cb);
bfc8c901 1850 if (ret)
242831eb 1851 BUG();
6677e3ea 1852
46c66c4b
YI
1853 /* remove memmap entry */
1854 firmware_map_remove(start, start + size, "System RAM");
f9126ab9
XQ
1855 memblock_free(start, size);
1856 memblock_remove(start, size);
46c66c4b 1857
2c2a5af6 1858 arch_remove_memory(nid, start, size, NULL);
d9eb1417 1859 __release_memory_resource(start, size);
24d335ca 1860
60a5a19e
TC
1861 try_offline_node(nid);
1862
bfc8c901 1863 mem_hotplug_done();
71088785 1864}
d15e5926
DH
1865
1866void remove_memory(int nid, u64 start, u64 size)
1867{
1868 lock_device_hotplug();
1869 __remove_memory(nid, start, size);
1870 unlock_device_hotplug();
1871}
71088785 1872EXPORT_SYMBOL_GPL(remove_memory);
aba6efc4 1873#endif /* CONFIG_MEMORY_HOTREMOVE */