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