blob: e60e28131f67936085391e668c90df8c20c7c087 [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>
Michal Hocko2c870722018-12-28 00:38:01 -080038#include <linux/rmap.h>
Dave Hansen3947be12005-10-29 18:16:54 -070039
40#include <asm/tlbflush.h>
41
Adrian Bunk1e5ad9a2008-04-28 20:40:08 +030042#include "internal.h"
43
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -070044/*
45 * online_page_callback contains pointer to current page onlining function.
46 * Initially it is generic_online_page(). If it is required it could be
47 * changed by calling set_online_page_callback() for callback registration
48 * and restore_online_page_callback() for generic callback restore.
49 */
50
51static void generic_online_page(struct page *page);
52
53static online_page_callback_t online_page_callback = generic_online_page;
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070054static DEFINE_MUTEX(online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -070055
Thomas Gleixner3f906ba2017-07-10 15:50:09 -070056DEFINE_STATIC_PERCPU_RWSEM(mem_hotplug_lock);
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080057
Thomas Gleixner3f906ba2017-07-10 15:50:09 -070058void get_online_mems(void)
59{
60 percpu_down_read(&mem_hotplug_lock);
61}
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070062
Thomas Gleixner3f906ba2017-07-10 15:50:09 -070063void put_online_mems(void)
64{
65 percpu_up_read(&mem_hotplug_lock);
66}
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070067
Michal Hocko49323812017-07-06 15:41:05 -070068bool movable_node_enabled = false;
69
Vitaly Kuznetsov8604d9e2016-05-19 17:13:03 -070070#ifndef CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -070071bool memhp_auto_online;
Vitaly Kuznetsov8604d9e2016-05-19 17:13:03 -070072#else
73bool memhp_auto_online = true;
74#endif
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -070075EXPORT_SYMBOL_GPL(memhp_auto_online);
76
Vitaly Kuznetsov86dd9952016-05-19 17:13:06 -070077static int __init setup_memhp_default_state(char *str)
78{
79 if (!strcmp(str, "online"))
80 memhp_auto_online = true;
81 else if (!strcmp(str, "offline"))
82 memhp_auto_online = false;
83
84 return 1;
85}
86__setup("memhp_default_state=", setup_memhp_default_state);
87
David Rientjes30467e02015-04-14 15:45:11 -070088void mem_hotplug_begin(void)
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070089{
Thomas Gleixner3f906ba2017-07-10 15:50:09 -070090 cpus_read_lock();
91 percpu_down_write(&mem_hotplug_lock);
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070092}
93
David Rientjes30467e02015-04-14 15:45:11 -070094void mem_hotplug_done(void)
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070095{
Thomas Gleixner3f906ba2017-07-10 15:50:09 -070096 percpu_up_write(&mem_hotplug_lock);
97 cpus_read_unlock();
Vladimir Davydovbfc8c902014-06-04 16:07:18 -070098}
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080099
Keith Mannthey45e0b782006-09-30 23:27:09 -0700100/* add this memory to iomem resource */
101static struct resource *register_memory_resource(u64 start, u64 size)
102{
Jérôme Glisse5042db42017-09-08 16:11:43 -0700103 struct resource *res, *conflict;
Keith Mannthey45e0b782006-09-30 23:27:09 -0700104 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -0800105 if (!res)
106 return ERR_PTR(-ENOMEM);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700107
108 res->name = "System RAM";
109 res->start = start;
110 res->end = start + size - 1;
Toshi Kani782b8662016-01-26 21:57:24 +0100111 res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
Jérôme Glisse5042db42017-09-08 16:11:43 -0700112 conflict = request_resource_conflict(&iomem_resource, res);
113 if (conflict) {
114 if (conflict->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY) {
115 pr_debug("Device unaddressable memory block "
116 "memory hotplug at %#010llx !\n",
117 (unsigned long long)start);
118 }
Toshi Kani4996eed2013-07-03 15:02:39 -0700119 pr_debug("System RAM resource %pR cannot be added\n", res);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700120 kfree(res);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -0800121 return ERR_PTR(-EEXIST);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700122 }
123 return res;
124}
125
126static void release_memory_resource(struct resource *res)
127{
128 if (!res)
129 return;
130 release_resource(res);
131 kfree(res);
132 return;
133}
134
Keith Mannthey53947022006-09-30 23:27:08 -0700135#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800136void get_page_bootmem(unsigned long info, struct page *page,
137 unsigned long type)
Yasunori Goto04753272008-04-28 02:13:31 -0700138{
Yasuaki Ishimatsuddffe982017-02-22 15:45:13 -0800139 page->freelist = (void *)type;
Yasunori Goto04753272008-04-28 02:13:31 -0700140 SetPagePrivate(page);
141 set_page_private(page, info);
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700142 page_ref_inc(page);
Yasunori Goto04753272008-04-28 02:13:31 -0700143}
144
Jiang Liu170a5a72013-07-03 15:03:17 -0700145void put_page_bootmem(struct page *page)
Yasunori Goto04753272008-04-28 02:13:31 -0700146{
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800147 unsigned long type;
Yasunori Goto04753272008-04-28 02:13:31 -0700148
Yasuaki Ishimatsuddffe982017-02-22 15:45:13 -0800149 type = (unsigned long) page->freelist;
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800150 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
151 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
Yasunori Goto04753272008-04-28 02:13:31 -0700152
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700153 if (page_ref_dec_return(page) == 1) {
Yasuaki Ishimatsuddffe982017-02-22 15:45:13 -0800154 page->freelist = NULL;
Yasunori Goto04753272008-04-28 02:13:31 -0700155 ClearPagePrivate(page);
156 set_page_private(page, 0);
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800157 INIT_LIST_HEAD(&page->lru);
Jiang Liu170a5a72013-07-03 15:03:17 -0700158 free_reserved_page(page);
Yasunori Goto04753272008-04-28 02:13:31 -0700159 }
Yasunori Goto04753272008-04-28 02:13:31 -0700160}
161
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800162#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
163#ifndef CONFIG_SPARSEMEM_VMEMMAP
Adrian Bunkd92bc312008-07-23 21:28:12 -0700164static void register_page_bootmem_info_section(unsigned long start_pfn)
Yasunori Goto04753272008-04-28 02:13:31 -0700165{
166 unsigned long *usemap, mapsize, section_nr, i;
167 struct mem_section *ms;
168 struct page *page, *memmap;
169
Yasunori Goto04753272008-04-28 02:13:31 -0700170 section_nr = pfn_to_section_nr(start_pfn);
171 ms = __nr_to_section(section_nr);
172
173 /* Get section's memmap address */
174 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
175
176 /*
177 * Get page for the memmap's phys address
178 * XXX: need more consideration for sparse_vmemmap...
179 */
180 page = virt_to_page(memmap);
181 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
182 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
183
184 /* remember memmap's page */
185 for (i = 0; i < mapsize; i++, page++)
186 get_page_bootmem(section_nr, page, SECTION_INFO);
187
Oscar Salvador9ac93222018-01-31 16:17:25 -0800188 usemap = ms->pageblock_flags;
Yasunori Goto04753272008-04-28 02:13:31 -0700189 page = virt_to_page(usemap);
190
191 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
192
193 for (i = 0; i < mapsize; i++, page++)
Yasunori Gotoaf370fb2008-07-23 21:28:17 -0700194 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
Yasunori Goto04753272008-04-28 02:13:31 -0700195
196}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800197#else /* CONFIG_SPARSEMEM_VMEMMAP */
198static void register_page_bootmem_info_section(unsigned long start_pfn)
199{
200 unsigned long *usemap, mapsize, section_nr, i;
201 struct mem_section *ms;
202 struct page *page, *memmap;
203
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800204 section_nr = pfn_to_section_nr(start_pfn);
205 ms = __nr_to_section(section_nr);
206
207 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
208
209 register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
210
Oscar Salvador9ac93222018-01-31 16:17:25 -0800211 usemap = ms->pageblock_flags;
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800212 page = virt_to_page(usemap);
213
214 mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
215
216 for (i = 0; i < mapsize; i++, page++)
217 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
218}
219#endif /* !CONFIG_SPARSEMEM_VMEMMAP */
Yasunori Goto04753272008-04-28 02:13:31 -0700220
Linus Torvalds7ded3842016-05-27 15:23:32 -0700221void __init register_page_bootmem_info_node(struct pglist_data *pgdat)
Yasunori Goto04753272008-04-28 02:13:31 -0700222{
223 unsigned long i, pfn, end_pfn, nr_pages;
224 int node = pgdat->node_id;
225 struct page *page;
Yasunori Goto04753272008-04-28 02:13:31 -0700226
227 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
228 page = virt_to_page(pgdat);
229
230 for (i = 0; i < nr_pages; i++, page++)
231 get_page_bootmem(node, page, NODE_INFO);
232
Yasunori Goto04753272008-04-28 02:13:31 -0700233 pfn = pgdat->node_start_pfn;
Cody P Schaferc1f19492013-02-22 16:35:32 -0800234 end_pfn = pgdat_end_pfn(pgdat);
Yasunori Goto04753272008-04-28 02:13:31 -0700235
Tang Chen7e9f5eb2013-07-08 16:00:23 -0700236 /* register section info */
qiuxishif14851a2012-09-17 14:09:24 -0700237 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
238 /*
239 * Some platforms can assign the same pfn to multiple nodes - on
240 * node0 as well as nodeN. To avoid registering a pfn against
241 * multiple nodes we check that this pfn does not already
Tang Chen7e9f5eb2013-07-08 16:00:23 -0700242 * reside in some other nodes.
qiuxishif14851a2012-09-17 14:09:24 -0700243 */
Yang Shif65e91df2016-05-27 14:27:32 -0700244 if (pfn_valid(pfn) && (early_pfn_to_nid(pfn) == node))
qiuxishif14851a2012-09-17 14:09:24 -0700245 register_page_bootmem_info_section(pfn);
246 }
Yasunori Goto04753272008-04-28 02:13:31 -0700247}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -0800248#endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
Yasunori Goto04753272008-04-28 02:13:31 -0700249
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700250static int __meminit __add_section(int nid, unsigned long phys_start_pfn,
Christoph Hellwig7b73d972017-12-29 08:53:54 +0100251 struct vmem_altmap *altmap, bool want_memblock)
Dave Hansen3947be12005-10-29 18:16:54 -0700252{
Dave Hansen3947be12005-10-29 18:16:54 -0700253 int ret;
254
KAMEZAWA Hiroyukiebd15302006-08-05 12:15:06 -0700255 if (pfn_valid(phys_start_pfn))
256 return -EEXIST;
257
Wei Yangaa2e8b682020-01-28 10:50:00 +0100258 ret = sparse_add_one_section(nid, phys_start_pfn, altmap);
David Hildenbrandaa49b6ab2020-01-28 10:50:13 +0100259 return ret < 0 ? ret : 0;
Dave Hansen3947be12005-10-29 18:16:54 -0700260}
261
David Rientjes4edd7ce2013-04-29 15:08:22 -0700262/*
263 * Reasonably generic function for adding memory. It is
264 * expected that archs that support memory hotplug will
265 * call this function after deciding the zone to which to
266 * add the new pages.
267 */
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700268int __ref __add_pages(int nid, unsigned long phys_start_pfn,
Christoph Hellwig24e6d5a2017-12-29 08:53:53 +0100269 unsigned long nr_pages, struct vmem_altmap *altmap,
270 bool want_memblock)
David Rientjes4edd7ce2013-04-29 15:08:22 -0700271{
272 unsigned long i;
273 int err = 0;
274 int start_sec, end_sec;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800275
David Rientjes4edd7ce2013-04-29 15:08:22 -0700276 /* during initialize mem_map, align hot-added range to section */
277 start_sec = pfn_to_section_nr(phys_start_pfn);
278 end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
279
Dan Williams4b94ffd2016-01-15 16:56:22 -0800280 if (altmap) {
281 /*
282 * Validate altmap is within bounds of the total request
283 */
284 if (altmap->base_pfn != phys_start_pfn
285 || vmem_altmap_offset(altmap) > nr_pages) {
286 pr_warn_once("memory add fail, invalid altmap\n");
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700287 err = -EINVAL;
288 goto out;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800289 }
290 altmap->alloc = 0;
291 }
292
David Rientjes4edd7ce2013-04-29 15:08:22 -0700293 for (i = start_sec; i <= end_sec; i++) {
Christoph Hellwig7b73d972017-12-29 08:53:54 +0100294 err = __add_section(nid, section_nr_to_pfn(i), altmap,
295 want_memblock);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700296
297 /*
298 * EEXIST is finally dealt with by ioresource collision
299 * check. see add_memory() => register_memory_resource()
300 * Warning will be printed if there is collision.
301 */
302 if (err && (err != -EEXIST))
303 break;
304 err = 0;
Michal Hockof64ac5e2017-10-03 16:16:16 -0700305 cond_resched();
David Rientjes4edd7ce2013-04-29 15:08:22 -0700306 }
Zhu Guihuac435a392015-06-24 16:58:42 -0700307 vmemmap_populate_print_last();
Joonsoo Kim7cf91a92016-03-15 14:57:51 -0700308out:
David Rientjes4edd7ce2013-04-29 15:08:22 -0700309 return err;
310}
David Rientjes4edd7ce2013-04-29 15:08:22 -0700311
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800312/* find the smallest valid pfn in the range [start_pfn, end_pfn) */
YASUAKI ISHIMATSUd09b0132017-10-03 16:16:32 -0700313static unsigned long find_smallest_section_pfn(int nid, struct zone *zone,
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800314 unsigned long start_pfn,
315 unsigned long end_pfn)
316{
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800317 for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
David Hildenbrand5779cbc2019-11-21 17:53:56 -0800318 if (unlikely(!pfn_to_online_page(start_pfn)))
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800319 continue;
320
321 if (unlikely(pfn_to_nid(start_pfn) != nid))
322 continue;
323
324 if (zone && zone != page_zone(pfn_to_page(start_pfn)))
325 continue;
326
327 return start_pfn;
328 }
329
330 return 0;
331}
332
333/* find the biggest valid pfn in the range [start_pfn, end_pfn). */
YASUAKI ISHIMATSUd09b0132017-10-03 16:16:32 -0700334static unsigned long find_biggest_section_pfn(int nid, struct zone *zone,
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800335 unsigned long start_pfn,
336 unsigned long end_pfn)
337{
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800338 unsigned long pfn;
339
340 /* pfn is the end pfn of a memory section. */
341 pfn = end_pfn - 1;
342 for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
David Hildenbrand5779cbc2019-11-21 17:53:56 -0800343 if (unlikely(!pfn_to_online_page(pfn)))
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800344 continue;
345
346 if (unlikely(pfn_to_nid(pfn) != nid))
347 continue;
348
349 if (zone && zone != page_zone(pfn_to_page(pfn)))
350 continue;
351
352 return pfn;
353 }
354
355 return 0;
356}
357
358static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
359 unsigned long end_pfn)
360{
Xishi Qiuc33bc312013-09-11 14:21:44 -0700361 unsigned long zone_start_pfn = zone->zone_start_pfn;
362 unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
363 unsigned long zone_end_pfn = z;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800364 unsigned long pfn;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800365 int nid = zone_to_nid(zone);
366
367 zone_span_writelock(zone);
368 if (zone_start_pfn == start_pfn) {
369 /*
370 * If the section is smallest section in the zone, it need
371 * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
372 * In this case, we find second smallest valid mem_section
373 * for shrinking zone.
374 */
375 pfn = find_smallest_section_pfn(nid, zone, end_pfn,
376 zone_end_pfn);
377 if (pfn) {
378 zone->zone_start_pfn = pfn;
379 zone->spanned_pages = zone_end_pfn - pfn;
380 }
381 } else if (zone_end_pfn == end_pfn) {
382 /*
383 * If the section is biggest section in the zone, it need
384 * shrink zone->spanned_pages.
385 * In this case, we find second biggest valid mem_section for
386 * shrinking zone.
387 */
388 pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
389 start_pfn);
390 if (pfn)
391 zone->spanned_pages = pfn - zone_start_pfn + 1;
392 }
393
394 /*
395 * The section is not biggest or smallest mem_section in the zone, it
396 * only creates a hole in the zone. So in this case, we need not
397 * change the zone. But perhaps, the zone has only hole data. Thus
398 * it check the zone has only hole or not.
399 */
400 pfn = zone_start_pfn;
401 for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
David Hildenbrand5779cbc2019-11-21 17:53:56 -0800402 if (unlikely(!pfn_to_online_page(pfn)))
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800403 continue;
404
405 if (page_zone(pfn_to_page(pfn)) != zone)
406 continue;
407
408 /* If the section is current section, it continues the loop */
409 if (start_pfn == pfn)
410 continue;
411
412 /* If we find valid section, we have nothing to do */
413 zone_span_writeunlock(zone);
414 return;
415 }
416
417 /* The zone has no valid section */
418 zone->zone_start_pfn = 0;
419 zone->spanned_pages = 0;
420 zone_span_writeunlock(zone);
421}
422
David Hildenbrand6631def2019-10-18 20:19:33 -0700423static void update_pgdat_span(struct pglist_data *pgdat)
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800424{
David Hildenbrand6631def2019-10-18 20:19:33 -0700425 unsigned long node_start_pfn = 0, node_end_pfn = 0;
426 struct zone *zone;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800427
David Hildenbrand6631def2019-10-18 20:19:33 -0700428 for (zone = pgdat->node_zones;
429 zone < pgdat->node_zones + MAX_NR_ZONES; zone++) {
430 unsigned long zone_end_pfn = zone->zone_start_pfn +
431 zone->spanned_pages;
432
433 /* No need to lock the zones, they can't change. */
David Hildenbrandf8b09a02019-11-05 21:17:10 -0800434 if (!zone->spanned_pages)
435 continue;
436 if (!node_end_pfn) {
437 node_start_pfn = zone->zone_start_pfn;
438 node_end_pfn = zone_end_pfn;
439 continue;
440 }
441
David Hildenbrand6631def2019-10-18 20:19:33 -0700442 if (zone_end_pfn > node_end_pfn)
443 node_end_pfn = zone_end_pfn;
444 if (zone->zone_start_pfn < node_start_pfn)
445 node_start_pfn = zone->zone_start_pfn;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800446 }
447
David Hildenbrand6631def2019-10-18 20:19:33 -0700448 pgdat->node_start_pfn = node_start_pfn;
449 pgdat->node_spanned_pages = node_end_pfn - node_start_pfn;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800450}
451
David Hildenbrand86834892020-01-28 10:50:21 +0100452void __ref remove_pfn_range_from_zone(struct zone *zone,
453 unsigned long start_pfn,
454 unsigned long nr_pages)
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800455{
456 struct pglist_data *pgdat = zone->zone_pgdat;
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800457 unsigned long flags;
458
David Hildenbrand5779cbc2019-11-21 17:53:56 -0800459#ifdef CONFIG_ZONE_DEVICE
460 /*
461 * Zone shrinking code cannot properly deal with ZONE_DEVICE. So
462 * we will not try to shrink the zones - which is okay as
463 * set_zone_contiguous() cannot deal with ZONE_DEVICE either way.
464 */
465 if (zone_idx(zone) == ZONE_DEVICE)
466 return;
467#endif
468
David Hildenbrand86834892020-01-28 10:50:21 +0100469 clear_zone_contiguous(zone);
470
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800471 pgdat_resize_lock(zone->zone_pgdat, &flags);
472 shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
David Hildenbrand6631def2019-10-18 20:19:33 -0700473 update_pgdat_span(pgdat);
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800474 pgdat_resize_unlock(zone->zone_pgdat, &flags);
David Hildenbrand86834892020-01-28 10:50:21 +0100475
476 set_zone_contiguous(zone);
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800477}
478
David Hildenbrand86834892020-01-28 10:50:21 +0100479static void __remove_section(struct mem_section *ms, unsigned long map_offset,
David Hildenbrandefaa8fb2020-01-28 10:50:07 +0100480 struct vmem_altmap *altmap)
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700481{
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800482 unsigned long start_pfn;
483 int scn_nr;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700484
David Hildenbrandefaa8fb2020-01-28 10:50:07 +0100485 if (WARN_ON_ONCE(!valid_section(ms)))
486 return;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700487
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800488 scn_nr = __section_nr(ms);
YASUAKI ISHIMATSU1dd2bfc2017-10-03 16:16:29 -0700489 start_pfn = section_nr_to_pfn((unsigned long)scn_nr);
Yasuaki Ishimatsu815121d2013-02-22 16:33:12 -0800490
David Hildenbranddc6be852020-01-28 10:50:16 +0100491 sparse_remove_one_section(ms, map_offset, altmap);
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700492}
493
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700494/**
David Hildenbrand86834892020-01-28 10:50:21 +0100495 * __remove_pages() - remove sections of pages
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700496 * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
497 * @nr_pages: number of pages to remove (must be multiple of section size)
Mike Rapoporte8b098f2018-04-05 16:24:57 -0700498 * @altmap: alternative device page map or %NULL if default memmap is used
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700499 *
500 * Generic helper function to remove section mappings and sysfs entries
501 * for the section of the memory we are removing. Caller needs to make
502 * sure that pages are marked reserved and zones are adjust properly by
503 * calling offline_pages().
504 */
David Hildenbrand86834892020-01-28 10:50:21 +0100505void __remove_pages(unsigned long phys_start_pfn, unsigned long nr_pages,
506 struct vmem_altmap *altmap)
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700507{
Toshi Kanife74ebb2013-04-29 15:08:20 -0700508 unsigned long i;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800509 unsigned long map_offset = 0;
David Hildenbrandefaa8fb2020-01-28 10:50:07 +0100510 int sections_to_remove;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800511
Dan Williamsb9cda652020-01-28 10:50:17 +0100512 if (altmap)
513 map_offset = vmem_altmap_offset(altmap);
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700514
515 /*
516 * We can only remove entire sections
517 */
518 BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
519 BUG_ON(nr_pages % PAGES_PER_SECTION);
520
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700521 sections_to_remove = nr_pages / PAGES_PER_SECTION;
522 for (i = 0; i < sections_to_remove; i++) {
523 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800524
Michal Hockoccd35ba52018-11-02 15:48:46 -0700525 cond_resched();
David Hildenbrand86834892020-01-28 10:50:21 +0100526 __remove_section(__pfn_to_section(pfn), map_offset, altmap);
Dan Williams4b94ffd2016-01-15 16:56:22 -0800527 map_offset = 0;
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700528 }
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700529}
Badari Pulavartyea01ea92008-04-28 02:12:01 -0700530
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700531int set_online_page_callback(online_page_callback_t callback)
532{
533 int rc = -EINVAL;
534
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700535 get_online_mems();
536 mutex_lock(&online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700537
538 if (online_page_callback == generic_online_page) {
539 online_page_callback = callback;
540 rc = 0;
541 }
542
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700543 mutex_unlock(&online_page_callback_lock);
544 put_online_mems();
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700545
546 return rc;
547}
548EXPORT_SYMBOL_GPL(set_online_page_callback);
549
550int restore_online_page_callback(online_page_callback_t callback)
551{
552 int rc = -EINVAL;
553
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700554 get_online_mems();
555 mutex_lock(&online_page_callback_lock);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700556
557 if (online_page_callback == callback) {
558 online_page_callback = generic_online_page;
559 rc = 0;
560 }
561
Vladimir Davydovbfc8c902014-06-04 16:07:18 -0700562 mutex_unlock(&online_page_callback_lock);
563 put_online_mems();
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700564
565 return rc;
566}
567EXPORT_SYMBOL_GPL(restore_online_page_callback);
568
569void __online_page_set_limits(struct page *page)
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700570{
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700571}
572EXPORT_SYMBOL_GPL(__online_page_set_limits);
573
574void __online_page_increment_counters(struct page *page)
575{
Jiang Liu3dcc0572013-07-03 15:03:21 -0700576 adjust_managed_page_count(page, 1);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700577}
578EXPORT_SYMBOL_GPL(__online_page_increment_counters);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700579
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700580void __online_page_free(struct page *page)
581{
Jiang Liu3dcc0572013-07-03 15:03:21 -0700582 __free_reserved_page(page);
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700583}
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700584EXPORT_SYMBOL_GPL(__online_page_free);
585
586static void generic_online_page(struct page *page)
587{
588 __online_page_set_limits(page);
589 __online_page_increment_counters(page);
590 __online_page_free(page);
591}
Jeremy Fitzhardinge180c06e2008-04-28 02:12:03 -0700592
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700593static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
594 void *arg)
Dave Hansen3947be12005-10-29 18:16:54 -0700595{
596 unsigned long i;
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700597 unsigned long onlined_pages = *(unsigned long *)arg;
598 struct page *page;
Michal Hocko2d070ea2017-07-06 15:37:56 -0700599
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700600 if (PageReserved(pfn_to_page(start_pfn)))
601 for (i = 0; i < nr_pages; i++) {
602 page = pfn_to_page(start_pfn + i);
Daniel Kiper9d0ad8c2011-07-25 17:12:05 -0700603 (*online_page_callback)(page);
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700604 onlined_pages++;
605 }
Michal Hocko2d070ea2017-07-06 15:37:56 -0700606
607 online_mem_sections(start_pfn, start_pfn + nr_pages);
608
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700609 *(unsigned long *)arg = onlined_pages;
610 return 0;
611}
612
Lai Jiangshand9713672012-12-11 16:01:03 -0800613/* check which state of node_states will be changed when online memory */
614static void node_states_check_changes_online(unsigned long nr_pages,
615 struct zone *zone, struct memory_notify *arg)
616{
617 int nid = zone_to_nid(zone);
618 enum zone_type zone_last = ZONE_NORMAL;
619
620 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800621 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
622 * contains nodes which have zones of 0...ZONE_NORMAL,
623 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -0800624 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800625 * If we don't have HIGHMEM nor movable node,
626 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
627 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -0800628 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800629 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -0800630 zone_last = ZONE_MOVABLE;
631
632 /*
633 * if the memory to be online is in a zone of 0...zone_last, and
634 * the zones of 0...zone_last don't have memory before online, we will
635 * need to set the node to node_states[N_NORMAL_MEMORY] after
636 * the memory is online.
637 */
638 if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
639 arg->status_change_nid_normal = nid;
640 else
641 arg->status_change_nid_normal = -1;
642
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800643#ifdef CONFIG_HIGHMEM
644 /*
645 * If we have movable node, node_states[N_HIGH_MEMORY]
646 * contains nodes which have zones of 0...ZONE_HIGHMEM,
647 * set zone_last to ZONE_HIGHMEM.
648 *
649 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
650 * contains nodes which have zones of 0...ZONE_MOVABLE,
651 * set zone_last to ZONE_MOVABLE.
652 */
653 zone_last = ZONE_HIGHMEM;
654 if (N_MEMORY == N_HIGH_MEMORY)
655 zone_last = ZONE_MOVABLE;
656
657 if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
658 arg->status_change_nid_high = nid;
659 else
660 arg->status_change_nid_high = -1;
661#else
662 arg->status_change_nid_high = arg->status_change_nid_normal;
663#endif
664
Lai Jiangshand9713672012-12-11 16:01:03 -0800665 /*
666 * if the node don't have memory befor online, we will need to
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800667 * set the node to node_states[N_MEMORY] after the memory
Lai Jiangshand9713672012-12-11 16:01:03 -0800668 * is online.
669 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800670 if (!node_state(nid, N_MEMORY))
Lai Jiangshand9713672012-12-11 16:01:03 -0800671 arg->status_change_nid = nid;
672 else
673 arg->status_change_nid = -1;
674}
675
676static void node_states_set_node(int node, struct memory_notify *arg)
677{
678 if (arg->status_change_nid_normal >= 0)
679 node_set_state(node, N_NORMAL_MEMORY);
680
Lai Jiangshan6715ddf2012-12-12 13:51:49 -0800681 if (arg->status_change_nid_high >= 0)
682 node_set_state(node, N_HIGH_MEMORY);
683
684 node_set_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -0800685}
686
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700687static void __meminit resize_zone_range(struct zone *zone, unsigned long start_pfn,
688 unsigned long nr_pages)
689{
690 unsigned long old_end_pfn = zone_end_pfn(zone);
691
692 if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
693 zone->zone_start_pfn = start_pfn;
694
695 zone->spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - zone->zone_start_pfn;
696}
697
698static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned long start_pfn,
699 unsigned long nr_pages)
700{
701 unsigned long old_end_pfn = pgdat_end_pfn(pgdat);
702
703 if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
704 pgdat->node_start_pfn = start_pfn;
705
706 pgdat->node_spanned_pages = max(start_pfn + nr_pages, old_end_pfn) - pgdat->node_start_pfn;
707}
708
Christoph Hellwiga99583e2017-12-29 08:53:57 +0100709void __ref move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
710 unsigned long nr_pages, struct vmem_altmap *altmap)
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700711{
712 struct pglist_data *pgdat = zone->zone_pgdat;
713 int nid = pgdat->node_id;
714 unsigned long flags;
715
716 if (zone_is_empty(zone))
717 init_currently_empty_zone(zone, start_pfn, nr_pages);
718
719 clear_zone_contiguous(zone);
720
721 /* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */
722 pgdat_resize_lock(pgdat, &flags);
723 zone_span_writelock(zone);
724 resize_zone_range(zone, start_pfn, nr_pages);
725 zone_span_writeunlock(zone);
726 resize_pgdat_range(pgdat, start_pfn, nr_pages);
727 pgdat_resize_unlock(pgdat, &flags);
728
729 /*
730 * TODO now we have a visible range of pages which are not associated
731 * with their zone properly. Not nice but set_pfnblock_flags_mask
732 * expects the zone spans the pfn range. All the pages in the range
733 * are reserved so nobody should be touching them so we should be safe
734 */
Christoph Hellwiga99583e2017-12-29 08:53:57 +0100735 memmap_init_zone(nr_pages, nid, zone_idx(zone), start_pfn,
Laurent Dufour25eaea12020-09-25 21:19:28 -0700736 MEMINIT_HOTPLUG, altmap);
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700737
738 set_zone_contiguous(zone);
739}
740
741/*
Michal Hockoc246a212017-07-06 15:38:18 -0700742 * Returns a default kernel memory zone for the given pfn range.
743 * If no kernel zone covers this pfn range it will automatically go
744 * to the ZONE_NORMAL.
745 */
Michal Hockoc6f03e22017-09-06 16:19:40 -0700746static struct zone *default_kernel_zone_for_pfn(int nid, unsigned long start_pfn,
Michal Hockoc246a212017-07-06 15:38:18 -0700747 unsigned long nr_pages)
748{
749 struct pglist_data *pgdat = NODE_DATA(nid);
750 int zid;
751
752 for (zid = 0; zid <= ZONE_NORMAL; zid++) {
753 struct zone *zone = &pgdat->node_zones[zid];
754
755 if (zone_intersects(zone, start_pfn, nr_pages))
756 return zone;
757 }
758
759 return &pgdat->node_zones[ZONE_NORMAL];
760}
761
Michal Hockoc6f03e22017-09-06 16:19:40 -0700762static inline struct zone *default_zone_for_pfn(int nid, unsigned long start_pfn,
763 unsigned long nr_pages)
Michal Hockoe5e68932017-09-06 16:19:37 -0700764{
Michal Hockoc6f03e22017-09-06 16:19:40 -0700765 struct zone *kernel_zone = default_kernel_zone_for_pfn(nid, start_pfn,
766 nr_pages);
767 struct zone *movable_zone = &NODE_DATA(nid)->node_zones[ZONE_MOVABLE];
768 bool in_kernel = zone_intersects(kernel_zone, start_pfn, nr_pages);
769 bool in_movable = zone_intersects(movable_zone, start_pfn, nr_pages);
Michal Hockoe5e68932017-09-06 16:19:37 -0700770
771 /*
Michal Hockoc6f03e22017-09-06 16:19:40 -0700772 * We inherit the existing zone in a simple case where zones do not
773 * overlap in the given range
Michal Hockoe5e68932017-09-06 16:19:37 -0700774 */
Michal Hockoc6f03e22017-09-06 16:19:40 -0700775 if (in_kernel ^ in_movable)
776 return (in_kernel) ? kernel_zone : movable_zone;
Michal Hockoe5e68932017-09-06 16:19:37 -0700777
Michal Hockoc6f03e22017-09-06 16:19:40 -0700778 /*
779 * If the range doesn't belong to any zone or two zones overlap in the
780 * given range then we use movable zone only if movable_node is
781 * enabled because we always online to a kernel zone by default.
782 */
783 return movable_node_enabled ? movable_zone : kernel_zone;
Michal Hocko9f123ab2017-07-10 15:48:37 -0700784}
785
Michal Hockoe5e68932017-09-06 16:19:37 -0700786struct zone * zone_for_pfn_range(int online_type, int nid, unsigned start_pfn,
787 unsigned long nr_pages)
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700788{
Michal Hockoc6f03e22017-09-06 16:19:40 -0700789 if (online_type == MMOP_ONLINE_KERNEL)
790 return default_kernel_zone_for_pfn(nid, start_pfn, nr_pages);
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700791
Michal Hockoc6f03e22017-09-06 16:19:40 -0700792 if (online_type == MMOP_ONLINE_MOVABLE)
793 return &NODE_DATA(nid)->node_zones[ZONE_MOVABLE];
Reza Arbabdf429ac2016-07-26 15:22:23 -0700794
Michal Hockoc6f03e22017-09-06 16:19:40 -0700795 return default_zone_for_pfn(nid, start_pfn, nr_pages);
Michal Hockoe5e68932017-09-06 16:19:37 -0700796}
797
798/*
799 * Associates the given pfn range with the given node and the zone appropriate
800 * for the given online type.
801 */
802static struct zone * __meminit move_pfn_range(int online_type, int nid,
803 unsigned long start_pfn, unsigned long nr_pages)
804{
805 struct zone *zone;
806
807 zone = zone_for_pfn_range(online_type, nid, start_pfn, nr_pages);
Christoph Hellwiga99583e2017-12-29 08:53:57 +0100808 move_pfn_range_to_zone(zone, start_pfn, nr_pages, NULL);
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700809 return zone;
Reza Arbabdf429ac2016-07-26 15:22:23 -0700810}
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700811
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800812int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700813{
Cody P Schaferaa472282013-07-03 15:02:10 -0700814 unsigned long flags;
Dave Hansen3947be12005-10-29 18:16:54 -0700815 unsigned long onlined_pages = 0;
816 struct zone *zone;
Yasunori Goto68113782006-06-23 02:03:11 -0700817 int need_zonelists_rebuild = 0;
Yasunori Goto7b78d332007-10-21 16:41:36 -0700818 int nid;
819 int ret;
820 struct memory_notify arg;
Pavel Tatashind0dc12e2018-04-05 16:23:00 -0700821 struct memory_block *mem;
Dave Hansen3947be12005-10-29 18:16:54 -0700822
David Hildenbrand17523d72018-10-30 15:10:29 -0700823 mem_hotplug_begin();
824
Pavel Tatashind0dc12e2018-04-05 16:23:00 -0700825 /*
826 * We can't use pfn_to_nid() because nid might be stored in struct page
827 * which is not yet initialized. Instead, we find nid from memory block.
828 */
829 mem = find_memory_block(__pfn_to_section(pfn));
830 nid = mem->nid;
David Hildenbrand6a60fb62019-04-25 22:23:37 -0700831 put_device(&mem->dev);
Pavel Tatashind0dc12e2018-04-05 16:23:00 -0700832
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700833 /* associate pfn range with the zone */
834 zone = move_pfn_range(online_type, nid, pfn, nr_pages);
835
Yasunori Goto7b78d332007-10-21 16:41:36 -0700836 arg.start_pfn = pfn;
837 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -0800838 node_states_check_changes_online(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -0700839
Yasunori Goto7b78d332007-10-21 16:41:36 -0700840 ret = memory_notify(MEM_GOING_ONLINE, &arg);
841 ret = notifier_to_errno(ret);
Chen Yuconge33e33b2016-03-17 14:19:35 -0700842 if (ret)
843 goto failed_addition;
844
Dave Hansen3947be12005-10-29 18:16:54 -0700845 /*
Yasunori Goto68113782006-06-23 02:03:11 -0700846 * If this zone is not populated, then it is not in zonelist.
847 * This means the page allocator ignores this zone.
848 * So, zonelist must be updated after online.
849 */
Wen Congyang6dcd73d2012-12-11 16:01:01 -0800850 if (!populated_zone(zone)) {
Yasunori Goto68113782006-06-23 02:03:11 -0700851 need_zonelists_rebuild = 1;
Michal Hocko72675e12017-09-06 16:20:24 -0700852 setup_zone_pageset(zone);
Wen Congyang6dcd73d2012-12-11 16:01:01 -0800853 }
Yasunori Goto68113782006-06-23 02:03:11 -0700854
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700855 ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700856 online_pages_range);
Geoff Levandfd8a4222008-05-14 16:05:50 -0700857 if (ret) {
Wen Congyang6dcd73d2012-12-11 16:01:01 -0800858 if (need_zonelists_rebuild)
859 zone_pcp_reset(zone);
Chen Yuconge33e33b2016-03-17 14:19:35 -0700860 goto failed_addition;
Geoff Levandfd8a4222008-05-14 16:05:50 -0700861 }
862
Dave Hansen3947be12005-10-29 18:16:54 -0700863 zone->present_pages += onlined_pages;
Cody P Schaferaa472282013-07-03 15:02:10 -0700864
865 pgdat_resize_lock(zone->zone_pgdat, &flags);
Yasunori Gotof2937be2006-03-09 17:33:51 -0800866 zone->zone_pgdat->node_present_pages += onlined_pages;
Cody P Schaferaa472282013-07-03 15:02:10 -0700867 pgdat_resize_unlock(zone->zone_pgdat, &flags);
868
Jiang Liu08dff7b2012-07-31 16:43:30 -0700869 if (onlined_pages) {
Vlastimil Babkae888ca32016-03-17 14:18:12 -0700870 node_states_set_node(nid, &arg);
Jiang Liu08dff7b2012-07-31 16:43:30 -0700871 if (need_zonelists_rebuild)
Michal Hocko72675e12017-09-06 16:20:24 -0700872 build_all_zonelists(NULL);
Jiang Liu08dff7b2012-07-31 16:43:30 -0700873 else
874 zone_pcp_update(zone);
875 }
Dave Hansen3947be12005-10-29 18:16:54 -0700876
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -0700877 init_per_zone_wmark_min();
878
Vlastimil Babka698b1b32016-03-17 14:18:08 -0700879 if (onlined_pages) {
Vlastimil Babkae888ca32016-03-17 14:18:12 -0700880 kswapd_run(nid);
Vlastimil Babka698b1b32016-03-17 14:18:08 -0700881 kcompactd_run(nid);
882 }
Dave Hansen61b13992005-10-29 18:16:56 -0700883
Haicheng Li1f522502010-05-24 14:32:51 -0700884 vm_total_pages = nr_free_pagecache_pages();
Kent Liu2f7f24e2008-07-23 21:28:18 -0700885
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -0700886 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -0700887
888 if (onlined_pages)
889 memory_notify(MEM_ONLINE, &arg);
David Hildenbrand17523d72018-10-30 15:10:29 -0700890 mem_hotplug_done();
David Rientjes30467e02015-04-14 15:45:11 -0700891 return 0;
Chen Yuconge33e33b2016-03-17 14:19:35 -0700892
893failed_addition:
894 pr_debug("online_pages [mem %#010llx-%#010llx] failed\n",
895 (unsigned long long) pfn << PAGE_SHIFT,
896 (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1);
897 memory_notify(MEM_CANCEL_ONLINE, &arg);
David Hildenbrand86834892020-01-28 10:50:21 +0100898 remove_pfn_range_from_zone(zone, pfn, nr_pages);
David Hildenbrand17523d72018-10-30 15:10:29 -0700899 mem_hotplug_done();
Chen Yuconge33e33b2016-03-17 14:19:35 -0700900 return ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700901}
Keith Mannthey53947022006-09-30 23:27:08 -0700902#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
Yasunori Gotobc02af92006-06-27 02:53:30 -0700903
Tang Chen0bd85422014-11-13 15:19:41 -0800904static void reset_node_present_pages(pg_data_t *pgdat)
905{
906 struct zone *z;
907
908 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
909 z->present_pages = 0;
910
911 pgdat->node_present_pages = 0;
912}
913
Hidetoshi Setoe1319332009-11-17 14:06:18 -0800914/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
915static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700916{
917 struct pglist_data *pgdat;
Fabian Frederickc8e861a2014-06-04 16:07:51 -0700918 unsigned long start_pfn = PFN_DOWN(start);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700919
Tang Chena1e565a2013-02-22 16:33:18 -0800920 pgdat = NODE_DATA(nid);
921 if (!pgdat) {
922 pgdat = arch_alloc_nodedata(nid);
923 if (!pgdat)
924 return NULL;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700925
Tang Chena1e565a2013-02-22 16:33:18 -0800926 arch_refresh_nodedata(nid, pgdat);
Gu Zhengb0dc3a32015-03-25 15:55:20 -0700927 } else {
Mel Gormane716f2e2017-05-03 14:53:45 -0700928 /*
929 * Reset the nr_zones, order and classzone_idx before reuse.
930 * Note that kswapd will init kswapd_classzone_idx properly
931 * when it starts in the near future.
932 */
Gu Zhengb0dc3a32015-03-25 15:55:20 -0700933 pgdat->nr_zones = 0;
Mel Gorman38087d92016-07-28 15:45:49 -0700934 pgdat->kswapd_order = 0;
935 pgdat->kswapd_classzone_idx = 0;
Tang Chena1e565a2013-02-22 16:33:18 -0800936 }
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700937
938 /* we can use NODE_DATA(nid) from here */
939
Oscar Salvador03e85f92018-08-21 21:53:43 -0700940 pgdat->node_id = nid;
941 pgdat->node_start_pfn = start_pfn;
942
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700943 /* init node's zones as empty zones, we don't have any present pages.*/
Oscar Salvador03e85f92018-08-21 21:53:43 -0700944 free_area_init_core_hotplug(nid);
Reza Arbab58301692016-08-11 15:33:12 -0700945 pgdat->per_cpu_nodestats = alloc_percpu(struct per_cpu_nodestat);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700946
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -0700947 /*
948 * The node we allocated has no zone fallback lists. For avoiding
949 * to access not-initialized zonelist, build here.
950 */
Michal Hocko72675e12017-09-06 16:20:24 -0700951 build_all_zonelists(pgdat);
KAMEZAWA Hiroyuki959ecc42011-06-15 15:08:38 -0700952
Tang Chenf784a3f2014-11-13 15:19:39 -0800953 /*
Tang Chen0bd85422014-11-13 15:19:41 -0800954 * When memory is hot-added, all the memory is in offline state. So
955 * clear all zones' present_pages because they will be updated in
956 * online_pages() and offline_pages().
957 */
Oscar Salvador03e85f92018-08-21 21:53:43 -0700958 reset_node_managed_pages(pgdat);
Tang Chen0bd85422014-11-13 15:19:41 -0800959 reset_node_present_pages(pgdat);
960
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700961 return pgdat;
962}
963
Oscar Salvadorb9ff0362018-08-17 15:46:15 -0700964static void rollback_node_hotadd(int nid)
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700965{
Oscar Salvadorb9ff0362018-08-17 15:46:15 -0700966 pg_data_t *pgdat = NODE_DATA(nid);
967
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700968 arch_refresh_nodedata(nid, NULL);
Reza Arbab58301692016-08-11 15:33:12 -0700969 free_percpu(pgdat->per_cpu_nodestats);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -0700970 arch_free_nodedata(pgdat);
971 return;
972}
973
KAMEZAWA Hiroyuki0a547032006-06-27 02:53:35 -0700974
Toshi Kani01b0f192013-11-12 15:07:25 -0800975/**
976 * try_online_node - online a node if offlined
Mike Rapoporte8b098f2018-04-05 16:24:57 -0700977 * @nid: the node ID
Oscar Salvadorb9ff0362018-08-17 15:46:15 -0700978 * @start: start addr of the node
979 * @set_node_online: Whether we want to online the node
minskey guocf234222010-05-24 14:32:41 -0700980 * called by cpu_up() to online a node without onlined memory.
Oscar Salvadorb9ff0362018-08-17 15:46:15 -0700981 *
982 * Returns:
983 * 1 -> a new node has been allocated
984 * 0 -> the node is already online
985 * -ENOMEM -> the node could not be allocated
minskey guocf234222010-05-24 14:32:41 -0700986 */
Oscar Salvadorb9ff0362018-08-17 15:46:15 -0700987static int __try_online_node(int nid, u64 start, bool set_node_online)
minskey guocf234222010-05-24 14:32:41 -0700988{
Oscar Salvadorb9ff0362018-08-17 15:46:15 -0700989 pg_data_t *pgdat;
990 int ret = 1;
minskey guocf234222010-05-24 14:32:41 -0700991
Toshi Kani01b0f192013-11-12 15:07:25 -0800992 if (node_online(nid))
993 return 0;
994
Oscar Salvadorb9ff0362018-08-17 15:46:15 -0700995 pgdat = hotadd_new_pgdat(nid, start);
David Rientjes7553e8f2011-06-22 18:13:01 -0700996 if (!pgdat) {
Toshi Kani01b0f192013-11-12 15:07:25 -0800997 pr_err("Cannot online node %d due to NULL pgdat\n", nid);
minskey guocf234222010-05-24 14:32:41 -0700998 ret = -ENOMEM;
999 goto out;
1000 }
Oscar Salvadorb9ff0362018-08-17 15:46:15 -07001001
1002 if (set_node_online) {
1003 node_set_online(nid);
1004 ret = register_one_node(nid);
1005 BUG_ON(ret);
1006 }
minskey guocf234222010-05-24 14:32:41 -07001007out:
Oscar Salvadorb9ff0362018-08-17 15:46:15 -07001008 return ret;
1009}
1010
1011/*
1012 * Users of this function always want to online/register the node
1013 */
1014int try_online_node(int nid)
1015{
1016 int ret;
1017
1018 mem_hotplug_begin();
1019 ret = __try_online_node(nid, 0, true);
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001020 mem_hotplug_done();
minskey guocf234222010-05-24 14:32:41 -07001021 return ret;
1022}
1023
Toshi Kani27356f52013-09-11 14:21:49 -07001024static int check_hotplug_memory_range(u64 start, u64 size)
1025{
Pavel Tatashinba325582018-04-05 16:22:39 -07001026 unsigned long block_sz = memory_block_size_bytes();
1027 u64 block_nr_pages = block_sz >> PAGE_SHIFT;
Toshi Kani27356f52013-09-11 14:21:49 -07001028 u64 nr_pages = size >> PAGE_SHIFT;
Pavel Tatashinba325582018-04-05 16:22:39 -07001029 u64 start_pfn = PFN_DOWN(start);
Toshi Kani27356f52013-09-11 14:21:49 -07001030
Pavel Tatashinba325582018-04-05 16:22:39 -07001031 /* memory range must be block size aligned */
1032 if (!nr_pages || !IS_ALIGNED(start_pfn, block_nr_pages) ||
1033 !IS_ALIGNED(nr_pages, block_nr_pages)) {
1034 pr_err("Block size [%#lx] unaligned hotplug range: start %#llx, size %#llx",
1035 block_sz, start, size);
Toshi Kani27356f52013-09-11 14:21:49 -07001036 return -EINVAL;
1037 }
1038
1039 return 0;
1040}
1041
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001042static int online_memory_block(struct memory_block *mem, void *arg)
1043{
Nathan Fontenotdc18d702017-02-24 15:00:02 -08001044 return device_online(&mem->dev);
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001045}
1046
David Hildenbrand02735d52018-10-30 15:10:24 -07001047/*
1048 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1049 * and online/offline operations (triggered e.g. by sysfs).
1050 *
1051 * we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG
1052 */
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001053int __ref add_memory_resource(int nid, struct resource *res, bool online)
Yasunori Gotobc02af92006-06-27 02:53:30 -07001054{
David Vrabel62cedb92015-06-25 16:35:49 +01001055 u64 start, size;
Oscar Salvadorb9ff0362018-08-17 15:46:15 -07001056 bool new_node = false;
Yasunori Gotobc02af92006-06-27 02:53:30 -07001057 int ret;
1058
David Vrabel62cedb92015-06-25 16:35:49 +01001059 start = res->start;
1060 size = resource_size(res);
1061
Toshi Kani27356f52013-09-11 14:21:49 -07001062 ret = check_hotplug_memory_range(start, size);
1063 if (ret)
1064 return ret;
1065
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001066 mem_hotplug_begin();
Nathan Zimmerac13c462014-01-23 15:53:26 -08001067
Tang Chen7f36e3e2015-09-04 15:42:32 -07001068 /*
1069 * Add new range to memblock so that when hotadd_new_pgdat() is called
1070 * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
1071 * this new range and calculate total pages correctly. The range will
1072 * be removed at hot-remove time.
1073 */
1074 memblock_add_node(start, size, nid);
1075
Oscar Salvadorb9ff0362018-08-17 15:46:15 -07001076 ret = __try_online_node(nid, start, false);
1077 if (ret < 0)
1078 goto error;
1079 new_node = ret;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001080
Yasunori Gotobc02af92006-06-27 02:53:30 -07001081 /* call arch's memory hotadd */
Christoph Hellwig24e6d5a2017-12-29 08:53:53 +01001082 ret = arch_add_memory(nid, start, size, NULL, true);
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001083 if (ret < 0)
1084 goto error;
1085
David Hildenbrandaa49b6ab2020-01-28 10:50:13 +01001086 /* create memory block devices after memory was added */
1087 ret = create_memory_block_devices(start, size);
1088 if (ret) {
1089 arch_remove_memory(nid, start, size, NULL);
1090 goto error;
1091 }
1092
Tang Chena1e565a2013-02-22 16:33:18 -08001093 if (new_node) {
Oscar Salvadord5b6f6a32018-08-17 15:46:18 -07001094 /* If sysfs file of new node can't be created, cpu on the node
Yasunori Goto0fc44152006-06-27 02:53:38 -07001095 * can't be hot-added. There is no rollback way now.
1096 * So, check by BUG_ON() to catch it reluctantly..
Oscar Salvadord5b6f6a32018-08-17 15:46:18 -07001097 * We online node here. We can't roll back from here.
Yasunori Goto0fc44152006-06-27 02:53:38 -07001098 */
Oscar Salvadord5b6f6a32018-08-17 15:46:18 -07001099 node_set_online(nid);
1100 ret = __register_one_node(nid);
Yasunori Goto0fc44152006-06-27 02:53:38 -07001101 BUG_ON(ret);
1102 }
1103
Oscar Salvadord5b6f6a32018-08-17 15:46:18 -07001104 /* link memory sections under this node.*/
Laurent Dufourb6f69f72020-09-25 21:19:31 -07001105 ret = link_mem_sections(nid, PFN_DOWN(start), PFN_UP(start + size - 1),
1106 MEMINIT_HOTPLUG);
Oscar Salvadord5b6f6a32018-08-17 15:46:18 -07001107 BUG_ON(ret);
1108
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -08001109 /* create new memmap entry */
1110 firmware_map_add_hotplug(start, start + size, "System RAM");
1111
David Hildenbrand17523d72018-10-30 15:10:29 -07001112 /* device_online() will take the lock when calling online_pages() */
1113 mem_hotplug_done();
1114
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001115 /* online pages if requested */
1116 if (online)
1117 walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1),
1118 NULL, online_memory_block);
1119
David Hildenbrand17523d72018-10-30 15:10:29 -07001120 return ret;
Yasunori Goto9af3c2d2006-06-27 02:53:34 -07001121error:
1122 /* rollback pgdat allocation and others */
Oscar Salvadorb9ff0362018-08-17 15:46:15 -07001123 if (new_node)
1124 rollback_node_hotadd(nid);
Tang Chen7f36e3e2015-09-04 15:42:32 -07001125 memblock_remove(start, size);
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001126 mem_hotplug_done();
Yasunori Gotobc02af92006-06-27 02:53:30 -07001127 return ret;
1128}
David Vrabel62cedb92015-06-25 16:35:49 +01001129
David Hildenbrand02735d52018-10-30 15:10:24 -07001130/* requires device_hotplug_lock, see add_memory_resource() */
1131int __ref __add_memory(int nid, u64 start, u64 size)
David Vrabel62cedb92015-06-25 16:35:49 +01001132{
1133 struct resource *res;
1134 int ret;
1135
1136 res = register_memory_resource(start, size);
Vitaly Kuznetsov6f754ba2016-01-14 15:21:55 -08001137 if (IS_ERR(res))
1138 return PTR_ERR(res);
David Vrabel62cedb92015-06-25 16:35:49 +01001139
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -07001140 ret = add_memory_resource(nid, res, memhp_auto_online);
David Vrabel62cedb92015-06-25 16:35:49 +01001141 if (ret < 0)
1142 release_memory_resource(res);
1143 return ret;
1144}
David Hildenbrand02735d52018-10-30 15:10:24 -07001145
1146int add_memory(int nid, u64 start, u64 size)
1147{
1148 int rc;
1149
1150 lock_device_hotplug();
1151 rc = __add_memory(nid, start, size);
1152 unlock_device_hotplug();
1153
1154 return rc;
1155}
Yasunori Gotobc02af92006-06-27 02:53:30 -07001156EXPORT_SYMBOL_GPL(add_memory);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001157
1158#ifdef CONFIG_MEMORY_HOTREMOVE
1159/*
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001160 * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
1161 * set and the size of the free page is given by page_order(). Using this,
1162 * the function determines if the pageblock contains only free pages.
1163 * Due to buddy contraints, a free page at least the size of a pageblock will
1164 * be located at the start of the pageblock
1165 */
1166static inline int pageblock_free(struct page *page)
1167{
1168 return PageBuddy(page) && page_order(page) >= pageblock_order;
1169}
1170
Michal Hocko2cc84e22019-02-20 22:20:46 -08001171/* Return the pfn of the start of the next active pageblock after a given pfn */
1172static unsigned long next_active_pageblock(unsigned long pfn)
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001173{
Michal Hocko2cc84e22019-02-20 22:20:46 -08001174 struct page *page = pfn_to_page(pfn);
1175
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001176 /* Ensure the starting page is pageblock-aligned */
Michal Hocko2cc84e22019-02-20 22:20:46 -08001177 BUG_ON(pfn & (pageblock_nr_pages - 1));
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001178
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001179 /* If the entire pageblock is free, move to the end of free page */
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -07001180 if (pageblock_free(page)) {
1181 int order;
1182 /* be careful. we don't have locks, page_order can be changed.*/
1183 order = page_order(page);
1184 if ((order < MAX_ORDER) && (order >= pageblock_order))
Michal Hocko2cc84e22019-02-20 22:20:46 -08001185 return pfn + (1 << order);
KAMEZAWA Hiroyuki0dcc48c2010-09-09 16:38:01 -07001186 }
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001187
Michal Hocko2cc84e22019-02-20 22:20:46 -08001188 return pfn + pageblock_nr_pages;
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001189}
1190
Michal Hocko2cc84e22019-02-20 22:20:46 -08001191static bool is_pageblock_removable_nolock(unsigned long pfn)
Mathieu Malaterrefb52bba2018-06-07 17:07:43 -07001192{
Michal Hocko2cc84e22019-02-20 22:20:46 -08001193 struct page *page = pfn_to_page(pfn);
Mathieu Malaterrefb52bba2018-06-07 17:07:43 -07001194 struct zone *zone;
Mathieu Malaterrefb52bba2018-06-07 17:07:43 -07001195
1196 /*
1197 * We have to be careful here because we are iterating over memory
1198 * sections which are not zone aware so we might end up outside of
1199 * the zone but still within the section.
1200 * We have to take care about the node as well. If the node is offline
1201 * its NODE_DATA will be NULL - see page_zone.
1202 */
1203 if (!node_online(page_to_nid(page)))
1204 return false;
1205
1206 zone = page_zone(page);
1207 pfn = page_to_pfn(page);
1208 if (!zone_spans_pfn(zone, pfn))
1209 return false;
1210
1211 return !has_unmovable_pages(zone, page, 0, MIGRATE_MOVABLE, true);
1212}
1213
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001214/* Checks if this range of memory is likely to be hot-removable. */
Yaowei Baic98940f2016-05-19 17:11:26 -07001215bool is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001216{
Michal Hocko2cc84e22019-02-20 22:20:46 -08001217 unsigned long end_pfn, pfn;
1218
1219 end_pfn = min(start_pfn + nr_pages,
1220 zone_end_pfn(page_zone(pfn_to_page(start_pfn))));
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001221
1222 /* Check the starting page of each pageblock within the range */
Michal Hocko2cc84e22019-02-20 22:20:46 -08001223 for (pfn = start_pfn; pfn < end_pfn; pfn = next_active_pageblock(pfn)) {
1224 if (!is_pageblock_removable_nolock(pfn))
Yaowei Baic98940f2016-05-19 17:11:26 -07001225 return false;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07001226 cond_resched();
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001227 }
1228
1229 /* All pageblocks in the memory block are likely to be hot-removable */
Yaowei Baic98940f2016-05-19 17:11:26 -07001230 return true;
Badari Pulavarty5c755e92008-07-23 21:28:19 -07001231}
1232
1233/*
Toshi Kanideb88a22017-02-03 13:13:20 -08001234 * Confirm all pages in a range [start, end) belong to the same zone.
Toshi Kania96dfdd2017-02-03 13:13:23 -08001235 * When true, return its valid [start, end).
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001236 */
Toshi Kania96dfdd2017-02-03 13:13:23 -08001237int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn,
1238 unsigned long *valid_start, unsigned long *valid_end)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001239{
Andrew Banman5f0f2882015-12-29 14:54:25 -08001240 unsigned long pfn, sec_end_pfn;
Toshi Kania96dfdd2017-02-03 13:13:23 -08001241 unsigned long start, end;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001242 struct zone *zone = NULL;
1243 struct page *page;
1244 int i;
Toshi Kanideb88a22017-02-03 13:13:20 -08001245 for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn + 1);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001246 pfn < end_pfn;
Toshi Kanideb88a22017-02-03 13:13:20 -08001247 pfn = sec_end_pfn, sec_end_pfn += PAGES_PER_SECTION) {
Andrew Banman5f0f2882015-12-29 14:54:25 -08001248 /* Make sure the memory section is present first */
1249 if (!present_section_nr(pfn_to_section_nr(pfn)))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001250 continue;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001251 for (; pfn < sec_end_pfn && pfn < end_pfn;
1252 pfn += MAX_ORDER_NR_PAGES) {
1253 i = 0;
1254 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
1255 while ((i < MAX_ORDER_NR_PAGES) &&
1256 !pfn_valid_within(pfn + i))
1257 i++;
zhong jiangd6d8c8a2017-02-24 14:59:30 -08001258 if (i == MAX_ORDER_NR_PAGES || pfn + i >= end_pfn)
Andrew Banman5f0f2882015-12-29 14:54:25 -08001259 continue;
Mikhail Zaslonko71df1c82019-02-01 14:20:38 -08001260 /* Check if we got outside of the zone */
1261 if (zone && !zone_spans_pfn(zone, pfn + i))
1262 return 0;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001263 page = pfn_to_page(pfn + i);
1264 if (zone && page_zone(page) != zone)
1265 return 0;
Toshi Kania96dfdd2017-02-03 13:13:23 -08001266 if (!zone)
1267 start = pfn + i;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001268 zone = page_zone(page);
Toshi Kania96dfdd2017-02-03 13:13:23 -08001269 end = pfn + MAX_ORDER_NR_PAGES;
Andrew Banman5f0f2882015-12-29 14:54:25 -08001270 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001271 }
Toshi Kanideb88a22017-02-03 13:13:20 -08001272
Toshi Kania96dfdd2017-02-03 13:13:23 -08001273 if (zone) {
1274 *valid_start = start;
zhong jiangd6d8c8a2017-02-24 14:59:30 -08001275 *valid_end = min(end, end_pfn);
Toshi Kanideb88a22017-02-03 13:13:20 -08001276 return 1;
Toshi Kania96dfdd2017-02-03 13:13:23 -08001277 } else {
Toshi Kanideb88a22017-02-03 13:13:20 -08001278 return 0;
Toshi Kania96dfdd2017-02-03 13:13:23 -08001279 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001280}
1281
1282/*
Yisheng Xie0efadf42017-02-24 14:57:39 -08001283 * Scan pfn range [start,end) to find movable/migratable pages (LRU pages,
1284 * non-lru movable pages and hugepages). We scan pfn because it's much
1285 * easier than scanning over linked list. This function returns the pfn
1286 * of the first found movable page if it's found, otherwise 0.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001287 */
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001288static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001289{
1290 unsigned long pfn;
Oscar Salvadord9f4d882019-02-01 14:20:47 -08001291
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001292 for (pfn = start; pfn < end; pfn++) {
Oscar Salvadord9f4d882019-02-01 14:20:47 -08001293 struct page *page, *head;
1294 unsigned long skip;
1295
1296 if (!pfn_valid(pfn))
1297 continue;
1298 page = pfn_to_page(pfn);
1299 if (PageLRU(page))
1300 return pfn;
1301 if (__PageMovable(page))
1302 return pfn;
1303
1304 if (!PageHuge(page))
1305 continue;
1306 head = compound_head(page);
1307 if (hugepage_migration_supported(page_hstate(head)) &&
1308 page_huge_active(head))
1309 return pfn;
1310 skip = (1 << compound_order(head)) - (page - head);
1311 pfn += skip - 1;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001312 }
1313 return 0;
1314}
1315
Michal Hocko666feb22018-04-10 16:30:03 -07001316static struct page *new_node_page(struct page *page, unsigned long private)
Xishi Qiu394e31d2016-07-28 15:48:53 -07001317{
Xishi Qiu394e31d2016-07-28 15:48:53 -07001318 int nid = page_to_nid(page);
Li Zhong231e97e2016-09-28 15:22:38 -07001319 nodemask_t nmask = node_states[N_MEMORY];
Michal Hocko7f252f22017-07-10 15:48:41 -07001320
1321 /*
1322 * try to allocate from a different node but reuse this node if there
1323 * are no other online nodes to be used (e.g. we are offlining a part
1324 * of the only existing node)
1325 */
1326 node_clear(nid, nmask);
1327 if (nodes_empty(nmask))
1328 node_set(nid, nmask);
Xishi Qiu394e31d2016-07-28 15:48:53 -07001329
Michal Hocko8b913232017-07-10 15:48:47 -07001330 return new_page_nodemask(page, nid, &nmask);
Xishi Qiu394e31d2016-07-28 15:48:53 -07001331}
1332
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001333#define NR_OFFLINE_AT_ONCE_PAGES (256)
1334static int
1335do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
1336{
1337 unsigned long pfn;
1338 struct page *page;
1339 int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
1340 int not_managed = 0;
1341 int ret = 0;
1342 LIST_HEAD(source);
1343
1344 for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
1345 if (!pfn_valid(pfn))
1346 continue;
1347 page = pfn_to_page(pfn);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001348
1349 if (PageHuge(page)) {
1350 struct page *head = compound_head(page);
1351 pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
1352 if (compound_order(head) > PFN_SECTION_SHIFT) {
1353 ret = -EBUSY;
1354 break;
1355 }
1356 if (isolate_huge_page(page, &source))
1357 move_pages -= 1 << compound_order(head);
1358 continue;
Michal Hocko94723aa2018-04-10 16:30:07 -07001359 } else if (PageTransHuge(page))
Naoya Horiguchi8135d892017-09-08 16:11:15 -07001360 pfn = page_to_pfn(compound_head(page))
1361 + hpage_nr_pages(page) - 1;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001362
Michal Hocko2c870722018-12-28 00:38:01 -08001363 /*
1364 * HWPoison pages have elevated reference counts so the migration would
1365 * fail on them. It also doesn't make any sense to migrate them in the
1366 * first place. Still try to unmap such a page in case it is still mapped
1367 * (e.g. current hwpoison implementation doesn't unmap KSM pages but keep
1368 * the unmap as the catch all safety net).
1369 */
1370 if (PageHWPoison(page)) {
1371 if (WARN_ON(PageLRU(page)))
1372 isolate_lru_page(page);
1373 if (page_mapped(page))
1374 try_to_unmap(page, TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS);
1375 continue;
1376 }
1377
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001378 if (!get_page_unless_zero(page))
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001379 continue;
1380 /*
Yisheng Xie0efadf42017-02-24 14:57:39 -08001381 * We can skip free pages. And we can deal with pages on
1382 * LRU and non-lru movable pages.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001383 */
Yisheng Xie0efadf42017-02-24 14:57:39 -08001384 if (PageLRU(page))
1385 ret = isolate_lru_page(page);
1386 else
1387 ret = isolate_movable_page(page, ISOLATE_UNEVICTABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001388 if (!ret) { /* Success */
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001389 put_page(page);
Nick Piggin62695a82008-10-18 20:26:09 -07001390 list_add_tail(&page->lru, &source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001391 move_pages--;
Yisheng Xie0efadf42017-02-24 14:57:39 -08001392 if (!__PageMovable(page))
1393 inc_node_page_state(page, NR_ISOLATED_ANON +
1394 page_is_file_cache(page));
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001395
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001396 } else {
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001397#ifdef CONFIG_DEBUG_VM
Yisheng Xie0efadf42017-02-24 14:57:39 -08001398 pr_alert("failed to isolate pfn %lx\n", pfn);
1399 dump_page(page, "isolation failed");
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001400#endif
Konstantin Khlebnikov700c2a42011-05-24 17:12:19 -07001401 put_page(page);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001402 /* Because we don't have big zone->lock. we should
Bob Liu809c4442010-10-26 14:22:10 -07001403 check this again here. */
1404 if (page_count(page)) {
1405 not_managed++;
Bob Liuf3ab2632010-10-26 14:22:10 -07001406 ret = -EBUSY;
Bob Liu809c4442010-10-26 14:22:10 -07001407 break;
1408 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001409 }
1410 }
Bob Liuf3ab2632010-10-26 14:22:10 -07001411 if (!list_empty(&source)) {
1412 if (not_managed) {
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001413 putback_movable_pages(&source);
Bob Liuf3ab2632010-10-26 14:22:10 -07001414 goto out;
1415 }
Minchan Kim74c08f92012-10-08 16:32:54 -07001416
Xishi Qiu394e31d2016-07-28 15:48:53 -07001417 /* Allocate a new page from the nearest neighbor node */
1418 ret = migrate_pages(&source, new_node_page, NULL, 0,
Hugh Dickins9c620e22013-02-22 16:35:14 -08001419 MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
Bob Liuf3ab2632010-10-26 14:22:10 -07001420 if (ret)
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001421 putback_movable_pages(&source);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001422 }
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001423out:
1424 return ret;
1425}
1426
1427/*
1428 * remove from free_area[] and mark all as Reserved.
1429 */
1430static int
1431offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1432 void *data)
1433{
1434 __offline_isolated_pages(start, start + nr_pages);
1435 return 0;
1436}
1437
1438static void
1439offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1440{
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001441 walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001442 offline_isolated_pages_cb);
1443}
1444
1445/*
1446 * Check all pages in range, recoreded as memory resource, are isolated.
1447 */
1448static int
1449check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1450 void *data)
1451{
1452 int ret;
1453 long offlined = *(long *)data;
Wen Congyangb023f462012-12-11 16:00:45 -08001454 ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001455 offlined = nr_pages;
1456 if (!ret)
1457 *(long *)data += offlined;
1458 return ret;
1459}
1460
1461static long
1462check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1463{
1464 long offlined = 0;
1465 int ret;
1466
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -07001467 ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001468 check_pages_isolated_cb);
1469 if (ret < 0)
1470 offlined = (long)ret;
1471 return offlined;
1472}
1473
Tang Chenc5320922013-11-12 15:08:10 -08001474static int __init cmdline_parse_movable_node(char *p)
1475{
Michal Hocko49323812017-07-06 15:41:05 -07001476#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Tang Chen55ac5902014-01-21 15:49:35 -08001477 movable_node_enabled = true;
Michal Hocko49323812017-07-06 15:41:05 -07001478#else
1479 pr_warn("movable_node parameter depends on CONFIG_HAVE_MEMBLOCK_NODE_MAP to work properly\n");
1480#endif
Tang Chenc5320922013-11-12 15:08:10 -08001481 return 0;
1482}
1483early_param("movable_node", cmdline_parse_movable_node);
1484
Lai Jiangshand9713672012-12-11 16:01:03 -08001485/* check which state of node_states will be changed when offline memory */
1486static void node_states_check_changes_offline(unsigned long nr_pages,
1487 struct zone *zone, struct memory_notify *arg)
1488{
1489 struct pglist_data *pgdat = zone->zone_pgdat;
1490 unsigned long present_pages = 0;
1491 enum zone_type zt, zone_last = ZONE_NORMAL;
1492
1493 /*
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001494 * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
1495 * contains nodes which have zones of 0...ZONE_NORMAL,
1496 * set zone_last to ZONE_NORMAL.
Lai Jiangshand9713672012-12-11 16:01:03 -08001497 *
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001498 * If we don't have HIGHMEM nor movable node,
1499 * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
1500 * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
Lai Jiangshand9713672012-12-11 16:01:03 -08001501 */
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001502 if (N_MEMORY == N_NORMAL_MEMORY)
Lai Jiangshand9713672012-12-11 16:01:03 -08001503 zone_last = ZONE_MOVABLE;
1504
1505 /*
1506 * check whether node_states[N_NORMAL_MEMORY] will be changed.
1507 * If the memory to be offline is in a zone of 0...zone_last,
1508 * and it is the last present memory, 0...zone_last will
1509 * become empty after offline , thus we can determind we will
1510 * need to clear the node from node_states[N_NORMAL_MEMORY].
1511 */
1512 for (zt = 0; zt <= zone_last; zt++)
1513 present_pages += pgdat->node_zones[zt].present_pages;
1514 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1515 arg->status_change_nid_normal = zone_to_nid(zone);
1516 else
1517 arg->status_change_nid_normal = -1;
1518
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001519#ifdef CONFIG_HIGHMEM
1520 /*
1521 * If we have movable node, node_states[N_HIGH_MEMORY]
1522 * contains nodes which have zones of 0...ZONE_HIGHMEM,
1523 * set zone_last to ZONE_HIGHMEM.
1524 *
1525 * If we don't have movable node, node_states[N_NORMAL_MEMORY]
1526 * contains nodes which have zones of 0...ZONE_MOVABLE,
1527 * set zone_last to ZONE_MOVABLE.
1528 */
1529 zone_last = ZONE_HIGHMEM;
1530 if (N_MEMORY == N_HIGH_MEMORY)
1531 zone_last = ZONE_MOVABLE;
1532
1533 for (; zt <= zone_last; zt++)
1534 present_pages += pgdat->node_zones[zt].present_pages;
1535 if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1536 arg->status_change_nid_high = zone_to_nid(zone);
1537 else
1538 arg->status_change_nid_high = -1;
1539#else
1540 arg->status_change_nid_high = arg->status_change_nid_normal;
1541#endif
1542
Lai Jiangshand9713672012-12-11 16:01:03 -08001543 /*
1544 * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1545 */
1546 zone_last = ZONE_MOVABLE;
1547
1548 /*
1549 * check whether node_states[N_HIGH_MEMORY] will be changed
1550 * If we try to offline the last present @nr_pages from the node,
1551 * we can determind we will need to clear the node from
1552 * node_states[N_HIGH_MEMORY].
1553 */
1554 for (; zt <= zone_last; zt++)
1555 present_pages += pgdat->node_zones[zt].present_pages;
1556 if (nr_pages >= present_pages)
1557 arg->status_change_nid = zone_to_nid(zone);
1558 else
1559 arg->status_change_nid = -1;
1560}
1561
1562static void node_states_clear_node(int node, struct memory_notify *arg)
1563{
1564 if (arg->status_change_nid_normal >= 0)
1565 node_clear_state(node, N_NORMAL_MEMORY);
1566
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001567 if ((N_MEMORY != N_NORMAL_MEMORY) &&
1568 (arg->status_change_nid_high >= 0))
Lai Jiangshand9713672012-12-11 16:01:03 -08001569 node_clear_state(node, N_HIGH_MEMORY);
Lai Jiangshan6715ddf2012-12-12 13:51:49 -08001570
1571 if ((N_MEMORY != N_HIGH_MEMORY) &&
1572 (arg->status_change_nid >= 0))
1573 node_clear_state(node, N_MEMORY);
Lai Jiangshand9713672012-12-11 16:01:03 -08001574}
1575
Wen Congyanga16cee12012-10-08 16:33:58 -07001576static int __ref __offline_pages(unsigned long start_pfn,
Michal Hockoecde0f32017-11-15 17:33:38 -08001577 unsigned long end_pfn)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001578{
Michal Hockoecde0f32017-11-15 17:33:38 -08001579 unsigned long pfn, nr_pages;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001580 long offlined_pages;
Michal Hocko72b39cf2017-11-15 17:33:34 -08001581 int ret, node;
Cody P Schaferd7029092013-07-03 15:02:11 -07001582 unsigned long flags;
Toshi Kania96dfdd2017-02-03 13:13:23 -08001583 unsigned long valid_start, valid_end;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001584 struct zone *zone;
Yasunori Goto7b78d332007-10-21 16:41:36 -07001585 struct memory_notify arg;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001586
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001587 /* at least, alignment against pageblock is necessary */
1588 if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1589 return -EINVAL;
1590 if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1591 return -EINVAL;
David Hildenbrand17523d72018-10-30 15:10:29 -07001592
1593 mem_hotplug_begin();
1594
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001595 /* This makes hotplug much easier...and readable.
1596 we assume this for now. .*/
David Hildenbrand17523d72018-10-30 15:10:29 -07001597 if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start,
1598 &valid_end)) {
1599 mem_hotplug_done();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001600 return -EINVAL;
David Hildenbrand17523d72018-10-30 15:10:29 -07001601 }
Yasunori Goto7b78d332007-10-21 16:41:36 -07001602
Toshi Kania96dfdd2017-02-03 13:13:23 -08001603 zone = page_zone(pfn_to_page(valid_start));
Yasunori Goto7b78d332007-10-21 16:41:36 -07001604 node = zone_to_nid(zone);
1605 nr_pages = end_pfn - start_pfn;
1606
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001607 /* set above range as isolated */
Wen Congyangb023f462012-12-11 16:00:45 -08001608 ret = start_isolate_page_range(start_pfn, end_pfn,
1609 MIGRATE_MOVABLE, true);
David Hildenbrand17523d72018-10-30 15:10:29 -07001610 if (ret) {
1611 mem_hotplug_done();
David Rientjes30467e02015-04-14 15:45:11 -07001612 return ret;
David Hildenbrand17523d72018-10-30 15:10:29 -07001613 }
Yasunori Goto7b78d332007-10-21 16:41:36 -07001614
1615 arg.start_pfn = start_pfn;
1616 arg.nr_pages = nr_pages;
Lai Jiangshand9713672012-12-11 16:01:03 -08001617 node_states_check_changes_offline(nr_pages, zone, &arg);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001618
1619 ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1620 ret = notifier_to_errno(ret);
1621 if (ret)
1622 goto failed_removal;
1623
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001624 pfn = start_pfn;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001625repeat:
1626 /* start memory hot removal */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001627 ret = -EINTR;
1628 if (signal_pending(current))
1629 goto failed_removal;
Michal Hocko72b39cf2017-11-15 17:33:34 -08001630
1631 cond_resched();
Michal Hocko9852a722018-01-31 16:16:19 -08001632 lru_add_drain_all();
Michal Hocko72b39cf2017-11-15 17:33:34 -08001633 drain_all_pages(zone);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001634
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001635 pfn = scan_movable_pages(start_pfn, end_pfn);
1636 if (pfn) { /* We have movable pages */
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001637 ret = do_migrate_range(pfn, end_pfn);
Michal Hocko72b39cf2017-11-15 17:33:34 -08001638 goto repeat;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001639 }
Michal Hocko72b39cf2017-11-15 17:33:34 -08001640
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001641 /*
1642 * dissolve free hugepages in the memory block before doing offlining
1643 * actually in order to make hugetlbfs's object counting consistent.
1644 */
Gerald Schaefer082d5b62016-10-07 17:01:10 -07001645 ret = dissolve_free_huge_pages(start_pfn, end_pfn);
1646 if (ret)
1647 goto failed_removal;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001648 /* check again */
1649 offlined_pages = check_pages_isolated(start_pfn, end_pfn);
Michal Hocko72b39cf2017-11-15 17:33:34 -08001650 if (offlined_pages < 0)
1651 goto repeat;
Chen Yuconge33e33b2016-03-17 14:19:35 -07001652 pr_info("Offlined Pages %ld\n", offlined_pages);
Adam Buchbinderb3834be2012-09-19 21:48:02 -04001653 /* Ok, all of our target is isolated.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001654 We cannot do rollback at this point. */
1655 offline_isolated_pages(start_pfn, end_pfn);
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -08001656 /* reset pagetype flags and makes migrate type to be MOVABLE */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02001657 undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001658 /* removal success */
Jiang Liu3dcc0572013-07-03 15:03:21 -07001659 adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001660 zone->present_pages -= offlined_pages;
Cody P Schaferd7029092013-07-03 15:02:11 -07001661
1662 pgdat_resize_lock(zone->zone_pgdat, &flags);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001663 zone->zone_pgdat->node_present_pages -= offlined_pages;
Cody P Schaferd7029092013-07-03 15:02:11 -07001664 pgdat_resize_unlock(zone->zone_pgdat, &flags);
Yasunori Goto7b78d332007-10-21 16:41:36 -07001665
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07001666 init_per_zone_wmark_min();
1667
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001668 if (!populated_zone(zone)) {
Jiang Liu340175b2012-07-31 16:43:32 -07001669 zone_pcp_reset(zone);
Michal Hocko72675e12017-09-06 16:20:24 -07001670 build_all_zonelists(NULL);
Xishi Qiu1e8537b2012-10-08 16:31:51 -07001671 } else
1672 zone_pcp_update(zone);
Jiang Liu340175b2012-07-31 16:43:32 -07001673
Lai Jiangshand9713672012-12-11 16:01:03 -08001674 node_states_clear_node(node, &arg);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001675 if (arg.status_change_nid >= 0) {
David Rientjes8fe23e02009-12-14 17:58:33 -08001676 kswapd_stop(node);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001677 kcompactd_stop(node);
1678 }
Minchan Kimbce73942009-06-16 15:32:50 -07001679
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001680 vm_total_pages = nr_free_pagecache_pages();
1681 writeback_set_ratelimit();
Yasunori Goto7b78d332007-10-21 16:41:36 -07001682
1683 memory_notify(MEM_OFFLINE, &arg);
David Hildenbrand86834892020-01-28 10:50:21 +01001684 remove_pfn_range_from_zone(zone, start_pfn, nr_pages);
David Hildenbrand17523d72018-10-30 15:10:29 -07001685 mem_hotplug_done();
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);
David Hildenbrand17523d72018-10-30 15:10:29 -07001695 mem_hotplug_done();
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07001696 return ret;
1697}
Badari Pulavarty71088782008-10-18 20:25:58 -07001698
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
David Hildenbrandd98d0532020-01-28 10:50:20 +01001815static int check_no_memblock_for_node_cb(struct memory_block *mem, void *arg)
1816{
1817 int nid = *(int *)arg;
1818
1819 /*
1820 * If a memory block belongs to multiple nodes, the stored nid is not
1821 * reliable. However, such blocks are always online (e.g., cannot get
1822 * offlined) and, therefore, are still spanned by the node.
1823 */
1824 return mem->nid == nid ? -EEXIST : 0;
1825}
1826
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001827/**
1828 * try_offline_node
Mike Rapoporte8b098f2018-04-05 16:24:57 -07001829 * @nid: the node ID
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001830 *
1831 * Offline a node if all memory sections and cpus of the node are removed.
1832 *
1833 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1834 * and online/offline operations before this call.
1835 */
Wen Congyang90b30cd2013-02-22 16:33:27 -08001836void try_offline_node(int nid)
Tang Chen60a5a192013-02-22 16:33:14 -08001837{
Wen Congyangd822b862013-02-22 16:33:16 -08001838 pg_data_t *pgdat = NODE_DATA(nid);
David Hildenbrandd98d0532020-01-28 10:50:20 +01001839 int rc;
Tang Chen60a5a192013-02-22 16:33:14 -08001840
David Hildenbrandd98d0532020-01-28 10:50:20 +01001841 /*
1842 * If the node still spans pages (especially ZONE_DEVICE), don't
1843 * offline it. A node spans memory after move_pfn_range_to_zone(),
1844 * e.g., after the memory block was onlined.
1845 */
1846 if (pgdat->node_spanned_pages)
Tang Chen60a5a192013-02-22 16:33:14 -08001847 return;
David Hildenbrandd98d0532020-01-28 10:50:20 +01001848
1849 /*
1850 * Especially offline memory blocks might not be spanned by the
1851 * node. They will get spanned by the node once they get onlined.
1852 * However, they link to the node in sysfs and can get onlined later.
1853 */
1854 rc = for_each_memory_block(&nid, check_no_memblock_for_node_cb);
1855 if (rc)
1856 return;
Tang Chen60a5a192013-02-22 16:33:14 -08001857
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001858 if (check_and_unmap_cpu_on_node(pgdat))
Tang Chen60a5a192013-02-22 16:33:14 -08001859 return;
1860
1861 /*
1862 * all memory/cpu of this node are removed, we can offline this
1863 * node now.
1864 */
1865 node_set_offline(nid);
1866 unregister_one_node(nid);
1867}
Wen Congyang90b30cd2013-02-22 16:33:27 -08001868EXPORT_SYMBOL(try_offline_node);
Tang Chen60a5a192013-02-22 16:33:14 -08001869
David Hildenbrand2ad264f2020-01-28 10:50:03 +01001870static void __release_memory_resource(resource_size_t start,
1871 resource_size_t size)
1872{
1873 int ret;
1874
1875 /*
1876 * When removing memory in the same granularity as it was added,
1877 * this function never fails. It might only fail if resources
1878 * have to be adjusted or split. We'll ignore the error, as
1879 * removing of memory cannot fail.
1880 */
1881 ret = release_mem_region_adjustable(&iomem_resource, start, size);
1882 if (ret) {
1883 resource_size_t endres = start + size - 1;
1884
1885 pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
1886 &start, &endres, ret);
1887 }
1888}
1889
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001890/**
1891 * remove_memory
Mike Rapoporte8b098f2018-04-05 16:24:57 -07001892 * @nid: the node ID
1893 * @start: physical address of the region to remove
1894 * @size: size of the region to remove
Toshi Kani0f1cfe92013-09-11 14:21:50 -07001895 *
1896 * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
1897 * and online/offline operations before this call, as required by
1898 * try_offline_node().
1899 */
David Hildenbranda3cf10b2020-01-28 10:49:58 +01001900void __ref __remove_memory(int nid, u64 start, u64 size)
Wen Congyangbbc76be2013-02-22 16:32:54 -08001901{
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02001902 int ret;
Wen Congyang993c1aa2013-02-22 16:32:50 -08001903
Toshi Kani27356f52013-09-11 14:21:49 -07001904 BUG_ON(check_hotplug_memory_range(start, size));
1905
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08001906 /*
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02001907 * All memory blocks must be offlined before removing memory. Check
1908 * whether all memory blocks in question are offline and trigger a BUG()
1909 * if this is not the case.
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08001910 */
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02001911 ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
Xishi Qiud6de9d52013-11-12 15:07:20 -08001912 check_memblock_offlined_cb);
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001913 if (ret)
Rafael J. Wysocki242831e2013-05-27 12:58:46 +02001914 BUG();
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -08001915
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -08001916 /* remove memmap entry */
1917 firmware_map_remove(start, start + size, "System RAM");
Xishi Qiuf9126ab2015-08-14 15:35:16 -07001918 memblock_free(start, size);
1919 memblock_remove(start, size);
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -08001920
Dan Williams9a6873a2020-01-30 22:11:17 -08001921 /*
1922 * Memory block device removal under the device_hotplug_lock is
1923 * a barrier against racing online attempts.
1924 */
David Hildenbrandd883abb2020-01-28 10:50:14 +01001925 remove_memory_block_devices(start, size);
1926
Dan Williams9a6873a2020-01-30 22:11:17 -08001927 mem_hotplug_begin();
1928
Oscar Salvador5c1f8f52020-01-28 10:50:02 +01001929 arch_remove_memory(nid, start, size, NULL);
David Hildenbrand2ad264f2020-01-28 10:50:03 +01001930 __release_memory_resource(start, size);
Wen Congyang24d335c2013-02-22 16:32:58 -08001931
Tang Chen60a5a192013-02-22 16:33:14 -08001932 try_offline_node(nid);
1933
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001934 mem_hotplug_done();
Badari Pulavarty71088782008-10-18 20:25:58 -07001935}
David Hildenbranda3cf10b2020-01-28 10:49:58 +01001936
1937void remove_memory(int nid, u64 start, u64 size)
1938{
1939 lock_device_hotplug();
1940 __remove_memory(nid, start, size);
1941 unlock_device_hotplug();
1942}
Badari Pulavarty71088782008-10-18 20:25:58 -07001943EXPORT_SYMBOL_GPL(remove_memory);
Rafael J. Wysockiaba6efc2013-06-01 22:24:07 +02001944#endif /* CONFIG_MEMORY_HOTREMOVE */