blob: b2bd52ff760571093c1eb39cbfdc4073139e2671 [file] [log] [blame]
Dave Hansen3947be12005-10-29 18:16:54 -07001/*
2 * linux/mm/memory_hotplug.c
3 *
4 * Copyright (C)
5 */
6
Dave Hansen3947be12005-10-29 18:16:54 -07007#include <linux/stddef.h>
8#include <linux/mm.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +01009#include <linux/sched/signal.h>
Dave Hansen3947be12005-10-29 18:16:54 -070010#include <linux/swap.h>
11#include <linux/interrupt.h>
12#include <linux/pagemap.h>
Dave Hansen3947be12005-10-29 18:16:54 -070013#include <linux/compiler.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040014#include <linux/export.h>
Dave Hansen3947be12005-10-29 18:16:54 -070015#include <linux/pagevec.h>
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -070016#include <linux/writeback.h>
Dave Hansen3947be12005-10-29 18:16:54 -070017#include <linux/slab.h>
18#include <linux/sysctl.h>
19#include <linux/cpu.h>
20#include <linux/memory.h>
Dan Williams4b94ffd2016-01-15 16:56:22 -080021#include <linux/memremap.h>
Dave Hansen3947be12005-10-29 18:16:54 -070022#include <linux/memory_hotplug.h>
23#include <linux/highmem.h>
24#include <linux/vmalloc.h>
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -070025#include <linux/ioport.h>
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -070026#include <linux/delay.h>
27#include <linux/migrate.h>
28#include <linux/page-isolation.h>
Badari Pulavarty71088782008-10-18 20:25:58 -070029#include <linux/pfn.h>
Andi Kleen6ad696d2009-11-17 14:06:22 -080030#include <linux/suspend.h>
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -080031#include <linux/mm_inline.h>
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -080032#include <linux/firmware-map.h>
Tang Chen60a5a192013-02-22 16:33:14 -080033#include <linux/stop_machine.h>
Naoya Horiguchic8721bb2013-09-11 14:22:09 -070034#include <linux/hugetlb.h>
Tang Chenc5320922013-11-12 15:08:10 -080035#include <linux/memblock.h>
Tang Chenf784a3f2014-11-13 15:19:39 -080036#include <linux/bootmem.h>
Vlastimil Babka698b1b32016-03-17 14:18:08 -070037#include <linux/compaction.h>
Dave Hansen3947be12005-10-29 18:16:54 -070038
39#include <asm/tlbflush.h>
40
Adrian Bunk1e5ad9a2008-04-28 20:40:08 +030041#include "internal.h"
42
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -070043/*
44 * online_page_callback contains pointer to current page onlining function.
45 * Initially it is generic_online_page(). If it is required it could be
46 * changed by calling set_online_page_callback() for callback registration
47 * and restore_online_page_callback() for generic callback restore.
48 */
49
50static void generic_online_page(struct page *page);
51
52static online_page_callback_t online_page_callback = generic_online_page;
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070053static DEFINE_MUTEX(online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -070054
Thomas Gleixner3f906ba2017-07-10 15:50:09 -070055DEFINE_STATIC_PERCPU_RWSEM(mem_hotplug_lock);
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080056
Thomas Gleixner3f906ba2017-07-10 15:50:09 -070057void get_online_mems(void)
58{
59 percpu_down_read(&mem_hotplug_lock);
60}
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070061
Thomas Gleixner3f906ba2017-07-10 15:50:09 -070062void put_online_mems(void)
63{
64 percpu_up_read(&mem_hotplug_lock);
65}
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070066
Michal Hocko49323812017-07-06 15:41:05 -070067bool movable_node_enabled = false;
68
Vitaly Kuznetsov8604d9e2016-05-19 17:13:03 -070069#ifndef CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -070070bool memhp_auto_online;
Vitaly Kuznetsov8604d9e2016-05-19 17:13:03 -070071#else
72bool memhp_auto_online = true;
73#endif
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -070074EXPORT_SYMBOL_GPL(memhp_auto_online);
75
Vitaly Kuznetsov86dd9952016-05-19 17:13:06 -070076static int __init setup_memhp_default_state(char *str)
77{
78 if (!strcmp(str, "online"))
79 memhp_auto_online = true;
80 else if (!strcmp(str, "offline"))
81 memhp_auto_online = false;
82
83 return 1;
84}
85__setup("memhp_default_state=", setup_memhp_default_state);
86
David Rientjes30467e02015-04-14 15:45:11 -070087void mem_hotplug_begin(void)
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070088{
Thomas Gleixner3f906ba2017-07-10 15:50:09 -070089 cpus_read_lock();
90 percpu_down_write(&mem_hotplug_lock);
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070091}
92
David Rientjes30467e02015-04-14 15:45:11 -070093void mem_hotplug_done(void)
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070094{
Thomas Gleixner3f906ba2017-07-10 15:50:09 -070095 percpu_up_write(&mem_hotplug_lock);
96 cpus_read_unlock();
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070097}
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080098
Keith Mannthey45e0b782006-09-30 23:27:09 -070099/* add this memory to iomem resource */
100static struct resource *register_memory_resource(u64 start, u64 size)
101{
Jérôme Glisse5042db42017-09-08 16:11:43 -0700102 struct resource *res, *conflict;
Keith Mannthey45e0b782006-09-30 23:27:09 -0700103 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -0800104 if (!res)
105 return ERR_PTR(-ENOMEM);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700106
107 res->name = "System RAM";
108 res->start = start;
109 res->end = start + size - 1;
Toshi Kani782b8662016-01-26 21:57:24 +0100110 res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
Jérôme Glisse5042db42017-09-08 16:11:43 -0700111 conflict = request_resource_conflict(&iomem_resource, res);
112 if (conflict) {
113 if (conflict->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY) {
114 pr_debug("Device unaddressable memory block "
115 "memory hotplug at %#010llx !\n",
116 (unsigned long long)start);
117 }
Toshi Kani4996eed2013-07-03 15:02:39 -0700118 pr_debug("System RAM resource %pR cannot be added\n", res);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700119 kfree(res);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -0800120 return ERR_PTR(-EEXIST);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700121 }
122 return res;
123}
124
125static void release_memory_resource(struct resource *res)
126{
127 if (!res)
128 return;
129 release_resource(res);
130 kfree(res);
131 return;
132}
133
Keith Mannthey53947022006-09-30 23:27:08 -0700134#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800135void get_page_bootmem(unsigned long info, struct page *page,
136 unsigned long type)
Yasunori Goto04753272008-04-28 02:13:31 -0700137{
Yasuaki Ishimatsuddffe982017-02-22 15:45:13 -0800138 page->freelist = (void *)type;
Yasunori Goto04753272008-04-28 02:13:31 -0700139 SetPagePrivate(page);
140 set_page_private(page, info);
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700141 page_ref_inc(page);
Yasunori Goto04753272008-04-28 02:13:31 -0700142}
143
Jiang Liu170a5a72013-07-03 15:03:17 -0700144void put_page_bootmem(struct page *page)
Yasunori Goto04753272008-04-28 02:13:31 -0700145{
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800146 unsigned long type;
Yasunori Goto04753272008-04-28 02:13:31 -0700147
Yasuaki Ishimatsuddffe982017-02-22 15:45:13 -0800148 type = (unsigned long) page->freelist;
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800149 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
150 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
Yasunori Goto04753272008-04-28 02:13:31 -0700151
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700152 if (page_ref_dec_return(page) == 1) {
Yasuaki Ishimatsuddffe982017-02-22 15:45:13 -0800153 page->freelist = NULL;
Yasunori Goto04753272008-04-28 02:13:31 -0700154 ClearPagePrivate(page);
155 set_page_private(page, 0);
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800156 INIT_LIST_HEAD(&page->lru);
Jiang Liu170a5a72013-07-03 15:03:17 -0700157 free_reserved_page(page);
Yasunori Goto04753272008-04-28 02:13:31 -0700158 }
Yasunori Goto04753272008-04-28 02:13:31 -0700159}
160
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800161#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
162#ifndef CONFIG_SPARSEMEM_VMEMMAP
Adrian Bunkd92bc312008-07-23 21:28:12 -0700163static void register_page_bootmem_info_section(unsigned long start_pfn)
Yasunori Goto04753272008-04-28 02:13:31 -0700164{
165 unsigned long *usemap, mapsize, section_nr, i;
166 struct mem_section *ms;
167 struct page *page, *memmap;
168
Yasunori Goto04753272008-04-28 02:13:31 -0700169 section_nr = pfn_to_section_nr(start_pfn);
170 ms = __nr_to_section(section_nr);
171
172 /* Get section's memmap address */
173 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
174
175 /*
176 * Get page for the memmap's phys address
177 * XXX: need more consideration for sparse_vmemmap...
178 */
179 page = virt_to_page(memmap);
180 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
181 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
182
183 /* remember memmap's page */
184 for (i = 0; i < mapsize; i++, page++)
185 get_page_bootmem(section_nr, page, SECTION_INFO);
186
Oscar Salvador9ac93222018-01-31 16:17:25 -0800187 usemap = ms->pageblock_flags;
Yasunori Goto04753272008-04-28 02:13:31 -0700188 page = virt_to_page(usemap);
189
190 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
191
192 for (i = 0; i < mapsize; i++, page++)
Yasunori Gotoaf370fb2008-07-23 21:28:17 -0700193 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
Yasunori Goto04753272008-04-28 02:13:31 -0700194
195}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800196#else /* CONFIG_SPARSEMEM_VMEMMAP */
197static void register_page_bootmem_info_section(unsigned long start_pfn)
198{
199 unsigned long *usemap, mapsize, section_nr, i;
200 struct mem_section *ms;
201 struct page *page, *memmap;
202
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800203 section_nr = pfn_to_section_nr(start_pfn);
204 ms = __nr_to_section(section_nr);
205
206 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
207
208 register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
209
Oscar Salvador9ac93222018-01-31 16:17:25 -0800210 usemap = ms->pageblock_flags;
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800211 page = virt_to_page(usemap);
212
213 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
214
215 for (i = 0; i < mapsize; i++, page++)
216 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
217}
218#endif /* !CONFIG_SPARSEMEM_VMEMMAP */
Yasunori Goto04753272008-04-28 02:13:31 -0700219
Linus Torvalds7ded3842016-05-27 15:23:32 -0700220void __init register_page_bootmem_info_node(struct pglist_data *pgdat)
Yasunori Goto04753272008-04-28 02:13:31 -0700221{
222 unsigned long i, pfn, end_pfn, nr_pages;
223 int node = pgdat->node_id;
224 struct page *page;
Yasunori Goto04753272008-04-28 02:13:31 -0700225
226 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
227 page = virt_to_page(pgdat);
228
229 for (i = 0; i < nr_pages; i++, page++)
230 get_page_bootmem(node, page, NODE_INFO);
231
Yasunori Goto04753272008-04-28 02:13:31 -0700232 pfn = pgdat->node_start_pfn;
Cody P Schaferc1f19492013-02-22 16:35:32 -0800233 end_pfn = pgdat_end_pfn(pgdat);
Yasunori Goto04753272008-04-28 02:13:31 -0700234
Tang Chen7e9f5eb2013-07-08 16:00:23 -0700235 /* register section info */
qiuxishif14851a2012-09-17 14:09:24 -0700236 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
237 /*
238 * Some platforms can assign the same pfn to multiple nodes - on
239 * node0 as well as nodeN. To avoid registering a pfn against
240 * multiple nodes we check that this pfn does not already
Tang Chen7e9f5eb2013-07-08 16:00:23 -0700241 * reside in some other nodes.
qiuxishif14851a2012-09-17 14:09:24 -0700242 */
Yang Shif65e91df2016-05-27 14:27:32 -0700243 if (pfn_valid(pfn) && (early_pfn_to_nid(pfn) == node))
qiuxishif14851a2012-09-17 14:09:24 -0700244 register_page_bootmem_info_section(pfn);
245 }
Yasunori Goto04753272008-04-28 02:13:31 -0700246}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800247#endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
Yasunori Goto04753272008-04-28 02:13:31 -0700248
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700249static int __meminit __add_section(int nid, unsigned long phys_start_pfn,
Christoph Hellwig7b73d972017-12-29 08:53:54 +0100250 struct vmem_altmap *altmap, bool want_memblock)
Dave Hansen3947be12005-10-29 18:16:54 -0700251{
Dave Hansen3947be12005-10-29 18:16:54 -0700252 int ret;
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700253 int i;
Dave Hansen3947be12005-10-29 18:16:54 -0700254
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700255 if (pfn_valid(phys_start_pfn))
256 return -EEXIST;
257
Christoph Hellwig7b73d972017-12-29 08:53:54 +0100258 ret = sparse_add_one_section(NODE_DATA(nid), phys_start_pfn, altmap);
Dave Hansen3947be12005-10-29 18:16:54 -0700259 if (ret < 0)
260 return ret;
261
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700262 /*
263 * Make all the pages reserved so that nobody will stumble over half
264 * initialized state.
Fan Du1b7176a2017-11-15 17:39:21 -0800265 * FIXME: We also have to associate it with a node because page_to_nid
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700266 * relies on having page with the proper node.
267 */
268 for (i = 0; i < PAGES_PER_SECTION; i++) {
269 unsigned long pfn = phys_start_pfn + i;
270 struct page *page;
271 if (!pfn_valid(pfn))
272 continue;
Yasunori Goto718127c2006-06-23 02:03:10 -0700273
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700274 page = pfn_to_page(pfn);
275 set_page_node(page, nid);
276 SetPageReserved(page);
277 }
Yasunori Goto718127c2006-06-23 02:03:10 -0700278
Michal Hocko1b862ae2017-07-06 15:37:45 -0700279 if (!want_memblock)
280 return 0;
281
Gary Hadec04fc582009-01-06 14:39:14 -0800282 return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
Dave Hansen3947be12005-10-29 18:16:54 -0700283}
284
David Rientjes4edd7ce2013-04-29 15:08:22 -0700285/*
286 * Reasonably generic function for adding memory. It is
287 * expected that archs that support memory hotplug will
288 * call this function after deciding the zone to which to
289 * add the new pages.
290 */
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700291int __ref __add_pages(int nid, unsigned long phys_start_pfn,
Christoph Hellwig24e6d5a2017-12-29 08:53:53 +0100292 unsigned long nr_pages, struct vmem_altmap *altmap,
293 bool want_memblock)
David Rientjes4edd7ce2013-04-29 15:08:22 -0700294{
295 unsigned long i;
296 int err = 0;
297 int start_sec, end_sec;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800298
David Rientjes4edd7ce2013-04-29 15:08:22 -0700299 /* during initialize mem_map, align hot-added range to section */
300 start_sec = pfn_to_section_nr(phys_start_pfn);
301 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
302
Dan Williams4b94ffd2016-01-15 16:56:22 -0800303 if (altmap) {
304 /*
305 * Validate altmap is within bounds of the total request
306 */
307 if (altmap->base_pfn != phys_start_pfn
308 || vmem_altmap_offset(altmap) > nr_pages) {
309 pr_warn_once("memory add fail, invalid altmap\n");
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700310 err = -EINVAL;
311 goto out;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800312 }
313 altmap->alloc = 0;
314 }
315
David Rientjes4edd7ce2013-04-29 15:08:22 -0700316 for (i = start_sec; i <= end_sec; i++) {
Christoph Hellwig7b73d972017-12-29 08:53:54 +0100317 err = __add_section(nid, section_nr_to_pfn(i), altmap,
318 want_memblock);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700319
320 /*
321 * EEXIST is finally dealt with by ioresource collision
322 * check. see add_memory() => register_memory_resource()
323 * Warning will be printed if there is collision.
324 */
325 if (err && (err != -EEXIST))
326 break;
327 err = 0;
Michal Hockof64ac5e2017-10-03 16:16:16 -0700328 cond_resched();
David Rientjes4edd7ce2013-04-29 15:08:22 -0700329 }
Zhu Guihuac435a392015-06-24 16:58:42 -0700330 vmemmap_populate_print_last();
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700331out:
David Rientjes4edd7ce2013-04-29 15:08:22 -0700332 return err;
333}
David Rientjes4edd7ce2013-04-29 15:08:22 -0700334
335#ifdef CONFIG_MEMORY_HOTREMOVE
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800336/* find the smallest valid pfn in the range [start_pfn, end_pfn) */
YASUAKI ISHIMATSUd09b0132017-10-03 16:16:32 -0700337static unsigned long find_smallest_section_pfn(int nid, struct zone *zone,
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800338 unsigned long start_pfn,
339 unsigned long end_pfn)
340{
341 struct mem_section *ms;
342
343 for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
344 ms = __pfn_to_section(start_pfn);
345
346 if (unlikely(!valid_section(ms)))
347 continue;
348
349 if (unlikely(pfn_to_nid(start_pfn) != nid))
350 continue;
351
352 if (zone && zone != page_zone(pfn_to_page(start_pfn)))
353 continue;
354
355 return start_pfn;
356 }
357
358 return 0;
359}
360
361/* find the biggest valid pfn in the range [start_pfn, end_pfn). */
YASUAKI ISHIMATSUd09b0132017-10-03 16:16:32 -0700362static unsigned long find_biggest_section_pfn(int nid, struct zone *zone,
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800363 unsigned long start_pfn,
364 unsigned long end_pfn)
365{
366 struct mem_section *ms;
367 unsigned long pfn;
368
369 /* pfn is the end pfn of a memory section. */
370 pfn = end_pfn - 1;
371 for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
372 ms = __pfn_to_section(pfn);
373
374 if (unlikely(!valid_section(ms)))
375 continue;
376
377 if (unlikely(pfn_to_nid(pfn) != nid))
378 continue;
379
380 if (zone && zone != page_zone(pfn_to_page(pfn)))
381 continue;
382
383 return pfn;
384 }
385
386 return 0;
387}
388
389static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
390 unsigned long end_pfn)
391{
Xishi Qiuc33bc312013-09-11 14:21:44 -0700392 unsigned long zone_start_pfn = zone->zone_start_pfn;
393 unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
394 unsigned long zone_end_pfn = z;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800395 unsigned long pfn;
396 struct mem_section *ms;
397 int nid = zone_to_nid(zone);
398
399 zone_span_writelock(zone);
400 if (zone_start_pfn == start_pfn) {
401 /*
402 * If the section is smallest section in the zone, it need
403 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
404 * In this case, we find second smallest valid mem_section
405 * for shrinking zone.
406 */
407 pfn = find_smallest_section_pfn(nid, zone, end_pfn,
408 zone_end_pfn);
409 if (pfn) {
410 zone->zone_start_pfn = pfn;
411 zone->spanned_pages = zone_end_pfn - pfn;
412 }
413 } else if (zone_end_pfn == end_pfn) {
414 /*
415 * If the section is biggest section in the zone, it need
416 * shrink zone->spanned_pages.
417 * In this case, we find second biggest valid mem_section for
418 * shrinking zone.
419 */
420 pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
421 start_pfn);
422 if (pfn)
423 zone->spanned_pages = pfn - zone_start_pfn + 1;
424 }
425
426 /*
427 * The section is not biggest or smallest mem_section in the zone, it
428 * only creates a hole in the zone. So in this case, we need not
429 * change the zone. But perhaps, the zone has only hole data. Thus
430 * it check the zone has only hole or not.
431 */
432 pfn = zone_start_pfn;
433 for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
434 ms = __pfn_to_section(pfn);
435
436 if (unlikely(!valid_section(ms)))
437 continue;
438
439 if (page_zone(pfn_to_page(pfn)) != zone)
440 continue;
441
442 /* If the section is current section, it continues the loop */
443 if (start_pfn == pfn)
444 continue;
445
446 /* If we find valid section, we have nothing to do */
447 zone_span_writeunlock(zone);
448 return;
449 }
450
451 /* The zone has no valid section */
452 zone->zone_start_pfn = 0;
453 zone->spanned_pages = 0;
454 zone_span_writeunlock(zone);
455}
456
457static void shrink_pgdat_span(struct pglist_data *pgdat,
458 unsigned long start_pfn, unsigned long end_pfn)
459{
Xishi Qiu83285c72013-11-12 15:07:19 -0800460 unsigned long pgdat_start_pfn = pgdat->node_start_pfn;
461 unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */
462 unsigned long pgdat_end_pfn = p;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800463 unsigned long pfn;
464 struct mem_section *ms;
465 int nid = pgdat->node_id;
466
467 if (pgdat_start_pfn == start_pfn) {
468 /*
469 * If the section is smallest section in the pgdat, it need
470 * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
471 * In this case, we find second smallest valid mem_section
472 * for shrinking zone.
473 */
474 pfn = find_smallest_section_pfn(nid, NULL, end_pfn,
475 pgdat_end_pfn);
476 if (pfn) {
477 pgdat->node_start_pfn = pfn;
478 pgdat->node_spanned_pages = pgdat_end_pfn - pfn;
479 }
480 } else if (pgdat_end_pfn == end_pfn) {
481 /*
482 * If the section is biggest section in the pgdat, it need
483 * shrink pgdat->node_spanned_pages.
484 * In this case, we find second biggest valid mem_section for
485 * shrinking zone.
486 */
487 pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn,
488 start_pfn);
489 if (pfn)
490 pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1;
491 }
492
493 /*
494 * If the section is not biggest or smallest mem_section in the pgdat,
495 * it only creates a hole in the pgdat. So in this case, we need not
496 * change the pgdat.
497 * But perhaps, the pgdat has only hole data. Thus it check the pgdat
498 * has only hole or not.
499 */
500 pfn = pgdat_start_pfn;
501 for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) {
502 ms = __pfn_to_section(pfn);
503
504 if (unlikely(!valid_section(ms)))
505 continue;
506
507 if (pfn_to_nid(pfn) != nid)
508 continue;
509
510 /* If the section is current section, it continues the loop */
511 if (start_pfn == pfn)
512 continue;
513
514 /* If we find valid section, we have nothing to do */
515 return;
516 }
517
518 /* The pgdat has no valid section */
519 pgdat->node_start_pfn = 0;
520 pgdat->node_spanned_pages = 0;
521}
522
523static void __remove_zone(struct zone *zone, unsigned long start_pfn)
524{
525 struct pglist_data *pgdat = zone->zone_pgdat;
526 int nr_pages = PAGES_PER_SECTION;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800527 unsigned long flags;
528
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800529 pgdat_resize_lock(zone->zone_pgdat, &flags);
530 shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
531 shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages);
532 pgdat_resize_unlock(zone->zone_pgdat, &flags);
533}
534
Dan Williams4b94ffd2016-01-15 16:56:22 -0800535static int __remove_section(struct zone *zone, struct mem_section *ms,
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100536 unsigned long map_offset, struct vmem_altmap *altmap)
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700537{
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800538 unsigned long start_pfn;
539 int scn_nr;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700540 int ret = -EINVAL;
541
542 if (!valid_section(ms))
543 return ret;
544
545 ret = unregister_memory_section(ms);
546 if (ret)
547 return ret;
548
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800549 scn_nr = __section_nr(ms);
YASUAKI ISHIMATSU1dd2bfc2017-10-03 16:16:29 -0700550 start_pfn = section_nr_to_pfn((unsigned long)scn_nr);
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800551 __remove_zone(zone, start_pfn);
552
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100553 sparse_remove_one_section(zone, ms, map_offset, altmap);
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700554 return 0;
555}
556
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700557/**
558 * __remove_pages() - remove sections of pages from a zone
559 * @zone: zone from which pages need to be removed
560 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
561 * @nr_pages: number of pages to remove (must be multiple of section size)
562 *
563 * Generic helper function to remove section mappings and sysfs entries
564 * for the section of the memory we are removing. Caller needs to make
565 * sure that pages are marked reserved and zones are adjust properly by
566 * calling offline_pages().
567 */
568int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
Christoph Hellwigda024512017-12-29 08:53:55 +0100569 unsigned long nr_pages, struct vmem_altmap *altmap)
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700570{
Toshi Kanife74ebb2013-04-29 15:08:20 -0700571 unsigned long i;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800572 unsigned long map_offset = 0;
573 int sections_to_remove, ret = 0;
574
575 /* In the ZONE_DEVICE case device driver owns the memory region */
576 if (is_dev_zone(zone)) {
Dan Williams4b94ffd2016-01-15 16:56:22 -0800577 if (altmap)
578 map_offset = vmem_altmap_offset(altmap);
579 } else {
580 resource_size_t start, size;
581
582 start = phys_start_pfn << PAGE_SHIFT;
583 size = nr_pages * PAGE_SIZE;
584
585 ret = release_mem_region_adjustable(&iomem_resource, start,
586 size);
587 if (ret) {
588 resource_size_t endres = start + size - 1;
589
590 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
591 &start, &endres, ret);
592 }
593 }
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700594
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700595 clear_zone_contiguous(zone);
596
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700597 /*
598 * We can only remove entire sections
599 */
600 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
601 BUG_ON(nr_pages % PAGES_PER_SECTION);
602
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700603 sections_to_remove = nr_pages / PAGES_PER_SECTION;
604 for (i = 0; i < sections_to_remove; i++) {
605 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800606
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100607 ret = __remove_section(zone, __pfn_to_section(pfn), map_offset,
608 altmap);
Dan Williams4b94ffd2016-01-15 16:56:22 -0800609 map_offset = 0;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700610 if (ret)
611 break;
612 }
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700613
614 set_zone_contiguous(zone);
615
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700616 return ret;
617}
David Rientjes4edd7ce2013-04-29 15:08:22 -0700618#endif /* CONFIG_MEMORY_HOTREMOVE */
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700619
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700620int set_online_page_callback(online_page_callback_t callback)
621{
622 int rc = -EINVAL;
623
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700624 get_online_mems();
625 mutex_lock(&online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700626
627 if (online_page_callback == generic_online_page) {
628 online_page_callback = callback;
629 rc = 0;
630 }
631
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700632 mutex_unlock(&online_page_callback_lock);
633 put_online_mems();
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700634
635 return rc;
636}
637EXPORT_SYMBOL_GPL(set_online_page_callback);
638
639int restore_online_page_callback(online_page_callback_t callback)
640{
641 int rc = -EINVAL;
642
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700643 get_online_mems();
644 mutex_lock(&online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700645
646 if (online_page_callback == callback) {
647 online_page_callback = generic_online_page;
648 rc = 0;
649 }
650
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700651 mutex_unlock(&online_page_callback_lock);
652 put_online_mems();
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700653
654 return rc;
655}
656EXPORT_SYMBOL_GPL(restore_online_page_callback);
657
658void __online_page_set_limits(struct page *page)
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700659{
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700660}
661EXPORT_SYMBOL_GPL(__online_page_set_limits);
662
663void __online_page_increment_counters(struct page *page)
664{
Jiang Liu3dcc0572013-07-03 15:03:21 -0700665 adjust_managed_page_count(page, 1);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700666}
667EXPORT_SYMBOL_GPL(__online_page_increment_counters);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700668
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700669void __online_page_free(struct page *page)
670{
Jiang Liu3dcc0572013-07-03 15:03:21 -0700671 __free_reserved_page(page);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700672}
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700673EXPORT_SYMBOL_GPL(__online_page_free);
674
675static void generic_online_page(struct page *page)
676{
677 __online_page_set_limits(page);
678 __online_page_increment_counters(page);
679 __online_page_free(page);
680}
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700681
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700682static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
683 void *arg)
Dave Hansen3947be12005-10-29 18:16:54 -0700684{
685 unsigned long i;
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700686 unsigned long onlined_pages = *(unsigned long *)arg;
687 struct page *page;
Michal Hocko2d070ea2017-07-06 15:37:56 -0700688
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700689 if (PageReserved(pfn_to_page(start_pfn)))
690 for (i = 0; i < nr_pages; i++) {
691 page = pfn_to_page(start_pfn + i);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700692 (*online_page_callback)(page);
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700693 onlined_pages++;
694 }
Michal Hocko2d070ea2017-07-06 15:37:56 -0700695
696 online_mem_sections(start_pfn, start_pfn + nr_pages);
697
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700698 *(unsigned long *)arg = onlined_pages;
699 return 0;
700}
701
Lai Jiangshand9713672012-12-11 16:01:03 -0800702/* check which state of node_states will be changed when online memory */
703static void node_states_check_changes_online(unsigned long nr_pages,
704 struct zone *zone, struct memory_notify *arg)
705{
706 int nid = zone_to_nid(zone);
707 enum zone_type zone_last = ZONE_NORMAL;
708
709 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800710 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
711 * contains nodes which have zones of 0...ZONE_NORMAL,
712 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -0800713 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800714 * If we don't have HIGHMEM nor movable node,
715 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
716 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -0800717 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800718 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -0800719 zone_last = ZONE_MOVABLE;
720
721 /*
722 * if the memory to be online is in a zone of 0...zone_last, and
723 * the zones of 0...zone_last don't have memory before online, we will
724 * need to set the node to node_states[N_NORMAL_MEMORY] after
725 * the memory is online.
726 */
727 if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
728 arg->status_change_nid_normal = nid;
729 else
730 arg->status_change_nid_normal = -1;
731
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800732#ifdef CONFIG_HIGHMEM
733 /*
734 * If we have movable node, node_states[N_HIGH_MEMORY]
735 * contains nodes which have zones of 0...ZONE_HIGHMEM,
736 * set zone_last to ZONE_HIGHMEM.
737 *
738 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
739 * contains nodes which have zones of 0...ZONE_MOVABLE,
740 * set zone_last to ZONE_MOVABLE.
741 */
742 zone_last = ZONE_HIGHMEM;
743 if (N_MEMORY == N_HIGH_MEMORY)
744 zone_last = ZONE_MOVABLE;
745
746 if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
747 arg->status_change_nid_high = nid;
748 else
749 arg->status_change_nid_high = -1;
750#else
751 arg->status_change_nid_high = arg->status_change_nid_normal;
752#endif
753
Lai Jiangshand9713672012-12-11 16:01:03 -0800754 /*
755 * if the node don't have memory befor online, we will need to
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800756 * set the node to node_states[N_MEMORY] after the memory
Lai Jiangshand9713672012-12-11 16:01:03 -0800757 * is online.
758 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800759 if (!node_state(nid, N_MEMORY))
Lai Jiangshand9713672012-12-11 16:01:03 -0800760 arg->status_change_nid = nid;
761 else
762 arg->status_change_nid = -1;
763}
764
765static void node_states_set_node(int node, struct memory_notify *arg)
766{
767 if (arg->status_change_nid_normal >= 0)
768 node_set_state(node, N_NORMAL_MEMORY);
769
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800770 if (arg->status_change_nid_high >= 0)
771 node_set_state(node, N_HIGH_MEMORY);
772
773 node_set_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -0800774}
775
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700776static void __meminit resize_zone_range(struct zone *zone, unsigned long start_pfn,
777 unsigned long nr_pages)
778{
779 unsigned long old_end_pfn = zone_end_pfn(zone);
780
781 if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
782 zone->zone_start_pfn = start_pfn;
783
784 zone->spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - zone->zone_start_pfn;
785}
786
787static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned long start_pfn,
788 unsigned long nr_pages)
789{
790 unsigned long old_end_pfn = pgdat_end_pfn(pgdat);
791
792 if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
793 pgdat->node_start_pfn = start_pfn;
794
795 pgdat->node_spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - pgdat->node_start_pfn;
796}
797
Christoph Hellwiga99583e2017-12-29 08:53:57 +0100798void __ref move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
799 unsigned long nr_pages, struct vmem_altmap *altmap)
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700800{
801 struct pglist_data *pgdat = zone->zone_pgdat;
802 int nid = pgdat->node_id;
803 unsigned long flags;
804
805 if (zone_is_empty(zone))
806 init_currently_empty_zone(zone, start_pfn, nr_pages);
807
808 clear_zone_contiguous(zone);
809
810 /* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */
811 pgdat_resize_lock(pgdat, &flags);
812 zone_span_writelock(zone);
813 resize_zone_range(zone, start_pfn, nr_pages);
814 zone_span_writeunlock(zone);
815 resize_pgdat_range(pgdat, start_pfn, nr_pages);
816 pgdat_resize_unlock(pgdat, &flags);
817
818 /*
819 * TODO now we have a visible range of pages which are not associated
820 * with their zone properly. Not nice but set_pfnblock_flags_mask
821 * expects the zone spans the pfn range. All the pages in the range
822 * are reserved so nobody should be touching them so we should be safe
823 */
Christoph Hellwiga99583e2017-12-29 08:53:57 +0100824 memmap_init_zone(nr_pages, nid, zone_idx(zone), start_pfn,
825 MEMMAP_HOTPLUG, altmap);
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700826
827 set_zone_contiguous(zone);
828}
829
830/*
Michal Hockoc246a212017-07-06 15:38:18 -0700831 * Returns a default kernel memory zone for the given pfn range.
832 * If no kernel zone covers this pfn range it will automatically go
833 * to the ZONE_NORMAL.
834 */
Michal Hockoc6f03e22017-09-06 16:19:40 -0700835static struct zone *default_kernel_zone_for_pfn(int nid, unsigned long start_pfn,
Michal Hockoc246a212017-07-06 15:38:18 -0700836 unsigned long nr_pages)
837{
838 struct pglist_data *pgdat = NODE_DATA(nid);
839 int zid;
840
841 for (zid = 0; zid <= ZONE_NORMAL; zid++) {
842 struct zone *zone = &pgdat->node_zones[zid];
843
844 if (zone_intersects(zone, start_pfn, nr_pages))
845 return zone;
846 }
847
848 return &pgdat->node_zones[ZONE_NORMAL];
849}
850
Michal Hockoc6f03e22017-09-06 16:19:40 -0700851static inline struct zone *default_zone_for_pfn(int nid, unsigned long start_pfn,
852 unsigned long nr_pages)
Michal Hockoe5e68932017-09-06 16:19:37 -0700853{
Michal Hockoc6f03e22017-09-06 16:19:40 -0700854 struct zone *kernel_zone = default_kernel_zone_for_pfn(nid, start_pfn,
855 nr_pages);
856 struct zone *movable_zone = &NODE_DATA(nid)->node_zones[ZONE_MOVABLE];
857 bool in_kernel = zone_intersects(kernel_zone, start_pfn, nr_pages);
858 bool in_movable = zone_intersects(movable_zone, start_pfn, nr_pages);
Michal Hockoe5e68932017-09-06 16:19:37 -0700859
860 /*
Michal Hockoc6f03e22017-09-06 16:19:40 -0700861 * We inherit the existing zone in a simple case where zones do not
862 * overlap in the given range
Michal Hockoe5e68932017-09-06 16:19:37 -0700863 */
Michal Hockoc6f03e22017-09-06 16:19:40 -0700864 if (in_kernel ^ in_movable)
865 return (in_kernel) ? kernel_zone : movable_zone;
Michal Hockoe5e68932017-09-06 16:19:37 -0700866
Michal Hockoc6f03e22017-09-06 16:19:40 -0700867 /*
868 * If the range doesn't belong to any zone or two zones overlap in the
869 * given range then we use movable zone only if movable_node is
870 * enabled because we always online to a kernel zone by default.
871 */
872 return movable_node_enabled ? movable_zone : kernel_zone;
Michal Hocko9f123ab2017-07-10 15:48:37 -0700873}
874
Michal Hockoe5e68932017-09-06 16:19:37 -0700875struct zone * zone_for_pfn_range(int online_type, int nid, unsigned start_pfn,
876 unsigned long nr_pages)
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700877{
Michal Hockoc6f03e22017-09-06 16:19:40 -0700878 if (online_type == MMOP_ONLINE_KERNEL)
879 return default_kernel_zone_for_pfn(nid, start_pfn, nr_pages);
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700880
Michal Hockoc6f03e22017-09-06 16:19:40 -0700881 if (online_type == MMOP_ONLINE_MOVABLE)
882 return &NODE_DATA(nid)->node_zones[ZONE_MOVABLE];
Reza Arbabdf429ac2016-07-26 15:22:23 -0700883
Michal Hockoc6f03e22017-09-06 16:19:40 -0700884 return default_zone_for_pfn(nid, start_pfn, nr_pages);
Michal Hockoe5e68932017-09-06 16:19:37 -0700885}
886
887/*
888 * Associates the given pfn range with the given node and the zone appropriate
889 * for the given online type.
890 */
891static struct zone * __meminit move_pfn_range(int online_type, int nid,
892 unsigned long start_pfn, unsigned long nr_pages)
893{
894 struct zone *zone;
895
896 zone = zone_for_pfn_range(online_type, nid, start_pfn, nr_pages);
Christoph Hellwiga99583e2017-12-29 08:53:57 +0100897 move_pfn_range_to_zone(zone, start_pfn, nr_pages, NULL);
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700898 return zone;
Reza Arbabdf429ac2016-07-26 15:22:23 -0700899}
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700900
Michal Hockob93e0f32017-09-06 16:20:37 -0700901/* Must be protected by mem_hotplug_begin() or a device_lock */
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800902int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700903{
Cody P Schaferaa472282013-07-03 15:02:10 -0700904 unsigned long flags;
Dave Hansen3947be12005-10-29 18:16:54 -0700905 unsigned long onlined_pages = 0;
906 struct zone *zone;
Yasunori Goto68113782006-06-23 02:03:11 -0700907 int need_zonelists_rebuild = 0;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700908 int nid;
909 int ret;
910 struct memory_notify arg;
Dave Hansen3947be12005-10-29 18:16:54 -0700911
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700912 nid = pfn_to_nid(pfn);
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700913 /* associate pfn range with the zone */
914 zone = move_pfn_range(online_type, nid, pfn, nr_pages);
915
Yasunori Goto7b78d332007-10-21 16:41:36 -0700916 arg.start_pfn = pfn;
917 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -0800918 node_states_check_changes_online(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -0700919
Yasunori Goto7b78d332007-10-21 16:41:36 -0700920 ret = memory_notify(MEM_GOING_ONLINE, &arg);
921 ret = notifier_to_errno(ret);
Chen Yuconge33e33b2016-03-17 14:19:35 -0700922 if (ret)
923 goto failed_addition;
924
Dave Hansen3947be12005-10-29 18:16:54 -0700925 /*
Yasunori Goto68113782006-06-23 02:03:11 -0700926 * If this zone is not populated, then it is not in zonelist.
927 * This means the page allocator ignores this zone.
928 * So, zonelist must be updated after online.
929 */
Wen Congyang6dcd73d2012-12-11 16:01:01 -0800930 if (!populated_zone(zone)) {
Yasunori Goto68113782006-06-23 02:03:11 -0700931 need_zonelists_rebuild = 1;
Michal Hocko72675e12017-09-06 16:20:24 -0700932 setup_zone_pageset(zone);
Wen Congyang6dcd73d2012-12-11 16:01:01 -0800933 }
Yasunori Goto68113782006-06-23 02:03:11 -0700934
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700935 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700936 online_pages_range);
Geoff Levandfd8a4222008-05-14 16:05:50 -0700937 if (ret) {
Wen Congyang6dcd73d2012-12-11 16:01:01 -0800938 if (need_zonelists_rebuild)
939 zone_pcp_reset(zone);
Chen Yuconge33e33b2016-03-17 14:19:35 -0700940 goto failed_addition;
Geoff Levandfd8a4222008-05-14 16:05:50 -0700941 }
942
Dave Hansen3947be12005-10-29 18:16:54 -0700943 zone->present_pages += onlined_pages;
Cody P Schaferaa472282013-07-03 15:02:10 -0700944
945 pgdat_resize_lock(zone->zone_pgdat, &flags);
Yasunori Gotof2937be2006-03-09 17:33:51 -0800946 zone->zone_pgdat->node_present_pages += onlined_pages;
Cody P Schaferaa472282013-07-03 15:02:10 -0700947 pgdat_resize_unlock(zone->zone_pgdat, &flags);
948
Jiang Liu08dff7b2012-07-31 16:43:30 -0700949 if (onlined_pages) {
Vlastimil Babkae888ca32016-03-17 14:18:12 -0700950 node_states_set_node(nid, &arg);
Jiang Liu08dff7b2012-07-31 16:43:30 -0700951 if (need_zonelists_rebuild)
Michal Hocko72675e12017-09-06 16:20:24 -0700952 build_all_zonelists(NULL);
Jiang Liu08dff7b2012-07-31 16:43:30 -0700953 else
954 zone_pcp_update(zone);
955 }
Dave Hansen3947be12005-10-29 18:16:54 -0700956
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -0700957 init_per_zone_wmark_min();
958
Vlastimil Babka698b1b32016-03-17 14:18:08 -0700959 if (onlined_pages) {
Vlastimil Babkae888ca32016-03-17 14:18:12 -0700960 kswapd_run(nid);
Vlastimil Babka698b1b32016-03-17 14:18:08 -0700961 kcompactd_run(nid);
962 }
Dave Hansen61b13992005-10-29 18:16:56 -0700963
Haicheng Li1f522502010-05-24 14:32:51 -0700964 vm_total_pages = nr_free_pagecache_pages();
Kent Liu2f7f24e2008-07-23 21:28:18 -0700965
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -0700966 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -0700967
968 if (onlined_pages)
969 memory_notify(MEM_ONLINE, &arg);
David Rientjes30467e02015-04-14 15:45:11 -0700970 return 0;
Chen Yuconge33e33b2016-03-17 14:19:35 -0700971
972failed_addition:
973 pr_debug("online_pages [mem %#010llx-%#010llx] failed\n",
974 (unsigned long long) pfn << PAGE_SHIFT,
975 (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1);
976 memory_notify(MEM_CANCEL_ONLINE, &arg);
977 return ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700978}
Keith Mannthey53947022006-09-30 23:27:08 -0700979#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
Yasunori Gotobc02af92006-06-27 02:53:30 -0700980
Tang Chen0bd85422014-11-13 15:19:41 -0800981static void reset_node_present_pages(pg_data_t *pgdat)
982{
983 struct zone *z;
984
985 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
986 z->present_pages = 0;
987
988 pgdat->node_present_pages = 0;
989}
990
Hidetoshi Setoe1319332009-11-17 14:06:18 -0800991/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
992static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700993{
994 struct pglist_data *pgdat;
995 unsigned long zones_size[MAX_NR_ZONES] = {0};
996 unsigned long zholes_size[MAX_NR_ZONES] = {0};
Fabian Frederickc8e861a2014-06-04 16:07:51 -0700997 unsigned long start_pfn = PFN_DOWN(start);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700998
Tang Chena1e565a2013-02-22 16:33:18 -0800999 pgdat = NODE_DATA(nid);
1000 if (!pgdat) {
1001 pgdat = arch_alloc_nodedata(nid);
1002 if (!pgdat)
1003 return NULL;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001004
Tang Chena1e565a2013-02-22 16:33:18 -08001005 arch_refresh_nodedata(nid, pgdat);
Gu Zhengb0dc3a32015-03-25 15:55:20 -07001006 } else {
Mel Gormane716f2e2017-05-03 14:53:45 -07001007 /*
1008 * Reset the nr_zones, order and classzone_idx before reuse.
1009 * Note that kswapd will init kswapd_classzone_idx properly
1010 * when it starts in the near future.
1011 */
Gu Zhengb0dc3a32015-03-25 15:55:20 -07001012 pgdat->nr_zones = 0;
Mel Gorman38087d92016-07-28 15:45:49 -07001013 pgdat->kswapd_order = 0;
1014 pgdat->kswapd_classzone_idx = 0;
Tang Chena1e565a2013-02-22 16:33:18 -08001015 }
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001016
1017 /* we can use NODE_DATA(nid) from here */
1018
1019 /* init node's zones as empty zones, we don't have any present pages.*/
Johannes Weiner9109fb72008-07-23 21:27:20 -07001020 free_area_init_node(nid, zones_size, start_pfn, zholes_size);
Reza Arbab58301692016-08-11 15:33:12 -07001021 pgdat->per_cpu_nodestats = alloc_percpu(struct per_cpu_nodestat);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001022
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -07001023 /*
1024 * The node we allocated has no zone fallback lists. For avoiding
1025 * to access not-initialized zonelist, build here.
1026 */
Michal Hocko72675e12017-09-06 16:20:24 -07001027 build_all_zonelists(pgdat);
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -07001028
Tang Chenf784a3f2014-11-13 15:19:39 -08001029 /*
1030 * zone->managed_pages is set to an approximate value in
1031 * free_area_init_core(), which will cause
1032 * /sys/device/system/node/nodeX/meminfo has wrong data.
1033 * So reset it to 0 before any memory is onlined.
1034 */
1035 reset_node_managed_pages(pgdat);
1036
Tang Chen0bd85422014-11-13 15:19:41 -08001037 /*
1038 * When memory is hot-added, all the memory is in offline state. So
1039 * clear all zones' present_pages because they will be updated in
1040 * online_pages() and offline_pages().
1041 */
1042 reset_node_present_pages(pgdat);
1043
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001044 return pgdat;
1045}
1046
1047static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
1048{
1049 arch_refresh_nodedata(nid, NULL);
Reza Arbab58301692016-08-11 15:33:12 -07001050 free_percpu(pgdat->per_cpu_nodestats);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001051 arch_free_nodedata(pgdat);
1052 return;
1053}
1054
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -07001055
Toshi Kani01b0f192013-11-12 15:07:25 -08001056/**
1057 * try_online_node - online a node if offlined
1058 *
minskey guocf234222010-05-24 14:32:41 -07001059 * called by cpu_up() to online a node without onlined memory.
1060 */
Toshi Kani01b0f192013-11-12 15:07:25 -08001061int try_online_node(int nid)
minskey guocf234222010-05-24 14:32:41 -07001062{
1063 pg_data_t *pgdat;
1064 int ret;
1065
Toshi Kani01b0f192013-11-12 15:07:25 -08001066 if (node_online(nid))
1067 return 0;
1068
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001069 mem_hotplug_begin();
minskey guocf234222010-05-24 14:32:41 -07001070 pgdat = hotadd_new_pgdat(nid, 0);
David Rientjes7553e8f2011-06-22 18:13:01 -07001071 if (!pgdat) {
Toshi Kani01b0f192013-11-12 15:07:25 -08001072 pr_err("Cannot online node %d due to NULL pgdat\n", nid);
minskey guocf234222010-05-24 14:32:41 -07001073 ret = -ENOMEM;
1074 goto out;
1075 }
1076 node_set_online(nid);
1077 ret = register_one_node(nid);
1078 BUG_ON(ret);
minskey guocf234222010-05-24 14:32:41 -07001079out:
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001080 mem_hotplug_done();
minskey guocf234222010-05-24 14:32:41 -07001081 return ret;
1082}
1083
Toshi Kani27356f52013-09-11 14:21:49 -07001084static int check_hotplug_memory_range(u64 start, u64 size)
1085{
Fabian Frederickc8e861a2014-06-04 16:07:51 -07001086 u64 start_pfn = PFN_DOWN(start);
Toshi Kani27356f52013-09-11 14:21:49 -07001087 u64 nr_pages = size >> PAGE_SHIFT;
1088
1089 /* Memory range must be aligned with section */
1090 if ((start_pfn & ~PAGE_SECTION_MASK) ||
1091 (nr_pages % PAGES_PER_SECTION) || (!nr_pages)) {
1092 pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
1093 (unsigned long long)start,
1094 (unsigned long long)size);
1095 return -EINVAL;
1096 }
1097
1098 return 0;
1099}
1100
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001101static int online_memory_block(struct memory_block *mem, void *arg)
1102{
Nathan Fontenotdc18d702017-02-24 15:00:02 -08001103 return device_online(&mem->dev);
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001104}
1105
Al Viro31168482008-11-22 17:33:24 +00001106/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001107int __ref add_memory_resource(int nid, struct resource *res, bool online)
Yasunori Gotobc02af92006-06-27 02:53:30 -07001108{
David Vrabel62cedb92015-06-25 16:35:49 +01001109 u64 start, size;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001110 pg_data_t *pgdat = NULL;
Tang Chena1e565a2013-02-22 16:33:18 -08001111 bool new_pgdat;
1112 bool new_node;
Yasunori Gotobc02af92006-06-27 02:53:30 -07001113 int ret;
1114
David Vrabel62cedb92015-06-25 16:35:49 +01001115 start = res->start;
1116 size = resource_size(res);
1117
Toshi Kani27356f52013-09-11 14:21:49 -07001118 ret = check_hotplug_memory_range(start, size);
1119 if (ret)
1120 return ret;
1121
Tang Chena1e565a2013-02-22 16:33:18 -08001122 { /* Stupid hack to suppress address-never-null warning */
1123 void *p = NODE_DATA(nid);
1124 new_pgdat = !p;
1125 }
Nathan Zimmerac13c462014-01-23 15:53:26 -08001126
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001127 mem_hotplug_begin();
Nathan Zimmerac13c462014-01-23 15:53:26 -08001128
Tang Chen7f36e3e2015-09-04 15:42:32 -07001129 /*
1130 * Add new range to memblock so that when hotadd_new_pgdat() is called
1131 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
1132 * this new range and calculate total pages correctly. The range will
1133 * be removed at hot-remove time.
1134 */
1135 memblock_add_node(start, size, nid);
1136
Tang Chena1e565a2013-02-22 16:33:18 -08001137 new_node = !node_online(nid);
1138 if (new_node) {
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001139 pgdat = hotadd_new_pgdat(nid, start);
Andi Kleen6ad696d2009-11-17 14:06:22 -08001140 ret = -ENOMEM;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001141 if (!pgdat)
Wen Congyang41b9e2d2012-07-11 14:02:31 -07001142 goto error;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001143 }
1144
Yasunori Gotobc02af92006-06-27 02:53:30 -07001145 /* call arch's memory hotadd */
Christoph Hellwig24e6d5a2017-12-29 08:53:53 +01001146 ret = arch_add_memory(nid, start, size, NULL, true);
Yasunori Gotobc02af92006-06-27 02:53:30 -07001147
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001148 if (ret < 0)
1149 goto error;
1150
Yasunori Goto0fc44152006-06-27 02:53:38 -07001151 /* we online node here. we can't roll back from here. */
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001152 node_set_online(nid);
1153
Tang Chena1e565a2013-02-22 16:33:18 -08001154 if (new_node) {
Michal Hocko9037a992017-07-06 15:37:49 -07001155 unsigned long start_pfn = start >> PAGE_SHIFT;
1156 unsigned long nr_pages = size >> PAGE_SHIFT;
1157
1158 ret = __register_one_node(nid);
1159 if (ret)
1160 goto register_fail;
1161
1162 /*
1163 * link memory sections under this node. This is already
1164 * done when creatig memory section in register_new_memory
1165 * but that depends to have the node registered so offline
1166 * nodes have to go through register_node.
1167 * TODO clean up this mess.
1168 */
1169 ret = link_mem_sections(nid, start_pfn, nr_pages);
1170register_fail:
Yasunori Goto0fc44152006-06-27 02:53:38 -07001171 /*
1172 * If sysfs file of new node can't create, cpu on the node
1173 * can't be hot-added. There is no rollback way now.
1174 * So, check by BUG_ON() to catch it reluctantly..
1175 */
1176 BUG_ON(ret);
1177 }
1178
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -08001179 /* create new memmap entry */
1180 firmware_map_add_hotplug(start, start + size, "System RAM");
1181
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001182 /* online pages if requested */
1183 if (online)
1184 walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1),
1185 NULL, online_memory_block);
1186
Andi Kleen6ad696d2009-11-17 14:06:22 -08001187 goto out;
1188
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001189error:
1190 /* rollback pgdat allocation and others */
Gustavo A. R. Silvadbac61a2017-07-10 15:47:23 -07001191 if (new_pgdat && pgdat)
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001192 rollback_node_hotadd(nid, pgdat);
Tang Chen7f36e3e2015-09-04 15:42:32 -07001193 memblock_remove(start, size);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001194
Andi Kleen6ad696d2009-11-17 14:06:22 -08001195out:
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001196 mem_hotplug_done();
Yasunori Gotobc02af92006-06-27 02:53:30 -07001197 return ret;
1198}
David Vrabel62cedb92015-06-25 16:35:49 +01001199EXPORT_SYMBOL_GPL(add_memory_resource);
1200
1201int __ref add_memory(int nid, u64 start, u64 size)
1202{
1203 struct resource *res;
1204 int ret;
1205
1206 res = register_memory_resource(start, size);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -08001207 if (IS_ERR(res))
1208 return PTR_ERR(res);
David Vrabel62cedb92015-06-25 16:35:49 +01001209
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001210 ret = add_memory_resource(nid, res, memhp_auto_online);
David Vrabel62cedb92015-06-25 16:35:49 +01001211 if (ret < 0)
1212 release_memory_resource(res);
1213 return ret;
1214}
Yasunori Gotobc02af92006-06-27 02:53:30 -07001215EXPORT_SYMBOL_GPL(add_memory);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001216
1217#ifdef CONFIG_MEMORY_HOTREMOVE
1218/*
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001219 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1220 * set and the size of the free page is given by page_order(). Using this,
1221 * the function determines if the pageblock contains only free pages.
1222 * Due to buddy contraints, a free page at least the size of a pageblock will
1223 * be located at the start of the pageblock
1224 */
1225static inline int pageblock_free(struct page *page)
1226{
1227 return PageBuddy(page) && page_order(page) >= pageblock_order;
1228}
1229
1230/* Return the start of the next active pageblock after a given page */
1231static struct page *next_active_pageblock(struct page *page)
1232{
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001233 /* Ensure the starting page is pageblock-aligned */
1234 BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
1235
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001236 /* If the entire pageblock is free, move to the end of free page */
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -07001237 if (pageblock_free(page)) {
1238 int order;
1239 /* be careful. we don't have locks, page_order can be changed.*/
1240 order = page_order(page);
1241 if ((order < MAX_ORDER) && (order >= pageblock_order))
1242 return page + (1 << order);
1243 }
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001244
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -07001245 return page + pageblock_nr_pages;
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001246}
1247
1248/* Checks if this range of memory is likely to be hot-removable. */
Yaowei Baic98940f2016-05-19 17:11:26 -07001249bool is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001250{
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001251 struct page *page = pfn_to_page(start_pfn);
1252 struct page *end_page = page + nr_pages;
1253
1254 /* Check the starting page of each pageblock within the range */
1255 for (; page < end_page; page = next_active_pageblock(page)) {
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07001256 if (!is_pageblock_removable_nolock(page))
Yaowei Baic98940f2016-05-19 17:11:26 -07001257 return false;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07001258 cond_resched();
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001259 }
1260
1261 /* All pageblocks in the memory block are likely to be hot-removable */
Yaowei Baic98940f2016-05-19 17:11:26 -07001262 return true;
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001263}
1264
1265/*
Toshi Kanideb88a22017-02-03 13:13:20 -08001266 * Confirm all pages in a range [start, end) belong to the same zone.
Toshi Kania96dfdd2017-02-03 13:13:23 -08001267 * When true, return its valid [start, end).
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001268 */
Toshi Kania96dfdd2017-02-03 13:13:23 -08001269int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn,
1270 unsigned long *valid_start, unsigned long *valid_end)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001271{
Andrew Banman5f0f2882015-12-29 14:54:25 -08001272 unsigned long pfn, sec_end_pfn;
Toshi Kania96dfdd2017-02-03 13:13:23 -08001273 unsigned long start, end;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001274 struct zone *zone = NULL;
1275 struct page *page;
1276 int i;
Toshi Kanideb88a22017-02-03 13:13:20 -08001277 for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn + 1);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001278 pfn < end_pfn;
Toshi Kanideb88a22017-02-03 13:13:20 -08001279 pfn = sec_end_pfn, sec_end_pfn += PAGES_PER_SECTION) {
Andrew Banman5f0f2882015-12-29 14:54:25 -08001280 /* Make sure the memory section is present first */
1281 if (!present_section_nr(pfn_to_section_nr(pfn)))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001282 continue;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001283 for (; pfn < sec_end_pfn && pfn < end_pfn;
1284 pfn += MAX_ORDER_NR_PAGES) {
1285 i = 0;
1286 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1287 while ((i < MAX_ORDER_NR_PAGES) &&
1288 !pfn_valid_within(pfn + i))
1289 i++;
zhong jiangd6d8c8a2017-02-24 14:59:30 -08001290 if (i == MAX_ORDER_NR_PAGES || pfn + i >= end_pfn)
Andrew Banman5f0f2882015-12-29 14:54:25 -08001291 continue;
1292 page = pfn_to_page(pfn + i);
1293 if (zone && page_zone(page) != zone)
1294 return 0;
Toshi Kania96dfdd2017-02-03 13:13:23 -08001295 if (!zone)
1296 start = pfn + i;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001297 zone = page_zone(page);
Toshi Kania96dfdd2017-02-03 13:13:23 -08001298 end = pfn + MAX_ORDER_NR_PAGES;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001299 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001300 }
Toshi Kanideb88a22017-02-03 13:13:20 -08001301
Toshi Kania96dfdd2017-02-03 13:13:23 -08001302 if (zone) {
1303 *valid_start = start;
zhong jiangd6d8c8a2017-02-24 14:59:30 -08001304 *valid_end = min(end, end_pfn);
Toshi Kanideb88a22017-02-03 13:13:20 -08001305 return 1;
Toshi Kania96dfdd2017-02-03 13:13:23 -08001306 } else {
Toshi Kanideb88a22017-02-03 13:13:20 -08001307 return 0;
Toshi Kania96dfdd2017-02-03 13:13:23 -08001308 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001309}
1310
1311/*
Yisheng Xie0efadf42017-02-24 14:57:39 -08001312 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages,
1313 * non-lru movable pages and hugepages). We scan pfn because it's much
1314 * easier than scanning over linked list. This function returns the pfn
1315 * of the first found movable page if it's found, otherwise 0.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001316 */
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001317static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001318{
1319 unsigned long pfn;
1320 struct page *page;
1321 for (pfn = start; pfn < end; pfn++) {
1322 if (pfn_valid(pfn)) {
1323 page = pfn_to_page(pfn);
1324 if (PageLRU(page))
1325 return pfn;
Yisheng Xie0efadf42017-02-24 14:57:39 -08001326 if (__PageMovable(page))
1327 return pfn;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001328 if (PageHuge(page)) {
Naoya Horiguchi7e1f0492015-04-15 16:14:41 -07001329 if (page_huge_active(page))
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001330 return pfn;
1331 else
1332 pfn = round_up(pfn + 1,
1333 1 << compound_order(page)) - 1;
1334 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001335 }
1336 }
1337 return 0;
1338}
1339
Xishi Qiu394e31d2016-07-28 15:48:53 -07001340static struct page *new_node_page(struct page *page, unsigned long private,
1341 int **result)
1342{
Xishi Qiu394e31d2016-07-28 15:48:53 -07001343 int nid = page_to_nid(page);
Li Zhong231e97e2016-09-28 15:22:38 -07001344 nodemask_t nmask = node_states[N_MEMORY];
Michal Hocko7f252f22017-07-10 15:48:41 -07001345
1346 /*
1347 * try to allocate from a different node but reuse this node if there
1348 * are no other online nodes to be used (e.g. we are offlining a part
1349 * of the only existing node)
1350 */
1351 node_clear(nid, nmask);
1352 if (nodes_empty(nmask))
1353 node_set(nid, nmask);
Xishi Qiu394e31d2016-07-28 15:48:53 -07001354
Michal Hocko8b913232017-07-10 15:48:47 -07001355 return new_page_nodemask(page, nid, &nmask);
Xishi Qiu394e31d2016-07-28 15:48:53 -07001356}
1357
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001358#define NR_OFFLINE_AT_ONCE_PAGES (256)
1359static int
1360do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1361{
1362 unsigned long pfn;
1363 struct page *page;
1364 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
1365 int not_managed = 0;
1366 int ret = 0;
1367 LIST_HEAD(source);
1368
1369 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
1370 if (!pfn_valid(pfn))
1371 continue;
1372 page = pfn_to_page(pfn);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001373
1374 if (PageHuge(page)) {
1375 struct page *head = compound_head(page);
1376 pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
1377 if (compound_order(head) > PFN_SECTION_SHIFT) {
1378 ret = -EBUSY;
1379 break;
1380 }
1381 if (isolate_huge_page(page, &source))
1382 move_pages -= 1 << compound_order(head);
1383 continue;
Naoya Horiguchi8135d892017-09-08 16:11:15 -07001384 } else if (thp_migration_supported() && PageTransHuge(page))
1385 pfn = page_to_pfn(compound_head(page))
1386 + hpage_nr_pages(page) - 1;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001387
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001388 if (!get_page_unless_zero(page))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001389 continue;
1390 /*
Yisheng Xie0efadf42017-02-24 14:57:39 -08001391 * We can skip free pages. And we can deal with pages on
1392 * LRU and non-lru movable pages.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001393 */
Yisheng Xie0efadf42017-02-24 14:57:39 -08001394 if (PageLRU(page))
1395 ret = isolate_lru_page(page);
1396 else
1397 ret = isolate_movable_page(page, ISOLATE_UNEVICTABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001398 if (!ret) { /* Success */
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001399 put_page(page);
Nick Piggin62695a82008-10-18 20:26:09 -07001400 list_add_tail(&page->lru, &source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001401 move_pages--;
Yisheng Xie0efadf42017-02-24 14:57:39 -08001402 if (!__PageMovable(page))
1403 inc_node_page_state(page, NR_ISOLATED_ANON +
1404 page_is_file_cache(page));
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001405
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001406 } else {
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001407#ifdef CONFIG_DEBUG_VM
Yisheng Xie0efadf42017-02-24 14:57:39 -08001408 pr_alert("failed to isolate pfn %lx\n", pfn);
1409 dump_page(page, "isolation failed");
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001410#endif
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001411 put_page(page);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001412 /* Because we don't have big zone->lock. we should
Bob Liu809c4442010-10-26 14:22:10 -07001413 check this again here. */
1414 if (page_count(page)) {
1415 not_managed++;
Bob Liuf3ab2632010-10-26 14:22:10 -07001416 ret = -EBUSY;
Bob Liu809c4442010-10-26 14:22:10 -07001417 break;
1418 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001419 }
1420 }
Bob Liuf3ab2632010-10-26 14:22:10 -07001421 if (!list_empty(&source)) {
1422 if (not_managed) {
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001423 putback_movable_pages(&source);
Bob Liuf3ab2632010-10-26 14:22:10 -07001424 goto out;
1425 }
Minchan Kim74c08f92012-10-08 16:32:54 -07001426
Xishi Qiu394e31d2016-07-28 15:48:53 -07001427 /* Allocate a new page from the nearest neighbor node */
1428 ret = migrate_pages(&source, new_node_page, NULL, 0,
Hugh Dickins9c620e22013-02-22 16:35:14 -08001429 MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
Bob Liuf3ab2632010-10-26 14:22:10 -07001430 if (ret)
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001431 putback_movable_pages(&source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001432 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001433out:
1434 return ret;
1435}
1436
1437/*
1438 * remove from free_area[] and mark all as Reserved.
1439 */
1440static int
1441offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1442 void *data)
1443{
1444 __offline_isolated_pages(start, start + nr_pages);
1445 return 0;
1446}
1447
1448static void
1449offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1450{
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001451 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001452 offline_isolated_pages_cb);
1453}
1454
1455/*
1456 * Check all pages in range, recoreded as memory resource, are isolated.
1457 */
1458static int
1459check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1460 void *data)
1461{
1462 int ret;
1463 long offlined = *(long *)data;
Wen Congyangb023f462012-12-11 16:00:45 -08001464 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001465 offlined = nr_pages;
1466 if (!ret)
1467 *(long *)data += offlined;
1468 return ret;
1469}
1470
1471static long
1472check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1473{
1474 long offlined = 0;
1475 int ret;
1476
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001477 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001478 check_pages_isolated_cb);
1479 if (ret < 0)
1480 offlined = (long)ret;
1481 return offlined;
1482}
1483
Tang Chenc5320922013-11-12 15:08:10 -08001484static int __init cmdline_parse_movable_node(char *p)
1485{
Michal Hocko49323812017-07-06 15:41:05 -07001486#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Tang Chen55ac5902014-01-21 15:49:35 -08001487 movable_node_enabled = true;
Michal Hocko49323812017-07-06 15:41:05 -07001488#else
1489 pr_warn("movable_node parameter depends on CONFIG_HAVE_MEMBLOCK_NODE_MAP to work properly\n");
1490#endif
Tang Chenc5320922013-11-12 15:08:10 -08001491 return 0;
1492}
1493early_param("movable_node", cmdline_parse_movable_node);
1494
Lai Jiangshand9713672012-12-11 16:01:03 -08001495/* check which state of node_states will be changed when offline memory */
1496static void node_states_check_changes_offline(unsigned long nr_pages,
1497 struct zone *zone, struct memory_notify *arg)
1498{
1499 struct pglist_data *pgdat = zone->zone_pgdat;
1500 unsigned long present_pages = 0;
1501 enum zone_type zt, zone_last = ZONE_NORMAL;
1502
1503 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001504 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1505 * contains nodes which have zones of 0...ZONE_NORMAL,
1506 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -08001507 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001508 * If we don't have HIGHMEM nor movable node,
1509 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1510 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -08001511 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001512 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -08001513 zone_last = ZONE_MOVABLE;
1514
1515 /*
1516 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1517 * If the memory to be offline is in a zone of 0...zone_last,
1518 * and it is the last present memory, 0...zone_last will
1519 * become empty after offline , thus we can determind we will
1520 * need to clear the node from node_states[N_NORMAL_MEMORY].
1521 */
1522 for (zt = 0; zt <= zone_last; zt++)
1523 present_pages += pgdat->node_zones[zt].present_pages;
1524 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1525 arg->status_change_nid_normal = zone_to_nid(zone);
1526 else
1527 arg->status_change_nid_normal = -1;
1528
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001529#ifdef CONFIG_HIGHMEM
1530 /*
1531 * If we have movable node, node_states[N_HIGH_MEMORY]
1532 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1533 * set zone_last to ZONE_HIGHMEM.
1534 *
1535 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1536 * contains nodes which have zones of 0...ZONE_MOVABLE,
1537 * set zone_last to ZONE_MOVABLE.
1538 */
1539 zone_last = ZONE_HIGHMEM;
1540 if (N_MEMORY == N_HIGH_MEMORY)
1541 zone_last = ZONE_MOVABLE;
1542
1543 for (; zt <= zone_last; zt++)
1544 present_pages += pgdat->node_zones[zt].present_pages;
1545 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1546 arg->status_change_nid_high = zone_to_nid(zone);
1547 else
1548 arg->status_change_nid_high = -1;
1549#else
1550 arg->status_change_nid_high = arg->status_change_nid_normal;
1551#endif
1552
Lai Jiangshand9713672012-12-11 16:01:03 -08001553 /*
1554 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1555 */
1556 zone_last = ZONE_MOVABLE;
1557
1558 /*
1559 * check whether node_states[N_HIGH_MEMORY] will be changed
1560 * If we try to offline the last present @nr_pages from the node,
1561 * we can determind we will need to clear the node from
1562 * node_states[N_HIGH_MEMORY].
1563 */
1564 for (; zt <= zone_last; zt++)
1565 present_pages += pgdat->node_zones[zt].present_pages;
1566 if (nr_pages >= present_pages)
1567 arg->status_change_nid = zone_to_nid(zone);
1568 else
1569 arg->status_change_nid = -1;
1570}
1571
1572static void node_states_clear_node(int node, struct memory_notify *arg)
1573{
1574 if (arg->status_change_nid_normal >= 0)
1575 node_clear_state(node, N_NORMAL_MEMORY);
1576
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001577 if ((N_MEMORY != N_NORMAL_MEMORY) &&
1578 (arg->status_change_nid_high >= 0))
Lai Jiangshand9713672012-12-11 16:01:03 -08001579 node_clear_state(node, N_HIGH_MEMORY);
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001580
1581 if ((N_MEMORY != N_HIGH_MEMORY) &&
1582 (arg->status_change_nid >= 0))
1583 node_clear_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -08001584}
1585
Wen Congyanga16cee12012-10-08 16:33:58 -07001586static int __ref __offline_pages(unsigned long start_pfn,
Michal Hockoecde0f32017-11-15 17:33:38 -08001587 unsigned long end_pfn)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001588{
Michal Hockoecde0f32017-11-15 17:33:38 -08001589 unsigned long pfn, nr_pages;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001590 long offlined_pages;
Michal Hocko72b39cf2017-11-15 17:33:34 -08001591 int ret, node;
Cody P Schaferd7029092013-07-03 15:02:11 -07001592 unsigned long flags;
Toshi Kania96dfdd2017-02-03 13:13:23 -08001593 unsigned long valid_start, valid_end;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001594 struct zone *zone;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001595 struct memory_notify arg;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001596
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001597 /* at least, alignment against pageblock is necessary */
1598 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1599 return -EINVAL;
1600 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1601 return -EINVAL;
1602 /* This makes hotplug much easier...and readable.
1603 we assume this for now. .*/
Toshi Kania96dfdd2017-02-03 13:13:23 -08001604 if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start, &valid_end))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001605 return -EINVAL;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001606
Toshi Kania96dfdd2017-02-03 13:13:23 -08001607 zone = page_zone(pfn_to_page(valid_start));
Yasunori Goto7b78d332007-10-21 16:41:36 -07001608 node = zone_to_nid(zone);
1609 nr_pages = end_pfn - start_pfn;
1610
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001611 /* set above range as isolated */
Wen Congyangb023f462012-12-11 16:00:45 -08001612 ret = start_isolate_page_range(start_pfn, end_pfn,
1613 MIGRATE_MOVABLE, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001614 if (ret)
David Rientjes30467e02015-04-14 15:45:11 -07001615 return ret;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001616
1617 arg.start_pfn = start_pfn;
1618 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -08001619 node_states_check_changes_offline(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001620
1621 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1622 ret = notifier_to_errno(ret);
1623 if (ret)
1624 goto failed_removal;
1625
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001626 pfn = start_pfn;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001627repeat:
1628 /* start memory hot removal */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001629 ret = -EINTR;
1630 if (signal_pending(current))
1631 goto failed_removal;
Michal Hocko72b39cf2017-11-15 17:33:34 -08001632
1633 cond_resched();
Michal Hocko9852a722018-01-31 16:16:19 -08001634 lru_add_drain_all();
Michal Hocko72b39cf2017-11-15 17:33:34 -08001635 drain_all_pages(zone);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001636
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001637 pfn = scan_movable_pages(start_pfn, end_pfn);
1638 if (pfn) { /* We have movable pages */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001639 ret = do_migrate_range(pfn, end_pfn);
Michal Hocko72b39cf2017-11-15 17:33:34 -08001640 goto repeat;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001641 }
Michal Hocko72b39cf2017-11-15 17:33:34 -08001642
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001643 /*
1644 * dissolve free hugepages in the memory block before doing offlining
1645 * actually in order to make hugetlbfs's object counting consistent.
1646 */
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001647 ret = dissolve_free_huge_pages(start_pfn, end_pfn);
1648 if (ret)
1649 goto failed_removal;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001650 /* check again */
1651 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
Michal Hocko72b39cf2017-11-15 17:33:34 -08001652 if (offlined_pages < 0)
1653 goto repeat;
Chen Yuconge33e33b2016-03-17 14:19:35 -07001654 pr_info("Offlined Pages %ld\n", offlined_pages);
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001655 /* Ok, all of our target is isolated.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001656 We cannot do rollback at this point. */
1657 offline_isolated_pages(start_pfn, end_pfn);
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -08001658 /* reset pagetype flags and makes migrate type to be MOVABLE */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02001659 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001660 /* removal success */
Jiang Liu3dcc0572013-07-03 15:03:21 -07001661 adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001662 zone->present_pages -= offlined_pages;
Cody P Schaferd7029092013-07-03 15:02:11 -07001663
1664 pgdat_resize_lock(zone->zone_pgdat, &flags);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001665 zone->zone_pgdat->node_present_pages -= offlined_pages;
Cody P Schaferd7029092013-07-03 15:02:11 -07001666 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001667
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07001668 init_per_zone_wmark_min();
1669
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001670 if (!populated_zone(zone)) {
Jiang Liu340175b2012-07-31 16:43:32 -07001671 zone_pcp_reset(zone);
Michal Hocko72675e12017-09-06 16:20:24 -07001672 build_all_zonelists(NULL);
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001673 } else
1674 zone_pcp_update(zone);
Jiang Liu340175b2012-07-31 16:43:32 -07001675
Lai Jiangshand9713672012-12-11 16:01:03 -08001676 node_states_clear_node(node, &arg);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001677 if (arg.status_change_nid >= 0) {
David Rientjes8fe23e02009-12-14 17:58:33 -08001678 kswapd_stop(node);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001679 kcompactd_stop(node);
1680 }
Minchan Kimbce73942009-06-16 15:32:50 -07001681
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001682 vm_total_pages = nr_free_pagecache_pages();
1683 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001684
1685 memory_notify(MEM_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001686 return 0;
1687
1688failed_removal:
Chen Yuconge33e33b2016-03-17 14:19:35 -07001689 pr_debug("memory offlining [mem %#010llx-%#010llx] failed\n",
1690 (unsigned long long) start_pfn << PAGE_SHIFT,
1691 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001692 memory_notify(MEM_CANCEL_OFFLINE, &arg);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001693 /* pushback to free area */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02001694 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001695 return ret;
1696}
Badari Pulavarty71088782008-10-18 20:25:58 -07001697
Michal Hockob93e0f32017-09-06 16:20:37 -07001698/* Must be protected by mem_hotplug_begin() or a device_lock */
Wen Congyanga16cee12012-10-08 16:33:58 -07001699int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1700{
Michal Hockoecde0f32017-11-15 17:33:38 -08001701 return __offline_pages(start_pfn, start_pfn + nr_pages);
Wen Congyanga16cee12012-10-08 16:33:58 -07001702}
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02001703#endif /* CONFIG_MEMORY_HOTREMOVE */
Wen Congyanga16cee12012-10-08 16:33:58 -07001704
Wen Congyangbbc76be2013-02-22 16:32:54 -08001705/**
1706 * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
1707 * @start_pfn: start pfn of the memory range
Toshi Kanie05c4bb2013-04-29 15:06:16 -07001708 * @end_pfn: end pfn of the memory range
Wen Congyangbbc76be2013-02-22 16:32:54 -08001709 * @arg: argument passed to func
1710 * @func: callback for each memory section walked
1711 *
1712 * This function walks through all present mem sections in range
1713 * [start_pfn, end_pfn) and call func on each mem section.
1714 *
1715 * Returns the return value of func.
1716 */
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02001717int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
Wen Congyangbbc76be2013-02-22 16:32:54 -08001718 void *arg, int (*func)(struct memory_block *, void *))
Badari Pulavarty71088782008-10-18 20:25:58 -07001719{
Wen Congyange90bdb72012-10-08 16:34:01 -07001720 struct memory_block *mem = NULL;
1721 struct mem_section *section;
Wen Congyange90bdb72012-10-08 16:34:01 -07001722 unsigned long pfn, section_nr;
1723 int ret;
Badari Pulavarty71088782008-10-18 20:25:58 -07001724
Wen Congyange90bdb72012-10-08 16:34:01 -07001725 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1726 section_nr = pfn_to_section_nr(pfn);
1727 if (!present_section_nr(section_nr))
1728 continue;
1729
1730 section = __nr_to_section(section_nr);
1731 /* same memblock? */
1732 if (mem)
1733 if ((section_nr >= mem->start_section_nr) &&
1734 (section_nr <= mem->end_section_nr))
1735 continue;
1736
1737 mem = find_memory_block_hinted(section, mem);
1738 if (!mem)
1739 continue;
1740
Wen Congyangbbc76be2013-02-22 16:32:54 -08001741 ret = func(mem, arg);
Wen Congyange90bdb72012-10-08 16:34:01 -07001742 if (ret) {
Wen Congyangbbc76be2013-02-22 16:32:54 -08001743 kobject_put(&mem->dev.kobj);
1744 return ret;
Wen Congyange90bdb72012-10-08 16:34:01 -07001745 }
1746 }
1747
1748 if (mem)
1749 kobject_put(&mem->dev.kobj);
1750
Wen Congyangbbc76be2013-02-22 16:32:54 -08001751 return 0;
1752}
1753
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +02001754#ifdef CONFIG_MEMORY_HOTREMOVE
Xishi Qiud6de9d52013-11-12 15:07:20 -08001755static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
Wen Congyangbbc76be2013-02-22 16:32:54 -08001756{
1757 int ret = !is_memblock_offlined(mem);
1758
Randy Dunlap349daa02013-04-29 15:08:49 -07001759 if (unlikely(ret)) {
1760 phys_addr_t beginpa, endpa;
1761
1762 beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
1763 endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
Joe Perches756a0252016-03-17 14:19:47 -07001764 pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n",
Randy Dunlap349daa02013-04-29 15:08:49 -07001765 &beginpa, &endpa);
1766 }
Wen Congyangbbc76be2013-02-22 16:32:54 -08001767
1768 return ret;
1769}
1770
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001771static int check_cpu_on_node(pg_data_t *pgdat)
Tang Chen60a5a192013-02-22 16:33:14 -08001772{
Tang Chen60a5a192013-02-22 16:33:14 -08001773 int cpu;
1774
1775 for_each_present_cpu(cpu) {
1776 if (cpu_to_node(cpu) == pgdat->node_id)
1777 /*
1778 * the cpu on this node isn't removed, and we can't
1779 * offline this node.
1780 */
1781 return -EBUSY;
1782 }
1783
1784 return 0;
1785}
1786
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001787static void unmap_cpu_on_node(pg_data_t *pgdat)
Wen Congyange13fe862013-02-22 16:33:31 -08001788{
1789#ifdef CONFIG_ACPI_NUMA
Wen Congyange13fe862013-02-22 16:33:31 -08001790 int cpu;
1791
1792 for_each_possible_cpu(cpu)
1793 if (cpu_to_node(cpu) == pgdat->node_id)
1794 numa_clear_node(cpu);
1795#endif
1796}
1797
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001798static int check_and_unmap_cpu_on_node(pg_data_t *pgdat)
Wen Congyange13fe862013-02-22 16:33:31 -08001799{
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001800 int ret;
Wen Congyange13fe862013-02-22 16:33:31 -08001801
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001802 ret = check_cpu_on_node(pgdat);
Wen Congyange13fe862013-02-22 16:33:31 -08001803 if (ret)
1804 return ret;
1805
1806 /*
1807 * the node will be offlined when we come here, so we can clear
1808 * the cpu_to_node() now.
1809 */
1810
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001811 unmap_cpu_on_node(pgdat);
Wen Congyange13fe862013-02-22 16:33:31 -08001812 return 0;
1813}
1814
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001815/**
1816 * try_offline_node
1817 *
1818 * Offline a node if all memory sections and cpus of the node are removed.
1819 *
1820 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1821 * and online/offline operations before this call.
1822 */
Wen Congyang90b30cd2013-02-22 16:33:27 -08001823void try_offline_node(int nid)
Tang Chen60a5a192013-02-22 16:33:14 -08001824{
Wen Congyangd822b862013-02-22 16:33:16 -08001825 pg_data_t *pgdat = NODE_DATA(nid);
1826 unsigned long start_pfn = pgdat->node_start_pfn;
1827 unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
Tang Chen60a5a192013-02-22 16:33:14 -08001828 unsigned long pfn;
1829
1830 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1831 unsigned long section_nr = pfn_to_section_nr(pfn);
1832
1833 if (!present_section_nr(section_nr))
1834 continue;
1835
1836 if (pfn_to_nid(pfn) != nid)
1837 continue;
1838
1839 /*
1840 * some memory sections of this node are not removed, and we
1841 * can't offline node now.
1842 */
1843 return;
1844 }
1845
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001846 if (check_and_unmap_cpu_on_node(pgdat))
Tang Chen60a5a192013-02-22 16:33:14 -08001847 return;
1848
1849 /*
1850 * all memory/cpu of this node are removed, we can offline this
1851 * node now.
1852 */
1853 node_set_offline(nid);
1854 unregister_one_node(nid);
1855}
Wen Congyang90b30cd2013-02-22 16:33:27 -08001856EXPORT_SYMBOL(try_offline_node);
Tang Chen60a5a192013-02-22 16:33:14 -08001857
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001858/**
1859 * remove_memory
1860 *
1861 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1862 * and online/offline operations before this call, as required by
1863 * try_offline_node().
1864 */
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02001865void __ref remove_memory(int nid, u64 start, u64 size)
Wen Congyangbbc76be2013-02-22 16:32:54 -08001866{
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02001867 int ret;
Wen Congyang993c1aa2013-02-22 16:32:50 -08001868
Toshi Kani27356f52013-09-11 14:21:49 -07001869 BUG_ON(check_hotplug_memory_range(start, size));
1870
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001871 mem_hotplug_begin();
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08001872
1873 /*
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02001874 * All memory blocks must be offlined before removing memory. Check
1875 * whether all memory blocks in question are offline and trigger a BUG()
1876 * if this is not the case.
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08001877 */
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02001878 ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
Xishi Qiud6de9d52013-11-12 15:07:20 -08001879 check_memblock_offlined_cb);
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001880 if (ret)
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02001881 BUG();
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08001882
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -08001883 /* remove memmap entry */
1884 firmware_map_remove(start, start + size, "System RAM");
Xishi Qiuf9126ab2015-08-14 15:35:16 -07001885 memblock_free(start, size);
1886 memblock_remove(start, size);
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -08001887
Christoph Hellwigda024512017-12-29 08:53:55 +01001888 arch_remove_memory(start, size, NULL);
Wen Congyang24d335c2013-02-22 16:32:58 -08001889
Tang Chen60a5a192013-02-22 16:33:14 -08001890 try_offline_node(nid);
1891
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001892 mem_hotplug_done();
Badari Pulavarty71088782008-10-18 20:25:58 -07001893}
Badari Pulavarty71088782008-10-18 20:25:58 -07001894EXPORT_SYMBOL_GPL(remove_memory);
Rafael J. Wysockiaba6efc2013-06-01 22:24:07 +02001895#endif /* CONFIG_MEMORY_HOTREMOVE */