blob: 484e86761b3e79dba76de62ba2a3ae5c7386e631 [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>
Dan Williams4b94ffd2016-01-15 16:56:22 -080020#include <linux/memremap.h>
Dave Hansen3947be12005-10-29 18:16:54 -070021#include <linux/memory_hotplug.h>
22#include <linux/highmem.h>
23#include <linux/vmalloc.h>
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -070024#include <linux/ioport.h>
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -070025#include <linux/delay.h>
26#include <linux/migrate.h>
27#include <linux/page-isolation.h>
Badari Pulavarty71088782008-10-18 20:25:58 -070028#include <linux/pfn.h>
Andi Kleen6ad696d2009-11-17 14:06:22 -080029#include <linux/suspend.h>
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -080030#include <linux/mm_inline.h>
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -080031#include <linux/firmware-map.h>
Tang Chen60a5a192013-02-22 16:33:14 -080032#include <linux/stop_machine.h>
Naoya Horiguchic8721bb2013-09-11 14:22:09 -070033#include <linux/hugetlb.h>
Tang Chenc5320922013-11-12 15:08:10 -080034#include <linux/memblock.h>
Tang Chenf784a3f2014-11-13 15:19:39 -080035#include <linux/bootmem.h>
Dave Hansen3947be12005-10-29 18:16:54 -070036
37#include <asm/tlbflush.h>
38
Adrian Bunk1e5ad9a2008-04-28 20:40:08 +030039#include "internal.h"
40
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -070041/*
42 * online_page_callback contains pointer to current page onlining function.
43 * Initially it is generic_online_page(). If it is required it could be
44 * changed by calling set_online_page_callback() for callback registration
45 * and restore_online_page_callback() for generic callback restore.
46 */
47
48static void generic_online_page(struct page *page);
49
50static online_page_callback_t online_page_callback = generic_online_page;
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070051static DEFINE_MUTEX(online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -070052
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070053/* The same as the cpu_hotplug lock, but for memory hotplug. */
54static struct {
55 struct task_struct *active_writer;
56 struct mutex lock; /* Synchronizes accesses to refcount, */
57 /*
58 * Also blocks the new readers during
59 * an ongoing mem hotplug operation.
60 */
61 int refcount;
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080062
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070063#ifdef CONFIG_DEBUG_LOCK_ALLOC
64 struct lockdep_map dep_map;
65#endif
66} mem_hotplug = {
67 .active_writer = NULL,
68 .lock = __MUTEX_INITIALIZER(mem_hotplug.lock),
69 .refcount = 0,
70#ifdef CONFIG_DEBUG_LOCK_ALLOC
71 .dep_map = {.name = "mem_hotplug.lock" },
72#endif
73};
74
75/* Lockdep annotations for get/put_online_mems() and mem_hotplug_begin/end() */
76#define memhp_lock_acquire_read() lock_map_acquire_read(&mem_hotplug.dep_map)
77#define memhp_lock_acquire() lock_map_acquire(&mem_hotplug.dep_map)
78#define memhp_lock_release() lock_map_release(&mem_hotplug.dep_map)
79
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -070080bool memhp_auto_online;
81EXPORT_SYMBOL_GPL(memhp_auto_online);
82
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070083void get_online_mems(void)
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080084{
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070085 might_sleep();
86 if (mem_hotplug.active_writer == current)
87 return;
88 memhp_lock_acquire_read();
89 mutex_lock(&mem_hotplug.lock);
90 mem_hotplug.refcount++;
91 mutex_unlock(&mem_hotplug.lock);
92
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080093}
94
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070095void put_online_mems(void)
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080096{
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070097 if (mem_hotplug.active_writer == current)
98 return;
99 mutex_lock(&mem_hotplug.lock);
100
101 if (WARN_ON(!mem_hotplug.refcount))
102 mem_hotplug.refcount++; /* try to fix things up */
103
104 if (!--mem_hotplug.refcount && unlikely(mem_hotplug.active_writer))
105 wake_up_process(mem_hotplug.active_writer);
106 mutex_unlock(&mem_hotplug.lock);
107 memhp_lock_release();
108
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800109}
110
David Rientjes30467e02015-04-14 15:45:11 -0700111void mem_hotplug_begin(void)
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700112{
113 mem_hotplug.active_writer = current;
114
115 memhp_lock_acquire();
116 for (;;) {
117 mutex_lock(&mem_hotplug.lock);
118 if (likely(!mem_hotplug.refcount))
119 break;
120 __set_current_state(TASK_UNINTERRUPTIBLE);
121 mutex_unlock(&mem_hotplug.lock);
122 schedule();
123 }
124}
125
David Rientjes30467e02015-04-14 15:45:11 -0700126void mem_hotplug_done(void)
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700127{
128 mem_hotplug.active_writer = NULL;
129 mutex_unlock(&mem_hotplug.lock);
130 memhp_lock_release();
131}
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -0800132
Keith Mannthey45e0b782006-09-30 23:27:09 -0700133/* add this memory to iomem resource */
134static struct resource *register_memory_resource(u64 start, u64 size)
135{
136 struct resource *res;
137 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -0800138 if (!res)
139 return ERR_PTR(-ENOMEM);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700140
141 res->name = "System RAM";
142 res->start = start;
143 res->end = start + size - 1;
Toshi Kani782b8662016-01-26 21:57:24 +0100144 res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
Keith Mannthey45e0b782006-09-30 23:27:09 -0700145 if (request_resource(&iomem_resource, res) < 0) {
Toshi Kani4996eed2013-07-03 15:02:39 -0700146 pr_debug("System RAM resource %pR cannot be added\n", res);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700147 kfree(res);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -0800148 return ERR_PTR(-EEXIST);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700149 }
150 return res;
151}
152
153static void release_memory_resource(struct resource *res)
154{
155 if (!res)
156 return;
157 release_resource(res);
158 kfree(res);
159 return;
160}
161
Keith Mannthey53947022006-09-30 23:27:08 -0700162#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800163void get_page_bootmem(unsigned long info, struct page *page,
164 unsigned long type)
Yasunori Goto04753272008-04-28 02:13:31 -0700165{
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800166 page->lru.next = (struct list_head *) type;
Yasunori Goto04753272008-04-28 02:13:31 -0700167 SetPagePrivate(page);
168 set_page_private(page, info);
169 atomic_inc(&page->_count);
170}
171
Jiang Liu170a5a72013-07-03 15:03:17 -0700172void put_page_bootmem(struct page *page)
Yasunori Goto04753272008-04-28 02:13:31 -0700173{
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800174 unsigned long type;
Yasunori Goto04753272008-04-28 02:13:31 -0700175
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800176 type = (unsigned long) page->lru.next;
177 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
178 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
Yasunori Goto04753272008-04-28 02:13:31 -0700179
180 if (atomic_dec_return(&page->_count) == 1) {
181 ClearPagePrivate(page);
182 set_page_private(page, 0);
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800183 INIT_LIST_HEAD(&page->lru);
Jiang Liu170a5a72013-07-03 15:03:17 -0700184 free_reserved_page(page);
Yasunori Goto04753272008-04-28 02:13:31 -0700185 }
Yasunori Goto04753272008-04-28 02:13:31 -0700186}
187
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800188#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
189#ifndef CONFIG_SPARSEMEM_VMEMMAP
Adrian Bunkd92bc312008-07-23 21:28:12 -0700190static void register_page_bootmem_info_section(unsigned long start_pfn)
Yasunori Goto04753272008-04-28 02:13:31 -0700191{
192 unsigned long *usemap, mapsize, section_nr, i;
193 struct mem_section *ms;
194 struct page *page, *memmap;
195
Yasunori Goto04753272008-04-28 02:13:31 -0700196 section_nr = pfn_to_section_nr(start_pfn);
197 ms = __nr_to_section(section_nr);
198
199 /* Get section's memmap address */
200 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
201
202 /*
203 * Get page for the memmap's phys address
204 * XXX: need more consideration for sparse_vmemmap...
205 */
206 page = virt_to_page(memmap);
207 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
208 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
209
210 /* remember memmap's page */
211 for (i = 0; i < mapsize; i++, page++)
212 get_page_bootmem(section_nr, page, SECTION_INFO);
213
214 usemap = __nr_to_section(section_nr)->pageblock_flags;
215 page = virt_to_page(usemap);
216
217 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
218
219 for (i = 0; i < mapsize; i++, page++)
Yasunori Gotoaf370fb2008-07-23 21:28:17 -0700220 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
Yasunori Goto04753272008-04-28 02:13:31 -0700221
222}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800223#else /* CONFIG_SPARSEMEM_VMEMMAP */
224static void register_page_bootmem_info_section(unsigned long start_pfn)
225{
226 unsigned long *usemap, mapsize, section_nr, i;
227 struct mem_section *ms;
228 struct page *page, *memmap;
229
230 if (!pfn_valid(start_pfn))
231 return;
232
233 section_nr = pfn_to_section_nr(start_pfn);
234 ms = __nr_to_section(section_nr);
235
236 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
237
238 register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
239
240 usemap = __nr_to_section(section_nr)->pageblock_flags;
241 page = virt_to_page(usemap);
242
243 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
244
245 for (i = 0; i < mapsize; i++, page++)
246 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
247}
248#endif /* !CONFIG_SPARSEMEM_VMEMMAP */
Yasunori Goto04753272008-04-28 02:13:31 -0700249
250void register_page_bootmem_info_node(struct pglist_data *pgdat)
251{
252 unsigned long i, pfn, end_pfn, nr_pages;
253 int node = pgdat->node_id;
254 struct page *page;
255 struct zone *zone;
256
257 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
258 page = virt_to_page(pgdat);
259
260 for (i = 0; i < nr_pages; i++, page++)
261 get_page_bootmem(node, page, NODE_INFO);
262
263 zone = &pgdat->node_zones[0];
264 for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
Xishi Qiu139c2d72013-09-11 14:21:46 -0700265 if (zone_is_initialized(zone)) {
Yasunori Goto04753272008-04-28 02:13:31 -0700266 nr_pages = zone->wait_table_hash_nr_entries
267 * sizeof(wait_queue_head_t);
268 nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
269 page = virt_to_page(zone->wait_table);
270
271 for (i = 0; i < nr_pages; i++, page++)
272 get_page_bootmem(node, page, NODE_INFO);
273 }
274 }
275
276 pfn = pgdat->node_start_pfn;
Cody P Schaferc1f19492013-02-22 16:35:32 -0800277 end_pfn = pgdat_end_pfn(pgdat);
Yasunori Goto04753272008-04-28 02:13:31 -0700278
Tang Chen7e9f5eb2013-07-08 16:00:23 -0700279 /* register section info */
qiuxishif14851a2012-09-17 14:09:24 -0700280 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
281 /*
282 * Some platforms can assign the same pfn to multiple nodes - on
283 * node0 as well as nodeN. To avoid registering a pfn against
284 * multiple nodes we check that this pfn does not already
Tang Chen7e9f5eb2013-07-08 16:00:23 -0700285 * reside in some other nodes.
qiuxishif14851a2012-09-17 14:09:24 -0700286 */
287 if (pfn_valid(pfn) && (pfn_to_nid(pfn) == node))
288 register_page_bootmem_info_section(pfn);
289 }
Yasunori Goto04753272008-04-28 02:13:31 -0700290}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800291#endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
Yasunori Goto04753272008-04-28 02:13:31 -0700292
Fabian Frederickf2765402014-08-06 16:04:57 -0700293static void __meminit grow_zone_span(struct zone *zone, unsigned long start_pfn,
294 unsigned long end_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700295{
296 unsigned long old_zone_end_pfn;
297
298 zone_span_writelock(zone);
299
Xishi Qiuc33bc312013-09-11 14:21:44 -0700300 old_zone_end_pfn = zone_end_pfn(zone);
Xishi Qiu8080fc02013-09-11 14:21:45 -0700301 if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700302 zone->zone_start_pfn = start_pfn;
303
304 zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
305 zone->zone_start_pfn;
306
307 zone_span_writeunlock(zone);
308}
309
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800310static void resize_zone(struct zone *zone, unsigned long start_pfn,
311 unsigned long end_pfn)
312{
313 zone_span_writelock(zone);
314
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800315 if (end_pfn - start_pfn) {
316 zone->zone_start_pfn = start_pfn;
317 zone->spanned_pages = end_pfn - start_pfn;
318 } else {
319 /*
320 * make it consist as free_area_init_core(),
321 * if spanned_pages = 0, then keep start_pfn = 0
322 */
323 zone->zone_start_pfn = 0;
324 zone->spanned_pages = 0;
325 }
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800326
327 zone_span_writeunlock(zone);
328}
329
330static void fix_zone_id(struct zone *zone, unsigned long start_pfn,
331 unsigned long end_pfn)
332{
333 enum zone_type zid = zone_idx(zone);
334 int nid = zone->zone_pgdat->node_id;
335 unsigned long pfn;
336
337 for (pfn = start_pfn; pfn < end_pfn; pfn++)
338 set_page_links(pfn_to_page(pfn), zid, nid, pfn);
339}
340
Cody P Schaferf6bbb782013-02-22 16:35:30 -0800341/* Can fail with -ENOMEM from allocating a wait table with vmalloc() or
Santosh Shilimkar9e43aa22014-01-21 15:50:43 -0800342 * alloc_bootmem_node_nopanic()/memblock_virt_alloc_node_nopanic() */
Cody P Schaferf6bbb782013-02-22 16:35:30 -0800343static int __ref ensure_zone_is_initialized(struct zone *zone,
344 unsigned long start_pfn, unsigned long num_pages)
345{
346 if (!zone_is_initialized(zone))
Yaowei Baib171e402015-11-05 18:47:06 -0800347 return init_currently_empty_zone(zone, start_pfn, num_pages);
348
Cody P Schaferf6bbb782013-02-22 16:35:30 -0800349 return 0;
350}
351
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800352static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800353 unsigned long start_pfn, unsigned long end_pfn)
354{
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800355 int ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800356 unsigned long flags;
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800357 unsigned long z1_start_pfn;
358
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800359 ret = ensure_zone_is_initialized(z1, start_pfn, end_pfn - start_pfn);
360 if (ret)
361 return ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800362
363 pgdat_resize_lock(z1->zone_pgdat, &flags);
364
365 /* can't move pfns which are higher than @z2 */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800366 if (end_pfn > zone_end_pfn(z2))
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800367 goto out_fail;
Jiang Liu834405c2013-07-03 15:03:04 -0700368 /* the move out part must be at the left most of @z2 */
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800369 if (start_pfn > z2->zone_start_pfn)
370 goto out_fail;
371 /* must included/overlap */
372 if (end_pfn <= z2->zone_start_pfn)
373 goto out_fail;
374
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800375 /* use start_pfn for z1's start_pfn if z1 is empty */
Xishi Qiu8080fc02013-09-11 14:21:45 -0700376 if (!zone_is_empty(z1))
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800377 z1_start_pfn = z1->zone_start_pfn;
378 else
379 z1_start_pfn = start_pfn;
380
381 resize_zone(z1, z1_start_pfn, end_pfn);
Cody P Schafer108bcc92013-02-22 16:35:23 -0800382 resize_zone(z2, end_pfn, zone_end_pfn(z2));
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800383
384 pgdat_resize_unlock(z1->zone_pgdat, &flags);
385
386 fix_zone_id(z1, start_pfn, end_pfn);
387
388 return 0;
389out_fail:
390 pgdat_resize_unlock(z1->zone_pgdat, &flags);
391 return -1;
392}
393
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800394static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800395 unsigned long start_pfn, unsigned long end_pfn)
396{
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800397 int ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800398 unsigned long flags;
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800399 unsigned long z2_end_pfn;
400
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800401 ret = ensure_zone_is_initialized(z2, start_pfn, end_pfn - start_pfn);
402 if (ret)
403 return ret;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800404
405 pgdat_resize_lock(z1->zone_pgdat, &flags);
406
407 /* can't move pfns which are lower than @z1 */
408 if (z1->zone_start_pfn > start_pfn)
409 goto out_fail;
410 /* the move out part mast at the right most of @z1 */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800411 if (zone_end_pfn(z1) > end_pfn)
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800412 goto out_fail;
413 /* must included/overlap */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800414 if (start_pfn >= zone_end_pfn(z1))
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800415 goto out_fail;
416
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800417 /* use end_pfn for z2's end_pfn if z2 is empty */
Xishi Qiu8080fc02013-09-11 14:21:45 -0700418 if (!zone_is_empty(z2))
Cody P Schafer108bcc92013-02-22 16:35:23 -0800419 z2_end_pfn = zone_end_pfn(z2);
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800420 else
421 z2_end_pfn = end_pfn;
422
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800423 resize_zone(z1, z1->zone_start_pfn, start_pfn);
Lai Jiangshane455a9b2012-12-11 16:03:20 -0800424 resize_zone(z2, start_pfn, z2_end_pfn);
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800425
426 pgdat_resize_unlock(z1->zone_pgdat, &flags);
427
428 fix_zone_id(z2, start_pfn, end_pfn);
429
430 return 0;
431out_fail:
432 pgdat_resize_unlock(z1->zone_pgdat, &flags);
433 return -1;
434}
435
Fabian Frederickf2765402014-08-06 16:04:57 -0700436static void __meminit grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
437 unsigned long end_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700438{
Xishi Qiu83285c72013-11-12 15:07:19 -0800439 unsigned long old_pgdat_end_pfn = pgdat_end_pfn(pgdat);
Heiko Carstens76cdd582008-05-14 16:05:52 -0700440
Tang Chen712cd382012-12-11 16:01:07 -0800441 if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
Heiko Carstens76cdd582008-05-14 16:05:52 -0700442 pgdat->node_start_pfn = start_pfn;
443
444 pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
445 pgdat->node_start_pfn;
446}
447
Al Viro31168482008-11-22 17:33:24 +0000448static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700449{
450 struct pglist_data *pgdat = zone->zone_pgdat;
451 int nr_pages = PAGES_PER_SECTION;
452 int nid = pgdat->node_id;
453 int zone_type;
Mel Gormane298ff72015-08-06 15:46:51 -0700454 unsigned long flags, pfn;
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800455 int ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700456
457 zone_type = zone - pgdat->node_zones;
Cody P Schafer64dd1b22013-02-22 16:35:31 -0800458 ret = ensure_zone_is_initialized(zone, phys_start_pfn, nr_pages);
459 if (ret)
460 return ret;
Heiko Carstens76cdd582008-05-14 16:05:52 -0700461
Heiko Carstens76cdd582008-05-14 16:05:52 -0700462 pgdat_resize_lock(zone->zone_pgdat, &flags);
463 grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
464 grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
465 phys_start_pfn + nr_pages);
466 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Dave Hansena2f3aa022007-01-10 23:15:30 -0800467 memmap_init_zone(nr_pages, nid, zone_type,
468 phys_start_pfn, MEMMAP_HOTPLUG);
Mel Gormane298ff72015-08-06 15:46:51 -0700469
470 /* online_page_range is called later and expects pages reserved */
471 for (pfn = phys_start_pfn; pfn < phys_start_pfn + nr_pages; pfn++) {
472 if (!pfn_valid(pfn))
473 continue;
474
475 SetPageReserved(pfn_to_page(pfn));
476 }
Yasunori Goto718127c2006-06-23 02:03:10 -0700477 return 0;
Dave Hansen3947be12005-10-29 18:16:54 -0700478}
479
Gary Hadec04fc582009-01-06 14:39:14 -0800480static int __meminit __add_section(int nid, struct zone *zone,
481 unsigned long phys_start_pfn)
Dave Hansen3947be12005-10-29 18:16:54 -0700482{
Dave Hansen3947be12005-10-29 18:16:54 -0700483 int ret;
484
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700485 if (pfn_valid(phys_start_pfn))
486 return -EEXIST;
487
Zhang Yanfei85b35fe2013-11-12 15:07:42 -0800488 ret = sparse_add_one_section(zone, phys_start_pfn);
Dave Hansen3947be12005-10-29 18:16:54 -0700489
490 if (ret < 0)
491 return ret;
492
Yasunori Goto718127c2006-06-23 02:03:10 -0700493 ret = __add_zone(zone, phys_start_pfn);
494
495 if (ret < 0)
496 return ret;
497
Gary Hadec04fc582009-01-06 14:39:14 -0800498 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
Dave Hansen3947be12005-10-29 18:16:54 -0700499}
500
David Rientjes4edd7ce2013-04-29 15:08:22 -0700501/*
502 * Reasonably generic function for adding memory. It is
503 * expected that archs that support memory hotplug will
504 * call this function after deciding the zone to which to
505 * add the new pages.
506 */
507int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
508 unsigned long nr_pages)
509{
510 unsigned long i;
511 int err = 0;
512 int start_sec, end_sec;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800513 struct vmem_altmap *altmap;
514
David Rientjes4edd7ce2013-04-29 15:08:22 -0700515 /* during initialize mem_map, align hot-added range to section */
516 start_sec = pfn_to_section_nr(phys_start_pfn);
517 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
518
Dan Williams4b94ffd2016-01-15 16:56:22 -0800519 altmap = to_vmem_altmap((unsigned long) pfn_to_page(phys_start_pfn));
520 if (altmap) {
521 /*
522 * Validate altmap is within bounds of the total request
523 */
524 if (altmap->base_pfn != phys_start_pfn
525 || vmem_altmap_offset(altmap) > nr_pages) {
526 pr_warn_once("memory add fail, invalid altmap\n");
527 return -EINVAL;
528 }
529 altmap->alloc = 0;
530 }
531
David Rientjes4edd7ce2013-04-29 15:08:22 -0700532 for (i = start_sec; i <= end_sec; i++) {
Sheng Yong19c07d52015-04-14 15:44:54 -0700533 err = __add_section(nid, zone, section_nr_to_pfn(i));
David Rientjes4edd7ce2013-04-29 15:08:22 -0700534
535 /*
536 * EEXIST is finally dealt with by ioresource collision
537 * check. see add_memory() => register_memory_resource()
538 * Warning will be printed if there is collision.
539 */
540 if (err && (err != -EEXIST))
541 break;
542 err = 0;
543 }
Zhu Guihuac435a392015-06-24 16:58:42 -0700544 vmemmap_populate_print_last();
David Rientjes4edd7ce2013-04-29 15:08:22 -0700545
546 return err;
547}
548EXPORT_SYMBOL_GPL(__add_pages);
549
550#ifdef CONFIG_MEMORY_HOTREMOVE
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800551/* find the smallest valid pfn in the range [start_pfn, end_pfn) */
552static int find_smallest_section_pfn(int nid, struct zone *zone,
553 unsigned long start_pfn,
554 unsigned long end_pfn)
555{
556 struct mem_section *ms;
557
558 for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
559 ms = __pfn_to_section(start_pfn);
560
561 if (unlikely(!valid_section(ms)))
562 continue;
563
564 if (unlikely(pfn_to_nid(start_pfn) != nid))
565 continue;
566
567 if (zone && zone != page_zone(pfn_to_page(start_pfn)))
568 continue;
569
570 return start_pfn;
571 }
572
573 return 0;
574}
575
576/* find the biggest valid pfn in the range [start_pfn, end_pfn). */
577static int find_biggest_section_pfn(int nid, struct zone *zone,
578 unsigned long start_pfn,
579 unsigned long end_pfn)
580{
581 struct mem_section *ms;
582 unsigned long pfn;
583
584 /* pfn is the end pfn of a memory section. */
585 pfn = end_pfn - 1;
586 for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
587 ms = __pfn_to_section(pfn);
588
589 if (unlikely(!valid_section(ms)))
590 continue;
591
592 if (unlikely(pfn_to_nid(pfn) != nid))
593 continue;
594
595 if (zone && zone != page_zone(pfn_to_page(pfn)))
596 continue;
597
598 return pfn;
599 }
600
601 return 0;
602}
603
604static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
605 unsigned long end_pfn)
606{
Xishi Qiuc33bc312013-09-11 14:21:44 -0700607 unsigned long zone_start_pfn = zone->zone_start_pfn;
608 unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
609 unsigned long zone_end_pfn = z;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800610 unsigned long pfn;
611 struct mem_section *ms;
612 int nid = zone_to_nid(zone);
613
614 zone_span_writelock(zone);
615 if (zone_start_pfn == start_pfn) {
616 /*
617 * If the section is smallest section in the zone, it need
618 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
619 * In this case, we find second smallest valid mem_section
620 * for shrinking zone.
621 */
622 pfn = find_smallest_section_pfn(nid, zone, end_pfn,
623 zone_end_pfn);
624 if (pfn) {
625 zone->zone_start_pfn = pfn;
626 zone->spanned_pages = zone_end_pfn - pfn;
627 }
628 } else if (zone_end_pfn == end_pfn) {
629 /*
630 * If the section is biggest section in the zone, it need
631 * shrink zone->spanned_pages.
632 * In this case, we find second biggest valid mem_section for
633 * shrinking zone.
634 */
635 pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
636 start_pfn);
637 if (pfn)
638 zone->spanned_pages = pfn - zone_start_pfn + 1;
639 }
640
641 /*
642 * The section is not biggest or smallest mem_section in the zone, it
643 * only creates a hole in the zone. So in this case, we need not
644 * change the zone. But perhaps, the zone has only hole data. Thus
645 * it check the zone has only hole or not.
646 */
647 pfn = zone_start_pfn;
648 for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
649 ms = __pfn_to_section(pfn);
650
651 if (unlikely(!valid_section(ms)))
652 continue;
653
654 if (page_zone(pfn_to_page(pfn)) != zone)
655 continue;
656
657 /* If the section is current section, it continues the loop */
658 if (start_pfn == pfn)
659 continue;
660
661 /* If we find valid section, we have nothing to do */
662 zone_span_writeunlock(zone);
663 return;
664 }
665
666 /* The zone has no valid section */
667 zone->zone_start_pfn = 0;
668 zone->spanned_pages = 0;
669 zone_span_writeunlock(zone);
670}
671
672static void shrink_pgdat_span(struct pglist_data *pgdat,
673 unsigned long start_pfn, unsigned long end_pfn)
674{
Xishi Qiu83285c72013-11-12 15:07:19 -0800675 unsigned long pgdat_start_pfn = pgdat->node_start_pfn;
676 unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */
677 unsigned long pgdat_end_pfn = p;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800678 unsigned long pfn;
679 struct mem_section *ms;
680 int nid = pgdat->node_id;
681
682 if (pgdat_start_pfn == start_pfn) {
683 /*
684 * If the section is smallest section in the pgdat, it need
685 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
686 * In this case, we find second smallest valid mem_section
687 * for shrinking zone.
688 */
689 pfn = find_smallest_section_pfn(nid, NULL, end_pfn,
690 pgdat_end_pfn);
691 if (pfn) {
692 pgdat->node_start_pfn = pfn;
693 pgdat->node_spanned_pages = pgdat_end_pfn - pfn;
694 }
695 } else if (pgdat_end_pfn == end_pfn) {
696 /*
697 * If the section is biggest section in the pgdat, it need
698 * shrink pgdat->node_spanned_pages.
699 * In this case, we find second biggest valid mem_section for
700 * shrinking zone.
701 */
702 pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn,
703 start_pfn);
704 if (pfn)
705 pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1;
706 }
707
708 /*
709 * If the section is not biggest or smallest mem_section in the pgdat,
710 * it only creates a hole in the pgdat. So in this case, we need not
711 * change the pgdat.
712 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
713 * has only hole or not.
714 */
715 pfn = pgdat_start_pfn;
716 for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) {
717 ms = __pfn_to_section(pfn);
718
719 if (unlikely(!valid_section(ms)))
720 continue;
721
722 if (pfn_to_nid(pfn) != nid)
723 continue;
724
725 /* If the section is current section, it continues the loop */
726 if (start_pfn == pfn)
727 continue;
728
729 /* If we find valid section, we have nothing to do */
730 return;
731 }
732
733 /* The pgdat has no valid section */
734 pgdat->node_start_pfn = 0;
735 pgdat->node_spanned_pages = 0;
736}
737
738static void __remove_zone(struct zone *zone, unsigned long start_pfn)
739{
740 struct pglist_data *pgdat = zone->zone_pgdat;
741 int nr_pages = PAGES_PER_SECTION;
742 int zone_type;
743 unsigned long flags;
744
745 zone_type = zone - pgdat->node_zones;
746
747 pgdat_resize_lock(zone->zone_pgdat, &flags);
748 shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
749 shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages);
750 pgdat_resize_unlock(zone->zone_pgdat, &flags);
751}
752
Dan Williams4b94ffd2016-01-15 16:56:22 -0800753static int __remove_section(struct zone *zone, struct mem_section *ms,
754 unsigned long map_offset)
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700755{
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800756 unsigned long start_pfn;
757 int scn_nr;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700758 int ret = -EINVAL;
759
760 if (!valid_section(ms))
761 return ret;
762
763 ret = unregister_memory_section(ms);
764 if (ret)
765 return ret;
766
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800767 scn_nr = __section_nr(ms);
768 start_pfn = section_nr_to_pfn(scn_nr);
769 __remove_zone(zone, start_pfn);
770
Dan Williams4b94ffd2016-01-15 16:56:22 -0800771 sparse_remove_one_section(zone, ms, map_offset);
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700772 return 0;
773}
774
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700775/**
776 * __remove_pages() - remove sections of pages from a zone
777 * @zone: zone from which pages need to be removed
778 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
779 * @nr_pages: number of pages to remove (must be multiple of section size)
780 *
781 * Generic helper function to remove section mappings and sysfs entries
782 * for the section of the memory we are removing. Caller needs to make
783 * sure that pages are marked reserved and zones are adjust properly by
784 * calling offline_pages().
785 */
786int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
787 unsigned long nr_pages)
788{
Toshi Kanife74ebb2013-04-29 15:08:20 -0700789 unsigned long i;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800790 unsigned long map_offset = 0;
791 int sections_to_remove, ret = 0;
792
793 /* In the ZONE_DEVICE case device driver owns the memory region */
794 if (is_dev_zone(zone)) {
795 struct page *page = pfn_to_page(phys_start_pfn);
796 struct vmem_altmap *altmap;
797
798 altmap = to_vmem_altmap((unsigned long) page);
799 if (altmap)
800 map_offset = vmem_altmap_offset(altmap);
801 } else {
802 resource_size_t start, size;
803
804 start = phys_start_pfn << PAGE_SHIFT;
805 size = nr_pages * PAGE_SIZE;
806
807 ret = release_mem_region_adjustable(&iomem_resource, start,
808 size);
809 if (ret) {
810 resource_size_t endres = start + size - 1;
811
812 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
813 &start, &endres, ret);
814 }
815 }
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700816
817 /*
818 * We can only remove entire sections
819 */
820 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
821 BUG_ON(nr_pages % PAGES_PER_SECTION);
822
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700823 sections_to_remove = nr_pages / PAGES_PER_SECTION;
824 for (i = 0; i < sections_to_remove; i++) {
825 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800826
827 ret = __remove_section(zone, __pfn_to_section(pfn), map_offset);
828 map_offset = 0;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700829 if (ret)
830 break;
831 }
832 return ret;
833}
834EXPORT_SYMBOL_GPL(__remove_pages);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700835#endif /* CONFIG_MEMORY_HOTREMOVE */
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700836
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700837int set_online_page_callback(online_page_callback_t callback)
838{
839 int rc = -EINVAL;
840
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700841 get_online_mems();
842 mutex_lock(&online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700843
844 if (online_page_callback == generic_online_page) {
845 online_page_callback = callback;
846 rc = 0;
847 }
848
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700849 mutex_unlock(&online_page_callback_lock);
850 put_online_mems();
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700851
852 return rc;
853}
854EXPORT_SYMBOL_GPL(set_online_page_callback);
855
856int restore_online_page_callback(online_page_callback_t callback)
857{
858 int rc = -EINVAL;
859
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700860 get_online_mems();
861 mutex_lock(&online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700862
863 if (online_page_callback == callback) {
864 online_page_callback = generic_online_page;
865 rc = 0;
866 }
867
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700868 mutex_unlock(&online_page_callback_lock);
869 put_online_mems();
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700870
871 return rc;
872}
873EXPORT_SYMBOL_GPL(restore_online_page_callback);
874
875void __online_page_set_limits(struct page *page)
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700876{
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700877}
878EXPORT_SYMBOL_GPL(__online_page_set_limits);
879
880void __online_page_increment_counters(struct page *page)
881{
Jiang Liu3dcc0572013-07-03 15:03:21 -0700882 adjust_managed_page_count(page, 1);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700883}
884EXPORT_SYMBOL_GPL(__online_page_increment_counters);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700885
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700886void __online_page_free(struct page *page)
887{
Jiang Liu3dcc0572013-07-03 15:03:21 -0700888 __free_reserved_page(page);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700889}
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700890EXPORT_SYMBOL_GPL(__online_page_free);
891
892static void generic_online_page(struct page *page)
893{
894 __online_page_set_limits(page);
895 __online_page_increment_counters(page);
896 __online_page_free(page);
897}
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700898
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700899static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
900 void *arg)
Dave Hansen3947be12005-10-29 18:16:54 -0700901{
902 unsigned long i;
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700903 unsigned long onlined_pages = *(unsigned long *)arg;
904 struct page *page;
905 if (PageReserved(pfn_to_page(start_pfn)))
906 for (i = 0; i < nr_pages; i++) {
907 page = pfn_to_page(start_pfn + i);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700908 (*online_page_callback)(page);
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700909 onlined_pages++;
910 }
911 *(unsigned long *)arg = onlined_pages;
912 return 0;
913}
914
Lai Jiangshan09285af2012-12-12 13:52:04 -0800915#ifdef CONFIG_MOVABLE_NODE
Tang Chen79a4dce2012-12-18 14:23:24 -0800916/*
917 * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
918 * normal memory.
919 */
Lai Jiangshan09285af2012-12-12 13:52:04 -0800920static bool can_online_high_movable(struct zone *zone)
921{
922 return true;
923}
Tang Chen79a4dce2012-12-18 14:23:24 -0800924#else /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800925/* ensure every online node has NORMAL memory */
926static bool can_online_high_movable(struct zone *zone)
927{
928 return node_state(zone_to_nid(zone), N_NORMAL_MEMORY);
929}
Tang Chen79a4dce2012-12-18 14:23:24 -0800930#endif /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -0800931
Lai Jiangshand9713672012-12-11 16:01:03 -0800932/* check which state of node_states will be changed when online memory */
933static void node_states_check_changes_online(unsigned long nr_pages,
934 struct zone *zone, struct memory_notify *arg)
935{
936 int nid = zone_to_nid(zone);
937 enum zone_type zone_last = ZONE_NORMAL;
938
939 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800940 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
941 * contains nodes which have zones of 0...ZONE_NORMAL,
942 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -0800943 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800944 * If we don't have HIGHMEM nor movable node,
945 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
946 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -0800947 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800948 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -0800949 zone_last = ZONE_MOVABLE;
950
951 /*
952 * if the memory to be online is in a zone of 0...zone_last, and
953 * the zones of 0...zone_last don't have memory before online, we will
954 * need to set the node to node_states[N_NORMAL_MEMORY] after
955 * the memory is online.
956 */
957 if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
958 arg->status_change_nid_normal = nid;
959 else
960 arg->status_change_nid_normal = -1;
961
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800962#ifdef CONFIG_HIGHMEM
963 /*
964 * If we have movable node, node_states[N_HIGH_MEMORY]
965 * contains nodes which have zones of 0...ZONE_HIGHMEM,
966 * set zone_last to ZONE_HIGHMEM.
967 *
968 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
969 * contains nodes which have zones of 0...ZONE_MOVABLE,
970 * set zone_last to ZONE_MOVABLE.
971 */
972 zone_last = ZONE_HIGHMEM;
973 if (N_MEMORY == N_HIGH_MEMORY)
974 zone_last = ZONE_MOVABLE;
975
976 if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
977 arg->status_change_nid_high = nid;
978 else
979 arg->status_change_nid_high = -1;
980#else
981 arg->status_change_nid_high = arg->status_change_nid_normal;
982#endif
983
Lai Jiangshand9713672012-12-11 16:01:03 -0800984 /*
985 * if the node don't have memory befor online, we will need to
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800986 * set the node to node_states[N_MEMORY] after the memory
Lai Jiangshand9713672012-12-11 16:01:03 -0800987 * is online.
988 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800989 if (!node_state(nid, N_MEMORY))
Lai Jiangshand9713672012-12-11 16:01:03 -0800990 arg->status_change_nid = nid;
991 else
992 arg->status_change_nid = -1;
993}
994
995static void node_states_set_node(int node, struct memory_notify *arg)
996{
997 if (arg->status_change_nid_normal >= 0)
998 node_set_state(node, N_NORMAL_MEMORY);
999
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001000 if (arg->status_change_nid_high >= 0)
1001 node_set_state(node, N_HIGH_MEMORY);
1002
1003 node_set_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -08001004}
1005
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -07001006
David Rientjes30467e02015-04-14 15:45:11 -07001007/* Must be protected by mem_hotplug_begin() */
Lai Jiangshan511c2ab2012-12-11 16:03:16 -08001008int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -07001009{
Cody P Schaferaa472282013-07-03 15:02:10 -07001010 unsigned long flags;
Dave Hansen3947be12005-10-29 18:16:54 -07001011 unsigned long onlined_pages = 0;
1012 struct zone *zone;
Yasunori Goto68113782006-06-23 02:03:11 -07001013 int need_zonelists_rebuild = 0;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001014 int nid;
1015 int ret;
1016 struct memory_notify arg;
Dave Hansen3947be12005-10-29 18:16:54 -07001017
Lai Jiangshand9713672012-12-11 16:01:03 -08001018 /*
1019 * This doesn't need a lock to do pfn_to_page().
1020 * The section can't be removed here because of the
1021 * memory_block->state_mutex.
1022 */
1023 zone = page_zone(pfn_to_page(pfn));
1024
Tang Chen4f7c6b42014-08-06 16:05:13 -07001025 if ((zone_idx(zone) > ZONE_NORMAL ||
1026 online_type == MMOP_ONLINE_MOVABLE) &&
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001027 !can_online_high_movable(zone))
David Rientjes30467e02015-04-14 15:45:11 -07001028 return -EINVAL;
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001029
Tang Chen4f7c6b42014-08-06 16:05:13 -07001030 if (online_type == MMOP_ONLINE_KERNEL &&
1031 zone_idx(zone) == ZONE_MOVABLE) {
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001032 if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages))
David Rientjes30467e02015-04-14 15:45:11 -07001033 return -EINVAL;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -08001034 }
Tang Chen4f7c6b42014-08-06 16:05:13 -07001035 if (online_type == MMOP_ONLINE_MOVABLE &&
1036 zone_idx(zone) == ZONE_MOVABLE - 1) {
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001037 if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages))
David Rientjes30467e02015-04-14 15:45:11 -07001038 return -EINVAL;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -08001039 }
1040
1041 /* Previous code may changed the zone of the pfn range */
1042 zone = page_zone(pfn_to_page(pfn));
1043
Yasunori Goto7b78d332007-10-21 16:41:36 -07001044 arg.start_pfn = pfn;
1045 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -08001046 node_states_check_changes_online(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001047
Xishi Qiu9c2606b2013-11-12 15:07:21 -08001048 nid = pfn_to_nid(pfn);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001049
1050 ret = memory_notify(MEM_GOING_ONLINE, &arg);
1051 ret = notifier_to_errno(ret);
1052 if (ret) {
1053 memory_notify(MEM_CANCEL_ONLINE, &arg);
David Rientjes30467e02015-04-14 15:45:11 -07001054 return ret;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001055 }
Dave Hansen3947be12005-10-29 18:16:54 -07001056 /*
Yasunori Goto68113782006-06-23 02:03:11 -07001057 * If this zone is not populated, then it is not in zonelist.
1058 * This means the page allocator ignores this zone.
1059 * So, zonelist must be updated after online.
1060 */
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001061 mutex_lock(&zonelists_mutex);
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001062 if (!populated_zone(zone)) {
Yasunori Goto68113782006-06-23 02:03:11 -07001063 need_zonelists_rebuild = 1;
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001064 build_all_zonelists(NULL, zone);
1065 }
Yasunori Goto68113782006-06-23 02:03:11 -07001066
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001067 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -07001068 online_pages_range);
Geoff Levandfd8a4222008-05-14 16:05:50 -07001069 if (ret) {
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001070 if (need_zonelists_rebuild)
1071 zone_pcp_reset(zone);
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001072 mutex_unlock(&zonelists_mutex);
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07001073 printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n",
1074 (unsigned long long) pfn << PAGE_SHIFT,
1075 (((unsigned long long) pfn + nr_pages)
1076 << PAGE_SHIFT) - 1);
Geoff Levandfd8a4222008-05-14 16:05:50 -07001077 memory_notify(MEM_CANCEL_ONLINE, &arg);
David Rientjes30467e02015-04-14 15:45:11 -07001078 return ret;
Geoff Levandfd8a4222008-05-14 16:05:50 -07001079 }
1080
Dave Hansen3947be12005-10-29 18:16:54 -07001081 zone->present_pages += onlined_pages;
Cody P Schaferaa472282013-07-03 15:02:10 -07001082
1083 pgdat_resize_lock(zone->zone_pgdat, &flags);
Yasunori Gotof2937be2006-03-09 17:33:51 -08001084 zone->zone_pgdat->node_present_pages += onlined_pages;
Cody P Schaferaa472282013-07-03 15:02:10 -07001085 pgdat_resize_unlock(zone->zone_pgdat, &flags);
1086
Jiang Liu08dff7b2012-07-31 16:43:30 -07001087 if (onlined_pages) {
Lai Jiangshand9713672012-12-11 16:01:03 -08001088 node_states_set_node(zone_to_nid(zone), &arg);
Jiang Liu08dff7b2012-07-31 16:43:30 -07001089 if (need_zonelists_rebuild)
Wen Congyang6dcd73d2012-12-11 16:01:01 -08001090 build_all_zonelists(NULL, NULL);
Jiang Liu08dff7b2012-07-31 16:43:30 -07001091 else
1092 zone_pcp_update(zone);
1093 }
Dave Hansen3947be12005-10-29 18:16:54 -07001094
Haicheng Li4eaf3f62010-05-24 14:32:52 -07001095 mutex_unlock(&zonelists_mutex);
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07001096
1097 init_per_zone_wmark_min();
1098
Jiang Liu08dff7b2012-07-31 16:43:30 -07001099 if (onlined_pages)
Christoph Lameter7ea15302007-10-16 01:25:29 -07001100 kswapd_run(zone_to_nid(zone));
Dave Hansen61b13992005-10-29 18:16:56 -07001101
Haicheng Li1f522502010-05-24 14:32:51 -07001102 vm_total_pages = nr_free_pagecache_pages();
Kent Liu2f7f24e2008-07-23 21:28:18 -07001103
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -07001104 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001105
1106 if (onlined_pages)
1107 memory_notify(MEM_ONLINE, &arg);
David Rientjes30467e02015-04-14 15:45:11 -07001108 return 0;
Dave Hansen3947be12005-10-29 18:16:54 -07001109}
Keith Mannthey53947022006-09-30 23:27:08 -07001110#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
Yasunori Gotobc02af92006-06-27 02:53:30 -07001111
Tang Chen0bd85422014-11-13 15:19:41 -08001112static void reset_node_present_pages(pg_data_t *pgdat)
1113{
1114 struct zone *z;
1115
1116 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
1117 z->present_pages = 0;
1118
1119 pgdat->node_present_pages = 0;
1120}
1121
Hidetoshi Setoe1319332009-11-17 14:06:18 -08001122/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
1123static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001124{
1125 struct pglist_data *pgdat;
1126 unsigned long zones_size[MAX_NR_ZONES] = {0};
1127 unsigned long zholes_size[MAX_NR_ZONES] = {0};
Fabian Frederickc8e861a2014-06-04 16:07:51 -07001128 unsigned long start_pfn = PFN_DOWN(start);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001129
Tang Chena1e565a2013-02-22 16:33:18 -08001130 pgdat = NODE_DATA(nid);
1131 if (!pgdat) {
1132 pgdat = arch_alloc_nodedata(nid);
1133 if (!pgdat)
1134 return NULL;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001135
Tang Chena1e565a2013-02-22 16:33:18 -08001136 arch_refresh_nodedata(nid, pgdat);
Gu Zhengb0dc3a32015-03-25 15:55:20 -07001137 } else {
1138 /* Reset the nr_zones and classzone_idx to 0 before reuse */
1139 pgdat->nr_zones = 0;
1140 pgdat->classzone_idx = 0;
Tang Chena1e565a2013-02-22 16:33:18 -08001141 }
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001142
1143 /* we can use NODE_DATA(nid) from here */
1144
1145 /* init node's zones as empty zones, we don't have any present pages.*/
Johannes Weiner9109fb72008-07-23 21:27:20 -07001146 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001147
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -07001148 /*
1149 * The node we allocated has no zone fallback lists. For avoiding
1150 * to access not-initialized zonelist, build here.
1151 */
David Rientjesf957db42011-06-22 18:13:04 -07001152 mutex_lock(&zonelists_mutex);
Jiang Liu9adb62a2012-07-31 16:43:28 -07001153 build_all_zonelists(pgdat, NULL);
David Rientjesf957db42011-06-22 18:13:04 -07001154 mutex_unlock(&zonelists_mutex);
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -07001155
Tang Chenf784a3f2014-11-13 15:19:39 -08001156 /*
1157 * zone->managed_pages is set to an approximate value in
1158 * free_area_init_core(), which will cause
1159 * /sys/device/system/node/nodeX/meminfo has wrong data.
1160 * So reset it to 0 before any memory is onlined.
1161 */
1162 reset_node_managed_pages(pgdat);
1163
Tang Chen0bd85422014-11-13 15:19:41 -08001164 /*
1165 * When memory is hot-added, all the memory is in offline state. So
1166 * clear all zones' present_pages because they will be updated in
1167 * online_pages() and offline_pages().
1168 */
1169 reset_node_present_pages(pgdat);
1170
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001171 return pgdat;
1172}
1173
1174static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
1175{
1176 arch_refresh_nodedata(nid, NULL);
1177 arch_free_nodedata(pgdat);
1178 return;
1179}
1180
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -07001181
Toshi Kani01b0f192013-11-12 15:07:25 -08001182/**
1183 * try_online_node - online a node if offlined
1184 *
minskey guocf234222010-05-24 14:32:41 -07001185 * called by cpu_up() to online a node without onlined memory.
1186 */
Toshi Kani01b0f192013-11-12 15:07:25 -08001187int try_online_node(int nid)
minskey guocf234222010-05-24 14:32:41 -07001188{
1189 pg_data_t *pgdat;
1190 int ret;
1191
Toshi Kani01b0f192013-11-12 15:07:25 -08001192 if (node_online(nid))
1193 return 0;
1194
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001195 mem_hotplug_begin();
minskey guocf234222010-05-24 14:32:41 -07001196 pgdat = hotadd_new_pgdat(nid, 0);
David Rientjes7553e8f2011-06-22 18:13:01 -07001197 if (!pgdat) {
Toshi Kani01b0f192013-11-12 15:07:25 -08001198 pr_err("Cannot online node %d due to NULL pgdat\n", nid);
minskey guocf234222010-05-24 14:32:41 -07001199 ret = -ENOMEM;
1200 goto out;
1201 }
1202 node_set_online(nid);
1203 ret = register_one_node(nid);
1204 BUG_ON(ret);
1205
Toshi Kani01b0f192013-11-12 15:07:25 -08001206 if (pgdat->node_zonelists->_zonerefs->zone == NULL) {
1207 mutex_lock(&zonelists_mutex);
1208 build_all_zonelists(NULL, NULL);
1209 mutex_unlock(&zonelists_mutex);
1210 }
1211
minskey guocf234222010-05-24 14:32:41 -07001212out:
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001213 mem_hotplug_done();
minskey guocf234222010-05-24 14:32:41 -07001214 return ret;
1215}
1216
Toshi Kani27356f52013-09-11 14:21:49 -07001217static int check_hotplug_memory_range(u64 start, u64 size)
1218{
Fabian Frederickc8e861a2014-06-04 16:07:51 -07001219 u64 start_pfn = PFN_DOWN(start);
Toshi Kani27356f52013-09-11 14:21:49 -07001220 u64 nr_pages = size >> PAGE_SHIFT;
1221
1222 /* Memory range must be aligned with section */
1223 if ((start_pfn & ~PAGE_SECTION_MASK) ||
1224 (nr_pages % PAGES_PER_SECTION) || (!nr_pages)) {
1225 pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
1226 (unsigned long long)start,
1227 (unsigned long long)size);
1228 return -EINVAL;
1229 }
1230
1231 return 0;
1232}
1233
Wang Nan63264402014-08-06 16:07:36 -07001234/*
1235 * If movable zone has already been setup, newly added memory should be check.
1236 * If its address is higher than movable zone, it should be added as movable.
1237 * Without this check, movable zone may overlap with other zone.
1238 */
1239static int should_add_memory_movable(int nid, u64 start, u64 size)
1240{
1241 unsigned long start_pfn = start >> PAGE_SHIFT;
1242 pg_data_t *pgdat = NODE_DATA(nid);
1243 struct zone *movable_zone = pgdat->node_zones + ZONE_MOVABLE;
1244
1245 if (zone_is_empty(movable_zone))
1246 return 0;
1247
1248 if (movable_zone->zone_start_pfn <= start_pfn)
1249 return 1;
1250
1251 return 0;
1252}
1253
Dan Williams033fbae2015-08-09 15:29:06 -04001254int zone_for_memory(int nid, u64 start, u64 size, int zone_default,
1255 bool for_device)
Wang Nan63264402014-08-06 16:07:36 -07001256{
Dan Williams033fbae2015-08-09 15:29:06 -04001257#ifdef CONFIG_ZONE_DEVICE
1258 if (for_device)
1259 return ZONE_DEVICE;
1260#endif
Wang Nan63264402014-08-06 16:07:36 -07001261 if (should_add_memory_movable(nid, start, size))
1262 return ZONE_MOVABLE;
1263
1264 return zone_default;
1265}
1266
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001267static int online_memory_block(struct memory_block *mem, void *arg)
1268{
1269 return memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
1270}
1271
Al Viro31168482008-11-22 17:33:24 +00001272/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001273int __ref add_memory_resource(int nid, struct resource *res, bool online)
Yasunori Gotobc02af92006-06-27 02:53:30 -07001274{
David Vrabel62cedb92015-06-25 16:35:49 +01001275 u64 start, size;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001276 pg_data_t *pgdat = NULL;
Tang Chena1e565a2013-02-22 16:33:18 -08001277 bool new_pgdat;
1278 bool new_node;
Yasunori Gotobc02af92006-06-27 02:53:30 -07001279 int ret;
1280
David Vrabel62cedb92015-06-25 16:35:49 +01001281 start = res->start;
1282 size = resource_size(res);
1283
Toshi Kani27356f52013-09-11 14:21:49 -07001284 ret = check_hotplug_memory_range(start, size);
1285 if (ret)
1286 return ret;
1287
Tang Chena1e565a2013-02-22 16:33:18 -08001288 { /* Stupid hack to suppress address-never-null warning */
1289 void *p = NODE_DATA(nid);
1290 new_pgdat = !p;
1291 }
Nathan Zimmerac13c462014-01-23 15:53:26 -08001292
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001293 mem_hotplug_begin();
Nathan Zimmerac13c462014-01-23 15:53:26 -08001294
Tang Chen7f36e3e2015-09-04 15:42:32 -07001295 /*
1296 * Add new range to memblock so that when hotadd_new_pgdat() is called
1297 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
1298 * this new range and calculate total pages correctly. The range will
1299 * be removed at hot-remove time.
1300 */
1301 memblock_add_node(start, size, nid);
1302
Tang Chena1e565a2013-02-22 16:33:18 -08001303 new_node = !node_online(nid);
1304 if (new_node) {
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001305 pgdat = hotadd_new_pgdat(nid, start);
Andi Kleen6ad696d2009-11-17 14:06:22 -08001306 ret = -ENOMEM;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001307 if (!pgdat)
Wen Congyang41b9e2d2012-07-11 14:02:31 -07001308 goto error;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001309 }
1310
Yasunori Gotobc02af92006-06-27 02:53:30 -07001311 /* call arch's memory hotadd */
Dan Williams033fbae2015-08-09 15:29:06 -04001312 ret = arch_add_memory(nid, start, size, false);
Yasunori Gotobc02af92006-06-27 02:53:30 -07001313
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001314 if (ret < 0)
1315 goto error;
1316
Yasunori Goto0fc44152006-06-27 02:53:38 -07001317 /* we online node here. we can't roll back from here. */
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001318 node_set_online(nid);
1319
Tang Chena1e565a2013-02-22 16:33:18 -08001320 if (new_node) {
Yasunori Goto0fc44152006-06-27 02:53:38 -07001321 ret = register_one_node(nid);
1322 /*
1323 * If sysfs file of new node can't create, cpu on the node
1324 * can't be hot-added. There is no rollback way now.
1325 * So, check by BUG_ON() to catch it reluctantly..
1326 */
1327 BUG_ON(ret);
1328 }
1329
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -08001330 /* create new memmap entry */
1331 firmware_map_add_hotplug(start, start + size, "System RAM");
1332
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001333 /* online pages if requested */
1334 if (online)
1335 walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1),
1336 NULL, online_memory_block);
1337
Andi Kleen6ad696d2009-11-17 14:06:22 -08001338 goto out;
1339
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001340error:
1341 /* rollback pgdat allocation and others */
1342 if (new_pgdat)
1343 rollback_node_hotadd(nid, pgdat);
Tang Chen7f36e3e2015-09-04 15:42:32 -07001344 memblock_remove(start, size);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001345
Andi Kleen6ad696d2009-11-17 14:06:22 -08001346out:
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001347 mem_hotplug_done();
Yasunori Gotobc02af92006-06-27 02:53:30 -07001348 return ret;
1349}
David Vrabel62cedb92015-06-25 16:35:49 +01001350EXPORT_SYMBOL_GPL(add_memory_resource);
1351
1352int __ref add_memory(int nid, u64 start, u64 size)
1353{
1354 struct resource *res;
1355 int ret;
1356
1357 res = register_memory_resource(start, size);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -08001358 if (IS_ERR(res))
1359 return PTR_ERR(res);
David Vrabel62cedb92015-06-25 16:35:49 +01001360
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001361 ret = add_memory_resource(nid, res, memhp_auto_online);
David Vrabel62cedb92015-06-25 16:35:49 +01001362 if (ret < 0)
1363 release_memory_resource(res);
1364 return ret;
1365}
Yasunori Gotobc02af92006-06-27 02:53:30 -07001366EXPORT_SYMBOL_GPL(add_memory);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001367
1368#ifdef CONFIG_MEMORY_HOTREMOVE
1369/*
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001370 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1371 * set and the size of the free page is given by page_order(). Using this,
1372 * the function determines if the pageblock contains only free pages.
1373 * Due to buddy contraints, a free page at least the size of a pageblock will
1374 * be located at the start of the pageblock
1375 */
1376static inline int pageblock_free(struct page *page)
1377{
1378 return PageBuddy(page) && page_order(page) >= pageblock_order;
1379}
1380
1381/* Return the start of the next active pageblock after a given page */
1382static struct page *next_active_pageblock(struct page *page)
1383{
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001384 /* Ensure the starting page is pageblock-aligned */
1385 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
1386
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001387 /* If the entire pageblock is free, move to the end of free page */
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -07001388 if (pageblock_free(page)) {
1389 int order;
1390 /* be careful. we don't have locks, page_order can be changed.*/
1391 order = page_order(page);
1392 if ((order < MAX_ORDER) && (order >= pageblock_order))
1393 return page + (1 << order);
1394 }
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001395
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -07001396 return page + pageblock_nr_pages;
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001397}
1398
1399/* Checks if this range of memory is likely to be hot-removable. */
1400int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
1401{
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001402 struct page *page = pfn_to_page(start_pfn);
1403 struct page *end_page = page + nr_pages;
1404
1405 /* Check the starting page of each pageblock within the range */
1406 for (; page < end_page; page = next_active_pageblock(page)) {
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07001407 if (!is_pageblock_removable_nolock(page))
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001408 return 0;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07001409 cond_resched();
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001410 }
1411
1412 /* All pageblocks in the memory block are likely to be hot-removable */
1413 return 1;
1414}
1415
1416/*
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001417 * Confirm all pages in a range [start, end) is belongs to the same zone.
1418 */
Zhang Zhened2f2402014-10-09 15:26:31 -07001419int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001420{
Andrew Banman5f0f2882015-12-29 14:54:25 -08001421 unsigned long pfn, sec_end_pfn;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001422 struct zone *zone = NULL;
1423 struct page *page;
1424 int i;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001425 for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001426 pfn < end_pfn;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001427 pfn = sec_end_pfn + 1, sec_end_pfn += PAGES_PER_SECTION) {
1428 /* Make sure the memory section is present first */
1429 if (!present_section_nr(pfn_to_section_nr(pfn)))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001430 continue;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001431 for (; pfn < sec_end_pfn && pfn < end_pfn;
1432 pfn += MAX_ORDER_NR_PAGES) {
1433 i = 0;
1434 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1435 while ((i < MAX_ORDER_NR_PAGES) &&
1436 !pfn_valid_within(pfn + i))
1437 i++;
1438 if (i == MAX_ORDER_NR_PAGES)
1439 continue;
1440 page = pfn_to_page(pfn + i);
1441 if (zone && page_zone(page) != zone)
1442 return 0;
1443 zone = page_zone(page);
1444 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001445 }
1446 return 1;
1447}
1448
1449/*
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001450 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages
1451 * and hugepages). We scan pfn because it's much easier than scanning over
1452 * linked list. This function returns the pfn of the first found movable
1453 * page if it's found, otherwise 0.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001454 */
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001455static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001456{
1457 unsigned long pfn;
1458 struct page *page;
1459 for (pfn = start; pfn < end; pfn++) {
1460 if (pfn_valid(pfn)) {
1461 page = pfn_to_page(pfn);
1462 if (PageLRU(page))
1463 return pfn;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001464 if (PageHuge(page)) {
Naoya Horiguchi7e1f0492015-04-15 16:14:41 -07001465 if (page_huge_active(page))
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001466 return pfn;
1467 else
1468 pfn = round_up(pfn + 1,
1469 1 << compound_order(page)) - 1;
1470 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001471 }
1472 }
1473 return 0;
1474}
1475
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001476#define NR_OFFLINE_AT_ONCE_PAGES (256)
1477static int
1478do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1479{
1480 unsigned long pfn;
1481 struct page *page;
1482 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
1483 int not_managed = 0;
1484 int ret = 0;
1485 LIST_HEAD(source);
1486
1487 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
1488 if (!pfn_valid(pfn))
1489 continue;
1490 page = pfn_to_page(pfn);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001491
1492 if (PageHuge(page)) {
1493 struct page *head = compound_head(page);
1494 pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
1495 if (compound_order(head) > PFN_SECTION_SHIFT) {
1496 ret = -EBUSY;
1497 break;
1498 }
1499 if (isolate_huge_page(page, &source))
1500 move_pages -= 1 << compound_order(head);
1501 continue;
1502 }
1503
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001504 if (!get_page_unless_zero(page))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001505 continue;
1506 /*
1507 * We can skip free pages. And we can only deal with pages on
1508 * LRU.
1509 */
Nick Piggin62695a82008-10-18 20:26:09 -07001510 ret = isolate_lru_page(page);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001511 if (!ret) { /* Success */
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001512 put_page(page);
Nick Piggin62695a82008-10-18 20:26:09 -07001513 list_add_tail(&page->lru, &source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001514 move_pages--;
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001515 inc_zone_page_state(page, NR_ISOLATED_ANON +
1516 page_is_file_cache(page));
1517
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001518 } else {
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001519#ifdef CONFIG_DEBUG_VM
Wu Fengguang718a3822010-03-10 15:20:43 -08001520 printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
1521 pfn);
Dave Hansenf0b791a2014-01-23 15:52:49 -08001522 dump_page(page, "failed to remove from LRU");
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001523#endif
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001524 put_page(page);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001525 /* Because we don't have big zone->lock. we should
Bob Liu809c4442010-10-26 14:22:10 -07001526 check this again here. */
1527 if (page_count(page)) {
1528 not_managed++;
Bob Liuf3ab2632010-10-26 14:22:10 -07001529 ret = -EBUSY;
Bob Liu809c4442010-10-26 14:22:10 -07001530 break;
1531 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001532 }
1533 }
Bob Liuf3ab2632010-10-26 14:22:10 -07001534 if (!list_empty(&source)) {
1535 if (not_managed) {
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001536 putback_movable_pages(&source);
Bob Liuf3ab2632010-10-26 14:22:10 -07001537 goto out;
1538 }
Minchan Kim74c08f92012-10-08 16:32:54 -07001539
1540 /*
1541 * alloc_migrate_target should be improooooved!!
1542 * migrate_pages returns # of failed pages.
1543 */
David Rientjes68711a72014-06-04 16:08:25 -07001544 ret = migrate_pages(&source, alloc_migrate_target, NULL, 0,
Hugh Dickins9c620e22013-02-22 16:35:14 -08001545 MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
Bob Liuf3ab2632010-10-26 14:22:10 -07001546 if (ret)
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001547 putback_movable_pages(&source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001548 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001549out:
1550 return ret;
1551}
1552
1553/*
1554 * remove from free_area[] and mark all as Reserved.
1555 */
1556static int
1557offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1558 void *data)
1559{
1560 __offline_isolated_pages(start, start + nr_pages);
1561 return 0;
1562}
1563
1564static void
1565offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1566{
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001567 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001568 offline_isolated_pages_cb);
1569}
1570
1571/*
1572 * Check all pages in range, recoreded as memory resource, are isolated.
1573 */
1574static int
1575check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1576 void *data)
1577{
1578 int ret;
1579 long offlined = *(long *)data;
Wen Congyangb023f462012-12-11 16:00:45 -08001580 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001581 offlined = nr_pages;
1582 if (!ret)
1583 *(long *)data += offlined;
1584 return ret;
1585}
1586
1587static long
1588check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1589{
1590 long offlined = 0;
1591 int ret;
1592
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001593 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001594 check_pages_isolated_cb);
1595 if (ret < 0)
1596 offlined = (long)ret;
1597 return offlined;
1598}
1599
Lai Jiangshan09285af2012-12-12 13:52:04 -08001600#ifdef CONFIG_MOVABLE_NODE
Tang Chen79a4dce2012-12-18 14:23:24 -08001601/*
1602 * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
1603 * normal memory.
1604 */
Lai Jiangshan09285af2012-12-12 13:52:04 -08001605static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1606{
1607 return true;
1608}
Tang Chen79a4dce2012-12-18 14:23:24 -08001609#else /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001610/* ensure the node has NORMAL memory if it is still online */
1611static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
1612{
1613 struct pglist_data *pgdat = zone->zone_pgdat;
1614 unsigned long present_pages = 0;
1615 enum zone_type zt;
1616
1617 for (zt = 0; zt <= ZONE_NORMAL; zt++)
1618 present_pages += pgdat->node_zones[zt].present_pages;
1619
1620 if (present_pages > nr_pages)
1621 return true;
1622
1623 present_pages = 0;
1624 for (; zt <= ZONE_MOVABLE; zt++)
1625 present_pages += pgdat->node_zones[zt].present_pages;
1626
1627 /*
1628 * we can't offline the last normal memory until all
1629 * higher memory is offlined.
1630 */
1631 return present_pages == 0;
1632}
Tang Chen79a4dce2012-12-18 14:23:24 -08001633#endif /* CONFIG_MOVABLE_NODE */
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001634
Tang Chenc5320922013-11-12 15:08:10 -08001635static int __init cmdline_parse_movable_node(char *p)
1636{
1637#ifdef CONFIG_MOVABLE_NODE
1638 /*
1639 * Memory used by the kernel cannot be hot-removed because Linux
1640 * cannot migrate the kernel pages. When memory hotplug is
1641 * enabled, we should prevent memblock from allocating memory
1642 * for the kernel.
1643 *
1644 * ACPI SRAT records all hotpluggable memory ranges. But before
1645 * SRAT is parsed, we don't know about it.
1646 *
1647 * The kernel image is loaded into memory at very early time. We
1648 * cannot prevent this anyway. So on NUMA system, we set any
1649 * node the kernel resides in as un-hotpluggable.
1650 *
1651 * Since on modern servers, one node could have double-digit
1652 * gigabytes memory, we can assume the memory around the kernel
1653 * image is also un-hotpluggable. So before SRAT is parsed, just
1654 * allocate memory near the kernel image to try the best to keep
1655 * the kernel away from hotpluggable memory.
1656 */
1657 memblock_set_bottom_up(true);
Tang Chen55ac5902014-01-21 15:49:35 -08001658 movable_node_enabled = true;
Tang Chenc5320922013-11-12 15:08:10 -08001659#else
1660 pr_warn("movable_node option not supported\n");
1661#endif
1662 return 0;
1663}
1664early_param("movable_node", cmdline_parse_movable_node);
1665
Lai Jiangshand9713672012-12-11 16:01:03 -08001666/* check which state of node_states will be changed when offline memory */
1667static void node_states_check_changes_offline(unsigned long nr_pages,
1668 struct zone *zone, struct memory_notify *arg)
1669{
1670 struct pglist_data *pgdat = zone->zone_pgdat;
1671 unsigned long present_pages = 0;
1672 enum zone_type zt, zone_last = ZONE_NORMAL;
1673
1674 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001675 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1676 * contains nodes which have zones of 0...ZONE_NORMAL,
1677 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -08001678 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001679 * If we don't have HIGHMEM nor movable node,
1680 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1681 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -08001682 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001683 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -08001684 zone_last = ZONE_MOVABLE;
1685
1686 /*
1687 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1688 * If the memory to be offline is in a zone of 0...zone_last,
1689 * and it is the last present memory, 0...zone_last will
1690 * become empty after offline , thus we can determind we will
1691 * need to clear the node from node_states[N_NORMAL_MEMORY].
1692 */
1693 for (zt = 0; zt <= zone_last; zt++)
1694 present_pages += pgdat->node_zones[zt].present_pages;
1695 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1696 arg->status_change_nid_normal = zone_to_nid(zone);
1697 else
1698 arg->status_change_nid_normal = -1;
1699
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001700#ifdef CONFIG_HIGHMEM
1701 /*
1702 * If we have movable node, node_states[N_HIGH_MEMORY]
1703 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1704 * set zone_last to ZONE_HIGHMEM.
1705 *
1706 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1707 * contains nodes which have zones of 0...ZONE_MOVABLE,
1708 * set zone_last to ZONE_MOVABLE.
1709 */
1710 zone_last = ZONE_HIGHMEM;
1711 if (N_MEMORY == N_HIGH_MEMORY)
1712 zone_last = ZONE_MOVABLE;
1713
1714 for (; zt <= zone_last; zt++)
1715 present_pages += pgdat->node_zones[zt].present_pages;
1716 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1717 arg->status_change_nid_high = zone_to_nid(zone);
1718 else
1719 arg->status_change_nid_high = -1;
1720#else
1721 arg->status_change_nid_high = arg->status_change_nid_normal;
1722#endif
1723
Lai Jiangshand9713672012-12-11 16:01:03 -08001724 /*
1725 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1726 */
1727 zone_last = ZONE_MOVABLE;
1728
1729 /*
1730 * check whether node_states[N_HIGH_MEMORY] will be changed
1731 * If we try to offline the last present @nr_pages from the node,
1732 * we can determind we will need to clear the node from
1733 * node_states[N_HIGH_MEMORY].
1734 */
1735 for (; zt <= zone_last; zt++)
1736 present_pages += pgdat->node_zones[zt].present_pages;
1737 if (nr_pages >= present_pages)
1738 arg->status_change_nid = zone_to_nid(zone);
1739 else
1740 arg->status_change_nid = -1;
1741}
1742
1743static void node_states_clear_node(int node, struct memory_notify *arg)
1744{
1745 if (arg->status_change_nid_normal >= 0)
1746 node_clear_state(node, N_NORMAL_MEMORY);
1747
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001748 if ((N_MEMORY != N_NORMAL_MEMORY) &&
1749 (arg->status_change_nid_high >= 0))
Lai Jiangshand9713672012-12-11 16:01:03 -08001750 node_clear_state(node, N_HIGH_MEMORY);
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001751
1752 if ((N_MEMORY != N_HIGH_MEMORY) &&
1753 (arg->status_change_nid >= 0))
1754 node_clear_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -08001755}
1756
Wen Congyanga16cee12012-10-08 16:33:58 -07001757static int __ref __offline_pages(unsigned long start_pfn,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001758 unsigned long end_pfn, unsigned long timeout)
1759{
1760 unsigned long pfn, nr_pages, expire;
1761 long offlined_pages;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001762 int ret, drain, retry_max, node;
Cody P Schaferd7029092013-07-03 15:02:11 -07001763 unsigned long flags;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001764 struct zone *zone;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001765 struct memory_notify arg;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001766
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001767 /* at least, alignment against pageblock is necessary */
1768 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1769 return -EINVAL;
1770 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1771 return -EINVAL;
1772 /* This makes hotplug much easier...and readable.
1773 we assume this for now. .*/
1774 if (!test_pages_in_a_zone(start_pfn, end_pfn))
1775 return -EINVAL;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001776
1777 zone = page_zone(pfn_to_page(start_pfn));
1778 node = zone_to_nid(zone);
1779 nr_pages = end_pfn - start_pfn;
1780
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001781 if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
David Rientjes30467e02015-04-14 15:45:11 -07001782 return -EINVAL;
Lai Jiangshan74d42d82012-12-11 16:03:23 -08001783
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001784 /* set above range as isolated */
Wen Congyangb023f462012-12-11 16:00:45 -08001785 ret = start_isolate_page_range(start_pfn, end_pfn,
1786 MIGRATE_MOVABLE, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001787 if (ret)
David Rientjes30467e02015-04-14 15:45:11 -07001788 return ret;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001789
1790 arg.start_pfn = start_pfn;
1791 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -08001792 node_states_check_changes_offline(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001793
1794 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1795 ret = notifier_to_errno(ret);
1796 if (ret)
1797 goto failed_removal;
1798
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001799 pfn = start_pfn;
1800 expire = jiffies + timeout;
1801 drain = 0;
1802 retry_max = 5;
1803repeat:
1804 /* start memory hot removal */
1805 ret = -EAGAIN;
1806 if (time_after(jiffies, expire))
1807 goto failed_removal;
1808 ret = -EINTR;
1809 if (signal_pending(current))
1810 goto failed_removal;
1811 ret = 0;
1812 if (drain) {
1813 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001814 cond_resched();
Vlastimil Babkac0554322014-12-10 15:43:10 -08001815 drain_all_pages(zone);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001816 }
1817
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001818 pfn = scan_movable_pages(start_pfn, end_pfn);
1819 if (pfn) { /* We have movable pages */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001820 ret = do_migrate_range(pfn, end_pfn);
1821 if (!ret) {
1822 drain = 1;
1823 goto repeat;
1824 } else {
1825 if (ret < 0)
1826 if (--retry_max == 0)
1827 goto failed_removal;
1828 yield();
1829 drain = 1;
1830 goto repeat;
1831 }
1832 }
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001833 /* drain all zone's lru pagevec, this is asynchronous... */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001834 lru_add_drain_all();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001835 yield();
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001836 /* drain pcp pages, this is synchronous. */
Vlastimil Babkac0554322014-12-10 15:43:10 -08001837 drain_all_pages(zone);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001838 /*
1839 * dissolve free hugepages in the memory block before doing offlining
1840 * actually in order to make hugetlbfs's object counting consistent.
1841 */
1842 dissolve_free_huge_pages(start_pfn, end_pfn);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001843 /* check again */
1844 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1845 if (offlined_pages < 0) {
1846 ret = -EBUSY;
1847 goto failed_removal;
1848 }
1849 printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001850 /* Ok, all of our target is isolated.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001851 We cannot do rollback at this point. */
1852 offline_isolated_pages(start_pfn, end_pfn);
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -08001853 /* reset pagetype flags and makes migrate type to be MOVABLE */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02001854 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001855 /* removal success */
Jiang Liu3dcc0572013-07-03 15:03:21 -07001856 adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001857 zone->present_pages -= offlined_pages;
Cody P Schaferd7029092013-07-03 15:02:11 -07001858
1859 pgdat_resize_lock(zone->zone_pgdat, &flags);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001860 zone->zone_pgdat->node_present_pages -= offlined_pages;
Cody P Schaferd7029092013-07-03 15:02:11 -07001861 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001862
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07001863 init_per_zone_wmark_min();
1864
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001865 if (!populated_zone(zone)) {
Jiang Liu340175b2012-07-31 16:43:32 -07001866 zone_pcp_reset(zone);
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001867 mutex_lock(&zonelists_mutex);
1868 build_all_zonelists(NULL, NULL);
1869 mutex_unlock(&zonelists_mutex);
1870 } else
1871 zone_pcp_update(zone);
Jiang Liu340175b2012-07-31 16:43:32 -07001872
Lai Jiangshand9713672012-12-11 16:01:03 -08001873 node_states_clear_node(node, &arg);
1874 if (arg.status_change_nid >= 0)
David Rientjes8fe23e02009-12-14 17:58:33 -08001875 kswapd_stop(node);
Minchan Kimbce73942009-06-16 15:32:50 -07001876
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001877 vm_total_pages = nr_free_pagecache_pages();
1878 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001879
1880 memory_notify(MEM_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001881 return 0;
1882
1883failed_removal:
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07001884 printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n",
1885 (unsigned long long) start_pfn << PAGE_SHIFT,
1886 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001887 memory_notify(MEM_CANCEL_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001888 /* pushback to free area */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02001889 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001890 return ret;
1891}
Badari Pulavarty71088782008-10-18 20:25:58 -07001892
David Rientjes30467e02015-04-14 15:45:11 -07001893/* Must be protected by mem_hotplug_begin() */
Wen Congyanga16cee12012-10-08 16:33:58 -07001894int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1895{
1896 return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
1897}
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02001898#endif /* CONFIG_MEMORY_HOTREMOVE */
Wen Congyanga16cee12012-10-08 16:33:58 -07001899
Wen Congyangbbc76be2013-02-22 16:32:54 -08001900/**
1901 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1902 * @start_pfn: start pfn of the memory range
Toshi Kanie05c4bb2013-04-29 15:06:16 -07001903 * @end_pfn: end pfn of the memory range
Wen Congyangbbc76be2013-02-22 16:32:54 -08001904 * @arg: argument passed to func
1905 * @func: callback for each memory section walked
1906 *
1907 * This function walks through all present mem sections in range
1908 * [start_pfn, end_pfn) and call func on each mem section.
1909 *
1910 * Returns the return value of func.
1911 */
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02001912int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
Wen Congyangbbc76be2013-02-22 16:32:54 -08001913 void *arg, int (*func)(struct memory_block *, void *))
Badari Pulavarty71088782008-10-18 20:25:58 -07001914{
Wen Congyange90bdb72012-10-08 16:34:01 -07001915 struct memory_block *mem = NULL;
1916 struct mem_section *section;
Wen Congyange90bdb72012-10-08 16:34:01 -07001917 unsigned long pfn, section_nr;
1918 int ret;
Badari Pulavarty71088782008-10-18 20:25:58 -07001919
Wen Congyange90bdb72012-10-08 16:34:01 -07001920 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1921 section_nr = pfn_to_section_nr(pfn);
1922 if (!present_section_nr(section_nr))
1923 continue;
1924
1925 section = __nr_to_section(section_nr);
1926 /* same memblock? */
1927 if (mem)
1928 if ((section_nr >= mem->start_section_nr) &&
1929 (section_nr <= mem->end_section_nr))
1930 continue;
1931
1932 mem = find_memory_block_hinted(section, mem);
1933 if (!mem)
1934 continue;
1935
Wen Congyangbbc76be2013-02-22 16:32:54 -08001936 ret = func(mem, arg);
Wen Congyange90bdb72012-10-08 16:34:01 -07001937 if (ret) {
Wen Congyangbbc76be2013-02-22 16:32:54 -08001938 kobject_put(&mem->dev.kobj);
1939 return ret;
Wen Congyange90bdb72012-10-08 16:34:01 -07001940 }
1941 }
1942
1943 if (mem)
1944 kobject_put(&mem->dev.kobj);
1945
Wen Congyangbbc76be2013-02-22 16:32:54 -08001946 return 0;
1947}
1948
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02001949#ifdef CONFIG_MEMORY_HOTREMOVE
Xishi Qiud6de9d52013-11-12 15:07:20 -08001950static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
Wen Congyangbbc76be2013-02-22 16:32:54 -08001951{
1952 int ret = !is_memblock_offlined(mem);
1953
Randy Dunlap349daa02013-04-29 15:08:49 -07001954 if (unlikely(ret)) {
1955 phys_addr_t beginpa, endpa;
1956
1957 beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
1958 endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
Wen Congyangbbc76be2013-02-22 16:32:54 -08001959 pr_warn("removing memory fails, because memory "
Randy Dunlap349daa02013-04-29 15:08:49 -07001960 "[%pa-%pa] is onlined\n",
1961 &beginpa, &endpa);
1962 }
Wen Congyangbbc76be2013-02-22 16:32:54 -08001963
1964 return ret;
1965}
1966
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001967static int check_cpu_on_node(pg_data_t *pgdat)
Tang Chen60a5a192013-02-22 16:33:14 -08001968{
Tang Chen60a5a192013-02-22 16:33:14 -08001969 int cpu;
1970
1971 for_each_present_cpu(cpu) {
1972 if (cpu_to_node(cpu) == pgdat->node_id)
1973 /*
1974 * the cpu on this node isn't removed, and we can't
1975 * offline this node.
1976 */
1977 return -EBUSY;
1978 }
1979
1980 return 0;
1981}
1982
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001983static void unmap_cpu_on_node(pg_data_t *pgdat)
Wen Congyange13fe862013-02-22 16:33:31 -08001984{
1985#ifdef CONFIG_ACPI_NUMA
Wen Congyange13fe862013-02-22 16:33:31 -08001986 int cpu;
1987
1988 for_each_possible_cpu(cpu)
1989 if (cpu_to_node(cpu) == pgdat->node_id)
1990 numa_clear_node(cpu);
1991#endif
1992}
1993
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001994static int check_and_unmap_cpu_on_node(pg_data_t *pgdat)
Wen Congyange13fe862013-02-22 16:33:31 -08001995{
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001996 int ret;
Wen Congyange13fe862013-02-22 16:33:31 -08001997
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001998 ret = check_cpu_on_node(pgdat);
Wen Congyange13fe862013-02-22 16:33:31 -08001999 if (ret)
2000 return ret;
2001
2002 /*
2003 * the node will be offlined when we come here, so we can clear
2004 * the cpu_to_node() now.
2005 */
2006
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002007 unmap_cpu_on_node(pgdat);
Wen Congyange13fe862013-02-22 16:33:31 -08002008 return 0;
2009}
2010
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002011/**
2012 * try_offline_node
2013 *
2014 * Offline a node if all memory sections and cpus of the node are removed.
2015 *
2016 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2017 * and online/offline operations before this call.
2018 */
Wen Congyang90b30cd2013-02-22 16:33:27 -08002019void try_offline_node(int nid)
Tang Chen60a5a192013-02-22 16:33:14 -08002020{
Wen Congyangd822b862013-02-22 16:33:16 -08002021 pg_data_t *pgdat = NODE_DATA(nid);
2022 unsigned long start_pfn = pgdat->node_start_pfn;
2023 unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
Tang Chen60a5a192013-02-22 16:33:14 -08002024 unsigned long pfn;
Wen Congyangd822b862013-02-22 16:33:16 -08002025 int i;
Tang Chen60a5a192013-02-22 16:33:14 -08002026
2027 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
2028 unsigned long section_nr = pfn_to_section_nr(pfn);
2029
2030 if (!present_section_nr(section_nr))
2031 continue;
2032
2033 if (pfn_to_nid(pfn) != nid)
2034 continue;
2035
2036 /*
2037 * some memory sections of this node are not removed, and we
2038 * can't offline node now.
2039 */
2040 return;
2041 }
2042
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002043 if (check_and_unmap_cpu_on_node(pgdat))
Tang Chen60a5a192013-02-22 16:33:14 -08002044 return;
2045
2046 /*
2047 * all memory/cpu of this node are removed, we can offline this
2048 * node now.
2049 */
2050 node_set_offline(nid);
2051 unregister_one_node(nid);
Wen Congyangd822b862013-02-22 16:33:16 -08002052
Wen Congyangd822b862013-02-22 16:33:16 -08002053 /* free waittable in each zone */
2054 for (i = 0; i < MAX_NR_ZONES; i++) {
2055 struct zone *zone = pgdat->node_zones + i;
2056
Jianguo Wuca4b3f32013-03-22 15:04:50 -07002057 /*
2058 * wait_table may be allocated from boot memory,
2059 * here only free if it's allocated by vmalloc.
2060 */
Gu Zheng85bd8392015-06-10 11:14:43 -07002061 if (is_vmalloc_addr(zone->wait_table)) {
Wen Congyangd822b862013-02-22 16:33:16 -08002062 vfree(zone->wait_table);
Gu Zheng85bd8392015-06-10 11:14:43 -07002063 zone->wait_table = NULL;
2064 }
Wen Congyangd822b862013-02-22 16:33:16 -08002065 }
Tang Chen60a5a192013-02-22 16:33:14 -08002066}
Wen Congyang90b30cd2013-02-22 16:33:27 -08002067EXPORT_SYMBOL(try_offline_node);
Tang Chen60a5a192013-02-22 16:33:14 -08002068
Toshi Kani0f1cfe92013-09-11 14:21:50 -07002069/**
2070 * remove_memory
2071 *
2072 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
2073 * and online/offline operations before this call, as required by
2074 * try_offline_node().
2075 */
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002076void __ref remove_memory(int nid, u64 start, u64 size)
Wen Congyangbbc76be2013-02-22 16:32:54 -08002077{
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002078 int ret;
Wen Congyang993c1aa2013-02-22 16:32:50 -08002079
Toshi Kani27356f52013-09-11 14:21:49 -07002080 BUG_ON(check_hotplug_memory_range(start, size));
2081
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002082 mem_hotplug_begin();
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002083
2084 /*
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002085 * All memory blocks must be offlined before removing memory. Check
2086 * whether all memory blocks in question are offline and trigger a BUG()
2087 * if this is not the case.
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002088 */
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002089 ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
Xishi Qiud6de9d52013-11-12 15:07:20 -08002090 check_memblock_offlined_cb);
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002091 if (ret)
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02002092 BUG();
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08002093
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -08002094 /* remove memmap entry */
2095 firmware_map_remove(start, start + size, "System RAM");
Xishi Qiuf9126ab2015-08-14 15:35:16 -07002096 memblock_free(start, size);
2097 memblock_remove(start, size);
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -08002098
Wen Congyang24d335c2013-02-22 16:32:58 -08002099 arch_remove_memory(start, size);
2100
Tang Chen60a5a192013-02-22 16:33:14 -08002101 try_offline_node(nid);
2102
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07002103 mem_hotplug_done();
Badari Pulavarty71088782008-10-18 20:25:58 -07002104}
Badari Pulavarty71088782008-10-18 20:25:58 -07002105EXPORT_SYMBOL_GPL(remove_memory);
Rafael J. Wysockiaba6efc2013-06-01 22:24:07 +02002106#endif /* CONFIG_MEMORY_HOTREMOVE */