blob: eb2027892ef9077ffb2643c0b20939810dadd397 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/mm/page_alloc.c
3 *
4 * Manages the free list, the system allocates free pages here.
5 * Note that kmalloc() lives in slab.c
6 *
7 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
8 * Swap reorganised 29.12.95, Stephen Tweedie
9 * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
10 * Reshaped it to be a zoned allocator, Ingo Molnar, Red Hat, 1999
11 * Discontiguous memory support, Kanoj Sarcar, SGI, Nov 1999
12 * Zone balancing, Kanoj Sarcar, SGI, Jan 2000
13 * Per cpu hot/cold page lists, bulk allocation, Martin J. Bligh, Sept 2002
14 * (lots of bits borrowed from Ingo Molnar & Andrew Morton)
15 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/stddef.h>
18#include <linux/mm.h>
Arun KSca79b0c2018-12-28 00:34:29 -080019#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/swap.h>
21#include <linux/interrupt.h>
22#include <linux/pagemap.h>
KOSAKI Motohiro10ed2732008-03-04 14:28:32 -080023#include <linux/jiffies.h>
Yinghai Luedbe7d22010-08-25 13:39:16 -070024#include <linux/memblock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/compiler.h>
Randy Dunlap9f158332005-09-13 01:25:16 -070026#include <linux/kernel.h>
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -080027#include <linux/kasan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
29#include <linux/suspend.h>
30#include <linux/pagevec.h>
31#include <linux/blkdev.h>
32#include <linux/slab.h>
Dave Hansena238ab52011-05-24 17:12:16 -070033#include <linux/ratelimit.h>
David Rientjes5a3135c22007-10-16 23:25:53 -070034#include <linux/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/topology.h>
36#include <linux/sysctl.h>
37#include <linux/cpu.h>
38#include <linux/cpuset.h>
Dave Hansenbdc8cb92005-10-29 18:16:53 -070039#include <linux/memory_hotplug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/nodemask.h>
41#include <linux/vmalloc.h>
KOSAKI Motohiroa6cccdc2011-05-24 17:11:33 -070042#include <linux/vmstat.h>
Christoph Lameter4be38e32006-01-06 00:11:17 -080043#include <linux/mempolicy.h>
Dan Williams4b94ffd2016-01-15 16:56:22 -080044#include <linux/memremap.h>
Yasunori Goto68113782006-06-23 02:03:11 -070045#include <linux/stop_machine.h>
Mel Gormanc7132162006-09-27 01:49:43 -070046#include <linux/sort.h>
47#include <linux/pfn.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070048#include <linux/backing-dev.h>
Akinobu Mita933e3122006-12-08 02:39:45 -080049#include <linux/fault-inject.h>
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -070050#include <linux/page-isolation.h>
Joonsoo Kimeefa864b2014-12-12 16:55:46 -080051#include <linux/page_ext.h>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070052#include <linux/debugobjects.h>
Catalin Marinasdbb1f812009-06-11 13:23:19 +010053#include <linux/kmemleak.h>
Mel Gorman56de7262010-05-24 14:32:30 -070054#include <linux/compaction.h>
Mel Gorman0d3d0622009-09-21 17:02:44 -070055#include <trace/events/kmem.h>
Michal Hockod379f012017-02-22 15:42:00 -080056#include <trace/events/oom.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070057#include <linux/prefetch.h>
Lisa Du6e543d52013-09-11 14:22:36 -070058#include <linux/mm_inline.h>
Michal Nazarewicz041d3a82011-12-29 13:09:50 +010059#include <linux/migrate.h>
David Rientjes949f7ec2013-04-29 15:07:48 -070060#include <linux/hugetlb.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060061#include <linux/sched/rt.h>
Ingo Molnar5b3cc152017-02-02 20:43:54 +010062#include <linux/sched/mm.h>
Joonsoo Kim48c96a32014-12-12 16:56:01 -080063#include <linux/page_owner.h>
Mel Gorman0e1cc952015-06-30 14:57:27 -070064#include <linux/kthread.h>
Vladimir Davydov49491482016-07-26 15:24:24 -070065#include <linux/memcontrol.h>
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -050066#include <linux/ftrace.h>
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +010067#include <linux/lockdep.h>
Chen Yu556b9692017-08-25 15:55:30 -070068#include <linux/nmi.h>
Johannes Weinereb414682018-10-26 15:06:27 -070069#include <linux/psi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Jiang Liu7ee3d4e2013-07-03 15:03:41 -070071#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <asm/tlbflush.h>
Andrew Mortonac924c62006-05-15 09:43:59 -070073#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include "internal.h"
75
Cody P Schaferc8e251f2013-07-03 15:01:29 -070076/* prevent >1 _updater_ of zone percpu pageset ->high and ->batch fields */
77static DEFINE_MUTEX(pcp_batch_high_lock);
David Rientjes7cd2b0a2014-06-23 13:22:04 -070078#define MIN_PERCPU_PAGELIST_FRACTION (8)
Cody P Schaferc8e251f2013-07-03 15:01:29 -070079
Lee Schermerhorn72812012010-05-26 14:44:56 -070080#ifdef CONFIG_USE_PERCPU_NUMA_NODE_ID
81DEFINE_PER_CPU(int, numa_node);
82EXPORT_PER_CPU_SYMBOL(numa_node);
83#endif
84
Kemi Wang45180852017-11-15 17:38:22 -080085DEFINE_STATIC_KEY_TRUE(vm_numa_stat_key);
86
Lee Schermerhorn7aac7892010-05-26 14:45:00 -070087#ifdef CONFIG_HAVE_MEMORYLESS_NODES
88/*
89 * N.B., Do NOT reference the '_numa_mem_' per cpu variable directly.
90 * It will not be defined when CONFIG_HAVE_MEMORYLESS_NODES is not defined.
91 * Use the accessor functions set_numa_mem(), numa_mem_id() and cpu_to_mem()
92 * defined in <linux/topology.h>.
93 */
94DEFINE_PER_CPU(int, _numa_mem_); /* Kernel "local memory" node */
95EXPORT_PER_CPU_SYMBOL(_numa_mem_);
Joonsoo Kimad2c8142014-10-09 15:26:13 -070096int _node_numa_mem_[MAX_NUMNODES];
Lee Schermerhorn7aac7892010-05-26 14:45:00 -070097#endif
98
Mel Gormanbd233f52017-02-24 14:56:56 -080099/* work_structs for global per-cpu drains */
100DEFINE_MUTEX(pcpu_drain_mutex);
101DEFINE_PER_CPU(struct work_struct, pcpu_drain);
102
Emese Revfy38addce2016-06-20 20:41:19 +0200103#ifdef CONFIG_GCC_PLUGIN_LATENT_ENTROPY
Kees Cook58bea412016-10-19 00:08:04 +0200104volatile unsigned long latent_entropy __latent_entropy;
Emese Revfy38addce2016-06-20 20:41:19 +0200105EXPORT_SYMBOL(latent_entropy);
106#endif
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/*
Christoph Lameter13808912007-10-16 01:25:27 -0700109 * Array of node states.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 */
Christoph Lameter13808912007-10-16 01:25:27 -0700111nodemask_t node_states[NR_NODE_STATES] __read_mostly = {
112 [N_POSSIBLE] = NODE_MASK_ALL,
113 [N_ONLINE] = { { [0] = 1UL } },
114#ifndef CONFIG_NUMA
115 [N_NORMAL_MEMORY] = { { [0] = 1UL } },
116#ifdef CONFIG_HIGHMEM
117 [N_HIGH_MEMORY] = { { [0] = 1UL } },
118#endif
Lai Jiangshan20b2f522012-12-12 13:52:00 -0800119 [N_MEMORY] = { { [0] = 1UL } },
Christoph Lameter13808912007-10-16 01:25:27 -0700120 [N_CPU] = { { [0] = 1UL } },
121#endif /* NUMA */
122};
123EXPORT_SYMBOL(node_states);
124
Jiang Liuc3d5f5f2013-07-03 15:03:14 -0700125/* Protect totalram_pages and zone->managed_pages */
126static DEFINE_SPINLOCK(managed_page_count_lock);
127
Arun KSca79b0c2018-12-28 00:34:29 -0800128atomic_long_t _totalram_pages __read_mostly;
129EXPORT_SYMBOL(_totalram_pages);
Hideo AOKIcb45b0e2006-04-10 22:52:59 -0700130unsigned long totalreserve_pages __read_mostly;
Pintu Kumare48322a2014-12-18 16:17:15 -0800131unsigned long totalcma_pages __read_mostly;
Johannes Weinerab8fabd2012-01-10 15:07:42 -0800132
Hugh Dickins1b76b022012-05-11 01:00:07 -0700133int percpu_pagelist_fraction;
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +1000134gfp_t gfp_allowed_mask __read_mostly = GFP_BOOT_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Vlastimil Babkabb14c2c2015-09-08 15:01:25 -0700136/*
137 * A cached value of the page's pageblock's migratetype, used when the page is
138 * put on a pcplist. Used to avoid the pageblock migratetype lookup when
139 * freeing from pcplists in most cases, at the cost of possibly becoming stale.
140 * Also the migratetype set in the page does not necessarily match the pcplist
141 * index, e.g. page might have MIGRATE_CMA set but be on a pcplist with any
142 * other index - this ensures that it will be put on the correct CMA freelist.
143 */
144static inline int get_pcppage_migratetype(struct page *page)
145{
146 return page->index;
147}
148
149static inline void set_pcppage_migratetype(struct page *page, int migratetype)
150{
151 page->index = migratetype;
152}
153
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800154#ifdef CONFIG_PM_SLEEP
155/*
156 * The following functions are used by the suspend/hibernate code to temporarily
157 * change gfp_allowed_mask in order to avoid using I/O during memory allocations
158 * while devices are suspended. To avoid races with the suspend/hibernate code,
Pingfan Liu55f25032018-07-31 16:51:32 +0800159 * they should always be called with system_transition_mutex held
160 * (gfp_allowed_mask also should only be modified with system_transition_mutex
161 * held, unless the suspend/hibernate code is guaranteed not to run in parallel
162 * with that modification).
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800163 */
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100164
165static gfp_t saved_gfp_mask;
166
167void pm_restore_gfp_mask(void)
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800168{
Pingfan Liu55f25032018-07-31 16:51:32 +0800169 WARN_ON(!mutex_is_locked(&system_transition_mutex));
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100170 if (saved_gfp_mask) {
171 gfp_allowed_mask = saved_gfp_mask;
172 saved_gfp_mask = 0;
173 }
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800174}
175
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100176void pm_restrict_gfp_mask(void)
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800177{
Pingfan Liu55f25032018-07-31 16:51:32 +0800178 WARN_ON(!mutex_is_locked(&system_transition_mutex));
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100179 WARN_ON(saved_gfp_mask);
180 saved_gfp_mask = gfp_allowed_mask;
Mel Gormand0164ad2015-11-06 16:28:21 -0800181 gfp_allowed_mask &= ~(__GFP_IO | __GFP_FS);
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800182}
Mel Gormanf90ac392012-01-10 15:07:15 -0800183
184bool pm_suspended_storage(void)
185{
Mel Gormand0164ad2015-11-06 16:28:21 -0800186 if ((gfp_allowed_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS))
Mel Gormanf90ac392012-01-10 15:07:15 -0800187 return false;
188 return true;
189}
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800190#endif /* CONFIG_PM_SLEEP */
191
Mel Gormand9c23402007-10-16 01:26:01 -0700192#ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -0800193unsigned int pageblock_order __read_mostly;
Mel Gormand9c23402007-10-16 01:26:01 -0700194#endif
195
Hugh Dickinsd98c7a02006-02-14 13:52:59 -0800196static void __free_pages_ok(struct page *page, unsigned int order);
David Howellsa226f6c2006-01-06 00:11:08 -0800197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198/*
199 * results with 256, 32 in the lowmem_reserve sysctl:
200 * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
201 * 1G machine -> (16M dma, 784M normal, 224M high)
202 * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
203 * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
Yaowei Bai84109e12015-02-12 15:00:22 -0800204 * HIGHMEM allocation will leave (224M+784M)/256 of ram reserved in ZONE_DMA
Andi Kleena2f1b422005-11-05 17:25:53 +0100205 *
206 * TBD: should special case ZONE_DMA32 machines here - in those we normally
207 * don't need any ZONE_NORMAL reservation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 */
Joonsoo Kimd3cda232018-04-10 16:30:11 -0700209int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES] = {
Christoph Lameter4b51d662007-02-10 01:43:10 -0800210#ifdef CONFIG_ZONE_DMA
Joonsoo Kimd3cda232018-04-10 16:30:11 -0700211 [ZONE_DMA] = 256,
Christoph Lameter4b51d662007-02-10 01:43:10 -0800212#endif
Christoph Lameterfb0e7942006-09-25 23:31:13 -0700213#ifdef CONFIG_ZONE_DMA32
Joonsoo Kimd3cda232018-04-10 16:30:11 -0700214 [ZONE_DMA32] = 256,
Christoph Lameterfb0e7942006-09-25 23:31:13 -0700215#endif
Joonsoo Kimd3cda232018-04-10 16:30:11 -0700216 [ZONE_NORMAL] = 32,
Christoph Lametere53ef382006-09-25 23:31:14 -0700217#ifdef CONFIG_HIGHMEM
Joonsoo Kimd3cda232018-04-10 16:30:11 -0700218 [ZONE_HIGHMEM] = 0,
Christoph Lametere53ef382006-09-25 23:31:14 -0700219#endif
Joonsoo Kimd3cda232018-04-10 16:30:11 -0700220 [ZONE_MOVABLE] = 0,
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700221};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223EXPORT_SYMBOL(totalram_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Helge Deller15ad7cd2006-12-06 20:40:36 -0800225static char * const zone_names[MAX_NR_ZONES] = {
Christoph Lameter4b51d662007-02-10 01:43:10 -0800226#ifdef CONFIG_ZONE_DMA
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700227 "DMA",
Christoph Lameter4b51d662007-02-10 01:43:10 -0800228#endif
Christoph Lameterfb0e7942006-09-25 23:31:13 -0700229#ifdef CONFIG_ZONE_DMA32
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700230 "DMA32",
Christoph Lameterfb0e7942006-09-25 23:31:13 -0700231#endif
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700232 "Normal",
Christoph Lametere53ef382006-09-25 23:31:14 -0700233#ifdef CONFIG_HIGHMEM
Mel Gorman2a1e2742007-07-17 04:03:12 -0700234 "HighMem",
Christoph Lametere53ef382006-09-25 23:31:14 -0700235#endif
Mel Gorman2a1e2742007-07-17 04:03:12 -0700236 "Movable",
Dan Williams033fbae2015-08-09 15:29:06 -0400237#ifdef CONFIG_ZONE_DEVICE
238 "Device",
239#endif
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700240};
241
Vlastimil Babka60f30352016-03-15 14:56:08 -0700242char * const migratetype_names[MIGRATE_TYPES] = {
243 "Unmovable",
244 "Movable",
245 "Reclaimable",
246 "HighAtomic",
247#ifdef CONFIG_CMA
248 "CMA",
249#endif
250#ifdef CONFIG_MEMORY_ISOLATION
251 "Isolate",
252#endif
253};
254
Kirill A. Shutemovf1e61552015-11-06 16:29:50 -0800255compound_page_dtor * const compound_page_dtors[] = {
256 NULL,
257 free_compound_page,
258#ifdef CONFIG_HUGETLB_PAGE
259 free_huge_page,
260#endif
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800261#ifdef CONFIG_TRANSPARENT_HUGEPAGE
262 free_transhuge_page,
263#endif
Kirill A. Shutemovf1e61552015-11-06 16:29:50 -0800264};
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266int min_free_kbytes = 1024;
Han Pingtian42aa83c2014-01-23 15:53:28 -0800267int user_min_free_kbytes = -1;
Johannes Weiner795ae7a2016-03-17 14:19:14 -0700268int watermark_scale_factor = 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
David Rientjes7f16f912018-04-05 16:23:12 -0700270static unsigned long nr_kernel_pages __meminitdata;
271static unsigned long nr_all_pages __meminitdata;
272static unsigned long dma_reserve __meminitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Tejun Heo0ee332c2011-12-08 10:22:09 -0800274#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
David Rientjes7f16f912018-04-05 16:23:12 -0700275static unsigned long arch_zone_lowest_possible_pfn[MAX_NR_ZONES] __meminitdata;
276static unsigned long arch_zone_highest_possible_pfn[MAX_NR_ZONES] __meminitdata;
277static unsigned long required_kernelcore __initdata;
David Rientjesa5c6d652018-04-05 16:23:09 -0700278static unsigned long required_kernelcore_percent __initdata;
David Rientjes7f16f912018-04-05 16:23:12 -0700279static unsigned long required_movablecore __initdata;
David Rientjesa5c6d652018-04-05 16:23:09 -0700280static unsigned long required_movablecore_percent __initdata;
David Rientjes7f16f912018-04-05 16:23:12 -0700281static unsigned long zone_movable_pfn[MAX_NUMNODES] __meminitdata;
282static bool mirrored_kernelcore __meminitdata;
Mel Gormanc7132162006-09-27 01:49:43 -0700283
Tejun Heo0ee332c2011-12-08 10:22:09 -0800284/* movable_zone is the "real" zone pages in ZONE_MOVABLE are taken from */
285int movable_zone;
286EXPORT_SYMBOL(movable_zone);
287#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
Mel Gormanc7132162006-09-27 01:49:43 -0700288
Miklos Szeredi418508c2007-05-23 13:57:55 -0700289#if MAX_NUMNODES > 1
290int nr_node_ids __read_mostly = MAX_NUMNODES;
Christoph Lameter62bc62a2009-06-16 15:32:15 -0700291int nr_online_nodes __read_mostly = 1;
Miklos Szeredi418508c2007-05-23 13:57:55 -0700292EXPORT_SYMBOL(nr_node_ids);
Christoph Lameter62bc62a2009-06-16 15:32:15 -0700293EXPORT_SYMBOL(nr_online_nodes);
Miklos Szeredi418508c2007-05-23 13:57:55 -0700294#endif
295
Mel Gorman9ef9acb2007-10-16 01:25:54 -0700296int page_group_by_mobility_disabled __read_mostly;
297
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700298#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700299/* Returns true if the struct page for the pfn is uninitialised */
Mel Gorman0e1cc952015-06-30 14:57:27 -0700300static inline bool __meminit early_page_uninitialised(unsigned long pfn)
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700301{
Mel Gormanef70b6f2016-07-14 12:07:23 -0700302 int nid = early_pfn_to_nid(pfn);
303
304 if (node_online(nid) && pfn >= NODE_DATA(nid)->first_deferred_pfn)
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700305 return true;
306
307 return false;
308}
309
310/*
Pavel Tatashind3035be2018-10-26 15:09:37 -0700311 * Returns true when the remaining initialisation should be deferred until
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700312 * later in the boot cycle when it can be parallelised.
313 */
Pavel Tatashind3035be2018-10-26 15:09:37 -0700314static bool __meminit
315defer_init(int nid, unsigned long pfn, unsigned long end_pfn)
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700316{
Pavel Tatashind3035be2018-10-26 15:09:37 -0700317 static unsigned long prev_end_pfn, nr_initialised;
318
319 /*
320 * prev_end_pfn static that contains the end of previous zone
321 * No need to protect because called very early in boot before smp_init.
322 */
323 if (prev_end_pfn != end_pfn) {
324 prev_end_pfn = end_pfn;
325 nr_initialised = 0;
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700326 }
327
Pavel Tatashind3035be2018-10-26 15:09:37 -0700328 /* Always populate low zones for address-constrained allocations */
329 if (end_pfn < pgdat_end_pfn(NODE_DATA(nid)))
330 return false;
331 nr_initialised++;
332 if ((nr_initialised > NODE_DATA(nid)->static_init_pgcnt) &&
333 (pfn & (PAGES_PER_SECTION - 1)) == 0) {
334 NODE_DATA(nid)->first_deferred_pfn = pfn;
335 return true;
336 }
337 return false;
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700338}
339#else
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700340static inline bool early_page_uninitialised(unsigned long pfn)
341{
342 return false;
343}
344
Pavel Tatashind3035be2018-10-26 15:09:37 -0700345static inline bool defer_init(int nid, unsigned long pfn, unsigned long end_pfn)
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700346{
Pavel Tatashind3035be2018-10-26 15:09:37 -0700347 return false;
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700348}
349#endif
350
Mel Gorman0b423ca2016-05-19 17:14:27 -0700351/* Return a pointer to the bitmap storing bits affecting a block of pages */
352static inline unsigned long *get_pageblock_bitmap(struct page *page,
353 unsigned long pfn)
354{
355#ifdef CONFIG_SPARSEMEM
356 return __pfn_to_section(pfn)->pageblock_flags;
357#else
358 return page_zone(page)->pageblock_flags;
359#endif /* CONFIG_SPARSEMEM */
360}
361
362static inline int pfn_to_bitidx(struct page *page, unsigned long pfn)
363{
364#ifdef CONFIG_SPARSEMEM
365 pfn &= (PAGES_PER_SECTION-1);
366 return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
367#else
368 pfn = pfn - round_down(page_zone(page)->zone_start_pfn, pageblock_nr_pages);
369 return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
370#endif /* CONFIG_SPARSEMEM */
371}
372
373/**
374 * get_pfnblock_flags_mask - Return the requested group of flags for the pageblock_nr_pages block of pages
375 * @page: The page within the block of interest
376 * @pfn: The target page frame number
377 * @end_bitidx: The last bit of interest to retrieve
378 * @mask: mask of bits that the caller is interested in
379 *
380 * Return: pageblock_bits flags
381 */
382static __always_inline unsigned long __get_pfnblock_flags_mask(struct page *page,
383 unsigned long pfn,
384 unsigned long end_bitidx,
385 unsigned long mask)
386{
387 unsigned long *bitmap;
388 unsigned long bitidx, word_bitidx;
389 unsigned long word;
390
391 bitmap = get_pageblock_bitmap(page, pfn);
392 bitidx = pfn_to_bitidx(page, pfn);
393 word_bitidx = bitidx / BITS_PER_LONG;
394 bitidx &= (BITS_PER_LONG-1);
395
396 word = bitmap[word_bitidx];
397 bitidx += end_bitidx;
398 return (word >> (BITS_PER_LONG - bitidx - 1)) & mask;
399}
400
401unsigned long get_pfnblock_flags_mask(struct page *page, unsigned long pfn,
402 unsigned long end_bitidx,
403 unsigned long mask)
404{
405 return __get_pfnblock_flags_mask(page, pfn, end_bitidx, mask);
406}
407
408static __always_inline int get_pfnblock_migratetype(struct page *page, unsigned long pfn)
409{
410 return __get_pfnblock_flags_mask(page, pfn, PB_migrate_end, MIGRATETYPE_MASK);
411}
412
413/**
414 * set_pfnblock_flags_mask - Set the requested group of flags for a pageblock_nr_pages block of pages
415 * @page: The page within the block of interest
416 * @flags: The flags to set
417 * @pfn: The target page frame number
418 * @end_bitidx: The last bit of interest
419 * @mask: mask of bits that the caller is interested in
420 */
421void set_pfnblock_flags_mask(struct page *page, unsigned long flags,
422 unsigned long pfn,
423 unsigned long end_bitidx,
424 unsigned long mask)
425{
426 unsigned long *bitmap;
427 unsigned long bitidx, word_bitidx;
428 unsigned long old_word, word;
429
430 BUILD_BUG_ON(NR_PAGEBLOCK_BITS != 4);
431
432 bitmap = get_pageblock_bitmap(page, pfn);
433 bitidx = pfn_to_bitidx(page, pfn);
434 word_bitidx = bitidx / BITS_PER_LONG;
435 bitidx &= (BITS_PER_LONG-1);
436
437 VM_BUG_ON_PAGE(!zone_spans_pfn(page_zone(page), pfn), page);
438
439 bitidx += end_bitidx;
440 mask <<= (BITS_PER_LONG - bitidx - 1);
441 flags <<= (BITS_PER_LONG - bitidx - 1);
442
443 word = READ_ONCE(bitmap[word_bitidx]);
444 for (;;) {
445 old_word = cmpxchg(&bitmap[word_bitidx], word, (word & ~mask) | flags);
446 if (word == old_word)
447 break;
448 word = old_word;
449 }
450}
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700451
Minchan Kimee6f5092012-07-31 16:43:50 -0700452void set_pageblock_migratetype(struct page *page, int migratetype)
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700453{
KOSAKI Motohiro5d0f3f72013-11-12 15:08:18 -0800454 if (unlikely(page_group_by_mobility_disabled &&
455 migratetype < MIGRATE_PCPTYPES))
Mel Gorman49255c62009-06-16 15:31:58 -0700456 migratetype = MIGRATE_UNMOVABLE;
457
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700458 set_pageblock_flags_group(page, (unsigned long)migratetype,
459 PB_migrate, PB_migrate_end);
460}
461
Nick Piggin13e74442006-01-06 00:10:58 -0800462#ifdef CONFIG_DEBUG_VM
Dave Hansenc6a57e12005-10-29 18:16:52 -0700463static int page_outside_zone_boundaries(struct zone *zone, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Dave Hansenbdc8cb92005-10-29 18:16:53 -0700465 int ret = 0;
466 unsigned seq;
467 unsigned long pfn = page_to_pfn(page);
Cody P Schaferb5e6a5a2013-02-22 16:35:28 -0800468 unsigned long sp, start_pfn;
Dave Hansenc6a57e12005-10-29 18:16:52 -0700469
Dave Hansenbdc8cb92005-10-29 18:16:53 -0700470 do {
471 seq = zone_span_seqbegin(zone);
Cody P Schaferb5e6a5a2013-02-22 16:35:28 -0800472 start_pfn = zone->zone_start_pfn;
473 sp = zone->spanned_pages;
Cody P Schafer108bcc92013-02-22 16:35:23 -0800474 if (!zone_spans_pfn(zone, pfn))
Dave Hansenbdc8cb92005-10-29 18:16:53 -0700475 ret = 1;
476 } while (zone_span_seqretry(zone, seq));
477
Cody P Schaferb5e6a5a2013-02-22 16:35:28 -0800478 if (ret)
Dave Hansen613813e2014-06-04 16:07:27 -0700479 pr_err("page 0x%lx outside node %d zone %s [ 0x%lx - 0x%lx ]\n",
480 pfn, zone_to_nid(zone), zone->name,
481 start_pfn, start_pfn + sp);
Cody P Schaferb5e6a5a2013-02-22 16:35:28 -0800482
Dave Hansenbdc8cb92005-10-29 18:16:53 -0700483 return ret;
Dave Hansenc6a57e12005-10-29 18:16:52 -0700484}
485
486static int page_is_consistent(struct zone *zone, struct page *page)
487{
Andy Whitcroft14e07292007-05-06 14:49:14 -0700488 if (!pfn_valid_within(page_to_pfn(page)))
Dave Hansenc6a57e12005-10-29 18:16:52 -0700489 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if (zone != page_zone(page))
Dave Hansenc6a57e12005-10-29 18:16:52 -0700491 return 0;
492
493 return 1;
494}
495/*
496 * Temporary debugging check for pages not lying within a given zone.
497 */
Matthias Kaehlcked73d3c9f2017-07-06 15:39:23 -0700498static int __maybe_unused bad_range(struct zone *zone, struct page *page)
Dave Hansenc6a57e12005-10-29 18:16:52 -0700499{
500 if (page_outside_zone_boundaries(zone, page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return 1;
Dave Hansenc6a57e12005-10-29 18:16:52 -0700502 if (!page_is_consistent(zone, page))
503 return 1;
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return 0;
506}
Nick Piggin13e74442006-01-06 00:10:58 -0800507#else
Matthias Kaehlcked73d3c9f2017-07-06 15:39:23 -0700508static inline int __maybe_unused bad_range(struct zone *zone, struct page *page)
Nick Piggin13e74442006-01-06 00:10:58 -0800509{
510 return 0;
511}
512#endif
513
Kirill A. Shutemovd230dec2014-04-07 15:37:38 -0700514static void bad_page(struct page *page, const char *reason,
515 unsigned long bad_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800517 static unsigned long resume;
518 static unsigned long nr_shown;
519 static unsigned long nr_unshown;
520
521 /*
522 * Allow a burst of 60 reports, then keep quiet for that minute;
523 * or allow a steady drip of one report per second.
524 */
525 if (nr_shown == 60) {
526 if (time_before(jiffies, resume)) {
527 nr_unshown++;
528 goto out;
529 }
530 if (nr_unshown) {
Vlastimil Babkaff8e8112016-03-15 14:56:24 -0700531 pr_alert(
Hugh Dickins1e9e6362009-01-06 14:40:13 -0800532 "BUG: Bad page state: %lu messages suppressed\n",
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800533 nr_unshown);
534 nr_unshown = 0;
535 }
536 nr_shown = 0;
537 }
538 if (nr_shown++ == 0)
539 resume = jiffies + 60 * HZ;
540
Vlastimil Babkaff8e8112016-03-15 14:56:24 -0700541 pr_alert("BUG: Bad page state in process %s pfn:%05lx\n",
Hugh Dickins3dc14742009-01-06 14:40:08 -0800542 current->comm, page_to_pfn(page));
Vlastimil Babkaff8e8112016-03-15 14:56:24 -0700543 __dump_page(page, reason);
544 bad_flags &= page->flags;
545 if (bad_flags)
546 pr_alert("bad because of flags: %#lx(%pGp)\n",
547 bad_flags, &bad_flags);
Vlastimil Babka4e462112016-03-15 14:56:21 -0700548 dump_page_owner(page);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700549
Dave Jones4f318882011-10-31 17:07:24 -0700550 print_modules();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 dump_stack();
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800552out:
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800553 /* Leave bad fields for debug, except PageBuddy could make trouble */
Mel Gorman22b751c2013-02-22 16:34:59 -0800554 page_mapcount_reset(page); /* remove PageBuddy */
Rusty Russell373d4d02013-01-21 17:17:39 +1030555 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558/*
559 * Higher-order pages are called "compound pages". They are structured thusly:
560 *
Kirill A. Shutemov1d798ca2015-11-06 16:29:54 -0800561 * The first PAGE_SIZE page is called the "head page" and have PG_head set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 *
Kirill A. Shutemov1d798ca2015-11-06 16:29:54 -0800563 * The remaining PAGE_SIZE pages are called "tail pages". PageTail() is encoded
564 * in bit 0 of page->compound_head. The rest of bits is pointer to head page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 *
Kirill A. Shutemov1d798ca2015-11-06 16:29:54 -0800566 * The first tail page's ->compound_dtor holds the offset in array of compound
567 * page destructors. See compound_page_dtors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 *
Kirill A. Shutemov1d798ca2015-11-06 16:29:54 -0800569 * The first tail page's ->compound_order holds the order of allocation.
Hugh Dickins41d78ba2006-02-14 13:52:58 -0800570 * This usage means that zero-order pages may not be compound.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 */
Hugh Dickinsd98c7a02006-02-14 13:52:59 -0800572
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800573void free_compound_page(struct page *page)
Hugh Dickinsd98c7a02006-02-14 13:52:59 -0800574{
Christoph Lameterd85f3382007-05-06 14:49:39 -0700575 __free_pages_ok(page, compound_order(page));
Hugh Dickinsd98c7a02006-02-14 13:52:59 -0800576}
577
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -0800578void prep_compound_page(struct page *page, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 int i;
581 int nr_pages = 1 << order;
582
Kirill A. Shutemovf1e61552015-11-06 16:29:50 -0800583 set_compound_page_dtor(page, COMPOUND_PAGE_DTOR);
Christoph Lameterd85f3382007-05-06 14:49:39 -0700584 set_compound_order(page, order);
Christoph Lameter6d777952007-05-06 14:49:40 -0700585 __SetPageHead(page);
Andy Whitcroft18229df2008-11-06 12:53:27 -0800586 for (i = 1; i < nr_pages; i++) {
587 struct page *p = page + i;
Youquan Song58a84aa2011-12-08 14:34:18 -0800588 set_page_count(p, 0);
Kirill A. Shutemov1c290f62016-01-15 16:52:07 -0800589 p->mapping = TAIL_MAPPING;
Kirill A. Shutemov1d798ca2015-11-06 16:29:54 -0800590 set_compound_head(p, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
Kirill A. Shutemov53f92632016-01-15 16:53:42 -0800592 atomic_set(compound_mapcount_ptr(page), -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -0800595#ifdef CONFIG_DEBUG_PAGEALLOC
596unsigned int _debug_guardpage_minorder;
Christian Borntraegerea6eabb2016-03-15 14:55:30 -0700597bool _debug_pagealloc_enabled __read_mostly
598 = IS_ENABLED(CONFIG_DEBUG_PAGEALLOC_ENABLE_DEFAULT);
Joonsoo Kim505f6d22016-03-17 14:17:56 -0700599EXPORT_SYMBOL(_debug_pagealloc_enabled);
Joonsoo Kime30825f2014-12-12 16:55:49 -0800600bool _debug_guardpage_enabled __read_mostly;
601
Joonsoo Kim031bc572014-12-12 16:55:52 -0800602static int __init early_debug_pagealloc(char *buf)
603{
604 if (!buf)
605 return -EINVAL;
Minfei Huang2a138dc2016-05-20 16:58:13 -0700606 return kstrtobool(buf, &_debug_pagealloc_enabled);
Joonsoo Kim031bc572014-12-12 16:55:52 -0800607}
608early_param("debug_pagealloc", early_debug_pagealloc);
609
Joonsoo Kime30825f2014-12-12 16:55:49 -0800610static bool need_debug_guardpage(void)
611{
Joonsoo Kim031bc572014-12-12 16:55:52 -0800612 /* If we don't use debug_pagealloc, we don't need guard page */
613 if (!debug_pagealloc_enabled())
614 return false;
615
Joonsoo Kimf1c1e9f2016-10-07 16:58:18 -0700616 if (!debug_guardpage_minorder())
617 return false;
618
Joonsoo Kime30825f2014-12-12 16:55:49 -0800619 return true;
620}
621
622static void init_debug_guardpage(void)
623{
Joonsoo Kim031bc572014-12-12 16:55:52 -0800624 if (!debug_pagealloc_enabled())
625 return;
626
Joonsoo Kimf1c1e9f2016-10-07 16:58:18 -0700627 if (!debug_guardpage_minorder())
628 return;
629
Joonsoo Kime30825f2014-12-12 16:55:49 -0800630 _debug_guardpage_enabled = true;
631}
632
633struct page_ext_operations debug_guardpage_ops = {
634 .need = need_debug_guardpage,
635 .init = init_debug_guardpage,
636};
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -0800637
638static int __init debug_guardpage_minorder_setup(char *buf)
639{
640 unsigned long res;
641
642 if (kstrtoul(buf, 10, &res) < 0 || res > MAX_ORDER / 2) {
Joe Perches11705322016-03-17 14:19:50 -0700643 pr_err("Bad debug_guardpage_minorder value\n");
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -0800644 return 0;
645 }
646 _debug_guardpage_minorder = res;
Joe Perches11705322016-03-17 14:19:50 -0700647 pr_info("Setting debug_guardpage_minorder to %lu\n", res);
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -0800648 return 0;
649}
Joonsoo Kimf1c1e9f2016-10-07 16:58:18 -0700650early_param("debug_guardpage_minorder", debug_guardpage_minorder_setup);
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -0800651
Joonsoo Kimacbc15a2016-10-07 16:58:15 -0700652static inline bool set_page_guard(struct zone *zone, struct page *page,
Joonsoo Kim2847cf92014-12-12 16:55:01 -0800653 unsigned int order, int migratetype)
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -0800654{
Joonsoo Kime30825f2014-12-12 16:55:49 -0800655 struct page_ext *page_ext;
656
657 if (!debug_guardpage_enabled())
Joonsoo Kimacbc15a2016-10-07 16:58:15 -0700658 return false;
659
660 if (order >= debug_guardpage_minorder())
661 return false;
Joonsoo Kime30825f2014-12-12 16:55:49 -0800662
663 page_ext = lookup_page_ext(page);
Yang Shif86e4272016-06-03 14:55:38 -0700664 if (unlikely(!page_ext))
Joonsoo Kimacbc15a2016-10-07 16:58:15 -0700665 return false;
Yang Shif86e4272016-06-03 14:55:38 -0700666
Joonsoo Kime30825f2014-12-12 16:55:49 -0800667 __set_bit(PAGE_EXT_DEBUG_GUARD, &page_ext->flags);
668
Joonsoo Kim2847cf92014-12-12 16:55:01 -0800669 INIT_LIST_HEAD(&page->lru);
670 set_page_private(page, order);
671 /* Guard pages are not available for any usage */
672 __mod_zone_freepage_state(zone, -(1 << order), migratetype);
Joonsoo Kimacbc15a2016-10-07 16:58:15 -0700673
674 return true;
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -0800675}
676
Joonsoo Kim2847cf92014-12-12 16:55:01 -0800677static inline void clear_page_guard(struct zone *zone, struct page *page,
678 unsigned int order, int migratetype)
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -0800679{
Joonsoo Kime30825f2014-12-12 16:55:49 -0800680 struct page_ext *page_ext;
681
682 if (!debug_guardpage_enabled())
683 return;
684
685 page_ext = lookup_page_ext(page);
Yang Shif86e4272016-06-03 14:55:38 -0700686 if (unlikely(!page_ext))
687 return;
688
Joonsoo Kime30825f2014-12-12 16:55:49 -0800689 __clear_bit(PAGE_EXT_DEBUG_GUARD, &page_ext->flags);
690
Joonsoo Kim2847cf92014-12-12 16:55:01 -0800691 set_page_private(page, 0);
692 if (!is_migrate_isolate(migratetype))
693 __mod_zone_freepage_state(zone, (1 << order), migratetype);
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -0800694}
695#else
Joonsoo Kim980ac162016-10-07 16:58:27 -0700696struct page_ext_operations debug_guardpage_ops;
Joonsoo Kimacbc15a2016-10-07 16:58:15 -0700697static inline bool set_page_guard(struct zone *zone, struct page *page,
698 unsigned int order, int migratetype) { return false; }
Joonsoo Kim2847cf92014-12-12 16:55:01 -0800699static inline void clear_page_guard(struct zone *zone, struct page *page,
700 unsigned int order, int migratetype) {}
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -0800701#endif
702
Mel Gorman7aeb09f2014-06-04 16:10:21 -0700703static inline void set_page_order(struct page *page, unsigned int order)
Andrew Morton6aa3001b22006-04-18 22:20:52 -0700704{
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700705 set_page_private(page, order);
Nick Piggin676165a2006-04-10 11:21:48 +1000706 __SetPageBuddy(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
709static inline void rmv_page_order(struct page *page)
710{
Nick Piggin676165a2006-04-10 11:21:48 +1000711 __ClearPageBuddy(page);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700712 set_page_private(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
715/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 * This function checks whether a page is free && is the buddy
Matthew Wilcox6e292b92018-06-07 17:08:18 -0700717 * we can coalesce a page and its buddy if
Vlastimil Babka13ad59d2017-02-22 15:41:51 -0800718 * (a) the buddy is not in a hole (check before calling!) &&
Nick Piggin676165a2006-04-10 11:21:48 +1000719 * (b) the buddy is in the buddy system &&
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700720 * (c) a page and its buddy have the same order &&
721 * (d) a page and its buddy are in the same zone.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 *
Matthew Wilcox6e292b92018-06-07 17:08:18 -0700723 * For recording whether a page is in the buddy system, we set PageBuddy.
724 * Setting, clearing, and testing PageBuddy is serialized by zone->lock.
Nick Piggin676165a2006-04-10 11:21:48 +1000725 *
726 * For recording page's order, we use page_private(page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 */
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700728static inline int page_is_buddy(struct page *page, struct page *buddy,
Mel Gorman7aeb09f2014-06-04 16:10:21 -0700729 unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -0800731 if (page_is_guard(buddy) && page_order(buddy) == order) {
Mel Gormand34c5fa2014-06-04 16:10:10 -0700732 if (page_zone_id(page) != page_zone_id(buddy))
733 return 0;
734
Weijie Yang4c5018c2015-02-10 14:11:39 -0800735 VM_BUG_ON_PAGE(page_count(buddy) != 0, buddy);
736
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -0800737 return 1;
738 }
739
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700740 if (PageBuddy(buddy) && page_order(buddy) == order) {
Mel Gormand34c5fa2014-06-04 16:10:10 -0700741 /*
742 * zone check is done late to avoid uselessly
743 * calculating zone/node ids for pages that could
744 * never merge.
745 */
746 if (page_zone_id(page) != page_zone_id(buddy))
747 return 0;
748
Weijie Yang4c5018c2015-02-10 14:11:39 -0800749 VM_BUG_ON_PAGE(page_count(buddy) != 0, buddy);
750
Andrew Morton6aa3001b22006-04-18 22:20:52 -0700751 return 1;
Nick Piggin676165a2006-04-10 11:21:48 +1000752 }
Andrew Morton6aa3001b22006-04-18 22:20:52 -0700753 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
756/*
757 * Freeing function for a buddy system allocator.
758 *
759 * The concept of a buddy system is to maintain direct-mapped table
760 * (containing bit values) for memory blocks of various "orders".
761 * The bottom level table contains the map for the smallest allocatable
762 * units of memory (here, pages), and each level above it describes
763 * pairs of units from the levels below, hence, "buddies".
764 * At a high level, all that happens here is marking the table entry
765 * at the bottom level available, and propagating the changes upward
766 * as necessary, plus some accounting needed to play nicely with other
767 * parts of the VM system.
768 * At each level, we keep a list of pages, which are heads of continuous
Matthew Wilcox6e292b92018-06-07 17:08:18 -0700769 * free pages of length of (1 << order) and marked with PageBuddy.
770 * Page's order is recorded in page_private(page) field.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 * So when we are allocating or freeing one, we can derive the state of the
Michal Nazarewicz5f63b722012-01-11 15:16:11 +0100772 * other. That is, if we allocate a small block, and both were
773 * free, the remainder of the region must be split into blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 * If a block is freed, and its buddy is also free, then this
Michal Nazarewicz5f63b722012-01-11 15:16:11 +0100775 * triggers coalescing into a block of larger size.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 *
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +0100777 * -- nyc
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 */
779
Nick Piggin48db57f2006-01-08 01:00:42 -0800780static inline void __free_one_page(struct page *page,
Mel Gormandc4b0ca2014-06-04 16:10:17 -0700781 unsigned long pfn,
Mel Gormaned0ae212009-06-16 15:32:07 -0700782 struct zone *zone, unsigned int order,
783 int migratetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Vlastimil Babka76741e72017-02-22 15:41:48 -0800785 unsigned long combined_pfn;
786 unsigned long uninitialized_var(buddy_pfn);
Corrado Zoccolo6dda9d52010-05-24 14:31:54 -0700787 struct page *buddy;
Vlastimil Babkad9dddbf2016-03-25 14:21:50 -0700788 unsigned int max_order;
789
790 max_order = min_t(unsigned int, MAX_ORDER, pageblock_order + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Cody P Schaferd29bb972013-02-22 16:35:25 -0800792 VM_BUG_ON(!zone_is_initialized(zone));
Kirill A. Shutemov6e9f0d52015-02-11 15:25:50 -0800793 VM_BUG_ON_PAGE(page->flags & PAGE_FLAGS_CHECK_AT_PREP, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Mel Gormaned0ae212009-06-16 15:32:07 -0700795 VM_BUG_ON(migratetype == -1);
Vlastimil Babkad9dddbf2016-03-25 14:21:50 -0700796 if (likely(!is_migrate_isolate(migratetype)))
Joonsoo Kim8f82b552014-11-13 15:19:18 -0800797 __mod_zone_freepage_state(zone, 1 << order, migratetype);
Mel Gormaned0ae212009-06-16 15:32:07 -0700798
Vlastimil Babka76741e72017-02-22 15:41:48 -0800799 VM_BUG_ON_PAGE(pfn & ((1 << order) - 1), page);
Sasha Levin309381fea2014-01-23 15:52:54 -0800800 VM_BUG_ON_PAGE(bad_range(zone, page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Vlastimil Babkad9dddbf2016-03-25 14:21:50 -0700802continue_merging:
Joonsoo Kim3c605092014-11-13 15:19:21 -0800803 while (order < max_order - 1) {
Vlastimil Babka76741e72017-02-22 15:41:48 -0800804 buddy_pfn = __find_buddy_pfn(pfn, order);
805 buddy = page + (buddy_pfn - pfn);
Vlastimil Babka13ad59d2017-02-22 15:41:51 -0800806
807 if (!pfn_valid_within(buddy_pfn))
808 goto done_merging;
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700809 if (!page_is_buddy(page, buddy, order))
Vlastimil Babkad9dddbf2016-03-25 14:21:50 -0700810 goto done_merging;
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -0800811 /*
812 * Our buddy is free or it is CONFIG_DEBUG_PAGEALLOC guard page,
813 * merge with it and move up one order.
814 */
815 if (page_is_guard(buddy)) {
Joonsoo Kim2847cf92014-12-12 16:55:01 -0800816 clear_page_guard(zone, buddy, order, migratetype);
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -0800817 } else {
818 list_del(&buddy->lru);
819 zone->free_area[order].nr_free--;
820 rmv_page_order(buddy);
821 }
Vlastimil Babka76741e72017-02-22 15:41:48 -0800822 combined_pfn = buddy_pfn & pfn;
823 page = page + (combined_pfn - pfn);
824 pfn = combined_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 order++;
826 }
Vlastimil Babkad9dddbf2016-03-25 14:21:50 -0700827 if (max_order < MAX_ORDER) {
828 /* If we are here, it means order is >= pageblock_order.
829 * We want to prevent merge between freepages on isolate
830 * pageblock and normal pageblock. Without this, pageblock
831 * isolation could cause incorrect freepage or CMA accounting.
832 *
833 * We don't want to hit this code for the more frequent
834 * low-order merging.
835 */
836 if (unlikely(has_isolate_pageblock(zone))) {
837 int buddy_mt;
838
Vlastimil Babka76741e72017-02-22 15:41:48 -0800839 buddy_pfn = __find_buddy_pfn(pfn, order);
840 buddy = page + (buddy_pfn - pfn);
Vlastimil Babkad9dddbf2016-03-25 14:21:50 -0700841 buddy_mt = get_pageblock_migratetype(buddy);
842
843 if (migratetype != buddy_mt
844 && (is_migrate_isolate(migratetype) ||
845 is_migrate_isolate(buddy_mt)))
846 goto done_merging;
847 }
848 max_order++;
849 goto continue_merging;
850 }
851
852done_merging:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 set_page_order(page, order);
Corrado Zoccolo6dda9d52010-05-24 14:31:54 -0700854
855 /*
856 * If this is not the largest possible page, check if the buddy
857 * of the next-highest order is free. If it is, it's possible
858 * that pages are being freed that will coalesce soon. In case,
859 * that is happening, add the free page to the tail of the list
860 * so it's less likely to be used soon and more likely to be merged
861 * as a higher order page
862 */
Vlastimil Babka13ad59d2017-02-22 15:41:51 -0800863 if ((order < MAX_ORDER-2) && pfn_valid_within(buddy_pfn)) {
Corrado Zoccolo6dda9d52010-05-24 14:31:54 -0700864 struct page *higher_page, *higher_buddy;
Vlastimil Babka76741e72017-02-22 15:41:48 -0800865 combined_pfn = buddy_pfn & pfn;
866 higher_page = page + (combined_pfn - pfn);
867 buddy_pfn = __find_buddy_pfn(combined_pfn, order + 1);
868 higher_buddy = higher_page + (buddy_pfn - combined_pfn);
Tony Luckb4fb8f62017-03-08 09:35:39 -0800869 if (pfn_valid_within(buddy_pfn) &&
870 page_is_buddy(higher_page, higher_buddy, order + 1)) {
Corrado Zoccolo6dda9d52010-05-24 14:31:54 -0700871 list_add_tail(&page->lru,
872 &zone->free_area[order].free_list[migratetype]);
873 goto out;
874 }
875 }
876
877 list_add(&page->lru, &zone->free_area[order].free_list[migratetype]);
878out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 zone->free_area[order].nr_free++;
880}
881
Mel Gorman7bfec6f2016-05-19 17:14:15 -0700882/*
883 * A bad page could be due to a number of fields. Instead of multiple branches,
884 * try and check multiple fields with one check. The caller must do a detailed
885 * check if necessary.
886 */
887static inline bool page_expected_state(struct page *page,
888 unsigned long check_flags)
889{
890 if (unlikely(atomic_read(&page->_mapcount) != -1))
891 return false;
892
893 if (unlikely((unsigned long)page->mapping |
894 page_ref_count(page) |
895#ifdef CONFIG_MEMCG
896 (unsigned long)page->mem_cgroup |
897#endif
898 (page->flags & check_flags)))
899 return false;
900
901 return true;
902}
903
Mel Gormanbb552ac2016-05-19 17:14:18 -0700904static void free_pages_check_bad(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Mel Gorman7bfec6f2016-05-19 17:14:15 -0700906 const char *bad_reason;
907 unsigned long bad_flags;
908
Mel Gorman7bfec6f2016-05-19 17:14:15 -0700909 bad_reason = NULL;
910 bad_flags = 0;
Dave Hansenf0b791a2014-01-23 15:52:49 -0800911
Kirill A. Shutemov53f92632016-01-15 16:53:42 -0800912 if (unlikely(atomic_read(&page->_mapcount) != -1))
Dave Hansenf0b791a2014-01-23 15:52:49 -0800913 bad_reason = "nonzero mapcount";
914 if (unlikely(page->mapping != NULL))
915 bad_reason = "non-NULL mapping";
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700916 if (unlikely(page_ref_count(page) != 0))
Joonsoo Kim0139aa72016-05-19 17:10:49 -0700917 bad_reason = "nonzero _refcount";
Dave Hansenf0b791a2014-01-23 15:52:49 -0800918 if (unlikely(page->flags & PAGE_FLAGS_CHECK_AT_FREE)) {
919 bad_reason = "PAGE_FLAGS_CHECK_AT_FREE flag(s) set";
920 bad_flags = PAGE_FLAGS_CHECK_AT_FREE;
921 }
Johannes Weiner9edad6e2014-12-10 15:44:58 -0800922#ifdef CONFIG_MEMCG
923 if (unlikely(page->mem_cgroup))
924 bad_reason = "page still charged to cgroup";
925#endif
Mel Gorman7bfec6f2016-05-19 17:14:15 -0700926 bad_page(page, bad_reason, bad_flags);
Mel Gormanbb552ac2016-05-19 17:14:18 -0700927}
928
929static inline int free_pages_check(struct page *page)
930{
Mel Gormanda838d42016-05-19 17:14:21 -0700931 if (likely(page_expected_state(page, PAGE_FLAGS_CHECK_AT_FREE)))
Mel Gormanbb552ac2016-05-19 17:14:18 -0700932 return 0;
Mel Gormanbb552ac2016-05-19 17:14:18 -0700933
934 /* Something has gone sideways, find it */
935 free_pages_check_bad(page);
Mel Gorman7bfec6f2016-05-19 17:14:15 -0700936 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
938
Mel Gorman4db75482016-05-19 17:14:32 -0700939static int free_tail_pages_check(struct page *head_page, struct page *page)
940{
941 int ret = 1;
942
943 /*
944 * We rely page->lru.next never has bit 0 set, unless the page
945 * is PageTail(). Let's make sure that's true even for poisoned ->lru.
946 */
947 BUILD_BUG_ON((unsigned long)LIST_POISON1 & 1);
948
949 if (!IS_ENABLED(CONFIG_DEBUG_VM)) {
950 ret = 0;
951 goto out;
952 }
953 switch (page - head_page) {
954 case 1:
Matthew Wilcox4da19842018-06-07 17:08:50 -0700955 /* the first tail page: ->mapping may be compound_mapcount() */
Mel Gorman4db75482016-05-19 17:14:32 -0700956 if (unlikely(compound_mapcount(page))) {
957 bad_page(page, "nonzero compound_mapcount", 0);
958 goto out;
959 }
960 break;
961 case 2:
962 /*
963 * the second tail page: ->mapping is
Matthew Wilcoxfa3015b2018-06-07 17:08:42 -0700964 * deferred_list.next -- ignore value.
Mel Gorman4db75482016-05-19 17:14:32 -0700965 */
966 break;
967 default:
968 if (page->mapping != TAIL_MAPPING) {
969 bad_page(page, "corrupted mapping in tail page", 0);
970 goto out;
971 }
972 break;
973 }
974 if (unlikely(!PageTail(page))) {
975 bad_page(page, "PageTail not set", 0);
976 goto out;
977 }
978 if (unlikely(compound_head(page) != head_page)) {
979 bad_page(page, "compound_head not consistent", 0);
980 goto out;
981 }
982 ret = 0;
983out:
984 page->mapping = NULL;
985 clear_compound_head(page);
986 return ret;
987}
988
Mel Gormane2769db2016-05-19 17:14:38 -0700989static __always_inline bool free_pages_prepare(struct page *page,
990 unsigned int order, bool check_free)
991{
992 int bad = 0;
993
994 VM_BUG_ON_PAGE(PageTail(page), page);
995
996 trace_mm_page_free(page, order);
Mel Gormane2769db2016-05-19 17:14:38 -0700997
998 /*
999 * Check tail pages before head page information is cleared to
1000 * avoid checking PageCompound for order-0 pages.
1001 */
1002 if (unlikely(order)) {
1003 bool compound = PageCompound(page);
1004 int i;
1005
1006 VM_BUG_ON_PAGE(compound && compound_order(page) != order, page);
1007
Kirill A. Shutemov9a73f612016-07-26 15:25:53 -07001008 if (compound)
1009 ClearPageDoubleMap(page);
Mel Gormane2769db2016-05-19 17:14:38 -07001010 for (i = 1; i < (1 << order); i++) {
1011 if (compound)
1012 bad += free_tail_pages_check(page, page + i);
1013 if (unlikely(free_pages_check(page + i))) {
1014 bad++;
1015 continue;
1016 }
1017 (page + i)->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
1018 }
1019 }
Minchan Kimbda807d2016-07-26 15:23:05 -07001020 if (PageMappingFlags(page))
Mel Gormane2769db2016-05-19 17:14:38 -07001021 page->mapping = NULL;
Vladimir Davydovc4159a72016-08-08 23:03:12 +03001022 if (memcg_kmem_enabled() && PageKmemcg(page))
Vladimir Davydov49491482016-07-26 15:24:24 -07001023 memcg_kmem_uncharge(page, order);
Mel Gormane2769db2016-05-19 17:14:38 -07001024 if (check_free)
1025 bad += free_pages_check(page);
1026 if (bad)
1027 return false;
1028
1029 page_cpupid_reset_last(page);
1030 page->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
1031 reset_page_owner(page, order);
1032
1033 if (!PageHighMem(page)) {
1034 debug_check_no_locks_freed(page_address(page),
1035 PAGE_SIZE << order);
1036 debug_check_no_obj_freed(page_address(page),
1037 PAGE_SIZE << order);
1038 }
1039 arch_free_page(page, order);
1040 kernel_poison_pages(page, 1 << order, 0);
1041 kernel_map_pages(page, 1 << order, 0);
seokhoon.yoon29b52de2016-05-20 16:58:47 -07001042 kasan_free_pages(page, order);
Mel Gormane2769db2016-05-19 17:14:38 -07001043
1044 return true;
1045}
Mel Gorman4db75482016-05-19 17:14:32 -07001046
1047#ifdef CONFIG_DEBUG_VM
1048static inline bool free_pcp_prepare(struct page *page)
1049{
Mel Gormane2769db2016-05-19 17:14:38 -07001050 return free_pages_prepare(page, 0, true);
Mel Gorman4db75482016-05-19 17:14:32 -07001051}
1052
1053static inline bool bulkfree_pcp_prepare(struct page *page)
1054{
1055 return false;
1056}
1057#else
1058static bool free_pcp_prepare(struct page *page)
1059{
Mel Gormane2769db2016-05-19 17:14:38 -07001060 return free_pages_prepare(page, 0, false);
Mel Gorman4db75482016-05-19 17:14:32 -07001061}
1062
1063static bool bulkfree_pcp_prepare(struct page *page)
1064{
1065 return free_pages_check(page);
1066}
1067#endif /* CONFIG_DEBUG_VM */
1068
Aaron Lu97334162018-04-05 16:24:14 -07001069static inline void prefetch_buddy(struct page *page)
1070{
1071 unsigned long pfn = page_to_pfn(page);
1072 unsigned long buddy_pfn = __find_buddy_pfn(pfn, 0);
1073 struct page *buddy = page + (buddy_pfn - pfn);
1074
1075 prefetch(buddy);
1076}
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078/*
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001079 * Frees a number of pages from the PCP lists
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 * Assumes all pages on list are in same zone, and of same order.
Renaud Lienhart207f36e2005-09-10 00:26:59 -07001081 * count is the number of pages to free.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 *
1083 * If the zone was previously in an "all pages pinned" state then look to
1084 * see if this freeing clears that state.
1085 *
1086 * And clear the zone's pages_scanned counter, to hold off the "all pages are
1087 * pinned" detection logic.
1088 */
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001089static void free_pcppages_bulk(struct zone *zone, int count,
1090 struct per_cpu_pages *pcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001092 int migratetype = 0;
Mel Gormana6f9edd62009-09-21 17:03:20 -07001093 int batch_free = 0;
Aaron Lu97334162018-04-05 16:24:14 -07001094 int prefetch_nr = 0;
Mel Gorman37779992016-05-19 17:13:58 -07001095 bool isolated_pageblocks;
Aaron Lu0a5f4e52018-04-05 16:24:10 -07001096 struct page *page, *tmp;
1097 LIST_HEAD(head);
Mel Gormanf2260e62009-06-16 15:32:13 -07001098
Mel Gormane5b31ac2016-05-19 17:14:24 -07001099 while (count) {
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001100 struct list_head *list;
Nick Piggin48db57f2006-01-08 01:00:42 -08001101
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001102 /*
Mel Gormana6f9edd62009-09-21 17:03:20 -07001103 * Remove pages from lists in a round-robin fashion. A
1104 * batch_free count is maintained that is incremented when an
1105 * empty list is encountered. This is so more pages are freed
1106 * off fuller lists instead of spinning excessively around empty
1107 * lists
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001108 */
1109 do {
Mel Gormana6f9edd62009-09-21 17:03:20 -07001110 batch_free++;
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001111 if (++migratetype == MIGRATE_PCPTYPES)
1112 migratetype = 0;
1113 list = &pcp->lists[migratetype];
1114 } while (list_empty(list));
1115
Namhyung Kim1d168712011-03-22 16:32:45 -07001116 /* This is the only non-empty list. Free them all. */
1117 if (batch_free == MIGRATE_PCPTYPES)
Mel Gormane5b31ac2016-05-19 17:14:24 -07001118 batch_free = count;
Namhyung Kim1d168712011-03-22 16:32:45 -07001119
Mel Gormana6f9edd62009-09-21 17:03:20 -07001120 do {
Geliang Tanga16601c2016-01-14 15:20:30 -08001121 page = list_last_entry(list, struct page, lru);
Aaron Lu0a5f4e52018-04-05 16:24:10 -07001122 /* must delete to avoid corrupting pcp list */
Mel Gormana6f9edd62009-09-21 17:03:20 -07001123 list_del(&page->lru);
Aaron Lu77ba9062018-04-05 16:24:06 -07001124 pcp->count--;
Vlastimil Babkaaa016d12015-09-08 15:01:22 -07001125
Mel Gorman4db75482016-05-19 17:14:32 -07001126 if (bulkfree_pcp_prepare(page))
1127 continue;
1128
Aaron Lu0a5f4e52018-04-05 16:24:10 -07001129 list_add_tail(&page->lru, &head);
Aaron Lu97334162018-04-05 16:24:14 -07001130
1131 /*
1132 * We are going to put the page back to the global
1133 * pool, prefetch its buddy to speed up later access
1134 * under zone->lock. It is believed the overhead of
1135 * an additional test and calculating buddy_pfn here
1136 * can be offset by reduced memory latency later. To
1137 * avoid excessive prefetching due to large count, only
1138 * prefetch buddy for the first pcp->batch nr of pages.
1139 */
1140 if (prefetch_nr++ < pcp->batch)
1141 prefetch_buddy(page);
Mel Gormane5b31ac2016-05-19 17:14:24 -07001142 } while (--count && --batch_free && !list_empty(list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
Aaron Lu0a5f4e52018-04-05 16:24:10 -07001144
1145 spin_lock(&zone->lock);
1146 isolated_pageblocks = has_isolate_pageblock(zone);
1147
1148 /*
1149 * Use safe version since after __free_one_page(),
1150 * page->lru.next will not point to original list.
1151 */
1152 list_for_each_entry_safe(page, tmp, &head, lru) {
1153 int mt = get_pcppage_migratetype(page);
1154 /* MIGRATE_ISOLATE page should not go to pcplists */
1155 VM_BUG_ON_PAGE(is_migrate_isolate(mt), page);
1156 /* Pageblock could have been isolated meanwhile */
1157 if (unlikely(isolated_pageblocks))
1158 mt = get_pageblock_migratetype(page);
1159
1160 __free_one_page(page, page_to_pfn(page), zone, 0, mt);
1161 trace_mm_page_pcpu_drain(page, 0, mt);
1162 }
Mel Gormand34b0732017-04-20 14:37:43 -07001163 spin_unlock(&zone->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164}
1165
Mel Gormandc4b0ca2014-06-04 16:10:17 -07001166static void free_one_page(struct zone *zone,
1167 struct page *page, unsigned long pfn,
Mel Gorman7aeb09f2014-06-04 16:10:21 -07001168 unsigned int order,
Mel Gormaned0ae212009-06-16 15:32:07 -07001169 int migratetype)
Nick Piggin48db57f2006-01-08 01:00:42 -08001170{
Mel Gormand34b0732017-04-20 14:37:43 -07001171 spin_lock(&zone->lock);
Joonsoo Kimad53f922014-11-13 15:19:11 -08001172 if (unlikely(has_isolate_pageblock(zone) ||
1173 is_migrate_isolate(migratetype))) {
1174 migratetype = get_pfnblock_migratetype(page, pfn);
Joonsoo Kimad53f922014-11-13 15:19:11 -08001175 }
Mel Gormandc4b0ca2014-06-04 16:10:17 -07001176 __free_one_page(page, pfn, zone, order, migratetype);
Mel Gormand34b0732017-04-20 14:37:43 -07001177 spin_unlock(&zone->lock);
Nick Piggin48db57f2006-01-08 01:00:42 -08001178}
1179
Robin Holt1e8ce832015-06-30 14:56:45 -07001180static void __meminit __init_single_page(struct page *page, unsigned long pfn,
Pavel Tatashind0dc12e2018-04-05 16:23:00 -07001181 unsigned long zone, int nid)
Robin Holt1e8ce832015-06-30 14:56:45 -07001182{
Pavel Tatashind0dc12e2018-04-05 16:23:00 -07001183 mm_zero_struct_page(page);
Robin Holt1e8ce832015-06-30 14:56:45 -07001184 set_page_links(page, zone, nid, pfn);
Robin Holt1e8ce832015-06-30 14:56:45 -07001185 init_page_count(page);
1186 page_mapcount_reset(page);
1187 page_cpupid_reset_last(page);
Andrey Konovalov2813b9c2018-12-28 00:30:57 -08001188 page_kasan_tag_reset(page);
Robin Holt1e8ce832015-06-30 14:56:45 -07001189
Robin Holt1e8ce832015-06-30 14:56:45 -07001190 INIT_LIST_HEAD(&page->lru);
1191#ifdef WANT_PAGE_VIRTUAL
1192 /* The shift won't overflow because ZONE_NORMAL is below 4G. */
1193 if (!is_highmem_idx(zone))
1194 set_page_address(page, __va(pfn << PAGE_SHIFT));
1195#endif
1196}
1197
Mel Gorman7e18adb2015-06-30 14:57:05 -07001198#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
Arnd Bergmann57148a62017-10-03 16:15:10 -07001199static void __meminit init_reserved_page(unsigned long pfn)
Mel Gorman7e18adb2015-06-30 14:57:05 -07001200{
1201 pg_data_t *pgdat;
1202 int nid, zid;
1203
1204 if (!early_page_uninitialised(pfn))
1205 return;
1206
1207 nid = early_pfn_to_nid(pfn);
1208 pgdat = NODE_DATA(nid);
1209
1210 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
1211 struct zone *zone = &pgdat->node_zones[zid];
1212
1213 if (pfn >= zone->zone_start_pfn && pfn < zone_end_pfn(zone))
1214 break;
1215 }
Pavel Tatashind0dc12e2018-04-05 16:23:00 -07001216 __init_single_page(pfn_to_page(pfn), pfn, zid, nid);
Mel Gorman7e18adb2015-06-30 14:57:05 -07001217}
1218#else
1219static inline void init_reserved_page(unsigned long pfn)
1220{
1221}
1222#endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
1223
Nathan Zimmer92923ca2015-06-30 14:56:48 -07001224/*
1225 * Initialised pages do not have PageReserved set. This function is
1226 * called for each range allocated by the bootmem allocator and
1227 * marks the pages PageReserved. The remaining valid pages are later
1228 * sent to the buddy page allocator.
1229 */
Stefan Bader4b50bcc2016-05-20 16:58:38 -07001230void __meminit reserve_bootmem_region(phys_addr_t start, phys_addr_t end)
Nathan Zimmer92923ca2015-06-30 14:56:48 -07001231{
1232 unsigned long start_pfn = PFN_DOWN(start);
1233 unsigned long end_pfn = PFN_UP(end);
1234
Mel Gorman7e18adb2015-06-30 14:57:05 -07001235 for (; start_pfn < end_pfn; start_pfn++) {
1236 if (pfn_valid(start_pfn)) {
1237 struct page *page = pfn_to_page(start_pfn);
1238
1239 init_reserved_page(start_pfn);
Kirill A. Shutemov1d798ca2015-11-06 16:29:54 -08001240
1241 /* Avoid false-positive PageTail() */
1242 INIT_LIST_HEAD(&page->lru);
1243
Alexander Duyckd483da52018-10-26 15:07:48 -07001244 /*
1245 * no need for atomic set_bit because the struct
1246 * page is not visible yet so nobody should
1247 * access it yet.
1248 */
1249 __SetPageReserved(page);
Mel Gorman7e18adb2015-06-30 14:57:05 -07001250 }
1251 }
Nathan Zimmer92923ca2015-06-30 14:56:48 -07001252}
1253
KOSAKI Motohiroec95f532010-05-24 14:32:38 -07001254static void __free_pages_ok(struct page *page, unsigned int order)
1255{
Mel Gormand34b0732017-04-20 14:37:43 -07001256 unsigned long flags;
Minchan Kim95e34412012-10-08 16:32:11 -07001257 int migratetype;
Mel Gormandc4b0ca2014-06-04 16:10:17 -07001258 unsigned long pfn = page_to_pfn(page);
KOSAKI Motohiroec95f532010-05-24 14:32:38 -07001259
Mel Gormane2769db2016-05-19 17:14:38 -07001260 if (!free_pages_prepare(page, order, true))
KOSAKI Motohiroec95f532010-05-24 14:32:38 -07001261 return;
1262
Mel Gormancfc47a22014-06-04 16:10:19 -07001263 migratetype = get_pfnblock_migratetype(page, pfn);
Mel Gormand34b0732017-04-20 14:37:43 -07001264 local_irq_save(flags);
1265 __count_vm_events(PGFREE, 1 << order);
Mel Gormandc4b0ca2014-06-04 16:10:17 -07001266 free_one_page(page_zone(page), page, pfn, order, migratetype);
Mel Gormand34b0732017-04-20 14:37:43 -07001267 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268}
1269
Li Zhang949698a2016-05-19 17:11:37 -07001270static void __init __free_pages_boot_core(struct page *page, unsigned int order)
David Howellsa226f6c2006-01-06 00:11:08 -08001271{
Johannes Weinerc3993072012-01-10 15:08:10 -08001272 unsigned int nr_pages = 1 << order;
Yinghai Lue2d0bd22013-09-11 14:20:37 -07001273 struct page *p = page;
Johannes Weinerc3993072012-01-10 15:08:10 -08001274 unsigned int loop;
David Howellsa226f6c2006-01-06 00:11:08 -08001275
Yinghai Lue2d0bd22013-09-11 14:20:37 -07001276 prefetchw(p);
1277 for (loop = 0; loop < (nr_pages - 1); loop++, p++) {
1278 prefetchw(p + 1);
Johannes Weinerc3993072012-01-10 15:08:10 -08001279 __ClearPageReserved(p);
1280 set_page_count(p, 0);
David Howellsa226f6c2006-01-06 00:11:08 -08001281 }
Yinghai Lue2d0bd22013-09-11 14:20:37 -07001282 __ClearPageReserved(p);
1283 set_page_count(p, 0);
Johannes Weinerc3993072012-01-10 15:08:10 -08001284
Arun KS9705bea2018-12-28 00:34:24 -08001285 atomic_long_add(nr_pages, &page_zone(page)->managed_pages);
Johannes Weinerc3993072012-01-10 15:08:10 -08001286 set_page_refcounted(page);
1287 __free_pages(page, order);
David Howellsa226f6c2006-01-06 00:11:08 -08001288}
1289
Mel Gorman75a592a2015-06-30 14:56:59 -07001290#if defined(CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID) || \
1291 defined(CONFIG_HAVE_MEMBLOCK_NODE_MAP)
Mel Gorman7ace9912015-08-06 15:46:13 -07001292
Mel Gorman75a592a2015-06-30 14:56:59 -07001293static struct mminit_pfnnid_cache early_pfnnid_cache __meminitdata;
1294
1295int __meminit early_pfn_to_nid(unsigned long pfn)
1296{
Mel Gorman7ace9912015-08-06 15:46:13 -07001297 static DEFINE_SPINLOCK(early_pfn_lock);
Mel Gorman75a592a2015-06-30 14:56:59 -07001298 int nid;
1299
Mel Gorman7ace9912015-08-06 15:46:13 -07001300 spin_lock(&early_pfn_lock);
Mel Gorman75a592a2015-06-30 14:56:59 -07001301 nid = __early_pfn_to_nid(pfn, &early_pfnnid_cache);
Mel Gorman7ace9912015-08-06 15:46:13 -07001302 if (nid < 0)
Mel Gormane4568d32016-07-14 12:07:20 -07001303 nid = first_online_node;
Mel Gorman7ace9912015-08-06 15:46:13 -07001304 spin_unlock(&early_pfn_lock);
1305
1306 return nid;
Mel Gorman75a592a2015-06-30 14:56:59 -07001307}
1308#endif
1309
1310#ifdef CONFIG_NODES_SPAN_OTHER_NODES
Matthias Kaehlcked73d3c9f2017-07-06 15:39:23 -07001311static inline bool __meminit __maybe_unused
1312meminit_pfn_in_nid(unsigned long pfn, int node,
1313 struct mminit_pfnnid_cache *state)
Mel Gorman75a592a2015-06-30 14:56:59 -07001314{
1315 int nid;
1316
1317 nid = __early_pfn_to_nid(pfn, state);
1318 if (nid >= 0 && nid != node)
1319 return false;
1320 return true;
1321}
1322
1323/* Only safe to use early in boot when initialisation is single-threaded */
1324static inline bool __meminit early_pfn_in_nid(unsigned long pfn, int node)
1325{
1326 return meminit_pfn_in_nid(pfn, node, &early_pfnnid_cache);
1327}
1328
1329#else
1330
1331static inline bool __meminit early_pfn_in_nid(unsigned long pfn, int node)
1332{
1333 return true;
1334}
Matthias Kaehlcked73d3c9f2017-07-06 15:39:23 -07001335static inline bool __meminit __maybe_unused
1336meminit_pfn_in_nid(unsigned long pfn, int node,
1337 struct mminit_pfnnid_cache *state)
Mel Gorman75a592a2015-06-30 14:56:59 -07001338{
1339 return true;
1340}
1341#endif
1342
1343
Mike Rapoport7c2ee342018-10-30 15:09:36 -07001344void __init memblock_free_pages(struct page *page, unsigned long pfn,
Mel Gorman3a80a7f2015-06-30 14:57:02 -07001345 unsigned int order)
1346{
1347 if (early_page_uninitialised(pfn))
1348 return;
Li Zhang949698a2016-05-19 17:11:37 -07001349 return __free_pages_boot_core(page, order);
Mel Gorman3a80a7f2015-06-30 14:57:02 -07001350}
1351
Joonsoo Kim7cf91a92016-03-15 14:57:51 -07001352/*
1353 * Check that the whole (or subset of) a pageblock given by the interval of
1354 * [start_pfn, end_pfn) is valid and within the same zone, before scanning it
1355 * with the migration of free compaction scanner. The scanners then need to
1356 * use only pfn_valid_within() check for arches that allow holes within
1357 * pageblocks.
1358 *
1359 * Return struct page pointer of start_pfn, or NULL if checks were not passed.
1360 *
1361 * It's possible on some configurations to have a setup like node0 node1 node0
1362 * i.e. it's possible that all pages within a zones range of pages do not
1363 * belong to a single zone. We assume that a border between node0 and node1
1364 * can occur within a single pageblock, but not a node0 node1 node0
1365 * interleaving within a single pageblock. It is therefore sufficient to check
1366 * the first and last page of a pageblock and avoid checking each individual
1367 * page in a pageblock.
1368 */
1369struct page *__pageblock_pfn_to_page(unsigned long start_pfn,
1370 unsigned long end_pfn, struct zone *zone)
1371{
1372 struct page *start_page;
1373 struct page *end_page;
1374
1375 /* end_pfn is one past the range we are checking */
1376 end_pfn--;
1377
1378 if (!pfn_valid(start_pfn) || !pfn_valid(end_pfn))
1379 return NULL;
1380
Michal Hocko2d070ea2017-07-06 15:37:56 -07001381 start_page = pfn_to_online_page(start_pfn);
1382 if (!start_page)
1383 return NULL;
Joonsoo Kim7cf91a92016-03-15 14:57:51 -07001384
1385 if (page_zone(start_page) != zone)
1386 return NULL;
1387
1388 end_page = pfn_to_page(end_pfn);
1389
1390 /* This gives a shorter code than deriving page_zone(end_page) */
1391 if (page_zone_id(start_page) != page_zone_id(end_page))
1392 return NULL;
1393
1394 return start_page;
1395}
1396
1397void set_zone_contiguous(struct zone *zone)
1398{
1399 unsigned long block_start_pfn = zone->zone_start_pfn;
1400 unsigned long block_end_pfn;
1401
1402 block_end_pfn = ALIGN(block_start_pfn + 1, pageblock_nr_pages);
1403 for (; block_start_pfn < zone_end_pfn(zone);
1404 block_start_pfn = block_end_pfn,
1405 block_end_pfn += pageblock_nr_pages) {
1406
1407 block_end_pfn = min(block_end_pfn, zone_end_pfn(zone));
1408
1409 if (!__pageblock_pfn_to_page(block_start_pfn,
1410 block_end_pfn, zone))
1411 return;
1412 }
1413
1414 /* We confirm that there is no hole */
1415 zone->contiguous = true;
1416}
1417
1418void clear_zone_contiguous(struct zone *zone)
1419{
1420 zone->contiguous = false;
1421}
1422
Mel Gorman7e18adb2015-06-30 14:57:05 -07001423#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001424static void __init deferred_free_range(unsigned long pfn,
1425 unsigned long nr_pages)
Mel Gormana4de83d2015-06-30 14:57:16 -07001426{
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001427 struct page *page;
1428 unsigned long i;
Mel Gormana4de83d2015-06-30 14:57:16 -07001429
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001430 if (!nr_pages)
Mel Gormana4de83d2015-06-30 14:57:16 -07001431 return;
1432
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001433 page = pfn_to_page(pfn);
1434
Mel Gormana4de83d2015-06-30 14:57:16 -07001435 /* Free a large naturally-aligned chunk if possible */
Xishi Qiue7801492016-10-07 16:58:09 -07001436 if (nr_pages == pageblock_nr_pages &&
1437 (pfn & (pageblock_nr_pages - 1)) == 0) {
Mel Gormanac5d2532015-06-30 14:57:20 -07001438 set_pageblock_migratetype(page, MIGRATE_MOVABLE);
Xishi Qiue7801492016-10-07 16:58:09 -07001439 __free_pages_boot_core(page, pageblock_order);
Mel Gormana4de83d2015-06-30 14:57:16 -07001440 return;
1441 }
1442
Xishi Qiue7801492016-10-07 16:58:09 -07001443 for (i = 0; i < nr_pages; i++, page++, pfn++) {
1444 if ((pfn & (pageblock_nr_pages - 1)) == 0)
1445 set_pageblock_migratetype(page, MIGRATE_MOVABLE);
Li Zhang949698a2016-05-19 17:11:37 -07001446 __free_pages_boot_core(page, 0);
Xishi Qiue7801492016-10-07 16:58:09 -07001447 }
Mel Gormana4de83d2015-06-30 14:57:16 -07001448}
1449
Nicolai Stanged3cd1312015-08-06 15:46:16 -07001450/* Completion tracking for deferred_init_memmap() threads */
1451static atomic_t pgdat_init_n_undone __initdata;
1452static __initdata DECLARE_COMPLETION(pgdat_init_all_done_comp);
1453
1454static inline void __init pgdat_init_report_one_done(void)
1455{
1456 if (atomic_dec_and_test(&pgdat_init_n_undone))
1457 complete(&pgdat_init_all_done_comp);
1458}
Mel Gorman0e1cc952015-06-30 14:57:27 -07001459
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001460/*
Pavel Tatashin80b1f412018-01-31 16:16:30 -08001461 * Returns true if page needs to be initialized or freed to buddy allocator.
1462 *
1463 * First we check if pfn is valid on architectures where it is possible to have
1464 * holes within pageblock_nr_pages. On systems where it is not possible, this
1465 * function is optimized out.
1466 *
1467 * Then, we check if a current large page is valid by only checking the validity
1468 * of the head pfn.
1469 *
1470 * Finally, meminit_pfn_in_nid is checked on systems where pfns can interleave
1471 * within a node: a pfn is between start and end of a node, but does not belong
1472 * to this memory node.
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001473 */
Pavel Tatashin80b1f412018-01-31 16:16:30 -08001474static inline bool __init
1475deferred_pfn_valid(int nid, unsigned long pfn,
1476 struct mminit_pfnnid_cache *nid_init_state)
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001477{
Pavel Tatashin80b1f412018-01-31 16:16:30 -08001478 if (!pfn_valid_within(pfn))
1479 return false;
1480 if (!(pfn & (pageblock_nr_pages - 1)) && !pfn_valid(pfn))
1481 return false;
1482 if (!meminit_pfn_in_nid(pfn, nid, nid_init_state))
1483 return false;
1484 return true;
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001485}
1486
Pavel Tatashin80b1f412018-01-31 16:16:30 -08001487/*
1488 * Free pages to buddy allocator. Try to free aligned pages in
1489 * pageblock_nr_pages sizes.
1490 */
1491static void __init deferred_free_pages(int nid, int zid, unsigned long pfn,
1492 unsigned long end_pfn)
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001493{
1494 struct mminit_pfnnid_cache nid_init_state = { };
1495 unsigned long nr_pgmask = pageblock_nr_pages - 1;
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001496 unsigned long nr_free = 0;
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001497
Pavel Tatashin80b1f412018-01-31 16:16:30 -08001498 for (; pfn < end_pfn; pfn++) {
1499 if (!deferred_pfn_valid(nid, pfn, &nid_init_state)) {
1500 deferred_free_range(pfn - nr_free, nr_free);
1501 nr_free = 0;
1502 } else if (!(pfn & nr_pgmask)) {
1503 deferred_free_range(pfn - nr_free, nr_free);
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001504 nr_free = 1;
Pavel Tatashin3a2d7fa2018-04-05 16:22:27 -07001505 touch_nmi_watchdog();
Pavel Tatashin80b1f412018-01-31 16:16:30 -08001506 } else {
1507 nr_free++;
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001508 }
1509 }
1510 /* Free the last block of pages to allocator */
Pavel Tatashin80b1f412018-01-31 16:16:30 -08001511 deferred_free_range(pfn - nr_free, nr_free);
1512}
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001513
Pavel Tatashin80b1f412018-01-31 16:16:30 -08001514/*
1515 * Initialize struct pages. We minimize pfn page lookups and scheduler checks
1516 * by performing it only once every pageblock_nr_pages.
1517 * Return number of pages initialized.
1518 */
1519static unsigned long __init deferred_init_pages(int nid, int zid,
1520 unsigned long pfn,
1521 unsigned long end_pfn)
1522{
1523 struct mminit_pfnnid_cache nid_init_state = { };
1524 unsigned long nr_pgmask = pageblock_nr_pages - 1;
1525 unsigned long nr_pages = 0;
1526 struct page *page = NULL;
1527
1528 for (; pfn < end_pfn; pfn++) {
1529 if (!deferred_pfn_valid(nid, pfn, &nid_init_state)) {
1530 page = NULL;
1531 continue;
1532 } else if (!page || !(pfn & nr_pgmask)) {
1533 page = pfn_to_page(pfn);
Pavel Tatashin3a2d7fa2018-04-05 16:22:27 -07001534 touch_nmi_watchdog();
Pavel Tatashin80b1f412018-01-31 16:16:30 -08001535 } else {
1536 page++;
1537 }
Pavel Tatashind0dc12e2018-04-05 16:23:00 -07001538 __init_single_page(page, pfn, zid, nid);
Pavel Tatashin80b1f412018-01-31 16:16:30 -08001539 nr_pages++;
1540 }
1541 return (nr_pages);
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001542}
1543
Mel Gorman7e18adb2015-06-30 14:57:05 -07001544/* Initialise remaining memory on a node */
Mel Gorman0e1cc952015-06-30 14:57:27 -07001545static int __init deferred_init_memmap(void *data)
Mel Gorman7e18adb2015-06-30 14:57:05 -07001546{
Mel Gorman0e1cc952015-06-30 14:57:27 -07001547 pg_data_t *pgdat = data;
1548 int nid = pgdat->node_id;
Mel Gorman7e18adb2015-06-30 14:57:05 -07001549 unsigned long start = jiffies;
1550 unsigned long nr_pages = 0;
Pavel Tatashin3a2d7fa2018-04-05 16:22:27 -07001551 unsigned long spfn, epfn, first_init_pfn, flags;
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001552 phys_addr_t spa, epa;
1553 int zid;
Mel Gorman7e18adb2015-06-30 14:57:05 -07001554 struct zone *zone;
Mel Gorman0e1cc952015-06-30 14:57:27 -07001555 const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001556 u64 i;
Mel Gorman7e18adb2015-06-30 14:57:05 -07001557
Mel Gorman0e1cc952015-06-30 14:57:27 -07001558 /* Bind memory initialisation thread to a local node if possible */
1559 if (!cpumask_empty(cpumask))
1560 set_cpus_allowed_ptr(current, cpumask);
Mel Gorman7e18adb2015-06-30 14:57:05 -07001561
Pavel Tatashin3a2d7fa2018-04-05 16:22:27 -07001562 pgdat_resize_lock(pgdat, &flags);
1563 first_init_pfn = pgdat->first_deferred_pfn;
1564 if (first_init_pfn == ULONG_MAX) {
1565 pgdat_resize_unlock(pgdat, &flags);
1566 pgdat_init_report_one_done();
1567 return 0;
1568 }
1569
Mel Gorman7e18adb2015-06-30 14:57:05 -07001570 /* Sanity check boundaries */
1571 BUG_ON(pgdat->first_deferred_pfn < pgdat->node_start_pfn);
1572 BUG_ON(pgdat->first_deferred_pfn > pgdat_end_pfn(pgdat));
1573 pgdat->first_deferred_pfn = ULONG_MAX;
1574
1575 /* Only the highest zone is deferred so find it */
1576 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
1577 zone = pgdat->node_zones + zid;
1578 if (first_init_pfn < zone_end_pfn(zone))
1579 break;
1580 }
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001581 first_init_pfn = max(zone->zone_start_pfn, first_init_pfn);
Mel Gorman7e18adb2015-06-30 14:57:05 -07001582
Pavel Tatashin80b1f412018-01-31 16:16:30 -08001583 /*
1584 * Initialize and free pages. We do it in two loops: first we initialize
1585 * struct page, than free to buddy allocator, because while we are
1586 * freeing pages we can access pages that are ahead (computing buddy
1587 * page in __free_one_page()).
1588 */
Pavel Tatashin2f47a912017-11-15 17:36:09 -08001589 for_each_free_mem_range(i, nid, MEMBLOCK_NONE, &spa, &epa, NULL) {
1590 spfn = max_t(unsigned long, first_init_pfn, PFN_UP(spa));
1591 epfn = min_t(unsigned long, zone_end_pfn(zone), PFN_DOWN(epa));
Pavel Tatashin80b1f412018-01-31 16:16:30 -08001592 nr_pages += deferred_init_pages(nid, zid, spfn, epfn);
1593 }
1594 for_each_free_mem_range(i, nid, MEMBLOCK_NONE, &spa, &epa, NULL) {
1595 spfn = max_t(unsigned long, first_init_pfn, PFN_UP(spa));
1596 epfn = min_t(unsigned long, zone_end_pfn(zone), PFN_DOWN(epa));
1597 deferred_free_pages(nid, zid, spfn, epfn);
Mel Gorman7e18adb2015-06-30 14:57:05 -07001598 }
Pavel Tatashin3a2d7fa2018-04-05 16:22:27 -07001599 pgdat_resize_unlock(pgdat, &flags);
Mel Gorman7e18adb2015-06-30 14:57:05 -07001600
1601 /* Sanity check that the next zone really is unpopulated */
1602 WARN_ON(++zid < MAX_NR_ZONES && populated_zone(++zone));
1603
Mel Gorman0e1cc952015-06-30 14:57:27 -07001604 pr_info("node %d initialised, %lu pages in %ums\n", nid, nr_pages,
Mel Gorman7e18adb2015-06-30 14:57:05 -07001605 jiffies_to_msecs(jiffies - start));
Nicolai Stanged3cd1312015-08-06 15:46:16 -07001606
1607 pgdat_init_report_one_done();
Mel Gorman0e1cc952015-06-30 14:57:27 -07001608 return 0;
1609}
Pavel Tatashinc9e97a12018-04-05 16:22:31 -07001610
1611/*
1612 * During boot we initialize deferred pages on-demand, as needed, but once
1613 * page_alloc_init_late() has finished, the deferred pages are all initialized,
1614 * and we can permanently disable that path.
1615 */
1616static DEFINE_STATIC_KEY_TRUE(deferred_pages);
1617
1618/*
1619 * If this zone has deferred pages, try to grow it by initializing enough
1620 * deferred pages to satisfy the allocation specified by order, rounded up to
1621 * the nearest PAGES_PER_SECTION boundary. So we're adding memory in increments
1622 * of SECTION_SIZE bytes by initializing struct pages in increments of
1623 * PAGES_PER_SECTION * sizeof(struct page) bytes.
1624 *
1625 * Return true when zone was grown, otherwise return false. We return true even
1626 * when we grow less than requested, to let the caller decide if there are
1627 * enough pages to satisfy the allocation.
1628 *
1629 * Note: We use noinline because this function is needed only during boot, and
1630 * it is called from a __ref function _deferred_grow_zone. This way we are
1631 * making sure that it is not inlined into permanent text section.
1632 */
1633static noinline bool __init
1634deferred_grow_zone(struct zone *zone, unsigned int order)
1635{
1636 int zid = zone_idx(zone);
1637 int nid = zone_to_nid(zone);
1638 pg_data_t *pgdat = NODE_DATA(nid);
1639 unsigned long nr_pages_needed = ALIGN(1 << order, PAGES_PER_SECTION);
1640 unsigned long nr_pages = 0;
1641 unsigned long first_init_pfn, spfn, epfn, t, flags;
1642 unsigned long first_deferred_pfn = pgdat->first_deferred_pfn;
1643 phys_addr_t spa, epa;
1644 u64 i;
1645
1646 /* Only the last zone may have deferred pages */
1647 if (zone_end_pfn(zone) != pgdat_end_pfn(pgdat))
1648 return false;
1649
1650 pgdat_resize_lock(pgdat, &flags);
1651
1652 /*
1653 * If deferred pages have been initialized while we were waiting for
1654 * the lock, return true, as the zone was grown. The caller will retry
1655 * this zone. We won't return to this function since the caller also
1656 * has this static branch.
1657 */
1658 if (!static_branch_unlikely(&deferred_pages)) {
1659 pgdat_resize_unlock(pgdat, &flags);
1660 return true;
1661 }
1662
1663 /*
1664 * If someone grew this zone while we were waiting for spinlock, return
1665 * true, as there might be enough pages already.
1666 */
1667 if (first_deferred_pfn != pgdat->first_deferred_pfn) {
1668 pgdat_resize_unlock(pgdat, &flags);
1669 return true;
1670 }
1671
1672 first_init_pfn = max(zone->zone_start_pfn, first_deferred_pfn);
1673
1674 if (first_init_pfn >= pgdat_end_pfn(pgdat)) {
1675 pgdat_resize_unlock(pgdat, &flags);
1676 return false;
1677 }
1678
1679 for_each_free_mem_range(i, nid, MEMBLOCK_NONE, &spa, &epa, NULL) {
1680 spfn = max_t(unsigned long, first_init_pfn, PFN_UP(spa));
1681 epfn = min_t(unsigned long, zone_end_pfn(zone), PFN_DOWN(epa));
1682
1683 while (spfn < epfn && nr_pages < nr_pages_needed) {
1684 t = ALIGN(spfn + PAGES_PER_SECTION, PAGES_PER_SECTION);
1685 first_deferred_pfn = min(t, epfn);
1686 nr_pages += deferred_init_pages(nid, zid, spfn,
1687 first_deferred_pfn);
1688 spfn = first_deferred_pfn;
1689 }
1690
1691 if (nr_pages >= nr_pages_needed)
1692 break;
1693 }
1694
1695 for_each_free_mem_range(i, nid, MEMBLOCK_NONE, &spa, &epa, NULL) {
1696 spfn = max_t(unsigned long, first_init_pfn, PFN_UP(spa));
1697 epfn = min_t(unsigned long, first_deferred_pfn, PFN_DOWN(epa));
1698 deferred_free_pages(nid, zid, spfn, epfn);
1699
1700 if (first_deferred_pfn == epfn)
1701 break;
1702 }
1703 pgdat->first_deferred_pfn = first_deferred_pfn;
1704 pgdat_resize_unlock(pgdat, &flags);
1705
1706 return nr_pages > 0;
1707}
1708
1709/*
1710 * deferred_grow_zone() is __init, but it is called from
1711 * get_page_from_freelist() during early boot until deferred_pages permanently
1712 * disables this call. This is why we have refdata wrapper to avoid warning,
1713 * and to ensure that the function body gets unloaded.
1714 */
1715static bool __ref
1716_deferred_grow_zone(struct zone *zone, unsigned int order)
1717{
1718 return deferred_grow_zone(zone, order);
1719}
1720
Joonsoo Kim7cf91a92016-03-15 14:57:51 -07001721#endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
Mel Gorman0e1cc952015-06-30 14:57:27 -07001722
1723void __init page_alloc_init_late(void)
1724{
Joonsoo Kim7cf91a92016-03-15 14:57:51 -07001725 struct zone *zone;
1726
1727#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
Mel Gorman0e1cc952015-06-30 14:57:27 -07001728 int nid;
1729
Nicolai Stanged3cd1312015-08-06 15:46:16 -07001730 /* There will be num_node_state(N_MEMORY) threads */
1731 atomic_set(&pgdat_init_n_undone, num_node_state(N_MEMORY));
Mel Gorman0e1cc952015-06-30 14:57:27 -07001732 for_each_node_state(nid, N_MEMORY) {
Mel Gorman0e1cc952015-06-30 14:57:27 -07001733 kthread_run(deferred_init_memmap, NODE_DATA(nid), "pgdatinit%d", nid);
1734 }
1735
1736 /* Block until all are initialised */
Nicolai Stanged3cd1312015-08-06 15:46:16 -07001737 wait_for_completion(&pgdat_init_all_done_comp);
Mel Gorman4248b0d2015-08-06 15:46:20 -07001738
Pavel Tatashinc9e97a12018-04-05 16:22:31 -07001739 /*
1740 * We initialized the rest of the deferred pages. Permanently disable
1741 * on-demand struct page initialization.
1742 */
1743 static_branch_disable(&deferred_pages);
1744
Mel Gorman4248b0d2015-08-06 15:46:20 -07001745 /* Reinit limits that are based on free pages after the kernel is up */
1746 files_maxfiles_init();
Joonsoo Kim7cf91a92016-03-15 14:57:51 -07001747#endif
Pavel Tatashin3010f872017-08-18 15:16:05 -07001748#ifdef CONFIG_ARCH_DISCARD_MEMBLOCK
1749 /* Discard memblock private memory */
1750 memblock_discard();
1751#endif
Joonsoo Kim7cf91a92016-03-15 14:57:51 -07001752
1753 for_each_populated_zone(zone)
1754 set_zone_contiguous(zone);
Mel Gorman7e18adb2015-06-30 14:57:05 -07001755}
Mel Gorman7e18adb2015-06-30 14:57:05 -07001756
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001757#ifdef CONFIG_CMA
Li Zhong9cf510a2013-08-23 13:52:52 +08001758/* Free whole pageblock and set its migration type to MIGRATE_CMA. */
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001759void __init init_cma_reserved_pageblock(struct page *page)
1760{
1761 unsigned i = pageblock_nr_pages;
1762 struct page *p = page;
1763
1764 do {
1765 __ClearPageReserved(p);
1766 set_page_count(p, 0);
Joonsoo Kimd883c6c2018-05-23 10:18:21 +09001767 } while (++p, --i);
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001768
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001769 set_pageblock_migratetype(page, MIGRATE_CMA);
Michal Nazarewiczdc783272014-07-02 15:22:35 -07001770
1771 if (pageblock_order >= MAX_ORDER) {
1772 i = pageblock_nr_pages;
1773 p = page;
1774 do {
1775 set_page_refcounted(p);
1776 __free_pages(p, MAX_ORDER - 1);
1777 p += MAX_ORDER_NR_PAGES;
1778 } while (i -= MAX_ORDER_NR_PAGES);
1779 } else {
1780 set_page_refcounted(page);
1781 __free_pages(page, pageblock_order);
1782 }
1783
Jiang Liu3dcc0572013-07-03 15:03:21 -07001784 adjust_managed_page_count(page, pageblock_nr_pages);
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001785}
1786#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
1788/*
1789 * The order of subdivision here is critical for the IO subsystem.
1790 * Please do not alter this order without good reasons and regression
1791 * testing. Specifically, as large blocks of memory are subdivided,
1792 * the order in which smaller blocks are delivered depends on the order
1793 * they're subdivided in this function. This is the primary factor
1794 * influencing the order in which pages are delivered to the IO
1795 * subsystem according to empirical testing, and this is also justified
1796 * by considering the behavior of a buddy system containing a single
1797 * large block of memory acted on by a series of small allocations.
1798 * This behavior is a critical factor in sglist merging's success.
1799 *
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +01001800 * -- nyc
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 */
Nick Piggin085cc7d52006-01-06 00:11:01 -08001802static inline void expand(struct zone *zone, struct page *page,
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001803 int low, int high, struct free_area *area,
1804 int migratetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
1806 unsigned long size = 1 << high;
1807
1808 while (high > low) {
1809 area--;
1810 high--;
1811 size >>= 1;
Sasha Levin309381fea2014-01-23 15:52:54 -08001812 VM_BUG_ON_PAGE(bad_range(zone, &page[size]), &page[size]);
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -08001813
Joonsoo Kimacbc15a2016-10-07 16:58:15 -07001814 /*
1815 * Mark as guard pages (or page), that will allow to
1816 * merge back to allocator when buddy will be freed.
1817 * Corresponding page table entries will not be touched,
1818 * pages will stay not present in virtual address space
1819 */
1820 if (set_page_guard(zone, &page[size], high, migratetype))
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -08001821 continue;
Joonsoo Kimacbc15a2016-10-07 16:58:15 -07001822
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001823 list_add(&page[size].lru, &area->free_list[migratetype]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 area->nr_free++;
1825 set_page_order(&page[size], high);
1826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827}
1828
Vlastimil Babka4e611802016-05-19 17:14:41 -07001829static void check_new_page_bad(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
Vlastimil Babka4e611802016-05-19 17:14:41 -07001831 const char *bad_reason = NULL;
1832 unsigned long bad_flags = 0;
Dave Hansenf0b791a2014-01-23 15:52:49 -08001833
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08001834 if (unlikely(atomic_read(&page->_mapcount) != -1))
Dave Hansenf0b791a2014-01-23 15:52:49 -08001835 bad_reason = "nonzero mapcount";
1836 if (unlikely(page->mapping != NULL))
1837 bad_reason = "non-NULL mapping";
Joonsoo Kimfe896d12016-03-17 14:19:26 -07001838 if (unlikely(page_ref_count(page) != 0))
Dave Hansenf0b791a2014-01-23 15:52:49 -08001839 bad_reason = "nonzero _count";
Naoya Horiguchif4c18e62015-08-06 15:47:08 -07001840 if (unlikely(page->flags & __PG_HWPOISON)) {
1841 bad_reason = "HWPoisoned (hardware-corrupted)";
1842 bad_flags = __PG_HWPOISON;
Naoya Horiguchie570f562016-05-20 16:58:50 -07001843 /* Don't complain about hwpoisoned pages */
1844 page_mapcount_reset(page); /* remove PageBuddy */
1845 return;
Naoya Horiguchif4c18e62015-08-06 15:47:08 -07001846 }
Dave Hansenf0b791a2014-01-23 15:52:49 -08001847 if (unlikely(page->flags & PAGE_FLAGS_CHECK_AT_PREP)) {
1848 bad_reason = "PAGE_FLAGS_CHECK_AT_PREP flag set";
1849 bad_flags = PAGE_FLAGS_CHECK_AT_PREP;
1850 }
Johannes Weiner9edad6e2014-12-10 15:44:58 -08001851#ifdef CONFIG_MEMCG
1852 if (unlikely(page->mem_cgroup))
1853 bad_reason = "page still charged to cgroup";
1854#endif
Vlastimil Babka4e611802016-05-19 17:14:41 -07001855 bad_page(page, bad_reason, bad_flags);
1856}
1857
1858/*
1859 * This page is about to be returned from the page allocator
1860 */
1861static inline int check_new_page(struct page *page)
1862{
1863 if (likely(page_expected_state(page,
1864 PAGE_FLAGS_CHECK_AT_PREP|__PG_HWPOISON)))
1865 return 0;
1866
1867 check_new_page_bad(page);
1868 return 1;
Wu Fengguang2a7684a2009-09-16 11:50:12 +02001869}
1870
Vinayak Menonbd33ef32017-05-03 14:54:42 -07001871static inline bool free_pages_prezeroed(void)
Laura Abbott1414c7f2016-03-15 14:56:30 -07001872{
1873 return IS_ENABLED(CONFIG_PAGE_POISONING_ZERO) &&
Vinayak Menonbd33ef32017-05-03 14:54:42 -07001874 page_poisoning_enabled();
Laura Abbott1414c7f2016-03-15 14:56:30 -07001875}
1876
Mel Gorman479f8542016-05-19 17:14:35 -07001877#ifdef CONFIG_DEBUG_VM
1878static bool check_pcp_refill(struct page *page)
1879{
1880 return false;
1881}
1882
1883static bool check_new_pcp(struct page *page)
1884{
1885 return check_new_page(page);
1886}
1887#else
1888static bool check_pcp_refill(struct page *page)
1889{
1890 return check_new_page(page);
1891}
1892static bool check_new_pcp(struct page *page)
1893{
1894 return false;
1895}
1896#endif /* CONFIG_DEBUG_VM */
1897
1898static bool check_new_pages(struct page *page, unsigned int order)
1899{
1900 int i;
1901 for (i = 0; i < (1 << order); i++) {
1902 struct page *p = page + i;
1903
1904 if (unlikely(check_new_page(p)))
1905 return true;
1906 }
1907
1908 return false;
1909}
1910
Joonsoo Kim46f24fd2016-07-26 15:23:58 -07001911inline void post_alloc_hook(struct page *page, unsigned int order,
1912 gfp_t gfp_flags)
1913{
1914 set_page_private(page, 0);
1915 set_page_refcounted(page);
1916
1917 arch_alloc_page(page, order);
1918 kernel_map_pages(page, 1 << order, 1);
1919 kernel_poison_pages(page, 1 << order, 1);
1920 kasan_alloc_pages(page, order);
1921 set_page_owner(page, order, gfp_flags);
1922}
1923
Mel Gorman479f8542016-05-19 17:14:35 -07001924static void prep_new_page(struct page *page, unsigned int order, gfp_t gfp_flags,
Mel Gormanc6038442016-05-19 17:13:38 -07001925 unsigned int alloc_flags)
Wu Fengguang2a7684a2009-09-16 11:50:12 +02001926{
1927 int i;
Hugh Dickins689bceb2005-11-21 21:32:20 -08001928
Joonsoo Kim46f24fd2016-07-26 15:23:58 -07001929 post_alloc_hook(page, order, gfp_flags);
Nick Piggin17cf4402006-03-22 00:08:41 -08001930
Vinayak Menonbd33ef32017-05-03 14:54:42 -07001931 if (!free_pages_prezeroed() && (gfp_flags & __GFP_ZERO))
Anisse Astierf4d28972015-06-24 16:56:36 -07001932 for (i = 0; i < (1 << order); i++)
1933 clear_highpage(page + i);
Nick Piggin17cf4402006-03-22 00:08:41 -08001934
1935 if (order && (gfp_flags & __GFP_COMP))
1936 prep_compound_page(page, order);
1937
Vlastimil Babka75379192015-02-11 15:25:38 -08001938 /*
Michal Hocko2f064f32015-08-21 14:11:51 -07001939 * page is set pfmemalloc when ALLOC_NO_WATERMARKS was necessary to
Vlastimil Babka75379192015-02-11 15:25:38 -08001940 * allocate the page. The expectation is that the caller is taking
1941 * steps that will free more memory. The caller should avoid the page
1942 * being used for !PFMEMALLOC purposes.
1943 */
Michal Hocko2f064f32015-08-21 14:11:51 -07001944 if (alloc_flags & ALLOC_NO_WATERMARKS)
1945 set_page_pfmemalloc(page);
1946 else
1947 clear_page_pfmemalloc(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948}
1949
Mel Gorman56fd56b2007-10-16 01:25:58 -07001950/*
1951 * Go through the free lists for the given migratetype and remove
1952 * the smallest available page from the freelists
1953 */
Aaron Lu85ccc8f2017-11-15 17:36:53 -08001954static __always_inline
Mel Gorman728ec982009-06-16 15:32:04 -07001955struct page *__rmqueue_smallest(struct zone *zone, unsigned int order,
Mel Gorman56fd56b2007-10-16 01:25:58 -07001956 int migratetype)
1957{
1958 unsigned int current_order;
Pintu Kumarb8af2942013-09-11 14:20:34 -07001959 struct free_area *area;
Mel Gorman56fd56b2007-10-16 01:25:58 -07001960 struct page *page;
1961
1962 /* Find a page of the appropriate size in the preferred list */
1963 for (current_order = order; current_order < MAX_ORDER; ++current_order) {
1964 area = &(zone->free_area[current_order]);
Geliang Tanga16601c2016-01-14 15:20:30 -08001965 page = list_first_entry_or_null(&area->free_list[migratetype],
Mel Gorman56fd56b2007-10-16 01:25:58 -07001966 struct page, lru);
Geliang Tanga16601c2016-01-14 15:20:30 -08001967 if (!page)
1968 continue;
Mel Gorman56fd56b2007-10-16 01:25:58 -07001969 list_del(&page->lru);
1970 rmv_page_order(page);
1971 area->nr_free--;
Mel Gorman56fd56b2007-10-16 01:25:58 -07001972 expand(zone, page, order, current_order, area, migratetype);
Vlastimil Babkabb14c2c2015-09-08 15:01:25 -07001973 set_pcppage_migratetype(page, migratetype);
Mel Gorman56fd56b2007-10-16 01:25:58 -07001974 return page;
1975 }
1976
1977 return NULL;
1978}
1979
1980
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001981/*
1982 * This array describes the order lists are fallen back to when
1983 * the free lists for the desirable migrate type are depleted
1984 */
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001985static int fallbacks[MIGRATE_TYPES][4] = {
Mel Gorman974a7862015-11-06 16:28:34 -08001986 [MIGRATE_UNMOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_MOVABLE, MIGRATE_TYPES },
1987 [MIGRATE_RECLAIMABLE] = { MIGRATE_UNMOVABLE, MIGRATE_MOVABLE, MIGRATE_TYPES },
1988 [MIGRATE_MOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE, MIGRATE_TYPES },
Joonsoo Kimdc676472015-04-14 15:45:15 -07001989#ifdef CONFIG_CMA
Mel Gorman974a7862015-11-06 16:28:34 -08001990 [MIGRATE_CMA] = { MIGRATE_TYPES }, /* Never used */
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001991#endif
Minchan Kim194159f2013-02-22 16:33:58 -08001992#ifdef CONFIG_MEMORY_ISOLATION
Mel Gorman974a7862015-11-06 16:28:34 -08001993 [MIGRATE_ISOLATE] = { MIGRATE_TYPES }, /* Never used */
Minchan Kim194159f2013-02-22 16:33:58 -08001994#endif
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001995};
1996
Joonsoo Kimdc676472015-04-14 15:45:15 -07001997#ifdef CONFIG_CMA
Aaron Lu85ccc8f2017-11-15 17:36:53 -08001998static __always_inline struct page *__rmqueue_cma_fallback(struct zone *zone,
Joonsoo Kimdc676472015-04-14 15:45:15 -07001999 unsigned int order)
2000{
2001 return __rmqueue_smallest(zone, order, MIGRATE_CMA);
2002}
2003#else
2004static inline struct page *__rmqueue_cma_fallback(struct zone *zone,
2005 unsigned int order) { return NULL; }
2006#endif
2007
Mel Gormanc361be52007-10-16 01:25:51 -07002008/*
2009 * Move the free pages in a range to the free lists of the requested type.
Mel Gormand9c23402007-10-16 01:26:01 -07002010 * Note that start_page and end_pages are not aligned on a pageblock
Mel Gormanc361be52007-10-16 01:25:51 -07002011 * boundary. If alignment is required, use move_freepages_block()
2012 */
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -07002013static int move_freepages(struct zone *zone,
Adrian Bunkb69a7282008-07-23 21:28:12 -07002014 struct page *start_page, struct page *end_page,
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -07002015 int migratetype, int *num_movable)
Mel Gormanc361be52007-10-16 01:25:51 -07002016{
2017 struct page *page;
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08002018 unsigned int order;
Mel Gormand1003132007-10-16 01:26:00 -07002019 int pages_moved = 0;
Mel Gormanc361be52007-10-16 01:25:51 -07002020
2021#ifndef CONFIG_HOLES_IN_ZONE
2022 /*
2023 * page_zone is not safe to call in this context when
2024 * CONFIG_HOLES_IN_ZONE is set. This bug check is probably redundant
2025 * anyway as we check zone boundaries in move_freepages_block().
2026 * Remove at a later date when no bug reports exist related to
Mel Gormanac0e5b72007-10-16 01:25:58 -07002027 * grouping pages by mobility
Mel Gormanc361be52007-10-16 01:25:51 -07002028 */
Ard Biesheuvel3e040402018-03-14 19:29:37 +00002029 VM_BUG_ON(pfn_valid(page_to_pfn(start_page)) &&
2030 pfn_valid(page_to_pfn(end_page)) &&
2031 page_zone(start_page) != page_zone(end_page));
Mel Gormanc361be52007-10-16 01:25:51 -07002032#endif
Mel Gormanc361be52007-10-16 01:25:51 -07002033 for (page = start_page; page <= end_page;) {
2034 if (!pfn_valid_within(page_to_pfn(page))) {
2035 page++;
2036 continue;
2037 }
2038
Ard Biesheuvelf073bdc2017-01-10 16:58:00 -08002039 /* Make sure we are not inadvertently changing nodes */
2040 VM_BUG_ON_PAGE(page_to_nid(page) != zone_to_nid(zone), page);
2041
Mel Gormanc361be52007-10-16 01:25:51 -07002042 if (!PageBuddy(page)) {
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -07002043 /*
2044 * We assume that pages that could be isolated for
2045 * migration are movable. But we don't actually try
2046 * isolating, as that would be expensive.
2047 */
2048 if (num_movable &&
2049 (PageLRU(page) || __PageMovable(page)))
2050 (*num_movable)++;
2051
Mel Gormanc361be52007-10-16 01:25:51 -07002052 page++;
2053 continue;
2054 }
2055
2056 order = page_order(page);
Kirill A. Shutemov84be48d2011-03-22 16:33:41 -07002057 list_move(&page->lru,
2058 &zone->free_area[order].free_list[migratetype]);
Mel Gormanc361be52007-10-16 01:25:51 -07002059 page += 1 << order;
Mel Gormand1003132007-10-16 01:26:00 -07002060 pages_moved += 1 << order;
Mel Gormanc361be52007-10-16 01:25:51 -07002061 }
2062
Mel Gormand1003132007-10-16 01:26:00 -07002063 return pages_moved;
Mel Gormanc361be52007-10-16 01:25:51 -07002064}
2065
Minchan Kimee6f5092012-07-31 16:43:50 -07002066int move_freepages_block(struct zone *zone, struct page *page,
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -07002067 int migratetype, int *num_movable)
Mel Gormanc361be52007-10-16 01:25:51 -07002068{
2069 unsigned long start_pfn, end_pfn;
2070 struct page *start_page, *end_page;
2071
David Rientjes4a222122018-10-26 15:09:24 -07002072 if (num_movable)
2073 *num_movable = 0;
2074
Mel Gormanc361be52007-10-16 01:25:51 -07002075 start_pfn = page_to_pfn(page);
Mel Gormand9c23402007-10-16 01:26:01 -07002076 start_pfn = start_pfn & ~(pageblock_nr_pages-1);
Mel Gormanc361be52007-10-16 01:25:51 -07002077 start_page = pfn_to_page(start_pfn);
Mel Gormand9c23402007-10-16 01:26:01 -07002078 end_page = start_page + pageblock_nr_pages - 1;
2079 end_pfn = start_pfn + pageblock_nr_pages - 1;
Mel Gormanc361be52007-10-16 01:25:51 -07002080
2081 /* Do not cross zone boundaries */
Cody P Schafer108bcc92013-02-22 16:35:23 -08002082 if (!zone_spans_pfn(zone, start_pfn))
Mel Gormanc361be52007-10-16 01:25:51 -07002083 start_page = page;
Cody P Schafer108bcc92013-02-22 16:35:23 -08002084 if (!zone_spans_pfn(zone, end_pfn))
Mel Gormanc361be52007-10-16 01:25:51 -07002085 return 0;
2086
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -07002087 return move_freepages(zone, start_page, end_page, migratetype,
2088 num_movable);
Mel Gormanc361be52007-10-16 01:25:51 -07002089}
2090
Mel Gorman2f66a682009-09-21 17:02:31 -07002091static void change_pageblock_range(struct page *pageblock_page,
2092 int start_order, int migratetype)
2093{
2094 int nr_pageblocks = 1 << (start_order - pageblock_order);
2095
2096 while (nr_pageblocks--) {
2097 set_pageblock_migratetype(pageblock_page, migratetype);
2098 pageblock_page += pageblock_nr_pages;
2099 }
2100}
2101
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07002102/*
Vlastimil Babka9c0415e2015-02-11 15:28:21 -08002103 * When we are falling back to another migratetype during allocation, try to
2104 * steal extra free pages from the same pageblocks to satisfy further
2105 * allocations, instead of polluting multiple pageblocks.
2106 *
2107 * If we are stealing a relatively large buddy page, it is likely there will
2108 * be more free pages in the pageblock, so try to steal them all. For
2109 * reclaimable and unmovable allocations, we steal regardless of page size,
2110 * as fragmentation caused by those allocations polluting movable pageblocks
2111 * is worse than movable allocations stealing from unmovable and reclaimable
2112 * pageblocks.
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07002113 */
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002114static bool can_steal_fallback(unsigned int order, int start_mt)
2115{
2116 /*
2117 * Leaving this order check is intended, although there is
2118 * relaxed order check in next check. The reason is that
2119 * we can actually steal whole pageblock if this condition met,
2120 * but, below check doesn't guarantee it and that is just heuristic
2121 * so could be changed anytime.
2122 */
2123 if (order >= pageblock_order)
2124 return true;
2125
2126 if (order >= pageblock_order / 2 ||
2127 start_mt == MIGRATE_RECLAIMABLE ||
2128 start_mt == MIGRATE_UNMOVABLE ||
2129 page_group_by_mobility_disabled)
2130 return true;
2131
2132 return false;
2133}
2134
2135/*
2136 * This function implements actual steal behaviour. If order is large enough,
2137 * we can steal whole pageblock. If not, we first move freepages in this
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -07002138 * pageblock to our migratetype and determine how many already-allocated pages
2139 * are there in the pageblock with a compatible migratetype. If at least half
2140 * of pages are free or compatible, we can change migratetype of the pageblock
2141 * itself, so pages freed in the future will be put on the correct free list.
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002142 */
2143static void steal_suitable_fallback(struct zone *zone, struct page *page,
Vlastimil Babka3bc48f92017-05-08 15:54:37 -07002144 int start_type, bool whole_block)
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07002145{
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08002146 unsigned int current_order = page_order(page);
Vlastimil Babka3bc48f92017-05-08 15:54:37 -07002147 struct free_area *area;
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -07002148 int free_pages, movable_pages, alike_pages;
2149 int old_block_type;
2150
2151 old_block_type = get_pageblock_migratetype(page);
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07002152
Vlastimil Babka3bc48f92017-05-08 15:54:37 -07002153 /*
2154 * This can happen due to races and we want to prevent broken
2155 * highatomic accounting.
2156 */
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -07002157 if (is_migrate_highatomic(old_block_type))
Vlastimil Babka3bc48f92017-05-08 15:54:37 -07002158 goto single_page;
2159
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07002160 /* Take ownership for orders >= pageblock_order */
2161 if (current_order >= pageblock_order) {
2162 change_pageblock_range(page, current_order, start_type);
Vlastimil Babka3bc48f92017-05-08 15:54:37 -07002163 goto single_page;
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07002164 }
2165
Vlastimil Babka3bc48f92017-05-08 15:54:37 -07002166 /* We are not allowed to try stealing from the whole block */
2167 if (!whole_block)
2168 goto single_page;
2169
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -07002170 free_pages = move_freepages_block(zone, page, start_type,
2171 &movable_pages);
2172 /*
2173 * Determine how many pages are compatible with our allocation.
2174 * For movable allocation, it's the number of movable pages which
2175 * we just obtained. For other types it's a bit more tricky.
2176 */
2177 if (start_type == MIGRATE_MOVABLE) {
2178 alike_pages = movable_pages;
2179 } else {
2180 /*
2181 * If we are falling back a RECLAIMABLE or UNMOVABLE allocation
2182 * to MOVABLE pageblock, consider all non-movable pages as
2183 * compatible. If it's UNMOVABLE falling back to RECLAIMABLE or
2184 * vice versa, be conservative since we can't distinguish the
2185 * exact migratetype of non-movable pages.
2186 */
2187 if (old_block_type == MIGRATE_MOVABLE)
2188 alike_pages = pageblock_nr_pages
2189 - (free_pages + movable_pages);
2190 else
2191 alike_pages = 0;
2192 }
2193
Vlastimil Babka3bc48f92017-05-08 15:54:37 -07002194 /* moving whole block can fail due to zone boundary conditions */
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -07002195 if (!free_pages)
Vlastimil Babka3bc48f92017-05-08 15:54:37 -07002196 goto single_page;
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07002197
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -07002198 /*
2199 * If a sufficient number of pages in the block are either free or of
2200 * comparable migratability as our allocation, claim the whole block.
2201 */
2202 if (free_pages + alike_pages >= (1 << (pageblock_order-1)) ||
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002203 page_group_by_mobility_disabled)
2204 set_pageblock_migratetype(page, start_type);
Vlastimil Babka3bc48f92017-05-08 15:54:37 -07002205
2206 return;
2207
2208single_page:
2209 area = &zone->free_area[current_order];
2210 list_move(&page->lru, &area->free_list[start_type]);
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002211}
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07002212
Joonsoo Kim2149cda2015-04-14 15:45:21 -07002213/*
2214 * Check whether there is a suitable fallback freepage with requested order.
2215 * If only_stealable is true, this function returns fallback_mt only if
2216 * we can steal other freepages all together. This would help to reduce
2217 * fragmentation due to mixed migratetype pages in one pageblock.
2218 */
2219int find_suitable_fallback(struct free_area *area, unsigned int order,
2220 int migratetype, bool only_stealable, bool *can_steal)
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002221{
2222 int i;
2223 int fallback_mt;
2224
2225 if (area->nr_free == 0)
2226 return -1;
2227
2228 *can_steal = false;
2229 for (i = 0;; i++) {
2230 fallback_mt = fallbacks[migratetype][i];
Mel Gorman974a7862015-11-06 16:28:34 -08002231 if (fallback_mt == MIGRATE_TYPES)
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002232 break;
2233
2234 if (list_empty(&area->free_list[fallback_mt]))
2235 continue;
2236
2237 if (can_steal_fallback(order, migratetype))
2238 *can_steal = true;
2239
Joonsoo Kim2149cda2015-04-14 15:45:21 -07002240 if (!only_stealable)
2241 return fallback_mt;
2242
2243 if (*can_steal)
2244 return fallback_mt;
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07002245 }
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002246
2247 return -1;
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07002248}
2249
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002250/*
2251 * Reserve a pageblock for exclusive use of high-order atomic allocations if
2252 * there are no empty page blocks that contain a page with a suitable order
2253 */
2254static void reserve_highatomic_pageblock(struct page *page, struct zone *zone,
2255 unsigned int alloc_order)
2256{
2257 int mt;
2258 unsigned long max_managed, flags;
2259
2260 /*
2261 * Limit the number reserved to 1 pageblock or roughly 1% of a zone.
2262 * Check is race-prone but harmless.
2263 */
Arun KS9705bea2018-12-28 00:34:24 -08002264 max_managed = (zone_managed_pages(zone) / 100) + pageblock_nr_pages;
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002265 if (zone->nr_reserved_highatomic >= max_managed)
2266 return;
2267
2268 spin_lock_irqsave(&zone->lock, flags);
2269
2270 /* Recheck the nr_reserved_highatomic limit under the lock */
2271 if (zone->nr_reserved_highatomic >= max_managed)
2272 goto out_unlock;
2273
2274 /* Yoink! */
2275 mt = get_pageblock_migratetype(page);
Xishi Qiua6ffdc02017-05-03 14:52:52 -07002276 if (!is_migrate_highatomic(mt) && !is_migrate_isolate(mt)
2277 && !is_migrate_cma(mt)) {
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002278 zone->nr_reserved_highatomic += pageblock_nr_pages;
2279 set_pageblock_migratetype(page, MIGRATE_HIGHATOMIC);
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -07002280 move_freepages_block(zone, page, MIGRATE_HIGHATOMIC, NULL);
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002281 }
2282
2283out_unlock:
2284 spin_unlock_irqrestore(&zone->lock, flags);
2285}
2286
2287/*
2288 * Used when an allocation is about to fail under memory pressure. This
2289 * potentially hurts the reliability of high-order allocations when under
2290 * intense memory pressure but failed atomic allocations should be easier
2291 * to recover from than an OOM.
Minchan Kim29fac032016-12-12 16:42:14 -08002292 *
2293 * If @force is true, try to unreserve a pageblock even though highatomic
2294 * pageblock is exhausted.
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002295 */
Minchan Kim29fac032016-12-12 16:42:14 -08002296static bool unreserve_highatomic_pageblock(const struct alloc_context *ac,
2297 bool force)
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002298{
2299 struct zonelist *zonelist = ac->zonelist;
2300 unsigned long flags;
2301 struct zoneref *z;
2302 struct zone *zone;
2303 struct page *page;
2304 int order;
Minchan Kim04c87162016-12-12 16:42:11 -08002305 bool ret;
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002306
2307 for_each_zone_zonelist_nodemask(zone, z, zonelist, ac->high_zoneidx,
2308 ac->nodemask) {
Minchan Kim29fac032016-12-12 16:42:14 -08002309 /*
2310 * Preserve at least one pageblock unless memory pressure
2311 * is really high.
2312 */
2313 if (!force && zone->nr_reserved_highatomic <=
2314 pageblock_nr_pages)
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002315 continue;
2316
2317 spin_lock_irqsave(&zone->lock, flags);
2318 for (order = 0; order < MAX_ORDER; order++) {
2319 struct free_area *area = &(zone->free_area[order]);
2320
Geliang Tanga16601c2016-01-14 15:20:30 -08002321 page = list_first_entry_or_null(
2322 &area->free_list[MIGRATE_HIGHATOMIC],
2323 struct page, lru);
2324 if (!page)
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002325 continue;
2326
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002327 /*
Minchan Kim4855e4a2016-12-12 16:42:08 -08002328 * In page freeing path, migratetype change is racy so
2329 * we can counter several free pages in a pageblock
2330 * in this loop althoug we changed the pageblock type
2331 * from highatomic to ac->migratetype. So we should
2332 * adjust the count once.
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002333 */
Xishi Qiua6ffdc02017-05-03 14:52:52 -07002334 if (is_migrate_highatomic_page(page)) {
Minchan Kim4855e4a2016-12-12 16:42:08 -08002335 /*
2336 * It should never happen but changes to
2337 * locking could inadvertently allow a per-cpu
2338 * drain to add pages to MIGRATE_HIGHATOMIC
2339 * while unreserving so be safe and watch for
2340 * underflows.
2341 */
2342 zone->nr_reserved_highatomic -= min(
2343 pageblock_nr_pages,
2344 zone->nr_reserved_highatomic);
2345 }
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002346
2347 /*
2348 * Convert to ac->migratetype and avoid the normal
2349 * pageblock stealing heuristics. Minimally, the caller
2350 * is doing the work and needs the pages. More
2351 * importantly, if the block was always converted to
2352 * MIGRATE_UNMOVABLE or another type then the number
2353 * of pageblocks that cannot be completely freed
2354 * may increase.
2355 */
2356 set_pageblock_migratetype(page, ac->migratetype);
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -07002357 ret = move_freepages_block(zone, page, ac->migratetype,
2358 NULL);
Minchan Kim29fac032016-12-12 16:42:14 -08002359 if (ret) {
2360 spin_unlock_irqrestore(&zone->lock, flags);
2361 return ret;
2362 }
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002363 }
2364 spin_unlock_irqrestore(&zone->lock, flags);
2365 }
Minchan Kim04c87162016-12-12 16:42:11 -08002366
2367 return false;
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002368}
2369
Vlastimil Babka3bc48f92017-05-08 15:54:37 -07002370/*
2371 * Try finding a free buddy page on the fallback list and put it on the free
2372 * list of requested migratetype, possibly along with other pages from the same
2373 * block, depending on fragmentation avoidance heuristics. Returns true if
2374 * fallback was found so that __rmqueue_smallest() can grab it.
Rasmus Villemoesb0025292017-07-10 15:49:26 -07002375 *
2376 * The use of signed ints for order and current_order is a deliberate
2377 * deviation from the rest of this file, to make the for loop
2378 * condition simpler.
Vlastimil Babka3bc48f92017-05-08 15:54:37 -07002379 */
Aaron Lu85ccc8f2017-11-15 17:36:53 -08002380static __always_inline bool
Rasmus Villemoesb0025292017-07-10 15:49:26 -07002381__rmqueue_fallback(struct zone *zone, int order, int start_migratetype)
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002382{
Pintu Kumarb8af2942013-09-11 14:20:34 -07002383 struct free_area *area;
Rasmus Villemoesb0025292017-07-10 15:49:26 -07002384 int current_order;
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002385 struct page *page;
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002386 int fallback_mt;
2387 bool can_steal;
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002388
Vlastimil Babka7a8f58f2017-07-10 15:47:14 -07002389 /*
2390 * Find the largest available free page in the other list. This roughly
2391 * approximates finding the pageblock with the most free pages, which
2392 * would be too costly to do exactly.
2393 */
Rasmus Villemoesb0025292017-07-10 15:49:26 -07002394 for (current_order = MAX_ORDER - 1; current_order >= order;
Mel Gorman7aeb09f2014-06-04 16:10:21 -07002395 --current_order) {
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002396 area = &(zone->free_area[current_order]);
2397 fallback_mt = find_suitable_fallback(area, current_order,
Joonsoo Kim2149cda2015-04-14 15:45:21 -07002398 start_migratetype, false, &can_steal);
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002399 if (fallback_mt == -1)
2400 continue;
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002401
Vlastimil Babka7a8f58f2017-07-10 15:47:14 -07002402 /*
2403 * We cannot steal all free pages from the pageblock and the
2404 * requested migratetype is movable. In that case it's better to
2405 * steal and split the smallest available page instead of the
2406 * largest available page, because even if the next movable
2407 * allocation falls back into a different pageblock than this
2408 * one, it won't cause permanent fragmentation.
2409 */
2410 if (!can_steal && start_migratetype == MIGRATE_MOVABLE
2411 && current_order > order)
2412 goto find_smallest;
Mel Gormane0104872007-10-16 01:25:53 -07002413
Vlastimil Babka7a8f58f2017-07-10 15:47:14 -07002414 goto do_steal;
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002415 }
2416
Vlastimil Babka3bc48f92017-05-08 15:54:37 -07002417 return false;
Vlastimil Babka7a8f58f2017-07-10 15:47:14 -07002418
2419find_smallest:
2420 for (current_order = order; current_order < MAX_ORDER;
2421 current_order++) {
2422 area = &(zone->free_area[current_order]);
2423 fallback_mt = find_suitable_fallback(area, current_order,
2424 start_migratetype, false, &can_steal);
2425 if (fallback_mt != -1)
2426 break;
2427 }
2428
2429 /*
2430 * This should not happen - we already found a suitable fallback
2431 * when looking for the largest page.
2432 */
2433 VM_BUG_ON(current_order == MAX_ORDER);
2434
2435do_steal:
2436 page = list_first_entry(&area->free_list[fallback_mt],
2437 struct page, lru);
2438
2439 steal_suitable_fallback(zone, page, start_migratetype, can_steal);
2440
2441 trace_mm_page_alloc_extfrag(page, order, current_order,
2442 start_migratetype, fallback_mt);
2443
2444 return true;
2445
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002446}
2447
Mel Gorman56fd56b2007-10-16 01:25:58 -07002448/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 * Do the hard work of removing an element from the buddy allocator.
2450 * Call me with the zone->lock already held.
2451 */
Aaron Lu85ccc8f2017-11-15 17:36:53 -08002452static __always_inline struct page *
2453__rmqueue(struct zone *zone, unsigned int order, int migratetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 struct page *page;
2456
Vlastimil Babka3bc48f92017-05-08 15:54:37 -07002457retry:
Mel Gorman56fd56b2007-10-16 01:25:58 -07002458 page = __rmqueue_smallest(zone, order, migratetype);
Mel Gorman974a7862015-11-06 16:28:34 -08002459 if (unlikely(!page)) {
Joonsoo Kimdc676472015-04-14 15:45:15 -07002460 if (migratetype == MIGRATE_MOVABLE)
2461 page = __rmqueue_cma_fallback(zone, order);
2462
Vlastimil Babka3bc48f92017-05-08 15:54:37 -07002463 if (!page && __rmqueue_fallback(zone, order, migratetype))
2464 goto retry;
Mel Gorman728ec982009-06-16 15:32:04 -07002465 }
2466
Mel Gorman0d3d0622009-09-21 17:02:44 -07002467 trace_mm_page_alloc_zone_locked(page, order, migratetype);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002468 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469}
2470
Michal Nazarewicz5f63b722012-01-11 15:16:11 +01002471/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 * Obtain a specified number of elements from the buddy allocator, all under
2473 * a single hold of the lock, for efficiency. Add them to the supplied list.
2474 * Returns the number of new pages which were placed at *list.
2475 */
Michal Nazarewicz5f63b722012-01-11 15:16:11 +01002476static int rmqueue_bulk(struct zone *zone, unsigned int order,
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002477 unsigned long count, struct list_head *list,
Mel Gorman453f85d2017-11-15 17:38:03 -08002478 int migratetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479{
Mel Gormana6de7342016-12-12 16:44:41 -08002480 int i, alloced = 0;
Michal Nazarewicz5f63b722012-01-11 15:16:11 +01002481
Mel Gormand34b0732017-04-20 14:37:43 -07002482 spin_lock(&zone->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 for (i = 0; i < count; ++i) {
Mel Gorman6ac02062016-01-14 15:20:28 -08002484 struct page *page = __rmqueue(zone, order, migratetype);
Nick Piggin085cc7d52006-01-06 00:11:01 -08002485 if (unlikely(page == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 break;
Mel Gorman81eabcb2007-12-17 16:20:05 -08002487
Mel Gorman479f8542016-05-19 17:14:35 -07002488 if (unlikely(check_pcp_refill(page)))
2489 continue;
2490
Mel Gorman81eabcb2007-12-17 16:20:05 -08002491 /*
Vlastimil Babka0fac3ba2017-11-15 17:38:07 -08002492 * Split buddy pages returned by expand() are received here in
2493 * physical page order. The page is added to the tail of
2494 * caller's list. From the callers perspective, the linked list
2495 * is ordered by page number under some conditions. This is
2496 * useful for IO devices that can forward direction from the
2497 * head, thus also in the physical page order. This is useful
2498 * for IO devices that can merge IO requests if the physical
2499 * pages are ordered properly.
Mel Gorman81eabcb2007-12-17 16:20:05 -08002500 */
Vlastimil Babka0fac3ba2017-11-15 17:38:07 -08002501 list_add_tail(&page->lru, list);
Mel Gormana6de7342016-12-12 16:44:41 -08002502 alloced++;
Vlastimil Babkabb14c2c2015-09-08 15:01:25 -07002503 if (is_migrate_cma(get_pcppage_migratetype(page)))
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -07002504 __mod_zone_page_state(zone, NR_FREE_CMA_PAGES,
2505 -(1 << order));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 }
Mel Gormana6de7342016-12-12 16:44:41 -08002507
2508 /*
2509 * i pages were removed from the buddy list even if some leak due
2510 * to check_pcp_refill failing so adjust NR_FREE_PAGES based
2511 * on i. Do not confuse with 'alloced' which is the number of
2512 * pages added to the pcp list.
2513 */
Mel Gormanf2260e62009-06-16 15:32:13 -07002514 __mod_zone_page_state(zone, NR_FREE_PAGES, -(i << order));
Mel Gormand34b0732017-04-20 14:37:43 -07002515 spin_unlock(&zone->lock);
Mel Gormana6de7342016-12-12 16:44:41 -08002516 return alloced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517}
2518
Christoph Lameter4ae7c032005-06-21 17:14:57 -07002519#ifdef CONFIG_NUMA
Christoph Lameter8fce4d82006-03-09 17:33:54 -08002520/*
Christoph Lameter4037d452007-05-09 02:35:14 -07002521 * Called from the vmstat counter updater to drain pagesets of this
2522 * currently executing processor on remote nodes after they have
2523 * expired.
2524 *
Christoph Lameter879336c2006-03-22 00:09:08 -08002525 * Note that this function must be called with the thread pinned to
2526 * a single processor.
Christoph Lameter8fce4d82006-03-09 17:33:54 -08002527 */
Christoph Lameter4037d452007-05-09 02:35:14 -07002528void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
Christoph Lameter4ae7c032005-06-21 17:14:57 -07002529{
Christoph Lameter4ae7c032005-06-21 17:14:57 -07002530 unsigned long flags;
Michal Nazarewicz7be12fc2014-08-06 16:05:15 -07002531 int to_drain, batch;
Christoph Lameter4ae7c032005-06-21 17:14:57 -07002532
Christoph Lameter4037d452007-05-09 02:35:14 -07002533 local_irq_save(flags);
Jason Low4db0c3c2015-04-15 16:14:08 -07002534 batch = READ_ONCE(pcp->batch);
Michal Nazarewicz7be12fc2014-08-06 16:05:15 -07002535 to_drain = min(pcp->count, batch);
Aaron Lu77ba9062018-04-05 16:24:06 -07002536 if (to_drain > 0)
KOSAKI Motohiro2a135152012-07-31 16:42:53 -07002537 free_pcppages_bulk(zone, to_drain, pcp);
Christoph Lameter4037d452007-05-09 02:35:14 -07002538 local_irq_restore(flags);
Christoph Lameter4ae7c032005-06-21 17:14:57 -07002539}
2540#endif
2541
Christoph Lameter9f8f2172008-02-04 22:29:11 -08002542/*
Vlastimil Babka93481ff2014-12-10 15:43:01 -08002543 * Drain pcplists of the indicated processor and zone.
2544 *
2545 * The processor must either be the current processor and the
2546 * thread pinned to the current processor or a processor that
2547 * is not online.
2548 */
2549static void drain_pages_zone(unsigned int cpu, struct zone *zone)
2550{
2551 unsigned long flags;
2552 struct per_cpu_pageset *pset;
2553 struct per_cpu_pages *pcp;
2554
2555 local_irq_save(flags);
2556 pset = per_cpu_ptr(zone->pageset, cpu);
2557
2558 pcp = &pset->pcp;
Aaron Lu77ba9062018-04-05 16:24:06 -07002559 if (pcp->count)
Vlastimil Babka93481ff2014-12-10 15:43:01 -08002560 free_pcppages_bulk(zone, pcp->count, pcp);
Vlastimil Babka93481ff2014-12-10 15:43:01 -08002561 local_irq_restore(flags);
2562}
2563
2564/*
2565 * Drain pcplists of all zones on the indicated processor.
Christoph Lameter9f8f2172008-02-04 22:29:11 -08002566 *
2567 * The processor must either be the current processor and the
2568 * thread pinned to the current processor or a processor that
2569 * is not online.
2570 */
2571static void drain_pages(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572{
2573 struct zone *zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
KOSAKI Motohiroee99c712009-03-31 15:19:31 -07002575 for_each_populated_zone(zone) {
Vlastimil Babka93481ff2014-12-10 15:43:01 -08002576 drain_pages_zone(cpu, zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 }
2578}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
Christoph Lameter9f8f2172008-02-04 22:29:11 -08002580/*
2581 * Spill all of this CPU's per-cpu pages back into the buddy allocator.
Vlastimil Babka93481ff2014-12-10 15:43:01 -08002582 *
2583 * The CPU has to be pinned. When zone parameter is non-NULL, spill just
2584 * the single zone's pages.
Christoph Lameter9f8f2172008-02-04 22:29:11 -08002585 */
Vlastimil Babka93481ff2014-12-10 15:43:01 -08002586void drain_local_pages(struct zone *zone)
Christoph Lameter9f8f2172008-02-04 22:29:11 -08002587{
Vlastimil Babka93481ff2014-12-10 15:43:01 -08002588 int cpu = smp_processor_id();
2589
2590 if (zone)
2591 drain_pages_zone(cpu, zone);
2592 else
2593 drain_pages(cpu);
Christoph Lameter9f8f2172008-02-04 22:29:11 -08002594}
2595
Mel Gorman0ccce3b2017-02-24 14:56:32 -08002596static void drain_local_pages_wq(struct work_struct *work)
2597{
Michal Hockoa459eeb2017-02-24 14:56:35 -08002598 /*
2599 * drain_all_pages doesn't use proper cpu hotplug protection so
2600 * we can race with cpu offline when the WQ can move this from
2601 * a cpu pinned worker to an unbound one. We can operate on a different
2602 * cpu which is allright but we also have to make sure to not move to
2603 * a different one.
2604 */
2605 preempt_disable();
Mel Gorman0ccce3b2017-02-24 14:56:32 -08002606 drain_local_pages(NULL);
Michal Hockoa459eeb2017-02-24 14:56:35 -08002607 preempt_enable();
Mel Gorman0ccce3b2017-02-24 14:56:32 -08002608}
2609
Christoph Lameter9f8f2172008-02-04 22:29:11 -08002610/*
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07002611 * Spill all the per-cpu pages from all CPUs back into the buddy allocator.
2612 *
Vlastimil Babka93481ff2014-12-10 15:43:01 -08002613 * When zone parameter is non-NULL, spill just the single zone's pages.
2614 *
Mel Gorman0ccce3b2017-02-24 14:56:32 -08002615 * Note that this can be extremely slow as the draining happens in a workqueue.
Christoph Lameter9f8f2172008-02-04 22:29:11 -08002616 */
Vlastimil Babka93481ff2014-12-10 15:43:01 -08002617void drain_all_pages(struct zone *zone)
Christoph Lameter9f8f2172008-02-04 22:29:11 -08002618{
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07002619 int cpu;
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07002620
2621 /*
2622 * Allocate in the BSS so we wont require allocation in
2623 * direct reclaim path for CONFIG_CPUMASK_OFFSTACK=y
2624 */
2625 static cpumask_t cpus_with_pcps;
2626
Michal Hockoce612872017-04-07 16:05:05 -07002627 /*
2628 * Make sure nobody triggers this path before mm_percpu_wq is fully
2629 * initialized.
2630 */
2631 if (WARN_ON_ONCE(!mm_percpu_wq))
2632 return;
2633
Mel Gormanbd233f52017-02-24 14:56:56 -08002634 /*
2635 * Do not drain if one is already in progress unless it's specific to
2636 * a zone. Such callers are primarily CMA and memory hotplug and need
2637 * the drain to be complete when the call returns.
2638 */
2639 if (unlikely(!mutex_trylock(&pcpu_drain_mutex))) {
2640 if (!zone)
2641 return;
2642 mutex_lock(&pcpu_drain_mutex);
2643 }
Mel Gorman0ccce3b2017-02-24 14:56:32 -08002644
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07002645 /*
2646 * We don't care about racing with CPU hotplug event
2647 * as offline notification will cause the notified
2648 * cpu to drain that CPU pcps and on_each_cpu_mask
2649 * disables preemption as part of its processing
2650 */
2651 for_each_online_cpu(cpu) {
Vlastimil Babka93481ff2014-12-10 15:43:01 -08002652 struct per_cpu_pageset *pcp;
2653 struct zone *z;
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07002654 bool has_pcps = false;
Vlastimil Babka93481ff2014-12-10 15:43:01 -08002655
2656 if (zone) {
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07002657 pcp = per_cpu_ptr(zone->pageset, cpu);
Vlastimil Babka93481ff2014-12-10 15:43:01 -08002658 if (pcp->pcp.count)
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07002659 has_pcps = true;
Vlastimil Babka93481ff2014-12-10 15:43:01 -08002660 } else {
2661 for_each_populated_zone(z) {
2662 pcp = per_cpu_ptr(z->pageset, cpu);
2663 if (pcp->pcp.count) {
2664 has_pcps = true;
2665 break;
2666 }
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07002667 }
2668 }
Vlastimil Babka93481ff2014-12-10 15:43:01 -08002669
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07002670 if (has_pcps)
2671 cpumask_set_cpu(cpu, &cpus_with_pcps);
2672 else
2673 cpumask_clear_cpu(cpu, &cpus_with_pcps);
2674 }
Mel Gorman0ccce3b2017-02-24 14:56:32 -08002675
Mel Gormanbd233f52017-02-24 14:56:56 -08002676 for_each_cpu(cpu, &cpus_with_pcps) {
2677 struct work_struct *work = per_cpu_ptr(&pcpu_drain, cpu);
2678 INIT_WORK(work, drain_local_pages_wq);
Michal Hockoce612872017-04-07 16:05:05 -07002679 queue_work_on(cpu, mm_percpu_wq, work);
Mel Gorman0ccce3b2017-02-24 14:56:32 -08002680 }
Mel Gormanbd233f52017-02-24 14:56:56 -08002681 for_each_cpu(cpu, &cpus_with_pcps)
2682 flush_work(per_cpu_ptr(&pcpu_drain, cpu));
2683
2684 mutex_unlock(&pcpu_drain_mutex);
Christoph Lameter9f8f2172008-02-04 22:29:11 -08002685}
2686
Rafael J. Wysocki296699d2007-07-29 23:27:18 +02002687#ifdef CONFIG_HIBERNATION
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
Chen Yu556b9692017-08-25 15:55:30 -07002689/*
2690 * Touch the watchdog for every WD_PAGE_COUNT pages.
2691 */
2692#define WD_PAGE_COUNT (128*1024)
2693
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694void mark_free_pages(struct zone *zone)
2695{
Chen Yu556b9692017-08-25 15:55:30 -07002696 unsigned long pfn, max_zone_pfn, page_count = WD_PAGE_COUNT;
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07002697 unsigned long flags;
Mel Gorman7aeb09f2014-06-04 16:10:21 -07002698 unsigned int order, t;
Geliang Tang86760a22016-01-14 15:20:33 -08002699 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
Xishi Qiu8080fc02013-09-11 14:21:45 -07002701 if (zone_is_empty(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 return;
2703
2704 spin_lock_irqsave(&zone->lock, flags);
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07002705
Cody P Schafer108bcc92013-02-22 16:35:23 -08002706 max_zone_pfn = zone_end_pfn(zone);
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07002707 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
2708 if (pfn_valid(pfn)) {
Geliang Tang86760a22016-01-14 15:20:33 -08002709 page = pfn_to_page(pfn);
Joonsoo Kimba6b0972016-05-19 17:12:16 -07002710
Chen Yu556b9692017-08-25 15:55:30 -07002711 if (!--page_count) {
2712 touch_nmi_watchdog();
2713 page_count = WD_PAGE_COUNT;
2714 }
2715
Joonsoo Kimba6b0972016-05-19 17:12:16 -07002716 if (page_zone(page) != zone)
2717 continue;
2718
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07002719 if (!swsusp_page_is_forbidden(page))
2720 swsusp_unset_page_free(page);
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07002721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002723 for_each_migratetype_order(order, t) {
Geliang Tang86760a22016-01-14 15:20:33 -08002724 list_for_each_entry(page,
2725 &zone->free_area[order].free_list[t], lru) {
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07002726 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
Geliang Tang86760a22016-01-14 15:20:33 -08002728 pfn = page_to_pfn(page);
Chen Yu556b9692017-08-25 15:55:30 -07002729 for (i = 0; i < (1UL << order); i++) {
2730 if (!--page_count) {
2731 touch_nmi_watchdog();
2732 page_count = WD_PAGE_COUNT;
2733 }
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07002734 swsusp_set_page_free(pfn_to_page(pfn + i));
Chen Yu556b9692017-08-25 15:55:30 -07002735 }
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07002736 }
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 spin_unlock_irqrestore(&zone->lock, flags);
2739}
Mel Gormane2c55dc2007-10-16 01:25:50 -07002740#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
Mel Gorman2d4894b2017-11-15 17:37:59 -08002742static bool free_unref_page_prepare(struct page *page, unsigned long pfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743{
Mel Gorman5f8dcc22009-09-21 17:03:19 -07002744 int migratetype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
Mel Gorman4db75482016-05-19 17:14:32 -07002746 if (!free_pcp_prepare(page))
Mel Gorman9cca35d42017-11-15 17:37:37 -08002747 return false;
Hugh Dickins689bceb2005-11-21 21:32:20 -08002748
Mel Gormandc4b0ca2014-06-04 16:10:17 -07002749 migratetype = get_pfnblock_migratetype(page, pfn);
Vlastimil Babkabb14c2c2015-09-08 15:01:25 -07002750 set_pcppage_migratetype(page, migratetype);
Mel Gorman9cca35d42017-11-15 17:37:37 -08002751 return true;
2752}
2753
Mel Gorman2d4894b2017-11-15 17:37:59 -08002754static void free_unref_page_commit(struct page *page, unsigned long pfn)
Mel Gorman9cca35d42017-11-15 17:37:37 -08002755{
2756 struct zone *zone = page_zone(page);
2757 struct per_cpu_pages *pcp;
2758 int migratetype;
2759
2760 migratetype = get_pcppage_migratetype(page);
Mel Gormand34b0732017-04-20 14:37:43 -07002761 __count_vm_event(PGFREE);
Mel Gormanda456f12009-06-16 15:32:08 -07002762
Mel Gorman5f8dcc22009-09-21 17:03:19 -07002763 /*
2764 * We only track unmovable, reclaimable and movable on pcp lists.
2765 * Free ISOLATE pages back to the allocator because they are being
Xishi Qiua6ffdc02017-05-03 14:52:52 -07002766 * offlined but treat HIGHATOMIC as movable pages so we can get those
Mel Gorman5f8dcc22009-09-21 17:03:19 -07002767 * areas back if necessary. Otherwise, we may have to free
2768 * excessively into the page allocator
2769 */
2770 if (migratetype >= MIGRATE_PCPTYPES) {
Minchan Kim194159f2013-02-22 16:33:58 -08002771 if (unlikely(is_migrate_isolate(migratetype))) {
Mel Gormandc4b0ca2014-06-04 16:10:17 -07002772 free_one_page(zone, page, pfn, 0, migratetype);
Mel Gorman9cca35d42017-11-15 17:37:37 -08002773 return;
Mel Gorman5f8dcc22009-09-21 17:03:19 -07002774 }
2775 migratetype = MIGRATE_MOVABLE;
2776 }
2777
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09002778 pcp = &this_cpu_ptr(zone->pageset)->pcp;
Mel Gorman2d4894b2017-11-15 17:37:59 -08002779 list_add(&page->lru, &pcp->lists[migratetype]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 pcp->count++;
Nick Piggin48db57f2006-01-08 01:00:42 -08002781 if (pcp->count >= pcp->high) {
Jason Low4db0c3c2015-04-15 16:14:08 -07002782 unsigned long batch = READ_ONCE(pcp->batch);
Cody P Schafer998d39cb2013-07-03 15:01:32 -07002783 free_pcppages_bulk(zone, batch, pcp);
Nick Piggin48db57f2006-01-08 01:00:42 -08002784 }
Mel Gorman9cca35d42017-11-15 17:37:37 -08002785}
Mel Gorman5f8dcc22009-09-21 17:03:19 -07002786
Mel Gorman9cca35d42017-11-15 17:37:37 -08002787/*
2788 * Free a 0-order page
Mel Gorman9cca35d42017-11-15 17:37:37 -08002789 */
Mel Gorman2d4894b2017-11-15 17:37:59 -08002790void free_unref_page(struct page *page)
Mel Gorman9cca35d42017-11-15 17:37:37 -08002791{
2792 unsigned long flags;
2793 unsigned long pfn = page_to_pfn(page);
2794
Mel Gorman2d4894b2017-11-15 17:37:59 -08002795 if (!free_unref_page_prepare(page, pfn))
Mel Gorman9cca35d42017-11-15 17:37:37 -08002796 return;
2797
2798 local_irq_save(flags);
Mel Gorman2d4894b2017-11-15 17:37:59 -08002799 free_unref_page_commit(page, pfn);
Mel Gormand34b0732017-04-20 14:37:43 -07002800 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801}
2802
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08002803/*
Konstantin Khlebnikovcc598502012-01-10 15:07:04 -08002804 * Free a list of 0-order pages
2805 */
Mel Gorman2d4894b2017-11-15 17:37:59 -08002806void free_unref_page_list(struct list_head *list)
Konstantin Khlebnikovcc598502012-01-10 15:07:04 -08002807{
2808 struct page *page, *next;
Mel Gorman9cca35d42017-11-15 17:37:37 -08002809 unsigned long flags, pfn;
Lucas Stachc24ad772017-12-14 15:32:55 -08002810 int batch_count = 0;
Konstantin Khlebnikovcc598502012-01-10 15:07:04 -08002811
Mel Gorman9cca35d42017-11-15 17:37:37 -08002812 /* Prepare pages for freeing */
Konstantin Khlebnikovcc598502012-01-10 15:07:04 -08002813 list_for_each_entry_safe(page, next, list, lru) {
Mel Gorman9cca35d42017-11-15 17:37:37 -08002814 pfn = page_to_pfn(page);
Mel Gorman2d4894b2017-11-15 17:37:59 -08002815 if (!free_unref_page_prepare(page, pfn))
Mel Gorman9cca35d42017-11-15 17:37:37 -08002816 list_del(&page->lru);
2817 set_page_private(page, pfn);
Konstantin Khlebnikovcc598502012-01-10 15:07:04 -08002818 }
Mel Gorman9cca35d42017-11-15 17:37:37 -08002819
2820 local_irq_save(flags);
2821 list_for_each_entry_safe(page, next, list, lru) {
2822 unsigned long pfn = page_private(page);
2823
2824 set_page_private(page, 0);
Mel Gorman2d4894b2017-11-15 17:37:59 -08002825 trace_mm_page_free_batched(page);
2826 free_unref_page_commit(page, pfn);
Lucas Stachc24ad772017-12-14 15:32:55 -08002827
2828 /*
2829 * Guard against excessive IRQ disabled times when we get
2830 * a large list of pages to free.
2831 */
2832 if (++batch_count == SWAP_CLUSTER_MAX) {
2833 local_irq_restore(flags);
2834 batch_count = 0;
2835 local_irq_save(flags);
2836 }
Mel Gorman9cca35d42017-11-15 17:37:37 -08002837 }
2838 local_irq_restore(flags);
Konstantin Khlebnikovcc598502012-01-10 15:07:04 -08002839}
2840
2841/*
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08002842 * split_page takes a non-compound higher-order page, and splits it into
2843 * n (1<<order) sub-pages: page[0..n]
2844 * Each sub-page must be freed individually.
2845 *
2846 * Note: this is probably too low level an operation for use in drivers.
2847 * Please consult with lkml before using this in your driver.
2848 */
2849void split_page(struct page *page, unsigned int order)
2850{
2851 int i;
2852
Sasha Levin309381fea2014-01-23 15:52:54 -08002853 VM_BUG_ON_PAGE(PageCompound(page), page);
2854 VM_BUG_ON_PAGE(!page_count(page), page);
Vegard Nossumb1eeab62008-11-25 16:55:53 +01002855
Joonsoo Kima9627bc2016-07-26 15:23:49 -07002856 for (i = 1; i < (1 << order); i++)
Nick Piggin7835e982006-03-22 00:08:40 -08002857 set_page_refcounted(page + i);
Joonsoo Kima9627bc2016-07-26 15:23:49 -07002858 split_page_owner(page, order);
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08002859}
K. Y. Srinivasan5853ff22013-03-25 15:47:38 -07002860EXPORT_SYMBOL_GPL(split_page);
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08002861
Joonsoo Kim3c605092014-11-13 15:19:21 -08002862int __isolate_free_page(struct page *page, unsigned int order)
Mel Gorman748446b2010-05-24 14:32:27 -07002863{
Mel Gorman748446b2010-05-24 14:32:27 -07002864 unsigned long watermark;
2865 struct zone *zone;
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -07002866 int mt;
Mel Gorman748446b2010-05-24 14:32:27 -07002867
2868 BUG_ON(!PageBuddy(page));
2869
2870 zone = page_zone(page);
Marek Szyprowski2e30abd2012-12-11 16:02:57 -08002871 mt = get_pageblock_migratetype(page);
Mel Gorman748446b2010-05-24 14:32:27 -07002872
Minchan Kim194159f2013-02-22 16:33:58 -08002873 if (!is_migrate_isolate(mt)) {
Vlastimil Babka8348faf2016-10-07 16:58:00 -07002874 /*
2875 * Obey watermarks as if the page was being allocated. We can
2876 * emulate a high-order watermark check with a raised order-0
2877 * watermark, because we already know our high-order page
2878 * exists.
2879 */
2880 watermark = min_wmark_pages(zone) + (1UL << order);
Joonsoo Kimd883c6c2018-05-23 10:18:21 +09002881 if (!zone_watermark_ok(zone, 0, watermark, 0, ALLOC_CMA))
Marek Szyprowski2e30abd2012-12-11 16:02:57 -08002882 return 0;
2883
Mel Gorman8fb74b92013-01-11 14:32:16 -08002884 __mod_zone_freepage_state(zone, -(1UL << order), mt);
Marek Szyprowski2e30abd2012-12-11 16:02:57 -08002885 }
Mel Gorman748446b2010-05-24 14:32:27 -07002886
2887 /* Remove page from free list */
2888 list_del(&page->lru);
2889 zone->free_area[order].nr_free--;
2890 rmv_page_order(page);
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -07002891
zhong jiang400bc7f2016-07-28 15:45:07 -07002892 /*
2893 * Set the pageblock if the isolated page is at least half of a
2894 * pageblock
2895 */
Mel Gorman748446b2010-05-24 14:32:27 -07002896 if (order >= pageblock_order - 1) {
2897 struct page *endpage = page + (1 << order) - 1;
Michal Nazarewicz47118af2011-12-29 13:09:50 +01002898 for (; page < endpage; page += pageblock_nr_pages) {
2899 int mt = get_pageblock_migratetype(page);
Minchan Kim88ed3652016-12-12 16:42:05 -08002900 if (!is_migrate_isolate(mt) && !is_migrate_cma(mt)
Xishi Qiua6ffdc02017-05-03 14:52:52 -07002901 && !is_migrate_highatomic(mt))
Michal Nazarewicz47118af2011-12-29 13:09:50 +01002902 set_pageblock_migratetype(page,
2903 MIGRATE_MOVABLE);
2904 }
Mel Gorman748446b2010-05-24 14:32:27 -07002905 }
2906
Joonsoo Kimf3a14ce2015-07-17 16:24:15 -07002907
Mel Gorman8fb74b92013-01-11 14:32:16 -08002908 return 1UL << order;
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07002909}
2910
2911/*
Mel Gorman060e7412016-05-19 17:13:27 -07002912 * Update NUMA hit/miss statistics
2913 *
2914 * Must be called with interrupts disabled.
Mel Gorman060e7412016-05-19 17:13:27 -07002915 */
Michal Hocko41b61672017-01-10 16:57:42 -08002916static inline void zone_statistics(struct zone *preferred_zone, struct zone *z)
Mel Gorman060e7412016-05-19 17:13:27 -07002917{
2918#ifdef CONFIG_NUMA
Kemi Wang3a321d22017-09-08 16:12:48 -07002919 enum numa_stat_item local_stat = NUMA_LOCAL;
Mel Gorman060e7412016-05-19 17:13:27 -07002920
Kemi Wang45180852017-11-15 17:38:22 -08002921 /* skip numa counters update if numa stats is disabled */
2922 if (!static_branch_likely(&vm_numa_stat_key))
2923 return;
2924
Pavel Tatashinc1093b72018-08-21 21:53:32 -07002925 if (zone_to_nid(z) != numa_node_id())
Mel Gorman060e7412016-05-19 17:13:27 -07002926 local_stat = NUMA_OTHER;
Mel Gorman060e7412016-05-19 17:13:27 -07002927
Pavel Tatashinc1093b72018-08-21 21:53:32 -07002928 if (zone_to_nid(z) == zone_to_nid(preferred_zone))
Kemi Wang3a321d22017-09-08 16:12:48 -07002929 __inc_numa_state(z, NUMA_HIT);
Michal Hocko2df26632017-01-10 16:57:39 -08002930 else {
Kemi Wang3a321d22017-09-08 16:12:48 -07002931 __inc_numa_state(z, NUMA_MISS);
2932 __inc_numa_state(preferred_zone, NUMA_FOREIGN);
Mel Gorman060e7412016-05-19 17:13:27 -07002933 }
Kemi Wang3a321d22017-09-08 16:12:48 -07002934 __inc_numa_state(z, local_stat);
Mel Gorman060e7412016-05-19 17:13:27 -07002935#endif
2936}
2937
Mel Gorman066b2392017-02-24 14:56:26 -08002938/* Remove page from the per-cpu list, caller must protect the list */
2939static struct page *__rmqueue_pcplist(struct zone *zone, int migratetype,
Mel Gorman453f85d2017-11-15 17:38:03 -08002940 struct per_cpu_pages *pcp,
Mel Gorman066b2392017-02-24 14:56:26 -08002941 struct list_head *list)
2942{
2943 struct page *page;
2944
2945 do {
2946 if (list_empty(list)) {
2947 pcp->count += rmqueue_bulk(zone, 0,
2948 pcp->batch, list,
Mel Gorman453f85d2017-11-15 17:38:03 -08002949 migratetype);
Mel Gorman066b2392017-02-24 14:56:26 -08002950 if (unlikely(list_empty(list)))
2951 return NULL;
2952 }
2953
Mel Gorman453f85d2017-11-15 17:38:03 -08002954 page = list_first_entry(list, struct page, lru);
Mel Gorman066b2392017-02-24 14:56:26 -08002955 list_del(&page->lru);
2956 pcp->count--;
2957 } while (check_new_pcp(page));
2958
2959 return page;
2960}
2961
2962/* Lock and remove page from the per-cpu list */
2963static struct page *rmqueue_pcplist(struct zone *preferred_zone,
2964 struct zone *zone, unsigned int order,
2965 gfp_t gfp_flags, int migratetype)
2966{
2967 struct per_cpu_pages *pcp;
2968 struct list_head *list;
Mel Gorman066b2392017-02-24 14:56:26 -08002969 struct page *page;
Mel Gormand34b0732017-04-20 14:37:43 -07002970 unsigned long flags;
Mel Gorman066b2392017-02-24 14:56:26 -08002971
Mel Gormand34b0732017-04-20 14:37:43 -07002972 local_irq_save(flags);
Mel Gorman066b2392017-02-24 14:56:26 -08002973 pcp = &this_cpu_ptr(zone->pageset)->pcp;
2974 list = &pcp->lists[migratetype];
Mel Gorman453f85d2017-11-15 17:38:03 -08002975 page = __rmqueue_pcplist(zone, migratetype, pcp, list);
Mel Gorman066b2392017-02-24 14:56:26 -08002976 if (page) {
2977 __count_zid_vm_events(PGALLOC, page_zonenum(page), 1 << order);
2978 zone_statistics(preferred_zone, zone);
2979 }
Mel Gormand34b0732017-04-20 14:37:43 -07002980 local_irq_restore(flags);
Mel Gorman066b2392017-02-24 14:56:26 -08002981 return page;
2982}
2983
Mel Gorman060e7412016-05-19 17:13:27 -07002984/*
Vlastimil Babka75379192015-02-11 15:25:38 -08002985 * Allocate a page from the given zone. Use pcplists for order-0 allocations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 */
Mel Gorman0a15c3e2009-06-16 15:32:05 -07002987static inline
Mel Gorman066b2392017-02-24 14:56:26 -08002988struct page *rmqueue(struct zone *preferred_zone,
Mel Gorman7aeb09f2014-06-04 16:10:21 -07002989 struct zone *zone, unsigned int order,
Mel Gormanc6038442016-05-19 17:13:38 -07002990 gfp_t gfp_flags, unsigned int alloc_flags,
2991 int migratetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992{
2993 unsigned long flags;
Hugh Dickins689bceb2005-11-21 21:32:20 -08002994 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
Mel Gormand34b0732017-04-20 14:37:43 -07002996 if (likely(order == 0)) {
Mel Gorman066b2392017-02-24 14:56:26 -08002997 page = rmqueue_pcplist(preferred_zone, zone, order,
2998 gfp_flags, migratetype);
2999 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 }
3001
Mel Gorman066b2392017-02-24 14:56:26 -08003002 /*
3003 * We most definitely don't want callers attempting to
3004 * allocate greater than order-1 page units with __GFP_NOFAIL.
3005 */
3006 WARN_ON_ONCE((gfp_flags & __GFP_NOFAIL) && (order > 1));
3007 spin_lock_irqsave(&zone->lock, flags);
3008
3009 do {
3010 page = NULL;
3011 if (alloc_flags & ALLOC_HARDER) {
3012 page = __rmqueue_smallest(zone, order, MIGRATE_HIGHATOMIC);
3013 if (page)
3014 trace_mm_page_alloc_zone_locked(page, order, migratetype);
3015 }
3016 if (!page)
3017 page = __rmqueue(zone, order, migratetype);
3018 } while (page && check_new_pages(page, order));
3019 spin_unlock(&zone->lock);
3020 if (!page)
3021 goto failed;
3022 __mod_zone_freepage_state(zone, -(1 << order),
3023 get_pcppage_migratetype(page));
3024
Mel Gorman16709d12016-07-28 15:46:56 -07003025 __count_zid_vm_events(PGALLOC, page_zonenum(page), 1 << order);
Michal Hocko41b61672017-01-10 16:57:42 -08003026 zone_statistics(preferred_zone, zone);
Nick Piggina74609f2006-01-06 00:11:20 -08003027 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
Mel Gorman066b2392017-02-24 14:56:26 -08003029out:
3030 VM_BUG_ON_PAGE(page && bad_range(zone, page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 return page;
Nick Piggina74609f2006-01-06 00:11:20 -08003032
3033failed:
3034 local_irq_restore(flags);
Nick Piggina74609f2006-01-06 00:11:20 -08003035 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036}
3037
Akinobu Mita933e3122006-12-08 02:39:45 -08003038#ifdef CONFIG_FAIL_PAGE_ALLOC
3039
Akinobu Mitab2588c42011-07-26 16:09:03 -07003040static struct {
Akinobu Mita933e3122006-12-08 02:39:45 -08003041 struct fault_attr attr;
3042
Viresh Kumar621a5f72015-09-26 15:04:07 -07003043 bool ignore_gfp_highmem;
Mel Gorman71baba42015-11-06 16:28:28 -08003044 bool ignore_gfp_reclaim;
Akinobu Mita54114992007-07-15 23:40:23 -07003045 u32 min_order;
Akinobu Mita933e3122006-12-08 02:39:45 -08003046} fail_page_alloc = {
3047 .attr = FAULT_ATTR_INITIALIZER,
Mel Gorman71baba42015-11-06 16:28:28 -08003048 .ignore_gfp_reclaim = true,
Viresh Kumar621a5f72015-09-26 15:04:07 -07003049 .ignore_gfp_highmem = true,
Akinobu Mita54114992007-07-15 23:40:23 -07003050 .min_order = 1,
Akinobu Mita933e3122006-12-08 02:39:45 -08003051};
3052
3053static int __init setup_fail_page_alloc(char *str)
3054{
3055 return setup_fault_attr(&fail_page_alloc.attr, str);
3056}
3057__setup("fail_page_alloc=", setup_fail_page_alloc);
3058
Gavin Shandeaf3862012-07-31 16:41:51 -07003059static bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
Akinobu Mita933e3122006-12-08 02:39:45 -08003060{
Akinobu Mita54114992007-07-15 23:40:23 -07003061 if (order < fail_page_alloc.min_order)
Gavin Shandeaf3862012-07-31 16:41:51 -07003062 return false;
Akinobu Mita933e3122006-12-08 02:39:45 -08003063 if (gfp_mask & __GFP_NOFAIL)
Gavin Shandeaf3862012-07-31 16:41:51 -07003064 return false;
Akinobu Mita933e3122006-12-08 02:39:45 -08003065 if (fail_page_alloc.ignore_gfp_highmem && (gfp_mask & __GFP_HIGHMEM))
Gavin Shandeaf3862012-07-31 16:41:51 -07003066 return false;
Mel Gorman71baba42015-11-06 16:28:28 -08003067 if (fail_page_alloc.ignore_gfp_reclaim &&
3068 (gfp_mask & __GFP_DIRECT_RECLAIM))
Gavin Shandeaf3862012-07-31 16:41:51 -07003069 return false;
Akinobu Mita933e3122006-12-08 02:39:45 -08003070
3071 return should_fail(&fail_page_alloc.attr, 1 << order);
3072}
3073
3074#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
3075
3076static int __init fail_page_alloc_debugfs(void)
3077{
Joe Perches0825a6f2018-06-14 15:27:58 -07003078 umode_t mode = S_IFREG | 0600;
Akinobu Mita933e3122006-12-08 02:39:45 -08003079 struct dentry *dir;
Akinobu Mita933e3122006-12-08 02:39:45 -08003080
Akinobu Mitadd48c082011-08-03 16:21:01 -07003081 dir = fault_create_debugfs_attr("fail_page_alloc", NULL,
3082 &fail_page_alloc.attr);
3083 if (IS_ERR(dir))
3084 return PTR_ERR(dir);
Akinobu Mita933e3122006-12-08 02:39:45 -08003085
Akinobu Mitab2588c42011-07-26 16:09:03 -07003086 if (!debugfs_create_bool("ignore-gfp-wait", mode, dir,
Mel Gorman71baba42015-11-06 16:28:28 -08003087 &fail_page_alloc.ignore_gfp_reclaim))
Akinobu Mitab2588c42011-07-26 16:09:03 -07003088 goto fail;
3089 if (!debugfs_create_bool("ignore-gfp-highmem", mode, dir,
3090 &fail_page_alloc.ignore_gfp_highmem))
3091 goto fail;
3092 if (!debugfs_create_u32("min-order", mode, dir,
3093 &fail_page_alloc.min_order))
3094 goto fail;
Akinobu Mita933e3122006-12-08 02:39:45 -08003095
Akinobu Mitab2588c42011-07-26 16:09:03 -07003096 return 0;
3097fail:
Akinobu Mitadd48c082011-08-03 16:21:01 -07003098 debugfs_remove_recursive(dir);
Akinobu Mita933e3122006-12-08 02:39:45 -08003099
Akinobu Mitab2588c42011-07-26 16:09:03 -07003100 return -ENOMEM;
Akinobu Mita933e3122006-12-08 02:39:45 -08003101}
3102
3103late_initcall(fail_page_alloc_debugfs);
3104
3105#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
3106
3107#else /* CONFIG_FAIL_PAGE_ALLOC */
3108
Gavin Shandeaf3862012-07-31 16:41:51 -07003109static inline bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
Akinobu Mita933e3122006-12-08 02:39:45 -08003110{
Gavin Shandeaf3862012-07-31 16:41:51 -07003111 return false;
Akinobu Mita933e3122006-12-08 02:39:45 -08003112}
3113
3114#endif /* CONFIG_FAIL_PAGE_ALLOC */
3115
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116/*
Mel Gorman97a16fc2015-11-06 16:28:40 -08003117 * Return true if free base pages are above 'mark'. For high-order checks it
3118 * will return true of the order-0 watermark is reached and there is at least
3119 * one free page of a suitable size. Checking now avoids taking the zone lock
3120 * to check in the allocation paths if no pages are free.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 */
Michal Hocko86a294a2016-05-20 16:57:12 -07003122bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
3123 int classzone_idx, unsigned int alloc_flags,
3124 long free_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125{
Christoph Lameterd23ad422007-02-10 01:43:02 -08003126 long min = mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 int o;
Michal Hockocd04ae12017-09-06 16:24:50 -07003128 const bool alloc_harder = (alloc_flags & (ALLOC_HARDER|ALLOC_OOM));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
Mel Gorman0aaa29a2015-11-06 16:28:37 -08003130 /* free_pages may go negative - that's OK */
Michal Hockodf0a6da2012-01-10 15:08:02 -08003131 free_pages -= (1 << order) - 1;
Mel Gorman0aaa29a2015-11-06 16:28:37 -08003132
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08003133 if (alloc_flags & ALLOC_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 min -= min / 2;
Mel Gorman0aaa29a2015-11-06 16:28:37 -08003135
3136 /*
3137 * If the caller does not have rights to ALLOC_HARDER then subtract
3138 * the high-atomic reserves. This will over-estimate the size of the
3139 * atomic reserve but it avoids a search.
3140 */
Michal Hockocd04ae12017-09-06 16:24:50 -07003141 if (likely(!alloc_harder)) {
Mel Gorman0aaa29a2015-11-06 16:28:37 -08003142 free_pages -= z->nr_reserved_highatomic;
Michal Hockocd04ae12017-09-06 16:24:50 -07003143 } else {
3144 /*
3145 * OOM victims can try even harder than normal ALLOC_HARDER
3146 * users on the grounds that it's definitely going to be in
3147 * the exit path shortly and free memory. Any allocation it
3148 * makes during the free path will be small and short-lived.
3149 */
3150 if (alloc_flags & ALLOC_OOM)
3151 min -= min / 2;
3152 else
3153 min -= min / 4;
3154 }
3155
Mel Gormane2b19192015-11-06 16:28:09 -08003156
Joonsoo Kimd883c6c2018-05-23 10:18:21 +09003157#ifdef CONFIG_CMA
3158 /* If allocation can't use CMA areas don't use free CMA pages */
3159 if (!(alloc_flags & ALLOC_CMA))
3160 free_pages -= zone_page_state(z, NR_FREE_CMA_PAGES);
3161#endif
3162
Mel Gorman97a16fc2015-11-06 16:28:40 -08003163 /*
3164 * Check watermarks for an order-0 allocation request. If these
3165 * are not met, then a high-order request also cannot go ahead
3166 * even if a suitable page happened to be free.
3167 */
3168 if (free_pages <= min + z->lowmem_reserve[classzone_idx])
Mel Gorman88f5acf2011-01-13 15:45:41 -08003169 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170
Mel Gorman97a16fc2015-11-06 16:28:40 -08003171 /* If this is an order-0 request then the watermark is fine */
3172 if (!order)
3173 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174
Mel Gorman97a16fc2015-11-06 16:28:40 -08003175 /* For a high-order request, check at least one suitable page is free */
3176 for (o = order; o < MAX_ORDER; o++) {
3177 struct free_area *area = &z->free_area[o];
3178 int mt;
3179
3180 if (!area->nr_free)
3181 continue;
3182
Mel Gorman97a16fc2015-11-06 16:28:40 -08003183 for (mt = 0; mt < MIGRATE_PCPTYPES; mt++) {
3184 if (!list_empty(&area->free_list[mt]))
3185 return true;
3186 }
3187
3188#ifdef CONFIG_CMA
Joonsoo Kimd883c6c2018-05-23 10:18:21 +09003189 if ((alloc_flags & ALLOC_CMA) &&
3190 !list_empty(&area->free_list[MIGRATE_CMA])) {
Mel Gorman97a16fc2015-11-06 16:28:40 -08003191 return true;
Joonsoo Kimd883c6c2018-05-23 10:18:21 +09003192 }
Mel Gorman97a16fc2015-11-06 16:28:40 -08003193#endif
Vlastimil Babkab050e372017-11-15 17:38:30 -08003194 if (alloc_harder &&
3195 !list_empty(&area->free_list[MIGRATE_HIGHATOMIC]))
3196 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 }
Mel Gorman97a16fc2015-11-06 16:28:40 -08003198 return false;
Mel Gorman88f5acf2011-01-13 15:45:41 -08003199}
3200
Mel Gorman7aeb09f2014-06-04 16:10:21 -07003201bool zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
Mel Gormanc6038442016-05-19 17:13:38 -07003202 int classzone_idx, unsigned int alloc_flags)
Mel Gorman88f5acf2011-01-13 15:45:41 -08003203{
3204 return __zone_watermark_ok(z, order, mark, classzone_idx, alloc_flags,
3205 zone_page_state(z, NR_FREE_PAGES));
3206}
3207
Mel Gorman48ee5f32016-05-19 17:14:07 -07003208static inline bool zone_watermark_fast(struct zone *z, unsigned int order,
3209 unsigned long mark, int classzone_idx, unsigned int alloc_flags)
3210{
3211 long free_pages = zone_page_state(z, NR_FREE_PAGES);
Joonsoo Kimd883c6c2018-05-23 10:18:21 +09003212 long cma_pages = 0;
3213
3214#ifdef CONFIG_CMA
3215 /* If allocation can't use CMA areas don't use free CMA pages */
3216 if (!(alloc_flags & ALLOC_CMA))
3217 cma_pages = zone_page_state(z, NR_FREE_CMA_PAGES);
3218#endif
Mel Gorman48ee5f32016-05-19 17:14:07 -07003219
3220 /*
3221 * Fast check for order-0 only. If this fails then the reserves
3222 * need to be calculated. There is a corner case where the check
3223 * passes but only the high-order atomic reserve are free. If
3224 * the caller is !atomic then it'll uselessly search the free
3225 * list. That corner case is then slower but it is harmless.
3226 */
Joonsoo Kimd883c6c2018-05-23 10:18:21 +09003227 if (!order && (free_pages - cma_pages) > mark + z->lowmem_reserve[classzone_idx])
Mel Gorman48ee5f32016-05-19 17:14:07 -07003228 return true;
3229
3230 return __zone_watermark_ok(z, order, mark, classzone_idx, alloc_flags,
3231 free_pages);
3232}
3233
Mel Gorman7aeb09f2014-06-04 16:10:21 -07003234bool zone_watermark_ok_safe(struct zone *z, unsigned int order,
Mel Gormane2b19192015-11-06 16:28:09 -08003235 unsigned long mark, int classzone_idx)
Mel Gorman88f5acf2011-01-13 15:45:41 -08003236{
3237 long free_pages = zone_page_state(z, NR_FREE_PAGES);
3238
3239 if (z->percpu_drift_mark && free_pages < z->percpu_drift_mark)
3240 free_pages = zone_page_state_snapshot(z, NR_FREE_PAGES);
3241
Mel Gormane2b19192015-11-06 16:28:09 -08003242 return __zone_watermark_ok(z, order, mark, classzone_idx, 0,
Mel Gorman88f5acf2011-01-13 15:45:41 -08003243 free_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244}
3245
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003246#ifdef CONFIG_NUMA
David Rientjes957f8222012-10-08 16:33:24 -07003247static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone)
3248{
Gavin Shane02dc012017-02-24 14:59:33 -08003249 return node_distance(zone_to_nid(local_zone), zone_to_nid(zone)) <=
Mel Gorman5f7a75a2014-06-04 16:07:15 -07003250 RECLAIM_DISTANCE;
David Rientjes957f8222012-10-08 16:33:24 -07003251}
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003252#else /* CONFIG_NUMA */
David Rientjes957f8222012-10-08 16:33:24 -07003253static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone)
3254{
3255 return true;
3256}
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003257#endif /* CONFIG_NUMA */
3258
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08003259/*
Paul Jackson0798e512006-12-06 20:31:38 -08003260 * get_page_from_freelist goes through the zonelist trying to allocate
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08003261 * a page.
3262 */
3263static struct page *
Vlastimil Babkaa9263752015-02-11 15:25:41 -08003264get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
3265 const struct alloc_context *ac)
Martin Hicks753ee722005-06-21 17:14:41 -07003266{
Mel Gormanc33d6c02016-05-19 17:14:10 -07003267 struct zoneref *z = ac->preferred_zoneref;
Mel Gorman5117f452009-06-16 15:31:59 -07003268 struct zone *zone;
Mel Gorman3b8c0be2016-07-28 15:46:53 -07003269 struct pglist_data *last_pgdat_dirty_limit = NULL;
3270
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08003271 /*
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003272 * Scan zonelist, looking for a zone with enough free.
Vladimir Davydov344736f2014-10-20 15:50:30 +04003273 * See also __cpuset_node_allowed() comment in kernel/cpuset.c.
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08003274 */
Mel Gormanc33d6c02016-05-19 17:14:10 -07003275 for_next_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
Vlastimil Babkaa9263752015-02-11 15:25:41 -08003276 ac->nodemask) {
Mel Gormanbe06af02016-05-19 17:13:47 -07003277 struct page *page;
Johannes Weinere085dbc2013-09-11 14:20:46 -07003278 unsigned long mark;
3279
Mel Gorman664eedd2014-06-04 16:10:08 -07003280 if (cpusets_enabled() &&
3281 (alloc_flags & ALLOC_CPUSET) &&
Vlastimil Babka002f2902016-05-19 17:14:30 -07003282 !__cpuset_zone_allowed(zone, gfp_mask))
Mel Gormancd38b112011-07-25 17:12:29 -07003283 continue;
Johannes Weinera756cf52012-01-10 15:07:49 -08003284 /*
3285 * When allocating a page cache page for writing, we
Mel Gorman281e3722016-07-28 15:46:11 -07003286 * want to get it from a node that is within its dirty
3287 * limit, such that no single node holds more than its
Johannes Weinera756cf52012-01-10 15:07:49 -08003288 * proportional share of globally allowed dirty pages.
Mel Gorman281e3722016-07-28 15:46:11 -07003289 * The dirty limits take into account the node's
Johannes Weinera756cf52012-01-10 15:07:49 -08003290 * lowmem reserves and high watermark so that kswapd
3291 * should be able to balance it without having to
3292 * write pages from its LRU list.
3293 *
Johannes Weinera756cf52012-01-10 15:07:49 -08003294 * XXX: For now, allow allocations to potentially
Mel Gorman281e3722016-07-28 15:46:11 -07003295 * exceed the per-node dirty limit in the slowpath
Mel Gormanc9ab0c42015-11-06 16:28:12 -08003296 * (spread_dirty_pages unset) before going into reclaim,
Johannes Weinera756cf52012-01-10 15:07:49 -08003297 * which is important when on a NUMA setup the allowed
Mel Gorman281e3722016-07-28 15:46:11 -07003298 * nodes are together not big enough to reach the
Johannes Weinera756cf52012-01-10 15:07:49 -08003299 * global limit. The proper fix for these situations
Mel Gorman281e3722016-07-28 15:46:11 -07003300 * will require awareness of nodes in the
Johannes Weinera756cf52012-01-10 15:07:49 -08003301 * dirty-throttling and the flusher threads.
3302 */
Mel Gorman3b8c0be2016-07-28 15:46:53 -07003303 if (ac->spread_dirty_pages) {
3304 if (last_pgdat_dirty_limit == zone->zone_pgdat)
3305 continue;
3306
3307 if (!node_dirty_ok(zone->zone_pgdat)) {
3308 last_pgdat_dirty_limit = zone->zone_pgdat;
3309 continue;
3310 }
3311 }
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08003312
Johannes Weinere085dbc2013-09-11 14:20:46 -07003313 mark = zone->watermark[alloc_flags & ALLOC_WMARK_MASK];
Mel Gorman48ee5f32016-05-19 17:14:07 -07003314 if (!zone_watermark_fast(zone, order, mark,
Mel Gorman93ea9962016-05-19 17:14:13 -07003315 ac_classzone_idx(ac), alloc_flags)) {
Mel Gormanfa5e0842009-06-16 15:33:22 -07003316 int ret;
3317
Pavel Tatashinc9e97a12018-04-05 16:22:31 -07003318#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
3319 /*
3320 * Watermark failed for this zone, but see if we can
3321 * grow this zone if it contains deferred pages.
3322 */
3323 if (static_branch_unlikely(&deferred_pages)) {
3324 if (_deferred_grow_zone(zone, order))
3325 goto try_this_zone;
3326 }
3327#endif
Mel Gorman5dab2912014-06-04 16:10:14 -07003328 /* Checked here to keep the fast path fast */
3329 BUILD_BUG_ON(ALLOC_NO_WATERMARKS < NR_WMARK);
3330 if (alloc_flags & ALLOC_NO_WATERMARKS)
3331 goto try_this_zone;
3332
Mel Gormana5f5f912016-07-28 15:46:32 -07003333 if (node_reclaim_mode == 0 ||
Mel Gormanc33d6c02016-05-19 17:14:10 -07003334 !zone_allows_reclaim(ac->preferred_zoneref->zone, zone))
Mel Gormancd38b112011-07-25 17:12:29 -07003335 continue;
3336
Mel Gormana5f5f912016-07-28 15:46:32 -07003337 ret = node_reclaim(zone->zone_pgdat, gfp_mask, order);
Mel Gormanfa5e0842009-06-16 15:33:22 -07003338 switch (ret) {
Mel Gormana5f5f912016-07-28 15:46:32 -07003339 case NODE_RECLAIM_NOSCAN:
Mel Gormanfa5e0842009-06-16 15:33:22 -07003340 /* did not scan */
Mel Gormancd38b112011-07-25 17:12:29 -07003341 continue;
Mel Gormana5f5f912016-07-28 15:46:32 -07003342 case NODE_RECLAIM_FULL:
Mel Gormanfa5e0842009-06-16 15:33:22 -07003343 /* scanned but unreclaimable */
Mel Gormancd38b112011-07-25 17:12:29 -07003344 continue;
Mel Gormanfa5e0842009-06-16 15:33:22 -07003345 default:
3346 /* did we reclaim enough */
Mel Gormanfed27192013-04-29 15:07:57 -07003347 if (zone_watermark_ok(zone, order, mark,
Mel Gorman93ea9962016-05-19 17:14:13 -07003348 ac_classzone_idx(ac), alloc_flags))
Mel Gormanfed27192013-04-29 15:07:57 -07003349 goto try_this_zone;
3350
Mel Gormanfed27192013-04-29 15:07:57 -07003351 continue;
Paul Jackson0798e512006-12-06 20:31:38 -08003352 }
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08003353 }
3354
Mel Gormanfa5e0842009-06-16 15:33:22 -07003355try_this_zone:
Mel Gorman066b2392017-02-24 14:56:26 -08003356 page = rmqueue(ac->preferred_zoneref->zone, zone, order,
Mel Gorman0aaa29a2015-11-06 16:28:37 -08003357 gfp_mask, alloc_flags, ac->migratetype);
Vlastimil Babka75379192015-02-11 15:25:38 -08003358 if (page) {
Mel Gorman479f8542016-05-19 17:14:35 -07003359 prep_new_page(page, order, gfp_mask, alloc_flags);
Mel Gorman0aaa29a2015-11-06 16:28:37 -08003360
3361 /*
3362 * If this is a high-order atomic allocation then check
3363 * if the pageblock should be reserved for the future
3364 */
3365 if (unlikely(order && (alloc_flags & ALLOC_HARDER)))
3366 reserve_highatomic_pageblock(page, zone, order);
3367
Vlastimil Babka75379192015-02-11 15:25:38 -08003368 return page;
Pavel Tatashinc9e97a12018-04-05 16:22:31 -07003369 } else {
3370#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
3371 /* Try again if zone has deferred pages */
3372 if (static_branch_unlikely(&deferred_pages)) {
3373 if (_deferred_grow_zone(zone, order))
3374 goto try_this_zone;
3375 }
3376#endif
Vlastimil Babka75379192015-02-11 15:25:38 -08003377 }
Mel Gorman54a6eb52008-04-28 02:12:16 -07003378 }
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003379
Mel Gorman4ffeaf32014-08-06 16:07:22 -07003380 return NULL;
Martin Hicks753ee722005-06-21 17:14:41 -07003381}
3382
Michal Hocko9af744d2017-02-22 15:46:16 -08003383static void warn_alloc_show_mem(gfp_t gfp_mask, nodemask_t *nodemask)
Dave Hansena238ab52011-05-24 17:12:16 -07003384{
Dave Hansena238ab52011-05-24 17:12:16 -07003385 unsigned int filter = SHOW_MEM_FILTER_NODES;
Michal Hockoaa187502017-02-22 15:41:45 -08003386 static DEFINE_RATELIMIT_STATE(show_mem_rs, HZ, 1);
Dave Hansena238ab52011-05-24 17:12:16 -07003387
Michal Hocko2c029a12018-10-26 15:06:49 -07003388 if (!__ratelimit(&show_mem_rs))
Dave Hansena238ab52011-05-24 17:12:16 -07003389 return;
3390
3391 /*
3392 * This documents exceptions given to allocations in certain
3393 * contexts that are allowed to allocate outside current's set
3394 * of allowed nodes.
3395 */
3396 if (!(gfp_mask & __GFP_NOMEMALLOC))
Michal Hockocd04ae12017-09-06 16:24:50 -07003397 if (tsk_is_oom_victim(current) ||
Dave Hansena238ab52011-05-24 17:12:16 -07003398 (current->flags & (PF_MEMALLOC | PF_EXITING)))
3399 filter &= ~SHOW_MEM_FILTER_NODES;
Mel Gormand0164ad2015-11-06 16:28:21 -08003400 if (in_interrupt() || !(gfp_mask & __GFP_DIRECT_RECLAIM))
Dave Hansena238ab52011-05-24 17:12:16 -07003401 filter &= ~SHOW_MEM_FILTER_NODES;
3402
Michal Hocko9af744d2017-02-22 15:46:16 -08003403 show_mem(filter, nodemask);
Michal Hockoaa187502017-02-22 15:41:45 -08003404}
3405
Michal Hockoa8e99252017-02-22 15:46:10 -08003406void warn_alloc(gfp_t gfp_mask, nodemask_t *nodemask, const char *fmt, ...)
Michal Hockoaa187502017-02-22 15:41:45 -08003407{
3408 struct va_format vaf;
3409 va_list args;
3410 static DEFINE_RATELIMIT_STATE(nopage_rs, DEFAULT_RATELIMIT_INTERVAL,
3411 DEFAULT_RATELIMIT_BURST);
3412
Tetsuo Handa0f7896f2017-05-03 14:55:34 -07003413 if ((gfp_mask & __GFP_NOWARN) || !__ratelimit(&nopage_rs))
Michal Hockoaa187502017-02-22 15:41:45 -08003414 return;
3415
Michal Hocko7877cdc2016-10-07 17:01:55 -07003416 va_start(args, fmt);
3417 vaf.fmt = fmt;
3418 vaf.va = &args;
Michal Hocko0205f752017-11-15 17:39:14 -08003419 pr_warn("%s: %pV, mode:%#x(%pGg), nodemask=%*pbl\n",
3420 current->comm, &vaf, gfp_mask, &gfp_mask,
3421 nodemask_pr_args(nodemask));
Michal Hocko7877cdc2016-10-07 17:01:55 -07003422 va_end(args);
Joe Perches3ee9a4f2011-10-31 17:08:35 -07003423
Michal Hockoa8e99252017-02-22 15:46:10 -08003424 cpuset_print_current_mems_allowed();
Joe Perches3ee9a4f2011-10-31 17:08:35 -07003425
Dave Hansena238ab52011-05-24 17:12:16 -07003426 dump_stack();
David Rientjes685dbf62017-02-22 15:46:28 -08003427 warn_alloc_show_mem(gfp_mask, nodemask);
Dave Hansena238ab52011-05-24 17:12:16 -07003428}
3429
Mel Gorman11e33f62009-06-16 15:31:57 -07003430static inline struct page *
Michal Hocko6c18ba72017-02-22 15:46:25 -08003431__alloc_pages_cpuset_fallback(gfp_t gfp_mask, unsigned int order,
3432 unsigned int alloc_flags,
3433 const struct alloc_context *ac)
3434{
3435 struct page *page;
3436
3437 page = get_page_from_freelist(gfp_mask, order,
3438 alloc_flags|ALLOC_CPUSET, ac);
3439 /*
3440 * fallback to ignore cpuset restriction if our nodes
3441 * are depleted
3442 */
3443 if (!page)
3444 page = get_page_from_freelist(gfp_mask, order,
3445 alloc_flags, ac);
3446
3447 return page;
3448}
3449
3450static inline struct page *
Mel Gorman11e33f62009-06-16 15:31:57 -07003451__alloc_pages_may_oom(gfp_t gfp_mask, unsigned int order,
Vlastimil Babkaa9263752015-02-11 15:25:41 -08003452 const struct alloc_context *ac, unsigned long *did_some_progress)
Mel Gorman11e33f62009-06-16 15:31:57 -07003453{
David Rientjes6e0fc462015-09-08 15:00:36 -07003454 struct oom_control oc = {
3455 .zonelist = ac->zonelist,
3456 .nodemask = ac->nodemask,
Vladimir Davydov2a966b72016-07-26 15:22:33 -07003457 .memcg = NULL,
David Rientjes6e0fc462015-09-08 15:00:36 -07003458 .gfp_mask = gfp_mask,
3459 .order = order,
David Rientjes6e0fc462015-09-08 15:00:36 -07003460 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462
Johannes Weiner9879de72015-01-26 12:58:32 -08003463 *did_some_progress = 0;
3464
Johannes Weiner9879de72015-01-26 12:58:32 -08003465 /*
Johannes Weinerdc564012015-06-24 16:57:19 -07003466 * Acquire the oom lock. If that fails, somebody else is
3467 * making progress for us.
Johannes Weiner9879de72015-01-26 12:58:32 -08003468 */
Johannes Weinerdc564012015-06-24 16:57:19 -07003469 if (!mutex_trylock(&oom_lock)) {
Johannes Weiner9879de72015-01-26 12:58:32 -08003470 *did_some_progress = 1;
Mel Gorman11e33f62009-06-16 15:31:57 -07003471 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 return NULL;
3473 }
Jens Axboe6b1de912005-11-17 21:35:02 +01003474
Mel Gorman11e33f62009-06-16 15:31:57 -07003475 /*
3476 * Go through the zonelist yet one more time, keep very high watermark
3477 * here, this is only to catch a parallel oom killing, we must fail if
Tetsuo Handae746bf72017-08-31 16:15:20 -07003478 * we're still under heavy pressure. But make sure that this reclaim
3479 * attempt shall not depend on __GFP_DIRECT_RECLAIM && !__GFP_NORETRY
3480 * allocation which will never fail due to oom_lock already held.
Mel Gorman11e33f62009-06-16 15:31:57 -07003481 */
Tetsuo Handae746bf72017-08-31 16:15:20 -07003482 page = get_page_from_freelist((gfp_mask | __GFP_HARDWALL) &
3483 ~__GFP_DIRECT_RECLAIM, order,
3484 ALLOC_WMARK_HIGH|ALLOC_CPUSET, ac);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08003485 if (page)
Mel Gorman11e33f62009-06-16 15:31:57 -07003486 goto out;
3487
Michal Hocko06ad2762017-02-22 15:46:22 -08003488 /* Coredumps can quickly deplete all memory reserves */
3489 if (current->flags & PF_DUMPCORE)
3490 goto out;
3491 /* The OOM killer will not help higher order allocs */
3492 if (order > PAGE_ALLOC_COSTLY_ORDER)
3493 goto out;
Michal Hockodcda9b02017-07-12 14:36:45 -07003494 /*
3495 * We have already exhausted all our reclaim opportunities without any
3496 * success so it is time to admit defeat. We will skip the OOM killer
3497 * because it is very likely that the caller has a more reasonable
3498 * fallback than shooting a random task.
3499 */
3500 if (gfp_mask & __GFP_RETRY_MAYFAIL)
3501 goto out;
Michal Hocko06ad2762017-02-22 15:46:22 -08003502 /* The OOM killer does not needlessly kill tasks for lowmem */
3503 if (ac->high_zoneidx < ZONE_NORMAL)
3504 goto out;
3505 if (pm_suspended_storage())
3506 goto out;
3507 /*
3508 * XXX: GFP_NOFS allocations should rather fail than rely on
3509 * other request to make a forward progress.
3510 * We are in an unfortunate situation where out_of_memory cannot
3511 * do much for this context but let's try it to at least get
3512 * access to memory reserved if the current task is killed (see
3513 * out_of_memory). Once filesystems are ready to handle allocation
3514 * failures more gracefully we should just bail out here.
3515 */
Michal Hocko3da88fb32016-05-19 17:13:09 -07003516
Michal Hocko06ad2762017-02-22 15:46:22 -08003517 /* The OOM killer may not free memory on a specific node */
3518 if (gfp_mask & __GFP_THISNODE)
3519 goto out;
3520
Shile Zhang3c2c6482018-01-31 16:20:07 -08003521 /* Exhausted what can be done so it's blame time */
Michal Hocko5020e282016-01-14 15:20:36 -08003522 if (out_of_memory(&oc) || WARN_ON_ONCE(gfp_mask & __GFP_NOFAIL)) {
Michal Hockoc32b3cb2015-02-11 15:26:24 -08003523 *did_some_progress = 1;
Michal Hocko5020e282016-01-14 15:20:36 -08003524
Michal Hocko6c18ba72017-02-22 15:46:25 -08003525 /*
3526 * Help non-failing allocations by giving them access to memory
3527 * reserves
3528 */
3529 if (gfp_mask & __GFP_NOFAIL)
3530 page = __alloc_pages_cpuset_fallback(gfp_mask, order,
Michal Hocko5020e282016-01-14 15:20:36 -08003531 ALLOC_NO_WATERMARKS, ac);
Michal Hocko5020e282016-01-14 15:20:36 -08003532 }
Mel Gorman11e33f62009-06-16 15:31:57 -07003533out:
Johannes Weinerdc564012015-06-24 16:57:19 -07003534 mutex_unlock(&oom_lock);
Mel Gorman11e33f62009-06-16 15:31:57 -07003535 return page;
3536}
3537
Michal Hocko33c2d212016-05-20 16:57:06 -07003538/*
3539 * Maximum number of compaction retries wit a progress before OOM
3540 * killer is consider as the only way to move forward.
3541 */
3542#define MAX_COMPACT_RETRIES 16
3543
Mel Gorman56de7262010-05-24 14:32:30 -07003544#ifdef CONFIG_COMPACTION
3545/* Try memory compaction for high-order allocations before reclaim */
3546static struct page *
3547__alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
Mel Gormanc6038442016-05-19 17:13:38 -07003548 unsigned int alloc_flags, const struct alloc_context *ac,
Vlastimil Babkaa5508cd2016-07-28 15:49:28 -07003549 enum compact_priority prio, enum compact_result *compact_result)
Mel Gorman56de7262010-05-24 14:32:30 -07003550{
Vlastimil Babka98dd3b42014-10-09 15:27:04 -07003551 struct page *page;
Johannes Weinereb414682018-10-26 15:06:27 -07003552 unsigned long pflags;
Vlastimil Babka499118e2017-05-08 15:59:50 -07003553 unsigned int noreclaim_flag;
Vlastimil Babka53853e22014-10-09 15:27:02 -07003554
Mel Gorman66199712012-01-12 17:19:41 -08003555 if (!order)
Mel Gorman56de7262010-05-24 14:32:30 -07003556 return NULL;
3557
Johannes Weinereb414682018-10-26 15:06:27 -07003558 psi_memstall_enter(&pflags);
Vlastimil Babka499118e2017-05-08 15:59:50 -07003559 noreclaim_flag = memalloc_noreclaim_save();
Johannes Weinereb414682018-10-26 15:06:27 -07003560
Michal Hockoc5d01d02016-05-20 16:56:53 -07003561 *compact_result = try_to_compact_pages(gfp_mask, order, alloc_flags, ac,
Vlastimil Babkac3486f52016-07-28 15:49:30 -07003562 prio);
Johannes Weinereb414682018-10-26 15:06:27 -07003563
Vlastimil Babka499118e2017-05-08 15:59:50 -07003564 memalloc_noreclaim_restore(noreclaim_flag);
Johannes Weinereb414682018-10-26 15:06:27 -07003565 psi_memstall_leave(&pflags);
Mel Gorman56de7262010-05-24 14:32:30 -07003566
Michal Hockoc5d01d02016-05-20 16:56:53 -07003567 if (*compact_result <= COMPACT_INACTIVE)
Vlastimil Babka98dd3b42014-10-09 15:27:04 -07003568 return NULL;
Mel Gorman56de7262010-05-24 14:32:30 -07003569
Vlastimil Babka98dd3b42014-10-09 15:27:04 -07003570 /*
3571 * At least in one zone compaction wasn't deferred or skipped, so let's
3572 * count a compaction stall
3573 */
3574 count_vm_event(COMPACTSTALL);
3575
Vlastimil Babka31a6c192016-07-28 15:49:13 -07003576 page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac);
Vlastimil Babka98dd3b42014-10-09 15:27:04 -07003577
3578 if (page) {
3579 struct zone *zone = page_zone(page);
3580
3581 zone->compact_blockskip_flush = false;
3582 compaction_defer_reset(zone, order, true);
3583 count_vm_event(COMPACTSUCCESS);
3584 return page;
3585 }
3586
3587 /*
Vlastimil Babka98dd3b42014-10-09 15:27:04 -07003588 * It's bad if compaction run occurs and fails. The most likely reason
3589 * is that pages exist, but not enough to satisfy watermarks.
3590 */
3591 count_vm_event(COMPACTFAIL);
3592
3593 cond_resched();
3594
Mel Gorman56de7262010-05-24 14:32:30 -07003595 return NULL;
3596}
Michal Hocko33c2d212016-05-20 16:57:06 -07003597
Vlastimil Babka32508452016-10-07 17:00:28 -07003598static inline bool
3599should_compact_retry(struct alloc_context *ac, int order, int alloc_flags,
3600 enum compact_result compact_result,
3601 enum compact_priority *compact_priority,
Vlastimil Babkad9436492016-10-07 17:00:31 -07003602 int *compaction_retries)
Vlastimil Babka32508452016-10-07 17:00:28 -07003603{
3604 int max_retries = MAX_COMPACT_RETRIES;
Vlastimil Babkac2033b02016-10-07 17:00:34 -07003605 int min_priority;
Michal Hocko65190cf2017-02-22 15:42:03 -08003606 bool ret = false;
3607 int retries = *compaction_retries;
3608 enum compact_priority priority = *compact_priority;
Vlastimil Babka32508452016-10-07 17:00:28 -07003609
3610 if (!order)
3611 return false;
3612
Vlastimil Babkad9436492016-10-07 17:00:31 -07003613 if (compaction_made_progress(compact_result))
3614 (*compaction_retries)++;
3615
Vlastimil Babka32508452016-10-07 17:00:28 -07003616 /*
3617 * compaction considers all the zone as desperately out of memory
3618 * so it doesn't really make much sense to retry except when the
3619 * failure could be caused by insufficient priority
3620 */
Vlastimil Babkad9436492016-10-07 17:00:31 -07003621 if (compaction_failed(compact_result))
3622 goto check_priority;
Vlastimil Babka32508452016-10-07 17:00:28 -07003623
3624 /*
3625 * make sure the compaction wasn't deferred or didn't bail out early
3626 * due to locks contention before we declare that we should give up.
3627 * But do not retry if the given zonelist is not suitable for
3628 * compaction.
3629 */
Michal Hocko65190cf2017-02-22 15:42:03 -08003630 if (compaction_withdrawn(compact_result)) {
3631 ret = compaction_zonelist_suitable(ac, order, alloc_flags);
3632 goto out;
3633 }
Vlastimil Babka32508452016-10-07 17:00:28 -07003634
3635 /*
Michal Hockodcda9b02017-07-12 14:36:45 -07003636 * !costly requests are much more important than __GFP_RETRY_MAYFAIL
Vlastimil Babka32508452016-10-07 17:00:28 -07003637 * costly ones because they are de facto nofail and invoke OOM
3638 * killer to move on while costly can fail and users are ready
3639 * to cope with that. 1/4 retries is rather arbitrary but we
3640 * would need much more detailed feedback from compaction to
3641 * make a better decision.
3642 */
3643 if (order > PAGE_ALLOC_COSTLY_ORDER)
3644 max_retries /= 4;
Michal Hocko65190cf2017-02-22 15:42:03 -08003645 if (*compaction_retries <= max_retries) {
3646 ret = true;
3647 goto out;
3648 }
Vlastimil Babka32508452016-10-07 17:00:28 -07003649
Vlastimil Babkad9436492016-10-07 17:00:31 -07003650 /*
3651 * Make sure there are attempts at the highest priority if we exhausted
3652 * all retries or failed at the lower priorities.
3653 */
3654check_priority:
Vlastimil Babkac2033b02016-10-07 17:00:34 -07003655 min_priority = (order > PAGE_ALLOC_COSTLY_ORDER) ?
3656 MIN_COMPACT_COSTLY_PRIORITY : MIN_COMPACT_PRIORITY;
Michal Hocko65190cf2017-02-22 15:42:03 -08003657
Vlastimil Babkac2033b02016-10-07 17:00:34 -07003658 if (*compact_priority > min_priority) {
Vlastimil Babkad9436492016-10-07 17:00:31 -07003659 (*compact_priority)--;
3660 *compaction_retries = 0;
Michal Hocko65190cf2017-02-22 15:42:03 -08003661 ret = true;
Vlastimil Babkad9436492016-10-07 17:00:31 -07003662 }
Michal Hocko65190cf2017-02-22 15:42:03 -08003663out:
3664 trace_compact_retry(order, priority, compact_result, retries, max_retries, ret);
3665 return ret;
Vlastimil Babka32508452016-10-07 17:00:28 -07003666}
Mel Gorman56de7262010-05-24 14:32:30 -07003667#else
3668static inline struct page *
3669__alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
Mel Gormanc6038442016-05-19 17:13:38 -07003670 unsigned int alloc_flags, const struct alloc_context *ac,
Vlastimil Babkaa5508cd2016-07-28 15:49:28 -07003671 enum compact_priority prio, enum compact_result *compact_result)
Mel Gorman56de7262010-05-24 14:32:30 -07003672{
Michal Hocko33c2d212016-05-20 16:57:06 -07003673 *compact_result = COMPACT_SKIPPED;
Mel Gorman56de7262010-05-24 14:32:30 -07003674 return NULL;
3675}
Michal Hocko33c2d212016-05-20 16:57:06 -07003676
3677static inline bool
Michal Hocko86a294a2016-05-20 16:57:12 -07003678should_compact_retry(struct alloc_context *ac, unsigned int order, int alloc_flags,
3679 enum compact_result compact_result,
Vlastimil Babkaa5508cd2016-07-28 15:49:28 -07003680 enum compact_priority *compact_priority,
Vlastimil Babkad9436492016-10-07 17:00:31 -07003681 int *compaction_retries)
Michal Hocko33c2d212016-05-20 16:57:06 -07003682{
Michal Hocko31e49bf2016-05-20 16:57:15 -07003683 struct zone *zone;
3684 struct zoneref *z;
3685
3686 if (!order || order > PAGE_ALLOC_COSTLY_ORDER)
3687 return false;
3688
3689 /*
3690 * There are setups with compaction disabled which would prefer to loop
3691 * inside the allocator rather than hit the oom killer prematurely.
3692 * Let's give them a good hope and keep retrying while the order-0
3693 * watermarks are OK.
3694 */
3695 for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
3696 ac->nodemask) {
3697 if (zone_watermark_ok(zone, 0, min_wmark_pages(zone),
3698 ac_classzone_idx(ac), alloc_flags))
3699 return true;
3700 }
Michal Hocko33c2d212016-05-20 16:57:06 -07003701 return false;
3702}
Vlastimil Babka32508452016-10-07 17:00:28 -07003703#endif /* CONFIG_COMPACTION */
Mel Gorman56de7262010-05-24 14:32:30 -07003704
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01003705#ifdef CONFIG_LOCKDEP
Omar Sandoval93781322018-06-07 17:07:02 -07003706static struct lockdep_map __fs_reclaim_map =
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01003707 STATIC_LOCKDEP_MAP_INIT("fs_reclaim", &__fs_reclaim_map);
3708
3709static bool __need_fs_reclaim(gfp_t gfp_mask)
3710{
3711 gfp_mask = current_gfp_context(gfp_mask);
3712
3713 /* no reclaim without waiting on it */
3714 if (!(gfp_mask & __GFP_DIRECT_RECLAIM))
3715 return false;
3716
3717 /* this guy won't enter reclaim */
Tetsuo Handa2e517d682018-03-22 16:17:10 -07003718 if (current->flags & PF_MEMALLOC)
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01003719 return false;
3720
3721 /* We're only interested __GFP_FS allocations for now */
3722 if (!(gfp_mask & __GFP_FS))
3723 return false;
3724
3725 if (gfp_mask & __GFP_NOLOCKDEP)
3726 return false;
3727
3728 return true;
3729}
3730
Omar Sandoval93781322018-06-07 17:07:02 -07003731void __fs_reclaim_acquire(void)
3732{
3733 lock_map_acquire(&__fs_reclaim_map);
3734}
3735
3736void __fs_reclaim_release(void)
3737{
3738 lock_map_release(&__fs_reclaim_map);
3739}
3740
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01003741void fs_reclaim_acquire(gfp_t gfp_mask)
3742{
3743 if (__need_fs_reclaim(gfp_mask))
Omar Sandoval93781322018-06-07 17:07:02 -07003744 __fs_reclaim_acquire();
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01003745}
3746EXPORT_SYMBOL_GPL(fs_reclaim_acquire);
3747
3748void fs_reclaim_release(gfp_t gfp_mask)
3749{
3750 if (__need_fs_reclaim(gfp_mask))
Omar Sandoval93781322018-06-07 17:07:02 -07003751 __fs_reclaim_release();
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01003752}
3753EXPORT_SYMBOL_GPL(fs_reclaim_release);
3754#endif
3755
Marek Szyprowskibba90712012-01-25 12:09:52 +01003756/* Perform direct synchronous page reclaim */
3757static int
Vlastimil Babkaa9263752015-02-11 15:25:41 -08003758__perform_reclaim(gfp_t gfp_mask, unsigned int order,
3759 const struct alloc_context *ac)
Mel Gorman11e33f62009-06-16 15:31:57 -07003760{
Mel Gorman11e33f62009-06-16 15:31:57 -07003761 struct reclaim_state reclaim_state;
Marek Szyprowskibba90712012-01-25 12:09:52 +01003762 int progress;
Vlastimil Babka499118e2017-05-08 15:59:50 -07003763 unsigned int noreclaim_flag;
Johannes Weinereb414682018-10-26 15:06:27 -07003764 unsigned long pflags;
Mel Gorman11e33f62009-06-16 15:31:57 -07003765
3766 cond_resched();
3767
3768 /* We now go into synchronous reclaim */
3769 cpuset_memory_pressure_bump();
Johannes Weinereb414682018-10-26 15:06:27 -07003770 psi_memstall_enter(&pflags);
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01003771 fs_reclaim_acquire(gfp_mask);
Omar Sandoval93781322018-06-07 17:07:02 -07003772 noreclaim_flag = memalloc_noreclaim_save();
Mel Gorman11e33f62009-06-16 15:31:57 -07003773 reclaim_state.reclaimed_slab = 0;
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08003774 current->reclaim_state = &reclaim_state;
Mel Gorman11e33f62009-06-16 15:31:57 -07003775
Vlastimil Babkaa9263752015-02-11 15:25:41 -08003776 progress = try_to_free_pages(ac->zonelist, order, gfp_mask,
3777 ac->nodemask);
Mel Gorman11e33f62009-06-16 15:31:57 -07003778
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08003779 current->reclaim_state = NULL;
Vlastimil Babka499118e2017-05-08 15:59:50 -07003780 memalloc_noreclaim_restore(noreclaim_flag);
Omar Sandoval93781322018-06-07 17:07:02 -07003781 fs_reclaim_release(gfp_mask);
Johannes Weinereb414682018-10-26 15:06:27 -07003782 psi_memstall_leave(&pflags);
Mel Gorman11e33f62009-06-16 15:31:57 -07003783
3784 cond_resched();
3785
Marek Szyprowskibba90712012-01-25 12:09:52 +01003786 return progress;
3787}
3788
3789/* The really slow allocator path where we enter direct reclaim */
3790static inline struct page *
3791__alloc_pages_direct_reclaim(gfp_t gfp_mask, unsigned int order,
Mel Gormanc6038442016-05-19 17:13:38 -07003792 unsigned int alloc_flags, const struct alloc_context *ac,
Vlastimil Babkaa9263752015-02-11 15:25:41 -08003793 unsigned long *did_some_progress)
Marek Szyprowskibba90712012-01-25 12:09:52 +01003794{
3795 struct page *page = NULL;
3796 bool drained = false;
3797
Vlastimil Babkaa9263752015-02-11 15:25:41 -08003798 *did_some_progress = __perform_reclaim(gfp_mask, order, ac);
Mel Gorman9ee493c2010-09-09 16:38:18 -07003799 if (unlikely(!(*did_some_progress)))
3800 return NULL;
Mel Gorman11e33f62009-06-16 15:31:57 -07003801
Mel Gorman9ee493c2010-09-09 16:38:18 -07003802retry:
Vlastimil Babka31a6c192016-07-28 15:49:13 -07003803 page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac);
Mel Gorman9ee493c2010-09-09 16:38:18 -07003804
3805 /*
3806 * If an allocation failed after direct reclaim, it could be because
Mel Gorman0aaa29a2015-11-06 16:28:37 -08003807 * pages are pinned on the per-cpu lists or in high alloc reserves.
3808 * Shrink them them and try again
Mel Gorman9ee493c2010-09-09 16:38:18 -07003809 */
3810 if (!page && !drained) {
Minchan Kim29fac032016-12-12 16:42:14 -08003811 unreserve_highatomic_pageblock(ac, false);
Vlastimil Babka93481ff2014-12-10 15:43:01 -08003812 drain_all_pages(NULL);
Mel Gorman9ee493c2010-09-09 16:38:18 -07003813 drained = true;
3814 goto retry;
3815 }
3816
Mel Gorman11e33f62009-06-16 15:31:57 -07003817 return page;
3818}
3819
David Rientjes5ecd9d42018-04-05 16:25:16 -07003820static void wake_all_kswapds(unsigned int order, gfp_t gfp_mask,
3821 const struct alloc_context *ac)
Mel Gorman11e33f62009-06-16 15:31:57 -07003822{
3823 struct zoneref *z;
3824 struct zone *zone;
Mel Gormane1a55632016-07-28 15:46:26 -07003825 pg_data_t *last_pgdat = NULL;
David Rientjes5ecd9d42018-04-05 16:25:16 -07003826 enum zone_type high_zoneidx = ac->high_zoneidx;
Mel Gorman11e33f62009-06-16 15:31:57 -07003827
David Rientjes5ecd9d42018-04-05 16:25:16 -07003828 for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, high_zoneidx,
3829 ac->nodemask) {
Mel Gormane1a55632016-07-28 15:46:26 -07003830 if (last_pgdat != zone->zone_pgdat)
David Rientjes5ecd9d42018-04-05 16:25:16 -07003831 wakeup_kswapd(zone, gfp_mask, order, high_zoneidx);
Mel Gormane1a55632016-07-28 15:46:26 -07003832 last_pgdat = zone->zone_pgdat;
3833 }
Mel Gorman11e33f62009-06-16 15:31:57 -07003834}
3835
Mel Gormanc6038442016-05-19 17:13:38 -07003836static inline unsigned int
Peter Zijlstra341ce062009-06-16 15:32:02 -07003837gfp_to_alloc_flags(gfp_t gfp_mask)
3838{
Mel Gormanc6038442016-05-19 17:13:38 -07003839 unsigned int alloc_flags = ALLOC_WMARK_MIN | ALLOC_CPUSET;
Peter Zijlstra341ce062009-06-16 15:32:02 -07003840
Mel Gormana56f57f2009-06-16 15:32:02 -07003841 /* __GFP_HIGH is assumed to be the same as ALLOC_HIGH to save a branch. */
Namhyung Kime6223a32010-10-26 14:21:59 -07003842 BUILD_BUG_ON(__GFP_HIGH != (__force gfp_t) ALLOC_HIGH);
Mel Gormana56f57f2009-06-16 15:32:02 -07003843
Peter Zijlstra341ce062009-06-16 15:32:02 -07003844 /*
3845 * The caller may dip into page reserves a bit more if the caller
3846 * cannot run direct reclaim, or if the caller has realtime scheduling
3847 * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will
Mel Gormand0164ad2015-11-06 16:28:21 -08003848 * set both ALLOC_HARDER (__GFP_ATOMIC) and ALLOC_HIGH (__GFP_HIGH).
Peter Zijlstra341ce062009-06-16 15:32:02 -07003849 */
Namhyung Kime6223a32010-10-26 14:21:59 -07003850 alloc_flags |= (__force int) (gfp_mask & __GFP_HIGH);
Peter Zijlstra341ce062009-06-16 15:32:02 -07003851
Mel Gormand0164ad2015-11-06 16:28:21 -08003852 if (gfp_mask & __GFP_ATOMIC) {
Andrea Arcangeli5c3240d2011-01-13 15:46:49 -08003853 /*
David Rientjesb104a352014-07-30 16:08:24 -07003854 * Not worth trying to allocate harder for __GFP_NOMEMALLOC even
3855 * if it can't schedule.
Andrea Arcangeli5c3240d2011-01-13 15:46:49 -08003856 */
David Rientjesb104a352014-07-30 16:08:24 -07003857 if (!(gfp_mask & __GFP_NOMEMALLOC))
Andrea Arcangeli5c3240d2011-01-13 15:46:49 -08003858 alloc_flags |= ALLOC_HARDER;
Peter Zijlstra341ce062009-06-16 15:32:02 -07003859 /*
David Rientjesb104a352014-07-30 16:08:24 -07003860 * Ignore cpuset mems for GFP_ATOMIC rather than fail, see the
Vladimir Davydov344736f2014-10-20 15:50:30 +04003861 * comment for __cpuset_node_allowed().
Peter Zijlstra341ce062009-06-16 15:32:02 -07003862 */
3863 alloc_flags &= ~ALLOC_CPUSET;
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08003864 } else if (unlikely(rt_task(current)) && !in_interrupt())
Peter Zijlstra341ce062009-06-16 15:32:02 -07003865 alloc_flags |= ALLOC_HARDER;
3866
Joonsoo Kimd883c6c2018-05-23 10:18:21 +09003867#ifdef CONFIG_CMA
3868 if (gfpflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
3869 alloc_flags |= ALLOC_CMA;
3870#endif
Peter Zijlstra341ce062009-06-16 15:32:02 -07003871 return alloc_flags;
3872}
3873
Michal Hockocd04ae12017-09-06 16:24:50 -07003874static bool oom_reserves_allowed(struct task_struct *tsk)
Mel Gorman072bb0a2012-07-31 16:43:58 -07003875{
Michal Hockocd04ae12017-09-06 16:24:50 -07003876 if (!tsk_is_oom_victim(tsk))
Vlastimil Babka31a6c192016-07-28 15:49:13 -07003877 return false;
3878
Michal Hockocd04ae12017-09-06 16:24:50 -07003879 /*
3880 * !MMU doesn't have oom reaper so give access to memory reserves
3881 * only to the thread with TIF_MEMDIE set
3882 */
3883 if (!IS_ENABLED(CONFIG_MMU) && !test_thread_flag(TIF_MEMDIE))
3884 return false;
Vlastimil Babka31a6c192016-07-28 15:49:13 -07003885
Michal Hockocd04ae12017-09-06 16:24:50 -07003886 return true;
3887}
3888
3889/*
3890 * Distinguish requests which really need access to full memory
3891 * reserves from oom victims which can live with a portion of it
3892 */
3893static inline int __gfp_pfmemalloc_flags(gfp_t gfp_mask)
3894{
3895 if (unlikely(gfp_mask & __GFP_NOMEMALLOC))
3896 return 0;
3897 if (gfp_mask & __GFP_MEMALLOC)
3898 return ALLOC_NO_WATERMARKS;
3899 if (in_serving_softirq() && (current->flags & PF_MEMALLOC))
3900 return ALLOC_NO_WATERMARKS;
3901 if (!in_interrupt()) {
3902 if (current->flags & PF_MEMALLOC)
3903 return ALLOC_NO_WATERMARKS;
3904 else if (oom_reserves_allowed(current))
3905 return ALLOC_OOM;
3906 }
3907
3908 return 0;
3909}
3910
3911bool gfp_pfmemalloc_allowed(gfp_t gfp_mask)
3912{
3913 return !!__gfp_pfmemalloc_flags(gfp_mask);
Mel Gorman072bb0a2012-07-31 16:43:58 -07003914}
3915
Michal Hocko0a0337e2016-05-20 16:57:00 -07003916/*
Michal Hocko0a0337e2016-05-20 16:57:00 -07003917 * Checks whether it makes sense to retry the reclaim to make a forward progress
3918 * for the given allocation request.
Johannes Weiner491d79a2017-05-03 14:52:16 -07003919 *
3920 * We give up when we either have tried MAX_RECLAIM_RETRIES in a row
3921 * without success, or when we couldn't even meet the watermark if we
3922 * reclaimed all remaining pages on the LRU lists.
Michal Hocko0a0337e2016-05-20 16:57:00 -07003923 *
3924 * Returns true if a retry is viable or false to enter the oom path.
3925 */
3926static inline bool
3927should_reclaim_retry(gfp_t gfp_mask, unsigned order,
3928 struct alloc_context *ac, int alloc_flags,
Vlastimil Babka423b4522016-10-07 17:00:40 -07003929 bool did_some_progress, int *no_progress_loops)
Michal Hocko0a0337e2016-05-20 16:57:00 -07003930{
3931 struct zone *zone;
3932 struct zoneref *z;
Michal Hocko15f570b2018-10-26 15:03:31 -07003933 bool ret = false;
Michal Hocko0a0337e2016-05-20 16:57:00 -07003934
3935 /*
Vlastimil Babka423b4522016-10-07 17:00:40 -07003936 * Costly allocations might have made a progress but this doesn't mean
3937 * their order will become available due to high fragmentation so
3938 * always increment the no progress counter for them
3939 */
3940 if (did_some_progress && order <= PAGE_ALLOC_COSTLY_ORDER)
3941 *no_progress_loops = 0;
3942 else
3943 (*no_progress_loops)++;
3944
3945 /*
Michal Hocko0a0337e2016-05-20 16:57:00 -07003946 * Make sure we converge to OOM if we cannot make any progress
3947 * several times in the row.
3948 */
Minchan Kim04c87162016-12-12 16:42:11 -08003949 if (*no_progress_loops > MAX_RECLAIM_RETRIES) {
3950 /* Before OOM, exhaust highatomic_reserve */
Minchan Kim29fac032016-12-12 16:42:14 -08003951 return unreserve_highatomic_pageblock(ac, true);
Minchan Kim04c87162016-12-12 16:42:11 -08003952 }
Michal Hocko0a0337e2016-05-20 16:57:00 -07003953
Michal Hocko0a0337e2016-05-20 16:57:00 -07003954 /*
Mel Gormanbca67592016-07-28 15:47:05 -07003955 * Keep reclaiming pages while there is a chance this will lead
3956 * somewhere. If none of the target zones can satisfy our allocation
3957 * request even if all reclaimable pages are considered then we are
3958 * screwed and have to go OOM.
Michal Hocko0a0337e2016-05-20 16:57:00 -07003959 */
3960 for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
3961 ac->nodemask) {
3962 unsigned long available;
Michal Hockoede37712016-05-20 16:57:03 -07003963 unsigned long reclaimable;
Michal Hockod379f012017-02-22 15:42:00 -08003964 unsigned long min_wmark = min_wmark_pages(zone);
3965 bool wmark;
Michal Hocko0a0337e2016-05-20 16:57:00 -07003966
Mel Gorman5a1c84b2016-07-28 15:47:31 -07003967 available = reclaimable = zone_reclaimable_pages(zone);
Mel Gorman5a1c84b2016-07-28 15:47:31 -07003968 available += zone_page_state_snapshot(zone, NR_FREE_PAGES);
Michal Hocko0a0337e2016-05-20 16:57:00 -07003969
3970 /*
Johannes Weiner491d79a2017-05-03 14:52:16 -07003971 * Would the allocation succeed if we reclaimed all
3972 * reclaimable pages?
Michal Hocko0a0337e2016-05-20 16:57:00 -07003973 */
Michal Hockod379f012017-02-22 15:42:00 -08003974 wmark = __zone_watermark_ok(zone, order, min_wmark,
3975 ac_classzone_idx(ac), alloc_flags, available);
3976 trace_reclaim_retry_zone(z, order, reclaimable,
3977 available, min_wmark, *no_progress_loops, wmark);
3978 if (wmark) {
Michal Hockoede37712016-05-20 16:57:03 -07003979 /*
3980 * If we didn't make any progress and have a lot of
3981 * dirty + writeback pages then we should wait for
3982 * an IO to complete to slow down the reclaim and
3983 * prevent from pre mature OOM
3984 */
3985 if (!did_some_progress) {
Mel Gorman11fb9982016-07-28 15:46:20 -07003986 unsigned long write_pending;
Michal Hockoede37712016-05-20 16:57:03 -07003987
Mel Gorman5a1c84b2016-07-28 15:47:31 -07003988 write_pending = zone_page_state_snapshot(zone,
3989 NR_ZONE_WRITE_PENDING);
Michal Hockoede37712016-05-20 16:57:03 -07003990
Mel Gorman11fb9982016-07-28 15:46:20 -07003991 if (2 * write_pending > reclaimable) {
Michal Hockoede37712016-05-20 16:57:03 -07003992 congestion_wait(BLK_RW_ASYNC, HZ/10);
3993 return true;
3994 }
3995 }
Mel Gorman5a1c84b2016-07-28 15:47:31 -07003996
Michal Hocko15f570b2018-10-26 15:03:31 -07003997 ret = true;
3998 goto out;
Michal Hocko0a0337e2016-05-20 16:57:00 -07003999 }
4000 }
4001
Michal Hocko15f570b2018-10-26 15:03:31 -07004002out:
4003 /*
4004 * Memory allocation/reclaim might be called from a WQ context and the
4005 * current implementation of the WQ concurrency control doesn't
4006 * recognize that a particular WQ is congested if the worker thread is
4007 * looping without ever sleeping. Therefore we have to do a short sleep
4008 * here rather than calling cond_resched().
4009 */
4010 if (current->flags & PF_WQ_WORKER)
4011 schedule_timeout_uninterruptible(1);
4012 else
4013 cond_resched();
4014 return ret;
Michal Hocko0a0337e2016-05-20 16:57:00 -07004015}
4016
Vlastimil Babka902b6282017-07-06 15:39:56 -07004017static inline bool
4018check_retry_cpuset(int cpuset_mems_cookie, struct alloc_context *ac)
4019{
4020 /*
4021 * It's possible that cpuset's mems_allowed and the nodemask from
4022 * mempolicy don't intersect. This should be normally dealt with by
4023 * policy_nodemask(), but it's possible to race with cpuset update in
4024 * such a way the check therein was true, and then it became false
4025 * before we got our cpuset_mems_cookie here.
4026 * This assumes that for all allocations, ac->nodemask can come only
4027 * from MPOL_BIND mempolicy (whose documented semantics is to be ignored
4028 * when it does not intersect with the cpuset restrictions) or the
4029 * caller can deal with a violated nodemask.
4030 */
4031 if (cpusets_enabled() && ac->nodemask &&
4032 !cpuset_nodemask_valid_mems_allowed(ac->nodemask)) {
4033 ac->nodemask = NULL;
4034 return true;
4035 }
4036
4037 /*
4038 * When updating a task's mems_allowed or mempolicy nodemask, it is
4039 * possible to race with parallel threads in such a way that our
4040 * allocation can fail while the mask is being updated. If we are about
4041 * to fail, check if the cpuset changed during allocation and if so,
4042 * retry.
4043 */
4044 if (read_mems_allowed_retry(cpuset_mems_cookie))
4045 return true;
4046
4047 return false;
4048}
4049
Mel Gorman11e33f62009-06-16 15:31:57 -07004050static inline struct page *
4051__alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
Vlastimil Babkaa9263752015-02-11 15:25:41 -08004052 struct alloc_context *ac)
Mel Gorman11e33f62009-06-16 15:31:57 -07004053{
Mel Gormand0164ad2015-11-06 16:28:21 -08004054 bool can_direct_reclaim = gfp_mask & __GFP_DIRECT_RECLAIM;
Vlastimil Babka282722b2017-05-08 15:54:49 -07004055 const bool costly_order = order > PAGE_ALLOC_COSTLY_ORDER;
Mel Gorman11e33f62009-06-16 15:31:57 -07004056 struct page *page = NULL;
Mel Gormanc6038442016-05-19 17:13:38 -07004057 unsigned int alloc_flags;
Mel Gorman11e33f62009-06-16 15:31:57 -07004058 unsigned long did_some_progress;
Vlastimil Babka5ce9bfe2017-01-24 15:18:38 -08004059 enum compact_priority compact_priority;
Michal Hockoc5d01d02016-05-20 16:56:53 -07004060 enum compact_result compact_result;
Vlastimil Babka5ce9bfe2017-01-24 15:18:38 -08004061 int compaction_retries;
4062 int no_progress_loops;
Vlastimil Babka5ce9bfe2017-01-24 15:18:38 -08004063 unsigned int cpuset_mems_cookie;
Michal Hockocd04ae12017-09-06 16:24:50 -07004064 int reserve_flags;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08004065
Christoph Lameter952f3b52006-12-06 20:33:26 -08004066 /*
Mel Gormand0164ad2015-11-06 16:28:21 -08004067 * We also sanity check to catch abuse of atomic reserves being used by
4068 * callers that are not in atomic context.
4069 */
4070 if (WARN_ON_ONCE((gfp_mask & (__GFP_ATOMIC|__GFP_DIRECT_RECLAIM)) ==
4071 (__GFP_ATOMIC|__GFP_DIRECT_RECLAIM)))
4072 gfp_mask &= ~__GFP_ATOMIC;
4073
Vlastimil Babka5ce9bfe2017-01-24 15:18:38 -08004074retry_cpuset:
4075 compaction_retries = 0;
4076 no_progress_loops = 0;
4077 compact_priority = DEF_COMPACT_PRIORITY;
4078 cpuset_mems_cookie = read_mems_allowed_begin();
Michal Hocko9a67f642017-02-22 15:46:19 -08004079
4080 /*
4081 * The fast path uses conservative alloc_flags to succeed only until
4082 * kswapd needs to be woken up, and to avoid the cost of setting up
4083 * alloc_flags precisely. So we do that now.
4084 */
4085 alloc_flags = gfp_to_alloc_flags(gfp_mask);
4086
Vlastimil Babkae47483b2017-01-24 15:18:41 -08004087 /*
4088 * We need to recalculate the starting point for the zonelist iterator
4089 * because we might have used different nodemask in the fast path, or
4090 * there was a cpuset modification and we are retrying - otherwise we
4091 * could end up iterating over non-eligible zones endlessly.
4092 */
4093 ac->preferred_zoneref = first_zones_zonelist(ac->zonelist,
4094 ac->high_zoneidx, ac->nodemask);
4095 if (!ac->preferred_zoneref->zone)
4096 goto nopage;
4097
Mel Gormand0164ad2015-11-06 16:28:21 -08004098 if (gfp_mask & __GFP_KSWAPD_RECLAIM)
David Rientjes5ecd9d42018-04-05 16:25:16 -07004099 wake_all_kswapds(order, gfp_mask, ac);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08004100
Paul Jackson9bf22292005-09-06 15:18:12 -07004101 /*
Vlastimil Babka23771232016-07-28 15:49:16 -07004102 * The adjusted alloc_flags might result in immediate success, so try
4103 * that first
4104 */
4105 page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac);
4106 if (page)
4107 goto got_pg;
4108
Vlastimil Babkaa8161d12016-07-28 15:49:19 -07004109 /*
4110 * For costly allocations, try direct compaction first, as it's likely
Vlastimil Babka282722b2017-05-08 15:54:49 -07004111 * that we have enough base pages and don't need to reclaim. For non-
4112 * movable high-order allocations, do that as well, as compaction will
4113 * try prevent permanent fragmentation by migrating from blocks of the
4114 * same migratetype.
4115 * Don't try this for allocations that are allowed to ignore
4116 * watermarks, as the ALLOC_NO_WATERMARKS attempt didn't yet happen.
Vlastimil Babkaa8161d12016-07-28 15:49:19 -07004117 */
Vlastimil Babka282722b2017-05-08 15:54:49 -07004118 if (can_direct_reclaim &&
4119 (costly_order ||
4120 (order > 0 && ac->migratetype != MIGRATE_MOVABLE))
4121 && !gfp_pfmemalloc_allowed(gfp_mask)) {
Vlastimil Babkaa8161d12016-07-28 15:49:19 -07004122 page = __alloc_pages_direct_compact(gfp_mask, order,
4123 alloc_flags, ac,
Vlastimil Babkaa5508cd2016-07-28 15:49:28 -07004124 INIT_COMPACT_PRIORITY,
Vlastimil Babkaa8161d12016-07-28 15:49:19 -07004125 &compact_result);
4126 if (page)
4127 goto got_pg;
4128
Vlastimil Babka3eb27712016-07-28 15:49:22 -07004129 /*
4130 * Checks for costly allocations with __GFP_NORETRY, which
4131 * includes THP page fault allocations
4132 */
Vlastimil Babka282722b2017-05-08 15:54:49 -07004133 if (costly_order && (gfp_mask & __GFP_NORETRY)) {
Vlastimil Babkaa8161d12016-07-28 15:49:19 -07004134 /*
4135 * If compaction is deferred for high-order allocations,
4136 * it is because sync compaction recently failed. If
4137 * this is the case and the caller requested a THP
4138 * allocation, we do not want to heavily disrupt the
4139 * system, so we fail the allocation instead of entering
4140 * direct reclaim.
4141 */
4142 if (compact_result == COMPACT_DEFERRED)
4143 goto nopage;
4144
4145 /*
Vlastimil Babka3eb27712016-07-28 15:49:22 -07004146 * Looks like reclaim/compaction is worth trying, but
4147 * sync compaction could be very expensive, so keep
Vlastimil Babka25160352016-07-28 15:49:25 -07004148 * using async compaction.
Vlastimil Babkaa8161d12016-07-28 15:49:19 -07004149 */
Vlastimil Babkaa5508cd2016-07-28 15:49:28 -07004150 compact_priority = INIT_COMPACT_PRIORITY;
Vlastimil Babkaa8161d12016-07-28 15:49:19 -07004151 }
4152 }
Vlastimil Babka23771232016-07-28 15:49:16 -07004153
4154retry:
4155 /* Ensure kswapd doesn't accidentally go to sleep as long as we loop */
4156 if (gfp_mask & __GFP_KSWAPD_RECLAIM)
David Rientjes5ecd9d42018-04-05 16:25:16 -07004157 wake_all_kswapds(order, gfp_mask, ac);
Vlastimil Babka23771232016-07-28 15:49:16 -07004158
Michal Hockocd04ae12017-09-06 16:24:50 -07004159 reserve_flags = __gfp_pfmemalloc_flags(gfp_mask);
4160 if (reserve_flags)
4161 alloc_flags = reserve_flags;
Vlastimil Babka23771232016-07-28 15:49:16 -07004162
4163 /*
Vlastimil Babkad6a24df2018-08-17 15:45:05 -07004164 * Reset the nodemask and zonelist iterators if memory policies can be
4165 * ignored. These allocations are high priority and system rather than
4166 * user oriented.
Mel Gormane46e7b72016-06-03 14:56:01 -07004167 */
Michal Hockocd04ae12017-09-06 16:24:50 -07004168 if (!(alloc_flags & ALLOC_CPUSET) || reserve_flags) {
Vlastimil Babkad6a24df2018-08-17 15:45:05 -07004169 ac->nodemask = NULL;
Mel Gormane46e7b72016-06-03 14:56:01 -07004170 ac->preferred_zoneref = first_zones_zonelist(ac->zonelist,
4171 ac->high_zoneidx, ac->nodemask);
4172 }
4173
Vlastimil Babka23771232016-07-28 15:49:16 -07004174 /* Attempt with potentially adjusted zonelist and alloc_flags */
Vlastimil Babka31a6c192016-07-28 15:49:13 -07004175 page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08004176 if (page)
4177 goto got_pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178
Mel Gormand0164ad2015-11-06 16:28:21 -08004179 /* Caller is not willing to reclaim, we can't balance anything */
Michal Hocko9a67f642017-02-22 15:46:19 -08004180 if (!can_direct_reclaim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 goto nopage;
Michal Hocko9a67f642017-02-22 15:46:19 -08004182
Peter Zijlstra341ce062009-06-16 15:32:02 -07004183 /* Avoid recursion of direct reclaim */
Michal Hocko9a67f642017-02-22 15:46:19 -08004184 if (current->flags & PF_MEMALLOC)
Peter Zijlstra341ce062009-06-16 15:32:02 -07004185 goto nopage;
David Rientjes8fe78042014-08-06 16:07:54 -07004186
Mel Gorman11e33f62009-06-16 15:31:57 -07004187 /* Try direct reclaim and then allocating */
Vlastimil Babkaa9263752015-02-11 15:25:41 -08004188 page = __alloc_pages_direct_reclaim(gfp_mask, order, alloc_flags, ac,
4189 &did_some_progress);
Mel Gorman11e33f62009-06-16 15:31:57 -07004190 if (page)
4191 goto got_pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192
Vlastimil Babkaa8161d12016-07-28 15:49:19 -07004193 /* Try direct compaction and then allocating */
4194 page = __alloc_pages_direct_compact(gfp_mask, order, alloc_flags, ac,
Vlastimil Babkaa5508cd2016-07-28 15:49:28 -07004195 compact_priority, &compact_result);
Vlastimil Babkaa8161d12016-07-28 15:49:19 -07004196 if (page)
4197 goto got_pg;
4198
Johannes Weiner90839052015-06-24 16:57:21 -07004199 /* Do not loop if specifically requested */
4200 if (gfp_mask & __GFP_NORETRY)
Vlastimil Babkaa8161d12016-07-28 15:49:19 -07004201 goto nopage;
Johannes Weiner90839052015-06-24 16:57:21 -07004202
Michal Hocko0a0337e2016-05-20 16:57:00 -07004203 /*
4204 * Do not retry costly high order allocations unless they are
Michal Hockodcda9b02017-07-12 14:36:45 -07004205 * __GFP_RETRY_MAYFAIL
Michal Hocko0a0337e2016-05-20 16:57:00 -07004206 */
Michal Hockodcda9b02017-07-12 14:36:45 -07004207 if (costly_order && !(gfp_mask & __GFP_RETRY_MAYFAIL))
Vlastimil Babkaa8161d12016-07-28 15:49:19 -07004208 goto nopage;
Michal Hocko0a0337e2016-05-20 16:57:00 -07004209
Michal Hocko0a0337e2016-05-20 16:57:00 -07004210 if (should_reclaim_retry(gfp_mask, order, ac, alloc_flags,
Vlastimil Babka423b4522016-10-07 17:00:40 -07004211 did_some_progress > 0, &no_progress_loops))
Michal Hocko0a0337e2016-05-20 16:57:00 -07004212 goto retry;
4213
Michal Hocko33c2d212016-05-20 16:57:06 -07004214 /*
4215 * It doesn't make any sense to retry for the compaction if the order-0
4216 * reclaim is not able to make any progress because the current
4217 * implementation of the compaction depends on the sufficient amount
4218 * of free memory (see __compaction_suitable)
4219 */
4220 if (did_some_progress > 0 &&
Michal Hocko86a294a2016-05-20 16:57:12 -07004221 should_compact_retry(ac, order, alloc_flags,
Vlastimil Babkaa5508cd2016-07-28 15:49:28 -07004222 compact_result, &compact_priority,
Vlastimil Babkad9436492016-10-07 17:00:31 -07004223 &compaction_retries))
Michal Hocko33c2d212016-05-20 16:57:06 -07004224 goto retry;
4225
Vlastimil Babka902b6282017-07-06 15:39:56 -07004226
4227 /* Deal with possible cpuset update races before we start OOM killing */
4228 if (check_retry_cpuset(cpuset_mems_cookie, ac))
Vlastimil Babkae47483b2017-01-24 15:18:41 -08004229 goto retry_cpuset;
4230
Johannes Weiner90839052015-06-24 16:57:21 -07004231 /* Reclaim has failed us, start killing things */
4232 page = __alloc_pages_may_oom(gfp_mask, order, ac, &did_some_progress);
4233 if (page)
4234 goto got_pg;
4235
Michal Hocko9a67f642017-02-22 15:46:19 -08004236 /* Avoid allocations with no watermarks from looping endlessly */
Michal Hockocd04ae12017-09-06 16:24:50 -07004237 if (tsk_is_oom_victim(current) &&
4238 (alloc_flags == ALLOC_OOM ||
Tetsuo Handac2889832017-06-02 14:46:31 -07004239 (gfp_mask & __GFP_NOMEMALLOC)))
Michal Hocko9a67f642017-02-22 15:46:19 -08004240 goto nopage;
4241
Johannes Weiner90839052015-06-24 16:57:21 -07004242 /* Retry as long as the OOM killer is making progress */
Michal Hocko0a0337e2016-05-20 16:57:00 -07004243 if (did_some_progress) {
4244 no_progress_loops = 0;
Johannes Weiner90839052015-06-24 16:57:21 -07004245 goto retry;
Michal Hocko0a0337e2016-05-20 16:57:00 -07004246 }
Johannes Weiner90839052015-06-24 16:57:21 -07004247
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248nopage:
Vlastimil Babka902b6282017-07-06 15:39:56 -07004249 /* Deal with possible cpuset update races before we fail */
4250 if (check_retry_cpuset(cpuset_mems_cookie, ac))
Vlastimil Babka5ce9bfe2017-01-24 15:18:38 -08004251 goto retry_cpuset;
4252
Michal Hocko9a67f642017-02-22 15:46:19 -08004253 /*
4254 * Make sure that __GFP_NOFAIL request doesn't leak out and make sure
4255 * we always retry
4256 */
4257 if (gfp_mask & __GFP_NOFAIL) {
4258 /*
4259 * All existing users of the __GFP_NOFAIL are blockable, so warn
4260 * of any new users that actually require GFP_NOWAIT
4261 */
4262 if (WARN_ON_ONCE(!can_direct_reclaim))
4263 goto fail;
4264
4265 /*
4266 * PF_MEMALLOC request from this context is rather bizarre
4267 * because we cannot reclaim anything and only can loop waiting
4268 * for somebody to do a work for us
4269 */
4270 WARN_ON_ONCE(current->flags & PF_MEMALLOC);
4271
4272 /*
4273 * non failing costly orders are a hard requirement which we
4274 * are not prepared for much so let's warn about these users
4275 * so that we can identify them and convert them to something
4276 * else.
4277 */
4278 WARN_ON_ONCE(order > PAGE_ALLOC_COSTLY_ORDER);
4279
Michal Hocko6c18ba72017-02-22 15:46:25 -08004280 /*
4281 * Help non-failing allocations by giving them access to memory
4282 * reserves but do not use ALLOC_NO_WATERMARKS because this
4283 * could deplete whole memory reserves which would just make
4284 * the situation worse
4285 */
4286 page = __alloc_pages_cpuset_fallback(gfp_mask, order, ALLOC_HARDER, ac);
4287 if (page)
4288 goto got_pg;
4289
Michal Hocko9a67f642017-02-22 15:46:19 -08004290 cond_resched();
4291 goto retry;
4292 }
4293fail:
Michal Hockoa8e99252017-02-22 15:46:10 -08004294 warn_alloc(gfp_mask, ac->nodemask,
Michal Hocko7877cdc2016-10-07 17:01:55 -07004295 "page allocation failure: order:%u", order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296got_pg:
Mel Gorman072bb0a2012-07-31 16:43:58 -07004297 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298}
Mel Gorman11e33f62009-06-16 15:31:57 -07004299
Mel Gorman9cd75552017-02-24 14:56:29 -08004300static inline bool prepare_alloc_pages(gfp_t gfp_mask, unsigned int order,
Vlastimil Babka04ec6262017-07-06 15:40:03 -07004301 int preferred_nid, nodemask_t *nodemask,
Mel Gorman9cd75552017-02-24 14:56:29 -08004302 struct alloc_context *ac, gfp_t *alloc_mask,
4303 unsigned int *alloc_flags)
4304{
4305 ac->high_zoneidx = gfp_zone(gfp_mask);
Vlastimil Babka04ec6262017-07-06 15:40:03 -07004306 ac->zonelist = node_zonelist(preferred_nid, gfp_mask);
Mel Gorman9cd75552017-02-24 14:56:29 -08004307 ac->nodemask = nodemask;
4308 ac->migratetype = gfpflags_to_migratetype(gfp_mask);
4309
4310 if (cpusets_enabled()) {
4311 *alloc_mask |= __GFP_HARDWALL;
Mel Gorman9cd75552017-02-24 14:56:29 -08004312 if (!ac->nodemask)
4313 ac->nodemask = &cpuset_current_mems_allowed;
Vlastimil Babka51047822017-02-24 14:56:53 -08004314 else
4315 *alloc_flags |= ALLOC_CPUSET;
Mel Gorman9cd75552017-02-24 14:56:29 -08004316 }
4317
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +01004318 fs_reclaim_acquire(gfp_mask);
4319 fs_reclaim_release(gfp_mask);
Mel Gorman9cd75552017-02-24 14:56:29 -08004320
4321 might_sleep_if(gfp_mask & __GFP_DIRECT_RECLAIM);
4322
4323 if (should_fail_alloc_page(gfp_mask, order))
4324 return false;
4325
Joonsoo Kimd883c6c2018-05-23 10:18:21 +09004326 if (IS_ENABLED(CONFIG_CMA) && ac->migratetype == MIGRATE_MOVABLE)
4327 *alloc_flags |= ALLOC_CMA;
4328
Mel Gorman9cd75552017-02-24 14:56:29 -08004329 return true;
4330}
4331
4332/* Determine whether to spread dirty pages and what the first usable zone */
Huaisheng Yea380b402018-06-07 17:07:57 -07004333static inline void finalise_ac(gfp_t gfp_mask, struct alloc_context *ac)
Mel Gorman9cd75552017-02-24 14:56:29 -08004334{
4335 /* Dirty zone balancing only done in the fast path */
4336 ac->spread_dirty_pages = (gfp_mask & __GFP_WRITE);
4337
4338 /*
4339 * The preferred zone is used for statistics but crucially it is
4340 * also used as the starting point for the zonelist iterator. It
4341 * may get reset for allocations that ignore memory policies.
4342 */
4343 ac->preferred_zoneref = first_zones_zonelist(ac->zonelist,
4344 ac->high_zoneidx, ac->nodemask);
4345}
4346
Mel Gorman11e33f62009-06-16 15:31:57 -07004347/*
4348 * This is the 'heart' of the zoned buddy allocator.
4349 */
4350struct page *
Vlastimil Babka04ec6262017-07-06 15:40:03 -07004351__alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order, int preferred_nid,
4352 nodemask_t *nodemask)
Mel Gorman11e33f62009-06-16 15:31:57 -07004353{
Mel Gorman5bb1b162016-05-19 17:13:50 -07004354 struct page *page;
Mel Gormane6cbd7f2016-07-28 15:46:50 -07004355 unsigned int alloc_flags = ALLOC_WMARK_LOW;
Tetsuo Handaf19360f2017-09-08 16:13:22 -07004356 gfp_t alloc_mask; /* The gfp_t that was actually used for allocation */
Mel Gorman9cd75552017-02-24 14:56:29 -08004357 struct alloc_context ac = { };
Mel Gorman682a3382016-05-19 17:13:30 -07004358
Michal Hockoc63ae432018-11-16 15:08:53 -08004359 /*
4360 * There are several places where we assume that the order value is sane
4361 * so bail out early if the request is out of bound.
4362 */
4363 if (unlikely(order >= MAX_ORDER)) {
4364 WARN_ON_ONCE(!(gfp_mask & __GFP_NOWARN));
4365 return NULL;
4366 }
4367
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +10004368 gfp_mask &= gfp_allowed_mask;
Tetsuo Handaf19360f2017-09-08 16:13:22 -07004369 alloc_mask = gfp_mask;
Vlastimil Babka04ec6262017-07-06 15:40:03 -07004370 if (!prepare_alloc_pages(gfp_mask, order, preferred_nid, nodemask, &ac, &alloc_mask, &alloc_flags))
Mel Gorman11e33f62009-06-16 15:31:57 -07004371 return NULL;
4372
Huaisheng Yea380b402018-06-07 17:07:57 -07004373 finalise_ac(gfp_mask, &ac);
Mel Gorman5bb1b162016-05-19 17:13:50 -07004374
Mel Gorman5117f452009-06-16 15:31:59 -07004375 /* First allocation attempt */
Vlastimil Babkaa9263752015-02-11 15:25:41 -08004376 page = get_page_from_freelist(alloc_mask, order, alloc_flags, &ac);
Mel Gorman4fcb0972016-05-19 17:14:01 -07004377 if (likely(page))
4378 goto out;
Andrew Morton91fbdc02015-02-11 15:25:04 -08004379
Mel Gorman4fcb0972016-05-19 17:14:01 -07004380 /*
Michal Hocko7dea19f2017-05-03 14:53:15 -07004381 * Apply scoped allocation constraints. This is mainly about GFP_NOFS
4382 * resp. GFP_NOIO which has to be inherited for all allocation requests
4383 * from a particular context which has been marked by
4384 * memalloc_no{fs,io}_{save,restore}.
Mel Gorman4fcb0972016-05-19 17:14:01 -07004385 */
Michal Hocko7dea19f2017-05-03 14:53:15 -07004386 alloc_mask = current_gfp_context(gfp_mask);
Mel Gorman4fcb0972016-05-19 17:14:01 -07004387 ac.spread_dirty_pages = false;
Mel Gorman11e33f62009-06-16 15:31:57 -07004388
Mel Gorman47415262016-05-19 17:14:44 -07004389 /*
4390 * Restore the original nodemask if it was potentially replaced with
4391 * &cpuset_current_mems_allowed to optimize the fast-path attempt.
4392 */
Vlastimil Babkae47483b2017-01-24 15:18:41 -08004393 if (unlikely(ac.nodemask != nodemask))
Mel Gorman47415262016-05-19 17:14:44 -07004394 ac.nodemask = nodemask;
Vlastimil Babka16096c22017-01-24 15:18:35 -08004395
Mel Gorman4fcb0972016-05-19 17:14:01 -07004396 page = __alloc_pages_slowpath(alloc_mask, order, &ac);
Xishi Qiu23f086f2015-02-11 15:25:07 -08004397
Mel Gorman4fcb0972016-05-19 17:14:01 -07004398out:
Vladimir Davydovc4159a72016-08-08 23:03:12 +03004399 if (memcg_kmem_enabled() && (gfp_mask & __GFP_ACCOUNT) && page &&
4400 unlikely(memcg_kmem_charge(page, gfp_mask, order) != 0)) {
4401 __free_pages(page, order);
4402 page = NULL;
Vladimir Davydov49491482016-07-26 15:24:24 -07004403 }
4404
Mel Gorman4fcb0972016-05-19 17:14:01 -07004405 trace_mm_page_alloc(page, order, alloc_mask, ac.migratetype);
4406
Mel Gorman11e33f62009-06-16 15:31:57 -07004407 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408}
Mel Gormand2391712009-06-16 15:31:52 -07004409EXPORT_SYMBOL(__alloc_pages_nodemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410
4411/*
Michal Hocko9ea9a682018-08-17 15:46:01 -07004412 * Common helper functions. Never use with __GFP_HIGHMEM because the returned
4413 * address cannot represent highmem pages. Use alloc_pages and then kmap if
4414 * you need to access high mem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 */
Harvey Harrison920c7a52008-02-04 22:29:26 -08004416unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417{
Akinobu Mita945a1112009-09-21 17:01:47 -07004418 struct page *page;
4419
Michal Hocko9ea9a682018-08-17 15:46:01 -07004420 page = alloc_pages(gfp_mask & ~__GFP_HIGHMEM, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 if (!page)
4422 return 0;
4423 return (unsigned long) page_address(page);
4424}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425EXPORT_SYMBOL(__get_free_pages);
4426
Harvey Harrison920c7a52008-02-04 22:29:26 -08004427unsigned long get_zeroed_page(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428{
Akinobu Mita945a1112009-09-21 17:01:47 -07004429 return __get_free_pages(gfp_mask | __GFP_ZERO, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431EXPORT_SYMBOL(get_zeroed_page);
4432
Harvey Harrison920c7a52008-02-04 22:29:26 -08004433void __free_pages(struct page *page, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434{
Nick Pigginb5810032005-10-29 18:16:12 -07004435 if (put_page_testzero(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 if (order == 0)
Mel Gorman2d4894b2017-11-15 17:37:59 -08004437 free_unref_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 else
4439 __free_pages_ok(page, order);
4440 }
4441}
4442
4443EXPORT_SYMBOL(__free_pages);
4444
Harvey Harrison920c7a52008-02-04 22:29:26 -08004445void free_pages(unsigned long addr, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446{
4447 if (addr != 0) {
Nick Piggin725d7042006-09-25 23:30:55 -07004448 VM_BUG_ON(!virt_addr_valid((void *)addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 __free_pages(virt_to_page((void *)addr), order);
4450 }
4451}
4452
4453EXPORT_SYMBOL(free_pages);
4454
Glauber Costa6a1a0d32012-12-18 14:22:00 -08004455/*
Alexander Duyckb63ae8c2015-05-06 21:11:57 -07004456 * Page Fragment:
4457 * An arbitrary-length arbitrary-offset area of memory which resides
4458 * within a 0 or higher order page. Multiple fragments within that page
4459 * are individually refcounted, in the page's reference counter.
4460 *
4461 * The page_frag functions below provide a simple allocation framework for
4462 * page fragments. This is used by the network stack and network device
4463 * drivers to provide a backing region of memory for use as either an
4464 * sk_buff->head, or to be used in the "frags" portion of skb_shared_info.
4465 */
Alexander Duyck2976db82017-01-10 16:58:09 -08004466static struct page *__page_frag_cache_refill(struct page_frag_cache *nc,
4467 gfp_t gfp_mask)
Alexander Duyckb63ae8c2015-05-06 21:11:57 -07004468{
4469 struct page *page = NULL;
4470 gfp_t gfp = gfp_mask;
4471
4472#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
4473 gfp_mask |= __GFP_COMP | __GFP_NOWARN | __GFP_NORETRY |
4474 __GFP_NOMEMALLOC;
4475 page = alloc_pages_node(NUMA_NO_NODE, gfp_mask,
4476 PAGE_FRAG_CACHE_MAX_ORDER);
4477 nc->size = page ? PAGE_FRAG_CACHE_MAX_SIZE : PAGE_SIZE;
4478#endif
4479 if (unlikely(!page))
4480 page = alloc_pages_node(NUMA_NO_NODE, gfp, 0);
4481
4482 nc->va = page ? page_address(page) : NULL;
4483
4484 return page;
4485}
4486
Alexander Duyck2976db82017-01-10 16:58:09 -08004487void __page_frag_cache_drain(struct page *page, unsigned int count)
Alexander Duyck44fdffd2016-12-14 15:05:26 -08004488{
4489 VM_BUG_ON_PAGE(page_ref_count(page) == 0, page);
4490
4491 if (page_ref_sub_and_test(page, count)) {
Alexander Duyck2976db82017-01-10 16:58:09 -08004492 unsigned int order = compound_order(page);
4493
Alexander Duyck44fdffd2016-12-14 15:05:26 -08004494 if (order == 0)
Mel Gorman2d4894b2017-11-15 17:37:59 -08004495 free_unref_page(page);
Alexander Duyck44fdffd2016-12-14 15:05:26 -08004496 else
4497 __free_pages_ok(page, order);
4498 }
4499}
Alexander Duyck2976db82017-01-10 16:58:09 -08004500EXPORT_SYMBOL(__page_frag_cache_drain);
Alexander Duyck44fdffd2016-12-14 15:05:26 -08004501
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -08004502void *page_frag_alloc(struct page_frag_cache *nc,
4503 unsigned int fragsz, gfp_t gfp_mask)
Alexander Duyckb63ae8c2015-05-06 21:11:57 -07004504{
4505 unsigned int size = PAGE_SIZE;
4506 struct page *page;
4507 int offset;
4508
4509 if (unlikely(!nc->va)) {
4510refill:
Alexander Duyck2976db82017-01-10 16:58:09 -08004511 page = __page_frag_cache_refill(nc, gfp_mask);
Alexander Duyckb63ae8c2015-05-06 21:11:57 -07004512 if (!page)
4513 return NULL;
4514
4515#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
4516 /* if size can vary use size else just use PAGE_SIZE */
4517 size = nc->size;
4518#endif
4519 /* Even if we own the page, we do not use atomic_set().
4520 * This would break get_page_unless_zero() users.
4521 */
Joonsoo Kimfe896d12016-03-17 14:19:26 -07004522 page_ref_add(page, size - 1);
Alexander Duyckb63ae8c2015-05-06 21:11:57 -07004523
4524 /* reset page count bias and offset to start of new frag */
Michal Hocko2f064f32015-08-21 14:11:51 -07004525 nc->pfmemalloc = page_is_pfmemalloc(page);
Alexander Duyckb63ae8c2015-05-06 21:11:57 -07004526 nc->pagecnt_bias = size;
4527 nc->offset = size;
4528 }
4529
4530 offset = nc->offset - fragsz;
4531 if (unlikely(offset < 0)) {
4532 page = virt_to_page(nc->va);
4533
Joonsoo Kimfe896d12016-03-17 14:19:26 -07004534 if (!page_ref_sub_and_test(page, nc->pagecnt_bias))
Alexander Duyckb63ae8c2015-05-06 21:11:57 -07004535 goto refill;
4536
4537#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
4538 /* if size can vary use size else just use PAGE_SIZE */
4539 size = nc->size;
4540#endif
4541 /* OK, page count is 0, we can safely set it */
Joonsoo Kimfe896d12016-03-17 14:19:26 -07004542 set_page_count(page, size);
Alexander Duyckb63ae8c2015-05-06 21:11:57 -07004543
4544 /* reset page count bias and offset to start of new frag */
4545 nc->pagecnt_bias = size;
4546 offset = size - fragsz;
4547 }
4548
4549 nc->pagecnt_bias--;
4550 nc->offset = offset;
4551
4552 return nc->va + offset;
4553}
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -08004554EXPORT_SYMBOL(page_frag_alloc);
Alexander Duyckb63ae8c2015-05-06 21:11:57 -07004555
4556/*
4557 * Frees a page fragment allocated out of either a compound or order 0 page.
4558 */
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -08004559void page_frag_free(void *addr)
Alexander Duyckb63ae8c2015-05-06 21:11:57 -07004560{
4561 struct page *page = virt_to_head_page(addr);
4562
4563 if (unlikely(put_page_testzero(page)))
4564 __free_pages_ok(page, compound_order(page));
4565}
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -08004566EXPORT_SYMBOL(page_frag_free);
Alexander Duyckb63ae8c2015-05-06 21:11:57 -07004567
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08004568static void *make_alloc_exact(unsigned long addr, unsigned int order,
4569 size_t size)
Andi Kleenee85c2e2011-05-11 15:13:34 -07004570{
4571 if (addr) {
4572 unsigned long alloc_end = addr + (PAGE_SIZE << order);
4573 unsigned long used = addr + PAGE_ALIGN(size);
4574
4575 split_page(virt_to_page((void *)addr), order);
4576 while (used < alloc_end) {
4577 free_page(used);
4578 used += PAGE_SIZE;
4579 }
4580 }
4581 return (void *)addr;
4582}
4583
Timur Tabi2be0ffe2008-07-23 21:28:11 -07004584/**
4585 * alloc_pages_exact - allocate an exact number physically-contiguous pages.
4586 * @size: the number of bytes to allocate
4587 * @gfp_mask: GFP flags for the allocation
4588 *
4589 * This function is similar to alloc_pages(), except that it allocates the
4590 * minimum number of pages to satisfy the request. alloc_pages() can only
4591 * allocate memory in power-of-two pages.
4592 *
4593 * This function is also limited by MAX_ORDER.
4594 *
4595 * Memory allocated by this function must be released by free_pages_exact().
4596 */
4597void *alloc_pages_exact(size_t size, gfp_t gfp_mask)
4598{
4599 unsigned int order = get_order(size);
4600 unsigned long addr;
4601
4602 addr = __get_free_pages(gfp_mask, order);
Andi Kleenee85c2e2011-05-11 15:13:34 -07004603 return make_alloc_exact(addr, order, size);
Timur Tabi2be0ffe2008-07-23 21:28:11 -07004604}
4605EXPORT_SYMBOL(alloc_pages_exact);
4606
4607/**
Andi Kleenee85c2e2011-05-11 15:13:34 -07004608 * alloc_pages_exact_nid - allocate an exact number of physically-contiguous
4609 * pages on a node.
Randy Dunlapb5e6ab52011-05-16 13:16:54 -07004610 * @nid: the preferred node ID where memory should be allocated
Andi Kleenee85c2e2011-05-11 15:13:34 -07004611 * @size: the number of bytes to allocate
4612 * @gfp_mask: GFP flags for the allocation
4613 *
4614 * Like alloc_pages_exact(), but try to allocate on node nid first before falling
4615 * back.
Andi Kleenee85c2e2011-05-11 15:13:34 -07004616 */
Fabian Fredericke1931812014-08-06 16:04:59 -07004617void * __meminit alloc_pages_exact_nid(int nid, size_t size, gfp_t gfp_mask)
Andi Kleenee85c2e2011-05-11 15:13:34 -07004618{
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08004619 unsigned int order = get_order(size);
Andi Kleenee85c2e2011-05-11 15:13:34 -07004620 struct page *p = alloc_pages_node(nid, gfp_mask, order);
4621 if (!p)
4622 return NULL;
4623 return make_alloc_exact((unsigned long)page_address(p), order, size);
4624}
Andi Kleenee85c2e2011-05-11 15:13:34 -07004625
4626/**
Timur Tabi2be0ffe2008-07-23 21:28:11 -07004627 * free_pages_exact - release memory allocated via alloc_pages_exact()
4628 * @virt: the value returned by alloc_pages_exact.
4629 * @size: size of allocation, same value as passed to alloc_pages_exact().
4630 *
4631 * Release the memory allocated by a previous call to alloc_pages_exact.
4632 */
4633void free_pages_exact(void *virt, size_t size)
4634{
4635 unsigned long addr = (unsigned long)virt;
4636 unsigned long end = addr + PAGE_ALIGN(size);
4637
4638 while (addr < end) {
4639 free_page(addr);
4640 addr += PAGE_SIZE;
4641 }
4642}
4643EXPORT_SYMBOL(free_pages_exact);
4644
Zhang Yanfeie0fb5812013-02-22 16:35:54 -08004645/**
4646 * nr_free_zone_pages - count number of pages beyond high watermark
4647 * @offset: The zone index of the highest zone
4648 *
4649 * nr_free_zone_pages() counts the number of counts pages which are beyond the
4650 * high watermark within all zones at or below a given zone index. For each
4651 * zone, the number of pages is calculated as:
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -03004652 *
4653 * nr_free_zone_pages = managed_pages - high_pages
Zhang Yanfeie0fb5812013-02-22 16:35:54 -08004654 */
Zhang Yanfeiebec3862013-02-22 16:35:43 -08004655static unsigned long nr_free_zone_pages(int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656{
Mel Gormandd1a2392008-04-28 02:12:17 -07004657 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07004658 struct zone *zone;
4659
Martin J. Blighe310fd42005-07-29 22:59:18 -07004660 /* Just pick one node, since fallback list is circular */
Zhang Yanfeiebec3862013-02-22 16:35:43 -08004661 unsigned long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662
Mel Gorman0e884602008-04-28 02:12:14 -07004663 struct zonelist *zonelist = node_zonelist(numa_node_id(), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664
Mel Gorman54a6eb52008-04-28 02:12:16 -07004665 for_each_zone_zonelist(zone, z, zonelist, offset) {
Arun KS9705bea2018-12-28 00:34:24 -08004666 unsigned long size = zone_managed_pages(zone);
Mel Gorman41858962009-06-16 15:32:12 -07004667 unsigned long high = high_wmark_pages(zone);
Martin J. Blighe310fd42005-07-29 22:59:18 -07004668 if (size > high)
4669 sum += size - high;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 }
4671
4672 return sum;
4673}
4674
Zhang Yanfeie0fb5812013-02-22 16:35:54 -08004675/**
4676 * nr_free_buffer_pages - count number of pages beyond high watermark
4677 *
4678 * nr_free_buffer_pages() counts the number of pages which are beyond the high
4679 * watermark within ZONE_DMA and ZONE_NORMAL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 */
Zhang Yanfeiebec3862013-02-22 16:35:43 -08004681unsigned long nr_free_buffer_pages(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682{
Al Viroaf4ca452005-10-21 02:55:38 -04004683 return nr_free_zone_pages(gfp_zone(GFP_USER));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684}
Meelap Shahc2f1a552007-07-17 04:04:39 -07004685EXPORT_SYMBOL_GPL(nr_free_buffer_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686
Zhang Yanfeie0fb5812013-02-22 16:35:54 -08004687/**
4688 * nr_free_pagecache_pages - count number of pages beyond high watermark
4689 *
4690 * nr_free_pagecache_pages() counts the number of pages which are beyond the
4691 * high watermark within all zones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 */
Zhang Yanfeiebec3862013-02-22 16:35:43 -08004693unsigned long nr_free_pagecache_pages(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694{
Mel Gorman2a1e2742007-07-17 04:03:12 -07004695 return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER_MOVABLE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696}
Christoph Lameter08e0f6a2006-09-27 01:50:06 -07004697
4698static inline void show_node(struct zone *zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699{
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08004700 if (IS_ENABLED(CONFIG_NUMA))
Andy Whitcroft25ba77c2006-12-06 20:33:03 -08004701 printk("Node %d ", zone_to_nid(zone));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703
Igor Redkod02bd272016-03-17 14:19:05 -07004704long si_mem_available(void)
4705{
4706 long available;
4707 unsigned long pagecache;
4708 unsigned long wmark_low = 0;
4709 unsigned long pages[NR_LRU_LISTS];
Vlastimil Babkab29940c2018-10-26 15:05:46 -07004710 unsigned long reclaimable;
Igor Redkod02bd272016-03-17 14:19:05 -07004711 struct zone *zone;
4712 int lru;
4713
4714 for (lru = LRU_BASE; lru < NR_LRU_LISTS; lru++)
Mel Gorman2f95ff92016-08-11 15:32:57 -07004715 pages[lru] = global_node_page_state(NR_LRU_BASE + lru);
Igor Redkod02bd272016-03-17 14:19:05 -07004716
4717 for_each_zone(zone)
4718 wmark_low += zone->watermark[WMARK_LOW];
4719
4720 /*
4721 * Estimate the amount of memory available for userspace allocations,
4722 * without causing swapping.
4723 */
Michal Hockoc41f0122017-09-06 16:23:36 -07004724 available = global_zone_page_state(NR_FREE_PAGES) - totalreserve_pages;
Igor Redkod02bd272016-03-17 14:19:05 -07004725
4726 /*
4727 * Not all the page cache can be freed, otherwise the system will
4728 * start swapping. Assume at least half of the page cache, or the
4729 * low watermark worth of cache, needs to stay.
4730 */
4731 pagecache = pages[LRU_ACTIVE_FILE] + pages[LRU_INACTIVE_FILE];
4732 pagecache -= min(pagecache / 2, wmark_low);
4733 available += pagecache;
4734
4735 /*
Vlastimil Babkab29940c2018-10-26 15:05:46 -07004736 * Part of the reclaimable slab and other kernel memory consists of
4737 * items that are in use, and cannot be freed. Cap this estimate at the
4738 * low watermark.
Igor Redkod02bd272016-03-17 14:19:05 -07004739 */
Vlastimil Babkab29940c2018-10-26 15:05:46 -07004740 reclaimable = global_node_page_state(NR_SLAB_RECLAIMABLE) +
4741 global_node_page_state(NR_KERNEL_MISC_RECLAIMABLE);
4742 available += reclaimable - min(reclaimable / 2, wmark_low);
Roman Gushchin034ebf62018-04-10 16:27:40 -07004743
Igor Redkod02bd272016-03-17 14:19:05 -07004744 if (available < 0)
4745 available = 0;
4746 return available;
4747}
4748EXPORT_SYMBOL_GPL(si_mem_available);
4749
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750void si_meminfo(struct sysinfo *val)
4751{
Arun KSca79b0c2018-12-28 00:34:29 -08004752 val->totalram = totalram_pages();
Mel Gorman11fb9982016-07-28 15:46:20 -07004753 val->sharedram = global_node_page_state(NR_SHMEM);
Michal Hockoc41f0122017-09-06 16:23:36 -07004754 val->freeram = global_zone_page_state(NR_FREE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 val->bufferram = nr_blockdev_pages();
Arun KSca79b0c2018-12-28 00:34:29 -08004756 val->totalhigh = totalhigh_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 val->freehigh = nr_free_highpages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 val->mem_unit = PAGE_SIZE;
4759}
4760
4761EXPORT_SYMBOL(si_meminfo);
4762
4763#ifdef CONFIG_NUMA
4764void si_meminfo_node(struct sysinfo *val, int nid)
4765{
Jiang Liucdd91a72013-07-03 15:03:27 -07004766 int zone_type; /* needs to be signed */
4767 unsigned long managed_pages = 0;
Joonsoo Kimfc2bd792016-05-19 17:12:23 -07004768 unsigned long managed_highpages = 0;
4769 unsigned long free_highpages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 pg_data_t *pgdat = NODE_DATA(nid);
4771
Jiang Liucdd91a72013-07-03 15:03:27 -07004772 for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++)
Arun KS9705bea2018-12-28 00:34:24 -08004773 managed_pages += zone_managed_pages(&pgdat->node_zones[zone_type]);
Jiang Liucdd91a72013-07-03 15:03:27 -07004774 val->totalram = managed_pages;
Mel Gorman11fb9982016-07-28 15:46:20 -07004775 val->sharedram = node_page_state(pgdat, NR_SHMEM);
Mel Gorman75ef7182016-07-28 15:45:24 -07004776 val->freeram = sum_zone_node_page_state(nid, NR_FREE_PAGES);
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07004777#ifdef CONFIG_HIGHMEM
Joonsoo Kimfc2bd792016-05-19 17:12:23 -07004778 for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
4779 struct zone *zone = &pgdat->node_zones[zone_type];
4780
4781 if (is_highmem(zone)) {
Arun KS9705bea2018-12-28 00:34:24 -08004782 managed_highpages += zone_managed_pages(zone);
Joonsoo Kimfc2bd792016-05-19 17:12:23 -07004783 free_highpages += zone_page_state(zone, NR_FREE_PAGES);
4784 }
4785 }
4786 val->totalhigh = managed_highpages;
4787 val->freehigh = free_highpages;
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07004788#else
Joonsoo Kimfc2bd792016-05-19 17:12:23 -07004789 val->totalhigh = managed_highpages;
4790 val->freehigh = free_highpages;
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07004791#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 val->mem_unit = PAGE_SIZE;
4793}
4794#endif
4795
David Rientjesddd588b2011-03-22 16:30:46 -07004796/*
David Rientjes7bf02ea2011-05-24 17:11:16 -07004797 * Determine whether the node should be displayed or not, depending on whether
4798 * SHOW_MEM_FILTER_NODES was passed to show_free_areas().
David Rientjesddd588b2011-03-22 16:30:46 -07004799 */
Michal Hocko9af744d2017-02-22 15:46:16 -08004800static bool show_mem_node_skip(unsigned int flags, int nid, nodemask_t *nodemask)
David Rientjesddd588b2011-03-22 16:30:46 -07004801{
David Rientjesddd588b2011-03-22 16:30:46 -07004802 if (!(flags & SHOW_MEM_FILTER_NODES))
Michal Hocko9af744d2017-02-22 15:46:16 -08004803 return false;
David Rientjesddd588b2011-03-22 16:30:46 -07004804
Michal Hocko9af744d2017-02-22 15:46:16 -08004805 /*
4806 * no node mask - aka implicit memory numa policy. Do not bother with
4807 * the synchronization - read_mems_allowed_begin - because we do not
4808 * have to be precise here.
4809 */
4810 if (!nodemask)
4811 nodemask = &cpuset_current_mems_allowed;
4812
4813 return !node_isset(nid, *nodemask);
David Rientjesddd588b2011-03-22 16:30:46 -07004814}
4815
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816#define K(x) ((x) << (PAGE_SHIFT-10))
4817
Rabin Vincent377e4f12012-12-11 16:00:24 -08004818static void show_migration_types(unsigned char type)
4819{
4820 static const char types[MIGRATE_TYPES] = {
4821 [MIGRATE_UNMOVABLE] = 'U',
Rabin Vincent377e4f12012-12-11 16:00:24 -08004822 [MIGRATE_MOVABLE] = 'M',
Vlastimil Babka475a2f92015-12-11 13:40:29 -08004823 [MIGRATE_RECLAIMABLE] = 'E',
4824 [MIGRATE_HIGHATOMIC] = 'H',
Rabin Vincent377e4f12012-12-11 16:00:24 -08004825#ifdef CONFIG_CMA
4826 [MIGRATE_CMA] = 'C',
4827#endif
Minchan Kim194159f2013-02-22 16:33:58 -08004828#ifdef CONFIG_MEMORY_ISOLATION
Rabin Vincent377e4f12012-12-11 16:00:24 -08004829 [MIGRATE_ISOLATE] = 'I',
Minchan Kim194159f2013-02-22 16:33:58 -08004830#endif
Rabin Vincent377e4f12012-12-11 16:00:24 -08004831 };
4832 char tmp[MIGRATE_TYPES + 1];
4833 char *p = tmp;
4834 int i;
4835
4836 for (i = 0; i < MIGRATE_TYPES; i++) {
4837 if (type & (1 << i))
4838 *p++ = types[i];
4839 }
4840
4841 *p = '\0';
Joe Perches1f84a182016-10-27 17:46:29 -07004842 printk(KERN_CONT "(%s) ", tmp);
Rabin Vincent377e4f12012-12-11 16:00:24 -08004843}
4844
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845/*
4846 * Show free area list (used inside shift_scroll-lock stuff)
4847 * We also calculate the percentage fragmentation. We do this by counting the
4848 * memory on each free list with the exception of the first item on the list.
Konstantin Khlebnikovd1bfcdb2015-04-14 15:45:30 -07004849 *
4850 * Bits in @filter:
4851 * SHOW_MEM_FILTER_NODES: suppress nodes that are not allowed by current's
4852 * cpuset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 */
Michal Hocko9af744d2017-02-22 15:46:16 -08004854void show_free_areas(unsigned int filter, nodemask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855{
Konstantin Khlebnikovd1bfcdb2015-04-14 15:45:30 -07004856 unsigned long free_pcp = 0;
Jes Sorensenc7241912006-09-27 01:50:05 -07004857 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 struct zone *zone;
Mel Gorman599d0c92016-07-28 15:45:31 -07004859 pg_data_t *pgdat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860
KOSAKI Motohiroee99c712009-03-31 15:19:31 -07004861 for_each_populated_zone(zone) {
Michal Hocko9af744d2017-02-22 15:46:16 -08004862 if (show_mem_node_skip(filter, zone_to_nid(zone), nodemask))
David Rientjesddd588b2011-03-22 16:30:46 -07004863 continue;
Konstantin Khlebnikovd1bfcdb2015-04-14 15:45:30 -07004864
Konstantin Khlebnikov761b0672015-04-14 15:45:32 -07004865 for_each_online_cpu(cpu)
4866 free_pcp += per_cpu_ptr(zone->pageset, cpu)->pcp.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 }
4868
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07004869 printk("active_anon:%lu inactive_anon:%lu isolated_anon:%lu\n"
4870 " active_file:%lu inactive_file:%lu isolated_file:%lu\n"
Konstantin Khlebnikovd1bfcdb2015-04-14 15:45:30 -07004871 " unevictable:%lu dirty:%lu writeback:%lu unstable:%lu\n"
4872 " slab_reclaimable:%lu slab_unreclaimable:%lu\n"
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -07004873 " mapped:%lu shmem:%lu pagetables:%lu bounce:%lu\n"
Konstantin Khlebnikovd1bfcdb2015-04-14 15:45:30 -07004874 " free:%lu free_pcp:%lu free_cma:%lu\n",
Mel Gorman599d0c92016-07-28 15:45:31 -07004875 global_node_page_state(NR_ACTIVE_ANON),
4876 global_node_page_state(NR_INACTIVE_ANON),
4877 global_node_page_state(NR_ISOLATED_ANON),
4878 global_node_page_state(NR_ACTIVE_FILE),
4879 global_node_page_state(NR_INACTIVE_FILE),
4880 global_node_page_state(NR_ISOLATED_FILE),
4881 global_node_page_state(NR_UNEVICTABLE),
Mel Gorman11fb9982016-07-28 15:46:20 -07004882 global_node_page_state(NR_FILE_DIRTY),
4883 global_node_page_state(NR_WRITEBACK),
4884 global_node_page_state(NR_UNSTABLE_NFS),
Johannes Weinerd507e2eb2017-08-10 15:23:31 -07004885 global_node_page_state(NR_SLAB_RECLAIMABLE),
4886 global_node_page_state(NR_SLAB_UNRECLAIMABLE),
Mel Gorman50658e22016-07-28 15:46:14 -07004887 global_node_page_state(NR_FILE_MAPPED),
Mel Gorman11fb9982016-07-28 15:46:20 -07004888 global_node_page_state(NR_SHMEM),
Michal Hockoc41f0122017-09-06 16:23:36 -07004889 global_zone_page_state(NR_PAGETABLE),
4890 global_zone_page_state(NR_BOUNCE),
4891 global_zone_page_state(NR_FREE_PAGES),
Konstantin Khlebnikovd1bfcdb2015-04-14 15:45:30 -07004892 free_pcp,
Michal Hockoc41f0122017-09-06 16:23:36 -07004893 global_zone_page_state(NR_FREE_CMA_PAGES));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894
Mel Gorman599d0c92016-07-28 15:45:31 -07004895 for_each_online_pgdat(pgdat) {
Michal Hocko9af744d2017-02-22 15:46:16 -08004896 if (show_mem_node_skip(filter, pgdat->node_id, nodemask))
Michal Hockoc02e50b2017-02-22 15:46:07 -08004897 continue;
4898
Mel Gorman599d0c92016-07-28 15:45:31 -07004899 printk("Node %d"
4900 " active_anon:%lukB"
4901 " inactive_anon:%lukB"
4902 " active_file:%lukB"
4903 " inactive_file:%lukB"
4904 " unevictable:%lukB"
4905 " isolated(anon):%lukB"
4906 " isolated(file):%lukB"
Mel Gorman50658e22016-07-28 15:46:14 -07004907 " mapped:%lukB"
Mel Gorman11fb9982016-07-28 15:46:20 -07004908 " dirty:%lukB"
4909 " writeback:%lukB"
4910 " shmem:%lukB"
4911#ifdef CONFIG_TRANSPARENT_HUGEPAGE
4912 " shmem_thp: %lukB"
4913 " shmem_pmdmapped: %lukB"
4914 " anon_thp: %lukB"
4915#endif
4916 " writeback_tmp:%lukB"
4917 " unstable:%lukB"
Mel Gorman599d0c92016-07-28 15:45:31 -07004918 " all_unreclaimable? %s"
4919 "\n",
4920 pgdat->node_id,
4921 K(node_page_state(pgdat, NR_ACTIVE_ANON)),
4922 K(node_page_state(pgdat, NR_INACTIVE_ANON)),
4923 K(node_page_state(pgdat, NR_ACTIVE_FILE)),
4924 K(node_page_state(pgdat, NR_INACTIVE_FILE)),
4925 K(node_page_state(pgdat, NR_UNEVICTABLE)),
4926 K(node_page_state(pgdat, NR_ISOLATED_ANON)),
4927 K(node_page_state(pgdat, NR_ISOLATED_FILE)),
Mel Gorman50658e22016-07-28 15:46:14 -07004928 K(node_page_state(pgdat, NR_FILE_MAPPED)),
Mel Gorman11fb9982016-07-28 15:46:20 -07004929 K(node_page_state(pgdat, NR_FILE_DIRTY)),
4930 K(node_page_state(pgdat, NR_WRITEBACK)),
Alexander Polakov1f06b812017-04-07 16:04:45 -07004931 K(node_page_state(pgdat, NR_SHMEM)),
Mel Gorman11fb9982016-07-28 15:46:20 -07004932#ifdef CONFIG_TRANSPARENT_HUGEPAGE
4933 K(node_page_state(pgdat, NR_SHMEM_THPS) * HPAGE_PMD_NR),
4934 K(node_page_state(pgdat, NR_SHMEM_PMDMAPPED)
4935 * HPAGE_PMD_NR),
4936 K(node_page_state(pgdat, NR_ANON_THPS) * HPAGE_PMD_NR),
4937#endif
Mel Gorman11fb9982016-07-28 15:46:20 -07004938 K(node_page_state(pgdat, NR_WRITEBACK_TEMP)),
4939 K(node_page_state(pgdat, NR_UNSTABLE_NFS)),
Johannes Weinerc73322d2017-05-03 14:51:51 -07004940 pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES ?
4941 "yes" : "no");
Mel Gorman599d0c92016-07-28 15:45:31 -07004942 }
4943
KOSAKI Motohiroee99c712009-03-31 15:19:31 -07004944 for_each_populated_zone(zone) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 int i;
4946
Michal Hocko9af744d2017-02-22 15:46:16 -08004947 if (show_mem_node_skip(filter, zone_to_nid(zone), nodemask))
David Rientjesddd588b2011-03-22 16:30:46 -07004948 continue;
Konstantin Khlebnikovd1bfcdb2015-04-14 15:45:30 -07004949
4950 free_pcp = 0;
4951 for_each_online_cpu(cpu)
4952 free_pcp += per_cpu_ptr(zone->pageset, cpu)->pcp.count;
4953
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 show_node(zone);
Joe Perches1f84a182016-10-27 17:46:29 -07004955 printk(KERN_CONT
4956 "%s"
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 " free:%lukB"
4958 " min:%lukB"
4959 " low:%lukB"
4960 " high:%lukB"
Minchan Kim71c799f2016-07-28 15:47:26 -07004961 " active_anon:%lukB"
4962 " inactive_anon:%lukB"
4963 " active_file:%lukB"
4964 " inactive_file:%lukB"
4965 " unevictable:%lukB"
Mel Gorman5a1c84b2016-07-28 15:47:31 -07004966 " writepending:%lukB"
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 " present:%lukB"
Jiang Liu9feedc92012-12-12 13:52:12 -08004968 " managed:%lukB"
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07004969 " mlocked:%lukB"
KOSAKI Motohiroc6a7f572009-09-21 17:01:32 -07004970 " kernel_stack:%lukB"
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07004971 " pagetables:%lukB"
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07004972 " bounce:%lukB"
Konstantin Khlebnikovd1bfcdb2015-04-14 15:45:30 -07004973 " free_pcp:%lukB"
4974 " local_pcp:%ukB"
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -07004975 " free_cma:%lukB"
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 "\n",
4977 zone->name,
Mel Gorman88f5acf2011-01-13 15:45:41 -08004978 K(zone_page_state(zone, NR_FREE_PAGES)),
Mel Gorman41858962009-06-16 15:32:12 -07004979 K(min_wmark_pages(zone)),
4980 K(low_wmark_pages(zone)),
4981 K(high_wmark_pages(zone)),
Minchan Kim71c799f2016-07-28 15:47:26 -07004982 K(zone_page_state(zone, NR_ZONE_ACTIVE_ANON)),
4983 K(zone_page_state(zone, NR_ZONE_INACTIVE_ANON)),
4984 K(zone_page_state(zone, NR_ZONE_ACTIVE_FILE)),
4985 K(zone_page_state(zone, NR_ZONE_INACTIVE_FILE)),
4986 K(zone_page_state(zone, NR_ZONE_UNEVICTABLE)),
Mel Gorman5a1c84b2016-07-28 15:47:31 -07004987 K(zone_page_state(zone, NR_ZONE_WRITE_PENDING)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 K(zone->present_pages),
Arun KS9705bea2018-12-28 00:34:24 -08004989 K(zone_managed_pages(zone)),
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07004990 K(zone_page_state(zone, NR_MLOCK)),
Andy Lutomirskid30dd8b2016-07-28 15:48:14 -07004991 zone_page_state(zone, NR_KERNEL_STACK_KB),
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07004992 K(zone_page_state(zone, NR_PAGETABLE)),
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07004993 K(zone_page_state(zone, NR_BOUNCE)),
Konstantin Khlebnikovd1bfcdb2015-04-14 15:45:30 -07004994 K(free_pcp),
4995 K(this_cpu_read(zone->pageset->pcp.count)),
Minchan Kim33e077b2016-07-28 15:47:14 -07004996 K(zone_page_state(zone, NR_FREE_CMA_PAGES)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 printk("lowmem_reserve[]:");
4998 for (i = 0; i < MAX_NR_ZONES; i++)
Joe Perches1f84a182016-10-27 17:46:29 -07004999 printk(KERN_CONT " %ld", zone->lowmem_reserve[i]);
5000 printk(KERN_CONT "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 }
5002
KOSAKI Motohiroee99c712009-03-31 15:19:31 -07005003 for_each_populated_zone(zone) {
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08005004 unsigned int order;
5005 unsigned long nr[MAX_ORDER], flags, total = 0;
Rabin Vincent377e4f12012-12-11 16:00:24 -08005006 unsigned char types[MAX_ORDER];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007
Michal Hocko9af744d2017-02-22 15:46:16 -08005008 if (show_mem_node_skip(filter, zone_to_nid(zone), nodemask))
David Rientjesddd588b2011-03-22 16:30:46 -07005009 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 show_node(zone);
Joe Perches1f84a182016-10-27 17:46:29 -07005011 printk(KERN_CONT "%s: ", zone->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012
5013 spin_lock_irqsave(&zone->lock, flags);
5014 for (order = 0; order < MAX_ORDER; order++) {
Rabin Vincent377e4f12012-12-11 16:00:24 -08005015 struct free_area *area = &zone->free_area[order];
5016 int type;
5017
5018 nr[order] = area->nr_free;
Kirill Korotaev8f9de512006-06-23 02:03:50 -07005019 total += nr[order] << order;
Rabin Vincent377e4f12012-12-11 16:00:24 -08005020
5021 types[order] = 0;
5022 for (type = 0; type < MIGRATE_TYPES; type++) {
5023 if (!list_empty(&area->free_list[type]))
5024 types[order] |= 1 << type;
5025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 }
5027 spin_unlock_irqrestore(&zone->lock, flags);
Rabin Vincent377e4f12012-12-11 16:00:24 -08005028 for (order = 0; order < MAX_ORDER; order++) {
Joe Perches1f84a182016-10-27 17:46:29 -07005029 printk(KERN_CONT "%lu*%lukB ",
5030 nr[order], K(1UL) << order);
Rabin Vincent377e4f12012-12-11 16:00:24 -08005031 if (nr[order])
5032 show_migration_types(types[order]);
5033 }
Joe Perches1f84a182016-10-27 17:46:29 -07005034 printk(KERN_CONT "= %lukB\n", K(total));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 }
5036
David Rientjes949f7ec2013-04-29 15:07:48 -07005037 hugetlb_show_meminfo();
5038
Mel Gorman11fb9982016-07-28 15:46:20 -07005039 printk("%ld total pagecache pages\n", global_node_page_state(NR_FILE_PAGES));
Larry Woodmane6f36022008-02-04 22:29:30 -08005040
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 show_swap_cache_info();
5042}
5043
Mel Gorman19770b32008-04-28 02:12:18 -07005044static void zoneref_set_zone(struct zone *zone, struct zoneref *zoneref)
5045{
5046 zoneref->zone = zone;
5047 zoneref->zone_idx = zone_idx(zone);
5048}
5049
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050/*
5051 * Builds allocation fallback zone lists.
Christoph Lameter1a932052006-01-06 00:11:16 -08005052 *
5053 * Add all populated zones of a node to the zonelist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 */
Michal Hocko9d3be212017-09-06 16:20:30 -07005055static int build_zonerefs_node(pg_data_t *pgdat, struct zoneref *zonerefs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056{
Christoph Lameter1a932052006-01-06 00:11:16 -08005057 struct zone *zone;
Zhang Yanfeibc732f12013-07-08 16:00:06 -07005058 enum zone_type zone_type = MAX_NR_ZONES;
Michal Hocko9d3be212017-09-06 16:20:30 -07005059 int nr_zones = 0;
Christoph Lameter02a68a52006-01-06 00:11:18 -08005060
5061 do {
Christoph Lameter2f6726e2006-09-25 23:31:18 -07005062 zone_type--;
Christoph Lameter070f8032006-01-06 00:11:19 -08005063 zone = pgdat->node_zones + zone_type;
Mel Gorman6aa303d2016-09-01 16:14:55 -07005064 if (managed_zone(zone)) {
Michal Hocko9d3be212017-09-06 16:20:30 -07005065 zoneref_set_zone(zone, &zonerefs[nr_zones++]);
Christoph Lameter070f8032006-01-06 00:11:19 -08005066 check_highest_zone(zone_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 }
Christoph Lameter2f6726e2006-09-25 23:31:18 -07005068 } while (zone_type);
Zhang Yanfeibc732f12013-07-08 16:00:06 -07005069
Christoph Lameter070f8032006-01-06 00:11:19 -08005070 return nr_zones;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071}
5072
5073#ifdef CONFIG_NUMA
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005074
5075static int __parse_numa_zonelist_order(char *s)
5076{
Michal Hockoc9bff3e2017-09-06 16:20:13 -07005077 /*
5078 * We used to support different zonlists modes but they turned
5079 * out to be just not useful. Let's keep the warning in place
5080 * if somebody still use the cmd line parameter so that we do
5081 * not fail it silently
5082 */
5083 if (!(*s == 'd' || *s == 'D' || *s == 'n' || *s == 'N')) {
5084 pr_warn("Ignoring unsupported numa_zonelist_order value: %s\n", s);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005085 return -EINVAL;
5086 }
5087 return 0;
5088}
5089
5090static __init int setup_numa_zonelist_order(char *s)
5091{
Volodymyr G. Lukiianykecb256f2011-01-13 15:46:26 -08005092 if (!s)
5093 return 0;
5094
Michal Hockoc9bff3e2017-09-06 16:20:13 -07005095 return __parse_numa_zonelist_order(s);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005096}
5097early_param("numa_zonelist_order", setup_numa_zonelist_order);
5098
Michal Hockoc9bff3e2017-09-06 16:20:13 -07005099char numa_zonelist_order[] = "Node";
5100
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005101/*
5102 * sysctl handler for numa_zonelist_order
5103 */
Joe Perchescccad5b2014-06-06 14:38:09 -07005104int numa_zonelist_order_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005105 void __user *buffer, size_t *length,
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005106 loff_t *ppos)
5107{
Michal Hockoc9bff3e2017-09-06 16:20:13 -07005108 char *str;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005109 int ret;
5110
Michal Hockoc9bff3e2017-09-06 16:20:13 -07005111 if (!write)
5112 return proc_dostring(table, write, buffer, length, ppos);
5113 str = memdup_user_nul(buffer, 16);
5114 if (IS_ERR(str))
5115 return PTR_ERR(str);
Chen Gangdacbde02013-07-03 15:02:35 -07005116
Michal Hockoc9bff3e2017-09-06 16:20:13 -07005117 ret = __parse_numa_zonelist_order(str);
5118 kfree(str);
Andi Kleen443c6f12009-12-23 21:00:47 +01005119 return ret;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005120}
5121
5122
Christoph Lameter62bc62a2009-06-16 15:32:15 -07005123#define MAX_NODE_LOAD (nr_online_nodes)
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005124static int node_load[MAX_NUMNODES];
5125
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126/**
Pavel Pisa4dc3b162005-05-01 08:59:25 -07005127 * 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 -07005128 * @node: node whose fallback list we're appending
5129 * @used_node_mask: nodemask_t of already used nodes
5130 *
5131 * We use a number of factors to determine which is the next node that should
5132 * appear on a given node's fallback list. The node should not have appeared
5133 * already in @node's fallback list, and it should be the next closest node
5134 * according to the distance array (which contains arbitrary distance values
5135 * from each node to each node in the system), and should also prefer nodes
5136 * with no CPUs, since presumably they'll have very little allocation pressure
5137 * on them otherwise.
5138 * It returns -1 if no node is found.
5139 */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005140static int find_next_best_node(int node, nodemask_t *used_node_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141{
Linus Torvalds4cf808eb2006-02-17 20:38:21 +01005142 int n, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 int min_val = INT_MAX;
David Rientjes00ef2d22013-02-22 16:35:36 -08005144 int best_node = NUMA_NO_NODE;
Rusty Russella70f7302009-03-13 14:49:46 +10305145 const struct cpumask *tmp = cpumask_of_node(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146
Linus Torvalds4cf808eb2006-02-17 20:38:21 +01005147 /* Use the local node if we haven't already */
5148 if (!node_isset(node, *used_node_mask)) {
5149 node_set(node, *used_node_mask);
5150 return node;
5151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08005153 for_each_node_state(n, N_MEMORY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154
5155 /* Don't want a node to appear more than once */
5156 if (node_isset(n, *used_node_mask))
5157 continue;
5158
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 /* Use the distance array to find the distance */
5160 val = node_distance(node, n);
5161
Linus Torvalds4cf808eb2006-02-17 20:38:21 +01005162 /* Penalize nodes under us ("prefer the next node") */
5163 val += (n < node);
5164
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 /* Give preference to headless and unused nodes */
Rusty Russella70f7302009-03-13 14:49:46 +10305166 tmp = cpumask_of_node(n);
5167 if (!cpumask_empty(tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 val += PENALTY_FOR_NODE_WITH_CPUS;
5169
5170 /* Slight preference for less loaded node */
5171 val *= (MAX_NODE_LOAD*MAX_NUMNODES);
5172 val += node_load[n];
5173
5174 if (val < min_val) {
5175 min_val = val;
5176 best_node = n;
5177 }
5178 }
5179
5180 if (best_node >= 0)
5181 node_set(best_node, *used_node_mask);
5182
5183 return best_node;
5184}
5185
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005186
5187/*
5188 * Build zonelists ordered by node and zones within node.
5189 * This results in maximum locality--normal zone overflows into local
5190 * DMA zone, if any--but risks exhausting DMA zone.
5191 */
Michal Hocko9d3be212017-09-06 16:20:30 -07005192static void build_zonelists_in_node_order(pg_data_t *pgdat, int *node_order,
5193 unsigned nr_nodes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194{
Michal Hocko9d3be212017-09-06 16:20:30 -07005195 struct zoneref *zonerefs;
5196 int i;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005197
Michal Hocko9d3be212017-09-06 16:20:30 -07005198 zonerefs = pgdat->node_zonelists[ZONELIST_FALLBACK]._zonerefs;
5199
5200 for (i = 0; i < nr_nodes; i++) {
5201 int nr_zones;
5202
5203 pg_data_t *node = NODE_DATA(node_order[i]);
5204
5205 nr_zones = build_zonerefs_node(node, zonerefs);
5206 zonerefs += nr_zones;
5207 }
5208 zonerefs->zone = NULL;
5209 zonerefs->zone_idx = 0;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005210}
5211
5212/*
Christoph Lameter523b9452007-10-16 01:25:37 -07005213 * Build gfp_thisnode zonelists
5214 */
5215static void build_thisnode_zonelists(pg_data_t *pgdat)
5216{
Michal Hocko9d3be212017-09-06 16:20:30 -07005217 struct zoneref *zonerefs;
5218 int nr_zones;
Christoph Lameter523b9452007-10-16 01:25:37 -07005219
Michal Hocko9d3be212017-09-06 16:20:30 -07005220 zonerefs = pgdat->node_zonelists[ZONELIST_NOFALLBACK]._zonerefs;
5221 nr_zones = build_zonerefs_node(pgdat, zonerefs);
5222 zonerefs += nr_zones;
5223 zonerefs->zone = NULL;
5224 zonerefs->zone_idx = 0;
Christoph Lameter523b9452007-10-16 01:25:37 -07005225}
5226
5227/*
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005228 * Build zonelists ordered by zone and nodes within zones.
5229 * This results in conserving DMA zone[s] until all Normal memory is
5230 * exhausted, but results in overflowing to remote node while memory
5231 * may still exist in local DMA zone.
5232 */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005233
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005234static void build_zonelists(pg_data_t *pgdat)
5235{
Michal Hocko9d3be212017-09-06 16:20:30 -07005236 static int node_order[MAX_NUMNODES];
5237 int node, load, nr_nodes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 nodemask_t used_mask;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005239 int local_node, prev_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240
5241 /* NUMA-aware ordering of nodes */
5242 local_node = pgdat->node_id;
Christoph Lameter62bc62a2009-06-16 15:32:15 -07005243 load = nr_online_nodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 prev_node = local_node;
5245 nodes_clear(used_mask);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005246
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005247 memset(node_order, 0, sizeof(node_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
5249 /*
5250 * We don't want to pressure a particular node.
5251 * So adding penalty to the first node in same
5252 * distance group to make it round-robin.
5253 */
David Rientjes957f8222012-10-08 16:33:24 -07005254 if (node_distance(local_node, node) !=
5255 node_distance(local_node, prev_node))
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005256 node_load[node] = load;
5257
Michal Hocko9d3be212017-09-06 16:20:30 -07005258 node_order[nr_nodes++] = node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 prev_node = node;
5260 load--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 }
Christoph Lameter523b9452007-10-16 01:25:37 -07005262
Michal Hocko9d3be212017-09-06 16:20:30 -07005263 build_zonelists_in_node_order(pgdat, node_order, nr_nodes);
Christoph Lameter523b9452007-10-16 01:25:37 -07005264 build_thisnode_zonelists(pgdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265}
5266
Lee Schermerhorn7aac7892010-05-26 14:45:00 -07005267#ifdef CONFIG_HAVE_MEMORYLESS_NODES
5268/*
5269 * Return node id of node used for "local" allocations.
5270 * I.e., first node id of first zone in arg node's generic zonelist.
5271 * Used for initializing percpu 'numa_mem', which is used primarily
5272 * for kernel allocations, so use GFP_KERNEL flags to locate zonelist.
5273 */
5274int local_memory_node(int node)
5275{
Mel Gormanc33d6c02016-05-19 17:14:10 -07005276 struct zoneref *z;
Lee Schermerhorn7aac7892010-05-26 14:45:00 -07005277
Mel Gormanc33d6c02016-05-19 17:14:10 -07005278 z = first_zones_zonelist(node_zonelist(node, GFP_KERNEL),
Lee Schermerhorn7aac7892010-05-26 14:45:00 -07005279 gfp_zone(GFP_KERNEL),
Mel Gormanc33d6c02016-05-19 17:14:10 -07005280 NULL);
Pavel Tatashinc1093b72018-08-21 21:53:32 -07005281 return zone_to_nid(z->zone);
Lee Schermerhorn7aac7892010-05-26 14:45:00 -07005282}
5283#endif
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005284
Joonsoo Kim6423aa82016-08-10 16:27:49 -07005285static void setup_min_unmapped_ratio(void);
5286static void setup_min_slab_ratio(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287#else /* CONFIG_NUMA */
5288
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005289static void build_zonelists(pg_data_t *pgdat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290{
Christoph Lameter19655d32006-09-25 23:31:19 -07005291 int node, local_node;
Michal Hocko9d3be212017-09-06 16:20:30 -07005292 struct zoneref *zonerefs;
5293 int nr_zones;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294
5295 local_node = pgdat->node_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296
Michal Hocko9d3be212017-09-06 16:20:30 -07005297 zonerefs = pgdat->node_zonelists[ZONELIST_FALLBACK]._zonerefs;
5298 nr_zones = build_zonerefs_node(pgdat, zonerefs);
5299 zonerefs += nr_zones;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300
Mel Gorman54a6eb52008-04-28 02:12:16 -07005301 /*
5302 * Now we build the zonelist so that it contains the zones
5303 * of all the other nodes.
5304 * We don't want to pressure a particular node, so when
5305 * building the zones for node N, we make sure that the
5306 * zones coming right after the local ones are those from
5307 * node N+1 (modulo N)
5308 */
5309 for (node = local_node + 1; node < MAX_NUMNODES; node++) {
5310 if (!node_online(node))
5311 continue;
Michal Hocko9d3be212017-09-06 16:20:30 -07005312 nr_zones = build_zonerefs_node(NODE_DATA(node), zonerefs);
5313 zonerefs += nr_zones;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 }
Mel Gorman54a6eb52008-04-28 02:12:16 -07005315 for (node = 0; node < local_node; node++) {
5316 if (!node_online(node))
5317 continue;
Michal Hocko9d3be212017-09-06 16:20:30 -07005318 nr_zones = build_zonerefs_node(NODE_DATA(node), zonerefs);
5319 zonerefs += nr_zones;
Mel Gorman54a6eb52008-04-28 02:12:16 -07005320 }
5321
Michal Hocko9d3be212017-09-06 16:20:30 -07005322 zonerefs->zone = NULL;
5323 zonerefs->zone_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324}
5325
5326#endif /* CONFIG_NUMA */
5327
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09005328/*
5329 * Boot pageset table. One per cpu which is going to be used for all
5330 * zones and all nodes. The parameters will be set in such a way
5331 * that an item put on a list will immediately be handed over to
5332 * the buddy list. This is safe since pageset manipulation is done
5333 * with interrupts disabled.
5334 *
5335 * The boot_pagesets must be kept even after bootup is complete for
5336 * unused processors and/or zones. They do play a role for bootstrapping
5337 * hotplugged processors.
5338 *
5339 * zoneinfo_show() and maybe other functions do
5340 * not check if the processor is online before following the pageset pointer.
5341 * Other parts of the kernel may not check if the zone is available.
5342 */
5343static void setup_pageset(struct per_cpu_pageset *p, unsigned long batch);
5344static DEFINE_PER_CPU(struct per_cpu_pageset, boot_pageset);
Johannes Weiner385386c2017-07-06 15:40:43 -07005345static DEFINE_PER_CPU(struct per_cpu_nodestat, boot_nodestats);
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09005346
Michal Hocko11cd8632017-09-06 16:20:34 -07005347static void __build_all_zonelists(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348{
Yasunori Goto68113782006-06-23 02:03:11 -07005349 int nid;
Michal Hockoafb6ebb2017-09-06 16:20:17 -07005350 int __maybe_unused cpu;
Jiang Liu9adb62a2012-07-31 16:43:28 -07005351 pg_data_t *self = data;
Michal Hockob93e0f32017-09-06 16:20:37 -07005352 static DEFINE_SPINLOCK(lock);
5353
5354 spin_lock(&lock);
Paul Jackson9276b1bc2006-12-06 20:31:48 -08005355
Bo Liu7f9cfb32009-08-18 14:11:19 -07005356#ifdef CONFIG_NUMA
5357 memset(node_load, 0, sizeof(node_load));
5358#endif
Jiang Liu9adb62a2012-07-31 16:43:28 -07005359
Wei Yangc1152582017-09-06 16:19:33 -07005360 /*
5361 * This node is hotadded and no memory is yet present. So just
5362 * building zonelists is fine - no need to touch other nodes.
5363 */
Jiang Liu9adb62a2012-07-31 16:43:28 -07005364 if (self && !node_online(self->node_id)) {
5365 build_zonelists(self);
Wei Yangc1152582017-09-06 16:19:33 -07005366 } else {
5367 for_each_online_node(nid) {
5368 pg_data_t *pgdat = NODE_DATA(nid);
Jiang Liu9adb62a2012-07-31 16:43:28 -07005369
Wei Yangc1152582017-09-06 16:19:33 -07005370 build_zonelists(pgdat);
5371 }
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09005372
Michal Hockoafb6ebb2017-09-06 16:20:17 -07005373#ifdef CONFIG_HAVE_MEMORYLESS_NODES
Michal Hockoafb6ebb2017-09-06 16:20:17 -07005374 /*
5375 * We now know the "local memory node" for each node--
5376 * i.e., the node of the first zone in the generic zonelist.
5377 * Set up numa_mem percpu variable for on-line cpus. During
5378 * boot, only the boot cpu should be on-line; we'll init the
5379 * secondary cpus' numa_mem as they come on-line. During
5380 * node/memory hotplug, we'll fixup all on-line cpus.
5381 */
Michal Hockod9c9a0b2017-09-06 16:20:20 -07005382 for_each_online_cpu(cpu)
Michal Hockoafb6ebb2017-09-06 16:20:17 -07005383 set_cpu_numa_mem(cpu, local_memory_node(cpu_to_node(cpu)));
Michal Hockoafb6ebb2017-09-06 16:20:17 -07005384#endif
Michal Hockod9c9a0b2017-09-06 16:20:20 -07005385 }
Michal Hockob93e0f32017-09-06 16:20:37 -07005386
5387 spin_unlock(&lock);
Michal Hockoafb6ebb2017-09-06 16:20:17 -07005388}
5389
5390static noinline void __init
5391build_all_zonelists_init(void)
5392{
5393 int cpu;
5394
5395 __build_all_zonelists(NULL);
5396
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09005397 /*
5398 * Initialize the boot_pagesets that are going to be used
5399 * for bootstrapping processors. The real pagesets for
5400 * each zone will be allocated later when the per cpu
5401 * allocator is available.
5402 *
5403 * boot_pagesets are used also for bootstrapping offline
5404 * cpus if the system is already booted because the pagesets
5405 * are needed to initialize allocators on a specific cpu too.
5406 * F.e. the percpu allocator needs the page allocator which
5407 * needs the percpu allocator in order to allocate its pagesets
5408 * (a chicken-egg dilemma).
5409 */
Michal Hockoafb6ebb2017-09-06 16:20:17 -07005410 for_each_possible_cpu(cpu)
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09005411 setup_pageset(&per_cpu(boot_pageset, cpu), 0);
5412
Rasmus Villemoes061f67b2015-02-12 15:00:06 -08005413 mminit_verify_zonelist();
5414 cpuset_init_current_mems_allowed();
5415}
5416
Haicheng Li4eaf3f62010-05-24 14:32:52 -07005417/*
Haicheng Li4eaf3f62010-05-24 14:32:52 -07005418 * unless system_state == SYSTEM_BOOTING.
Rasmus Villemoes061f67b2015-02-12 15:00:06 -08005419 *
Michal Hocko72675e12017-09-06 16:20:24 -07005420 * __ref due to call of __init annotated helper build_all_zonelists_init
Rasmus Villemoes061f67b2015-02-12 15:00:06 -08005421 * [protected by SYSTEM_BOOTING].
Haicheng Li4eaf3f62010-05-24 14:32:52 -07005422 */
Michal Hocko72675e12017-09-06 16:20:24 -07005423void __ref build_all_zonelists(pg_data_t *pgdat)
Yasunori Goto68113782006-06-23 02:03:11 -07005424{
5425 if (system_state == SYSTEM_BOOTING) {
Rasmus Villemoes061f67b2015-02-12 15:00:06 -08005426 build_all_zonelists_init();
Yasunori Goto68113782006-06-23 02:03:11 -07005427 } else {
Michal Hocko11cd8632017-09-06 16:20:34 -07005428 __build_all_zonelists(pgdat);
Yasunori Goto68113782006-06-23 02:03:11 -07005429 /* cpuset refresh routine should be here */
5430 }
Andrew Mortonbd1e22b2006-06-23 02:03:47 -07005431 vm_total_pages = nr_free_pagecache_pages();
Mel Gorman9ef9acb2007-10-16 01:25:54 -07005432 /*
5433 * Disable grouping by mobility if the number of pages in the
5434 * system is too low to allow the mechanism to work. It would be
5435 * more accurate, but expensive to check per-zone. This check is
5436 * made on memory-hotadd so a system can start with mobility
5437 * disabled and enable it later
5438 */
Mel Gormand9c23402007-10-16 01:26:01 -07005439 if (vm_total_pages < (pageblock_nr_pages * MIGRATE_TYPES))
Mel Gorman9ef9acb2007-10-16 01:25:54 -07005440 page_group_by_mobility_disabled = 1;
5441 else
5442 page_group_by_mobility_disabled = 0;
5443
Michal Hockoc9bff3e2017-09-06 16:20:13 -07005444 pr_info("Built %i zonelists, mobility grouping %s. Total pages: %ld\n",
Joe Perches756a0252016-03-17 14:19:47 -07005445 nr_online_nodes,
Joe Perches756a0252016-03-17 14:19:47 -07005446 page_group_by_mobility_disabled ? "off" : "on",
5447 vm_total_pages);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005448#ifdef CONFIG_NUMA
Anton Blanchardf88dfff2014-12-10 15:42:53 -08005449 pr_info("Policy zone: %s\n", zone_names[policy_zone]);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005450#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451}
5452
Pavel Tatashina9a9e772018-10-26 15:09:40 -07005453/* If zone is ZONE_MOVABLE but memory is mirrored, it is an overlapped init */
5454static bool __meminit
5455overlap_memmap_init(unsigned long zone, unsigned long *pfn)
5456{
5457#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
5458 static struct memblock_region *r;
5459
5460 if (mirrored_kernelcore && zone == ZONE_MOVABLE) {
5461 if (!r || *pfn >= memblock_region_memory_end_pfn(r)) {
5462 for_each_memblock(memory, r) {
5463 if (*pfn < memblock_region_memory_end_pfn(r))
5464 break;
5465 }
5466 }
5467 if (*pfn >= memblock_region_memory_base_pfn(r) &&
5468 memblock_is_mirror(r)) {
5469 *pfn = memblock_region_memory_end_pfn(r);
5470 return true;
5471 }
5472 }
5473#endif
5474 return false;
5475}
5476
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 * Initially all pages are reserved - free ones are freed
Mike Rapoportc6ffc5c2018-10-30 15:09:30 -07005479 * up by memblock_free_all() once the early boot process is
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 * done. Non-atomic initialization, single-pass.
5481 */
Matt Tolentinoc09b4242006-01-17 07:03:44 +01005482void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
Christoph Hellwiga99583e2017-12-29 08:53:57 +01005483 unsigned long start_pfn, enum memmap_context context,
5484 struct vmem_altmap *altmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485{
Pavel Tatashina9a9e772018-10-26 15:09:40 -07005486 unsigned long pfn, end_pfn = start_pfn + size;
Pavel Tatashind0dc12e2018-04-05 16:23:00 -07005487 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488
Hugh Dickins22b31ee2009-01-06 14:40:09 -08005489 if (highest_memmap_pfn < end_pfn - 1)
5490 highest_memmap_pfn = end_pfn - 1;
5491
Alexander Duyck966cf442018-10-26 15:07:52 -07005492#ifdef CONFIG_ZONE_DEVICE
Dan Williams4b94ffd2016-01-15 16:56:22 -08005493 /*
5494 * Honor reservation requested by the driver for this ZONE_DEVICE
Alexander Duyck966cf442018-10-26 15:07:52 -07005495 * memory. We limit the total number of pages to initialize to just
5496 * those that might contain the memory mapping. We will defer the
5497 * ZONE_DEVICE page initialization until after we have released
5498 * the hotplug lock.
Dan Williams4b94ffd2016-01-15 16:56:22 -08005499 */
Alexander Duyck966cf442018-10-26 15:07:52 -07005500 if (zone == ZONE_DEVICE) {
5501 if (!altmap)
5502 return;
5503
5504 if (start_pfn == altmap->base_pfn)
5505 start_pfn += altmap->reserve;
5506 end_pfn = altmap->base_pfn + vmem_altmap_offset(altmap);
5507 }
5508#endif
Dan Williams4b94ffd2016-01-15 16:56:22 -08005509
Greg Ungerercbe8dd42006-01-12 01:05:24 -08005510 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
Dave Hansena2f3aa022007-01-10 23:15:30 -08005511 /*
Andrew Mortonb72d0ff2016-03-15 14:55:25 -07005512 * There can be holes in boot-time mem_map[]s handed to this
5513 * function. They do not exist on hotplugged memory.
Dave Hansena2f3aa022007-01-10 23:15:30 -08005514 */
Pavel Tatashina9a9e772018-10-26 15:09:40 -07005515 if (context == MEMMAP_EARLY) {
5516 if (!early_pfn_valid(pfn))
Andrew Mortonb72d0ff2016-03-15 14:55:25 -07005517 continue;
Pavel Tatashina9a9e772018-10-26 15:09:40 -07005518 if (!early_pfn_in_nid(pfn, nid))
5519 continue;
5520 if (overlap_memmap_init(zone, &pfn))
5521 continue;
5522 if (defer_init(nid, pfn, end_pfn))
5523 break;
Dave Hansena2f3aa022007-01-10 23:15:30 -08005524 }
Mel Gormanac5d2532015-06-30 14:57:20 -07005525
Pavel Tatashind0dc12e2018-04-05 16:23:00 -07005526 page = pfn_to_page(pfn);
5527 __init_single_page(page, pfn, zone, nid);
5528 if (context == MEMMAP_HOTPLUG)
Alexander Duyckd483da52018-10-26 15:07:48 -07005529 __SetPageReserved(page);
Pavel Tatashind0dc12e2018-04-05 16:23:00 -07005530
Mel Gormanac5d2532015-06-30 14:57:20 -07005531 /*
5532 * Mark the block movable so that blocks are reserved for
5533 * movable at startup. This will force kernel allocations
5534 * to reserve their blocks rather than leaking throughout
5535 * the address space during boot when many long-lived
Mel Gorman974a7862015-11-06 16:28:34 -08005536 * kernel allocations are made.
Mel Gormanac5d2532015-06-30 14:57:20 -07005537 *
5538 * bitmap is created for zone's valid pfn range. but memmap
5539 * can be created for invalid pages (for alignment)
5540 * check here not to call set_pageblock_migratetype() against
5541 * pfn out of zone.
5542 */
5543 if (!(pfn & (pageblock_nr_pages - 1))) {
Mel Gormanac5d2532015-06-30 14:57:20 -07005544 set_pageblock_migratetype(page, MIGRATE_MOVABLE);
Michal Hocko9b6e63c2017-10-03 16:16:19 -07005545 cond_resched();
Mel Gormanac5d2532015-06-30 14:57:20 -07005546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 }
Mikhail Zaslonko2830bf62018-12-21 14:30:46 -08005548#ifdef CONFIG_SPARSEMEM
5549 /*
5550 * If the zone does not span the rest of the section then
5551 * we should at least initialize those pages. Otherwise we
5552 * could blow up on a poisoned page in some paths which depend
5553 * on full sections being initialized (e.g. memory hotplug).
5554 */
5555 while (end_pfn % PAGES_PER_SECTION) {
5556 __init_single_page(pfn_to_page(end_pfn), end_pfn, zone, nid);
5557 end_pfn++;
5558 }
5559#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560}
5561
Alexander Duyck966cf442018-10-26 15:07:52 -07005562#ifdef CONFIG_ZONE_DEVICE
5563void __ref memmap_init_zone_device(struct zone *zone,
5564 unsigned long start_pfn,
5565 unsigned long size,
5566 struct dev_pagemap *pgmap)
5567{
5568 unsigned long pfn, end_pfn = start_pfn + size;
5569 struct pglist_data *pgdat = zone->zone_pgdat;
5570 unsigned long zone_idx = zone_idx(zone);
5571 unsigned long start = jiffies;
5572 int nid = pgdat->node_id;
5573
5574 if (WARN_ON_ONCE(!pgmap || !is_dev_zone(zone)))
5575 return;
5576
5577 /*
5578 * The call to memmap_init_zone should have already taken care
5579 * of the pages reserved for the memmap, so we can just jump to
5580 * the end of that region and start processing the device pages.
5581 */
5582 if (pgmap->altmap_valid) {
5583 struct vmem_altmap *altmap = &pgmap->altmap;
5584
5585 start_pfn = altmap->base_pfn + vmem_altmap_offset(altmap);
5586 size = end_pfn - start_pfn;
5587 }
5588
5589 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
5590 struct page *page = pfn_to_page(pfn);
5591
5592 __init_single_page(page, pfn, zone_idx, nid);
5593
5594 /*
5595 * Mark page reserved as it will need to wait for onlining
5596 * phase for it to be fully associated with a zone.
5597 *
5598 * We can use the non-atomic __set_bit operation for setting
5599 * the flag as we are still initializing the pages.
5600 */
5601 __SetPageReserved(page);
5602
5603 /*
5604 * ZONE_DEVICE pages union ->lru with a ->pgmap back
5605 * pointer and hmm_data. It is a bug if a ZONE_DEVICE
5606 * page is ever freed or placed on a driver-private list.
5607 */
5608 page->pgmap = pgmap;
5609 page->hmm_data = 0;
5610
5611 /*
5612 * Mark the block movable so that blocks are reserved for
5613 * movable at startup. This will force kernel allocations
5614 * to reserve their blocks rather than leaking throughout
5615 * the address space during boot when many long-lived
5616 * kernel allocations are made.
5617 *
5618 * bitmap is created for zone's valid pfn range. but memmap
5619 * can be created for invalid pages (for alignment)
5620 * check here not to call set_pageblock_migratetype() against
5621 * pfn out of zone.
5622 *
5623 * Please note that MEMMAP_HOTPLUG path doesn't clear memmap
5624 * because this is done early in sparse_add_one_section
5625 */
5626 if (!(pfn & (pageblock_nr_pages - 1))) {
5627 set_pageblock_migratetype(page, MIGRATE_MOVABLE);
5628 cond_resched();
5629 }
5630 }
5631
5632 pr_info("%s initialised, %lu pages in %ums\n", dev_name(pgmap->dev),
5633 size, jiffies_to_msecs(jiffies - start));
5634}
5635
5636#endif
Andi Kleen1e548de2008-02-04 22:29:26 -08005637static void __meminit zone_init_free_lists(struct zone *zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638{
Mel Gorman7aeb09f2014-06-04 16:10:21 -07005639 unsigned int order, t;
Mel Gormanb2a0ac82007-10-16 01:25:48 -07005640 for_each_migratetype_order(order, t) {
5641 INIT_LIST_HEAD(&zone->free_area[order].free_list[t]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 zone->free_area[order].nr_free = 0;
5643 }
5644}
5645
Pavel Tatashindfb3ccd2018-10-26 15:09:32 -07005646void __meminit __weak memmap_init(unsigned long size, int nid,
5647 unsigned long zone, unsigned long start_pfn)
5648{
5649 memmap_init_zone(size, nid, zone, start_pfn, MEMMAP_EARLY, NULL);
5650}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651
David Rientjes7cd2b0a2014-06-23 13:22:04 -07005652static int zone_batchsize(struct zone *zone)
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07005653{
David Howells3a6be872009-05-06 16:03:03 -07005654#ifdef CONFIG_MMU
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07005655 int batch;
5656
5657 /*
5658 * The per-cpu-pages pools are set to around 1000th of the
Aaron Lud8a759b2018-08-17 15:49:14 -07005659 * size of the zone.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07005660 */
Arun KS9705bea2018-12-28 00:34:24 -08005661 batch = zone_managed_pages(zone) / 1024;
Aaron Lud8a759b2018-08-17 15:49:14 -07005662 /* But no more than a meg. */
5663 if (batch * PAGE_SIZE > 1024 * 1024)
5664 batch = (1024 * 1024) / PAGE_SIZE;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07005665 batch /= 4; /* We effectively *= 4 below */
5666 if (batch < 1)
5667 batch = 1;
5668
5669 /*
Nick Piggin0ceaacc2005-12-04 13:55:25 +11005670 * Clamp the batch to a 2^n - 1 value. Having a power
5671 * of 2 value was found to be more likely to have
5672 * suboptimal cache aliasing properties in some cases.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07005673 *
Nick Piggin0ceaacc2005-12-04 13:55:25 +11005674 * For example if 2 tasks are alternately allocating
5675 * batches of pages, one task can end up with a lot
5676 * of pages of one half of the possible page colors
5677 * and the other with pages of the other colors.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07005678 */
David Howells91552032009-05-06 16:03:02 -07005679 batch = rounddown_pow_of_two(batch + batch/2) - 1;
Seth, Rohitba56e912005-10-29 18:15:47 -07005680
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07005681 return batch;
David Howells3a6be872009-05-06 16:03:03 -07005682
5683#else
5684 /* The deferral and batching of frees should be suppressed under NOMMU
5685 * conditions.
5686 *
5687 * The problem is that NOMMU needs to be able to allocate large chunks
5688 * of contiguous memory as there's no hardware page translation to
5689 * assemble apparent contiguous memory from discontiguous pages.
5690 *
5691 * Queueing large contiguous runs of pages for batching, however,
5692 * causes the pages to actually be freed in smaller chunks. As there
5693 * can be a significant delay between the individual batches being
5694 * recycled, this leads to the once large chunks of space being
5695 * fragmented and becoming unavailable for high-order allocations.
5696 */
5697 return 0;
5698#endif
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07005699}
5700
Cody P Schafer8d7a8fa2013-07-03 15:01:31 -07005701/*
5702 * pcp->high and pcp->batch values are related and dependent on one another:
5703 * ->batch must never be higher then ->high.
5704 * The following function updates them in a safe manner without read side
5705 * locking.
5706 *
5707 * Any new users of pcp->batch and pcp->high should ensure they can cope with
5708 * those fields changing asynchronously (acording the the above rule).
5709 *
5710 * mutex_is_locked(&pcp_batch_high_lock) required when calling this function
5711 * outside of boot time (or some other assurance that no concurrent updaters
5712 * exist).
5713 */
5714static void pageset_update(struct per_cpu_pages *pcp, unsigned long high,
5715 unsigned long batch)
5716{
5717 /* start with a fail safe value for batch */
5718 pcp->batch = 1;
5719 smp_wmb();
5720
5721 /* Update high, then batch, in order */
5722 pcp->high = high;
5723 smp_wmb();
5724
5725 pcp->batch = batch;
5726}
5727
Cody P Schafer36640332013-07-03 15:01:40 -07005728/* a companion to pageset_set_high() */
Cody P Schafer4008bab2013-07-03 15:01:28 -07005729static void pageset_set_batch(struct per_cpu_pageset *p, unsigned long batch)
5730{
Cody P Schafer8d7a8fa2013-07-03 15:01:31 -07005731 pageset_update(&p->pcp, 6 * batch, max(1UL, 1 * batch));
Cody P Schafer4008bab2013-07-03 15:01:28 -07005732}
5733
Cody P Schafer88c90db2013-07-03 15:01:35 -07005734static void pageset_init(struct per_cpu_pageset *p)
Christoph Lameter2caaad42005-06-21 17:15:00 -07005735{
5736 struct per_cpu_pages *pcp;
Mel Gorman5f8dcc22009-09-21 17:03:19 -07005737 int migratetype;
Christoph Lameter2caaad42005-06-21 17:15:00 -07005738
Magnus Damm1c6fe942005-10-26 01:58:59 -07005739 memset(p, 0, sizeof(*p));
5740
Christoph Lameter3dfa5722008-02-04 22:29:19 -08005741 pcp = &p->pcp;
Mel Gorman5f8dcc22009-09-21 17:03:19 -07005742 for (migratetype = 0; migratetype < MIGRATE_PCPTYPES; migratetype++)
5743 INIT_LIST_HEAD(&pcp->lists[migratetype]);
Christoph Lameter2caaad42005-06-21 17:15:00 -07005744}
5745
Cody P Schafer88c90db2013-07-03 15:01:35 -07005746static void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
5747{
5748 pageset_init(p);
5749 pageset_set_batch(p, batch);
5750}
5751
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08005752/*
Cody P Schafer36640332013-07-03 15:01:40 -07005753 * pageset_set_high() sets the high water mark for hot per_cpu_pagelist
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08005754 * to the value high for the pageset p.
5755 */
Cody P Schafer36640332013-07-03 15:01:40 -07005756static void pageset_set_high(struct per_cpu_pageset *p,
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08005757 unsigned long high)
5758{
Cody P Schafer8d7a8fa2013-07-03 15:01:31 -07005759 unsigned long batch = max(1UL, high / 4);
5760 if ((high / 4) > (PAGE_SHIFT * 8))
5761 batch = PAGE_SHIFT * 8;
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08005762
Cody P Schafer8d7a8fa2013-07-03 15:01:31 -07005763 pageset_update(&p->pcp, high, batch);
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08005764}
5765
David Rientjes7cd2b0a2014-06-23 13:22:04 -07005766static void pageset_set_high_and_batch(struct zone *zone,
5767 struct per_cpu_pageset *pcp)
Cody P Schafer56cef2b2013-07-03 15:01:38 -07005768{
Cody P Schafer56cef2b2013-07-03 15:01:38 -07005769 if (percpu_pagelist_fraction)
Cody P Schafer36640332013-07-03 15:01:40 -07005770 pageset_set_high(pcp,
Arun KS9705bea2018-12-28 00:34:24 -08005771 (zone_managed_pages(zone) /
Cody P Schafer56cef2b2013-07-03 15:01:38 -07005772 percpu_pagelist_fraction));
5773 else
5774 pageset_set_batch(pcp, zone_batchsize(zone));
5775}
5776
Cody P Schafer169f6c12013-07-03 15:01:41 -07005777static void __meminit zone_pageset_init(struct zone *zone, int cpu)
5778{
5779 struct per_cpu_pageset *pcp = per_cpu_ptr(zone->pageset, cpu);
5780
5781 pageset_init(pcp);
5782 pageset_set_high_and_batch(zone, pcp);
5783}
5784
Michal Hocko72675e12017-09-06 16:20:24 -07005785void __meminit setup_zone_pageset(struct zone *zone)
Wu Fengguang319774e2010-05-24 14:32:49 -07005786{
5787 int cpu;
Wu Fengguang319774e2010-05-24 14:32:49 -07005788 zone->pageset = alloc_percpu(struct per_cpu_pageset);
Cody P Schafer56cef2b2013-07-03 15:01:38 -07005789 for_each_possible_cpu(cpu)
5790 zone_pageset_init(zone, cpu);
Wu Fengguang319774e2010-05-24 14:32:49 -07005791}
5792
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07005793/*
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09005794 * Allocate per cpu pagesets and initialize them.
5795 * Before this call only boot pagesets were available.
Christoph Lameter2caaad42005-06-21 17:15:00 -07005796 */
Al Viro78d99552005-12-15 09:18:25 +00005797void __init setup_per_cpu_pageset(void)
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07005798{
Mel Gormanb4911ea2016-08-04 15:31:49 -07005799 struct pglist_data *pgdat;
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09005800 struct zone *zone;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07005801
Wu Fengguang319774e2010-05-24 14:32:49 -07005802 for_each_populated_zone(zone)
5803 setup_zone_pageset(zone);
Mel Gormanb4911ea2016-08-04 15:31:49 -07005804
5805 for_each_online_pgdat(pgdat)
5806 pgdat->per_cpu_nodestats =
5807 alloc_percpu(struct per_cpu_nodestat);
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07005808}
5809
Matt Tolentinoc09b4242006-01-17 07:03:44 +01005810static __meminit void zone_pcp_init(struct zone *zone)
Dave Hansened8ece22005-10-29 18:16:50 -07005811{
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09005812 /*
5813 * per cpu subsystem is not up at this point. The following code
5814 * relies on the ability of the linker to provide the
5815 * offset of a (static) per cpu variable into the per cpu area.
5816 */
5817 zone->pageset = &boot_pageset;
Dave Hansened8ece22005-10-29 18:16:50 -07005818
Xishi Qiub38a8722013-11-12 15:07:20 -08005819 if (populated_zone(zone))
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09005820 printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%u\n",
5821 zone->name, zone->present_pages,
5822 zone_batchsize(zone));
Dave Hansened8ece22005-10-29 18:16:50 -07005823}
5824
Michal Hockodc0bbf32017-07-06 15:37:35 -07005825void __meminit init_currently_empty_zone(struct zone *zone,
Yasunori Goto718127c2006-06-23 02:03:10 -07005826 unsigned long zone_start_pfn,
Yaowei Baib171e402015-11-05 18:47:06 -08005827 unsigned long size)
Dave Hansened8ece22005-10-29 18:16:50 -07005828{
5829 struct pglist_data *pgdat = zone->zone_pgdat;
Wei Yang8f416832018-11-30 14:09:07 -08005830 int zone_idx = zone_idx(zone) + 1;
Linus Torvalds9dcb8b62016-10-26 10:15:30 -07005831
Wei Yang8f416832018-11-30 14:09:07 -08005832 if (zone_idx > pgdat->nr_zones)
5833 pgdat->nr_zones = zone_idx;
Dave Hansened8ece22005-10-29 18:16:50 -07005834
Dave Hansened8ece22005-10-29 18:16:50 -07005835 zone->zone_start_pfn = zone_start_pfn;
5836
Mel Gorman708614e2008-07-23 21:26:51 -07005837 mminit_dprintk(MMINIT_TRACE, "memmap_init",
5838 "Initialising map node %d zone %lu pfns %lu -> %lu\n",
5839 pgdat->node_id,
5840 (unsigned long)zone_idx(zone),
5841 zone_start_pfn, (zone_start_pfn + size));
5842
Andi Kleen1e548de2008-02-04 22:29:26 -08005843 zone_init_free_lists(zone);
Linus Torvalds9dcb8b62016-10-26 10:15:30 -07005844 zone->initialized = 1;
Dave Hansened8ece22005-10-29 18:16:50 -07005845}
5846
Tejun Heo0ee332c2011-12-08 10:22:09 -08005847#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Mel Gormanc7132162006-09-27 01:49:43 -07005848#ifndef CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID
Mel Gorman8a942fd2015-06-30 14:56:55 -07005849
Mel Gormanc7132162006-09-27 01:49:43 -07005850/*
5851 * Required by SPARSEMEM. Given a PFN, return what node the PFN is on.
Mel Gormanc7132162006-09-27 01:49:43 -07005852 */
Mel Gorman8a942fd2015-06-30 14:56:55 -07005853int __meminit __early_pfn_to_nid(unsigned long pfn,
5854 struct mminit_pfnnid_cache *state)
Mel Gormanc7132162006-09-27 01:49:43 -07005855{
Tejun Heoc13291a2011-07-12 10:46:30 +02005856 unsigned long start_pfn, end_pfn;
Yinghai Lue76b63f2013-09-11 14:22:17 -07005857 int nid;
Russ Anderson7c243c72013-04-29 15:07:59 -07005858
Mel Gorman8a942fd2015-06-30 14:56:55 -07005859 if (state->last_start <= pfn && pfn < state->last_end)
5860 return state->last_nid;
Mel Gormanc7132162006-09-27 01:49:43 -07005861
Yinghai Lue76b63f2013-09-11 14:22:17 -07005862 nid = memblock_search_pfn_nid(pfn, &start_pfn, &end_pfn);
5863 if (nid != -1) {
Mel Gorman8a942fd2015-06-30 14:56:55 -07005864 state->last_start = start_pfn;
5865 state->last_end = end_pfn;
5866 state->last_nid = nid;
Yinghai Lue76b63f2013-09-11 14:22:17 -07005867 }
5868
5869 return nid;
Mel Gormanc7132162006-09-27 01:49:43 -07005870}
5871#endif /* CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID */
5872
Mel Gormanc7132162006-09-27 01:49:43 -07005873/**
Santosh Shilimkar67828322014-01-21 15:50:25 -08005874 * free_bootmem_with_active_regions - Call memblock_free_early_nid for each active range
Randy Dunlap88ca3b92006-10-04 02:15:25 -07005875 * @nid: The node to free memory on. If MAX_NUMNODES, all nodes are freed.
Santosh Shilimkar67828322014-01-21 15:50:25 -08005876 * @max_low_pfn: The highest PFN that will be passed to memblock_free_early_nid
Mel Gormanc7132162006-09-27 01:49:43 -07005877 *
Zhang Zhen7d018172014-06-04 16:10:53 -07005878 * If an architecture guarantees that all ranges registered contain no holes
5879 * and may be freed, this this function may be used instead of calling
5880 * memblock_free_early_nid() manually.
Mel Gormanc7132162006-09-27 01:49:43 -07005881 */
Tejun Heoc13291a2011-07-12 10:46:30 +02005882void __init free_bootmem_with_active_regions(int nid, unsigned long max_low_pfn)
Mel Gormanc7132162006-09-27 01:49:43 -07005883{
Tejun Heoc13291a2011-07-12 10:46:30 +02005884 unsigned long start_pfn, end_pfn;
5885 int i, this_nid;
Mel Gormanc7132162006-09-27 01:49:43 -07005886
Tejun Heoc13291a2011-07-12 10:46:30 +02005887 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, &this_nid) {
5888 start_pfn = min(start_pfn, max_low_pfn);
5889 end_pfn = min(end_pfn, max_low_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07005890
Tejun Heoc13291a2011-07-12 10:46:30 +02005891 if (start_pfn < end_pfn)
Santosh Shilimkar67828322014-01-21 15:50:25 -08005892 memblock_free_early_nid(PFN_PHYS(start_pfn),
5893 (end_pfn - start_pfn) << PAGE_SHIFT,
5894 this_nid);
Mel Gormanc7132162006-09-27 01:49:43 -07005895 }
5896}
5897
5898/**
5899 * sparse_memory_present_with_active_regions - Call memory_present for each active range
Randy Dunlap88ca3b92006-10-04 02:15:25 -07005900 * @nid: The node to call memory_present for. If MAX_NUMNODES, all nodes will be used.
Mel Gormanc7132162006-09-27 01:49:43 -07005901 *
Zhang Zhen7d018172014-06-04 16:10:53 -07005902 * If an architecture guarantees that all ranges registered contain no holes and may
5903 * be freed, this function may be used instead of calling memory_present() manually.
Mel Gormanc7132162006-09-27 01:49:43 -07005904 */
5905void __init sparse_memory_present_with_active_regions(int nid)
5906{
Tejun Heoc13291a2011-07-12 10:46:30 +02005907 unsigned long start_pfn, end_pfn;
5908 int i, this_nid;
Mel Gormanc7132162006-09-27 01:49:43 -07005909
Tejun Heoc13291a2011-07-12 10:46:30 +02005910 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, &this_nid)
5911 memory_present(this_nid, start_pfn, end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07005912}
5913
5914/**
5915 * get_pfn_range_for_nid - Return the start and end page frames for a node
Randy Dunlap88ca3b92006-10-04 02:15:25 -07005916 * @nid: The nid to return the range for. If MAX_NUMNODES, the min and max PFN are returned.
5917 * @start_pfn: Passed by reference. On return, it will have the node start_pfn.
5918 * @end_pfn: Passed by reference. On return, it will have the node end_pfn.
Mel Gormanc7132162006-09-27 01:49:43 -07005919 *
5920 * It returns the start and end page frame of a node based on information
Zhang Zhen7d018172014-06-04 16:10:53 -07005921 * provided by memblock_set_node(). If called for a node
Mel Gormanc7132162006-09-27 01:49:43 -07005922 * with no available memory, a warning is printed and the start and end
Randy Dunlap88ca3b92006-10-04 02:15:25 -07005923 * PFNs will be 0.
Mel Gormanc7132162006-09-27 01:49:43 -07005924 */
Yasunori Gotoa3142c82007-05-08 00:23:07 -07005925void __meminit get_pfn_range_for_nid(unsigned int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07005926 unsigned long *start_pfn, unsigned long *end_pfn)
5927{
Tejun Heoc13291a2011-07-12 10:46:30 +02005928 unsigned long this_start_pfn, this_end_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07005929 int i;
Tejun Heoc13291a2011-07-12 10:46:30 +02005930
Mel Gormanc7132162006-09-27 01:49:43 -07005931 *start_pfn = -1UL;
5932 *end_pfn = 0;
5933
Tejun Heoc13291a2011-07-12 10:46:30 +02005934 for_each_mem_pfn_range(i, nid, &this_start_pfn, &this_end_pfn, NULL) {
5935 *start_pfn = min(*start_pfn, this_start_pfn);
5936 *end_pfn = max(*end_pfn, this_end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07005937 }
5938
Christoph Lameter633c0662007-10-16 01:25:37 -07005939 if (*start_pfn == -1UL)
Mel Gormanc7132162006-09-27 01:49:43 -07005940 *start_pfn = 0;
Mel Gormanc7132162006-09-27 01:49:43 -07005941}
5942
5943/*
Mel Gorman2a1e2742007-07-17 04:03:12 -07005944 * This finds a zone that can be used for ZONE_MOVABLE pages. The
5945 * assumption is made that zones within a node are ordered in monotonic
5946 * increasing memory addresses so that the "highest" populated zone is used
5947 */
Adrian Bunkb69a7282008-07-23 21:28:12 -07005948static void __init find_usable_zone_for_movable(void)
Mel Gorman2a1e2742007-07-17 04:03:12 -07005949{
5950 int zone_index;
5951 for (zone_index = MAX_NR_ZONES - 1; zone_index >= 0; zone_index--) {
5952 if (zone_index == ZONE_MOVABLE)
5953 continue;
5954
5955 if (arch_zone_highest_possible_pfn[zone_index] >
5956 arch_zone_lowest_possible_pfn[zone_index])
5957 break;
5958 }
5959
5960 VM_BUG_ON(zone_index == -1);
5961 movable_zone = zone_index;
5962}
5963
5964/*
5965 * The zone ranges provided by the architecture do not include ZONE_MOVABLE
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005966 * because it is sized independent of architecture. Unlike the other zones,
Mel Gorman2a1e2742007-07-17 04:03:12 -07005967 * the starting point for ZONE_MOVABLE is not fixed. It may be different
5968 * in each node depending on the size of each node and how evenly kernelcore
5969 * is distributed. This helper function adjusts the zone ranges
5970 * provided by the architecture for a given node by using the end of the
5971 * highest usable zone for ZONE_MOVABLE. This preserves the assumption that
5972 * zones within a node are in order of monotonic increases memory addresses
5973 */
Adrian Bunkb69a7282008-07-23 21:28:12 -07005974static void __meminit adjust_zone_range_for_zone_movable(int nid,
Mel Gorman2a1e2742007-07-17 04:03:12 -07005975 unsigned long zone_type,
5976 unsigned long node_start_pfn,
5977 unsigned long node_end_pfn,
5978 unsigned long *zone_start_pfn,
5979 unsigned long *zone_end_pfn)
5980{
5981 /* Only adjust if ZONE_MOVABLE is on this node */
5982 if (zone_movable_pfn[nid]) {
5983 /* Size ZONE_MOVABLE */
5984 if (zone_type == ZONE_MOVABLE) {
5985 *zone_start_pfn = zone_movable_pfn[nid];
5986 *zone_end_pfn = min(node_end_pfn,
5987 arch_zone_highest_possible_pfn[movable_zone]);
5988
Xishi Qiue506b992016-10-07 16:58:06 -07005989 /* Adjust for ZONE_MOVABLE starting within this range */
5990 } else if (!mirrored_kernelcore &&
5991 *zone_start_pfn < zone_movable_pfn[nid] &&
5992 *zone_end_pfn > zone_movable_pfn[nid]) {
5993 *zone_end_pfn = zone_movable_pfn[nid];
5994
Mel Gorman2a1e2742007-07-17 04:03:12 -07005995 /* Check if this whole range is within ZONE_MOVABLE */
5996 } else if (*zone_start_pfn >= zone_movable_pfn[nid])
5997 *zone_start_pfn = *zone_end_pfn;
5998 }
5999}
6000
6001/*
Mel Gormanc7132162006-09-27 01:49:43 -07006002 * Return the number of pages a zone spans in a node, including holes
6003 * present_pages = zone_spanned_pages_in_node() - zone_absent_pages_in_node()
6004 */
Paul Mundt6ea6e682007-07-15 23:38:20 -07006005static unsigned long __meminit zone_spanned_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07006006 unsigned long zone_type,
Zhang Yanfei7960aed2013-07-08 15:59:52 -07006007 unsigned long node_start_pfn,
6008 unsigned long node_end_pfn,
Taku Izumid91749c2016-03-15 14:55:18 -07006009 unsigned long *zone_start_pfn,
6010 unsigned long *zone_end_pfn,
Mel Gormanc7132162006-09-27 01:49:43 -07006011 unsigned long *ignored)
6012{
Xishi Qiub5685e92015-09-08 15:04:16 -07006013 /* When hotadd a new node from cpu_up(), the node should be empty */
Xishi Qiuf9126ab2015-08-14 15:35:16 -07006014 if (!node_start_pfn && !node_end_pfn)
6015 return 0;
6016
Zhang Yanfei7960aed2013-07-08 15:59:52 -07006017 /* Get the start and end of the zone */
Taku Izumid91749c2016-03-15 14:55:18 -07006018 *zone_start_pfn = arch_zone_lowest_possible_pfn[zone_type];
6019 *zone_end_pfn = arch_zone_highest_possible_pfn[zone_type];
Mel Gorman2a1e2742007-07-17 04:03:12 -07006020 adjust_zone_range_for_zone_movable(nid, zone_type,
6021 node_start_pfn, node_end_pfn,
Taku Izumid91749c2016-03-15 14:55:18 -07006022 zone_start_pfn, zone_end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07006023
6024 /* Check that this node has pages within the zone's required range */
Taku Izumid91749c2016-03-15 14:55:18 -07006025 if (*zone_end_pfn < node_start_pfn || *zone_start_pfn > node_end_pfn)
Mel Gormanc7132162006-09-27 01:49:43 -07006026 return 0;
6027
6028 /* Move the zone boundaries inside the node if necessary */
Taku Izumid91749c2016-03-15 14:55:18 -07006029 *zone_end_pfn = min(*zone_end_pfn, node_end_pfn);
6030 *zone_start_pfn = max(*zone_start_pfn, node_start_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07006031
6032 /* Return the spanned pages */
Taku Izumid91749c2016-03-15 14:55:18 -07006033 return *zone_end_pfn - *zone_start_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07006034}
6035
6036/*
6037 * Return the number of holes in a range on a node. If nid is MAX_NUMNODES,
Randy Dunlap88ca3b92006-10-04 02:15:25 -07006038 * then all holes in the requested range will be accounted for.
Mel Gormanc7132162006-09-27 01:49:43 -07006039 */
Yinghai Lu32996252009-12-15 17:59:02 -08006040unsigned long __meminit __absent_pages_in_range(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07006041 unsigned long range_start_pfn,
6042 unsigned long range_end_pfn)
6043{
Tejun Heo96e907d2011-07-12 10:46:29 +02006044 unsigned long nr_absent = range_end_pfn - range_start_pfn;
6045 unsigned long start_pfn, end_pfn;
6046 int i;
Mel Gormanc7132162006-09-27 01:49:43 -07006047
Tejun Heo96e907d2011-07-12 10:46:29 +02006048 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
6049 start_pfn = clamp(start_pfn, range_start_pfn, range_end_pfn);
6050 end_pfn = clamp(end_pfn, range_start_pfn, range_end_pfn);
6051 nr_absent -= end_pfn - start_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07006052 }
Tejun Heo96e907d2011-07-12 10:46:29 +02006053 return nr_absent;
Mel Gormanc7132162006-09-27 01:49:43 -07006054}
6055
6056/**
6057 * absent_pages_in_range - Return number of page frames in holes within a range
6058 * @start_pfn: The start PFN to start searching for holes
6059 * @end_pfn: The end PFN to stop searching for holes
6060 *
Randy Dunlap88ca3b92006-10-04 02:15:25 -07006061 * It returns the number of pages frames in memory holes within a range.
Mel Gormanc7132162006-09-27 01:49:43 -07006062 */
6063unsigned long __init absent_pages_in_range(unsigned long start_pfn,
6064 unsigned long end_pfn)
6065{
6066 return __absent_pages_in_range(MAX_NUMNODES, start_pfn, end_pfn);
6067}
6068
6069/* Return the number of page frames in holes in a zone on a node */
Paul Mundt6ea6e682007-07-15 23:38:20 -07006070static unsigned long __meminit zone_absent_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07006071 unsigned long zone_type,
Zhang Yanfei7960aed2013-07-08 15:59:52 -07006072 unsigned long node_start_pfn,
6073 unsigned long node_end_pfn,
Mel Gormanc7132162006-09-27 01:49:43 -07006074 unsigned long *ignored)
6075{
Tejun Heo96e907d2011-07-12 10:46:29 +02006076 unsigned long zone_low = arch_zone_lowest_possible_pfn[zone_type];
6077 unsigned long zone_high = arch_zone_highest_possible_pfn[zone_type];
Mel Gorman9c7cd682006-09-27 01:49:58 -07006078 unsigned long zone_start_pfn, zone_end_pfn;
Taku Izumi342332e2016-03-15 14:55:22 -07006079 unsigned long nr_absent;
Mel Gorman9c7cd682006-09-27 01:49:58 -07006080
Xishi Qiub5685e92015-09-08 15:04:16 -07006081 /* When hotadd a new node from cpu_up(), the node should be empty */
Xishi Qiuf9126ab2015-08-14 15:35:16 -07006082 if (!node_start_pfn && !node_end_pfn)
6083 return 0;
6084
Tejun Heo96e907d2011-07-12 10:46:29 +02006085 zone_start_pfn = clamp(node_start_pfn, zone_low, zone_high);
6086 zone_end_pfn = clamp(node_end_pfn, zone_low, zone_high);
Mel Gorman9c7cd682006-09-27 01:49:58 -07006087
Mel Gorman2a1e2742007-07-17 04:03:12 -07006088 adjust_zone_range_for_zone_movable(nid, zone_type,
6089 node_start_pfn, node_end_pfn,
6090 &zone_start_pfn, &zone_end_pfn);
Taku Izumi342332e2016-03-15 14:55:22 -07006091 nr_absent = __absent_pages_in_range(nid, zone_start_pfn, zone_end_pfn);
6092
6093 /*
6094 * ZONE_MOVABLE handling.
6095 * Treat pages to be ZONE_MOVABLE in ZONE_NORMAL as absent pages
6096 * and vice versa.
6097 */
Xishi Qiue506b992016-10-07 16:58:06 -07006098 if (mirrored_kernelcore && zone_movable_pfn[nid]) {
6099 unsigned long start_pfn, end_pfn;
6100 struct memblock_region *r;
Taku Izumi342332e2016-03-15 14:55:22 -07006101
Xishi Qiue506b992016-10-07 16:58:06 -07006102 for_each_memblock(memory, r) {
6103 start_pfn = clamp(memblock_region_memory_base_pfn(r),
6104 zone_start_pfn, zone_end_pfn);
6105 end_pfn = clamp(memblock_region_memory_end_pfn(r),
6106 zone_start_pfn, zone_end_pfn);
Taku Izumi342332e2016-03-15 14:55:22 -07006107
Xishi Qiue506b992016-10-07 16:58:06 -07006108 if (zone_type == ZONE_MOVABLE &&
6109 memblock_is_mirror(r))
6110 nr_absent += end_pfn - start_pfn;
Taku Izumi342332e2016-03-15 14:55:22 -07006111
Xishi Qiue506b992016-10-07 16:58:06 -07006112 if (zone_type == ZONE_NORMAL &&
6113 !memblock_is_mirror(r))
6114 nr_absent += end_pfn - start_pfn;
Taku Izumi342332e2016-03-15 14:55:22 -07006115 }
6116 }
6117
6118 return nr_absent;
Mel Gormanc7132162006-09-27 01:49:43 -07006119}
Mel Gorman0e0b8642006-09-27 01:49:56 -07006120
Tejun Heo0ee332c2011-12-08 10:22:09 -08006121#else /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
Paul Mundt6ea6e682007-07-15 23:38:20 -07006122static inline unsigned long __meminit zone_spanned_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07006123 unsigned long zone_type,
Zhang Yanfei7960aed2013-07-08 15:59:52 -07006124 unsigned long node_start_pfn,
6125 unsigned long node_end_pfn,
Taku Izumid91749c2016-03-15 14:55:18 -07006126 unsigned long *zone_start_pfn,
6127 unsigned long *zone_end_pfn,
Mel Gormanc7132162006-09-27 01:49:43 -07006128 unsigned long *zones_size)
6129{
Taku Izumid91749c2016-03-15 14:55:18 -07006130 unsigned int zone;
6131
6132 *zone_start_pfn = node_start_pfn;
6133 for (zone = 0; zone < zone_type; zone++)
6134 *zone_start_pfn += zones_size[zone];
6135
6136 *zone_end_pfn = *zone_start_pfn + zones_size[zone_type];
6137
Mel Gormanc7132162006-09-27 01:49:43 -07006138 return zones_size[zone_type];
6139}
6140
Paul Mundt6ea6e682007-07-15 23:38:20 -07006141static inline unsigned long __meminit zone_absent_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07006142 unsigned long zone_type,
Zhang Yanfei7960aed2013-07-08 15:59:52 -07006143 unsigned long node_start_pfn,
6144 unsigned long node_end_pfn,
Mel Gormanc7132162006-09-27 01:49:43 -07006145 unsigned long *zholes_size)
6146{
6147 if (!zholes_size)
6148 return 0;
6149
6150 return zholes_size[zone_type];
6151}
Yinghai Lu20e69262013-03-01 14:51:27 -08006152
Tejun Heo0ee332c2011-12-08 10:22:09 -08006153#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
Mel Gormanc7132162006-09-27 01:49:43 -07006154
Yasunori Gotoa3142c82007-05-08 00:23:07 -07006155static void __meminit calculate_node_totalpages(struct pglist_data *pgdat,
Zhang Yanfei7960aed2013-07-08 15:59:52 -07006156 unsigned long node_start_pfn,
6157 unsigned long node_end_pfn,
6158 unsigned long *zones_size,
6159 unsigned long *zholes_size)
Mel Gormanc7132162006-09-27 01:49:43 -07006160{
Gu Zhengfebd5942015-06-24 16:57:02 -07006161 unsigned long realtotalpages = 0, totalpages = 0;
Mel Gormanc7132162006-09-27 01:49:43 -07006162 enum zone_type i;
6163
Gu Zhengfebd5942015-06-24 16:57:02 -07006164 for (i = 0; i < MAX_NR_ZONES; i++) {
6165 struct zone *zone = pgdat->node_zones + i;
Taku Izumid91749c2016-03-15 14:55:18 -07006166 unsigned long zone_start_pfn, zone_end_pfn;
Gu Zhengfebd5942015-06-24 16:57:02 -07006167 unsigned long size, real_size;
Mel Gormanc7132162006-09-27 01:49:43 -07006168
Gu Zhengfebd5942015-06-24 16:57:02 -07006169 size = zone_spanned_pages_in_node(pgdat->node_id, i,
6170 node_start_pfn,
6171 node_end_pfn,
Taku Izumid91749c2016-03-15 14:55:18 -07006172 &zone_start_pfn,
6173 &zone_end_pfn,
Gu Zhengfebd5942015-06-24 16:57:02 -07006174 zones_size);
6175 real_size = size - zone_absent_pages_in_node(pgdat->node_id, i,
Zhang Yanfei7960aed2013-07-08 15:59:52 -07006176 node_start_pfn, node_end_pfn,
6177 zholes_size);
Taku Izumid91749c2016-03-15 14:55:18 -07006178 if (size)
6179 zone->zone_start_pfn = zone_start_pfn;
6180 else
6181 zone->zone_start_pfn = 0;
Gu Zhengfebd5942015-06-24 16:57:02 -07006182 zone->spanned_pages = size;
6183 zone->present_pages = real_size;
6184
6185 totalpages += size;
6186 realtotalpages += real_size;
6187 }
6188
6189 pgdat->node_spanned_pages = totalpages;
Mel Gormanc7132162006-09-27 01:49:43 -07006190 pgdat->node_present_pages = realtotalpages;
6191 printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id,
6192 realtotalpages);
6193}
6194
Mel Gorman835c1342007-10-16 01:25:47 -07006195#ifndef CONFIG_SPARSEMEM
6196/*
6197 * Calculate the size of the zone->blockflags rounded to an unsigned long
Mel Gormand9c23402007-10-16 01:26:01 -07006198 * Start by making sure zonesize is a multiple of pageblock_order by rounding
6199 * up. Then use 1 NR_PAGEBLOCK_BITS worth of bits per pageblock, finally
Mel Gorman835c1342007-10-16 01:25:47 -07006200 * round what is now in bits to nearest long in bits, then return it in
6201 * bytes.
6202 */
Linus Torvalds7c455122013-02-18 09:58:02 -08006203static unsigned long __init usemap_size(unsigned long zone_start_pfn, unsigned long zonesize)
Mel Gorman835c1342007-10-16 01:25:47 -07006204{
6205 unsigned long usemapsize;
6206
Linus Torvalds7c455122013-02-18 09:58:02 -08006207 zonesize += zone_start_pfn & (pageblock_nr_pages-1);
Mel Gormand9c23402007-10-16 01:26:01 -07006208 usemapsize = roundup(zonesize, pageblock_nr_pages);
6209 usemapsize = usemapsize >> pageblock_order;
Mel Gorman835c1342007-10-16 01:25:47 -07006210 usemapsize *= NR_PAGEBLOCK_BITS;
6211 usemapsize = roundup(usemapsize, 8 * sizeof(unsigned long));
6212
6213 return usemapsize / 8;
6214}
6215
Pavel Tatashin7cc2a952018-08-21 21:53:36 -07006216static void __ref setup_usemap(struct pglist_data *pgdat,
Linus Torvalds7c455122013-02-18 09:58:02 -08006217 struct zone *zone,
6218 unsigned long zone_start_pfn,
6219 unsigned long zonesize)
Mel Gorman835c1342007-10-16 01:25:47 -07006220{
Linus Torvalds7c455122013-02-18 09:58:02 -08006221 unsigned long usemapsize = usemap_size(zone_start_pfn, zonesize);
Mel Gorman835c1342007-10-16 01:25:47 -07006222 zone->pageblock_flags = NULL;
Julia Lawall58a01a42009-01-06 14:39:28 -08006223 if (usemapsize)
Santosh Shilimkar67828322014-01-21 15:50:25 -08006224 zone->pageblock_flags =
Mike Rapoporteb31d552018-10-30 15:08:04 -07006225 memblock_alloc_node_nopanic(usemapsize,
Santosh Shilimkar67828322014-01-21 15:50:25 -08006226 pgdat->node_id);
Mel Gorman835c1342007-10-16 01:25:47 -07006227}
6228#else
Linus Torvalds7c455122013-02-18 09:58:02 -08006229static inline void setup_usemap(struct pglist_data *pgdat, struct zone *zone,
6230 unsigned long zone_start_pfn, unsigned long zonesize) {}
Mel Gorman835c1342007-10-16 01:25:47 -07006231#endif /* CONFIG_SPARSEMEM */
6232
Mel Gormand9c23402007-10-16 01:26:01 -07006233#ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
Mel Gormanba72cb82007-11-28 16:21:13 -08006234
Mel Gormand9c23402007-10-16 01:26:01 -07006235/* Initialise the number of pages represented by NR_PAGEBLOCK_BITS */
Oscar Salvador03e85f92018-08-21 21:53:43 -07006236void __init set_pageblock_order(void)
Mel Gormand9c23402007-10-16 01:26:01 -07006237{
Andrew Morton955c1cd2012-05-29 15:06:31 -07006238 unsigned int order;
6239
Mel Gormand9c23402007-10-16 01:26:01 -07006240 /* Check that pageblock_nr_pages has not already been setup */
6241 if (pageblock_order)
6242 return;
6243
Andrew Morton955c1cd2012-05-29 15:06:31 -07006244 if (HPAGE_SHIFT > PAGE_SHIFT)
6245 order = HUGETLB_PAGE_ORDER;
6246 else
6247 order = MAX_ORDER - 1;
6248
Mel Gormand9c23402007-10-16 01:26:01 -07006249 /*
6250 * Assume the largest contiguous order of interest is a huge page.
Andrew Morton955c1cd2012-05-29 15:06:31 -07006251 * This value may be variable depending on boot parameters on IA64 and
6252 * powerpc.
Mel Gormand9c23402007-10-16 01:26:01 -07006253 */
6254 pageblock_order = order;
6255}
6256#else /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
6257
Mel Gormanba72cb82007-11-28 16:21:13 -08006258/*
6259 * When CONFIG_HUGETLB_PAGE_SIZE_VARIABLE is not set, set_pageblock_order()
Andrew Morton955c1cd2012-05-29 15:06:31 -07006260 * is unused as pageblock_order is set at compile-time. See
6261 * include/linux/pageblock-flags.h for the values of pageblock_order based on
6262 * the kernel config
Mel Gormanba72cb82007-11-28 16:21:13 -08006263 */
Oscar Salvador03e85f92018-08-21 21:53:43 -07006264void __init set_pageblock_order(void)
Mel Gormanba72cb82007-11-28 16:21:13 -08006265{
Mel Gormanba72cb82007-11-28 16:21:13 -08006266}
Mel Gormand9c23402007-10-16 01:26:01 -07006267
6268#endif /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
6269
Oscar Salvador03e85f92018-08-21 21:53:43 -07006270static unsigned long __init calc_memmap_size(unsigned long spanned_pages,
Pavel Tatashin7cc2a952018-08-21 21:53:36 -07006271 unsigned long present_pages)
Jiang Liu01cefae2012-12-12 13:52:19 -08006272{
6273 unsigned long pages = spanned_pages;
6274
6275 /*
6276 * Provide a more accurate estimation if there are holes within
6277 * the zone and SPARSEMEM is in use. If there are holes within the
6278 * zone, each populated memory region may cost us one or two extra
6279 * memmap pages due to alignment because memmap pages for each
Masahiro Yamada89d790a2017-02-27 14:29:01 -08006280 * populated regions may not be naturally aligned on page boundary.
Jiang Liu01cefae2012-12-12 13:52:19 -08006281 * So the (present_pages >> 4) heuristic is a tradeoff for that.
6282 */
6283 if (spanned_pages > present_pages + (present_pages >> 4) &&
6284 IS_ENABLED(CONFIG_SPARSEMEM))
6285 pages = present_pages;
6286
6287 return PAGE_ALIGN(pages * sizeof(struct page)) >> PAGE_SHIFT;
6288}
6289
Oscar Salvadorace1db32018-08-21 21:53:29 -07006290#ifdef CONFIG_TRANSPARENT_HUGEPAGE
6291static void pgdat_init_split_queue(struct pglist_data *pgdat)
6292{
6293 spin_lock_init(&pgdat->split_queue_lock);
6294 INIT_LIST_HEAD(&pgdat->split_queue);
6295 pgdat->split_queue_len = 0;
6296}
6297#else
6298static void pgdat_init_split_queue(struct pglist_data *pgdat) {}
6299#endif
6300
6301#ifdef CONFIG_COMPACTION
6302static void pgdat_init_kcompactd(struct pglist_data *pgdat)
6303{
6304 init_waitqueue_head(&pgdat->kcompactd_wait);
6305}
6306#else
6307static void pgdat_init_kcompactd(struct pglist_data *pgdat) {}
6308#endif
6309
Oscar Salvador03e85f92018-08-21 21:53:43 -07006310static void __meminit pgdat_init_internals(struct pglist_data *pgdat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311{
Dave Hansen208d54e2005-10-29 18:16:52 -07006312 pgdat_resize_init(pgdat);
Oscar Salvadorace1db32018-08-21 21:53:29 -07006313
Oscar Salvadorace1db32018-08-21 21:53:29 -07006314 pgdat_init_split_queue(pgdat);
6315 pgdat_init_kcompactd(pgdat);
6316
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 init_waitqueue_head(&pgdat->kswapd_wait);
Mel Gorman55150612012-07-31 16:44:35 -07006318 init_waitqueue_head(&pgdat->pfmemalloc_wait);
Oscar Salvadorace1db32018-08-21 21:53:29 -07006319
Joonsoo Kimeefa864b2014-12-12 16:55:46 -08006320 pgdat_page_ext_init(pgdat);
Mel Gormana52633d2016-07-28 15:45:28 -07006321 spin_lock_init(&pgdat->lru_lock);
Mel Gormana9dd0a82016-07-28 15:46:02 -07006322 lruvec_init(node_lruvec(pgdat));
Oscar Salvador03e85f92018-08-21 21:53:43 -07006323}
Michal Nazarewicz5f63b722012-01-11 15:16:11 +01006324
Oscar Salvador03e85f92018-08-21 21:53:43 -07006325static void __meminit zone_init_internals(struct zone *zone, enum zone_type idx, int nid,
6326 unsigned long remaining_pages)
6327{
Arun KS9705bea2018-12-28 00:34:24 -08006328 atomic_long_set(&zone->managed_pages, remaining_pages);
Oscar Salvador03e85f92018-08-21 21:53:43 -07006329 zone_set_nid(zone, nid);
6330 zone->name = zone_names[idx];
6331 zone->zone_pgdat = NODE_DATA(nid);
6332 spin_lock_init(&zone->lock);
6333 zone_seqlock_init(zone);
6334 zone_pcp_init(zone);
6335}
6336
6337/*
6338 * Set up the zone data structures
6339 * - init pgdat internals
6340 * - init all zones belonging to this node
6341 *
6342 * NOTE: this function is only called during memory hotplug
6343 */
6344#ifdef CONFIG_MEMORY_HOTPLUG
6345void __ref free_area_init_core_hotplug(int nid)
6346{
6347 enum zone_type z;
6348 pg_data_t *pgdat = NODE_DATA(nid);
6349
6350 pgdat_init_internals(pgdat);
6351 for (z = 0; z < MAX_NR_ZONES; z++)
6352 zone_init_internals(&pgdat->node_zones[z], z, nid, 0);
6353}
6354#endif
6355
6356/*
6357 * Set up the zone data structures:
6358 * - mark all pages reserved
6359 * - mark all memory queues empty
6360 * - clear the memory bitmaps
6361 *
6362 * NOTE: pgdat should get zeroed by caller.
6363 * NOTE: this function is only called during early init.
6364 */
6365static void __init free_area_init_core(struct pglist_data *pgdat)
6366{
6367 enum zone_type j;
6368 int nid = pgdat->node_id;
6369
6370 pgdat_init_internals(pgdat);
Johannes Weiner385386c2017-07-06 15:40:43 -07006371 pgdat->per_cpu_nodestats = &boot_nodestats;
6372
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373 for (j = 0; j < MAX_NR_ZONES; j++) {
6374 struct zone *zone = pgdat->node_zones + j;
Wei Yange6943852018-06-07 17:06:04 -07006375 unsigned long size, freesize, memmap_pages;
Taku Izumid91749c2016-03-15 14:55:18 -07006376 unsigned long zone_start_pfn = zone->zone_start_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377
Gu Zhengfebd5942015-06-24 16:57:02 -07006378 size = zone->spanned_pages;
Wei Yange6943852018-06-07 17:06:04 -07006379 freesize = zone->present_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380
Mel Gorman0e0b8642006-09-27 01:49:56 -07006381 /*
Jiang Liu9feedc92012-12-12 13:52:12 -08006382 * Adjust freesize so that it accounts for how much memory
Mel Gorman0e0b8642006-09-27 01:49:56 -07006383 * is used by this zone for memmap. This affects the watermark
6384 * and per-cpu initialisations
6385 */
Wei Yange6943852018-06-07 17:06:04 -07006386 memmap_pages = calc_memmap_size(size, freesize);
Zhong Hongboba914f42014-12-12 16:56:21 -08006387 if (!is_highmem_idx(j)) {
6388 if (freesize >= memmap_pages) {
6389 freesize -= memmap_pages;
6390 if (memmap_pages)
6391 printk(KERN_DEBUG
6392 " %s zone: %lu pages used for memmap\n",
6393 zone_names[j], memmap_pages);
6394 } else
Joe Perches11705322016-03-17 14:19:50 -07006395 pr_warn(" %s zone: %lu pages exceeds freesize %lu\n",
Zhong Hongboba914f42014-12-12 16:56:21 -08006396 zone_names[j], memmap_pages, freesize);
6397 }
Mel Gorman0e0b8642006-09-27 01:49:56 -07006398
Christoph Lameter62672762007-02-10 01:43:07 -08006399 /* Account for reserved pages */
Jiang Liu9feedc92012-12-12 13:52:12 -08006400 if (j == 0 && freesize > dma_reserve) {
6401 freesize -= dma_reserve;
Yinghai Lud903ef92008-10-18 20:27:06 -07006402 printk(KERN_DEBUG " %s zone: %lu pages reserved\n",
Christoph Lameter62672762007-02-10 01:43:07 -08006403 zone_names[0], dma_reserve);
Mel Gorman0e0b8642006-09-27 01:49:56 -07006404 }
6405
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07006406 if (!is_highmem_idx(j))
Jiang Liu9feedc92012-12-12 13:52:12 -08006407 nr_kernel_pages += freesize;
Jiang Liu01cefae2012-12-12 13:52:19 -08006408 /* Charge for highmem memmap if there are enough kernel pages */
6409 else if (nr_kernel_pages > memmap_pages * 2)
6410 nr_kernel_pages -= memmap_pages;
Jiang Liu9feedc92012-12-12 13:52:12 -08006411 nr_all_pages += freesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412
Jiang Liu9feedc92012-12-12 13:52:12 -08006413 /*
6414 * Set an approximate value for lowmem here, it will be adjusted
6415 * when the bootmem allocator frees pages into the buddy system.
6416 * And all highmem pages will be managed by the buddy system.
6417 */
Oscar Salvador03e85f92018-08-21 21:53:43 -07006418 zone_init_internals(zone, j, nid, freesize);
Johannes Weiner81c0a2b2013-09-11 14:20:47 -07006419
Joonsoo Kimd883c6c2018-05-23 10:18:21 +09006420 if (!size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421 continue;
6422
Andrew Morton955c1cd2012-05-29 15:06:31 -07006423 set_pageblock_order();
Joonsoo Kimd883c6c2018-05-23 10:18:21 +09006424 setup_usemap(pgdat, zone, zone_start_pfn, size);
6425 init_currently_empty_zone(zone, zone_start_pfn, size);
Heiko Carstens76cdd582008-05-14 16:05:52 -07006426 memmap_init(size, nid, j, zone_start_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427 }
6428}
6429
Oscar Salvador0cd842f2017-11-15 17:39:18 -08006430#ifdef CONFIG_FLAT_NODE_MEM_MAP
Fabian Frederickbd721ea2016-08-02 14:03:33 -07006431static void __ref alloc_node_mem_map(struct pglist_data *pgdat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006432{
Tony Luckb0aeba72015-11-10 10:09:47 -08006433 unsigned long __maybe_unused start = 0;
Laura Abbotta1c34a32015-11-05 18:48:46 -08006434 unsigned long __maybe_unused offset = 0;
6435
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436 /* Skip empty nodes */
6437 if (!pgdat->node_spanned_pages)
6438 return;
6439
Tony Luckb0aeba72015-11-10 10:09:47 -08006440 start = pgdat->node_start_pfn & ~(MAX_ORDER_NR_PAGES - 1);
6441 offset = pgdat->node_start_pfn - start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006442 /* ia64 gets its own node_mem_map, before this, without bootmem */
6443 if (!pgdat->node_mem_map) {
Tony Luckb0aeba72015-11-10 10:09:47 -08006444 unsigned long size, end;
Andy Whitcroftd41dee32005-06-23 00:07:54 -07006445 struct page *map;
6446
Bob Piccoe984bb42006-05-20 15:00:31 -07006447 /*
6448 * The zone's endpoints aren't required to be MAX_ORDER
6449 * aligned but the node_mem_map endpoints must be in order
6450 * for the buddy allocator to function correctly.
6451 */
Cody P Schafer108bcc92013-02-22 16:35:23 -08006452 end = pgdat_end_pfn(pgdat);
Bob Piccoe984bb42006-05-20 15:00:31 -07006453 end = ALIGN(end, MAX_ORDER_NR_PAGES);
6454 size = (end - start) * sizeof(struct page);
Mike Rapoporteb31d552018-10-30 15:08:04 -07006455 map = memblock_alloc_node_nopanic(size, pgdat->node_id);
Laura Abbotta1c34a32015-11-05 18:48:46 -08006456 pgdat->node_mem_map = map + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457 }
Oscar Salvador0cd842f2017-11-15 17:39:18 -08006458 pr_debug("%s: node %d, pgdat %08lx, node_mem_map %08lx\n",
6459 __func__, pgdat->node_id, (unsigned long)pgdat,
6460 (unsigned long)pgdat->node_mem_map);
Roman Zippel12d810c2007-05-31 00:40:54 -07006461#ifndef CONFIG_NEED_MULTIPLE_NODES
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462 /*
6463 * With no DISCONTIG, the global mem_map is just set as node 0's
6464 */
Mel Gormanc7132162006-09-27 01:49:43 -07006465 if (pgdat == NODE_DATA(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466 mem_map = NODE_DATA(0)->node_mem_map;
Laura Abbotta1c34a32015-11-05 18:48:46 -08006467#if defined(CONFIG_HAVE_MEMBLOCK_NODE_MAP) || defined(CONFIG_FLATMEM)
Mel Gormanc7132162006-09-27 01:49:43 -07006468 if (page_to_pfn(mem_map) != pgdat->node_start_pfn)
Laura Abbotta1c34a32015-11-05 18:48:46 -08006469 mem_map -= offset;
Tejun Heo0ee332c2011-12-08 10:22:09 -08006470#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
Mel Gormanc7132162006-09-27 01:49:43 -07006471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472#endif
6473}
Oscar Salvador0cd842f2017-11-15 17:39:18 -08006474#else
6475static void __ref alloc_node_mem_map(struct pglist_data *pgdat) { }
6476#endif /* CONFIG_FLAT_NODE_MEM_MAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477
Oscar Salvador0188dc92018-08-21 21:53:39 -07006478#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
6479static inline void pgdat_set_deferred_range(pg_data_t *pgdat)
6480{
6481 /*
6482 * We start only with one section of pages, more pages are added as
6483 * needed until the rest of deferred pages are initialized.
6484 */
6485 pgdat->static_init_pgcnt = min_t(unsigned long, PAGES_PER_SECTION,
6486 pgdat->node_spanned_pages);
6487 pgdat->first_deferred_pfn = ULONG_MAX;
6488}
6489#else
6490static inline void pgdat_set_deferred_range(pg_data_t *pgdat) {}
6491#endif
6492
Oscar Salvador03e85f92018-08-21 21:53:43 -07006493void __init free_area_init_node(int nid, unsigned long *zones_size,
Pavel Tatashin7cc2a952018-08-21 21:53:36 -07006494 unsigned long node_start_pfn,
6495 unsigned long *zholes_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496{
Johannes Weiner9109fb72008-07-23 21:27:20 -07006497 pg_data_t *pgdat = NODE_DATA(nid);
Zhang Yanfei7960aed2013-07-08 15:59:52 -07006498 unsigned long start_pfn = 0;
6499 unsigned long end_pfn = 0;
Johannes Weiner9109fb72008-07-23 21:27:20 -07006500
Minchan Kim88fdf752012-07-31 16:46:14 -07006501 /* pg_data_t should be reset to zero when it's allocated */
Mel Gorman38087d92016-07-28 15:45:49 -07006502 WARN_ON(pgdat->nr_zones || pgdat->kswapd_classzone_idx);
Minchan Kim88fdf752012-07-31 16:46:14 -07006503
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504 pgdat->node_id = nid;
6505 pgdat->node_start_pfn = node_start_pfn;
Mel Gorman75ef7182016-07-28 15:45:24 -07006506 pgdat->per_cpu_nodestats = NULL;
Zhang Yanfei7960aed2013-07-08 15:59:52 -07006507#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
6508 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
Juergen Gross8d29e182015-02-11 15:26:01 -08006509 pr_info("Initmem setup node %d [mem %#018Lx-%#018Lx]\n", nid,
Zhen Lei4ada0c52015-09-08 15:04:19 -07006510 (u64)start_pfn << PAGE_SHIFT,
6511 end_pfn ? ((u64)end_pfn << PAGE_SHIFT) - 1 : 0);
Taku Izumid91749c2016-03-15 14:55:18 -07006512#else
6513 start_pfn = node_start_pfn;
Zhang Yanfei7960aed2013-07-08 15:59:52 -07006514#endif
6515 calculate_node_totalpages(pgdat, start_pfn, end_pfn,
6516 zones_size, zholes_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517
6518 alloc_node_mem_map(pgdat);
Oscar Salvador0188dc92018-08-21 21:53:39 -07006519 pgdat_set_deferred_range(pgdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520
Wei Yang7f3eb552015-09-08 14:59:50 -07006521 free_area_init_core(pgdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006522}
6523
Mike Rapoportaca52c32018-10-30 15:07:44 -07006524#if !defined(CONFIG_FLAT_NODE_MEM_MAP)
Pavel Tatashinec393a0f2018-10-26 15:10:21 -07006525/*
6526 * Zero all valid struct pages in range [spfn, epfn), return number of struct
6527 * pages zeroed
6528 */
6529static u64 zero_pfn_range(unsigned long spfn, unsigned long epfn)
6530{
6531 unsigned long pfn;
6532 u64 pgcnt = 0;
6533
6534 for (pfn = spfn; pfn < epfn; pfn++) {
6535 if (!pfn_valid(ALIGN_DOWN(pfn, pageblock_nr_pages))) {
6536 pfn = ALIGN_DOWN(pfn, pageblock_nr_pages)
6537 + pageblock_nr_pages - 1;
6538 continue;
6539 }
6540 mm_zero_struct_page(pfn_to_page(pfn));
6541 pgcnt++;
6542 }
6543
6544 return pgcnt;
6545}
6546
Pavel Tatashina4a3ede2017-11-15 17:36:31 -08006547/*
6548 * Only struct pages that are backed by physical memory are zeroed and
6549 * initialized by going through __init_single_page(). But, there are some
6550 * struct pages which are reserved in memblock allocator and their fields
6551 * may be accessed (for example page_to_pfn() on some configuration accesses
6552 * flags). We must explicitly zero those struct pages.
Naoya Horiguchi907ec5f2018-10-26 15:10:15 -07006553 *
6554 * This function also addresses a similar issue where struct pages are left
6555 * uninitialized because the physical address range is not covered by
6556 * memblock.memory or memblock.reserved. That could happen when memblock
6557 * layout is manually configured via memmap=.
Pavel Tatashina4a3ede2017-11-15 17:36:31 -08006558 */
Oscar Salvador03e85f92018-08-21 21:53:43 -07006559void __init zero_resv_unavail(void)
Pavel Tatashina4a3ede2017-11-15 17:36:31 -08006560{
6561 phys_addr_t start, end;
Pavel Tatashina4a3ede2017-11-15 17:36:31 -08006562 u64 i, pgcnt;
Naoya Horiguchi907ec5f2018-10-26 15:10:15 -07006563 phys_addr_t next = 0;
Pavel Tatashina4a3ede2017-11-15 17:36:31 -08006564
6565 /*
Naoya Horiguchi907ec5f2018-10-26 15:10:15 -07006566 * Loop through unavailable ranges not covered by memblock.memory.
Pavel Tatashina4a3ede2017-11-15 17:36:31 -08006567 */
6568 pgcnt = 0;
Naoya Horiguchi907ec5f2018-10-26 15:10:15 -07006569 for_each_mem_range(i, &memblock.memory, NULL,
6570 NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end, NULL) {
Pavel Tatashinec393a0f2018-10-26 15:10:21 -07006571 if (next < start)
6572 pgcnt += zero_pfn_range(PFN_DOWN(next), PFN_UP(start));
Naoya Horiguchi907ec5f2018-10-26 15:10:15 -07006573 next = end;
Pavel Tatashina4a3ede2017-11-15 17:36:31 -08006574 }
Pavel Tatashinec393a0f2018-10-26 15:10:21 -07006575 pgcnt += zero_pfn_range(PFN_DOWN(next), max_pfn);
Pavel Tatashina4a3ede2017-11-15 17:36:31 -08006576
6577 /*
6578 * Struct pages that do not have backing memory. This could be because
6579 * firmware is using some of this memory, or for some other reasons.
Pavel Tatashina4a3ede2017-11-15 17:36:31 -08006580 */
6581 if (pgcnt)
Naoya Horiguchi907ec5f2018-10-26 15:10:15 -07006582 pr_info("Zeroed struct page in unavailable ranges: %lld pages", pgcnt);
Pavel Tatashina4a3ede2017-11-15 17:36:31 -08006583}
Mike Rapoportaca52c32018-10-30 15:07:44 -07006584#endif /* !CONFIG_FLAT_NODE_MEM_MAP */
Pavel Tatashina4a3ede2017-11-15 17:36:31 -08006585
Tejun Heo0ee332c2011-12-08 10:22:09 -08006586#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Miklos Szeredi418508c2007-05-23 13:57:55 -07006587
6588#if MAX_NUMNODES > 1
6589/*
6590 * Figure out the number of possible node ids.
6591 */
Cody P Schaferf9872ca2013-04-29 15:08:01 -07006592void __init setup_nr_node_ids(void)
Miklos Szeredi418508c2007-05-23 13:57:55 -07006593{
Wei Yang904a9552015-09-08 14:59:48 -07006594 unsigned int highest;
Miklos Szeredi418508c2007-05-23 13:57:55 -07006595
Wei Yang904a9552015-09-08 14:59:48 -07006596 highest = find_last_bit(node_possible_map.bits, MAX_NUMNODES);
Miklos Szeredi418508c2007-05-23 13:57:55 -07006597 nr_node_ids = highest + 1;
6598}
Miklos Szeredi418508c2007-05-23 13:57:55 -07006599#endif
6600
Mel Gormanc7132162006-09-27 01:49:43 -07006601/**
Tejun Heo1e019792011-07-12 09:45:34 +02006602 * node_map_pfn_alignment - determine the maximum internode alignment
6603 *
6604 * This function should be called after node map is populated and sorted.
6605 * It calculates the maximum power of two alignment which can distinguish
6606 * all the nodes.
6607 *
6608 * For example, if all nodes are 1GiB and aligned to 1GiB, the return value
6609 * would indicate 1GiB alignment with (1 << (30 - PAGE_SHIFT)). If the
6610 * nodes are shifted by 256MiB, 256MiB. Note that if only the last node is
6611 * shifted, 1GiB is enough and this function will indicate so.
6612 *
6613 * This is used to test whether pfn -> nid mapping of the chosen memory
6614 * model has fine enough granularity to avoid incorrect mapping for the
6615 * populated node map.
6616 *
6617 * Returns the determined alignment in pfn's. 0 if there is no alignment
6618 * requirement (single node).
6619 */
6620unsigned long __init node_map_pfn_alignment(void)
6621{
6622 unsigned long accl_mask = 0, last_end = 0;
Tejun Heoc13291a2011-07-12 10:46:30 +02006623 unsigned long start, end, mask;
Tejun Heo1e019792011-07-12 09:45:34 +02006624 int last_nid = -1;
Tejun Heoc13291a2011-07-12 10:46:30 +02006625 int i, nid;
Tejun Heo1e019792011-07-12 09:45:34 +02006626
Tejun Heoc13291a2011-07-12 10:46:30 +02006627 for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, &nid) {
Tejun Heo1e019792011-07-12 09:45:34 +02006628 if (!start || last_nid < 0 || last_nid == nid) {
6629 last_nid = nid;
6630 last_end = end;
6631 continue;
6632 }
6633
6634 /*
6635 * Start with a mask granular enough to pin-point to the
6636 * start pfn and tick off bits one-by-one until it becomes
6637 * too coarse to separate the current node from the last.
6638 */
6639 mask = ~((1 << __ffs(start)) - 1);
6640 while (mask && last_end <= (start & (mask << 1)))
6641 mask <<= 1;
6642
6643 /* accumulate all internode masks */
6644 accl_mask |= mask;
6645 }
6646
6647 /* convert mask to number of pages */
6648 return ~accl_mask + 1;
6649}
6650
Mel Gormana6af2bc2007-02-10 01:42:57 -08006651/* Find the lowest pfn for a node */
Adrian Bunkb69a7282008-07-23 21:28:12 -07006652static unsigned long __init find_min_pfn_for_node(int nid)
Mel Gormanc7132162006-09-27 01:49:43 -07006653{
Mel Gormana6af2bc2007-02-10 01:42:57 -08006654 unsigned long min_pfn = ULONG_MAX;
Tejun Heoc13291a2011-07-12 10:46:30 +02006655 unsigned long start_pfn;
6656 int i;
Mel Gorman1abbfb42006-11-23 12:01:41 +00006657
Tejun Heoc13291a2011-07-12 10:46:30 +02006658 for_each_mem_pfn_range(i, nid, &start_pfn, NULL, NULL)
6659 min_pfn = min(min_pfn, start_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07006660
Mel Gormana6af2bc2007-02-10 01:42:57 -08006661 if (min_pfn == ULONG_MAX) {
Joe Perches11705322016-03-17 14:19:50 -07006662 pr_warn("Could not find start_pfn for node %d\n", nid);
Mel Gormana6af2bc2007-02-10 01:42:57 -08006663 return 0;
6664 }
6665
6666 return min_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07006667}
6668
6669/**
6670 * find_min_pfn_with_active_regions - Find the minimum PFN registered
6671 *
6672 * It returns the minimum PFN based on information provided via
Zhang Zhen7d018172014-06-04 16:10:53 -07006673 * memblock_set_node().
Mel Gormanc7132162006-09-27 01:49:43 -07006674 */
6675unsigned long __init find_min_pfn_with_active_regions(void)
6676{
6677 return find_min_pfn_for_node(MAX_NUMNODES);
6678}
6679
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07006680/*
6681 * early_calculate_totalpages()
6682 * Sum pages in active regions for movable zone.
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08006683 * Populate N_MEMORY for calculating usable_nodes.
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07006684 */
Adrian Bunk484f51f2007-10-16 01:26:03 -07006685static unsigned long __init early_calculate_totalpages(void)
Mel Gorman7e63efef2007-07-17 04:03:15 -07006686{
Mel Gorman7e63efef2007-07-17 04:03:15 -07006687 unsigned long totalpages = 0;
Tejun Heoc13291a2011-07-12 10:46:30 +02006688 unsigned long start_pfn, end_pfn;
6689 int i, nid;
Mel Gorman7e63efef2007-07-17 04:03:15 -07006690
Tejun Heoc13291a2011-07-12 10:46:30 +02006691 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) {
6692 unsigned long pages = end_pfn - start_pfn;
6693
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07006694 totalpages += pages;
6695 if (pages)
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08006696 node_set_state(nid, N_MEMORY);
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07006697 }
Pintu Kumarb8af2942013-09-11 14:20:34 -07006698 return totalpages;
Mel Gorman7e63efef2007-07-17 04:03:15 -07006699}
6700
Mel Gorman2a1e2742007-07-17 04:03:12 -07006701/*
6702 * Find the PFN the Movable zone begins in each node. Kernel memory
6703 * is spread evenly between nodes as long as the nodes have enough
6704 * memory. When they don't, some nodes will have more kernelcore than
6705 * others
6706 */
Kautuk Consulb224ef82012-03-21 16:34:15 -07006707static void __init find_zone_movable_pfns_for_nodes(void)
Mel Gorman2a1e2742007-07-17 04:03:12 -07006708{
6709 int i, nid;
6710 unsigned long usable_startpfn;
6711 unsigned long kernelcore_node, kernelcore_remaining;
Yinghai Lu66918dc2009-06-30 11:41:37 -07006712 /* save the state before borrow the nodemask */
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08006713 nodemask_t saved_node_state = node_states[N_MEMORY];
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07006714 unsigned long totalpages = early_calculate_totalpages();
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08006715 int usable_nodes = nodes_weight(node_states[N_MEMORY]);
Emil Medve136199f2014-04-07 15:37:52 -07006716 struct memblock_region *r;
Tang Chenb2f3eeb2014-01-21 15:49:38 -08006717
6718 /* Need to find movable_zone earlier when movable_node is specified. */
6719 find_usable_zone_for_movable();
Mel Gorman2a1e2742007-07-17 04:03:12 -07006720
Mel Gorman7e63efef2007-07-17 04:03:15 -07006721 /*
Tang Chenb2f3eeb2014-01-21 15:49:38 -08006722 * If movable_node is specified, ignore kernelcore and movablecore
6723 * options.
6724 */
6725 if (movable_node_is_enabled()) {
Emil Medve136199f2014-04-07 15:37:52 -07006726 for_each_memblock(memory, r) {
6727 if (!memblock_is_hotpluggable(r))
Tang Chenb2f3eeb2014-01-21 15:49:38 -08006728 continue;
6729
Emil Medve136199f2014-04-07 15:37:52 -07006730 nid = r->nid;
Tang Chenb2f3eeb2014-01-21 15:49:38 -08006731
Emil Medve136199f2014-04-07 15:37:52 -07006732 usable_startpfn = PFN_DOWN(r->base);
Tang Chenb2f3eeb2014-01-21 15:49:38 -08006733 zone_movable_pfn[nid] = zone_movable_pfn[nid] ?
6734 min(usable_startpfn, zone_movable_pfn[nid]) :
6735 usable_startpfn;
6736 }
6737
6738 goto out2;
6739 }
6740
6741 /*
Taku Izumi342332e2016-03-15 14:55:22 -07006742 * If kernelcore=mirror is specified, ignore movablecore option
6743 */
6744 if (mirrored_kernelcore) {
6745 bool mem_below_4gb_not_mirrored = false;
6746
6747 for_each_memblock(memory, r) {
6748 if (memblock_is_mirror(r))
6749 continue;
6750
6751 nid = r->nid;
6752
6753 usable_startpfn = memblock_region_memory_base_pfn(r);
6754
6755 if (usable_startpfn < 0x100000) {
6756 mem_below_4gb_not_mirrored = true;
6757 continue;
6758 }
6759
6760 zone_movable_pfn[nid] = zone_movable_pfn[nid] ?
6761 min(usable_startpfn, zone_movable_pfn[nid]) :
6762 usable_startpfn;
6763 }
6764
6765 if (mem_below_4gb_not_mirrored)
6766 pr_warn("This configuration results in unmirrored kernel memory.");
6767
6768 goto out2;
6769 }
6770
6771 /*
David Rientjesa5c6d652018-04-05 16:23:09 -07006772 * If kernelcore=nn% or movablecore=nn% was specified, calculate the
6773 * amount of necessary memory.
6774 */
6775 if (required_kernelcore_percent)
6776 required_kernelcore = (totalpages * 100 * required_kernelcore_percent) /
6777 10000UL;
6778 if (required_movablecore_percent)
6779 required_movablecore = (totalpages * 100 * required_movablecore_percent) /
6780 10000UL;
6781
6782 /*
6783 * If movablecore= was specified, calculate what size of
Mel Gorman7e63efef2007-07-17 04:03:15 -07006784 * kernelcore that corresponds so that memory usable for
6785 * any allocation type is evenly spread. If both kernelcore
6786 * and movablecore are specified, then the value of kernelcore
6787 * will be used for required_kernelcore if it's greater than
6788 * what movablecore would have allowed.
6789 */
6790 if (required_movablecore) {
Mel Gorman7e63efef2007-07-17 04:03:15 -07006791 unsigned long corepages;
6792
6793 /*
6794 * Round-up so that ZONE_MOVABLE is at least as large as what
6795 * was requested by the user
6796 */
6797 required_movablecore =
6798 roundup(required_movablecore, MAX_ORDER_NR_PAGES);
Xishi Qiu9fd745d2015-11-05 18:48:11 -08006799 required_movablecore = min(totalpages, required_movablecore);
Mel Gorman7e63efef2007-07-17 04:03:15 -07006800 corepages = totalpages - required_movablecore;
6801
6802 required_kernelcore = max(required_kernelcore, corepages);
6803 }
6804
Xishi Qiubde304b2015-11-05 18:48:56 -08006805 /*
6806 * If kernelcore was not specified or kernelcore size is larger
6807 * than totalpages, there is no ZONE_MOVABLE.
6808 */
6809 if (!required_kernelcore || required_kernelcore >= totalpages)
Yinghai Lu66918dc2009-06-30 11:41:37 -07006810 goto out;
Mel Gorman2a1e2742007-07-17 04:03:12 -07006811
6812 /* usable_startpfn is the lowest possible pfn ZONE_MOVABLE can be at */
Mel Gorman2a1e2742007-07-17 04:03:12 -07006813 usable_startpfn = arch_zone_lowest_possible_pfn[movable_zone];
6814
6815restart:
6816 /* Spread kernelcore memory as evenly as possible throughout nodes */
6817 kernelcore_node = required_kernelcore / usable_nodes;
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08006818 for_each_node_state(nid, N_MEMORY) {
Tejun Heoc13291a2011-07-12 10:46:30 +02006819 unsigned long start_pfn, end_pfn;
6820
Mel Gorman2a1e2742007-07-17 04:03:12 -07006821 /*
6822 * Recalculate kernelcore_node if the division per node
6823 * now exceeds what is necessary to satisfy the requested
6824 * amount of memory for the kernel
6825 */
6826 if (required_kernelcore < kernelcore_node)
6827 kernelcore_node = required_kernelcore / usable_nodes;
6828
6829 /*
6830 * As the map is walked, we track how much memory is usable
6831 * by the kernel using kernelcore_remaining. When it is
6832 * 0, the rest of the node is usable by ZONE_MOVABLE
6833 */
6834 kernelcore_remaining = kernelcore_node;
6835
6836 /* Go through each range of PFNs within this node */
Tejun Heoc13291a2011-07-12 10:46:30 +02006837 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
Mel Gorman2a1e2742007-07-17 04:03:12 -07006838 unsigned long size_pages;
6839
Tejun Heoc13291a2011-07-12 10:46:30 +02006840 start_pfn = max(start_pfn, zone_movable_pfn[nid]);
Mel Gorman2a1e2742007-07-17 04:03:12 -07006841 if (start_pfn >= end_pfn)
6842 continue;
6843
6844 /* Account for what is only usable for kernelcore */
6845 if (start_pfn < usable_startpfn) {
6846 unsigned long kernel_pages;
6847 kernel_pages = min(end_pfn, usable_startpfn)
6848 - start_pfn;
6849
6850 kernelcore_remaining -= min(kernel_pages,
6851 kernelcore_remaining);
6852 required_kernelcore -= min(kernel_pages,
6853 required_kernelcore);
6854
6855 /* Continue if range is now fully accounted */
6856 if (end_pfn <= usable_startpfn) {
6857
6858 /*
6859 * Push zone_movable_pfn to the end so
6860 * that if we have to rebalance
6861 * kernelcore across nodes, we will
6862 * not double account here
6863 */
6864 zone_movable_pfn[nid] = end_pfn;
6865 continue;
6866 }
6867 start_pfn = usable_startpfn;
6868 }
6869
6870 /*
6871 * The usable PFN range for ZONE_MOVABLE is from
6872 * start_pfn->end_pfn. Calculate size_pages as the
6873 * number of pages used as kernelcore
6874 */
6875 size_pages = end_pfn - start_pfn;
6876 if (size_pages > kernelcore_remaining)
6877 size_pages = kernelcore_remaining;
6878 zone_movable_pfn[nid] = start_pfn + size_pages;
6879
6880 /*
6881 * Some kernelcore has been met, update counts and
6882 * break if the kernelcore for this node has been
Pintu Kumarb8af2942013-09-11 14:20:34 -07006883 * satisfied
Mel Gorman2a1e2742007-07-17 04:03:12 -07006884 */
6885 required_kernelcore -= min(required_kernelcore,
6886 size_pages);
6887 kernelcore_remaining -= size_pages;
6888 if (!kernelcore_remaining)
6889 break;
6890 }
6891 }
6892
6893 /*
6894 * If there is still required_kernelcore, we do another pass with one
6895 * less node in the count. This will push zone_movable_pfn[nid] further
6896 * along on the nodes that still have memory until kernelcore is
Pintu Kumarb8af2942013-09-11 14:20:34 -07006897 * satisfied
Mel Gorman2a1e2742007-07-17 04:03:12 -07006898 */
6899 usable_nodes--;
6900 if (usable_nodes && required_kernelcore > usable_nodes)
6901 goto restart;
6902
Tang Chenb2f3eeb2014-01-21 15:49:38 -08006903out2:
Mel Gorman2a1e2742007-07-17 04:03:12 -07006904 /* Align start of ZONE_MOVABLE on all nids to MAX_ORDER_NR_PAGES */
6905 for (nid = 0; nid < MAX_NUMNODES; nid++)
6906 zone_movable_pfn[nid] =
6907 roundup(zone_movable_pfn[nid], MAX_ORDER_NR_PAGES);
Yinghai Lu66918dc2009-06-30 11:41:37 -07006908
Yinghai Lu20e69262013-03-01 14:51:27 -08006909out:
Yinghai Lu66918dc2009-06-30 11:41:37 -07006910 /* restore the node_state */
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08006911 node_states[N_MEMORY] = saved_node_state;
Mel Gorman2a1e2742007-07-17 04:03:12 -07006912}
6913
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08006914/* Any regular or high memory on that node ? */
6915static void check_for_memory(pg_data_t *pgdat, int nid)
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07006916{
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07006917 enum zone_type zone_type;
6918
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08006919 for (zone_type = 0; zone_type <= ZONE_MOVABLE - 1; zone_type++) {
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07006920 struct zone *zone = &pgdat->node_zones[zone_type];
Xishi Qiub38a8722013-11-12 15:07:20 -08006921 if (populated_zone(zone)) {
Oscar Salvador7b0e0c02018-10-26 15:03:58 -07006922 if (IS_ENABLED(CONFIG_HIGHMEM))
6923 node_set_state(nid, N_HIGH_MEMORY);
6924 if (zone_type <= ZONE_NORMAL)
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08006925 node_set_state(nid, N_NORMAL_MEMORY);
Bob Liud0048b02012-01-12 17:19:07 -08006926 break;
6927 }
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07006928 }
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07006929}
6930
Mel Gormanc7132162006-09-27 01:49:43 -07006931/**
6932 * free_area_init_nodes - Initialise all pg_data_t and zone data
Randy Dunlap88ca3b92006-10-04 02:15:25 -07006933 * @max_zone_pfn: an array of max PFNs for each zone
Mel Gormanc7132162006-09-27 01:49:43 -07006934 *
6935 * This will call free_area_init_node() for each active node in the system.
Zhang Zhen7d018172014-06-04 16:10:53 -07006936 * Using the page ranges provided by memblock_set_node(), the size of each
Mel Gormanc7132162006-09-27 01:49:43 -07006937 * zone in each node and their holes is calculated. If the maximum PFN
6938 * between two adjacent zones match, it is assumed that the zone is empty.
6939 * For example, if arch_max_dma_pfn == arch_max_dma32_pfn, it is assumed
6940 * that arch_max_dma32_pfn has no pages. It is also assumed that a zone
6941 * starts where the previous one ended. For example, ZONE_DMA32 starts
6942 * at arch_max_dma_pfn.
6943 */
6944void __init free_area_init_nodes(unsigned long *max_zone_pfn)
6945{
Tejun Heoc13291a2011-07-12 10:46:30 +02006946 unsigned long start_pfn, end_pfn;
6947 int i, nid;
Mel Gormana6af2bc2007-02-10 01:42:57 -08006948
Mel Gormanc7132162006-09-27 01:49:43 -07006949 /* Record where the zone boundaries are */
6950 memset(arch_zone_lowest_possible_pfn, 0,
6951 sizeof(arch_zone_lowest_possible_pfn));
6952 memset(arch_zone_highest_possible_pfn, 0,
6953 sizeof(arch_zone_highest_possible_pfn));
Oliver O'Halloran90cae1f2016-07-26 15:22:17 -07006954
6955 start_pfn = find_min_pfn_with_active_regions();
6956
6957 for (i = 0; i < MAX_NR_ZONES; i++) {
Mel Gorman2a1e2742007-07-17 04:03:12 -07006958 if (i == ZONE_MOVABLE)
6959 continue;
Oliver O'Halloran90cae1f2016-07-26 15:22:17 -07006960
6961 end_pfn = max(max_zone_pfn[i], start_pfn);
6962 arch_zone_lowest_possible_pfn[i] = start_pfn;
6963 arch_zone_highest_possible_pfn[i] = end_pfn;
6964
6965 start_pfn = end_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07006966 }
Mel Gorman2a1e2742007-07-17 04:03:12 -07006967
6968 /* Find the PFNs that ZONE_MOVABLE begins at in each node */
6969 memset(zone_movable_pfn, 0, sizeof(zone_movable_pfn));
Kautuk Consulb224ef82012-03-21 16:34:15 -07006970 find_zone_movable_pfns_for_nodes();
Mel Gormanc7132162006-09-27 01:49:43 -07006971
Mel Gormanc7132162006-09-27 01:49:43 -07006972 /* Print out the zone ranges */
Anton Blanchardf88dfff2014-12-10 15:42:53 -08006973 pr_info("Zone ranges:\n");
Mel Gorman2a1e2742007-07-17 04:03:12 -07006974 for (i = 0; i < MAX_NR_ZONES; i++) {
6975 if (i == ZONE_MOVABLE)
6976 continue;
Anton Blanchardf88dfff2014-12-10 15:42:53 -08006977 pr_info(" %-8s ", zone_names[i]);
David Rientjes72f0ba02010-03-05 13:42:14 -08006978 if (arch_zone_lowest_possible_pfn[i] ==
6979 arch_zone_highest_possible_pfn[i])
Anton Blanchardf88dfff2014-12-10 15:42:53 -08006980 pr_cont("empty\n");
David Rientjes72f0ba02010-03-05 13:42:14 -08006981 else
Juergen Gross8d29e182015-02-11 15:26:01 -08006982 pr_cont("[mem %#018Lx-%#018Lx]\n",
6983 (u64)arch_zone_lowest_possible_pfn[i]
6984 << PAGE_SHIFT,
6985 ((u64)arch_zone_highest_possible_pfn[i]
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07006986 << PAGE_SHIFT) - 1);
Mel Gorman2a1e2742007-07-17 04:03:12 -07006987 }
6988
6989 /* Print out the PFNs ZONE_MOVABLE begins at in each node */
Anton Blanchardf88dfff2014-12-10 15:42:53 -08006990 pr_info("Movable zone start for each node\n");
Mel Gorman2a1e2742007-07-17 04:03:12 -07006991 for (i = 0; i < MAX_NUMNODES; i++) {
6992 if (zone_movable_pfn[i])
Juergen Gross8d29e182015-02-11 15:26:01 -08006993 pr_info(" Node %d: %#018Lx\n", i,
6994 (u64)zone_movable_pfn[i] << PAGE_SHIFT);
Mel Gorman2a1e2742007-07-17 04:03:12 -07006995 }
Mel Gormanc7132162006-09-27 01:49:43 -07006996
Wanpeng Lif2d52fe2012-10-08 16:32:24 -07006997 /* Print out the early node map */
Anton Blanchardf88dfff2014-12-10 15:42:53 -08006998 pr_info("Early memory node ranges\n");
Tejun Heoc13291a2011-07-12 10:46:30 +02006999 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid)
Juergen Gross8d29e182015-02-11 15:26:01 -08007000 pr_info(" node %3d: [mem %#018Lx-%#018Lx]\n", nid,
7001 (u64)start_pfn << PAGE_SHIFT,
7002 ((u64)end_pfn << PAGE_SHIFT) - 1);
Mel Gormanc7132162006-09-27 01:49:43 -07007003
7004 /* Initialise every node */
Mel Gorman708614e2008-07-23 21:26:51 -07007005 mminit_verify_pageflags_layout();
Christoph Lameter8ef82862007-02-20 13:57:52 -08007006 setup_nr_node_ids();
Pavel Tatashine181ae0c2018-07-14 09:15:07 -04007007 zero_resv_unavail();
Mel Gormanc7132162006-09-27 01:49:43 -07007008 for_each_online_node(nid) {
7009 pg_data_t *pgdat = NODE_DATA(nid);
Johannes Weiner9109fb72008-07-23 21:27:20 -07007010 free_area_init_node(nid, NULL,
Mel Gormanc7132162006-09-27 01:49:43 -07007011 find_min_pfn_for_node(nid), NULL);
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07007012
7013 /* Any memory on that node */
7014 if (pgdat->node_present_pages)
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08007015 node_set_state(nid, N_MEMORY);
7016 check_for_memory(pgdat, nid);
Mel Gormanc7132162006-09-27 01:49:43 -07007017 }
7018}
Mel Gorman2a1e2742007-07-17 04:03:12 -07007019
David Rientjesa5c6d652018-04-05 16:23:09 -07007020static int __init cmdline_parse_core(char *p, unsigned long *core,
7021 unsigned long *percent)
Mel Gorman2a1e2742007-07-17 04:03:12 -07007022{
7023 unsigned long long coremem;
David Rientjesa5c6d652018-04-05 16:23:09 -07007024 char *endptr;
7025
Mel Gorman2a1e2742007-07-17 04:03:12 -07007026 if (!p)
7027 return -EINVAL;
7028
David Rientjesa5c6d652018-04-05 16:23:09 -07007029 /* Value may be a percentage of total memory, otherwise bytes */
7030 coremem = simple_strtoull(p, &endptr, 0);
7031 if (*endptr == '%') {
7032 /* Paranoid check for percent values greater than 100 */
7033 WARN_ON(coremem > 100);
Mel Gorman2a1e2742007-07-17 04:03:12 -07007034
David Rientjesa5c6d652018-04-05 16:23:09 -07007035 *percent = coremem;
7036 } else {
7037 coremem = memparse(p, &p);
7038 /* Paranoid check that UL is enough for the coremem value */
7039 WARN_ON((coremem >> PAGE_SHIFT) > ULONG_MAX);
Mel Gorman2a1e2742007-07-17 04:03:12 -07007040
David Rientjesa5c6d652018-04-05 16:23:09 -07007041 *core = coremem >> PAGE_SHIFT;
7042 *percent = 0UL;
7043 }
Mel Gorman2a1e2742007-07-17 04:03:12 -07007044 return 0;
7045}
Mel Gormaned7ed362007-07-17 04:03:14 -07007046
Mel Gorman7e63efef2007-07-17 04:03:15 -07007047/*
7048 * kernelcore=size sets the amount of memory for use for allocations that
7049 * cannot be reclaimed or migrated.
7050 */
7051static int __init cmdline_parse_kernelcore(char *p)
7052{
Taku Izumi342332e2016-03-15 14:55:22 -07007053 /* parse kernelcore=mirror */
7054 if (parse_option_str(p, "mirror")) {
7055 mirrored_kernelcore = true;
7056 return 0;
7057 }
7058
David Rientjesa5c6d652018-04-05 16:23:09 -07007059 return cmdline_parse_core(p, &required_kernelcore,
7060 &required_kernelcore_percent);
Mel Gorman7e63efef2007-07-17 04:03:15 -07007061}
7062
7063/*
7064 * movablecore=size sets the amount of memory for use for allocations that
7065 * can be reclaimed or migrated.
7066 */
7067static int __init cmdline_parse_movablecore(char *p)
7068{
David Rientjesa5c6d652018-04-05 16:23:09 -07007069 return cmdline_parse_core(p, &required_movablecore,
7070 &required_movablecore_percent);
Mel Gorman7e63efef2007-07-17 04:03:15 -07007071}
7072
Mel Gormaned7ed362007-07-17 04:03:14 -07007073early_param("kernelcore", cmdline_parse_kernelcore);
Mel Gorman7e63efef2007-07-17 04:03:15 -07007074early_param("movablecore", cmdline_parse_movablecore);
Mel Gormaned7ed362007-07-17 04:03:14 -07007075
Tejun Heo0ee332c2011-12-08 10:22:09 -08007076#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
Mel Gormanc7132162006-09-27 01:49:43 -07007077
Jiang Liuc3d5f5f2013-07-03 15:03:14 -07007078void adjust_managed_page_count(struct page *page, long count)
7079{
7080 spin_lock(&managed_page_count_lock);
Arun KS9705bea2018-12-28 00:34:24 -08007081 atomic_long_add(count, &page_zone(page)->managed_pages);
Arun KSca79b0c2018-12-28 00:34:29 -08007082 totalram_pages_add(count);
Jiang Liu3dcc0572013-07-03 15:03:21 -07007083#ifdef CONFIG_HIGHMEM
7084 if (PageHighMem(page))
Arun KSca79b0c2018-12-28 00:34:29 -08007085 totalhigh_pages_add(count);
Jiang Liu3dcc0572013-07-03 15:03:21 -07007086#endif
Jiang Liuc3d5f5f2013-07-03 15:03:14 -07007087 spin_unlock(&managed_page_count_lock);
7088}
Jiang Liu3dcc0572013-07-03 15:03:21 -07007089EXPORT_SYMBOL(adjust_managed_page_count);
Jiang Liuc3d5f5f2013-07-03 15:03:14 -07007090
Jiang Liu11199692013-07-03 15:02:48 -07007091unsigned long free_reserved_area(void *start, void *end, int poison, char *s)
Jiang Liu69afade2013-04-29 15:06:21 -07007092{
Jiang Liu11199692013-07-03 15:02:48 -07007093 void *pos;
7094 unsigned long pages = 0;
Jiang Liu69afade2013-04-29 15:06:21 -07007095
Jiang Liu11199692013-07-03 15:02:48 -07007096 start = (void *)PAGE_ALIGN((unsigned long)start);
7097 end = (void *)((unsigned long)end & PAGE_MASK);
7098 for (pos = start; pos < end; pos += PAGE_SIZE, pages++) {
Dave Hansen0d834322018-08-02 15:58:26 -07007099 struct page *page = virt_to_page(pos);
7100 void *direct_map_addr;
7101
7102 /*
7103 * 'direct_map_addr' might be different from 'pos'
7104 * because some architectures' virt_to_page()
7105 * work with aliases. Getting the direct map
7106 * address ensures that we get a _writeable_
7107 * alias for the memset().
7108 */
7109 direct_map_addr = page_address(page);
Jiang Liudbe67df2013-07-03 15:02:51 -07007110 if ((unsigned int)poison <= 0xFF)
Dave Hansen0d834322018-08-02 15:58:26 -07007111 memset(direct_map_addr, poison, PAGE_SIZE);
7112
7113 free_reserved_page(page);
Jiang Liu69afade2013-04-29 15:06:21 -07007114 }
7115
7116 if (pages && s)
Josh Poimboeufadb1fe92016-10-25 09:51:14 -05007117 pr_info("Freeing %s memory: %ldK\n",
7118 s, pages << (PAGE_SHIFT - 10));
Jiang Liu69afade2013-04-29 15:06:21 -07007119
7120 return pages;
7121}
Jiang Liu11199692013-07-03 15:02:48 -07007122EXPORT_SYMBOL(free_reserved_area);
Jiang Liu69afade2013-04-29 15:06:21 -07007123
Jiang Liucfa11e02013-04-29 15:07:00 -07007124#ifdef CONFIG_HIGHMEM
7125void free_highmem_page(struct page *page)
7126{
7127 __free_reserved_page(page);
Arun KSca79b0c2018-12-28 00:34:29 -08007128 totalram_pages_inc();
Arun KS9705bea2018-12-28 00:34:24 -08007129 atomic_long_inc(&page_zone(page)->managed_pages);
Arun KSca79b0c2018-12-28 00:34:29 -08007130 totalhigh_pages_inc();
Jiang Liucfa11e02013-04-29 15:07:00 -07007131}
7132#endif
7133
Jiang Liu7ee3d4e2013-07-03 15:03:41 -07007134
7135void __init mem_init_print_info(const char *str)
7136{
7137 unsigned long physpages, codesize, datasize, rosize, bss_size;
7138 unsigned long init_code_size, init_data_size;
7139
7140 physpages = get_num_physpages();
7141 codesize = _etext - _stext;
7142 datasize = _edata - _sdata;
7143 rosize = __end_rodata - __start_rodata;
7144 bss_size = __bss_stop - __bss_start;
7145 init_data_size = __init_end - __init_begin;
7146 init_code_size = _einittext - _sinittext;
7147
7148 /*
7149 * Detect special cases and adjust section sizes accordingly:
7150 * 1) .init.* may be embedded into .data sections
7151 * 2) .init.text.* may be out of [__init_begin, __init_end],
7152 * please refer to arch/tile/kernel/vmlinux.lds.S.
7153 * 3) .rodata.* may be embedded into .text or .data sections.
7154 */
7155#define adj_init_size(start, end, size, pos, adj) \
Pintu Kumarb8af2942013-09-11 14:20:34 -07007156 do { \
7157 if (start <= pos && pos < end && size > adj) \
7158 size -= adj; \
7159 } while (0)
Jiang Liu7ee3d4e2013-07-03 15:03:41 -07007160
7161 adj_init_size(__init_begin, __init_end, init_data_size,
7162 _sinittext, init_code_size);
7163 adj_init_size(_stext, _etext, codesize, _sinittext, init_code_size);
7164 adj_init_size(_sdata, _edata, datasize, __init_begin, init_data_size);
7165 adj_init_size(_stext, _etext, codesize, __start_rodata, rosize);
7166 adj_init_size(_sdata, _edata, datasize, __start_rodata, rosize);
7167
7168#undef adj_init_size
7169
Joe Perches756a0252016-03-17 14:19:47 -07007170 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 -07007171#ifdef CONFIG_HIGHMEM
Joe Perches756a0252016-03-17 14:19:47 -07007172 ", %luK highmem"
Jiang Liu7ee3d4e2013-07-03 15:03:41 -07007173#endif
Joe Perches756a0252016-03-17 14:19:47 -07007174 "%s%s)\n",
7175 nr_free_pages() << (PAGE_SHIFT - 10),
7176 physpages << (PAGE_SHIFT - 10),
7177 codesize >> 10, datasize >> 10, rosize >> 10,
7178 (init_data_size + init_code_size) >> 10, bss_size >> 10,
Arun KSca79b0c2018-12-28 00:34:29 -08007179 (physpages - totalram_pages() - totalcma_pages) << (PAGE_SHIFT - 10),
Joe Perches756a0252016-03-17 14:19:47 -07007180 totalcma_pages << (PAGE_SHIFT - 10),
Jiang Liu7ee3d4e2013-07-03 15:03:41 -07007181#ifdef CONFIG_HIGHMEM
Arun KSca79b0c2018-12-28 00:34:29 -08007182 totalhigh_pages() << (PAGE_SHIFT - 10),
Jiang Liu7ee3d4e2013-07-03 15:03:41 -07007183#endif
Joe Perches756a0252016-03-17 14:19:47 -07007184 str ? ", " : "", str ? str : "");
Jiang Liu7ee3d4e2013-07-03 15:03:41 -07007185}
7186
Mel Gorman0e0b8642006-09-27 01:49:56 -07007187/**
Randy Dunlap88ca3b92006-10-04 02:15:25 -07007188 * set_dma_reserve - set the specified number of pages reserved in the first zone
7189 * @new_dma_reserve: The number of pages to mark reserved
Mel Gorman0e0b8642006-09-27 01:49:56 -07007190 *
Yaowei Bai013110a2015-09-08 15:04:10 -07007191 * The per-cpu batchsize and zone watermarks are determined by managed_pages.
Mel Gorman0e0b8642006-09-27 01:49:56 -07007192 * In the DMA zone, a significant percentage may be consumed by kernel image
7193 * and other unfreeable allocations which can skew the watermarks badly. This
Randy Dunlap88ca3b92006-10-04 02:15:25 -07007194 * function may optionally be used to account for unfreeable pages in the
7195 * first zone (e.g., ZONE_DMA). The effect will be lower watermarks and
7196 * smaller per-cpu batchsize.
Mel Gorman0e0b8642006-09-27 01:49:56 -07007197 */
7198void __init set_dma_reserve(unsigned long new_dma_reserve)
7199{
7200 dma_reserve = new_dma_reserve;
7201}
7202
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203void __init free_area_init(unsigned long *zones_size)
7204{
Pavel Tatashine181ae0c2018-07-14 09:15:07 -04007205 zero_resv_unavail();
Johannes Weiner9109fb72008-07-23 21:27:20 -07007206 free_area_init_node(0, zones_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007207 __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
7208}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209
Sebastian Andrzej Siewior005fd4b2016-11-03 15:50:02 +01007210static int page_alloc_cpu_dead(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007211{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007212
Sebastian Andrzej Siewior005fd4b2016-11-03 15:50:02 +01007213 lru_add_drain_cpu(cpu);
7214 drain_pages(cpu);
Christoph Lameter9f8f2172008-02-04 22:29:11 -08007215
Sebastian Andrzej Siewior005fd4b2016-11-03 15:50:02 +01007216 /*
7217 * Spill the event counters of the dead processor
7218 * into the current processors event counters.
7219 * This artificially elevates the count of the current
7220 * processor.
7221 */
7222 vm_events_fold_cpu(cpu);
Christoph Lameter9f8f2172008-02-04 22:29:11 -08007223
Sebastian Andrzej Siewior005fd4b2016-11-03 15:50:02 +01007224 /*
7225 * Zero the differential counters of the dead processor
7226 * so that the vm statistics are consistent.
7227 *
7228 * This is only okay since the processor is dead and cannot
7229 * race with what we are doing.
7230 */
7231 cpu_vm_stats_fold(cpu);
7232 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007233}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234
7235void __init page_alloc_init(void)
7236{
Sebastian Andrzej Siewior005fd4b2016-11-03 15:50:02 +01007237 int ret;
7238
7239 ret = cpuhp_setup_state_nocalls(CPUHP_PAGE_ALLOC_DEAD,
7240 "mm/page_alloc:dead", NULL,
7241 page_alloc_cpu_dead);
7242 WARN_ON(ret < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007243}
7244
7245/*
Yaowei Bai34b10062015-09-08 15:04:13 -07007246 * calculate_totalreserve_pages - called when sysctl_lowmem_reserve_ratio
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07007247 * or min_free_kbytes changes.
7248 */
7249static void calculate_totalreserve_pages(void)
7250{
7251 struct pglist_data *pgdat;
7252 unsigned long reserve_pages = 0;
Christoph Lameter2f6726e2006-09-25 23:31:18 -07007253 enum zone_type i, j;
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07007254
7255 for_each_online_pgdat(pgdat) {
Mel Gorman281e3722016-07-28 15:46:11 -07007256
7257 pgdat->totalreserve_pages = 0;
7258
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07007259 for (i = 0; i < MAX_NR_ZONES; i++) {
7260 struct zone *zone = pgdat->node_zones + i;
Mel Gorman3484b2d2014-08-06 16:07:14 -07007261 long max = 0;
Arun KS9705bea2018-12-28 00:34:24 -08007262 unsigned long managed_pages = zone_managed_pages(zone);
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07007263
7264 /* Find valid and maximum lowmem_reserve in the zone */
7265 for (j = i; j < MAX_NR_ZONES; j++) {
7266 if (zone->lowmem_reserve[j] > max)
7267 max = zone->lowmem_reserve[j];
7268 }
7269
Mel Gorman41858962009-06-16 15:32:12 -07007270 /* we treat the high watermark as reserved pages. */
7271 max += high_wmark_pages(zone);
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07007272
Arun KS3d6357d2018-12-28 00:34:20 -08007273 if (max > managed_pages)
7274 max = managed_pages;
Johannes Weinera8d01432016-01-14 15:20:15 -08007275
Mel Gorman281e3722016-07-28 15:46:11 -07007276 pgdat->totalreserve_pages += max;
Johannes Weinera8d01432016-01-14 15:20:15 -08007277
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07007278 reserve_pages += max;
7279 }
7280 }
7281 totalreserve_pages = reserve_pages;
7282}
7283
7284/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007285 * setup_per_zone_lowmem_reserve - called whenever
Yaowei Bai34b10062015-09-08 15:04:13 -07007286 * sysctl_lowmem_reserve_ratio changes. Ensures that each zone
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287 * has a correct pages reserved value, so an adequate number of
7288 * pages are left in the zone after a successful __alloc_pages().
7289 */
7290static void setup_per_zone_lowmem_reserve(void)
7291{
7292 struct pglist_data *pgdat;
Christoph Lameter2f6726e2006-09-25 23:31:18 -07007293 enum zone_type j, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007294
KAMEZAWA Hiroyukiec936fc2006-03-27 01:15:59 -08007295 for_each_online_pgdat(pgdat) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007296 for (j = 0; j < MAX_NR_ZONES; j++) {
7297 struct zone *zone = pgdat->node_zones + j;
Arun KS9705bea2018-12-28 00:34:24 -08007298 unsigned long managed_pages = zone_managed_pages(zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007299
7300 zone->lowmem_reserve[j] = 0;
7301
Christoph Lameter2f6726e2006-09-25 23:31:18 -07007302 idx = j;
7303 while (idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007304 struct zone *lower_zone;
7305
Christoph Lameter2f6726e2006-09-25 23:31:18 -07007306 idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007307 lower_zone = pgdat->node_zones + idx;
Joonsoo Kimd3cda232018-04-10 16:30:11 -07007308
7309 if (sysctl_lowmem_reserve_ratio[idx] < 1) {
7310 sysctl_lowmem_reserve_ratio[idx] = 0;
7311 lower_zone->lowmem_reserve[j] = 0;
7312 } else {
7313 lower_zone->lowmem_reserve[j] =
7314 managed_pages / sysctl_lowmem_reserve_ratio[idx];
7315 }
Arun KS9705bea2018-12-28 00:34:24 -08007316 managed_pages += zone_managed_pages(lower_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007317 }
7318 }
7319 }
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07007320
7321 /* update totalreserve_pages */
7322 calculate_totalreserve_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007323}
7324
Mel Gormancfd3da12011-04-25 21:36:42 +00007325static void __setup_per_zone_wmarks(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007326{
7327 unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
7328 unsigned long lowmem_pages = 0;
7329 struct zone *zone;
7330 unsigned long flags;
7331
7332 /* Calculate total number of !ZONE_HIGHMEM pages */
7333 for_each_zone(zone) {
7334 if (!is_highmem(zone))
Arun KS9705bea2018-12-28 00:34:24 -08007335 lowmem_pages += zone_managed_pages(zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007336 }
7337
7338 for_each_zone(zone) {
Andrew Mortonac924c62006-05-15 09:43:59 -07007339 u64 tmp;
7340
Gerald Schaefer1125b4e2008-10-18 20:27:11 -07007341 spin_lock_irqsave(&zone->lock, flags);
Arun KS9705bea2018-12-28 00:34:24 -08007342 tmp = (u64)pages_min * zone_managed_pages(zone);
Andrew Mortonac924c62006-05-15 09:43:59 -07007343 do_div(tmp, lowmem_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344 if (is_highmem(zone)) {
7345 /*
Nick Piggin669ed172005-11-13 16:06:45 -08007346 * __GFP_HIGH and PF_MEMALLOC allocations usually don't
7347 * need highmem pages, so cap pages_min to a small
7348 * value here.
7349 *
Mel Gorman41858962009-06-16 15:32:12 -07007350 * The WMARK_HIGH-WMARK_LOW and (WMARK_LOW-WMARK_MIN)
Yaowei Bai42ff2702015-04-14 15:47:14 -07007351 * deltas control asynch page reclaim, and so should
Nick Piggin669ed172005-11-13 16:06:45 -08007352 * not be capped for highmem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007353 */
Andrew Morton90ae8d62013-02-22 16:32:22 -08007354 unsigned long min_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355
Arun KS9705bea2018-12-28 00:34:24 -08007356 min_pages = zone_managed_pages(zone) / 1024;
Andrew Morton90ae8d62013-02-22 16:32:22 -08007357 min_pages = clamp(min_pages, SWAP_CLUSTER_MAX, 128UL);
Mel Gorman41858962009-06-16 15:32:12 -07007358 zone->watermark[WMARK_MIN] = min_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007359 } else {
Nick Piggin669ed172005-11-13 16:06:45 -08007360 /*
7361 * If it's a lowmem zone, reserve a number of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07007362 * proportionate to the zone's size.
7363 */
Mel Gorman41858962009-06-16 15:32:12 -07007364 zone->watermark[WMARK_MIN] = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007365 }
7366
Johannes Weiner795ae7a2016-03-17 14:19:14 -07007367 /*
7368 * Set the kswapd watermarks distance according to the
7369 * scale factor in proportion to available memory, but
7370 * ensure a minimum size on small systems.
7371 */
7372 tmp = max_t(u64, tmp >> 2,
Arun KS9705bea2018-12-28 00:34:24 -08007373 mult_frac(zone_managed_pages(zone),
Johannes Weiner795ae7a2016-03-17 14:19:14 -07007374 watermark_scale_factor, 10000));
7375
7376 zone->watermark[WMARK_LOW] = min_wmark_pages(zone) + tmp;
7377 zone->watermark[WMARK_HIGH] = min_wmark_pages(zone) + tmp * 2;
Marek Szyprowski49f223a2012-01-25 12:49:24 +01007378
Gerald Schaefer1125b4e2008-10-18 20:27:11 -07007379 spin_unlock_irqrestore(&zone->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007380 }
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07007381
7382 /* update totalreserve_pages */
7383 calculate_totalreserve_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007384}
7385
Mel Gormancfd3da12011-04-25 21:36:42 +00007386/**
7387 * setup_per_zone_wmarks - called when min_free_kbytes changes
7388 * or when memory is hot-{added|removed}
7389 *
7390 * Ensures that the watermark[min,low,high] values for each zone are set
7391 * correctly with respect to min_free_kbytes.
7392 */
7393void setup_per_zone_wmarks(void)
7394{
Michal Hockob93e0f32017-09-06 16:20:37 -07007395 static DEFINE_SPINLOCK(lock);
7396
7397 spin_lock(&lock);
Mel Gormancfd3da12011-04-25 21:36:42 +00007398 __setup_per_zone_wmarks();
Michal Hockob93e0f32017-09-06 16:20:37 -07007399 spin_unlock(&lock);
Mel Gormancfd3da12011-04-25 21:36:42 +00007400}
7401
Randy Dunlap55a44622009-09-21 17:01:20 -07007402/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007403 * Initialise min_free_kbytes.
7404 *
7405 * For small machines we want it small (128k min). For large machines
7406 * we want it large (64MB max). But it is not linear, because network
7407 * bandwidth does not increase linearly with machine size. We use
7408 *
Pintu Kumarb8af2942013-09-11 14:20:34 -07007409 * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007410 * min_free_kbytes = sqrt(lowmem_kbytes * 16)
7411 *
7412 * which yields
7413 *
7414 * 16MB: 512k
7415 * 32MB: 724k
7416 * 64MB: 1024k
7417 * 128MB: 1448k
7418 * 256MB: 2048k
7419 * 512MB: 2896k
7420 * 1024MB: 4096k
7421 * 2048MB: 5792k
7422 * 4096MB: 8192k
7423 * 8192MB: 11584k
7424 * 16384MB: 16384k
7425 */
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07007426int __meminit init_per_zone_wmark_min(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007427{
7428 unsigned long lowmem_kbytes;
Michal Hocko5f127332013-07-08 16:00:40 -07007429 int new_min_free_kbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007430
7431 lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
Michal Hocko5f127332013-07-08 16:00:40 -07007432 new_min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433
Michal Hocko5f127332013-07-08 16:00:40 -07007434 if (new_min_free_kbytes > user_min_free_kbytes) {
7435 min_free_kbytes = new_min_free_kbytes;
7436 if (min_free_kbytes < 128)
7437 min_free_kbytes = 128;
7438 if (min_free_kbytes > 65536)
7439 min_free_kbytes = 65536;
7440 } else {
7441 pr_warn("min_free_kbytes is not updated to %d because user defined value %d is preferred\n",
7442 new_min_free_kbytes, user_min_free_kbytes);
7443 }
Minchan Kimbc75d332009-06-16 15:32:48 -07007444 setup_per_zone_wmarks();
KOSAKI Motohiroa6cccdc2011-05-24 17:11:33 -07007445 refresh_zone_stat_thresholds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007446 setup_per_zone_lowmem_reserve();
Joonsoo Kim6423aa82016-08-10 16:27:49 -07007447
7448#ifdef CONFIG_NUMA
7449 setup_min_unmapped_ratio();
7450 setup_min_slab_ratio();
7451#endif
7452
Linus Torvalds1da177e2005-04-16 15:20:36 -07007453 return 0;
7454}
Jason Baronbc22af742016-05-05 16:22:12 -07007455core_initcall(init_per_zone_wmark_min)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007456
7457/*
Pintu Kumarb8af2942013-09-11 14:20:34 -07007458 * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459 * that we can call two helper functions whenever min_free_kbytes
7460 * changes.
7461 */
Joe Perchescccad5b2014-06-06 14:38:09 -07007462int min_free_kbytes_sysctl_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07007463 void __user *buffer, size_t *length, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464{
Han Pingtianda8c7572014-01-23 15:53:17 -08007465 int rc;
7466
7467 rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
7468 if (rc)
7469 return rc;
7470
Michal Hocko5f127332013-07-08 16:00:40 -07007471 if (write) {
7472 user_min_free_kbytes = min_free_kbytes;
Minchan Kimbc75d332009-06-16 15:32:48 -07007473 setup_per_zone_wmarks();
Michal Hocko5f127332013-07-08 16:00:40 -07007474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007475 return 0;
7476}
7477
Johannes Weiner795ae7a2016-03-17 14:19:14 -07007478int watermark_scale_factor_sysctl_handler(struct ctl_table *table, int write,
7479 void __user *buffer, size_t *length, loff_t *ppos)
7480{
7481 int rc;
7482
7483 rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
7484 if (rc)
7485 return rc;
7486
7487 if (write)
7488 setup_per_zone_wmarks();
7489
7490 return 0;
7491}
7492
Christoph Lameter96146342006-07-03 00:24:13 -07007493#ifdef CONFIG_NUMA
Joonsoo Kim6423aa82016-08-10 16:27:49 -07007494static void setup_min_unmapped_ratio(void)
Christoph Lameter96146342006-07-03 00:24:13 -07007495{
Joonsoo Kim6423aa82016-08-10 16:27:49 -07007496 pg_data_t *pgdat;
Christoph Lameter96146342006-07-03 00:24:13 -07007497 struct zone *zone;
Christoph Lameter96146342006-07-03 00:24:13 -07007498
Mel Gormana5f5f912016-07-28 15:46:32 -07007499 for_each_online_pgdat(pgdat)
Joonsoo Kim81cbcbc2016-08-10 16:27:46 -07007500 pgdat->min_unmapped_pages = 0;
Mel Gormana5f5f912016-07-28 15:46:32 -07007501
Christoph Lameter96146342006-07-03 00:24:13 -07007502 for_each_zone(zone)
Arun KS9705bea2018-12-28 00:34:24 -08007503 zone->zone_pgdat->min_unmapped_pages += (zone_managed_pages(zone) *
7504 sysctl_min_unmapped_ratio) / 100;
Christoph Lameter96146342006-07-03 00:24:13 -07007505}
Christoph Lameter0ff38492006-09-25 23:31:52 -07007506
Joonsoo Kim6423aa82016-08-10 16:27:49 -07007507
7508int sysctl_min_unmapped_ratio_sysctl_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07007509 void __user *buffer, size_t *length, loff_t *ppos)
Christoph Lameter0ff38492006-09-25 23:31:52 -07007510{
Christoph Lameter0ff38492006-09-25 23:31:52 -07007511 int rc;
7512
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07007513 rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
Christoph Lameter0ff38492006-09-25 23:31:52 -07007514 if (rc)
7515 return rc;
7516
Joonsoo Kim6423aa82016-08-10 16:27:49 -07007517 setup_min_unmapped_ratio();
7518
7519 return 0;
7520}
7521
7522static void setup_min_slab_ratio(void)
7523{
7524 pg_data_t *pgdat;
7525 struct zone *zone;
7526
Mel Gormana5f5f912016-07-28 15:46:32 -07007527 for_each_online_pgdat(pgdat)
7528 pgdat->min_slab_pages = 0;
7529
Christoph Lameter0ff38492006-09-25 23:31:52 -07007530 for_each_zone(zone)
Arun KS9705bea2018-12-28 00:34:24 -08007531 zone->zone_pgdat->min_slab_pages += (zone_managed_pages(zone) *
7532 sysctl_min_slab_ratio) / 100;
Joonsoo Kim6423aa82016-08-10 16:27:49 -07007533}
7534
7535int sysctl_min_slab_ratio_sysctl_handler(struct ctl_table *table, int write,
7536 void __user *buffer, size_t *length, loff_t *ppos)
7537{
7538 int rc;
7539
7540 rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
7541 if (rc)
7542 return rc;
7543
7544 setup_min_slab_ratio();
7545
Christoph Lameter0ff38492006-09-25 23:31:52 -07007546 return 0;
7547}
Christoph Lameter96146342006-07-03 00:24:13 -07007548#endif
7549
Linus Torvalds1da177e2005-04-16 15:20:36 -07007550/*
7551 * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
7552 * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
7553 * whenever sysctl_lowmem_reserve_ratio changes.
7554 *
7555 * The reserve ratio obviously has absolutely no relation with the
Mel Gorman41858962009-06-16 15:32:12 -07007556 * minimum watermarks. The lowmem reserve ratio can only make sense
Linus Torvalds1da177e2005-04-16 15:20:36 -07007557 * if in function of the boot time zone sizes.
7558 */
Joe Perchescccad5b2014-06-06 14:38:09 -07007559int lowmem_reserve_ratio_sysctl_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07007560 void __user *buffer, size_t *length, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007561{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07007562 proc_dointvec_minmax(table, write, buffer, length, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007563 setup_per_zone_lowmem_reserve();
7564 return 0;
7565}
7566
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08007567/*
7568 * percpu_pagelist_fraction - changes the pcp->high for each zone on each
Pintu Kumarb8af2942013-09-11 14:20:34 -07007569 * cpu. It is the fraction of total pages in each zone that a hot per cpu
7570 * pagelist can have before it gets flushed back to buddy allocator.
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08007571 */
Joe Perchescccad5b2014-06-06 14:38:09 -07007572int percpu_pagelist_fraction_sysctl_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07007573 void __user *buffer, size_t *length, loff_t *ppos)
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08007574{
7575 struct zone *zone;
David Rientjes7cd2b0a2014-06-23 13:22:04 -07007576 int old_percpu_pagelist_fraction;
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08007577 int ret;
7578
Cody P Schaferc8e251f2013-07-03 15:01:29 -07007579 mutex_lock(&pcp_batch_high_lock);
David Rientjes7cd2b0a2014-06-23 13:22:04 -07007580 old_percpu_pagelist_fraction = percpu_pagelist_fraction;
7581
7582 ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
7583 if (!write || ret < 0)
7584 goto out;
7585
7586 /* Sanity checking to avoid pcp imbalance */
7587 if (percpu_pagelist_fraction &&
7588 percpu_pagelist_fraction < MIN_PERCPU_PAGELIST_FRACTION) {
7589 percpu_pagelist_fraction = old_percpu_pagelist_fraction;
7590 ret = -EINVAL;
7591 goto out;
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08007592 }
David Rientjes7cd2b0a2014-06-23 13:22:04 -07007593
7594 /* No change? */
7595 if (percpu_pagelist_fraction == old_percpu_pagelist_fraction)
7596 goto out;
7597
7598 for_each_populated_zone(zone) {
7599 unsigned int cpu;
7600
7601 for_each_possible_cpu(cpu)
7602 pageset_set_high_and_batch(zone,
7603 per_cpu_ptr(zone->pageset, cpu));
7604 }
7605out:
Cody P Schaferc8e251f2013-07-03 15:01:29 -07007606 mutex_unlock(&pcp_batch_high_lock);
David Rientjes7cd2b0a2014-06-23 13:22:04 -07007607 return ret;
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08007608}
7609
Rasmus Villemoesa9919c72015-06-24 16:56:28 -07007610#ifdef CONFIG_NUMA
David S. Millerf034b5d2006-08-24 03:08:07 -07007611int hashdist = HASHDIST_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007612
Linus Torvalds1da177e2005-04-16 15:20:36 -07007613static int __init set_hashdist(char *str)
7614{
7615 if (!str)
7616 return 0;
7617 hashdist = simple_strtoul(str, &str, 0);
7618 return 1;
7619}
7620__setup("hashdist=", set_hashdist);
7621#endif
7622
Srikar Dronamrajuf6f34b42016-10-07 16:59:15 -07007623#ifndef __HAVE_ARCH_RESERVED_KERNEL_PAGES
7624/*
7625 * Returns the number of pages that arch has reserved but
7626 * is not known to alloc_large_system_hash().
7627 */
7628static unsigned long __init arch_reserved_kernel_pages(void)
7629{
7630 return 0;
7631}
7632#endif
7633
Linus Torvalds1da177e2005-04-16 15:20:36 -07007634/*
Pavel Tatashin90172172017-07-06 15:39:14 -07007635 * Adaptive scale is meant to reduce sizes of hash tables on large memory
7636 * machines. As memory size is increased the scale is also increased but at
7637 * slower pace. Starting from ADAPT_SCALE_BASE (64G), every time memory
7638 * quadruples the scale is increased by one, which means the size of hash table
7639 * only doubles, instead of quadrupling as well.
7640 * Because 32-bit systems cannot have large physical memory, where this scaling
7641 * makes sense, it is disabled on such platforms.
7642 */
7643#if __BITS_PER_LONG > 32
7644#define ADAPT_SCALE_BASE (64ul << 30)
7645#define ADAPT_SCALE_SHIFT 2
7646#define ADAPT_SCALE_NPAGES (ADAPT_SCALE_BASE >> PAGE_SHIFT)
7647#endif
7648
7649/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007650 * allocate a large system hash table from bootmem
7651 * - it is assumed that the hash table must contain an exact power-of-2
7652 * quantity of entries
7653 * - limit is the number of hash buckets, not the total allocation size
7654 */
7655void *__init alloc_large_system_hash(const char *tablename,
7656 unsigned long bucketsize,
7657 unsigned long numentries,
7658 int scale,
7659 int flags,
7660 unsigned int *_hash_shift,
7661 unsigned int *_hash_mask,
Tim Bird31fe62b2012-05-23 13:33:35 +00007662 unsigned long low_limit,
7663 unsigned long high_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007664{
Tim Bird31fe62b2012-05-23 13:33:35 +00007665 unsigned long long max = high_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007666 unsigned long log2qty, size;
7667 void *table = NULL;
Pavel Tatashin3749a8f2017-07-06 15:39:08 -07007668 gfp_t gfp_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007669
7670 /* allow the kernel cmdline to have a say */
7671 if (!numentries) {
7672 /* round applicable memory size up to nearest megabyte */
Andrew Morton04903662006-12-06 20:37:33 -08007673 numentries = nr_kernel_pages;
Srikar Dronamrajuf6f34b42016-10-07 16:59:15 -07007674 numentries -= arch_reserved_kernel_pages();
Jerry Zhoua7e83312013-09-11 14:20:26 -07007675
7676 /* It isn't necessary when PAGE_SIZE >= 1MB */
7677 if (PAGE_SHIFT < 20)
7678 numentries = round_up(numentries, (1<<20)/PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007679
Pavel Tatashin90172172017-07-06 15:39:14 -07007680#if __BITS_PER_LONG > 32
7681 if (!high_limit) {
7682 unsigned long adapt;
7683
7684 for (adapt = ADAPT_SCALE_NPAGES; adapt < numentries;
7685 adapt <<= ADAPT_SCALE_SHIFT)
7686 scale++;
7687 }
7688#endif
7689
Linus Torvalds1da177e2005-04-16 15:20:36 -07007690 /* limit to 1 bucket per 2^scale bytes of low memory */
7691 if (scale > PAGE_SHIFT)
7692 numentries >>= (scale - PAGE_SHIFT);
7693 else
7694 numentries <<= (PAGE_SHIFT - scale);
Paul Mundt9ab37b82007-01-05 16:36:30 -08007695
7696 /* Make sure we've got at least a 0-order allocation.. */
Jan Beulich2c85f512009-09-21 17:03:07 -07007697 if (unlikely(flags & HASH_SMALL)) {
7698 /* Makes no sense without HASH_EARLY */
7699 WARN_ON(!(flags & HASH_EARLY));
7700 if (!(numentries >> *_hash_shift)) {
7701 numentries = 1UL << *_hash_shift;
7702 BUG_ON(!numentries);
7703 }
7704 } else if (unlikely((numentries * bucketsize) < PAGE_SIZE))
Paul Mundt9ab37b82007-01-05 16:36:30 -08007705 numentries = PAGE_SIZE / bucketsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007706 }
John Hawkes6e692ed2006-03-25 03:08:02 -08007707 numentries = roundup_pow_of_two(numentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007708
7709 /* limit allocation size to 1/16 total memory by default */
7710 if (max == 0) {
7711 max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
7712 do_div(max, bucketsize);
7713 }
Dimitri Sivanich074b8512012-02-08 12:39:07 -08007714 max = min(max, 0x80000000ULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007715
Tim Bird31fe62b2012-05-23 13:33:35 +00007716 if (numentries < low_limit)
7717 numentries = low_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007718 if (numentries > max)
7719 numentries = max;
7720
David Howellsf0d1b0b2006-12-08 02:37:49 -08007721 log2qty = ilog2(numentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007722
Pavel Tatashin3749a8f2017-07-06 15:39:08 -07007723 gfp_flags = (flags & HASH_ZERO) ? GFP_ATOMIC | __GFP_ZERO : GFP_ATOMIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007724 do {
7725 size = bucketsize << log2qty;
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08007726 if (flags & HASH_EARLY) {
7727 if (flags & HASH_ZERO)
Mike Rapoport7e1c4e22018-10-30 15:09:57 -07007728 table = memblock_alloc_nopanic(size,
7729 SMP_CACHE_BYTES);
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08007730 else
Mike Rapoport7e1c4e22018-10-30 15:09:57 -07007731 table = memblock_alloc_raw(size,
7732 SMP_CACHE_BYTES);
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08007733 } else if (hashdist) {
Pavel Tatashin3749a8f2017-07-06 15:39:08 -07007734 table = __vmalloc(size, gfp_flags, PAGE_KERNEL);
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08007735 } else {
Eric Dumazet1037b832007-07-15 23:38:05 -07007736 /*
7737 * If bucketsize is not a power-of-two, we may free
Mel Gormana1dd2682009-06-16 15:32:19 -07007738 * some pages at the end of hash table which
7739 * alloc_pages_exact() automatically does
Eric Dumazet1037b832007-07-15 23:38:05 -07007740 */
Catalin Marinas264ef8a2009-07-07 10:33:01 +01007741 if (get_order(size) < MAX_ORDER) {
Pavel Tatashin3749a8f2017-07-06 15:39:08 -07007742 table = alloc_pages_exact(size, gfp_flags);
7743 kmemleak_alloc(table, size, 1, gfp_flags);
Catalin Marinas264ef8a2009-07-07 10:33:01 +01007744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007745 }
7746 } while (!table && size > PAGE_SIZE && --log2qty);
7747
7748 if (!table)
7749 panic("Failed to allocate %s hash table\n", tablename);
7750
Joe Perches11705322016-03-17 14:19:50 -07007751 pr_info("%s hash table entries: %ld (order: %d, %lu bytes)\n",
7752 tablename, 1UL << log2qty, ilog2(size) - PAGE_SHIFT, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007753
7754 if (_hash_shift)
7755 *_hash_shift = log2qty;
7756 if (_hash_mask)
7757 *_hash_mask = (1 << log2qty) - 1;
7758
7759 return table;
7760}
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -08007761
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -07007762/*
Minchan Kim80934512012-07-31 16:43:01 -07007763 * This function checks whether pageblock includes unmovable pages or not.
7764 * If @count is not zero, it is okay to include less @count unmovable pages
7765 *
Pintu Kumarb8af2942013-09-11 14:20:34 -07007766 * PageLRU check without isolation or lru_lock could race so that
Yisheng Xie0efadf42017-02-24 14:57:39 -08007767 * MIGRATE_MOVABLE block might include unmovable pages. And __PageMovable
7768 * check without lock_page also may miss some movable non-lru pages at
7769 * race condition. So you can't expect this function should be exact.
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -07007770 */
Wen Congyangb023f462012-12-11 16:00:45 -08007771bool has_unmovable_pages(struct zone *zone, struct page *page, int count,
Michal Hockod381c542018-12-28 00:33:56 -08007772 int migratetype, int flags)
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07007773{
7774 unsigned long pfn, iter, found;
Michal Nazarewicz47118af2011-12-29 13:09:50 +01007775
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07007776 /*
Michal Hocko15c30bc2018-05-25 14:47:42 -07007777 * TODO we could make this much more efficient by not checking every
7778 * page in the range if we know all of them are in MOVABLE_ZONE and
7779 * that the movable zone guarantees that pages are migratable but
7780 * the later is not the case right now unfortunatelly. E.g. movablecore
7781 * can still lead to having bootmem allocations in zone_movable.
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07007782 */
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07007783
Michal Hocko4da2ce22017-11-15 17:33:26 -08007784 /*
7785 * CMA allocations (alloc_contig_range) really need to mark isolate
7786 * CMA pageblocks even when they are not movable in fact so consider
7787 * them movable here.
7788 */
7789 if (is_migrate_cma(migratetype) &&
7790 is_migrate_cma(get_pageblock_migratetype(page)))
7791 return false;
7792
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07007793 pfn = page_to_pfn(page);
7794 for (found = 0, iter = 0; iter < pageblock_nr_pages; iter++) {
7795 unsigned long check = pfn + iter;
7796
Namhyung Kim29723fc2011-02-25 14:44:25 -08007797 if (!pfn_valid_within(check))
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07007798 continue;
Namhyung Kim29723fc2011-02-25 14:44:25 -08007799
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07007800 page = pfn_to_page(check);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07007801
Michal Hockod7ab3672017-11-15 17:33:30 -08007802 if (PageReserved(page))
Michal Hocko15c30bc2018-05-25 14:47:42 -07007803 goto unmovable;
Michal Hockod7ab3672017-11-15 17:33:30 -08007804
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07007805 /*
Michal Hocko9d789992018-11-16 15:08:15 -08007806 * If the zone is movable and we have ruled out all reserved
7807 * pages then it should be reasonably safe to assume the rest
7808 * is movable.
7809 */
7810 if (zone_idx(zone) == ZONE_MOVABLE)
7811 continue;
7812
7813 /*
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07007814 * Hugepages are not in LRU lists, but they're movable.
7815 * We need not scan over tail pages bacause we don't
7816 * handle each tail page individually in migration.
7817 */
7818 if (PageHuge(page)) {
Oscar Salvador17e2e7d2018-12-21 14:31:00 -08007819 struct page *head = compound_head(page);
7820 unsigned int skip_pages;
Aneesh Kumar K.V464c7ff2018-09-04 15:45:59 -07007821
Oscar Salvador17e2e7d2018-12-21 14:31:00 -08007822 if (!hugepage_migration_supported(page_hstate(head)))
Aneesh Kumar K.V464c7ff2018-09-04 15:45:59 -07007823 goto unmovable;
7824
Oscar Salvador17e2e7d2018-12-21 14:31:00 -08007825 skip_pages = (1 << compound_order(head)) - (page - head);
7826 iter += skip_pages - 1;
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07007827 continue;
7828 }
7829
Minchan Kim97d255c2012-07-31 16:42:59 -07007830 /*
7831 * We can't use page_count without pin a page
7832 * because another CPU can free compound page.
7833 * This check already skips compound tails of THP
Joonsoo Kim0139aa72016-05-19 17:10:49 -07007834 * because their page->_refcount is zero at all time.
Minchan Kim97d255c2012-07-31 16:42:59 -07007835 */
Joonsoo Kimfe896d12016-03-17 14:19:26 -07007836 if (!page_ref_count(page)) {
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07007837 if (PageBuddy(page))
7838 iter += (1 << page_order(page)) - 1;
7839 continue;
7840 }
Minchan Kim97d255c2012-07-31 16:42:59 -07007841
Wen Congyangb023f462012-12-11 16:00:45 -08007842 /*
7843 * The HWPoisoned page may be not in buddy system, and
7844 * page_count() is not 0.
7845 */
Michal Hockod381c542018-12-28 00:33:56 -08007846 if ((flags & SKIP_HWPOISON) && PageHWPoison(page))
Wen Congyangb023f462012-12-11 16:00:45 -08007847 continue;
7848
Yisheng Xie0efadf42017-02-24 14:57:39 -08007849 if (__PageMovable(page))
7850 continue;
7851
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07007852 if (!PageLRU(page))
7853 found++;
7854 /*
Johannes Weiner6b4f7792014-12-12 16:56:13 -08007855 * If there are RECLAIMABLE pages, we need to check
7856 * it. But now, memory offline itself doesn't call
7857 * shrink_node_slabs() and it still to be fixed.
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07007858 */
7859 /*
7860 * If the page is not RAM, page_count()should be 0.
7861 * we don't need more check. This is an _used_ not-movable page.
7862 *
7863 * The problematic thing here is PG_reserved pages. PG_reserved
7864 * is set to both of a memory hole page and a _used_ kernel
7865 * page at boot.
7866 */
7867 if (found > count)
Michal Hocko15c30bc2018-05-25 14:47:42 -07007868 goto unmovable;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07007869 }
Minchan Kim80934512012-07-31 16:43:01 -07007870 return false;
Michal Hocko15c30bc2018-05-25 14:47:42 -07007871unmovable:
7872 WARN_ON_ONCE(zone_idx(zone) == ZONE_MOVABLE);
Michal Hockod381c542018-12-28 00:33:56 -08007873 if (flags & REPORT_FAILURE)
7874 dump_page(pfn_to_page(pfn+iter), "unmovable page");
Michal Hocko15c30bc2018-05-25 14:47:42 -07007875 return true;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07007876}
7877
Vlastimil Babka080fe202016-02-05 15:36:41 -08007878#if (defined(CONFIG_MEMORY_ISOLATION) && defined(CONFIG_COMPACTION)) || defined(CONFIG_CMA)
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007879
7880static unsigned long pfn_max_align_down(unsigned long pfn)
7881{
7882 return pfn & ~(max_t(unsigned long, MAX_ORDER_NR_PAGES,
7883 pageblock_nr_pages) - 1);
7884}
7885
7886static unsigned long pfn_max_align_up(unsigned long pfn)
7887{
7888 return ALIGN(pfn, max_t(unsigned long, MAX_ORDER_NR_PAGES,
7889 pageblock_nr_pages));
7890}
7891
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007892/* [start, end) must belong to a single zone. */
Mel Gormanbb13ffe2012-10-08 16:32:41 -07007893static int __alloc_contig_migrate_range(struct compact_control *cc,
7894 unsigned long start, unsigned long end)
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007895{
7896 /* This function is based on compact_zone() from compaction.c. */
Minchan Kimbeb51ea2012-10-08 16:33:51 -07007897 unsigned long nr_reclaimed;
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007898 unsigned long pfn = start;
7899 unsigned int tries = 0;
7900 int ret = 0;
7901
Marek Szyprowskibe49a6e2012-12-12 13:51:19 -08007902 migrate_prep();
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007903
Mel Gormanbb13ffe2012-10-08 16:32:41 -07007904 while (pfn < end || !list_empty(&cc->migratepages)) {
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007905 if (fatal_signal_pending(current)) {
7906 ret = -EINTR;
7907 break;
7908 }
7909
Mel Gormanbb13ffe2012-10-08 16:32:41 -07007910 if (list_empty(&cc->migratepages)) {
7911 cc->nr_migratepages = 0;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07007912 pfn = isolate_migratepages_range(cc, pfn, end);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007913 if (!pfn) {
7914 ret = -EINTR;
7915 break;
7916 }
7917 tries = 0;
7918 } else if (++tries == 5) {
7919 ret = ret < 0 ? ret : -EBUSY;
7920 break;
7921 }
7922
Minchan Kimbeb51ea2012-10-08 16:33:51 -07007923 nr_reclaimed = reclaim_clean_pages_from_list(cc->zone,
7924 &cc->migratepages);
7925 cc->nr_migratepages -= nr_reclaimed;
Minchan Kim02c6de82012-10-08 16:31:55 -07007926
Hugh Dickins9c620e22013-02-22 16:35:14 -08007927 ret = migrate_pages(&cc->migratepages, alloc_migrate_target,
Anshuman Khandual31025352018-04-05 16:22:08 -07007928 NULL, 0, cc->mode, MR_CONTIG_RANGE);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007929 }
Srinivas Pandruvada2a6f5122013-02-22 16:32:09 -08007930 if (ret < 0) {
7931 putback_movable_pages(&cc->migratepages);
7932 return ret;
7933 }
7934 return 0;
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007935}
7936
7937/**
7938 * alloc_contig_range() -- tries to allocate given range of pages
7939 * @start: start PFN to allocate
7940 * @end: one-past-the-last PFN to allocate
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02007941 * @migratetype: migratetype of the underlaying pageblocks (either
7942 * #MIGRATE_MOVABLE or #MIGRATE_CMA). All pageblocks
7943 * in range must have the same migratetype and it must
7944 * be either of the two.
Lucas Stachca96b622017-02-24 14:58:37 -08007945 * @gfp_mask: GFP mask to use during compaction
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007946 *
7947 * The PFN range does not have to be pageblock or MAX_ORDER_NR_PAGES
Mike Kravetz2c7452a2018-04-05 16:25:26 -07007948 * aligned. The PFN range must belong to a single zone.
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007949 *
Mike Kravetz2c7452a2018-04-05 16:25:26 -07007950 * The first thing this routine does is attempt to MIGRATE_ISOLATE all
7951 * pageblocks in the range. Once isolated, the pageblocks should not
7952 * be modified by others.
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007953 *
7954 * Returns zero on success or negative error code. On success all
7955 * pages which PFN is in [start, end) are allocated for the caller and
7956 * need to be freed with free_contig_range().
7957 */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02007958int alloc_contig_range(unsigned long start, unsigned long end,
Lucas Stachca96b622017-02-24 14:58:37 -08007959 unsigned migratetype, gfp_t gfp_mask)
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007960{
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007961 unsigned long outer_start, outer_end;
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08007962 unsigned int order;
7963 int ret = 0;
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007964
Mel Gormanbb13ffe2012-10-08 16:32:41 -07007965 struct compact_control cc = {
7966 .nr_migratepages = 0,
7967 .order = -1,
7968 .zone = page_zone(pfn_to_page(start)),
David Rientjese0b9dae2014-06-04 16:08:28 -07007969 .mode = MIGRATE_SYNC,
Mel Gormanbb13ffe2012-10-08 16:32:41 -07007970 .ignore_skip_hint = true,
Vlastimil Babka2583d672017-11-17 15:26:38 -08007971 .no_set_skip_hint = true,
Michal Hocko7dea19f2017-05-03 14:53:15 -07007972 .gfp_mask = current_gfp_context(gfp_mask),
Mel Gormanbb13ffe2012-10-08 16:32:41 -07007973 };
7974 INIT_LIST_HEAD(&cc.migratepages);
7975
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007976 /*
7977 * What we do here is we mark all pageblocks in range as
7978 * MIGRATE_ISOLATE. Because pageblock and max order pages may
7979 * have different sizes, and due to the way page allocator
7980 * work, we align the range to biggest of the two pages so
7981 * that page allocator won't try to merge buddies from
7982 * different pageblocks and change MIGRATE_ISOLATE to some
7983 * other migration type.
7984 *
7985 * Once the pageblocks are marked as MIGRATE_ISOLATE, we
7986 * migrate the pages from an unaligned range (ie. pages that
7987 * we are interested in). This will put all the pages in
7988 * range back to page allocator as MIGRATE_ISOLATE.
7989 *
7990 * When this is done, we take the pages in range from page
7991 * allocator removing them from the buddy system. This way
7992 * page allocator will never consider using them.
7993 *
7994 * This lets us mark the pageblocks back as
7995 * MIGRATE_CMA/MIGRATE_MOVABLE so that free pages in the
7996 * aligned range but not in the unaligned, original range are
7997 * put back to page allocator so that buddy can use them.
7998 */
7999
8000 ret = start_isolate_page_range(pfn_max_align_down(start),
Michal Hockod381c542018-12-28 00:33:56 -08008001 pfn_max_align_up(end), migratetype, 0);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008002 if (ret)
Bob Liu86a595f2012-10-25 13:37:56 -07008003 return ret;
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008004
Joonsoo Kim8ef58492016-01-14 15:18:45 -08008005 /*
8006 * In case of -EBUSY, we'd like to know which page causes problem.
Mike Kravetz63cd4482017-11-29 16:10:01 -08008007 * So, just fall through. test_pages_isolated() has a tracepoint
8008 * which will report the busy page.
8009 *
8010 * It is possible that busy pages could become available before
8011 * the call to test_pages_isolated, and the range will actually be
8012 * allocated. So, if we fall through be sure to clear ret so that
8013 * -EBUSY is not accidentally used or returned to caller.
Joonsoo Kim8ef58492016-01-14 15:18:45 -08008014 */
Mel Gormanbb13ffe2012-10-08 16:32:41 -07008015 ret = __alloc_contig_migrate_range(&cc, start, end);
Joonsoo Kim8ef58492016-01-14 15:18:45 -08008016 if (ret && ret != -EBUSY)
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008017 goto done;
Mike Kravetz63cd4482017-11-29 16:10:01 -08008018 ret =0;
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008019
8020 /*
8021 * Pages from [start, end) are within a MAX_ORDER_NR_PAGES
8022 * aligned blocks that are marked as MIGRATE_ISOLATE. What's
8023 * more, all pages in [start, end) are free in page allocator.
8024 * What we are going to do is to allocate all pages from
8025 * [start, end) (that is remove them from page allocator).
8026 *
8027 * The only problem is that pages at the beginning and at the
8028 * end of interesting range may be not aligned with pages that
8029 * page allocator holds, ie. they can be part of higher order
8030 * pages. Because of this, we reserve the bigger range and
8031 * once this is done free the pages we are not interested in.
8032 *
8033 * We don't have to hold zone->lock here because the pages are
8034 * isolated thus they won't get removed from buddy.
8035 */
8036
8037 lru_add_drain_all();
Vlastimil Babka510f5502014-12-10 15:43:07 -08008038 drain_all_pages(cc.zone);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008039
8040 order = 0;
8041 outer_start = start;
8042 while (!PageBuddy(pfn_to_page(outer_start))) {
8043 if (++order >= MAX_ORDER) {
Joonsoo Kim8ef58492016-01-14 15:18:45 -08008044 outer_start = start;
8045 break;
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008046 }
8047 outer_start &= ~0UL << order;
8048 }
8049
Joonsoo Kim8ef58492016-01-14 15:18:45 -08008050 if (outer_start != start) {
8051 order = page_order(pfn_to_page(outer_start));
8052
8053 /*
8054 * outer_start page could be small order buddy page and
8055 * it doesn't include start page. Adjust outer_start
8056 * in this case to report failed page properly
8057 * on tracepoint in test_pages_isolated()
8058 */
8059 if (outer_start + (1UL << order) <= start)
8060 outer_start = start;
8061 }
8062
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008063 /* Make sure the range is really isolated. */
Wen Congyangb023f462012-12-11 16:00:45 -08008064 if (test_pages_isolated(outer_start, end, false)) {
Jonathan Toppins75dddef2017-08-10 15:23:35 -07008065 pr_info_ratelimited("%s: [%lx, %lx) PFNs busy\n",
Michal Nazarewiczdae803e2014-11-13 15:19:27 -08008066 __func__, outer_start, end);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008067 ret = -EBUSY;
8068 goto done;
8069 }
8070
Marek Szyprowski49f223a2012-01-25 12:49:24 +01008071 /* Grab isolated pages from freelists. */
Mel Gormanbb13ffe2012-10-08 16:32:41 -07008072 outer_end = isolate_freepages_range(&cc, outer_start, end);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008073 if (!outer_end) {
8074 ret = -EBUSY;
8075 goto done;
8076 }
8077
8078 /* Free head and tail (if any) */
8079 if (start != outer_start)
8080 free_contig_range(outer_start, start - outer_start);
8081 if (end != outer_end)
8082 free_contig_range(end, outer_end - end);
8083
8084done:
8085 undo_isolate_page_range(pfn_max_align_down(start),
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02008086 pfn_max_align_up(end), migratetype);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008087 return ret;
8088}
8089
8090void free_contig_range(unsigned long pfn, unsigned nr_pages)
8091{
Marek Szyprowskibcc2b022012-12-20 15:05:18 -08008092 unsigned int count = 0;
8093
8094 for (; nr_pages--; pfn++) {
8095 struct page *page = pfn_to_page(pfn);
8096
8097 count += page_count(page) != 1;
8098 __free_page(page);
8099 }
8100 WARN(count != 0, "%d pages are still in use!\n", count);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01008101}
8102#endif
8103
Joonsoo Kimd883c6c2018-05-23 10:18:21 +09008104#ifdef CONFIG_MEMORY_HOTPLUG
Cody P Schafer0a647f32013-07-03 15:01:33 -07008105/*
8106 * The zone indicated has a new number of managed_pages; batch sizes and percpu
8107 * page high values need to be recalulated.
8108 */
Jiang Liu4ed7e022012-07-31 16:43:35 -07008109void __meminit zone_pcp_update(struct zone *zone)
8110{
Cody P Schafer0a647f32013-07-03 15:01:33 -07008111 unsigned cpu;
Cody P Schaferc8e251f2013-07-03 15:01:29 -07008112 mutex_lock(&pcp_batch_high_lock);
Cody P Schafer0a647f32013-07-03 15:01:33 -07008113 for_each_possible_cpu(cpu)
Cody P Schafer169f6c12013-07-03 15:01:41 -07008114 pageset_set_high_and_batch(zone,
8115 per_cpu_ptr(zone->pageset, cpu));
Cody P Schaferc8e251f2013-07-03 15:01:29 -07008116 mutex_unlock(&pcp_batch_high_lock);
Jiang Liu4ed7e022012-07-31 16:43:35 -07008117}
8118#endif
8119
Jiang Liu340175b2012-07-31 16:43:32 -07008120void zone_pcp_reset(struct zone *zone)
8121{
8122 unsigned long flags;
Minchan Kim5a883812012-10-08 16:33:39 -07008123 int cpu;
8124 struct per_cpu_pageset *pset;
Jiang Liu340175b2012-07-31 16:43:32 -07008125
8126 /* avoid races with drain_pages() */
8127 local_irq_save(flags);
8128 if (zone->pageset != &boot_pageset) {
Minchan Kim5a883812012-10-08 16:33:39 -07008129 for_each_online_cpu(cpu) {
8130 pset = per_cpu_ptr(zone->pageset, cpu);
8131 drain_zonestat(zone, pset);
8132 }
Jiang Liu340175b2012-07-31 16:43:32 -07008133 free_percpu(zone->pageset);
8134 zone->pageset = &boot_pageset;
8135 }
8136 local_irq_restore(flags);
8137}
8138
Wen Congyang6dcd73d2012-12-11 16:01:01 -08008139#ifdef CONFIG_MEMORY_HOTREMOVE
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07008140/*
Joonsoo Kimb9eb6312016-05-19 17:12:06 -07008141 * All pages in the range must be in a single zone and isolated
8142 * before calling this.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07008143 */
8144void
8145__offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
8146{
8147 struct page *page;
8148 struct zone *zone;
Mel Gorman7aeb09f2014-06-04 16:10:21 -07008149 unsigned int order, i;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07008150 unsigned long pfn;
8151 unsigned long flags;
8152 /* find the first valid pfn */
8153 for (pfn = start_pfn; pfn < end_pfn; pfn++)
8154 if (pfn_valid(pfn))
8155 break;
8156 if (pfn == end_pfn)
8157 return;
Michal Hocko2d070ea2017-07-06 15:37:56 -07008158 offline_mem_sections(pfn, end_pfn);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07008159 zone = page_zone(pfn_to_page(pfn));
8160 spin_lock_irqsave(&zone->lock, flags);
8161 pfn = start_pfn;
8162 while (pfn < end_pfn) {
8163 if (!pfn_valid(pfn)) {
8164 pfn++;
8165 continue;
8166 }
8167 page = pfn_to_page(pfn);
Wen Congyangb023f462012-12-11 16:00:45 -08008168 /*
8169 * The HWPoisoned page may be not in buddy system, and
8170 * page_count() is not 0.
8171 */
8172 if (unlikely(!PageBuddy(page) && PageHWPoison(page))) {
8173 pfn++;
8174 SetPageReserved(page);
8175 continue;
8176 }
8177
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07008178 BUG_ON(page_count(page));
8179 BUG_ON(!PageBuddy(page));
8180 order = page_order(page);
8181#ifdef CONFIG_DEBUG_VM
Joe Perches11705322016-03-17 14:19:50 -07008182 pr_info("remove from free list %lx %d %lx\n",
8183 pfn, 1 << order, end_pfn);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07008184#endif
8185 list_del(&page->lru);
8186 rmv_page_order(page);
8187 zone->free_area[order].nr_free--;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07008188 for (i = 0; i < (1 << order); i++)
8189 SetPageReserved((page+i));
8190 pfn += (1 << order);
8191 }
8192 spin_unlock_irqrestore(&zone->lock, flags);
8193}
8194#endif
Wu Fengguang8d22ba12009-12-16 12:19:58 +01008195
Wu Fengguang8d22ba12009-12-16 12:19:58 +01008196bool is_free_buddy_page(struct page *page)
8197{
8198 struct zone *zone = page_zone(page);
8199 unsigned long pfn = page_to_pfn(page);
8200 unsigned long flags;
Mel Gorman7aeb09f2014-06-04 16:10:21 -07008201 unsigned int order;
Wu Fengguang8d22ba12009-12-16 12:19:58 +01008202
8203 spin_lock_irqsave(&zone->lock, flags);
8204 for (order = 0; order < MAX_ORDER; order++) {
8205 struct page *page_head = page - (pfn & ((1 << order) - 1));
8206
8207 if (PageBuddy(page_head) && page_order(page_head) >= order)
8208 break;
8209 }
8210 spin_unlock_irqrestore(&zone->lock, flags);
8211
8212 return order < MAX_ORDER;
8213}
Naoya Horiguchid4ae9912018-08-23 17:00:42 -07008214
8215#ifdef CONFIG_MEMORY_FAILURE
8216/*
8217 * Set PG_hwpoison flag if a given page is confirmed to be a free page. This
8218 * test is performed under the zone lock to prevent a race against page
8219 * allocation.
8220 */
8221bool set_hwpoison_free_buddy_page(struct page *page)
8222{
8223 struct zone *zone = page_zone(page);
8224 unsigned long pfn = page_to_pfn(page);
8225 unsigned long flags;
8226 unsigned int order;
8227 bool hwpoisoned = false;
8228
8229 spin_lock_irqsave(&zone->lock, flags);
8230 for (order = 0; order < MAX_ORDER; order++) {
8231 struct page *page_head = page - (pfn & ((1 << order) - 1));
8232
8233 if (PageBuddy(page_head) && page_order(page_head) >= order) {
8234 if (!TestSetPageHWPoison(page))
8235 hwpoisoned = true;
8236 break;
8237 }
8238 }
8239 spin_unlock_irqrestore(&zone->lock, flags);
8240
8241 return hwpoisoned;
8242}
8243#endif