blob: f42a8ef93ec47952b2bc54417086cc8011ff5775 [file] [log] [blame]
Dave Hansen3947be12005-10-29 18:16:54 -07001/*
2 * linux/mm/memory_hotplug.c
3 *
4 * Copyright (C)
5 */
6
Dave Hansen3947be12005-10-29 18:16:54 -07007#include <linux/stddef.h>
8#include <linux/mm.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +01009#include <linux/sched/signal.h>
Dave Hansen3947be12005-10-29 18:16:54 -070010#include <linux/swap.h>
11#include <linux/interrupt.h>
12#include <linux/pagemap.h>
Dave Hansen3947be12005-10-29 18:16:54 -070013#include <linux/compiler.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040014#include <linux/export.h>
Dave Hansen3947be12005-10-29 18:16:54 -070015#include <linux/pagevec.h>
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -070016#include <linux/writeback.h>
Dave Hansen3947be12005-10-29 18:16:54 -070017#include <linux/slab.h>
18#include <linux/sysctl.h>
19#include <linux/cpu.h>
20#include <linux/memory.h>
Dan Williams4b94ffd2016-01-15 16:56:22 -080021#include <linux/memremap.h>
Dave Hansen3947be12005-10-29 18:16:54 -070022#include <linux/memory_hotplug.h>
23#include <linux/highmem.h>
24#include <linux/vmalloc.h>
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -070025#include <linux/ioport.h>
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -070026#include <linux/delay.h>
27#include <linux/migrate.h>
28#include <linux/page-isolation.h>
Badari Pulavarty71088782008-10-18 20:25:58 -070029#include <linux/pfn.h>
Andi Kleen6ad696d2009-11-17 14:06:22 -080030#include <linux/suspend.h>
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -080031#include <linux/mm_inline.h>
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -080032#include <linux/firmware-map.h>
Tang Chen60a5a192013-02-22 16:33:14 -080033#include <linux/stop_machine.h>
Naoya Horiguchic8721bb2013-09-11 14:22:09 -070034#include <linux/hugetlb.h>
Tang Chenc5320922013-11-12 15:08:10 -080035#include <linux/memblock.h>
Tang Chenf784a3f2014-11-13 15:19:39 -080036#include <linux/bootmem.h>
Vlastimil Babka698b1b32016-03-17 14:18:08 -070037#include <linux/compaction.h>
Dave Hansen3947be12005-10-29 18:16:54 -070038
39#include <asm/tlbflush.h>
40
Adrian Bunk1e5ad9a2008-04-28 20:40:08 +030041#include "internal.h"
42
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -070043/*
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
50static void generic_online_page(struct page *page);
51
52static online_page_callback_t online_page_callback = generic_online_page;
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070053static DEFINE_MUTEX(online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -070054
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070055/* The same as the cpu_hotplug lock, but for memory hotplug. */
56static struct {
57 struct task_struct *active_writer;
58 struct mutex lock; /* Synchronizes accesses to refcount, */
59 /*
60 * Also blocks the new readers during
61 * an ongoing mem hotplug operation.
62 */
63 int refcount;
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080064
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070065#ifdef CONFIG_DEBUG_LOCK_ALLOC
66 struct lockdep_map dep_map;
67#endif
68} mem_hotplug = {
69 .active_writer = NULL,
70 .lock = __MUTEX_INITIALIZER(mem_hotplug.lock),
71 .refcount = 0,
72#ifdef CONFIG_DEBUG_LOCK_ALLOC
73 .dep_map = {.name = "mem_hotplug.lock" },
74#endif
75};
76
77/* Lockdep annotations for get/put_online_mems() and mem_hotplug_begin/end() */
78#define memhp_lock_acquire_read() lock_map_acquire_read(&mem_hotplug.dep_map)
79#define memhp_lock_acquire() lock_map_acquire(&mem_hotplug.dep_map)
80#define memhp_lock_release() lock_map_release(&mem_hotplug.dep_map)
81
Michal Hocko49323812017-07-06 15:41:05 -070082bool movable_node_enabled = false;
83
Vitaly Kuznetsov8604d9e2016-05-19 17:13:03 -070084#ifndef CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -070085bool memhp_auto_online;
Vitaly Kuznetsov8604d9e2016-05-19 17:13:03 -070086#else
87bool memhp_auto_online = true;
88#endif
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -070089EXPORT_SYMBOL_GPL(memhp_auto_online);
90
Vitaly Kuznetsov86dd9952016-05-19 17:13:06 -070091static int __init setup_memhp_default_state(char *str)
92{
93 if (!strcmp(str, "online"))
94 memhp_auto_online = true;
95 else if (!strcmp(str, "offline"))
96 memhp_auto_online = false;
97
98 return 1;
99}
100__setup("memhp_default_state=", setup_memhp_default_state);
101
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700102void get_online_mems(void)
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800103{
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700104 might_sleep();
105 if (mem_hotplug.active_writer == current)
106 return;
107 memhp_lock_acquire_read();
108 mutex_lock(&mem_hotplug.lock);
109 mem_hotplug.refcount++;
110 mutex_unlock(&mem_hotplug.lock);
111
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800112}
113
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700114void put_online_mems(void)
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800115{
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700116 if (mem_hotplug.active_writer == current)
117 return;
118 mutex_lock(&mem_hotplug.lock);
119
120 if (WARN_ON(!mem_hotplug.refcount))
121 mem_hotplug.refcount++; /* try to fix things up */
122
123 if (!--mem_hotplug.refcount && unlikely(mem_hotplug.active_writer))
124 wake_up_process(mem_hotplug.active_writer);
125 mutex_unlock(&mem_hotplug.lock);
126 memhp_lock_release();
127
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800128}
129
Heiko Carstens55adc1d2017-03-16 16:40:30 -0700130/* Serializes write accesses to mem_hotplug.active_writer. */
131static DEFINE_MUTEX(memory_add_remove_lock);
132
David Rientjes30467e02015-04-14 15:45:11 -0700133void mem_hotplug_begin(void)
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700134{
Heiko Carstens55adc1d2017-03-16 16:40:30 -0700135 mutex_lock(&memory_add_remove_lock);
Dan Williams3fc21922017-02-24 14:55:48 -0800136
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700137 mem_hotplug.active_writer = current;
138
139 memhp_lock_acquire();
140 for (;;) {
141 mutex_lock(&mem_hotplug.lock);
142 if (likely(!mem_hotplug.refcount))
143 break;
144 __set_current_state(TASK_UNINTERRUPTIBLE);
145 mutex_unlock(&mem_hotplug.lock);
146 schedule();
147 }
148}
149
David Rientjes30467e02015-04-14 15:45:11 -0700150void mem_hotplug_done(void)
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700151{
152 mem_hotplug.active_writer = NULL;
153 mutex_unlock(&mem_hotplug.lock);
154 memhp_lock_release();
Heiko Carstens55adc1d2017-03-16 16:40:30 -0700155 mutex_unlock(&memory_add_remove_lock);
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700156}
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800157
Keith Mannthey45e0b782006-09-30 23:27:09 -0700158/* add this memory to iomem resource */
159static struct resource *register_memory_resource(u64 start, u64 size)
160{
161 struct resource *res;
162 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -0800163 if (!res)
164 return ERR_PTR(-ENOMEM);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700165
166 res->name = "System RAM";
167 res->start = start;
168 res->end = start + size - 1;
Toshi Kani782b8662016-01-26 21:57:24 +0100169 res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
Keith Mannthey45e0b782006-09-30 23:27:09 -0700170 if (request_resource(&iomem_resource, res) < 0) {
Toshi Kani4996eed2013-07-03 15:02:39 -0700171 pr_debug("System RAM resource %pR cannot be added\n", res);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700172 kfree(res);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -0800173 return ERR_PTR(-EEXIST);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700174 }
175 return res;
176}
177
178static void release_memory_resource(struct resource *res)
179{
180 if (!res)
181 return;
182 release_resource(res);
183 kfree(res);
184 return;
185}
186
Keith Mannthey53947022006-09-30 23:27:08 -0700187#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800188void get_page_bootmem(unsigned long info, struct page *page,
189 unsigned long type)
Yasunori Goto04753272008-04-28 02:13:31 -0700190{
Yasuaki Ishimatsuddffe982017-02-22 15:45:13 -0800191 page->freelist = (void *)type;
Yasunori Goto04753272008-04-28 02:13:31 -0700192 SetPagePrivate(page);
193 set_page_private(page, info);
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700194 page_ref_inc(page);
Yasunori Goto04753272008-04-28 02:13:31 -0700195}
196
Jiang Liu170a5a72013-07-03 15:03:17 -0700197void put_page_bootmem(struct page *page)
Yasunori Goto04753272008-04-28 02:13:31 -0700198{
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800199 unsigned long type;
Yasunori Goto04753272008-04-28 02:13:31 -0700200
Yasuaki Ishimatsuddffe982017-02-22 15:45:13 -0800201 type = (unsigned long) page->freelist;
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800202 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
203 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
Yasunori Goto04753272008-04-28 02:13:31 -0700204
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700205 if (page_ref_dec_return(page) == 1) {
Yasuaki Ishimatsuddffe982017-02-22 15:45:13 -0800206 page->freelist = NULL;
Yasunori Goto04753272008-04-28 02:13:31 -0700207 ClearPagePrivate(page);
208 set_page_private(page, 0);
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800209 INIT_LIST_HEAD(&page->lru);
Jiang Liu170a5a72013-07-03 15:03:17 -0700210 free_reserved_page(page);
Yasunori Goto04753272008-04-28 02:13:31 -0700211 }
Yasunori Goto04753272008-04-28 02:13:31 -0700212}
213
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800214#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
215#ifndef CONFIG_SPARSEMEM_VMEMMAP
Adrian Bunkd92bc312008-07-23 21:28:12 -0700216static void register_page_bootmem_info_section(unsigned long start_pfn)
Yasunori Goto04753272008-04-28 02:13:31 -0700217{
218 unsigned long *usemap, mapsize, section_nr, i;
219 struct mem_section *ms;
220 struct page *page, *memmap;
221
Yasunori Goto04753272008-04-28 02:13:31 -0700222 section_nr = pfn_to_section_nr(start_pfn);
223 ms = __nr_to_section(section_nr);
224
225 /* Get section's memmap address */
226 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
227
228 /*
229 * Get page for the memmap's phys address
230 * XXX: need more consideration for sparse_vmemmap...
231 */
232 page = virt_to_page(memmap);
233 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
234 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
235
236 /* remember memmap's page */
237 for (i = 0; i < mapsize; i++, page++)
238 get_page_bootmem(section_nr, page, SECTION_INFO);
239
240 usemap = __nr_to_section(section_nr)->pageblock_flags;
241 page = virt_to_page(usemap);
242
243 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
244
245 for (i = 0; i < mapsize; i++, page++)
Yasunori Gotoaf370fb2008-07-23 21:28:17 -0700246 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
Yasunori Goto04753272008-04-28 02:13:31 -0700247
248}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800249#else /* CONFIG_SPARSEMEM_VMEMMAP */
250static void register_page_bootmem_info_section(unsigned long start_pfn)
251{
252 unsigned long *usemap, mapsize, section_nr, i;
253 struct mem_section *ms;
254 struct page *page, *memmap;
255
256 if (!pfn_valid(start_pfn))
257 return;
258
259 section_nr = pfn_to_section_nr(start_pfn);
260 ms = __nr_to_section(section_nr);
261
262 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
263
264 register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
265
266 usemap = __nr_to_section(section_nr)->pageblock_flags;
267 page = virt_to_page(usemap);
268
269 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
270
271 for (i = 0; i < mapsize; i++, page++)
272 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
273}
274#endif /* !CONFIG_SPARSEMEM_VMEMMAP */
Yasunori Goto04753272008-04-28 02:13:31 -0700275
Linus Torvalds7ded3842016-05-27 15:23:32 -0700276void __init register_page_bootmem_info_node(struct pglist_data *pgdat)
Yasunori Goto04753272008-04-28 02:13:31 -0700277{
278 unsigned long i, pfn, end_pfn, nr_pages;
279 int node = pgdat->node_id;
280 struct page *page;
Yasunori Goto04753272008-04-28 02:13:31 -0700281
282 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
283 page = virt_to_page(pgdat);
284
285 for (i = 0; i < nr_pages; i++, page++)
286 get_page_bootmem(node, page, NODE_INFO);
287
Yasunori Goto04753272008-04-28 02:13:31 -0700288 pfn = pgdat->node_start_pfn;
Cody P Schaferc1f19492013-02-22 16:35:32 -0800289 end_pfn = pgdat_end_pfn(pgdat);
Yasunori Goto04753272008-04-28 02:13:31 -0700290
Tang Chen7e9f5eb2013-07-08 16:00:23 -0700291 /* register section info */
qiuxishif14851a2012-09-17 14:09:24 -0700292 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
293 /*
294 * Some platforms can assign the same pfn to multiple nodes - on
295 * node0 as well as nodeN. To avoid registering a pfn against
296 * multiple nodes we check that this pfn does not already
Tang Chen7e9f5eb2013-07-08 16:00:23 -0700297 * reside in some other nodes.
qiuxishif14851a2012-09-17 14:09:24 -0700298 */
Yang Shif65e91df2016-05-27 14:27:32 -0700299 if (pfn_valid(pfn) && (early_pfn_to_nid(pfn) == node))
qiuxishif14851a2012-09-17 14:09:24 -0700300 register_page_bootmem_info_section(pfn);
301 }
Yasunori Goto04753272008-04-28 02:13:31 -0700302}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800303#endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
Yasunori Goto04753272008-04-28 02:13:31 -0700304
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700305static int __meminit __add_section(int nid, unsigned long phys_start_pfn,
306 bool want_memblock)
Dave Hansen3947be12005-10-29 18:16:54 -0700307{
Dave Hansen3947be12005-10-29 18:16:54 -0700308 int ret;
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700309 int i;
Dave Hansen3947be12005-10-29 18:16:54 -0700310
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700311 if (pfn_valid(phys_start_pfn))
312 return -EEXIST;
313
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700314 ret = sparse_add_one_section(NODE_DATA(nid), phys_start_pfn);
Dave Hansen3947be12005-10-29 18:16:54 -0700315 if (ret < 0)
316 return ret;
317
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700318 /*
319 * Make all the pages reserved so that nobody will stumble over half
320 * initialized state.
321 * FIXME: We also have to associate it with a node because pfn_to_node
322 * relies on having page with the proper node.
323 */
324 for (i = 0; i < PAGES_PER_SECTION; i++) {
325 unsigned long pfn = phys_start_pfn + i;
326 struct page *page;
327 if (!pfn_valid(pfn))
328 continue;
Yasunori Goto718127c2006-06-23 02:03:10 -0700329
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700330 page = pfn_to_page(pfn);
331 set_page_node(page, nid);
332 SetPageReserved(page);
333 }
Yasunori Goto718127c2006-06-23 02:03:10 -0700334
Michal Hocko1b862ae2017-07-06 15:37:45 -0700335 if (!want_memblock)
336 return 0;
337
Gary Hadec04fc582009-01-06 14:39:14 -0800338 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
Dave Hansen3947be12005-10-29 18:16:54 -0700339}
340
David Rientjes4edd7ce2013-04-29 15:08:22 -0700341/*
342 * Reasonably generic function for adding memory. It is
343 * expected that archs that support memory hotplug will
344 * call this function after deciding the zone to which to
345 * add the new pages.
346 */
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700347int __ref __add_pages(int nid, unsigned long phys_start_pfn,
Michal Hocko1b862ae2017-07-06 15:37:45 -0700348 unsigned long nr_pages, bool want_memblock)
David Rientjes4edd7ce2013-04-29 15:08:22 -0700349{
350 unsigned long i;
351 int err = 0;
352 int start_sec, end_sec;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800353 struct vmem_altmap *altmap;
354
David Rientjes4edd7ce2013-04-29 15:08:22 -0700355 /* during initialize mem_map, align hot-added range to section */
356 start_sec = pfn_to_section_nr(phys_start_pfn);
357 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
358
Dan Williams4b94ffd2016-01-15 16:56:22 -0800359 altmap = to_vmem_altmap((unsigned long) pfn_to_page(phys_start_pfn));
360 if (altmap) {
361 /*
362 * Validate altmap is within bounds of the total request
363 */
364 if (altmap->base_pfn != phys_start_pfn
365 || vmem_altmap_offset(altmap) > nr_pages) {
366 pr_warn_once("memory add fail, invalid altmap\n");
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700367 err = -EINVAL;
368 goto out;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800369 }
370 altmap->alloc = 0;
371 }
372
David Rientjes4edd7ce2013-04-29 15:08:22 -0700373 for (i = start_sec; i <= end_sec; i++) {
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700374 err = __add_section(nid, section_nr_to_pfn(i), want_memblock);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700375
376 /*
377 * EEXIST is finally dealt with by ioresource collision
378 * check. see add_memory() => register_memory_resource()
379 * Warning will be printed if there is collision.
380 */
381 if (err && (err != -EEXIST))
382 break;
383 err = 0;
384 }
Zhu Guihuac435a392015-06-24 16:58:42 -0700385 vmemmap_populate_print_last();
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700386out:
David Rientjes4edd7ce2013-04-29 15:08:22 -0700387 return err;
388}
389EXPORT_SYMBOL_GPL(__add_pages);
390
391#ifdef CONFIG_MEMORY_HOTREMOVE
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800392/* find the smallest valid pfn in the range [start_pfn, end_pfn) */
393static int find_smallest_section_pfn(int nid, struct zone *zone,
394 unsigned long start_pfn,
395 unsigned long end_pfn)
396{
397 struct mem_section *ms;
398
399 for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
400 ms = __pfn_to_section(start_pfn);
401
402 if (unlikely(!valid_section(ms)))
403 continue;
404
405 if (unlikely(pfn_to_nid(start_pfn) != nid))
406 continue;
407
408 if (zone && zone != page_zone(pfn_to_page(start_pfn)))
409 continue;
410
411 return start_pfn;
412 }
413
414 return 0;
415}
416
417/* find the biggest valid pfn in the range [start_pfn, end_pfn). */
418static int find_biggest_section_pfn(int nid, struct zone *zone,
419 unsigned long start_pfn,
420 unsigned long end_pfn)
421{
422 struct mem_section *ms;
423 unsigned long pfn;
424
425 /* pfn is the end pfn of a memory section. */
426 pfn = end_pfn - 1;
427 for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
428 ms = __pfn_to_section(pfn);
429
430 if (unlikely(!valid_section(ms)))
431 continue;
432
433 if (unlikely(pfn_to_nid(pfn) != nid))
434 continue;
435
436 if (zone && zone != page_zone(pfn_to_page(pfn)))
437 continue;
438
439 return pfn;
440 }
441
442 return 0;
443}
444
445static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
446 unsigned long end_pfn)
447{
Xishi Qiuc33bc312013-09-11 14:21:44 -0700448 unsigned long zone_start_pfn = zone->zone_start_pfn;
449 unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
450 unsigned long zone_end_pfn = z;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800451 unsigned long pfn;
452 struct mem_section *ms;
453 int nid = zone_to_nid(zone);
454
455 zone_span_writelock(zone);
456 if (zone_start_pfn == start_pfn) {
457 /*
458 * If the section is smallest section in the zone, it need
459 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
460 * In this case, we find second smallest valid mem_section
461 * for shrinking zone.
462 */
463 pfn = find_smallest_section_pfn(nid, zone, end_pfn,
464 zone_end_pfn);
465 if (pfn) {
466 zone->zone_start_pfn = pfn;
467 zone->spanned_pages = zone_end_pfn - pfn;
468 }
469 } else if (zone_end_pfn == end_pfn) {
470 /*
471 * If the section is biggest section in the zone, it need
472 * shrink zone->spanned_pages.
473 * In this case, we find second biggest valid mem_section for
474 * shrinking zone.
475 */
476 pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
477 start_pfn);
478 if (pfn)
479 zone->spanned_pages = pfn - zone_start_pfn + 1;
480 }
481
482 /*
483 * The section is not biggest or smallest mem_section in the zone, it
484 * only creates a hole in the zone. So in this case, we need not
485 * change the zone. But perhaps, the zone has only hole data. Thus
486 * it check the zone has only hole or not.
487 */
488 pfn = zone_start_pfn;
489 for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
490 ms = __pfn_to_section(pfn);
491
492 if (unlikely(!valid_section(ms)))
493 continue;
494
495 if (page_zone(pfn_to_page(pfn)) != zone)
496 continue;
497
498 /* If the section is current section, it continues the loop */
499 if (start_pfn == pfn)
500 continue;
501
502 /* If we find valid section, we have nothing to do */
503 zone_span_writeunlock(zone);
504 return;
505 }
506
507 /* The zone has no valid section */
508 zone->zone_start_pfn = 0;
509 zone->spanned_pages = 0;
510 zone_span_writeunlock(zone);
511}
512
513static void shrink_pgdat_span(struct pglist_data *pgdat,
514 unsigned long start_pfn, unsigned long end_pfn)
515{
Xishi Qiu83285c72013-11-12 15:07:19 -0800516 unsigned long pgdat_start_pfn = pgdat->node_start_pfn;
517 unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */
518 unsigned long pgdat_end_pfn = p;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800519 unsigned long pfn;
520 struct mem_section *ms;
521 int nid = pgdat->node_id;
522
523 if (pgdat_start_pfn == start_pfn) {
524 /*
525 * If the section is smallest section in the pgdat, it need
526 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
527 * In this case, we find second smallest valid mem_section
528 * for shrinking zone.
529 */
530 pfn = find_smallest_section_pfn(nid, NULL, end_pfn,
531 pgdat_end_pfn);
532 if (pfn) {
533 pgdat->node_start_pfn = pfn;
534 pgdat->node_spanned_pages = pgdat_end_pfn - pfn;
535 }
536 } else if (pgdat_end_pfn == end_pfn) {
537 /*
538 * If the section is biggest section in the pgdat, it need
539 * shrink pgdat->node_spanned_pages.
540 * In this case, we find second biggest valid mem_section for
541 * shrinking zone.
542 */
543 pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn,
544 start_pfn);
545 if (pfn)
546 pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1;
547 }
548
549 /*
550 * If the section is not biggest or smallest mem_section in the pgdat,
551 * it only creates a hole in the pgdat. So in this case, we need not
552 * change the pgdat.
553 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
554 * has only hole or not.
555 */
556 pfn = pgdat_start_pfn;
557 for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) {
558 ms = __pfn_to_section(pfn);
559
560 if (unlikely(!valid_section(ms)))
561 continue;
562
563 if (pfn_to_nid(pfn) != nid)
564 continue;
565
566 /* If the section is current section, it continues the loop */
567 if (start_pfn == pfn)
568 continue;
569
570 /* If we find valid section, we have nothing to do */
571 return;
572 }
573
574 /* The pgdat has no valid section */
575 pgdat->node_start_pfn = 0;
576 pgdat->node_spanned_pages = 0;
577}
578
579static void __remove_zone(struct zone *zone, unsigned long start_pfn)
580{
581 struct pglist_data *pgdat = zone->zone_pgdat;
582 int nr_pages = PAGES_PER_SECTION;
583 int zone_type;
584 unsigned long flags;
585
586 zone_type = zone - pgdat->node_zones;
587
588 pgdat_resize_lock(zone->zone_pgdat, &flags);
589 shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
590 shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages);
591 pgdat_resize_unlock(zone->zone_pgdat, &flags);
592}
593
Dan Williams4b94ffd2016-01-15 16:56:22 -0800594static int __remove_section(struct zone *zone, struct mem_section *ms,
595 unsigned long map_offset)
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700596{
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800597 unsigned long start_pfn;
598 int scn_nr;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700599 int ret = -EINVAL;
600
601 if (!valid_section(ms))
602 return ret;
603
604 ret = unregister_memory_section(ms);
605 if (ret)
606 return ret;
607
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800608 scn_nr = __section_nr(ms);
609 start_pfn = section_nr_to_pfn(scn_nr);
610 __remove_zone(zone, start_pfn);
611
Dan Williams4b94ffd2016-01-15 16:56:22 -0800612 sparse_remove_one_section(zone, ms, map_offset);
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700613 return 0;
614}
615
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700616/**
617 * __remove_pages() - remove sections of pages from a zone
618 * @zone: zone from which pages need to be removed
619 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
620 * @nr_pages: number of pages to remove (must be multiple of section size)
621 *
622 * Generic helper function to remove section mappings and sysfs entries
623 * for the section of the memory we are removing. Caller needs to make
624 * sure that pages are marked reserved and zones are adjust properly by
625 * calling offline_pages().
626 */
627int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
628 unsigned long nr_pages)
629{
Toshi Kanife74ebb2013-04-29 15:08:20 -0700630 unsigned long i;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800631 unsigned long map_offset = 0;
632 int sections_to_remove, ret = 0;
633
634 /* In the ZONE_DEVICE case device driver owns the memory region */
635 if (is_dev_zone(zone)) {
636 struct page *page = pfn_to_page(phys_start_pfn);
637 struct vmem_altmap *altmap;
638
639 altmap = to_vmem_altmap((unsigned long) page);
640 if (altmap)
641 map_offset = vmem_altmap_offset(altmap);
642 } else {
643 resource_size_t start, size;
644
645 start = phys_start_pfn << PAGE_SHIFT;
646 size = nr_pages * PAGE_SIZE;
647
648 ret = release_mem_region_adjustable(&iomem_resource, start,
649 size);
650 if (ret) {
651 resource_size_t endres = start + size - 1;
652
653 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
654 &start, &endres, ret);
655 }
656 }
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700657
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700658 clear_zone_contiguous(zone);
659
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700660 /*
661 * We can only remove entire sections
662 */
663 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
664 BUG_ON(nr_pages % PAGES_PER_SECTION);
665
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700666 sections_to_remove = nr_pages / PAGES_PER_SECTION;
667 for (i = 0; i < sections_to_remove; i++) {
668 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800669
670 ret = __remove_section(zone, __pfn_to_section(pfn), map_offset);
671 map_offset = 0;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700672 if (ret)
673 break;
674 }
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700675
676 set_zone_contiguous(zone);
677
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700678 return ret;
679}
David Rientjes4edd7ce2013-04-29 15:08:22 -0700680#endif /* CONFIG_MEMORY_HOTREMOVE */
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700681
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700682int set_online_page_callback(online_page_callback_t callback)
683{
684 int rc = -EINVAL;
685
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700686 get_online_mems();
687 mutex_lock(&online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700688
689 if (online_page_callback == generic_online_page) {
690 online_page_callback = callback;
691 rc = 0;
692 }
693
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700694 mutex_unlock(&online_page_callback_lock);
695 put_online_mems();
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700696
697 return rc;
698}
699EXPORT_SYMBOL_GPL(set_online_page_callback);
700
701int restore_online_page_callback(online_page_callback_t callback)
702{
703 int rc = -EINVAL;
704
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700705 get_online_mems();
706 mutex_lock(&online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700707
708 if (online_page_callback == callback) {
709 online_page_callback = generic_online_page;
710 rc = 0;
711 }
712
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700713 mutex_unlock(&online_page_callback_lock);
714 put_online_mems();
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700715
716 return rc;
717}
718EXPORT_SYMBOL_GPL(restore_online_page_callback);
719
720void __online_page_set_limits(struct page *page)
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700721{
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700722}
723EXPORT_SYMBOL_GPL(__online_page_set_limits);
724
725void __online_page_increment_counters(struct page *page)
726{
Jiang Liu3dcc0572013-07-03 15:03:21 -0700727 adjust_managed_page_count(page, 1);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700728}
729EXPORT_SYMBOL_GPL(__online_page_increment_counters);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700730
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700731void __online_page_free(struct page *page)
732{
Jiang Liu3dcc0572013-07-03 15:03:21 -0700733 __free_reserved_page(page);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700734}
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700735EXPORT_SYMBOL_GPL(__online_page_free);
736
737static void generic_online_page(struct page *page)
738{
739 __online_page_set_limits(page);
740 __online_page_increment_counters(page);
741 __online_page_free(page);
742}
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700743
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700744static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
745 void *arg)
Dave Hansen3947be12005-10-29 18:16:54 -0700746{
747 unsigned long i;
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700748 unsigned long onlined_pages = *(unsigned long *)arg;
749 struct page *page;
Michal Hocko2d070ea2017-07-06 15:37:56 -0700750
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700751 if (PageReserved(pfn_to_page(start_pfn)))
752 for (i = 0; i < nr_pages; i++) {
753 page = pfn_to_page(start_pfn + i);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700754 (*online_page_callback)(page);
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700755 onlined_pages++;
756 }
Michal Hocko2d070ea2017-07-06 15:37:56 -0700757
758 online_mem_sections(start_pfn, start_pfn + nr_pages);
759
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700760 *(unsigned long *)arg = onlined_pages;
761 return 0;
762}
763
Lai Jiangshand9713672012-12-11 16:01:03 -0800764/* check which state of node_states will be changed when online memory */
765static void node_states_check_changes_online(unsigned long nr_pages,
766 struct zone *zone, struct memory_notify *arg)
767{
768 int nid = zone_to_nid(zone);
769 enum zone_type zone_last = ZONE_NORMAL;
770
771 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800772 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
773 * contains nodes which have zones of 0...ZONE_NORMAL,
774 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -0800775 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800776 * If we don't have HIGHMEM nor movable node,
777 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
778 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -0800779 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800780 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -0800781 zone_last = ZONE_MOVABLE;
782
783 /*
784 * if the memory to be online is in a zone of 0...zone_last, and
785 * the zones of 0...zone_last don't have memory before online, we will
786 * need to set the node to node_states[N_NORMAL_MEMORY] after
787 * the memory is online.
788 */
789 if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
790 arg->status_change_nid_normal = nid;
791 else
792 arg->status_change_nid_normal = -1;
793
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800794#ifdef CONFIG_HIGHMEM
795 /*
796 * If we have movable node, node_states[N_HIGH_MEMORY]
797 * contains nodes which have zones of 0...ZONE_HIGHMEM,
798 * set zone_last to ZONE_HIGHMEM.
799 *
800 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
801 * contains nodes which have zones of 0...ZONE_MOVABLE,
802 * set zone_last to ZONE_MOVABLE.
803 */
804 zone_last = ZONE_HIGHMEM;
805 if (N_MEMORY == N_HIGH_MEMORY)
806 zone_last = ZONE_MOVABLE;
807
808 if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
809 arg->status_change_nid_high = nid;
810 else
811 arg->status_change_nid_high = -1;
812#else
813 arg->status_change_nid_high = arg->status_change_nid_normal;
814#endif
815
Lai Jiangshand9713672012-12-11 16:01:03 -0800816 /*
817 * if the node don't have memory befor online, we will need to
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800818 * set the node to node_states[N_MEMORY] after the memory
Lai Jiangshand9713672012-12-11 16:01:03 -0800819 * is online.
820 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800821 if (!node_state(nid, N_MEMORY))
Lai Jiangshand9713672012-12-11 16:01:03 -0800822 arg->status_change_nid = nid;
823 else
824 arg->status_change_nid = -1;
825}
826
827static void node_states_set_node(int node, struct memory_notify *arg)
828{
829 if (arg->status_change_nid_normal >= 0)
830 node_set_state(node, N_NORMAL_MEMORY);
831
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800832 if (arg->status_change_nid_high >= 0)
833 node_set_state(node, N_HIGH_MEMORY);
834
835 node_set_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -0800836}
837
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700838bool allow_online_pfn_range(int nid, unsigned long pfn, unsigned long nr_pages, int online_type)
Reza Arbabdf429ac2016-07-26 15:22:23 -0700839{
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700840 struct pglist_data *pgdat = NODE_DATA(nid);
841 struct zone *movable_zone = &pgdat->node_zones[ZONE_MOVABLE];
Michal Hockoc246a212017-07-06 15:38:18 -0700842 struct zone *default_zone = default_zone_for_pfn(nid, pfn, nr_pages);
Reza Arbabdf429ac2016-07-26 15:22:23 -0700843
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700844 /*
845 * TODO there shouldn't be any inherent reason to have ZONE_NORMAL
846 * physically before ZONE_MOVABLE. All we need is they do not
847 * overlap. Historically we didn't allow ZONE_NORMAL after ZONE_MOVABLE
848 * though so let's stick with it for simplicity for now.
849 * TODO make sure we do not overlap with ZONE_DEVICE
850 */
851 if (online_type == MMOP_ONLINE_KERNEL) {
852 if (zone_is_empty(movable_zone))
853 return true;
854 return movable_zone->zone_start_pfn >= pfn + nr_pages;
855 } else if (online_type == MMOP_ONLINE_MOVABLE) {
Michal Hockoc246a212017-07-06 15:38:18 -0700856 return zone_end_pfn(default_zone) <= pfn;
Reza Arbabdf429ac2016-07-26 15:22:23 -0700857 }
858
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700859 /* MMOP_ONLINE_KEEP will always succeed and inherits the current zone */
860 return online_type == MMOP_ONLINE_KEEP;
861}
Reza Arbabdf429ac2016-07-26 15:22:23 -0700862
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700863static void __meminit resize_zone_range(struct zone *zone, unsigned long start_pfn,
864 unsigned long nr_pages)
865{
866 unsigned long old_end_pfn = zone_end_pfn(zone);
867
868 if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
869 zone->zone_start_pfn = start_pfn;
870
871 zone->spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - zone->zone_start_pfn;
872}
873
874static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned long start_pfn,
875 unsigned long nr_pages)
876{
877 unsigned long old_end_pfn = pgdat_end_pfn(pgdat);
878
879 if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
880 pgdat->node_start_pfn = start_pfn;
881
882 pgdat->node_spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - pgdat->node_start_pfn;
883}
884
Michal Hockocdf72f22017-07-06 15:38:25 -0700885void __ref move_pfn_range_to_zone(struct zone *zone,
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700886 unsigned long start_pfn, unsigned long nr_pages)
887{
888 struct pglist_data *pgdat = zone->zone_pgdat;
889 int nid = pgdat->node_id;
890 unsigned long flags;
891
892 if (zone_is_empty(zone))
893 init_currently_empty_zone(zone, start_pfn, nr_pages);
894
895 clear_zone_contiguous(zone);
896
897 /* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */
898 pgdat_resize_lock(pgdat, &flags);
899 zone_span_writelock(zone);
900 resize_zone_range(zone, start_pfn, nr_pages);
901 zone_span_writeunlock(zone);
902 resize_pgdat_range(pgdat, start_pfn, nr_pages);
903 pgdat_resize_unlock(pgdat, &flags);
904
905 /*
906 * TODO now we have a visible range of pages which are not associated
907 * with their zone properly. Not nice but set_pfnblock_flags_mask
908 * expects the zone spans the pfn range. All the pages in the range
909 * are reserved so nobody should be touching them so we should be safe
910 */
911 memmap_init_zone(nr_pages, nid, zone_idx(zone), start_pfn, MEMMAP_HOTPLUG);
912
913 set_zone_contiguous(zone);
914}
915
916/*
Michal Hockoc246a212017-07-06 15:38:18 -0700917 * Returns a default kernel memory zone for the given pfn range.
918 * If no kernel zone covers this pfn range it will automatically go
919 * to the ZONE_NORMAL.
920 */
921struct zone *default_zone_for_pfn(int nid, unsigned long start_pfn,
922 unsigned long nr_pages)
923{
924 struct pglist_data *pgdat = NODE_DATA(nid);
925 int zid;
926
927 for (zid = 0; zid <= ZONE_NORMAL; zid++) {
928 struct zone *zone = &pgdat->node_zones[zid];
929
930 if (zone_intersects(zone, start_pfn, nr_pages))
931 return zone;
932 }
933
934 return &pgdat->node_zones[ZONE_NORMAL];
935}
936
Michal Hocko9f123ab2017-07-10 15:48:37 -0700937static inline bool movable_pfn_range(int nid, struct zone *default_zone,
938 unsigned long start_pfn, unsigned long nr_pages)
939{
940 if (!allow_online_pfn_range(nid, start_pfn, nr_pages,
941 MMOP_ONLINE_KERNEL))
942 return true;
943
944 if (!movable_node_is_enabled())
945 return false;
946
947 return !zone_intersects(default_zone, start_pfn, nr_pages);
948}
949
Michal Hockoc246a212017-07-06 15:38:18 -0700950/*
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700951 * Associates the given pfn range with the given node and the zone appropriate
952 * for the given online type.
953 */
954static struct zone * __meminit move_pfn_range(int online_type, int nid,
955 unsigned long start_pfn, unsigned long nr_pages)
956{
957 struct pglist_data *pgdat = NODE_DATA(nid);
Michal Hockoc246a212017-07-06 15:38:18 -0700958 struct zone *zone = default_zone_for_pfn(nid, start_pfn, nr_pages);
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700959
960 if (online_type == MMOP_ONLINE_KEEP) {
961 struct zone *movable_zone = &pgdat->node_zones[ZONE_MOVABLE];
962 /*
Michal Hockoa69578a2017-07-06 15:38:15 -0700963 * MMOP_ONLINE_KEEP defaults to MMOP_ONLINE_KERNEL but use
964 * movable zone if that is not possible (e.g. we are within
Michal Hocko9f123ab2017-07-10 15:48:37 -0700965 * or past the existing movable zone). movable_node overrides
966 * this default and defaults to movable zone
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700967 */
Michal Hocko9f123ab2017-07-10 15:48:37 -0700968 if (movable_pfn_range(nid, zone, start_pfn, nr_pages))
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700969 zone = movable_zone;
970 } else if (online_type == MMOP_ONLINE_MOVABLE) {
971 zone = &pgdat->node_zones[ZONE_MOVABLE];
Reza Arbabdf429ac2016-07-26 15:22:23 -0700972 }
973
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700974 move_pfn_range_to_zone(zone, start_pfn, nr_pages);
975 return zone;
Reza Arbabdf429ac2016-07-26 15:22:23 -0700976}
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700977
David Rientjes30467e02015-04-14 15:45:11 -0700978/* Must be protected by mem_hotplug_begin() */
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800979int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700980{
Cody P Schaferaa472282013-07-03 15:02:10 -0700981 unsigned long flags;
Dave Hansen3947be12005-10-29 18:16:54 -0700982 unsigned long onlined_pages = 0;
983 struct zone *zone;
Yasunori Goto68113782006-06-23 02:03:11 -0700984 int need_zonelists_rebuild = 0;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700985 int nid;
986 int ret;
987 struct memory_notify arg;
Dave Hansen3947be12005-10-29 18:16:54 -0700988
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700989 nid = pfn_to_nid(pfn);
990 if (!allow_online_pfn_range(nid, pfn, nr_pages, online_type))
David Rientjes30467e02015-04-14 15:45:11 -0700991 return -EINVAL;
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800992
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700993 /* associate pfn range with the zone */
994 zone = move_pfn_range(online_type, nid, pfn, nr_pages);
995
Yasunori Goto7b78d332007-10-21 16:41:36 -0700996 arg.start_pfn = pfn;
997 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -0800998 node_states_check_changes_online(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -0700999
Yasunori Goto7b78d332007-10-21 16:41:36 -07001000 ret = memory_notify(MEM_GOING_ONLINE, &arg);
1001 ret = notifier_to_errno(ret);
Chen Yuconge33e33b2016-03-17 14:19:35 -07001002 if (ret)
1003 goto failed_addition;
1004
Dave Hansen3947be12005-10-29 18:16:54 -07001005 /*
Yasunori Goto68113782006-06-23 02:03:11 -07001006 * If this zone is not populated, then it is not in zonelist.
1007 * This means the page allocator ignores this zone.
1008 * So, zonelist must be updated after online.
1009 */
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001010 mutex_lock(&zonelists_mutex);
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001011 if (!populated_zone(zone)) {
Yasunori Goto68113782006-06-23 02:03:11 -07001012 need_zonelists_rebuild = 1;
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001013 build_all_zonelists(NULL, zone);
1014 }
Yasunori Goto68113782006-06-23 02:03:11 -07001015
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001016 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -07001017 online_pages_range);
Geoff Levandfd8a4222008-05-14 16:05:50 -07001018 if (ret) {
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001019 if (need_zonelists_rebuild)
1020 zone_pcp_reset(zone);
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001021 mutex_unlock(&zonelists_mutex);
Chen Yuconge33e33b2016-03-17 14:19:35 -07001022 goto failed_addition;
Geoff Levandfd8a4222008-05-14 16:05:50 -07001023 }
1024
Dave Hansen3947be12005-10-29 18:16:54 -07001025 zone->present_pages += onlined_pages;
Cody P Schaferaa472282013-07-03 15:02:10 -07001026
1027 pgdat_resize_lock(zone->zone_pgdat, &flags);
Yasunori Gotof2937be2006-03-09 17:33:51 -08001028 zone->zone_pgdat->node_present_pages += onlined_pages;
Cody P Schaferaa472282013-07-03 15:02:10 -07001029 pgdat_resize_unlock(zone->zone_pgdat, &flags);
1030
Jiang Liu08dff7b2012-07-31 16:43:30 -07001031 if (onlined_pages) {
Vlastimil Babkae888ca32016-03-17 14:18:12 -07001032 node_states_set_node(nid, &arg);
Jiang Liu08dff7b2012-07-31 16:43:30 -07001033 if (need_zonelists_rebuild)
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001034 build_all_zonelists(NULL, NULL);
Jiang Liu08dff7b2012-07-31 16:43:30 -07001035 else
1036 zone_pcp_update(zone);
1037 }
Dave Hansen3947be12005-10-29 18:16:54 -07001038
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001039 mutex_unlock(&zonelists_mutex);
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07001040
1041 init_per_zone_wmark_min();
1042
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001043 if (onlined_pages) {
Vlastimil Babkae888ca32016-03-17 14:18:12 -07001044 kswapd_run(nid);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001045 kcompactd_run(nid);
1046 }
Dave Hansen61b13992005-10-29 18:16:56 -07001047
Haicheng Li1f522502010-05-24 14:32:51 -07001048 vm_total_pages = nr_free_pagecache_pages();
Kent Liu2f7f24e2008-07-23 21:28:18 -07001049
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -07001050 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001051
1052 if (onlined_pages)
1053 memory_notify(MEM_ONLINE, &arg);
David Rientjes30467e02015-04-14 15:45:11 -07001054 return 0;
Chen Yuconge33e33b2016-03-17 14:19:35 -07001055
1056failed_addition:
1057 pr_debug("online_pages [mem %#010llx-%#010llx] failed\n",
1058 (unsigned long long) pfn << PAGE_SHIFT,
1059 (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1);
1060 memory_notify(MEM_CANCEL_ONLINE, &arg);
1061 return ret;
Dave Hansen3947be12005-10-29 18:16:54 -07001062}
Keith Mannthey53947022006-09-30 23:27:08 -07001063#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
Yasunori Gotobc02af92006-06-27 02:53:30 -07001064
Tang Chen0bd85422014-11-13 15:19:41 -08001065static void reset_node_present_pages(pg_data_t *pgdat)
1066{
1067 struct zone *z;
1068
1069 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
1070 z->present_pages = 0;
1071
1072 pgdat->node_present_pages = 0;
1073}
1074
Hidetoshi Setoe1319332009-11-17 14:06:18 -08001075/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1076static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001077{
1078 struct pglist_data *pgdat;
1079 unsigned long zones_size[MAX_NR_ZONES] = {0};
1080 unsigned long zholes_size[MAX_NR_ZONES] = {0};
Fabian Frederickc8e861a2014-06-04 16:07:51 -07001081 unsigned long start_pfn = PFN_DOWN(start);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001082
Tang Chena1e565a2013-02-22 16:33:18 -08001083 pgdat = NODE_DATA(nid);
1084 if (!pgdat) {
1085 pgdat = arch_alloc_nodedata(nid);
1086 if (!pgdat)
1087 return NULL;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001088
Tang Chena1e565a2013-02-22 16:33:18 -08001089 arch_refresh_nodedata(nid, pgdat);
Gu Zhengb0dc3a32015-03-25 15:55:20 -07001090 } else {
Mel Gormane716f2e2017-05-03 14:53:45 -07001091 /*
1092 * Reset the nr_zones, order and classzone_idx before reuse.
1093 * Note that kswapd will init kswapd_classzone_idx properly
1094 * when it starts in the near future.
1095 */
Gu Zhengb0dc3a32015-03-25 15:55:20 -07001096 pgdat->nr_zones = 0;
Mel Gorman38087d92016-07-28 15:45:49 -07001097 pgdat->kswapd_order = 0;
1098 pgdat->kswapd_classzone_idx = 0;
Tang Chena1e565a2013-02-22 16:33:18 -08001099 }
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001100
1101 /* we can use NODE_DATA(nid) from here */
1102
1103 /* init node's zones as empty zones, we don't have any present pages.*/
Johannes Weiner9109fb72008-07-23 21:27:20 -07001104 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
Reza Arbab58301692016-08-11 15:33:12 -07001105 pgdat->per_cpu_nodestats = alloc_percpu(struct per_cpu_nodestat);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001106
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -07001107 /*
1108 * The node we allocated has no zone fallback lists. For avoiding
1109 * to access not-initialized zonelist, build here.
1110 */
David Rientjesf957db42011-06-22 18:13:04 -07001111 mutex_lock(&zonelists_mutex);
Jiang Liu9adb62a2012-07-31 16:43:28 -07001112 build_all_zonelists(pgdat, NULL);
David Rientjesf957db42011-06-22 18:13:04 -07001113 mutex_unlock(&zonelists_mutex);
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -07001114
Tang Chenf784a3f2014-11-13 15:19:39 -08001115 /*
1116 * zone->managed_pages is set to an approximate value in
1117 * free_area_init_core(), which will cause
1118 * /sys/device/system/node/nodeX/meminfo has wrong data.
1119 * So reset it to 0 before any memory is onlined.
1120 */
1121 reset_node_managed_pages(pgdat);
1122
Tang Chen0bd85422014-11-13 15:19:41 -08001123 /*
1124 * When memory is hot-added, all the memory is in offline state. So
1125 * clear all zones' present_pages because they will be updated in
1126 * online_pages() and offline_pages().
1127 */
1128 reset_node_present_pages(pgdat);
1129
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001130 return pgdat;
1131}
1132
1133static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
1134{
1135 arch_refresh_nodedata(nid, NULL);
Reza Arbab58301692016-08-11 15:33:12 -07001136 free_percpu(pgdat->per_cpu_nodestats);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001137 arch_free_nodedata(pgdat);
1138 return;
1139}
1140
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -07001141
Toshi Kani01b0f192013-11-12 15:07:25 -08001142/**
1143 * try_online_node - online a node if offlined
1144 *
minskey guocf234222010-05-24 14:32:41 -07001145 * called by cpu_up() to online a node without onlined memory.
1146 */
Toshi Kani01b0f192013-11-12 15:07:25 -08001147int try_online_node(int nid)
minskey guocf234222010-05-24 14:32:41 -07001148{
1149 pg_data_t *pgdat;
1150 int ret;
1151
Toshi Kani01b0f192013-11-12 15:07:25 -08001152 if (node_online(nid))
1153 return 0;
1154
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001155 mem_hotplug_begin();
minskey guocf234222010-05-24 14:32:41 -07001156 pgdat = hotadd_new_pgdat(nid, 0);
David Rientjes7553e8f2011-06-22 18:13:01 -07001157 if (!pgdat) {
Toshi Kani01b0f192013-11-12 15:07:25 -08001158 pr_err("Cannot online node %d due to NULL pgdat\n", nid);
minskey guocf234222010-05-24 14:32:41 -07001159 ret = -ENOMEM;
1160 goto out;
1161 }
1162 node_set_online(nid);
1163 ret = register_one_node(nid);
1164 BUG_ON(ret);
1165
Toshi Kani01b0f192013-11-12 15:07:25 -08001166 if (pgdat->node_zonelists->_zonerefs->zone == NULL) {
1167 mutex_lock(&zonelists_mutex);
1168 build_all_zonelists(NULL, NULL);
1169 mutex_unlock(&zonelists_mutex);
1170 }
1171
minskey guocf234222010-05-24 14:32:41 -07001172out:
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001173 mem_hotplug_done();
minskey guocf234222010-05-24 14:32:41 -07001174 return ret;
1175}
1176
Toshi Kani27356f52013-09-11 14:21:49 -07001177static int check_hotplug_memory_range(u64 start, u64 size)
1178{
Fabian Frederickc8e861a2014-06-04 16:07:51 -07001179 u64 start_pfn = PFN_DOWN(start);
Toshi Kani27356f52013-09-11 14:21:49 -07001180 u64 nr_pages = size >> PAGE_SHIFT;
1181
1182 /* Memory range must be aligned with section */
1183 if ((start_pfn & ~PAGE_SECTION_MASK) ||
1184 (nr_pages % PAGES_PER_SECTION) || (!nr_pages)) {
1185 pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
1186 (unsigned long long)start,
1187 (unsigned long long)size);
1188 return -EINVAL;
1189 }
1190
1191 return 0;
1192}
1193
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001194static int online_memory_block(struct memory_block *mem, void *arg)
1195{
Nathan Fontenotdc18d702017-02-24 15:00:02 -08001196 return device_online(&mem->dev);
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001197}
1198
Al Viro31168482008-11-22 17:33:24 +00001199/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001200int __ref add_memory_resource(int nid, struct resource *res, bool online)
Yasunori Gotobc02af92006-06-27 02:53:30 -07001201{
David Vrabel62cedb92015-06-25 16:35:49 +01001202 u64 start, size;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001203 pg_data_t *pgdat = NULL;
Tang Chena1e565a2013-02-22 16:33:18 -08001204 bool new_pgdat;
1205 bool new_node;
Yasunori Gotobc02af92006-06-27 02:53:30 -07001206 int ret;
1207
David Vrabel62cedb92015-06-25 16:35:49 +01001208 start = res->start;
1209 size = resource_size(res);
1210
Toshi Kani27356f52013-09-11 14:21:49 -07001211 ret = check_hotplug_memory_range(start, size);
1212 if (ret)
1213 return ret;
1214
Tang Chena1e565a2013-02-22 16:33:18 -08001215 { /* Stupid hack to suppress address-never-null warning */
1216 void *p = NODE_DATA(nid);
1217 new_pgdat = !p;
1218 }
Nathan Zimmerac13c462014-01-23 15:53:26 -08001219
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001220 mem_hotplug_begin();
Nathan Zimmerac13c462014-01-23 15:53:26 -08001221
Tang Chen7f36e3e2015-09-04 15:42:32 -07001222 /*
1223 * Add new range to memblock so that when hotadd_new_pgdat() is called
1224 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
1225 * this new range and calculate total pages correctly. The range will
1226 * be removed at hot-remove time.
1227 */
1228 memblock_add_node(start, size, nid);
1229
Tang Chena1e565a2013-02-22 16:33:18 -08001230 new_node = !node_online(nid);
1231 if (new_node) {
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001232 pgdat = hotadd_new_pgdat(nid, start);
Andi Kleen6ad696d2009-11-17 14:06:22 -08001233 ret = -ENOMEM;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001234 if (!pgdat)
Wen Congyang41b9e2d2012-07-11 14:02:31 -07001235 goto error;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001236 }
1237
Yasunori Gotobc02af92006-06-27 02:53:30 -07001238 /* call arch's memory hotadd */
Michal Hocko3d79a722017-07-06 15:38:21 -07001239 ret = arch_add_memory(nid, start, size, true);
Yasunori Gotobc02af92006-06-27 02:53:30 -07001240
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001241 if (ret < 0)
1242 goto error;
1243
Yasunori Goto0fc44152006-06-27 02:53:38 -07001244 /* we online node here. we can't roll back from here. */
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001245 node_set_online(nid);
1246
Tang Chena1e565a2013-02-22 16:33:18 -08001247 if (new_node) {
Michal Hocko9037a992017-07-06 15:37:49 -07001248 unsigned long start_pfn = start >> PAGE_SHIFT;
1249 unsigned long nr_pages = size >> PAGE_SHIFT;
1250
1251 ret = __register_one_node(nid);
1252 if (ret)
1253 goto register_fail;
1254
1255 /*
1256 * link memory sections under this node. This is already
1257 * done when creatig memory section in register_new_memory
1258 * but that depends to have the node registered so offline
1259 * nodes have to go through register_node.
1260 * TODO clean up this mess.
1261 */
1262 ret = link_mem_sections(nid, start_pfn, nr_pages);
1263register_fail:
Yasunori Goto0fc44152006-06-27 02:53:38 -07001264 /*
1265 * If sysfs file of new node can't create, cpu on the node
1266 * can't be hot-added. There is no rollback way now.
1267 * So, check by BUG_ON() to catch it reluctantly..
1268 */
1269 BUG_ON(ret);
1270 }
1271
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -08001272 /* create new memmap entry */
1273 firmware_map_add_hotplug(start, start + size, "System RAM");
1274
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001275 /* online pages if requested */
1276 if (online)
1277 walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1),
1278 NULL, online_memory_block);
1279
Andi Kleen6ad696d2009-11-17 14:06:22 -08001280 goto out;
1281
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001282error:
1283 /* rollback pgdat allocation and others */
Gustavo A. R. Silvadbac61a2017-07-10 15:47:23 -07001284 if (new_pgdat && pgdat)
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001285 rollback_node_hotadd(nid, pgdat);
Tang Chen7f36e3e2015-09-04 15:42:32 -07001286 memblock_remove(start, size);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001287
Andi Kleen6ad696d2009-11-17 14:06:22 -08001288out:
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001289 mem_hotplug_done();
Yasunori Gotobc02af92006-06-27 02:53:30 -07001290 return ret;
1291}
David Vrabel62cedb92015-06-25 16:35:49 +01001292EXPORT_SYMBOL_GPL(add_memory_resource);
1293
1294int __ref add_memory(int nid, u64 start, u64 size)
1295{
1296 struct resource *res;
1297 int ret;
1298
1299 res = register_memory_resource(start, size);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -08001300 if (IS_ERR(res))
1301 return PTR_ERR(res);
David Vrabel62cedb92015-06-25 16:35:49 +01001302
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001303 ret = add_memory_resource(nid, res, memhp_auto_online);
David Vrabel62cedb92015-06-25 16:35:49 +01001304 if (ret < 0)
1305 release_memory_resource(res);
1306 return ret;
1307}
Yasunori Gotobc02af92006-06-27 02:53:30 -07001308EXPORT_SYMBOL_GPL(add_memory);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001309
1310#ifdef CONFIG_MEMORY_HOTREMOVE
1311/*
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001312 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1313 * set and the size of the free page is given by page_order(). Using this,
1314 * the function determines if the pageblock contains only free pages.
1315 * Due to buddy contraints, a free page at least the size of a pageblock will
1316 * be located at the start of the pageblock
1317 */
1318static inline int pageblock_free(struct page *page)
1319{
1320 return PageBuddy(page) && page_order(page) >= pageblock_order;
1321}
1322
1323/* Return the start of the next active pageblock after a given page */
1324static struct page *next_active_pageblock(struct page *page)
1325{
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001326 /* Ensure the starting page is pageblock-aligned */
1327 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
1328
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001329 /* If the entire pageblock is free, move to the end of free page */
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -07001330 if (pageblock_free(page)) {
1331 int order;
1332 /* be careful. we don't have locks, page_order can be changed.*/
1333 order = page_order(page);
1334 if ((order < MAX_ORDER) && (order >= pageblock_order))
1335 return page + (1 << order);
1336 }
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001337
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -07001338 return page + pageblock_nr_pages;
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001339}
1340
1341/* Checks if this range of memory is likely to be hot-removable. */
Yaowei Baic98940f2016-05-19 17:11:26 -07001342bool is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001343{
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001344 struct page *page = pfn_to_page(start_pfn);
1345 struct page *end_page = page + nr_pages;
1346
1347 /* Check the starting page of each pageblock within the range */
1348 for (; page < end_page; page = next_active_pageblock(page)) {
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07001349 if (!is_pageblock_removable_nolock(page))
Yaowei Baic98940f2016-05-19 17:11:26 -07001350 return false;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07001351 cond_resched();
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001352 }
1353
1354 /* All pageblocks in the memory block are likely to be hot-removable */
Yaowei Baic98940f2016-05-19 17:11:26 -07001355 return true;
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001356}
1357
1358/*
Toshi Kanideb88a22017-02-03 13:13:20 -08001359 * Confirm all pages in a range [start, end) belong to the same zone.
Toshi Kania96dfdd2017-02-03 13:13:23 -08001360 * When true, return its valid [start, end).
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001361 */
Toshi Kania96dfdd2017-02-03 13:13:23 -08001362int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn,
1363 unsigned long *valid_start, unsigned long *valid_end)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001364{
Andrew Banman5f0f2882015-12-29 14:54:25 -08001365 unsigned long pfn, sec_end_pfn;
Toshi Kania96dfdd2017-02-03 13:13:23 -08001366 unsigned long start, end;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001367 struct zone *zone = NULL;
1368 struct page *page;
1369 int i;
Toshi Kanideb88a22017-02-03 13:13:20 -08001370 for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn + 1);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001371 pfn < end_pfn;
Toshi Kanideb88a22017-02-03 13:13:20 -08001372 pfn = sec_end_pfn, sec_end_pfn += PAGES_PER_SECTION) {
Andrew Banman5f0f2882015-12-29 14:54:25 -08001373 /* Make sure the memory section is present first */
1374 if (!present_section_nr(pfn_to_section_nr(pfn)))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001375 continue;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001376 for (; pfn < sec_end_pfn && pfn < end_pfn;
1377 pfn += MAX_ORDER_NR_PAGES) {
1378 i = 0;
1379 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1380 while ((i < MAX_ORDER_NR_PAGES) &&
1381 !pfn_valid_within(pfn + i))
1382 i++;
zhong jiangd6d8c8a2017-02-24 14:59:30 -08001383 if (i == MAX_ORDER_NR_PAGES || pfn + i >= end_pfn)
Andrew Banman5f0f2882015-12-29 14:54:25 -08001384 continue;
1385 page = pfn_to_page(pfn + i);
1386 if (zone && page_zone(page) != zone)
1387 return 0;
Toshi Kania96dfdd2017-02-03 13:13:23 -08001388 if (!zone)
1389 start = pfn + i;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001390 zone = page_zone(page);
Toshi Kania96dfdd2017-02-03 13:13:23 -08001391 end = pfn + MAX_ORDER_NR_PAGES;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001392 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001393 }
Toshi Kanideb88a22017-02-03 13:13:20 -08001394
Toshi Kania96dfdd2017-02-03 13:13:23 -08001395 if (zone) {
1396 *valid_start = start;
zhong jiangd6d8c8a2017-02-24 14:59:30 -08001397 *valid_end = min(end, end_pfn);
Toshi Kanideb88a22017-02-03 13:13:20 -08001398 return 1;
Toshi Kania96dfdd2017-02-03 13:13:23 -08001399 } else {
Toshi Kanideb88a22017-02-03 13:13:20 -08001400 return 0;
Toshi Kania96dfdd2017-02-03 13:13:23 -08001401 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001402}
1403
1404/*
Yisheng Xie0efadf42017-02-24 14:57:39 -08001405 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages,
1406 * non-lru movable pages and hugepages). We scan pfn because it's much
1407 * easier than scanning over linked list. This function returns the pfn
1408 * of the first found movable page if it's found, otherwise 0.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001409 */
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001410static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001411{
1412 unsigned long pfn;
1413 struct page *page;
1414 for (pfn = start; pfn < end; pfn++) {
1415 if (pfn_valid(pfn)) {
1416 page = pfn_to_page(pfn);
1417 if (PageLRU(page))
1418 return pfn;
Yisheng Xie0efadf42017-02-24 14:57:39 -08001419 if (__PageMovable(page))
1420 return pfn;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001421 if (PageHuge(page)) {
Naoya Horiguchi7e1f0492015-04-15 16:14:41 -07001422 if (page_huge_active(page))
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001423 return pfn;
1424 else
1425 pfn = round_up(pfn + 1,
1426 1 << compound_order(page)) - 1;
1427 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001428 }
1429 }
1430 return 0;
1431}
1432
Xishi Qiu394e31d2016-07-28 15:48:53 -07001433static struct page *new_node_page(struct page *page, unsigned long private,
1434 int **result)
1435{
1436 gfp_t gfp_mask = GFP_USER | __GFP_MOVABLE;
1437 int nid = page_to_nid(page);
Li Zhong231e97e2016-09-28 15:22:38 -07001438 nodemask_t nmask = node_states[N_MEMORY];
Michal Hocko7f252f22017-07-10 15:48:41 -07001439
1440 /*
1441 * try to allocate from a different node but reuse this node if there
1442 * are no other online nodes to be used (e.g. we are offlining a part
1443 * of the only existing node)
1444 */
1445 node_clear(nid, nmask);
1446 if (nodes_empty(nmask))
1447 node_set(nid, nmask);
Xishi Qiu394e31d2016-07-28 15:48:53 -07001448
1449 /*
1450 * TODO: allocate a destination hugepage from a nearest neighbor node,
1451 * accordance with memory policy of the user process if possible. For
1452 * now as a simple work-around, we use the next node for destination.
1453 */
1454 if (PageHuge(page))
1455 return alloc_huge_page_node(page_hstate(compound_head(page)),
1456 next_node_in(nid, nmask));
1457
Xishi Qiu394e31d2016-07-28 15:48:53 -07001458 if (PageHighMem(page)
1459 || (zone_idx(page_zone(page)) == ZONE_MOVABLE))
1460 gfp_mask |= __GFP_HIGHMEM;
1461
Michal Hocko7f252f22017-07-10 15:48:41 -07001462 return __alloc_pages_nodemask(gfp_mask, 0, nid, &nmask);
Xishi Qiu394e31d2016-07-28 15:48:53 -07001463}
1464
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001465#define NR_OFFLINE_AT_ONCE_PAGES (256)
1466static int
1467do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1468{
1469 unsigned long pfn;
1470 struct page *page;
1471 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
1472 int not_managed = 0;
1473 int ret = 0;
1474 LIST_HEAD(source);
1475
1476 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
1477 if (!pfn_valid(pfn))
1478 continue;
1479 page = pfn_to_page(pfn);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001480
1481 if (PageHuge(page)) {
1482 struct page *head = compound_head(page);
1483 pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
1484 if (compound_order(head) > PFN_SECTION_SHIFT) {
1485 ret = -EBUSY;
1486 break;
1487 }
1488 if (isolate_huge_page(page, &source))
1489 move_pages -= 1 << compound_order(head);
1490 continue;
1491 }
1492
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001493 if (!get_page_unless_zero(page))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001494 continue;
1495 /*
Yisheng Xie0efadf42017-02-24 14:57:39 -08001496 * We can skip free pages. And we can deal with pages on
1497 * LRU and non-lru movable pages.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001498 */
Yisheng Xie0efadf42017-02-24 14:57:39 -08001499 if (PageLRU(page))
1500 ret = isolate_lru_page(page);
1501 else
1502 ret = isolate_movable_page(page, ISOLATE_UNEVICTABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001503 if (!ret) { /* Success */
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001504 put_page(page);
Nick Piggin62695a82008-10-18 20:26:09 -07001505 list_add_tail(&page->lru, &source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001506 move_pages--;
Yisheng Xie0efadf42017-02-24 14:57:39 -08001507 if (!__PageMovable(page))
1508 inc_node_page_state(page, NR_ISOLATED_ANON +
1509 page_is_file_cache(page));
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001510
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001511 } else {
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001512#ifdef CONFIG_DEBUG_VM
Yisheng Xie0efadf42017-02-24 14:57:39 -08001513 pr_alert("failed to isolate pfn %lx\n", pfn);
1514 dump_page(page, "isolation failed");
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001515#endif
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001516 put_page(page);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001517 /* Because we don't have big zone->lock. we should
Bob Liu809c4442010-10-26 14:22:10 -07001518 check this again here. */
1519 if (page_count(page)) {
1520 not_managed++;
Bob Liuf3ab2632010-10-26 14:22:10 -07001521 ret = -EBUSY;
Bob Liu809c4442010-10-26 14:22:10 -07001522 break;
1523 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001524 }
1525 }
Bob Liuf3ab2632010-10-26 14:22:10 -07001526 if (!list_empty(&source)) {
1527 if (not_managed) {
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001528 putback_movable_pages(&source);
Bob Liuf3ab2632010-10-26 14:22:10 -07001529 goto out;
1530 }
Minchan Kim74c08f92012-10-08 16:32:54 -07001531
Xishi Qiu394e31d2016-07-28 15:48:53 -07001532 /* Allocate a new page from the nearest neighbor node */
1533 ret = migrate_pages(&source, new_node_page, NULL, 0,
Hugh Dickins9c620e22013-02-22 16:35:14 -08001534 MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
Bob Liuf3ab2632010-10-26 14:22:10 -07001535 if (ret)
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001536 putback_movable_pages(&source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001537 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001538out:
1539 return ret;
1540}
1541
1542/*
1543 * remove from free_area[] and mark all as Reserved.
1544 */
1545static int
1546offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1547 void *data)
1548{
1549 __offline_isolated_pages(start, start + nr_pages);
1550 return 0;
1551}
1552
1553static void
1554offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1555{
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001556 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001557 offline_isolated_pages_cb);
1558}
1559
1560/*
1561 * Check all pages in range, recoreded as memory resource, are isolated.
1562 */
1563static int
1564check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1565 void *data)
1566{
1567 int ret;
1568 long offlined = *(long *)data;
Wen Congyangb023f462012-12-11 16:00:45 -08001569 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001570 offlined = nr_pages;
1571 if (!ret)
1572 *(long *)data += offlined;
1573 return ret;
1574}
1575
1576static long
1577check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1578{
1579 long offlined = 0;
1580 int ret;
1581
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001582 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001583 check_pages_isolated_cb);
1584 if (ret < 0)
1585 offlined = (long)ret;
1586 return offlined;
1587}
1588
Tang Chenc5320922013-11-12 15:08:10 -08001589static int __init cmdline_parse_movable_node(char *p)
1590{
Michal Hocko49323812017-07-06 15:41:05 -07001591#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Tang Chen55ac5902014-01-21 15:49:35 -08001592 movable_node_enabled = true;
Michal Hocko49323812017-07-06 15:41:05 -07001593#else
1594 pr_warn("movable_node parameter depends on CONFIG_HAVE_MEMBLOCK_NODE_MAP to work properly\n");
1595#endif
Tang Chenc5320922013-11-12 15:08:10 -08001596 return 0;
1597}
1598early_param("movable_node", cmdline_parse_movable_node);
1599
Lai Jiangshand9713672012-12-11 16:01:03 -08001600/* check which state of node_states will be changed when offline memory */
1601static void node_states_check_changes_offline(unsigned long nr_pages,
1602 struct zone *zone, struct memory_notify *arg)
1603{
1604 struct pglist_data *pgdat = zone->zone_pgdat;
1605 unsigned long present_pages = 0;
1606 enum zone_type zt, zone_last = ZONE_NORMAL;
1607
1608 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001609 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1610 * contains nodes which have zones of 0...ZONE_NORMAL,
1611 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -08001612 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001613 * If we don't have HIGHMEM nor movable node,
1614 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1615 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -08001616 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001617 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -08001618 zone_last = ZONE_MOVABLE;
1619
1620 /*
1621 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1622 * If the memory to be offline is in a zone of 0...zone_last,
1623 * and it is the last present memory, 0...zone_last will
1624 * become empty after offline , thus we can determind we will
1625 * need to clear the node from node_states[N_NORMAL_MEMORY].
1626 */
1627 for (zt = 0; zt <= zone_last; zt++)
1628 present_pages += pgdat->node_zones[zt].present_pages;
1629 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1630 arg->status_change_nid_normal = zone_to_nid(zone);
1631 else
1632 arg->status_change_nid_normal = -1;
1633
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001634#ifdef CONFIG_HIGHMEM
1635 /*
1636 * If we have movable node, node_states[N_HIGH_MEMORY]
1637 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1638 * set zone_last to ZONE_HIGHMEM.
1639 *
1640 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1641 * contains nodes which have zones of 0...ZONE_MOVABLE,
1642 * set zone_last to ZONE_MOVABLE.
1643 */
1644 zone_last = ZONE_HIGHMEM;
1645 if (N_MEMORY == N_HIGH_MEMORY)
1646 zone_last = ZONE_MOVABLE;
1647
1648 for (; zt <= zone_last; zt++)
1649 present_pages += pgdat->node_zones[zt].present_pages;
1650 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1651 arg->status_change_nid_high = zone_to_nid(zone);
1652 else
1653 arg->status_change_nid_high = -1;
1654#else
1655 arg->status_change_nid_high = arg->status_change_nid_normal;
1656#endif
1657
Lai Jiangshand9713672012-12-11 16:01:03 -08001658 /*
1659 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1660 */
1661 zone_last = ZONE_MOVABLE;
1662
1663 /*
1664 * check whether node_states[N_HIGH_MEMORY] will be changed
1665 * If we try to offline the last present @nr_pages from the node,
1666 * we can determind we will need to clear the node from
1667 * node_states[N_HIGH_MEMORY].
1668 */
1669 for (; zt <= zone_last; zt++)
1670 present_pages += pgdat->node_zones[zt].present_pages;
1671 if (nr_pages >= present_pages)
1672 arg->status_change_nid = zone_to_nid(zone);
1673 else
1674 arg->status_change_nid = -1;
1675}
1676
1677static void node_states_clear_node(int node, struct memory_notify *arg)
1678{
1679 if (arg->status_change_nid_normal >= 0)
1680 node_clear_state(node, N_NORMAL_MEMORY);
1681
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001682 if ((N_MEMORY != N_NORMAL_MEMORY) &&
1683 (arg->status_change_nid_high >= 0))
Lai Jiangshand9713672012-12-11 16:01:03 -08001684 node_clear_state(node, N_HIGH_MEMORY);
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001685
1686 if ((N_MEMORY != N_HIGH_MEMORY) &&
1687 (arg->status_change_nid >= 0))
1688 node_clear_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -08001689}
1690
Wen Congyanga16cee12012-10-08 16:33:58 -07001691static int __ref __offline_pages(unsigned long start_pfn,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001692 unsigned long end_pfn, unsigned long timeout)
1693{
1694 unsigned long pfn, nr_pages, expire;
1695 long offlined_pages;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001696 int ret, drain, retry_max, node;
Cody P Schaferd7029092013-07-03 15:02:11 -07001697 unsigned long flags;
Toshi Kania96dfdd2017-02-03 13:13:23 -08001698 unsigned long valid_start, valid_end;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001699 struct zone *zone;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001700 struct memory_notify arg;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001701
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001702 /* at least, alignment against pageblock is necessary */
1703 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1704 return -EINVAL;
1705 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1706 return -EINVAL;
1707 /* This makes hotplug much easier...and readable.
1708 we assume this for now. .*/
Toshi Kania96dfdd2017-02-03 13:13:23 -08001709 if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start, &valid_end))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001710 return -EINVAL;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001711
Toshi Kania96dfdd2017-02-03 13:13:23 -08001712 zone = page_zone(pfn_to_page(valid_start));
Yasunori Goto7b78d332007-10-21 16:41:36 -07001713 node = zone_to_nid(zone);
1714 nr_pages = end_pfn - start_pfn;
1715
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001716 /* set above range as isolated */
Wen Congyangb023f462012-12-11 16:00:45 -08001717 ret = start_isolate_page_range(start_pfn, end_pfn,
1718 MIGRATE_MOVABLE, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001719 if (ret)
David Rientjes30467e02015-04-14 15:45:11 -07001720 return ret;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001721
1722 arg.start_pfn = start_pfn;
1723 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -08001724 node_states_check_changes_offline(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001725
1726 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1727 ret = notifier_to_errno(ret);
1728 if (ret)
1729 goto failed_removal;
1730
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001731 pfn = start_pfn;
1732 expire = jiffies + timeout;
1733 drain = 0;
1734 retry_max = 5;
1735repeat:
1736 /* start memory hot removal */
1737 ret = -EAGAIN;
1738 if (time_after(jiffies, expire))
1739 goto failed_removal;
1740 ret = -EINTR;
1741 if (signal_pending(current))
1742 goto failed_removal;
1743 ret = 0;
1744 if (drain) {
1745 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001746 cond_resched();
Vlastimil Babkac0554322014-12-10 15:43:10 -08001747 drain_all_pages(zone);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001748 }
1749
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001750 pfn = scan_movable_pages(start_pfn, end_pfn);
1751 if (pfn) { /* We have movable pages */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001752 ret = do_migrate_range(pfn, end_pfn);
1753 if (!ret) {
1754 drain = 1;
1755 goto repeat;
1756 } else {
1757 if (ret < 0)
1758 if (--retry_max == 0)
1759 goto failed_removal;
1760 yield();
1761 drain = 1;
1762 goto repeat;
1763 }
1764 }
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001765 /* drain all zone's lru pagevec, this is asynchronous... */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001766 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001767 yield();
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001768 /* drain pcp pages, this is synchronous. */
Vlastimil Babkac0554322014-12-10 15:43:10 -08001769 drain_all_pages(zone);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001770 /*
1771 * dissolve free hugepages in the memory block before doing offlining
1772 * actually in order to make hugetlbfs's object counting consistent.
1773 */
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001774 ret = dissolve_free_huge_pages(start_pfn, end_pfn);
1775 if (ret)
1776 goto failed_removal;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001777 /* check again */
1778 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1779 if (offlined_pages < 0) {
1780 ret = -EBUSY;
1781 goto failed_removal;
1782 }
Chen Yuconge33e33b2016-03-17 14:19:35 -07001783 pr_info("Offlined Pages %ld\n", offlined_pages);
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001784 /* Ok, all of our target is isolated.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001785 We cannot do rollback at this point. */
1786 offline_isolated_pages(start_pfn, end_pfn);
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -08001787 /* reset pagetype flags and makes migrate type to be MOVABLE */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02001788 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001789 /* removal success */
Jiang Liu3dcc0572013-07-03 15:03:21 -07001790 adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001791 zone->present_pages -= offlined_pages;
Cody P Schaferd7029092013-07-03 15:02:11 -07001792
1793 pgdat_resize_lock(zone->zone_pgdat, &flags);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001794 zone->zone_pgdat->node_present_pages -= offlined_pages;
Cody P Schaferd7029092013-07-03 15:02:11 -07001795 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001796
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07001797 init_per_zone_wmark_min();
1798
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001799 if (!populated_zone(zone)) {
Jiang Liu340175b2012-07-31 16:43:32 -07001800 zone_pcp_reset(zone);
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001801 mutex_lock(&zonelists_mutex);
1802 build_all_zonelists(NULL, NULL);
1803 mutex_unlock(&zonelists_mutex);
1804 } else
1805 zone_pcp_update(zone);
Jiang Liu340175b2012-07-31 16:43:32 -07001806
Lai Jiangshand9713672012-12-11 16:01:03 -08001807 node_states_clear_node(node, &arg);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001808 if (arg.status_change_nid >= 0) {
David Rientjes8fe23e02009-12-14 17:58:33 -08001809 kswapd_stop(node);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001810 kcompactd_stop(node);
1811 }
Minchan Kimbce73942009-06-16 15:32:50 -07001812
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001813 vm_total_pages = nr_free_pagecache_pages();
1814 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001815
1816 memory_notify(MEM_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001817 return 0;
1818
1819failed_removal:
Chen Yuconge33e33b2016-03-17 14:19:35 -07001820 pr_debug("memory offlining [mem %#010llx-%#010llx] failed\n",
1821 (unsigned long long) start_pfn << PAGE_SHIFT,
1822 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001823 memory_notify(MEM_CANCEL_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001824 /* pushback to free area */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02001825 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001826 return ret;
1827}
Badari Pulavarty71088782008-10-18 20:25:58 -07001828
David Rientjes30467e02015-04-14 15:45:11 -07001829/* Must be protected by mem_hotplug_begin() */
Wen Congyanga16cee12012-10-08 16:33:58 -07001830int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1831{
1832 return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
1833}
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02001834#endif /* CONFIG_MEMORY_HOTREMOVE */
Wen Congyanga16cee12012-10-08 16:33:58 -07001835
Wen Congyangbbc76be2013-02-22 16:32:54 -08001836/**
1837 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1838 * @start_pfn: start pfn of the memory range
Toshi Kanie05c4bb2013-04-29 15:06:16 -07001839 * @end_pfn: end pfn of the memory range
Wen Congyangbbc76be2013-02-22 16:32:54 -08001840 * @arg: argument passed to func
1841 * @func: callback for each memory section walked
1842 *
1843 * This function walks through all present mem sections in range
1844 * [start_pfn, end_pfn) and call func on each mem section.
1845 *
1846 * Returns the return value of func.
1847 */
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02001848int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
Wen Congyangbbc76be2013-02-22 16:32:54 -08001849 void *arg, int (*func)(struct memory_block *, void *))
Badari Pulavarty71088782008-10-18 20:25:58 -07001850{
Wen Congyange90bdb72012-10-08 16:34:01 -07001851 struct memory_block *mem = NULL;
1852 struct mem_section *section;
Wen Congyange90bdb72012-10-08 16:34:01 -07001853 unsigned long pfn, section_nr;
1854 int ret;
Badari Pulavarty71088782008-10-18 20:25:58 -07001855
Wen Congyange90bdb72012-10-08 16:34:01 -07001856 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1857 section_nr = pfn_to_section_nr(pfn);
1858 if (!present_section_nr(section_nr))
1859 continue;
1860
1861 section = __nr_to_section(section_nr);
1862 /* same memblock? */
1863 if (mem)
1864 if ((section_nr >= mem->start_section_nr) &&
1865 (section_nr <= mem->end_section_nr))
1866 continue;
1867
1868 mem = find_memory_block_hinted(section, mem);
1869 if (!mem)
1870 continue;
1871
Wen Congyangbbc76be2013-02-22 16:32:54 -08001872 ret = func(mem, arg);
Wen Congyange90bdb72012-10-08 16:34:01 -07001873 if (ret) {
Wen Congyangbbc76be2013-02-22 16:32:54 -08001874 kobject_put(&mem->dev.kobj);
1875 return ret;
Wen Congyange90bdb72012-10-08 16:34:01 -07001876 }
1877 }
1878
1879 if (mem)
1880 kobject_put(&mem->dev.kobj);
1881
Wen Congyangbbc76be2013-02-22 16:32:54 -08001882 return 0;
1883}
1884
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02001885#ifdef CONFIG_MEMORY_HOTREMOVE
Xishi Qiud6de9d52013-11-12 15:07:20 -08001886static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
Wen Congyangbbc76be2013-02-22 16:32:54 -08001887{
1888 int ret = !is_memblock_offlined(mem);
1889
Randy Dunlap349daa02013-04-29 15:08:49 -07001890 if (unlikely(ret)) {
1891 phys_addr_t beginpa, endpa;
1892
1893 beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
1894 endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
Joe Perches756a0252016-03-17 14:19:47 -07001895 pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n",
Randy Dunlap349daa02013-04-29 15:08:49 -07001896 &beginpa, &endpa);
1897 }
Wen Congyangbbc76be2013-02-22 16:32:54 -08001898
1899 return ret;
1900}
1901
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001902static int check_cpu_on_node(pg_data_t *pgdat)
Tang Chen60a5a192013-02-22 16:33:14 -08001903{
Tang Chen60a5a192013-02-22 16:33:14 -08001904 int cpu;
1905
1906 for_each_present_cpu(cpu) {
1907 if (cpu_to_node(cpu) == pgdat->node_id)
1908 /*
1909 * the cpu on this node isn't removed, and we can't
1910 * offline this node.
1911 */
1912 return -EBUSY;
1913 }
1914
1915 return 0;
1916}
1917
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001918static void unmap_cpu_on_node(pg_data_t *pgdat)
Wen Congyange13fe862013-02-22 16:33:31 -08001919{
1920#ifdef CONFIG_ACPI_NUMA
Wen Congyange13fe862013-02-22 16:33:31 -08001921 int cpu;
1922
1923 for_each_possible_cpu(cpu)
1924 if (cpu_to_node(cpu) == pgdat->node_id)
1925 numa_clear_node(cpu);
1926#endif
1927}
1928
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001929static int check_and_unmap_cpu_on_node(pg_data_t *pgdat)
Wen Congyange13fe862013-02-22 16:33:31 -08001930{
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001931 int ret;
Wen Congyange13fe862013-02-22 16:33:31 -08001932
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001933 ret = check_cpu_on_node(pgdat);
Wen Congyange13fe862013-02-22 16:33:31 -08001934 if (ret)
1935 return ret;
1936
1937 /*
1938 * the node will be offlined when we come here, so we can clear
1939 * the cpu_to_node() now.
1940 */
1941
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001942 unmap_cpu_on_node(pgdat);
Wen Congyange13fe862013-02-22 16:33:31 -08001943 return 0;
1944}
1945
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001946/**
1947 * try_offline_node
1948 *
1949 * Offline a node if all memory sections and cpus of the node are removed.
1950 *
1951 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1952 * and online/offline operations before this call.
1953 */
Wen Congyang90b30cd2013-02-22 16:33:27 -08001954void try_offline_node(int nid)
Tang Chen60a5a192013-02-22 16:33:14 -08001955{
Wen Congyangd822b862013-02-22 16:33:16 -08001956 pg_data_t *pgdat = NODE_DATA(nid);
1957 unsigned long start_pfn = pgdat->node_start_pfn;
1958 unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
Tang Chen60a5a192013-02-22 16:33:14 -08001959 unsigned long pfn;
1960
1961 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1962 unsigned long section_nr = pfn_to_section_nr(pfn);
1963
1964 if (!present_section_nr(section_nr))
1965 continue;
1966
1967 if (pfn_to_nid(pfn) != nid)
1968 continue;
1969
1970 /*
1971 * some memory sections of this node are not removed, and we
1972 * can't offline node now.
1973 */
1974 return;
1975 }
1976
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001977 if (check_and_unmap_cpu_on_node(pgdat))
Tang Chen60a5a192013-02-22 16:33:14 -08001978 return;
1979
1980 /*
1981 * all memory/cpu of this node are removed, we can offline this
1982 * node now.
1983 */
1984 node_set_offline(nid);
1985 unregister_one_node(nid);
1986}
Wen Congyang90b30cd2013-02-22 16:33:27 -08001987EXPORT_SYMBOL(try_offline_node);
Tang Chen60a5a192013-02-22 16:33:14 -08001988
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001989/**
1990 * remove_memory
1991 *
1992 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1993 * and online/offline operations before this call, as required by
1994 * try_offline_node().
1995 */
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02001996void __ref remove_memory(int nid, u64 start, u64 size)
Wen Congyangbbc76be2013-02-22 16:32:54 -08001997{
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02001998 int ret;
Wen Congyang993c1aa2013-02-22 16:32:50 -08001999
Toshi Kani27356f52013-09-11 14:21:49 -07002000 BUG_ON(check_hotplug_memory_range(start, size));
2001
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002002 mem_hotplug_begin();
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002003
2004 /*
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002005 * All memory blocks must be offlined before removing memory. Check
2006 * whether all memory blocks in question are offline and trigger a BUG()
2007 * if this is not the case.
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002008 */
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002009 ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
Xishi Qiud6de9d52013-11-12 15:07:20 -08002010 check_memblock_offlined_cb);
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002011 if (ret)
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002012 BUG();
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002013
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -08002014 /* remove memmap entry */
2015 firmware_map_remove(start, start + size, "System RAM");
Xishi Qiuf9126ab2015-08-14 15:35:16 -07002016 memblock_free(start, size);
2017 memblock_remove(start, size);
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -08002018
Wen Congyang24d335c2013-02-22 16:32:58 -08002019 arch_remove_memory(start, size);
2020
Tang Chen60a5a192013-02-22 16:33:14 -08002021 try_offline_node(nid);
2022
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002023 mem_hotplug_done();
Badari Pulavarty71088782008-10-18 20:25:58 -07002024}
Badari Pulavarty71088782008-10-18 20:25:58 -07002025EXPORT_SYMBOL_GPL(remove_memory);
Rafael J. Wysockiaba6efc2013-06-01 22:24:07 +02002026#endif /* CONFIG_MEMORY_HOTREMOVE */