blob: 92f95952692b0f9aa20bec8afa3cd1725e573452 [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>
9#include <linux/swap.h>
10#include <linux/interrupt.h>
11#include <linux/pagemap.h>
Dave Hansen3947be12005-10-29 18:16:54 -070012#include <linux/compiler.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040013#include <linux/export.h>
Dave Hansen3947be12005-10-29 18:16:54 -070014#include <linux/pagevec.h>
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -070015#include <linux/writeback.h>
Dave Hansen3947be12005-10-29 18:16:54 -070016#include <linux/slab.h>
17#include <linux/sysctl.h>
18#include <linux/cpu.h>
19#include <linux/memory.h>
20#include <linux/memory_hotplug.h>
21#include <linux/highmem.h>
22#include <linux/vmalloc.h>
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -070023#include <linux/ioport.h>
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -070024#include <linux/delay.h>
25#include <linux/migrate.h>
26#include <linux/page-isolation.h>
Badari Pulavarty71088782008-10-18 20:25:58 -070027#include <linux/pfn.h>
Andi Kleen6ad696d2009-11-17 14:06:22 -080028#include <linux/suspend.h>
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -080029#include <linux/mm_inline.h>
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -080030#include <linux/firmware-map.h>
Tang Chen60a5a192013-02-22 16:33:14 -080031#include <linux/stop_machine.h>
Naoya Horiguchic8721bb2013-09-11 14:22:09 -070032#include <linux/hugetlb.h>
Tang Chenc5320922013-11-12 15:08:10 -080033#include <linux/memblock.h>
Tang Chenf784a3f2014-11-13 15:19:39 -080034#include <linux/bootmem.h>
Dave Hansen3947be12005-10-29 18:16:54 -070035
36#include <asm/tlbflush.h>
37
Adrian Bunk1e5ad9a2008-04-28 20:40:08 +030038#include "internal.h"
39
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -070040/*
41 * online_page_callback contains pointer to current page onlining function.
42 * Initially it is generic_online_page(). If it is required it could be
43 * changed by calling set_online_page_callback() for callback registration
44 * and restore_online_page_callback() for generic callback restore.
45 */
46
47static void generic_online_page(struct page *page);
48
49static online_page_callback_t online_page_callback = generic_online_page;
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070050static DEFINE_MUTEX(online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -070051
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070052/* The same as the cpu_hotplug lock, but for memory hotplug. */
53static struct {
54 struct task_struct *active_writer;
55 struct mutex lock; /* Synchronizes accesses to refcount, */
56 /*
57 * Also blocks the new readers during
58 * an ongoing mem hotplug operation.
59 */
60 int refcount;
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080061
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070062#ifdef CONFIG_DEBUG_LOCK_ALLOC
63 struct lockdep_map dep_map;
64#endif
65} mem_hotplug = {
66 .active_writer = NULL,
67 .lock = __MUTEX_INITIALIZER(mem_hotplug.lock),
68 .refcount = 0,
69#ifdef CONFIG_DEBUG_LOCK_ALLOC
70 .dep_map = {.name = "mem_hotplug.lock" },
71#endif
72};
73
74/* Lockdep annotations for get/put_online_mems() and mem_hotplug_begin/end() */
75#define memhp_lock_acquire_read() lock_map_acquire_read(&mem_hotplug.dep_map)
76#define memhp_lock_acquire() lock_map_acquire(&mem_hotplug.dep_map)
77#define memhp_lock_release() lock_map_release(&mem_hotplug.dep_map)
78
79void get_online_mems(void)
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080080{
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070081 might_sleep();
82 if (mem_hotplug.active_writer == current)
83 return;
84 memhp_lock_acquire_read();
85 mutex_lock(&mem_hotplug.lock);
86 mem_hotplug.refcount++;
87 mutex_unlock(&mem_hotplug.lock);
88
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080089}
90
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070091void put_online_mems(void)
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080092{
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070093 if (mem_hotplug.active_writer == current)
94 return;
95 mutex_lock(&mem_hotplug.lock);
96
97 if (WARN_ON(!mem_hotplug.refcount))
98 mem_hotplug.refcount++; /* try to fix things up */
99
100 if (!--mem_hotplug.refcount && unlikely(mem_hotplug.active_writer))
101 wake_up_process(mem_hotplug.active_writer);
102 mutex_unlock(&mem_hotplug.lock);
103 memhp_lock_release();
104
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800105}
106
David Rientjes30467e02015-04-14 15:45:11 -0700107void mem_hotplug_begin(void)
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700108{
109 mem_hotplug.active_writer = current;
110
111 memhp_lock_acquire();
112 for (;;) {
113 mutex_lock(&mem_hotplug.lock);
114 if (likely(!mem_hotplug.refcount))
115 break;
116 __set_current_state(TASK_UNINTERRUPTIBLE);
117 mutex_unlock(&mem_hotplug.lock);
118 schedule();
119 }
120}
121
David Rientjes30467e02015-04-14 15:45:11 -0700122void mem_hotplug_done(void)
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700123{
124 mem_hotplug.active_writer = NULL;
125 mutex_unlock(&mem_hotplug.lock);
126 memhp_lock_release();
127}
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800128
Keith Mannthey45e0b782006-09-30 23:27:09 -0700129/* add this memory to iomem resource */
130static struct resource *register_memory_resource(u64 start, u64 size)
131{
132 struct resource *res;
133 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -0800134 if (!res)
135 return ERR_PTR(-ENOMEM);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700136
137 res->name = "System RAM";
138 res->start = start;
139 res->end = start + size - 1;
Yasunori Goto887c3cb2007-11-14 16:59:20 -0800140 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
Keith Mannthey45e0b782006-09-30 23:27:09 -0700141 if (request_resource(&iomem_resource, res) < 0) {
Toshi Kani4996eed2013-07-03 15:02:39 -0700142 pr_debug("System RAM resource %pR cannot be added\n", res);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700143 kfree(res);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -0800144 return ERR_PTR(-EEXIST);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700145 }
146 return res;
147}
148
149static void release_memory_resource(struct resource *res)
150{
151 if (!res)
152 return;
153 release_resource(res);
154 kfree(res);
155 return;
156}
157
Keith Mannthey53947022006-09-30 23:27:08 -0700158#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800159void get_page_bootmem(unsigned long info, struct page *page,
160 unsigned long type)
Yasunori Goto04753272008-04-28 02:13:31 -0700161{
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800162 page->lru.next = (struct list_head *) type;
Yasunori Goto04753272008-04-28 02:13:31 -0700163 SetPagePrivate(page);
164 set_page_private(page, info);
165 atomic_inc(&page->_count);
166}
167
Jiang Liu170a5a72013-07-03 15:03:17 -0700168void put_page_bootmem(struct page *page)
Yasunori Goto04753272008-04-28 02:13:31 -0700169{
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800170 unsigned long type;
Yasunori Goto04753272008-04-28 02:13:31 -0700171
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800172 type = (unsigned long) page->lru.next;
173 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
174 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
Yasunori Goto04753272008-04-28 02:13:31 -0700175
176 if (atomic_dec_return(&page->_count) == 1) {
177 ClearPagePrivate(page);
178 set_page_private(page, 0);
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800179 INIT_LIST_HEAD(&page->lru);
Jiang Liu170a5a72013-07-03 15:03:17 -0700180 free_reserved_page(page);
Yasunori Goto04753272008-04-28 02:13:31 -0700181 }
Yasunori Goto04753272008-04-28 02:13:31 -0700182}
183
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800184#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
185#ifndef CONFIG_SPARSEMEM_VMEMMAP
Adrian Bunkd92bc312008-07-23 21:28:12 -0700186static void register_page_bootmem_info_section(unsigned long start_pfn)
Yasunori Goto04753272008-04-28 02:13:31 -0700187{
188 unsigned long *usemap, mapsize, section_nr, i;
189 struct mem_section *ms;
190 struct page *page, *memmap;
191
Yasunori Goto04753272008-04-28 02:13:31 -0700192 section_nr = pfn_to_section_nr(start_pfn);
193 ms = __nr_to_section(section_nr);
194
195 /* Get section's memmap address */
196 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
197
198 /*
199 * Get page for the memmap's phys address
200 * XXX: need more consideration for sparse_vmemmap...
201 */
202 page = virt_to_page(memmap);
203 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
204 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
205
206 /* remember memmap's page */
207 for (i = 0; i < mapsize; i++, page++)
208 get_page_bootmem(section_nr, page, SECTION_INFO);
209
210 usemap = __nr_to_section(section_nr)->pageblock_flags;
211 page = virt_to_page(usemap);
212
213 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
214
215 for (i = 0; i < mapsize; i++, page++)
Yasunori Gotoaf370fb2008-07-23 21:28:17 -0700216 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
Yasunori Goto04753272008-04-28 02:13:31 -0700217
218}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800219#else /* CONFIG_SPARSEMEM_VMEMMAP */
220static void register_page_bootmem_info_section(unsigned long start_pfn)
221{
222 unsigned long *usemap, mapsize, section_nr, i;
223 struct mem_section *ms;
224 struct page *page, *memmap;
225
226 if (!pfn_valid(start_pfn))
227 return;
228
229 section_nr = pfn_to_section_nr(start_pfn);
230 ms = __nr_to_section(section_nr);
231
232 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
233
234 register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
235
236 usemap = __nr_to_section(section_nr)->pageblock_flags;
237 page = virt_to_page(usemap);
238
239 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
240
241 for (i = 0; i < mapsize; i++, page++)
242 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
243}
244#endif /* !CONFIG_SPARSEMEM_VMEMMAP */
Yasunori Goto04753272008-04-28 02:13:31 -0700245
246void register_page_bootmem_info_node(struct pglist_data *pgdat)
247{
248 unsigned long i, pfn, end_pfn, nr_pages;
249 int node = pgdat->node_id;
250 struct page *page;
251 struct zone *zone;
252
253 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
254 page = virt_to_page(pgdat);
255
256 for (i = 0; i < nr_pages; i++, page++)
257 get_page_bootmem(node, page, NODE_INFO);
258
259 zone = &pgdat->node_zones[0];
260 for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
Xishi Qiu139c2d72013-09-11 14:21:46 -0700261 if (zone_is_initialized(zone)) {
Yasunori Goto04753272008-04-28 02:13:31 -0700262 nr_pages = zone->wait_table_hash_nr_entries
263 * sizeof(wait_queue_head_t);
264 nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
265 page = virt_to_page(zone->wait_table);
266
267 for (i = 0; i < nr_pages; i++, page++)
268 get_page_bootmem(node, page, NODE_INFO);
269 }
270 }
271
272 pfn = pgdat->node_start_pfn;
Cody P Schaferc1f19492013-02-22 16:35:32 -0800273 end_pfn = pgdat_end_pfn(pgdat);
Yasunori Goto04753272008-04-28 02:13:31 -0700274
Tang Chen7e9f5eb2013-07-08 16:00:23 -0700275 /* register section info */
qiuxishif14851a2012-09-17 14:09:24 -0700276 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
277 /*
278 * Some platforms can assign the same pfn to multiple nodes - on
279 * node0 as well as nodeN. To avoid registering a pfn against
280 * multiple nodes we check that this pfn does not already
Tang Chen7e9f5eb2013-07-08 16:00:23 -0700281 * reside in some other nodes.
qiuxishif14851a2012-09-17 14:09:24 -0700282 */
283 if (pfn_valid(pfn) && (pfn_to_nid(pfn) == node))
284 register_page_bootmem_info_section(pfn);
285 }
Yasunori Goto04753272008-04-28 02:13:31 -0700286}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800287#endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
Yasunori Goto04753272008-04-28 02:13:31 -0700288
Fabian Frederickf2765402014-08-06 16:04:57 -0700289static void __meminit grow_zone_span(struct zone *zone, unsigned long start_pfn,
290 unsigned long end_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700291{
292 unsigned long old_zone_end_pfn;
293
294 zone_span_writelock(zone);
295
Xishi Qiuc33bc312013-09-11 14:21:44 -0700296 old_zone_end_pfn = zone_end_pfn(zone);
Xishi Qiu8080fc02013-09-11 14:21:45 -0700297 if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700298 zone->zone_start_pfn = start_pfn;
299
300 zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
301 zone->zone_start_pfn;
302
303 zone_span_writeunlock(zone);
304}
305
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800306static void resize_zone(struct zone *zone, unsigned long start_pfn,
307 unsigned long end_pfn)
308{
309 zone_span_writelock(zone);
310
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800311 if (end_pfn - start_pfn) {
312 zone->zone_start_pfn = start_pfn;
313 zone->spanned_pages = end_pfn - start_pfn;
314 } else {
315 /*
316 * make it consist as free_area_init_core(),
317 * if spanned_pages = 0, then keep start_pfn = 0
318 */
319 zone->zone_start_pfn = 0;
320 zone->spanned_pages = 0;
321 }
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800322
323 zone_span_writeunlock(zone);
324}
325
326static void fix_zone_id(struct zone *zone, unsigned long start_pfn,
327 unsigned long end_pfn)
328{
329 enum zone_type zid = zone_idx(zone);
330 int nid = zone->zone_pgdat->node_id;
331 unsigned long pfn;
332
333 for (pfn = start_pfn; pfn < end_pfn; pfn++)
334 set_page_links(pfn_to_page(pfn), zid, nid, pfn);
335}
336
Cody P Schaferf6bbb782013-02-22 16:35:30 -0800337/* Can fail with -ENOMEM from allocating a wait table with vmalloc() or
Santosh Shilimkar9e43aa22014-01-21 15:50:43 -0800338 * alloc_bootmem_node_nopanic()/memblock_virt_alloc_node_nopanic() */
Cody P Schaferf6bbb782013-02-22 16:35:30 -0800339static int __ref ensure_zone_is_initialized(struct zone *zone,
340 unsigned long start_pfn, unsigned long num_pages)
341{
342 if (!zone_is_initialized(zone))
Yaowei Baib171e402015-11-05 18:47:06 -0800343 return init_currently_empty_zone(zone, start_pfn, num_pages);
344
Cody P Schaferf6bbb782013-02-22 16:35:30 -0800345 return 0;
346}
347
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800348static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800349 unsigned long start_pfn, unsigned long end_pfn)
350{
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800351 int ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800352 unsigned long flags;
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800353 unsigned long z1_start_pfn;
354
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800355 ret = ensure_zone_is_initialized(z1, start_pfn, end_pfn - start_pfn);
356 if (ret)
357 return ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800358
359 pgdat_resize_lock(z1->zone_pgdat, &flags);
360
361 /* can't move pfns which are higher than @z2 */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800362 if (end_pfn > zone_end_pfn(z2))
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800363 goto out_fail;
Jiang Liu834405c2013-07-03 15:03:04 -0700364 /* the move out part must be at the left most of @z2 */
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800365 if (start_pfn > z2->zone_start_pfn)
366 goto out_fail;
367 /* must included/overlap */
368 if (end_pfn <= z2->zone_start_pfn)
369 goto out_fail;
370
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800371 /* use start_pfn for z1's start_pfn if z1 is empty */
Xishi Qiu8080fc02013-09-11 14:21:45 -0700372 if (!zone_is_empty(z1))
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800373 z1_start_pfn = z1->zone_start_pfn;
374 else
375 z1_start_pfn = start_pfn;
376
377 resize_zone(z1, z1_start_pfn, end_pfn);
Cody P Schafer108bcc92013-02-22 16:35:23 -0800378 resize_zone(z2, end_pfn, zone_end_pfn(z2));
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800379
380 pgdat_resize_unlock(z1->zone_pgdat, &flags);
381
382 fix_zone_id(z1, start_pfn, end_pfn);
383
384 return 0;
385out_fail:
386 pgdat_resize_unlock(z1->zone_pgdat, &flags);
387 return -1;
388}
389
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800390static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800391 unsigned long start_pfn, unsigned long end_pfn)
392{
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800393 int ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800394 unsigned long flags;
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800395 unsigned long z2_end_pfn;
396
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800397 ret = ensure_zone_is_initialized(z2, start_pfn, end_pfn - start_pfn);
398 if (ret)
399 return ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800400
401 pgdat_resize_lock(z1->zone_pgdat, &flags);
402
403 /* can't move pfns which are lower than @z1 */
404 if (z1->zone_start_pfn > start_pfn)
405 goto out_fail;
406 /* the move out part mast at the right most of @z1 */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800407 if (zone_end_pfn(z1) > end_pfn)
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800408 goto out_fail;
409 /* must included/overlap */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800410 if (start_pfn >= zone_end_pfn(z1))
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800411 goto out_fail;
412
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800413 /* use end_pfn for z2's end_pfn if z2 is empty */
Xishi Qiu8080fc02013-09-11 14:21:45 -0700414 if (!zone_is_empty(z2))
Cody P Schafer108bcc92013-02-22 16:35:23 -0800415 z2_end_pfn = zone_end_pfn(z2);
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800416 else
417 z2_end_pfn = end_pfn;
418
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800419 resize_zone(z1, z1->zone_start_pfn, start_pfn);
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800420 resize_zone(z2, start_pfn, z2_end_pfn);
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800421
422 pgdat_resize_unlock(z1->zone_pgdat, &flags);
423
424 fix_zone_id(z2, start_pfn, end_pfn);
425
426 return 0;
427out_fail:
428 pgdat_resize_unlock(z1->zone_pgdat, &flags);
429 return -1;
430}
431
Fabian Frederickf2765402014-08-06 16:04:57 -0700432static void __meminit grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
433 unsigned long end_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700434{
Xishi Qiu83285c72013-11-12 15:07:19 -0800435 unsigned long old_pgdat_end_pfn = pgdat_end_pfn(pgdat);
Heiko Carstens76cdd582008-05-14 16:05:52 -0700436
Tang Chen712cd382012-12-11 16:01:07 -0800437 if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700438 pgdat->node_start_pfn = start_pfn;
439
440 pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
441 pgdat->node_start_pfn;
442}
443
Al Viro31168482008-11-22 17:33:24 +0000444static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700445{
446 struct pglist_data *pgdat = zone->zone_pgdat;
447 int nr_pages = PAGES_PER_SECTION;
448 int nid = pgdat->node_id;
449 int zone_type;
Mel Gormane298ff72015-08-06 15:46:51 -0700450 unsigned long flags, pfn;
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800451 int ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700452
453 zone_type = zone - pgdat->node_zones;
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800454 ret = ensure_zone_is_initialized(zone, phys_start_pfn, nr_pages);
455 if (ret)
456 return ret;
Heiko Carstens76cdd582008-05-14 16:05:52 -0700457
Heiko Carstens76cdd582008-05-14 16:05:52 -0700458 pgdat_resize_lock(zone->zone_pgdat, &flags);
459 grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
460 grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
461 phys_start_pfn + nr_pages);
462 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Dave Hansena2f3aa022007-01-10 23:15:30 -0800463 memmap_init_zone(nr_pages, nid, zone_type,
464 phys_start_pfn, MEMMAP_HOTPLUG);
Mel Gormane298ff72015-08-06 15:46:51 -0700465
466 /* online_page_range is called later and expects pages reserved */
467 for (pfn = phys_start_pfn; pfn < phys_start_pfn + nr_pages; pfn++) {
468 if (!pfn_valid(pfn))
469 continue;
470
471 SetPageReserved(pfn_to_page(pfn));
472 }
Yasunori Goto718127c2006-06-23 02:03:10 -0700473 return 0;
Dave Hansen3947be12005-10-29 18:16:54 -0700474}
475
Gary Hadec04fc582009-01-06 14:39:14 -0800476static int __meminit __add_section(int nid, struct zone *zone,
477 unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700478{
Dave Hansen3947be12005-10-29 18:16:54 -0700479 int ret;
480
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700481 if (pfn_valid(phys_start_pfn))
482 return -EEXIST;
483
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800484 ret = sparse_add_one_section(zone, phys_start_pfn);
Dave Hansen3947be12005-10-29 18:16:54 -0700485
486 if (ret < 0)
487 return ret;
488
Yasunori Goto718127c2006-06-23 02:03:10 -0700489 ret = __add_zone(zone, phys_start_pfn);
490
491 if (ret < 0)
492 return ret;
493
Gary Hadec04fc582009-01-06 14:39:14 -0800494 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
Dave Hansen3947be12005-10-29 18:16:54 -0700495}
496
David Rientjes4edd7ce2013-04-29 15:08:22 -0700497/*
498 * Reasonably generic function for adding memory. It is
499 * expected that archs that support memory hotplug will
500 * call this function after deciding the zone to which to
501 * add the new pages.
502 */
503int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
504 unsigned long nr_pages)
505{
506 unsigned long i;
507 int err = 0;
508 int start_sec, end_sec;
509 /* during initialize mem_map, align hot-added range to section */
510 start_sec = pfn_to_section_nr(phys_start_pfn);
511 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
512
513 for (i = start_sec; i <= end_sec; i++) {
Sheng Yong19c07d52015-04-14 15:44:54 -0700514 err = __add_section(nid, zone, section_nr_to_pfn(i));
David Rientjes4edd7ce2013-04-29 15:08:22 -0700515
516 /*
517 * EEXIST is finally dealt with by ioresource collision
518 * check. see add_memory() => register_memory_resource()
519 * Warning will be printed if there is collision.
520 */
521 if (err && (err != -EEXIST))
522 break;
523 err = 0;
524 }
Zhu Guihuac435a392015-06-24 16:58:42 -0700525 vmemmap_populate_print_last();
David Rientjes4edd7ce2013-04-29 15:08:22 -0700526
527 return err;
528}
529EXPORT_SYMBOL_GPL(__add_pages);
530
531#ifdef CONFIG_MEMORY_HOTREMOVE
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800532/* find the smallest valid pfn in the range [start_pfn, end_pfn) */
533static int find_smallest_section_pfn(int nid, struct zone *zone,
534 unsigned long start_pfn,
535 unsigned long end_pfn)
536{
537 struct mem_section *ms;
538
539 for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
540 ms = __pfn_to_section(start_pfn);
541
542 if (unlikely(!valid_section(ms)))
543 continue;
544
545 if (unlikely(pfn_to_nid(start_pfn) != nid))
546 continue;
547
548 if (zone && zone != page_zone(pfn_to_page(start_pfn)))
549 continue;
550
551 return start_pfn;
552 }
553
554 return 0;
555}
556
557/* find the biggest valid pfn in the range [start_pfn, end_pfn). */
558static int find_biggest_section_pfn(int nid, struct zone *zone,
559 unsigned long start_pfn,
560 unsigned long end_pfn)
561{
562 struct mem_section *ms;
563 unsigned long pfn;
564
565 /* pfn is the end pfn of a memory section. */
566 pfn = end_pfn - 1;
567 for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
568 ms = __pfn_to_section(pfn);
569
570 if (unlikely(!valid_section(ms)))
571 continue;
572
573 if (unlikely(pfn_to_nid(pfn) != nid))
574 continue;
575
576 if (zone && zone != page_zone(pfn_to_page(pfn)))
577 continue;
578
579 return pfn;
580 }
581
582 return 0;
583}
584
585static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
586 unsigned long end_pfn)
587{
Xishi Qiuc33bc312013-09-11 14:21:44 -0700588 unsigned long zone_start_pfn = zone->zone_start_pfn;
589 unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
590 unsigned long zone_end_pfn = z;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800591 unsigned long pfn;
592 struct mem_section *ms;
593 int nid = zone_to_nid(zone);
594
595 zone_span_writelock(zone);
596 if (zone_start_pfn == start_pfn) {
597 /*
598 * If the section is smallest section in the zone, it need
599 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
600 * In this case, we find second smallest valid mem_section
601 * for shrinking zone.
602 */
603 pfn = find_smallest_section_pfn(nid, zone, end_pfn,
604 zone_end_pfn);
605 if (pfn) {
606 zone->zone_start_pfn = pfn;
607 zone->spanned_pages = zone_end_pfn - pfn;
608 }
609 } else if (zone_end_pfn == end_pfn) {
610 /*
611 * If the section is biggest section in the zone, it need
612 * shrink zone->spanned_pages.
613 * In this case, we find second biggest valid mem_section for
614 * shrinking zone.
615 */
616 pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
617 start_pfn);
618 if (pfn)
619 zone->spanned_pages = pfn - zone_start_pfn + 1;
620 }
621
622 /*
623 * The section is not biggest or smallest mem_section in the zone, it
624 * only creates a hole in the zone. So in this case, we need not
625 * change the zone. But perhaps, the zone has only hole data. Thus
626 * it check the zone has only hole or not.
627 */
628 pfn = zone_start_pfn;
629 for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
630 ms = __pfn_to_section(pfn);
631
632 if (unlikely(!valid_section(ms)))
633 continue;
634
635 if (page_zone(pfn_to_page(pfn)) != zone)
636 continue;
637
638 /* If the section is current section, it continues the loop */
639 if (start_pfn == pfn)
640 continue;
641
642 /* If we find valid section, we have nothing to do */
643 zone_span_writeunlock(zone);
644 return;
645 }
646
647 /* The zone has no valid section */
648 zone->zone_start_pfn = 0;
649 zone->spanned_pages = 0;
650 zone_span_writeunlock(zone);
651}
652
653static void shrink_pgdat_span(struct pglist_data *pgdat,
654 unsigned long start_pfn, unsigned long end_pfn)
655{
Xishi Qiu83285c72013-11-12 15:07:19 -0800656 unsigned long pgdat_start_pfn = pgdat->node_start_pfn;
657 unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */
658 unsigned long pgdat_end_pfn = p;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800659 unsigned long pfn;
660 struct mem_section *ms;
661 int nid = pgdat->node_id;
662
663 if (pgdat_start_pfn == start_pfn) {
664 /*
665 * If the section is smallest section in the pgdat, it need
666 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
667 * In this case, we find second smallest valid mem_section
668 * for shrinking zone.
669 */
670 pfn = find_smallest_section_pfn(nid, NULL, end_pfn,
671 pgdat_end_pfn);
672 if (pfn) {
673 pgdat->node_start_pfn = pfn;
674 pgdat->node_spanned_pages = pgdat_end_pfn - pfn;
675 }
676 } else if (pgdat_end_pfn == end_pfn) {
677 /*
678 * If the section is biggest section in the pgdat, it need
679 * shrink pgdat->node_spanned_pages.
680 * In this case, we find second biggest valid mem_section for
681 * shrinking zone.
682 */
683 pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn,
684 start_pfn);
685 if (pfn)
686 pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1;
687 }
688
689 /*
690 * If the section is not biggest or smallest mem_section in the pgdat,
691 * it only creates a hole in the pgdat. So in this case, we need not
692 * change the pgdat.
693 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
694 * has only hole or not.
695 */
696 pfn = pgdat_start_pfn;
697 for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) {
698 ms = __pfn_to_section(pfn);
699
700 if (unlikely(!valid_section(ms)))
701 continue;
702
703 if (pfn_to_nid(pfn) != nid)
704 continue;
705
706 /* If the section is current section, it continues the loop */
707 if (start_pfn == pfn)
708 continue;
709
710 /* If we find valid section, we have nothing to do */
711 return;
712 }
713
714 /* The pgdat has no valid section */
715 pgdat->node_start_pfn = 0;
716 pgdat->node_spanned_pages = 0;
717}
718
719static void __remove_zone(struct zone *zone, unsigned long start_pfn)
720{
721 struct pglist_data *pgdat = zone->zone_pgdat;
722 int nr_pages = PAGES_PER_SECTION;
723 int zone_type;
724 unsigned long flags;
725
726 zone_type = zone - pgdat->node_zones;
727
728 pgdat_resize_lock(zone->zone_pgdat, &flags);
729 shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
730 shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages);
731 pgdat_resize_unlock(zone->zone_pgdat, &flags);
732}
733
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700734static int __remove_section(struct zone *zone, struct mem_section *ms)
735{
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800736 unsigned long start_pfn;
737 int scn_nr;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700738 int ret = -EINVAL;
739
740 if (!valid_section(ms))
741 return ret;
742
743 ret = unregister_memory_section(ms);
744 if (ret)
745 return ret;
746
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800747 scn_nr = __section_nr(ms);
748 start_pfn = section_nr_to_pfn(scn_nr);
749 __remove_zone(zone, start_pfn);
750
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700751 sparse_remove_one_section(zone, ms);
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700752 return 0;
753}
754
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700755/**
756 * __remove_pages() - remove sections of pages from a zone
757 * @zone: zone from which pages need to be removed
758 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
759 * @nr_pages: number of pages to remove (must be multiple of section size)
760 *
761 * Generic helper function to remove section mappings and sysfs entries
762 * for the section of the memory we are removing. Caller needs to make
763 * sure that pages are marked reserved and zones are adjust properly by
764 * calling offline_pages().
765 */
766int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
767 unsigned long nr_pages)
768{
Toshi Kanife74ebb2013-04-29 15:08:20 -0700769 unsigned long i;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700770 int sections_to_remove;
Toshi Kanife74ebb2013-04-29 15:08:20 -0700771 resource_size_t start, size;
772 int ret = 0;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700773
774 /*
775 * We can only remove entire sections
776 */
777 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
778 BUG_ON(nr_pages % PAGES_PER_SECTION);
779
Toshi Kanife74ebb2013-04-29 15:08:20 -0700780 start = phys_start_pfn << PAGE_SHIFT;
781 size = nr_pages * PAGE_SIZE;
Dan Williams033fbae2015-08-09 15:29:06 -0400782
783 /* in the ZONE_DEVICE case device driver owns the memory region */
784 if (!is_dev_zone(zone))
785 ret = release_mem_region_adjustable(&iomem_resource, start, size);
Randy Dunlap348f9f02013-05-24 15:55:30 -0700786 if (ret) {
787 resource_size_t endres = start + size - 1;
788
789 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
790 &start, &endres, ret);
791 }
Yasuaki Ishimatsud760afd2012-10-08 16:34:14 -0700792
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700793 sections_to_remove = nr_pages / PAGES_PER_SECTION;
794 for (i = 0; i < sections_to_remove; i++) {
795 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
796 ret = __remove_section(zone, __pfn_to_section(pfn));
797 if (ret)
798 break;
799 }
800 return ret;
801}
802EXPORT_SYMBOL_GPL(__remove_pages);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700803#endif /* CONFIG_MEMORY_HOTREMOVE */
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700804
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700805int set_online_page_callback(online_page_callback_t callback)
806{
807 int rc = -EINVAL;
808
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700809 get_online_mems();
810 mutex_lock(&online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700811
812 if (online_page_callback == generic_online_page) {
813 online_page_callback = callback;
814 rc = 0;
815 }
816
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700817 mutex_unlock(&online_page_callback_lock);
818 put_online_mems();
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700819
820 return rc;
821}
822EXPORT_SYMBOL_GPL(set_online_page_callback);
823
824int restore_online_page_callback(online_page_callback_t callback)
825{
826 int rc = -EINVAL;
827
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700828 get_online_mems();
829 mutex_lock(&online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700830
831 if (online_page_callback == callback) {
832 online_page_callback = generic_online_page;
833 rc = 0;
834 }
835
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700836 mutex_unlock(&online_page_callback_lock);
837 put_online_mems();
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700838
839 return rc;
840}
841EXPORT_SYMBOL_GPL(restore_online_page_callback);
842
843void __online_page_set_limits(struct page *page)
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700844{
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700845}
846EXPORT_SYMBOL_GPL(__online_page_set_limits);
847
848void __online_page_increment_counters(struct page *page)
849{
Jiang Liu3dcc0572013-07-03 15:03:21 -0700850 adjust_managed_page_count(page, 1);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700851}
852EXPORT_SYMBOL_GPL(__online_page_increment_counters);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700853
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700854void __online_page_free(struct page *page)
855{
Jiang Liu3dcc0572013-07-03 15:03:21 -0700856 __free_reserved_page(page);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700857}
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700858EXPORT_SYMBOL_GPL(__online_page_free);
859
860static void generic_online_page(struct page *page)
861{
862 __online_page_set_limits(page);
863 __online_page_increment_counters(page);
864 __online_page_free(page);
865}
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700866
KAMEZAWA Hiroyuki75884fb12007-10-16 01:26:10 -0700867static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
868 void *arg)
Dave Hansen3947be12005-10-29 18:16:54 -0700869{
870 unsigned long i;
KAMEZAWA Hiroyuki75884fb12007-10-16 01:26:10 -0700871 unsigned long onlined_pages = *(unsigned long *)arg;
872 struct page *page;
873 if (PageReserved(pfn_to_page(start_pfn)))
874 for (i = 0; i < nr_pages; i++) {
875 page = pfn_to_page(start_pfn + i);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700876 (*online_page_callback)(page);
KAMEZAWA Hiroyuki75884fb12007-10-16 01:26:10 -0700877 onlined_pages++;
878 }
879 *(unsigned long *)arg = onlined_pages;
880 return 0;
881}
882
Lai Jiangshan09285af2012-12-12 13:52:04 -0800883#ifdef CONFIG_MOVABLE_NODE
Tang Chen79a4dce2012-12-18 14:23:24 -0800884/*
885 * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
886 * normal memory.
887 */
Lai Jiangshan09285af2012-12-12 13:52:04 -0800888static bool can_online_high_movable(struct zone *zone)
889{
890 return true;
891}
Tang Chen79a4dce2012-12-18 14:23:24 -0800892#else /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800893/* ensure every online node has NORMAL memory */
894static bool can_online_high_movable(struct zone *zone)
895{
896 return node_state(zone_to_nid(zone), N_NORMAL_MEMORY);
897}
Tang Chen79a4dce2012-12-18 14:23:24 -0800898#endif /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800899
Lai Jiangshand9713672012-12-11 16:01:03 -0800900/* check which state of node_states will be changed when online memory */
901static void node_states_check_changes_online(unsigned long nr_pages,
902 struct zone *zone, struct memory_notify *arg)
903{
904 int nid = zone_to_nid(zone);
905 enum zone_type zone_last = ZONE_NORMAL;
906
907 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800908 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
909 * contains nodes which have zones of 0...ZONE_NORMAL,
910 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -0800911 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800912 * If we don't have HIGHMEM nor movable node,
913 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
914 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -0800915 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800916 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -0800917 zone_last = ZONE_MOVABLE;
918
919 /*
920 * if the memory to be online is in a zone of 0...zone_last, and
921 * the zones of 0...zone_last don't have memory before online, we will
922 * need to set the node to node_states[N_NORMAL_MEMORY] after
923 * the memory is online.
924 */
925 if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
926 arg->status_change_nid_normal = nid;
927 else
928 arg->status_change_nid_normal = -1;
929
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800930#ifdef CONFIG_HIGHMEM
931 /*
932 * If we have movable node, node_states[N_HIGH_MEMORY]
933 * contains nodes which have zones of 0...ZONE_HIGHMEM,
934 * set zone_last to ZONE_HIGHMEM.
935 *
936 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
937 * contains nodes which have zones of 0...ZONE_MOVABLE,
938 * set zone_last to ZONE_MOVABLE.
939 */
940 zone_last = ZONE_HIGHMEM;
941 if (N_MEMORY == N_HIGH_MEMORY)
942 zone_last = ZONE_MOVABLE;
943
944 if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
945 arg->status_change_nid_high = nid;
946 else
947 arg->status_change_nid_high = -1;
948#else
949 arg->status_change_nid_high = arg->status_change_nid_normal;
950#endif
951
Lai Jiangshand9713672012-12-11 16:01:03 -0800952 /*
953 * if the node don't have memory befor online, we will need to
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800954 * set the node to node_states[N_MEMORY] after the memory
Lai Jiangshand9713672012-12-11 16:01:03 -0800955 * is online.
956 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800957 if (!node_state(nid, N_MEMORY))
Lai Jiangshand9713672012-12-11 16:01:03 -0800958 arg->status_change_nid = nid;
959 else
960 arg->status_change_nid = -1;
961}
962
963static void node_states_set_node(int node, struct memory_notify *arg)
964{
965 if (arg->status_change_nid_normal >= 0)
966 node_set_state(node, N_NORMAL_MEMORY);
967
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800968 if (arg->status_change_nid_high >= 0)
969 node_set_state(node, N_HIGH_MEMORY);
970
971 node_set_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -0800972}
973
KAMEZAWA Hiroyuki75884fb12007-10-16 01:26:10 -0700974
David Rientjes30467e02015-04-14 15:45:11 -0700975/* Must be protected by mem_hotplug_begin() */
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800976int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
KAMEZAWA Hiroyuki75884fb12007-10-16 01:26:10 -0700977{
Cody P Schaferaa472282013-07-03 15:02:10 -0700978 unsigned long flags;
Dave Hansen3947be12005-10-29 18:16:54 -0700979 unsigned long onlined_pages = 0;
980 struct zone *zone;
Yasunori Goto68113782006-06-23 02:03:11 -0700981 int need_zonelists_rebuild = 0;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700982 int nid;
983 int ret;
984 struct memory_notify arg;
Dave Hansen3947be12005-10-29 18:16:54 -0700985
Lai Jiangshand9713672012-12-11 16:01:03 -0800986 /*
987 * This doesn't need a lock to do pfn_to_page().
988 * The section can't be removed here because of the
989 * memory_block->state_mutex.
990 */
991 zone = page_zone(pfn_to_page(pfn));
992
Tang Chen4f7c6b42014-08-06 16:05:13 -0700993 if ((zone_idx(zone) > ZONE_NORMAL ||
994 online_type == MMOP_ONLINE_MOVABLE) &&
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700995 !can_online_high_movable(zone))
David Rientjes30467e02015-04-14 15:45:11 -0700996 return -EINVAL;
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800997
Tang Chen4f7c6b42014-08-06 16:05:13 -0700998 if (online_type == MMOP_ONLINE_KERNEL &&
999 zone_idx(zone) == ZONE_MOVABLE) {
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001000 if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages))
David Rientjes30467e02015-04-14 15:45:11 -07001001 return -EINVAL;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -08001002 }
Tang Chen4f7c6b42014-08-06 16:05:13 -07001003 if (online_type == MMOP_ONLINE_MOVABLE &&
1004 zone_idx(zone) == ZONE_MOVABLE - 1) {
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001005 if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages))
David Rientjes30467e02015-04-14 15:45:11 -07001006 return -EINVAL;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -08001007 }
1008
1009 /* Previous code may changed the zone of the pfn range */
1010 zone = page_zone(pfn_to_page(pfn));
1011
Yasunori Goto7b78d332007-10-21 16:41:36 -07001012 arg.start_pfn = pfn;
1013 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -08001014 node_states_check_changes_online(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001015
Xishi Qiu9c2606b2013-11-12 15:07:21 -08001016 nid = pfn_to_nid(pfn);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001017
1018 ret = memory_notify(MEM_GOING_ONLINE, &arg);
1019 ret = notifier_to_errno(ret);
1020 if (ret) {
1021 memory_notify(MEM_CANCEL_ONLINE, &arg);
David Rientjes30467e02015-04-14 15:45:11 -07001022 return ret;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001023 }
Dave Hansen3947be12005-10-29 18:16:54 -07001024 /*
Yasunori Goto68113782006-06-23 02:03:11 -07001025 * If this zone is not populated, then it is not in zonelist.
1026 * This means the page allocator ignores this zone.
1027 * So, zonelist must be updated after online.
1028 */
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001029 mutex_lock(&zonelists_mutex);
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001030 if (!populated_zone(zone)) {
Yasunori Goto68113782006-06-23 02:03:11 -07001031 need_zonelists_rebuild = 1;
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001032 build_all_zonelists(NULL, zone);
1033 }
Yasunori Goto68113782006-06-23 02:03:11 -07001034
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001035 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
KAMEZAWA Hiroyuki75884fb12007-10-16 01:26:10 -07001036 online_pages_range);
Geoff Levandfd8a4222008-05-14 16:05:50 -07001037 if (ret) {
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001038 if (need_zonelists_rebuild)
1039 zone_pcp_reset(zone);
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001040 mutex_unlock(&zonelists_mutex);
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07001041 printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n",
1042 (unsigned long long) pfn << PAGE_SHIFT,
1043 (((unsigned long long) pfn + nr_pages)
1044 << PAGE_SHIFT) - 1);
Geoff Levandfd8a4222008-05-14 16:05:50 -07001045 memory_notify(MEM_CANCEL_ONLINE, &arg);
David Rientjes30467e02015-04-14 15:45:11 -07001046 return ret;
Geoff Levandfd8a4222008-05-14 16:05:50 -07001047 }
1048
Dave Hansen3947be12005-10-29 18:16:54 -07001049 zone->present_pages += onlined_pages;
Cody P Schaferaa472282013-07-03 15:02:10 -07001050
1051 pgdat_resize_lock(zone->zone_pgdat, &flags);
Yasunori Gotof2937be2006-03-09 17:33:51 -08001052 zone->zone_pgdat->node_present_pages += onlined_pages;
Cody P Schaferaa472282013-07-03 15:02:10 -07001053 pgdat_resize_unlock(zone->zone_pgdat, &flags);
1054
Jiang Liu08dff7b2012-07-31 16:43:30 -07001055 if (onlined_pages) {
Lai Jiangshand9713672012-12-11 16:01:03 -08001056 node_states_set_node(zone_to_nid(zone), &arg);
Jiang Liu08dff7b2012-07-31 16:43:30 -07001057 if (need_zonelists_rebuild)
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001058 build_all_zonelists(NULL, NULL);
Jiang Liu08dff7b2012-07-31 16:43:30 -07001059 else
1060 zone_pcp_update(zone);
1061 }
Dave Hansen3947be12005-10-29 18:16:54 -07001062
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001063 mutex_unlock(&zonelists_mutex);
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07001064
1065 init_per_zone_wmark_min();
1066
Jiang Liu08dff7b2012-07-31 16:43:30 -07001067 if (onlined_pages)
Christoph Lameter7ea15302007-10-16 01:25:29 -07001068 kswapd_run(zone_to_nid(zone));
Dave Hansen61b13992005-10-29 18:16:56 -07001069
Haicheng Li1f522502010-05-24 14:32:51 -07001070 vm_total_pages = nr_free_pagecache_pages();
Kent Liu2f7f24e2008-07-23 21:28:18 -07001071
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -07001072 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001073
1074 if (onlined_pages)
1075 memory_notify(MEM_ONLINE, &arg);
David Rientjes30467e02015-04-14 15:45:11 -07001076 return 0;
Dave Hansen3947be12005-10-29 18:16:54 -07001077}
Keith Mannthey53947022006-09-30 23:27:08 -07001078#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
Yasunori Gotobc02af92006-06-27 02:53:30 -07001079
Tang Chen0bd85422014-11-13 15:19:41 -08001080static void reset_node_present_pages(pg_data_t *pgdat)
1081{
1082 struct zone *z;
1083
1084 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
1085 z->present_pages = 0;
1086
1087 pgdat->node_present_pages = 0;
1088}
1089
Hidetoshi Setoe1319332009-11-17 14:06:18 -08001090/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1091static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001092{
1093 struct pglist_data *pgdat;
1094 unsigned long zones_size[MAX_NR_ZONES] = {0};
1095 unsigned long zholes_size[MAX_NR_ZONES] = {0};
Fabian Frederickc8e861a2014-06-04 16:07:51 -07001096 unsigned long start_pfn = PFN_DOWN(start);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001097
Tang Chena1e565a2013-02-22 16:33:18 -08001098 pgdat = NODE_DATA(nid);
1099 if (!pgdat) {
1100 pgdat = arch_alloc_nodedata(nid);
1101 if (!pgdat)
1102 return NULL;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001103
Tang Chena1e565a2013-02-22 16:33:18 -08001104 arch_refresh_nodedata(nid, pgdat);
Gu Zhengb0dc3a32015-03-25 15:55:20 -07001105 } else {
1106 /* Reset the nr_zones and classzone_idx to 0 before reuse */
1107 pgdat->nr_zones = 0;
1108 pgdat->classzone_idx = 0;
Tang Chena1e565a2013-02-22 16:33:18 -08001109 }
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001110
1111 /* we can use NODE_DATA(nid) from here */
1112
1113 /* init node's zones as empty zones, we don't have any present pages.*/
Johannes Weiner9109fb72008-07-23 21:27:20 -07001114 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001115
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -07001116 /*
1117 * The node we allocated has no zone fallback lists. For avoiding
1118 * to access not-initialized zonelist, build here.
1119 */
David Rientjesf957db42011-06-22 18:13:04 -07001120 mutex_lock(&zonelists_mutex);
Jiang Liu9adb62a2012-07-31 16:43:28 -07001121 build_all_zonelists(pgdat, NULL);
David Rientjesf957db42011-06-22 18:13:04 -07001122 mutex_unlock(&zonelists_mutex);
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -07001123
Tang Chenf784a3f2014-11-13 15:19:39 -08001124 /*
1125 * zone->managed_pages is set to an approximate value in
1126 * free_area_init_core(), which will cause
1127 * /sys/device/system/node/nodeX/meminfo has wrong data.
1128 * So reset it to 0 before any memory is onlined.
1129 */
1130 reset_node_managed_pages(pgdat);
1131
Tang Chen0bd85422014-11-13 15:19:41 -08001132 /*
1133 * When memory is hot-added, all the memory is in offline state. So
1134 * clear all zones' present_pages because they will be updated in
1135 * online_pages() and offline_pages().
1136 */
1137 reset_node_present_pages(pgdat);
1138
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001139 return pgdat;
1140}
1141
1142static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
1143{
1144 arch_refresh_nodedata(nid, NULL);
1145 arch_free_nodedata(pgdat);
1146 return;
1147}
1148
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -07001149
Toshi Kani01b0f192013-11-12 15:07:25 -08001150/**
1151 * try_online_node - online a node if offlined
1152 *
minskey guocf234222010-05-24 14:32:41 -07001153 * called by cpu_up() to online a node without onlined memory.
1154 */
Toshi Kani01b0f192013-11-12 15:07:25 -08001155int try_online_node(int nid)
minskey guocf234222010-05-24 14:32:41 -07001156{
1157 pg_data_t *pgdat;
1158 int ret;
1159
Toshi Kani01b0f192013-11-12 15:07:25 -08001160 if (node_online(nid))
1161 return 0;
1162
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001163 mem_hotplug_begin();
minskey guocf234222010-05-24 14:32:41 -07001164 pgdat = hotadd_new_pgdat(nid, 0);
David Rientjes7553e8f2011-06-22 18:13:01 -07001165 if (!pgdat) {
Toshi Kani01b0f192013-11-12 15:07:25 -08001166 pr_err("Cannot online node %d due to NULL pgdat\n", nid);
minskey guocf234222010-05-24 14:32:41 -07001167 ret = -ENOMEM;
1168 goto out;
1169 }
1170 node_set_online(nid);
1171 ret = register_one_node(nid);
1172 BUG_ON(ret);
1173
Toshi Kani01b0f192013-11-12 15:07:25 -08001174 if (pgdat->node_zonelists->_zonerefs->zone == NULL) {
1175 mutex_lock(&zonelists_mutex);
1176 build_all_zonelists(NULL, NULL);
1177 mutex_unlock(&zonelists_mutex);
1178 }
1179
minskey guocf234222010-05-24 14:32:41 -07001180out:
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001181 mem_hotplug_done();
minskey guocf234222010-05-24 14:32:41 -07001182 return ret;
1183}
1184
Toshi Kani27356f52013-09-11 14:21:49 -07001185static int check_hotplug_memory_range(u64 start, u64 size)
1186{
Fabian Frederickc8e861a2014-06-04 16:07:51 -07001187 u64 start_pfn = PFN_DOWN(start);
Toshi Kani27356f52013-09-11 14:21:49 -07001188 u64 nr_pages = size >> PAGE_SHIFT;
1189
1190 /* Memory range must be aligned with section */
1191 if ((start_pfn & ~PAGE_SECTION_MASK) ||
1192 (nr_pages % PAGES_PER_SECTION) || (!nr_pages)) {
1193 pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
1194 (unsigned long long)start,
1195 (unsigned long long)size);
1196 return -EINVAL;
1197 }
1198
1199 return 0;
1200}
1201
Wang Nan63264402014-08-06 16:07:36 -07001202/*
1203 * If movable zone has already been setup, newly added memory should be check.
1204 * If its address is higher than movable zone, it should be added as movable.
1205 * Without this check, movable zone may overlap with other zone.
1206 */
1207static int should_add_memory_movable(int nid, u64 start, u64 size)
1208{
1209 unsigned long start_pfn = start >> PAGE_SHIFT;
1210 pg_data_t *pgdat = NODE_DATA(nid);
1211 struct zone *movable_zone = pgdat->node_zones + ZONE_MOVABLE;
1212
1213 if (zone_is_empty(movable_zone))
1214 return 0;
1215
1216 if (movable_zone->zone_start_pfn <= start_pfn)
1217 return 1;
1218
1219 return 0;
1220}
1221
Dan Williams033fbae2015-08-09 15:29:06 -04001222int zone_for_memory(int nid, u64 start, u64 size, int zone_default,
1223 bool for_device)
Wang Nan63264402014-08-06 16:07:36 -07001224{
Dan Williams033fbae2015-08-09 15:29:06 -04001225#ifdef CONFIG_ZONE_DEVICE
1226 if (for_device)
1227 return ZONE_DEVICE;
1228#endif
Wang Nan63264402014-08-06 16:07:36 -07001229 if (should_add_memory_movable(nid, start, size))
1230 return ZONE_MOVABLE;
1231
1232 return zone_default;
1233}
1234
Al Viro31168482008-11-22 17:33:24 +00001235/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
David Vrabel62cedb92015-06-25 16:35:49 +01001236int __ref add_memory_resource(int nid, struct resource *res)
Yasunori Gotobc02af92006-06-27 02:53:30 -07001237{
David Vrabel62cedb92015-06-25 16:35:49 +01001238 u64 start, size;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001239 pg_data_t *pgdat = NULL;
Tang Chena1e565a2013-02-22 16:33:18 -08001240 bool new_pgdat;
1241 bool new_node;
Yasunori Gotobc02af92006-06-27 02:53:30 -07001242 int ret;
1243
David Vrabel62cedb92015-06-25 16:35:49 +01001244 start = res->start;
1245 size = resource_size(res);
1246
Toshi Kani27356f52013-09-11 14:21:49 -07001247 ret = check_hotplug_memory_range(start, size);
1248 if (ret)
1249 return ret;
1250
Tang Chena1e565a2013-02-22 16:33:18 -08001251 { /* Stupid hack to suppress address-never-null warning */
1252 void *p = NODE_DATA(nid);
1253 new_pgdat = !p;
1254 }
Nathan Zimmerac13c462014-01-23 15:53:26 -08001255
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001256 mem_hotplug_begin();
Nathan Zimmerac13c462014-01-23 15:53:26 -08001257
Tang Chen7f36e3e2015-09-04 15:42:32 -07001258 /*
1259 * Add new range to memblock so that when hotadd_new_pgdat() is called
1260 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
1261 * this new range and calculate total pages correctly. The range will
1262 * be removed at hot-remove time.
1263 */
1264 memblock_add_node(start, size, nid);
1265
Tang Chena1e565a2013-02-22 16:33:18 -08001266 new_node = !node_online(nid);
1267 if (new_node) {
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001268 pgdat = hotadd_new_pgdat(nid, start);
Andi Kleen6ad696d2009-11-17 14:06:22 -08001269 ret = -ENOMEM;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001270 if (!pgdat)
Wen Congyang41b9e2d2012-07-11 14:02:31 -07001271 goto error;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001272 }
1273
Yasunori Gotobc02af92006-06-27 02:53:30 -07001274 /* call arch's memory hotadd */
Dan Williams033fbae2015-08-09 15:29:06 -04001275 ret = arch_add_memory(nid, start, size, false);
Yasunori Gotobc02af92006-06-27 02:53:30 -07001276
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001277 if (ret < 0)
1278 goto error;
1279
Yasunori Goto0fc44152006-06-27 02:53:38 -07001280 /* we online node here. we can't roll back from here. */
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001281 node_set_online(nid);
1282
Tang Chena1e565a2013-02-22 16:33:18 -08001283 if (new_node) {
Yasunori Goto0fc44152006-06-27 02:53:38 -07001284 ret = register_one_node(nid);
1285 /*
1286 * If sysfs file of new node can't create, cpu on the node
1287 * can't be hot-added. There is no rollback way now.
1288 * So, check by BUG_ON() to catch it reluctantly..
1289 */
1290 BUG_ON(ret);
1291 }
1292
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -08001293 /* create new memmap entry */
1294 firmware_map_add_hotplug(start, start + size, "System RAM");
1295
Andi Kleen6ad696d2009-11-17 14:06:22 -08001296 goto out;
1297
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001298error:
1299 /* rollback pgdat allocation and others */
1300 if (new_pgdat)
1301 rollback_node_hotadd(nid, pgdat);
Tang Chen7f36e3e2015-09-04 15:42:32 -07001302 memblock_remove(start, size);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001303
Andi Kleen6ad696d2009-11-17 14:06:22 -08001304out:
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001305 mem_hotplug_done();
Yasunori Gotobc02af92006-06-27 02:53:30 -07001306 return ret;
1307}
David Vrabel62cedb92015-06-25 16:35:49 +01001308EXPORT_SYMBOL_GPL(add_memory_resource);
1309
1310int __ref add_memory(int nid, u64 start, u64 size)
1311{
1312 struct resource *res;
1313 int ret;
1314
1315 res = register_memory_resource(start, size);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -08001316 if (IS_ERR(res))
1317 return PTR_ERR(res);
David Vrabel62cedb92015-06-25 16:35:49 +01001318
1319 ret = add_memory_resource(nid, res);
1320 if (ret < 0)
1321 release_memory_resource(res);
1322 return ret;
1323}
Yasunori Gotobc02af92006-06-27 02:53:30 -07001324EXPORT_SYMBOL_GPL(add_memory);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001325
1326#ifdef CONFIG_MEMORY_HOTREMOVE
1327/*
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001328 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1329 * set and the size of the free page is given by page_order(). Using this,
1330 * the function determines if the pageblock contains only free pages.
1331 * Due to buddy contraints, a free page at least the size of a pageblock will
1332 * be located at the start of the pageblock
1333 */
1334static inline int pageblock_free(struct page *page)
1335{
1336 return PageBuddy(page) && page_order(page) >= pageblock_order;
1337}
1338
1339/* Return the start of the next active pageblock after a given page */
1340static struct page *next_active_pageblock(struct page *page)
1341{
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001342 /* Ensure the starting page is pageblock-aligned */
1343 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
1344
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001345 /* If the entire pageblock is free, move to the end of free page */
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -07001346 if (pageblock_free(page)) {
1347 int order;
1348 /* be careful. we don't have locks, page_order can be changed.*/
1349 order = page_order(page);
1350 if ((order < MAX_ORDER) && (order >= pageblock_order))
1351 return page + (1 << order);
1352 }
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001353
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -07001354 return page + pageblock_nr_pages;
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001355}
1356
1357/* Checks if this range of memory is likely to be hot-removable. */
1358int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
1359{
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001360 struct page *page = pfn_to_page(start_pfn);
1361 struct page *end_page = page + nr_pages;
1362
1363 /* Check the starting page of each pageblock within the range */
1364 for (; page < end_page; page = next_active_pageblock(page)) {
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07001365 if (!is_pageblock_removable_nolock(page))
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001366 return 0;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07001367 cond_resched();
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001368 }
1369
1370 /* All pageblocks in the memory block are likely to be hot-removable */
1371 return 1;
1372}
1373
1374/*
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001375 * Confirm all pages in a range [start, end) is belongs to the same zone.
1376 */
Zhang Zhened2f2402014-10-09 15:26:31 -07001377int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001378{
Andrew Banman5f0f2882015-12-29 14:54:25 -08001379 unsigned long pfn, sec_end_pfn;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001380 struct zone *zone = NULL;
1381 struct page *page;
1382 int i;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001383 for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001384 pfn < end_pfn;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001385 pfn = sec_end_pfn + 1, sec_end_pfn += PAGES_PER_SECTION) {
1386 /* Make sure the memory section is present first */
1387 if (!present_section_nr(pfn_to_section_nr(pfn)))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001388 continue;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001389 for (; pfn < sec_end_pfn && pfn < end_pfn;
1390 pfn += MAX_ORDER_NR_PAGES) {
1391 i = 0;
1392 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1393 while ((i < MAX_ORDER_NR_PAGES) &&
1394 !pfn_valid_within(pfn + i))
1395 i++;
1396 if (i == MAX_ORDER_NR_PAGES)
1397 continue;
1398 page = pfn_to_page(pfn + i);
1399 if (zone && page_zone(page) != zone)
1400 return 0;
1401 zone = page_zone(page);
1402 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001403 }
1404 return 1;
1405}
1406
1407/*
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001408 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages
1409 * and hugepages). We scan pfn because it's much easier than scanning over
1410 * linked list. This function returns the pfn of the first found movable
1411 * page if it's found, otherwise 0.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001412 */
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001413static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001414{
1415 unsigned long pfn;
1416 struct page *page;
1417 for (pfn = start; pfn < end; pfn++) {
1418 if (pfn_valid(pfn)) {
1419 page = pfn_to_page(pfn);
1420 if (PageLRU(page))
1421 return pfn;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001422 if (PageHuge(page)) {
Naoya Horiguchi7e1f0492015-04-15 16:14:41 -07001423 if (page_huge_active(page))
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001424 return pfn;
1425 else
1426 pfn = round_up(pfn + 1,
1427 1 << compound_order(page)) - 1;
1428 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001429 }
1430 }
1431 return 0;
1432}
1433
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001434#define NR_OFFLINE_AT_ONCE_PAGES (256)
1435static int
1436do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1437{
1438 unsigned long pfn;
1439 struct page *page;
1440 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
1441 int not_managed = 0;
1442 int ret = 0;
1443 LIST_HEAD(source);
1444
1445 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
1446 if (!pfn_valid(pfn))
1447 continue;
1448 page = pfn_to_page(pfn);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001449
1450 if (PageHuge(page)) {
1451 struct page *head = compound_head(page);
1452 pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
1453 if (compound_order(head) > PFN_SECTION_SHIFT) {
1454 ret = -EBUSY;
1455 break;
1456 }
1457 if (isolate_huge_page(page, &source))
1458 move_pages -= 1 << compound_order(head);
1459 continue;
1460 }
1461
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001462 if (!get_page_unless_zero(page))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001463 continue;
1464 /*
1465 * We can skip free pages. And we can only deal with pages on
1466 * LRU.
1467 */
Nick Piggin62695a82008-10-18 20:26:09 -07001468 ret = isolate_lru_page(page);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001469 if (!ret) { /* Success */
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001470 put_page(page);
Nick Piggin62695a82008-10-18 20:26:09 -07001471 list_add_tail(&page->lru, &source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001472 move_pages--;
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001473 inc_zone_page_state(page, NR_ISOLATED_ANON +
1474 page_is_file_cache(page));
1475
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001476 } else {
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001477#ifdef CONFIG_DEBUG_VM
Wu Fengguang718a3822010-03-10 15:20:43 -08001478 printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
1479 pfn);
Dave Hansenf0b791a2014-01-23 15:52:49 -08001480 dump_page(page, "failed to remove from LRU");
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001481#endif
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001482 put_page(page);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001483 /* Because we don't have big zone->lock. we should
Bob Liu809c4442010-10-26 14:22:10 -07001484 check this again here. */
1485 if (page_count(page)) {
1486 not_managed++;
Bob Liuf3ab2632010-10-26 14:22:10 -07001487 ret = -EBUSY;
Bob Liu809c4442010-10-26 14:22:10 -07001488 break;
1489 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001490 }
1491 }
Bob Liuf3ab2632010-10-26 14:22:10 -07001492 if (!list_empty(&source)) {
1493 if (not_managed) {
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001494 putback_movable_pages(&source);
Bob Liuf3ab2632010-10-26 14:22:10 -07001495 goto out;
1496 }
Minchan Kim74c08f92012-10-08 16:32:54 -07001497
1498 /*
1499 * alloc_migrate_target should be improooooved!!
1500 * migrate_pages returns # of failed pages.
1501 */
David Rientjes68711a72014-06-04 16:08:25 -07001502 ret = migrate_pages(&source, alloc_migrate_target, NULL, 0,
Hugh Dickins9c620e22013-02-22 16:35:14 -08001503 MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
Bob Liuf3ab2632010-10-26 14:22:10 -07001504 if (ret)
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001505 putback_movable_pages(&source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001506 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001507out:
1508 return ret;
1509}
1510
1511/*
1512 * remove from free_area[] and mark all as Reserved.
1513 */
1514static int
1515offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1516 void *data)
1517{
1518 __offline_isolated_pages(start, start + nr_pages);
1519 return 0;
1520}
1521
1522static void
1523offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1524{
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001525 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001526 offline_isolated_pages_cb);
1527}
1528
1529/*
1530 * Check all pages in range, recoreded as memory resource, are isolated.
1531 */
1532static int
1533check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1534 void *data)
1535{
1536 int ret;
1537 long offlined = *(long *)data;
Wen Congyangb023f462012-12-11 16:00:45 -08001538 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001539 offlined = nr_pages;
1540 if (!ret)
1541 *(long *)data += offlined;
1542 return ret;
1543}
1544
1545static long
1546check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1547{
1548 long offlined = 0;
1549 int ret;
1550
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001551 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001552 check_pages_isolated_cb);
1553 if (ret < 0)
1554 offlined = (long)ret;
1555 return offlined;
1556}
1557
Lai Jiangshan09285af2012-12-12 13:52:04 -08001558#ifdef CONFIG_MOVABLE_NODE
Tang Chen79a4dce2012-12-18 14:23:24 -08001559/*
1560 * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
1561 * normal memory.
1562 */
Lai Jiangshan09285af2012-12-12 13:52:04 -08001563static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1564{
1565 return true;
1566}
Tang Chen79a4dce2012-12-18 14:23:24 -08001567#else /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001568/* ensure the node has NORMAL memory if it is still online */
1569static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1570{
1571 struct pglist_data *pgdat = zone->zone_pgdat;
1572 unsigned long present_pages = 0;
1573 enum zone_type zt;
1574
1575 for (zt = 0; zt <= ZONE_NORMAL; zt++)
1576 present_pages += pgdat->node_zones[zt].present_pages;
1577
1578 if (present_pages > nr_pages)
1579 return true;
1580
1581 present_pages = 0;
1582 for (; zt <= ZONE_MOVABLE; zt++)
1583 present_pages += pgdat->node_zones[zt].present_pages;
1584
1585 /*
1586 * we can't offline the last normal memory until all
1587 * higher memory is offlined.
1588 */
1589 return present_pages == 0;
1590}
Tang Chen79a4dce2012-12-18 14:23:24 -08001591#endif /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001592
Tang Chenc5320922013-11-12 15:08:10 -08001593static int __init cmdline_parse_movable_node(char *p)
1594{
1595#ifdef CONFIG_MOVABLE_NODE
1596 /*
1597 * Memory used by the kernel cannot be hot-removed because Linux
1598 * cannot migrate the kernel pages. When memory hotplug is
1599 * enabled, we should prevent memblock from allocating memory
1600 * for the kernel.
1601 *
1602 * ACPI SRAT records all hotpluggable memory ranges. But before
1603 * SRAT is parsed, we don't know about it.
1604 *
1605 * The kernel image is loaded into memory at very early time. We
1606 * cannot prevent this anyway. So on NUMA system, we set any
1607 * node the kernel resides in as un-hotpluggable.
1608 *
1609 * Since on modern servers, one node could have double-digit
1610 * gigabytes memory, we can assume the memory around the kernel
1611 * image is also un-hotpluggable. So before SRAT is parsed, just
1612 * allocate memory near the kernel image to try the best to keep
1613 * the kernel away from hotpluggable memory.
1614 */
1615 memblock_set_bottom_up(true);
Tang Chen55ac5902014-01-21 15:49:35 -08001616 movable_node_enabled = true;
Tang Chenc5320922013-11-12 15:08:10 -08001617#else
1618 pr_warn("movable_node option not supported\n");
1619#endif
1620 return 0;
1621}
1622early_param("movable_node", cmdline_parse_movable_node);
1623
Lai Jiangshand9713672012-12-11 16:01:03 -08001624/* check which state of node_states will be changed when offline memory */
1625static void node_states_check_changes_offline(unsigned long nr_pages,
1626 struct zone *zone, struct memory_notify *arg)
1627{
1628 struct pglist_data *pgdat = zone->zone_pgdat;
1629 unsigned long present_pages = 0;
1630 enum zone_type zt, zone_last = ZONE_NORMAL;
1631
1632 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001633 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1634 * contains nodes which have zones of 0...ZONE_NORMAL,
1635 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -08001636 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001637 * If we don't have HIGHMEM nor movable node,
1638 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1639 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -08001640 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001641 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -08001642 zone_last = ZONE_MOVABLE;
1643
1644 /*
1645 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1646 * If the memory to be offline is in a zone of 0...zone_last,
1647 * and it is the last present memory, 0...zone_last will
1648 * become empty after offline , thus we can determind we will
1649 * need to clear the node from node_states[N_NORMAL_MEMORY].
1650 */
1651 for (zt = 0; zt <= zone_last; zt++)
1652 present_pages += pgdat->node_zones[zt].present_pages;
1653 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1654 arg->status_change_nid_normal = zone_to_nid(zone);
1655 else
1656 arg->status_change_nid_normal = -1;
1657
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001658#ifdef CONFIG_HIGHMEM
1659 /*
1660 * If we have movable node, node_states[N_HIGH_MEMORY]
1661 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1662 * set zone_last to ZONE_HIGHMEM.
1663 *
1664 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1665 * contains nodes which have zones of 0...ZONE_MOVABLE,
1666 * set zone_last to ZONE_MOVABLE.
1667 */
1668 zone_last = ZONE_HIGHMEM;
1669 if (N_MEMORY == N_HIGH_MEMORY)
1670 zone_last = ZONE_MOVABLE;
1671
1672 for (; zt <= zone_last; zt++)
1673 present_pages += pgdat->node_zones[zt].present_pages;
1674 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1675 arg->status_change_nid_high = zone_to_nid(zone);
1676 else
1677 arg->status_change_nid_high = -1;
1678#else
1679 arg->status_change_nid_high = arg->status_change_nid_normal;
1680#endif
1681
Lai Jiangshand9713672012-12-11 16:01:03 -08001682 /*
1683 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1684 */
1685 zone_last = ZONE_MOVABLE;
1686
1687 /*
1688 * check whether node_states[N_HIGH_MEMORY] will be changed
1689 * If we try to offline the last present @nr_pages from the node,
1690 * we can determind we will need to clear the node from
1691 * node_states[N_HIGH_MEMORY].
1692 */
1693 for (; zt <= zone_last; zt++)
1694 present_pages += pgdat->node_zones[zt].present_pages;
1695 if (nr_pages >= present_pages)
1696 arg->status_change_nid = zone_to_nid(zone);
1697 else
1698 arg->status_change_nid = -1;
1699}
1700
1701static void node_states_clear_node(int node, struct memory_notify *arg)
1702{
1703 if (arg->status_change_nid_normal >= 0)
1704 node_clear_state(node, N_NORMAL_MEMORY);
1705
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001706 if ((N_MEMORY != N_NORMAL_MEMORY) &&
1707 (arg->status_change_nid_high >= 0))
Lai Jiangshand9713672012-12-11 16:01:03 -08001708 node_clear_state(node, N_HIGH_MEMORY);
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001709
1710 if ((N_MEMORY != N_HIGH_MEMORY) &&
1711 (arg->status_change_nid >= 0))
1712 node_clear_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -08001713}
1714
Wen Congyanga16cee12012-10-08 16:33:58 -07001715static int __ref __offline_pages(unsigned long start_pfn,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001716 unsigned long end_pfn, unsigned long timeout)
1717{
1718 unsigned long pfn, nr_pages, expire;
1719 long offlined_pages;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001720 int ret, drain, retry_max, node;
Cody P Schaferd7029092013-07-03 15:02:11 -07001721 unsigned long flags;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001722 struct zone *zone;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001723 struct memory_notify arg;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001724
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001725 /* at least, alignment against pageblock is necessary */
1726 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1727 return -EINVAL;
1728 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1729 return -EINVAL;
1730 /* This makes hotplug much easier...and readable.
1731 we assume this for now. .*/
1732 if (!test_pages_in_a_zone(start_pfn, end_pfn))
1733 return -EINVAL;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001734
1735 zone = page_zone(pfn_to_page(start_pfn));
1736 node = zone_to_nid(zone);
1737 nr_pages = end_pfn - start_pfn;
1738
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001739 if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
David Rientjes30467e02015-04-14 15:45:11 -07001740 return -EINVAL;
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001741
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001742 /* set above range as isolated */
Wen Congyangb023f462012-12-11 16:00:45 -08001743 ret = start_isolate_page_range(start_pfn, end_pfn,
1744 MIGRATE_MOVABLE, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001745 if (ret)
David Rientjes30467e02015-04-14 15:45:11 -07001746 return ret;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001747
1748 arg.start_pfn = start_pfn;
1749 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -08001750 node_states_check_changes_offline(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001751
1752 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1753 ret = notifier_to_errno(ret);
1754 if (ret)
1755 goto failed_removal;
1756
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001757 pfn = start_pfn;
1758 expire = jiffies + timeout;
1759 drain = 0;
1760 retry_max = 5;
1761repeat:
1762 /* start memory hot removal */
1763 ret = -EAGAIN;
1764 if (time_after(jiffies, expire))
1765 goto failed_removal;
1766 ret = -EINTR;
1767 if (signal_pending(current))
1768 goto failed_removal;
1769 ret = 0;
1770 if (drain) {
1771 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001772 cond_resched();
Vlastimil Babkac0554322014-12-10 15:43:10 -08001773 drain_all_pages(zone);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001774 }
1775
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001776 pfn = scan_movable_pages(start_pfn, end_pfn);
1777 if (pfn) { /* We have movable pages */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001778 ret = do_migrate_range(pfn, end_pfn);
1779 if (!ret) {
1780 drain = 1;
1781 goto repeat;
1782 } else {
1783 if (ret < 0)
1784 if (--retry_max == 0)
1785 goto failed_removal;
1786 yield();
1787 drain = 1;
1788 goto repeat;
1789 }
1790 }
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001791 /* drain all zone's lru pagevec, this is asynchronous... */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001792 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001793 yield();
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001794 /* drain pcp pages, this is synchronous. */
Vlastimil Babkac0554322014-12-10 15:43:10 -08001795 drain_all_pages(zone);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001796 /*
1797 * dissolve free hugepages in the memory block before doing offlining
1798 * actually in order to make hugetlbfs's object counting consistent.
1799 */
1800 dissolve_free_huge_pages(start_pfn, end_pfn);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001801 /* check again */
1802 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1803 if (offlined_pages < 0) {
1804 ret = -EBUSY;
1805 goto failed_removal;
1806 }
1807 printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001808 /* Ok, all of our target is isolated.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001809 We cannot do rollback at this point. */
1810 offline_isolated_pages(start_pfn, end_pfn);
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -08001811 /* reset pagetype flags and makes migrate type to be MOVABLE */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02001812 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001813 /* removal success */
Jiang Liu3dcc0572013-07-03 15:03:21 -07001814 adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001815 zone->present_pages -= offlined_pages;
Cody P Schaferd7029092013-07-03 15:02:11 -07001816
1817 pgdat_resize_lock(zone->zone_pgdat, &flags);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001818 zone->zone_pgdat->node_present_pages -= offlined_pages;
Cody P Schaferd7029092013-07-03 15:02:11 -07001819 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001820
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07001821 init_per_zone_wmark_min();
1822
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001823 if (!populated_zone(zone)) {
Jiang Liu340175b2012-07-31 16:43:32 -07001824 zone_pcp_reset(zone);
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001825 mutex_lock(&zonelists_mutex);
1826 build_all_zonelists(NULL, NULL);
1827 mutex_unlock(&zonelists_mutex);
1828 } else
1829 zone_pcp_update(zone);
Jiang Liu340175b2012-07-31 16:43:32 -07001830
Lai Jiangshand9713672012-12-11 16:01:03 -08001831 node_states_clear_node(node, &arg);
1832 if (arg.status_change_nid >= 0)
David Rientjes8fe23e02009-12-14 17:58:33 -08001833 kswapd_stop(node);
Minchan Kimbce73942009-06-16 15:32:50 -07001834
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001835 vm_total_pages = nr_free_pagecache_pages();
1836 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001837
1838 memory_notify(MEM_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001839 return 0;
1840
1841failed_removal:
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07001842 printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n",
1843 (unsigned long long) start_pfn << PAGE_SHIFT,
1844 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001845 memory_notify(MEM_CANCEL_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001846 /* pushback to free area */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02001847 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001848 return ret;
1849}
Badari Pulavarty71088782008-10-18 20:25:58 -07001850
David Rientjes30467e02015-04-14 15:45:11 -07001851/* Must be protected by mem_hotplug_begin() */
Wen Congyanga16cee12012-10-08 16:33:58 -07001852int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1853{
1854 return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
1855}
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02001856#endif /* CONFIG_MEMORY_HOTREMOVE */
Wen Congyanga16cee12012-10-08 16:33:58 -07001857
Wen Congyangbbc76be2013-02-22 16:32:54 -08001858/**
1859 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1860 * @start_pfn: start pfn of the memory range
Toshi Kanie05c4bb2013-04-29 15:06:16 -07001861 * @end_pfn: end pfn of the memory range
Wen Congyangbbc76be2013-02-22 16:32:54 -08001862 * @arg: argument passed to func
1863 * @func: callback for each memory section walked
1864 *
1865 * This function walks through all present mem sections in range
1866 * [start_pfn, end_pfn) and call func on each mem section.
1867 *
1868 * Returns the return value of func.
1869 */
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02001870int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
Wen Congyangbbc76be2013-02-22 16:32:54 -08001871 void *arg, int (*func)(struct memory_block *, void *))
Badari Pulavarty71088782008-10-18 20:25:58 -07001872{
Wen Congyange90bdb72012-10-08 16:34:01 -07001873 struct memory_block *mem = NULL;
1874 struct mem_section *section;
Wen Congyange90bdb72012-10-08 16:34:01 -07001875 unsigned long pfn, section_nr;
1876 int ret;
Badari Pulavarty71088782008-10-18 20:25:58 -07001877
Wen Congyange90bdb72012-10-08 16:34:01 -07001878 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1879 section_nr = pfn_to_section_nr(pfn);
1880 if (!present_section_nr(section_nr))
1881 continue;
1882
1883 section = __nr_to_section(section_nr);
1884 /* same memblock? */
1885 if (mem)
1886 if ((section_nr >= mem->start_section_nr) &&
1887 (section_nr <= mem->end_section_nr))
1888 continue;
1889
1890 mem = find_memory_block_hinted(section, mem);
1891 if (!mem)
1892 continue;
1893
Wen Congyangbbc76be2013-02-22 16:32:54 -08001894 ret = func(mem, arg);
Wen Congyange90bdb72012-10-08 16:34:01 -07001895 if (ret) {
Wen Congyangbbc76be2013-02-22 16:32:54 -08001896 kobject_put(&mem->dev.kobj);
1897 return ret;
Wen Congyange90bdb72012-10-08 16:34:01 -07001898 }
1899 }
1900
1901 if (mem)
1902 kobject_put(&mem->dev.kobj);
1903
Wen Congyangbbc76be2013-02-22 16:32:54 -08001904 return 0;
1905}
1906
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02001907#ifdef CONFIG_MEMORY_HOTREMOVE
Xishi Qiud6de9d52013-11-12 15:07:20 -08001908static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
Wen Congyangbbc76be2013-02-22 16:32:54 -08001909{
1910 int ret = !is_memblock_offlined(mem);
1911
Randy Dunlap349daa02013-04-29 15:08:49 -07001912 if (unlikely(ret)) {
1913 phys_addr_t beginpa, endpa;
1914
1915 beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
1916 endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
Wen Congyangbbc76be2013-02-22 16:32:54 -08001917 pr_warn("removing memory fails, because memory "
Randy Dunlap349daa02013-04-29 15:08:49 -07001918 "[%pa-%pa] is onlined\n",
1919 &beginpa, &endpa);
1920 }
Wen Congyangbbc76be2013-02-22 16:32:54 -08001921
1922 return ret;
1923}
1924
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001925static int check_cpu_on_node(pg_data_t *pgdat)
Tang Chen60a5a192013-02-22 16:33:14 -08001926{
Tang Chen60a5a192013-02-22 16:33:14 -08001927 int cpu;
1928
1929 for_each_present_cpu(cpu) {
1930 if (cpu_to_node(cpu) == pgdat->node_id)
1931 /*
1932 * the cpu on this node isn't removed, and we can't
1933 * offline this node.
1934 */
1935 return -EBUSY;
1936 }
1937
1938 return 0;
1939}
1940
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001941static void unmap_cpu_on_node(pg_data_t *pgdat)
Wen Congyange13fe862013-02-22 16:33:31 -08001942{
1943#ifdef CONFIG_ACPI_NUMA
Wen Congyange13fe862013-02-22 16:33:31 -08001944 int cpu;
1945
1946 for_each_possible_cpu(cpu)
1947 if (cpu_to_node(cpu) == pgdat->node_id)
1948 numa_clear_node(cpu);
1949#endif
1950}
1951
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001952static int check_and_unmap_cpu_on_node(pg_data_t *pgdat)
Wen Congyange13fe862013-02-22 16:33:31 -08001953{
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001954 int ret;
Wen Congyange13fe862013-02-22 16:33:31 -08001955
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001956 ret = check_cpu_on_node(pgdat);
Wen Congyange13fe862013-02-22 16:33:31 -08001957 if (ret)
1958 return ret;
1959
1960 /*
1961 * the node will be offlined when we come here, so we can clear
1962 * the cpu_to_node() now.
1963 */
1964
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001965 unmap_cpu_on_node(pgdat);
Wen Congyange13fe862013-02-22 16:33:31 -08001966 return 0;
1967}
1968
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001969/**
1970 * try_offline_node
1971 *
1972 * Offline a node if all memory sections and cpus of the node are removed.
1973 *
1974 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1975 * and online/offline operations before this call.
1976 */
Wen Congyang90b30cd2013-02-22 16:33:27 -08001977void try_offline_node(int nid)
Tang Chen60a5a192013-02-22 16:33:14 -08001978{
Wen Congyangd822b862013-02-22 16:33:16 -08001979 pg_data_t *pgdat = NODE_DATA(nid);
1980 unsigned long start_pfn = pgdat->node_start_pfn;
1981 unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
Tang Chen60a5a192013-02-22 16:33:14 -08001982 unsigned long pfn;
Wen Congyangd822b862013-02-22 16:33:16 -08001983 int i;
Tang Chen60a5a192013-02-22 16:33:14 -08001984
1985 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1986 unsigned long section_nr = pfn_to_section_nr(pfn);
1987
1988 if (!present_section_nr(section_nr))
1989 continue;
1990
1991 if (pfn_to_nid(pfn) != nid)
1992 continue;
1993
1994 /*
1995 * some memory sections of this node are not removed, and we
1996 * can't offline node now.
1997 */
1998 return;
1999 }
2000
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002001 if (check_and_unmap_cpu_on_node(pgdat))
Tang Chen60a5a192013-02-22 16:33:14 -08002002 return;
2003
2004 /*
2005 * all memory/cpu of this node are removed, we can offline this
2006 * node now.
2007 */
2008 node_set_offline(nid);
2009 unregister_one_node(nid);
Wen Congyangd822b862013-02-22 16:33:16 -08002010
Wen Congyangd822b862013-02-22 16:33:16 -08002011 /* free waittable in each zone */
2012 for (i = 0; i < MAX_NR_ZONES; i++) {
2013 struct zone *zone = pgdat->node_zones + i;
2014
Jianguo Wuca4b3f32013-03-22 15:04:50 -07002015 /*
2016 * wait_table may be allocated from boot memory,
2017 * here only free if it's allocated by vmalloc.
2018 */
Gu Zheng85bd8392015-06-10 11:14:43 -07002019 if (is_vmalloc_addr(zone->wait_table)) {
Wen Congyangd822b862013-02-22 16:33:16 -08002020 vfree(zone->wait_table);
Gu Zheng85bd8392015-06-10 11:14:43 -07002021 zone->wait_table = NULL;
2022 }
Wen Congyangd822b862013-02-22 16:33:16 -08002023 }
Tang Chen60a5a192013-02-22 16:33:14 -08002024}
Wen Congyang90b30cd2013-02-22 16:33:27 -08002025EXPORT_SYMBOL(try_offline_node);
Tang Chen60a5a192013-02-22 16:33:14 -08002026
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002027/**
2028 * remove_memory
2029 *
2030 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2031 * and online/offline operations before this call, as required by
2032 * try_offline_node().
2033 */
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002034void __ref remove_memory(int nid, u64 start, u64 size)
Wen Congyangbbc76be2013-02-22 16:32:54 -08002035{
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002036 int ret;
Wen Congyang993c1aa2013-02-22 16:32:50 -08002037
Toshi Kani27356f52013-09-11 14:21:49 -07002038 BUG_ON(check_hotplug_memory_range(start, size));
2039
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002040 mem_hotplug_begin();
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002041
2042 /*
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002043 * All memory blocks must be offlined before removing memory. Check
2044 * whether all memory blocks in question are offline and trigger a BUG()
2045 * if this is not the case.
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002046 */
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002047 ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
Xishi Qiud6de9d52013-11-12 15:07:20 -08002048 check_memblock_offlined_cb);
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002049 if (ret)
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002050 BUG();
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002051
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -08002052 /* remove memmap entry */
2053 firmware_map_remove(start, start + size, "System RAM");
Xishi Qiuf9126ab2015-08-14 15:35:16 -07002054 memblock_free(start, size);
2055 memblock_remove(start, size);
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -08002056
Wen Congyang24d335c2013-02-22 16:32:58 -08002057 arch_remove_memory(start, size);
2058
Tang Chen60a5a192013-02-22 16:33:14 -08002059 try_offline_node(nid);
2060
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002061 mem_hotplug_done();
Badari Pulavarty71088782008-10-18 20:25:58 -07002062}
Badari Pulavarty71088782008-10-18 20:25:58 -07002063EXPORT_SYMBOL_GPL(remove_memory);
Rafael J. Wysockiaba6efc2013-06-01 22:24:07 +02002064#endif /* CONFIG_MEMORY_HOTREMOVE */