blob: 67d488ab495e57b9018484932e135078c787903c [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);
134 BUG_ON(!res);
135
136 res->name = "System RAM";
137 res->start = start;
138 res->end = start + size - 1;
Yasunori Goto887c3cb2007-11-14 16:59:20 -0800139 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
Keith Mannthey45e0b782006-09-30 23:27:09 -0700140 if (request_resource(&iomem_resource, res) < 0) {
Toshi Kani4996eed2013-07-03 15:02:39 -0700141 pr_debug("System RAM resource %pR cannot be added\n", res);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700142 kfree(res);
143 res = NULL;
144 }
145 return res;
146}
147
148static void release_memory_resource(struct resource *res)
149{
150 if (!res)
151 return;
152 release_resource(res);
153 kfree(res);
154 return;
155}
156
Keith Mannthey53947022006-09-30 23:27:08 -0700157#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800158void get_page_bootmem(unsigned long info, struct page *page,
159 unsigned long type)
Yasunori Goto04753272008-04-28 02:13:31 -0700160{
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800161 page->lru.next = (struct list_head *) type;
Yasunori Goto04753272008-04-28 02:13:31 -0700162 SetPagePrivate(page);
163 set_page_private(page, info);
164 atomic_inc(&page->_count);
165}
166
Jiang Liu170a5a72013-07-03 15:03:17 -0700167void put_page_bootmem(struct page *page)
Yasunori Goto04753272008-04-28 02:13:31 -0700168{
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800169 unsigned long type;
Yasunori Goto04753272008-04-28 02:13:31 -0700170
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800171 type = (unsigned long) page->lru.next;
172 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
173 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
Yasunori Goto04753272008-04-28 02:13:31 -0700174
175 if (atomic_dec_return(&page->_count) == 1) {
176 ClearPagePrivate(page);
177 set_page_private(page, 0);
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800178 INIT_LIST_HEAD(&page->lru);
Jiang Liu170a5a72013-07-03 15:03:17 -0700179 free_reserved_page(page);
Yasunori Goto04753272008-04-28 02:13:31 -0700180 }
Yasunori Goto04753272008-04-28 02:13:31 -0700181}
182
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800183#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
184#ifndef CONFIG_SPARSEMEM_VMEMMAP
Adrian Bunkd92bc312008-07-23 21:28:12 -0700185static void register_page_bootmem_info_section(unsigned long start_pfn)
Yasunori Goto04753272008-04-28 02:13:31 -0700186{
187 unsigned long *usemap, mapsize, section_nr, i;
188 struct mem_section *ms;
189 struct page *page, *memmap;
190
Yasunori Goto04753272008-04-28 02:13:31 -0700191 section_nr = pfn_to_section_nr(start_pfn);
192 ms = __nr_to_section(section_nr);
193
194 /* Get section's memmap address */
195 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
196
197 /*
198 * Get page for the memmap's phys address
199 * XXX: need more consideration for sparse_vmemmap...
200 */
201 page = virt_to_page(memmap);
202 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
203 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
204
205 /* remember memmap's page */
206 for (i = 0; i < mapsize; i++, page++)
207 get_page_bootmem(section_nr, page, SECTION_INFO);
208
209 usemap = __nr_to_section(section_nr)->pageblock_flags;
210 page = virt_to_page(usemap);
211
212 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
213
214 for (i = 0; i < mapsize; i++, page++)
Yasunori Gotoaf370fb2008-07-23 21:28:17 -0700215 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
Yasunori Goto04753272008-04-28 02:13:31 -0700216
217}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800218#else /* CONFIG_SPARSEMEM_VMEMMAP */
219static void register_page_bootmem_info_section(unsigned long start_pfn)
220{
221 unsigned long *usemap, mapsize, section_nr, i;
222 struct mem_section *ms;
223 struct page *page, *memmap;
224
225 if (!pfn_valid(start_pfn))
226 return;
227
228 section_nr = pfn_to_section_nr(start_pfn);
229 ms = __nr_to_section(section_nr);
230
231 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
232
233 register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
234
235 usemap = __nr_to_section(section_nr)->pageblock_flags;
236 page = virt_to_page(usemap);
237
238 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
239
240 for (i = 0; i < mapsize; i++, page++)
241 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
242}
243#endif /* !CONFIG_SPARSEMEM_VMEMMAP */
Yasunori Goto04753272008-04-28 02:13:31 -0700244
245void register_page_bootmem_info_node(struct pglist_data *pgdat)
246{
247 unsigned long i, pfn, end_pfn, nr_pages;
248 int node = pgdat->node_id;
249 struct page *page;
250 struct zone *zone;
251
252 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
253 page = virt_to_page(pgdat);
254
255 for (i = 0; i < nr_pages; i++, page++)
256 get_page_bootmem(node, page, NODE_INFO);
257
258 zone = &pgdat->node_zones[0];
259 for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
Xishi Qiu139c2d72013-09-11 14:21:46 -0700260 if (zone_is_initialized(zone)) {
Yasunori Goto04753272008-04-28 02:13:31 -0700261 nr_pages = zone->wait_table_hash_nr_entries
262 * sizeof(wait_queue_head_t);
263 nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
264 page = virt_to_page(zone->wait_table);
265
266 for (i = 0; i < nr_pages; i++, page++)
267 get_page_bootmem(node, page, NODE_INFO);
268 }
269 }
270
271 pfn = pgdat->node_start_pfn;
Cody P Schaferc1f19492013-02-22 16:35:32 -0800272 end_pfn = pgdat_end_pfn(pgdat);
Yasunori Goto04753272008-04-28 02:13:31 -0700273
Tang Chen7e9f5eb2013-07-08 16:00:23 -0700274 /* register section info */
qiuxishif14851a2012-09-17 14:09:24 -0700275 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
276 /*
277 * Some platforms can assign the same pfn to multiple nodes - on
278 * node0 as well as nodeN. To avoid registering a pfn against
279 * multiple nodes we check that this pfn does not already
Tang Chen7e9f5eb2013-07-08 16:00:23 -0700280 * reside in some other nodes.
qiuxishif14851a2012-09-17 14:09:24 -0700281 */
282 if (pfn_valid(pfn) && (pfn_to_nid(pfn) == node))
283 register_page_bootmem_info_section(pfn);
284 }
Yasunori Goto04753272008-04-28 02:13:31 -0700285}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800286#endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
Yasunori Goto04753272008-04-28 02:13:31 -0700287
Fabian Frederickf2765402014-08-06 16:04:57 -0700288static void __meminit grow_zone_span(struct zone *zone, unsigned long start_pfn,
289 unsigned long end_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700290{
291 unsigned long old_zone_end_pfn;
292
293 zone_span_writelock(zone);
294
Xishi Qiuc33bc312013-09-11 14:21:44 -0700295 old_zone_end_pfn = zone_end_pfn(zone);
Xishi Qiu8080fc02013-09-11 14:21:45 -0700296 if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700297 zone->zone_start_pfn = start_pfn;
298
299 zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
300 zone->zone_start_pfn;
301
302 zone_span_writeunlock(zone);
303}
304
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800305static void resize_zone(struct zone *zone, unsigned long start_pfn,
306 unsigned long end_pfn)
307{
308 zone_span_writelock(zone);
309
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800310 if (end_pfn - start_pfn) {
311 zone->zone_start_pfn = start_pfn;
312 zone->spanned_pages = end_pfn - start_pfn;
313 } else {
314 /*
315 * make it consist as free_area_init_core(),
316 * if spanned_pages = 0, then keep start_pfn = 0
317 */
318 zone->zone_start_pfn = 0;
319 zone->spanned_pages = 0;
320 }
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800321
322 zone_span_writeunlock(zone);
323}
324
325static void fix_zone_id(struct zone *zone, unsigned long start_pfn,
326 unsigned long end_pfn)
327{
328 enum zone_type zid = zone_idx(zone);
329 int nid = zone->zone_pgdat->node_id;
330 unsigned long pfn;
331
332 for (pfn = start_pfn; pfn < end_pfn; pfn++)
333 set_page_links(pfn_to_page(pfn), zid, nid, pfn);
334}
335
Cody P Schaferf6bbb782013-02-22 16:35:30 -0800336/* Can fail with -ENOMEM from allocating a wait table with vmalloc() or
Santosh Shilimkar9e43aa22014-01-21 15:50:43 -0800337 * alloc_bootmem_node_nopanic()/memblock_virt_alloc_node_nopanic() */
Cody P Schaferf6bbb782013-02-22 16:35:30 -0800338static int __ref ensure_zone_is_initialized(struct zone *zone,
339 unsigned long start_pfn, unsigned long num_pages)
340{
341 if (!zone_is_initialized(zone))
Yaowei Baib171e402015-11-05 18:47:06 -0800342 return init_currently_empty_zone(zone, start_pfn, num_pages);
343
Cody P Schaferf6bbb782013-02-22 16:35:30 -0800344 return 0;
345}
346
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800347static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800348 unsigned long start_pfn, unsigned long end_pfn)
349{
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800350 int ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800351 unsigned long flags;
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800352 unsigned long z1_start_pfn;
353
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800354 ret = ensure_zone_is_initialized(z1, start_pfn, end_pfn - start_pfn);
355 if (ret)
356 return ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800357
358 pgdat_resize_lock(z1->zone_pgdat, &flags);
359
360 /* can't move pfns which are higher than @z2 */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800361 if (end_pfn > zone_end_pfn(z2))
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800362 goto out_fail;
Jiang Liu834405c2013-07-03 15:03:04 -0700363 /* the move out part must be at the left most of @z2 */
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800364 if (start_pfn > z2->zone_start_pfn)
365 goto out_fail;
366 /* must included/overlap */
367 if (end_pfn <= z2->zone_start_pfn)
368 goto out_fail;
369
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800370 /* use start_pfn for z1's start_pfn if z1 is empty */
Xishi Qiu8080fc02013-09-11 14:21:45 -0700371 if (!zone_is_empty(z1))
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800372 z1_start_pfn = z1->zone_start_pfn;
373 else
374 z1_start_pfn = start_pfn;
375
376 resize_zone(z1, z1_start_pfn, end_pfn);
Cody P Schafer108bcc92013-02-22 16:35:23 -0800377 resize_zone(z2, end_pfn, zone_end_pfn(z2));
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800378
379 pgdat_resize_unlock(z1->zone_pgdat, &flags);
380
381 fix_zone_id(z1, start_pfn, end_pfn);
382
383 return 0;
384out_fail:
385 pgdat_resize_unlock(z1->zone_pgdat, &flags);
386 return -1;
387}
388
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800389static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800390 unsigned long start_pfn, unsigned long end_pfn)
391{
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800392 int ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800393 unsigned long flags;
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800394 unsigned long z2_end_pfn;
395
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800396 ret = ensure_zone_is_initialized(z2, start_pfn, end_pfn - start_pfn);
397 if (ret)
398 return ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800399
400 pgdat_resize_lock(z1->zone_pgdat, &flags);
401
402 /* can't move pfns which are lower than @z1 */
403 if (z1->zone_start_pfn > start_pfn)
404 goto out_fail;
405 /* the move out part mast at the right most of @z1 */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800406 if (zone_end_pfn(z1) > end_pfn)
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800407 goto out_fail;
408 /* must included/overlap */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800409 if (start_pfn >= zone_end_pfn(z1))
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800410 goto out_fail;
411
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800412 /* use end_pfn for z2's end_pfn if z2 is empty */
Xishi Qiu8080fc02013-09-11 14:21:45 -0700413 if (!zone_is_empty(z2))
Cody P Schafer108bcc92013-02-22 16:35:23 -0800414 z2_end_pfn = zone_end_pfn(z2);
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800415 else
416 z2_end_pfn = end_pfn;
417
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800418 resize_zone(z1, z1->zone_start_pfn, start_pfn);
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800419 resize_zone(z2, start_pfn, z2_end_pfn);
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800420
421 pgdat_resize_unlock(z1->zone_pgdat, &flags);
422
423 fix_zone_id(z2, start_pfn, end_pfn);
424
425 return 0;
426out_fail:
427 pgdat_resize_unlock(z1->zone_pgdat, &flags);
428 return -1;
429}
430
Fabian Frederickf2765402014-08-06 16:04:57 -0700431static void __meminit grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
432 unsigned long end_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700433{
Xishi Qiu83285c72013-11-12 15:07:19 -0800434 unsigned long old_pgdat_end_pfn = pgdat_end_pfn(pgdat);
Heiko Carstens76cdd582008-05-14 16:05:52 -0700435
Tang Chen712cd382012-12-11 16:01:07 -0800436 if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700437 pgdat->node_start_pfn = start_pfn;
438
439 pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
440 pgdat->node_start_pfn;
441}
442
Al Viro31168482008-11-22 17:33:24 +0000443static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700444{
445 struct pglist_data *pgdat = zone->zone_pgdat;
446 int nr_pages = PAGES_PER_SECTION;
447 int nid = pgdat->node_id;
448 int zone_type;
Mel Gormane298ff72015-08-06 15:46:51 -0700449 unsigned long flags, pfn;
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800450 int ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700451
452 zone_type = zone - pgdat->node_zones;
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800453 ret = ensure_zone_is_initialized(zone, phys_start_pfn, nr_pages);
454 if (ret)
455 return ret;
Heiko Carstens76cdd582008-05-14 16:05:52 -0700456
Heiko Carstens76cdd582008-05-14 16:05:52 -0700457 pgdat_resize_lock(zone->zone_pgdat, &flags);
458 grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
459 grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
460 phys_start_pfn + nr_pages);
461 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Dave Hansena2f3aa022007-01-10 23:15:30 -0800462 memmap_init_zone(nr_pages, nid, zone_type,
463 phys_start_pfn, MEMMAP_HOTPLUG);
Mel Gormane298ff72015-08-06 15:46:51 -0700464
465 /* online_page_range is called later and expects pages reserved */
466 for (pfn = phys_start_pfn; pfn < phys_start_pfn + nr_pages; pfn++) {
467 if (!pfn_valid(pfn))
468 continue;
469
470 SetPageReserved(pfn_to_page(pfn));
471 }
Yasunori Goto718127c2006-06-23 02:03:10 -0700472 return 0;
Dave Hansen3947be12005-10-29 18:16:54 -0700473}
474
Gary Hadec04fc582009-01-06 14:39:14 -0800475static int __meminit __add_section(int nid, struct zone *zone,
476 unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700477{
Dave Hansen3947be12005-10-29 18:16:54 -0700478 int ret;
479
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700480 if (pfn_valid(phys_start_pfn))
481 return -EEXIST;
482
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800483 ret = sparse_add_one_section(zone, phys_start_pfn);
Dave Hansen3947be12005-10-29 18:16:54 -0700484
485 if (ret < 0)
486 return ret;
487
Yasunori Goto718127c2006-06-23 02:03:10 -0700488 ret = __add_zone(zone, phys_start_pfn);
489
490 if (ret < 0)
491 return ret;
492
Gary Hadec04fc582009-01-06 14:39:14 -0800493 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
Dave Hansen3947be12005-10-29 18:16:54 -0700494}
495
David Rientjes4edd7ce2013-04-29 15:08:22 -0700496/*
497 * Reasonably generic function for adding memory. It is
498 * expected that archs that support memory hotplug will
499 * call this function after deciding the zone to which to
500 * add the new pages.
501 */
502int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
503 unsigned long nr_pages)
504{
505 unsigned long i;
506 int err = 0;
507 int start_sec, end_sec;
508 /* during initialize mem_map, align hot-added range to section */
509 start_sec = pfn_to_section_nr(phys_start_pfn);
510 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
511
512 for (i = start_sec; i <= end_sec; i++) {
Sheng Yong19c07d52015-04-14 15:44:54 -0700513 err = __add_section(nid, zone, section_nr_to_pfn(i));
David Rientjes4edd7ce2013-04-29 15:08:22 -0700514
515 /*
516 * EEXIST is finally dealt with by ioresource collision
517 * check. see add_memory() => register_memory_resource()
518 * Warning will be printed if there is collision.
519 */
520 if (err && (err != -EEXIST))
521 break;
522 err = 0;
523 }
Zhu Guihuac435a392015-06-24 16:58:42 -0700524 vmemmap_populate_print_last();
David Rientjes4edd7ce2013-04-29 15:08:22 -0700525
526 return err;
527}
528EXPORT_SYMBOL_GPL(__add_pages);
529
530#ifdef CONFIG_MEMORY_HOTREMOVE
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800531/* find the smallest valid pfn in the range [start_pfn, end_pfn) */
532static int find_smallest_section_pfn(int nid, struct zone *zone,
533 unsigned long start_pfn,
534 unsigned long end_pfn)
535{
536 struct mem_section *ms;
537
538 for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
539 ms = __pfn_to_section(start_pfn);
540
541 if (unlikely(!valid_section(ms)))
542 continue;
543
544 if (unlikely(pfn_to_nid(start_pfn) != nid))
545 continue;
546
547 if (zone && zone != page_zone(pfn_to_page(start_pfn)))
548 continue;
549
550 return start_pfn;
551 }
552
553 return 0;
554}
555
556/* find the biggest valid pfn in the range [start_pfn, end_pfn). */
557static int find_biggest_section_pfn(int nid, struct zone *zone,
558 unsigned long start_pfn,
559 unsigned long end_pfn)
560{
561 struct mem_section *ms;
562 unsigned long pfn;
563
564 /* pfn is the end pfn of a memory section. */
565 pfn = end_pfn - 1;
566 for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
567 ms = __pfn_to_section(pfn);
568
569 if (unlikely(!valid_section(ms)))
570 continue;
571
572 if (unlikely(pfn_to_nid(pfn) != nid))
573 continue;
574
575 if (zone && zone != page_zone(pfn_to_page(pfn)))
576 continue;
577
578 return pfn;
579 }
580
581 return 0;
582}
583
584static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
585 unsigned long end_pfn)
586{
Xishi Qiuc33bc312013-09-11 14:21:44 -0700587 unsigned long zone_start_pfn = zone->zone_start_pfn;
588 unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
589 unsigned long zone_end_pfn = z;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800590 unsigned long pfn;
591 struct mem_section *ms;
592 int nid = zone_to_nid(zone);
593
594 zone_span_writelock(zone);
595 if (zone_start_pfn == start_pfn) {
596 /*
597 * If the section is smallest section in the zone, it need
598 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
599 * In this case, we find second smallest valid mem_section
600 * for shrinking zone.
601 */
602 pfn = find_smallest_section_pfn(nid, zone, end_pfn,
603 zone_end_pfn);
604 if (pfn) {
605 zone->zone_start_pfn = pfn;
606 zone->spanned_pages = zone_end_pfn - pfn;
607 }
608 } else if (zone_end_pfn == end_pfn) {
609 /*
610 * If the section is biggest section in the zone, it need
611 * shrink zone->spanned_pages.
612 * In this case, we find second biggest valid mem_section for
613 * shrinking zone.
614 */
615 pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
616 start_pfn);
617 if (pfn)
618 zone->spanned_pages = pfn - zone_start_pfn + 1;
619 }
620
621 /*
622 * The section is not biggest or smallest mem_section in the zone, it
623 * only creates a hole in the zone. So in this case, we need not
624 * change the zone. But perhaps, the zone has only hole data. Thus
625 * it check the zone has only hole or not.
626 */
627 pfn = zone_start_pfn;
628 for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
629 ms = __pfn_to_section(pfn);
630
631 if (unlikely(!valid_section(ms)))
632 continue;
633
634 if (page_zone(pfn_to_page(pfn)) != zone)
635 continue;
636
637 /* If the section is current section, it continues the loop */
638 if (start_pfn == pfn)
639 continue;
640
641 /* If we find valid section, we have nothing to do */
642 zone_span_writeunlock(zone);
643 return;
644 }
645
646 /* The zone has no valid section */
647 zone->zone_start_pfn = 0;
648 zone->spanned_pages = 0;
649 zone_span_writeunlock(zone);
650}
651
652static void shrink_pgdat_span(struct pglist_data *pgdat,
653 unsigned long start_pfn, unsigned long end_pfn)
654{
Xishi Qiu83285c72013-11-12 15:07:19 -0800655 unsigned long pgdat_start_pfn = pgdat->node_start_pfn;
656 unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */
657 unsigned long pgdat_end_pfn = p;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800658 unsigned long pfn;
659 struct mem_section *ms;
660 int nid = pgdat->node_id;
661
662 if (pgdat_start_pfn == start_pfn) {
663 /*
664 * If the section is smallest section in the pgdat, it need
665 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
666 * In this case, we find second smallest valid mem_section
667 * for shrinking zone.
668 */
669 pfn = find_smallest_section_pfn(nid, NULL, end_pfn,
670 pgdat_end_pfn);
671 if (pfn) {
672 pgdat->node_start_pfn = pfn;
673 pgdat->node_spanned_pages = pgdat_end_pfn - pfn;
674 }
675 } else if (pgdat_end_pfn == end_pfn) {
676 /*
677 * If the section is biggest section in the pgdat, it need
678 * shrink pgdat->node_spanned_pages.
679 * In this case, we find second biggest valid mem_section for
680 * shrinking zone.
681 */
682 pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn,
683 start_pfn);
684 if (pfn)
685 pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1;
686 }
687
688 /*
689 * If the section is not biggest or smallest mem_section in the pgdat,
690 * it only creates a hole in the pgdat. So in this case, we need not
691 * change the pgdat.
692 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
693 * has only hole or not.
694 */
695 pfn = pgdat_start_pfn;
696 for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) {
697 ms = __pfn_to_section(pfn);
698
699 if (unlikely(!valid_section(ms)))
700 continue;
701
702 if (pfn_to_nid(pfn) != nid)
703 continue;
704
705 /* If the section is current section, it continues the loop */
706 if (start_pfn == pfn)
707 continue;
708
709 /* If we find valid section, we have nothing to do */
710 return;
711 }
712
713 /* The pgdat has no valid section */
714 pgdat->node_start_pfn = 0;
715 pgdat->node_spanned_pages = 0;
716}
717
718static void __remove_zone(struct zone *zone, unsigned long start_pfn)
719{
720 struct pglist_data *pgdat = zone->zone_pgdat;
721 int nr_pages = PAGES_PER_SECTION;
722 int zone_type;
723 unsigned long flags;
724
725 zone_type = zone - pgdat->node_zones;
726
727 pgdat_resize_lock(zone->zone_pgdat, &flags);
728 shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
729 shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages);
730 pgdat_resize_unlock(zone->zone_pgdat, &flags);
731}
732
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700733static int __remove_section(struct zone *zone, struct mem_section *ms)
734{
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800735 unsigned long start_pfn;
736 int scn_nr;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700737 int ret = -EINVAL;
738
739 if (!valid_section(ms))
740 return ret;
741
742 ret = unregister_memory_section(ms);
743 if (ret)
744 return ret;
745
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800746 scn_nr = __section_nr(ms);
747 start_pfn = section_nr_to_pfn(scn_nr);
748 __remove_zone(zone, start_pfn);
749
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700750 sparse_remove_one_section(zone, ms);
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700751 return 0;
752}
753
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700754/**
755 * __remove_pages() - remove sections of pages from a zone
756 * @zone: zone from which pages need to be removed
757 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
758 * @nr_pages: number of pages to remove (must be multiple of section size)
759 *
760 * Generic helper function to remove section mappings and sysfs entries
761 * for the section of the memory we are removing. Caller needs to make
762 * sure that pages are marked reserved and zones are adjust properly by
763 * calling offline_pages().
764 */
765int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
766 unsigned long nr_pages)
767{
Toshi Kanife74ebb2013-04-29 15:08:20 -0700768 unsigned long i;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700769 int sections_to_remove;
Toshi Kanife74ebb2013-04-29 15:08:20 -0700770 resource_size_t start, size;
771 int ret = 0;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700772
773 /*
774 * We can only remove entire sections
775 */
776 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
777 BUG_ON(nr_pages % PAGES_PER_SECTION);
778
Toshi Kanife74ebb2013-04-29 15:08:20 -0700779 start = phys_start_pfn << PAGE_SHIFT;
780 size = nr_pages * PAGE_SIZE;
Dan Williams033fbae2015-08-09 15:29:06 -0400781
782 /* in the ZONE_DEVICE case device driver owns the memory region */
783 if (!is_dev_zone(zone))
784 ret = release_mem_region_adjustable(&iomem_resource, start, size);
Randy Dunlap348f9f02013-05-24 15:55:30 -0700785 if (ret) {
786 resource_size_t endres = start + size - 1;
787
788 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
789 &start, &endres, ret);
790 }
Yasuaki Ishimatsud760afd2012-10-08 16:34:14 -0700791
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700792 sections_to_remove = nr_pages / PAGES_PER_SECTION;
793 for (i = 0; i < sections_to_remove; i++) {
794 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
795 ret = __remove_section(zone, __pfn_to_section(pfn));
796 if (ret)
797 break;
798 }
799 return ret;
800}
801EXPORT_SYMBOL_GPL(__remove_pages);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700802#endif /* CONFIG_MEMORY_HOTREMOVE */
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700803
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700804int set_online_page_callback(online_page_callback_t callback)
805{
806 int rc = -EINVAL;
807
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700808 get_online_mems();
809 mutex_lock(&online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700810
811 if (online_page_callback == generic_online_page) {
812 online_page_callback = callback;
813 rc = 0;
814 }
815
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700816 mutex_unlock(&online_page_callback_lock);
817 put_online_mems();
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700818
819 return rc;
820}
821EXPORT_SYMBOL_GPL(set_online_page_callback);
822
823int restore_online_page_callback(online_page_callback_t callback)
824{
825 int rc = -EINVAL;
826
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700827 get_online_mems();
828 mutex_lock(&online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700829
830 if (online_page_callback == callback) {
831 online_page_callback = generic_online_page;
832 rc = 0;
833 }
834
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700835 mutex_unlock(&online_page_callback_lock);
836 put_online_mems();
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700837
838 return rc;
839}
840EXPORT_SYMBOL_GPL(restore_online_page_callback);
841
842void __online_page_set_limits(struct page *page)
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700843{
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700844}
845EXPORT_SYMBOL_GPL(__online_page_set_limits);
846
847void __online_page_increment_counters(struct page *page)
848{
Jiang Liu3dcc0572013-07-03 15:03:21 -0700849 adjust_managed_page_count(page, 1);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700850}
851EXPORT_SYMBOL_GPL(__online_page_increment_counters);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700852
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700853void __online_page_free(struct page *page)
854{
Jiang Liu3dcc0572013-07-03 15:03:21 -0700855 __free_reserved_page(page);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700856}
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700857EXPORT_SYMBOL_GPL(__online_page_free);
858
859static void generic_online_page(struct page *page)
860{
861 __online_page_set_limits(page);
862 __online_page_increment_counters(page);
863 __online_page_free(page);
864}
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700865
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700866static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
867 void *arg)
Dave Hansen3947be12005-10-29 18:16:54 -0700868{
869 unsigned long i;
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700870 unsigned long onlined_pages = *(unsigned long *)arg;
871 struct page *page;
872 if (PageReserved(pfn_to_page(start_pfn)))
873 for (i = 0; i < nr_pages; i++) {
874 page = pfn_to_page(start_pfn + i);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700875 (*online_page_callback)(page);
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700876 onlined_pages++;
877 }
878 *(unsigned long *)arg = onlined_pages;
879 return 0;
880}
881
Lai Jiangshan09285af2012-12-12 13:52:04 -0800882#ifdef CONFIG_MOVABLE_NODE
Tang Chen79a4dce2012-12-18 14:23:24 -0800883/*
884 * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
885 * normal memory.
886 */
Lai Jiangshan09285af2012-12-12 13:52:04 -0800887static bool can_online_high_movable(struct zone *zone)
888{
889 return true;
890}
Tang Chen79a4dce2012-12-18 14:23:24 -0800891#else /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800892/* ensure every online node has NORMAL memory */
893static bool can_online_high_movable(struct zone *zone)
894{
895 return node_state(zone_to_nid(zone), N_NORMAL_MEMORY);
896}
Tang Chen79a4dce2012-12-18 14:23:24 -0800897#endif /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800898
Lai Jiangshand9713672012-12-11 16:01:03 -0800899/* check which state of node_states will be changed when online memory */
900static void node_states_check_changes_online(unsigned long nr_pages,
901 struct zone *zone, struct memory_notify *arg)
902{
903 int nid = zone_to_nid(zone);
904 enum zone_type zone_last = ZONE_NORMAL;
905
906 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800907 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
908 * contains nodes which have zones of 0...ZONE_NORMAL,
909 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -0800910 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800911 * If we don't have HIGHMEM nor movable node,
912 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
913 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -0800914 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800915 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -0800916 zone_last = ZONE_MOVABLE;
917
918 /*
919 * if the memory to be online is in a zone of 0...zone_last, and
920 * the zones of 0...zone_last don't have memory before online, we will
921 * need to set the node to node_states[N_NORMAL_MEMORY] after
922 * the memory is online.
923 */
924 if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
925 arg->status_change_nid_normal = nid;
926 else
927 arg->status_change_nid_normal = -1;
928
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800929#ifdef CONFIG_HIGHMEM
930 /*
931 * If we have movable node, node_states[N_HIGH_MEMORY]
932 * contains nodes which have zones of 0...ZONE_HIGHMEM,
933 * set zone_last to ZONE_HIGHMEM.
934 *
935 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
936 * contains nodes which have zones of 0...ZONE_MOVABLE,
937 * set zone_last to ZONE_MOVABLE.
938 */
939 zone_last = ZONE_HIGHMEM;
940 if (N_MEMORY == N_HIGH_MEMORY)
941 zone_last = ZONE_MOVABLE;
942
943 if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
944 arg->status_change_nid_high = nid;
945 else
946 arg->status_change_nid_high = -1;
947#else
948 arg->status_change_nid_high = arg->status_change_nid_normal;
949#endif
950
Lai Jiangshand9713672012-12-11 16:01:03 -0800951 /*
952 * if the node don't have memory befor online, we will need to
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800953 * set the node to node_states[N_MEMORY] after the memory
Lai Jiangshand9713672012-12-11 16:01:03 -0800954 * is online.
955 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800956 if (!node_state(nid, N_MEMORY))
Lai Jiangshand9713672012-12-11 16:01:03 -0800957 arg->status_change_nid = nid;
958 else
959 arg->status_change_nid = -1;
960}
961
962static void node_states_set_node(int node, struct memory_notify *arg)
963{
964 if (arg->status_change_nid_normal >= 0)
965 node_set_state(node, N_NORMAL_MEMORY);
966
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800967 if (arg->status_change_nid_high >= 0)
968 node_set_state(node, N_HIGH_MEMORY);
969
970 node_set_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -0800971}
972
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700973
David Rientjes30467e02015-04-14 15:45:11 -0700974/* Must be protected by mem_hotplug_begin() */
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800975int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700976{
Cody P Schaferaa472282013-07-03 15:02:10 -0700977 unsigned long flags;
Dave Hansen3947be12005-10-29 18:16:54 -0700978 unsigned long onlined_pages = 0;
979 struct zone *zone;
Yasunori Goto68113782006-06-23 02:03:11 -0700980 int need_zonelists_rebuild = 0;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700981 int nid;
982 int ret;
983 struct memory_notify arg;
Dave Hansen3947be12005-10-29 18:16:54 -0700984
Lai Jiangshand9713672012-12-11 16:01:03 -0800985 /*
986 * This doesn't need a lock to do pfn_to_page().
987 * The section can't be removed here because of the
988 * memory_block->state_mutex.
989 */
990 zone = page_zone(pfn_to_page(pfn));
991
Tang Chen4f7c6b42014-08-06 16:05:13 -0700992 if ((zone_idx(zone) > ZONE_NORMAL ||
993 online_type == MMOP_ONLINE_MOVABLE) &&
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700994 !can_online_high_movable(zone))
David Rientjes30467e02015-04-14 15:45:11 -0700995 return -EINVAL;
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800996
Tang Chen4f7c6b42014-08-06 16:05:13 -0700997 if (online_type == MMOP_ONLINE_KERNEL &&
998 zone_idx(zone) == ZONE_MOVABLE) {
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700999 if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages))
David Rientjes30467e02015-04-14 15:45:11 -07001000 return -EINVAL;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -08001001 }
Tang Chen4f7c6b42014-08-06 16:05:13 -07001002 if (online_type == MMOP_ONLINE_MOVABLE &&
1003 zone_idx(zone) == ZONE_MOVABLE - 1) {
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001004 if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages))
David Rientjes30467e02015-04-14 15:45:11 -07001005 return -EINVAL;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -08001006 }
1007
1008 /* Previous code may changed the zone of the pfn range */
1009 zone = page_zone(pfn_to_page(pfn));
1010
Yasunori Goto7b78d332007-10-21 16:41:36 -07001011 arg.start_pfn = pfn;
1012 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -08001013 node_states_check_changes_online(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001014
Xishi Qiu9c2606b2013-11-12 15:07:21 -08001015 nid = pfn_to_nid(pfn);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001016
1017 ret = memory_notify(MEM_GOING_ONLINE, &arg);
1018 ret = notifier_to_errno(ret);
1019 if (ret) {
1020 memory_notify(MEM_CANCEL_ONLINE, &arg);
David Rientjes30467e02015-04-14 15:45:11 -07001021 return ret;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001022 }
Dave Hansen3947be12005-10-29 18:16:54 -07001023 /*
Yasunori Goto68113782006-06-23 02:03:11 -07001024 * If this zone is not populated, then it is not in zonelist.
1025 * This means the page allocator ignores this zone.
1026 * So, zonelist must be updated after online.
1027 */
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001028 mutex_lock(&zonelists_mutex);
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001029 if (!populated_zone(zone)) {
Yasunori Goto68113782006-06-23 02:03:11 -07001030 need_zonelists_rebuild = 1;
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001031 build_all_zonelists(NULL, zone);
1032 }
Yasunori Goto68113782006-06-23 02:03:11 -07001033
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001034 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -07001035 online_pages_range);
Geoff Levandfd8a4222008-05-14 16:05:50 -07001036 if (ret) {
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001037 if (need_zonelists_rebuild)
1038 zone_pcp_reset(zone);
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001039 mutex_unlock(&zonelists_mutex);
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07001040 printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n",
1041 (unsigned long long) pfn << PAGE_SHIFT,
1042 (((unsigned long long) pfn + nr_pages)
1043 << PAGE_SHIFT) - 1);
Geoff Levandfd8a4222008-05-14 16:05:50 -07001044 memory_notify(MEM_CANCEL_ONLINE, &arg);
David Rientjes30467e02015-04-14 15:45:11 -07001045 return ret;
Geoff Levandfd8a4222008-05-14 16:05:50 -07001046 }
1047
Dave Hansen3947be12005-10-29 18:16:54 -07001048 zone->present_pages += onlined_pages;
Cody P Schaferaa472282013-07-03 15:02:10 -07001049
1050 pgdat_resize_lock(zone->zone_pgdat, &flags);
Yasunori Gotof2937be2006-03-09 17:33:51 -08001051 zone->zone_pgdat->node_present_pages += onlined_pages;
Cody P Schaferaa472282013-07-03 15:02:10 -07001052 pgdat_resize_unlock(zone->zone_pgdat, &flags);
1053
Jiang Liu08dff7b2012-07-31 16:43:30 -07001054 if (onlined_pages) {
Lai Jiangshand9713672012-12-11 16:01:03 -08001055 node_states_set_node(zone_to_nid(zone), &arg);
Jiang Liu08dff7b2012-07-31 16:43:30 -07001056 if (need_zonelists_rebuild)
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001057 build_all_zonelists(NULL, NULL);
Jiang Liu08dff7b2012-07-31 16:43:30 -07001058 else
1059 zone_pcp_update(zone);
1060 }
Dave Hansen3947be12005-10-29 18:16:54 -07001061
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001062 mutex_unlock(&zonelists_mutex);
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07001063
1064 init_per_zone_wmark_min();
1065
Jiang Liu08dff7b2012-07-31 16:43:30 -07001066 if (onlined_pages)
Christoph Lameter7ea15302007-10-16 01:25:29 -07001067 kswapd_run(zone_to_nid(zone));
Dave Hansen61b13992005-10-29 18:16:56 -07001068
Haicheng Li1f522502010-05-24 14:32:51 -07001069 vm_total_pages = nr_free_pagecache_pages();
Kent Liu2f7f24e2008-07-23 21:28:18 -07001070
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -07001071 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001072
1073 if (onlined_pages)
1074 memory_notify(MEM_ONLINE, &arg);
David Rientjes30467e02015-04-14 15:45:11 -07001075 return 0;
Dave Hansen3947be12005-10-29 18:16:54 -07001076}
Keith Mannthey53947022006-09-30 23:27:08 -07001077#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
Yasunori Gotobc02af92006-06-27 02:53:30 -07001078
Tang Chen0bd85422014-11-13 15:19:41 -08001079static void reset_node_present_pages(pg_data_t *pgdat)
1080{
1081 struct zone *z;
1082
1083 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
1084 z->present_pages = 0;
1085
1086 pgdat->node_present_pages = 0;
1087}
1088
Hidetoshi Setoe1319332009-11-17 14:06:18 -08001089/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1090static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001091{
1092 struct pglist_data *pgdat;
1093 unsigned long zones_size[MAX_NR_ZONES] = {0};
1094 unsigned long zholes_size[MAX_NR_ZONES] = {0};
Fabian Frederickc8e861a2014-06-04 16:07:51 -07001095 unsigned long start_pfn = PFN_DOWN(start);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001096
Tang Chena1e565a2013-02-22 16:33:18 -08001097 pgdat = NODE_DATA(nid);
1098 if (!pgdat) {
1099 pgdat = arch_alloc_nodedata(nid);
1100 if (!pgdat)
1101 return NULL;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001102
Tang Chena1e565a2013-02-22 16:33:18 -08001103 arch_refresh_nodedata(nid, pgdat);
Gu Zhengb0dc3a32015-03-25 15:55:20 -07001104 } else {
1105 /* Reset the nr_zones and classzone_idx to 0 before reuse */
1106 pgdat->nr_zones = 0;
1107 pgdat->classzone_idx = 0;
Tang Chena1e565a2013-02-22 16:33:18 -08001108 }
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001109
1110 /* we can use NODE_DATA(nid) from here */
1111
1112 /* init node's zones as empty zones, we don't have any present pages.*/
Johannes Weiner9109fb72008-07-23 21:27:20 -07001113 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001114
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -07001115 /*
1116 * The node we allocated has no zone fallback lists. For avoiding
1117 * to access not-initialized zonelist, build here.
1118 */
David Rientjesf957db42011-06-22 18:13:04 -07001119 mutex_lock(&zonelists_mutex);
Jiang Liu9adb62a2012-07-31 16:43:28 -07001120 build_all_zonelists(pgdat, NULL);
David Rientjesf957db42011-06-22 18:13:04 -07001121 mutex_unlock(&zonelists_mutex);
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -07001122
Tang Chenf784a3f2014-11-13 15:19:39 -08001123 /*
1124 * zone->managed_pages is set to an approximate value in
1125 * free_area_init_core(), which will cause
1126 * /sys/device/system/node/nodeX/meminfo has wrong data.
1127 * So reset it to 0 before any memory is onlined.
1128 */
1129 reset_node_managed_pages(pgdat);
1130
Tang Chen0bd85422014-11-13 15:19:41 -08001131 /*
1132 * When memory is hot-added, all the memory is in offline state. So
1133 * clear all zones' present_pages because they will be updated in
1134 * online_pages() and offline_pages().
1135 */
1136 reset_node_present_pages(pgdat);
1137
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001138 return pgdat;
1139}
1140
1141static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
1142{
1143 arch_refresh_nodedata(nid, NULL);
1144 arch_free_nodedata(pgdat);
1145 return;
1146}
1147
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -07001148
Toshi Kani01b0f192013-11-12 15:07:25 -08001149/**
1150 * try_online_node - online a node if offlined
1151 *
minskey guocf234222010-05-24 14:32:41 -07001152 * called by cpu_up() to online a node without onlined memory.
1153 */
Toshi Kani01b0f192013-11-12 15:07:25 -08001154int try_online_node(int nid)
minskey guocf234222010-05-24 14:32:41 -07001155{
1156 pg_data_t *pgdat;
1157 int ret;
1158
Toshi Kani01b0f192013-11-12 15:07:25 -08001159 if (node_online(nid))
1160 return 0;
1161
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001162 mem_hotplug_begin();
minskey guocf234222010-05-24 14:32:41 -07001163 pgdat = hotadd_new_pgdat(nid, 0);
David Rientjes7553e8f2011-06-22 18:13:01 -07001164 if (!pgdat) {
Toshi Kani01b0f192013-11-12 15:07:25 -08001165 pr_err("Cannot online node %d due to NULL pgdat\n", nid);
minskey guocf234222010-05-24 14:32:41 -07001166 ret = -ENOMEM;
1167 goto out;
1168 }
1169 node_set_online(nid);
1170 ret = register_one_node(nid);
1171 BUG_ON(ret);
1172
Toshi Kani01b0f192013-11-12 15:07:25 -08001173 if (pgdat->node_zonelists->_zonerefs->zone == NULL) {
1174 mutex_lock(&zonelists_mutex);
1175 build_all_zonelists(NULL, NULL);
1176 mutex_unlock(&zonelists_mutex);
1177 }
1178
minskey guocf234222010-05-24 14:32:41 -07001179out:
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001180 mem_hotplug_done();
minskey guocf234222010-05-24 14:32:41 -07001181 return ret;
1182}
1183
Toshi Kani27356f52013-09-11 14:21:49 -07001184static int check_hotplug_memory_range(u64 start, u64 size)
1185{
Fabian Frederickc8e861a2014-06-04 16:07:51 -07001186 u64 start_pfn = PFN_DOWN(start);
Toshi Kani27356f52013-09-11 14:21:49 -07001187 u64 nr_pages = size >> PAGE_SHIFT;
1188
1189 /* Memory range must be aligned with section */
1190 if ((start_pfn & ~PAGE_SECTION_MASK) ||
1191 (nr_pages % PAGES_PER_SECTION) || (!nr_pages)) {
1192 pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
1193 (unsigned long long)start,
1194 (unsigned long long)size);
1195 return -EINVAL;
1196 }
1197
1198 return 0;
1199}
1200
Wang Nan63264402014-08-06 16:07:36 -07001201/*
1202 * If movable zone has already been setup, newly added memory should be check.
1203 * If its address is higher than movable zone, it should be added as movable.
1204 * Without this check, movable zone may overlap with other zone.
1205 */
1206static int should_add_memory_movable(int nid, u64 start, u64 size)
1207{
1208 unsigned long start_pfn = start >> PAGE_SHIFT;
1209 pg_data_t *pgdat = NODE_DATA(nid);
1210 struct zone *movable_zone = pgdat->node_zones + ZONE_MOVABLE;
1211
1212 if (zone_is_empty(movable_zone))
1213 return 0;
1214
1215 if (movable_zone->zone_start_pfn <= start_pfn)
1216 return 1;
1217
1218 return 0;
1219}
1220
Dan Williams033fbae2015-08-09 15:29:06 -04001221int zone_for_memory(int nid, u64 start, u64 size, int zone_default,
1222 bool for_device)
Wang Nan63264402014-08-06 16:07:36 -07001223{
Dan Williams033fbae2015-08-09 15:29:06 -04001224#ifdef CONFIG_ZONE_DEVICE
1225 if (for_device)
1226 return ZONE_DEVICE;
1227#endif
Wang Nan63264402014-08-06 16:07:36 -07001228 if (should_add_memory_movable(nid, start, size))
1229 return ZONE_MOVABLE;
1230
1231 return zone_default;
1232}
1233
Al Viro31168482008-11-22 17:33:24 +00001234/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
David Vrabel62cedb92015-06-25 16:35:49 +01001235int __ref add_memory_resource(int nid, struct resource *res)
Yasunori Gotobc02af92006-06-27 02:53:30 -07001236{
David Vrabel62cedb92015-06-25 16:35:49 +01001237 u64 start, size;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001238 pg_data_t *pgdat = NULL;
Tang Chena1e565a2013-02-22 16:33:18 -08001239 bool new_pgdat;
1240 bool new_node;
Yasunori Gotobc02af92006-06-27 02:53:30 -07001241 int ret;
1242
David Vrabel62cedb92015-06-25 16:35:49 +01001243 start = res->start;
1244 size = resource_size(res);
1245
Toshi Kani27356f52013-09-11 14:21:49 -07001246 ret = check_hotplug_memory_range(start, size);
1247 if (ret)
1248 return ret;
1249
Tang Chena1e565a2013-02-22 16:33:18 -08001250 { /* Stupid hack to suppress address-never-null warning */
1251 void *p = NODE_DATA(nid);
1252 new_pgdat = !p;
1253 }
Nathan Zimmerac13c462014-01-23 15:53:26 -08001254
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001255 mem_hotplug_begin();
Nathan Zimmerac13c462014-01-23 15:53:26 -08001256
Tang Chen7f36e3e2015-09-04 15:42:32 -07001257 /*
1258 * Add new range to memblock so that when hotadd_new_pgdat() is called
1259 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
1260 * this new range and calculate total pages correctly. The range will
1261 * be removed at hot-remove time.
1262 */
1263 memblock_add_node(start, size, nid);
1264
Tang Chena1e565a2013-02-22 16:33:18 -08001265 new_node = !node_online(nid);
1266 if (new_node) {
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001267 pgdat = hotadd_new_pgdat(nid, start);
Andi Kleen6ad696d2009-11-17 14:06:22 -08001268 ret = -ENOMEM;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001269 if (!pgdat)
Wen Congyang41b9e2d2012-07-11 14:02:31 -07001270 goto error;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001271 }
1272
Yasunori Gotobc02af92006-06-27 02:53:30 -07001273 /* call arch's memory hotadd */
Dan Williams033fbae2015-08-09 15:29:06 -04001274 ret = arch_add_memory(nid, start, size, false);
Yasunori Gotobc02af92006-06-27 02:53:30 -07001275
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001276 if (ret < 0)
1277 goto error;
1278
Yasunori Goto0fc44152006-06-27 02:53:38 -07001279 /* we online node here. we can't roll back from here. */
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001280 node_set_online(nid);
1281
Tang Chena1e565a2013-02-22 16:33:18 -08001282 if (new_node) {
Yasunori Goto0fc44152006-06-27 02:53:38 -07001283 ret = register_one_node(nid);
1284 /*
1285 * If sysfs file of new node can't create, cpu on the node
1286 * can't be hot-added. There is no rollback way now.
1287 * So, check by BUG_ON() to catch it reluctantly..
1288 */
1289 BUG_ON(ret);
1290 }
1291
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -08001292 /* create new memmap entry */
1293 firmware_map_add_hotplug(start, start + size, "System RAM");
1294
Andi Kleen6ad696d2009-11-17 14:06:22 -08001295 goto out;
1296
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001297error:
1298 /* rollback pgdat allocation and others */
1299 if (new_pgdat)
1300 rollback_node_hotadd(nid, pgdat);
Tang Chen7f36e3e2015-09-04 15:42:32 -07001301 memblock_remove(start, size);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001302
Andi Kleen6ad696d2009-11-17 14:06:22 -08001303out:
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001304 mem_hotplug_done();
Yasunori Gotobc02af92006-06-27 02:53:30 -07001305 return ret;
1306}
David Vrabel62cedb92015-06-25 16:35:49 +01001307EXPORT_SYMBOL_GPL(add_memory_resource);
1308
1309int __ref add_memory(int nid, u64 start, u64 size)
1310{
1311 struct resource *res;
1312 int ret;
1313
1314 res = register_memory_resource(start, size);
1315 if (!res)
1316 return -EEXIST;
1317
1318 ret = add_memory_resource(nid, res);
1319 if (ret < 0)
1320 release_memory_resource(res);
1321 return ret;
1322}
Yasunori Gotobc02af92006-06-27 02:53:30 -07001323EXPORT_SYMBOL_GPL(add_memory);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001324
1325#ifdef CONFIG_MEMORY_HOTREMOVE
1326/*
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001327 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1328 * set and the size of the free page is given by page_order(). Using this,
1329 * the function determines if the pageblock contains only free pages.
1330 * Due to buddy contraints, a free page at least the size of a pageblock will
1331 * be located at the start of the pageblock
1332 */
1333static inline int pageblock_free(struct page *page)
1334{
1335 return PageBuddy(page) && page_order(page) >= pageblock_order;
1336}
1337
1338/* Return the start of the next active pageblock after a given page */
1339static struct page *next_active_pageblock(struct page *page)
1340{
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001341 /* Ensure the starting page is pageblock-aligned */
1342 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
1343
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001344 /* If the entire pageblock is free, move to the end of free page */
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -07001345 if (pageblock_free(page)) {
1346 int order;
1347 /* be careful. we don't have locks, page_order can be changed.*/
1348 order = page_order(page);
1349 if ((order < MAX_ORDER) && (order >= pageblock_order))
1350 return page + (1 << order);
1351 }
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001352
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -07001353 return page + pageblock_nr_pages;
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001354}
1355
1356/* Checks if this range of memory is likely to be hot-removable. */
1357int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
1358{
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001359 struct page *page = pfn_to_page(start_pfn);
1360 struct page *end_page = page + nr_pages;
1361
1362 /* Check the starting page of each pageblock within the range */
1363 for (; page < end_page; page = next_active_pageblock(page)) {
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07001364 if (!is_pageblock_removable_nolock(page))
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001365 return 0;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07001366 cond_resched();
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001367 }
1368
1369 /* All pageblocks in the memory block are likely to be hot-removable */
1370 return 1;
1371}
1372
1373/*
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001374 * Confirm all pages in a range [start, end) is belongs to the same zone.
1375 */
Zhang Zhened2f2402014-10-09 15:26:31 -07001376int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001377{
1378 unsigned long pfn;
1379 struct zone *zone = NULL;
1380 struct page *page;
1381 int i;
1382 for (pfn = start_pfn;
1383 pfn < end_pfn;
1384 pfn += MAX_ORDER_NR_PAGES) {
1385 i = 0;
1386 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1387 while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
1388 i++;
1389 if (i == MAX_ORDER_NR_PAGES)
1390 continue;
1391 page = pfn_to_page(pfn + i);
1392 if (zone && page_zone(page) != zone)
1393 return 0;
1394 zone = page_zone(page);
1395 }
1396 return 1;
1397}
1398
1399/*
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001400 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages
1401 * and hugepages). We scan pfn because it's much easier than scanning over
1402 * linked list. This function returns the pfn of the first found movable
1403 * page if it's found, otherwise 0.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001404 */
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001405static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001406{
1407 unsigned long pfn;
1408 struct page *page;
1409 for (pfn = start; pfn < end; pfn++) {
1410 if (pfn_valid(pfn)) {
1411 page = pfn_to_page(pfn);
1412 if (PageLRU(page))
1413 return pfn;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001414 if (PageHuge(page)) {
Naoya Horiguchi7e1f0492015-04-15 16:14:41 -07001415 if (page_huge_active(page))
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001416 return pfn;
1417 else
1418 pfn = round_up(pfn + 1,
1419 1 << compound_order(page)) - 1;
1420 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001421 }
1422 }
1423 return 0;
1424}
1425
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001426#define NR_OFFLINE_AT_ONCE_PAGES (256)
1427static int
1428do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1429{
1430 unsigned long pfn;
1431 struct page *page;
1432 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
1433 int not_managed = 0;
1434 int ret = 0;
1435 LIST_HEAD(source);
1436
1437 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
1438 if (!pfn_valid(pfn))
1439 continue;
1440 page = pfn_to_page(pfn);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001441
1442 if (PageHuge(page)) {
1443 struct page *head = compound_head(page);
1444 pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
1445 if (compound_order(head) > PFN_SECTION_SHIFT) {
1446 ret = -EBUSY;
1447 break;
1448 }
1449 if (isolate_huge_page(page, &source))
1450 move_pages -= 1 << compound_order(head);
1451 continue;
1452 }
1453
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001454 if (!get_page_unless_zero(page))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001455 continue;
1456 /*
1457 * We can skip free pages. And we can only deal with pages on
1458 * LRU.
1459 */
Nick Piggin62695a82008-10-18 20:26:09 -07001460 ret = isolate_lru_page(page);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001461 if (!ret) { /* Success */
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001462 put_page(page);
Nick Piggin62695a82008-10-18 20:26:09 -07001463 list_add_tail(&page->lru, &source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001464 move_pages--;
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001465 inc_zone_page_state(page, NR_ISOLATED_ANON +
1466 page_is_file_cache(page));
1467
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001468 } else {
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001469#ifdef CONFIG_DEBUG_VM
Wu Fengguang718a3822010-03-10 15:20:43 -08001470 printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
1471 pfn);
Dave Hansenf0b791a2014-01-23 15:52:49 -08001472 dump_page(page, "failed to remove from LRU");
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001473#endif
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001474 put_page(page);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001475 /* Because we don't have big zone->lock. we should
Bob Liu809c4442010-10-26 14:22:10 -07001476 check this again here. */
1477 if (page_count(page)) {
1478 not_managed++;
Bob Liuf3ab2632010-10-26 14:22:10 -07001479 ret = -EBUSY;
Bob Liu809c4442010-10-26 14:22:10 -07001480 break;
1481 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001482 }
1483 }
Bob Liuf3ab2632010-10-26 14:22:10 -07001484 if (!list_empty(&source)) {
1485 if (not_managed) {
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001486 putback_movable_pages(&source);
Bob Liuf3ab2632010-10-26 14:22:10 -07001487 goto out;
1488 }
Minchan Kim74c08f92012-10-08 16:32:54 -07001489
1490 /*
1491 * alloc_migrate_target should be improooooved!!
1492 * migrate_pages returns # of failed pages.
1493 */
David Rientjes68711a72014-06-04 16:08:25 -07001494 ret = migrate_pages(&source, alloc_migrate_target, NULL, 0,
Hugh Dickins9c620e22013-02-22 16:35:14 -08001495 MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
Bob Liuf3ab2632010-10-26 14:22:10 -07001496 if (ret)
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001497 putback_movable_pages(&source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001498 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001499out:
1500 return ret;
1501}
1502
1503/*
1504 * remove from free_area[] and mark all as Reserved.
1505 */
1506static int
1507offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1508 void *data)
1509{
1510 __offline_isolated_pages(start, start + nr_pages);
1511 return 0;
1512}
1513
1514static void
1515offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1516{
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001517 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001518 offline_isolated_pages_cb);
1519}
1520
1521/*
1522 * Check all pages in range, recoreded as memory resource, are isolated.
1523 */
1524static int
1525check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1526 void *data)
1527{
1528 int ret;
1529 long offlined = *(long *)data;
Wen Congyangb023f462012-12-11 16:00:45 -08001530 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001531 offlined = nr_pages;
1532 if (!ret)
1533 *(long *)data += offlined;
1534 return ret;
1535}
1536
1537static long
1538check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1539{
1540 long offlined = 0;
1541 int ret;
1542
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001543 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001544 check_pages_isolated_cb);
1545 if (ret < 0)
1546 offlined = (long)ret;
1547 return offlined;
1548}
1549
Lai Jiangshan09285af2012-12-12 13:52:04 -08001550#ifdef CONFIG_MOVABLE_NODE
Tang Chen79a4dce2012-12-18 14:23:24 -08001551/*
1552 * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
1553 * normal memory.
1554 */
Lai Jiangshan09285af2012-12-12 13:52:04 -08001555static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1556{
1557 return true;
1558}
Tang Chen79a4dce2012-12-18 14:23:24 -08001559#else /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001560/* ensure the node has NORMAL memory if it is still online */
1561static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1562{
1563 struct pglist_data *pgdat = zone->zone_pgdat;
1564 unsigned long present_pages = 0;
1565 enum zone_type zt;
1566
1567 for (zt = 0; zt <= ZONE_NORMAL; zt++)
1568 present_pages += pgdat->node_zones[zt].present_pages;
1569
1570 if (present_pages > nr_pages)
1571 return true;
1572
1573 present_pages = 0;
1574 for (; zt <= ZONE_MOVABLE; zt++)
1575 present_pages += pgdat->node_zones[zt].present_pages;
1576
1577 /*
1578 * we can't offline the last normal memory until all
1579 * higher memory is offlined.
1580 */
1581 return present_pages == 0;
1582}
Tang Chen79a4dce2012-12-18 14:23:24 -08001583#endif /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001584
Tang Chenc5320922013-11-12 15:08:10 -08001585static int __init cmdline_parse_movable_node(char *p)
1586{
1587#ifdef CONFIG_MOVABLE_NODE
1588 /*
1589 * Memory used by the kernel cannot be hot-removed because Linux
1590 * cannot migrate the kernel pages. When memory hotplug is
1591 * enabled, we should prevent memblock from allocating memory
1592 * for the kernel.
1593 *
1594 * ACPI SRAT records all hotpluggable memory ranges. But before
1595 * SRAT is parsed, we don't know about it.
1596 *
1597 * The kernel image is loaded into memory at very early time. We
1598 * cannot prevent this anyway. So on NUMA system, we set any
1599 * node the kernel resides in as un-hotpluggable.
1600 *
1601 * Since on modern servers, one node could have double-digit
1602 * gigabytes memory, we can assume the memory around the kernel
1603 * image is also un-hotpluggable. So before SRAT is parsed, just
1604 * allocate memory near the kernel image to try the best to keep
1605 * the kernel away from hotpluggable memory.
1606 */
1607 memblock_set_bottom_up(true);
Tang Chen55ac5902014-01-21 15:49:35 -08001608 movable_node_enabled = true;
Tang Chenc5320922013-11-12 15:08:10 -08001609#else
1610 pr_warn("movable_node option not supported\n");
1611#endif
1612 return 0;
1613}
1614early_param("movable_node", cmdline_parse_movable_node);
1615
Lai Jiangshand9713672012-12-11 16:01:03 -08001616/* check which state of node_states will be changed when offline memory */
1617static void node_states_check_changes_offline(unsigned long nr_pages,
1618 struct zone *zone, struct memory_notify *arg)
1619{
1620 struct pglist_data *pgdat = zone->zone_pgdat;
1621 unsigned long present_pages = 0;
1622 enum zone_type zt, zone_last = ZONE_NORMAL;
1623
1624 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001625 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1626 * contains nodes which have zones of 0...ZONE_NORMAL,
1627 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -08001628 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001629 * If we don't have HIGHMEM nor movable node,
1630 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1631 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -08001632 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001633 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -08001634 zone_last = ZONE_MOVABLE;
1635
1636 /*
1637 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1638 * If the memory to be offline is in a zone of 0...zone_last,
1639 * and it is the last present memory, 0...zone_last will
1640 * become empty after offline , thus we can determind we will
1641 * need to clear the node from node_states[N_NORMAL_MEMORY].
1642 */
1643 for (zt = 0; zt <= zone_last; zt++)
1644 present_pages += pgdat->node_zones[zt].present_pages;
1645 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1646 arg->status_change_nid_normal = zone_to_nid(zone);
1647 else
1648 arg->status_change_nid_normal = -1;
1649
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001650#ifdef CONFIG_HIGHMEM
1651 /*
1652 * If we have movable node, node_states[N_HIGH_MEMORY]
1653 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1654 * set zone_last to ZONE_HIGHMEM.
1655 *
1656 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1657 * contains nodes which have zones of 0...ZONE_MOVABLE,
1658 * set zone_last to ZONE_MOVABLE.
1659 */
1660 zone_last = ZONE_HIGHMEM;
1661 if (N_MEMORY == N_HIGH_MEMORY)
1662 zone_last = ZONE_MOVABLE;
1663
1664 for (; zt <= zone_last; zt++)
1665 present_pages += pgdat->node_zones[zt].present_pages;
1666 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1667 arg->status_change_nid_high = zone_to_nid(zone);
1668 else
1669 arg->status_change_nid_high = -1;
1670#else
1671 arg->status_change_nid_high = arg->status_change_nid_normal;
1672#endif
1673
Lai Jiangshand9713672012-12-11 16:01:03 -08001674 /*
1675 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1676 */
1677 zone_last = ZONE_MOVABLE;
1678
1679 /*
1680 * check whether node_states[N_HIGH_MEMORY] will be changed
1681 * If we try to offline the last present @nr_pages from the node,
1682 * we can determind we will need to clear the node from
1683 * node_states[N_HIGH_MEMORY].
1684 */
1685 for (; zt <= zone_last; zt++)
1686 present_pages += pgdat->node_zones[zt].present_pages;
1687 if (nr_pages >= present_pages)
1688 arg->status_change_nid = zone_to_nid(zone);
1689 else
1690 arg->status_change_nid = -1;
1691}
1692
1693static void node_states_clear_node(int node, struct memory_notify *arg)
1694{
1695 if (arg->status_change_nid_normal >= 0)
1696 node_clear_state(node, N_NORMAL_MEMORY);
1697
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001698 if ((N_MEMORY != N_NORMAL_MEMORY) &&
1699 (arg->status_change_nid_high >= 0))
Lai Jiangshand9713672012-12-11 16:01:03 -08001700 node_clear_state(node, N_HIGH_MEMORY);
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001701
1702 if ((N_MEMORY != N_HIGH_MEMORY) &&
1703 (arg->status_change_nid >= 0))
1704 node_clear_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -08001705}
1706
Wen Congyanga16cee12012-10-08 16:33:58 -07001707static int __ref __offline_pages(unsigned long start_pfn,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001708 unsigned long end_pfn, unsigned long timeout)
1709{
1710 unsigned long pfn, nr_pages, expire;
1711 long offlined_pages;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001712 int ret, drain, retry_max, node;
Cody P Schaferd7029092013-07-03 15:02:11 -07001713 unsigned long flags;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001714 struct zone *zone;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001715 struct memory_notify arg;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001716
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001717 /* at least, alignment against pageblock is necessary */
1718 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1719 return -EINVAL;
1720 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1721 return -EINVAL;
1722 /* This makes hotplug much easier...and readable.
1723 we assume this for now. .*/
1724 if (!test_pages_in_a_zone(start_pfn, end_pfn))
1725 return -EINVAL;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001726
1727 zone = page_zone(pfn_to_page(start_pfn));
1728 node = zone_to_nid(zone);
1729 nr_pages = end_pfn - start_pfn;
1730
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001731 if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
David Rientjes30467e02015-04-14 15:45:11 -07001732 return -EINVAL;
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001733
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001734 /* set above range as isolated */
Wen Congyangb023f462012-12-11 16:00:45 -08001735 ret = start_isolate_page_range(start_pfn, end_pfn,
1736 MIGRATE_MOVABLE, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001737 if (ret)
David Rientjes30467e02015-04-14 15:45:11 -07001738 return ret;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001739
1740 arg.start_pfn = start_pfn;
1741 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -08001742 node_states_check_changes_offline(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001743
1744 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1745 ret = notifier_to_errno(ret);
1746 if (ret)
1747 goto failed_removal;
1748
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001749 pfn = start_pfn;
1750 expire = jiffies + timeout;
1751 drain = 0;
1752 retry_max = 5;
1753repeat:
1754 /* start memory hot removal */
1755 ret = -EAGAIN;
1756 if (time_after(jiffies, expire))
1757 goto failed_removal;
1758 ret = -EINTR;
1759 if (signal_pending(current))
1760 goto failed_removal;
1761 ret = 0;
1762 if (drain) {
1763 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001764 cond_resched();
Vlastimil Babkac0554322014-12-10 15:43:10 -08001765 drain_all_pages(zone);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001766 }
1767
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001768 pfn = scan_movable_pages(start_pfn, end_pfn);
1769 if (pfn) { /* We have movable pages */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001770 ret = do_migrate_range(pfn, end_pfn);
1771 if (!ret) {
1772 drain = 1;
1773 goto repeat;
1774 } else {
1775 if (ret < 0)
1776 if (--retry_max == 0)
1777 goto failed_removal;
1778 yield();
1779 drain = 1;
1780 goto repeat;
1781 }
1782 }
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001783 /* drain all zone's lru pagevec, this is asynchronous... */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001784 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001785 yield();
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001786 /* drain pcp pages, this is synchronous. */
Vlastimil Babkac0554322014-12-10 15:43:10 -08001787 drain_all_pages(zone);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001788 /*
1789 * dissolve free hugepages in the memory block before doing offlining
1790 * actually in order to make hugetlbfs's object counting consistent.
1791 */
1792 dissolve_free_huge_pages(start_pfn, end_pfn);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001793 /* check again */
1794 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1795 if (offlined_pages < 0) {
1796 ret = -EBUSY;
1797 goto failed_removal;
1798 }
1799 printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001800 /* Ok, all of our target is isolated.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001801 We cannot do rollback at this point. */
1802 offline_isolated_pages(start_pfn, end_pfn);
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -08001803 /* reset pagetype flags and makes migrate type to be MOVABLE */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02001804 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001805 /* removal success */
Jiang Liu3dcc0572013-07-03 15:03:21 -07001806 adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001807 zone->present_pages -= offlined_pages;
Cody P Schaferd7029092013-07-03 15:02:11 -07001808
1809 pgdat_resize_lock(zone->zone_pgdat, &flags);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001810 zone->zone_pgdat->node_present_pages -= offlined_pages;
Cody P Schaferd7029092013-07-03 15:02:11 -07001811 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001812
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07001813 init_per_zone_wmark_min();
1814
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001815 if (!populated_zone(zone)) {
Jiang Liu340175b2012-07-31 16:43:32 -07001816 zone_pcp_reset(zone);
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001817 mutex_lock(&zonelists_mutex);
1818 build_all_zonelists(NULL, NULL);
1819 mutex_unlock(&zonelists_mutex);
1820 } else
1821 zone_pcp_update(zone);
Jiang Liu340175b2012-07-31 16:43:32 -07001822
Lai Jiangshand9713672012-12-11 16:01:03 -08001823 node_states_clear_node(node, &arg);
1824 if (arg.status_change_nid >= 0)
David Rientjes8fe23e02009-12-14 17:58:33 -08001825 kswapd_stop(node);
Minchan Kimbce73942009-06-16 15:32:50 -07001826
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001827 vm_total_pages = nr_free_pagecache_pages();
1828 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001829
1830 memory_notify(MEM_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001831 return 0;
1832
1833failed_removal:
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07001834 printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n",
1835 (unsigned long long) start_pfn << PAGE_SHIFT,
1836 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001837 memory_notify(MEM_CANCEL_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001838 /* pushback to free area */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02001839 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001840 return ret;
1841}
Badari Pulavarty71088782008-10-18 20:25:58 -07001842
David Rientjes30467e02015-04-14 15:45:11 -07001843/* Must be protected by mem_hotplug_begin() */
Wen Congyanga16cee12012-10-08 16:33:58 -07001844int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1845{
1846 return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
1847}
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02001848#endif /* CONFIG_MEMORY_HOTREMOVE */
Wen Congyanga16cee12012-10-08 16:33:58 -07001849
Wen Congyangbbc76be2013-02-22 16:32:54 -08001850/**
1851 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1852 * @start_pfn: start pfn of the memory range
Toshi Kanie05c4bb2013-04-29 15:06:16 -07001853 * @end_pfn: end pfn of the memory range
Wen Congyangbbc76be2013-02-22 16:32:54 -08001854 * @arg: argument passed to func
1855 * @func: callback for each memory section walked
1856 *
1857 * This function walks through all present mem sections in range
1858 * [start_pfn, end_pfn) and call func on each mem section.
1859 *
1860 * Returns the return value of func.
1861 */
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02001862int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
Wen Congyangbbc76be2013-02-22 16:32:54 -08001863 void *arg, int (*func)(struct memory_block *, void *))
Badari Pulavarty71088782008-10-18 20:25:58 -07001864{
Wen Congyange90bdb72012-10-08 16:34:01 -07001865 struct memory_block *mem = NULL;
1866 struct mem_section *section;
Wen Congyange90bdb72012-10-08 16:34:01 -07001867 unsigned long pfn, section_nr;
1868 int ret;
Badari Pulavarty71088782008-10-18 20:25:58 -07001869
Wen Congyange90bdb72012-10-08 16:34:01 -07001870 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1871 section_nr = pfn_to_section_nr(pfn);
1872 if (!present_section_nr(section_nr))
1873 continue;
1874
1875 section = __nr_to_section(section_nr);
1876 /* same memblock? */
1877 if (mem)
1878 if ((section_nr >= mem->start_section_nr) &&
1879 (section_nr <= mem->end_section_nr))
1880 continue;
1881
1882 mem = find_memory_block_hinted(section, mem);
1883 if (!mem)
1884 continue;
1885
Wen Congyangbbc76be2013-02-22 16:32:54 -08001886 ret = func(mem, arg);
Wen Congyange90bdb72012-10-08 16:34:01 -07001887 if (ret) {
Wen Congyangbbc76be2013-02-22 16:32:54 -08001888 kobject_put(&mem->dev.kobj);
1889 return ret;
Wen Congyange90bdb72012-10-08 16:34:01 -07001890 }
1891 }
1892
1893 if (mem)
1894 kobject_put(&mem->dev.kobj);
1895
Wen Congyangbbc76be2013-02-22 16:32:54 -08001896 return 0;
1897}
1898
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02001899#ifdef CONFIG_MEMORY_HOTREMOVE
Xishi Qiud6de9d52013-11-12 15:07:20 -08001900static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
Wen Congyangbbc76be2013-02-22 16:32:54 -08001901{
1902 int ret = !is_memblock_offlined(mem);
1903
Randy Dunlap349daa02013-04-29 15:08:49 -07001904 if (unlikely(ret)) {
1905 phys_addr_t beginpa, endpa;
1906
1907 beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
1908 endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
Wen Congyangbbc76be2013-02-22 16:32:54 -08001909 pr_warn("removing memory fails, because memory "
Randy Dunlap349daa02013-04-29 15:08:49 -07001910 "[%pa-%pa] is onlined\n",
1911 &beginpa, &endpa);
1912 }
Wen Congyangbbc76be2013-02-22 16:32:54 -08001913
1914 return ret;
1915}
1916
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001917static int check_cpu_on_node(pg_data_t *pgdat)
Tang Chen60a5a192013-02-22 16:33:14 -08001918{
Tang Chen60a5a192013-02-22 16:33:14 -08001919 int cpu;
1920
1921 for_each_present_cpu(cpu) {
1922 if (cpu_to_node(cpu) == pgdat->node_id)
1923 /*
1924 * the cpu on this node isn't removed, and we can't
1925 * offline this node.
1926 */
1927 return -EBUSY;
1928 }
1929
1930 return 0;
1931}
1932
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001933static void unmap_cpu_on_node(pg_data_t *pgdat)
Wen Congyange13fe862013-02-22 16:33:31 -08001934{
1935#ifdef CONFIG_ACPI_NUMA
Wen Congyange13fe862013-02-22 16:33:31 -08001936 int cpu;
1937
1938 for_each_possible_cpu(cpu)
1939 if (cpu_to_node(cpu) == pgdat->node_id)
1940 numa_clear_node(cpu);
1941#endif
1942}
1943
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001944static int check_and_unmap_cpu_on_node(pg_data_t *pgdat)
Wen Congyange13fe862013-02-22 16:33:31 -08001945{
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001946 int ret;
Wen Congyange13fe862013-02-22 16:33:31 -08001947
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001948 ret = check_cpu_on_node(pgdat);
Wen Congyange13fe862013-02-22 16:33:31 -08001949 if (ret)
1950 return ret;
1951
1952 /*
1953 * the node will be offlined when we come here, so we can clear
1954 * the cpu_to_node() now.
1955 */
1956
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001957 unmap_cpu_on_node(pgdat);
Wen Congyange13fe862013-02-22 16:33:31 -08001958 return 0;
1959}
1960
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001961/**
1962 * try_offline_node
1963 *
1964 * Offline a node if all memory sections and cpus of the node are removed.
1965 *
1966 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1967 * and online/offline operations before this call.
1968 */
Wen Congyang90b30cd2013-02-22 16:33:27 -08001969void try_offline_node(int nid)
Tang Chen60a5a192013-02-22 16:33:14 -08001970{
Wen Congyangd822b862013-02-22 16:33:16 -08001971 pg_data_t *pgdat = NODE_DATA(nid);
1972 unsigned long start_pfn = pgdat->node_start_pfn;
1973 unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
Tang Chen60a5a192013-02-22 16:33:14 -08001974 unsigned long pfn;
Wen Congyangd822b862013-02-22 16:33:16 -08001975 int i;
Tang Chen60a5a192013-02-22 16:33:14 -08001976
1977 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1978 unsigned long section_nr = pfn_to_section_nr(pfn);
1979
1980 if (!present_section_nr(section_nr))
1981 continue;
1982
1983 if (pfn_to_nid(pfn) != nid)
1984 continue;
1985
1986 /*
1987 * some memory sections of this node are not removed, and we
1988 * can't offline node now.
1989 */
1990 return;
1991 }
1992
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001993 if (check_and_unmap_cpu_on_node(pgdat))
Tang Chen60a5a192013-02-22 16:33:14 -08001994 return;
1995
1996 /*
1997 * all memory/cpu of this node are removed, we can offline this
1998 * node now.
1999 */
2000 node_set_offline(nid);
2001 unregister_one_node(nid);
Wen Congyangd822b862013-02-22 16:33:16 -08002002
Wen Congyangd822b862013-02-22 16:33:16 -08002003 /* free waittable in each zone */
2004 for (i = 0; i < MAX_NR_ZONES; i++) {
2005 struct zone *zone = pgdat->node_zones + i;
2006
Jianguo Wuca4b3f32013-03-22 15:04:50 -07002007 /*
2008 * wait_table may be allocated from boot memory,
2009 * here only free if it's allocated by vmalloc.
2010 */
Gu Zheng85bd8392015-06-10 11:14:43 -07002011 if (is_vmalloc_addr(zone->wait_table)) {
Wen Congyangd822b862013-02-22 16:33:16 -08002012 vfree(zone->wait_table);
Gu Zheng85bd8392015-06-10 11:14:43 -07002013 zone->wait_table = NULL;
2014 }
Wen Congyangd822b862013-02-22 16:33:16 -08002015 }
Tang Chen60a5a192013-02-22 16:33:14 -08002016}
Wen Congyang90b30cd2013-02-22 16:33:27 -08002017EXPORT_SYMBOL(try_offline_node);
Tang Chen60a5a192013-02-22 16:33:14 -08002018
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002019/**
2020 * remove_memory
2021 *
2022 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2023 * and online/offline operations before this call, as required by
2024 * try_offline_node().
2025 */
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002026void __ref remove_memory(int nid, u64 start, u64 size)
Wen Congyangbbc76be2013-02-22 16:32:54 -08002027{
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002028 int ret;
Wen Congyang993c1aa2013-02-22 16:32:50 -08002029
Toshi Kani27356f52013-09-11 14:21:49 -07002030 BUG_ON(check_hotplug_memory_range(start, size));
2031
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002032 mem_hotplug_begin();
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002033
2034 /*
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002035 * All memory blocks must be offlined before removing memory. Check
2036 * whether all memory blocks in question are offline and trigger a BUG()
2037 * if this is not the case.
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002038 */
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002039 ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
Xishi Qiud6de9d52013-11-12 15:07:20 -08002040 check_memblock_offlined_cb);
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002041 if (ret)
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002042 BUG();
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002043
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -08002044 /* remove memmap entry */
2045 firmware_map_remove(start, start + size, "System RAM");
Xishi Qiuf9126ab2015-08-14 15:35:16 -07002046 memblock_free(start, size);
2047 memblock_remove(start, size);
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -08002048
Wen Congyang24d335c2013-02-22 16:32:58 -08002049 arch_remove_memory(start, size);
2050
Tang Chen60a5a192013-02-22 16:33:14 -08002051 try_offline_node(nid);
2052
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002053 mem_hotplug_done();
Badari Pulavarty71088782008-10-18 20:25:58 -07002054}
Badari Pulavarty71088782008-10-18 20:25:58 -07002055EXPORT_SYMBOL_GPL(remove_memory);
Rafael J. Wysockiaba6efc2013-06-01 22:24:07 +02002056#endif /* CONFIG_MEMORY_HOTREMOVE */