blob: 4087340fca32f11e3b76b958c1e38d30635c52b5 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/mm/page_alloc.c
4 *
5 * Manages the free list, the system allocates free pages here.
6 * Note that kmalloc() lives in slab.c
7 *
8 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
9 * Swap reorganised 29.12.95, Stephen Tweedie
10 * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
11 * Reshaped it to be a zoned allocator, Ingo Molnar, Red Hat, 1999
12 * Discontiguous memory support, Kanoj Sarcar, SGI, Nov 1999
13 * Zone balancing, Kanoj Sarcar, SGI, Jan 2000
14 * Per cpu hot/cold page lists, bulk allocation, Martin J. Bligh, Sept 2002
15 * (lots of bits borrowed from Ingo Molnar & Andrew Morton)
16 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/stddef.h>
19#include <linux/mm.h>
Arun KSca79b0c2018-12-28 00:34:29 -080020#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/swap.h>
22#include <linux/interrupt.h>
23#include <linux/pagemap.h>
KOSAKI Motohiro10ed2732008-03-04 14:28:32 -080024#include <linux/jiffies.h>
Yinghai Luedbe7d22010-08-25 13:39:16 -070025#include <linux/memblock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/compiler.h>
Randy Dunlap9f158332005-09-13 01:25:16 -070027#include <linux/kernel.h>
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -080028#include <linux/kasan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/module.h>
30#include <linux/suspend.h>
31#include <linux/pagevec.h>
32#include <linux/blkdev.h>
33#include <linux/slab.h>
Dave Hansena238ab52011-05-24 17:12:16 -070034#include <linux/ratelimit.h>
David Rientjes5a3135c22007-10-16 23:25:53 -070035#include <linux/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/topology.h>
37#include <linux/sysctl.h>
38#include <linux/cpu.h>
39#include <linux/cpuset.h>
Dave Hansenbdc8cb92005-10-29 18:16:53 -070040#include <linux/memory_hotplug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/nodemask.h>
42#include <linux/vmalloc.h>
KOSAKI Motohiroa6cccdc2011-05-24 17:11:33 -070043#include <linux/vmstat.h>
Christoph Lameter4be38e32006-01-06 00:11:17 -080044#include <linux/mempolicy.h>
Dan Williams4b94ffd2016-01-15 16:56:22 -080045#include <linux/memremap.h>
Yasunori Goto68113782006-06-23 02:03:11 -070046#include <linux/stop_machine.h>
Dan Williams97500a42019-05-14 15:41:35 -070047#include <linux/random.h>
Mel Gormanc7132162006-09-27 01:49:43 -070048#include <linux/sort.h>
49#include <linux/pfn.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070050#include <linux/backing-dev.h>
Akinobu Mita933e3122006-12-08 02:39:45 -080051#include <linux/fault-inject.h>
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -070052#include <linux/page-isolation.h>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070053#include <linux/debugobjects.h>
Catalin Marinasdbb1f812009-06-11 13:23:19 +010054#include <linux/kmemleak.h>
Mel Gorman56de7262010-05-24 14:32:30 -070055#include <linux/compaction.h>
Mel Gorman0d3d0622009-09-21 17:02:44 -070056#include <trace/events/kmem.h>
Michal Hockod379f012017-02-22 15:42:00 -080057#include <trace/events/oom.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070058#include <linux/prefetch.h>
Lisa Du6e543d52013-09-11 14:22:36 -070059#include <linux/mm_inline.h>
Daniel Vetterf920e412020-12-14 19:08:30 -080060#include <linux/mmu_notifier.h>
Michal Nazarewicz041d3a82011-12-29 13:09:50 +010061#include <linux/migrate.h>
David Rientjes949f7ec2013-04-29 15:07:48 -070062#include <linux/hugetlb.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060063#include <linux/sched/rt.h>
Ingo Molnar5b3cc152017-02-02 20:43:54 +010064#include <linux/sched/mm.h>
Joonsoo Kim48c96a32014-12-12 16:56:01 -080065#include <linux/page_owner.h>
Mel Gorman0e1cc952015-06-30 14:57:27 -070066#include <linux/kthread.h>
Vladimir Davydov49491482016-07-26 15:24:24 -070067#include <linux/memcontrol.h>
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -050068#include <linux/ftrace.h>
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +010069#include <linux/lockdep.h>
Chen Yu556b9692017-08-25 15:55:30 -070070#include <linux/nmi.h>
Johannes Weinereb414682018-10-26 15:06:27 -070071#include <linux/psi.h>
Daniel Jordane4443142020-06-03 15:59:51 -070072#include <linux/padata.h>
Vijay Balakrishna4aab2be2020-10-10 23:16:40 -070073#include <linux/khugepaged.h>
Lin Fengba8f3582020-12-14 19:11:19 -080074#include <linux/buffer_head.h>
Jiang Liu7ee3d4e2013-07-03 15:03:41 -070075#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <asm/tlbflush.h>
Andrew Mortonac924c62006-05-15 09:43:59 -070077#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include "internal.h"
Dan Williamse900a912019-05-14 15:41:28 -070079#include "shuffle.h"
Alexander Duyck36e66c52020-04-06 20:04:56 -070080#include "page_reporting.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
David Hildenbrandf04a5d52020-10-15 20:09:20 -070082/* Free Page Internal flags: for internal, non-pcp variants of free_pages(). */
83typedef int __bitwise fpi_t;
84
85/* No special request */
86#define FPI_NONE ((__force fpi_t)0)
87
88/*
89 * Skip free page reporting notification for the (possibly merged) page.
90 * This does not hinder free page reporting from grabbing the page,
91 * reporting it and marking it "reported" - it only skips notifying
92 * the free page reporting infrastructure about a newly freed page. For
93 * example, used when temporarily pulling a page from a freelist and
94 * putting it back unmodified.
95 */
96#define FPI_SKIP_REPORT_NOTIFY ((__force fpi_t)BIT(0))
97
David Hildenbrand47b6a24a22020-10-15 20:09:26 -070098/*
99 * Place the (possibly merged) page to the tail of the freelist. Will ignore
100 * page shuffling (relevant code - e.g., memory onlining - is expected to
101 * shuffle the whole zone).
102 *
103 * Note: No code should rely on this flag for correctness - it's purely
104 * to allow for optimizations when handing back either fresh pages
105 * (memory onlining) or untouched pages (page isolation, free page
106 * reporting).
107 */
108#define FPI_TO_TAIL ((__force fpi_t)BIT(1))
109
Andrey Konovalov2c335682021-04-29 22:59:52 -0700110/*
111 * Don't poison memory with KASAN (only for the tag-based modes).
112 * During boot, all non-reserved memblock memory is exposed to page_alloc.
113 * Poisoning all that memory lengthens boot time, especially on systems with
114 * large amount of RAM. This flag is used to skip that poisoning.
115 * This is only done for the tag-based KASAN modes, as those are able to
116 * detect memory corruptions with the memory tags assigned by default.
117 * All memory allocated normally after boot gets poisoned as usual.
118 */
119#define FPI_SKIP_KASAN_POISON ((__force fpi_t)BIT(2))
120
Cody P Schaferc8e251f2013-07-03 15:01:29 -0700121/* prevent >1 _updater_ of zone percpu pageset ->high and ->batch fields */
122static DEFINE_MUTEX(pcp_batch_high_lock);
David Rientjes7cd2b0a2014-06-23 13:22:04 -0700123#define MIN_PERCPU_PAGELIST_FRACTION (8)
Cody P Schaferc8e251f2013-07-03 15:01:29 -0700124
Lee Schermerhorn72812012010-05-26 14:44:56 -0700125#ifdef CONFIG_USE_PERCPU_NUMA_NODE_ID
126DEFINE_PER_CPU(int, numa_node);
127EXPORT_PER_CPU_SYMBOL(numa_node);
128#endif
129
Kemi Wang45180852017-11-15 17:38:22 -0800130DEFINE_STATIC_KEY_TRUE(vm_numa_stat_key);
131
Lee Schermerhorn7aac7892010-05-26 14:45:00 -0700132#ifdef CONFIG_HAVE_MEMORYLESS_NODES
133/*
134 * N.B., Do NOT reference the '_numa_mem_' per cpu variable directly.
135 * It will not be defined when CONFIG_HAVE_MEMORYLESS_NODES is not defined.
136 * Use the accessor functions set_numa_mem(), numa_mem_id() and cpu_to_mem()
137 * defined in <linux/topology.h>.
138 */
139DEFINE_PER_CPU(int, _numa_mem_); /* Kernel "local memory" node */
140EXPORT_PER_CPU_SYMBOL(_numa_mem_);
141#endif
142
Mel Gormanbd233f52017-02-24 14:56:56 -0800143/* work_structs for global per-cpu drains */
Wei Yangd9367bd2018-12-28 00:38:58 -0800144struct pcpu_drain {
145 struct zone *zone;
146 struct work_struct work;
147};
Jason Yan8b885f52020-04-10 14:32:32 -0700148static DEFINE_MUTEX(pcpu_drain_mutex);
149static DEFINE_PER_CPU(struct pcpu_drain, pcpu_drain);
Mel Gormanbd233f52017-02-24 14:56:56 -0800150
Emese Revfy38addce2016-06-20 20:41:19 +0200151#ifdef CONFIG_GCC_PLUGIN_LATENT_ENTROPY
Kees Cook58bea412016-10-19 00:08:04 +0200152volatile unsigned long latent_entropy __latent_entropy;
Emese Revfy38addce2016-06-20 20:41:19 +0200153EXPORT_SYMBOL(latent_entropy);
154#endif
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/*
Christoph Lameter13808912007-10-16 01:25:27 -0700157 * Array of node states.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 */
Christoph Lameter13808912007-10-16 01:25:27 -0700159nodemask_t node_states[NR_NODE_STATES] __read_mostly = {
160 [N_POSSIBLE] = NODE_MASK_ALL,
161 [N_ONLINE] = { { [0] = 1UL } },
162#ifndef CONFIG_NUMA
163 [N_NORMAL_MEMORY] = { { [0] = 1UL } },
164#ifdef CONFIG_HIGHMEM
165 [N_HIGH_MEMORY] = { { [0] = 1UL } },
166#endif
Lai Jiangshan20b2f522012-12-12 13:52:00 -0800167 [N_MEMORY] = { { [0] = 1UL } },
Christoph Lameter13808912007-10-16 01:25:27 -0700168 [N_CPU] = { { [0] = 1UL } },
169#endif /* NUMA */
170};
171EXPORT_SYMBOL(node_states);
172
Arun KSca79b0c2018-12-28 00:34:29 -0800173atomic_long_t _totalram_pages __read_mostly;
174EXPORT_SYMBOL(_totalram_pages);
Hideo AOKIcb45b0e2006-04-10 22:52:59 -0700175unsigned long totalreserve_pages __read_mostly;
Pintu Kumare48322a2014-12-18 16:17:15 -0800176unsigned long totalcma_pages __read_mostly;
Johannes Weinerab8fabd2012-01-10 15:07:42 -0800177
Hugh Dickins1b76b022012-05-11 01:00:07 -0700178int percpu_pagelist_fraction;
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +1000179gfp_t gfp_allowed_mask __read_mostly = GFP_BOOT_MASK;
Kees Cook51cba1e2021-04-01 16:23:43 -0700180DEFINE_STATIC_KEY_MAYBE(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, init_on_alloc);
Alexander Potapenko64713842019-07-11 20:59:19 -0700181EXPORT_SYMBOL(init_on_alloc);
182
Kees Cook51cba1e2021-04-01 16:23:43 -0700183DEFINE_STATIC_KEY_MAYBE(CONFIG_INIT_ON_FREE_DEFAULT_ON, init_on_free);
Alexander Potapenko64713842019-07-11 20:59:19 -0700184EXPORT_SYMBOL(init_on_free);
185
Vlastimil Babka04013512020-12-14 19:13:30 -0800186static bool _init_on_alloc_enabled_early __read_mostly
187 = IS_ENABLED(CONFIG_INIT_ON_ALLOC_DEFAULT_ON);
Alexander Potapenko64713842019-07-11 20:59:19 -0700188static int __init early_init_on_alloc(char *buf)
189{
Alexander Potapenko64713842019-07-11 20:59:19 -0700190
Vlastimil Babka04013512020-12-14 19:13:30 -0800191 return kstrtobool(buf, &_init_on_alloc_enabled_early);
Alexander Potapenko64713842019-07-11 20:59:19 -0700192}
193early_param("init_on_alloc", early_init_on_alloc);
194
Vlastimil Babka04013512020-12-14 19:13:30 -0800195static bool _init_on_free_enabled_early __read_mostly
196 = IS_ENABLED(CONFIG_INIT_ON_FREE_DEFAULT_ON);
Alexander Potapenko64713842019-07-11 20:59:19 -0700197static int __init early_init_on_free(char *buf)
198{
Vlastimil Babka04013512020-12-14 19:13:30 -0800199 return kstrtobool(buf, &_init_on_free_enabled_early);
Alexander Potapenko64713842019-07-11 20:59:19 -0700200}
201early_param("init_on_free", early_init_on_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Vlastimil Babkabb14c2c2015-09-08 15:01:25 -0700203/*
204 * A cached value of the page's pageblock's migratetype, used when the page is
205 * put on a pcplist. Used to avoid the pageblock migratetype lookup when
206 * freeing from pcplists in most cases, at the cost of possibly becoming stale.
207 * Also the migratetype set in the page does not necessarily match the pcplist
208 * index, e.g. page might have MIGRATE_CMA set but be on a pcplist with any
209 * other index - this ensures that it will be put on the correct CMA freelist.
210 */
211static inline int get_pcppage_migratetype(struct page *page)
212{
213 return page->index;
214}
215
216static inline void set_pcppage_migratetype(struct page *page, int migratetype)
217{
218 page->index = migratetype;
219}
220
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800221#ifdef CONFIG_PM_SLEEP
222/*
223 * The following functions are used by the suspend/hibernate code to temporarily
224 * change gfp_allowed_mask in order to avoid using I/O during memory allocations
225 * while devices are suspended. To avoid races with the suspend/hibernate code,
Pingfan Liu55f25032018-07-31 16:51:32 +0800226 * they should always be called with system_transition_mutex held
227 * (gfp_allowed_mask also should only be modified with system_transition_mutex
228 * held, unless the suspend/hibernate code is guaranteed not to run in parallel
229 * with that modification).
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800230 */
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100231
232static gfp_t saved_gfp_mask;
233
234void pm_restore_gfp_mask(void)
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800235{
Pingfan Liu55f25032018-07-31 16:51:32 +0800236 WARN_ON(!mutex_is_locked(&system_transition_mutex));
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100237 if (saved_gfp_mask) {
238 gfp_allowed_mask = saved_gfp_mask;
239 saved_gfp_mask = 0;
240 }
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800241}
242
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100243void pm_restrict_gfp_mask(void)
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800244{
Pingfan Liu55f25032018-07-31 16:51:32 +0800245 WARN_ON(!mutex_is_locked(&system_transition_mutex));
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100246 WARN_ON(saved_gfp_mask);
247 saved_gfp_mask = gfp_allowed_mask;
Mel Gormand0164ad2015-11-06 16:28:21 -0800248 gfp_allowed_mask &= ~(__GFP_IO | __GFP_FS);
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800249}
Mel Gormanf90ac392012-01-10 15:07:15 -0800250
251bool pm_suspended_storage(void)
252{
Mel Gormand0164ad2015-11-06 16:28:21 -0800253 if ((gfp_allowed_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS))
Mel Gormanf90ac392012-01-10 15:07:15 -0800254 return false;
255 return true;
256}
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800257#endif /* CONFIG_PM_SLEEP */
258
Mel Gormand9c23402007-10-16 01:26:01 -0700259#ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -0800260unsigned int pageblock_order __read_mostly;
Mel Gormand9c23402007-10-16 01:26:01 -0700261#endif
262
David Hildenbrand7fef4312020-10-15 20:09:35 -0700263static void __free_pages_ok(struct page *page, unsigned int order,
264 fpi_t fpi_flags);
David Howellsa226f6c2006-01-06 00:11:08 -0800265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266/*
267 * results with 256, 32 in the lowmem_reserve sysctl:
268 * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
269 * 1G machine -> (16M dma, 784M normal, 224M high)
270 * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
271 * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
Yaowei Bai84109e12015-02-12 15:00:22 -0800272 * HIGHMEM allocation will leave (224M+784M)/256 of ram reserved in ZONE_DMA
Andi Kleena2f1b422005-11-05 17:25:53 +0100273 *
274 * TBD: should special case ZONE_DMA32 machines here - in those we normally
275 * don't need any ZONE_NORMAL reservation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 */
Joonsoo Kimd3cda232018-04-10 16:30:11 -0700277int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES] = {
Christoph Lameter4b51d662007-02-10 01:43:10 -0800278#ifdef CONFIG_ZONE_DMA
Joonsoo Kimd3cda232018-04-10 16:30:11 -0700279 [ZONE_DMA] = 256,
Christoph Lameter4b51d662007-02-10 01:43:10 -0800280#endif
Christoph Lameterfb0e7942006-09-25 23:31:13 -0700281#ifdef CONFIG_ZONE_DMA32
Joonsoo Kimd3cda232018-04-10 16:30:11 -0700282 [ZONE_DMA32] = 256,
Christoph Lameterfb0e7942006-09-25 23:31:13 -0700283#endif
Joonsoo Kimd3cda232018-04-10 16:30:11 -0700284 [ZONE_NORMAL] = 32,
Christoph Lametere53ef382006-09-25 23:31:14 -0700285#ifdef CONFIG_HIGHMEM
Joonsoo Kimd3cda232018-04-10 16:30:11 -0700286 [ZONE_HIGHMEM] = 0,
Christoph Lametere53ef382006-09-25 23:31:14 -0700287#endif
Joonsoo Kimd3cda232018-04-10 16:30:11 -0700288 [ZONE_MOVABLE] = 0,
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700289};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Helge Deller15ad7cd2006-12-06 20:40:36 -0800291static char * const zone_names[MAX_NR_ZONES] = {
Christoph Lameter4b51d662007-02-10 01:43:10 -0800292#ifdef CONFIG_ZONE_DMA
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700293 "DMA",
Christoph Lameter4b51d662007-02-10 01:43:10 -0800294#endif
Christoph Lameterfb0e7942006-09-25 23:31:13 -0700295#ifdef CONFIG_ZONE_DMA32
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700296 "DMA32",
Christoph Lameterfb0e7942006-09-25 23:31:13 -0700297#endif
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700298 "Normal",
Christoph Lametere53ef382006-09-25 23:31:14 -0700299#ifdef CONFIG_HIGHMEM
Mel Gorman2a1e2742007-07-17 04:03:12 -0700300 "HighMem",
Christoph Lametere53ef382006-09-25 23:31:14 -0700301#endif
Mel Gorman2a1e2742007-07-17 04:03:12 -0700302 "Movable",
Dan Williams033fbae2015-08-09 15:29:06 -0400303#ifdef CONFIG_ZONE_DEVICE
304 "Device",
305#endif
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700306};
307
Alexey Dobriyanc999fbd2018-12-28 00:35:55 -0800308const char * const migratetype_names[MIGRATE_TYPES] = {
Vlastimil Babka60f30352016-03-15 14:56:08 -0700309 "Unmovable",
310 "Movable",
311 "Reclaimable",
312 "HighAtomic",
313#ifdef CONFIG_CMA
314 "CMA",
315#endif
316#ifdef CONFIG_MEMORY_ISOLATION
317 "Isolate",
318#endif
319};
320
Anshuman Khandualae70edd2020-06-03 15:59:17 -0700321compound_page_dtor * const compound_page_dtors[NR_COMPOUND_DTORS] = {
322 [NULL_COMPOUND_DTOR] = NULL,
323 [COMPOUND_PAGE_DTOR] = free_compound_page,
Kirill A. Shutemovf1e61552015-11-06 16:29:50 -0800324#ifdef CONFIG_HUGETLB_PAGE
Anshuman Khandualae70edd2020-06-03 15:59:17 -0700325 [HUGETLB_PAGE_DTOR] = free_huge_page,
Kirill A. Shutemovf1e61552015-11-06 16:29:50 -0800326#endif
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800327#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Anshuman Khandualae70edd2020-06-03 15:59:17 -0700328 [TRANSHUGE_PAGE_DTOR] = free_transhuge_page,
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800329#endif
Kirill A. Shutemovf1e61552015-11-06 16:29:50 -0800330};
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332int min_free_kbytes = 1024;
Han Pingtian42aa83c2014-01-23 15:53:28 -0800333int user_min_free_kbytes = -1;
Mel Gorman24512228b2019-04-25 22:23:51 -0700334#ifdef CONFIG_DISCONTIGMEM
335/*
336 * DiscontigMem defines memory ranges as separate pg_data_t even if the ranges
337 * are not on separate NUMA nodes. Functionally this works but with
338 * watermark_boost_factor, it can reclaim prematurely as the ranges can be
339 * quite small. By default, do not boost watermarks on discontigmem as in
340 * many cases very high-order allocations like THP are likely to be
341 * unsupported and the premature reclaim offsets the advantage of long-term
342 * fragmentation avoidance.
343 */
344int watermark_boost_factor __read_mostly;
345#else
Mel Gorman1c308442018-12-28 00:35:52 -0800346int watermark_boost_factor __read_mostly = 15000;
Mel Gorman24512228b2019-04-25 22:23:51 -0700347#endif
Johannes Weiner795ae7a2016-03-17 14:19:14 -0700348int watermark_scale_factor = 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Oscar Salvadorbbe5d992018-12-28 00:37:24 -0800350static unsigned long nr_kernel_pages __initdata;
351static unsigned long nr_all_pages __initdata;
352static unsigned long dma_reserve __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Oscar Salvadorbbe5d992018-12-28 00:37:24 -0800354static unsigned long arch_zone_lowest_possible_pfn[MAX_NR_ZONES] __initdata;
355static unsigned long arch_zone_highest_possible_pfn[MAX_NR_ZONES] __initdata;
David Rientjes7f16f912018-04-05 16:23:12 -0700356static unsigned long required_kernelcore __initdata;
David Rientjesa5c6d652018-04-05 16:23:09 -0700357static unsigned long required_kernelcore_percent __initdata;
David Rientjes7f16f912018-04-05 16:23:12 -0700358static unsigned long required_movablecore __initdata;
David Rientjesa5c6d652018-04-05 16:23:09 -0700359static unsigned long required_movablecore_percent __initdata;
Oscar Salvadorbbe5d992018-12-28 00:37:24 -0800360static unsigned long zone_movable_pfn[MAX_NUMNODES] __initdata;
David Rientjes7f16f912018-04-05 16:23:12 -0700361static bool mirrored_kernelcore __meminitdata;
Mel Gormanc7132162006-09-27 01:49:43 -0700362
Tejun Heo0ee332c2011-12-08 10:22:09 -0800363/* movable_zone is the "real" zone pages in ZONE_MOVABLE are taken from */
364int movable_zone;
365EXPORT_SYMBOL(movable_zone);
Mel Gormanc7132162006-09-27 01:49:43 -0700366
Miklos Szeredi418508c2007-05-23 13:57:55 -0700367#if MAX_NUMNODES > 1
Alexey Dobriyanb9726c22019-03-05 15:48:26 -0800368unsigned int nr_node_ids __read_mostly = MAX_NUMNODES;
Alexey Dobriyance0725f2019-03-05 15:48:29 -0800369unsigned int nr_online_nodes __read_mostly = 1;
Miklos Szeredi418508c2007-05-23 13:57:55 -0700370EXPORT_SYMBOL(nr_node_ids);
Christoph Lameter62bc62a2009-06-16 15:32:15 -0700371EXPORT_SYMBOL(nr_online_nodes);
Miklos Szeredi418508c2007-05-23 13:57:55 -0700372#endif
373
Mel Gorman9ef9acb2007-10-16 01:25:54 -0700374int page_group_by_mobility_disabled __read_mostly;
375
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700376#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
Waiman Long3c0c12c2018-12-28 00:38:51 -0800377/*
378 * During boot we initialize deferred pages on-demand, as needed, but once
379 * page_alloc_init_late() has finished, the deferred pages are all initialized,
380 * and we can permanently disable that path.
381 */
382static DEFINE_STATIC_KEY_TRUE(deferred_pages);
383
384/*
385 * Calling kasan_free_pages() only after deferred memory initialization
386 * has completed. Poisoning pages during deferred memory init will greatly
387 * lengthen the process and cause problem in large memory systems as the
388 * deferred pages initialization is done with interrupt disabled.
389 *
390 * Assuming that there will be no reference to those newly initialized
391 * pages before they are ever allocated, this should have no effect on
392 * KASAN memory tracking as the poison will be properly inserted at page
393 * allocation time. The only corner case is when pages are allocated by
394 * on-demand allocation and then freed again before the deferred pages
395 * initialization is done, but this is not likely to happen.
396 */
Andrey Konovalov2c335682021-04-29 22:59:52 -0700397static inline void kasan_free_nondeferred_pages(struct page *page, int order,
Andrey Konovalov1bb5eab2021-04-29 23:00:02 -0700398 bool init, fpi_t fpi_flags)
Waiman Long3c0c12c2018-12-28 00:38:51 -0800399{
Andrey Konovalov2c335682021-04-29 22:59:52 -0700400 if (static_branch_unlikely(&deferred_pages))
401 return;
402 if (!IS_ENABLED(CONFIG_KASAN_GENERIC) &&
403 (fpi_flags & FPI_SKIP_KASAN_POISON))
404 return;
Andrey Konovalov1bb5eab2021-04-29 23:00:02 -0700405 kasan_free_pages(page, order, init);
Waiman Long3c0c12c2018-12-28 00:38:51 -0800406}
407
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700408/* Returns true if the struct page for the pfn is uninitialised */
Mel Gorman0e1cc952015-06-30 14:57:27 -0700409static inline bool __meminit early_page_uninitialised(unsigned long pfn)
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700410{
Mel Gormanef70b6f2016-07-14 12:07:23 -0700411 int nid = early_pfn_to_nid(pfn);
412
413 if (node_online(nid) && pfn >= NODE_DATA(nid)->first_deferred_pfn)
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700414 return true;
415
416 return false;
417}
418
419/*
Pavel Tatashind3035be2018-10-26 15:09:37 -0700420 * Returns true when the remaining initialisation should be deferred until
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700421 * later in the boot cycle when it can be parallelised.
422 */
Pavel Tatashind3035be2018-10-26 15:09:37 -0700423static bool __meminit
424defer_init(int nid, unsigned long pfn, unsigned long end_pfn)
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700425{
Pavel Tatashind3035be2018-10-26 15:09:37 -0700426 static unsigned long prev_end_pfn, nr_initialised;
427
428 /*
429 * prev_end_pfn static that contains the end of previous zone
430 * No need to protect because called very early in boot before smp_init.
431 */
432 if (prev_end_pfn != end_pfn) {
433 prev_end_pfn = end_pfn;
434 nr_initialised = 0;
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700435 }
436
Pavel Tatashind3035be2018-10-26 15:09:37 -0700437 /* Always populate low zones for address-constrained allocations */
438 if (end_pfn < pgdat_end_pfn(NODE_DATA(nid)))
439 return false;
Wei Yang23b68cf2018-12-28 00:36:18 -0800440
Baoquan Hedc2da7b2020-12-29 15:14:37 -0800441 if (NODE_DATA(nid)->first_deferred_pfn != ULONG_MAX)
442 return true;
Wei Yang23b68cf2018-12-28 00:36:18 -0800443 /*
444 * We start only with one section of pages, more pages are added as
445 * needed until the rest of deferred pages are initialized.
446 */
Pavel Tatashind3035be2018-10-26 15:09:37 -0700447 nr_initialised++;
Wei Yang23b68cf2018-12-28 00:36:18 -0800448 if ((nr_initialised > PAGES_PER_SECTION) &&
Pavel Tatashind3035be2018-10-26 15:09:37 -0700449 (pfn & (PAGES_PER_SECTION - 1)) == 0) {
450 NODE_DATA(nid)->first_deferred_pfn = pfn;
451 return true;
452 }
453 return false;
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700454}
455#else
Andrey Konovalov2c335682021-04-29 22:59:52 -0700456static inline void kasan_free_nondeferred_pages(struct page *page, int order,
Andrey Konovalov1bb5eab2021-04-29 23:00:02 -0700457 bool init, fpi_t fpi_flags)
Andrey Konovalov2c335682021-04-29 22:59:52 -0700458{
459 if (!IS_ENABLED(CONFIG_KASAN_GENERIC) &&
460 (fpi_flags & FPI_SKIP_KASAN_POISON))
461 return;
Andrey Konovalov1bb5eab2021-04-29 23:00:02 -0700462 kasan_free_pages(page, order, init);
Andrey Konovalov2c335682021-04-29 22:59:52 -0700463}
Waiman Long3c0c12c2018-12-28 00:38:51 -0800464
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700465static inline bool early_page_uninitialised(unsigned long pfn)
466{
467 return false;
468}
469
Pavel Tatashind3035be2018-10-26 15:09:37 -0700470static inline bool defer_init(int nid, unsigned long pfn, unsigned long end_pfn)
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700471{
Pavel Tatashind3035be2018-10-26 15:09:37 -0700472 return false;
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700473}
474#endif
475
Mel Gorman0b423ca2016-05-19 17:14:27 -0700476/* Return a pointer to the bitmap storing bits affecting a block of pages */
477static inline unsigned long *get_pageblock_bitmap(struct page *page,
478 unsigned long pfn)
479{
480#ifdef CONFIG_SPARSEMEM
Dan Williamsf1eca352019-07-18 15:57:57 -0700481 return section_to_usemap(__pfn_to_section(pfn));
Mel Gorman0b423ca2016-05-19 17:14:27 -0700482#else
483 return page_zone(page)->pageblock_flags;
484#endif /* CONFIG_SPARSEMEM */
485}
486
487static inline int pfn_to_bitidx(struct page *page, unsigned long pfn)
488{
489#ifdef CONFIG_SPARSEMEM
490 pfn &= (PAGES_PER_SECTION-1);
Mel Gorman0b423ca2016-05-19 17:14:27 -0700491#else
492 pfn = pfn - round_down(page_zone(page)->zone_start_pfn, pageblock_nr_pages);
Mel Gorman0b423ca2016-05-19 17:14:27 -0700493#endif /* CONFIG_SPARSEMEM */
Wei Yang399b7952020-08-06 23:25:44 -0700494 return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
Mel Gorman0b423ca2016-05-19 17:14:27 -0700495}
496
Wei Yang535b81e2020-08-06 23:25:51 -0700497static __always_inline
498unsigned long __get_pfnblock_flags_mask(struct page *page,
Mel Gorman0b423ca2016-05-19 17:14:27 -0700499 unsigned long pfn,
Mel Gorman0b423ca2016-05-19 17:14:27 -0700500 unsigned long mask)
501{
502 unsigned long *bitmap;
503 unsigned long bitidx, word_bitidx;
504 unsigned long word;
505
506 bitmap = get_pageblock_bitmap(page, pfn);
507 bitidx = pfn_to_bitidx(page, pfn);
508 word_bitidx = bitidx / BITS_PER_LONG;
509 bitidx &= (BITS_PER_LONG-1);
510
511 word = bitmap[word_bitidx];
Wei Yangd93d5ab2020-08-06 23:25:48 -0700512 return (word >> bitidx) & mask;
Mel Gorman0b423ca2016-05-19 17:14:27 -0700513}
514
Mauro Carvalho Chehaba00cda32020-12-14 19:14:39 -0800515/**
516 * get_pfnblock_flags_mask - Return the requested group of flags for the pageblock_nr_pages block of pages
517 * @page: The page within the block of interest
518 * @pfn: The target page frame number
519 * @mask: mask of bits that the caller is interested in
520 *
521 * Return: pageblock_bits flags
522 */
Mel Gorman0b423ca2016-05-19 17:14:27 -0700523unsigned long get_pfnblock_flags_mask(struct page *page, unsigned long pfn,
Mel Gorman0b423ca2016-05-19 17:14:27 -0700524 unsigned long mask)
525{
Wei Yang535b81e2020-08-06 23:25:51 -0700526 return __get_pfnblock_flags_mask(page, pfn, mask);
Mel Gorman0b423ca2016-05-19 17:14:27 -0700527}
528
529static __always_inline int get_pfnblock_migratetype(struct page *page, unsigned long pfn)
530{
Wei Yang535b81e2020-08-06 23:25:51 -0700531 return __get_pfnblock_flags_mask(page, pfn, MIGRATETYPE_MASK);
Mel Gorman0b423ca2016-05-19 17:14:27 -0700532}
533
534/**
535 * set_pfnblock_flags_mask - Set the requested group of flags for a pageblock_nr_pages block of pages
536 * @page: The page within the block of interest
537 * @flags: The flags to set
538 * @pfn: The target page frame number
Mel Gorman0b423ca2016-05-19 17:14:27 -0700539 * @mask: mask of bits that the caller is interested in
540 */
541void set_pfnblock_flags_mask(struct page *page, unsigned long flags,
542 unsigned long pfn,
Mel Gorman0b423ca2016-05-19 17:14:27 -0700543 unsigned long mask)
544{
545 unsigned long *bitmap;
546 unsigned long bitidx, word_bitidx;
547 unsigned long old_word, word;
548
549 BUILD_BUG_ON(NR_PAGEBLOCK_BITS != 4);
Pingfan Liu125b8602018-12-28 00:38:43 -0800550 BUILD_BUG_ON(MIGRATE_TYPES > (1 << PB_migratetype_bits));
Mel Gorman0b423ca2016-05-19 17:14:27 -0700551
552 bitmap = get_pageblock_bitmap(page, pfn);
553 bitidx = pfn_to_bitidx(page, pfn);
554 word_bitidx = bitidx / BITS_PER_LONG;
555 bitidx &= (BITS_PER_LONG-1);
556
557 VM_BUG_ON_PAGE(!zone_spans_pfn(page_zone(page), pfn), page);
558
Wei Yangd93d5ab2020-08-06 23:25:48 -0700559 mask <<= bitidx;
560 flags <<= bitidx;
Mel Gorman0b423ca2016-05-19 17:14:27 -0700561
562 word = READ_ONCE(bitmap[word_bitidx]);
563 for (;;) {
564 old_word = cmpxchg(&bitmap[word_bitidx], word, (word & ~mask) | flags);
565 if (word == old_word)
566 break;
567 word = old_word;
568 }
569}
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700570
Minchan Kimee6f5092012-07-31 16:43:50 -0700571void set_pageblock_migratetype(struct page *page, int migratetype)
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700572{
KOSAKI Motohiro5d0f3f72013-11-12 15:08:18 -0800573 if (unlikely(page_group_by_mobility_disabled &&
574 migratetype < MIGRATE_PCPTYPES))
Mel Gorman49255c62009-06-16 15:31:58 -0700575 migratetype = MIGRATE_UNMOVABLE;
576
Wei Yangd93d5ab2020-08-06 23:25:48 -0700577 set_pfnblock_flags_mask(page, (unsigned long)migratetype,
Wei Yang535b81e2020-08-06 23:25:51 -0700578 page_to_pfn(page), MIGRATETYPE_MASK);
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700579}
580
Nick Piggin13e74442006-01-06 00:10:58 -0800581#ifdef CONFIG_DEBUG_VM
Dave Hansenc6a57e12005-10-29 18:16:52 -0700582static int page_outside_zone_boundaries(struct zone *zone, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Dave Hansenbdc8cb92005-10-29 18:16:53 -0700584 int ret = 0;
585 unsigned seq;
586 unsigned long pfn = page_to_pfn(page);
Cody P Schaferb5e6a5a2013-02-22 16:35:28 -0800587 unsigned long sp, start_pfn;
Dave Hansenc6a57e12005-10-29 18:16:52 -0700588
Dave Hansenbdc8cb92005-10-29 18:16:53 -0700589 do {
590 seq = zone_span_seqbegin(zone);
Cody P Schaferb5e6a5a2013-02-22 16:35:28 -0800591 start_pfn = zone->zone_start_pfn;
592 sp = zone->spanned_pages;
Cody P Schafer108bcc92013-02-22 16:35:23 -0800593 if (!zone_spans_pfn(zone, pfn))
Dave Hansenbdc8cb92005-10-29 18:16:53 -0700594 ret = 1;
595 } while (zone_span_seqretry(zone, seq));
596
Cody P Schaferb5e6a5a2013-02-22 16:35:28 -0800597 if (ret)
Dave Hansen613813e2014-06-04 16:07:27 -0700598 pr_err("page 0x%lx outside node %d zone %s [ 0x%lx - 0x%lx ]\n",
599 pfn, zone_to_nid(zone), zone->name,
600 start_pfn, start_pfn + sp);
Cody P Schaferb5e6a5a2013-02-22 16:35:28 -0800601
Dave Hansenbdc8cb92005-10-29 18:16:53 -0700602 return ret;
Dave Hansenc6a57e12005-10-29 18:16:52 -0700603}
604
605static int page_is_consistent(struct zone *zone, struct page *page)
606{
Andy Whitcroft14e07292007-05-06 14:49:14 -0700607 if (!pfn_valid_within(page_to_pfn(page)))
Dave Hansenc6a57e12005-10-29 18:16:52 -0700608 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (zone != page_zone(page))
Dave Hansenc6a57e12005-10-29 18:16:52 -0700610 return 0;
611
612 return 1;
613}
614/*
615 * Temporary debugging check for pages not lying within a given zone.
616 */
Matthias Kaehlcked73d3c9f2017-07-06 15:39:23 -0700617static int __maybe_unused bad_range(struct zone *zone, struct page *page)
Dave Hansenc6a57e12005-10-29 18:16:52 -0700618{
619 if (page_outside_zone_boundaries(zone, page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return 1;
Dave Hansenc6a57e12005-10-29 18:16:52 -0700621 if (!page_is_consistent(zone, page))
622 return 1;
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return 0;
625}
Nick Piggin13e74442006-01-06 00:10:58 -0800626#else
Matthias Kaehlcked73d3c9f2017-07-06 15:39:23 -0700627static inline int __maybe_unused bad_range(struct zone *zone, struct page *page)
Nick Piggin13e74442006-01-06 00:10:58 -0800628{
629 return 0;
630}
631#endif
632
Wei Yang82a32412020-06-03 15:58:29 -0700633static void bad_page(struct page *page, const char *reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800635 static unsigned long resume;
636 static unsigned long nr_shown;
637 static unsigned long nr_unshown;
638
639 /*
640 * Allow a burst of 60 reports, then keep quiet for that minute;
641 * or allow a steady drip of one report per second.
642 */
643 if (nr_shown == 60) {
644 if (time_before(jiffies, resume)) {
645 nr_unshown++;
646 goto out;
647 }
648 if (nr_unshown) {
Vlastimil Babkaff8e8112016-03-15 14:56:24 -0700649 pr_alert(
Hugh Dickins1e9e6362009-01-06 14:40:13 -0800650 "BUG: Bad page state: %lu messages suppressed\n",
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800651 nr_unshown);
652 nr_unshown = 0;
653 }
654 nr_shown = 0;
655 }
656 if (nr_shown++ == 0)
657 resume = jiffies + 60 * HZ;
658
Vlastimil Babkaff8e8112016-03-15 14:56:24 -0700659 pr_alert("BUG: Bad page state in process %s pfn:%05lx\n",
Hugh Dickins3dc14742009-01-06 14:40:08 -0800660 current->comm, page_to_pfn(page));
Matthew Wilcox (Oracle)d2f07ec2021-06-28 19:41:07 -0700661 dump_page(page, reason);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700662
Dave Jones4f318882011-10-31 17:07:24 -0700663 print_modules();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 dump_stack();
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800665out:
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800666 /* Leave bad fields for debug, except PageBuddy could make trouble */
Mel Gorman22b751c2013-02-22 16:34:59 -0800667 page_mapcount_reset(page); /* remove PageBuddy */
Rusty Russell373d4d02013-01-21 17:17:39 +1030668 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671/*
672 * Higher-order pages are called "compound pages". They are structured thusly:
673 *
Kirill A. Shutemov1d798ca2015-11-06 16:29:54 -0800674 * The first PAGE_SIZE page is called the "head page" and have PG_head set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 *
Kirill A. Shutemov1d798ca2015-11-06 16:29:54 -0800676 * The remaining PAGE_SIZE pages are called "tail pages". PageTail() is encoded
677 * in bit 0 of page->compound_head. The rest of bits is pointer to head page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 *
Kirill A. Shutemov1d798ca2015-11-06 16:29:54 -0800679 * The first tail page's ->compound_dtor holds the offset in array of compound
680 * page destructors. See compound_page_dtors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 *
Kirill A. Shutemov1d798ca2015-11-06 16:29:54 -0800682 * The first tail page's ->compound_order holds the order of allocation.
Hugh Dickins41d78ba2006-02-14 13:52:58 -0800683 * This usage means that zero-order pages may not be compound.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 */
Hugh Dickinsd98c7a02006-02-14 13:52:59 -0800685
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800686void free_compound_page(struct page *page)
Hugh Dickinsd98c7a02006-02-14 13:52:59 -0800687{
Yang Shi7ae88532019-09-23 15:38:09 -0700688 mem_cgroup_uncharge(page);
David Hildenbrand7fef4312020-10-15 20:09:35 -0700689 __free_pages_ok(page, compound_order(page), FPI_NONE);
Hugh Dickinsd98c7a02006-02-14 13:52:59 -0800690}
691
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -0800692void prep_compound_page(struct page *page, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
694 int i;
695 int nr_pages = 1 << order;
696
Christoph Lameter6d777952007-05-06 14:49:40 -0700697 __SetPageHead(page);
Andy Whitcroft18229df2008-11-06 12:53:27 -0800698 for (i = 1; i < nr_pages; i++) {
699 struct page *p = page + i;
Youquan Song58a84aa2011-12-08 14:34:18 -0800700 set_page_count(p, 0);
Kirill A. Shutemov1c290f62016-01-15 16:52:07 -0800701 p->mapping = TAIL_MAPPING;
Kirill A. Shutemov1d798ca2015-11-06 16:29:54 -0800702 set_compound_head(p, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
Matthew Wilcox (Oracle)1378a5e2020-08-14 17:30:23 -0700704
705 set_compound_page_dtor(page, COMPOUND_PAGE_DTOR);
706 set_compound_order(page, order);
Kirill A. Shutemov53f92632016-01-15 16:53:42 -0800707 atomic_set(compound_mapcount_ptr(page), -1);
John Hubbard47e29d32020-04-01 21:05:33 -0700708 if (hpage_pincount_available(page))
709 atomic_set(compound_pincount_ptr(page), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -0800712#ifdef CONFIG_DEBUG_PAGEALLOC
713unsigned int _debug_guardpage_minorder;
Vlastimil Babka96a2b032019-07-11 20:55:06 -0700714
Vlastimil Babka8e57f8a2020-01-13 16:29:20 -0800715bool _debug_pagealloc_enabled_early __read_mostly
716 = IS_ENABLED(CONFIG_DEBUG_PAGEALLOC_ENABLE_DEFAULT);
717EXPORT_SYMBOL(_debug_pagealloc_enabled_early);
Vlastimil Babka96a2b032019-07-11 20:55:06 -0700718DEFINE_STATIC_KEY_FALSE(_debug_pagealloc_enabled);
Joonsoo Kim505f6d22016-03-17 14:17:56 -0700719EXPORT_SYMBOL(_debug_pagealloc_enabled);
Vlastimil Babka96a2b032019-07-11 20:55:06 -0700720
721DEFINE_STATIC_KEY_FALSE(_debug_guardpage_enabled);
Joonsoo Kime30825f2014-12-12 16:55:49 -0800722
Joonsoo Kim031bc572014-12-12 16:55:52 -0800723static int __init early_debug_pagealloc(char *buf)
724{
Vlastimil Babka8e57f8a2020-01-13 16:29:20 -0800725 return kstrtobool(buf, &_debug_pagealloc_enabled_early);
Joonsoo Kim031bc572014-12-12 16:55:52 -0800726}
727early_param("debug_pagealloc", early_debug_pagealloc);
728
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -0800729static int __init debug_guardpage_minorder_setup(char *buf)
730{
731 unsigned long res;
732
733 if (kstrtoul(buf, 10, &res) < 0 || res > MAX_ORDER / 2) {
Joe Perches11705322016-03-17 14:19:50 -0700734 pr_err("Bad debug_guardpage_minorder value\n");
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -0800735 return 0;
736 }
737 _debug_guardpage_minorder = res;
Joe Perches11705322016-03-17 14:19:50 -0700738 pr_info("Setting debug_guardpage_minorder to %lu\n", res);
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -0800739 return 0;
740}
Joonsoo Kimf1c1e9f2016-10-07 16:58:18 -0700741early_param("debug_guardpage_minorder", debug_guardpage_minorder_setup);
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -0800742
Joonsoo Kimacbc15a2016-10-07 16:58:15 -0700743static inline bool set_page_guard(struct zone *zone, struct page *page,
Joonsoo Kim2847cf92014-12-12 16:55:01 -0800744 unsigned int order, int migratetype)
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -0800745{
Joonsoo Kime30825f2014-12-12 16:55:49 -0800746 if (!debug_guardpage_enabled())
Joonsoo Kimacbc15a2016-10-07 16:58:15 -0700747 return false;
748
749 if (order >= debug_guardpage_minorder())
750 return false;
Joonsoo Kime30825f2014-12-12 16:55:49 -0800751
Vlastimil Babka3972f6b2019-07-11 20:55:13 -0700752 __SetPageGuard(page);
Joonsoo Kim2847cf92014-12-12 16:55:01 -0800753 INIT_LIST_HEAD(&page->lru);
754 set_page_private(page, order);
755 /* Guard pages are not available for any usage */
756 __mod_zone_freepage_state(zone, -(1 << order), migratetype);
Joonsoo Kimacbc15a2016-10-07 16:58:15 -0700757
758 return true;
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -0800759}
760
Joonsoo Kim2847cf92014-12-12 16:55:01 -0800761static inline void clear_page_guard(struct zone *zone, struct page *page,
762 unsigned int order, int migratetype)
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -0800763{
Joonsoo Kime30825f2014-12-12 16:55:49 -0800764 if (!debug_guardpage_enabled())
765 return;
766
Vlastimil Babka3972f6b2019-07-11 20:55:13 -0700767 __ClearPageGuard(page);
Joonsoo Kime30825f2014-12-12 16:55:49 -0800768
Joonsoo Kim2847cf92014-12-12 16:55:01 -0800769 set_page_private(page, 0);
770 if (!is_migrate_isolate(migratetype))
771 __mod_zone_freepage_state(zone, (1 << order), migratetype);
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -0800772}
773#else
Joonsoo Kimacbc15a2016-10-07 16:58:15 -0700774static inline bool set_page_guard(struct zone *zone, struct page *page,
775 unsigned int order, int migratetype) { return false; }
Joonsoo Kim2847cf92014-12-12 16:55:01 -0800776static inline void clear_page_guard(struct zone *zone, struct page *page,
777 unsigned int order, int migratetype) {}
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -0800778#endif
779
Vlastimil Babka04013512020-12-14 19:13:30 -0800780/*
781 * Enable static keys related to various memory debugging and hardening options.
782 * Some override others, and depend on early params that are evaluated in the
783 * order of appearance. So we need to first gather the full picture of what was
784 * enabled, and then make decisions.
785 */
786void init_mem_debugging_and_hardening(void)
787{
Sergei Trofimovich9df65f52021-04-29 23:02:11 -0700788 bool page_poisoning_requested = false;
Vlastimil Babka04013512020-12-14 19:13:30 -0800789
Vlastimil Babka8db26a32020-12-14 19:13:34 -0800790#ifdef CONFIG_PAGE_POISONING
791 /*
792 * Page poisoning is debug page alloc for some arches. If
793 * either of those options are enabled, enable poisoning.
794 */
795 if (page_poisoning_enabled() ||
796 (!IS_ENABLED(CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC) &&
Sergei Trofimovich9df65f52021-04-29 23:02:11 -0700797 debug_pagealloc_enabled())) {
Vlastimil Babka8db26a32020-12-14 19:13:34 -0800798 static_branch_enable(&_page_poisoning_enabled);
Sergei Trofimovich9df65f52021-04-29 23:02:11 -0700799 page_poisoning_requested = true;
800 }
Vlastimil Babka8db26a32020-12-14 19:13:34 -0800801#endif
802
Sergei Trofimovich9df65f52021-04-29 23:02:11 -0700803 if (_init_on_alloc_enabled_early) {
804 if (page_poisoning_requested)
805 pr_info("mem auto-init: CONFIG_PAGE_POISONING is on, "
806 "will take precedence over init_on_alloc\n");
807 else
808 static_branch_enable(&init_on_alloc);
809 }
810 if (_init_on_free_enabled_early) {
811 if (page_poisoning_requested)
812 pr_info("mem auto-init: CONFIG_PAGE_POISONING is on, "
813 "will take precedence over init_on_free\n");
814 else
815 static_branch_enable(&init_on_free);
816 }
817
Vlastimil Babka04013512020-12-14 19:13:30 -0800818#ifdef CONFIG_DEBUG_PAGEALLOC
819 if (!debug_pagealloc_enabled())
820 return;
821
822 static_branch_enable(&_debug_pagealloc_enabled);
823
824 if (!debug_guardpage_minorder())
825 return;
826
827 static_branch_enable(&_debug_guardpage_enabled);
828#endif
829}
830
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -0700831static inline void set_buddy_order(struct page *page, unsigned int order)
Andrew Morton6aa3001b22006-04-18 22:20:52 -0700832{
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700833 set_page_private(page, order);
Nick Piggin676165a2006-04-10 11:21:48 +1000834 __SetPageBuddy(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 * This function checks whether a page is free && is the buddy
Matthew Wilcox6e292b92018-06-07 17:08:18 -0700839 * we can coalesce a page and its buddy if
Vlastimil Babka13ad59d2017-02-22 15:41:51 -0800840 * (a) the buddy is not in a hole (check before calling!) &&
Nick Piggin676165a2006-04-10 11:21:48 +1000841 * (b) the buddy is in the buddy system &&
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700842 * (c) a page and its buddy have the same order &&
843 * (d) a page and its buddy are in the same zone.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 *
Matthew Wilcox6e292b92018-06-07 17:08:18 -0700845 * For recording whether a page is in the buddy system, we set PageBuddy.
846 * Setting, clearing, and testing PageBuddy is serialized by zone->lock.
Nick Piggin676165a2006-04-10 11:21:48 +1000847 *
848 * For recording page's order, we use page_private(page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 */
chenqiwufe925c02020-04-01 21:09:56 -0700850static inline bool page_is_buddy(struct page *page, struct page *buddy,
Mel Gorman7aeb09f2014-06-04 16:10:21 -0700851 unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
chenqiwufe925c02020-04-01 21:09:56 -0700853 if (!page_is_guard(buddy) && !PageBuddy(buddy))
854 return false;
Mel Gormand34c5fa2014-06-04 16:10:10 -0700855
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -0700856 if (buddy_order(buddy) != order)
chenqiwufe925c02020-04-01 21:09:56 -0700857 return false;
Weijie Yang4c5018c2015-02-10 14:11:39 -0800858
chenqiwufe925c02020-04-01 21:09:56 -0700859 /*
860 * zone check is done late to avoid uselessly calculating
861 * zone/node ids for pages that could never merge.
862 */
863 if (page_zone_id(page) != page_zone_id(buddy))
864 return false;
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -0800865
chenqiwufe925c02020-04-01 21:09:56 -0700866 VM_BUG_ON_PAGE(page_count(buddy) != 0, buddy);
Mel Gormand34c5fa2014-06-04 16:10:10 -0700867
chenqiwufe925c02020-04-01 21:09:56 -0700868 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
Mel Gorman5e1f0f02019-03-05 15:45:41 -0800871#ifdef CONFIG_COMPACTION
872static inline struct capture_control *task_capc(struct zone *zone)
873{
874 struct capture_control *capc = current->capture_control;
875
Vlastimil Babkadeba0482020-08-06 23:25:16 -0700876 return unlikely(capc) &&
Mel Gorman5e1f0f02019-03-05 15:45:41 -0800877 !(current->flags & PF_KTHREAD) &&
878 !capc->page &&
Vlastimil Babkadeba0482020-08-06 23:25:16 -0700879 capc->cc->zone == zone ? capc : NULL;
Mel Gorman5e1f0f02019-03-05 15:45:41 -0800880}
881
882static inline bool
883compaction_capture(struct capture_control *capc, struct page *page,
884 int order, int migratetype)
885{
886 if (!capc || order != capc->cc->order)
887 return false;
888
889 /* Do not accidentally pollute CMA or isolated regions*/
890 if (is_migrate_cma(migratetype) ||
891 is_migrate_isolate(migratetype))
892 return false;
893
894 /*
Ingo Molnarf0953a12021-05-06 18:06:47 -0700895 * Do not let lower order allocations pollute a movable pageblock.
Mel Gorman5e1f0f02019-03-05 15:45:41 -0800896 * This might let an unmovable request use a reclaimable pageblock
897 * and vice-versa but no more than normal fallback logic which can
898 * have trouble finding a high-order free page.
899 */
900 if (order < pageblock_order && migratetype == MIGRATE_MOVABLE)
901 return false;
902
903 capc->page = page;
904 return true;
905}
906
907#else
908static inline struct capture_control *task_capc(struct zone *zone)
909{
910 return NULL;
911}
912
913static inline bool
914compaction_capture(struct capture_control *capc, struct page *page,
915 int order, int migratetype)
916{
917 return false;
918}
919#endif /* CONFIG_COMPACTION */
920
Alexander Duyck6ab01362020-04-06 20:04:49 -0700921/* Used for pages not on another list */
922static inline void add_to_free_list(struct page *page, struct zone *zone,
923 unsigned int order, int migratetype)
924{
925 struct free_area *area = &zone->free_area[order];
926
927 list_add(&page->lru, &area->free_list[migratetype]);
928 area->nr_free++;
929}
930
931/* Used for pages not on another list */
932static inline void add_to_free_list_tail(struct page *page, struct zone *zone,
933 unsigned int order, int migratetype)
934{
935 struct free_area *area = &zone->free_area[order];
936
937 list_add_tail(&page->lru, &area->free_list[migratetype]);
938 area->nr_free++;
939}
940
David Hildenbrand293ffa52020-10-15 20:09:30 -0700941/*
942 * Used for pages which are on another list. Move the pages to the tail
943 * of the list - so the moved pages won't immediately be considered for
944 * allocation again (e.g., optimization for memory onlining).
945 */
Alexander Duyck6ab01362020-04-06 20:04:49 -0700946static inline void move_to_free_list(struct page *page, struct zone *zone,
947 unsigned int order, int migratetype)
948{
949 struct free_area *area = &zone->free_area[order];
950
David Hildenbrand293ffa52020-10-15 20:09:30 -0700951 list_move_tail(&page->lru, &area->free_list[migratetype]);
Alexander Duyck6ab01362020-04-06 20:04:49 -0700952}
953
954static inline void del_page_from_free_list(struct page *page, struct zone *zone,
955 unsigned int order)
956{
Alexander Duyck36e66c52020-04-06 20:04:56 -0700957 /* clear reported state and update reported page count */
958 if (page_reported(page))
959 __ClearPageReported(page);
960
Alexander Duyck6ab01362020-04-06 20:04:49 -0700961 list_del(&page->lru);
962 __ClearPageBuddy(page);
963 set_page_private(page, 0);
964 zone->free_area[order].nr_free--;
965}
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967/*
Alexander Duycka2129f22020-04-06 20:04:45 -0700968 * If this is not the largest possible page, check if the buddy
969 * of the next-highest order is free. If it is, it's possible
970 * that pages are being freed that will coalesce soon. In case,
971 * that is happening, add the free page to the tail of the list
972 * so it's less likely to be used soon and more likely to be merged
973 * as a higher order page
974 */
975static inline bool
976buddy_merge_likely(unsigned long pfn, unsigned long buddy_pfn,
977 struct page *page, unsigned int order)
978{
979 struct page *higher_page, *higher_buddy;
980 unsigned long combined_pfn;
981
982 if (order >= MAX_ORDER - 2)
983 return false;
984
985 if (!pfn_valid_within(buddy_pfn))
986 return false;
987
988 combined_pfn = buddy_pfn & pfn;
989 higher_page = page + (combined_pfn - pfn);
990 buddy_pfn = __find_buddy_pfn(combined_pfn, order + 1);
991 higher_buddy = higher_page + (buddy_pfn - combined_pfn);
992
993 return pfn_valid_within(buddy_pfn) &&
994 page_is_buddy(higher_page, higher_buddy, order + 1);
995}
996
997/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 * Freeing function for a buddy system allocator.
999 *
1000 * The concept of a buddy system is to maintain direct-mapped table
1001 * (containing bit values) for memory blocks of various "orders".
1002 * The bottom level table contains the map for the smallest allocatable
1003 * units of memory (here, pages), and each level above it describes
1004 * pairs of units from the levels below, hence, "buddies".
1005 * At a high level, all that happens here is marking the table entry
1006 * at the bottom level available, and propagating the changes upward
1007 * as necessary, plus some accounting needed to play nicely with other
1008 * parts of the VM system.
1009 * At each level, we keep a list of pages, which are heads of continuous
Matthew Wilcox6e292b92018-06-07 17:08:18 -07001010 * free pages of length of (1 << order) and marked with PageBuddy.
1011 * Page's order is recorded in page_private(page) field.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 * So when we are allocating or freeing one, we can derive the state of the
Michal Nazarewicz5f63b722012-01-11 15:16:11 +01001013 * other. That is, if we allocate a small block, and both were
1014 * free, the remainder of the region must be split into blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 * If a block is freed, and its buddy is also free, then this
Michal Nazarewicz5f63b722012-01-11 15:16:11 +01001016 * triggers coalescing into a block of larger size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 *
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +01001018 * -- nyc
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 */
1020
Nick Piggin48db57f2006-01-08 01:00:42 -08001021static inline void __free_one_page(struct page *page,
Mel Gormandc4b0ca2014-06-04 16:10:17 -07001022 unsigned long pfn,
Mel Gormaned0ae212009-06-16 15:32:07 -07001023 struct zone *zone, unsigned int order,
David Hildenbrandf04a5d52020-10-15 20:09:20 -07001024 int migratetype, fpi_t fpi_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Mel Gorman5e1f0f02019-03-05 15:45:41 -08001026 struct capture_control *capc = task_capc(zone);
Kees Cook3f649ab2020-06-03 13:09:38 -07001027 unsigned long buddy_pfn;
Alexander Duycka2129f22020-04-06 20:04:45 -07001028 unsigned long combined_pfn;
Alexander Duycka2129f22020-04-06 20:04:45 -07001029 unsigned int max_order;
1030 struct page *buddy;
1031 bool to_tail;
Vlastimil Babkad9dddbf2016-03-25 14:21:50 -07001032
Muchun Song7ad69832020-12-14 19:11:25 -08001033 max_order = min_t(unsigned int, MAX_ORDER - 1, pageblock_order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Cody P Schaferd29bb972013-02-22 16:35:25 -08001035 VM_BUG_ON(!zone_is_initialized(zone));
Kirill A. Shutemov6e9f0d52015-02-11 15:25:50 -08001036 VM_BUG_ON_PAGE(page->flags & PAGE_FLAGS_CHECK_AT_PREP, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Mel Gormaned0ae212009-06-16 15:32:07 -07001038 VM_BUG_ON(migratetype == -1);
Vlastimil Babkad9dddbf2016-03-25 14:21:50 -07001039 if (likely(!is_migrate_isolate(migratetype)))
Joonsoo Kim8f82b552014-11-13 15:19:18 -08001040 __mod_zone_freepage_state(zone, 1 << order, migratetype);
Mel Gormaned0ae212009-06-16 15:32:07 -07001041
Vlastimil Babka76741e72017-02-22 15:41:48 -08001042 VM_BUG_ON_PAGE(pfn & ((1 << order) - 1), page);
Sasha Levin309381fea2014-01-23 15:52:54 -08001043 VM_BUG_ON_PAGE(bad_range(zone, page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Vlastimil Babkad9dddbf2016-03-25 14:21:50 -07001045continue_merging:
Muchun Song7ad69832020-12-14 19:11:25 -08001046 while (order < max_order) {
Mel Gorman5e1f0f02019-03-05 15:45:41 -08001047 if (compaction_capture(capc, page, order, migratetype)) {
1048 __mod_zone_freepage_state(zone, -(1 << order),
1049 migratetype);
1050 return;
1051 }
Vlastimil Babka76741e72017-02-22 15:41:48 -08001052 buddy_pfn = __find_buddy_pfn(pfn, order);
1053 buddy = page + (buddy_pfn - pfn);
Vlastimil Babka13ad59d2017-02-22 15:41:51 -08001054
1055 if (!pfn_valid_within(buddy_pfn))
1056 goto done_merging;
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -07001057 if (!page_is_buddy(page, buddy, order))
Vlastimil Babkad9dddbf2016-03-25 14:21:50 -07001058 goto done_merging;
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -08001059 /*
1060 * Our buddy is free or it is CONFIG_DEBUG_PAGEALLOC guard page,
1061 * merge with it and move up one order.
1062 */
Dan Williamsb03641a2019-05-14 15:41:32 -07001063 if (page_is_guard(buddy))
Joonsoo Kim2847cf92014-12-12 16:55:01 -08001064 clear_page_guard(zone, buddy, order, migratetype);
Dan Williamsb03641a2019-05-14 15:41:32 -07001065 else
Alexander Duyck6ab01362020-04-06 20:04:49 -07001066 del_page_from_free_list(buddy, zone, order);
Vlastimil Babka76741e72017-02-22 15:41:48 -08001067 combined_pfn = buddy_pfn & pfn;
1068 page = page + (combined_pfn - pfn);
1069 pfn = combined_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 order++;
1071 }
Muchun Song7ad69832020-12-14 19:11:25 -08001072 if (order < MAX_ORDER - 1) {
Vlastimil Babkad9dddbf2016-03-25 14:21:50 -07001073 /* If we are here, it means order is >= pageblock_order.
1074 * We want to prevent merge between freepages on isolate
1075 * pageblock and normal pageblock. Without this, pageblock
1076 * isolation could cause incorrect freepage or CMA accounting.
1077 *
1078 * We don't want to hit this code for the more frequent
1079 * low-order merging.
1080 */
1081 if (unlikely(has_isolate_pageblock(zone))) {
1082 int buddy_mt;
1083
Vlastimil Babka76741e72017-02-22 15:41:48 -08001084 buddy_pfn = __find_buddy_pfn(pfn, order);
1085 buddy = page + (buddy_pfn - pfn);
Vlastimil Babkad9dddbf2016-03-25 14:21:50 -07001086 buddy_mt = get_pageblock_migratetype(buddy);
1087
1088 if (migratetype != buddy_mt
1089 && (is_migrate_isolate(migratetype) ||
1090 is_migrate_isolate(buddy_mt)))
1091 goto done_merging;
1092 }
Muchun Song7ad69832020-12-14 19:11:25 -08001093 max_order = order + 1;
Vlastimil Babkad9dddbf2016-03-25 14:21:50 -07001094 goto continue_merging;
1095 }
1096
1097done_merging:
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -07001098 set_buddy_order(page, order);
Corrado Zoccolo6dda9d52010-05-24 14:31:54 -07001099
David Hildenbrand47b6a24a22020-10-15 20:09:26 -07001100 if (fpi_flags & FPI_TO_TAIL)
1101 to_tail = true;
1102 else if (is_shuffle_order(order))
Alexander Duycka2129f22020-04-06 20:04:45 -07001103 to_tail = shuffle_pick_tail();
Dan Williams97500a42019-05-14 15:41:35 -07001104 else
Alexander Duycka2129f22020-04-06 20:04:45 -07001105 to_tail = buddy_merge_likely(pfn, buddy_pfn, page, order);
Dan Williams97500a42019-05-14 15:41:35 -07001106
Alexander Duycka2129f22020-04-06 20:04:45 -07001107 if (to_tail)
Alexander Duyck6ab01362020-04-06 20:04:49 -07001108 add_to_free_list_tail(page, zone, order, migratetype);
Alexander Duycka2129f22020-04-06 20:04:45 -07001109 else
Alexander Duyck6ab01362020-04-06 20:04:49 -07001110 add_to_free_list(page, zone, order, migratetype);
Alexander Duyck36e66c52020-04-06 20:04:56 -07001111
1112 /* Notify page reporting subsystem of freed page */
David Hildenbrandf04a5d52020-10-15 20:09:20 -07001113 if (!(fpi_flags & FPI_SKIP_REPORT_NOTIFY))
Alexander Duyck36e66c52020-04-06 20:04:56 -07001114 page_reporting_notify_free(order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
1116
Mel Gorman7bfec6f2016-05-19 17:14:15 -07001117/*
1118 * A bad page could be due to a number of fields. Instead of multiple branches,
1119 * try and check multiple fields with one check. The caller must do a detailed
1120 * check if necessary.
1121 */
1122static inline bool page_expected_state(struct page *page,
1123 unsigned long check_flags)
1124{
1125 if (unlikely(atomic_read(&page->_mapcount) != -1))
1126 return false;
1127
1128 if (unlikely((unsigned long)page->mapping |
1129 page_ref_count(page) |
1130#ifdef CONFIG_MEMCG
Muchun Song48060832021-04-29 22:56:45 -07001131 page->memcg_data |
Mel Gorman7bfec6f2016-05-19 17:14:15 -07001132#endif
1133 (page->flags & check_flags)))
1134 return false;
1135
1136 return true;
1137}
1138
Wei Yang58b7f112020-06-03 15:58:39 -07001139static const char *page_bad_reason(struct page *page, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
Wei Yang82a32412020-06-03 15:58:29 -07001141 const char *bad_reason = NULL;
Dave Hansenf0b791a2014-01-23 15:52:49 -08001142
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08001143 if (unlikely(atomic_read(&page->_mapcount) != -1))
Dave Hansenf0b791a2014-01-23 15:52:49 -08001144 bad_reason = "nonzero mapcount";
1145 if (unlikely(page->mapping != NULL))
1146 bad_reason = "non-NULL mapping";
Joonsoo Kimfe896d12016-03-17 14:19:26 -07001147 if (unlikely(page_ref_count(page) != 0))
Joonsoo Kim0139aa72016-05-19 17:10:49 -07001148 bad_reason = "nonzero _refcount";
Wei Yang58b7f112020-06-03 15:58:39 -07001149 if (unlikely(page->flags & flags)) {
1150 if (flags == PAGE_FLAGS_CHECK_AT_PREP)
1151 bad_reason = "PAGE_FLAGS_CHECK_AT_PREP flag(s) set";
1152 else
1153 bad_reason = "PAGE_FLAGS_CHECK_AT_FREE flag(s) set";
Dave Hansenf0b791a2014-01-23 15:52:49 -08001154 }
Johannes Weiner9edad6e2014-12-10 15:44:58 -08001155#ifdef CONFIG_MEMCG
Muchun Song48060832021-04-29 22:56:45 -07001156 if (unlikely(page->memcg_data))
Johannes Weiner9edad6e2014-12-10 15:44:58 -08001157 bad_reason = "page still charged to cgroup";
1158#endif
Wei Yang58b7f112020-06-03 15:58:39 -07001159 return bad_reason;
Mel Gormanbb552ac2016-05-19 17:14:18 -07001160}
1161
Wei Yang58b7f112020-06-03 15:58:39 -07001162static void check_free_page_bad(struct page *page)
1163{
1164 bad_page(page,
1165 page_bad_reason(page, PAGE_FLAGS_CHECK_AT_FREE));
Mel Gormanbb552ac2016-05-19 17:14:18 -07001166}
1167
Wei Yang534fe5e2020-06-03 15:58:36 -07001168static inline int check_free_page(struct page *page)
Mel Gormanbb552ac2016-05-19 17:14:18 -07001169{
Mel Gormanda838d42016-05-19 17:14:21 -07001170 if (likely(page_expected_state(page, PAGE_FLAGS_CHECK_AT_FREE)))
Mel Gormanbb552ac2016-05-19 17:14:18 -07001171 return 0;
Mel Gormanbb552ac2016-05-19 17:14:18 -07001172
1173 /* Something has gone sideways, find it */
Wei Yang0d0c48a2020-06-03 15:58:33 -07001174 check_free_page_bad(page);
Mel Gorman7bfec6f2016-05-19 17:14:15 -07001175 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
1177
Mel Gorman4db75482016-05-19 17:14:32 -07001178static int free_tail_pages_check(struct page *head_page, struct page *page)
1179{
1180 int ret = 1;
1181
1182 /*
1183 * We rely page->lru.next never has bit 0 set, unless the page
1184 * is PageTail(). Let's make sure that's true even for poisoned ->lru.
1185 */
1186 BUILD_BUG_ON((unsigned long)LIST_POISON1 & 1);
1187
1188 if (!IS_ENABLED(CONFIG_DEBUG_VM)) {
1189 ret = 0;
1190 goto out;
1191 }
1192 switch (page - head_page) {
1193 case 1:
Matthew Wilcox4da19842018-06-07 17:08:50 -07001194 /* the first tail page: ->mapping may be compound_mapcount() */
Mel Gorman4db75482016-05-19 17:14:32 -07001195 if (unlikely(compound_mapcount(page))) {
Wei Yang82a32412020-06-03 15:58:29 -07001196 bad_page(page, "nonzero compound_mapcount");
Mel Gorman4db75482016-05-19 17:14:32 -07001197 goto out;
1198 }
1199 break;
1200 case 2:
1201 /*
1202 * the second tail page: ->mapping is
Matthew Wilcoxfa3015b2018-06-07 17:08:42 -07001203 * deferred_list.next -- ignore value.
Mel Gorman4db75482016-05-19 17:14:32 -07001204 */
1205 break;
1206 default:
1207 if (page->mapping != TAIL_MAPPING) {
Wei Yang82a32412020-06-03 15:58:29 -07001208 bad_page(page, "corrupted mapping in tail page");
Mel Gorman4db75482016-05-19 17:14:32 -07001209 goto out;
1210 }
1211 break;
1212 }
1213 if (unlikely(!PageTail(page))) {
Wei Yang82a32412020-06-03 15:58:29 -07001214 bad_page(page, "PageTail not set");
Mel Gorman4db75482016-05-19 17:14:32 -07001215 goto out;
1216 }
1217 if (unlikely(compound_head(page) != head_page)) {
Wei Yang82a32412020-06-03 15:58:29 -07001218 bad_page(page, "compound_head not consistent");
Mel Gorman4db75482016-05-19 17:14:32 -07001219 goto out;
1220 }
1221 ret = 0;
1222out:
1223 page->mapping = NULL;
1224 clear_compound_head(page);
1225 return ret;
1226}
1227
Alexander Potapenko64713842019-07-11 20:59:19 -07001228static void kernel_init_free_pages(struct page *page, int numpages)
1229{
1230 int i;
1231
Qian Cai9e15afa2020-08-06 23:25:54 -07001232 /* s390's use of memset() could override KASAN redzones. */
1233 kasan_disable_current();
Andrey Konovalovaa1ef4d2020-12-22 12:02:17 -08001234 for (i = 0; i < numpages; i++) {
Andrey Konovalovacb35b12021-01-23 21:01:43 -08001235 u8 tag = page_kasan_tag(page + i);
Andrey Konovalovaa1ef4d2020-12-22 12:02:17 -08001236 page_kasan_tag_reset(page + i);
Alexander Potapenko64713842019-07-11 20:59:19 -07001237 clear_highpage(page + i);
Andrey Konovalovacb35b12021-01-23 21:01:43 -08001238 page_kasan_tag_set(page + i, tag);
Andrey Konovalovaa1ef4d2020-12-22 12:02:17 -08001239 }
Qian Cai9e15afa2020-08-06 23:25:54 -07001240 kasan_enable_current();
Alexander Potapenko64713842019-07-11 20:59:19 -07001241}
1242
Mel Gormane2769db2016-05-19 17:14:38 -07001243static __always_inline bool free_pages_prepare(struct page *page,
Andrey Konovalov2c335682021-04-29 22:59:52 -07001244 unsigned int order, bool check_free, fpi_t fpi_flags)
Mel Gormane2769db2016-05-19 17:14:38 -07001245{
1246 int bad = 0;
Andrey Konovalov1bb5eab2021-04-29 23:00:02 -07001247 bool init;
Mel Gormane2769db2016-05-19 17:14:38 -07001248
1249 VM_BUG_ON_PAGE(PageTail(page), page);
1250
1251 trace_mm_page_free(page, order);
Mel Gormane2769db2016-05-19 17:14:38 -07001252
Oscar Salvador79f5f8f2020-10-15 20:07:09 -07001253 if (unlikely(PageHWPoison(page)) && !order) {
1254 /*
1255 * Do not let hwpoison pages hit pcplists/buddy
1256 * Untie memcg state and reset page's owner
1257 */
Roman Gushchin18b2db32020-12-01 13:58:30 -08001258 if (memcg_kmem_enabled() && PageMemcgKmem(page))
Oscar Salvador79f5f8f2020-10-15 20:07:09 -07001259 __memcg_kmem_uncharge_page(page, order);
1260 reset_page_owner(page, order);
1261 return false;
1262 }
1263
Mel Gormane2769db2016-05-19 17:14:38 -07001264 /*
1265 * Check tail pages before head page information is cleared to
1266 * avoid checking PageCompound for order-0 pages.
1267 */
1268 if (unlikely(order)) {
1269 bool compound = PageCompound(page);
1270 int i;
1271
1272 VM_BUG_ON_PAGE(compound && compound_order(page) != order, page);
1273
Kirill A. Shutemov9a73f612016-07-26 15:25:53 -07001274 if (compound)
1275 ClearPageDoubleMap(page);
Mel Gormane2769db2016-05-19 17:14:38 -07001276 for (i = 1; i < (1 << order); i++) {
1277 if (compound)
1278 bad += free_tail_pages_check(page, page + i);
Wei Yang534fe5e2020-06-03 15:58:36 -07001279 if (unlikely(check_free_page(page + i))) {
Mel Gormane2769db2016-05-19 17:14:38 -07001280 bad++;
1281 continue;
1282 }
1283 (page + i)->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
1284 }
1285 }
Minchan Kimbda807d2016-07-26 15:23:05 -07001286 if (PageMappingFlags(page))
Mel Gormane2769db2016-05-19 17:14:38 -07001287 page->mapping = NULL;
Roman Gushchin18b2db32020-12-01 13:58:30 -08001288 if (memcg_kmem_enabled() && PageMemcgKmem(page))
Roman Gushchinf4b00ea2020-04-01 21:06:46 -07001289 __memcg_kmem_uncharge_page(page, order);
Mel Gormane2769db2016-05-19 17:14:38 -07001290 if (check_free)
Wei Yang534fe5e2020-06-03 15:58:36 -07001291 bad += check_free_page(page);
Mel Gormane2769db2016-05-19 17:14:38 -07001292 if (bad)
1293 return false;
1294
1295 page_cpupid_reset_last(page);
1296 page->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
1297 reset_page_owner(page, order);
1298
1299 if (!PageHighMem(page)) {
1300 debug_check_no_locks_freed(page_address(page),
1301 PAGE_SIZE << order);
1302 debug_check_no_obj_freed(page_address(page),
1303 PAGE_SIZE << order);
1304 }
Alexander Potapenko64713842019-07-11 20:59:19 -07001305
Vlastimil Babka8db26a32020-12-14 19:13:34 -08001306 kernel_poison_pages(page, 1 << order);
1307
Qian Cai234fdce2019-10-06 17:58:25 -07001308 /*
Andrey Konovalov1bb5eab2021-04-29 23:00:02 -07001309 * As memory initialization might be integrated into KASAN,
1310 * kasan_free_pages and kernel_init_free_pages must be
1311 * kept together to avoid discrepancies in behavior.
1312 *
Andrey Konovalovf9d79e82021-03-12 21:08:10 -08001313 * With hardware tag-based KASAN, memory tags must be set before the
1314 * page becomes unavailable via debug_pagealloc or arch_free_page.
1315 */
Andrey Konovalov1bb5eab2021-04-29 23:00:02 -07001316 init = want_init_on_free();
1317 if (init && !kasan_has_integrated_init())
1318 kernel_init_free_pages(page, 1 << order);
1319 kasan_free_nondeferred_pages(page, order, init, fpi_flags);
Andrey Konovalovf9d79e82021-03-12 21:08:10 -08001320
1321 /*
Qian Cai234fdce2019-10-06 17:58:25 -07001322 * arch_free_page() can make the page's contents inaccessible. s390
1323 * does this. So nothing which can access the page's contents should
1324 * happen after this.
1325 */
1326 arch_free_page(page, order);
1327
Mike Rapoport77bc7fd2020-12-14 19:10:20 -08001328 debug_pagealloc_unmap_pages(page, 1 << order);
Rick Edgecombed6332692019-04-25 17:11:35 -07001329
Mel Gormane2769db2016-05-19 17:14:38 -07001330 return true;
1331}
Mel Gorman4db75482016-05-19 17:14:32 -07001332
1333#ifdef CONFIG_DEBUG_VM
Vlastimil Babka4462b322019-07-11 20:55:09 -07001334/*
1335 * With DEBUG_VM enabled, order-0 pages are checked immediately when being freed
1336 * to pcp lists. With debug_pagealloc also enabled, they are also rechecked when
1337 * moved from pcp lists to free lists.
1338 */
1339static bool free_pcp_prepare(struct page *page)
Mel Gorman4db75482016-05-19 17:14:32 -07001340{
Andrey Konovalov2c335682021-04-29 22:59:52 -07001341 return free_pages_prepare(page, 0, true, FPI_NONE);
Mel Gorman4db75482016-05-19 17:14:32 -07001342}
1343
Vlastimil Babka4462b322019-07-11 20:55:09 -07001344static bool bulkfree_pcp_prepare(struct page *page)
Mel Gorman4db75482016-05-19 17:14:32 -07001345{
Vlastimil Babka8e57f8a2020-01-13 16:29:20 -08001346 if (debug_pagealloc_enabled_static())
Wei Yang534fe5e2020-06-03 15:58:36 -07001347 return check_free_page(page);
Vlastimil Babka4462b322019-07-11 20:55:09 -07001348 else
1349 return false;
Mel Gorman4db75482016-05-19 17:14:32 -07001350}
1351#else
Vlastimil Babka4462b322019-07-11 20:55:09 -07001352/*
1353 * With DEBUG_VM disabled, order-0 pages being freed are checked only when
1354 * moving from pcp lists to free list in order to reduce overhead. With
1355 * debug_pagealloc enabled, they are checked also immediately when being freed
1356 * to the pcp lists.
1357 */
Mel Gorman4db75482016-05-19 17:14:32 -07001358static bool free_pcp_prepare(struct page *page)
1359{
Vlastimil Babka8e57f8a2020-01-13 16:29:20 -08001360 if (debug_pagealloc_enabled_static())
Andrey Konovalov2c335682021-04-29 22:59:52 -07001361 return free_pages_prepare(page, 0, true, FPI_NONE);
Vlastimil Babka4462b322019-07-11 20:55:09 -07001362 else
Andrey Konovalov2c335682021-04-29 22:59:52 -07001363 return free_pages_prepare(page, 0, false, FPI_NONE);
Mel Gorman4db75482016-05-19 17:14:32 -07001364}
1365
1366static bool bulkfree_pcp_prepare(struct page *page)
1367{
Wei Yang534fe5e2020-06-03 15:58:36 -07001368 return check_free_page(page);
Mel Gorman4db75482016-05-19 17:14:32 -07001369}
1370#endif /* CONFIG_DEBUG_VM */
1371
Aaron Lu97334162018-04-05 16:24:14 -07001372static inline void prefetch_buddy(struct page *page)
1373{
1374 unsigned long pfn = page_to_pfn(page);
1375 unsigned long buddy_pfn = __find_buddy_pfn(pfn, 0);
1376 struct page *buddy = page + (buddy_pfn - pfn);
1377
1378 prefetch(buddy);
1379}
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381/*
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001382 * Frees a number of pages from the PCP lists
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 * Assumes all pages on list are in same zone, and of same order.
Renaud Lienhart207f36e2005-09-10 00:26:59 -07001384 * count is the number of pages to free.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 *
1386 * If the zone was previously in an "all pages pinned" state then look to
1387 * see if this freeing clears that state.
1388 *
1389 * And clear the zone's pages_scanned counter, to hold off the "all pages are
1390 * pinned" detection logic.
1391 */
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001392static void free_pcppages_bulk(struct zone *zone, int count,
1393 struct per_cpu_pages *pcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001395 int migratetype = 0;
Mel Gormana6f9edd62009-09-21 17:03:20 -07001396 int batch_free = 0;
Vlastimil Babka5c3ad2e2020-12-14 19:10:50 -08001397 int prefetch_nr = READ_ONCE(pcp->batch);
Mel Gorman37779992016-05-19 17:13:58 -07001398 bool isolated_pageblocks;
Aaron Lu0a5f4e52018-04-05 16:24:10 -07001399 struct page *page, *tmp;
1400 LIST_HEAD(head);
Mel Gormanf2260e62009-06-16 15:32:13 -07001401
Charan Teja Reddy88e8ac12020-08-20 17:42:27 -07001402 /*
1403 * Ensure proper count is passed which otherwise would stuck in the
1404 * below while (list_empty(list)) loop.
1405 */
1406 count = min(pcp->count, count);
Mel Gormane5b31ac2016-05-19 17:14:24 -07001407 while (count) {
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001408 struct list_head *list;
Nick Piggin48db57f2006-01-08 01:00:42 -08001409
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001410 /*
Mel Gormana6f9edd62009-09-21 17:03:20 -07001411 * Remove pages from lists in a round-robin fashion. A
1412 * batch_free count is maintained that is incremented when an
1413 * empty list is encountered. This is so more pages are freed
1414 * off fuller lists instead of spinning excessively around empty
1415 * lists
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001416 */
1417 do {
Mel Gormana6f9edd62009-09-21 17:03:20 -07001418 batch_free++;
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001419 if (++migratetype == MIGRATE_PCPTYPES)
1420 migratetype = 0;
1421 list = &pcp->lists[migratetype];
1422 } while (list_empty(list));
1423
Namhyung Kim1d168712011-03-22 16:32:45 -07001424 /* This is the only non-empty list. Free them all. */
1425 if (batch_free == MIGRATE_PCPTYPES)
Mel Gormane5b31ac2016-05-19 17:14:24 -07001426 batch_free = count;
Namhyung Kim1d168712011-03-22 16:32:45 -07001427
Mel Gormana6f9edd62009-09-21 17:03:20 -07001428 do {
Geliang Tanga16601c2016-01-14 15:20:30 -08001429 page = list_last_entry(list, struct page, lru);
Aaron Lu0a5f4e52018-04-05 16:24:10 -07001430 /* must delete to avoid corrupting pcp list */
Mel Gormana6f9edd62009-09-21 17:03:20 -07001431 list_del(&page->lru);
Aaron Lu77ba9062018-04-05 16:24:06 -07001432 pcp->count--;
Vlastimil Babkaaa016d12015-09-08 15:01:22 -07001433
Mel Gorman4db75482016-05-19 17:14:32 -07001434 if (bulkfree_pcp_prepare(page))
1435 continue;
1436
Aaron Lu0a5f4e52018-04-05 16:24:10 -07001437 list_add_tail(&page->lru, &head);
Aaron Lu97334162018-04-05 16:24:14 -07001438
1439 /*
1440 * We are going to put the page back to the global
1441 * pool, prefetch its buddy to speed up later access
1442 * under zone->lock. It is believed the overhead of
1443 * an additional test and calculating buddy_pfn here
1444 * can be offset by reduced memory latency later. To
1445 * avoid excessive prefetching due to large count, only
1446 * prefetch buddy for the first pcp->batch nr of pages.
1447 */
Vlastimil Babka5c3ad2e2020-12-14 19:10:50 -08001448 if (prefetch_nr) {
Aaron Lu97334162018-04-05 16:24:14 -07001449 prefetch_buddy(page);
Vlastimil Babka5c3ad2e2020-12-14 19:10:50 -08001450 prefetch_nr--;
1451 }
Mel Gormane5b31ac2016-05-19 17:14:24 -07001452 } while (--count && --batch_free && !list_empty(list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 }
Aaron Lu0a5f4e52018-04-05 16:24:10 -07001454
1455 spin_lock(&zone->lock);
1456 isolated_pageblocks = has_isolate_pageblock(zone);
1457
1458 /*
1459 * Use safe version since after __free_one_page(),
1460 * page->lru.next will not point to original list.
1461 */
1462 list_for_each_entry_safe(page, tmp, &head, lru) {
1463 int mt = get_pcppage_migratetype(page);
1464 /* MIGRATE_ISOLATE page should not go to pcplists */
1465 VM_BUG_ON_PAGE(is_migrate_isolate(mt), page);
1466 /* Pageblock could have been isolated meanwhile */
1467 if (unlikely(isolated_pageblocks))
1468 mt = get_pageblock_migratetype(page);
1469
David Hildenbrandf04a5d52020-10-15 20:09:20 -07001470 __free_one_page(page, page_to_pfn(page), zone, 0, mt, FPI_NONE);
Aaron Lu0a5f4e52018-04-05 16:24:10 -07001471 trace_mm_page_pcpu_drain(page, 0, mt);
1472 }
Mel Gormand34b0732017-04-20 14:37:43 -07001473 spin_unlock(&zone->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474}
1475
Mel Gormandc4b0ca2014-06-04 16:10:17 -07001476static void free_one_page(struct zone *zone,
1477 struct page *page, unsigned long pfn,
Mel Gorman7aeb09f2014-06-04 16:10:21 -07001478 unsigned int order,
David Hildenbrand7fef4312020-10-15 20:09:35 -07001479 int migratetype, fpi_t fpi_flags)
Nick Piggin48db57f2006-01-08 01:00:42 -08001480{
Mel Gormand34b0732017-04-20 14:37:43 -07001481 spin_lock(&zone->lock);
Joonsoo Kimad53f922014-11-13 15:19:11 -08001482 if (unlikely(has_isolate_pageblock(zone) ||
1483 is_migrate_isolate(migratetype))) {
1484 migratetype = get_pfnblock_migratetype(page, pfn);
Joonsoo Kimad53f922014-11-13 15:19:11 -08001485 }
David Hildenbrand7fef4312020-10-15 20:09:35 -07001486 __free_one_page(page, pfn, zone, order, migratetype, fpi_flags);
Mel Gormand34b0732017-04-20 14:37:43 -07001487 spin_unlock(&zone->lock);
Nick Piggin48db57f2006-01-08 01:00:42 -08001488}
1489
Robin Holt1e8ce832015-06-30 14:56:45 -07001490static void __meminit __init_single_page(struct page *page, unsigned long pfn,
Pavel Tatashind0dc12e2018-04-05 16:23:00 -07001491 unsigned long zone, int nid)
Robin Holt1e8ce832015-06-30 14:56:45 -07001492{
Pavel Tatashind0dc12e2018-04-05 16:23:00 -07001493 mm_zero_struct_page(page);
Robin Holt1e8ce832015-06-30 14:56:45 -07001494 set_page_links(page, zone, nid, pfn);
Robin Holt1e8ce832015-06-30 14:56:45 -07001495 init_page_count(page);
1496 page_mapcount_reset(page);
1497 page_cpupid_reset_last(page);
Andrey Konovalov2813b9c2018-12-28 00:30:57 -08001498 page_kasan_tag_reset(page);
Robin Holt1e8ce832015-06-30 14:56:45 -07001499
Robin Holt1e8ce832015-06-30 14:56:45 -07001500 INIT_LIST_HEAD(&page->lru);
1501#ifdef WANT_PAGE_VIRTUAL
1502 /* The shift won't overflow because ZONE_NORMAL is below 4G. */
1503 if (!is_highmem_idx(zone))
1504 set_page_address(page, __va(pfn << PAGE_SHIFT));
1505#endif
1506}
1507
Mel Gorman7e18adb2015-06-30 14:57:05 -07001508#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
Arnd Bergmann57148a62017-10-03 16:15:10 -07001509static void __meminit init_reserved_page(unsigned long pfn)
Mel Gorman7e18adb2015-06-30 14:57:05 -07001510{
1511 pg_data_t *pgdat;
1512 int nid, zid;
1513
1514 if (!early_page_uninitialised(pfn))
1515 return;
1516
1517 nid = early_pfn_to_nid(pfn);
1518 pgdat = NODE_DATA(nid);
1519
1520 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
1521 struct zone *zone = &pgdat->node_zones[zid];
1522
1523 if (pfn >= zone->zone_start_pfn && pfn < zone_end_pfn(zone))
1524 break;
1525 }
Pavel Tatashind0dc12e2018-04-05 16:23:00 -07001526 __init_single_page(pfn_to_page(pfn), pfn, zid, nid);
Mel Gorman7e18adb2015-06-30 14:57:05 -07001527}
1528#else
1529static inline void init_reserved_page(unsigned long pfn)
1530{
1531}
1532#endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
1533
Nathan Zimmer92923ca2015-06-30 14:56:48 -07001534/*
1535 * Initialised pages do not have PageReserved set. This function is
1536 * called for each range allocated by the bootmem allocator and
1537 * marks the pages PageReserved. The remaining valid pages are later
1538 * sent to the buddy page allocator.
1539 */
Stefan Bader4b50bcc2016-05-20 16:58:38 -07001540void __meminit reserve_bootmem_region(phys_addr_t start, phys_addr_t end)
Nathan Zimmer92923ca2015-06-30 14:56:48 -07001541{
1542 unsigned long start_pfn = PFN_DOWN(start);
1543 unsigned long end_pfn = PFN_UP(end);
1544
Mel Gorman7e18adb2015-06-30 14:57:05 -07001545 for (; start_pfn < end_pfn; start_pfn++) {
1546 if (pfn_valid(start_pfn)) {
1547 struct page *page = pfn_to_page(start_pfn);
1548
1549 init_reserved_page(start_pfn);
Kirill A. Shutemov1d798ca2015-11-06 16:29:54 -08001550
1551 /* Avoid false-positive PageTail() */
1552 INIT_LIST_HEAD(&page->lru);
1553
Alexander Duyckd483da52018-10-26 15:07:48 -07001554 /*
1555 * no need for atomic set_bit because the struct
1556 * page is not visible yet so nobody should
1557 * access it yet.
1558 */
1559 __SetPageReserved(page);
Mel Gorman7e18adb2015-06-30 14:57:05 -07001560 }
1561 }
Nathan Zimmer92923ca2015-06-30 14:56:48 -07001562}
1563
David Hildenbrand7fef4312020-10-15 20:09:35 -07001564static void __free_pages_ok(struct page *page, unsigned int order,
1565 fpi_t fpi_flags)
KOSAKI Motohiroec95f532010-05-24 14:32:38 -07001566{
Mel Gormand34b0732017-04-20 14:37:43 -07001567 unsigned long flags;
Minchan Kim95e34412012-10-08 16:32:11 -07001568 int migratetype;
Mel Gormandc4b0ca2014-06-04 16:10:17 -07001569 unsigned long pfn = page_to_pfn(page);
KOSAKI Motohiroec95f532010-05-24 14:32:38 -07001570
Andrey Konovalov2c335682021-04-29 22:59:52 -07001571 if (!free_pages_prepare(page, order, true, fpi_flags))
KOSAKI Motohiroec95f532010-05-24 14:32:38 -07001572 return;
1573
Mel Gormancfc47a22014-06-04 16:10:19 -07001574 migratetype = get_pfnblock_migratetype(page, pfn);
Mel Gormand34b0732017-04-20 14:37:43 -07001575 local_irq_save(flags);
1576 __count_vm_events(PGFREE, 1 << order);
David Hildenbrand7fef4312020-10-15 20:09:35 -07001577 free_one_page(page_zone(page), page, pfn, order, migratetype,
1578 fpi_flags);
Mel Gormand34b0732017-04-20 14:37:43 -07001579 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580}
1581
Arun KSa9cd4102019-03-05 15:42:14 -08001582void __free_pages_core(struct page *page, unsigned int order)
David Howellsa226f6c2006-01-06 00:11:08 -08001583{
Johannes Weinerc3993072012-01-10 15:08:10 -08001584 unsigned int nr_pages = 1 << order;
Yinghai Lue2d0bd22013-09-11 14:20:37 -07001585 struct page *p = page;
Johannes Weinerc3993072012-01-10 15:08:10 -08001586 unsigned int loop;
David Howellsa226f6c2006-01-06 00:11:08 -08001587
David Hildenbrand7fef4312020-10-15 20:09:35 -07001588 /*
1589 * When initializing the memmap, __init_single_page() sets the refcount
1590 * of all pages to 1 ("allocated"/"not free"). We have to set the
1591 * refcount of all involved pages to 0.
1592 */
Yinghai Lue2d0bd22013-09-11 14:20:37 -07001593 prefetchw(p);
1594 for (loop = 0; loop < (nr_pages - 1); loop++, p++) {
1595 prefetchw(p + 1);
Johannes Weinerc3993072012-01-10 15:08:10 -08001596 __ClearPageReserved(p);
1597 set_page_count(p, 0);
David Howellsa226f6c2006-01-06 00:11:08 -08001598 }
Yinghai Lue2d0bd22013-09-11 14:20:37 -07001599 __ClearPageReserved(p);
1600 set_page_count(p, 0);
Johannes Weinerc3993072012-01-10 15:08:10 -08001601
Arun KS9705bea2018-12-28 00:34:24 -08001602 atomic_long_add(nr_pages, &page_zone(page)->managed_pages);
David Hildenbrand7fef4312020-10-15 20:09:35 -07001603
1604 /*
1605 * Bypass PCP and place fresh pages right to the tail, primarily
1606 * relevant for memory onlining.
1607 */
Andrey Konovalov2c335682021-04-29 22:59:52 -07001608 __free_pages_ok(page, order, FPI_TO_TAIL | FPI_SKIP_KASAN_POISON);
David Howellsa226f6c2006-01-06 00:11:08 -08001609}
1610
Mike Rapoport3f08a302020-06-03 15:57:02 -07001611#ifdef CONFIG_NEED_MULTIPLE_NODES
Mel Gorman7ace9912015-08-06 15:46:13 -07001612
Mike Rapoport03e92a52020-12-14 19:09:32 -08001613/*
1614 * During memory init memblocks map pfns to nids. The search is expensive and
1615 * this caches recent lookups. The implementation of __early_pfn_to_nid
1616 * treats start/end as pfns.
1617 */
1618struct mminit_pfnnid_cache {
1619 unsigned long last_start;
1620 unsigned long last_end;
1621 int last_nid;
1622};
Mel Gorman75a592a2015-06-30 14:56:59 -07001623
Mike Rapoport03e92a52020-12-14 19:09:32 -08001624static struct mminit_pfnnid_cache early_pfnnid_cache __meminitdata;
Mike Rapoport6f24fbd2020-06-03 15:56:57 -07001625
1626/*
1627 * Required by SPARSEMEM. Given a PFN, return what node the PFN is on.
1628 */
Mike Rapoport03e92a52020-12-14 19:09:32 -08001629static int __meminit __early_pfn_to_nid(unsigned long pfn,
Mike Rapoport6f24fbd2020-06-03 15:56:57 -07001630 struct mminit_pfnnid_cache *state)
1631{
1632 unsigned long start_pfn, end_pfn;
1633 int nid;
1634
1635 if (state->last_start <= pfn && pfn < state->last_end)
1636 return state->last_nid;
1637
1638 nid = memblock_search_pfn_nid(pfn, &start_pfn, &end_pfn);
1639 if (nid != NUMA_NO_NODE) {
1640 state->last_start = start_pfn;
1641 state->last_end = end_pfn;
1642 state->last_nid = nid;
1643 }
1644
1645 return nid;
1646}
Mike Rapoport6f24fbd2020-06-03 15:56:57 -07001647
Mel Gorman75a592a2015-06-30 14:56:59 -07001648int __meminit early_pfn_to_nid(unsigned long pfn)
1649{
Mel Gorman7ace9912015-08-06 15:46:13 -07001650 static DEFINE_SPINLOCK(early_pfn_lock);
Mel Gorman75a592a2015-06-30 14:56:59 -07001651 int nid;
1652
Mel Gorman7ace9912015-08-06 15:46:13 -07001653 spin_lock(&early_pfn_lock);
Mel Gorman75a592a2015-06-30 14:56:59 -07001654 nid = __early_pfn_to_nid(pfn, &early_pfnnid_cache);
Mel Gorman7ace9912015-08-06 15:46:13 -07001655 if (nid < 0)
Mel Gormane4568d32016-07-14 12:07:20 -07001656 nid = first_online_node;
Mel Gorman7ace9912015-08-06 15:46:13 -07001657 spin_unlock(&early_pfn_lock);
1658
1659 return nid;
Mel Gorman75a592a2015-06-30 14:56:59 -07001660}
Mike Rapoport3f08a302020-06-03 15:57:02 -07001661#endif /* CONFIG_NEED_MULTIPLE_NODES */
Mel Gorman75a592a2015-06-30 14:56:59 -07001662
Mike Rapoport7c2ee342018-10-30 15:09:36 -07001663void __init memblock_free_pages(struct page *page, unsigned long pfn,
Mel Gorman3a80a7f2015-06-30 14:57:02 -07001664 unsigned int order)
1665{
1666 if (early_page_uninitialised(pfn))
1667 return;
Arun KSa9cd4102019-03-05 15:42:14 -08001668 __free_pages_core(page, order);
Mel Gorman3a80a7f2015-06-30 14:57:02 -07001669}
1670
Joonsoo Kim7cf91a92016-03-15 14:57:51 -07001671/*
1672 * Check that the whole (or subset of) a pageblock given by the interval of
1673 * [start_pfn, end_pfn) is valid and within the same zone, before scanning it
1674 * with the migration of free compaction scanner. The scanners then need to
1675 * use only pfn_valid_within() check for arches that allow holes within
1676 * pageblocks.
1677 *
1678 * Return struct page pointer of start_pfn, or NULL if checks were not passed.
1679 *
1680 * It's possible on some configurations to have a setup like node0 node1 node0
1681 * i.e. it's possible that all pages within a zones range of pages do not
1682 * belong to a single zone. We assume that a border between node0 and node1
1683 * can occur within a single pageblock, but not a node0 node1 node0
1684 * interleaving within a single pageblock. It is therefore sufficient to check
1685 * the first and last page of a pageblock and avoid checking each individual
1686 * page in a pageblock.
1687 */
1688struct page *__pageblock_pfn_to_page(unsigned long start_pfn,
1689 unsigned long end_pfn, struct zone *zone)
1690{
1691 struct page *start_page;
1692 struct page *end_page;
1693
1694 /* end_pfn is one past the range we are checking */
1695 end_pfn--;
1696
1697 if (!pfn_valid(start_pfn) || !pfn_valid(end_pfn))
1698 return NULL;
1699
Michal Hocko2d070ea2017-07-06 15:37:56 -07001700 start_page = pfn_to_online_page(start_pfn);
1701 if (!start_page)
1702 return NULL;
Joonsoo Kim7cf91a92016-03-15 14:57:51 -07001703
1704 if (page_zone(start_page) != zone)
1705 return NULL;
1706
1707 end_page = pfn_to_page(end_pfn);
1708
1709 /* This gives a shorter code than deriving page_zone(end_page) */
1710 if (page_zone_id(start_page) != page_zone_id(end_page))
1711 return NULL;
1712
1713 return start_page;
1714}
1715
1716void set_zone_contiguous(struct zone *zone)
1717{
1718 unsigned long block_start_pfn = zone->zone_start_pfn;
1719 unsigned long block_end_pfn;
1720
1721 block_end_pfn = ALIGN(block_start_pfn + 1, pageblock_nr_pages);
1722 for (; block_start_pfn < zone_end_pfn(zone);
1723 block_start_pfn = block_end_pfn,
1724 block_end_pfn += pageblock_nr_pages) {
1725
1726 block_end_pfn = min(block_end_pfn, zone_end_pfn(zone));
1727
1728 if (!__pageblock_pfn_to_page(block_start_pfn,
1729 block_end_pfn, zone))
1730 return;
David Hildenbrande84fe992020-05-07 18:35:46 -07001731 cond_resched();
Joonsoo Kim7cf91a92016-03-15 14:57:51 -07001732 }
1733
1734 /* We confirm that there is no hole */
1735 zone->contiguous = true;
1736}
1737
1738void clear_zone_contiguous(struct zone *zone)
1739{
1740 zone->contiguous = false;
1741}
1742
Mel Gorman7e18adb2015-06-30 14:57:05 -07001743#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001744static void __init deferred_free_range(unsigned long pfn,
1745 unsigned long nr_pages)
Mel Gormana4de83d2015-06-30 14:57:16 -07001746{
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001747 struct page *page;
1748 unsigned long i;
Mel Gormana4de83d2015-06-30 14:57:16 -07001749
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001750 if (!nr_pages)
Mel Gormana4de83d2015-06-30 14:57:16 -07001751 return;
1752
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001753 page = pfn_to_page(pfn);
1754
Mel Gormana4de83d2015-06-30 14:57:16 -07001755 /* Free a large naturally-aligned chunk if possible */
Xishi Qiue7801492016-10-07 16:58:09 -07001756 if (nr_pages == pageblock_nr_pages &&
1757 (pfn & (pageblock_nr_pages - 1)) == 0) {
Mel Gormanac5d2532015-06-30 14:57:20 -07001758 set_pageblock_migratetype(page, MIGRATE_MOVABLE);
Arun KSa9cd4102019-03-05 15:42:14 -08001759 __free_pages_core(page, pageblock_order);
Mel Gormana4de83d2015-06-30 14:57:16 -07001760 return;
1761 }
1762
Xishi Qiue7801492016-10-07 16:58:09 -07001763 for (i = 0; i < nr_pages; i++, page++, pfn++) {
1764 if ((pfn & (pageblock_nr_pages - 1)) == 0)
1765 set_pageblock_migratetype(page, MIGRATE_MOVABLE);
Arun KSa9cd4102019-03-05 15:42:14 -08001766 __free_pages_core(page, 0);
Xishi Qiue7801492016-10-07 16:58:09 -07001767 }
Mel Gormana4de83d2015-06-30 14:57:16 -07001768}
1769
Nicolai Stanged3cd1312015-08-06 15:46:16 -07001770/* Completion tracking for deferred_init_memmap() threads */
1771static atomic_t pgdat_init_n_undone __initdata;
1772static __initdata DECLARE_COMPLETION(pgdat_init_all_done_comp);
1773
1774static inline void __init pgdat_init_report_one_done(void)
1775{
1776 if (atomic_dec_and_test(&pgdat_init_n_undone))
1777 complete(&pgdat_init_all_done_comp);
1778}
Mel Gorman0e1cc952015-06-30 14:57:27 -07001779
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001780/*
Pavel Tatashin80b1f412018-01-31 16:16:30 -08001781 * Returns true if page needs to be initialized or freed to buddy allocator.
1782 *
1783 * First we check if pfn is valid on architectures where it is possible to have
1784 * holes within pageblock_nr_pages. On systems where it is not possible, this
1785 * function is optimized out.
1786 *
1787 * Then, we check if a current large page is valid by only checking the validity
1788 * of the head pfn.
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001789 */
Alexander Duyck56ec43d2019-05-13 17:21:13 -07001790static inline bool __init deferred_pfn_valid(unsigned long pfn)
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001791{
Pavel Tatashin80b1f412018-01-31 16:16:30 -08001792 if (!pfn_valid_within(pfn))
1793 return false;
1794 if (!(pfn & (pageblock_nr_pages - 1)) && !pfn_valid(pfn))
1795 return false;
Pavel Tatashin80b1f412018-01-31 16:16:30 -08001796 return true;
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001797}
1798
Pavel Tatashin80b1f412018-01-31 16:16:30 -08001799/*
1800 * Free pages to buddy allocator. Try to free aligned pages in
1801 * pageblock_nr_pages sizes.
1802 */
Alexander Duyck56ec43d2019-05-13 17:21:13 -07001803static void __init deferred_free_pages(unsigned long pfn,
Pavel Tatashin80b1f412018-01-31 16:16:30 -08001804 unsigned long end_pfn)
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001805{
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001806 unsigned long nr_pgmask = pageblock_nr_pages - 1;
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001807 unsigned long nr_free = 0;
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001808
Pavel Tatashin80b1f412018-01-31 16:16:30 -08001809 for (; pfn < end_pfn; pfn++) {
Alexander Duyck56ec43d2019-05-13 17:21:13 -07001810 if (!deferred_pfn_valid(pfn)) {
Pavel Tatashin80b1f412018-01-31 16:16:30 -08001811 deferred_free_range(pfn - nr_free, nr_free);
1812 nr_free = 0;
1813 } else if (!(pfn & nr_pgmask)) {
1814 deferred_free_range(pfn - nr_free, nr_free);
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001815 nr_free = 1;
Pavel Tatashin80b1f412018-01-31 16:16:30 -08001816 } else {
1817 nr_free++;
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001818 }
1819 }
1820 /* Free the last block of pages to allocator */
Pavel Tatashin80b1f412018-01-31 16:16:30 -08001821 deferred_free_range(pfn - nr_free, nr_free);
1822}
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001823
Pavel Tatashin80b1f412018-01-31 16:16:30 -08001824/*
1825 * Initialize struct pages. We minimize pfn page lookups and scheduler checks
1826 * by performing it only once every pageblock_nr_pages.
1827 * Return number of pages initialized.
1828 */
Alexander Duyck56ec43d2019-05-13 17:21:13 -07001829static unsigned long __init deferred_init_pages(struct zone *zone,
Pavel Tatashin80b1f412018-01-31 16:16:30 -08001830 unsigned long pfn,
1831 unsigned long end_pfn)
1832{
Pavel Tatashin80b1f412018-01-31 16:16:30 -08001833 unsigned long nr_pgmask = pageblock_nr_pages - 1;
Alexander Duyck56ec43d2019-05-13 17:21:13 -07001834 int nid = zone_to_nid(zone);
Pavel Tatashin80b1f412018-01-31 16:16:30 -08001835 unsigned long nr_pages = 0;
Alexander Duyck56ec43d2019-05-13 17:21:13 -07001836 int zid = zone_idx(zone);
Pavel Tatashin80b1f412018-01-31 16:16:30 -08001837 struct page *page = NULL;
1838
1839 for (; pfn < end_pfn; pfn++) {
Alexander Duyck56ec43d2019-05-13 17:21:13 -07001840 if (!deferred_pfn_valid(pfn)) {
Pavel Tatashin80b1f412018-01-31 16:16:30 -08001841 page = NULL;
1842 continue;
1843 } else if (!page || !(pfn & nr_pgmask)) {
1844 page = pfn_to_page(pfn);
Pavel Tatashin80b1f412018-01-31 16:16:30 -08001845 } else {
1846 page++;
1847 }
Pavel Tatashind0dc12e2018-04-05 16:23:00 -07001848 __init_single_page(page, pfn, zid, nid);
Pavel Tatashin80b1f412018-01-31 16:16:30 -08001849 nr_pages++;
1850 }
1851 return (nr_pages);
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001852}
1853
Alexander Duyck0e56aca2019-05-13 17:21:20 -07001854/*
1855 * This function is meant to pre-load the iterator for the zone init.
1856 * Specifically it walks through the ranges until we are caught up to the
1857 * first_init_pfn value and exits there. If we never encounter the value we
1858 * return false indicating there are no valid ranges left.
1859 */
1860static bool __init
1861deferred_init_mem_pfn_range_in_zone(u64 *i, struct zone *zone,
1862 unsigned long *spfn, unsigned long *epfn,
1863 unsigned long first_init_pfn)
1864{
1865 u64 j;
1866
1867 /*
1868 * Start out by walking through the ranges in this zone that have
1869 * already been initialized. We don't need to do anything with them
1870 * so we just need to flush them out of the system.
1871 */
1872 for_each_free_mem_pfn_range_in_zone(j, zone, spfn, epfn) {
1873 if (*epfn <= first_init_pfn)
1874 continue;
1875 if (*spfn < first_init_pfn)
1876 *spfn = first_init_pfn;
1877 *i = j;
1878 return true;
1879 }
1880
1881 return false;
1882}
1883
1884/*
1885 * Initialize and free pages. We do it in two loops: first we initialize
1886 * struct page, then free to buddy allocator, because while we are
1887 * freeing pages we can access pages that are ahead (computing buddy
1888 * page in __free_one_page()).
1889 *
1890 * In order to try and keep some memory in the cache we have the loop
1891 * broken along max page order boundaries. This way we will not cause
1892 * any issues with the buddy page computation.
1893 */
1894static unsigned long __init
1895deferred_init_maxorder(u64 *i, struct zone *zone, unsigned long *start_pfn,
1896 unsigned long *end_pfn)
1897{
1898 unsigned long mo_pfn = ALIGN(*start_pfn + 1, MAX_ORDER_NR_PAGES);
1899 unsigned long spfn = *start_pfn, epfn = *end_pfn;
1900 unsigned long nr_pages = 0;
1901 u64 j = *i;
1902
1903 /* First we loop through and initialize the page values */
1904 for_each_free_mem_pfn_range_in_zone_from(j, zone, start_pfn, end_pfn) {
1905 unsigned long t;
1906
1907 if (mo_pfn <= *start_pfn)
1908 break;
1909
1910 t = min(mo_pfn, *end_pfn);
1911 nr_pages += deferred_init_pages(zone, *start_pfn, t);
1912
1913 if (mo_pfn < *end_pfn) {
1914 *start_pfn = mo_pfn;
1915 break;
1916 }
1917 }
1918
1919 /* Reset values and now loop through freeing pages as needed */
1920 swap(j, *i);
1921
1922 for_each_free_mem_pfn_range_in_zone_from(j, zone, &spfn, &epfn) {
1923 unsigned long t;
1924
1925 if (mo_pfn <= spfn)
1926 break;
1927
1928 t = min(mo_pfn, epfn);
1929 deferred_free_pages(spfn, t);
1930
1931 if (mo_pfn <= epfn)
1932 break;
1933 }
1934
1935 return nr_pages;
1936}
1937
Daniel Jordane4443142020-06-03 15:59:51 -07001938static void __init
1939deferred_init_memmap_chunk(unsigned long start_pfn, unsigned long end_pfn,
1940 void *arg)
1941{
1942 unsigned long spfn, epfn;
1943 struct zone *zone = arg;
1944 u64 i;
1945
1946 deferred_init_mem_pfn_range_in_zone(&i, zone, &spfn, &epfn, start_pfn);
1947
1948 /*
1949 * Initialize and free pages in MAX_ORDER sized increments so that we
1950 * can avoid introducing any issues with the buddy allocator.
1951 */
1952 while (spfn < end_pfn) {
1953 deferred_init_maxorder(&i, zone, &spfn, &epfn);
1954 cond_resched();
1955 }
1956}
1957
Daniel Jordanecd09652020-06-03 15:59:55 -07001958/* An arch may override for more concurrency. */
1959__weak int __init
1960deferred_page_init_max_threads(const struct cpumask *node_cpumask)
1961{
1962 return 1;
1963}
1964
Mel Gorman7e18adb2015-06-30 14:57:05 -07001965/* Initialise remaining memory on a node */
Mel Gorman0e1cc952015-06-30 14:57:27 -07001966static int __init deferred_init_memmap(void *data)
Mel Gorman7e18adb2015-06-30 14:57:05 -07001967{
Mel Gorman0e1cc952015-06-30 14:57:27 -07001968 pg_data_t *pgdat = data;
Mel Gorman0e1cc952015-06-30 14:57:27 -07001969 const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
Daniel Jordan89c7c402020-06-03 15:59:47 -07001970 unsigned long spfn = 0, epfn = 0;
Alexander Duyck0e56aca2019-05-13 17:21:20 -07001971 unsigned long first_init_pfn, flags;
1972 unsigned long start = jiffies;
1973 struct zone *zone;
Daniel Jordane4443142020-06-03 15:59:51 -07001974 int zid, max_threads;
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001975 u64 i;
Mel Gorman7e18adb2015-06-30 14:57:05 -07001976
Mel Gorman0e1cc952015-06-30 14:57:27 -07001977 /* Bind memory initialisation thread to a local node if possible */
1978 if (!cpumask_empty(cpumask))
1979 set_cpus_allowed_ptr(current, cpumask);
Mel Gorman7e18adb2015-06-30 14:57:05 -07001980
Pavel Tatashin3a2d7fa2018-04-05 16:22:27 -07001981 pgdat_resize_lock(pgdat, &flags);
1982 first_init_pfn = pgdat->first_deferred_pfn;
1983 if (first_init_pfn == ULONG_MAX) {
1984 pgdat_resize_unlock(pgdat, &flags);
1985 pgdat_init_report_one_done();
1986 return 0;
1987 }
1988
Mel Gorman7e18adb2015-06-30 14:57:05 -07001989 /* Sanity check boundaries */
1990 BUG_ON(pgdat->first_deferred_pfn < pgdat->node_start_pfn);
1991 BUG_ON(pgdat->first_deferred_pfn > pgdat_end_pfn(pgdat));
1992 pgdat->first_deferred_pfn = ULONG_MAX;
1993
Pavel Tatashin3d060852020-06-03 15:59:24 -07001994 /*
1995 * Once we unlock here, the zone cannot be grown anymore, thus if an
1996 * interrupt thread must allocate this early in boot, zone must be
1997 * pre-grown prior to start of deferred page initialization.
1998 */
1999 pgdat_resize_unlock(pgdat, &flags);
2000
Mel Gorman7e18adb2015-06-30 14:57:05 -07002001 /* Only the highest zone is deferred so find it */
2002 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
2003 zone = pgdat->node_zones + zid;
2004 if (first_init_pfn < zone_end_pfn(zone))
2005 break;
2006 }
Alexander Duyck0e56aca2019-05-13 17:21:20 -07002007
2008 /* If the zone is empty somebody else may have cleared out the zone */
2009 if (!deferred_init_mem_pfn_range_in_zone(&i, zone, &spfn, &epfn,
2010 first_init_pfn))
2011 goto zone_empty;
Mel Gorman7e18adb2015-06-30 14:57:05 -07002012
Daniel Jordanecd09652020-06-03 15:59:55 -07002013 max_threads = deferred_page_init_max_threads(cpumask);
Mel Gorman7e18adb2015-06-30 14:57:05 -07002014
Daniel Jordan117003c2020-06-03 15:59:20 -07002015 while (spfn < epfn) {
Daniel Jordane4443142020-06-03 15:59:51 -07002016 unsigned long epfn_align = ALIGN(epfn, PAGES_PER_SECTION);
2017 struct padata_mt_job job = {
2018 .thread_fn = deferred_init_memmap_chunk,
2019 .fn_arg = zone,
2020 .start = spfn,
2021 .size = epfn_align - spfn,
2022 .align = PAGES_PER_SECTION,
2023 .min_chunk = PAGES_PER_SECTION,
2024 .max_threads = max_threads,
2025 };
2026
2027 padata_do_multithreaded(&job);
2028 deferred_init_mem_pfn_range_in_zone(&i, zone, &spfn, &epfn,
2029 epfn_align);
Daniel Jordan117003c2020-06-03 15:59:20 -07002030 }
Mel Gorman7e18adb2015-06-30 14:57:05 -07002031zone_empty:
Mel Gorman7e18adb2015-06-30 14:57:05 -07002032 /* Sanity check that the next zone really is unpopulated */
2033 WARN_ON(++zid < MAX_NR_ZONES && populated_zone(++zone));
2034
Daniel Jordan89c7c402020-06-03 15:59:47 -07002035 pr_info("node %d deferred pages initialised in %ums\n",
2036 pgdat->node_id, jiffies_to_msecs(jiffies - start));
Nicolai Stanged3cd1312015-08-06 15:46:16 -07002037
2038 pgdat_init_report_one_done();
Mel Gorman0e1cc952015-06-30 14:57:27 -07002039 return 0;
2040}
Pavel Tatashinc9e97a12018-04-05 16:22:31 -07002041
2042/*
Pavel Tatashinc9e97a12018-04-05 16:22:31 -07002043 * If this zone has deferred pages, try to grow it by initializing enough
2044 * deferred pages to satisfy the allocation specified by order, rounded up to
2045 * the nearest PAGES_PER_SECTION boundary. So we're adding memory in increments
2046 * of SECTION_SIZE bytes by initializing struct pages in increments of
2047 * PAGES_PER_SECTION * sizeof(struct page) bytes.
2048 *
2049 * Return true when zone was grown, otherwise return false. We return true even
2050 * when we grow less than requested, to let the caller decide if there are
2051 * enough pages to satisfy the allocation.
2052 *
2053 * Note: We use noinline because this function is needed only during boot, and
2054 * it is called from a __ref function _deferred_grow_zone. This way we are
2055 * making sure that it is not inlined into permanent text section.
2056 */
2057static noinline bool __init
2058deferred_grow_zone(struct zone *zone, unsigned int order)
2059{
Pavel Tatashinc9e97a12018-04-05 16:22:31 -07002060 unsigned long nr_pages_needed = ALIGN(1 << order, PAGES_PER_SECTION);
Alexander Duyck837566e2019-05-13 17:21:17 -07002061 pg_data_t *pgdat = zone->zone_pgdat;
Pavel Tatashinc9e97a12018-04-05 16:22:31 -07002062 unsigned long first_deferred_pfn = pgdat->first_deferred_pfn;
Alexander Duyck0e56aca2019-05-13 17:21:20 -07002063 unsigned long spfn, epfn, flags;
2064 unsigned long nr_pages = 0;
Pavel Tatashinc9e97a12018-04-05 16:22:31 -07002065 u64 i;
2066
2067 /* Only the last zone may have deferred pages */
2068 if (zone_end_pfn(zone) != pgdat_end_pfn(pgdat))
2069 return false;
2070
2071 pgdat_resize_lock(pgdat, &flags);
2072
2073 /*
Pavel Tatashinc9e97a12018-04-05 16:22:31 -07002074 * If someone grew this zone while we were waiting for spinlock, return
2075 * true, as there might be enough pages already.
2076 */
2077 if (first_deferred_pfn != pgdat->first_deferred_pfn) {
2078 pgdat_resize_unlock(pgdat, &flags);
2079 return true;
2080 }
2081
Alexander Duyck0e56aca2019-05-13 17:21:20 -07002082 /* If the zone is empty somebody else may have cleared out the zone */
2083 if (!deferred_init_mem_pfn_range_in_zone(&i, zone, &spfn, &epfn,
2084 first_deferred_pfn)) {
2085 pgdat->first_deferred_pfn = ULONG_MAX;
Pavel Tatashinc9e97a12018-04-05 16:22:31 -07002086 pgdat_resize_unlock(pgdat, &flags);
Juergen Grossb9705d82019-07-04 15:14:36 -07002087 /* Retry only once. */
2088 return first_deferred_pfn != ULONG_MAX;
Pavel Tatashinc9e97a12018-04-05 16:22:31 -07002089 }
2090
Alexander Duyck0e56aca2019-05-13 17:21:20 -07002091 /*
2092 * Initialize and free pages in MAX_ORDER sized increments so
2093 * that we can avoid introducing any issues with the buddy
2094 * allocator.
2095 */
2096 while (spfn < epfn) {
2097 /* update our first deferred PFN for this section */
2098 first_deferred_pfn = spfn;
Pavel Tatashinc9e97a12018-04-05 16:22:31 -07002099
Alexander Duyck0e56aca2019-05-13 17:21:20 -07002100 nr_pages += deferred_init_maxorder(&i, zone, &spfn, &epfn);
Daniel Jordan117003c2020-06-03 15:59:20 -07002101 touch_nmi_watchdog();
Pavel Tatashinc9e97a12018-04-05 16:22:31 -07002102
Alexander Duyck0e56aca2019-05-13 17:21:20 -07002103 /* We should only stop along section boundaries */
2104 if ((first_deferred_pfn ^ spfn) < PAGES_PER_SECTION)
2105 continue;
2106
2107 /* If our quota has been met we can stop here */
Pavel Tatashinc9e97a12018-04-05 16:22:31 -07002108 if (nr_pages >= nr_pages_needed)
2109 break;
2110 }
2111
Alexander Duyck0e56aca2019-05-13 17:21:20 -07002112 pgdat->first_deferred_pfn = spfn;
Pavel Tatashinc9e97a12018-04-05 16:22:31 -07002113 pgdat_resize_unlock(pgdat, &flags);
2114
2115 return nr_pages > 0;
2116}
2117
2118/*
2119 * deferred_grow_zone() is __init, but it is called from
2120 * get_page_from_freelist() during early boot until deferred_pages permanently
2121 * disables this call. This is why we have refdata wrapper to avoid warning,
2122 * and to ensure that the function body gets unloaded.
2123 */
2124static bool __ref
2125_deferred_grow_zone(struct zone *zone, unsigned int order)
2126{
2127 return deferred_grow_zone(zone, order);
2128}
2129
Joonsoo Kim7cf91a92016-03-15 14:57:51 -07002130#endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
Mel Gorman0e1cc952015-06-30 14:57:27 -07002131
2132void __init page_alloc_init_late(void)
2133{
Joonsoo Kim7cf91a92016-03-15 14:57:51 -07002134 struct zone *zone;
Dan Williamse900a912019-05-14 15:41:28 -07002135 int nid;
Joonsoo Kim7cf91a92016-03-15 14:57:51 -07002136
2137#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
Mel Gorman0e1cc952015-06-30 14:57:27 -07002138
Nicolai Stanged3cd1312015-08-06 15:46:16 -07002139 /* There will be num_node_state(N_MEMORY) threads */
2140 atomic_set(&pgdat_init_n_undone, num_node_state(N_MEMORY));
Mel Gorman0e1cc952015-06-30 14:57:27 -07002141 for_each_node_state(nid, N_MEMORY) {
Mel Gorman0e1cc952015-06-30 14:57:27 -07002142 kthread_run(deferred_init_memmap, NODE_DATA(nid), "pgdatinit%d", nid);
2143 }
2144
2145 /* Block until all are initialised */
Nicolai Stanged3cd1312015-08-06 15:46:16 -07002146 wait_for_completion(&pgdat_init_all_done_comp);
Mel Gorman4248b0d2015-08-06 15:46:20 -07002147
Pavel Tatashinc9e97a12018-04-05 16:22:31 -07002148 /*
Mel Gorman3e8fc002019-11-05 21:16:27 -08002149 * The number of managed pages has changed due to the initialisation
2150 * so the pcpu batch and high limits needs to be updated or the limits
2151 * will be artificially small.
2152 */
2153 for_each_populated_zone(zone)
2154 zone_pcp_update(zone);
2155
2156 /*
Pavel Tatashinc9e97a12018-04-05 16:22:31 -07002157 * We initialized the rest of the deferred pages. Permanently disable
2158 * on-demand struct page initialization.
2159 */
2160 static_branch_disable(&deferred_pages);
2161
Mel Gorman4248b0d2015-08-06 15:46:20 -07002162 /* Reinit limits that are based on free pages after the kernel is up */
2163 files_maxfiles_init();
Joonsoo Kim7cf91a92016-03-15 14:57:51 -07002164#endif
Mike Rapoport350e88b2019-05-13 17:22:59 -07002165
Lin Fengba8f3582020-12-14 19:11:19 -08002166 buffer_init();
2167
Pavel Tatashin3010f872017-08-18 15:16:05 -07002168 /* Discard memblock private memory */
2169 memblock_discard();
Joonsoo Kim7cf91a92016-03-15 14:57:51 -07002170
Dan Williamse900a912019-05-14 15:41:28 -07002171 for_each_node_state(nid, N_MEMORY)
2172 shuffle_free_memory(NODE_DATA(nid));
2173
Joonsoo Kim7cf91a92016-03-15 14:57:51 -07002174 for_each_populated_zone(zone)
2175 set_zone_contiguous(zone);
Mel Gorman7e18adb2015-06-30 14:57:05 -07002176}
Mel Gorman7e18adb2015-06-30 14:57:05 -07002177
Michal Nazarewicz47118af2011-12-29 13:09:50 +01002178#ifdef CONFIG_CMA
Li Zhong9cf510a2013-08-23 13:52:52 +08002179/* Free whole pageblock and set its migration type to MIGRATE_CMA. */
Michal Nazarewicz47118af2011-12-29 13:09:50 +01002180void __init init_cma_reserved_pageblock(struct page *page)
2181{
2182 unsigned i = pageblock_nr_pages;
2183 struct page *p = page;
2184
2185 do {
2186 __ClearPageReserved(p);
2187 set_page_count(p, 0);
Joonsoo Kimd883c6c2018-05-23 10:18:21 +09002188 } while (++p, --i);
Michal Nazarewicz47118af2011-12-29 13:09:50 +01002189
Michal Nazarewicz47118af2011-12-29 13:09:50 +01002190 set_pageblock_migratetype(page, MIGRATE_CMA);
Michal Nazarewiczdc783272014-07-02 15:22:35 -07002191
2192 if (pageblock_order >= MAX_ORDER) {
2193 i = pageblock_nr_pages;
2194 p = page;
2195 do {
2196 set_page_refcounted(p);
2197 __free_pages(p, MAX_ORDER - 1);
2198 p += MAX_ORDER_NR_PAGES;
2199 } while (i -= MAX_ORDER_NR_PAGES);
2200 } else {
2201 set_page_refcounted(page);
2202 __free_pages(page, pageblock_order);
2203 }
2204
Jiang Liu3dcc0572013-07-03 15:03:21 -07002205 adjust_managed_page_count(page, pageblock_nr_pages);
David Hildenbrand3c381db2021-02-25 17:16:40 -08002206 page_zone(page)->cma_pages += pageblock_nr_pages;
Michal Nazarewicz47118af2011-12-29 13:09:50 +01002207}
2208#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210/*
2211 * The order of subdivision here is critical for the IO subsystem.
2212 * Please do not alter this order without good reasons and regression
2213 * testing. Specifically, as large blocks of memory are subdivided,
2214 * the order in which smaller blocks are delivered depends on the order
2215 * they're subdivided in this function. This is the primary factor
2216 * influencing the order in which pages are delivered to the IO
2217 * subsystem according to empirical testing, and this is also justified
2218 * by considering the behavior of a buddy system containing a single
2219 * large block of memory acted on by a series of small allocations.
2220 * This behavior is a critical factor in sglist merging's success.
2221 *
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +01002222 * -- nyc
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 */
Nick Piggin085cc7d52006-01-06 00:11:01 -08002224static inline void expand(struct zone *zone, struct page *page,
Alexander Duyck6ab01362020-04-06 20:04:49 -07002225 int low, int high, int migratetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226{
2227 unsigned long size = 1 << high;
2228
2229 while (high > low) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 high--;
2231 size >>= 1;
Sasha Levin309381fea2014-01-23 15:52:54 -08002232 VM_BUG_ON_PAGE(bad_range(zone, &page[size]), &page[size]);
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -08002233
Joonsoo Kimacbc15a2016-10-07 16:58:15 -07002234 /*
2235 * Mark as guard pages (or page), that will allow to
2236 * merge back to allocator when buddy will be freed.
2237 * Corresponding page table entries will not be touched,
2238 * pages will stay not present in virtual address space
2239 */
2240 if (set_page_guard(zone, &page[size], high, migratetype))
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -08002241 continue;
Joonsoo Kimacbc15a2016-10-07 16:58:15 -07002242
Alexander Duyck6ab01362020-04-06 20:04:49 -07002243 add_to_free_list(&page[size], zone, high, migratetype);
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -07002244 set_buddy_order(&page[size], high);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246}
2247
Vlastimil Babka4e611802016-05-19 17:14:41 -07002248static void check_new_page_bad(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
Naoya Horiguchif4c18e62015-08-06 15:47:08 -07002250 if (unlikely(page->flags & __PG_HWPOISON)) {
Naoya Horiguchie570f562016-05-20 16:58:50 -07002251 /* Don't complain about hwpoisoned pages */
2252 page_mapcount_reset(page); /* remove PageBuddy */
2253 return;
Naoya Horiguchif4c18e62015-08-06 15:47:08 -07002254 }
Wei Yang58b7f112020-06-03 15:58:39 -07002255
2256 bad_page(page,
2257 page_bad_reason(page, PAGE_FLAGS_CHECK_AT_PREP));
Vlastimil Babka4e611802016-05-19 17:14:41 -07002258}
2259
2260/*
2261 * This page is about to be returned from the page allocator
2262 */
2263static inline int check_new_page(struct page *page)
2264{
2265 if (likely(page_expected_state(page,
2266 PAGE_FLAGS_CHECK_AT_PREP|__PG_HWPOISON)))
2267 return 0;
2268
2269 check_new_page_bad(page);
2270 return 1;
Wu Fengguang2a7684a2009-09-16 11:50:12 +02002271}
2272
Mel Gorman479f8542016-05-19 17:14:35 -07002273#ifdef CONFIG_DEBUG_VM
Vlastimil Babka4462b322019-07-11 20:55:09 -07002274/*
2275 * With DEBUG_VM enabled, order-0 pages are checked for expected state when
2276 * being allocated from pcp lists. With debug_pagealloc also enabled, they are
2277 * also checked when pcp lists are refilled from the free lists.
2278 */
2279static inline bool check_pcp_refill(struct page *page)
Mel Gorman479f8542016-05-19 17:14:35 -07002280{
Vlastimil Babka8e57f8a2020-01-13 16:29:20 -08002281 if (debug_pagealloc_enabled_static())
Vlastimil Babka4462b322019-07-11 20:55:09 -07002282 return check_new_page(page);
2283 else
2284 return false;
Mel Gorman479f8542016-05-19 17:14:35 -07002285}
2286
Vlastimil Babka4462b322019-07-11 20:55:09 -07002287static inline bool check_new_pcp(struct page *page)
Mel Gorman479f8542016-05-19 17:14:35 -07002288{
2289 return check_new_page(page);
2290}
2291#else
Vlastimil Babka4462b322019-07-11 20:55:09 -07002292/*
2293 * With DEBUG_VM disabled, free order-0 pages are checked for expected state
2294 * when pcp lists are being refilled from the free lists. With debug_pagealloc
2295 * enabled, they are also checked when being allocated from the pcp lists.
2296 */
2297static inline bool check_pcp_refill(struct page *page)
Mel Gorman479f8542016-05-19 17:14:35 -07002298{
2299 return check_new_page(page);
2300}
Vlastimil Babka4462b322019-07-11 20:55:09 -07002301static inline bool check_new_pcp(struct page *page)
Mel Gorman479f8542016-05-19 17:14:35 -07002302{
Vlastimil Babka8e57f8a2020-01-13 16:29:20 -08002303 if (debug_pagealloc_enabled_static())
Vlastimil Babka4462b322019-07-11 20:55:09 -07002304 return check_new_page(page);
2305 else
2306 return false;
Mel Gorman479f8542016-05-19 17:14:35 -07002307}
2308#endif /* CONFIG_DEBUG_VM */
2309
2310static bool check_new_pages(struct page *page, unsigned int order)
2311{
2312 int i;
2313 for (i = 0; i < (1 << order); i++) {
2314 struct page *p = page + i;
2315
2316 if (unlikely(check_new_page(p)))
2317 return true;
2318 }
2319
2320 return false;
2321}
2322
Joonsoo Kim46f24fd2016-07-26 15:23:58 -07002323inline void post_alloc_hook(struct page *page, unsigned int order,
2324 gfp_t gfp_flags)
2325{
Andrey Konovalov1bb5eab2021-04-29 23:00:02 -07002326 bool init;
2327
Joonsoo Kim46f24fd2016-07-26 15:23:58 -07002328 set_page_private(page, 0);
2329 set_page_refcounted(page);
2330
2331 arch_alloc_page(page, order);
Mike Rapoport77bc7fd2020-12-14 19:10:20 -08002332 debug_pagealloc_map_pages(page, 1 << order);
David Hildenbrand862b6de2020-12-14 19:11:15 -08002333
Andrey Konovalov1bb5eab2021-04-29 23:00:02 -07002334 /*
2335 * Page unpoisoning must happen before memory initialization.
2336 * Otherwise, the poison pattern will be overwritten for __GFP_ZERO
2337 * allocations and the page unpoisoning code will complain.
2338 */
2339 kernel_unpoison_pages(page, 1 << order);
2340
2341 /*
2342 * As memory initialization might be integrated into KASAN,
2343 * kasan_alloc_pages and kernel_init_free_pages must be
2344 * kept together to avoid discrepancies in behavior.
2345 */
2346 init = !want_init_on_free() && want_init_on_alloc(gfp_flags);
2347 kasan_alloc_pages(page, order, init);
2348 if (init && !kasan_has_integrated_init())
David Hildenbrand862b6de2020-12-14 19:11:15 -08002349 kernel_init_free_pages(page, 1 << order);
Andrey Konovalov1bb5eab2021-04-29 23:00:02 -07002350
2351 set_page_owner(page, order, gfp_flags);
Joonsoo Kim46f24fd2016-07-26 15:23:58 -07002352}
2353
Mel Gorman479f8542016-05-19 17:14:35 -07002354static void prep_new_page(struct page *page, unsigned int order, gfp_t gfp_flags,
Mel Gormanc6038442016-05-19 17:13:38 -07002355 unsigned int alloc_flags)
Wu Fengguang2a7684a2009-09-16 11:50:12 +02002356{
Joonsoo Kim46f24fd2016-07-26 15:23:58 -07002357 post_alloc_hook(page, order, gfp_flags);
Nick Piggin17cf4402006-03-22 00:08:41 -08002358
Nick Piggin17cf4402006-03-22 00:08:41 -08002359 if (order && (gfp_flags & __GFP_COMP))
2360 prep_compound_page(page, order);
2361
Vlastimil Babka75379192015-02-11 15:25:38 -08002362 /*
Michal Hocko2f064f32015-08-21 14:11:51 -07002363 * page is set pfmemalloc when ALLOC_NO_WATERMARKS was necessary to
Vlastimil Babka75379192015-02-11 15:25:38 -08002364 * allocate the page. The expectation is that the caller is taking
2365 * steps that will free more memory. The caller should avoid the page
2366 * being used for !PFMEMALLOC purposes.
2367 */
Michal Hocko2f064f32015-08-21 14:11:51 -07002368 if (alloc_flags & ALLOC_NO_WATERMARKS)
2369 set_page_pfmemalloc(page);
2370 else
2371 clear_page_pfmemalloc(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372}
2373
Mel Gorman56fd56b2007-10-16 01:25:58 -07002374/*
2375 * Go through the free lists for the given migratetype and remove
2376 * the smallest available page from the freelists
2377 */
Aaron Lu85ccc8f2017-11-15 17:36:53 -08002378static __always_inline
Mel Gorman728ec982009-06-16 15:32:04 -07002379struct page *__rmqueue_smallest(struct zone *zone, unsigned int order,
Mel Gorman56fd56b2007-10-16 01:25:58 -07002380 int migratetype)
2381{
2382 unsigned int current_order;
Pintu Kumarb8af2942013-09-11 14:20:34 -07002383 struct free_area *area;
Mel Gorman56fd56b2007-10-16 01:25:58 -07002384 struct page *page;
2385
2386 /* Find a page of the appropriate size in the preferred list */
2387 for (current_order = order; current_order < MAX_ORDER; ++current_order) {
2388 area = &(zone->free_area[current_order]);
Dan Williamsb03641a2019-05-14 15:41:32 -07002389 page = get_page_from_free_area(area, migratetype);
Geliang Tanga16601c2016-01-14 15:20:30 -08002390 if (!page)
2391 continue;
Alexander Duyck6ab01362020-04-06 20:04:49 -07002392 del_page_from_free_list(page, zone, current_order);
2393 expand(zone, page, order, current_order, migratetype);
Vlastimil Babkabb14c2c2015-09-08 15:01:25 -07002394 set_pcppage_migratetype(page, migratetype);
Mel Gorman56fd56b2007-10-16 01:25:58 -07002395 return page;
2396 }
2397
2398 return NULL;
2399}
2400
2401
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002402/*
2403 * This array describes the order lists are fallen back to when
2404 * the free lists for the desirable migrate type are depleted
2405 */
Wei Yangda415662020-08-06 23:25:58 -07002406static int fallbacks[MIGRATE_TYPES][3] = {
Mel Gorman974a7862015-11-06 16:28:34 -08002407 [MIGRATE_UNMOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_MOVABLE, MIGRATE_TYPES },
Mel Gorman974a7862015-11-06 16:28:34 -08002408 [MIGRATE_MOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE, MIGRATE_TYPES },
Huang Shijie7ead3342018-12-28 00:34:46 -08002409 [MIGRATE_RECLAIMABLE] = { MIGRATE_UNMOVABLE, MIGRATE_MOVABLE, MIGRATE_TYPES },
Joonsoo Kimdc676472015-04-14 15:45:15 -07002410#ifdef CONFIG_CMA
Mel Gorman974a7862015-11-06 16:28:34 -08002411 [MIGRATE_CMA] = { MIGRATE_TYPES }, /* Never used */
Michal Nazarewicz47118af2011-12-29 13:09:50 +01002412#endif
Minchan Kim194159f2013-02-22 16:33:58 -08002413#ifdef CONFIG_MEMORY_ISOLATION
Mel Gorman974a7862015-11-06 16:28:34 -08002414 [MIGRATE_ISOLATE] = { MIGRATE_TYPES }, /* Never used */
Minchan Kim194159f2013-02-22 16:33:58 -08002415#endif
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002416};
2417
Joonsoo Kimdc676472015-04-14 15:45:15 -07002418#ifdef CONFIG_CMA
Aaron Lu85ccc8f2017-11-15 17:36:53 -08002419static __always_inline struct page *__rmqueue_cma_fallback(struct zone *zone,
Joonsoo Kimdc676472015-04-14 15:45:15 -07002420 unsigned int order)
2421{
2422 return __rmqueue_smallest(zone, order, MIGRATE_CMA);
2423}
2424#else
2425static inline struct page *__rmqueue_cma_fallback(struct zone *zone,
2426 unsigned int order) { return NULL; }
2427#endif
2428
Mel Gormanc361be52007-10-16 01:25:51 -07002429/*
David Hildenbrand293ffa52020-10-15 20:09:30 -07002430 * Move the free pages in a range to the freelist tail of the requested type.
Mel Gormand9c23402007-10-16 01:26:01 -07002431 * Note that start_page and end_pages are not aligned on a pageblock
Mel Gormanc361be52007-10-16 01:25:51 -07002432 * boundary. If alignment is required, use move_freepages_block()
2433 */
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -07002434static int move_freepages(struct zone *zone,
Kefeng Wang39ddb992021-04-29 23:01:36 -07002435 unsigned long start_pfn, unsigned long end_pfn,
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -07002436 int migratetype, int *num_movable)
Mel Gormanc361be52007-10-16 01:25:51 -07002437{
2438 struct page *page;
Kefeng Wang39ddb992021-04-29 23:01:36 -07002439 unsigned long pfn;
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08002440 unsigned int order;
Mel Gormand1003132007-10-16 01:26:00 -07002441 int pages_moved = 0;
Mel Gormanc361be52007-10-16 01:25:51 -07002442
Kefeng Wang39ddb992021-04-29 23:01:36 -07002443 for (pfn = start_pfn; pfn <= end_pfn;) {
2444 if (!pfn_valid_within(pfn)) {
2445 pfn++;
Mel Gormanc361be52007-10-16 01:25:51 -07002446 continue;
2447 }
2448
Kefeng Wang39ddb992021-04-29 23:01:36 -07002449 page = pfn_to_page(pfn);
Mel Gormanc361be52007-10-16 01:25:51 -07002450 if (!PageBuddy(page)) {
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -07002451 /*
2452 * We assume that pages that could be isolated for
2453 * migration are movable. But we don't actually try
2454 * isolating, as that would be expensive.
2455 */
2456 if (num_movable &&
2457 (PageLRU(page) || __PageMovable(page)))
2458 (*num_movable)++;
Kefeng Wang39ddb992021-04-29 23:01:36 -07002459 pfn++;
Mel Gormanc361be52007-10-16 01:25:51 -07002460 continue;
2461 }
2462
David Rientjescd961032019-08-24 17:54:40 -07002463 /* Make sure we are not inadvertently changing nodes */
2464 VM_BUG_ON_PAGE(page_to_nid(page) != zone_to_nid(zone), page);
2465 VM_BUG_ON_PAGE(page_zone(page) != zone, page);
2466
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -07002467 order = buddy_order(page);
Alexander Duyck6ab01362020-04-06 20:04:49 -07002468 move_to_free_list(page, zone, order, migratetype);
Kefeng Wang39ddb992021-04-29 23:01:36 -07002469 pfn += 1 << order;
Mel Gormand1003132007-10-16 01:26:00 -07002470 pages_moved += 1 << order;
Mel Gormanc361be52007-10-16 01:25:51 -07002471 }
2472
Mel Gormand1003132007-10-16 01:26:00 -07002473 return pages_moved;
Mel Gormanc361be52007-10-16 01:25:51 -07002474}
2475
Minchan Kimee6f5092012-07-31 16:43:50 -07002476int move_freepages_block(struct zone *zone, struct page *page,
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -07002477 int migratetype, int *num_movable)
Mel Gormanc361be52007-10-16 01:25:51 -07002478{
Kefeng Wang39ddb992021-04-29 23:01:36 -07002479 unsigned long start_pfn, end_pfn, pfn;
Mel Gormanc361be52007-10-16 01:25:51 -07002480
David Rientjes4a222122018-10-26 15:09:24 -07002481 if (num_movable)
2482 *num_movable = 0;
2483
Kefeng Wang39ddb992021-04-29 23:01:36 -07002484 pfn = page_to_pfn(page);
2485 start_pfn = pfn & ~(pageblock_nr_pages - 1);
Mel Gormand9c23402007-10-16 01:26:01 -07002486 end_pfn = start_pfn + pageblock_nr_pages - 1;
Mel Gormanc361be52007-10-16 01:25:51 -07002487
2488 /* Do not cross zone boundaries */
Cody P Schafer108bcc92013-02-22 16:35:23 -08002489 if (!zone_spans_pfn(zone, start_pfn))
Kefeng Wang39ddb992021-04-29 23:01:36 -07002490 start_pfn = pfn;
Cody P Schafer108bcc92013-02-22 16:35:23 -08002491 if (!zone_spans_pfn(zone, end_pfn))
Mel Gormanc361be52007-10-16 01:25:51 -07002492 return 0;
2493
Kefeng Wang39ddb992021-04-29 23:01:36 -07002494 return move_freepages(zone, start_pfn, end_pfn, migratetype,
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -07002495 num_movable);
Mel Gormanc361be52007-10-16 01:25:51 -07002496}
2497
Mel Gorman2f66a682009-09-21 17:02:31 -07002498static void change_pageblock_range(struct page *pageblock_page,
2499 int start_order, int migratetype)
2500{
2501 int nr_pageblocks = 1 << (start_order - pageblock_order);
2502
2503 while (nr_pageblocks--) {
2504 set_pageblock_migratetype(pageblock_page, migratetype);
2505 pageblock_page += pageblock_nr_pages;
2506 }
2507}
2508
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07002509/*
Vlastimil Babka9c0415e2015-02-11 15:28:21 -08002510 * When we are falling back to another migratetype during allocation, try to
2511 * steal extra free pages from the same pageblocks to satisfy further
2512 * allocations, instead of polluting multiple pageblocks.
2513 *
2514 * If we are stealing a relatively large buddy page, it is likely there will
2515 * be more free pages in the pageblock, so try to steal them all. For
2516 * reclaimable and unmovable allocations, we steal regardless of page size,
2517 * as fragmentation caused by those allocations polluting movable pageblocks
2518 * is worse than movable allocations stealing from unmovable and reclaimable
2519 * pageblocks.
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07002520 */
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002521static bool can_steal_fallback(unsigned int order, int start_mt)
2522{
2523 /*
2524 * Leaving this order check is intended, although there is
2525 * relaxed order check in next check. The reason is that
2526 * we can actually steal whole pageblock if this condition met,
2527 * but, below check doesn't guarantee it and that is just heuristic
2528 * so could be changed anytime.
2529 */
2530 if (order >= pageblock_order)
2531 return true;
2532
2533 if (order >= pageblock_order / 2 ||
2534 start_mt == MIGRATE_RECLAIMABLE ||
2535 start_mt == MIGRATE_UNMOVABLE ||
2536 page_group_by_mobility_disabled)
2537 return true;
2538
2539 return false;
2540}
2541
Johannes Weiner597c8922020-12-14 19:12:15 -08002542static inline bool boost_watermark(struct zone *zone)
Mel Gorman1c308442018-12-28 00:35:52 -08002543{
2544 unsigned long max_boost;
2545
2546 if (!watermark_boost_factor)
Johannes Weiner597c8922020-12-14 19:12:15 -08002547 return false;
Henry Willard14f69142020-05-07 18:36:27 -07002548 /*
2549 * Don't bother in zones that are unlikely to produce results.
2550 * On small machines, including kdump capture kernels running
2551 * in a small area, boosting the watermark can cause an out of
2552 * memory situation immediately.
2553 */
2554 if ((pageblock_nr_pages * 4) > zone_managed_pages(zone))
Johannes Weiner597c8922020-12-14 19:12:15 -08002555 return false;
Mel Gorman1c308442018-12-28 00:35:52 -08002556
2557 max_boost = mult_frac(zone->_watermark[WMARK_HIGH],
2558 watermark_boost_factor, 10000);
Mel Gorman94b33342019-02-20 22:19:49 -08002559
2560 /*
2561 * high watermark may be uninitialised if fragmentation occurs
2562 * very early in boot so do not boost. We do not fall
2563 * through and boost by pageblock_nr_pages as failing
2564 * allocations that early means that reclaim is not going
2565 * to help and it may even be impossible to reclaim the
2566 * boosted watermark resulting in a hang.
2567 */
2568 if (!max_boost)
Johannes Weiner597c8922020-12-14 19:12:15 -08002569 return false;
Mel Gorman94b33342019-02-20 22:19:49 -08002570
Mel Gorman1c308442018-12-28 00:35:52 -08002571 max_boost = max(pageblock_nr_pages, max_boost);
2572
2573 zone->watermark_boost = min(zone->watermark_boost + pageblock_nr_pages,
2574 max_boost);
Johannes Weiner597c8922020-12-14 19:12:15 -08002575
2576 return true;
Mel Gorman1c308442018-12-28 00:35:52 -08002577}
2578
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002579/*
2580 * This function implements actual steal behaviour. If order is large enough,
2581 * we can steal whole pageblock. If not, we first move freepages in this
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -07002582 * pageblock to our migratetype and determine how many already-allocated pages
2583 * are there in the pageblock with a compatible migratetype. If at least half
2584 * of pages are free or compatible, we can change migratetype of the pageblock
2585 * itself, so pages freed in the future will be put on the correct free list.
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002586 */
2587static void steal_suitable_fallback(struct zone *zone, struct page *page,
Mel Gorman1c308442018-12-28 00:35:52 -08002588 unsigned int alloc_flags, int start_type, bool whole_block)
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07002589{
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -07002590 unsigned int current_order = buddy_order(page);
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -07002591 int free_pages, movable_pages, alike_pages;
2592 int old_block_type;
2593
2594 old_block_type = get_pageblock_migratetype(page);
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07002595
Vlastimil Babka3bc48f92017-05-08 15:54:37 -07002596 /*
2597 * This can happen due to races and we want to prevent broken
2598 * highatomic accounting.
2599 */
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -07002600 if (is_migrate_highatomic(old_block_type))
Vlastimil Babka3bc48f92017-05-08 15:54:37 -07002601 goto single_page;
2602
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07002603 /* Take ownership for orders >= pageblock_order */
2604 if (current_order >= pageblock_order) {
2605 change_pageblock_range(page, current_order, start_type);
Vlastimil Babka3bc48f92017-05-08 15:54:37 -07002606 goto single_page;
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07002607 }
2608
Mel Gorman1c308442018-12-28 00:35:52 -08002609 /*
2610 * Boost watermarks to increase reclaim pressure to reduce the
2611 * likelihood of future fallbacks. Wake kswapd now as the node
2612 * may be balanced overall and kswapd will not wake naturally.
2613 */
Johannes Weiner597c8922020-12-14 19:12:15 -08002614 if (boost_watermark(zone) && (alloc_flags & ALLOC_KSWAPD))
Mel Gorman73444bc2019-01-08 15:23:39 -08002615 set_bit(ZONE_BOOSTED_WATERMARK, &zone->flags);
Mel Gorman1c308442018-12-28 00:35:52 -08002616
Vlastimil Babka3bc48f92017-05-08 15:54:37 -07002617 /* We are not allowed to try stealing from the whole block */
2618 if (!whole_block)
2619 goto single_page;
2620
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -07002621 free_pages = move_freepages_block(zone, page, start_type,
2622 &movable_pages);
2623 /*
2624 * Determine how many pages are compatible with our allocation.
2625 * For movable allocation, it's the number of movable pages which
2626 * we just obtained. For other types it's a bit more tricky.
2627 */
2628 if (start_type == MIGRATE_MOVABLE) {
2629 alike_pages = movable_pages;
2630 } else {
2631 /*
2632 * If we are falling back a RECLAIMABLE or UNMOVABLE allocation
2633 * to MOVABLE pageblock, consider all non-movable pages as
2634 * compatible. If it's UNMOVABLE falling back to RECLAIMABLE or
2635 * vice versa, be conservative since we can't distinguish the
2636 * exact migratetype of non-movable pages.
2637 */
2638 if (old_block_type == MIGRATE_MOVABLE)
2639 alike_pages = pageblock_nr_pages
2640 - (free_pages + movable_pages);
2641 else
2642 alike_pages = 0;
2643 }
2644
Vlastimil Babka3bc48f92017-05-08 15:54:37 -07002645 /* moving whole block can fail due to zone boundary conditions */
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -07002646 if (!free_pages)
Vlastimil Babka3bc48f92017-05-08 15:54:37 -07002647 goto single_page;
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07002648
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -07002649 /*
2650 * If a sufficient number of pages in the block are either free or of
2651 * comparable migratability as our allocation, claim the whole block.
2652 */
2653 if (free_pages + alike_pages >= (1 << (pageblock_order-1)) ||
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002654 page_group_by_mobility_disabled)
2655 set_pageblock_migratetype(page, start_type);
Vlastimil Babka3bc48f92017-05-08 15:54:37 -07002656
2657 return;
2658
2659single_page:
Alexander Duyck6ab01362020-04-06 20:04:49 -07002660 move_to_free_list(page, zone, current_order, start_type);
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002661}
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07002662
Joonsoo Kim2149cda2015-04-14 15:45:21 -07002663/*
2664 * Check whether there is a suitable fallback freepage with requested order.
2665 * If only_stealable is true, this function returns fallback_mt only if
2666 * we can steal other freepages all together. This would help to reduce
2667 * fragmentation due to mixed migratetype pages in one pageblock.
2668 */
2669int find_suitable_fallback(struct free_area *area, unsigned int order,
2670 int migratetype, bool only_stealable, bool *can_steal)
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002671{
2672 int i;
2673 int fallback_mt;
2674
2675 if (area->nr_free == 0)
2676 return -1;
2677
2678 *can_steal = false;
2679 for (i = 0;; i++) {
2680 fallback_mt = fallbacks[migratetype][i];
Mel Gorman974a7862015-11-06 16:28:34 -08002681 if (fallback_mt == MIGRATE_TYPES)
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002682 break;
2683
Dan Williamsb03641a2019-05-14 15:41:32 -07002684 if (free_area_empty(area, fallback_mt))
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002685 continue;
2686
2687 if (can_steal_fallback(order, migratetype))
2688 *can_steal = true;
2689
Joonsoo Kim2149cda2015-04-14 15:45:21 -07002690 if (!only_stealable)
2691 return fallback_mt;
2692
2693 if (*can_steal)
2694 return fallback_mt;
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07002695 }
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002696
2697 return -1;
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07002698}
2699
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002700/*
2701 * Reserve a pageblock for exclusive use of high-order atomic allocations if
2702 * there are no empty page blocks that contain a page with a suitable order
2703 */
2704static void reserve_highatomic_pageblock(struct page *page, struct zone *zone,
2705 unsigned int alloc_order)
2706{
2707 int mt;
2708 unsigned long max_managed, flags;
2709
2710 /*
2711 * Limit the number reserved to 1 pageblock or roughly 1% of a zone.
2712 * Check is race-prone but harmless.
2713 */
Arun KS9705bea2018-12-28 00:34:24 -08002714 max_managed = (zone_managed_pages(zone) / 100) + pageblock_nr_pages;
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002715 if (zone->nr_reserved_highatomic >= max_managed)
2716 return;
2717
2718 spin_lock_irqsave(&zone->lock, flags);
2719
2720 /* Recheck the nr_reserved_highatomic limit under the lock */
2721 if (zone->nr_reserved_highatomic >= max_managed)
2722 goto out_unlock;
2723
2724 /* Yoink! */
2725 mt = get_pageblock_migratetype(page);
Xishi Qiua6ffdc02017-05-03 14:52:52 -07002726 if (!is_migrate_highatomic(mt) && !is_migrate_isolate(mt)
2727 && !is_migrate_cma(mt)) {
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002728 zone->nr_reserved_highatomic += pageblock_nr_pages;
2729 set_pageblock_migratetype(page, MIGRATE_HIGHATOMIC);
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -07002730 move_freepages_block(zone, page, MIGRATE_HIGHATOMIC, NULL);
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002731 }
2732
2733out_unlock:
2734 spin_unlock_irqrestore(&zone->lock, flags);
2735}
2736
2737/*
2738 * Used when an allocation is about to fail under memory pressure. This
2739 * potentially hurts the reliability of high-order allocations when under
2740 * intense memory pressure but failed atomic allocations should be easier
2741 * to recover from than an OOM.
Minchan Kim29fac032016-12-12 16:42:14 -08002742 *
2743 * If @force is true, try to unreserve a pageblock even though highatomic
2744 * pageblock is exhausted.
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002745 */
Minchan Kim29fac032016-12-12 16:42:14 -08002746static bool unreserve_highatomic_pageblock(const struct alloc_context *ac,
2747 bool force)
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002748{
2749 struct zonelist *zonelist = ac->zonelist;
2750 unsigned long flags;
2751 struct zoneref *z;
2752 struct zone *zone;
2753 struct page *page;
2754 int order;
Minchan Kim04c87162016-12-12 16:42:11 -08002755 bool ret;
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002756
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002757 for_each_zone_zonelist_nodemask(zone, z, zonelist, ac->highest_zoneidx,
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002758 ac->nodemask) {
Minchan Kim29fac032016-12-12 16:42:14 -08002759 /*
2760 * Preserve at least one pageblock unless memory pressure
2761 * is really high.
2762 */
2763 if (!force && zone->nr_reserved_highatomic <=
2764 pageblock_nr_pages)
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002765 continue;
2766
2767 spin_lock_irqsave(&zone->lock, flags);
2768 for (order = 0; order < MAX_ORDER; order++) {
2769 struct free_area *area = &(zone->free_area[order]);
2770
Dan Williamsb03641a2019-05-14 15:41:32 -07002771 page = get_page_from_free_area(area, MIGRATE_HIGHATOMIC);
Geliang Tanga16601c2016-01-14 15:20:30 -08002772 if (!page)
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002773 continue;
2774
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002775 /*
Minchan Kim4855e4a2016-12-12 16:42:08 -08002776 * In page freeing path, migratetype change is racy so
2777 * we can counter several free pages in a pageblock
Ingo Molnarf0953a12021-05-06 18:06:47 -07002778 * in this loop although we changed the pageblock type
Minchan Kim4855e4a2016-12-12 16:42:08 -08002779 * from highatomic to ac->migratetype. So we should
2780 * adjust the count once.
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002781 */
Xishi Qiua6ffdc02017-05-03 14:52:52 -07002782 if (is_migrate_highatomic_page(page)) {
Minchan Kim4855e4a2016-12-12 16:42:08 -08002783 /*
2784 * It should never happen but changes to
2785 * locking could inadvertently allow a per-cpu
2786 * drain to add pages to MIGRATE_HIGHATOMIC
2787 * while unreserving so be safe and watch for
2788 * underflows.
2789 */
2790 zone->nr_reserved_highatomic -= min(
2791 pageblock_nr_pages,
2792 zone->nr_reserved_highatomic);
2793 }
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002794
2795 /*
2796 * Convert to ac->migratetype and avoid the normal
2797 * pageblock stealing heuristics. Minimally, the caller
2798 * is doing the work and needs the pages. More
2799 * importantly, if the block was always converted to
2800 * MIGRATE_UNMOVABLE or another type then the number
2801 * of pageblocks that cannot be completely freed
2802 * may increase.
2803 */
2804 set_pageblock_migratetype(page, ac->migratetype);
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -07002805 ret = move_freepages_block(zone, page, ac->migratetype,
2806 NULL);
Minchan Kim29fac032016-12-12 16:42:14 -08002807 if (ret) {
2808 spin_unlock_irqrestore(&zone->lock, flags);
2809 return ret;
2810 }
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002811 }
2812 spin_unlock_irqrestore(&zone->lock, flags);
2813 }
Minchan Kim04c87162016-12-12 16:42:11 -08002814
2815 return false;
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002816}
2817
Vlastimil Babka3bc48f92017-05-08 15:54:37 -07002818/*
2819 * Try finding a free buddy page on the fallback list and put it on the free
2820 * list of requested migratetype, possibly along with other pages from the same
2821 * block, depending on fragmentation avoidance heuristics. Returns true if
2822 * fallback was found so that __rmqueue_smallest() can grab it.
Rasmus Villemoesb0025292017-07-10 15:49:26 -07002823 *
2824 * The use of signed ints for order and current_order is a deliberate
2825 * deviation from the rest of this file, to make the for loop
2826 * condition simpler.
Vlastimil Babka3bc48f92017-05-08 15:54:37 -07002827 */
Aaron Lu85ccc8f2017-11-15 17:36:53 -08002828static __always_inline bool
Mel Gorman6bb15452018-12-28 00:35:41 -08002829__rmqueue_fallback(struct zone *zone, int order, int start_migratetype,
2830 unsigned int alloc_flags)
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002831{
Pintu Kumarb8af2942013-09-11 14:20:34 -07002832 struct free_area *area;
Rasmus Villemoesb0025292017-07-10 15:49:26 -07002833 int current_order;
Mel Gorman6bb15452018-12-28 00:35:41 -08002834 int min_order = order;
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002835 struct page *page;
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002836 int fallback_mt;
2837 bool can_steal;
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002838
Vlastimil Babka7a8f58f2017-07-10 15:47:14 -07002839 /*
Mel Gorman6bb15452018-12-28 00:35:41 -08002840 * Do not steal pages from freelists belonging to other pageblocks
2841 * i.e. orders < pageblock_order. If there are no local zones free,
2842 * the zonelists will be reiterated without ALLOC_NOFRAGMENT.
2843 */
2844 if (alloc_flags & ALLOC_NOFRAGMENT)
2845 min_order = pageblock_order;
2846
2847 /*
Vlastimil Babka7a8f58f2017-07-10 15:47:14 -07002848 * Find the largest available free page in the other list. This roughly
2849 * approximates finding the pageblock with the most free pages, which
2850 * would be too costly to do exactly.
2851 */
Mel Gorman6bb15452018-12-28 00:35:41 -08002852 for (current_order = MAX_ORDER - 1; current_order >= min_order;
Mel Gorman7aeb09f2014-06-04 16:10:21 -07002853 --current_order) {
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002854 area = &(zone->free_area[current_order]);
2855 fallback_mt = find_suitable_fallback(area, current_order,
Joonsoo Kim2149cda2015-04-14 15:45:21 -07002856 start_migratetype, false, &can_steal);
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002857 if (fallback_mt == -1)
2858 continue;
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002859
Vlastimil Babka7a8f58f2017-07-10 15:47:14 -07002860 /*
2861 * We cannot steal all free pages from the pageblock and the
2862 * requested migratetype is movable. In that case it's better to
2863 * steal and split the smallest available page instead of the
2864 * largest available page, because even if the next movable
2865 * allocation falls back into a different pageblock than this
2866 * one, it won't cause permanent fragmentation.
2867 */
2868 if (!can_steal && start_migratetype == MIGRATE_MOVABLE
2869 && current_order > order)
2870 goto find_smallest;
Mel Gormane0104872007-10-16 01:25:53 -07002871
Vlastimil Babka7a8f58f2017-07-10 15:47:14 -07002872 goto do_steal;
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002873 }
2874
Vlastimil Babka3bc48f92017-05-08 15:54:37 -07002875 return false;
Vlastimil Babka7a8f58f2017-07-10 15:47:14 -07002876
2877find_smallest:
2878 for (current_order = order; current_order < MAX_ORDER;
2879 current_order++) {
2880 area = &(zone->free_area[current_order]);
2881 fallback_mt = find_suitable_fallback(area, current_order,
2882 start_migratetype, false, &can_steal);
2883 if (fallback_mt != -1)
2884 break;
2885 }
2886
2887 /*
2888 * This should not happen - we already found a suitable fallback
2889 * when looking for the largest page.
2890 */
2891 VM_BUG_ON(current_order == MAX_ORDER);
2892
2893do_steal:
Dan Williamsb03641a2019-05-14 15:41:32 -07002894 page = get_page_from_free_area(area, fallback_mt);
Vlastimil Babka7a8f58f2017-07-10 15:47:14 -07002895
Mel Gorman1c308442018-12-28 00:35:52 -08002896 steal_suitable_fallback(zone, page, alloc_flags, start_migratetype,
2897 can_steal);
Vlastimil Babka7a8f58f2017-07-10 15:47:14 -07002898
2899 trace_mm_page_alloc_extfrag(page, order, current_order,
2900 start_migratetype, fallback_mt);
2901
2902 return true;
2903
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002904}
2905
Mel Gorman56fd56b2007-10-16 01:25:58 -07002906/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 * Do the hard work of removing an element from the buddy allocator.
2908 * Call me with the zone->lock already held.
2909 */
Aaron Lu85ccc8f2017-11-15 17:36:53 -08002910static __always_inline struct page *
Mel Gorman6bb15452018-12-28 00:35:41 -08002911__rmqueue(struct zone *zone, unsigned int order, int migratetype,
2912 unsigned int alloc_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 struct page *page;
2915
Hailong liuce8f86e2021-01-12 15:49:08 -08002916 if (IS_ENABLED(CONFIG_CMA)) {
2917 /*
2918 * Balance movable allocations between regular and CMA areas by
2919 * allocating from CMA when over half of the zone's free memory
2920 * is in the CMA area.
2921 */
2922 if (alloc_flags & ALLOC_CMA &&
2923 zone_page_state(zone, NR_FREE_CMA_PAGES) >
2924 zone_page_state(zone, NR_FREE_PAGES) / 2) {
2925 page = __rmqueue_cma_fallback(zone, order);
2926 if (page)
2927 goto out;
2928 }
Roman Gushchin16867662020-06-03 15:58:42 -07002929 }
Vlastimil Babka3bc48f92017-05-08 15:54:37 -07002930retry:
Mel Gorman56fd56b2007-10-16 01:25:58 -07002931 page = __rmqueue_smallest(zone, order, migratetype);
Mel Gorman974a7862015-11-06 16:28:34 -08002932 if (unlikely(!page)) {
Joonsoo Kim8510e692020-08-06 23:26:04 -07002933 if (alloc_flags & ALLOC_CMA)
Joonsoo Kimdc676472015-04-14 15:45:15 -07002934 page = __rmqueue_cma_fallback(zone, order);
2935
Mel Gorman6bb15452018-12-28 00:35:41 -08002936 if (!page && __rmqueue_fallback(zone, order, migratetype,
2937 alloc_flags))
Vlastimil Babka3bc48f92017-05-08 15:54:37 -07002938 goto retry;
Mel Gorman728ec982009-06-16 15:32:04 -07002939 }
Hailong liuce8f86e2021-01-12 15:49:08 -08002940out:
2941 if (page)
2942 trace_mm_page_alloc_zone_locked(page, order, migratetype);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002943 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944}
2945
Michal Nazarewicz5f63b722012-01-11 15:16:11 +01002946/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 * Obtain a specified number of elements from the buddy allocator, all under
2948 * a single hold of the lock, for efficiency. Add them to the supplied list.
2949 * Returns the number of new pages which were placed at *list.
2950 */
Michal Nazarewicz5f63b722012-01-11 15:16:11 +01002951static int rmqueue_bulk(struct zone *zone, unsigned int order,
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002952 unsigned long count, struct list_head *list,
Mel Gorman6bb15452018-12-28 00:35:41 -08002953 int migratetype, unsigned int alloc_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954{
Mel Gormancb66bed2021-04-29 23:01:42 -07002955 int i, allocated = 0;
Michal Nazarewicz5f63b722012-01-11 15:16:11 +01002956
Mel Gormand34b0732017-04-20 14:37:43 -07002957 spin_lock(&zone->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 for (i = 0; i < count; ++i) {
Mel Gorman6bb15452018-12-28 00:35:41 -08002959 struct page *page = __rmqueue(zone, order, migratetype,
2960 alloc_flags);
Nick Piggin085cc7d52006-01-06 00:11:01 -08002961 if (unlikely(page == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 break;
Mel Gorman81eabcb2007-12-17 16:20:05 -08002963
Mel Gorman479f8542016-05-19 17:14:35 -07002964 if (unlikely(check_pcp_refill(page)))
2965 continue;
2966
Mel Gorman81eabcb2007-12-17 16:20:05 -08002967 /*
Vlastimil Babka0fac3ba2017-11-15 17:38:07 -08002968 * Split buddy pages returned by expand() are received here in
2969 * physical page order. The page is added to the tail of
2970 * caller's list. From the callers perspective, the linked list
2971 * is ordered by page number under some conditions. This is
2972 * useful for IO devices that can forward direction from the
2973 * head, thus also in the physical page order. This is useful
2974 * for IO devices that can merge IO requests if the physical
2975 * pages are ordered properly.
Mel Gorman81eabcb2007-12-17 16:20:05 -08002976 */
Vlastimil Babka0fac3ba2017-11-15 17:38:07 -08002977 list_add_tail(&page->lru, list);
Mel Gormancb66bed2021-04-29 23:01:42 -07002978 allocated++;
Vlastimil Babkabb14c2c2015-09-08 15:01:25 -07002979 if (is_migrate_cma(get_pcppage_migratetype(page)))
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -07002980 __mod_zone_page_state(zone, NR_FREE_CMA_PAGES,
2981 -(1 << order));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 }
Mel Gormana6de7342016-12-12 16:44:41 -08002983
2984 /*
2985 * i pages were removed from the buddy list even if some leak due
2986 * to check_pcp_refill failing so adjust NR_FREE_PAGES based
Mel Gormancb66bed2021-04-29 23:01:42 -07002987 * on i. Do not confuse with 'allocated' which is the number of
Mel Gormana6de7342016-12-12 16:44:41 -08002988 * pages added to the pcp list.
2989 */
Mel Gormanf2260e62009-06-16 15:32:13 -07002990 __mod_zone_page_state(zone, NR_FREE_PAGES, -(i << order));
Mel Gormand34b0732017-04-20 14:37:43 -07002991 spin_unlock(&zone->lock);
Mel Gormancb66bed2021-04-29 23:01:42 -07002992 return allocated;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993}
2994
Christoph Lameter4ae7c032005-06-21 17:14:57 -07002995#ifdef CONFIG_NUMA
Christoph Lameter8fce4d82006-03-09 17:33:54 -08002996/*
Christoph Lameter4037d452007-05-09 02:35:14 -07002997 * Called from the vmstat counter updater to drain pagesets of this
2998 * currently executing processor on remote nodes after they have
2999 * expired.
3000 *
Christoph Lameter879336c2006-03-22 00:09:08 -08003001 * Note that this function must be called with the thread pinned to
3002 * a single processor.
Christoph Lameter8fce4d82006-03-09 17:33:54 -08003003 */
Christoph Lameter4037d452007-05-09 02:35:14 -07003004void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
Christoph Lameter4ae7c032005-06-21 17:14:57 -07003005{
Christoph Lameter4ae7c032005-06-21 17:14:57 -07003006 unsigned long flags;
Michal Nazarewicz7be12fc2014-08-06 16:05:15 -07003007 int to_drain, batch;
Christoph Lameter4ae7c032005-06-21 17:14:57 -07003008
Christoph Lameter4037d452007-05-09 02:35:14 -07003009 local_irq_save(flags);
Jason Low4db0c3c2015-04-15 16:14:08 -07003010 batch = READ_ONCE(pcp->batch);
Michal Nazarewicz7be12fc2014-08-06 16:05:15 -07003011 to_drain = min(pcp->count, batch);
Aaron Lu77ba9062018-04-05 16:24:06 -07003012 if (to_drain > 0)
KOSAKI Motohiro2a135152012-07-31 16:42:53 -07003013 free_pcppages_bulk(zone, to_drain, pcp);
Christoph Lameter4037d452007-05-09 02:35:14 -07003014 local_irq_restore(flags);
Christoph Lameter4ae7c032005-06-21 17:14:57 -07003015}
3016#endif
3017
Christoph Lameter9f8f2172008-02-04 22:29:11 -08003018/*
Vlastimil Babka93481ff2014-12-10 15:43:01 -08003019 * Drain pcplists of the indicated processor and zone.
3020 *
3021 * The processor must either be the current processor and the
3022 * thread pinned to the current processor or a processor that
3023 * is not online.
3024 */
3025static void drain_pages_zone(unsigned int cpu, struct zone *zone)
3026{
3027 unsigned long flags;
3028 struct per_cpu_pageset *pset;
3029 struct per_cpu_pages *pcp;
3030
3031 local_irq_save(flags);
3032 pset = per_cpu_ptr(zone->pageset, cpu);
3033
3034 pcp = &pset->pcp;
Aaron Lu77ba9062018-04-05 16:24:06 -07003035 if (pcp->count)
Vlastimil Babka93481ff2014-12-10 15:43:01 -08003036 free_pcppages_bulk(zone, pcp->count, pcp);
Vlastimil Babka93481ff2014-12-10 15:43:01 -08003037 local_irq_restore(flags);
3038}
3039
3040/*
3041 * Drain pcplists of all zones on the indicated processor.
Christoph Lameter9f8f2172008-02-04 22:29:11 -08003042 *
3043 * The processor must either be the current processor and the
3044 * thread pinned to the current processor or a processor that
3045 * is not online.
3046 */
3047static void drain_pages(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048{
3049 struct zone *zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
KOSAKI Motohiroee99c712009-03-31 15:19:31 -07003051 for_each_populated_zone(zone) {
Vlastimil Babka93481ff2014-12-10 15:43:01 -08003052 drain_pages_zone(cpu, zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 }
3054}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055
Christoph Lameter9f8f2172008-02-04 22:29:11 -08003056/*
3057 * Spill all of this CPU's per-cpu pages back into the buddy allocator.
Vlastimil Babka93481ff2014-12-10 15:43:01 -08003058 *
3059 * The CPU has to be pinned. When zone parameter is non-NULL, spill just
3060 * the single zone's pages.
Christoph Lameter9f8f2172008-02-04 22:29:11 -08003061 */
Vlastimil Babka93481ff2014-12-10 15:43:01 -08003062void drain_local_pages(struct zone *zone)
Christoph Lameter9f8f2172008-02-04 22:29:11 -08003063{
Vlastimil Babka93481ff2014-12-10 15:43:01 -08003064 int cpu = smp_processor_id();
3065
3066 if (zone)
3067 drain_pages_zone(cpu, zone);
3068 else
3069 drain_pages(cpu);
Christoph Lameter9f8f2172008-02-04 22:29:11 -08003070}
3071
Mel Gorman0ccce3b2017-02-24 14:56:32 -08003072static void drain_local_pages_wq(struct work_struct *work)
3073{
Wei Yangd9367bd2018-12-28 00:38:58 -08003074 struct pcpu_drain *drain;
3075
3076 drain = container_of(work, struct pcpu_drain, work);
3077
Michal Hockoa459eeb2017-02-24 14:56:35 -08003078 /*
3079 * drain_all_pages doesn't use proper cpu hotplug protection so
3080 * we can race with cpu offline when the WQ can move this from
3081 * a cpu pinned worker to an unbound one. We can operate on a different
Ingo Molnarf0953a12021-05-06 18:06:47 -07003082 * cpu which is alright but we also have to make sure to not move to
Michal Hockoa459eeb2017-02-24 14:56:35 -08003083 * a different one.
3084 */
3085 preempt_disable();
Wei Yangd9367bd2018-12-28 00:38:58 -08003086 drain_local_pages(drain->zone);
Michal Hockoa459eeb2017-02-24 14:56:35 -08003087 preempt_enable();
Mel Gorman0ccce3b2017-02-24 14:56:32 -08003088}
3089
Christoph Lameter9f8f2172008-02-04 22:29:11 -08003090/*
Vlastimil Babkaec6e8c7e2020-12-14 19:10:59 -08003091 * The implementation of drain_all_pages(), exposing an extra parameter to
3092 * drain on all cpus.
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07003093 *
Vlastimil Babkaec6e8c7e2020-12-14 19:10:59 -08003094 * drain_all_pages() is optimized to only execute on cpus where pcplists are
3095 * not empty. The check for non-emptiness can however race with a free to
3096 * pcplist that has not yet increased the pcp->count from 0 to 1. Callers
3097 * that need the guarantee that every CPU has drained can disable the
3098 * optimizing racy check.
Christoph Lameter9f8f2172008-02-04 22:29:11 -08003099 */
Zou Wei3b1f3652020-12-14 19:11:12 -08003100static void __drain_all_pages(struct zone *zone, bool force_all_cpus)
Christoph Lameter9f8f2172008-02-04 22:29:11 -08003101{
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07003102 int cpu;
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07003103
3104 /*
3105 * Allocate in the BSS so we wont require allocation in
3106 * direct reclaim path for CONFIG_CPUMASK_OFFSTACK=y
3107 */
3108 static cpumask_t cpus_with_pcps;
3109
Michal Hockoce612872017-04-07 16:05:05 -07003110 /*
3111 * Make sure nobody triggers this path before mm_percpu_wq is fully
3112 * initialized.
3113 */
3114 if (WARN_ON_ONCE(!mm_percpu_wq))
3115 return;
3116
Mel Gormanbd233f52017-02-24 14:56:56 -08003117 /*
3118 * Do not drain if one is already in progress unless it's specific to
3119 * a zone. Such callers are primarily CMA and memory hotplug and need
3120 * the drain to be complete when the call returns.
3121 */
3122 if (unlikely(!mutex_trylock(&pcpu_drain_mutex))) {
3123 if (!zone)
3124 return;
3125 mutex_lock(&pcpu_drain_mutex);
3126 }
Mel Gorman0ccce3b2017-02-24 14:56:32 -08003127
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07003128 /*
3129 * We don't care about racing with CPU hotplug event
3130 * as offline notification will cause the notified
3131 * cpu to drain that CPU pcps and on_each_cpu_mask
3132 * disables preemption as part of its processing
3133 */
3134 for_each_online_cpu(cpu) {
Vlastimil Babka93481ff2014-12-10 15:43:01 -08003135 struct per_cpu_pageset *pcp;
3136 struct zone *z;
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07003137 bool has_pcps = false;
Vlastimil Babka93481ff2014-12-10 15:43:01 -08003138
Vlastimil Babkaec6e8c7e2020-12-14 19:10:59 -08003139 if (force_all_cpus) {
3140 /*
3141 * The pcp.count check is racy, some callers need a
3142 * guarantee that no cpu is missed.
3143 */
3144 has_pcps = true;
3145 } else if (zone) {
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07003146 pcp = per_cpu_ptr(zone->pageset, cpu);
Vlastimil Babka93481ff2014-12-10 15:43:01 -08003147 if (pcp->pcp.count)
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07003148 has_pcps = true;
Vlastimil Babka93481ff2014-12-10 15:43:01 -08003149 } else {
3150 for_each_populated_zone(z) {
3151 pcp = per_cpu_ptr(z->pageset, cpu);
3152 if (pcp->pcp.count) {
3153 has_pcps = true;
3154 break;
3155 }
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07003156 }
3157 }
Vlastimil Babka93481ff2014-12-10 15:43:01 -08003158
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07003159 if (has_pcps)
3160 cpumask_set_cpu(cpu, &cpus_with_pcps);
3161 else
3162 cpumask_clear_cpu(cpu, &cpus_with_pcps);
3163 }
Mel Gorman0ccce3b2017-02-24 14:56:32 -08003164
Mel Gormanbd233f52017-02-24 14:56:56 -08003165 for_each_cpu(cpu, &cpus_with_pcps) {
Wei Yangd9367bd2018-12-28 00:38:58 -08003166 struct pcpu_drain *drain = per_cpu_ptr(&pcpu_drain, cpu);
3167
3168 drain->zone = zone;
3169 INIT_WORK(&drain->work, drain_local_pages_wq);
3170 queue_work_on(cpu, mm_percpu_wq, &drain->work);
Mel Gorman0ccce3b2017-02-24 14:56:32 -08003171 }
Mel Gormanbd233f52017-02-24 14:56:56 -08003172 for_each_cpu(cpu, &cpus_with_pcps)
Wei Yangd9367bd2018-12-28 00:38:58 -08003173 flush_work(&per_cpu_ptr(&pcpu_drain, cpu)->work);
Mel Gormanbd233f52017-02-24 14:56:56 -08003174
3175 mutex_unlock(&pcpu_drain_mutex);
Christoph Lameter9f8f2172008-02-04 22:29:11 -08003176}
3177
Vlastimil Babkaec6e8c7e2020-12-14 19:10:59 -08003178/*
3179 * Spill all the per-cpu pages from all CPUs back into the buddy allocator.
3180 *
3181 * When zone parameter is non-NULL, spill just the single zone's pages.
3182 *
3183 * Note that this can be extremely slow as the draining happens in a workqueue.
3184 */
3185void drain_all_pages(struct zone *zone)
3186{
3187 __drain_all_pages(zone, false);
3188}
3189
Rafael J. Wysocki296699d2007-07-29 23:27:18 +02003190#ifdef CONFIG_HIBERNATION
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
Chen Yu556b9692017-08-25 15:55:30 -07003192/*
3193 * Touch the watchdog for every WD_PAGE_COUNT pages.
3194 */
3195#define WD_PAGE_COUNT (128*1024)
3196
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197void mark_free_pages(struct zone *zone)
3198{
Chen Yu556b9692017-08-25 15:55:30 -07003199 unsigned long pfn, max_zone_pfn, page_count = WD_PAGE_COUNT;
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07003200 unsigned long flags;
Mel Gorman7aeb09f2014-06-04 16:10:21 -07003201 unsigned int order, t;
Geliang Tang86760a22016-01-14 15:20:33 -08003202 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
Xishi Qiu8080fc02013-09-11 14:21:45 -07003204 if (zone_is_empty(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 return;
3206
3207 spin_lock_irqsave(&zone->lock, flags);
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07003208
Cody P Schafer108bcc92013-02-22 16:35:23 -08003209 max_zone_pfn = zone_end_pfn(zone);
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07003210 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
3211 if (pfn_valid(pfn)) {
Geliang Tang86760a22016-01-14 15:20:33 -08003212 page = pfn_to_page(pfn);
Joonsoo Kimba6b0972016-05-19 17:12:16 -07003213
Chen Yu556b9692017-08-25 15:55:30 -07003214 if (!--page_count) {
3215 touch_nmi_watchdog();
3216 page_count = WD_PAGE_COUNT;
3217 }
3218
Joonsoo Kimba6b0972016-05-19 17:12:16 -07003219 if (page_zone(page) != zone)
3220 continue;
3221
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07003222 if (!swsusp_page_is_forbidden(page))
3223 swsusp_unset_page_free(page);
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07003224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225
Mel Gormanb2a0ac82007-10-16 01:25:48 -07003226 for_each_migratetype_order(order, t) {
Geliang Tang86760a22016-01-14 15:20:33 -08003227 list_for_each_entry(page,
3228 &zone->free_area[order].free_list[t], lru) {
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07003229 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
Geliang Tang86760a22016-01-14 15:20:33 -08003231 pfn = page_to_pfn(page);
Chen Yu556b9692017-08-25 15:55:30 -07003232 for (i = 0; i < (1UL << order); i++) {
3233 if (!--page_count) {
3234 touch_nmi_watchdog();
3235 page_count = WD_PAGE_COUNT;
3236 }
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07003237 swsusp_set_page_free(pfn_to_page(pfn + i));
Chen Yu556b9692017-08-25 15:55:30 -07003238 }
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07003239 }
Mel Gormanb2a0ac82007-10-16 01:25:48 -07003240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 spin_unlock_irqrestore(&zone->lock, flags);
3242}
Mel Gormane2c55dc2007-10-16 01:25:50 -07003243#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
Mel Gorman2d4894b2017-11-15 17:37:59 -08003245static bool free_unref_page_prepare(struct page *page, unsigned long pfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246{
Mel Gorman5f8dcc22009-09-21 17:03:19 -07003247 int migratetype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248
Mel Gorman4db75482016-05-19 17:14:32 -07003249 if (!free_pcp_prepare(page))
Mel Gorman9cca35d42017-11-15 17:37:37 -08003250 return false;
Hugh Dickins689bceb2005-11-21 21:32:20 -08003251
Mel Gormandc4b0ca2014-06-04 16:10:17 -07003252 migratetype = get_pfnblock_migratetype(page, pfn);
Vlastimil Babkabb14c2c2015-09-08 15:01:25 -07003253 set_pcppage_migratetype(page, migratetype);
Mel Gorman9cca35d42017-11-15 17:37:37 -08003254 return true;
3255}
3256
Mel Gorman2d4894b2017-11-15 17:37:59 -08003257static void free_unref_page_commit(struct page *page, unsigned long pfn)
Mel Gorman9cca35d42017-11-15 17:37:37 -08003258{
3259 struct zone *zone = page_zone(page);
3260 struct per_cpu_pages *pcp;
3261 int migratetype;
3262
3263 migratetype = get_pcppage_migratetype(page);
Mel Gormand34b0732017-04-20 14:37:43 -07003264 __count_vm_event(PGFREE);
Mel Gormanda456f12009-06-16 15:32:08 -07003265
Mel Gorman5f8dcc22009-09-21 17:03:19 -07003266 /*
3267 * We only track unmovable, reclaimable and movable on pcp lists.
3268 * Free ISOLATE pages back to the allocator because they are being
Xishi Qiua6ffdc02017-05-03 14:52:52 -07003269 * offlined but treat HIGHATOMIC as movable pages so we can get those
Mel Gorman5f8dcc22009-09-21 17:03:19 -07003270 * areas back if necessary. Otherwise, we may have to free
3271 * excessively into the page allocator
3272 */
3273 if (migratetype >= MIGRATE_PCPTYPES) {
Minchan Kim194159f2013-02-22 16:33:58 -08003274 if (unlikely(is_migrate_isolate(migratetype))) {
David Hildenbrand7fef4312020-10-15 20:09:35 -07003275 free_one_page(zone, page, pfn, 0, migratetype,
3276 FPI_NONE);
Mel Gorman9cca35d42017-11-15 17:37:37 -08003277 return;
Mel Gorman5f8dcc22009-09-21 17:03:19 -07003278 }
3279 migratetype = MIGRATE_MOVABLE;
3280 }
3281
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09003282 pcp = &this_cpu_ptr(zone->pageset)->pcp;
Mel Gorman2d4894b2017-11-15 17:37:59 -08003283 list_add(&page->lru, &pcp->lists[migratetype]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 pcp->count++;
Vlastimil Babka5c3ad2e2020-12-14 19:10:50 -08003285 if (pcp->count >= READ_ONCE(pcp->high))
3286 free_pcppages_bulk(zone, READ_ONCE(pcp->batch), pcp);
Mel Gorman9cca35d42017-11-15 17:37:37 -08003287}
Mel Gorman5f8dcc22009-09-21 17:03:19 -07003288
Mel Gorman9cca35d42017-11-15 17:37:37 -08003289/*
3290 * Free a 0-order page
Mel Gorman9cca35d42017-11-15 17:37:37 -08003291 */
Mel Gorman2d4894b2017-11-15 17:37:59 -08003292void free_unref_page(struct page *page)
Mel Gorman9cca35d42017-11-15 17:37:37 -08003293{
3294 unsigned long flags;
3295 unsigned long pfn = page_to_pfn(page);
3296
Mel Gorman2d4894b2017-11-15 17:37:59 -08003297 if (!free_unref_page_prepare(page, pfn))
Mel Gorman9cca35d42017-11-15 17:37:37 -08003298 return;
3299
3300 local_irq_save(flags);
Mel Gorman2d4894b2017-11-15 17:37:59 -08003301 free_unref_page_commit(page, pfn);
Mel Gormand34b0732017-04-20 14:37:43 -07003302 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303}
3304
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08003305/*
Konstantin Khlebnikovcc598502012-01-10 15:07:04 -08003306 * Free a list of 0-order pages
3307 */
Mel Gorman2d4894b2017-11-15 17:37:59 -08003308void free_unref_page_list(struct list_head *list)
Konstantin Khlebnikovcc598502012-01-10 15:07:04 -08003309{
3310 struct page *page, *next;
Mel Gorman9cca35d42017-11-15 17:37:37 -08003311 unsigned long flags, pfn;
Lucas Stachc24ad772017-12-14 15:32:55 -08003312 int batch_count = 0;
Konstantin Khlebnikovcc598502012-01-10 15:07:04 -08003313
Mel Gorman9cca35d42017-11-15 17:37:37 -08003314 /* Prepare pages for freeing */
Konstantin Khlebnikovcc598502012-01-10 15:07:04 -08003315 list_for_each_entry_safe(page, next, list, lru) {
Mel Gorman9cca35d42017-11-15 17:37:37 -08003316 pfn = page_to_pfn(page);
Mel Gorman2d4894b2017-11-15 17:37:59 -08003317 if (!free_unref_page_prepare(page, pfn))
Mel Gorman9cca35d42017-11-15 17:37:37 -08003318 list_del(&page->lru);
3319 set_page_private(page, pfn);
Konstantin Khlebnikovcc598502012-01-10 15:07:04 -08003320 }
Mel Gorman9cca35d42017-11-15 17:37:37 -08003321
3322 local_irq_save(flags);
3323 list_for_each_entry_safe(page, next, list, lru) {
3324 unsigned long pfn = page_private(page);
3325
3326 set_page_private(page, 0);
Mel Gorman2d4894b2017-11-15 17:37:59 -08003327 trace_mm_page_free_batched(page);
3328 free_unref_page_commit(page, pfn);
Lucas Stachc24ad772017-12-14 15:32:55 -08003329
3330 /*
3331 * Guard against excessive IRQ disabled times when we get
3332 * a large list of pages to free.
3333 */
3334 if (++batch_count == SWAP_CLUSTER_MAX) {
3335 local_irq_restore(flags);
3336 batch_count = 0;
3337 local_irq_save(flags);
3338 }
Mel Gorman9cca35d42017-11-15 17:37:37 -08003339 }
3340 local_irq_restore(flags);
Konstantin Khlebnikovcc598502012-01-10 15:07:04 -08003341}
3342
3343/*
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08003344 * split_page takes a non-compound higher-order page, and splits it into
3345 * n (1<<order) sub-pages: page[0..n]
3346 * Each sub-page must be freed individually.
3347 *
3348 * Note: this is probably too low level an operation for use in drivers.
3349 * Please consult with lkml before using this in your driver.
3350 */
3351void split_page(struct page *page, unsigned int order)
3352{
3353 int i;
3354
Sasha Levin309381fea2014-01-23 15:52:54 -08003355 VM_BUG_ON_PAGE(PageCompound(page), page);
3356 VM_BUG_ON_PAGE(!page_count(page), page);
Vegard Nossumb1eeab62008-11-25 16:55:53 +01003357
Joonsoo Kima9627bc2016-07-26 15:23:49 -07003358 for (i = 1; i < (1 << order); i++)
Nick Piggin7835e982006-03-22 00:08:40 -08003359 set_page_refcounted(page + i);
Matthew Wilcox (Oracle)8fb156c2020-10-15 20:05:29 -07003360 split_page_owner(page, 1 << order);
Zhou Guanghuie1baddf2021-03-12 21:08:33 -08003361 split_page_memcg(page, 1 << order);
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08003362}
K. Y. Srinivasan5853ff22013-03-25 15:47:38 -07003363EXPORT_SYMBOL_GPL(split_page);
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08003364
Joonsoo Kim3c605092014-11-13 15:19:21 -08003365int __isolate_free_page(struct page *page, unsigned int order)
Mel Gorman748446b2010-05-24 14:32:27 -07003366{
Mel Gorman748446b2010-05-24 14:32:27 -07003367 unsigned long watermark;
3368 struct zone *zone;
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -07003369 int mt;
Mel Gorman748446b2010-05-24 14:32:27 -07003370
3371 BUG_ON(!PageBuddy(page));
3372
3373 zone = page_zone(page);
Marek Szyprowski2e30abd2012-12-11 16:02:57 -08003374 mt = get_pageblock_migratetype(page);
Mel Gorman748446b2010-05-24 14:32:27 -07003375
Minchan Kim194159f2013-02-22 16:33:58 -08003376 if (!is_migrate_isolate(mt)) {
Vlastimil Babka8348faf2016-10-07 16:58:00 -07003377 /*
3378 * Obey watermarks as if the page was being allocated. We can
3379 * emulate a high-order watermark check with a raised order-0
3380 * watermark, because we already know our high-order page
3381 * exists.
3382 */
Mel Gormanfd1444b2019-03-05 15:44:50 -08003383 watermark = zone->_watermark[WMARK_MIN] + (1UL << order);
Joonsoo Kimd883c6c2018-05-23 10:18:21 +09003384 if (!zone_watermark_ok(zone, 0, watermark, 0, ALLOC_CMA))
Marek Szyprowski2e30abd2012-12-11 16:02:57 -08003385 return 0;
3386
Mel Gorman8fb74b92013-01-11 14:32:16 -08003387 __mod_zone_freepage_state(zone, -(1UL << order), mt);
Marek Szyprowski2e30abd2012-12-11 16:02:57 -08003388 }
Mel Gorman748446b2010-05-24 14:32:27 -07003389
3390 /* Remove page from free list */
Dan Williamsb03641a2019-05-14 15:41:32 -07003391
Alexander Duyck6ab01362020-04-06 20:04:49 -07003392 del_page_from_free_list(page, zone, order);
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -07003393
zhong jiang400bc7f2016-07-28 15:45:07 -07003394 /*
3395 * Set the pageblock if the isolated page is at least half of a
3396 * pageblock
3397 */
Mel Gorman748446b2010-05-24 14:32:27 -07003398 if (order >= pageblock_order - 1) {
3399 struct page *endpage = page + (1 << order) - 1;
Michal Nazarewicz47118af2011-12-29 13:09:50 +01003400 for (; page < endpage; page += pageblock_nr_pages) {
3401 int mt = get_pageblock_migratetype(page);
Minchan Kim88ed3652016-12-12 16:42:05 -08003402 if (!is_migrate_isolate(mt) && !is_migrate_cma(mt)
Xishi Qiua6ffdc02017-05-03 14:52:52 -07003403 && !is_migrate_highatomic(mt))
Michal Nazarewicz47118af2011-12-29 13:09:50 +01003404 set_pageblock_migratetype(page,
3405 MIGRATE_MOVABLE);
3406 }
Mel Gorman748446b2010-05-24 14:32:27 -07003407 }
3408
Joonsoo Kimf3a14ce2015-07-17 16:24:15 -07003409
Mel Gorman8fb74b92013-01-11 14:32:16 -08003410 return 1UL << order;
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07003411}
3412
Alexander Duyck624f58d2020-04-06 20:04:53 -07003413/**
3414 * __putback_isolated_page - Return a now-isolated page back where we got it
3415 * @page: Page that was isolated
3416 * @order: Order of the isolated page
Randy Dunlape6a0a7a2020-04-10 14:32:29 -07003417 * @mt: The page's pageblock's migratetype
Alexander Duyck624f58d2020-04-06 20:04:53 -07003418 *
3419 * This function is meant to return a page pulled from the free lists via
3420 * __isolate_free_page back to the free lists they were pulled from.
3421 */
3422void __putback_isolated_page(struct page *page, unsigned int order, int mt)
3423{
3424 struct zone *zone = page_zone(page);
3425
3426 /* zone lock should be held when this function is called */
3427 lockdep_assert_held(&zone->lock);
3428
3429 /* Return isolated page to tail of freelist. */
David Hildenbrandf04a5d52020-10-15 20:09:20 -07003430 __free_one_page(page, page_to_pfn(page), zone, order, mt,
David Hildenbrand47b6a24a22020-10-15 20:09:26 -07003431 FPI_SKIP_REPORT_NOTIFY | FPI_TO_TAIL);
Alexander Duyck624f58d2020-04-06 20:04:53 -07003432}
3433
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07003434/*
Mel Gorman060e7412016-05-19 17:13:27 -07003435 * Update NUMA hit/miss statistics
3436 *
3437 * Must be called with interrupts disabled.
Mel Gorman060e7412016-05-19 17:13:27 -07003438 */
Michal Hocko41b61672017-01-10 16:57:42 -08003439static inline void zone_statistics(struct zone *preferred_zone, struct zone *z)
Mel Gorman060e7412016-05-19 17:13:27 -07003440{
3441#ifdef CONFIG_NUMA
Kemi Wang3a321d22017-09-08 16:12:48 -07003442 enum numa_stat_item local_stat = NUMA_LOCAL;
Mel Gorman060e7412016-05-19 17:13:27 -07003443
Kemi Wang45180852017-11-15 17:38:22 -08003444 /* skip numa counters update if numa stats is disabled */
3445 if (!static_branch_likely(&vm_numa_stat_key))
3446 return;
3447
Pavel Tatashinc1093b72018-08-21 21:53:32 -07003448 if (zone_to_nid(z) != numa_node_id())
Mel Gorman060e7412016-05-19 17:13:27 -07003449 local_stat = NUMA_OTHER;
Mel Gorman060e7412016-05-19 17:13:27 -07003450
Pavel Tatashinc1093b72018-08-21 21:53:32 -07003451 if (zone_to_nid(z) == zone_to_nid(preferred_zone))
Kemi Wang3a321d22017-09-08 16:12:48 -07003452 __inc_numa_state(z, NUMA_HIT);
Michal Hocko2df26632017-01-10 16:57:39 -08003453 else {
Kemi Wang3a321d22017-09-08 16:12:48 -07003454 __inc_numa_state(z, NUMA_MISS);
3455 __inc_numa_state(preferred_zone, NUMA_FOREIGN);
Mel Gorman060e7412016-05-19 17:13:27 -07003456 }
Kemi Wang3a321d22017-09-08 16:12:48 -07003457 __inc_numa_state(z, local_stat);
Mel Gorman060e7412016-05-19 17:13:27 -07003458#endif
3459}
3460
Mel Gorman066b2392017-02-24 14:56:26 -08003461/* Remove page from the per-cpu list, caller must protect the list */
Jesper Dangaard Brouer3b822012021-04-29 23:01:55 -07003462static inline
3463struct page *__rmqueue_pcplist(struct zone *zone, int migratetype,
Mel Gorman6bb15452018-12-28 00:35:41 -08003464 unsigned int alloc_flags,
Mel Gorman453f85d2017-11-15 17:38:03 -08003465 struct per_cpu_pages *pcp,
Mel Gorman066b2392017-02-24 14:56:26 -08003466 struct list_head *list)
3467{
3468 struct page *page;
3469
3470 do {
3471 if (list_empty(list)) {
3472 pcp->count += rmqueue_bulk(zone, 0,
Vlastimil Babka5c3ad2e2020-12-14 19:10:50 -08003473 READ_ONCE(pcp->batch), list,
Mel Gorman6bb15452018-12-28 00:35:41 -08003474 migratetype, alloc_flags);
Mel Gorman066b2392017-02-24 14:56:26 -08003475 if (unlikely(list_empty(list)))
3476 return NULL;
3477 }
3478
Mel Gorman453f85d2017-11-15 17:38:03 -08003479 page = list_first_entry(list, struct page, lru);
Mel Gorman066b2392017-02-24 14:56:26 -08003480 list_del(&page->lru);
3481 pcp->count--;
3482 } while (check_new_pcp(page));
3483
3484 return page;
3485}
3486
3487/* Lock and remove page from the per-cpu list */
3488static struct page *rmqueue_pcplist(struct zone *preferred_zone,
Yafang Shao1c52e6d2019-05-13 17:22:40 -07003489 struct zone *zone, gfp_t gfp_flags,
3490 int migratetype, unsigned int alloc_flags)
Mel Gorman066b2392017-02-24 14:56:26 -08003491{
3492 struct per_cpu_pages *pcp;
3493 struct list_head *list;
Mel Gorman066b2392017-02-24 14:56:26 -08003494 struct page *page;
Mel Gormand34b0732017-04-20 14:37:43 -07003495 unsigned long flags;
Mel Gorman066b2392017-02-24 14:56:26 -08003496
Mel Gormand34b0732017-04-20 14:37:43 -07003497 local_irq_save(flags);
Mel Gorman066b2392017-02-24 14:56:26 -08003498 pcp = &this_cpu_ptr(zone->pageset)->pcp;
3499 list = &pcp->lists[migratetype];
Mel Gorman6bb15452018-12-28 00:35:41 -08003500 page = __rmqueue_pcplist(zone, migratetype, alloc_flags, pcp, list);
Mel Gorman066b2392017-02-24 14:56:26 -08003501 if (page) {
Yafang Shao1c52e6d2019-05-13 17:22:40 -07003502 __count_zid_vm_events(PGALLOC, page_zonenum(page), 1);
Mel Gorman066b2392017-02-24 14:56:26 -08003503 zone_statistics(preferred_zone, zone);
3504 }
Mel Gormand34b0732017-04-20 14:37:43 -07003505 local_irq_restore(flags);
Mel Gorman066b2392017-02-24 14:56:26 -08003506 return page;
3507}
3508
Mel Gorman060e7412016-05-19 17:13:27 -07003509/*
Vlastimil Babka75379192015-02-11 15:25:38 -08003510 * Allocate a page from the given zone. Use pcplists for order-0 allocations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 */
Mel Gorman0a15c3e2009-06-16 15:32:05 -07003512static inline
Mel Gorman066b2392017-02-24 14:56:26 -08003513struct page *rmqueue(struct zone *preferred_zone,
Mel Gorman7aeb09f2014-06-04 16:10:21 -07003514 struct zone *zone, unsigned int order,
Mel Gormanc6038442016-05-19 17:13:38 -07003515 gfp_t gfp_flags, unsigned int alloc_flags,
3516 int migratetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517{
3518 unsigned long flags;
Hugh Dickins689bceb2005-11-21 21:32:20 -08003519 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520
Mel Gormand34b0732017-04-20 14:37:43 -07003521 if (likely(order == 0)) {
Joonsoo Kim1d91df82020-10-02 22:21:45 -07003522 /*
3523 * MIGRATE_MOVABLE pcplist could have the pages on CMA area and
3524 * we need to skip it when CMA area isn't allowed.
3525 */
3526 if (!IS_ENABLED(CONFIG_CMA) || alloc_flags & ALLOC_CMA ||
3527 migratetype != MIGRATE_MOVABLE) {
3528 page = rmqueue_pcplist(preferred_zone, zone, gfp_flags,
Yafang Shao1c52e6d2019-05-13 17:22:40 -07003529 migratetype, alloc_flags);
Joonsoo Kim1d91df82020-10-02 22:21:45 -07003530 goto out;
3531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 }
3533
Mel Gorman066b2392017-02-24 14:56:26 -08003534 /*
3535 * We most definitely don't want callers attempting to
3536 * allocate greater than order-1 page units with __GFP_NOFAIL.
3537 */
3538 WARN_ON_ONCE((gfp_flags & __GFP_NOFAIL) && (order > 1));
3539 spin_lock_irqsave(&zone->lock, flags);
3540
3541 do {
3542 page = NULL;
Joonsoo Kim1d91df82020-10-02 22:21:45 -07003543 /*
3544 * order-0 request can reach here when the pcplist is skipped
3545 * due to non-CMA allocation context. HIGHATOMIC area is
3546 * reserved for high-order atomic allocation, so order-0
3547 * request should skip it.
3548 */
3549 if (order > 0 && alloc_flags & ALLOC_HARDER) {
Mel Gorman066b2392017-02-24 14:56:26 -08003550 page = __rmqueue_smallest(zone, order, MIGRATE_HIGHATOMIC);
3551 if (page)
3552 trace_mm_page_alloc_zone_locked(page, order, migratetype);
3553 }
3554 if (!page)
Mel Gorman6bb15452018-12-28 00:35:41 -08003555 page = __rmqueue(zone, order, migratetype, alloc_flags);
Mel Gorman066b2392017-02-24 14:56:26 -08003556 } while (page && check_new_pages(page, order));
3557 spin_unlock(&zone->lock);
3558 if (!page)
3559 goto failed;
3560 __mod_zone_freepage_state(zone, -(1 << order),
3561 get_pcppage_migratetype(page));
3562
Mel Gorman16709d12016-07-28 15:46:56 -07003563 __count_zid_vm_events(PGALLOC, page_zonenum(page), 1 << order);
Michal Hocko41b61672017-01-10 16:57:42 -08003564 zone_statistics(preferred_zone, zone);
Nick Piggina74609f2006-01-06 00:11:20 -08003565 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
Mel Gorman066b2392017-02-24 14:56:26 -08003567out:
Mel Gorman73444bc2019-01-08 15:23:39 -08003568 /* Separate test+clear to avoid unnecessary atomics */
3569 if (test_bit(ZONE_BOOSTED_WATERMARK, &zone->flags)) {
3570 clear_bit(ZONE_BOOSTED_WATERMARK, &zone->flags);
3571 wakeup_kswapd(zone, 0, 0, zone_idx(zone));
3572 }
3573
Mel Gorman066b2392017-02-24 14:56:26 -08003574 VM_BUG_ON_PAGE(page && bad_range(zone, page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 return page;
Nick Piggina74609f2006-01-06 00:11:20 -08003576
3577failed:
3578 local_irq_restore(flags);
Nick Piggina74609f2006-01-06 00:11:20 -08003579 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580}
3581
Akinobu Mita933e3122006-12-08 02:39:45 -08003582#ifdef CONFIG_FAIL_PAGE_ALLOC
3583
Akinobu Mitab2588c42011-07-26 16:09:03 -07003584static struct {
Akinobu Mita933e3122006-12-08 02:39:45 -08003585 struct fault_attr attr;
3586
Viresh Kumar621a5f72015-09-26 15:04:07 -07003587 bool ignore_gfp_highmem;
Mel Gorman71baba42015-11-06 16:28:28 -08003588 bool ignore_gfp_reclaim;
Akinobu Mita54114992007-07-15 23:40:23 -07003589 u32 min_order;
Akinobu Mita933e3122006-12-08 02:39:45 -08003590} fail_page_alloc = {
3591 .attr = FAULT_ATTR_INITIALIZER,
Mel Gorman71baba42015-11-06 16:28:28 -08003592 .ignore_gfp_reclaim = true,
Viresh Kumar621a5f72015-09-26 15:04:07 -07003593 .ignore_gfp_highmem = true,
Akinobu Mita54114992007-07-15 23:40:23 -07003594 .min_order = 1,
Akinobu Mita933e3122006-12-08 02:39:45 -08003595};
3596
3597static int __init setup_fail_page_alloc(char *str)
3598{
3599 return setup_fault_attr(&fail_page_alloc.attr, str);
3600}
3601__setup("fail_page_alloc=", setup_fail_page_alloc);
3602
Benjamin Poirieraf3b8542018-12-28 00:39:23 -08003603static bool __should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
Akinobu Mita933e3122006-12-08 02:39:45 -08003604{
Akinobu Mita54114992007-07-15 23:40:23 -07003605 if (order < fail_page_alloc.min_order)
Gavin Shandeaf3862012-07-31 16:41:51 -07003606 return false;
Akinobu Mita933e3122006-12-08 02:39:45 -08003607 if (gfp_mask & __GFP_NOFAIL)
Gavin Shandeaf3862012-07-31 16:41:51 -07003608 return false;
Akinobu Mita933e3122006-12-08 02:39:45 -08003609 if (fail_page_alloc.ignore_gfp_highmem && (gfp_mask & __GFP_HIGHMEM))
Gavin Shandeaf3862012-07-31 16:41:51 -07003610 return false;
Mel Gorman71baba42015-11-06 16:28:28 -08003611 if (fail_page_alloc.ignore_gfp_reclaim &&
3612 (gfp_mask & __GFP_DIRECT_RECLAIM))
Gavin Shandeaf3862012-07-31 16:41:51 -07003613 return false;
Akinobu Mita933e3122006-12-08 02:39:45 -08003614
3615 return should_fail(&fail_page_alloc.attr, 1 << order);
3616}
3617
3618#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
3619
3620static int __init fail_page_alloc_debugfs(void)
3621{
Joe Perches0825a6f2018-06-14 15:27:58 -07003622 umode_t mode = S_IFREG | 0600;
Akinobu Mita933e3122006-12-08 02:39:45 -08003623 struct dentry *dir;
Akinobu Mita933e3122006-12-08 02:39:45 -08003624
Akinobu Mitadd48c082011-08-03 16:21:01 -07003625 dir = fault_create_debugfs_attr("fail_page_alloc", NULL,
3626 &fail_page_alloc.attr);
Akinobu Mita933e3122006-12-08 02:39:45 -08003627
Greg Kroah-Hartmand9f79792019-03-05 15:46:09 -08003628 debugfs_create_bool("ignore-gfp-wait", mode, dir,
3629 &fail_page_alloc.ignore_gfp_reclaim);
3630 debugfs_create_bool("ignore-gfp-highmem", mode, dir,
3631 &fail_page_alloc.ignore_gfp_highmem);
3632 debugfs_create_u32("min-order", mode, dir, &fail_page_alloc.min_order);
Akinobu Mita933e3122006-12-08 02:39:45 -08003633
Akinobu Mitab2588c42011-07-26 16:09:03 -07003634 return 0;
Akinobu Mita933e3122006-12-08 02:39:45 -08003635}
3636
3637late_initcall(fail_page_alloc_debugfs);
3638
3639#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
3640
3641#else /* CONFIG_FAIL_PAGE_ALLOC */
3642
Benjamin Poirieraf3b8542018-12-28 00:39:23 -08003643static inline bool __should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
Akinobu Mita933e3122006-12-08 02:39:45 -08003644{
Gavin Shandeaf3862012-07-31 16:41:51 -07003645 return false;
Akinobu Mita933e3122006-12-08 02:39:45 -08003646}
3647
3648#endif /* CONFIG_FAIL_PAGE_ALLOC */
3649
Alexei Starovoitov76cd6172020-08-27 15:01:10 -07003650noinline bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
Benjamin Poirieraf3b8542018-12-28 00:39:23 -08003651{
3652 return __should_fail_alloc_page(gfp_mask, order);
3653}
3654ALLOW_ERROR_INJECTION(should_fail_alloc_page, TRUE);
3655
Jaewon Kimf27ce0e2020-08-06 23:25:20 -07003656static inline long __zone_watermark_unusable_free(struct zone *z,
3657 unsigned int order, unsigned int alloc_flags)
3658{
3659 const bool alloc_harder = (alloc_flags & (ALLOC_HARDER|ALLOC_OOM));
3660 long unusable_free = (1 << order) - 1;
3661
3662 /*
3663 * If the caller does not have rights to ALLOC_HARDER then subtract
3664 * the high-atomic reserves. This will over-estimate the size of the
3665 * atomic reserve but it avoids a search.
3666 */
3667 if (likely(!alloc_harder))
3668 unusable_free += z->nr_reserved_highatomic;
3669
3670#ifdef CONFIG_CMA
3671 /* If allocation can't use CMA areas don't use free CMA pages */
3672 if (!(alloc_flags & ALLOC_CMA))
3673 unusable_free += zone_page_state(z, NR_FREE_CMA_PAGES);
3674#endif
3675
3676 return unusable_free;
3677}
3678
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679/*
Mel Gorman97a16fc2015-11-06 16:28:40 -08003680 * Return true if free base pages are above 'mark'. For high-order checks it
3681 * will return true of the order-0 watermark is reached and there is at least
3682 * one free page of a suitable size. Checking now avoids taking the zone lock
3683 * to check in the allocation paths if no pages are free.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 */
Michal Hocko86a294a2016-05-20 16:57:12 -07003685bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003686 int highest_zoneidx, unsigned int alloc_flags,
Michal Hocko86a294a2016-05-20 16:57:12 -07003687 long free_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688{
Christoph Lameterd23ad422007-02-10 01:43:02 -08003689 long min = mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 int o;
Michal Hockocd04ae12017-09-06 16:24:50 -07003691 const bool alloc_harder = (alloc_flags & (ALLOC_HARDER|ALLOC_OOM));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692
Mel Gorman0aaa29a2015-11-06 16:28:37 -08003693 /* free_pages may go negative - that's OK */
Jaewon Kimf27ce0e2020-08-06 23:25:20 -07003694 free_pages -= __zone_watermark_unusable_free(z, order, alloc_flags);
Mel Gorman0aaa29a2015-11-06 16:28:37 -08003695
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08003696 if (alloc_flags & ALLOC_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 min -= min / 2;
Mel Gorman0aaa29a2015-11-06 16:28:37 -08003698
Jaewon Kimf27ce0e2020-08-06 23:25:20 -07003699 if (unlikely(alloc_harder)) {
Michal Hockocd04ae12017-09-06 16:24:50 -07003700 /*
3701 * OOM victims can try even harder than normal ALLOC_HARDER
3702 * users on the grounds that it's definitely going to be in
3703 * the exit path shortly and free memory. Any allocation it
3704 * makes during the free path will be small and short-lived.
3705 */
3706 if (alloc_flags & ALLOC_OOM)
3707 min -= min / 2;
3708 else
3709 min -= min / 4;
3710 }
3711
Mel Gorman97a16fc2015-11-06 16:28:40 -08003712 /*
3713 * Check watermarks for an order-0 allocation request. If these
3714 * are not met, then a high-order request also cannot go ahead
3715 * even if a suitable page happened to be free.
3716 */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003717 if (free_pages <= min + z->lowmem_reserve[highest_zoneidx])
Mel Gorman88f5acf2011-01-13 15:45:41 -08003718 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719
Mel Gorman97a16fc2015-11-06 16:28:40 -08003720 /* If this is an order-0 request then the watermark is fine */
3721 if (!order)
3722 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723
Mel Gorman97a16fc2015-11-06 16:28:40 -08003724 /* For a high-order request, check at least one suitable page is free */
3725 for (o = order; o < MAX_ORDER; o++) {
3726 struct free_area *area = &z->free_area[o];
3727 int mt;
3728
3729 if (!area->nr_free)
3730 continue;
3731
Mel Gorman97a16fc2015-11-06 16:28:40 -08003732 for (mt = 0; mt < MIGRATE_PCPTYPES; mt++) {
Dan Williamsb03641a2019-05-14 15:41:32 -07003733 if (!free_area_empty(area, mt))
Mel Gorman97a16fc2015-11-06 16:28:40 -08003734 return true;
3735 }
3736
3737#ifdef CONFIG_CMA
Joonsoo Kimd883c6c2018-05-23 10:18:21 +09003738 if ((alloc_flags & ALLOC_CMA) &&
Dan Williamsb03641a2019-05-14 15:41:32 -07003739 !free_area_empty(area, MIGRATE_CMA)) {
Mel Gorman97a16fc2015-11-06 16:28:40 -08003740 return true;
Joonsoo Kimd883c6c2018-05-23 10:18:21 +09003741 }
Mel Gorman97a16fc2015-11-06 16:28:40 -08003742#endif
chenqiwu76089d02020-04-01 21:09:50 -07003743 if (alloc_harder && !free_area_empty(area, MIGRATE_HIGHATOMIC))
Vlastimil Babkab050e372017-11-15 17:38:30 -08003744 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 }
Mel Gorman97a16fc2015-11-06 16:28:40 -08003746 return false;
Mel Gorman88f5acf2011-01-13 15:45:41 -08003747}
3748
Mel Gorman7aeb09f2014-06-04 16:10:21 -07003749bool zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003750 int highest_zoneidx, unsigned int alloc_flags)
Mel Gorman88f5acf2011-01-13 15:45:41 -08003751{
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003752 return __zone_watermark_ok(z, order, mark, highest_zoneidx, alloc_flags,
Mel Gorman88f5acf2011-01-13 15:45:41 -08003753 zone_page_state(z, NR_FREE_PAGES));
3754}
3755
Mel Gorman48ee5f32016-05-19 17:14:07 -07003756static inline bool zone_watermark_fast(struct zone *z, unsigned int order,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003757 unsigned long mark, int highest_zoneidx,
Charan Teja Reddyf80b08f2020-08-06 23:25:24 -07003758 unsigned int alloc_flags, gfp_t gfp_mask)
Mel Gorman48ee5f32016-05-19 17:14:07 -07003759{
Jaewon Kimf27ce0e2020-08-06 23:25:20 -07003760 long free_pages;
Joonsoo Kimd883c6c2018-05-23 10:18:21 +09003761
Jaewon Kimf27ce0e2020-08-06 23:25:20 -07003762 free_pages = zone_page_state(z, NR_FREE_PAGES);
Mel Gorman48ee5f32016-05-19 17:14:07 -07003763
3764 /*
3765 * Fast check for order-0 only. If this fails then the reserves
Jaewon Kimf27ce0e2020-08-06 23:25:20 -07003766 * need to be calculated.
Mel Gorman48ee5f32016-05-19 17:14:07 -07003767 */
Jaewon Kimf27ce0e2020-08-06 23:25:20 -07003768 if (!order) {
3769 long fast_free;
3770
3771 fast_free = free_pages;
3772 fast_free -= __zone_watermark_unusable_free(z, 0, alloc_flags);
3773 if (fast_free > mark + z->lowmem_reserve[highest_zoneidx])
3774 return true;
3775 }
Mel Gorman48ee5f32016-05-19 17:14:07 -07003776
Charan Teja Reddyf80b08f2020-08-06 23:25:24 -07003777 if (__zone_watermark_ok(z, order, mark, highest_zoneidx, alloc_flags,
3778 free_pages))
3779 return true;
3780 /*
3781 * Ignore watermark boosting for GFP_ATOMIC order-0 allocations
3782 * when checking the min watermark. The min watermark is the
3783 * point where boosting is ignored so that kswapd is woken up
3784 * when below the low watermark.
3785 */
3786 if (unlikely(!order && (gfp_mask & __GFP_ATOMIC) && z->watermark_boost
3787 && ((alloc_flags & ALLOC_WMARK_MASK) == WMARK_MIN))) {
3788 mark = z->_watermark[WMARK_MIN];
3789 return __zone_watermark_ok(z, order, mark, highest_zoneidx,
3790 alloc_flags, free_pages);
3791 }
3792
3793 return false;
Mel Gorman48ee5f32016-05-19 17:14:07 -07003794}
3795
Mel Gorman7aeb09f2014-06-04 16:10:21 -07003796bool zone_watermark_ok_safe(struct zone *z, unsigned int order,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003797 unsigned long mark, int highest_zoneidx)
Mel Gorman88f5acf2011-01-13 15:45:41 -08003798{
3799 long free_pages = zone_page_state(z, NR_FREE_PAGES);
3800
3801 if (z->percpu_drift_mark && free_pages < z->percpu_drift_mark)
3802 free_pages = zone_page_state_snapshot(z, NR_FREE_PAGES);
3803
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003804 return __zone_watermark_ok(z, order, mark, highest_zoneidx, 0,
Mel Gorman88f5acf2011-01-13 15:45:41 -08003805 free_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806}
3807
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003808#ifdef CONFIG_NUMA
David Rientjes957f8222012-10-08 16:33:24 -07003809static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone)
3810{
Gavin Shane02dc012017-02-24 14:59:33 -08003811 return node_distance(zone_to_nid(local_zone), zone_to_nid(zone)) <=
Matt Fleminga55c7452019-08-08 20:53:01 +01003812 node_reclaim_distance;
David Rientjes957f8222012-10-08 16:33:24 -07003813}
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003814#else /* CONFIG_NUMA */
David Rientjes957f8222012-10-08 16:33:24 -07003815static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone)
3816{
3817 return true;
3818}
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003819#endif /* CONFIG_NUMA */
3820
Mel Gorman6bb15452018-12-28 00:35:41 -08003821/*
3822 * The restriction on ZONE_DMA32 as being a suitable zone to use to avoid
3823 * fragmentation is subtle. If the preferred zone was HIGHMEM then
3824 * premature use of a lower zone may cause lowmem pressure problems that
3825 * are worse than fragmentation. If the next zone is ZONE_DMA then it is
3826 * probably too small. It only makes sense to spread allocations to avoid
3827 * fragmentation between the Normal and DMA32 zones.
3828 */
3829static inline unsigned int
Mel Gorman0a79cda2018-12-28 00:35:48 -08003830alloc_flags_nofragment(struct zone *zone, gfp_t gfp_mask)
Mel Gorman6bb15452018-12-28 00:35:41 -08003831{
Mateusz Nosek736838e2020-04-01 21:09:47 -07003832 unsigned int alloc_flags;
Mel Gorman0a79cda2018-12-28 00:35:48 -08003833
Mateusz Nosek736838e2020-04-01 21:09:47 -07003834 /*
3835 * __GFP_KSWAPD_RECLAIM is assumed to be the same as ALLOC_KSWAPD
3836 * to save a branch.
3837 */
3838 alloc_flags = (__force int) (gfp_mask & __GFP_KSWAPD_RECLAIM);
Mel Gorman0a79cda2018-12-28 00:35:48 -08003839
3840#ifdef CONFIG_ZONE_DMA32
Andrey Ryabinin8139ad02019-04-25 22:23:58 -07003841 if (!zone)
3842 return alloc_flags;
3843
Mel Gorman6bb15452018-12-28 00:35:41 -08003844 if (zone_idx(zone) != ZONE_NORMAL)
Andrey Ryabinin8118b822019-04-25 22:24:01 -07003845 return alloc_flags;
Mel Gorman6bb15452018-12-28 00:35:41 -08003846
3847 /*
3848 * If ZONE_DMA32 exists, assume it is the one after ZONE_NORMAL and
3849 * the pointer is within zone->zone_pgdat->node_zones[]. Also assume
3850 * on UMA that if Normal is populated then so is DMA32.
3851 */
3852 BUILD_BUG_ON(ZONE_NORMAL - ZONE_DMA32 != 1);
3853 if (nr_online_nodes > 1 && !populated_zone(--zone))
Andrey Ryabinin8118b822019-04-25 22:24:01 -07003854 return alloc_flags;
Mel Gorman6bb15452018-12-28 00:35:41 -08003855
Andrey Ryabinin8118b822019-04-25 22:24:01 -07003856 alloc_flags |= ALLOC_NOFRAGMENT;
Mel Gorman0a79cda2018-12-28 00:35:48 -08003857#endif /* CONFIG_ZONE_DMA32 */
3858 return alloc_flags;
Mel Gorman6bb15452018-12-28 00:35:41 -08003859}
Mel Gorman6bb15452018-12-28 00:35:41 -08003860
Pavel Tatashin8e3560d2021-05-04 18:39:00 -07003861/* Must be called after current_gfp_context() which can change gfp_mask */
3862static inline unsigned int gfp_to_alloc_flags_cma(gfp_t gfp_mask,
3863 unsigned int alloc_flags)
Joonsoo Kim8510e692020-08-06 23:26:04 -07003864{
3865#ifdef CONFIG_CMA
Pavel Tatashin8e3560d2021-05-04 18:39:00 -07003866 if (gfp_migratetype(gfp_mask) == MIGRATE_MOVABLE)
Joonsoo Kim8510e692020-08-06 23:26:04 -07003867 alloc_flags |= ALLOC_CMA;
Joonsoo Kim8510e692020-08-06 23:26:04 -07003868#endif
3869 return alloc_flags;
3870}
3871
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08003872/*
Paul Jackson0798e512006-12-06 20:31:38 -08003873 * get_page_from_freelist goes through the zonelist trying to allocate
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08003874 * a page.
3875 */
3876static struct page *
Vlastimil Babkaa9263752015-02-11 15:25:41 -08003877get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
3878 const struct alloc_context *ac)
Martin Hicks753ee722005-06-21 17:14:41 -07003879{
Mel Gorman6bb15452018-12-28 00:35:41 -08003880 struct zoneref *z;
Mel Gorman5117f452009-06-16 15:31:59 -07003881 struct zone *zone;
Mel Gorman3b8c0be2016-07-28 15:46:53 -07003882 struct pglist_data *last_pgdat_dirty_limit = NULL;
Mel Gorman6bb15452018-12-28 00:35:41 -08003883 bool no_fallback;
Mel Gorman3b8c0be2016-07-28 15:46:53 -07003884
Mel Gorman6bb15452018-12-28 00:35:41 -08003885retry:
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08003886 /*
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003887 * Scan zonelist, looking for a zone with enough free.
Vladimir Davydov344736f2014-10-20 15:50:30 +04003888 * See also __cpuset_node_allowed() comment in kernel/cpuset.c.
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08003889 */
Mel Gorman6bb15452018-12-28 00:35:41 -08003890 no_fallback = alloc_flags & ALLOC_NOFRAGMENT;
3891 z = ac->preferred_zoneref;
Mateusz Nosek30d8ec72020-10-13 16:55:57 -07003892 for_next_zone_zonelist_nodemask(zone, z, ac->highest_zoneidx,
3893 ac->nodemask) {
Mel Gormanbe06af02016-05-19 17:13:47 -07003894 struct page *page;
Johannes Weinere085dbc2013-09-11 14:20:46 -07003895 unsigned long mark;
3896
Mel Gorman664eedd2014-06-04 16:10:08 -07003897 if (cpusets_enabled() &&
3898 (alloc_flags & ALLOC_CPUSET) &&
Vlastimil Babka002f2902016-05-19 17:14:30 -07003899 !__cpuset_zone_allowed(zone, gfp_mask))
Mel Gormancd38b112011-07-25 17:12:29 -07003900 continue;
Johannes Weinera756cf52012-01-10 15:07:49 -08003901 /*
3902 * When allocating a page cache page for writing, we
Mel Gorman281e3722016-07-28 15:46:11 -07003903 * want to get it from a node that is within its dirty
3904 * limit, such that no single node holds more than its
Johannes Weinera756cf52012-01-10 15:07:49 -08003905 * proportional share of globally allowed dirty pages.
Mel Gorman281e3722016-07-28 15:46:11 -07003906 * The dirty limits take into account the node's
Johannes Weinera756cf52012-01-10 15:07:49 -08003907 * lowmem reserves and high watermark so that kswapd
3908 * should be able to balance it without having to
3909 * write pages from its LRU list.
3910 *
Johannes Weinera756cf52012-01-10 15:07:49 -08003911 * XXX: For now, allow allocations to potentially
Mel Gorman281e3722016-07-28 15:46:11 -07003912 * exceed the per-node dirty limit in the slowpath
Mel Gormanc9ab0c42015-11-06 16:28:12 -08003913 * (spread_dirty_pages unset) before going into reclaim,
Johannes Weinera756cf52012-01-10 15:07:49 -08003914 * which is important when on a NUMA setup the allowed
Mel Gorman281e3722016-07-28 15:46:11 -07003915 * nodes are together not big enough to reach the
Johannes Weinera756cf52012-01-10 15:07:49 -08003916 * global limit. The proper fix for these situations
Mel Gorman281e3722016-07-28 15:46:11 -07003917 * will require awareness of nodes in the
Johannes Weinera756cf52012-01-10 15:07:49 -08003918 * dirty-throttling and the flusher threads.
3919 */
Mel Gorman3b8c0be2016-07-28 15:46:53 -07003920 if (ac->spread_dirty_pages) {
3921 if (last_pgdat_dirty_limit == zone->zone_pgdat)
3922 continue;
3923
3924 if (!node_dirty_ok(zone->zone_pgdat)) {
3925 last_pgdat_dirty_limit = zone->zone_pgdat;
3926 continue;
3927 }
3928 }
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08003929
Mel Gorman6bb15452018-12-28 00:35:41 -08003930 if (no_fallback && nr_online_nodes > 1 &&
3931 zone != ac->preferred_zoneref->zone) {
3932 int local_nid;
3933
3934 /*
3935 * If moving to a remote node, retry but allow
3936 * fragmenting fallbacks. Locality is more important
3937 * than fragmentation avoidance.
3938 */
3939 local_nid = zone_to_nid(ac->preferred_zoneref->zone);
3940 if (zone_to_nid(zone) != local_nid) {
3941 alloc_flags &= ~ALLOC_NOFRAGMENT;
3942 goto retry;
3943 }
3944 }
3945
Mel Gormana9214442018-12-28 00:35:44 -08003946 mark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK);
Mel Gorman48ee5f32016-05-19 17:14:07 -07003947 if (!zone_watermark_fast(zone, order, mark,
Charan Teja Reddyf80b08f2020-08-06 23:25:24 -07003948 ac->highest_zoneidx, alloc_flags,
3949 gfp_mask)) {
Mel Gormanfa5e0842009-06-16 15:33:22 -07003950 int ret;
3951
Pavel Tatashinc9e97a12018-04-05 16:22:31 -07003952#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
3953 /*
3954 * Watermark failed for this zone, but see if we can
3955 * grow this zone if it contains deferred pages.
3956 */
3957 if (static_branch_unlikely(&deferred_pages)) {
3958 if (_deferred_grow_zone(zone, order))
3959 goto try_this_zone;
3960 }
3961#endif
Mel Gorman5dab2912014-06-04 16:10:14 -07003962 /* Checked here to keep the fast path fast */
3963 BUILD_BUG_ON(ALLOC_NO_WATERMARKS < NR_WMARK);
3964 if (alloc_flags & ALLOC_NO_WATERMARKS)
3965 goto try_this_zone;
3966
Dave Hansen202e35d2021-05-04 18:36:04 -07003967 if (!node_reclaim_enabled() ||
Mel Gormanc33d6c02016-05-19 17:14:10 -07003968 !zone_allows_reclaim(ac->preferred_zoneref->zone, zone))
Mel Gormancd38b112011-07-25 17:12:29 -07003969 continue;
3970
Mel Gormana5f5f912016-07-28 15:46:32 -07003971 ret = node_reclaim(zone->zone_pgdat, gfp_mask, order);
Mel Gormanfa5e0842009-06-16 15:33:22 -07003972 switch (ret) {
Mel Gormana5f5f912016-07-28 15:46:32 -07003973 case NODE_RECLAIM_NOSCAN:
Mel Gormanfa5e0842009-06-16 15:33:22 -07003974 /* did not scan */
Mel Gormancd38b112011-07-25 17:12:29 -07003975 continue;
Mel Gormana5f5f912016-07-28 15:46:32 -07003976 case NODE_RECLAIM_FULL:
Mel Gormanfa5e0842009-06-16 15:33:22 -07003977 /* scanned but unreclaimable */
Mel Gormancd38b112011-07-25 17:12:29 -07003978 continue;
Mel Gormanfa5e0842009-06-16 15:33:22 -07003979 default:
3980 /* did we reclaim enough */
Mel Gormanfed27192013-04-29 15:07:57 -07003981 if (zone_watermark_ok(zone, order, mark,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003982 ac->highest_zoneidx, alloc_flags))
Mel Gormanfed27192013-04-29 15:07:57 -07003983 goto try_this_zone;
3984
Mel Gormanfed27192013-04-29 15:07:57 -07003985 continue;
Paul Jackson0798e512006-12-06 20:31:38 -08003986 }
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08003987 }
3988
Mel Gormanfa5e0842009-06-16 15:33:22 -07003989try_this_zone:
Mel Gorman066b2392017-02-24 14:56:26 -08003990 page = rmqueue(ac->preferred_zoneref->zone, zone, order,
Mel Gorman0aaa29a2015-11-06 16:28:37 -08003991 gfp_mask, alloc_flags, ac->migratetype);
Vlastimil Babka75379192015-02-11 15:25:38 -08003992 if (page) {
Mel Gorman479f8542016-05-19 17:14:35 -07003993 prep_new_page(page, order, gfp_mask, alloc_flags);
Mel Gorman0aaa29a2015-11-06 16:28:37 -08003994
3995 /*
3996 * If this is a high-order atomic allocation then check
3997 * if the pageblock should be reserved for the future
3998 */
3999 if (unlikely(order && (alloc_flags & ALLOC_HARDER)))
4000 reserve_highatomic_pageblock(page, zone, order);
4001
Vlastimil Babka75379192015-02-11 15:25:38 -08004002 return page;
Pavel Tatashinc9e97a12018-04-05 16:22:31 -07004003 } else {
4004#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
4005 /* Try again if zone has deferred pages */
4006 if (static_branch_unlikely(&deferred_pages)) {
4007 if (_deferred_grow_zone(zone, order))
4008 goto try_this_zone;
4009 }
4010#endif
Vlastimil Babka75379192015-02-11 15:25:38 -08004011 }
Mel Gorman54a6eb52008-04-28 02:12:16 -07004012 }
Paul Jackson9276b1bc2006-12-06 20:31:48 -08004013
Mel Gorman6bb15452018-12-28 00:35:41 -08004014 /*
4015 * It's possible on a UMA machine to get through all zones that are
4016 * fragmented. If avoiding fragmentation, reset and try again.
4017 */
4018 if (no_fallback) {
4019 alloc_flags &= ~ALLOC_NOFRAGMENT;
4020 goto retry;
4021 }
4022
Mel Gorman4ffeaf32014-08-06 16:07:22 -07004023 return NULL;
Martin Hicks753ee722005-06-21 17:14:41 -07004024}
4025
Michal Hocko9af744d2017-02-22 15:46:16 -08004026static void warn_alloc_show_mem(gfp_t gfp_mask, nodemask_t *nodemask)
Dave Hansena238ab52011-05-24 17:12:16 -07004027{
Dave Hansena238ab52011-05-24 17:12:16 -07004028 unsigned int filter = SHOW_MEM_FILTER_NODES;
Dave Hansena238ab52011-05-24 17:12:16 -07004029
4030 /*
4031 * This documents exceptions given to allocations in certain
4032 * contexts that are allowed to allocate outside current's set
4033 * of allowed nodes.
4034 */
4035 if (!(gfp_mask & __GFP_NOMEMALLOC))
Michal Hockocd04ae12017-09-06 16:24:50 -07004036 if (tsk_is_oom_victim(current) ||
Dave Hansena238ab52011-05-24 17:12:16 -07004037 (current->flags & (PF_MEMALLOC | PF_EXITING)))
4038 filter &= ~SHOW_MEM_FILTER_NODES;
Mel Gormand0164ad2015-11-06 16:28:21 -08004039 if (in_interrupt() || !(gfp_mask & __GFP_DIRECT_RECLAIM))
Dave Hansena238ab52011-05-24 17:12:16 -07004040 filter &= ~SHOW_MEM_FILTER_NODES;
4041
Michal Hocko9af744d2017-02-22 15:46:16 -08004042 show_mem(filter, nodemask);
Michal Hockoaa187502017-02-22 15:41:45 -08004043}
4044
Michal Hockoa8e99252017-02-22 15:46:10 -08004045void warn_alloc(gfp_t gfp_mask, nodemask_t *nodemask, const char *fmt, ...)
Michal Hockoaa187502017-02-22 15:41:45 -08004046{
4047 struct va_format vaf;
4048 va_list args;
Johannes Weiner1be334e2019-11-05 21:16:51 -08004049 static DEFINE_RATELIMIT_STATE(nopage_rs, 10*HZ, 1);
Michal Hockoaa187502017-02-22 15:41:45 -08004050
Tetsuo Handa0f7896f2017-05-03 14:55:34 -07004051 if ((gfp_mask & __GFP_NOWARN) || !__ratelimit(&nopage_rs))
Michal Hockoaa187502017-02-22 15:41:45 -08004052 return;
4053
Michal Hocko7877cdc2016-10-07 17:01:55 -07004054 va_start(args, fmt);
4055 vaf.fmt = fmt;
4056 vaf.va = &args;
yuzhoujianef8444e2018-12-28 00:36:07 -08004057 pr_warn("%s: %pV, mode:%#x(%pGg), nodemask=%*pbl",
Michal Hocko0205f752017-11-15 17:39:14 -08004058 current->comm, &vaf, gfp_mask, &gfp_mask,
4059 nodemask_pr_args(nodemask));
Michal Hocko7877cdc2016-10-07 17:01:55 -07004060 va_end(args);
Joe Perches3ee9a4f2011-10-31 17:08:35 -07004061
Michal Hockoa8e99252017-02-22 15:46:10 -08004062 cpuset_print_current_mems_allowed();
yuzhoujianef8444e2018-12-28 00:36:07 -08004063 pr_cont("\n");
Dave Hansena238ab52011-05-24 17:12:16 -07004064 dump_stack();
David Rientjes685dbf62017-02-22 15:46:28 -08004065 warn_alloc_show_mem(gfp_mask, nodemask);
Dave Hansena238ab52011-05-24 17:12:16 -07004066}
4067
Mel Gorman11e33f62009-06-16 15:31:57 -07004068static inline struct page *
Michal Hocko6c18ba72017-02-22 15:46:25 -08004069__alloc_pages_cpuset_fallback(gfp_t gfp_mask, unsigned int order,
4070 unsigned int alloc_flags,
4071 const struct alloc_context *ac)
4072{
4073 struct page *page;
4074
4075 page = get_page_from_freelist(gfp_mask, order,
4076 alloc_flags|ALLOC_CPUSET, ac);
4077 /*
4078 * fallback to ignore cpuset restriction if our nodes
4079 * are depleted
4080 */
4081 if (!page)
4082 page = get_page_from_freelist(gfp_mask, order,
4083 alloc_flags, ac);
4084
4085 return page;
4086}
4087
4088static inline struct page *
Mel Gorman11e33f62009-06-16 15:31:57 -07004089__alloc_pages_may_oom(gfp_t gfp_mask, unsigned int order,
Vlastimil Babkaa9263752015-02-11 15:25:41 -08004090 const struct alloc_context *ac, unsigned long *did_some_progress)
Mel Gorman11e33f62009-06-16 15:31:57 -07004091{
David Rientjes6e0fc462015-09-08 15:00:36 -07004092 struct oom_control oc = {
4093 .zonelist = ac->zonelist,
4094 .nodemask = ac->nodemask,
Vladimir Davydov2a966b72016-07-26 15:22:33 -07004095 .memcg = NULL,
David Rientjes6e0fc462015-09-08 15:00:36 -07004096 .gfp_mask = gfp_mask,
4097 .order = order,
David Rientjes6e0fc462015-09-08 15:00:36 -07004098 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100
Johannes Weiner9879de72015-01-26 12:58:32 -08004101 *did_some_progress = 0;
4102
Johannes Weiner9879de72015-01-26 12:58:32 -08004103 /*
Johannes Weinerdc564012015-06-24 16:57:19 -07004104 * Acquire the oom lock. If that fails, somebody else is
4105 * making progress for us.
Johannes Weiner9879de72015-01-26 12:58:32 -08004106 */
Johannes Weinerdc564012015-06-24 16:57:19 -07004107 if (!mutex_trylock(&oom_lock)) {
Johannes Weiner9879de72015-01-26 12:58:32 -08004108 *did_some_progress = 1;
Mel Gorman11e33f62009-06-16 15:31:57 -07004109 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 return NULL;
4111 }
Jens Axboe6b1de912005-11-17 21:35:02 +01004112
Mel Gorman11e33f62009-06-16 15:31:57 -07004113 /*
4114 * Go through the zonelist yet one more time, keep very high watermark
4115 * here, this is only to catch a parallel oom killing, we must fail if
Tetsuo Handae746bf72017-08-31 16:15:20 -07004116 * we're still under heavy pressure. But make sure that this reclaim
4117 * attempt shall not depend on __GFP_DIRECT_RECLAIM && !__GFP_NORETRY
4118 * allocation which will never fail due to oom_lock already held.
Mel Gorman11e33f62009-06-16 15:31:57 -07004119 */
Tetsuo Handae746bf72017-08-31 16:15:20 -07004120 page = get_page_from_freelist((gfp_mask | __GFP_HARDWALL) &
4121 ~__GFP_DIRECT_RECLAIM, order,
4122 ALLOC_WMARK_HIGH|ALLOC_CPUSET, ac);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08004123 if (page)
Mel Gorman11e33f62009-06-16 15:31:57 -07004124 goto out;
4125
Michal Hocko06ad2762017-02-22 15:46:22 -08004126 /* Coredumps can quickly deplete all memory reserves */
4127 if (current->flags & PF_DUMPCORE)
4128 goto out;
4129 /* The OOM killer will not help higher order allocs */
4130 if (order > PAGE_ALLOC_COSTLY_ORDER)
4131 goto out;
Michal Hockodcda9b02017-07-12 14:36:45 -07004132 /*
4133 * We have already exhausted all our reclaim opportunities without any
4134 * success so it is time to admit defeat. We will skip the OOM killer
4135 * because it is very likely that the caller has a more reasonable
4136 * fallback than shooting a random task.
Mateusz Nosekcfb4a542020-10-13 16:55:45 -07004137 *
4138 * The OOM killer may not free memory on a specific node.
Michal Hockodcda9b02017-07-12 14:36:45 -07004139 */
Mateusz Nosekcfb4a542020-10-13 16:55:45 -07004140 if (gfp_mask & (__GFP_RETRY_MAYFAIL | __GFP_THISNODE))
Michal Hockodcda9b02017-07-12 14:36:45 -07004141 goto out;
Michal Hocko06ad2762017-02-22 15:46:22 -08004142 /* The OOM killer does not needlessly kill tasks for lowmem */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004143 if (ac->highest_zoneidx < ZONE_NORMAL)
Michal Hocko06ad2762017-02-22 15:46:22 -08004144 goto out;
4145 if (pm_suspended_storage())
4146 goto out;
4147 /*
4148 * XXX: GFP_NOFS allocations should rather fail than rely on
4149 * other request to make a forward progress.
4150 * We are in an unfortunate situation where out_of_memory cannot
4151 * do much for this context but let's try it to at least get
4152 * access to memory reserved if the current task is killed (see
4153 * out_of_memory). Once filesystems are ready to handle allocation
4154 * failures more gracefully we should just bail out here.
4155 */
Michal Hocko3da88fb32016-05-19 17:13:09 -07004156
Shile Zhang3c2c6482018-01-31 16:20:07 -08004157 /* Exhausted what can be done so it's blame time */
Michal Hocko5020e282016-01-14 15:20:36 -08004158 if (out_of_memory(&oc) || WARN_ON_ONCE(gfp_mask & __GFP_NOFAIL)) {
Michal Hockoc32b3cb2015-02-11 15:26:24 -08004159 *did_some_progress = 1;
Michal Hocko5020e282016-01-14 15:20:36 -08004160
Michal Hocko6c18ba72017-02-22 15:46:25 -08004161 /*
4162 * Help non-failing allocations by giving them access to memory
4163 * reserves
4164 */
4165 if (gfp_mask & __GFP_NOFAIL)
4166 page = __alloc_pages_cpuset_fallback(gfp_mask, order,
Michal Hocko5020e282016-01-14 15:20:36 -08004167 ALLOC_NO_WATERMARKS, ac);
Michal Hocko5020e282016-01-14 15:20:36 -08004168 }
Mel Gorman11e33f62009-06-16 15:31:57 -07004169out:
Johannes Weinerdc564012015-06-24 16:57:19 -07004170 mutex_unlock(&oom_lock);
Mel Gorman11e33f62009-06-16 15:31:57 -07004171 return page;
4172}
4173
Michal Hocko33c2d212016-05-20 16:57:06 -07004174/*
Lu Jialinbaf2f902021-05-06 18:06:50 -07004175 * Maximum number of compaction retries with a progress before OOM
Michal Hocko33c2d212016-05-20 16:57:06 -07004176 * killer is consider as the only way to move forward.
4177 */
4178#define MAX_COMPACT_RETRIES 16
4179
Mel Gorman56de7262010-05-24 14:32:30 -07004180#ifdef CONFIG_COMPACTION
4181/* Try memory compaction for high-order allocations before reclaim */
4182static struct page *
4183__alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
Mel Gormanc6038442016-05-19 17:13:38 -07004184 unsigned int alloc_flags, const struct alloc_context *ac,
Vlastimil Babkaa5508cd2016-07-28 15:49:28 -07004185 enum compact_priority prio, enum compact_result *compact_result)
Mel Gorman56de7262010-05-24 14:32:30 -07004186{
Mel Gorman5e1f0f02019-03-05 15:45:41 -08004187 struct page *page = NULL;
Johannes Weinereb414682018-10-26 15:06:27 -07004188 unsigned long pflags;
Vlastimil Babka499118e2017-05-08 15:59:50 -07004189 unsigned int noreclaim_flag;
Vlastimil Babka53853e22014-10-09 15:27:02 -07004190
Mel Gorman66199712012-01-12 17:19:41 -08004191 if (!order)
Mel Gorman56de7262010-05-24 14:32:30 -07004192 return NULL;
4193
Johannes Weinereb414682018-10-26 15:06:27 -07004194 psi_memstall_enter(&pflags);
Vlastimil Babka499118e2017-05-08 15:59:50 -07004195 noreclaim_flag = memalloc_noreclaim_save();
Johannes Weinereb414682018-10-26 15:06:27 -07004196
Michal Hockoc5d01d02016-05-20 16:56:53 -07004197 *compact_result = try_to_compact_pages(gfp_mask, order, alloc_flags, ac,
Mel Gorman5e1f0f02019-03-05 15:45:41 -08004198 prio, &page);
Johannes Weinereb414682018-10-26 15:06:27 -07004199
Vlastimil Babka499118e2017-05-08 15:59:50 -07004200 memalloc_noreclaim_restore(noreclaim_flag);
Johannes Weinereb414682018-10-26 15:06:27 -07004201 psi_memstall_leave(&pflags);
Mel Gorman56de7262010-05-24 14:32:30 -07004202
Charan Teja Reddy06dac2f2021-05-04 18:36:51 -07004203 if (*compact_result == COMPACT_SKIPPED)
4204 return NULL;
Vlastimil Babka98dd3b42014-10-09 15:27:04 -07004205 /*
4206 * At least in one zone compaction wasn't deferred or skipped, so let's
4207 * count a compaction stall
4208 */
4209 count_vm_event(COMPACTSTALL);
4210
Mel Gorman5e1f0f02019-03-05 15:45:41 -08004211 /* Prep a captured page if available */
4212 if (page)
4213 prep_new_page(page, order, gfp_mask, alloc_flags);
4214
4215 /* Try get a page from the freelist if available */
4216 if (!page)
4217 page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac);
Vlastimil Babka98dd3b42014-10-09 15:27:04 -07004218
4219 if (page) {
4220 struct zone *zone = page_zone(page);
4221
4222 zone->compact_blockskip_flush = false;
4223 compaction_defer_reset(zone, order, true);
4224 count_vm_event(COMPACTSUCCESS);
4225 return page;
4226 }
4227
4228 /*
Vlastimil Babka98dd3b42014-10-09 15:27:04 -07004229 * It's bad if compaction run occurs and fails. The most likely reason
4230 * is that pages exist, but not enough to satisfy watermarks.
4231 */
4232 count_vm_event(COMPACTFAIL);
4233
4234 cond_resched();
4235
Mel Gorman56de7262010-05-24 14:32:30 -07004236 return NULL;
4237}
Michal Hocko33c2d212016-05-20 16:57:06 -07004238
Vlastimil Babka32508452016-10-07 17:00:28 -07004239static inline bool
4240should_compact_retry(struct alloc_context *ac, int order, int alloc_flags,
4241 enum compact_result compact_result,
4242 enum compact_priority *compact_priority,
Vlastimil Babkad9436492016-10-07 17:00:31 -07004243 int *compaction_retries)
Vlastimil Babka32508452016-10-07 17:00:28 -07004244{
4245 int max_retries = MAX_COMPACT_RETRIES;
Vlastimil Babkac2033b02016-10-07 17:00:34 -07004246 int min_priority;
Michal Hocko65190cf2017-02-22 15:42:03 -08004247 bool ret = false;
4248 int retries = *compaction_retries;
4249 enum compact_priority priority = *compact_priority;
Vlastimil Babka32508452016-10-07 17:00:28 -07004250
4251 if (!order)
4252 return false;
4253
Vlastimil Babkad9436492016-10-07 17:00:31 -07004254 if (compaction_made_progress(compact_result))
4255 (*compaction_retries)++;
4256
Vlastimil Babka32508452016-10-07 17:00:28 -07004257 /*
4258 * compaction considers all the zone as desperately out of memory
4259 * so it doesn't really make much sense to retry except when the
4260 * failure could be caused by insufficient priority
4261 */
Vlastimil Babkad9436492016-10-07 17:00:31 -07004262 if (compaction_failed(compact_result))
4263 goto check_priority;
Vlastimil Babka32508452016-10-07 17:00:28 -07004264
4265 /*
Vlastimil Babka494330852019-09-23 15:37:32 -07004266 * compaction was skipped because there are not enough order-0 pages
4267 * to work with, so we retry only if it looks like reclaim can help.
Vlastimil Babka32508452016-10-07 17:00:28 -07004268 */
Vlastimil Babka494330852019-09-23 15:37:32 -07004269 if (compaction_needs_reclaim(compact_result)) {
Michal Hocko65190cf2017-02-22 15:42:03 -08004270 ret = compaction_zonelist_suitable(ac, order, alloc_flags);
4271 goto out;
4272 }
Vlastimil Babka32508452016-10-07 17:00:28 -07004273
4274 /*
Vlastimil Babka494330852019-09-23 15:37:32 -07004275 * make sure the compaction wasn't deferred or didn't bail out early
4276 * due to locks contention before we declare that we should give up.
4277 * But the next retry should use a higher priority if allowed, so
4278 * we don't just keep bailing out endlessly.
4279 */
4280 if (compaction_withdrawn(compact_result)) {
4281 goto check_priority;
4282 }
4283
4284 /*
Michal Hockodcda9b02017-07-12 14:36:45 -07004285 * !costly requests are much more important than __GFP_RETRY_MAYFAIL
Vlastimil Babka32508452016-10-07 17:00:28 -07004286 * costly ones because they are de facto nofail and invoke OOM
4287 * killer to move on while costly can fail and users are ready
4288 * to cope with that. 1/4 retries is rather arbitrary but we
4289 * would need much more detailed feedback from compaction to
4290 * make a better decision.
4291 */
4292 if (order > PAGE_ALLOC_COSTLY_ORDER)
4293 max_retries /= 4;
Michal Hocko65190cf2017-02-22 15:42:03 -08004294 if (*compaction_retries <= max_retries) {
4295 ret = true;
4296 goto out;
4297 }
Vlastimil Babka32508452016-10-07 17:00:28 -07004298
Vlastimil Babkad9436492016-10-07 17:00:31 -07004299 /*
4300 * Make sure there are attempts at the highest priority if we exhausted
4301 * all retries or failed at the lower priorities.
4302 */
4303check_priority:
Vlastimil Babkac2033b02016-10-07 17:00:34 -07004304 min_priority = (order > PAGE_ALLOC_COSTLY_ORDER) ?
4305 MIN_COMPACT_COSTLY_PRIORITY : MIN_COMPACT_PRIORITY;
Michal Hocko65190cf2017-02-22 15:42:03 -08004306
Vlastimil Babkac2033b02016-10-07 17:00:34 -07004307 if (*compact_priority > min_priority) {
Vlastimil Babkad9436492016-10-07 17:00:31 -07004308 (*compact_priority)--;
4309 *compaction_retries = 0;
Michal Hocko65190cf2017-02-22 15:42:03 -08004310 ret = true;
Vlastimil Babkad9436492016-10-07 17:00:31 -07004311 }
Michal Hocko65190cf2017-02-22 15:42:03 -08004312out:
4313 trace_compact_retry(order, priority, compact_result, retries, max_retries, ret);
4314 return ret;
Vlastimil Babka32508452016-10-07 17:00:28 -07004315}
Mel Gorman56de7262010-05-24 14:32:30 -07004316#else
4317static inline struct page *
4318__alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
Mel Gormanc6038442016-05-19 17:13:38 -07004319 unsigned int alloc_flags, const struct alloc_context *ac,
Vlastimil Babkaa5508cd2016-07-28 15:49:28 -07004320 enum compact_priority prio, enum compact_result *compact_result)
Mel Gorman56de7262010-05-24 14:32:30 -07004321{
Michal Hocko33c2d212016-05-20 16:57:06 -07004322 *compact_result = COMPACT_SKIPPED;
Mel Gorman56de7262010-05-24 14:32:30 -07004323 return NULL;
4324}
Michal Hocko33c2d212016-05-20 16:57:06 -07004325
4326static inline bool
Michal Hocko86a294a2016-05-20 16:57:12 -07004327should_compact_retry(struct alloc_context *ac, unsigned int order, int alloc_flags,
4328 enum compact_result compact_result,
Vlastimil Babkaa5508cd2016-07-28 15:49:28 -07004329 enum compact_priority *compact_priority,
Vlastimil Babkad9436492016-10-07 17:00:31 -07004330 int *compaction_retries)
Michal Hocko33c2d212016-05-20 16:57:06 -07004331{
Michal Hocko31e49bf2016-05-20 16:57:15 -07004332 struct zone *zone;
4333 struct zoneref *z;
4334
4335 if (!order || order > PAGE_ALLOC_COSTLY_ORDER)
4336 return false;
4337
4338 /*
4339 * There are setups with compaction disabled which would prefer to loop
4340 * inside the allocator rather than hit the oom killer prematurely.
4341 * Let's give them a good hope and keep retrying while the order-0
4342 * watermarks are OK.
4343 */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004344 for_each_zone_zonelist_nodemask(zone, z, ac->zonelist,
4345 ac->highest_zoneidx, ac->nodemask) {
Michal Hocko31e49bf2016-05-20 16:57:15 -07004346 if (zone_watermark_ok(zone, 0, min_wmark_pages(zone),
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004347 ac->highest_zoneidx, alloc_flags))
Michal Hocko31e49bf2016-05-20 16:57:15 -07004348 return true;
4349 }
Michal Hocko33c2d212016-05-20 16:57:06 -07004350 return false;
4351}
Vlastimil Babka32508452016-10-07 17:00:28 -07004352#endif /* CONFIG_COMPACTION */
Mel Gorman56de7262010-05-24 14:32:30 -07004353
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01004354#ifdef CONFIG_LOCKDEP
Omar Sandoval93781322018-06-07 17:07:02 -07004355static struct lockdep_map __fs_reclaim_map =
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01004356 STATIC_LOCKDEP_MAP_INIT("fs_reclaim", &__fs_reclaim_map);
4357
Daniel Vetterf920e412020-12-14 19:08:30 -08004358static bool __need_reclaim(gfp_t gfp_mask)
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01004359{
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01004360 /* no reclaim without waiting on it */
4361 if (!(gfp_mask & __GFP_DIRECT_RECLAIM))
4362 return false;
4363
4364 /* this guy won't enter reclaim */
Tetsuo Handa2e517d682018-03-22 16:17:10 -07004365 if (current->flags & PF_MEMALLOC)
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01004366 return false;
4367
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01004368 if (gfp_mask & __GFP_NOLOCKDEP)
4369 return false;
4370
4371 return true;
4372}
4373
Omar Sandoval93781322018-06-07 17:07:02 -07004374void __fs_reclaim_acquire(void)
4375{
4376 lock_map_acquire(&__fs_reclaim_map);
4377}
4378
4379void __fs_reclaim_release(void)
4380{
4381 lock_map_release(&__fs_reclaim_map);
4382}
4383
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01004384void fs_reclaim_acquire(gfp_t gfp_mask)
4385{
Daniel Vetterf920e412020-12-14 19:08:30 -08004386 gfp_mask = current_gfp_context(gfp_mask);
4387
4388 if (__need_reclaim(gfp_mask)) {
4389 if (gfp_mask & __GFP_FS)
4390 __fs_reclaim_acquire();
4391
4392#ifdef CONFIG_MMU_NOTIFIER
4393 lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
4394 lock_map_release(&__mmu_notifier_invalidate_range_start_map);
4395#endif
4396
4397 }
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01004398}
4399EXPORT_SYMBOL_GPL(fs_reclaim_acquire);
4400
4401void fs_reclaim_release(gfp_t gfp_mask)
4402{
Daniel Vetterf920e412020-12-14 19:08:30 -08004403 gfp_mask = current_gfp_context(gfp_mask);
4404
4405 if (__need_reclaim(gfp_mask)) {
4406 if (gfp_mask & __GFP_FS)
4407 __fs_reclaim_release();
4408 }
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01004409}
4410EXPORT_SYMBOL_GPL(fs_reclaim_release);
4411#endif
4412
Marek Szyprowskibba90712012-01-25 12:09:52 +01004413/* Perform direct synchronous page reclaim */
Yanfei Xu2187e172020-10-13 16:55:54 -07004414static unsigned long
Vlastimil Babkaa9263752015-02-11 15:25:41 -08004415__perform_reclaim(gfp_t gfp_mask, unsigned int order,
4416 const struct alloc_context *ac)
Mel Gorman11e33f62009-06-16 15:31:57 -07004417{
Vlastimil Babka499118e2017-05-08 15:59:50 -07004418 unsigned int noreclaim_flag;
Yanfei Xu2187e172020-10-13 16:55:54 -07004419 unsigned long pflags, progress;
Mel Gorman11e33f62009-06-16 15:31:57 -07004420
4421 cond_resched();
4422
4423 /* We now go into synchronous reclaim */
4424 cpuset_memory_pressure_bump();
Johannes Weinereb414682018-10-26 15:06:27 -07004425 psi_memstall_enter(&pflags);
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01004426 fs_reclaim_acquire(gfp_mask);
Omar Sandoval93781322018-06-07 17:07:02 -07004427 noreclaim_flag = memalloc_noreclaim_save();
Mel Gorman11e33f62009-06-16 15:31:57 -07004428
Vlastimil Babkaa9263752015-02-11 15:25:41 -08004429 progress = try_to_free_pages(ac->zonelist, order, gfp_mask,
4430 ac->nodemask);
Mel Gorman11e33f62009-06-16 15:31:57 -07004431
Vlastimil Babka499118e2017-05-08 15:59:50 -07004432 memalloc_noreclaim_restore(noreclaim_flag);
Omar Sandoval93781322018-06-07 17:07:02 -07004433 fs_reclaim_release(gfp_mask);
Johannes Weinereb414682018-10-26 15:06:27 -07004434 psi_memstall_leave(&pflags);
Mel Gorman11e33f62009-06-16 15:31:57 -07004435
4436 cond_resched();
4437
Marek Szyprowskibba90712012-01-25 12:09:52 +01004438 return progress;
4439}
4440
4441/* The really slow allocator path where we enter direct reclaim */
4442static inline struct page *
4443__alloc_pages_direct_reclaim(gfp_t gfp_mask, unsigned int order,
Mel Gormanc6038442016-05-19 17:13:38 -07004444 unsigned int alloc_flags, const struct alloc_context *ac,
Vlastimil Babkaa9263752015-02-11 15:25:41 -08004445 unsigned long *did_some_progress)
Marek Szyprowskibba90712012-01-25 12:09:52 +01004446{
4447 struct page *page = NULL;
4448 bool drained = false;
4449
Vlastimil Babkaa9263752015-02-11 15:25:41 -08004450 *did_some_progress = __perform_reclaim(gfp_mask, order, ac);
Mel Gorman9ee493c2010-09-09 16:38:18 -07004451 if (unlikely(!(*did_some_progress)))
4452 return NULL;
Mel Gorman11e33f62009-06-16 15:31:57 -07004453
Mel Gorman9ee493c2010-09-09 16:38:18 -07004454retry:
Vlastimil Babka31a6c192016-07-28 15:49:13 -07004455 page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac);
Mel Gorman9ee493c2010-09-09 16:38:18 -07004456
4457 /*
4458 * If an allocation failed after direct reclaim, it could be because
Mel Gorman0aaa29a2015-11-06 16:28:37 -08004459 * pages are pinned on the per-cpu lists or in high alloc reserves.
Randy Dunlap047b9962020-08-11 18:33:14 -07004460 * Shrink them and try again
Mel Gorman9ee493c2010-09-09 16:38:18 -07004461 */
4462 if (!page && !drained) {
Minchan Kim29fac032016-12-12 16:42:14 -08004463 unreserve_highatomic_pageblock(ac, false);
Vlastimil Babka93481ff2014-12-10 15:43:01 -08004464 drain_all_pages(NULL);
Mel Gorman9ee493c2010-09-09 16:38:18 -07004465 drained = true;
4466 goto retry;
4467 }
4468
Mel Gorman11e33f62009-06-16 15:31:57 -07004469 return page;
4470}
4471
David Rientjes5ecd9d42018-04-05 16:25:16 -07004472static void wake_all_kswapds(unsigned int order, gfp_t gfp_mask,
4473 const struct alloc_context *ac)
Mel Gorman11e33f62009-06-16 15:31:57 -07004474{
4475 struct zoneref *z;
4476 struct zone *zone;
Mel Gormane1a55632016-07-28 15:46:26 -07004477 pg_data_t *last_pgdat = NULL;
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004478 enum zone_type highest_zoneidx = ac->highest_zoneidx;
Mel Gorman11e33f62009-06-16 15:31:57 -07004479
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004480 for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, highest_zoneidx,
David Rientjes5ecd9d42018-04-05 16:25:16 -07004481 ac->nodemask) {
Mel Gormane1a55632016-07-28 15:46:26 -07004482 if (last_pgdat != zone->zone_pgdat)
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004483 wakeup_kswapd(zone, gfp_mask, order, highest_zoneidx);
Mel Gormane1a55632016-07-28 15:46:26 -07004484 last_pgdat = zone->zone_pgdat;
4485 }
Mel Gorman11e33f62009-06-16 15:31:57 -07004486}
4487
Mel Gormanc6038442016-05-19 17:13:38 -07004488static inline unsigned int
Peter Zijlstra341ce062009-06-16 15:32:02 -07004489gfp_to_alloc_flags(gfp_t gfp_mask)
4490{
Mel Gormanc6038442016-05-19 17:13:38 -07004491 unsigned int alloc_flags = ALLOC_WMARK_MIN | ALLOC_CPUSET;
Peter Zijlstra341ce062009-06-16 15:32:02 -07004492
Mateusz Nosek736838e2020-04-01 21:09:47 -07004493 /*
4494 * __GFP_HIGH is assumed to be the same as ALLOC_HIGH
4495 * and __GFP_KSWAPD_RECLAIM is assumed to be the same as ALLOC_KSWAPD
4496 * to save two branches.
4497 */
Namhyung Kime6223a32010-10-26 14:21:59 -07004498 BUILD_BUG_ON(__GFP_HIGH != (__force gfp_t) ALLOC_HIGH);
Mateusz Nosek736838e2020-04-01 21:09:47 -07004499 BUILD_BUG_ON(__GFP_KSWAPD_RECLAIM != (__force gfp_t) ALLOC_KSWAPD);
Mel Gormana56f57f2009-06-16 15:32:02 -07004500
Peter Zijlstra341ce062009-06-16 15:32:02 -07004501 /*
4502 * The caller may dip into page reserves a bit more if the caller
4503 * cannot run direct reclaim, or if the caller has realtime scheduling
4504 * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will
Mel Gormand0164ad2015-11-06 16:28:21 -08004505 * set both ALLOC_HARDER (__GFP_ATOMIC) and ALLOC_HIGH (__GFP_HIGH).
Peter Zijlstra341ce062009-06-16 15:32:02 -07004506 */
Mateusz Nosek736838e2020-04-01 21:09:47 -07004507 alloc_flags |= (__force int)
4508 (gfp_mask & (__GFP_HIGH | __GFP_KSWAPD_RECLAIM));
Peter Zijlstra341ce062009-06-16 15:32:02 -07004509
Mel Gormand0164ad2015-11-06 16:28:21 -08004510 if (gfp_mask & __GFP_ATOMIC) {
Andrea Arcangeli5c3240d2011-01-13 15:46:49 -08004511 /*
David Rientjesb104a352014-07-30 16:08:24 -07004512 * Not worth trying to allocate harder for __GFP_NOMEMALLOC even
4513 * if it can't schedule.
Andrea Arcangeli5c3240d2011-01-13 15:46:49 -08004514 */
David Rientjesb104a352014-07-30 16:08:24 -07004515 if (!(gfp_mask & __GFP_NOMEMALLOC))
Andrea Arcangeli5c3240d2011-01-13 15:46:49 -08004516 alloc_flags |= ALLOC_HARDER;
Peter Zijlstra341ce062009-06-16 15:32:02 -07004517 /*
David Rientjesb104a352014-07-30 16:08:24 -07004518 * Ignore cpuset mems for GFP_ATOMIC rather than fail, see the
Vladimir Davydov344736f2014-10-20 15:50:30 +04004519 * comment for __cpuset_node_allowed().
Peter Zijlstra341ce062009-06-16 15:32:02 -07004520 */
4521 alloc_flags &= ~ALLOC_CPUSET;
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08004522 } else if (unlikely(rt_task(current)) && !in_interrupt())
Peter Zijlstra341ce062009-06-16 15:32:02 -07004523 alloc_flags |= ALLOC_HARDER;
4524
Pavel Tatashin8e3560d2021-05-04 18:39:00 -07004525 alloc_flags = gfp_to_alloc_flags_cma(gfp_mask, alloc_flags);
Joonsoo Kim8510e692020-08-06 23:26:04 -07004526
Peter Zijlstra341ce062009-06-16 15:32:02 -07004527 return alloc_flags;
4528}
4529
Michal Hockocd04ae12017-09-06 16:24:50 -07004530static bool oom_reserves_allowed(struct task_struct *tsk)
Mel Gorman072bb0a2012-07-31 16:43:58 -07004531{
Michal Hockocd04ae12017-09-06 16:24:50 -07004532 if (!tsk_is_oom_victim(tsk))
Vlastimil Babka31a6c192016-07-28 15:49:13 -07004533 return false;
4534
Michal Hockocd04ae12017-09-06 16:24:50 -07004535 /*
4536 * !MMU doesn't have oom reaper so give access to memory reserves
4537 * only to the thread with TIF_MEMDIE set
4538 */
4539 if (!IS_ENABLED(CONFIG_MMU) && !test_thread_flag(TIF_MEMDIE))
4540 return false;
Vlastimil Babka31a6c192016-07-28 15:49:13 -07004541
Michal Hockocd04ae12017-09-06 16:24:50 -07004542 return true;
4543}
4544
4545/*
4546 * Distinguish requests which really need access to full memory
4547 * reserves from oom victims which can live with a portion of it
4548 */
4549static inline int __gfp_pfmemalloc_flags(gfp_t gfp_mask)
4550{
4551 if (unlikely(gfp_mask & __GFP_NOMEMALLOC))
4552 return 0;
4553 if (gfp_mask & __GFP_MEMALLOC)
4554 return ALLOC_NO_WATERMARKS;
4555 if (in_serving_softirq() && (current->flags & PF_MEMALLOC))
4556 return ALLOC_NO_WATERMARKS;
4557 if (!in_interrupt()) {
4558 if (current->flags & PF_MEMALLOC)
4559 return ALLOC_NO_WATERMARKS;
4560 else if (oom_reserves_allowed(current))
4561 return ALLOC_OOM;
4562 }
4563
4564 return 0;
4565}
4566
4567bool gfp_pfmemalloc_allowed(gfp_t gfp_mask)
4568{
4569 return !!__gfp_pfmemalloc_flags(gfp_mask);
Mel Gorman072bb0a2012-07-31 16:43:58 -07004570}
4571
Michal Hocko0a0337e2016-05-20 16:57:00 -07004572/*
Michal Hocko0a0337e2016-05-20 16:57:00 -07004573 * Checks whether it makes sense to retry the reclaim to make a forward progress
4574 * for the given allocation request.
Johannes Weiner491d79a2017-05-03 14:52:16 -07004575 *
4576 * We give up when we either have tried MAX_RECLAIM_RETRIES in a row
4577 * without success, or when we couldn't even meet the watermark if we
4578 * reclaimed all remaining pages on the LRU lists.
Michal Hocko0a0337e2016-05-20 16:57:00 -07004579 *
4580 * Returns true if a retry is viable or false to enter the oom path.
4581 */
4582static inline bool
4583should_reclaim_retry(gfp_t gfp_mask, unsigned order,
4584 struct alloc_context *ac, int alloc_flags,
Vlastimil Babka423b4522016-10-07 17:00:40 -07004585 bool did_some_progress, int *no_progress_loops)
Michal Hocko0a0337e2016-05-20 16:57:00 -07004586{
4587 struct zone *zone;
4588 struct zoneref *z;
Michal Hocko15f570b2018-10-26 15:03:31 -07004589 bool ret = false;
Michal Hocko0a0337e2016-05-20 16:57:00 -07004590
4591 /*
Vlastimil Babka423b4522016-10-07 17:00:40 -07004592 * Costly allocations might have made a progress but this doesn't mean
4593 * their order will become available due to high fragmentation so
4594 * always increment the no progress counter for them
4595 */
4596 if (did_some_progress && order <= PAGE_ALLOC_COSTLY_ORDER)
4597 *no_progress_loops = 0;
4598 else
4599 (*no_progress_loops)++;
4600
4601 /*
Michal Hocko0a0337e2016-05-20 16:57:00 -07004602 * Make sure we converge to OOM if we cannot make any progress
4603 * several times in the row.
4604 */
Minchan Kim04c87162016-12-12 16:42:11 -08004605 if (*no_progress_loops > MAX_RECLAIM_RETRIES) {
4606 /* Before OOM, exhaust highatomic_reserve */
Minchan Kim29fac032016-12-12 16:42:14 -08004607 return unreserve_highatomic_pageblock(ac, true);
Minchan Kim04c87162016-12-12 16:42:11 -08004608 }
Michal Hocko0a0337e2016-05-20 16:57:00 -07004609
Michal Hocko0a0337e2016-05-20 16:57:00 -07004610 /*
Mel Gormanbca67592016-07-28 15:47:05 -07004611 * Keep reclaiming pages while there is a chance this will lead
4612 * somewhere. If none of the target zones can satisfy our allocation
4613 * request even if all reclaimable pages are considered then we are
4614 * screwed and have to go OOM.
Michal Hocko0a0337e2016-05-20 16:57:00 -07004615 */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004616 for_each_zone_zonelist_nodemask(zone, z, ac->zonelist,
4617 ac->highest_zoneidx, ac->nodemask) {
Michal Hocko0a0337e2016-05-20 16:57:00 -07004618 unsigned long available;
Michal Hockoede37712016-05-20 16:57:03 -07004619 unsigned long reclaimable;
Michal Hockod379f012017-02-22 15:42:00 -08004620 unsigned long min_wmark = min_wmark_pages(zone);
4621 bool wmark;
Michal Hocko0a0337e2016-05-20 16:57:00 -07004622
Mel Gorman5a1c84b2016-07-28 15:47:31 -07004623 available = reclaimable = zone_reclaimable_pages(zone);
Mel Gorman5a1c84b2016-07-28 15:47:31 -07004624 available += zone_page_state_snapshot(zone, NR_FREE_PAGES);
Michal Hocko0a0337e2016-05-20 16:57:00 -07004625
4626 /*
Johannes Weiner491d79a2017-05-03 14:52:16 -07004627 * Would the allocation succeed if we reclaimed all
4628 * reclaimable pages?
Michal Hocko0a0337e2016-05-20 16:57:00 -07004629 */
Michal Hockod379f012017-02-22 15:42:00 -08004630 wmark = __zone_watermark_ok(zone, order, min_wmark,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004631 ac->highest_zoneidx, alloc_flags, available);
Michal Hockod379f012017-02-22 15:42:00 -08004632 trace_reclaim_retry_zone(z, order, reclaimable,
4633 available, min_wmark, *no_progress_loops, wmark);
4634 if (wmark) {
Michal Hockoede37712016-05-20 16:57:03 -07004635 /*
4636 * If we didn't make any progress and have a lot of
4637 * dirty + writeback pages then we should wait for
4638 * an IO to complete to slow down the reclaim and
4639 * prevent from pre mature OOM
4640 */
4641 if (!did_some_progress) {
Mel Gorman11fb9982016-07-28 15:46:20 -07004642 unsigned long write_pending;
Michal Hockoede37712016-05-20 16:57:03 -07004643
Mel Gorman5a1c84b2016-07-28 15:47:31 -07004644 write_pending = zone_page_state_snapshot(zone,
4645 NR_ZONE_WRITE_PENDING);
Michal Hockoede37712016-05-20 16:57:03 -07004646
Mel Gorman11fb9982016-07-28 15:46:20 -07004647 if (2 * write_pending > reclaimable) {
Michal Hockoede37712016-05-20 16:57:03 -07004648 congestion_wait(BLK_RW_ASYNC, HZ/10);
4649 return true;
4650 }
4651 }
Mel Gorman5a1c84b2016-07-28 15:47:31 -07004652
Michal Hocko15f570b2018-10-26 15:03:31 -07004653 ret = true;
4654 goto out;
Michal Hocko0a0337e2016-05-20 16:57:00 -07004655 }
4656 }
4657
Michal Hocko15f570b2018-10-26 15:03:31 -07004658out:
4659 /*
4660 * Memory allocation/reclaim might be called from a WQ context and the
4661 * current implementation of the WQ concurrency control doesn't
4662 * recognize that a particular WQ is congested if the worker thread is
4663 * looping without ever sleeping. Therefore we have to do a short sleep
4664 * here rather than calling cond_resched().
4665 */
4666 if (current->flags & PF_WQ_WORKER)
4667 schedule_timeout_uninterruptible(1);
4668 else
4669 cond_resched();
4670 return ret;
Michal Hocko0a0337e2016-05-20 16:57:00 -07004671}
4672
Vlastimil Babka902b6282017-07-06 15:39:56 -07004673static inline bool
4674check_retry_cpuset(int cpuset_mems_cookie, struct alloc_context *ac)
4675{
4676 /*
4677 * It's possible that cpuset's mems_allowed and the nodemask from
4678 * mempolicy don't intersect. This should be normally dealt with by
4679 * policy_nodemask(), but it's possible to race with cpuset update in
4680 * such a way the check therein was true, and then it became false
4681 * before we got our cpuset_mems_cookie here.
4682 * This assumes that for all allocations, ac->nodemask can come only
4683 * from MPOL_BIND mempolicy (whose documented semantics is to be ignored
4684 * when it does not intersect with the cpuset restrictions) or the
4685 * caller can deal with a violated nodemask.
4686 */
4687 if (cpusets_enabled() && ac->nodemask &&
4688 !cpuset_nodemask_valid_mems_allowed(ac->nodemask)) {
4689 ac->nodemask = NULL;
4690 return true;
4691 }
4692
4693 /*
4694 * When updating a task's mems_allowed or mempolicy nodemask, it is
4695 * possible to race with parallel threads in such a way that our
4696 * allocation can fail while the mask is being updated. If we are about
4697 * to fail, check if the cpuset changed during allocation and if so,
4698 * retry.
4699 */
4700 if (read_mems_allowed_retry(cpuset_mems_cookie))
4701 return true;
4702
4703 return false;
4704}
4705
Mel Gorman11e33f62009-06-16 15:31:57 -07004706static inline struct page *
4707__alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
Vlastimil Babkaa9263752015-02-11 15:25:41 -08004708 struct alloc_context *ac)
Mel Gorman11e33f62009-06-16 15:31:57 -07004709{
Mel Gormand0164ad2015-11-06 16:28:21 -08004710 bool can_direct_reclaim = gfp_mask & __GFP_DIRECT_RECLAIM;
Vlastimil Babka282722b2017-05-08 15:54:49 -07004711 const bool costly_order = order > PAGE_ALLOC_COSTLY_ORDER;
Mel Gorman11e33f62009-06-16 15:31:57 -07004712 struct page *page = NULL;
Mel Gormanc6038442016-05-19 17:13:38 -07004713 unsigned int alloc_flags;
Mel Gorman11e33f62009-06-16 15:31:57 -07004714 unsigned long did_some_progress;
Vlastimil Babka5ce9bfe2017-01-24 15:18:38 -08004715 enum compact_priority compact_priority;
Michal Hockoc5d01d02016-05-20 16:56:53 -07004716 enum compact_result compact_result;
Vlastimil Babka5ce9bfe2017-01-24 15:18:38 -08004717 int compaction_retries;
4718 int no_progress_loops;
Vlastimil Babka5ce9bfe2017-01-24 15:18:38 -08004719 unsigned int cpuset_mems_cookie;
Michal Hockocd04ae12017-09-06 16:24:50 -07004720 int reserve_flags;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08004721
Christoph Lameter952f3b52006-12-06 20:33:26 -08004722 /*
Mel Gormand0164ad2015-11-06 16:28:21 -08004723 * We also sanity check to catch abuse of atomic reserves being used by
4724 * callers that are not in atomic context.
4725 */
4726 if (WARN_ON_ONCE((gfp_mask & (__GFP_ATOMIC|__GFP_DIRECT_RECLAIM)) ==
4727 (__GFP_ATOMIC|__GFP_DIRECT_RECLAIM)))
4728 gfp_mask &= ~__GFP_ATOMIC;
4729
Vlastimil Babka5ce9bfe2017-01-24 15:18:38 -08004730retry_cpuset:
4731 compaction_retries = 0;
4732 no_progress_loops = 0;
4733 compact_priority = DEF_COMPACT_PRIORITY;
4734 cpuset_mems_cookie = read_mems_allowed_begin();
Michal Hocko9a67f642017-02-22 15:46:19 -08004735
4736 /*
4737 * The fast path uses conservative alloc_flags to succeed only until
4738 * kswapd needs to be woken up, and to avoid the cost of setting up
4739 * alloc_flags precisely. So we do that now.
4740 */
4741 alloc_flags = gfp_to_alloc_flags(gfp_mask);
4742
Vlastimil Babkae47483b2017-01-24 15:18:41 -08004743 /*
4744 * We need to recalculate the starting point for the zonelist iterator
4745 * because we might have used different nodemask in the fast path, or
4746 * there was a cpuset modification and we are retrying - otherwise we
4747 * could end up iterating over non-eligible zones endlessly.
4748 */
4749 ac->preferred_zoneref = first_zones_zonelist(ac->zonelist,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004750 ac->highest_zoneidx, ac->nodemask);
Vlastimil Babkae47483b2017-01-24 15:18:41 -08004751 if (!ac->preferred_zoneref->zone)
4752 goto nopage;
4753
Mel Gorman0a79cda2018-12-28 00:35:48 -08004754 if (alloc_flags & ALLOC_KSWAPD)
David Rientjes5ecd9d42018-04-05 16:25:16 -07004755 wake_all_kswapds(order, gfp_mask, ac);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08004756
Paul Jackson9bf22292005-09-06 15:18:12 -07004757 /*
Vlastimil Babka23771232016-07-28 15:49:16 -07004758 * The adjusted alloc_flags might result in immediate success, so try
4759 * that first
4760 */
4761 page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac);
4762 if (page)
4763 goto got_pg;
4764
Vlastimil Babkaa8161d12016-07-28 15:49:19 -07004765 /*
4766 * For costly allocations, try direct compaction first, as it's likely
Vlastimil Babka282722b2017-05-08 15:54:49 -07004767 * that we have enough base pages and don't need to reclaim. For non-
4768 * movable high-order allocations, do that as well, as compaction will
4769 * try prevent permanent fragmentation by migrating from blocks of the
4770 * same migratetype.
4771 * Don't try this for allocations that are allowed to ignore
4772 * watermarks, as the ALLOC_NO_WATERMARKS attempt didn't yet happen.
Vlastimil Babkaa8161d12016-07-28 15:49:19 -07004773 */
Vlastimil Babka282722b2017-05-08 15:54:49 -07004774 if (can_direct_reclaim &&
4775 (costly_order ||
4776 (order > 0 && ac->migratetype != MIGRATE_MOVABLE))
4777 && !gfp_pfmemalloc_allowed(gfp_mask)) {
Vlastimil Babkaa8161d12016-07-28 15:49:19 -07004778 page = __alloc_pages_direct_compact(gfp_mask, order,
4779 alloc_flags, ac,
Vlastimil Babkaa5508cd2016-07-28 15:49:28 -07004780 INIT_COMPACT_PRIORITY,
Vlastimil Babkaa8161d12016-07-28 15:49:19 -07004781 &compact_result);
4782 if (page)
4783 goto got_pg;
4784
Vlastimil Babkacc638f32020-01-13 16:29:04 -08004785 /*
4786 * Checks for costly allocations with __GFP_NORETRY, which
4787 * includes some THP page fault allocations
4788 */
4789 if (costly_order && (gfp_mask & __GFP_NORETRY)) {
David Rientjesb39d0ee2019-09-04 12:54:22 -07004790 /*
4791 * If allocating entire pageblock(s) and compaction
4792 * failed because all zones are below low watermarks
4793 * or is prohibited because it recently failed at this
David Rientjes3f36d862019-10-14 14:12:04 -07004794 * order, fail immediately unless the allocator has
4795 * requested compaction and reclaim retry.
David Rientjesb39d0ee2019-09-04 12:54:22 -07004796 *
4797 * Reclaim is
4798 * - potentially very expensive because zones are far
4799 * below their low watermarks or this is part of very
4800 * bursty high order allocations,
4801 * - not guaranteed to help because isolate_freepages()
4802 * may not iterate over freed pages as part of its
4803 * linear scan, and
4804 * - unlikely to make entire pageblocks free on its
4805 * own.
4806 */
4807 if (compact_result == COMPACT_SKIPPED ||
4808 compact_result == COMPACT_DEFERRED)
4809 goto nopage;
Vlastimil Babkaa8161d12016-07-28 15:49:19 -07004810
4811 /*
Vlastimil Babka3eb27712016-07-28 15:49:22 -07004812 * Looks like reclaim/compaction is worth trying, but
4813 * sync compaction could be very expensive, so keep
Vlastimil Babka25160352016-07-28 15:49:25 -07004814 * using async compaction.
Vlastimil Babkaa8161d12016-07-28 15:49:19 -07004815 */
Vlastimil Babkaa5508cd2016-07-28 15:49:28 -07004816 compact_priority = INIT_COMPACT_PRIORITY;
Vlastimil Babkaa8161d12016-07-28 15:49:19 -07004817 }
4818 }
Vlastimil Babka23771232016-07-28 15:49:16 -07004819
4820retry:
4821 /* Ensure kswapd doesn't accidentally go to sleep as long as we loop */
Mel Gorman0a79cda2018-12-28 00:35:48 -08004822 if (alloc_flags & ALLOC_KSWAPD)
David Rientjes5ecd9d42018-04-05 16:25:16 -07004823 wake_all_kswapds(order, gfp_mask, ac);
Vlastimil Babka23771232016-07-28 15:49:16 -07004824
Michal Hockocd04ae12017-09-06 16:24:50 -07004825 reserve_flags = __gfp_pfmemalloc_flags(gfp_mask);
4826 if (reserve_flags)
Pavel Tatashin8e3560d2021-05-04 18:39:00 -07004827 alloc_flags = gfp_to_alloc_flags_cma(gfp_mask, reserve_flags);
Vlastimil Babka23771232016-07-28 15:49:16 -07004828
4829 /*
Vlastimil Babkad6a24df2018-08-17 15:45:05 -07004830 * Reset the nodemask and zonelist iterators if memory policies can be
4831 * ignored. These allocations are high priority and system rather than
4832 * user oriented.
Mel Gormane46e7b72016-06-03 14:56:01 -07004833 */
Michal Hockocd04ae12017-09-06 16:24:50 -07004834 if (!(alloc_flags & ALLOC_CPUSET) || reserve_flags) {
Vlastimil Babkad6a24df2018-08-17 15:45:05 -07004835 ac->nodemask = NULL;
Mel Gormane46e7b72016-06-03 14:56:01 -07004836 ac->preferred_zoneref = first_zones_zonelist(ac->zonelist,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004837 ac->highest_zoneidx, ac->nodemask);
Mel Gormane46e7b72016-06-03 14:56:01 -07004838 }
4839
Vlastimil Babka23771232016-07-28 15:49:16 -07004840 /* Attempt with potentially adjusted zonelist and alloc_flags */
Vlastimil Babka31a6c192016-07-28 15:49:13 -07004841 page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08004842 if (page)
4843 goto got_pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844
Mel Gormand0164ad2015-11-06 16:28:21 -08004845 /* Caller is not willing to reclaim, we can't balance anything */
Michal Hocko9a67f642017-02-22 15:46:19 -08004846 if (!can_direct_reclaim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 goto nopage;
Michal Hocko9a67f642017-02-22 15:46:19 -08004848
Peter Zijlstra341ce062009-06-16 15:32:02 -07004849 /* Avoid recursion of direct reclaim */
Michal Hocko9a67f642017-02-22 15:46:19 -08004850 if (current->flags & PF_MEMALLOC)
Peter Zijlstra341ce062009-06-16 15:32:02 -07004851 goto nopage;
David Rientjes8fe78042014-08-06 16:07:54 -07004852
Mel Gorman11e33f62009-06-16 15:31:57 -07004853 /* Try direct reclaim and then allocating */
Vlastimil Babkaa9263752015-02-11 15:25:41 -08004854 page = __alloc_pages_direct_reclaim(gfp_mask, order, alloc_flags, ac,
4855 &did_some_progress);
Mel Gorman11e33f62009-06-16 15:31:57 -07004856 if (page)
4857 goto got_pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858
Vlastimil Babkaa8161d12016-07-28 15:49:19 -07004859 /* Try direct compaction and then allocating */
4860 page = __alloc_pages_direct_compact(gfp_mask, order, alloc_flags, ac,
Vlastimil Babkaa5508cd2016-07-28 15:49:28 -07004861 compact_priority, &compact_result);
Vlastimil Babkaa8161d12016-07-28 15:49:19 -07004862 if (page)
4863 goto got_pg;
4864
Johannes Weiner90839052015-06-24 16:57:21 -07004865 /* Do not loop if specifically requested */
4866 if (gfp_mask & __GFP_NORETRY)
Vlastimil Babkaa8161d12016-07-28 15:49:19 -07004867 goto nopage;
Johannes Weiner90839052015-06-24 16:57:21 -07004868
Michal Hocko0a0337e2016-05-20 16:57:00 -07004869 /*
4870 * Do not retry costly high order allocations unless they are
Michal Hockodcda9b02017-07-12 14:36:45 -07004871 * __GFP_RETRY_MAYFAIL
Michal Hocko0a0337e2016-05-20 16:57:00 -07004872 */
Michal Hockodcda9b02017-07-12 14:36:45 -07004873 if (costly_order && !(gfp_mask & __GFP_RETRY_MAYFAIL))
Vlastimil Babkaa8161d12016-07-28 15:49:19 -07004874 goto nopage;
Michal Hocko0a0337e2016-05-20 16:57:00 -07004875
Michal Hocko0a0337e2016-05-20 16:57:00 -07004876 if (should_reclaim_retry(gfp_mask, order, ac, alloc_flags,
Vlastimil Babka423b4522016-10-07 17:00:40 -07004877 did_some_progress > 0, &no_progress_loops))
Michal Hocko0a0337e2016-05-20 16:57:00 -07004878 goto retry;
4879
Michal Hocko33c2d212016-05-20 16:57:06 -07004880 /*
4881 * It doesn't make any sense to retry for the compaction if the order-0
4882 * reclaim is not able to make any progress because the current
4883 * implementation of the compaction depends on the sufficient amount
4884 * of free memory (see __compaction_suitable)
4885 */
4886 if (did_some_progress > 0 &&
Michal Hocko86a294a2016-05-20 16:57:12 -07004887 should_compact_retry(ac, order, alloc_flags,
Vlastimil Babkaa5508cd2016-07-28 15:49:28 -07004888 compact_result, &compact_priority,
Vlastimil Babkad9436492016-10-07 17:00:31 -07004889 &compaction_retries))
Michal Hocko33c2d212016-05-20 16:57:06 -07004890 goto retry;
4891
Vlastimil Babka902b6282017-07-06 15:39:56 -07004892
4893 /* Deal with possible cpuset update races before we start OOM killing */
4894 if (check_retry_cpuset(cpuset_mems_cookie, ac))
Vlastimil Babkae47483b2017-01-24 15:18:41 -08004895 goto retry_cpuset;
4896
Johannes Weiner90839052015-06-24 16:57:21 -07004897 /* Reclaim has failed us, start killing things */
4898 page = __alloc_pages_may_oom(gfp_mask, order, ac, &did_some_progress);
4899 if (page)
4900 goto got_pg;
4901
Michal Hocko9a67f642017-02-22 15:46:19 -08004902 /* Avoid allocations with no watermarks from looping endlessly */
Michal Hockocd04ae12017-09-06 16:24:50 -07004903 if (tsk_is_oom_victim(current) &&
Joonsoo Kim8510e692020-08-06 23:26:04 -07004904 (alloc_flags & ALLOC_OOM ||
Tetsuo Handac2889832017-06-02 14:46:31 -07004905 (gfp_mask & __GFP_NOMEMALLOC)))
Michal Hocko9a67f642017-02-22 15:46:19 -08004906 goto nopage;
4907
Johannes Weiner90839052015-06-24 16:57:21 -07004908 /* Retry as long as the OOM killer is making progress */
Michal Hocko0a0337e2016-05-20 16:57:00 -07004909 if (did_some_progress) {
4910 no_progress_loops = 0;
Johannes Weiner90839052015-06-24 16:57:21 -07004911 goto retry;
Michal Hocko0a0337e2016-05-20 16:57:00 -07004912 }
Johannes Weiner90839052015-06-24 16:57:21 -07004913
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914nopage:
Vlastimil Babka902b6282017-07-06 15:39:56 -07004915 /* Deal with possible cpuset update races before we fail */
4916 if (check_retry_cpuset(cpuset_mems_cookie, ac))
Vlastimil Babka5ce9bfe2017-01-24 15:18:38 -08004917 goto retry_cpuset;
4918
Michal Hocko9a67f642017-02-22 15:46:19 -08004919 /*
4920 * Make sure that __GFP_NOFAIL request doesn't leak out and make sure
4921 * we always retry
4922 */
4923 if (gfp_mask & __GFP_NOFAIL) {
4924 /*
4925 * All existing users of the __GFP_NOFAIL are blockable, so warn
4926 * of any new users that actually require GFP_NOWAIT
4927 */
4928 if (WARN_ON_ONCE(!can_direct_reclaim))
4929 goto fail;
4930
4931 /*
4932 * PF_MEMALLOC request from this context is rather bizarre
4933 * because we cannot reclaim anything and only can loop waiting
4934 * for somebody to do a work for us
4935 */
4936 WARN_ON_ONCE(current->flags & PF_MEMALLOC);
4937
4938 /*
4939 * non failing costly orders are a hard requirement which we
4940 * are not prepared for much so let's warn about these users
4941 * so that we can identify them and convert them to something
4942 * else.
4943 */
4944 WARN_ON_ONCE(order > PAGE_ALLOC_COSTLY_ORDER);
4945
Michal Hocko6c18ba72017-02-22 15:46:25 -08004946 /*
4947 * Help non-failing allocations by giving them access to memory
4948 * reserves but do not use ALLOC_NO_WATERMARKS because this
4949 * could deplete whole memory reserves which would just make
4950 * the situation worse
4951 */
4952 page = __alloc_pages_cpuset_fallback(gfp_mask, order, ALLOC_HARDER, ac);
4953 if (page)
4954 goto got_pg;
4955
Michal Hocko9a67f642017-02-22 15:46:19 -08004956 cond_resched();
4957 goto retry;
4958 }
4959fail:
Michal Hockoa8e99252017-02-22 15:46:10 -08004960 warn_alloc(gfp_mask, ac->nodemask,
Michal Hocko7877cdc2016-10-07 17:01:55 -07004961 "page allocation failure: order:%u", order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962got_pg:
Mel Gorman072bb0a2012-07-31 16:43:58 -07004963 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964}
Mel Gorman11e33f62009-06-16 15:31:57 -07004965
Mel Gorman9cd75552017-02-24 14:56:29 -08004966static inline bool prepare_alloc_pages(gfp_t gfp_mask, unsigned int order,
Vlastimil Babka04ec6262017-07-06 15:40:03 -07004967 int preferred_nid, nodemask_t *nodemask,
Matthew Wilcox (Oracle)8e6a9302021-04-29 23:01:10 -07004968 struct alloc_context *ac, gfp_t *alloc_gfp,
Mel Gorman9cd75552017-02-24 14:56:29 -08004969 unsigned int *alloc_flags)
4970{
Joonsoo Kim97a225e2020-06-03 15:59:01 -07004971 ac->highest_zoneidx = gfp_zone(gfp_mask);
Vlastimil Babka04ec6262017-07-06 15:40:03 -07004972 ac->zonelist = node_zonelist(preferred_nid, gfp_mask);
Mel Gorman9cd75552017-02-24 14:56:29 -08004973 ac->nodemask = nodemask;
Wei Yang01c0bfe2020-06-03 15:59:08 -07004974 ac->migratetype = gfp_migratetype(gfp_mask);
Mel Gorman9cd75552017-02-24 14:56:29 -08004975
4976 if (cpusets_enabled()) {
Matthew Wilcox (Oracle)8e6a9302021-04-29 23:01:10 -07004977 *alloc_gfp |= __GFP_HARDWALL;
Muchun Song182f3d72020-08-06 23:26:01 -07004978 /*
4979 * When we are in the interrupt context, it is irrelevant
4980 * to the current task context. It means that any node ok.
4981 */
4982 if (!in_interrupt() && !ac->nodemask)
Mel Gorman9cd75552017-02-24 14:56:29 -08004983 ac->nodemask = &cpuset_current_mems_allowed;
Vlastimil Babka51047822017-02-24 14:56:53 -08004984 else
4985 *alloc_flags |= ALLOC_CPUSET;
Mel Gorman9cd75552017-02-24 14:56:29 -08004986 }
4987
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01004988 fs_reclaim_acquire(gfp_mask);
4989 fs_reclaim_release(gfp_mask);
Mel Gorman9cd75552017-02-24 14:56:29 -08004990
4991 might_sleep_if(gfp_mask & __GFP_DIRECT_RECLAIM);
4992
4993 if (should_fail_alloc_page(gfp_mask, order))
4994 return false;
4995
Pavel Tatashin8e3560d2021-05-04 18:39:00 -07004996 *alloc_flags = gfp_to_alloc_flags_cma(gfp_mask, *alloc_flags);
Joonsoo Kimd883c6c2018-05-23 10:18:21 +09004997
Mel Gorman9cd75552017-02-24 14:56:29 -08004998 /* Dirty zone balancing only done in the fast path */
4999 ac->spread_dirty_pages = (gfp_mask & __GFP_WRITE);
5000
5001 /*
5002 * The preferred zone is used for statistics but crucially it is
5003 * also used as the starting point for the zonelist iterator. It
5004 * may get reset for allocations that ignore memory policies.
5005 */
5006 ac->preferred_zoneref = first_zones_zonelist(ac->zonelist,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07005007 ac->highest_zoneidx, ac->nodemask);
Mateusz Noseka0622d02020-10-13 16:55:51 -07005008
5009 return true;
Mel Gorman9cd75552017-02-24 14:56:29 -08005010}
5011
Mel Gorman11e33f62009-06-16 15:31:57 -07005012/*
Mel Gorman0f87d9d2021-04-29 23:01:48 -07005013 * __alloc_pages_bulk - Allocate a number of order-0 pages to a list or array
Mel Gorman387ba26f2021-04-29 23:01:45 -07005014 * @gfp: GFP flags for the allocation
5015 * @preferred_nid: The preferred NUMA node ID to allocate from
5016 * @nodemask: Set of nodes to allocate from, may be NULL
Mel Gorman0f87d9d2021-04-29 23:01:48 -07005017 * @nr_pages: The number of pages desired on the list or array
5018 * @page_list: Optional list to store the allocated pages
5019 * @page_array: Optional array to store the pages
Mel Gorman387ba26f2021-04-29 23:01:45 -07005020 *
5021 * This is a batched version of the page allocator that attempts to
Mel Gorman0f87d9d2021-04-29 23:01:48 -07005022 * allocate nr_pages quickly. Pages are added to page_list if page_list
5023 * is not NULL, otherwise it is assumed that the page_array is valid.
Mel Gorman387ba26f2021-04-29 23:01:45 -07005024 *
Mel Gorman0f87d9d2021-04-29 23:01:48 -07005025 * For lists, nr_pages is the number of pages that should be allocated.
5026 *
5027 * For arrays, only NULL elements are populated with pages and nr_pages
5028 * is the maximum number of pages that will be stored in the array.
5029 *
5030 * Returns the number of pages on the list or array.
Mel Gorman387ba26f2021-04-29 23:01:45 -07005031 */
5032unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,
5033 nodemask_t *nodemask, int nr_pages,
Mel Gorman0f87d9d2021-04-29 23:01:48 -07005034 struct list_head *page_list,
5035 struct page **page_array)
Mel Gorman387ba26f2021-04-29 23:01:45 -07005036{
5037 struct page *page;
5038 unsigned long flags;
5039 struct zone *zone;
5040 struct zoneref *z;
5041 struct per_cpu_pages *pcp;
5042 struct list_head *pcp_list;
5043 struct alloc_context ac;
5044 gfp_t alloc_gfp;
5045 unsigned int alloc_flags = ALLOC_WMARK_LOW;
Mel Gorman0f87d9d2021-04-29 23:01:48 -07005046 int nr_populated = 0;
Mel Gorman387ba26f2021-04-29 23:01:45 -07005047
Jesper Dangaard Brouerce76f9a2021-04-29 23:01:51 -07005048 if (unlikely(nr_pages <= 0))
Mel Gorman387ba26f2021-04-29 23:01:45 -07005049 return 0;
5050
Mel Gorman0f87d9d2021-04-29 23:01:48 -07005051 /*
5052 * Skip populated array elements to determine if any pages need
5053 * to be allocated before disabling IRQs.
5054 */
Rasmus Villemoesb08e50d2021-06-24 18:40:04 -07005055 while (page_array && nr_populated < nr_pages && page_array[nr_populated])
Mel Gorman0f87d9d2021-04-29 23:01:48 -07005056 nr_populated++;
5057
Mel Gormanb3b64eb2021-06-24 18:40:07 -07005058 /* Already populated array? */
5059 if (unlikely(page_array && nr_pages - nr_populated == 0))
Mel Gormanff4b2b42021-06-28 19:33:29 -07005060 return nr_populated;
Mel Gormanb3b64eb2021-06-24 18:40:07 -07005061
Mel Gorman387ba26f2021-04-29 23:01:45 -07005062 /* Use the single page allocator for one page. */
Mel Gorman0f87d9d2021-04-29 23:01:48 -07005063 if (nr_pages - nr_populated == 1)
Mel Gorman387ba26f2021-04-29 23:01:45 -07005064 goto failed;
5065
5066 /* May set ALLOC_NOFRAGMENT, fragmentation will return 1 page. */
5067 gfp &= gfp_allowed_mask;
5068 alloc_gfp = gfp;
5069 if (!prepare_alloc_pages(gfp, 0, preferred_nid, nodemask, &ac, &alloc_gfp, &alloc_flags))
5070 return 0;
5071 gfp = alloc_gfp;
5072
5073 /* Find an allowed local zone that meets the low watermark. */
5074 for_each_zone_zonelist_nodemask(zone, z, ac.zonelist, ac.highest_zoneidx, ac.nodemask) {
5075 unsigned long mark;
5076
5077 if (cpusets_enabled() && (alloc_flags & ALLOC_CPUSET) &&
5078 !__cpuset_zone_allowed(zone, gfp)) {
5079 continue;
5080 }
5081
5082 if (nr_online_nodes > 1 && zone != ac.preferred_zoneref->zone &&
5083 zone_to_nid(zone) != zone_to_nid(ac.preferred_zoneref->zone)) {
5084 goto failed;
5085 }
5086
5087 mark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK) + nr_pages;
5088 if (zone_watermark_fast(zone, 0, mark,
5089 zonelist_zone_idx(ac.preferred_zoneref),
5090 alloc_flags, gfp)) {
5091 break;
5092 }
5093 }
5094
5095 /*
5096 * If there are no allowed local zones that meets the watermarks then
5097 * try to allocate a single page and reclaim if necessary.
5098 */
Jesper Dangaard Brouerce76f9a2021-04-29 23:01:51 -07005099 if (unlikely(!zone))
Mel Gorman387ba26f2021-04-29 23:01:45 -07005100 goto failed;
5101
5102 /* Attempt the batch allocation */
5103 local_irq_save(flags);
5104 pcp = &this_cpu_ptr(zone->pageset)->pcp;
5105 pcp_list = &pcp->lists[ac.migratetype];
5106
Mel Gorman0f87d9d2021-04-29 23:01:48 -07005107 while (nr_populated < nr_pages) {
5108
5109 /* Skip existing pages */
5110 if (page_array && page_array[nr_populated]) {
5111 nr_populated++;
5112 continue;
5113 }
5114
Mel Gorman387ba26f2021-04-29 23:01:45 -07005115 page = __rmqueue_pcplist(zone, ac.migratetype, alloc_flags,
5116 pcp, pcp_list);
Jesper Dangaard Brouerce76f9a2021-04-29 23:01:51 -07005117 if (unlikely(!page)) {
Mel Gorman387ba26f2021-04-29 23:01:45 -07005118 /* Try and get at least one page */
Mel Gorman0f87d9d2021-04-29 23:01:48 -07005119 if (!nr_populated)
Mel Gorman387ba26f2021-04-29 23:01:45 -07005120 goto failed_irq;
5121 break;
5122 }
5123
5124 /*
5125 * Ideally this would be batched but the best way to do
5126 * that cheaply is to first convert zone_statistics to
5127 * be inaccurate per-cpu counter like vm_events to avoid
5128 * a RMW cycle then do the accounting with IRQs enabled.
5129 */
5130 __count_zid_vm_events(PGALLOC, zone_idx(zone), 1);
5131 zone_statistics(ac.preferred_zoneref->zone, zone);
5132
5133 prep_new_page(page, 0, gfp, 0);
Mel Gorman0f87d9d2021-04-29 23:01:48 -07005134 if (page_list)
5135 list_add(&page->lru, page_list);
5136 else
5137 page_array[nr_populated] = page;
5138 nr_populated++;
Mel Gorman387ba26f2021-04-29 23:01:45 -07005139 }
5140
5141 local_irq_restore(flags);
5142
Mel Gorman0f87d9d2021-04-29 23:01:48 -07005143 return nr_populated;
Mel Gorman387ba26f2021-04-29 23:01:45 -07005144
5145failed_irq:
5146 local_irq_restore(flags);
5147
5148failed:
5149 page = __alloc_pages(gfp, 0, preferred_nid, nodemask);
5150 if (page) {
Mel Gorman0f87d9d2021-04-29 23:01:48 -07005151 if (page_list)
5152 list_add(&page->lru, page_list);
5153 else
5154 page_array[nr_populated] = page;
5155 nr_populated++;
Mel Gorman387ba26f2021-04-29 23:01:45 -07005156 }
5157
Mel Gorman0f87d9d2021-04-29 23:01:48 -07005158 return nr_populated;
Mel Gorman387ba26f2021-04-29 23:01:45 -07005159}
5160EXPORT_SYMBOL_GPL(__alloc_pages_bulk);
5161
5162/*
Mel Gorman11e33f62009-06-16 15:31:57 -07005163 * This is the 'heart' of the zoned buddy allocator.
5164 */
Matthew Wilcox (Oracle)84172f42021-04-29 23:01:15 -07005165struct page *__alloc_pages(gfp_t gfp, unsigned int order, int preferred_nid,
Vlastimil Babka04ec6262017-07-06 15:40:03 -07005166 nodemask_t *nodemask)
Mel Gorman11e33f62009-06-16 15:31:57 -07005167{
Mel Gorman5bb1b162016-05-19 17:13:50 -07005168 struct page *page;
Mel Gormane6cbd7f2016-07-28 15:46:50 -07005169 unsigned int alloc_flags = ALLOC_WMARK_LOW;
Matthew Wilcox (Oracle)8e6a9302021-04-29 23:01:10 -07005170 gfp_t alloc_gfp; /* The gfp_t that was actually used for allocation */
Mel Gorman9cd75552017-02-24 14:56:29 -08005171 struct alloc_context ac = { };
Mel Gorman682a3382016-05-19 17:13:30 -07005172
Michal Hockoc63ae432018-11-16 15:08:53 -08005173 /*
5174 * There are several places where we assume that the order value is sane
5175 * so bail out early if the request is out of bound.
5176 */
5177 if (unlikely(order >= MAX_ORDER)) {
Matthew Wilcox (Oracle)6e5e0f22021-04-29 23:01:13 -07005178 WARN_ON_ONCE(!(gfp & __GFP_NOWARN));
Michal Hockoc63ae432018-11-16 15:08:53 -08005179 return NULL;
5180 }
5181
Matthew Wilcox (Oracle)6e5e0f22021-04-29 23:01:13 -07005182 gfp &= gfp_allowed_mask;
Pavel Tatashinda6df1b2021-05-04 18:38:57 -07005183 /*
5184 * Apply scoped allocation constraints. This is mainly about GFP_NOFS
5185 * resp. GFP_NOIO which has to be inherited for all allocation requests
5186 * from a particular context which has been marked by
Pavel Tatashin8e3560d2021-05-04 18:39:00 -07005187 * memalloc_no{fs,io}_{save,restore}. And PF_MEMALLOC_PIN which ensures
5188 * movable zones are not used during allocation.
Pavel Tatashinda6df1b2021-05-04 18:38:57 -07005189 */
5190 gfp = current_gfp_context(gfp);
Matthew Wilcox (Oracle)6e5e0f22021-04-29 23:01:13 -07005191 alloc_gfp = gfp;
5192 if (!prepare_alloc_pages(gfp, order, preferred_nid, nodemask, &ac,
Matthew Wilcox (Oracle)8e6a9302021-04-29 23:01:10 -07005193 &alloc_gfp, &alloc_flags))
Mel Gorman11e33f62009-06-16 15:31:57 -07005194 return NULL;
5195
Mel Gorman6bb15452018-12-28 00:35:41 -08005196 /*
5197 * Forbid the first pass from falling back to types that fragment
5198 * memory until all local zones are considered.
5199 */
Matthew Wilcox (Oracle)6e5e0f22021-04-29 23:01:13 -07005200 alloc_flags |= alloc_flags_nofragment(ac.preferred_zoneref->zone, gfp);
Mel Gorman6bb15452018-12-28 00:35:41 -08005201
Mel Gorman5117f452009-06-16 15:31:59 -07005202 /* First allocation attempt */
Matthew Wilcox (Oracle)8e6a9302021-04-29 23:01:10 -07005203 page = get_page_from_freelist(alloc_gfp, order, alloc_flags, &ac);
Mel Gorman4fcb0972016-05-19 17:14:01 -07005204 if (likely(page))
5205 goto out;
Andrew Morton91fbdc02015-02-11 15:25:04 -08005206
Pavel Tatashinda6df1b2021-05-04 18:38:57 -07005207 alloc_gfp = gfp;
Mel Gorman4fcb0972016-05-19 17:14:01 -07005208 ac.spread_dirty_pages = false;
Mel Gorman11e33f62009-06-16 15:31:57 -07005209
Mel Gorman47415262016-05-19 17:14:44 -07005210 /*
5211 * Restore the original nodemask if it was potentially replaced with
5212 * &cpuset_current_mems_allowed to optimize the fast-path attempt.
5213 */
Mateusz Nosek97ce86f2020-04-01 21:09:53 -07005214 ac.nodemask = nodemask;
Vlastimil Babka16096c22017-01-24 15:18:35 -08005215
Matthew Wilcox (Oracle)8e6a9302021-04-29 23:01:10 -07005216 page = __alloc_pages_slowpath(alloc_gfp, order, &ac);
Xishi Qiu23f086f2015-02-11 15:25:07 -08005217
Mel Gorman4fcb0972016-05-19 17:14:01 -07005218out:
Matthew Wilcox (Oracle)6e5e0f22021-04-29 23:01:13 -07005219 if (memcg_kmem_enabled() && (gfp & __GFP_ACCOUNT) && page &&
5220 unlikely(__memcg_kmem_charge_page(page, gfp, order) != 0)) {
Vladimir Davydovc4159a72016-08-08 23:03:12 +03005221 __free_pages(page, order);
5222 page = NULL;
Vladimir Davydov49491482016-07-26 15:24:24 -07005223 }
5224
Matthew Wilcox (Oracle)8e6a9302021-04-29 23:01:10 -07005225 trace_mm_page_alloc(page, order, alloc_gfp, ac.migratetype);
Mel Gorman4fcb0972016-05-19 17:14:01 -07005226
Mel Gorman11e33f62009-06-16 15:31:57 -07005227 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228}
Matthew Wilcox (Oracle)84172f42021-04-29 23:01:15 -07005229EXPORT_SYMBOL(__alloc_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230
5231/*
Michal Hocko9ea9a682018-08-17 15:46:01 -07005232 * Common helper functions. Never use with __GFP_HIGHMEM because the returned
5233 * address cannot represent highmem pages. Use alloc_pages and then kmap if
5234 * you need to access high mem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 */
Harvey Harrison920c7a52008-02-04 22:29:26 -08005236unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237{
Akinobu Mita945a1112009-09-21 17:01:47 -07005238 struct page *page;
5239
Michal Hocko9ea9a682018-08-17 15:46:01 -07005240 page = alloc_pages(gfp_mask & ~__GFP_HIGHMEM, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 if (!page)
5242 return 0;
5243 return (unsigned long) page_address(page);
5244}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245EXPORT_SYMBOL(__get_free_pages);
5246
Harvey Harrison920c7a52008-02-04 22:29:26 -08005247unsigned long get_zeroed_page(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248{
Akinobu Mita945a1112009-09-21 17:01:47 -07005249 return __get_free_pages(gfp_mask | __GFP_ZERO, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251EXPORT_SYMBOL(get_zeroed_page);
5252
Aaron Lu742aa7f2018-12-28 00:35:22 -08005253static inline void free_the_page(struct page *page, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254{
Aaron Lu742aa7f2018-12-28 00:35:22 -08005255 if (order == 0) /* Via pcp? */
5256 free_unref_page(page);
5257 else
David Hildenbrand7fef4312020-10-15 20:09:35 -07005258 __free_pages_ok(page, order, FPI_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259}
5260
Matthew Wilcox (Oracle)7f194fb2020-12-14 19:11:09 -08005261/**
5262 * __free_pages - Free pages allocated with alloc_pages().
5263 * @page: The page pointer returned from alloc_pages().
5264 * @order: The order of the allocation.
5265 *
5266 * This function can free multi-page allocations that are not compound
5267 * pages. It does not check that the @order passed in matches that of
5268 * the allocation, so it is easy to leak memory. Freeing more memory
5269 * than was allocated will probably emit a warning.
5270 *
5271 * If the last reference to this page is speculative, it will be released
5272 * by put_page() which only frees the first page of a non-compound
5273 * allocation. To prevent the remaining pages from being leaked, we free
5274 * the subsequent pages here. If you want to use the page's reference
5275 * count to decide when to free the allocation, you should allocate a
5276 * compound page, and use put_page() instead of __free_pages().
5277 *
5278 * Context: May be called in interrupt context or while holding a normal
5279 * spinlock, but not in NMI context or while holding a raw spinlock.
5280 */
Aaron Lu742aa7f2018-12-28 00:35:22 -08005281void __free_pages(struct page *page, unsigned int order)
5282{
5283 if (put_page_testzero(page))
5284 free_the_page(page, order);
Matthew Wilcox (Oracle)e320d302020-10-13 16:56:04 -07005285 else if (!PageHead(page))
5286 while (order-- > 0)
5287 free_the_page(page + (1 << order), order);
Aaron Lu742aa7f2018-12-28 00:35:22 -08005288}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289EXPORT_SYMBOL(__free_pages);
5290
Harvey Harrison920c7a52008-02-04 22:29:26 -08005291void free_pages(unsigned long addr, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292{
5293 if (addr != 0) {
Nick Piggin725d7042006-09-25 23:30:55 -07005294 VM_BUG_ON(!virt_addr_valid((void *)addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 __free_pages(virt_to_page((void *)addr), order);
5296 }
5297}
5298
5299EXPORT_SYMBOL(free_pages);
5300
Glauber Costa6a1a0d32012-12-18 14:22:00 -08005301/*
Alexander Duyckb63ae8c2015-05-06 21:11:57 -07005302 * Page Fragment:
5303 * An arbitrary-length arbitrary-offset area of memory which resides
5304 * within a 0 or higher order page. Multiple fragments within that page
5305 * are individually refcounted, in the page's reference counter.
5306 *
5307 * The page_frag functions below provide a simple allocation framework for
5308 * page fragments. This is used by the network stack and network device
5309 * drivers to provide a backing region of memory for use as either an
5310 * sk_buff->head, or to be used in the "frags" portion of skb_shared_info.
5311 */
Alexander Duyck2976db82017-01-10 16:58:09 -08005312static struct page *__page_frag_cache_refill(struct page_frag_cache *nc,
5313 gfp_t gfp_mask)
Alexander Duyckb63ae8c2015-05-06 21:11:57 -07005314{
5315 struct page *page = NULL;
5316 gfp_t gfp = gfp_mask;
5317
5318#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
5319 gfp_mask |= __GFP_COMP | __GFP_NOWARN | __GFP_NORETRY |
5320 __GFP_NOMEMALLOC;
5321 page = alloc_pages_node(NUMA_NO_NODE, gfp_mask,
5322 PAGE_FRAG_CACHE_MAX_ORDER);
5323 nc->size = page ? PAGE_FRAG_CACHE_MAX_SIZE : PAGE_SIZE;
5324#endif
5325 if (unlikely(!page))
5326 page = alloc_pages_node(NUMA_NO_NODE, gfp, 0);
5327
5328 nc->va = page ? page_address(page) : NULL;
5329
5330 return page;
5331}
5332
Alexander Duyck2976db82017-01-10 16:58:09 -08005333void __page_frag_cache_drain(struct page *page, unsigned int count)
Alexander Duyck44fdffd2016-12-14 15:05:26 -08005334{
5335 VM_BUG_ON_PAGE(page_ref_count(page) == 0, page);
5336
Aaron Lu742aa7f2018-12-28 00:35:22 -08005337 if (page_ref_sub_and_test(page, count))
5338 free_the_page(page, compound_order(page));
Alexander Duyck44fdffd2016-12-14 15:05:26 -08005339}
Alexander Duyck2976db82017-01-10 16:58:09 -08005340EXPORT_SYMBOL(__page_frag_cache_drain);
Alexander Duyck44fdffd2016-12-14 15:05:26 -08005341
Kevin Haob358e212021-02-04 18:56:35 +08005342void *page_frag_alloc_align(struct page_frag_cache *nc,
5343 unsigned int fragsz, gfp_t gfp_mask,
5344 unsigned int align_mask)
Alexander Duyckb63ae8c2015-05-06 21:11:57 -07005345{
5346 unsigned int size = PAGE_SIZE;
5347 struct page *page;
5348 int offset;
5349
5350 if (unlikely(!nc->va)) {
5351refill:
Alexander Duyck2976db82017-01-10 16:58:09 -08005352 page = __page_frag_cache_refill(nc, gfp_mask);
Alexander Duyckb63ae8c2015-05-06 21:11:57 -07005353 if (!page)
5354 return NULL;
5355
5356#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
5357 /* if size can vary use size else just use PAGE_SIZE */
5358 size = nc->size;
5359#endif
5360 /* Even if we own the page, we do not use atomic_set().
5361 * This would break get_page_unless_zero() users.
5362 */
Alexander Duyck86447722019-02-15 14:44:12 -08005363 page_ref_add(page, PAGE_FRAG_CACHE_MAX_SIZE);
Alexander Duyckb63ae8c2015-05-06 21:11:57 -07005364
5365 /* reset page count bias and offset to start of new frag */
Michal Hocko2f064f32015-08-21 14:11:51 -07005366 nc->pfmemalloc = page_is_pfmemalloc(page);
Alexander Duyck86447722019-02-15 14:44:12 -08005367 nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1;
Alexander Duyckb63ae8c2015-05-06 21:11:57 -07005368 nc->offset = size;
5369 }
5370
5371 offset = nc->offset - fragsz;
5372 if (unlikely(offset < 0)) {
5373 page = virt_to_page(nc->va);
5374
Joonsoo Kimfe896d12016-03-17 14:19:26 -07005375 if (!page_ref_sub_and_test(page, nc->pagecnt_bias))
Alexander Duyckb63ae8c2015-05-06 21:11:57 -07005376 goto refill;
5377
Dongli Zhangd8c19012020-11-15 12:10:29 -08005378 if (unlikely(nc->pfmemalloc)) {
5379 free_the_page(page, compound_order(page));
5380 goto refill;
5381 }
5382
Alexander Duyckb63ae8c2015-05-06 21:11:57 -07005383#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
5384 /* if size can vary use size else just use PAGE_SIZE */
5385 size = nc->size;
5386#endif
5387 /* OK, page count is 0, we can safely set it */
Alexander Duyck86447722019-02-15 14:44:12 -08005388 set_page_count(page, PAGE_FRAG_CACHE_MAX_SIZE + 1);
Alexander Duyckb63ae8c2015-05-06 21:11:57 -07005389
5390 /* reset page count bias and offset to start of new frag */
Alexander Duyck86447722019-02-15 14:44:12 -08005391 nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1;
Alexander Duyckb63ae8c2015-05-06 21:11:57 -07005392 offset = size - fragsz;
5393 }
5394
5395 nc->pagecnt_bias--;
Kevin Haob358e212021-02-04 18:56:35 +08005396 offset &= align_mask;
Alexander Duyckb63ae8c2015-05-06 21:11:57 -07005397 nc->offset = offset;
5398
5399 return nc->va + offset;
5400}
Kevin Haob358e212021-02-04 18:56:35 +08005401EXPORT_SYMBOL(page_frag_alloc_align);
Alexander Duyckb63ae8c2015-05-06 21:11:57 -07005402
5403/*
5404 * Frees a page fragment allocated out of either a compound or order 0 page.
5405 */
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -08005406void page_frag_free(void *addr)
Alexander Duyckb63ae8c2015-05-06 21:11:57 -07005407{
5408 struct page *page = virt_to_head_page(addr);
5409
Aaron Lu742aa7f2018-12-28 00:35:22 -08005410 if (unlikely(put_page_testzero(page)))
5411 free_the_page(page, compound_order(page));
Alexander Duyckb63ae8c2015-05-06 21:11:57 -07005412}
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -08005413EXPORT_SYMBOL(page_frag_free);
Alexander Duyckb63ae8c2015-05-06 21:11:57 -07005414
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08005415static void *make_alloc_exact(unsigned long addr, unsigned int order,
5416 size_t size)
Andi Kleenee85c2e2011-05-11 15:13:34 -07005417{
5418 if (addr) {
5419 unsigned long alloc_end = addr + (PAGE_SIZE << order);
5420 unsigned long used = addr + PAGE_ALIGN(size);
5421
5422 split_page(virt_to_page((void *)addr), order);
5423 while (used < alloc_end) {
5424 free_page(used);
5425 used += PAGE_SIZE;
5426 }
5427 }
5428 return (void *)addr;
5429}
5430
Timur Tabi2be0ffe2008-07-23 21:28:11 -07005431/**
5432 * alloc_pages_exact - allocate an exact number physically-contiguous pages.
5433 * @size: the number of bytes to allocate
Vlastimil Babka63931eb2019-05-13 17:16:47 -07005434 * @gfp_mask: GFP flags for the allocation, must not contain __GFP_COMP
Timur Tabi2be0ffe2008-07-23 21:28:11 -07005435 *
5436 * This function is similar to alloc_pages(), except that it allocates the
5437 * minimum number of pages to satisfy the request. alloc_pages() can only
5438 * allocate memory in power-of-two pages.
5439 *
5440 * This function is also limited by MAX_ORDER.
5441 *
5442 * Memory allocated by this function must be released by free_pages_exact().
Mike Rapoporta862f682019-03-05 15:48:42 -08005443 *
5444 * Return: pointer to the allocated area or %NULL in case of error.
Timur Tabi2be0ffe2008-07-23 21:28:11 -07005445 */
5446void *alloc_pages_exact(size_t size, gfp_t gfp_mask)
5447{
5448 unsigned int order = get_order(size);
5449 unsigned long addr;
5450
Vlastimil Babka63931eb2019-05-13 17:16:47 -07005451 if (WARN_ON_ONCE(gfp_mask & __GFP_COMP))
5452 gfp_mask &= ~__GFP_COMP;
5453
Timur Tabi2be0ffe2008-07-23 21:28:11 -07005454 addr = __get_free_pages(gfp_mask, order);
Andi Kleenee85c2e2011-05-11 15:13:34 -07005455 return make_alloc_exact(addr, order, size);
Timur Tabi2be0ffe2008-07-23 21:28:11 -07005456}
5457EXPORT_SYMBOL(alloc_pages_exact);
5458
5459/**
Andi Kleenee85c2e2011-05-11 15:13:34 -07005460 * alloc_pages_exact_nid - allocate an exact number of physically-contiguous
5461 * pages on a node.
Randy Dunlapb5e6ab52011-05-16 13:16:54 -07005462 * @nid: the preferred node ID where memory should be allocated
Andi Kleenee85c2e2011-05-11 15:13:34 -07005463 * @size: the number of bytes to allocate
Vlastimil Babka63931eb2019-05-13 17:16:47 -07005464 * @gfp_mask: GFP flags for the allocation, must not contain __GFP_COMP
Andi Kleenee85c2e2011-05-11 15:13:34 -07005465 *
5466 * Like alloc_pages_exact(), but try to allocate on node nid first before falling
5467 * back.
Mike Rapoporta862f682019-03-05 15:48:42 -08005468 *
5469 * Return: pointer to the allocated area or %NULL in case of error.
Andi Kleenee85c2e2011-05-11 15:13:34 -07005470 */
Fabian Fredericke1931812014-08-06 16:04:59 -07005471void * __meminit alloc_pages_exact_nid(int nid, size_t size, gfp_t gfp_mask)
Andi Kleenee85c2e2011-05-11 15:13:34 -07005472{
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08005473 unsigned int order = get_order(size);
Vlastimil Babka63931eb2019-05-13 17:16:47 -07005474 struct page *p;
5475
5476 if (WARN_ON_ONCE(gfp_mask & __GFP_COMP))
5477 gfp_mask &= ~__GFP_COMP;
5478
5479 p = alloc_pages_node(nid, gfp_mask, order);
Andi Kleenee85c2e2011-05-11 15:13:34 -07005480 if (!p)
5481 return NULL;
5482 return make_alloc_exact((unsigned long)page_address(p), order, size);
5483}
Andi Kleenee85c2e2011-05-11 15:13:34 -07005484
5485/**
Timur Tabi2be0ffe2008-07-23 21:28:11 -07005486 * free_pages_exact - release memory allocated via alloc_pages_exact()
5487 * @virt: the value returned by alloc_pages_exact.
5488 * @size: size of allocation, same value as passed to alloc_pages_exact().
5489 *
5490 * Release the memory allocated by a previous call to alloc_pages_exact.
5491 */
5492void free_pages_exact(void *virt, size_t size)
5493{
5494 unsigned long addr = (unsigned long)virt;
5495 unsigned long end = addr + PAGE_ALIGN(size);
5496
5497 while (addr < end) {
5498 free_page(addr);
5499 addr += PAGE_SIZE;
5500 }
5501}
5502EXPORT_SYMBOL(free_pages_exact);
5503
Zhang Yanfeie0fb5812013-02-22 16:35:54 -08005504/**
5505 * nr_free_zone_pages - count number of pages beyond high watermark
5506 * @offset: The zone index of the highest zone
5507 *
Mike Rapoporta862f682019-03-05 15:48:42 -08005508 * nr_free_zone_pages() counts the number of pages which are beyond the
Zhang Yanfeie0fb5812013-02-22 16:35:54 -08005509 * high watermark within all zones at or below a given zone index. For each
5510 * zone, the number of pages is calculated as:
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -03005511 *
5512 * nr_free_zone_pages = managed_pages - high_pages
Mike Rapoporta862f682019-03-05 15:48:42 -08005513 *
5514 * Return: number of pages beyond high watermark.
Zhang Yanfeie0fb5812013-02-22 16:35:54 -08005515 */
Zhang Yanfeiebec3862013-02-22 16:35:43 -08005516static unsigned long nr_free_zone_pages(int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517{
Mel Gormandd1a2392008-04-28 02:12:17 -07005518 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07005519 struct zone *zone;
5520
Martin J. Blighe310fd42005-07-29 22:59:18 -07005521 /* Just pick one node, since fallback list is circular */
Zhang Yanfeiebec3862013-02-22 16:35:43 -08005522 unsigned long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523
Mel Gorman0e884602008-04-28 02:12:14 -07005524 struct zonelist *zonelist = node_zonelist(numa_node_id(), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525
Mel Gorman54a6eb52008-04-28 02:12:16 -07005526 for_each_zone_zonelist(zone, z, zonelist, offset) {
Arun KS9705bea2018-12-28 00:34:24 -08005527 unsigned long size = zone_managed_pages(zone);
Mel Gorman41858962009-06-16 15:32:12 -07005528 unsigned long high = high_wmark_pages(zone);
Martin J. Blighe310fd42005-07-29 22:59:18 -07005529 if (size > high)
5530 sum += size - high;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 }
5532
5533 return sum;
5534}
5535
Zhang Yanfeie0fb5812013-02-22 16:35:54 -08005536/**
5537 * nr_free_buffer_pages - count number of pages beyond high watermark
5538 *
5539 * nr_free_buffer_pages() counts the number of pages which are beyond the high
5540 * watermark within ZONE_DMA and ZONE_NORMAL.
Mike Rapoporta862f682019-03-05 15:48:42 -08005541 *
5542 * Return: number of pages beyond high watermark within ZONE_DMA and
5543 * ZONE_NORMAL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 */
Zhang Yanfeiebec3862013-02-22 16:35:43 -08005545unsigned long nr_free_buffer_pages(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546{
Al Viroaf4ca452005-10-21 02:55:38 -04005547 return nr_free_zone_pages(gfp_zone(GFP_USER));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548}
Meelap Shahc2f1a552007-07-17 04:04:39 -07005549EXPORT_SYMBOL_GPL(nr_free_buffer_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550
Christoph Lameter08e0f6a2006-09-27 01:50:06 -07005551static inline void show_node(struct zone *zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552{
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08005553 if (IS_ENABLED(CONFIG_NUMA))
Andy Whitcroft25ba77c2006-12-06 20:33:03 -08005554 printk("Node %d ", zone_to_nid(zone));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556
Igor Redkod02bd272016-03-17 14:19:05 -07005557long si_mem_available(void)
5558{
5559 long available;
5560 unsigned long pagecache;
5561 unsigned long wmark_low = 0;
5562 unsigned long pages[NR_LRU_LISTS];
Vlastimil Babkab29940c2018-10-26 15:05:46 -07005563 unsigned long reclaimable;
Igor Redkod02bd272016-03-17 14:19:05 -07005564 struct zone *zone;
5565 int lru;
5566
5567 for (lru = LRU_BASE; lru < NR_LRU_LISTS; lru++)
Mel Gorman2f95ff92016-08-11 15:32:57 -07005568 pages[lru] = global_node_page_state(NR_LRU_BASE + lru);
Igor Redkod02bd272016-03-17 14:19:05 -07005569
5570 for_each_zone(zone)
Mel Gormana9214442018-12-28 00:35:44 -08005571 wmark_low += low_wmark_pages(zone);
Igor Redkod02bd272016-03-17 14:19:05 -07005572
5573 /*
5574 * Estimate the amount of memory available for userspace allocations,
5575 * without causing swapping.
5576 */
Michal Hockoc41f0122017-09-06 16:23:36 -07005577 available = global_zone_page_state(NR_FREE_PAGES) - totalreserve_pages;
Igor Redkod02bd272016-03-17 14:19:05 -07005578
5579 /*
5580 * Not all the page cache can be freed, otherwise the system will
5581 * start swapping. Assume at least half of the page cache, or the
5582 * low watermark worth of cache, needs to stay.
5583 */
5584 pagecache = pages[LRU_ACTIVE_FILE] + pages[LRU_INACTIVE_FILE];
5585 pagecache -= min(pagecache / 2, wmark_low);
5586 available += pagecache;
5587
5588 /*
Vlastimil Babkab29940c2018-10-26 15:05:46 -07005589 * Part of the reclaimable slab and other kernel memory consists of
5590 * items that are in use, and cannot be freed. Cap this estimate at the
5591 * low watermark.
Igor Redkod02bd272016-03-17 14:19:05 -07005592 */
Roman Gushchind42f3242020-08-06 23:20:39 -07005593 reclaimable = global_node_page_state_pages(NR_SLAB_RECLAIMABLE_B) +
5594 global_node_page_state(NR_KERNEL_MISC_RECLAIMABLE);
Vlastimil Babkab29940c2018-10-26 15:05:46 -07005595 available += reclaimable - min(reclaimable / 2, wmark_low);
Roman Gushchin034ebf62018-04-10 16:27:40 -07005596
Igor Redkod02bd272016-03-17 14:19:05 -07005597 if (available < 0)
5598 available = 0;
5599 return available;
5600}
5601EXPORT_SYMBOL_GPL(si_mem_available);
5602
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603void si_meminfo(struct sysinfo *val)
5604{
Arun KSca79b0c2018-12-28 00:34:29 -08005605 val->totalram = totalram_pages();
Mel Gorman11fb9982016-07-28 15:46:20 -07005606 val->sharedram = global_node_page_state(NR_SHMEM);
Michal Hockoc41f0122017-09-06 16:23:36 -07005607 val->freeram = global_zone_page_state(NR_FREE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 val->bufferram = nr_blockdev_pages();
Arun KSca79b0c2018-12-28 00:34:29 -08005609 val->totalhigh = totalhigh_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 val->freehigh = nr_free_highpages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611 val->mem_unit = PAGE_SIZE;
5612}
5613
5614EXPORT_SYMBOL(si_meminfo);
5615
5616#ifdef CONFIG_NUMA
5617void si_meminfo_node(struct sysinfo *val, int nid)
5618{
Jiang Liucdd91a72013-07-03 15:03:27 -07005619 int zone_type; /* needs to be signed */
5620 unsigned long managed_pages = 0;
Joonsoo Kimfc2bd792016-05-19 17:12:23 -07005621 unsigned long managed_highpages = 0;
5622 unsigned long free_highpages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 pg_data_t *pgdat = NODE_DATA(nid);
5624
Jiang Liucdd91a72013-07-03 15:03:27 -07005625 for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++)
Arun KS9705bea2018-12-28 00:34:24 -08005626 managed_pages += zone_managed_pages(&pgdat->node_zones[zone_type]);
Jiang Liucdd91a72013-07-03 15:03:27 -07005627 val->totalram = managed_pages;
Mel Gorman11fb9982016-07-28 15:46:20 -07005628 val->sharedram = node_page_state(pgdat, NR_SHMEM);
Mel Gorman75ef7182016-07-28 15:45:24 -07005629 val->freeram = sum_zone_node_page_state(nid, NR_FREE_PAGES);
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07005630#ifdef CONFIG_HIGHMEM
Joonsoo Kimfc2bd792016-05-19 17:12:23 -07005631 for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
5632 struct zone *zone = &pgdat->node_zones[zone_type];
5633
5634 if (is_highmem(zone)) {
Arun KS9705bea2018-12-28 00:34:24 -08005635 managed_highpages += zone_managed_pages(zone);
Joonsoo Kimfc2bd792016-05-19 17:12:23 -07005636 free_highpages += zone_page_state(zone, NR_FREE_PAGES);
5637 }
5638 }
5639 val->totalhigh = managed_highpages;
5640 val->freehigh = free_highpages;
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07005641#else
Joonsoo Kimfc2bd792016-05-19 17:12:23 -07005642 val->totalhigh = managed_highpages;
5643 val->freehigh = free_highpages;
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07005644#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 val->mem_unit = PAGE_SIZE;
5646}
5647#endif
5648
David Rientjesddd588b2011-03-22 16:30:46 -07005649/*
David Rientjes7bf02ea2011-05-24 17:11:16 -07005650 * Determine whether the node should be displayed or not, depending on whether
5651 * SHOW_MEM_FILTER_NODES was passed to show_free_areas().
David Rientjesddd588b2011-03-22 16:30:46 -07005652 */
Michal Hocko9af744d2017-02-22 15:46:16 -08005653static bool show_mem_node_skip(unsigned int flags, int nid, nodemask_t *nodemask)
David Rientjesddd588b2011-03-22 16:30:46 -07005654{
David Rientjesddd588b2011-03-22 16:30:46 -07005655 if (!(flags & SHOW_MEM_FILTER_NODES))
Michal Hocko9af744d2017-02-22 15:46:16 -08005656 return false;
David Rientjesddd588b2011-03-22 16:30:46 -07005657
Michal Hocko9af744d2017-02-22 15:46:16 -08005658 /*
5659 * no node mask - aka implicit memory numa policy. Do not bother with
5660 * the synchronization - read_mems_allowed_begin - because we do not
5661 * have to be precise here.
5662 */
5663 if (!nodemask)
5664 nodemask = &cpuset_current_mems_allowed;
5665
5666 return !node_isset(nid, *nodemask);
David Rientjesddd588b2011-03-22 16:30:46 -07005667}
5668
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669#define K(x) ((x) << (PAGE_SHIFT-10))
5670
Rabin Vincent377e4f12012-12-11 16:00:24 -08005671static void show_migration_types(unsigned char type)
5672{
5673 static const char types[MIGRATE_TYPES] = {
5674 [MIGRATE_UNMOVABLE] = 'U',
Rabin Vincent377e4f12012-12-11 16:00:24 -08005675 [MIGRATE_MOVABLE] = 'M',
Vlastimil Babka475a2f92015-12-11 13:40:29 -08005676 [MIGRATE_RECLAIMABLE] = 'E',
5677 [MIGRATE_HIGHATOMIC] = 'H',
Rabin Vincent377e4f12012-12-11 16:00:24 -08005678#ifdef CONFIG_CMA
5679 [MIGRATE_CMA] = 'C',
5680#endif
Minchan Kim194159f2013-02-22 16:33:58 -08005681#ifdef CONFIG_MEMORY_ISOLATION
Rabin Vincent377e4f12012-12-11 16:00:24 -08005682 [MIGRATE_ISOLATE] = 'I',
Minchan Kim194159f2013-02-22 16:33:58 -08005683#endif
Rabin Vincent377e4f12012-12-11 16:00:24 -08005684 };
5685 char tmp[MIGRATE_TYPES + 1];
5686 char *p = tmp;
5687 int i;
5688
5689 for (i = 0; i < MIGRATE_TYPES; i++) {
5690 if (type & (1 << i))
5691 *p++ = types[i];
5692 }
5693
5694 *p = '\0';
Joe Perches1f84a182016-10-27 17:46:29 -07005695 printk(KERN_CONT "(%s) ", tmp);
Rabin Vincent377e4f12012-12-11 16:00:24 -08005696}
5697
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698/*
5699 * Show free area list (used inside shift_scroll-lock stuff)
5700 * We also calculate the percentage fragmentation. We do this by counting the
5701 * memory on each free list with the exception of the first item on the list.
Konstantin Khlebnikovd1bfcdb2015-04-14 15:45:30 -07005702 *
5703 * Bits in @filter:
5704 * SHOW_MEM_FILTER_NODES: suppress nodes that are not allowed by current's
5705 * cpuset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 */
Michal Hocko9af744d2017-02-22 15:46:16 -08005707void show_free_areas(unsigned int filter, nodemask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708{
Konstantin Khlebnikovd1bfcdb2015-04-14 15:45:30 -07005709 unsigned long free_pcp = 0;
Jes Sorensenc7241912006-09-27 01:50:05 -07005710 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 struct zone *zone;
Mel Gorman599d0c92016-07-28 15:45:31 -07005712 pg_data_t *pgdat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713
KOSAKI Motohiroee99c712009-03-31 15:19:31 -07005714 for_each_populated_zone(zone) {
Michal Hocko9af744d2017-02-22 15:46:16 -08005715 if (show_mem_node_skip(filter, zone_to_nid(zone), nodemask))
David Rientjesddd588b2011-03-22 16:30:46 -07005716 continue;
Konstantin Khlebnikovd1bfcdb2015-04-14 15:45:30 -07005717
Konstantin Khlebnikov761b0672015-04-14 15:45:32 -07005718 for_each_online_cpu(cpu)
5719 free_pcp += per_cpu_ptr(zone->pageset, cpu)->pcp.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 }
5721
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07005722 printk("active_anon:%lu inactive_anon:%lu isolated_anon:%lu\n"
5723 " active_file:%lu inactive_file:%lu isolated_file:%lu\n"
NeilBrown8d928902020-06-01 21:48:21 -07005724 " unevictable:%lu dirty:%lu writeback:%lu\n"
Konstantin Khlebnikovd1bfcdb2015-04-14 15:45:30 -07005725 " slab_reclaimable:%lu slab_unreclaimable:%lu\n"
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -07005726 " mapped:%lu shmem:%lu pagetables:%lu bounce:%lu\n"
Konstantin Khlebnikovd1bfcdb2015-04-14 15:45:30 -07005727 " free:%lu free_pcp:%lu free_cma:%lu\n",
Mel Gorman599d0c92016-07-28 15:45:31 -07005728 global_node_page_state(NR_ACTIVE_ANON),
5729 global_node_page_state(NR_INACTIVE_ANON),
5730 global_node_page_state(NR_ISOLATED_ANON),
5731 global_node_page_state(NR_ACTIVE_FILE),
5732 global_node_page_state(NR_INACTIVE_FILE),
5733 global_node_page_state(NR_ISOLATED_FILE),
5734 global_node_page_state(NR_UNEVICTABLE),
Mel Gorman11fb9982016-07-28 15:46:20 -07005735 global_node_page_state(NR_FILE_DIRTY),
5736 global_node_page_state(NR_WRITEBACK),
Roman Gushchind42f3242020-08-06 23:20:39 -07005737 global_node_page_state_pages(NR_SLAB_RECLAIMABLE_B),
5738 global_node_page_state_pages(NR_SLAB_UNRECLAIMABLE_B),
Mel Gorman50658e22016-07-28 15:46:14 -07005739 global_node_page_state(NR_FILE_MAPPED),
Mel Gorman11fb9982016-07-28 15:46:20 -07005740 global_node_page_state(NR_SHMEM),
Shakeel Buttf0c0c112020-12-14 19:07:17 -08005741 global_node_page_state(NR_PAGETABLE),
Michal Hockoc41f0122017-09-06 16:23:36 -07005742 global_zone_page_state(NR_BOUNCE),
5743 global_zone_page_state(NR_FREE_PAGES),
Konstantin Khlebnikovd1bfcdb2015-04-14 15:45:30 -07005744 free_pcp,
Michal Hockoc41f0122017-09-06 16:23:36 -07005745 global_zone_page_state(NR_FREE_CMA_PAGES));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746
Mel Gorman599d0c92016-07-28 15:45:31 -07005747 for_each_online_pgdat(pgdat) {
Michal Hocko9af744d2017-02-22 15:46:16 -08005748 if (show_mem_node_skip(filter, pgdat->node_id, nodemask))
Michal Hockoc02e50b2017-02-22 15:46:07 -08005749 continue;
5750
Mel Gorman599d0c92016-07-28 15:45:31 -07005751 printk("Node %d"
5752 " active_anon:%lukB"
5753 " inactive_anon:%lukB"
5754 " active_file:%lukB"
5755 " inactive_file:%lukB"
5756 " unevictable:%lukB"
5757 " isolated(anon):%lukB"
5758 " isolated(file):%lukB"
Mel Gorman50658e22016-07-28 15:46:14 -07005759 " mapped:%lukB"
Mel Gorman11fb9982016-07-28 15:46:20 -07005760 " dirty:%lukB"
5761 " writeback:%lukB"
5762 " shmem:%lukB"
5763#ifdef CONFIG_TRANSPARENT_HUGEPAGE
5764 " shmem_thp: %lukB"
5765 " shmem_pmdmapped: %lukB"
5766 " anon_thp: %lukB"
5767#endif
5768 " writeback_tmp:%lukB"
Shakeel Butt991e7672020-08-06 23:21:37 -07005769 " kernel_stack:%lukB"
5770#ifdef CONFIG_SHADOW_CALL_STACK
5771 " shadow_call_stack:%lukB"
5772#endif
Shakeel Buttf0c0c112020-12-14 19:07:17 -08005773 " pagetables:%lukB"
Mel Gorman599d0c92016-07-28 15:45:31 -07005774 " all_unreclaimable? %s"
5775 "\n",
5776 pgdat->node_id,
5777 K(node_page_state(pgdat, NR_ACTIVE_ANON)),
5778 K(node_page_state(pgdat, NR_INACTIVE_ANON)),
5779 K(node_page_state(pgdat, NR_ACTIVE_FILE)),
5780 K(node_page_state(pgdat, NR_INACTIVE_FILE)),
5781 K(node_page_state(pgdat, NR_UNEVICTABLE)),
5782 K(node_page_state(pgdat, NR_ISOLATED_ANON)),
5783 K(node_page_state(pgdat, NR_ISOLATED_FILE)),
Mel Gorman50658e22016-07-28 15:46:14 -07005784 K(node_page_state(pgdat, NR_FILE_MAPPED)),
Mel Gorman11fb9982016-07-28 15:46:20 -07005785 K(node_page_state(pgdat, NR_FILE_DIRTY)),
5786 K(node_page_state(pgdat, NR_WRITEBACK)),
Alexander Polakov1f06b812017-04-07 16:04:45 -07005787 K(node_page_state(pgdat, NR_SHMEM)),
Mel Gorman11fb9982016-07-28 15:46:20 -07005788#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Muchun Song57b28472021-02-24 12:03:31 -08005789 K(node_page_state(pgdat, NR_SHMEM_THPS)),
Muchun Songa1528e22021-02-24 12:03:35 -08005790 K(node_page_state(pgdat, NR_SHMEM_PMDMAPPED)),
Muchun Song69473e52021-02-24 12:03:23 -08005791 K(node_page_state(pgdat, NR_ANON_THPS)),
Mel Gorman11fb9982016-07-28 15:46:20 -07005792#endif
Mel Gorman11fb9982016-07-28 15:46:20 -07005793 K(node_page_state(pgdat, NR_WRITEBACK_TEMP)),
Shakeel Butt991e7672020-08-06 23:21:37 -07005794 node_page_state(pgdat, NR_KERNEL_STACK_KB),
5795#ifdef CONFIG_SHADOW_CALL_STACK
5796 node_page_state(pgdat, NR_KERNEL_SCS_KB),
5797#endif
Shakeel Buttf0c0c112020-12-14 19:07:17 -08005798 K(node_page_state(pgdat, NR_PAGETABLE)),
Johannes Weinerc73322d2017-05-03 14:51:51 -07005799 pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES ?
5800 "yes" : "no");
Mel Gorman599d0c92016-07-28 15:45:31 -07005801 }
5802
KOSAKI Motohiroee99c712009-03-31 15:19:31 -07005803 for_each_populated_zone(zone) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 int i;
5805
Michal Hocko9af744d2017-02-22 15:46:16 -08005806 if (show_mem_node_skip(filter, zone_to_nid(zone), nodemask))
David Rientjesddd588b2011-03-22 16:30:46 -07005807 continue;
Konstantin Khlebnikovd1bfcdb2015-04-14 15:45:30 -07005808
5809 free_pcp = 0;
5810 for_each_online_cpu(cpu)
5811 free_pcp += per_cpu_ptr(zone->pageset, cpu)->pcp.count;
5812
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 show_node(zone);
Joe Perches1f84a182016-10-27 17:46:29 -07005814 printk(KERN_CONT
5815 "%s"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816 " free:%lukB"
5817 " min:%lukB"
5818 " low:%lukB"
5819 " high:%lukB"
lijiazie47b3462019-11-30 17:55:21 -08005820 " reserved_highatomic:%luKB"
Minchan Kim71c799f2016-07-28 15:47:26 -07005821 " active_anon:%lukB"
5822 " inactive_anon:%lukB"
5823 " active_file:%lukB"
5824 " inactive_file:%lukB"
5825 " unevictable:%lukB"
Mel Gorman5a1c84b2016-07-28 15:47:31 -07005826 " writepending:%lukB"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827 " present:%lukB"
Jiang Liu9feedc92012-12-12 13:52:12 -08005828 " managed:%lukB"
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07005829 " mlocked:%lukB"
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07005830 " bounce:%lukB"
Konstantin Khlebnikovd1bfcdb2015-04-14 15:45:30 -07005831 " free_pcp:%lukB"
5832 " local_pcp:%ukB"
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -07005833 " free_cma:%lukB"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834 "\n",
5835 zone->name,
Mel Gorman88f5acf2011-01-13 15:45:41 -08005836 K(zone_page_state(zone, NR_FREE_PAGES)),
Mel Gorman41858962009-06-16 15:32:12 -07005837 K(min_wmark_pages(zone)),
5838 K(low_wmark_pages(zone)),
5839 K(high_wmark_pages(zone)),
lijiazie47b3462019-11-30 17:55:21 -08005840 K(zone->nr_reserved_highatomic),
Minchan Kim71c799f2016-07-28 15:47:26 -07005841 K(zone_page_state(zone, NR_ZONE_ACTIVE_ANON)),
5842 K(zone_page_state(zone, NR_ZONE_INACTIVE_ANON)),
5843 K(zone_page_state(zone, NR_ZONE_ACTIVE_FILE)),
5844 K(zone_page_state(zone, NR_ZONE_INACTIVE_FILE)),
5845 K(zone_page_state(zone, NR_ZONE_UNEVICTABLE)),
Mel Gorman5a1c84b2016-07-28 15:47:31 -07005846 K(zone_page_state(zone, NR_ZONE_WRITE_PENDING)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 K(zone->present_pages),
Arun KS9705bea2018-12-28 00:34:24 -08005848 K(zone_managed_pages(zone)),
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07005849 K(zone_page_state(zone, NR_MLOCK)),
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07005850 K(zone_page_state(zone, NR_BOUNCE)),
Konstantin Khlebnikovd1bfcdb2015-04-14 15:45:30 -07005851 K(free_pcp),
5852 K(this_cpu_read(zone->pageset->pcp.count)),
Minchan Kim33e077b2016-07-28 15:47:14 -07005853 K(zone_page_state(zone, NR_FREE_CMA_PAGES)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 printk("lowmem_reserve[]:");
5855 for (i = 0; i < MAX_NR_ZONES; i++)
Joe Perches1f84a182016-10-27 17:46:29 -07005856 printk(KERN_CONT " %ld", zone->lowmem_reserve[i]);
5857 printk(KERN_CONT "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858 }
5859
KOSAKI Motohiroee99c712009-03-31 15:19:31 -07005860 for_each_populated_zone(zone) {
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08005861 unsigned int order;
5862 unsigned long nr[MAX_ORDER], flags, total = 0;
Rabin Vincent377e4f12012-12-11 16:00:24 -08005863 unsigned char types[MAX_ORDER];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864
Michal Hocko9af744d2017-02-22 15:46:16 -08005865 if (show_mem_node_skip(filter, zone_to_nid(zone), nodemask))
David Rientjesddd588b2011-03-22 16:30:46 -07005866 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867 show_node(zone);
Joe Perches1f84a182016-10-27 17:46:29 -07005868 printk(KERN_CONT "%s: ", zone->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869
5870 spin_lock_irqsave(&zone->lock, flags);
5871 for (order = 0; order < MAX_ORDER; order++) {
Rabin Vincent377e4f12012-12-11 16:00:24 -08005872 struct free_area *area = &zone->free_area[order];
5873 int type;
5874
5875 nr[order] = area->nr_free;
Kirill Korotaev8f9de512006-06-23 02:03:50 -07005876 total += nr[order] << order;
Rabin Vincent377e4f12012-12-11 16:00:24 -08005877
5878 types[order] = 0;
5879 for (type = 0; type < MIGRATE_TYPES; type++) {
Dan Williamsb03641a2019-05-14 15:41:32 -07005880 if (!free_area_empty(area, type))
Rabin Vincent377e4f12012-12-11 16:00:24 -08005881 types[order] |= 1 << type;
5882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883 }
5884 spin_unlock_irqrestore(&zone->lock, flags);
Rabin Vincent377e4f12012-12-11 16:00:24 -08005885 for (order = 0; order < MAX_ORDER; order++) {
Joe Perches1f84a182016-10-27 17:46:29 -07005886 printk(KERN_CONT "%lu*%lukB ",
5887 nr[order], K(1UL) << order);
Rabin Vincent377e4f12012-12-11 16:00:24 -08005888 if (nr[order])
5889 show_migration_types(types[order]);
5890 }
Joe Perches1f84a182016-10-27 17:46:29 -07005891 printk(KERN_CONT "= %lukB\n", K(total));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 }
5893
David Rientjes949f7ec2013-04-29 15:07:48 -07005894 hugetlb_show_meminfo();
5895
Mel Gorman11fb9982016-07-28 15:46:20 -07005896 printk("%ld total pagecache pages\n", global_node_page_state(NR_FILE_PAGES));
Larry Woodmane6f36022008-02-04 22:29:30 -08005897
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 show_swap_cache_info();
5899}
5900
Mel Gorman19770b32008-04-28 02:12:18 -07005901static void zoneref_set_zone(struct zone *zone, struct zoneref *zoneref)
5902{
5903 zoneref->zone = zone;
5904 zoneref->zone_idx = zone_idx(zone);
5905}
5906
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907/*
5908 * Builds allocation fallback zone lists.
Christoph Lameter1a932052006-01-06 00:11:16 -08005909 *
5910 * Add all populated zones of a node to the zonelist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 */
Michal Hocko9d3be212017-09-06 16:20:30 -07005912static int build_zonerefs_node(pg_data_t *pgdat, struct zoneref *zonerefs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913{
Christoph Lameter1a932052006-01-06 00:11:16 -08005914 struct zone *zone;
Zhang Yanfeibc732f12013-07-08 16:00:06 -07005915 enum zone_type zone_type = MAX_NR_ZONES;
Michal Hocko9d3be212017-09-06 16:20:30 -07005916 int nr_zones = 0;
Christoph Lameter02a68a52006-01-06 00:11:18 -08005917
5918 do {
Christoph Lameter2f6726e2006-09-25 23:31:18 -07005919 zone_type--;
Christoph Lameter070f8032006-01-06 00:11:19 -08005920 zone = pgdat->node_zones + zone_type;
Mel Gorman6aa303d2016-09-01 16:14:55 -07005921 if (managed_zone(zone)) {
Michal Hocko9d3be212017-09-06 16:20:30 -07005922 zoneref_set_zone(zone, &zonerefs[nr_zones++]);
Christoph Lameter070f8032006-01-06 00:11:19 -08005923 check_highest_zone(zone_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924 }
Christoph Lameter2f6726e2006-09-25 23:31:18 -07005925 } while (zone_type);
Zhang Yanfeibc732f12013-07-08 16:00:06 -07005926
Christoph Lameter070f8032006-01-06 00:11:19 -08005927 return nr_zones;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928}
5929
5930#ifdef CONFIG_NUMA
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005931
5932static int __parse_numa_zonelist_order(char *s)
5933{
Michal Hockoc9bff3e2017-09-06 16:20:13 -07005934 /*
Ingo Molnarf0953a12021-05-06 18:06:47 -07005935 * We used to support different zonelists modes but they turned
Michal Hockoc9bff3e2017-09-06 16:20:13 -07005936 * out to be just not useful. Let's keep the warning in place
5937 * if somebody still use the cmd line parameter so that we do
5938 * not fail it silently
5939 */
5940 if (!(*s == 'd' || *s == 'D' || *s == 'n' || *s == 'N')) {
5941 pr_warn("Ignoring unsupported numa_zonelist_order value: %s\n", s);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005942 return -EINVAL;
5943 }
5944 return 0;
5945}
5946
Michal Hockoc9bff3e2017-09-06 16:20:13 -07005947char numa_zonelist_order[] = "Node";
5948
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005949/*
5950 * sysctl handler for numa_zonelist_order
5951 */
Joe Perchescccad5b2014-06-06 14:38:09 -07005952int numa_zonelist_order_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02005953 void *buffer, size_t *length, loff_t *ppos)
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005954{
Christoph Hellwig32927392020-04-24 08:43:38 +02005955 if (write)
5956 return __parse_numa_zonelist_order(buffer);
5957 return proc_dostring(table, write, buffer, length, ppos);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005958}
5959
5960
Christoph Lameter62bc62a2009-06-16 15:32:15 -07005961#define MAX_NODE_LOAD (nr_online_nodes)
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005962static int node_load[MAX_NUMNODES];
5963
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964/**
Pavel Pisa4dc3b162005-05-01 08:59:25 -07005965 * find_next_best_node - find the next node that should appear in a given node's fallback list
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 * @node: node whose fallback list we're appending
5967 * @used_node_mask: nodemask_t of already used nodes
5968 *
5969 * We use a number of factors to determine which is the next node that should
5970 * appear on a given node's fallback list. The node should not have appeared
5971 * already in @node's fallback list, and it should be the next closest node
5972 * according to the distance array (which contains arbitrary distance values
5973 * from each node to each node in the system), and should also prefer nodes
5974 * with no CPUs, since presumably they'll have very little allocation pressure
5975 * on them otherwise.
Mike Rapoporta862f682019-03-05 15:48:42 -08005976 *
5977 * Return: node id of the found node or %NUMA_NO_NODE if no node is found.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978 */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005979static int find_next_best_node(int node, nodemask_t *used_node_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980{
Linus Torvalds4cf808eb2006-02-17 20:38:21 +01005981 int n, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 int min_val = INT_MAX;
David Rientjes00ef2d22013-02-22 16:35:36 -08005983 int best_node = NUMA_NO_NODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984
Linus Torvalds4cf808eb2006-02-17 20:38:21 +01005985 /* Use the local node if we haven't already */
5986 if (!node_isset(node, *used_node_mask)) {
5987 node_set(node, *used_node_mask);
5988 return node;
5989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08005991 for_each_node_state(n, N_MEMORY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992
5993 /* Don't want a node to appear more than once */
5994 if (node_isset(n, *used_node_mask))
5995 continue;
5996
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 /* Use the distance array to find the distance */
5998 val = node_distance(node, n);
5999
Linus Torvalds4cf808eb2006-02-17 20:38:21 +01006000 /* Penalize nodes under us ("prefer the next node") */
6001 val += (n < node);
6002
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003 /* Give preference to headless and unused nodes */
Mateusz Nosekb6307492020-10-13 16:55:42 -07006004 if (!cpumask_empty(cpumask_of_node(n)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005 val += PENALTY_FOR_NODE_WITH_CPUS;
6006
6007 /* Slight preference for less loaded node */
6008 val *= (MAX_NODE_LOAD*MAX_NUMNODES);
6009 val += node_load[n];
6010
6011 if (val < min_val) {
6012 min_val = val;
6013 best_node = n;
6014 }
6015 }
6016
6017 if (best_node >= 0)
6018 node_set(best_node, *used_node_mask);
6019
6020 return best_node;
6021}
6022
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07006023
6024/*
6025 * Build zonelists ordered by node and zones within node.
6026 * This results in maximum locality--normal zone overflows into local
6027 * DMA zone, if any--but risks exhausting DMA zone.
6028 */
Michal Hocko9d3be212017-09-06 16:20:30 -07006029static void build_zonelists_in_node_order(pg_data_t *pgdat, int *node_order,
6030 unsigned nr_nodes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031{
Michal Hocko9d3be212017-09-06 16:20:30 -07006032 struct zoneref *zonerefs;
6033 int i;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07006034
Michal Hocko9d3be212017-09-06 16:20:30 -07006035 zonerefs = pgdat->node_zonelists[ZONELIST_FALLBACK]._zonerefs;
6036
6037 for (i = 0; i < nr_nodes; i++) {
6038 int nr_zones;
6039
6040 pg_data_t *node = NODE_DATA(node_order[i]);
6041
6042 nr_zones = build_zonerefs_node(node, zonerefs);
6043 zonerefs += nr_zones;
6044 }
6045 zonerefs->zone = NULL;
6046 zonerefs->zone_idx = 0;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07006047}
6048
6049/*
Christoph Lameter523b9452007-10-16 01:25:37 -07006050 * Build gfp_thisnode zonelists
6051 */
6052static void build_thisnode_zonelists(pg_data_t *pgdat)
6053{
Michal Hocko9d3be212017-09-06 16:20:30 -07006054 struct zoneref *zonerefs;
6055 int nr_zones;
Christoph Lameter523b9452007-10-16 01:25:37 -07006056
Michal Hocko9d3be212017-09-06 16:20:30 -07006057 zonerefs = pgdat->node_zonelists[ZONELIST_NOFALLBACK]._zonerefs;
6058 nr_zones = build_zonerefs_node(pgdat, zonerefs);
6059 zonerefs += nr_zones;
6060 zonerefs->zone = NULL;
6061 zonerefs->zone_idx = 0;
Christoph Lameter523b9452007-10-16 01:25:37 -07006062}
6063
6064/*
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07006065 * Build zonelists ordered by zone and nodes within zones.
6066 * This results in conserving DMA zone[s] until all Normal memory is
6067 * exhausted, but results in overflowing to remote node while memory
6068 * may still exist in local DMA zone.
6069 */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07006070
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07006071static void build_zonelists(pg_data_t *pgdat)
6072{
Michal Hocko9d3be212017-09-06 16:20:30 -07006073 static int node_order[MAX_NUMNODES];
6074 int node, load, nr_nodes = 0;
Wei Yangd0ddf492020-06-03 15:59:05 -07006075 nodemask_t used_mask = NODE_MASK_NONE;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07006076 int local_node, prev_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077
6078 /* NUMA-aware ordering of nodes */
6079 local_node = pgdat->node_id;
Christoph Lameter62bc62a2009-06-16 15:32:15 -07006080 load = nr_online_nodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081 prev_node = local_node;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07006082
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07006083 memset(node_order, 0, sizeof(node_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
6085 /*
6086 * We don't want to pressure a particular node.
6087 * So adding penalty to the first node in same
6088 * distance group to make it round-robin.
6089 */
David Rientjes957f8222012-10-08 16:33:24 -07006090 if (node_distance(local_node, node) !=
6091 node_distance(local_node, prev_node))
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07006092 node_load[node] = load;
6093
Michal Hocko9d3be212017-09-06 16:20:30 -07006094 node_order[nr_nodes++] = node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 prev_node = node;
6096 load--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 }
Christoph Lameter523b9452007-10-16 01:25:37 -07006098
Michal Hocko9d3be212017-09-06 16:20:30 -07006099 build_zonelists_in_node_order(pgdat, node_order, nr_nodes);
Christoph Lameter523b9452007-10-16 01:25:37 -07006100 build_thisnode_zonelists(pgdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101}
6102
Lee Schermerhorn7aac7892010-05-26 14:45:00 -07006103#ifdef CONFIG_HAVE_MEMORYLESS_NODES
6104/*
6105 * Return node id of node used for "local" allocations.
6106 * I.e., first node id of first zone in arg node's generic zonelist.
6107 * Used for initializing percpu 'numa_mem', which is used primarily
6108 * for kernel allocations, so use GFP_KERNEL flags to locate zonelist.
6109 */
6110int local_memory_node(int node)
6111{
Mel Gormanc33d6c02016-05-19 17:14:10 -07006112 struct zoneref *z;
Lee Schermerhorn7aac7892010-05-26 14:45:00 -07006113
Mel Gormanc33d6c02016-05-19 17:14:10 -07006114 z = first_zones_zonelist(node_zonelist(node, GFP_KERNEL),
Lee Schermerhorn7aac7892010-05-26 14:45:00 -07006115 gfp_zone(GFP_KERNEL),
Mel Gormanc33d6c02016-05-19 17:14:10 -07006116 NULL);
Pavel Tatashinc1093b72018-08-21 21:53:32 -07006117 return zone_to_nid(z->zone);
Lee Schermerhorn7aac7892010-05-26 14:45:00 -07006118}
6119#endif
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07006120
Joonsoo Kim6423aa82016-08-10 16:27:49 -07006121static void setup_min_unmapped_ratio(void);
6122static void setup_min_slab_ratio(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123#else /* CONFIG_NUMA */
6124
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07006125static void build_zonelists(pg_data_t *pgdat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126{
Christoph Lameter19655d32006-09-25 23:31:19 -07006127 int node, local_node;
Michal Hocko9d3be212017-09-06 16:20:30 -07006128 struct zoneref *zonerefs;
6129 int nr_zones;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130
6131 local_node = pgdat->node_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132
Michal Hocko9d3be212017-09-06 16:20:30 -07006133 zonerefs = pgdat->node_zonelists[ZONELIST_FALLBACK]._zonerefs;
6134 nr_zones = build_zonerefs_node(pgdat, zonerefs);
6135 zonerefs += nr_zones;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136
Mel Gorman54a6eb52008-04-28 02:12:16 -07006137 /*
6138 * Now we build the zonelist so that it contains the zones
6139 * of all the other nodes.
6140 * We don't want to pressure a particular node, so when
6141 * building the zones for node N, we make sure that the
6142 * zones coming right after the local ones are those from
6143 * node N+1 (modulo N)
6144 */
6145 for (node = local_node + 1; node < MAX_NUMNODES; node++) {
6146 if (!node_online(node))
6147 continue;
Michal Hocko9d3be212017-09-06 16:20:30 -07006148 nr_zones = build_zonerefs_node(NODE_DATA(node), zonerefs);
6149 zonerefs += nr_zones;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150 }
Mel Gorman54a6eb52008-04-28 02:12:16 -07006151 for (node = 0; node < local_node; node++) {
6152 if (!node_online(node))
6153 continue;
Michal Hocko9d3be212017-09-06 16:20:30 -07006154 nr_zones = build_zonerefs_node(NODE_DATA(node), zonerefs);
6155 zonerefs += nr_zones;
Mel Gorman54a6eb52008-04-28 02:12:16 -07006156 }
6157
Michal Hocko9d3be212017-09-06 16:20:30 -07006158 zonerefs->zone = NULL;
6159 zonerefs->zone_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160}
6161
6162#endif /* CONFIG_NUMA */
6163
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09006164/*
6165 * Boot pageset table. One per cpu which is going to be used for all
6166 * zones and all nodes. The parameters will be set in such a way
6167 * that an item put on a list will immediately be handed over to
6168 * the buddy list. This is safe since pageset manipulation is done
6169 * with interrupts disabled.
6170 *
6171 * The boot_pagesets must be kept even after bootup is complete for
6172 * unused processors and/or zones. They do play a role for bootstrapping
6173 * hotplugged processors.
6174 *
6175 * zoneinfo_show() and maybe other functions do
6176 * not check if the processor is online before following the pageset pointer.
6177 * Other parts of the kernel may not check if the zone is available.
6178 */
Vlastimil Babka69a83962020-12-14 19:10:47 -08006179static void pageset_init(struct per_cpu_pageset *p);
Vlastimil Babka952eaf82020-12-14 19:10:53 -08006180/* These effectively disable the pcplists in the boot pageset completely */
6181#define BOOT_PAGESET_HIGH 0
6182#define BOOT_PAGESET_BATCH 1
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09006183static DEFINE_PER_CPU(struct per_cpu_pageset, boot_pageset);
Johannes Weiner385386c2017-07-06 15:40:43 -07006184static DEFINE_PER_CPU(struct per_cpu_nodestat, boot_nodestats);
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09006185
Michal Hocko11cd8632017-09-06 16:20:34 -07006186static void __build_all_zonelists(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187{
Yasunori Goto68113782006-06-23 02:03:11 -07006188 int nid;
Michal Hockoafb6ebb2017-09-06 16:20:17 -07006189 int __maybe_unused cpu;
Jiang Liu9adb62a2012-07-31 16:43:28 -07006190 pg_data_t *self = data;
Michal Hockob93e0f32017-09-06 16:20:37 -07006191 static DEFINE_SPINLOCK(lock);
6192
6193 spin_lock(&lock);
Paul Jackson9276b1bc2006-12-06 20:31:48 -08006194
Bo Liu7f9cfb32009-08-18 14:11:19 -07006195#ifdef CONFIG_NUMA
6196 memset(node_load, 0, sizeof(node_load));
6197#endif
Jiang Liu9adb62a2012-07-31 16:43:28 -07006198
Wei Yangc1152582017-09-06 16:19:33 -07006199 /*
6200 * This node is hotadded and no memory is yet present. So just
6201 * building zonelists is fine - no need to touch other nodes.
6202 */
Jiang Liu9adb62a2012-07-31 16:43:28 -07006203 if (self && !node_online(self->node_id)) {
6204 build_zonelists(self);
Wei Yangc1152582017-09-06 16:19:33 -07006205 } else {
6206 for_each_online_node(nid) {
6207 pg_data_t *pgdat = NODE_DATA(nid);
Jiang Liu9adb62a2012-07-31 16:43:28 -07006208
Wei Yangc1152582017-09-06 16:19:33 -07006209 build_zonelists(pgdat);
6210 }
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09006211
Michal Hockoafb6ebb2017-09-06 16:20:17 -07006212#ifdef CONFIG_HAVE_MEMORYLESS_NODES
Michal Hockoafb6ebb2017-09-06 16:20:17 -07006213 /*
6214 * We now know the "local memory node" for each node--
6215 * i.e., the node of the first zone in the generic zonelist.
6216 * Set up numa_mem percpu variable for on-line cpus. During
6217 * boot, only the boot cpu should be on-line; we'll init the
6218 * secondary cpus' numa_mem as they come on-line. During
6219 * node/memory hotplug, we'll fixup all on-line cpus.
6220 */
Michal Hockod9c9a0b2017-09-06 16:20:20 -07006221 for_each_online_cpu(cpu)
Michal Hockoafb6ebb2017-09-06 16:20:17 -07006222 set_cpu_numa_mem(cpu, local_memory_node(cpu_to_node(cpu)));
Michal Hockoafb6ebb2017-09-06 16:20:17 -07006223#endif
Michal Hockod9c9a0b2017-09-06 16:20:20 -07006224 }
Michal Hockob93e0f32017-09-06 16:20:37 -07006225
6226 spin_unlock(&lock);
Michal Hockoafb6ebb2017-09-06 16:20:17 -07006227}
6228
6229static noinline void __init
6230build_all_zonelists_init(void)
6231{
6232 int cpu;
6233
6234 __build_all_zonelists(NULL);
6235
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09006236 /*
6237 * Initialize the boot_pagesets that are going to be used
6238 * for bootstrapping processors. The real pagesets for
6239 * each zone will be allocated later when the per cpu
6240 * allocator is available.
6241 *
6242 * boot_pagesets are used also for bootstrapping offline
6243 * cpus if the system is already booted because the pagesets
6244 * are needed to initialize allocators on a specific cpu too.
6245 * F.e. the percpu allocator needs the page allocator which
6246 * needs the percpu allocator in order to allocate its pagesets
6247 * (a chicken-egg dilemma).
6248 */
Michal Hockoafb6ebb2017-09-06 16:20:17 -07006249 for_each_possible_cpu(cpu)
Vlastimil Babka69a83962020-12-14 19:10:47 -08006250 pageset_init(&per_cpu(boot_pageset, cpu));
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09006251
Rasmus Villemoes061f67b2015-02-12 15:00:06 -08006252 mminit_verify_zonelist();
6253 cpuset_init_current_mems_allowed();
6254}
6255
Haicheng Li4eaf3f62010-05-24 14:32:52 -07006256/*
Haicheng Li4eaf3f62010-05-24 14:32:52 -07006257 * unless system_state == SYSTEM_BOOTING.
Rasmus Villemoes061f67b2015-02-12 15:00:06 -08006258 *
Michal Hocko72675e12017-09-06 16:20:24 -07006259 * __ref due to call of __init annotated helper build_all_zonelists_init
Rasmus Villemoes061f67b2015-02-12 15:00:06 -08006260 * [protected by SYSTEM_BOOTING].
Haicheng Li4eaf3f62010-05-24 14:32:52 -07006261 */
Michal Hocko72675e12017-09-06 16:20:24 -07006262void __ref build_all_zonelists(pg_data_t *pgdat)
Yasunori Goto68113782006-06-23 02:03:11 -07006263{
David Hildenbrand0a18e602020-08-06 23:25:27 -07006264 unsigned long vm_total_pages;
6265
Yasunori Goto68113782006-06-23 02:03:11 -07006266 if (system_state == SYSTEM_BOOTING) {
Rasmus Villemoes061f67b2015-02-12 15:00:06 -08006267 build_all_zonelists_init();
Yasunori Goto68113782006-06-23 02:03:11 -07006268 } else {
Michal Hocko11cd8632017-09-06 16:20:34 -07006269 __build_all_zonelists(pgdat);
Yasunori Goto68113782006-06-23 02:03:11 -07006270 /* cpuset refresh routine should be here */
6271 }
David Hildenbrand56b94132020-08-06 23:25:30 -07006272 /* Get the number of free pages beyond high watermark in all zones. */
6273 vm_total_pages = nr_free_zone_pages(gfp_zone(GFP_HIGHUSER_MOVABLE));
Mel Gorman9ef9acb2007-10-16 01:25:54 -07006274 /*
6275 * Disable grouping by mobility if the number of pages in the
6276 * system is too low to allow the mechanism to work. It would be
6277 * more accurate, but expensive to check per-zone. This check is
6278 * made on memory-hotadd so a system can start with mobility
6279 * disabled and enable it later
6280 */
Mel Gormand9c23402007-10-16 01:26:01 -07006281 if (vm_total_pages < (pageblock_nr_pages * MIGRATE_TYPES))
Mel Gorman9ef9acb2007-10-16 01:25:54 -07006282 page_group_by_mobility_disabled = 1;
6283 else
6284 page_group_by_mobility_disabled = 0;
6285
Alexey Dobriyance0725f2019-03-05 15:48:29 -08006286 pr_info("Built %u zonelists, mobility grouping %s. Total pages: %ld\n",
Joe Perches756a0252016-03-17 14:19:47 -07006287 nr_online_nodes,
Joe Perches756a0252016-03-17 14:19:47 -07006288 page_group_by_mobility_disabled ? "off" : "on",
6289 vm_total_pages);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07006290#ifdef CONFIG_NUMA
Anton Blanchardf88dfff2014-12-10 15:42:53 -08006291 pr_info("Policy zone: %s\n", zone_names[policy_zone]);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07006292#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293}
6294
Pavel Tatashina9a9e772018-10-26 15:09:40 -07006295/* If zone is ZONE_MOVABLE but memory is mirrored, it is an overlapped init */
6296static bool __meminit
6297overlap_memmap_init(unsigned long zone, unsigned long *pfn)
6298{
Pavel Tatashina9a9e772018-10-26 15:09:40 -07006299 static struct memblock_region *r;
6300
6301 if (mirrored_kernelcore && zone == ZONE_MOVABLE) {
6302 if (!r || *pfn >= memblock_region_memory_end_pfn(r)) {
Mike Rapoportcc6de162020-10-13 16:58:30 -07006303 for_each_mem_region(r) {
Pavel Tatashina9a9e772018-10-26 15:09:40 -07006304 if (*pfn < memblock_region_memory_end_pfn(r))
6305 break;
6306 }
6307 }
6308 if (*pfn >= memblock_region_memory_base_pfn(r) &&
6309 memblock_is_mirror(r)) {
6310 *pfn = memblock_region_memory_end_pfn(r);
6311 return true;
6312 }
6313 }
Pavel Tatashina9a9e772018-10-26 15:09:40 -07006314 return false;
6315}
6316
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318 * Initially all pages are reserved - free ones are freed
Mike Rapoportc6ffc5c2018-10-30 15:09:30 -07006319 * up by memblock_free_all() once the early boot process is
Linus Torvalds1da177e2005-04-16 15:20:36 -07006320 * done. Non-atomic initialization, single-pass.
David Hildenbrandd882c002020-10-15 20:08:19 -07006321 *
6322 * All aligned pageblocks are initialized to the specified migratetype
6323 * (usually MIGRATE_MOVABLE). Besides setting the migratetype, no related
6324 * zone stats (e.g., nr_isolate_pageblock) are touched.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325 */
Baoquan Heab28cb62021-02-24 12:06:14 -08006326void __meminit memmap_init_range(unsigned long size, int nid, unsigned long zone,
Baoquan Hedc2da7b2020-12-29 15:14:37 -08006327 unsigned long start_pfn, unsigned long zone_end_pfn,
David Hildenbrandd882c002020-10-15 20:08:19 -07006328 enum meminit_context context,
6329 struct vmem_altmap *altmap, int migratetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330{
Pavel Tatashina9a9e772018-10-26 15:09:40 -07006331 unsigned long pfn, end_pfn = start_pfn + size;
Pavel Tatashind0dc12e2018-04-05 16:23:00 -07006332 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333
Hugh Dickins22b31ee2009-01-06 14:40:09 -08006334 if (highest_memmap_pfn < end_pfn - 1)
6335 highest_memmap_pfn = end_pfn - 1;
6336
Alexander Duyck966cf442018-10-26 15:07:52 -07006337#ifdef CONFIG_ZONE_DEVICE
Dan Williams4b94ffd2016-01-15 16:56:22 -08006338 /*
6339 * Honor reservation requested by the driver for this ZONE_DEVICE
Alexander Duyck966cf442018-10-26 15:07:52 -07006340 * memory. We limit the total number of pages to initialize to just
6341 * those that might contain the memory mapping. We will defer the
6342 * ZONE_DEVICE page initialization until after we have released
6343 * the hotplug lock.
Dan Williams4b94ffd2016-01-15 16:56:22 -08006344 */
Alexander Duyck966cf442018-10-26 15:07:52 -07006345 if (zone == ZONE_DEVICE) {
6346 if (!altmap)
6347 return;
6348
6349 if (start_pfn == altmap->base_pfn)
6350 start_pfn += altmap->reserve;
6351 end_pfn = altmap->base_pfn + vmem_altmap_offset(altmap);
6352 }
6353#endif
Dan Williams4b94ffd2016-01-15 16:56:22 -08006354
David Hildenbrand948c4362020-02-03 17:33:59 -08006355 for (pfn = start_pfn; pfn < end_pfn; ) {
Dave Hansena2f3aa022007-01-10 23:15:30 -08006356 /*
Andrew Mortonb72d0ff2016-03-15 14:55:25 -07006357 * There can be holes in boot-time mem_map[]s handed to this
6358 * function. They do not exist on hotplugged memory.
Dave Hansena2f3aa022007-01-10 23:15:30 -08006359 */
Laurent Dufourc1d0da82020-09-25 21:19:28 -07006360 if (context == MEMINIT_EARLY) {
Pavel Tatashina9a9e772018-10-26 15:09:40 -07006361 if (overlap_memmap_init(zone, &pfn))
6362 continue;
Baoquan Hedc2da7b2020-12-29 15:14:37 -08006363 if (defer_init(nid, pfn, zone_end_pfn))
Pavel Tatashina9a9e772018-10-26 15:09:40 -07006364 break;
Dave Hansena2f3aa022007-01-10 23:15:30 -08006365 }
Mel Gormanac5d2532015-06-30 14:57:20 -07006366
Pavel Tatashind0dc12e2018-04-05 16:23:00 -07006367 page = pfn_to_page(pfn);
6368 __init_single_page(page, pfn, zone, nid);
Laurent Dufourc1d0da82020-09-25 21:19:28 -07006369 if (context == MEMINIT_HOTPLUG)
Alexander Duyckd483da52018-10-26 15:07:48 -07006370 __SetPageReserved(page);
Pavel Tatashind0dc12e2018-04-05 16:23:00 -07006371
Mel Gormanac5d2532015-06-30 14:57:20 -07006372 /*
David Hildenbrandd882c002020-10-15 20:08:19 -07006373 * Usually, we want to mark the pageblock MIGRATE_MOVABLE,
6374 * such that unmovable allocations won't be scattered all
6375 * over the place during system boot.
Mel Gormanac5d2532015-06-30 14:57:20 -07006376 */
David Hildenbrand4eb29bd2020-10-15 20:08:15 -07006377 if (IS_ALIGNED(pfn, pageblock_nr_pages)) {
David Hildenbrandd882c002020-10-15 20:08:19 -07006378 set_pageblock_migratetype(page, migratetype);
Michal Hocko9b6e63c2017-10-03 16:16:19 -07006379 cond_resched();
Mel Gormanac5d2532015-06-30 14:57:20 -07006380 }
David Hildenbrand948c4362020-02-03 17:33:59 -08006381 pfn++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382 }
6383}
6384
Alexander Duyck966cf442018-10-26 15:07:52 -07006385#ifdef CONFIG_ZONE_DEVICE
6386void __ref memmap_init_zone_device(struct zone *zone,
6387 unsigned long start_pfn,
Aneesh Kumar K.V1f8d75c2020-02-03 17:34:06 -08006388 unsigned long nr_pages,
Alexander Duyck966cf442018-10-26 15:07:52 -07006389 struct dev_pagemap *pgmap)
6390{
Aneesh Kumar K.V1f8d75c2020-02-03 17:34:06 -08006391 unsigned long pfn, end_pfn = start_pfn + nr_pages;
Alexander Duyck966cf442018-10-26 15:07:52 -07006392 struct pglist_data *pgdat = zone->zone_pgdat;
Christoph Hellwig514caf22019-06-26 14:27:13 +02006393 struct vmem_altmap *altmap = pgmap_altmap(pgmap);
Alexander Duyck966cf442018-10-26 15:07:52 -07006394 unsigned long zone_idx = zone_idx(zone);
6395 unsigned long start = jiffies;
6396 int nid = pgdat->node_id;
6397
Dan Williams46d945a2019-07-18 15:58:18 -07006398 if (WARN_ON_ONCE(!pgmap || zone_idx(zone) != ZONE_DEVICE))
Alexander Duyck966cf442018-10-26 15:07:52 -07006399 return;
6400
6401 /*
Mike Rapoport122e0932021-06-28 19:33:26 -07006402 * The call to memmap_init should have already taken care
Alexander Duyck966cf442018-10-26 15:07:52 -07006403 * of the pages reserved for the memmap, so we can just jump to
6404 * the end of that region and start processing the device pages.
6405 */
Christoph Hellwig514caf22019-06-26 14:27:13 +02006406 if (altmap) {
Alexander Duyck966cf442018-10-26 15:07:52 -07006407 start_pfn = altmap->base_pfn + vmem_altmap_offset(altmap);
Aneesh Kumar K.V1f8d75c2020-02-03 17:34:06 -08006408 nr_pages = end_pfn - start_pfn;
Alexander Duyck966cf442018-10-26 15:07:52 -07006409 }
6410
6411 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
6412 struct page *page = pfn_to_page(pfn);
6413
6414 __init_single_page(page, pfn, zone_idx, nid);
6415
6416 /*
6417 * Mark page reserved as it will need to wait for onlining
6418 * phase for it to be fully associated with a zone.
6419 *
6420 * We can use the non-atomic __set_bit operation for setting
6421 * the flag as we are still initializing the pages.
6422 */
6423 __SetPageReserved(page);
6424
6425 /*
Christoph Hellwig8a164fe2019-06-26 14:27:21 +02006426 * ZONE_DEVICE pages union ->lru with a ->pgmap back pointer
6427 * and zone_device_data. It is a bug if a ZONE_DEVICE page is
6428 * ever freed or placed on a driver-private list.
Alexander Duyck966cf442018-10-26 15:07:52 -07006429 */
6430 page->pgmap = pgmap;
Christoph Hellwig8a164fe2019-06-26 14:27:21 +02006431 page->zone_device_data = NULL;
Alexander Duyck966cf442018-10-26 15:07:52 -07006432
6433 /*
6434 * Mark the block movable so that blocks are reserved for
6435 * movable at startup. This will force kernel allocations
6436 * to reserve their blocks rather than leaking throughout
6437 * the address space during boot when many long-lived
6438 * kernel allocations are made.
6439 *
Laurent Dufourc1d0da82020-09-25 21:19:28 -07006440 * Please note that MEMINIT_HOTPLUG path doesn't clear memmap
Dan Williamsba72b4c2019-07-18 15:58:26 -07006441 * because this is done early in section_activate()
Alexander Duyck966cf442018-10-26 15:07:52 -07006442 */
David Hildenbrand4eb29bd2020-10-15 20:08:15 -07006443 if (IS_ALIGNED(pfn, pageblock_nr_pages)) {
Alexander Duyck966cf442018-10-26 15:07:52 -07006444 set_pageblock_migratetype(page, MIGRATE_MOVABLE);
6445 cond_resched();
6446 }
6447 }
6448
Christoph Hellwigfdc029b2019-08-18 11:05:55 +02006449 pr_info("%s initialised %lu pages in %ums\n", __func__,
Aneesh Kumar K.V1f8d75c2020-02-03 17:34:06 -08006450 nr_pages, jiffies_to_msecs(jiffies - start));
Alexander Duyck966cf442018-10-26 15:07:52 -07006451}
6452
6453#endif
Andi Kleen1e548de2008-02-04 22:29:26 -08006454static void __meminit zone_init_free_lists(struct zone *zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006455{
Mel Gorman7aeb09f2014-06-04 16:10:21 -07006456 unsigned int order, t;
Mel Gormanb2a0ac82007-10-16 01:25:48 -07006457 for_each_migratetype_order(order, t) {
6458 INIT_LIST_HEAD(&zone->free_area[order].free_list[t]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006459 zone->free_area[order].nr_free = 0;
6460 }
6461}
6462
Mike Rapoport0740a502021-03-12 21:07:12 -08006463#if !defined(CONFIG_FLAT_NODE_MEM_MAP)
6464/*
6465 * Only struct pages that correspond to ranges defined by memblock.memory
6466 * are zeroed and initialized by going through __init_single_page() during
Mike Rapoport122e0932021-06-28 19:33:26 -07006467 * memmap_init_zone_range().
Mike Rapoport0740a502021-03-12 21:07:12 -08006468 *
6469 * But, there could be struct pages that correspond to holes in
6470 * memblock.memory. This can happen because of the following reasons:
6471 * - physical memory bank size is not necessarily the exact multiple of the
6472 * arbitrary section size
6473 * - early reserved memory may not be listed in memblock.memory
6474 * - memory layouts defined with memmap= kernel parameter may not align
6475 * nicely with memmap sections
6476 *
6477 * Explicitly initialize those struct pages so that:
6478 * - PG_Reserved is set
6479 * - zone and node links point to zone and node that span the page if the
6480 * hole is in the middle of a zone
6481 * - zone and node links point to adjacent zone/node if the hole falls on
6482 * the zone boundary; the pages in such holes will be prepended to the
6483 * zone/node above the hole except for the trailing pages in the last
6484 * section that will be appended to the zone/node below.
6485 */
Mike Rapoport122e0932021-06-28 19:33:26 -07006486static void __init init_unavailable_range(unsigned long spfn,
6487 unsigned long epfn,
6488 int zone, int node)
Mike Rapoport0740a502021-03-12 21:07:12 -08006489{
6490 unsigned long pfn;
6491 u64 pgcnt = 0;
6492
6493 for (pfn = spfn; pfn < epfn; pfn++) {
6494 if (!pfn_valid(ALIGN_DOWN(pfn, pageblock_nr_pages))) {
6495 pfn = ALIGN_DOWN(pfn, pageblock_nr_pages)
6496 + pageblock_nr_pages - 1;
6497 continue;
6498 }
6499 __init_single_page(pfn_to_page(pfn), pfn, zone, node);
6500 __SetPageReserved(pfn_to_page(pfn));
6501 pgcnt++;
6502 }
6503
Mike Rapoport122e0932021-06-28 19:33:26 -07006504 if (pgcnt)
6505 pr_info("On node %d, zone %s: %lld pages in unavailable ranges",
6506 node, zone_names[zone], pgcnt);
Mike Rapoport0740a502021-03-12 21:07:12 -08006507}
6508#else
Mike Rapoport122e0932021-06-28 19:33:26 -07006509static inline void init_unavailable_range(unsigned long spfn,
6510 unsigned long epfn,
6511 int zone, int node)
Mike Rapoport0740a502021-03-12 21:07:12 -08006512{
Mike Rapoport0740a502021-03-12 21:07:12 -08006513}
6514#endif
6515
Mike Rapoport122e0932021-06-28 19:33:26 -07006516static void __init memmap_init_zone_range(struct zone *zone,
6517 unsigned long start_pfn,
6518 unsigned long end_pfn,
6519 unsigned long *hole_pfn)
Pavel Tatashindfb3ccd2018-10-26 15:09:32 -07006520{
Baoquan He3256ff82021-02-24 12:06:17 -08006521 unsigned long zone_start_pfn = zone->zone_start_pfn;
6522 unsigned long zone_end_pfn = zone_start_pfn + zone->spanned_pages;
Mike Rapoport122e0932021-06-28 19:33:26 -07006523 int nid = zone_to_nid(zone), zone_id = zone_idx(zone);
6524
6525 start_pfn = clamp(start_pfn, zone_start_pfn, zone_end_pfn);
6526 end_pfn = clamp(end_pfn, zone_start_pfn, zone_end_pfn);
6527
6528 if (start_pfn >= end_pfn)
6529 return;
6530
6531 memmap_init_range(end_pfn - start_pfn, nid, zone_id, start_pfn,
6532 zone_end_pfn, MEMINIT_EARLY, NULL, MIGRATE_MOVABLE);
6533
6534 if (*hole_pfn < start_pfn)
6535 init_unavailable_range(*hole_pfn, start_pfn, zone_id, nid);
6536
6537 *hole_pfn = end_pfn;
6538}
6539
6540static void __init memmap_init(void)
6541{
Baoquan He73a6e472020-06-03 15:57:55 -07006542 unsigned long start_pfn, end_pfn;
Mike Rapoport122e0932021-06-28 19:33:26 -07006543 unsigned long hole_pfn = 0;
6544 int i, j, zone_id, nid;
Baoquan He73a6e472020-06-03 15:57:55 -07006545
Mike Rapoport122e0932021-06-28 19:33:26 -07006546 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) {
6547 struct pglist_data *node = NODE_DATA(nid);
Baoquan He73a6e472020-06-03 15:57:55 -07006548
Mike Rapoport122e0932021-06-28 19:33:26 -07006549 for (j = 0; j < MAX_NR_ZONES; j++) {
6550 struct zone *zone = node->node_zones + j;
Mike Rapoport0740a502021-03-12 21:07:12 -08006551
Mike Rapoport122e0932021-06-28 19:33:26 -07006552 if (!populated_zone(zone))
6553 continue;
6554
6555 memmap_init_zone_range(zone, start_pfn, end_pfn,
6556 &hole_pfn);
6557 zone_id = j;
6558 }
Baoquan He73a6e472020-06-03 15:57:55 -07006559 }
Mike Rapoport0740a502021-03-12 21:07:12 -08006560
6561#ifdef CONFIG_SPARSEMEM
6562 /*
Mike Rapoport122e0932021-06-28 19:33:26 -07006563 * Initialize the memory map for hole in the range [memory_end,
6564 * section_end].
6565 * Append the pages in this hole to the highest zone in the last
6566 * node.
6567 * The call to init_unavailable_range() is outside the ifdef to
6568 * silence the compiler warining about zone_id set but not used;
6569 * for FLATMEM it is a nop anyway
Mike Rapoport0740a502021-03-12 21:07:12 -08006570 */
Mike Rapoport122e0932021-06-28 19:33:26 -07006571 end_pfn = round_up(end_pfn, PAGES_PER_SECTION);
Mike Rapoport0740a502021-03-12 21:07:12 -08006572 if (hole_pfn < end_pfn)
Mike Rapoport0740a502021-03-12 21:07:12 -08006573#endif
Mike Rapoport122e0932021-06-28 19:33:26 -07006574 init_unavailable_range(hole_pfn, end_pfn, zone_id, nid);
Pavel Tatashindfb3ccd2018-10-26 15:09:32 -07006575}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576
David Rientjes7cd2b0a2014-06-23 13:22:04 -07006577static int zone_batchsize(struct zone *zone)
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07006578{
David Howells3a6be872009-05-06 16:03:03 -07006579#ifdef CONFIG_MMU
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07006580 int batch;
6581
6582 /*
6583 * The per-cpu-pages pools are set to around 1000th of the
Aaron Lud8a759b2018-08-17 15:49:14 -07006584 * size of the zone.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07006585 */
Arun KS9705bea2018-12-28 00:34:24 -08006586 batch = zone_managed_pages(zone) / 1024;
Aaron Lud8a759b2018-08-17 15:49:14 -07006587 /* But no more than a meg. */
6588 if (batch * PAGE_SIZE > 1024 * 1024)
6589 batch = (1024 * 1024) / PAGE_SIZE;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07006590 batch /= 4; /* We effectively *= 4 below */
6591 if (batch < 1)
6592 batch = 1;
6593
6594 /*
Nick Piggin0ceaacc2005-12-04 13:55:25 +11006595 * Clamp the batch to a 2^n - 1 value. Having a power
6596 * of 2 value was found to be more likely to have
6597 * suboptimal cache aliasing properties in some cases.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07006598 *
Nick Piggin0ceaacc2005-12-04 13:55:25 +11006599 * For example if 2 tasks are alternately allocating
6600 * batches of pages, one task can end up with a lot
6601 * of pages of one half of the possible page colors
6602 * and the other with pages of the other colors.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07006603 */
David Howells91552032009-05-06 16:03:02 -07006604 batch = rounddown_pow_of_two(batch + batch/2) - 1;
Seth, Rohitba56e912005-10-29 18:15:47 -07006605
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07006606 return batch;
David Howells3a6be872009-05-06 16:03:03 -07006607
6608#else
6609 /* The deferral and batching of frees should be suppressed under NOMMU
6610 * conditions.
6611 *
6612 * The problem is that NOMMU needs to be able to allocate large chunks
6613 * of contiguous memory as there's no hardware page translation to
6614 * assemble apparent contiguous memory from discontiguous pages.
6615 *
6616 * Queueing large contiguous runs of pages for batching, however,
6617 * causes the pages to actually be freed in smaller chunks. As there
6618 * can be a significant delay between the individual batches being
6619 * recycled, this leads to the once large chunks of space being
6620 * fragmented and becoming unavailable for high-order allocations.
6621 */
6622 return 0;
6623#endif
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07006624}
6625
Cody P Schafer8d7a8fa2013-07-03 15:01:31 -07006626/*
Vlastimil Babka5c3ad2e2020-12-14 19:10:50 -08006627 * pcp->high and pcp->batch values are related and generally batch is lower
6628 * than high. They are also related to pcp->count such that count is lower
6629 * than high, and as soon as it reaches high, the pcplist is flushed.
Cody P Schafer8d7a8fa2013-07-03 15:01:31 -07006630 *
Vlastimil Babka5c3ad2e2020-12-14 19:10:50 -08006631 * However, guaranteeing these relations at all times would require e.g. write
6632 * barriers here but also careful usage of read barriers at the read side, and
6633 * thus be prone to error and bad for performance. Thus the update only prevents
6634 * store tearing. Any new users of pcp->batch and pcp->high should ensure they
6635 * can cope with those fields changing asynchronously, and fully trust only the
6636 * pcp->count field on the local CPU with interrupts disabled.
Cody P Schafer8d7a8fa2013-07-03 15:01:31 -07006637 *
6638 * mutex_is_locked(&pcp_batch_high_lock) required when calling this function
6639 * outside of boot time (or some other assurance that no concurrent updaters
6640 * exist).
6641 */
6642static void pageset_update(struct per_cpu_pages *pcp, unsigned long high,
6643 unsigned long batch)
6644{
Vlastimil Babka5c3ad2e2020-12-14 19:10:50 -08006645 WRITE_ONCE(pcp->batch, batch);
6646 WRITE_ONCE(pcp->high, high);
Cody P Schafer8d7a8fa2013-07-03 15:01:31 -07006647}
6648
Cody P Schafer88c90db2013-07-03 15:01:35 -07006649static void pageset_init(struct per_cpu_pageset *p)
Christoph Lameter2caaad42005-06-21 17:15:00 -07006650{
6651 struct per_cpu_pages *pcp;
Mel Gorman5f8dcc22009-09-21 17:03:19 -07006652 int migratetype;
Christoph Lameter2caaad42005-06-21 17:15:00 -07006653
Magnus Damm1c6fe942005-10-26 01:58:59 -07006654 memset(p, 0, sizeof(*p));
6655
Christoph Lameter3dfa5722008-02-04 22:29:19 -08006656 pcp = &p->pcp;
Mel Gorman5f8dcc22009-09-21 17:03:19 -07006657 for (migratetype = 0; migratetype < MIGRATE_PCPTYPES; migratetype++)
6658 INIT_LIST_HEAD(&pcp->lists[migratetype]);
Christoph Lameter2caaad42005-06-21 17:15:00 -07006659
Vlastimil Babka69a83962020-12-14 19:10:47 -08006660 /*
6661 * Set batch and high values safe for a boot pageset. A true percpu
6662 * pageset's initialization will update them subsequently. Here we don't
6663 * need to be as careful as pageset_update() as nobody can access the
6664 * pageset yet.
6665 */
Vlastimil Babka952eaf82020-12-14 19:10:53 -08006666 pcp->high = BOOT_PAGESET_HIGH;
6667 pcp->batch = BOOT_PAGESET_BATCH;
Cody P Schafer88c90db2013-07-03 15:01:35 -07006668}
6669
Zou Wei3b1f3652020-12-14 19:11:12 -08006670static void __zone_set_pageset_high_and_batch(struct zone *zone, unsigned long high,
Vlastimil Babkaec6e8c7e2020-12-14 19:10:59 -08006671 unsigned long batch)
6672{
6673 struct per_cpu_pageset *p;
6674 int cpu;
6675
6676 for_each_possible_cpu(cpu) {
6677 p = per_cpu_ptr(zone->pageset, cpu);
6678 pageset_update(&p->pcp, high, batch);
6679 }
6680}
6681
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08006682/*
Vlastimil Babka0a8b4f12020-12-14 19:10:43 -08006683 * Calculate and set new high and batch values for all per-cpu pagesets of a
Vlastimil Babka7115ac62020-12-14 19:10:40 -08006684 * zone, based on the zone's size and the percpu_pagelist_fraction sysctl.
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08006685 */
Vlastimil Babka0a8b4f12020-12-14 19:10:43 -08006686static void zone_set_pageset_high_and_batch(struct zone *zone)
Cody P Schafer56cef2b2013-07-03 15:01:38 -07006687{
Vlastimil Babka7115ac62020-12-14 19:10:40 -08006688 unsigned long new_high, new_batch;
6689
6690 if (percpu_pagelist_fraction) {
6691 new_high = zone_managed_pages(zone) / percpu_pagelist_fraction;
6692 new_batch = max(1UL, new_high / 4);
6693 if ((new_high / 4) > (PAGE_SHIFT * 8))
6694 new_batch = PAGE_SHIFT * 8;
6695 } else {
6696 new_batch = zone_batchsize(zone);
6697 new_high = 6 * new_batch;
6698 new_batch = max(1UL, 1 * new_batch);
6699 }
Cody P Schafer56cef2b2013-07-03 15:01:38 -07006700
Vlastimil Babka952eaf82020-12-14 19:10:53 -08006701 if (zone->pageset_high == new_high &&
6702 zone->pageset_batch == new_batch)
6703 return;
6704
6705 zone->pageset_high = new_high;
6706 zone->pageset_batch = new_batch;
6707
Vlastimil Babkaec6e8c7e2020-12-14 19:10:59 -08006708 __zone_set_pageset_high_and_batch(zone, new_high, new_batch);
Cody P Schafer169f6c12013-07-03 15:01:41 -07006709}
6710
Michal Hocko72675e12017-09-06 16:20:24 -07006711void __meminit setup_zone_pageset(struct zone *zone)
Wu Fengguang319774e2010-05-24 14:32:49 -07006712{
Vlastimil Babka0a8b4f12020-12-14 19:10:43 -08006713 struct per_cpu_pageset *p;
Wu Fengguang319774e2010-05-24 14:32:49 -07006714 int cpu;
Vlastimil Babka0a8b4f12020-12-14 19:10:43 -08006715
Wu Fengguang319774e2010-05-24 14:32:49 -07006716 zone->pageset = alloc_percpu(struct per_cpu_pageset);
Vlastimil Babka0a8b4f12020-12-14 19:10:43 -08006717 for_each_possible_cpu(cpu) {
6718 p = per_cpu_ptr(zone->pageset, cpu);
6719 pageset_init(p);
6720 }
6721
6722 zone_set_pageset_high_and_batch(zone);
Wu Fengguang319774e2010-05-24 14:32:49 -07006723}
6724
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07006725/*
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09006726 * Allocate per cpu pagesets and initialize them.
6727 * Before this call only boot pagesets were available.
Christoph Lameter2caaad42005-06-21 17:15:00 -07006728 */
Al Viro78d99552005-12-15 09:18:25 +00006729void __init setup_per_cpu_pageset(void)
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07006730{
Mel Gormanb4911ea2016-08-04 15:31:49 -07006731 struct pglist_data *pgdat;
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09006732 struct zone *zone;
Sandipan Dasb418a0f2020-06-03 15:59:11 -07006733 int __maybe_unused cpu;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07006734
Wu Fengguang319774e2010-05-24 14:32:49 -07006735 for_each_populated_zone(zone)
6736 setup_zone_pageset(zone);
Mel Gormanb4911ea2016-08-04 15:31:49 -07006737
Sandipan Dasb418a0f2020-06-03 15:59:11 -07006738#ifdef CONFIG_NUMA
6739 /*
6740 * Unpopulated zones continue using the boot pagesets.
6741 * The numa stats for these pagesets need to be reset.
6742 * Otherwise, they will end up skewing the stats of
6743 * the nodes these zones are associated with.
6744 */
6745 for_each_possible_cpu(cpu) {
6746 struct per_cpu_pageset *pcp = &per_cpu(boot_pageset, cpu);
6747 memset(pcp->vm_numa_stat_diff, 0,
6748 sizeof(pcp->vm_numa_stat_diff));
6749 }
6750#endif
6751
Mel Gormanb4911ea2016-08-04 15:31:49 -07006752 for_each_online_pgdat(pgdat)
6753 pgdat->per_cpu_nodestats =
6754 alloc_percpu(struct per_cpu_nodestat);
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07006755}
6756
Matt Tolentinoc09b4242006-01-17 07:03:44 +01006757static __meminit void zone_pcp_init(struct zone *zone)
Dave Hansened8ece22005-10-29 18:16:50 -07006758{
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09006759 /*
6760 * per cpu subsystem is not up at this point. The following code
6761 * relies on the ability of the linker to provide the
6762 * offset of a (static) per cpu variable into the per cpu area.
6763 */
6764 zone->pageset = &boot_pageset;
Vlastimil Babka952eaf82020-12-14 19:10:53 -08006765 zone->pageset_high = BOOT_PAGESET_HIGH;
6766 zone->pageset_batch = BOOT_PAGESET_BATCH;
Dave Hansened8ece22005-10-29 18:16:50 -07006767
Xishi Qiub38a8722013-11-12 15:07:20 -08006768 if (populated_zone(zone))
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09006769 printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%u\n",
6770 zone->name, zone->present_pages,
6771 zone_batchsize(zone));
Dave Hansened8ece22005-10-29 18:16:50 -07006772}
6773
Michal Hockodc0bbf32017-07-06 15:37:35 -07006774void __meminit init_currently_empty_zone(struct zone *zone,
Yasunori Goto718127c2006-06-23 02:03:10 -07006775 unsigned long zone_start_pfn,
Yaowei Baib171e402015-11-05 18:47:06 -08006776 unsigned long size)
Dave Hansened8ece22005-10-29 18:16:50 -07006777{
6778 struct pglist_data *pgdat = zone->zone_pgdat;
Wei Yang8f416832018-11-30 14:09:07 -08006779 int zone_idx = zone_idx(zone) + 1;
Linus Torvalds9dcb8b62016-10-26 10:15:30 -07006780
Wei Yang8f416832018-11-30 14:09:07 -08006781 if (zone_idx > pgdat->nr_zones)
6782 pgdat->nr_zones = zone_idx;
Dave Hansened8ece22005-10-29 18:16:50 -07006783
Dave Hansened8ece22005-10-29 18:16:50 -07006784 zone->zone_start_pfn = zone_start_pfn;
6785
Mel Gorman708614e2008-07-23 21:26:51 -07006786 mminit_dprintk(MMINIT_TRACE, "memmap_init",
6787 "Initialising map node %d zone %lu pfns %lu -> %lu\n",
6788 pgdat->node_id,
6789 (unsigned long)zone_idx(zone),
6790 zone_start_pfn, (zone_start_pfn + size));
6791
Andi Kleen1e548de2008-02-04 22:29:26 -08006792 zone_init_free_lists(zone);
Linus Torvalds9dcb8b62016-10-26 10:15:30 -07006793 zone->initialized = 1;
Dave Hansened8ece22005-10-29 18:16:50 -07006794}
6795
Mel Gormanc7132162006-09-27 01:49:43 -07006796/**
Mel Gormanc7132162006-09-27 01:49:43 -07006797 * get_pfn_range_for_nid - Return the start and end page frames for a node
Randy Dunlap88ca3b92006-10-04 02:15:25 -07006798 * @nid: The nid to return the range for. If MAX_NUMNODES, the min and max PFN are returned.
6799 * @start_pfn: Passed by reference. On return, it will have the node start_pfn.
6800 * @end_pfn: Passed by reference. On return, it will have the node end_pfn.
Mel Gormanc7132162006-09-27 01:49:43 -07006801 *
6802 * It returns the start and end page frame of a node based on information
Zhang Zhen7d018172014-06-04 16:10:53 -07006803 * provided by memblock_set_node(). If called for a node
Mel Gormanc7132162006-09-27 01:49:43 -07006804 * with no available memory, a warning is printed and the start and end
Randy Dunlap88ca3b92006-10-04 02:15:25 -07006805 * PFNs will be 0.
Mel Gormanc7132162006-09-27 01:49:43 -07006806 */
Oscar Salvadorbbe5d992018-12-28 00:37:24 -08006807void __init get_pfn_range_for_nid(unsigned int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07006808 unsigned long *start_pfn, unsigned long *end_pfn)
6809{
Tejun Heoc13291a2011-07-12 10:46:30 +02006810 unsigned long this_start_pfn, this_end_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07006811 int i;
Tejun Heoc13291a2011-07-12 10:46:30 +02006812
Mel Gormanc7132162006-09-27 01:49:43 -07006813 *start_pfn = -1UL;
6814 *end_pfn = 0;
6815
Tejun Heoc13291a2011-07-12 10:46:30 +02006816 for_each_mem_pfn_range(i, nid, &this_start_pfn, &this_end_pfn, NULL) {
6817 *start_pfn = min(*start_pfn, this_start_pfn);
6818 *end_pfn = max(*end_pfn, this_end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07006819 }
6820
Christoph Lameter633c0662007-10-16 01:25:37 -07006821 if (*start_pfn == -1UL)
Mel Gormanc7132162006-09-27 01:49:43 -07006822 *start_pfn = 0;
Mel Gormanc7132162006-09-27 01:49:43 -07006823}
6824
6825/*
Mel Gorman2a1e2742007-07-17 04:03:12 -07006826 * This finds a zone that can be used for ZONE_MOVABLE pages. The
6827 * assumption is made that zones within a node are ordered in monotonic
6828 * increasing memory addresses so that the "highest" populated zone is used
6829 */
Adrian Bunkb69a7282008-07-23 21:28:12 -07006830static void __init find_usable_zone_for_movable(void)
Mel Gorman2a1e2742007-07-17 04:03:12 -07006831{
6832 int zone_index;
6833 for (zone_index = MAX_NR_ZONES - 1; zone_index >= 0; zone_index--) {
6834 if (zone_index == ZONE_MOVABLE)
6835 continue;
6836
6837 if (arch_zone_highest_possible_pfn[zone_index] >
6838 arch_zone_lowest_possible_pfn[zone_index])
6839 break;
6840 }
6841
6842 VM_BUG_ON(zone_index == -1);
6843 movable_zone = zone_index;
6844}
6845
6846/*
6847 * The zone ranges provided by the architecture do not include ZONE_MOVABLE
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006848 * because it is sized independent of architecture. Unlike the other zones,
Mel Gorman2a1e2742007-07-17 04:03:12 -07006849 * the starting point for ZONE_MOVABLE is not fixed. It may be different
6850 * in each node depending on the size of each node and how evenly kernelcore
6851 * is distributed. This helper function adjusts the zone ranges
6852 * provided by the architecture for a given node by using the end of the
6853 * highest usable zone for ZONE_MOVABLE. This preserves the assumption that
6854 * zones within a node are in order of monotonic increases memory addresses
6855 */
Oscar Salvadorbbe5d992018-12-28 00:37:24 -08006856static void __init adjust_zone_range_for_zone_movable(int nid,
Mel Gorman2a1e2742007-07-17 04:03:12 -07006857 unsigned long zone_type,
6858 unsigned long node_start_pfn,
6859 unsigned long node_end_pfn,
6860 unsigned long *zone_start_pfn,
6861 unsigned long *zone_end_pfn)
6862{
6863 /* Only adjust if ZONE_MOVABLE is on this node */
6864 if (zone_movable_pfn[nid]) {
6865 /* Size ZONE_MOVABLE */
6866 if (zone_type == ZONE_MOVABLE) {
6867 *zone_start_pfn = zone_movable_pfn[nid];
6868 *zone_end_pfn = min(node_end_pfn,
6869 arch_zone_highest_possible_pfn[movable_zone]);
6870
Xishi Qiue506b992016-10-07 16:58:06 -07006871 /* Adjust for ZONE_MOVABLE starting within this range */
6872 } else if (!mirrored_kernelcore &&
6873 *zone_start_pfn < zone_movable_pfn[nid] &&
6874 *zone_end_pfn > zone_movable_pfn[nid]) {
6875 *zone_end_pfn = zone_movable_pfn[nid];
6876
Mel Gorman2a1e2742007-07-17 04:03:12 -07006877 /* Check if this whole range is within ZONE_MOVABLE */
6878 } else if (*zone_start_pfn >= zone_movable_pfn[nid])
6879 *zone_start_pfn = *zone_end_pfn;
6880 }
6881}
6882
6883/*
Mel Gormanc7132162006-09-27 01:49:43 -07006884 * Return the number of pages a zone spans in a node, including holes
6885 * present_pages = zone_spanned_pages_in_node() - zone_absent_pages_in_node()
6886 */
Oscar Salvadorbbe5d992018-12-28 00:37:24 -08006887static unsigned long __init zone_spanned_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07006888 unsigned long zone_type,
Zhang Yanfei7960aed2013-07-08 15:59:52 -07006889 unsigned long node_start_pfn,
6890 unsigned long node_end_pfn,
Taku Izumid91749c2016-03-15 14:55:18 -07006891 unsigned long *zone_start_pfn,
Mike Rapoport854e8842020-06-03 15:58:13 -07006892 unsigned long *zone_end_pfn)
Mel Gormanc7132162006-09-27 01:49:43 -07006893{
Linxu Fang299c83d2019-05-13 17:19:17 -07006894 unsigned long zone_low = arch_zone_lowest_possible_pfn[zone_type];
6895 unsigned long zone_high = arch_zone_highest_possible_pfn[zone_type];
Xishi Qiub5685e92015-09-08 15:04:16 -07006896 /* When hotadd a new node from cpu_up(), the node should be empty */
Xishi Qiuf9126ab2015-08-14 15:35:16 -07006897 if (!node_start_pfn && !node_end_pfn)
6898 return 0;
6899
Zhang Yanfei7960aed2013-07-08 15:59:52 -07006900 /* Get the start and end of the zone */
Linxu Fang299c83d2019-05-13 17:19:17 -07006901 *zone_start_pfn = clamp(node_start_pfn, zone_low, zone_high);
6902 *zone_end_pfn = clamp(node_end_pfn, zone_low, zone_high);
Mel Gorman2a1e2742007-07-17 04:03:12 -07006903 adjust_zone_range_for_zone_movable(nid, zone_type,
6904 node_start_pfn, node_end_pfn,
Taku Izumid91749c2016-03-15 14:55:18 -07006905 zone_start_pfn, zone_end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07006906
6907 /* Check that this node has pages within the zone's required range */
Taku Izumid91749c2016-03-15 14:55:18 -07006908 if (*zone_end_pfn < node_start_pfn || *zone_start_pfn > node_end_pfn)
Mel Gormanc7132162006-09-27 01:49:43 -07006909 return 0;
6910
6911 /* Move the zone boundaries inside the node if necessary */
Taku Izumid91749c2016-03-15 14:55:18 -07006912 *zone_end_pfn = min(*zone_end_pfn, node_end_pfn);
6913 *zone_start_pfn = max(*zone_start_pfn, node_start_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07006914
6915 /* Return the spanned pages */
Taku Izumid91749c2016-03-15 14:55:18 -07006916 return *zone_end_pfn - *zone_start_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07006917}
6918
6919/*
6920 * Return the number of holes in a range on a node. If nid is MAX_NUMNODES,
Randy Dunlap88ca3b92006-10-04 02:15:25 -07006921 * then all holes in the requested range will be accounted for.
Mel Gormanc7132162006-09-27 01:49:43 -07006922 */
Oscar Salvadorbbe5d992018-12-28 00:37:24 -08006923unsigned long __init __absent_pages_in_range(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07006924 unsigned long range_start_pfn,
6925 unsigned long range_end_pfn)
6926{
Tejun Heo96e907d2011-07-12 10:46:29 +02006927 unsigned long nr_absent = range_end_pfn - range_start_pfn;
6928 unsigned long start_pfn, end_pfn;
6929 int i;
Mel Gormanc7132162006-09-27 01:49:43 -07006930
Tejun Heo96e907d2011-07-12 10:46:29 +02006931 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
6932 start_pfn = clamp(start_pfn, range_start_pfn, range_end_pfn);
6933 end_pfn = clamp(end_pfn, range_start_pfn, range_end_pfn);
6934 nr_absent -= end_pfn - start_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07006935 }
Tejun Heo96e907d2011-07-12 10:46:29 +02006936 return nr_absent;
Mel Gormanc7132162006-09-27 01:49:43 -07006937}
6938
6939/**
6940 * absent_pages_in_range - Return number of page frames in holes within a range
6941 * @start_pfn: The start PFN to start searching for holes
6942 * @end_pfn: The end PFN to stop searching for holes
6943 *
Mike Rapoporta862f682019-03-05 15:48:42 -08006944 * Return: the number of pages frames in memory holes within a range.
Mel Gormanc7132162006-09-27 01:49:43 -07006945 */
6946unsigned long __init absent_pages_in_range(unsigned long start_pfn,
6947 unsigned long end_pfn)
6948{
6949 return __absent_pages_in_range(MAX_NUMNODES, start_pfn, end_pfn);
6950}
6951
6952/* Return the number of page frames in holes in a zone on a node */
Oscar Salvadorbbe5d992018-12-28 00:37:24 -08006953static unsigned long __init zone_absent_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07006954 unsigned long zone_type,
Zhang Yanfei7960aed2013-07-08 15:59:52 -07006955 unsigned long node_start_pfn,
Mike Rapoport854e8842020-06-03 15:58:13 -07006956 unsigned long node_end_pfn)
Mel Gormanc7132162006-09-27 01:49:43 -07006957{
Tejun Heo96e907d2011-07-12 10:46:29 +02006958 unsigned long zone_low = arch_zone_lowest_possible_pfn[zone_type];
6959 unsigned long zone_high = arch_zone_highest_possible_pfn[zone_type];
Mel Gorman9c7cd682006-09-27 01:49:58 -07006960 unsigned long zone_start_pfn, zone_end_pfn;
Taku Izumi342332e2016-03-15 14:55:22 -07006961 unsigned long nr_absent;
Mel Gorman9c7cd682006-09-27 01:49:58 -07006962
Xishi Qiub5685e92015-09-08 15:04:16 -07006963 /* When hotadd a new node from cpu_up(), the node should be empty */
Xishi Qiuf9126ab2015-08-14 15:35:16 -07006964 if (!node_start_pfn && !node_end_pfn)
6965 return 0;
6966
Tejun Heo96e907d2011-07-12 10:46:29 +02006967 zone_start_pfn = clamp(node_start_pfn, zone_low, zone_high);
6968 zone_end_pfn = clamp(node_end_pfn, zone_low, zone_high);
Mel Gorman9c7cd682006-09-27 01:49:58 -07006969
Mel Gorman2a1e2742007-07-17 04:03:12 -07006970 adjust_zone_range_for_zone_movable(nid, zone_type,
6971 node_start_pfn, node_end_pfn,
6972 &zone_start_pfn, &zone_end_pfn);
Taku Izumi342332e2016-03-15 14:55:22 -07006973 nr_absent = __absent_pages_in_range(nid, zone_start_pfn, zone_end_pfn);
6974
6975 /*
6976 * ZONE_MOVABLE handling.
6977 * Treat pages to be ZONE_MOVABLE in ZONE_NORMAL as absent pages
6978 * and vice versa.
6979 */
Xishi Qiue506b992016-10-07 16:58:06 -07006980 if (mirrored_kernelcore && zone_movable_pfn[nid]) {
6981 unsigned long start_pfn, end_pfn;
6982 struct memblock_region *r;
Taku Izumi342332e2016-03-15 14:55:22 -07006983
Mike Rapoportcc6de162020-10-13 16:58:30 -07006984 for_each_mem_region(r) {
Xishi Qiue506b992016-10-07 16:58:06 -07006985 start_pfn = clamp(memblock_region_memory_base_pfn(r),
6986 zone_start_pfn, zone_end_pfn);
6987 end_pfn = clamp(memblock_region_memory_end_pfn(r),
6988 zone_start_pfn, zone_end_pfn);
Taku Izumi342332e2016-03-15 14:55:22 -07006989
Xishi Qiue506b992016-10-07 16:58:06 -07006990 if (zone_type == ZONE_MOVABLE &&
6991 memblock_is_mirror(r))
6992 nr_absent += end_pfn - start_pfn;
Taku Izumi342332e2016-03-15 14:55:22 -07006993
Xishi Qiue506b992016-10-07 16:58:06 -07006994 if (zone_type == ZONE_NORMAL &&
6995 !memblock_is_mirror(r))
6996 nr_absent += end_pfn - start_pfn;
Taku Izumi342332e2016-03-15 14:55:22 -07006997 }
6998 }
6999
7000 return nr_absent;
Mel Gormanc7132162006-09-27 01:49:43 -07007001}
Mel Gorman0e0b8642006-09-27 01:49:56 -07007002
Oscar Salvadorbbe5d992018-12-28 00:37:24 -08007003static void __init calculate_node_totalpages(struct pglist_data *pgdat,
Zhang Yanfei7960aed2013-07-08 15:59:52 -07007004 unsigned long node_start_pfn,
Mike Rapoport854e8842020-06-03 15:58:13 -07007005 unsigned long node_end_pfn)
Mel Gormanc7132162006-09-27 01:49:43 -07007006{
Gu Zhengfebd5942015-06-24 16:57:02 -07007007 unsigned long realtotalpages = 0, totalpages = 0;
Mel Gormanc7132162006-09-27 01:49:43 -07007008 enum zone_type i;
7009
Gu Zhengfebd5942015-06-24 16:57:02 -07007010 for (i = 0; i < MAX_NR_ZONES; i++) {
7011 struct zone *zone = pgdat->node_zones + i;
Taku Izumid91749c2016-03-15 14:55:18 -07007012 unsigned long zone_start_pfn, zone_end_pfn;
Mike Rapoport3f08a302020-06-03 15:57:02 -07007013 unsigned long spanned, absent;
Gu Zhengfebd5942015-06-24 16:57:02 -07007014 unsigned long size, real_size;
Mel Gormanc7132162006-09-27 01:49:43 -07007015
Mike Rapoport854e8842020-06-03 15:58:13 -07007016 spanned = zone_spanned_pages_in_node(pgdat->node_id, i,
7017 node_start_pfn,
7018 node_end_pfn,
7019 &zone_start_pfn,
7020 &zone_end_pfn);
7021 absent = zone_absent_pages_in_node(pgdat->node_id, i,
7022 node_start_pfn,
7023 node_end_pfn);
Mike Rapoport3f08a302020-06-03 15:57:02 -07007024
7025 size = spanned;
7026 real_size = size - absent;
7027
Taku Izumid91749c2016-03-15 14:55:18 -07007028 if (size)
7029 zone->zone_start_pfn = zone_start_pfn;
7030 else
7031 zone->zone_start_pfn = 0;
Gu Zhengfebd5942015-06-24 16:57:02 -07007032 zone->spanned_pages = size;
7033 zone->present_pages = real_size;
7034
7035 totalpages += size;
7036 realtotalpages += real_size;
7037 }
7038
7039 pgdat->node_spanned_pages = totalpages;
Mel Gormanc7132162006-09-27 01:49:43 -07007040 pgdat->node_present_pages = realtotalpages;
7041 printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id,
7042 realtotalpages);
7043}
7044
Mel Gorman835c1342007-10-16 01:25:47 -07007045#ifndef CONFIG_SPARSEMEM
7046/*
7047 * Calculate the size of the zone->blockflags rounded to an unsigned long
Mel Gormand9c23402007-10-16 01:26:01 -07007048 * Start by making sure zonesize is a multiple of pageblock_order by rounding
7049 * up. Then use 1 NR_PAGEBLOCK_BITS worth of bits per pageblock, finally
Mel Gorman835c1342007-10-16 01:25:47 -07007050 * round what is now in bits to nearest long in bits, then return it in
7051 * bytes.
7052 */
Linus Torvalds7c455122013-02-18 09:58:02 -08007053static unsigned long __init usemap_size(unsigned long zone_start_pfn, unsigned long zonesize)
Mel Gorman835c1342007-10-16 01:25:47 -07007054{
7055 unsigned long usemapsize;
7056
Linus Torvalds7c455122013-02-18 09:58:02 -08007057 zonesize += zone_start_pfn & (pageblock_nr_pages-1);
Mel Gormand9c23402007-10-16 01:26:01 -07007058 usemapsize = roundup(zonesize, pageblock_nr_pages);
7059 usemapsize = usemapsize >> pageblock_order;
Mel Gorman835c1342007-10-16 01:25:47 -07007060 usemapsize *= NR_PAGEBLOCK_BITS;
7061 usemapsize = roundup(usemapsize, 8 * sizeof(unsigned long));
7062
7063 return usemapsize / 8;
7064}
7065
Baoquan He7010a6e2021-02-24 12:06:20 -08007066static void __ref setup_usemap(struct zone *zone)
Mel Gorman835c1342007-10-16 01:25:47 -07007067{
Baoquan He7010a6e2021-02-24 12:06:20 -08007068 unsigned long usemapsize = usemap_size(zone->zone_start_pfn,
7069 zone->spanned_pages);
Mel Gorman835c1342007-10-16 01:25:47 -07007070 zone->pageblock_flags = NULL;
Mike Rapoport23a70522019-03-05 15:46:43 -08007071 if (usemapsize) {
Santosh Shilimkar67828322014-01-21 15:50:25 -08007072 zone->pageblock_flags =
Mike Rapoport26fb3da2019-03-11 23:30:42 -07007073 memblock_alloc_node(usemapsize, SMP_CACHE_BYTES,
Baoquan He7010a6e2021-02-24 12:06:20 -08007074 zone_to_nid(zone));
Mike Rapoport23a70522019-03-05 15:46:43 -08007075 if (!zone->pageblock_flags)
7076 panic("Failed to allocate %ld bytes for zone %s pageblock flags on node %d\n",
Baoquan He7010a6e2021-02-24 12:06:20 -08007077 usemapsize, zone->name, zone_to_nid(zone));
Mike Rapoport23a70522019-03-05 15:46:43 -08007078 }
Mel Gorman835c1342007-10-16 01:25:47 -07007079}
7080#else
Baoquan He7010a6e2021-02-24 12:06:20 -08007081static inline void setup_usemap(struct zone *zone) {}
Mel Gorman835c1342007-10-16 01:25:47 -07007082#endif /* CONFIG_SPARSEMEM */
7083
Mel Gormand9c23402007-10-16 01:26:01 -07007084#ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
Mel Gormanba72cb82007-11-28 16:21:13 -08007085
Mel Gormand9c23402007-10-16 01:26:01 -07007086/* Initialise the number of pages represented by NR_PAGEBLOCK_BITS */
Oscar Salvador03e85f92018-08-21 21:53:43 -07007087void __init set_pageblock_order(void)
Mel Gormand9c23402007-10-16 01:26:01 -07007088{
Andrew Morton955c1cd2012-05-29 15:06:31 -07007089 unsigned int order;
7090
Mel Gormand9c23402007-10-16 01:26:01 -07007091 /* Check that pageblock_nr_pages has not already been setup */
7092 if (pageblock_order)
7093 return;
7094
Andrew Morton955c1cd2012-05-29 15:06:31 -07007095 if (HPAGE_SHIFT > PAGE_SHIFT)
7096 order = HUGETLB_PAGE_ORDER;
7097 else
7098 order = MAX_ORDER - 1;
7099
Mel Gormand9c23402007-10-16 01:26:01 -07007100 /*
7101 * Assume the largest contiguous order of interest is a huge page.
Andrew Morton955c1cd2012-05-29 15:06:31 -07007102 * This value may be variable depending on boot parameters on IA64 and
7103 * powerpc.
Mel Gormand9c23402007-10-16 01:26:01 -07007104 */
7105 pageblock_order = order;
7106}
7107#else /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
7108
Mel Gormanba72cb82007-11-28 16:21:13 -08007109/*
7110 * When CONFIG_HUGETLB_PAGE_SIZE_VARIABLE is not set, set_pageblock_order()
Andrew Morton955c1cd2012-05-29 15:06:31 -07007111 * is unused as pageblock_order is set at compile-time. See
7112 * include/linux/pageblock-flags.h for the values of pageblock_order based on
7113 * the kernel config
Mel Gormanba72cb82007-11-28 16:21:13 -08007114 */
Oscar Salvador03e85f92018-08-21 21:53:43 -07007115void __init set_pageblock_order(void)
Mel Gormanba72cb82007-11-28 16:21:13 -08007116{
Mel Gormanba72cb82007-11-28 16:21:13 -08007117}
Mel Gormand9c23402007-10-16 01:26:01 -07007118
7119#endif /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
7120
Oscar Salvador03e85f92018-08-21 21:53:43 -07007121static unsigned long __init calc_memmap_size(unsigned long spanned_pages,
Pavel Tatashin7cc2a952018-08-21 21:53:36 -07007122 unsigned long present_pages)
Jiang Liu01cefae2012-12-12 13:52:19 -08007123{
7124 unsigned long pages = spanned_pages;
7125
7126 /*
7127 * Provide a more accurate estimation if there are holes within
7128 * the zone and SPARSEMEM is in use. If there are holes within the
7129 * zone, each populated memory region may cost us one or two extra
7130 * memmap pages due to alignment because memmap pages for each
Masahiro Yamada89d790a2017-02-27 14:29:01 -08007131 * populated regions may not be naturally aligned on page boundary.
Jiang Liu01cefae2012-12-12 13:52:19 -08007132 * So the (present_pages >> 4) heuristic is a tradeoff for that.
7133 */
7134 if (spanned_pages > present_pages + (present_pages >> 4) &&
7135 IS_ENABLED(CONFIG_SPARSEMEM))
7136 pages = present_pages;
7137
7138 return PAGE_ALIGN(pages * sizeof(struct page)) >> PAGE_SHIFT;
7139}
7140
Oscar Salvadorace1db32018-08-21 21:53:29 -07007141#ifdef CONFIG_TRANSPARENT_HUGEPAGE
7142static void pgdat_init_split_queue(struct pglist_data *pgdat)
7143{
Yang Shi364c1ee2019-09-23 15:38:06 -07007144 struct deferred_split *ds_queue = &pgdat->deferred_split_queue;
7145
7146 spin_lock_init(&ds_queue->split_queue_lock);
7147 INIT_LIST_HEAD(&ds_queue->split_queue);
7148 ds_queue->split_queue_len = 0;
Oscar Salvadorace1db32018-08-21 21:53:29 -07007149}
7150#else
7151static void pgdat_init_split_queue(struct pglist_data *pgdat) {}
7152#endif
7153
7154#ifdef CONFIG_COMPACTION
7155static void pgdat_init_kcompactd(struct pglist_data *pgdat)
7156{
7157 init_waitqueue_head(&pgdat->kcompactd_wait);
7158}
7159#else
7160static void pgdat_init_kcompactd(struct pglist_data *pgdat) {}
7161#endif
7162
Oscar Salvador03e85f92018-08-21 21:53:43 -07007163static void __meminit pgdat_init_internals(struct pglist_data *pgdat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007164{
Dave Hansen208d54e2005-10-29 18:16:52 -07007165 pgdat_resize_init(pgdat);
Oscar Salvadorace1db32018-08-21 21:53:29 -07007166
Oscar Salvadorace1db32018-08-21 21:53:29 -07007167 pgdat_init_split_queue(pgdat);
7168 pgdat_init_kcompactd(pgdat);
7169
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170 init_waitqueue_head(&pgdat->kswapd_wait);
Mel Gorman55150612012-07-31 16:44:35 -07007171 init_waitqueue_head(&pgdat->pfmemalloc_wait);
Oscar Salvadorace1db32018-08-21 21:53:29 -07007172
Joonsoo Kimeefa864b2014-12-12 16:55:46 -08007173 pgdat_page_ext_init(pgdat);
Johannes Weiner867e5e12019-11-30 17:55:34 -08007174 lruvec_init(&pgdat->__lruvec);
Oscar Salvador03e85f92018-08-21 21:53:43 -07007175}
Michal Nazarewicz5f63b722012-01-11 15:16:11 +01007176
Oscar Salvador03e85f92018-08-21 21:53:43 -07007177static void __meminit zone_init_internals(struct zone *zone, enum zone_type idx, int nid,
7178 unsigned long remaining_pages)
7179{
Arun KS9705bea2018-12-28 00:34:24 -08007180 atomic_long_set(&zone->managed_pages, remaining_pages);
Oscar Salvador03e85f92018-08-21 21:53:43 -07007181 zone_set_nid(zone, nid);
7182 zone->name = zone_names[idx];
7183 zone->zone_pgdat = NODE_DATA(nid);
7184 spin_lock_init(&zone->lock);
7185 zone_seqlock_init(zone);
7186 zone_pcp_init(zone);
7187}
7188
7189/*
7190 * Set up the zone data structures
7191 * - init pgdat internals
7192 * - init all zones belonging to this node
7193 *
7194 * NOTE: this function is only called during memory hotplug
7195 */
7196#ifdef CONFIG_MEMORY_HOTPLUG
7197void __ref free_area_init_core_hotplug(int nid)
7198{
7199 enum zone_type z;
7200 pg_data_t *pgdat = NODE_DATA(nid);
7201
7202 pgdat_init_internals(pgdat);
7203 for (z = 0; z < MAX_NR_ZONES; z++)
7204 zone_init_internals(&pgdat->node_zones[z], z, nid, 0);
7205}
7206#endif
7207
7208/*
7209 * Set up the zone data structures:
7210 * - mark all pages reserved
7211 * - mark all memory queues empty
7212 * - clear the memory bitmaps
7213 *
7214 * NOTE: pgdat should get zeroed by caller.
7215 * NOTE: this function is only called during early init.
7216 */
7217static void __init free_area_init_core(struct pglist_data *pgdat)
7218{
7219 enum zone_type j;
7220 int nid = pgdat->node_id;
7221
7222 pgdat_init_internals(pgdat);
Johannes Weiner385386c2017-07-06 15:40:43 -07007223 pgdat->per_cpu_nodestats = &boot_nodestats;
7224
Linus Torvalds1da177e2005-04-16 15:20:36 -07007225 for (j = 0; j < MAX_NR_ZONES; j++) {
7226 struct zone *zone = pgdat->node_zones + j;
Wei Yange6943852018-06-07 17:06:04 -07007227 unsigned long size, freesize, memmap_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007228
Gu Zhengfebd5942015-06-24 16:57:02 -07007229 size = zone->spanned_pages;
Wei Yange6943852018-06-07 17:06:04 -07007230 freesize = zone->present_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231
Mel Gorman0e0b8642006-09-27 01:49:56 -07007232 /*
Jiang Liu9feedc92012-12-12 13:52:12 -08007233 * Adjust freesize so that it accounts for how much memory
Mel Gorman0e0b8642006-09-27 01:49:56 -07007234 * is used by this zone for memmap. This affects the watermark
7235 * and per-cpu initialisations
7236 */
Wei Yange6943852018-06-07 17:06:04 -07007237 memmap_pages = calc_memmap_size(size, freesize);
Zhong Hongboba914f42014-12-12 16:56:21 -08007238 if (!is_highmem_idx(j)) {
7239 if (freesize >= memmap_pages) {
7240 freesize -= memmap_pages;
7241 if (memmap_pages)
7242 printk(KERN_DEBUG
7243 " %s zone: %lu pages used for memmap\n",
7244 zone_names[j], memmap_pages);
7245 } else
Joe Perches11705322016-03-17 14:19:50 -07007246 pr_warn(" %s zone: %lu pages exceeds freesize %lu\n",
Zhong Hongboba914f42014-12-12 16:56:21 -08007247 zone_names[j], memmap_pages, freesize);
7248 }
Mel Gorman0e0b8642006-09-27 01:49:56 -07007249
Christoph Lameter62672762007-02-10 01:43:07 -08007250 /* Account for reserved pages */
Jiang Liu9feedc92012-12-12 13:52:12 -08007251 if (j == 0 && freesize > dma_reserve) {
7252 freesize -= dma_reserve;
Yinghai Lud903ef92008-10-18 20:27:06 -07007253 printk(KERN_DEBUG " %s zone: %lu pages reserved\n",
Christoph Lameter62672762007-02-10 01:43:07 -08007254 zone_names[0], dma_reserve);
Mel Gorman0e0b8642006-09-27 01:49:56 -07007255 }
7256
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07007257 if (!is_highmem_idx(j))
Jiang Liu9feedc92012-12-12 13:52:12 -08007258 nr_kernel_pages += freesize;
Jiang Liu01cefae2012-12-12 13:52:19 -08007259 /* Charge for highmem memmap if there are enough kernel pages */
7260 else if (nr_kernel_pages > memmap_pages * 2)
7261 nr_kernel_pages -= memmap_pages;
Jiang Liu9feedc92012-12-12 13:52:12 -08007262 nr_all_pages += freesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007263
Jiang Liu9feedc92012-12-12 13:52:12 -08007264 /*
7265 * Set an approximate value for lowmem here, it will be adjusted
7266 * when the bootmem allocator frees pages into the buddy system.
7267 * And all highmem pages will be managed by the buddy system.
7268 */
Oscar Salvador03e85f92018-08-21 21:53:43 -07007269 zone_init_internals(zone, j, nid, freesize);
Johannes Weiner81c0a2b2013-09-11 14:20:47 -07007270
Joonsoo Kimd883c6c2018-05-23 10:18:21 +09007271 if (!size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007272 continue;
7273
Andrew Morton955c1cd2012-05-29 15:06:31 -07007274 set_pageblock_order();
Baoquan He7010a6e2021-02-24 12:06:20 -08007275 setup_usemap(zone);
Baoquan He9699ee72021-02-24 12:06:24 -08007276 init_currently_empty_zone(zone, zone->zone_start_pfn, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007277 }
7278}
7279
Oscar Salvador0cd842f2017-11-15 17:39:18 -08007280#ifdef CONFIG_FLAT_NODE_MEM_MAP
Fabian Frederickbd721ea2016-08-02 14:03:33 -07007281static void __ref alloc_node_mem_map(struct pglist_data *pgdat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007282{
Tony Luckb0aeba72015-11-10 10:09:47 -08007283 unsigned long __maybe_unused start = 0;
Laura Abbotta1c34a32015-11-05 18:48:46 -08007284 unsigned long __maybe_unused offset = 0;
7285
Linus Torvalds1da177e2005-04-16 15:20:36 -07007286 /* Skip empty nodes */
7287 if (!pgdat->node_spanned_pages)
7288 return;
7289
Tony Luckb0aeba72015-11-10 10:09:47 -08007290 start = pgdat->node_start_pfn & ~(MAX_ORDER_NR_PAGES - 1);
7291 offset = pgdat->node_start_pfn - start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007292 /* ia64 gets its own node_mem_map, before this, without bootmem */
7293 if (!pgdat->node_mem_map) {
Tony Luckb0aeba72015-11-10 10:09:47 -08007294 unsigned long size, end;
Andy Whitcroftd41dee32005-06-23 00:07:54 -07007295 struct page *map;
7296
Bob Piccoe984bb42006-05-20 15:00:31 -07007297 /*
7298 * The zone's endpoints aren't required to be MAX_ORDER
7299 * aligned but the node_mem_map endpoints must be in order
7300 * for the buddy allocator to function correctly.
7301 */
Cody P Schafer108bcc92013-02-22 16:35:23 -08007302 end = pgdat_end_pfn(pgdat);
Bob Piccoe984bb42006-05-20 15:00:31 -07007303 end = ALIGN(end, MAX_ORDER_NR_PAGES);
7304 size = (end - start) * sizeof(struct page);
Mike Rapoport26fb3da2019-03-11 23:30:42 -07007305 map = memblock_alloc_node(size, SMP_CACHE_BYTES,
7306 pgdat->node_id);
Mike Rapoport23a70522019-03-05 15:46:43 -08007307 if (!map)
7308 panic("Failed to allocate %ld bytes for node %d memory map\n",
7309 size, pgdat->node_id);
Laura Abbotta1c34a32015-11-05 18:48:46 -08007310 pgdat->node_mem_map = map + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007311 }
Oscar Salvador0cd842f2017-11-15 17:39:18 -08007312 pr_debug("%s: node %d, pgdat %08lx, node_mem_map %08lx\n",
7313 __func__, pgdat->node_id, (unsigned long)pgdat,
7314 (unsigned long)pgdat->node_mem_map);
Roman Zippel12d810c2007-05-31 00:40:54 -07007315#ifndef CONFIG_NEED_MULTIPLE_NODES
Linus Torvalds1da177e2005-04-16 15:20:36 -07007316 /*
7317 * With no DISCONTIG, the global mem_map is just set as node 0's
7318 */
Mel Gormanc7132162006-09-27 01:49:43 -07007319 if (pgdat == NODE_DATA(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007320 mem_map = NODE_DATA(0)->node_mem_map;
Mel Gormanc7132162006-09-27 01:49:43 -07007321 if (page_to_pfn(mem_map) != pgdat->node_start_pfn)
Laura Abbotta1c34a32015-11-05 18:48:46 -08007322 mem_map -= offset;
Mel Gormanc7132162006-09-27 01:49:43 -07007323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007324#endif
7325}
Oscar Salvador0cd842f2017-11-15 17:39:18 -08007326#else
7327static void __ref alloc_node_mem_map(struct pglist_data *pgdat) { }
7328#endif /* CONFIG_FLAT_NODE_MEM_MAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007329
Oscar Salvador0188dc92018-08-21 21:53:39 -07007330#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
7331static inline void pgdat_set_deferred_range(pg_data_t *pgdat)
7332{
Oscar Salvador0188dc92018-08-21 21:53:39 -07007333 pgdat->first_deferred_pfn = ULONG_MAX;
7334}
7335#else
7336static inline void pgdat_set_deferred_range(pg_data_t *pgdat) {}
7337#endif
7338
Mike Rapoport854e8842020-06-03 15:58:13 -07007339static void __init free_area_init_node(int nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007340{
Johannes Weiner9109fb72008-07-23 21:27:20 -07007341 pg_data_t *pgdat = NODE_DATA(nid);
Zhang Yanfei7960aed2013-07-08 15:59:52 -07007342 unsigned long start_pfn = 0;
7343 unsigned long end_pfn = 0;
Johannes Weiner9109fb72008-07-23 21:27:20 -07007344
Minchan Kim88fdf752012-07-31 16:46:14 -07007345 /* pg_data_t should be reset to zero when it's allocated */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07007346 WARN_ON(pgdat->nr_zones || pgdat->kswapd_highest_zoneidx);
Minchan Kim88fdf752012-07-31 16:46:14 -07007347
Mike Rapoport854e8842020-06-03 15:58:13 -07007348 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07007349
Linus Torvalds1da177e2005-04-16 15:20:36 -07007350 pgdat->node_id = nid;
Mike Rapoport854e8842020-06-03 15:58:13 -07007351 pgdat->node_start_pfn = start_pfn;
Mel Gorman75ef7182016-07-28 15:45:24 -07007352 pgdat->per_cpu_nodestats = NULL;
Mike Rapoport854e8842020-06-03 15:58:13 -07007353
Juergen Gross8d29e182015-02-11 15:26:01 -08007354 pr_info("Initmem setup node %d [mem %#018Lx-%#018Lx]\n", nid,
Zhen Lei4ada0c52015-09-08 15:04:19 -07007355 (u64)start_pfn << PAGE_SHIFT,
7356 end_pfn ? ((u64)end_pfn << PAGE_SHIFT) - 1 : 0);
Mike Rapoport854e8842020-06-03 15:58:13 -07007357 calculate_node_totalpages(pgdat, start_pfn, end_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007358
7359 alloc_node_mem_map(pgdat);
Oscar Salvador0188dc92018-08-21 21:53:39 -07007360 pgdat_set_deferred_range(pgdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007361
Wei Yang7f3eb552015-09-08 14:59:50 -07007362 free_area_init_core(pgdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007363}
7364
Mike Rapoportbc9331a2020-06-03 15:58:09 -07007365void __init free_area_init_memoryless_node(int nid)
Mike Rapoport3f08a302020-06-03 15:57:02 -07007366{
Mike Rapoport854e8842020-06-03 15:58:13 -07007367 free_area_init_node(nid);
Mike Rapoport3f08a302020-06-03 15:57:02 -07007368}
7369
Miklos Szeredi418508c2007-05-23 13:57:55 -07007370#if MAX_NUMNODES > 1
7371/*
7372 * Figure out the number of possible node ids.
7373 */
Cody P Schaferf9872ca2013-04-29 15:08:01 -07007374void __init setup_nr_node_ids(void)
Miklos Szeredi418508c2007-05-23 13:57:55 -07007375{
Wei Yang904a9552015-09-08 14:59:48 -07007376 unsigned int highest;
Miklos Szeredi418508c2007-05-23 13:57:55 -07007377
Wei Yang904a9552015-09-08 14:59:48 -07007378 highest = find_last_bit(node_possible_map.bits, MAX_NUMNODES);
Miklos Szeredi418508c2007-05-23 13:57:55 -07007379 nr_node_ids = highest + 1;
7380}
Miklos Szeredi418508c2007-05-23 13:57:55 -07007381#endif
7382
Mel Gormanc7132162006-09-27 01:49:43 -07007383/**
Tejun Heo1e019792011-07-12 09:45:34 +02007384 * node_map_pfn_alignment - determine the maximum internode alignment
7385 *
7386 * This function should be called after node map is populated and sorted.
7387 * It calculates the maximum power of two alignment which can distinguish
7388 * all the nodes.
7389 *
7390 * For example, if all nodes are 1GiB and aligned to 1GiB, the return value
7391 * would indicate 1GiB alignment with (1 << (30 - PAGE_SHIFT)). If the
7392 * nodes are shifted by 256MiB, 256MiB. Note that if only the last node is
7393 * shifted, 1GiB is enough and this function will indicate so.
7394 *
7395 * This is used to test whether pfn -> nid mapping of the chosen memory
7396 * model has fine enough granularity to avoid incorrect mapping for the
7397 * populated node map.
7398 *
Mike Rapoporta862f682019-03-05 15:48:42 -08007399 * Return: the determined alignment in pfn's. 0 if there is no alignment
Tejun Heo1e019792011-07-12 09:45:34 +02007400 * requirement (single node).
7401 */
7402unsigned long __init node_map_pfn_alignment(void)
7403{
7404 unsigned long accl_mask = 0, last_end = 0;
Tejun Heoc13291a2011-07-12 10:46:30 +02007405 unsigned long start, end, mask;
Anshuman Khandual98fa15f2019-03-05 15:42:58 -08007406 int last_nid = NUMA_NO_NODE;
Tejun Heoc13291a2011-07-12 10:46:30 +02007407 int i, nid;
Tejun Heo1e019792011-07-12 09:45:34 +02007408
Tejun Heoc13291a2011-07-12 10:46:30 +02007409 for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, &nid) {
Tejun Heo1e019792011-07-12 09:45:34 +02007410 if (!start || last_nid < 0 || last_nid == nid) {
7411 last_nid = nid;
7412 last_end = end;
7413 continue;
7414 }
7415
7416 /*
7417 * Start with a mask granular enough to pin-point to the
7418 * start pfn and tick off bits one-by-one until it becomes
7419 * too coarse to separate the current node from the last.
7420 */
7421 mask = ~((1 << __ffs(start)) - 1);
7422 while (mask && last_end <= (start & (mask << 1)))
7423 mask <<= 1;
7424
7425 /* accumulate all internode masks */
7426 accl_mask |= mask;
7427 }
7428
7429 /* convert mask to number of pages */
7430 return ~accl_mask + 1;
7431}
7432
Mel Gormanc7132162006-09-27 01:49:43 -07007433/**
7434 * find_min_pfn_with_active_regions - Find the minimum PFN registered
7435 *
Mike Rapoporta862f682019-03-05 15:48:42 -08007436 * Return: the minimum PFN based on information provided via
Zhang Zhen7d018172014-06-04 16:10:53 -07007437 * memblock_set_node().
Mel Gormanc7132162006-09-27 01:49:43 -07007438 */
7439unsigned long __init find_min_pfn_with_active_regions(void)
7440{
Mike Rapoport8a1b25f2020-06-03 15:58:18 -07007441 return PHYS_PFN(memblock_start_of_DRAM());
Mel Gormanc7132162006-09-27 01:49:43 -07007442}
7443
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07007444/*
7445 * early_calculate_totalpages()
7446 * Sum pages in active regions for movable zone.
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08007447 * Populate N_MEMORY for calculating usable_nodes.
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07007448 */
Adrian Bunk484f51f2007-10-16 01:26:03 -07007449static unsigned long __init early_calculate_totalpages(void)
Mel Gorman7e63efef2007-07-17 04:03:15 -07007450{
Mel Gorman7e63efef2007-07-17 04:03:15 -07007451 unsigned long totalpages = 0;
Tejun Heoc13291a2011-07-12 10:46:30 +02007452 unsigned long start_pfn, end_pfn;
7453 int i, nid;
Mel Gorman7e63efef2007-07-17 04:03:15 -07007454
Tejun Heoc13291a2011-07-12 10:46:30 +02007455 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) {
7456 unsigned long pages = end_pfn - start_pfn;
7457
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07007458 totalpages += pages;
7459 if (pages)
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08007460 node_set_state(nid, N_MEMORY);
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07007461 }
Pintu Kumarb8af2942013-09-11 14:20:34 -07007462 return totalpages;
Mel Gorman7e63efef2007-07-17 04:03:15 -07007463}
7464
Mel Gorman2a1e2742007-07-17 04:03:12 -07007465/*
7466 * Find the PFN the Movable zone begins in each node. Kernel memory
7467 * is spread evenly between nodes as long as the nodes have enough
7468 * memory. When they don't, some nodes will have more kernelcore than
7469 * others
7470 */
Kautuk Consulb224ef82012-03-21 16:34:15 -07007471static void __init find_zone_movable_pfns_for_nodes(void)
Mel Gorman2a1e2742007-07-17 04:03:12 -07007472{
7473 int i, nid;
7474 unsigned long usable_startpfn;
7475 unsigned long kernelcore_node, kernelcore_remaining;
Yinghai Lu66918dc2009-06-30 11:41:37 -07007476 /* save the state before borrow the nodemask */
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08007477 nodemask_t saved_node_state = node_states[N_MEMORY];
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07007478 unsigned long totalpages = early_calculate_totalpages();
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08007479 int usable_nodes = nodes_weight(node_states[N_MEMORY]);
Emil Medve136199f2014-04-07 15:37:52 -07007480 struct memblock_region *r;
Tang Chenb2f3eeb2014-01-21 15:49:38 -08007481
7482 /* Need to find movable_zone earlier when movable_node is specified. */
7483 find_usable_zone_for_movable();
Mel Gorman2a1e2742007-07-17 04:03:12 -07007484
Mel Gorman7e63efef2007-07-17 04:03:15 -07007485 /*
Tang Chenb2f3eeb2014-01-21 15:49:38 -08007486 * If movable_node is specified, ignore kernelcore and movablecore
7487 * options.
7488 */
7489 if (movable_node_is_enabled()) {
Mike Rapoportcc6de162020-10-13 16:58:30 -07007490 for_each_mem_region(r) {
Emil Medve136199f2014-04-07 15:37:52 -07007491 if (!memblock_is_hotpluggable(r))
Tang Chenb2f3eeb2014-01-21 15:49:38 -08007492 continue;
7493
Mike Rapoportd622abf2020-06-03 15:56:53 -07007494 nid = memblock_get_region_node(r);
Tang Chenb2f3eeb2014-01-21 15:49:38 -08007495
Emil Medve136199f2014-04-07 15:37:52 -07007496 usable_startpfn = PFN_DOWN(r->base);
Tang Chenb2f3eeb2014-01-21 15:49:38 -08007497 zone_movable_pfn[nid] = zone_movable_pfn[nid] ?
7498 min(usable_startpfn, zone_movable_pfn[nid]) :
7499 usable_startpfn;
7500 }
7501
7502 goto out2;
7503 }
7504
7505 /*
Taku Izumi342332e2016-03-15 14:55:22 -07007506 * If kernelcore=mirror is specified, ignore movablecore option
7507 */
7508 if (mirrored_kernelcore) {
7509 bool mem_below_4gb_not_mirrored = false;
7510
Mike Rapoportcc6de162020-10-13 16:58:30 -07007511 for_each_mem_region(r) {
Taku Izumi342332e2016-03-15 14:55:22 -07007512 if (memblock_is_mirror(r))
7513 continue;
7514
Mike Rapoportd622abf2020-06-03 15:56:53 -07007515 nid = memblock_get_region_node(r);
Taku Izumi342332e2016-03-15 14:55:22 -07007516
7517 usable_startpfn = memblock_region_memory_base_pfn(r);
7518
7519 if (usable_startpfn < 0x100000) {
7520 mem_below_4gb_not_mirrored = true;
7521 continue;
7522 }
7523
7524 zone_movable_pfn[nid] = zone_movable_pfn[nid] ?
7525 min(usable_startpfn, zone_movable_pfn[nid]) :
7526 usable_startpfn;
7527 }
7528
7529 if (mem_below_4gb_not_mirrored)
Chen Tao633bf2f2020-06-03 16:00:02 -07007530 pr_warn("This configuration results in unmirrored kernel memory.\n");
Taku Izumi342332e2016-03-15 14:55:22 -07007531
7532 goto out2;
7533 }
7534
7535 /*
David Rientjesa5c6d652018-04-05 16:23:09 -07007536 * If kernelcore=nn% or movablecore=nn% was specified, calculate the
7537 * amount of necessary memory.
7538 */
7539 if (required_kernelcore_percent)
7540 required_kernelcore = (totalpages * 100 * required_kernelcore_percent) /
7541 10000UL;
7542 if (required_movablecore_percent)
7543 required_movablecore = (totalpages * 100 * required_movablecore_percent) /
7544 10000UL;
7545
7546 /*
7547 * If movablecore= was specified, calculate what size of
Mel Gorman7e63efef2007-07-17 04:03:15 -07007548 * kernelcore that corresponds so that memory usable for
7549 * any allocation type is evenly spread. If both kernelcore
7550 * and movablecore are specified, then the value of kernelcore
7551 * will be used for required_kernelcore if it's greater than
7552 * what movablecore would have allowed.
7553 */
7554 if (required_movablecore) {
Mel Gorman7e63efef2007-07-17 04:03:15 -07007555 unsigned long corepages;
7556
7557 /*
7558 * Round-up so that ZONE_MOVABLE is at least as large as what
7559 * was requested by the user
7560 */
7561 required_movablecore =
7562 roundup(required_movablecore, MAX_ORDER_NR_PAGES);
Xishi Qiu9fd745d2015-11-05 18:48:11 -08007563 required_movablecore = min(totalpages, required_movablecore);
Mel Gorman7e63efef2007-07-17 04:03:15 -07007564 corepages = totalpages - required_movablecore;
7565
7566 required_kernelcore = max(required_kernelcore, corepages);
7567 }
7568
Xishi Qiubde304b2015-11-05 18:48:56 -08007569 /*
7570 * If kernelcore was not specified or kernelcore size is larger
7571 * than totalpages, there is no ZONE_MOVABLE.
7572 */
7573 if (!required_kernelcore || required_kernelcore >= totalpages)
Yinghai Lu66918dc2009-06-30 11:41:37 -07007574 goto out;
Mel Gorman2a1e2742007-07-17 04:03:12 -07007575
7576 /* usable_startpfn is the lowest possible pfn ZONE_MOVABLE can be at */
Mel Gorman2a1e2742007-07-17 04:03:12 -07007577 usable_startpfn = arch_zone_lowest_possible_pfn[movable_zone];
7578
7579restart:
7580 /* Spread kernelcore memory as evenly as possible throughout nodes */
7581 kernelcore_node = required_kernelcore / usable_nodes;
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08007582 for_each_node_state(nid, N_MEMORY) {
Tejun Heoc13291a2011-07-12 10:46:30 +02007583 unsigned long start_pfn, end_pfn;
7584
Mel Gorman2a1e2742007-07-17 04:03:12 -07007585 /*
7586 * Recalculate kernelcore_node if the division per node
7587 * now exceeds what is necessary to satisfy the requested
7588 * amount of memory for the kernel
7589 */
7590 if (required_kernelcore < kernelcore_node)
7591 kernelcore_node = required_kernelcore / usable_nodes;
7592
7593 /*
7594 * As the map is walked, we track how much memory is usable
7595 * by the kernel using kernelcore_remaining. When it is
7596 * 0, the rest of the node is usable by ZONE_MOVABLE
7597 */
7598 kernelcore_remaining = kernelcore_node;
7599
7600 /* Go through each range of PFNs within this node */
Tejun Heoc13291a2011-07-12 10:46:30 +02007601 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
Mel Gorman2a1e2742007-07-17 04:03:12 -07007602 unsigned long size_pages;
7603
Tejun Heoc13291a2011-07-12 10:46:30 +02007604 start_pfn = max(start_pfn, zone_movable_pfn[nid]);
Mel Gorman2a1e2742007-07-17 04:03:12 -07007605 if (start_pfn >= end_pfn)
7606 continue;
7607
7608 /* Account for what is only usable for kernelcore */
7609 if (start_pfn < usable_startpfn) {
7610 unsigned long kernel_pages;
7611 kernel_pages = min(end_pfn, usable_startpfn)
7612 - start_pfn;
7613
7614 kernelcore_remaining -= min(kernel_pages,
7615 kernelcore_remaining);
7616 required_kernelcore -= min(kernel_pages,
7617 required_kernelcore);
7618
7619 /* Continue if range is now fully accounted */
7620 if (end_pfn <= usable_startpfn) {
7621
7622 /*
7623 * Push zone_movable_pfn to the end so
7624 * that if we have to rebalance
7625 * kernelcore across nodes, we will
7626 * not double account here
7627 */
7628 zone_movable_pfn[nid] = end_pfn;
7629 continue;
7630 }
7631 start_pfn = usable_startpfn;
7632 }
7633
7634 /*
7635 * The usable PFN range for ZONE_MOVABLE is from
7636 * start_pfn->end_pfn. Calculate size_pages as the
7637 * number of pages used as kernelcore
7638 */
7639 size_pages = end_pfn - start_pfn;
7640 if (size_pages > kernelcore_remaining)
7641 size_pages = kernelcore_remaining;
7642 zone_movable_pfn[nid] = start_pfn + size_pages;
7643
7644 /*
7645 * Some kernelcore has been met, update counts and
7646 * break if the kernelcore for this node has been
Pintu Kumarb8af2942013-09-11 14:20:34 -07007647 * satisfied
Mel Gorman2a1e2742007-07-17 04:03:12 -07007648 */
7649 required_kernelcore -= min(required_kernelcore,
7650 size_pages);
7651 kernelcore_remaining -= size_pages;
7652 if (!kernelcore_remaining)
7653 break;
7654 }
7655 }
7656
7657 /*
7658 * If there is still required_kernelcore, we do another pass with one
7659 * less node in the count. This will push zone_movable_pfn[nid] further
7660 * along on the nodes that still have memory until kernelcore is
Pintu Kumarb8af2942013-09-11 14:20:34 -07007661 * satisfied
Mel Gorman2a1e2742007-07-17 04:03:12 -07007662 */
7663 usable_nodes--;
7664 if (usable_nodes && required_kernelcore > usable_nodes)
7665 goto restart;
7666
Tang Chenb2f3eeb2014-01-21 15:49:38 -08007667out2:
Mel Gorman2a1e2742007-07-17 04:03:12 -07007668 /* Align start of ZONE_MOVABLE on all nids to MAX_ORDER_NR_PAGES */
7669 for (nid = 0; nid < MAX_NUMNODES; nid++)
7670 zone_movable_pfn[nid] =
7671 roundup(zone_movable_pfn[nid], MAX_ORDER_NR_PAGES);
Yinghai Lu66918dc2009-06-30 11:41:37 -07007672
Yinghai Lu20e69262013-03-01 14:51:27 -08007673out:
Yinghai Lu66918dc2009-06-30 11:41:37 -07007674 /* restore the node_state */
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08007675 node_states[N_MEMORY] = saved_node_state;
Mel Gorman2a1e2742007-07-17 04:03:12 -07007676}
7677
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08007678/* Any regular or high memory on that node ? */
7679static void check_for_memory(pg_data_t *pgdat, int nid)
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07007680{
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07007681 enum zone_type zone_type;
7682
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08007683 for (zone_type = 0; zone_type <= ZONE_MOVABLE - 1; zone_type++) {
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07007684 struct zone *zone = &pgdat->node_zones[zone_type];
Xishi Qiub38a8722013-11-12 15:07:20 -08007685 if (populated_zone(zone)) {
Oscar Salvador7b0e0c02018-10-26 15:03:58 -07007686 if (IS_ENABLED(CONFIG_HIGHMEM))
7687 node_set_state(nid, N_HIGH_MEMORY);
7688 if (zone_type <= ZONE_NORMAL)
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08007689 node_set_state(nid, N_NORMAL_MEMORY);
Bob Liud0048b02012-01-12 17:19:07 -08007690 break;
7691 }
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07007692 }
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07007693}
7694
Mike Rapoport51930df2020-06-03 15:58:03 -07007695/*
Ingo Molnarf0953a12021-05-06 18:06:47 -07007696 * Some architectures, e.g. ARC may have ZONE_HIGHMEM below ZONE_NORMAL. For
Mike Rapoport51930df2020-06-03 15:58:03 -07007697 * such cases we allow max_zone_pfn sorted in the descending order
7698 */
7699bool __weak arch_has_descending_max_zone_pfns(void)
7700{
7701 return false;
7702}
7703
Mel Gormanc7132162006-09-27 01:49:43 -07007704/**
Mike Rapoport9691a072020-06-03 15:57:10 -07007705 * free_area_init - Initialise all pg_data_t and zone data
Randy Dunlap88ca3b92006-10-04 02:15:25 -07007706 * @max_zone_pfn: an array of max PFNs for each zone
Mel Gormanc7132162006-09-27 01:49:43 -07007707 *
7708 * This will call free_area_init_node() for each active node in the system.
Zhang Zhen7d018172014-06-04 16:10:53 -07007709 * Using the page ranges provided by memblock_set_node(), the size of each
Mel Gormanc7132162006-09-27 01:49:43 -07007710 * zone in each node and their holes is calculated. If the maximum PFN
7711 * between two adjacent zones match, it is assumed that the zone is empty.
7712 * For example, if arch_max_dma_pfn == arch_max_dma32_pfn, it is assumed
7713 * that arch_max_dma32_pfn has no pages. It is also assumed that a zone
7714 * starts where the previous one ended. For example, ZONE_DMA32 starts
7715 * at arch_max_dma_pfn.
7716 */
Mike Rapoport9691a072020-06-03 15:57:10 -07007717void __init free_area_init(unsigned long *max_zone_pfn)
Mel Gormanc7132162006-09-27 01:49:43 -07007718{
Tejun Heoc13291a2011-07-12 10:46:30 +02007719 unsigned long start_pfn, end_pfn;
Mike Rapoport51930df2020-06-03 15:58:03 -07007720 int i, nid, zone;
7721 bool descending;
Mel Gormana6af2bc2007-02-10 01:42:57 -08007722
Mel Gormanc7132162006-09-27 01:49:43 -07007723 /* Record where the zone boundaries are */
7724 memset(arch_zone_lowest_possible_pfn, 0,
7725 sizeof(arch_zone_lowest_possible_pfn));
7726 memset(arch_zone_highest_possible_pfn, 0,
7727 sizeof(arch_zone_highest_possible_pfn));
Oliver O'Halloran90cae1f2016-07-26 15:22:17 -07007728
7729 start_pfn = find_min_pfn_with_active_regions();
Mike Rapoport51930df2020-06-03 15:58:03 -07007730 descending = arch_has_descending_max_zone_pfns();
Oliver O'Halloran90cae1f2016-07-26 15:22:17 -07007731
7732 for (i = 0; i < MAX_NR_ZONES; i++) {
Mike Rapoport51930df2020-06-03 15:58:03 -07007733 if (descending)
7734 zone = MAX_NR_ZONES - i - 1;
7735 else
7736 zone = i;
7737
7738 if (zone == ZONE_MOVABLE)
Mel Gorman2a1e2742007-07-17 04:03:12 -07007739 continue;
Oliver O'Halloran90cae1f2016-07-26 15:22:17 -07007740
Mike Rapoport51930df2020-06-03 15:58:03 -07007741 end_pfn = max(max_zone_pfn[zone], start_pfn);
7742 arch_zone_lowest_possible_pfn[zone] = start_pfn;
7743 arch_zone_highest_possible_pfn[zone] = end_pfn;
Oliver O'Halloran90cae1f2016-07-26 15:22:17 -07007744
7745 start_pfn = end_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07007746 }
Mel Gorman2a1e2742007-07-17 04:03:12 -07007747
7748 /* Find the PFNs that ZONE_MOVABLE begins at in each node */
7749 memset(zone_movable_pfn, 0, sizeof(zone_movable_pfn));
Kautuk Consulb224ef82012-03-21 16:34:15 -07007750 find_zone_movable_pfns_for_nodes();
Mel Gormanc7132162006-09-27 01:49:43 -07007751
Mel Gormanc7132162006-09-27 01:49:43 -07007752 /* Print out the zone ranges */
Anton Blanchardf88dfff2014-12-10 15:42:53 -08007753 pr_info("Zone ranges:\n");
Mel Gorman2a1e2742007-07-17 04:03:12 -07007754 for (i = 0; i < MAX_NR_ZONES; i++) {
7755 if (i == ZONE_MOVABLE)
7756 continue;
Anton Blanchardf88dfff2014-12-10 15:42:53 -08007757 pr_info(" %-8s ", zone_names[i]);
David Rientjes72f0ba02010-03-05 13:42:14 -08007758 if (arch_zone_lowest_possible_pfn[i] ==
7759 arch_zone_highest_possible_pfn[i])
Anton Blanchardf88dfff2014-12-10 15:42:53 -08007760 pr_cont("empty\n");
David Rientjes72f0ba02010-03-05 13:42:14 -08007761 else
Juergen Gross8d29e182015-02-11 15:26:01 -08007762 pr_cont("[mem %#018Lx-%#018Lx]\n",
7763 (u64)arch_zone_lowest_possible_pfn[i]
7764 << PAGE_SHIFT,
7765 ((u64)arch_zone_highest_possible_pfn[i]
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07007766 << PAGE_SHIFT) - 1);
Mel Gorman2a1e2742007-07-17 04:03:12 -07007767 }
7768
7769 /* Print out the PFNs ZONE_MOVABLE begins at in each node */
Anton Blanchardf88dfff2014-12-10 15:42:53 -08007770 pr_info("Movable zone start for each node\n");
Mel Gorman2a1e2742007-07-17 04:03:12 -07007771 for (i = 0; i < MAX_NUMNODES; i++) {
7772 if (zone_movable_pfn[i])
Juergen Gross8d29e182015-02-11 15:26:01 -08007773 pr_info(" Node %d: %#018Lx\n", i,
7774 (u64)zone_movable_pfn[i] << PAGE_SHIFT);
Mel Gorman2a1e2742007-07-17 04:03:12 -07007775 }
Mel Gormanc7132162006-09-27 01:49:43 -07007776
Dan Williamsf46edbd2019-07-18 15:58:04 -07007777 /*
7778 * Print out the early node map, and initialize the
7779 * subsection-map relative to active online memory ranges to
7780 * enable future "sub-section" extensions of the memory map.
7781 */
Anton Blanchardf88dfff2014-12-10 15:42:53 -08007782 pr_info("Early memory node ranges\n");
Dan Williamsf46edbd2019-07-18 15:58:04 -07007783 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) {
Juergen Gross8d29e182015-02-11 15:26:01 -08007784 pr_info(" node %3d: [mem %#018Lx-%#018Lx]\n", nid,
7785 (u64)start_pfn << PAGE_SHIFT,
7786 ((u64)end_pfn << PAGE_SHIFT) - 1);
Dan Williamsf46edbd2019-07-18 15:58:04 -07007787 subsection_map_init(start_pfn, end_pfn - start_pfn);
7788 }
Mel Gormanc7132162006-09-27 01:49:43 -07007789
7790 /* Initialise every node */
Mel Gorman708614e2008-07-23 21:26:51 -07007791 mminit_verify_pageflags_layout();
Christoph Lameter8ef82862007-02-20 13:57:52 -08007792 setup_nr_node_ids();
Mel Gormanc7132162006-09-27 01:49:43 -07007793 for_each_online_node(nid) {
7794 pg_data_t *pgdat = NODE_DATA(nid);
Mike Rapoport854e8842020-06-03 15:58:13 -07007795 free_area_init_node(nid);
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07007796
7797 /* Any memory on that node */
7798 if (pgdat->node_present_pages)
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08007799 node_set_state(nid, N_MEMORY);
7800 check_for_memory(pgdat, nid);
Mel Gormanc7132162006-09-27 01:49:43 -07007801 }
Mike Rapoport122e0932021-06-28 19:33:26 -07007802
7803 memmap_init();
Mel Gormanc7132162006-09-27 01:49:43 -07007804}
Mel Gorman2a1e2742007-07-17 04:03:12 -07007805
David Rientjesa5c6d652018-04-05 16:23:09 -07007806static int __init cmdline_parse_core(char *p, unsigned long *core,
7807 unsigned long *percent)
Mel Gorman2a1e2742007-07-17 04:03:12 -07007808{
7809 unsigned long long coremem;
David Rientjesa5c6d652018-04-05 16:23:09 -07007810 char *endptr;
7811
Mel Gorman2a1e2742007-07-17 04:03:12 -07007812 if (!p)
7813 return -EINVAL;
7814
David Rientjesa5c6d652018-04-05 16:23:09 -07007815 /* Value may be a percentage of total memory, otherwise bytes */
7816 coremem = simple_strtoull(p, &endptr, 0);
7817 if (*endptr == '%') {
7818 /* Paranoid check for percent values greater than 100 */
7819 WARN_ON(coremem > 100);
Mel Gorman2a1e2742007-07-17 04:03:12 -07007820
David Rientjesa5c6d652018-04-05 16:23:09 -07007821 *percent = coremem;
7822 } else {
7823 coremem = memparse(p, &p);
7824 /* Paranoid check that UL is enough for the coremem value */
7825 WARN_ON((coremem >> PAGE_SHIFT) > ULONG_MAX);
Mel Gorman2a1e2742007-07-17 04:03:12 -07007826
David Rientjesa5c6d652018-04-05 16:23:09 -07007827 *core = coremem >> PAGE_SHIFT;
7828 *percent = 0UL;
7829 }
Mel Gorman2a1e2742007-07-17 04:03:12 -07007830 return 0;
7831}
Mel Gormaned7ed362007-07-17 04:03:14 -07007832
Mel Gorman7e63efef2007-07-17 04:03:15 -07007833/*
7834 * kernelcore=size sets the amount of memory for use for allocations that
7835 * cannot be reclaimed or migrated.
7836 */
7837static int __init cmdline_parse_kernelcore(char *p)
7838{
Taku Izumi342332e2016-03-15 14:55:22 -07007839 /* parse kernelcore=mirror */
7840 if (parse_option_str(p, "mirror")) {
7841 mirrored_kernelcore = true;
7842 return 0;
7843 }
7844
David Rientjesa5c6d652018-04-05 16:23:09 -07007845 return cmdline_parse_core(p, &required_kernelcore,
7846 &required_kernelcore_percent);
Mel Gorman7e63efef2007-07-17 04:03:15 -07007847}
7848
7849/*
7850 * movablecore=size sets the amount of memory for use for allocations that
7851 * can be reclaimed or migrated.
7852 */
7853static int __init cmdline_parse_movablecore(char *p)
7854{
David Rientjesa5c6d652018-04-05 16:23:09 -07007855 return cmdline_parse_core(p, &required_movablecore,
7856 &required_movablecore_percent);
Mel Gorman7e63efef2007-07-17 04:03:15 -07007857}
7858
Mel Gormaned7ed362007-07-17 04:03:14 -07007859early_param("kernelcore", cmdline_parse_kernelcore);
Mel Gorman7e63efef2007-07-17 04:03:15 -07007860early_param("movablecore", cmdline_parse_movablecore);
Mel Gormaned7ed362007-07-17 04:03:14 -07007861
Jiang Liuc3d5f5f2013-07-03 15:03:14 -07007862void adjust_managed_page_count(struct page *page, long count)
7863{
Arun KS9705bea2018-12-28 00:34:24 -08007864 atomic_long_add(count, &page_zone(page)->managed_pages);
Arun KSca79b0c2018-12-28 00:34:29 -08007865 totalram_pages_add(count);
Jiang Liu3dcc0572013-07-03 15:03:21 -07007866#ifdef CONFIG_HIGHMEM
7867 if (PageHighMem(page))
Arun KSca79b0c2018-12-28 00:34:29 -08007868 totalhigh_pages_add(count);
Jiang Liu3dcc0572013-07-03 15:03:21 -07007869#endif
Jiang Liuc3d5f5f2013-07-03 15:03:14 -07007870}
Jiang Liu3dcc0572013-07-03 15:03:21 -07007871EXPORT_SYMBOL(adjust_managed_page_count);
Jiang Liuc3d5f5f2013-07-03 15:03:14 -07007872
Alexey Dobriyane5cb1132018-12-28 00:36:03 -08007873unsigned long free_reserved_area(void *start, void *end, int poison, const char *s)
Jiang Liu69afade2013-04-29 15:06:21 -07007874{
Jiang Liu11199692013-07-03 15:02:48 -07007875 void *pos;
7876 unsigned long pages = 0;
Jiang Liu69afade2013-04-29 15:06:21 -07007877
Jiang Liu11199692013-07-03 15:02:48 -07007878 start = (void *)PAGE_ALIGN((unsigned long)start);
7879 end = (void *)((unsigned long)end & PAGE_MASK);
7880 for (pos = start; pos < end; pos += PAGE_SIZE, pages++) {
Dave Hansen0d834322018-08-02 15:58:26 -07007881 struct page *page = virt_to_page(pos);
7882 void *direct_map_addr;
7883
7884 /*
7885 * 'direct_map_addr' might be different from 'pos'
7886 * because some architectures' virt_to_page()
7887 * work with aliases. Getting the direct map
7888 * address ensures that we get a _writeable_
7889 * alias for the memset().
7890 */
7891 direct_map_addr = page_address(page);
Vincenzo Frascinoc7461702020-12-22 12:01:49 -08007892 /*
7893 * Perform a kasan-unchecked memset() since this memory
7894 * has not been initialized.
7895 */
7896 direct_map_addr = kasan_reset_tag(direct_map_addr);
Jiang Liudbe67df2013-07-03 15:02:51 -07007897 if ((unsigned int)poison <= 0xFF)
Dave Hansen0d834322018-08-02 15:58:26 -07007898 memset(direct_map_addr, poison, PAGE_SIZE);
7899
7900 free_reserved_page(page);
Jiang Liu69afade2013-04-29 15:06:21 -07007901 }
7902
7903 if (pages && s)
Josh Poimboeufadb1fe92016-10-25 09:51:14 -05007904 pr_info("Freeing %s memory: %ldK\n",
7905 s, pages << (PAGE_SHIFT - 10));
Jiang Liu69afade2013-04-29 15:06:21 -07007906
7907 return pages;
7908}
7909
Kefeng Wang1f9d03c2021-04-29 23:00:55 -07007910void __init mem_init_print_info(void)
Jiang Liu7ee3d4e2013-07-03 15:03:41 -07007911{
7912 unsigned long physpages, codesize, datasize, rosize, bss_size;
7913 unsigned long init_code_size, init_data_size;
7914
7915 physpages = get_num_physpages();
7916 codesize = _etext - _stext;
7917 datasize = _edata - _sdata;
7918 rosize = __end_rodata - __start_rodata;
7919 bss_size = __bss_stop - __bss_start;
7920 init_data_size = __init_end - __init_begin;
7921 init_code_size = _einittext - _sinittext;
7922
7923 /*
7924 * Detect special cases and adjust section sizes accordingly:
7925 * 1) .init.* may be embedded into .data sections
7926 * 2) .init.text.* may be out of [__init_begin, __init_end],
7927 * please refer to arch/tile/kernel/vmlinux.lds.S.
7928 * 3) .rodata.* may be embedded into .text or .data sections.
7929 */
7930#define adj_init_size(start, end, size, pos, adj) \
Pintu Kumarb8af2942013-09-11 14:20:34 -07007931 do { \
7932 if (start <= pos && pos < end && size > adj) \
7933 size -= adj; \
7934 } while (0)
Jiang Liu7ee3d4e2013-07-03 15:03:41 -07007935
7936 adj_init_size(__init_begin, __init_end, init_data_size,
7937 _sinittext, init_code_size);
7938 adj_init_size(_stext, _etext, codesize, _sinittext, init_code_size);
7939 adj_init_size(_sdata, _edata, datasize, __init_begin, init_data_size);
7940 adj_init_size(_stext, _etext, codesize, __start_rodata, rosize);
7941 adj_init_size(_sdata, _edata, datasize, __start_rodata, rosize);
7942
7943#undef adj_init_size
7944
Joe Perches756a0252016-03-17 14:19:47 -07007945 pr_info("Memory: %luK/%luK available (%luK kernel code, %luK rwdata, %luK rodata, %luK init, %luK bss, %luK reserved, %luK cma-reserved"
Jiang Liu7ee3d4e2013-07-03 15:03:41 -07007946#ifdef CONFIG_HIGHMEM
Joe Perches756a0252016-03-17 14:19:47 -07007947 ", %luK highmem"
Jiang Liu7ee3d4e2013-07-03 15:03:41 -07007948#endif
Kefeng Wang1f9d03c2021-04-29 23:00:55 -07007949 ")\n",
Joe Perches756a0252016-03-17 14:19:47 -07007950 nr_free_pages() << (PAGE_SHIFT - 10),
7951 physpages << (PAGE_SHIFT - 10),
7952 codesize >> 10, datasize >> 10, rosize >> 10,
7953 (init_data_size + init_code_size) >> 10, bss_size >> 10,
Arun KSca79b0c2018-12-28 00:34:29 -08007954 (physpages - totalram_pages() - totalcma_pages) << (PAGE_SHIFT - 10),
Kefeng Wang1f9d03c2021-04-29 23:00:55 -07007955 totalcma_pages << (PAGE_SHIFT - 10)
Jiang Liu7ee3d4e2013-07-03 15:03:41 -07007956#ifdef CONFIG_HIGHMEM
Kefeng Wang1f9d03c2021-04-29 23:00:55 -07007957 , totalhigh_pages() << (PAGE_SHIFT - 10)
Jiang Liu7ee3d4e2013-07-03 15:03:41 -07007958#endif
Kefeng Wang1f9d03c2021-04-29 23:00:55 -07007959 );
Jiang Liu7ee3d4e2013-07-03 15:03:41 -07007960}
7961
Mel Gorman0e0b8642006-09-27 01:49:56 -07007962/**
Randy Dunlap88ca3b92006-10-04 02:15:25 -07007963 * set_dma_reserve - set the specified number of pages reserved in the first zone
7964 * @new_dma_reserve: The number of pages to mark reserved
Mel Gorman0e0b8642006-09-27 01:49:56 -07007965 *
Yaowei Bai013110a2015-09-08 15:04:10 -07007966 * The per-cpu batchsize and zone watermarks are determined by managed_pages.
Mel Gorman0e0b8642006-09-27 01:49:56 -07007967 * In the DMA zone, a significant percentage may be consumed by kernel image
7968 * and other unfreeable allocations which can skew the watermarks badly. This
Randy Dunlap88ca3b92006-10-04 02:15:25 -07007969 * function may optionally be used to account for unfreeable pages in the
7970 * first zone (e.g., ZONE_DMA). The effect will be lower watermarks and
7971 * smaller per-cpu batchsize.
Mel Gorman0e0b8642006-09-27 01:49:56 -07007972 */
7973void __init set_dma_reserve(unsigned long new_dma_reserve)
7974{
7975 dma_reserve = new_dma_reserve;
7976}
7977
Sebastian Andrzej Siewior005fd4b2016-11-03 15:50:02 +01007978static int page_alloc_cpu_dead(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007979{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007980
Sebastian Andrzej Siewior005fd4b2016-11-03 15:50:02 +01007981 lru_add_drain_cpu(cpu);
7982 drain_pages(cpu);
Christoph Lameter9f8f2172008-02-04 22:29:11 -08007983
Sebastian Andrzej Siewior005fd4b2016-11-03 15:50:02 +01007984 /*
7985 * Spill the event counters of the dead processor
7986 * into the current processors event counters.
7987 * This artificially elevates the count of the current
7988 * processor.
7989 */
7990 vm_events_fold_cpu(cpu);
Christoph Lameter9f8f2172008-02-04 22:29:11 -08007991
Sebastian Andrzej Siewior005fd4b2016-11-03 15:50:02 +01007992 /*
7993 * Zero the differential counters of the dead processor
7994 * so that the vm statistics are consistent.
7995 *
7996 * This is only okay since the processor is dead and cannot
7997 * race with what we are doing.
7998 */
7999 cpu_vm_stats_fold(cpu);
8000 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008001}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008002
Nicholas Piggine03a5122019-07-11 20:59:12 -07008003#ifdef CONFIG_NUMA
8004int hashdist = HASHDIST_DEFAULT;
8005
8006static int __init set_hashdist(char *str)
8007{
8008 if (!str)
8009 return 0;
8010 hashdist = simple_strtoul(str, &str, 0);
8011 return 1;
8012}
8013__setup("hashdist=", set_hashdist);
8014#endif
8015
Linus Torvalds1da177e2005-04-16 15:20:36 -07008016void __init page_alloc_init(void)
8017{
Sebastian Andrzej Siewior005fd4b2016-11-03 15:50:02 +01008018 int ret;
8019
Nicholas Piggine03a5122019-07-11 20:59:12 -07008020#ifdef CONFIG_NUMA
8021 if (num_node_state(N_MEMORY) == 1)
8022 hashdist = 0;
8023#endif
8024
Sebastian Andrzej Siewior005fd4b2016-11-03 15:50:02 +01008025 ret = cpuhp_setup_state_nocalls(CPUHP_PAGE_ALLOC_DEAD,
8026 "mm/page_alloc:dead", NULL,
8027 page_alloc_cpu_dead);
8028 WARN_ON(ret < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008029}
8030
8031/*
Yaowei Bai34b10062015-09-08 15:04:13 -07008032 * calculate_totalreserve_pages - called when sysctl_lowmem_reserve_ratio
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07008033 * or min_free_kbytes changes.
8034 */
8035static void calculate_totalreserve_pages(void)
8036{
8037 struct pglist_data *pgdat;
8038 unsigned long reserve_pages = 0;
Christoph Lameter2f6726e2006-09-25 23:31:18 -07008039 enum zone_type i, j;
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07008040
8041 for_each_online_pgdat(pgdat) {
Mel Gorman281e3722016-07-28 15:46:11 -07008042
8043 pgdat->totalreserve_pages = 0;
8044
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07008045 for (i = 0; i < MAX_NR_ZONES; i++) {
8046 struct zone *zone = pgdat->node_zones + i;
Mel Gorman3484b2d2014-08-06 16:07:14 -07008047 long max = 0;
Arun KS9705bea2018-12-28 00:34:24 -08008048 unsigned long managed_pages = zone_managed_pages(zone);
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07008049
8050 /* Find valid and maximum lowmem_reserve in the zone */
8051 for (j = i; j < MAX_NR_ZONES; j++) {
8052 if (zone->lowmem_reserve[j] > max)
8053 max = zone->lowmem_reserve[j];
8054 }
8055
Mel Gorman41858962009-06-16 15:32:12 -07008056 /* we treat the high watermark as reserved pages. */
8057 max += high_wmark_pages(zone);
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07008058
Arun KS3d6357d2018-12-28 00:34:20 -08008059 if (max > managed_pages)
8060 max = managed_pages;
Johannes Weinera8d01432016-01-14 15:20:15 -08008061
Mel Gorman281e3722016-07-28 15:46:11 -07008062 pgdat->totalreserve_pages += max;
Johannes Weinera8d01432016-01-14 15:20:15 -08008063
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07008064 reserve_pages += max;
8065 }
8066 }
8067 totalreserve_pages = reserve_pages;
8068}
8069
8070/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07008071 * setup_per_zone_lowmem_reserve - called whenever
Yaowei Bai34b10062015-09-08 15:04:13 -07008072 * sysctl_lowmem_reserve_ratio changes. Ensures that each zone
Linus Torvalds1da177e2005-04-16 15:20:36 -07008073 * has a correct pages reserved value, so an adequate number of
8074 * pages are left in the zone after a successful __alloc_pages().
8075 */
8076static void setup_per_zone_lowmem_reserve(void)
8077{
8078 struct pglist_data *pgdat;
Lorenzo Stoakes470c61d2020-12-14 19:11:22 -08008079 enum zone_type i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008080
KAMEZAWA Hiroyukiec936fc2006-03-27 01:15:59 -08008081 for_each_online_pgdat(pgdat) {
Lorenzo Stoakes470c61d2020-12-14 19:11:22 -08008082 for (i = 0; i < MAX_NR_ZONES - 1; i++) {
8083 struct zone *zone = &pgdat->node_zones[i];
8084 int ratio = sysctl_lowmem_reserve_ratio[i];
8085 bool clear = !ratio || !zone_managed_pages(zone);
8086 unsigned long managed_pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008087
Lorenzo Stoakes470c61d2020-12-14 19:11:22 -08008088 for (j = i + 1; j < MAX_NR_ZONES; j++) {
8089 if (clear) {
8090 zone->lowmem_reserve[j] = 0;
Joonsoo Kimd3cda232018-04-10 16:30:11 -07008091 } else {
Lorenzo Stoakes470c61d2020-12-14 19:11:22 -08008092 struct zone *upper_zone = &pgdat->node_zones[j];
8093
8094 managed_pages += zone_managed_pages(upper_zone);
8095 zone->lowmem_reserve[j] = managed_pages / ratio;
Joonsoo Kimd3cda232018-04-10 16:30:11 -07008096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008097 }
8098 }
8099 }
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07008100
8101 /* update totalreserve_pages */
8102 calculate_totalreserve_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008103}
8104
Mel Gormancfd3da12011-04-25 21:36:42 +00008105static void __setup_per_zone_wmarks(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008106{
8107 unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
8108 unsigned long lowmem_pages = 0;
8109 struct zone *zone;
8110 unsigned long flags;
8111
8112 /* Calculate total number of !ZONE_HIGHMEM pages */
8113 for_each_zone(zone) {
8114 if (!is_highmem(zone))
Arun KS9705bea2018-12-28 00:34:24 -08008115 lowmem_pages += zone_managed_pages(zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008116 }
8117
8118 for_each_zone(zone) {
Andrew Mortonac924c62006-05-15 09:43:59 -07008119 u64 tmp;
8120
Gerald Schaefer1125b4e2008-10-18 20:27:11 -07008121 spin_lock_irqsave(&zone->lock, flags);
Arun KS9705bea2018-12-28 00:34:24 -08008122 tmp = (u64)pages_min * zone_managed_pages(zone);
Andrew Mortonac924c62006-05-15 09:43:59 -07008123 do_div(tmp, lowmem_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008124 if (is_highmem(zone)) {
8125 /*
Nick Piggin669ed172005-11-13 16:06:45 -08008126 * __GFP_HIGH and PF_MEMALLOC allocations usually don't
8127 * need highmem pages, so cap pages_min to a small
8128 * value here.
8129 *
Mel Gorman41858962009-06-16 15:32:12 -07008130 * The WMARK_HIGH-WMARK_LOW and (WMARK_LOW-WMARK_MIN)
Wei Yang8bb4e7a2019-03-05 15:46:22 -08008131 * deltas control async page reclaim, and so should
Nick Piggin669ed172005-11-13 16:06:45 -08008132 * not be capped for highmem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008133 */
Andrew Morton90ae8d62013-02-22 16:32:22 -08008134 unsigned long min_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008135
Arun KS9705bea2018-12-28 00:34:24 -08008136 min_pages = zone_managed_pages(zone) / 1024;
Andrew Morton90ae8d62013-02-22 16:32:22 -08008137 min_pages = clamp(min_pages, SWAP_CLUSTER_MAX, 128UL);
Mel Gormana9214442018-12-28 00:35:44 -08008138 zone->_watermark[WMARK_MIN] = min_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008139 } else {
Nick Piggin669ed172005-11-13 16:06:45 -08008140 /*
8141 * If it's a lowmem zone, reserve a number of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07008142 * proportionate to the zone's size.
8143 */
Mel Gormana9214442018-12-28 00:35:44 -08008144 zone->_watermark[WMARK_MIN] = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008145 }
8146
Johannes Weiner795ae7a2016-03-17 14:19:14 -07008147 /*
8148 * Set the kswapd watermarks distance according to the
8149 * scale factor in proportion to available memory, but
8150 * ensure a minimum size on small systems.
8151 */
8152 tmp = max_t(u64, tmp >> 2,
Arun KS9705bea2018-12-28 00:34:24 -08008153 mult_frac(zone_managed_pages(zone),
Johannes Weiner795ae7a2016-03-17 14:19:14 -07008154 watermark_scale_factor, 10000));
8155
Charan Teja Reddyaa092592020-06-03 15:59:14 -07008156 zone->watermark_boost = 0;
Mel Gormana9214442018-12-28 00:35:44 -08008157 zone->_watermark[WMARK_LOW] = min_wmark_pages(zone) + tmp;
8158 zone->_watermark[WMARK_HIGH] = min_wmark_pages(zone) + tmp * 2;
Marek Szyprowski49f223a2012-01-25 12:49:24 +01008159
Gerald Schaefer1125b4e2008-10-18 20:27:11 -07008160 spin_unlock_irqrestore(&zone->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008161 }
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07008162
8163 /* update totalreserve_pages */
8164 calculate_totalreserve_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008165}
8166
Mel Gormancfd3da12011-04-25 21:36:42 +00008167/**
8168 * setup_per_zone_wmarks - called when min_free_kbytes changes
8169 * or when memory is hot-{added|removed}
8170 *
8171 * Ensures that the watermark[min,low,high] values for each zone are set
8172 * correctly with respect to min_free_kbytes.
8173 */
8174void setup_per_zone_wmarks(void)
8175{
Michal Hockob93e0f32017-09-06 16:20:37 -07008176 static DEFINE_SPINLOCK(lock);
8177
8178 spin_lock(&lock);
Mel Gormancfd3da12011-04-25 21:36:42 +00008179 __setup_per_zone_wmarks();
Michal Hockob93e0f32017-09-06 16:20:37 -07008180 spin_unlock(&lock);
Mel Gormancfd3da12011-04-25 21:36:42 +00008181}
8182
Randy Dunlap55a44622009-09-21 17:01:20 -07008183/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07008184 * Initialise min_free_kbytes.
8185 *
8186 * For small machines we want it small (128k min). For large machines
Joel Savitz8beeae82020-07-03 15:15:30 -07008187 * we want it large (256MB max). But it is not linear, because network
Linus Torvalds1da177e2005-04-16 15:20:36 -07008188 * bandwidth does not increase linearly with machine size. We use
8189 *
Pintu Kumarb8af2942013-09-11 14:20:34 -07008190 * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008191 * min_free_kbytes = sqrt(lowmem_kbytes * 16)
8192 *
8193 * which yields
8194 *
8195 * 16MB: 512k
8196 * 32MB: 724k
8197 * 64MB: 1024k
8198 * 128MB: 1448k
8199 * 256MB: 2048k
8200 * 512MB: 2896k
8201 * 1024MB: 4096k
8202 * 2048MB: 5792k
8203 * 4096MB: 8192k
8204 * 8192MB: 11584k
8205 * 16384MB: 16384k
8206 */
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07008207int __meminit init_per_zone_wmark_min(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008208{
8209 unsigned long lowmem_kbytes;
Michal Hocko5f127332013-07-08 16:00:40 -07008210 int new_min_free_kbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008211
8212 lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
Michal Hocko5f127332013-07-08 16:00:40 -07008213 new_min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008214
Michal Hocko5f127332013-07-08 16:00:40 -07008215 if (new_min_free_kbytes > user_min_free_kbytes) {
8216 min_free_kbytes = new_min_free_kbytes;
8217 if (min_free_kbytes < 128)
8218 min_free_kbytes = 128;
Joel Savitzee8eb9a2020-04-01 21:09:44 -07008219 if (min_free_kbytes > 262144)
8220 min_free_kbytes = 262144;
Michal Hocko5f127332013-07-08 16:00:40 -07008221 } else {
8222 pr_warn("min_free_kbytes is not updated to %d because user defined value %d is preferred\n",
8223 new_min_free_kbytes, user_min_free_kbytes);
8224 }
Minchan Kimbc75d332009-06-16 15:32:48 -07008225 setup_per_zone_wmarks();
KOSAKI Motohiroa6cccdc2011-05-24 17:11:33 -07008226 refresh_zone_stat_thresholds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008227 setup_per_zone_lowmem_reserve();
Joonsoo Kim6423aa82016-08-10 16:27:49 -07008228
8229#ifdef CONFIG_NUMA
8230 setup_min_unmapped_ratio();
8231 setup_min_slab_ratio();
8232#endif
8233
Vijay Balakrishna4aab2be2020-10-10 23:16:40 -07008234 khugepaged_min_free_kbytes_update();
8235
Linus Torvalds1da177e2005-04-16 15:20:36 -07008236 return 0;
8237}
Doug Bergere08d3fd2020-08-20 17:42:24 -07008238postcore_initcall(init_per_zone_wmark_min)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008239
8240/*
Pintu Kumarb8af2942013-09-11 14:20:34 -07008241 * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
Linus Torvalds1da177e2005-04-16 15:20:36 -07008242 * that we can call two helper functions whenever min_free_kbytes
8243 * changes.
8244 */
Joe Perchescccad5b2014-06-06 14:38:09 -07008245int min_free_kbytes_sysctl_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02008246 void *buffer, size_t *length, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008247{
Han Pingtianda8c7572014-01-23 15:53:17 -08008248 int rc;
8249
8250 rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
8251 if (rc)
8252 return rc;
8253
Michal Hocko5f127332013-07-08 16:00:40 -07008254 if (write) {
8255 user_min_free_kbytes = min_free_kbytes;
Minchan Kimbc75d332009-06-16 15:32:48 -07008256 setup_per_zone_wmarks();
Michal Hocko5f127332013-07-08 16:00:40 -07008257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008258 return 0;
8259}
8260
Johannes Weiner795ae7a2016-03-17 14:19:14 -07008261int watermark_scale_factor_sysctl_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02008262 void *buffer, size_t *length, loff_t *ppos)
Johannes Weiner795ae7a2016-03-17 14:19:14 -07008263{
8264 int rc;
8265
8266 rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
8267 if (rc)
8268 return rc;
8269
8270 if (write)
8271 setup_per_zone_wmarks();
8272
8273 return 0;
8274}
8275
Christoph Lameter96146342006-07-03 00:24:13 -07008276#ifdef CONFIG_NUMA
Joonsoo Kim6423aa82016-08-10 16:27:49 -07008277static void setup_min_unmapped_ratio(void)
Christoph Lameter96146342006-07-03 00:24:13 -07008278{
Joonsoo Kim6423aa82016-08-10 16:27:49 -07008279 pg_data_t *pgdat;
Christoph Lameter96146342006-07-03 00:24:13 -07008280 struct zone *zone;
Christoph Lameter96146342006-07-03 00:24:13 -07008281
Mel Gormana5f5f912016-07-28 15:46:32 -07008282 for_each_online_pgdat(pgdat)
Joonsoo Kim81cbcbc2016-08-10 16:27:46 -07008283 pgdat->min_unmapped_pages = 0;
Mel Gormana5f5f912016-07-28 15:46:32 -07008284
Christoph Lameter96146342006-07-03 00:24:13 -07008285 for_each_zone(zone)
Arun KS9705bea2018-12-28 00:34:24 -08008286 zone->zone_pgdat->min_unmapped_pages += (zone_managed_pages(zone) *
8287 sysctl_min_unmapped_ratio) / 100;
Christoph Lameter96146342006-07-03 00:24:13 -07008288}
Christoph Lameter0ff38492006-09-25 23:31:52 -07008289
Joonsoo Kim6423aa82016-08-10 16:27:49 -07008290
8291int sysctl_min_unmapped_ratio_sysctl_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02008292 void *buffer, size_t *length, loff_t *ppos)
Christoph Lameter0ff38492006-09-25 23:31:52 -07008293{
Christoph Lameter0ff38492006-09-25 23:31:52 -07008294 int rc;
8295
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07008296 rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
Christoph Lameter0ff38492006-09-25 23:31:52 -07008297 if (rc)
8298 return rc;
8299
Joonsoo Kim6423aa82016-08-10 16:27:49 -07008300 setup_min_unmapped_ratio();
8301
8302 return 0;
8303}
8304
8305static void setup_min_slab_ratio(void)
8306{
8307 pg_data_t *pgdat;
8308 struct zone *zone;
8309
Mel Gormana5f5f912016-07-28 15:46:32 -07008310 for_each_online_pgdat(pgdat)
8311 pgdat->min_slab_pages = 0;
8312
Christoph Lameter0ff38492006-09-25 23:31:52 -07008313 for_each_zone(zone)
Arun KS9705bea2018-12-28 00:34:24 -08008314 zone->zone_pgdat->min_slab_pages += (zone_managed_pages(zone) *
8315 sysctl_min_slab_ratio) / 100;
Joonsoo Kim6423aa82016-08-10 16:27:49 -07008316}
8317
8318int sysctl_min_slab_ratio_sysctl_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02008319 void *buffer, size_t *length, loff_t *ppos)
Joonsoo Kim6423aa82016-08-10 16:27:49 -07008320{
8321 int rc;
8322
8323 rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
8324 if (rc)
8325 return rc;
8326
8327 setup_min_slab_ratio();
8328
Christoph Lameter0ff38492006-09-25 23:31:52 -07008329 return 0;
8330}
Christoph Lameter96146342006-07-03 00:24:13 -07008331#endif
8332
Linus Torvalds1da177e2005-04-16 15:20:36 -07008333/*
8334 * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
8335 * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
8336 * whenever sysctl_lowmem_reserve_ratio changes.
8337 *
8338 * The reserve ratio obviously has absolutely no relation with the
Mel Gorman41858962009-06-16 15:32:12 -07008339 * minimum watermarks. The lowmem reserve ratio can only make sense
Linus Torvalds1da177e2005-04-16 15:20:36 -07008340 * if in function of the boot time zone sizes.
8341 */
Joe Perchescccad5b2014-06-06 14:38:09 -07008342int lowmem_reserve_ratio_sysctl_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02008343 void *buffer, size_t *length, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008344{
Baoquan He86aaf252020-06-03 15:58:48 -07008345 int i;
8346
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07008347 proc_dointvec_minmax(table, write, buffer, length, ppos);
Baoquan He86aaf252020-06-03 15:58:48 -07008348
8349 for (i = 0; i < MAX_NR_ZONES; i++) {
8350 if (sysctl_lowmem_reserve_ratio[i] < 1)
8351 sysctl_lowmem_reserve_ratio[i] = 0;
8352 }
8353
Linus Torvalds1da177e2005-04-16 15:20:36 -07008354 setup_per_zone_lowmem_reserve();
8355 return 0;
8356}
8357
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08008358/*
8359 * percpu_pagelist_fraction - changes the pcp->high for each zone on each
Pintu Kumarb8af2942013-09-11 14:20:34 -07008360 * cpu. It is the fraction of total pages in each zone that a hot per cpu
8361 * pagelist can have before it gets flushed back to buddy allocator.
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08008362 */
Joe Perchescccad5b2014-06-06 14:38:09 -07008363int percpu_pagelist_fraction_sysctl_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02008364 void *buffer, size_t *length, loff_t *ppos)
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08008365{
8366 struct zone *zone;
David Rientjes7cd2b0a2014-06-23 13:22:04 -07008367 int old_percpu_pagelist_fraction;
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08008368 int ret;
8369
Cody P Schaferc8e251f2013-07-03 15:01:29 -07008370 mutex_lock(&pcp_batch_high_lock);
David Rientjes7cd2b0a2014-06-23 13:22:04 -07008371 old_percpu_pagelist_fraction = percpu_pagelist_fraction;
8372
8373 ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
8374 if (!write || ret < 0)
8375 goto out;
8376
8377 /* Sanity checking to avoid pcp imbalance */
8378 if (percpu_pagelist_fraction &&
8379 percpu_pagelist_fraction < MIN_PERCPU_PAGELIST_FRACTION) {
8380 percpu_pagelist_fraction = old_percpu_pagelist_fraction;
8381 ret = -EINVAL;
8382 goto out;
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08008383 }
David Rientjes7cd2b0a2014-06-23 13:22:04 -07008384
8385 /* No change? */
8386 if (percpu_pagelist_fraction == old_percpu_pagelist_fraction)
8387 goto out;
8388
Mel Gormancb1ef532019-11-30 17:55:11 -08008389 for_each_populated_zone(zone)
Vlastimil Babka0a8b4f12020-12-14 19:10:43 -08008390 zone_set_pageset_high_and_batch(zone);
David Rientjes7cd2b0a2014-06-23 13:22:04 -07008391out:
Cody P Schaferc8e251f2013-07-03 15:01:29 -07008392 mutex_unlock(&pcp_batch_high_lock);
David Rientjes7cd2b0a2014-06-23 13:22:04 -07008393 return ret;
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08008394}
8395
Srikar Dronamrajuf6f34b42016-10-07 16:59:15 -07008396#ifndef __HAVE_ARCH_RESERVED_KERNEL_PAGES
8397/*
8398 * Returns the number of pages that arch has reserved but
8399 * is not known to alloc_large_system_hash().
8400 */
8401static unsigned long __init arch_reserved_kernel_pages(void)
8402{
8403 return 0;
8404}
8405#endif
8406
Linus Torvalds1da177e2005-04-16 15:20:36 -07008407/*
Pavel Tatashin90172172017-07-06 15:39:14 -07008408 * Adaptive scale is meant to reduce sizes of hash tables on large memory
8409 * machines. As memory size is increased the scale is also increased but at
8410 * slower pace. Starting from ADAPT_SCALE_BASE (64G), every time memory
8411 * quadruples the scale is increased by one, which means the size of hash table
8412 * only doubles, instead of quadrupling as well.
8413 * Because 32-bit systems cannot have large physical memory, where this scaling
8414 * makes sense, it is disabled on such platforms.
8415 */
8416#if __BITS_PER_LONG > 32
8417#define ADAPT_SCALE_BASE (64ul << 30)
8418#define ADAPT_SCALE_SHIFT 2
8419#define ADAPT_SCALE_NPAGES (ADAPT_SCALE_BASE >> PAGE_SHIFT)
8420#endif
8421
8422/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07008423 * allocate a large system hash table from bootmem
8424 * - it is assumed that the hash table must contain an exact power-of-2
8425 * quantity of entries
8426 * - limit is the number of hash buckets, not the total allocation size
8427 */
8428void *__init alloc_large_system_hash(const char *tablename,
8429 unsigned long bucketsize,
8430 unsigned long numentries,
8431 int scale,
8432 int flags,
8433 unsigned int *_hash_shift,
8434 unsigned int *_hash_mask,
Tim Bird31fe62b2012-05-23 13:33:35 +00008435 unsigned long low_limit,
8436 unsigned long high_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008437{
Tim Bird31fe62b2012-05-23 13:33:35 +00008438 unsigned long long max = high_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008439 unsigned long log2qty, size;
8440 void *table = NULL;
Pavel Tatashin3749a8f2017-07-06 15:39:08 -07008441 gfp_t gfp_flags;
Nicholas Pigginec114082019-07-11 20:59:09 -07008442 bool virt;
Nicholas Piggin121e6f32021-04-29 22:58:49 -07008443 bool huge;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008444
8445 /* allow the kernel cmdline to have a say */
8446 if (!numentries) {
8447 /* round applicable memory size up to nearest megabyte */
Andrew Morton04903662006-12-06 20:37:33 -08008448 numentries = nr_kernel_pages;
Srikar Dronamrajuf6f34b42016-10-07 16:59:15 -07008449 numentries -= arch_reserved_kernel_pages();
Jerry Zhoua7e83312013-09-11 14:20:26 -07008450
8451 /* It isn't necessary when PAGE_SIZE >= 1MB */
8452 if (PAGE_SHIFT < 20)
8453 numentries = round_up(numentries, (1<<20)/PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008454
Pavel Tatashin90172172017-07-06 15:39:14 -07008455#if __BITS_PER_LONG > 32
8456 if (!high_limit) {
8457 unsigned long adapt;
8458
8459 for (adapt = ADAPT_SCALE_NPAGES; adapt < numentries;
8460 adapt <<= ADAPT_SCALE_SHIFT)
8461 scale++;
8462 }
8463#endif
8464
Linus Torvalds1da177e2005-04-16 15:20:36 -07008465 /* limit to 1 bucket per 2^scale bytes of low memory */
8466 if (scale > PAGE_SHIFT)
8467 numentries >>= (scale - PAGE_SHIFT);
8468 else
8469 numentries <<= (PAGE_SHIFT - scale);
Paul Mundt9ab37b82007-01-05 16:36:30 -08008470
8471 /* Make sure we've got at least a 0-order allocation.. */
Jan Beulich2c85f512009-09-21 17:03:07 -07008472 if (unlikely(flags & HASH_SMALL)) {
8473 /* Makes no sense without HASH_EARLY */
8474 WARN_ON(!(flags & HASH_EARLY));
8475 if (!(numentries >> *_hash_shift)) {
8476 numentries = 1UL << *_hash_shift;
8477 BUG_ON(!numentries);
8478 }
8479 } else if (unlikely((numentries * bucketsize) < PAGE_SIZE))
Paul Mundt9ab37b82007-01-05 16:36:30 -08008480 numentries = PAGE_SIZE / bucketsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008481 }
John Hawkes6e692ed2006-03-25 03:08:02 -08008482 numentries = roundup_pow_of_two(numentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008483
8484 /* limit allocation size to 1/16 total memory by default */
8485 if (max == 0) {
8486 max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
8487 do_div(max, bucketsize);
8488 }
Dimitri Sivanich074b8512012-02-08 12:39:07 -08008489 max = min(max, 0x80000000ULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008490
Tim Bird31fe62b2012-05-23 13:33:35 +00008491 if (numentries < low_limit)
8492 numentries = low_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008493 if (numentries > max)
8494 numentries = max;
8495
David Howellsf0d1b0b2006-12-08 02:37:49 -08008496 log2qty = ilog2(numentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008497
Pavel Tatashin3749a8f2017-07-06 15:39:08 -07008498 gfp_flags = (flags & HASH_ZERO) ? GFP_ATOMIC | __GFP_ZERO : GFP_ATOMIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008499 do {
Nicholas Pigginec114082019-07-11 20:59:09 -07008500 virt = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008501 size = bucketsize << log2qty;
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08008502 if (flags & HASH_EARLY) {
8503 if (flags & HASH_ZERO)
Mike Rapoport26fb3da2019-03-11 23:30:42 -07008504 table = memblock_alloc(size, SMP_CACHE_BYTES);
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08008505 else
Mike Rapoport7e1c4e22018-10-30 15:09:57 -07008506 table = memblock_alloc_raw(size,
8507 SMP_CACHE_BYTES);
Nicholas Pigginec114082019-07-11 20:59:09 -07008508 } else if (get_order(size) >= MAX_ORDER || hashdist) {
Christoph Hellwig88dca4c2020-06-01 21:51:40 -07008509 table = __vmalloc(size, gfp_flags);
Nicholas Pigginec114082019-07-11 20:59:09 -07008510 virt = true;
Nicholas Piggin121e6f32021-04-29 22:58:49 -07008511 huge = is_vm_area_hugepages(table);
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08008512 } else {
Eric Dumazet1037b832007-07-15 23:38:05 -07008513 /*
8514 * If bucketsize is not a power-of-two, we may free
Mel Gormana1dd2682009-06-16 15:32:19 -07008515 * some pages at the end of hash table which
8516 * alloc_pages_exact() automatically does
Eric Dumazet1037b832007-07-15 23:38:05 -07008517 */
Nicholas Pigginec114082019-07-11 20:59:09 -07008518 table = alloc_pages_exact(size, gfp_flags);
8519 kmemleak_alloc(table, size, 1, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008520 }
8521 } while (!table && size > PAGE_SIZE && --log2qty);
8522
8523 if (!table)
8524 panic("Failed to allocate %s hash table\n", tablename);
8525
Nicholas Pigginec114082019-07-11 20:59:09 -07008526 pr_info("%s hash table entries: %ld (order: %d, %lu bytes, %s)\n",
8527 tablename, 1UL << log2qty, ilog2(size) - PAGE_SHIFT, size,
Nicholas Piggin121e6f32021-04-29 22:58:49 -07008528 virt ? (huge ? "vmalloc hugepage" : "vmalloc") : "linear");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008529
8530 if (_hash_shift)
8531 *_hash_shift = log2qty;
8532 if (_hash_mask)
8533 *_hash_mask = (1 << log2qty) - 1;
8534
8535 return table;
8536}
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -08008537
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -07008538/*
Minchan Kim80934512012-07-31 16:43:01 -07008539 * This function checks whether pageblock includes unmovable pages or not.
Minchan Kim80934512012-07-31 16:43:01 -07008540 *
Pintu Kumarb8af2942013-09-11 14:20:34 -07008541 * PageLRU check without isolation or lru_lock could race so that
Yisheng Xie0efadf42017-02-24 14:57:39 -08008542 * MIGRATE_MOVABLE block might include unmovable pages. And __PageMovable
8543 * check without lock_page also may miss some movable non-lru pages at
8544 * race condition. So you can't expect this function should be exact.
Qian Cai4a55c042020-01-30 22:14:57 -08008545 *
8546 * Returns a page without holding a reference. If the caller wants to
Randy Dunlap047b9962020-08-11 18:33:14 -07008547 * dereference that page (e.g., dumping), it has to make sure that it
Qian Cai4a55c042020-01-30 22:14:57 -08008548 * cannot get removed (e.g., via memory unplug) concurrently.
8549 *
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -07008550 */
Qian Cai4a55c042020-01-30 22:14:57 -08008551struct page *has_unmovable_pages(struct zone *zone, struct page *page,
8552 int migratetype, int flags)
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07008553{
Qian Cai1a9f21912019-04-18 17:50:30 -07008554 unsigned long iter = 0;
8555 unsigned long pfn = page_to_pfn(page);
Li Xinhai6a654e32020-10-13 16:55:39 -07008556 unsigned long offset = pfn % pageblock_nr_pages;
Michal Nazarewicz47118af2011-12-29 13:09:50 +01008557
Qian Cai1a9f21912019-04-18 17:50:30 -07008558 if (is_migrate_cma_page(page)) {
8559 /*
8560 * CMA allocations (alloc_contig_range) really need to mark
8561 * isolate CMA pageblocks even when they are not movable in fact
8562 * so consider them movable here.
8563 */
8564 if (is_migrate_cma(migratetype))
Qian Cai4a55c042020-01-30 22:14:57 -08008565 return NULL;
Michal Hocko4da2ce22017-11-15 17:33:26 -08008566
Qian Cai3d680bd2020-01-30 22:15:01 -08008567 return page;
Qian Cai1a9f21912019-04-18 17:50:30 -07008568 }
8569
Li Xinhai6a654e32020-10-13 16:55:39 -07008570 for (; iter < pageblock_nr_pages - offset; iter++) {
David Hildenbrandfe4c86c2020-01-30 22:14:04 -08008571 if (!pfn_valid_within(pfn + iter))
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07008572 continue;
Namhyung Kim29723fc2011-02-25 14:44:25 -08008573
David Hildenbrandfe4c86c2020-01-30 22:14:04 -08008574 page = pfn_to_page(pfn + iter);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07008575
David Hildenbrandc9c510d2020-10-13 16:55:17 -07008576 /*
8577 * Both, bootmem allocations and memory holes are marked
8578 * PG_reserved and are unmovable. We can even have unmovable
8579 * allocations inside ZONE_MOVABLE, for example when
8580 * specifying "movablecore".
8581 */
Michal Hockod7ab3672017-11-15 17:33:30 -08008582 if (PageReserved(page))
Qian Cai3d680bd2020-01-30 22:15:01 -08008583 return page;
Michal Hockod7ab3672017-11-15 17:33:30 -08008584
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07008585 /*
Michal Hocko9d789992018-11-16 15:08:15 -08008586 * If the zone is movable and we have ruled out all reserved
8587 * pages then it should be reasonably safe to assume the rest
8588 * is movable.
8589 */
8590 if (zone_idx(zone) == ZONE_MOVABLE)
8591 continue;
8592
8593 /*
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07008594 * Hugepages are not in LRU lists, but they're movable.
Rik van Riel1da2f322020-04-01 21:10:31 -07008595 * THPs are on the LRU, but need to be counted as #small pages.
Wei Yang8bb4e7a2019-03-05 15:46:22 -08008596 * We need not scan over tail pages because we don't
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07008597 * handle each tail page individually in migration.
8598 */
Rik van Riel1da2f322020-04-01 21:10:31 -07008599 if (PageHuge(page) || PageTransCompound(page)) {
Oscar Salvador17e2e7d2018-12-21 14:31:00 -08008600 struct page *head = compound_head(page);
8601 unsigned int skip_pages;
Aneesh Kumar K.V464c7ff2018-09-04 15:45:59 -07008602
Rik van Riel1da2f322020-04-01 21:10:31 -07008603 if (PageHuge(page)) {
8604 if (!hugepage_migration_supported(page_hstate(head)))
8605 return page;
8606 } else if (!PageLRU(head) && !__PageMovable(head)) {
Qian Cai3d680bd2020-01-30 22:15:01 -08008607 return page;
Rik van Riel1da2f322020-04-01 21:10:31 -07008608 }
Aneesh Kumar K.V464c7ff2018-09-04 15:45:59 -07008609
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -07008610 skip_pages = compound_nr(head) - (page - head);
Oscar Salvador17e2e7d2018-12-21 14:31:00 -08008611 iter += skip_pages - 1;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07008612 continue;
8613 }
8614
Minchan Kim97d255c2012-07-31 16:42:59 -07008615 /*
8616 * We can't use page_count without pin a page
8617 * because another CPU can free compound page.
8618 * This check already skips compound tails of THP
Joonsoo Kim0139aa72016-05-19 17:10:49 -07008619 * because their page->_refcount is zero at all time.
Minchan Kim97d255c2012-07-31 16:42:59 -07008620 */
Joonsoo Kimfe896d12016-03-17 14:19:26 -07008621 if (!page_ref_count(page)) {
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07008622 if (PageBuddy(page))
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -07008623 iter += (1 << buddy_order(page)) - 1;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07008624 continue;
8625 }
Minchan Kim97d255c2012-07-31 16:42:59 -07008626
Wen Congyangb023f462012-12-11 16:00:45 -08008627 /*
8628 * The HWPoisoned page may be not in buddy system, and
8629 * page_count() is not 0.
8630 */
David Hildenbrand756d25b2019-11-30 17:54:07 -08008631 if ((flags & MEMORY_OFFLINE) && PageHWPoison(page))
Wen Congyangb023f462012-12-11 16:00:45 -08008632 continue;
8633
David Hildenbrandaa218792020-05-07 16:01:30 +02008634 /*
8635 * We treat all PageOffline() pages as movable when offlining
8636 * to give drivers a chance to decrement their reference count
8637 * in MEM_GOING_OFFLINE in order to indicate that these pages
8638 * can be offlined as there are no direct references anymore.
8639 * For actually unmovable PageOffline() where the driver does
8640 * not support this, we will fail later when trying to actually
8641 * move these pages that still have a reference count > 0.
8642 * (false negatives in this function only)
8643 */
8644 if ((flags & MEMORY_OFFLINE) && PageOffline(page))
8645 continue;
8646
David Hildenbrandfe4c86c2020-01-30 22:14:04 -08008647 if (__PageMovable(page) || PageLRU(page))
Yisheng Xie0efadf42017-02-24 14:57:39 -08008648 continue;
8649
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07008650 /*
Johannes Weiner6b4f7792014-12-12 16:56:13 -08008651 * If there are RECLAIMABLE pages, we need to check
8652 * it. But now, memory offline itself doesn't call
8653 * shrink_node_slabs() and it still to be fixed.
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07008654 */
Qian Cai3d680bd2020-01-30 22:15:01 -08008655 return page;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07008656 }
Qian Cai4a55c042020-01-30 22:14:57 -08008657 return NULL;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07008658}
8659
Alexandre Ghiti8df995f2019-05-13 17:19:00 -07008660#ifdef CONFIG_CONTIG_ALLOC
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008661static unsigned long pfn_max_align_down(unsigned long pfn)
8662{
8663 return pfn & ~(max_t(unsigned long, MAX_ORDER_NR_PAGES,
8664 pageblock_nr_pages) - 1);
8665}
8666
8667static unsigned long pfn_max_align_up(unsigned long pfn)
8668{
8669 return ALIGN(pfn, max_t(unsigned long, MAX_ORDER_NR_PAGES,
8670 pageblock_nr_pages));
8671}
8672
Minchan Kima1394bd2021-04-29 23:01:30 -07008673#if defined(CONFIG_DYNAMIC_DEBUG) || \
8674 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
8675/* Usage: See admin-guide/dynamic-debug-howto.rst */
8676static void alloc_contig_dump_pages(struct list_head *page_list)
8677{
8678 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, "migrate failure");
8679
8680 if (DYNAMIC_DEBUG_BRANCH(descriptor)) {
8681 struct page *page;
8682
8683 dump_stack();
8684 list_for_each_entry(page, page_list, lru)
8685 dump_page(page, "migration failure");
8686 }
8687}
8688#else
8689static inline void alloc_contig_dump_pages(struct list_head *page_list)
8690{
8691}
8692#endif
8693
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008694/* [start, end) must belong to a single zone. */
Mel Gormanbb13ffe2012-10-08 16:32:41 -07008695static int __alloc_contig_migrate_range(struct compact_control *cc,
8696 unsigned long start, unsigned long end)
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008697{
8698 /* This function is based on compact_zone() from compaction.c. */
Maninder Singh730ec8c2020-06-03 16:01:18 -07008699 unsigned int nr_reclaimed;
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008700 unsigned long pfn = start;
8701 unsigned int tries = 0;
8702 int ret = 0;
Joonsoo Kim8b94e0b2020-08-11 18:37:31 -07008703 struct migration_target_control mtc = {
8704 .nid = zone_to_nid(cc->zone),
8705 .gfp_mask = GFP_USER | __GFP_MOVABLE | __GFP_RETRY_MAYFAIL,
8706 };
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008707
Minchan Kim361a2a22021-05-04 18:36:57 -07008708 lru_cache_disable();
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008709
Mel Gormanbb13ffe2012-10-08 16:32:41 -07008710 while (pfn < end || !list_empty(&cc->migratepages)) {
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008711 if (fatal_signal_pending(current)) {
8712 ret = -EINTR;
8713 break;
8714 }
8715
Mel Gormanbb13ffe2012-10-08 16:32:41 -07008716 if (list_empty(&cc->migratepages)) {
8717 cc->nr_migratepages = 0;
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -07008718 ret = isolate_migratepages_range(cc, pfn, end);
8719 if (ret && ret != -EAGAIN)
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008720 break;
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -07008721 pfn = cc->migrate_pfn;
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008722 tries = 0;
8723 } else if (++tries == 5) {
Oscar Salvadorc8e28b42021-05-04 18:35:14 -07008724 ret = -EBUSY;
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008725 break;
8726 }
8727
Minchan Kimbeb51ea2012-10-08 16:33:51 -07008728 nr_reclaimed = reclaim_clean_pages_from_list(cc->zone,
8729 &cc->migratepages);
8730 cc->nr_migratepages -= nr_reclaimed;
Minchan Kim02c6de82012-10-08 16:31:55 -07008731
Joonsoo Kim8b94e0b2020-08-11 18:37:31 -07008732 ret = migrate_pages(&cc->migratepages, alloc_migration_target,
8733 NULL, (unsigned long)&mtc, cc->mode, MR_CONTIG_RANGE);
Oscar Salvadorc8e28b42021-05-04 18:35:14 -07008734
8735 /*
8736 * On -ENOMEM, migrate_pages() bails out right away. It is pointless
8737 * to retry again over this error, so do the same here.
8738 */
8739 if (ret == -ENOMEM)
8740 break;
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008741 }
Minchan Kimd479960e2021-05-04 18:36:54 -07008742
Minchan Kim361a2a22021-05-04 18:36:57 -07008743 lru_cache_enable();
Srinivas Pandruvada2a6f5122013-02-22 16:32:09 -08008744 if (ret < 0) {
Minchan Kima1394bd2021-04-29 23:01:30 -07008745 alloc_contig_dump_pages(&cc->migratepages);
Srinivas Pandruvada2a6f5122013-02-22 16:32:09 -08008746 putback_movable_pages(&cc->migratepages);
8747 return ret;
8748 }
8749 return 0;
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008750}
8751
8752/**
8753 * alloc_contig_range() -- tries to allocate given range of pages
8754 * @start: start PFN to allocate
8755 * @end: one-past-the-last PFN to allocate
Ingo Molnarf0953a12021-05-06 18:06:47 -07008756 * @migratetype: migratetype of the underlying pageblocks (either
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02008757 * #MIGRATE_MOVABLE or #MIGRATE_CMA). All pageblocks
8758 * in range must have the same migratetype and it must
8759 * be either of the two.
Lucas Stachca96b622017-02-24 14:58:37 -08008760 * @gfp_mask: GFP mask to use during compaction
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008761 *
8762 * The PFN range does not have to be pageblock or MAX_ORDER_NR_PAGES
Mike Kravetz2c7452a2018-04-05 16:25:26 -07008763 * aligned. The PFN range must belong to a single zone.
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008764 *
Mike Kravetz2c7452a2018-04-05 16:25:26 -07008765 * The first thing this routine does is attempt to MIGRATE_ISOLATE all
8766 * pageblocks in the range. Once isolated, the pageblocks should not
8767 * be modified by others.
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008768 *
Mike Rapoporta862f682019-03-05 15:48:42 -08008769 * Return: zero on success or negative error code. On success all
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008770 * pages which PFN is in [start, end) are allocated for the caller and
8771 * need to be freed with free_contig_range().
8772 */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02008773int alloc_contig_range(unsigned long start, unsigned long end,
Lucas Stachca96b622017-02-24 14:58:37 -08008774 unsigned migratetype, gfp_t gfp_mask)
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008775{
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008776 unsigned long outer_start, outer_end;
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08008777 unsigned int order;
8778 int ret = 0;
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008779
Mel Gormanbb13ffe2012-10-08 16:32:41 -07008780 struct compact_control cc = {
8781 .nr_migratepages = 0,
8782 .order = -1,
8783 .zone = page_zone(pfn_to_page(start)),
David Rientjese0b9dae2014-06-04 16:08:28 -07008784 .mode = MIGRATE_SYNC,
Mel Gormanbb13ffe2012-10-08 16:32:41 -07008785 .ignore_skip_hint = true,
Vlastimil Babka2583d672017-11-17 15:26:38 -08008786 .no_set_skip_hint = true,
Michal Hocko7dea19f2017-05-03 14:53:15 -07008787 .gfp_mask = current_gfp_context(gfp_mask),
Rik van Rielb06eda02020-04-01 21:10:28 -07008788 .alloc_contig = true,
Mel Gormanbb13ffe2012-10-08 16:32:41 -07008789 };
8790 INIT_LIST_HEAD(&cc.migratepages);
8791
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008792 /*
8793 * What we do here is we mark all pageblocks in range as
8794 * MIGRATE_ISOLATE. Because pageblock and max order pages may
8795 * have different sizes, and due to the way page allocator
8796 * work, we align the range to biggest of the two pages so
8797 * that page allocator won't try to merge buddies from
8798 * different pageblocks and change MIGRATE_ISOLATE to some
8799 * other migration type.
8800 *
8801 * Once the pageblocks are marked as MIGRATE_ISOLATE, we
8802 * migrate the pages from an unaligned range (ie. pages that
8803 * we are interested in). This will put all the pages in
8804 * range back to page allocator as MIGRATE_ISOLATE.
8805 *
8806 * When this is done, we take the pages in range from page
8807 * allocator removing them from the buddy system. This way
8808 * page allocator will never consider using them.
8809 *
8810 * This lets us mark the pageblocks back as
8811 * MIGRATE_CMA/MIGRATE_MOVABLE so that free pages in the
8812 * aligned range but not in the unaligned, original range are
8813 * put back to page allocator so that buddy can use them.
8814 */
8815
8816 ret = start_isolate_page_range(pfn_max_align_down(start),
Michal Hockod381c542018-12-28 00:33:56 -08008817 pfn_max_align_up(end), migratetype, 0);
David Hildenbrand3fa0c7c2020-10-15 20:08:07 -07008818 if (ret)
Bob Liu86a595f2012-10-25 13:37:56 -07008819 return ret;
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008820
Vlastimil Babka76129212020-12-14 19:10:56 -08008821 drain_all_pages(cc.zone);
8822
Joonsoo Kim8ef58492016-01-14 15:18:45 -08008823 /*
8824 * In case of -EBUSY, we'd like to know which page causes problem.
Mike Kravetz63cd4482017-11-29 16:10:01 -08008825 * So, just fall through. test_pages_isolated() has a tracepoint
8826 * which will report the busy page.
8827 *
8828 * It is possible that busy pages could become available before
8829 * the call to test_pages_isolated, and the range will actually be
8830 * allocated. So, if we fall through be sure to clear ret so that
8831 * -EBUSY is not accidentally used or returned to caller.
Joonsoo Kim8ef58492016-01-14 15:18:45 -08008832 */
Mel Gormanbb13ffe2012-10-08 16:32:41 -07008833 ret = __alloc_contig_migrate_range(&cc, start, end);
Joonsoo Kim8ef58492016-01-14 15:18:45 -08008834 if (ret && ret != -EBUSY)
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008835 goto done;
Zhiyuan Dai68d68ff2021-05-04 18:40:12 -07008836 ret = 0;
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008837
8838 /*
8839 * Pages from [start, end) are within a MAX_ORDER_NR_PAGES
8840 * aligned blocks that are marked as MIGRATE_ISOLATE. What's
8841 * more, all pages in [start, end) are free in page allocator.
8842 * What we are going to do is to allocate all pages from
8843 * [start, end) (that is remove them from page allocator).
8844 *
8845 * The only problem is that pages at the beginning and at the
8846 * end of interesting range may be not aligned with pages that
8847 * page allocator holds, ie. they can be part of higher order
8848 * pages. Because of this, we reserve the bigger range and
8849 * once this is done free the pages we are not interested in.
8850 *
8851 * We don't have to hold zone->lock here because the pages are
8852 * isolated thus they won't get removed from buddy.
8853 */
8854
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008855 order = 0;
8856 outer_start = start;
8857 while (!PageBuddy(pfn_to_page(outer_start))) {
8858 if (++order >= MAX_ORDER) {
Joonsoo Kim8ef58492016-01-14 15:18:45 -08008859 outer_start = start;
8860 break;
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008861 }
8862 outer_start &= ~0UL << order;
8863 }
8864
Joonsoo Kim8ef58492016-01-14 15:18:45 -08008865 if (outer_start != start) {
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -07008866 order = buddy_order(pfn_to_page(outer_start));
Joonsoo Kim8ef58492016-01-14 15:18:45 -08008867
8868 /*
8869 * outer_start page could be small order buddy page and
8870 * it doesn't include start page. Adjust outer_start
8871 * in this case to report failed page properly
8872 * on tracepoint in test_pages_isolated()
8873 */
8874 if (outer_start + (1UL << order) <= start)
8875 outer_start = start;
8876 }
8877
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008878 /* Make sure the range is really isolated. */
David Hildenbrand756d25b2019-11-30 17:54:07 -08008879 if (test_pages_isolated(outer_start, end, 0)) {
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008880 ret = -EBUSY;
8881 goto done;
8882 }
8883
Marek Szyprowski49f223a2012-01-25 12:49:24 +01008884 /* Grab isolated pages from freelists. */
Mel Gormanbb13ffe2012-10-08 16:32:41 -07008885 outer_end = isolate_freepages_range(&cc, outer_start, end);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008886 if (!outer_end) {
8887 ret = -EBUSY;
8888 goto done;
8889 }
8890
8891 /* Free head and tail (if any) */
8892 if (start != outer_start)
8893 free_contig_range(outer_start, start - outer_start);
8894 if (end != outer_end)
8895 free_contig_range(end, outer_end - end);
8896
8897done:
8898 undo_isolate_page_range(pfn_max_align_down(start),
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02008899 pfn_max_align_up(end), migratetype);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008900 return ret;
8901}
David Hildenbrand255f5982020-05-07 16:01:29 +02008902EXPORT_SYMBOL(alloc_contig_range);
Anshuman Khandual5e27a2d2019-11-30 17:55:06 -08008903
8904static int __alloc_contig_pages(unsigned long start_pfn,
8905 unsigned long nr_pages, gfp_t gfp_mask)
8906{
8907 unsigned long end_pfn = start_pfn + nr_pages;
8908
8909 return alloc_contig_range(start_pfn, end_pfn, MIGRATE_MOVABLE,
8910 gfp_mask);
8911}
8912
8913static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn,
8914 unsigned long nr_pages)
8915{
8916 unsigned long i, end_pfn = start_pfn + nr_pages;
8917 struct page *page;
8918
8919 for (i = start_pfn; i < end_pfn; i++) {
8920 page = pfn_to_online_page(i);
8921 if (!page)
8922 return false;
8923
8924 if (page_zone(page) != z)
8925 return false;
8926
8927 if (PageReserved(page))
8928 return false;
Anshuman Khandual5e27a2d2019-11-30 17:55:06 -08008929 }
8930 return true;
8931}
8932
8933static bool zone_spans_last_pfn(const struct zone *zone,
8934 unsigned long start_pfn, unsigned long nr_pages)
8935{
8936 unsigned long last_pfn = start_pfn + nr_pages - 1;
8937
8938 return zone_spans_pfn(zone, last_pfn);
8939}
8940
8941/**
8942 * alloc_contig_pages() -- tries to find and allocate contiguous range of pages
8943 * @nr_pages: Number of contiguous pages to allocate
8944 * @gfp_mask: GFP mask to limit search and used during compaction
8945 * @nid: Target node
8946 * @nodemask: Mask for other possible nodes
8947 *
8948 * This routine is a wrapper around alloc_contig_range(). It scans over zones
8949 * on an applicable zonelist to find a contiguous pfn range which can then be
8950 * tried for allocation with alloc_contig_range(). This routine is intended
8951 * for allocation requests which can not be fulfilled with the buddy allocator.
8952 *
8953 * The allocated memory is always aligned to a page boundary. If nr_pages is a
8954 * power of two then the alignment is guaranteed to be to the given nr_pages
8955 * (e.g. 1GB request would be aligned to 1GB).
8956 *
8957 * Allocated pages can be freed with free_contig_range() or by manually calling
8958 * __free_page() on each allocated page.
8959 *
8960 * Return: pointer to contiguous pages on success, or NULL if not successful.
8961 */
8962struct page *alloc_contig_pages(unsigned long nr_pages, gfp_t gfp_mask,
8963 int nid, nodemask_t *nodemask)
8964{
8965 unsigned long ret, pfn, flags;
8966 struct zonelist *zonelist;
8967 struct zone *zone;
8968 struct zoneref *z;
8969
8970 zonelist = node_zonelist(nid, gfp_mask);
8971 for_each_zone_zonelist_nodemask(zone, z, zonelist,
8972 gfp_zone(gfp_mask), nodemask) {
8973 spin_lock_irqsave(&zone->lock, flags);
8974
8975 pfn = ALIGN(zone->zone_start_pfn, nr_pages);
8976 while (zone_spans_last_pfn(zone, pfn, nr_pages)) {
8977 if (pfn_range_valid_contig(zone, pfn, nr_pages)) {
8978 /*
8979 * We release the zone lock here because
8980 * alloc_contig_range() will also lock the zone
8981 * at some point. If there's an allocation
8982 * spinning on this lock, it may win the race
8983 * and cause alloc_contig_range() to fail...
8984 */
8985 spin_unlock_irqrestore(&zone->lock, flags);
8986 ret = __alloc_contig_pages(pfn, nr_pages,
8987 gfp_mask);
8988 if (!ret)
8989 return pfn_to_page(pfn);
8990 spin_lock_irqsave(&zone->lock, flags);
8991 }
8992 pfn += nr_pages;
8993 }
8994 spin_unlock_irqrestore(&zone->lock, flags);
8995 }
8996 return NULL;
8997}
Alexandre Ghiti4eb07162019-05-13 17:19:04 -07008998#endif /* CONFIG_CONTIG_ALLOC */
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008999
Minchan Kim78fa5152021-05-04 18:37:34 -07009000void free_contig_range(unsigned long pfn, unsigned long nr_pages)
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01009001{
Minchan Kim78fa5152021-05-04 18:37:34 -07009002 unsigned long count = 0;
Marek Szyprowskibcc2b022012-12-20 15:05:18 -08009003
9004 for (; nr_pages--; pfn++) {
9005 struct page *page = pfn_to_page(pfn);
9006
9007 count += page_count(page) != 1;
9008 __free_page(page);
9009 }
Minchan Kim78fa5152021-05-04 18:37:34 -07009010 WARN(count != 0, "%lu pages are still in use!\n", count);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01009011}
David Hildenbrand255f5982020-05-07 16:01:29 +02009012EXPORT_SYMBOL(free_contig_range);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01009013
Cody P Schafer0a647f32013-07-03 15:01:33 -07009014/*
9015 * The zone indicated has a new number of managed_pages; batch sizes and percpu
Ingo Molnarf0953a12021-05-06 18:06:47 -07009016 * page high values need to be recalculated.
Cody P Schafer0a647f32013-07-03 15:01:33 -07009017 */
Jiang Liu4ed7e022012-07-31 16:43:35 -07009018void __meminit zone_pcp_update(struct zone *zone)
9019{
Cody P Schaferc8e251f2013-07-03 15:01:29 -07009020 mutex_lock(&pcp_batch_high_lock);
Vlastimil Babka0a8b4f12020-12-14 19:10:43 -08009021 zone_set_pageset_high_and_batch(zone);
Cody P Schaferc8e251f2013-07-03 15:01:29 -07009022 mutex_unlock(&pcp_batch_high_lock);
Jiang Liu4ed7e022012-07-31 16:43:35 -07009023}
Jiang Liu4ed7e022012-07-31 16:43:35 -07009024
Vlastimil Babkaec6e8c7e2020-12-14 19:10:59 -08009025/*
9026 * Effectively disable pcplists for the zone by setting the high limit to 0
9027 * and draining all cpus. A concurrent page freeing on another CPU that's about
9028 * to put the page on pcplist will either finish before the drain and the page
9029 * will be drained, or observe the new high limit and skip the pcplist.
9030 *
9031 * Must be paired with a call to zone_pcp_enable().
9032 */
9033void zone_pcp_disable(struct zone *zone)
9034{
9035 mutex_lock(&pcp_batch_high_lock);
9036 __zone_set_pageset_high_and_batch(zone, 0, 1);
9037 __drain_all_pages(zone, true);
9038}
9039
9040void zone_pcp_enable(struct zone *zone)
9041{
9042 __zone_set_pageset_high_and_batch(zone, zone->pageset_high, zone->pageset_batch);
9043 mutex_unlock(&pcp_batch_high_lock);
9044}
9045
Jiang Liu340175b2012-07-31 16:43:32 -07009046void zone_pcp_reset(struct zone *zone)
9047{
Minchan Kim5a883812012-10-08 16:33:39 -07009048 int cpu;
9049 struct per_cpu_pageset *pset;
Jiang Liu340175b2012-07-31 16:43:32 -07009050
Jiang Liu340175b2012-07-31 16:43:32 -07009051 if (zone->pageset != &boot_pageset) {
Minchan Kim5a883812012-10-08 16:33:39 -07009052 for_each_online_cpu(cpu) {
9053 pset = per_cpu_ptr(zone->pageset, cpu);
9054 drain_zonestat(zone, pset);
9055 }
Jiang Liu340175b2012-07-31 16:43:32 -07009056 free_percpu(zone->pageset);
9057 zone->pageset = &boot_pageset;
9058 }
Jiang Liu340175b2012-07-31 16:43:32 -07009059}
9060
Wen Congyang6dcd73d2012-12-11 16:01:01 -08009061#ifdef CONFIG_MEMORY_HOTREMOVE
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07009062/*
David Hildenbrand257bea72020-10-15 20:07:59 -07009063 * All pages in the range must be in a single zone, must not contain holes,
9064 * must span full sections, and must be isolated before calling this function.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07009065 */
David Hildenbrand257bea72020-10-15 20:07:59 -07009066void __offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07009067{
David Hildenbrand257bea72020-10-15 20:07:59 -07009068 unsigned long pfn = start_pfn;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07009069 struct page *page;
9070 struct zone *zone;
David Hildenbrand0ee5f4f2019-11-30 17:54:03 -08009071 unsigned int order;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07009072 unsigned long flags;
Michal Hocko5557c762019-05-13 17:21:24 -07009073
Michal Hocko2d070ea2017-07-06 15:37:56 -07009074 offline_mem_sections(pfn, end_pfn);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07009075 zone = page_zone(pfn_to_page(pfn));
9076 spin_lock_irqsave(&zone->lock, flags);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07009077 while (pfn < end_pfn) {
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07009078 page = pfn_to_page(pfn);
Wen Congyangb023f462012-12-11 16:00:45 -08009079 /*
9080 * The HWPoisoned page may be not in buddy system, and
9081 * page_count() is not 0.
9082 */
9083 if (unlikely(!PageBuddy(page) && PageHWPoison(page))) {
9084 pfn++;
Wen Congyangb023f462012-12-11 16:00:45 -08009085 continue;
9086 }
David Hildenbrandaa218792020-05-07 16:01:30 +02009087 /*
9088 * At this point all remaining PageOffline() pages have a
9089 * reference count of 0 and can simply be skipped.
9090 */
9091 if (PageOffline(page)) {
9092 BUG_ON(page_count(page));
9093 BUG_ON(PageBuddy(page));
9094 pfn++;
David Hildenbrandaa218792020-05-07 16:01:30 +02009095 continue;
9096 }
Wen Congyangb023f462012-12-11 16:00:45 -08009097
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07009098 BUG_ON(page_count(page));
9099 BUG_ON(!PageBuddy(page));
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -07009100 order = buddy_order(page);
Alexander Duyck6ab01362020-04-06 20:04:49 -07009101 del_page_from_free_list(page, zone, order);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07009102 pfn += (1 << order);
9103 }
9104 spin_unlock_irqrestore(&zone->lock, flags);
9105}
9106#endif
Wu Fengguang8d22ba12009-12-16 12:19:58 +01009107
Wu Fengguang8d22ba12009-12-16 12:19:58 +01009108bool is_free_buddy_page(struct page *page)
9109{
9110 struct zone *zone = page_zone(page);
9111 unsigned long pfn = page_to_pfn(page);
9112 unsigned long flags;
Mel Gorman7aeb09f2014-06-04 16:10:21 -07009113 unsigned int order;
Wu Fengguang8d22ba12009-12-16 12:19:58 +01009114
9115 spin_lock_irqsave(&zone->lock, flags);
9116 for (order = 0; order < MAX_ORDER; order++) {
9117 struct page *page_head = page - (pfn & ((1 << order) - 1));
9118
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -07009119 if (PageBuddy(page_head) && buddy_order(page_head) >= order)
Wu Fengguang8d22ba12009-12-16 12:19:58 +01009120 break;
9121 }
9122 spin_unlock_irqrestore(&zone->lock, flags);
9123
9124 return order < MAX_ORDER;
9125}
Naoya Horiguchid4ae9912018-08-23 17:00:42 -07009126
9127#ifdef CONFIG_MEMORY_FAILURE
9128/*
Oscar Salvador06be6ff2020-10-15 20:07:05 -07009129 * Break down a higher-order page in sub-pages, and keep our target out of
9130 * buddy allocator.
Naoya Horiguchid4ae9912018-08-23 17:00:42 -07009131 */
Oscar Salvador06be6ff2020-10-15 20:07:05 -07009132static void break_down_buddy_pages(struct zone *zone, struct page *page,
9133 struct page *target, int low, int high,
9134 int migratetype)
9135{
9136 unsigned long size = 1 << high;
9137 struct page *current_buddy, *next_page;
9138
9139 while (high > low) {
9140 high--;
9141 size >>= 1;
9142
9143 if (target >= &page[size]) {
9144 next_page = page + size;
9145 current_buddy = page;
9146 } else {
9147 next_page = page;
9148 current_buddy = page + size;
9149 }
9150
9151 if (set_page_guard(zone, current_buddy, high, migratetype))
9152 continue;
9153
9154 if (current_buddy != target) {
9155 add_to_free_list(current_buddy, zone, high, migratetype);
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -07009156 set_buddy_order(current_buddy, high);
Oscar Salvador06be6ff2020-10-15 20:07:05 -07009157 page = next_page;
9158 }
9159 }
9160}
9161
9162/*
9163 * Take a page that will be marked as poisoned off the buddy allocator.
9164 */
9165bool take_page_off_buddy(struct page *page)
Naoya Horiguchid4ae9912018-08-23 17:00:42 -07009166{
9167 struct zone *zone = page_zone(page);
9168 unsigned long pfn = page_to_pfn(page);
9169 unsigned long flags;
9170 unsigned int order;
Oscar Salvador06be6ff2020-10-15 20:07:05 -07009171 bool ret = false;
Naoya Horiguchid4ae9912018-08-23 17:00:42 -07009172
9173 spin_lock_irqsave(&zone->lock, flags);
9174 for (order = 0; order < MAX_ORDER; order++) {
9175 struct page *page_head = page - (pfn & ((1 << order) - 1));
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -07009176 int page_order = buddy_order(page_head);
Naoya Horiguchid4ae9912018-08-23 17:00:42 -07009177
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -07009178 if (PageBuddy(page_head) && page_order >= order) {
Oscar Salvador06be6ff2020-10-15 20:07:05 -07009179 unsigned long pfn_head = page_to_pfn(page_head);
9180 int migratetype = get_pfnblock_migratetype(page_head,
9181 pfn_head);
9182
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -07009183 del_page_from_free_list(page_head, zone, page_order);
Oscar Salvador06be6ff2020-10-15 20:07:05 -07009184 break_down_buddy_pages(zone, page_head, page, 0,
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -07009185 page_order, migratetype);
Ding Huibac9c6f2021-06-04 20:01:21 -07009186 if (!is_migrate_isolate(migratetype))
9187 __mod_zone_freepage_state(zone, -1, migratetype);
Oscar Salvador06be6ff2020-10-15 20:07:05 -07009188 ret = true;
Naoya Horiguchid4ae9912018-08-23 17:00:42 -07009189 break;
9190 }
Oscar Salvador06be6ff2020-10-15 20:07:05 -07009191 if (page_count(page_head) > 0)
9192 break;
Naoya Horiguchid4ae9912018-08-23 17:00:42 -07009193 }
9194 spin_unlock_irqrestore(&zone->lock, flags);
Oscar Salvador06be6ff2020-10-15 20:07:05 -07009195 return ret;
Naoya Horiguchid4ae9912018-08-23 17:00:42 -07009196}
9197#endif