blob: bda48518524d0a6259c0e7c5f0194f97c2c540e1 [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>
19#include <linux/swap.h>
20#include <linux/interrupt.h>
21#include <linux/pagemap.h>
KOSAKI Motohiro10ed2732008-03-04 14:28:32 -080022#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/bootmem.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>
Vegard Nossumb1eeab62008-11-25 16:55:53 +010027#include <linux/kmemcheck.h>
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -080028#include <linux/kasan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/module.h>
30#include <linux/suspend.h>
31#include <linux/pagevec.h>
32#include <linux/blkdev.h>
33#include <linux/slab.h>
Dave Hansena238ab52011-05-24 17:12:16 -070034#include <linux/ratelimit.h>
David Rientjes5a3135c22007-10-16 23:25:53 -070035#include <linux/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/notifier.h>
37#include <linux/topology.h>
38#include <linux/sysctl.h>
39#include <linux/cpu.h>
40#include <linux/cpuset.h>
Dave Hansenbdc8cb92005-10-29 18:16:53 -070041#include <linux/memory_hotplug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/nodemask.h>
43#include <linux/vmalloc.h>
KOSAKI Motohiroa6cccdc2011-05-24 17:11:33 -070044#include <linux/vmstat.h>
Christoph Lameter4be38e32006-01-06 00:11:17 -080045#include <linux/mempolicy.h>
Dan Williams4b94ffd2016-01-15 16:56:22 -080046#include <linux/memremap.h>
Yasunori Goto68113782006-06-23 02:03:11 -070047#include <linux/stop_machine.h>
Mel Gormanc7132162006-09-27 01:49:43 -070048#include <linux/sort.h>
49#include <linux/pfn.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070050#include <linux/backing-dev.h>
Akinobu Mita933e3122006-12-08 02:39:45 -080051#include <linux/fault-inject.h>
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -070052#include <linux/page-isolation.h>
Joonsoo Kimeefa8642014-12-12 16:55:46 -080053#include <linux/page_ext.h>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070054#include <linux/debugobjects.h>
Catalin Marinasdbb1f812009-06-11 13:23:19 +010055#include <linux/kmemleak.h>
Mel Gorman56de7262010-05-24 14:32:30 -070056#include <linux/compaction.h>
Mel Gorman0d3d0622009-09-21 17:02:44 -070057#include <trace/events/kmem.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070058#include <linux/prefetch.h>
Lisa Du6e543d52013-09-11 14:22:36 -070059#include <linux/mm_inline.h>
Michal Nazarewicz041d3a82011-12-29 13:09:50 +010060#include <linux/migrate.h>
Joonsoo Kime30825f2014-12-12 16:55:49 -080061#include <linux/page_ext.h>
David Rientjes949f7ec2013-04-29 15:07:48 -070062#include <linux/hugetlb.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060063#include <linux/sched/rt.h>
Joonsoo Kim48c96a32014-12-12 16:56:01 -080064#include <linux/page_owner.h>
Mel Gorman0e1cc952015-06-30 14:57:27 -070065#include <linux/kthread.h>
Vladimir Davydov49491482016-07-26 15:24:24 -070066#include <linux/memcontrol.h>
Johannes Weiner3df0e592018-10-26 15:06:27 -070067#include <linux/psi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Jiang Liu7ee3d4e2013-07-03 15:03:41 -070069#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <asm/tlbflush.h>
Andrew Mortonac924c62006-05-15 09:43:59 -070071#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include "internal.h"
73
Cody P Schaferc8e251f2013-07-03 15:01:29 -070074/* prevent >1 _updater_ of zone percpu pageset ->high and ->batch fields */
75static DEFINE_MUTEX(pcp_batch_high_lock);
David Rientjes7cd2b0a2014-06-23 13:22:04 -070076#define MIN_PERCPU_PAGELIST_FRACTION (8)
Cody P Schaferc8e251f2013-07-03 15:01:29 -070077
Lee Schermerhorn72812012010-05-26 14:44:56 -070078#ifdef CONFIG_USE_PERCPU_NUMA_NODE_ID
79DEFINE_PER_CPU(int, numa_node);
80EXPORT_PER_CPU_SYMBOL(numa_node);
81#endif
82
Lee Schermerhorn7aac7892010-05-26 14:45:00 -070083#ifdef CONFIG_HAVE_MEMORYLESS_NODES
84/*
85 * N.B., Do NOT reference the '_numa_mem_' per cpu variable directly.
86 * It will not be defined when CONFIG_HAVE_MEMORYLESS_NODES is not defined.
87 * Use the accessor functions set_numa_mem(), numa_mem_id() and cpu_to_mem()
88 * defined in <linux/topology.h>.
89 */
90DEFINE_PER_CPU(int, _numa_mem_); /* Kernel "local memory" node */
91EXPORT_PER_CPU_SYMBOL(_numa_mem_);
Joonsoo Kimad2c8142014-10-09 15:26:13 -070092int _node_numa_mem_[MAX_NUMNODES];
Lee Schermerhorn7aac7892010-05-26 14:45:00 -070093#endif
94
Emese Revfy38addce2016-06-20 20:41:19 +020095#ifdef CONFIG_GCC_PLUGIN_LATENT_ENTROPY
Kees Cook58bea412016-10-19 00:08:04 +020096volatile unsigned long latent_entropy __latent_entropy;
Emese Revfy38addce2016-06-20 20:41:19 +020097EXPORT_SYMBOL(latent_entropy);
98#endif
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/*
Christoph Lameter13808912007-10-16 01:25:27 -0700101 * Array of node states.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 */
Christoph Lameter13808912007-10-16 01:25:27 -0700103nodemask_t node_states[NR_NODE_STATES] __read_mostly = {
104 [N_POSSIBLE] = NODE_MASK_ALL,
105 [N_ONLINE] = { { [0] = 1UL } },
106#ifndef CONFIG_NUMA
107 [N_NORMAL_MEMORY] = { { [0] = 1UL } },
108#ifdef CONFIG_HIGHMEM
109 [N_HIGH_MEMORY] = { { [0] = 1UL } },
110#endif
Lai Jiangshan20b2f522012-12-12 13:52:00 -0800111#ifdef CONFIG_MOVABLE_NODE
112 [N_MEMORY] = { { [0] = 1UL } },
113#endif
Christoph Lameter13808912007-10-16 01:25:27 -0700114 [N_CPU] = { { [0] = 1UL } },
115#endif /* NUMA */
116};
117EXPORT_SYMBOL(node_states);
118
Jiang Liuc3d5f5f2013-07-03 15:03:14 -0700119/* Protect totalram_pages and zone->managed_pages */
120static DEFINE_SPINLOCK(managed_page_count_lock);
121
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -0700122unsigned long totalram_pages __read_mostly;
Hideo AOKIcb45b0e2006-04-10 22:52:59 -0700123unsigned long totalreserve_pages __read_mostly;
Pintu Kumare48322a2014-12-18 16:17:15 -0800124unsigned long totalcma_pages __read_mostly;
Johannes Weinerab8fabd2012-01-10 15:07:42 -0800125
Hugh Dickins1b76b022012-05-11 01:00:07 -0700126int percpu_pagelist_fraction;
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +1000127gfp_t gfp_allowed_mask __read_mostly = GFP_BOOT_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Vlastimil Babkabb14c2c2015-09-08 15:01:25 -0700129/*
130 * A cached value of the page's pageblock's migratetype, used when the page is
131 * put on a pcplist. Used to avoid the pageblock migratetype lookup when
132 * freeing from pcplists in most cases, at the cost of possibly becoming stale.
133 * Also the migratetype set in the page does not necessarily match the pcplist
134 * index, e.g. page might have MIGRATE_CMA set but be on a pcplist with any
135 * other index - this ensures that it will be put on the correct CMA freelist.
136 */
137static inline int get_pcppage_migratetype(struct page *page)
138{
139 return page->index;
140}
141
142static inline void set_pcppage_migratetype(struct page *page, int migratetype)
143{
144 page->index = migratetype;
145}
146
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800147#ifdef CONFIG_PM_SLEEP
148/*
149 * The following functions are used by the suspend/hibernate code to temporarily
150 * change gfp_allowed_mask in order to avoid using I/O during memory allocations
151 * while devices are suspended. To avoid races with the suspend/hibernate code,
152 * they should always be called with pm_mutex held (gfp_allowed_mask also should
153 * only be modified with pm_mutex held, unless the suspend/hibernate code is
154 * guaranteed not to run in parallel with that modification).
155 */
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100156
157static gfp_t saved_gfp_mask;
158
159void pm_restore_gfp_mask(void)
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800160{
161 WARN_ON(!mutex_is_locked(&pm_mutex));
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100162 if (saved_gfp_mask) {
163 gfp_allowed_mask = saved_gfp_mask;
164 saved_gfp_mask = 0;
165 }
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800166}
167
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100168void pm_restrict_gfp_mask(void)
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800169{
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800170 WARN_ON(!mutex_is_locked(&pm_mutex));
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100171 WARN_ON(saved_gfp_mask);
172 saved_gfp_mask = gfp_allowed_mask;
Mel Gormand0164ad2015-11-06 16:28:21 -0800173 gfp_allowed_mask &= ~(__GFP_IO | __GFP_FS);
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800174}
Mel Gormanf90ac392012-01-10 15:07:15 -0800175
176bool pm_suspended_storage(void)
177{
Mel Gormand0164ad2015-11-06 16:28:21 -0800178 if ((gfp_allowed_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS))
Mel Gormanf90ac392012-01-10 15:07:15 -0800179 return false;
180 return true;
181}
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800182#endif /* CONFIG_PM_SLEEP */
183
Mel Gormand9c23402007-10-16 01:26:01 -0700184#ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -0800185unsigned int pageblock_order __read_mostly;
Mel Gormand9c23402007-10-16 01:26:01 -0700186#endif
187
Hugh Dickinsd98c7a02006-02-14 13:52:59 -0800188static void __free_pages_ok(struct page *page, unsigned int order);
David Howellsa226f6c2006-01-06 00:11:08 -0800189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/*
191 * results with 256, 32 in the lowmem_reserve sysctl:
192 * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
193 * 1G machine -> (16M dma, 784M normal, 224M high)
194 * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
195 * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
Yaowei Bai84109e12015-02-12 15:00:22 -0800196 * HIGHMEM allocation will leave (224M+784M)/256 of ram reserved in ZONE_DMA
Andi Kleena2f1b422005-11-05 17:25:53 +0100197 *
198 * TBD: should special case ZONE_DMA32 machines here - in those we normally
199 * don't need any ZONE_NORMAL reservation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 */
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700201int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1] = {
Christoph Lameter4b51d662007-02-10 01:43:10 -0800202#ifdef CONFIG_ZONE_DMA
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700203 256,
Christoph Lameter4b51d662007-02-10 01:43:10 -0800204#endif
Christoph Lameterfb0e7942006-09-25 23:31:13 -0700205#ifdef CONFIG_ZONE_DMA32
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700206 256,
Christoph Lameterfb0e7942006-09-25 23:31:13 -0700207#endif
Christoph Lametere53ef382006-09-25 23:31:14 -0700208#ifdef CONFIG_HIGHMEM
Mel Gorman2a1e2742007-07-17 04:03:12 -0700209 32,
Christoph Lametere53ef382006-09-25 23:31:14 -0700210#endif
Mel Gorman2a1e2742007-07-17 04:03:12 -0700211 32,
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700212};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214EXPORT_SYMBOL(totalram_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Helge Deller15ad7cd2006-12-06 20:40:36 -0800216static char * const zone_names[MAX_NR_ZONES] = {
Christoph Lameter4b51d662007-02-10 01:43:10 -0800217#ifdef CONFIG_ZONE_DMA
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700218 "DMA",
Christoph Lameter4b51d662007-02-10 01:43:10 -0800219#endif
Christoph Lameterfb0e7942006-09-25 23:31:13 -0700220#ifdef CONFIG_ZONE_DMA32
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700221 "DMA32",
Christoph Lameterfb0e7942006-09-25 23:31:13 -0700222#endif
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700223 "Normal",
Christoph Lametere53ef382006-09-25 23:31:14 -0700224#ifdef CONFIG_HIGHMEM
Mel Gorman2a1e2742007-07-17 04:03:12 -0700225 "HighMem",
Christoph Lametere53ef382006-09-25 23:31:14 -0700226#endif
Mel Gorman2a1e2742007-07-17 04:03:12 -0700227 "Movable",
Dan Williams033fbae2015-08-09 15:29:06 -0400228#ifdef CONFIG_ZONE_DEVICE
229 "Device",
230#endif
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700231};
232
Vlastimil Babka60f30352016-03-15 14:56:08 -0700233char * const migratetype_names[MIGRATE_TYPES] = {
234 "Unmovable",
235 "Movable",
236 "Reclaimable",
237 "HighAtomic",
238#ifdef CONFIG_CMA
239 "CMA",
240#endif
241#ifdef CONFIG_MEMORY_ISOLATION
242 "Isolate",
243#endif
244};
245
Kirill A. Shutemovf1e61552015-11-06 16:29:50 -0800246compound_page_dtor * const compound_page_dtors[] = {
247 NULL,
248 free_compound_page,
249#ifdef CONFIG_HUGETLB_PAGE
250 free_huge_page,
251#endif
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800252#ifdef CONFIG_TRANSPARENT_HUGEPAGE
253 free_transhuge_page,
254#endif
Kirill A. Shutemovf1e61552015-11-06 16:29:50 -0800255};
256
Rik van Rieldbe0f612011-09-01 15:26:50 -0400257/*
258 * Try to keep at least this much lowmem free. Do not allow normal
259 * allocations below this point, only high priority ones. Automatically
260 * tuned according to the amount of memory in the system.
261 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262int min_free_kbytes = 1024;
Han Pingtian42aa83c2014-01-23 15:53:28 -0800263int user_min_free_kbytes = -1;
Johannes Weiner795ae7a2016-03-17 14:19:14 -0700264int watermark_scale_factor = 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Rik van Rieldbe0f612011-09-01 15:26:50 -0400266/*
267 * Extra memory for the system to try freeing. Used to temporarily
268 * free memory, to make space for new workloads. Anyone can allocate
269 * down to the min watermarks controlled by min_free_kbytes above.
270 */
271int extra_free_kbytes = 0;
272
Jan Beulich2c85f512009-09-21 17:03:07 -0700273static unsigned long __meminitdata nr_kernel_pages;
274static unsigned long __meminitdata nr_all_pages;
Yasunori Gotoa3142c82007-05-08 00:23:07 -0700275static unsigned long __meminitdata dma_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Tejun Heo0ee332c2011-12-08 10:22:09 -0800277#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
278static unsigned long __meminitdata arch_zone_lowest_possible_pfn[MAX_NR_ZONES];
279static unsigned long __meminitdata arch_zone_highest_possible_pfn[MAX_NR_ZONES];
280static unsigned long __initdata required_kernelcore;
281static unsigned long __initdata required_movablecore;
282static unsigned long __meminitdata zone_movable_pfn[MAX_NUMNODES];
Taku Izumi342332e2016-03-15 14:55:22 -0700283static bool mirrored_kernelcore;
Mel Gormanc7132162006-09-27 01:49:43 -0700284
Tejun Heo0ee332c2011-12-08 10:22:09 -0800285/* movable_zone is the "real" zone pages in ZONE_MOVABLE are taken from */
286int movable_zone;
287EXPORT_SYMBOL(movable_zone);
288#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
Mel Gormanc7132162006-09-27 01:49:43 -0700289
Miklos Szeredi418508c2007-05-23 13:57:55 -0700290#if MAX_NUMNODES > 1
291int nr_node_ids __read_mostly = MAX_NUMNODES;
Christoph Lameter62bc62a2009-06-16 15:32:15 -0700292int nr_online_nodes __read_mostly = 1;
Miklos Szeredi418508c2007-05-23 13:57:55 -0700293EXPORT_SYMBOL(nr_node_ids);
Christoph Lameter62bc62a2009-06-16 15:32:15 -0700294EXPORT_SYMBOL(nr_online_nodes);
Miklos Szeredi418508c2007-05-23 13:57:55 -0700295#endif
296
Mel Gorman9ef9acb2007-10-16 01:25:54 -0700297int page_group_by_mobility_disabled __read_mostly;
298
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700299#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
Pavel Tatashin9980b822017-11-15 17:38:41 -0800300
301/*
302 * Determine how many pages need to be initialized durig early boot
303 * (non-deferred initialization).
304 * The value of first_deferred_pfn will be set later, once non-deferred pages
305 * are initialized, but for now set it ULONG_MAX.
306 */
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700307static inline void reset_deferred_meminit(pg_data_t *pgdat)
308{
Pavel Tatashin9980b822017-11-15 17:38:41 -0800309 phys_addr_t start_addr, end_addr;
310 unsigned long max_pgcnt;
311 unsigned long reserved;
Michal Hocko292f70c2017-06-02 14:46:49 -0700312
313 /*
314 * Initialise at least 2G of a node but also take into account that
315 * two large system hashes that can take up 1GB for 0.25TB/node.
316 */
Pavel Tatashin9980b822017-11-15 17:38:41 -0800317 max_pgcnt = max(2UL << (30 - PAGE_SHIFT),
318 (pgdat->node_spanned_pages >> 8));
Michal Hocko292f70c2017-06-02 14:46:49 -0700319
320 /*
321 * Compensate the all the memblock reservations (e.g. crash kernel)
322 * from the initial estimation to make sure we will initialize enough
323 * memory to boot.
324 */
Pavel Tatashin9980b822017-11-15 17:38:41 -0800325 start_addr = PFN_PHYS(pgdat->node_start_pfn);
326 end_addr = PFN_PHYS(pgdat->node_start_pfn + max_pgcnt);
327 reserved = memblock_reserved_memory_within(start_addr, end_addr);
328 max_pgcnt += PHYS_PFN(reserved);
Michal Hocko292f70c2017-06-02 14:46:49 -0700329
Pavel Tatashin9980b822017-11-15 17:38:41 -0800330 pgdat->static_init_pgcnt = min(max_pgcnt, pgdat->node_spanned_pages);
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700331 pgdat->first_deferred_pfn = ULONG_MAX;
332}
333
334/* Returns true if the struct page for the pfn is uninitialised */
Mel Gorman0e1cc952015-06-30 14:57:27 -0700335static inline bool __meminit early_page_uninitialised(unsigned long pfn)
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700336{
Mel Gormanef70b6f2016-07-14 12:07:23 -0700337 int nid = early_pfn_to_nid(pfn);
338
339 if (node_online(nid) && pfn >= NODE_DATA(nid)->first_deferred_pfn)
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700340 return true;
341
342 return false;
343}
344
345/*
346 * Returns false when the remaining initialisation should be deferred until
347 * later in the boot cycle when it can be parallelised.
348 */
349static inline bool update_defer_init(pg_data_t *pgdat,
350 unsigned long pfn, unsigned long zone_end,
351 unsigned long *nr_initialised)
352{
353 /* Always populate low zones for address-contrained allocations */
354 if (zone_end < pgdat_end_pfn(pgdat))
355 return true;
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700356 (*nr_initialised)++;
Pavel Tatashin9980b822017-11-15 17:38:41 -0800357 if ((*nr_initialised > pgdat->static_init_pgcnt) &&
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700358 (pfn & (PAGES_PER_SECTION - 1)) == 0) {
359 pgdat->first_deferred_pfn = pfn;
360 return false;
361 }
362
363 return true;
364}
365#else
366static inline void reset_deferred_meminit(pg_data_t *pgdat)
367{
368}
369
370static inline bool early_page_uninitialised(unsigned long pfn)
371{
372 return false;
373}
374
375static inline bool update_defer_init(pg_data_t *pgdat,
376 unsigned long pfn, unsigned long zone_end,
377 unsigned long *nr_initialised)
378{
379 return true;
380}
381#endif
382
Mel Gorman0b423ca2016-05-19 17:14:27 -0700383/* Return a pointer to the bitmap storing bits affecting a block of pages */
384static inline unsigned long *get_pageblock_bitmap(struct page *page,
385 unsigned long pfn)
386{
387#ifdef CONFIG_SPARSEMEM
388 return __pfn_to_section(pfn)->pageblock_flags;
389#else
390 return page_zone(page)->pageblock_flags;
391#endif /* CONFIG_SPARSEMEM */
392}
393
394static inline int pfn_to_bitidx(struct page *page, unsigned long pfn)
395{
396#ifdef CONFIG_SPARSEMEM
397 pfn &= (PAGES_PER_SECTION-1);
398 return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
399#else
400 pfn = pfn - round_down(page_zone(page)->zone_start_pfn, pageblock_nr_pages);
401 return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
402#endif /* CONFIG_SPARSEMEM */
403}
404
405/**
406 * get_pfnblock_flags_mask - Return the requested group of flags for the pageblock_nr_pages block of pages
407 * @page: The page within the block of interest
408 * @pfn: The target page frame number
409 * @end_bitidx: The last bit of interest to retrieve
410 * @mask: mask of bits that the caller is interested in
411 *
412 * Return: pageblock_bits flags
413 */
414static __always_inline unsigned long __get_pfnblock_flags_mask(struct page *page,
415 unsigned long pfn,
416 unsigned long end_bitidx,
417 unsigned long mask)
418{
419 unsigned long *bitmap;
420 unsigned long bitidx, word_bitidx;
421 unsigned long word;
422
423 bitmap = get_pageblock_bitmap(page, pfn);
424 bitidx = pfn_to_bitidx(page, pfn);
425 word_bitidx = bitidx / BITS_PER_LONG;
426 bitidx &= (BITS_PER_LONG-1);
427
428 word = bitmap[word_bitidx];
429 bitidx += end_bitidx;
430 return (word >> (BITS_PER_LONG - bitidx - 1)) & mask;
431}
432
433unsigned long get_pfnblock_flags_mask(struct page *page, unsigned long pfn,
434 unsigned long end_bitidx,
435 unsigned long mask)
436{
437 return __get_pfnblock_flags_mask(page, pfn, end_bitidx, mask);
438}
439
440static __always_inline int get_pfnblock_migratetype(struct page *page, unsigned long pfn)
441{
442 return __get_pfnblock_flags_mask(page, pfn, PB_migrate_end, MIGRATETYPE_MASK);
443}
444
445/**
446 * set_pfnblock_flags_mask - Set the requested group of flags for a pageblock_nr_pages block of pages
447 * @page: The page within the block of interest
448 * @flags: The flags to set
449 * @pfn: The target page frame number
450 * @end_bitidx: The last bit of interest
451 * @mask: mask of bits that the caller is interested in
452 */
453void set_pfnblock_flags_mask(struct page *page, unsigned long flags,
454 unsigned long pfn,
455 unsigned long end_bitidx,
456 unsigned long mask)
457{
458 unsigned long *bitmap;
459 unsigned long bitidx, word_bitidx;
460 unsigned long old_word, word;
461
462 BUILD_BUG_ON(NR_PAGEBLOCK_BITS != 4);
463
464 bitmap = get_pageblock_bitmap(page, pfn);
465 bitidx = pfn_to_bitidx(page, pfn);
466 word_bitidx = bitidx / BITS_PER_LONG;
467 bitidx &= (BITS_PER_LONG-1);
468
469 VM_BUG_ON_PAGE(!zone_spans_pfn(page_zone(page), pfn), page);
470
471 bitidx += end_bitidx;
472 mask <<= (BITS_PER_LONG - bitidx - 1);
473 flags <<= (BITS_PER_LONG - bitidx - 1);
474
475 word = READ_ONCE(bitmap[word_bitidx]);
476 for (;;) {
477 old_word = cmpxchg(&bitmap[word_bitidx], word, (word & ~mask) | flags);
478 if (word == old_word)
479 break;
480 word = old_word;
481 }
482}
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700483
Minchan Kimee6f5092012-07-31 16:43:50 -0700484void set_pageblock_migratetype(struct page *page, int migratetype)
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700485{
KOSAKI Motohiro5d0f3f72013-11-12 15:08:18 -0800486 if (unlikely(page_group_by_mobility_disabled &&
487 migratetype < MIGRATE_PCPTYPES))
Mel Gorman49255c62009-06-16 15:31:58 -0700488 migratetype = MIGRATE_UNMOVABLE;
489
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700490 set_pageblock_flags_group(page, (unsigned long)migratetype,
491 PB_migrate, PB_migrate_end);
492}
493
Nick Piggin13e74442006-01-06 00:10:58 -0800494#ifdef CONFIG_DEBUG_VM
Dave Hansenc6a57e12005-10-29 18:16:52 -0700495static int page_outside_zone_boundaries(struct zone *zone, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Dave Hansenbdc8cb92005-10-29 18:16:53 -0700497 int ret = 0;
498 unsigned seq;
499 unsigned long pfn = page_to_pfn(page);
Cody P Schaferb5e6a5a2013-02-22 16:35:28 -0800500 unsigned long sp, start_pfn;
Dave Hansenc6a57e12005-10-29 18:16:52 -0700501
Dave Hansenbdc8cb92005-10-29 18:16:53 -0700502 do {
503 seq = zone_span_seqbegin(zone);
Cody P Schaferb5e6a5a2013-02-22 16:35:28 -0800504 start_pfn = zone->zone_start_pfn;
505 sp = zone->spanned_pages;
Cody P Schafer108bcc92013-02-22 16:35:23 -0800506 if (!zone_spans_pfn(zone, pfn))
Dave Hansenbdc8cb92005-10-29 18:16:53 -0700507 ret = 1;
508 } while (zone_span_seqretry(zone, seq));
509
Cody P Schaferb5e6a5a2013-02-22 16:35:28 -0800510 if (ret)
Dave Hansen613813e2014-06-04 16:07:27 -0700511 pr_err("page 0x%lx outside node %d zone %s [ 0x%lx - 0x%lx ]\n",
512 pfn, zone_to_nid(zone), zone->name,
513 start_pfn, start_pfn + sp);
Cody P Schaferb5e6a5a2013-02-22 16:35:28 -0800514
Dave Hansenbdc8cb92005-10-29 18:16:53 -0700515 return ret;
Dave Hansenc6a57e12005-10-29 18:16:52 -0700516}
517
518static int page_is_consistent(struct zone *zone, struct page *page)
519{
Andy Whitcroft14e07292007-05-06 14:49:14 -0700520 if (!pfn_valid_within(page_to_pfn(page)))
Dave Hansenc6a57e12005-10-29 18:16:52 -0700521 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (zone != page_zone(page))
Dave Hansenc6a57e12005-10-29 18:16:52 -0700523 return 0;
524
525 return 1;
526}
527/*
528 * Temporary debugging check for pages not lying within a given zone.
529 */
530static int bad_range(struct zone *zone, struct page *page)
531{
532 if (page_outside_zone_boundaries(zone, page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return 1;
Dave Hansenc6a57e12005-10-29 18:16:52 -0700534 if (!page_is_consistent(zone, page))
535 return 1;
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return 0;
538}
Nick Piggin13e74442006-01-06 00:10:58 -0800539#else
540static inline int bad_range(struct zone *zone, struct page *page)
541{
542 return 0;
543}
544#endif
545
Kirill A. Shutemovd230dec2014-04-07 15:37:38 -0700546static void bad_page(struct page *page, const char *reason,
547 unsigned long bad_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800549 static unsigned long resume;
550 static unsigned long nr_shown;
551 static unsigned long nr_unshown;
552
553 /*
554 * Allow a burst of 60 reports, then keep quiet for that minute;
555 * or allow a steady drip of one report per second.
556 */
557 if (nr_shown == 60) {
558 if (time_before(jiffies, resume)) {
559 nr_unshown++;
560 goto out;
561 }
562 if (nr_unshown) {
Vlastimil Babkaff8e8112016-03-15 14:56:24 -0700563 pr_alert(
Hugh Dickins1e9e6362009-01-06 14:40:13 -0800564 "BUG: Bad page state: %lu messages suppressed\n",
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800565 nr_unshown);
566 nr_unshown = 0;
567 }
568 nr_shown = 0;
569 }
570 if (nr_shown++ == 0)
571 resume = jiffies + 60 * HZ;
572
Vlastimil Babkaff8e8112016-03-15 14:56:24 -0700573 pr_alert("BUG: Bad page state in process %s pfn:%05lx\n",
Hugh Dickins3dc14742009-01-06 14:40:08 -0800574 current->comm, page_to_pfn(page));
Vlastimil Babkaff8e8112016-03-15 14:56:24 -0700575 __dump_page(page, reason);
576 bad_flags &= page->flags;
577 if (bad_flags)
578 pr_alert("bad because of flags: %#lx(%pGp)\n",
579 bad_flags, &bad_flags);
Vlastimil Babka4e462112016-03-15 14:56:21 -0700580 dump_page_owner(page);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700581
Dave Jones4f318882011-10-31 17:07:24 -0700582 print_modules();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 dump_stack();
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800584out:
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800585 /* Leave bad fields for debug, except PageBuddy could make trouble */
Mel Gorman22b751c2013-02-22 16:34:59 -0800586 page_mapcount_reset(page); /* remove PageBuddy */
Rusty Russell373d4d02013-01-21 17:17:39 +1030587 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590/*
591 * Higher-order pages are called "compound pages". They are structured thusly:
592 *
Kirill A. Shutemov1d798ca2015-11-06 16:29:54 -0800593 * The first PAGE_SIZE page is called the "head page" and have PG_head set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 *
Kirill A. Shutemov1d798ca2015-11-06 16:29:54 -0800595 * The remaining PAGE_SIZE pages are called "tail pages". PageTail() is encoded
596 * in bit 0 of page->compound_head. The rest of bits is pointer to head page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 *
Kirill A. Shutemov1d798ca2015-11-06 16:29:54 -0800598 * The first tail page's ->compound_dtor holds the offset in array of compound
599 * page destructors. See compound_page_dtors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 *
Kirill A. Shutemov1d798ca2015-11-06 16:29:54 -0800601 * The first tail page's ->compound_order holds the order of allocation.
Hugh Dickins41d78ba2006-02-14 13:52:58 -0800602 * This usage means that zero-order pages may not be compound.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 */
Hugh Dickinsd98c7a02006-02-14 13:52:59 -0800604
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800605void free_compound_page(struct page *page)
Hugh Dickinsd98c7a02006-02-14 13:52:59 -0800606{
Christoph Lameterd85f3382007-05-06 14:49:39 -0700607 __free_pages_ok(page, compound_order(page));
Hugh Dickinsd98c7a02006-02-14 13:52:59 -0800608}
609
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -0800610void prep_compound_page(struct page *page, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
612 int i;
613 int nr_pages = 1 << order;
614
Kirill A. Shutemovf1e61552015-11-06 16:29:50 -0800615 set_compound_page_dtor(page, COMPOUND_PAGE_DTOR);
Christoph Lameterd85f3382007-05-06 14:49:39 -0700616 set_compound_order(page, order);
Christoph Lameter6d777952007-05-06 14:49:40 -0700617 __SetPageHead(page);
Andy Whitcroft18229df2008-11-06 12:53:27 -0800618 for (i = 1; i < nr_pages; i++) {
619 struct page *p = page + i;
Youquan Song58a84aa2011-12-08 14:34:18 -0800620 set_page_count(p, 0);
Kirill A. Shutemov1c290f62016-01-15 16:52:07 -0800621 p->mapping = TAIL_MAPPING;
Kirill A. Shutemov1d798ca2015-11-06 16:29:54 -0800622 set_compound_head(p, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
Kirill A. Shutemov53f92632016-01-15 16:53:42 -0800624 atomic_set(compound_mapcount_ptr(page), -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -0800627#ifdef CONFIG_DEBUG_PAGEALLOC
628unsigned int _debug_guardpage_minorder;
Christian Borntraegerea6eabb2016-03-15 14:55:30 -0700629bool _debug_pagealloc_enabled __read_mostly
630 = IS_ENABLED(CONFIG_DEBUG_PAGEALLOC_ENABLE_DEFAULT);
Joonsoo Kim505f6d22016-03-17 14:17:56 -0700631EXPORT_SYMBOL(_debug_pagealloc_enabled);
Joonsoo Kime30825f2014-12-12 16:55:49 -0800632bool _debug_guardpage_enabled __read_mostly;
633
Joonsoo Kim031bc572014-12-12 16:55:52 -0800634static int __init early_debug_pagealloc(char *buf)
635{
636 if (!buf)
637 return -EINVAL;
Minfei Huang2a138dc2016-05-20 16:58:13 -0700638 return kstrtobool(buf, &_debug_pagealloc_enabled);
Joonsoo Kim031bc572014-12-12 16:55:52 -0800639}
640early_param("debug_pagealloc", early_debug_pagealloc);
641
Joonsoo Kime30825f2014-12-12 16:55:49 -0800642static bool need_debug_guardpage(void)
643{
Joonsoo Kim031bc572014-12-12 16:55:52 -0800644 /* If we don't use debug_pagealloc, we don't need guard page */
645 if (!debug_pagealloc_enabled())
646 return false;
647
Joonsoo Kimf1c1e9f2016-10-07 16:58:18 -0700648 if (!debug_guardpage_minorder())
649 return false;
650
Joonsoo Kime30825f2014-12-12 16:55:49 -0800651 return true;
652}
653
654static void init_debug_guardpage(void)
655{
Joonsoo Kim031bc572014-12-12 16:55:52 -0800656 if (!debug_pagealloc_enabled())
657 return;
658
Joonsoo Kimf1c1e9f2016-10-07 16:58:18 -0700659 if (!debug_guardpage_minorder())
660 return;
661
Joonsoo Kime30825f2014-12-12 16:55:49 -0800662 _debug_guardpage_enabled = true;
663}
664
665struct page_ext_operations debug_guardpage_ops = {
666 .need = need_debug_guardpage,
667 .init = init_debug_guardpage,
668};
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -0800669
670static int __init debug_guardpage_minorder_setup(char *buf)
671{
672 unsigned long res;
673
674 if (kstrtoul(buf, 10, &res) < 0 || res > MAX_ORDER / 2) {
Joe Perches11705322016-03-17 14:19:50 -0700675 pr_err("Bad debug_guardpage_minorder value\n");
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -0800676 return 0;
677 }
678 _debug_guardpage_minorder = res;
Joe Perches11705322016-03-17 14:19:50 -0700679 pr_info("Setting debug_guardpage_minorder to %lu\n", res);
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -0800680 return 0;
681}
Joonsoo Kimf1c1e9f2016-10-07 16:58:18 -0700682early_param("debug_guardpage_minorder", debug_guardpage_minorder_setup);
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -0800683
Joonsoo Kimacbc15a2016-10-07 16:58:15 -0700684static inline bool set_page_guard(struct zone *zone, struct page *page,
Joonsoo Kim2847cf92014-12-12 16:55:01 -0800685 unsigned int order, int migratetype)
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -0800686{
Joonsoo Kime30825f2014-12-12 16:55:49 -0800687 struct page_ext *page_ext;
688
689 if (!debug_guardpage_enabled())
Joonsoo Kimacbc15a2016-10-07 16:58:15 -0700690 return false;
691
692 if (order >= debug_guardpage_minorder())
693 return false;
Joonsoo Kime30825f2014-12-12 16:55:49 -0800694
695 page_ext = lookup_page_ext(page);
Yang Shif86e4272016-06-03 14:55:38 -0700696 if (unlikely(!page_ext))
Joonsoo Kimacbc15a2016-10-07 16:58:15 -0700697 return false;
Yang Shif86e4272016-06-03 14:55:38 -0700698
Joonsoo Kime30825f2014-12-12 16:55:49 -0800699 __set_bit(PAGE_EXT_DEBUG_GUARD, &page_ext->flags);
700
Joonsoo Kim2847cf92014-12-12 16:55:01 -0800701 INIT_LIST_HEAD(&page->lru);
702 set_page_private(page, order);
703 /* Guard pages are not available for any usage */
704 __mod_zone_freepage_state(zone, -(1 << order), migratetype);
Joonsoo Kimacbc15a2016-10-07 16:58:15 -0700705
706 return true;
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -0800707}
708
Joonsoo Kim2847cf92014-12-12 16:55:01 -0800709static inline void clear_page_guard(struct zone *zone, struct page *page,
710 unsigned int order, int migratetype)
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -0800711{
Joonsoo Kime30825f2014-12-12 16:55:49 -0800712 struct page_ext *page_ext;
713
714 if (!debug_guardpage_enabled())
715 return;
716
717 page_ext = lookup_page_ext(page);
Yang Shif86e4272016-06-03 14:55:38 -0700718 if (unlikely(!page_ext))
719 return;
720
Joonsoo Kime30825f2014-12-12 16:55:49 -0800721 __clear_bit(PAGE_EXT_DEBUG_GUARD, &page_ext->flags);
722
Joonsoo Kim2847cf92014-12-12 16:55:01 -0800723 set_page_private(page, 0);
724 if (!is_migrate_isolate(migratetype))
725 __mod_zone_freepage_state(zone, (1 << order), migratetype);
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -0800726}
727#else
Joonsoo Kim980ac162016-10-07 16:58:27 -0700728struct page_ext_operations debug_guardpage_ops;
Joonsoo Kimacbc15a2016-10-07 16:58:15 -0700729static inline bool set_page_guard(struct zone *zone, struct page *page,
730 unsigned int order, int migratetype) { return false; }
Joonsoo Kim2847cf92014-12-12 16:55:01 -0800731static inline void clear_page_guard(struct zone *zone, struct page *page,
732 unsigned int order, int migratetype) {}
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -0800733#endif
734
Mel Gorman7aeb09f2014-06-04 16:10:21 -0700735static inline void set_page_order(struct page *page, unsigned int order)
Andrew Morton6aa3001b22006-04-18 22:20:52 -0700736{
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700737 set_page_private(page, order);
Nick Piggin676165a2006-04-10 11:21:48 +1000738 __SetPageBuddy(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
741static inline void rmv_page_order(struct page *page)
742{
Nick Piggin676165a2006-04-10 11:21:48 +1000743 __ClearPageBuddy(page);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700744 set_page_private(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
747/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 * This function checks whether a page is free && is the buddy
749 * we can do coalesce a page and its buddy if
Nick Piggin13e74442006-01-06 00:10:58 -0800750 * (a) the buddy is not in a hole &&
Nick Piggin676165a2006-04-10 11:21:48 +1000751 * (b) the buddy is in the buddy system &&
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700752 * (c) a page and its buddy have the same order &&
753 * (d) a page and its buddy are in the same zone.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 *
Wang Sheng-Huicf6fe942013-09-11 14:22:48 -0700755 * For recording whether a page is in the buddy system, we set ->_mapcount
756 * PAGE_BUDDY_MAPCOUNT_VALUE.
757 * Setting, clearing, and testing _mapcount PAGE_BUDDY_MAPCOUNT_VALUE is
758 * serialized by zone->lock.
Nick Piggin676165a2006-04-10 11:21:48 +1000759 *
760 * For recording page's order, we use page_private(page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 */
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700762static inline int page_is_buddy(struct page *page, struct page *buddy,
Mel Gorman7aeb09f2014-06-04 16:10:21 -0700763 unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Andy Whitcroft14e07292007-05-06 14:49:14 -0700765 if (!pfn_valid_within(page_to_pfn(buddy)))
Nick Piggin13e74442006-01-06 00:10:58 -0800766 return 0;
Nick Piggin13e74442006-01-06 00:10:58 -0800767
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -0800768 if (page_is_guard(buddy) && page_order(buddy) == order) {
Mel Gormand34c5fa2014-06-04 16:10:10 -0700769 if (page_zone_id(page) != page_zone_id(buddy))
770 return 0;
771
Weijie Yang4c5018c2015-02-10 14:11:39 -0800772 VM_BUG_ON_PAGE(page_count(buddy) != 0, buddy);
773
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -0800774 return 1;
775 }
776
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700777 if (PageBuddy(buddy) && page_order(buddy) == order) {
Mel Gormand34c5fa2014-06-04 16:10:10 -0700778 /*
779 * zone check is done late to avoid uselessly
780 * calculating zone/node ids for pages that could
781 * never merge.
782 */
783 if (page_zone_id(page) != page_zone_id(buddy))
784 return 0;
785
Weijie Yang4c5018c2015-02-10 14:11:39 -0800786 VM_BUG_ON_PAGE(page_count(buddy) != 0, buddy);
787
Andrew Morton6aa3001b22006-04-18 22:20:52 -0700788 return 1;
Nick Piggin676165a2006-04-10 11:21:48 +1000789 }
Andrew Morton6aa3001b22006-04-18 22:20:52 -0700790 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
793/*
794 * Freeing function for a buddy system allocator.
795 *
796 * The concept of a buddy system is to maintain direct-mapped table
797 * (containing bit values) for memory blocks of various "orders".
798 * The bottom level table contains the map for the smallest allocatable
799 * units of memory (here, pages), and each level above it describes
800 * pairs of units from the levels below, hence, "buddies".
801 * At a high level, all that happens here is marking the table entry
802 * at the bottom level available, and propagating the changes upward
803 * as necessary, plus some accounting needed to play nicely with other
804 * parts of the VM system.
805 * At each level, we keep a list of pages, which are heads of continuous
Wang Sheng-Huicf6fe942013-09-11 14:22:48 -0700806 * free pages of length of (1 << order) and marked with _mapcount
807 * PAGE_BUDDY_MAPCOUNT_VALUE. Page's order is recorded in page_private(page)
808 * field.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 * So when we are allocating or freeing one, we can derive the state of the
Michal Nazarewicz5f63b722012-01-11 15:16:11 +0100810 * other. That is, if we allocate a small block, and both were
811 * free, the remainder of the region must be split into blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 * If a block is freed, and its buddy is also free, then this
Michal Nazarewicz5f63b722012-01-11 15:16:11 +0100813 * triggers coalescing into a block of larger size.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 *
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +0100815 * -- nyc
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 */
817
Nick Piggin48db57f2006-01-08 01:00:42 -0800818static inline void __free_one_page(struct page *page,
Mel Gormandc4b0ca2014-06-04 16:10:17 -0700819 unsigned long pfn,
Mel Gormaned0ae212009-06-16 15:32:07 -0700820 struct zone *zone, unsigned int order,
821 int migratetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
823 unsigned long page_idx;
Corrado Zoccolo6dda9d52010-05-24 14:31:54 -0700824 unsigned long combined_idx;
KyongHo Cho43506fa2011-01-13 15:47:24 -0800825 unsigned long uninitialized_var(buddy_idx);
Corrado Zoccolo6dda9d52010-05-24 14:31:54 -0700826 struct page *buddy;
Vlastimil Babkad9dddbf2016-03-25 14:21:50 -0700827 unsigned int max_order;
828
829 max_order = min_t(unsigned int, MAX_ORDER, pageblock_order + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Cody P Schaferd29bb972013-02-22 16:35:25 -0800831 VM_BUG_ON(!zone_is_initialized(zone));
Kirill A. Shutemov6e9f0d52015-02-11 15:25:50 -0800832 VM_BUG_ON_PAGE(page->flags & PAGE_FLAGS_CHECK_AT_PREP, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Mel Gormaned0ae212009-06-16 15:32:07 -0700834 VM_BUG_ON(migratetype == -1);
Vlastimil Babkad9dddbf2016-03-25 14:21:50 -0700835 if (likely(!is_migrate_isolate(migratetype)))
Joonsoo Kim8f82b552014-11-13 15:19:18 -0800836 __mod_zone_freepage_state(zone, 1 << order, migratetype);
Mel Gormaned0ae212009-06-16 15:32:07 -0700837
Vlastimil Babkad9dddbf2016-03-25 14:21:50 -0700838 page_idx = pfn & ((1 << MAX_ORDER) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Sasha Levin309381fea2014-01-23 15:52:54 -0800840 VM_BUG_ON_PAGE(page_idx & ((1 << order) - 1), page);
841 VM_BUG_ON_PAGE(bad_range(zone, page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Vlastimil Babkad9dddbf2016-03-25 14:21:50 -0700843continue_merging:
Joonsoo Kim3c605092014-11-13 15:19:21 -0800844 while (order < max_order - 1) {
KyongHo Cho43506fa2011-01-13 15:47:24 -0800845 buddy_idx = __find_buddy_index(page_idx, order);
846 buddy = page + (buddy_idx - page_idx);
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700847 if (!page_is_buddy(page, buddy, order))
Vlastimil Babkad9dddbf2016-03-25 14:21:50 -0700848 goto done_merging;
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -0800849 /*
850 * Our buddy is free or it is CONFIG_DEBUG_PAGEALLOC guard page,
851 * merge with it and move up one order.
852 */
853 if (page_is_guard(buddy)) {
Joonsoo Kim2847cf92014-12-12 16:55:01 -0800854 clear_page_guard(zone, buddy, order, migratetype);
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -0800855 } else {
856 list_del(&buddy->lru);
857 zone->free_area[order].nr_free--;
858 rmv_page_order(buddy);
859 }
KyongHo Cho43506fa2011-01-13 15:47:24 -0800860 combined_idx = buddy_idx & page_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 page = page + (combined_idx - page_idx);
862 page_idx = combined_idx;
863 order++;
864 }
Vlastimil Babkad9dddbf2016-03-25 14:21:50 -0700865 if (max_order < MAX_ORDER) {
866 /* If we are here, it means order is >= pageblock_order.
867 * We want to prevent merge between freepages on isolate
868 * pageblock and normal pageblock. Without this, pageblock
869 * isolation could cause incorrect freepage or CMA accounting.
870 *
871 * We don't want to hit this code for the more frequent
872 * low-order merging.
873 */
874 if (unlikely(has_isolate_pageblock(zone))) {
875 int buddy_mt;
876
877 buddy_idx = __find_buddy_index(page_idx, order);
878 buddy = page + (buddy_idx - page_idx);
879 buddy_mt = get_pageblock_migratetype(buddy);
880
881 if (migratetype != buddy_mt
882 && (is_migrate_isolate(migratetype) ||
883 is_migrate_isolate(buddy_mt)))
884 goto done_merging;
885 }
886 max_order++;
887 goto continue_merging;
888 }
889
890done_merging:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 set_page_order(page, order);
Corrado Zoccolo6dda9d52010-05-24 14:31:54 -0700892
893 /*
894 * If this is not the largest possible page, check if the buddy
895 * of the next-highest order is free. If it is, it's possible
896 * that pages are being freed that will coalesce soon. In case,
897 * that is happening, add the free page to the tail of the list
898 * so it's less likely to be used soon and more likely to be merged
899 * as a higher order page
900 */
Mel Gormanb7f50cf2010-10-26 14:21:11 -0700901 if ((order < MAX_ORDER-2) && pfn_valid_within(page_to_pfn(buddy))) {
Corrado Zoccolo6dda9d52010-05-24 14:31:54 -0700902 struct page *higher_page, *higher_buddy;
KyongHo Cho43506fa2011-01-13 15:47:24 -0800903 combined_idx = buddy_idx & page_idx;
904 higher_page = page + (combined_idx - page_idx);
905 buddy_idx = __find_buddy_index(combined_idx, order + 1);
Li Haifeng0ba8f2d2012-09-17 14:09:21 -0700906 higher_buddy = higher_page + (buddy_idx - combined_idx);
Corrado Zoccolo6dda9d52010-05-24 14:31:54 -0700907 if (page_is_buddy(higher_page, higher_buddy, order + 1)) {
908 list_add_tail(&page->lru,
909 &zone->free_area[order].free_list[migratetype]);
910 goto out;
911 }
912 }
913
914 list_add(&page->lru, &zone->free_area[order].free_list[migratetype]);
915out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 zone->free_area[order].nr_free++;
917}
918
Mel Gorman7bfec6f2016-05-19 17:14:15 -0700919/*
920 * A bad page could be due to a number of fields. Instead of multiple branches,
921 * try and check multiple fields with one check. The caller must do a detailed
922 * check if necessary.
923 */
924static inline bool page_expected_state(struct page *page,
925 unsigned long check_flags)
926{
927 if (unlikely(atomic_read(&page->_mapcount) != -1))
928 return false;
929
930 if (unlikely((unsigned long)page->mapping |
931 page_ref_count(page) |
932#ifdef CONFIG_MEMCG
933 (unsigned long)page->mem_cgroup |
934#endif
935 (page->flags & check_flags)))
936 return false;
937
938 return true;
939}
940
Mel Gormanbb552ac2016-05-19 17:14:18 -0700941static void free_pages_check_bad(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
Mel Gorman7bfec6f2016-05-19 17:14:15 -0700943 const char *bad_reason;
944 unsigned long bad_flags;
945
Mel Gorman7bfec6f2016-05-19 17:14:15 -0700946 bad_reason = NULL;
947 bad_flags = 0;
Dave Hansenf0b791a2014-01-23 15:52:49 -0800948
Kirill A. Shutemov53f92632016-01-15 16:53:42 -0800949 if (unlikely(atomic_read(&page->_mapcount) != -1))
Dave Hansenf0b791a2014-01-23 15:52:49 -0800950 bad_reason = "nonzero mapcount";
951 if (unlikely(page->mapping != NULL))
952 bad_reason = "non-NULL mapping";
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700953 if (unlikely(page_ref_count(page) != 0))
Joonsoo Kim0139aa72016-05-19 17:10:49 -0700954 bad_reason = "nonzero _refcount";
Dave Hansenf0b791a2014-01-23 15:52:49 -0800955 if (unlikely(page->flags & PAGE_FLAGS_CHECK_AT_FREE)) {
956 bad_reason = "PAGE_FLAGS_CHECK_AT_FREE flag(s) set";
957 bad_flags = PAGE_FLAGS_CHECK_AT_FREE;
958 }
Johannes Weiner9edad6e2014-12-10 15:44:58 -0800959#ifdef CONFIG_MEMCG
960 if (unlikely(page->mem_cgroup))
961 bad_reason = "page still charged to cgroup";
962#endif
Mel Gorman7bfec6f2016-05-19 17:14:15 -0700963 bad_page(page, bad_reason, bad_flags);
Mel Gormanbb552ac2016-05-19 17:14:18 -0700964}
965
966static inline int free_pages_check(struct page *page)
967{
Mel Gormanda838d42016-05-19 17:14:21 -0700968 if (likely(page_expected_state(page, PAGE_FLAGS_CHECK_AT_FREE)))
Mel Gormanbb552ac2016-05-19 17:14:18 -0700969 return 0;
Mel Gormanbb552ac2016-05-19 17:14:18 -0700970
971 /* Something has gone sideways, find it */
972 free_pages_check_bad(page);
Mel Gorman7bfec6f2016-05-19 17:14:15 -0700973 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975
Mel Gorman4db75482016-05-19 17:14:32 -0700976static int free_tail_pages_check(struct page *head_page, struct page *page)
977{
978 int ret = 1;
979
980 /*
981 * We rely page->lru.next never has bit 0 set, unless the page
982 * is PageTail(). Let's make sure that's true even for poisoned ->lru.
983 */
984 BUILD_BUG_ON((unsigned long)LIST_POISON1 & 1);
985
986 if (!IS_ENABLED(CONFIG_DEBUG_VM)) {
987 ret = 0;
988 goto out;
989 }
990 switch (page - head_page) {
991 case 1:
992 /* the first tail page: ->mapping is compound_mapcount() */
993 if (unlikely(compound_mapcount(page))) {
994 bad_page(page, "nonzero compound_mapcount", 0);
995 goto out;
996 }
997 break;
998 case 2:
999 /*
1000 * the second tail page: ->mapping is
1001 * page_deferred_list().next -- ignore value.
1002 */
1003 break;
1004 default:
1005 if (page->mapping != TAIL_MAPPING) {
1006 bad_page(page, "corrupted mapping in tail page", 0);
1007 goto out;
1008 }
1009 break;
1010 }
1011 if (unlikely(!PageTail(page))) {
1012 bad_page(page, "PageTail not set", 0);
1013 goto out;
1014 }
1015 if (unlikely(compound_head(page) != head_page)) {
1016 bad_page(page, "compound_head not consistent", 0);
1017 goto out;
1018 }
1019 ret = 0;
1020out:
1021 page->mapping = NULL;
1022 clear_compound_head(page);
1023 return ret;
1024}
1025
Mel Gormane2769db2016-05-19 17:14:38 -07001026static __always_inline bool free_pages_prepare(struct page *page,
1027 unsigned int order, bool check_free)
1028{
1029 int bad = 0;
1030
1031 VM_BUG_ON_PAGE(PageTail(page), page);
1032
1033 trace_mm_page_free(page, order);
1034 kmemcheck_free_shadow(page, order);
Mel Gormane2769db2016-05-19 17:14:38 -07001035
1036 /*
1037 * Check tail pages before head page information is cleared to
1038 * avoid checking PageCompound for order-0 pages.
1039 */
1040 if (unlikely(order)) {
1041 bool compound = PageCompound(page);
1042 int i;
1043
1044 VM_BUG_ON_PAGE(compound && compound_order(page) != order, page);
1045
Kirill A. Shutemov9a73f612016-07-26 15:25:53 -07001046 if (compound)
1047 ClearPageDoubleMap(page);
Mel Gormane2769db2016-05-19 17:14:38 -07001048 for (i = 1; i < (1 << order); i++) {
1049 if (compound)
1050 bad += free_tail_pages_check(page, page + i);
1051 if (unlikely(free_pages_check(page + i))) {
1052 bad++;
1053 continue;
1054 }
1055 (page + i)->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
1056 }
1057 }
Minchan Kimbda807d2016-07-26 15:23:05 -07001058 if (PageMappingFlags(page))
Mel Gormane2769db2016-05-19 17:14:38 -07001059 page->mapping = NULL;
Vladimir Davydovc4159a72016-08-08 23:03:12 +03001060 if (memcg_kmem_enabled() && PageKmemcg(page))
Vladimir Davydov49491482016-07-26 15:24:24 -07001061 memcg_kmem_uncharge(page, order);
Mel Gormane2769db2016-05-19 17:14:38 -07001062 if (check_free)
1063 bad += free_pages_check(page);
1064 if (bad)
1065 return false;
1066
1067 page_cpupid_reset_last(page);
1068 page->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
1069 reset_page_owner(page, order);
1070
1071 if (!PageHighMem(page)) {
1072 debug_check_no_locks_freed(page_address(page),
1073 PAGE_SIZE << order);
1074 debug_check_no_obj_freed(page_address(page),
1075 PAGE_SIZE << order);
1076 }
1077 arch_free_page(page, order);
1078 kernel_poison_pages(page, 1 << order, 0);
1079 kernel_map_pages(page, 1 << order, 0);
seokhoon.yoon29b52de2016-05-20 16:58:47 -07001080 kasan_free_pages(page, order);
Mel Gormane2769db2016-05-19 17:14:38 -07001081
1082 return true;
1083}
Mel Gorman4db75482016-05-19 17:14:32 -07001084
1085#ifdef CONFIG_DEBUG_VM
1086static inline bool free_pcp_prepare(struct page *page)
1087{
Mel Gormane2769db2016-05-19 17:14:38 -07001088 return free_pages_prepare(page, 0, true);
Mel Gorman4db75482016-05-19 17:14:32 -07001089}
1090
1091static inline bool bulkfree_pcp_prepare(struct page *page)
1092{
1093 return false;
1094}
1095#else
1096static bool free_pcp_prepare(struct page *page)
1097{
Mel Gormane2769db2016-05-19 17:14:38 -07001098 return free_pages_prepare(page, 0, false);
Mel Gorman4db75482016-05-19 17:14:32 -07001099}
1100
1101static bool bulkfree_pcp_prepare(struct page *page)
1102{
1103 return free_pages_check(page);
1104}
1105#endif /* CONFIG_DEBUG_VM */
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107/*
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001108 * Frees a number of pages from the PCP lists
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 * Assumes all pages on list are in same zone, and of same order.
Renaud Lienhart207f36e2005-09-10 00:26:59 -07001110 * count is the number of pages to free.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 *
1112 * If the zone was previously in an "all pages pinned" state then look to
1113 * see if this freeing clears that state.
1114 *
1115 * And clear the zone's pages_scanned counter, to hold off the "all pages are
1116 * pinned" detection logic.
1117 */
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001118static void free_pcppages_bulk(struct zone *zone, int count,
1119 struct per_cpu_pages *pcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001121 int migratetype = 0;
Mel Gormana6f9edd2009-09-21 17:03:20 -07001122 int batch_free = 0;
Mel Gorman0d5d8232014-08-06 16:07:16 -07001123 unsigned long nr_scanned;
Mel Gorman37779992016-05-19 17:13:58 -07001124 bool isolated_pageblocks;
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001125
Nick Pigginc54ad302006-01-06 00:10:56 -08001126 spin_lock(&zone->lock);
Mel Gorman37779992016-05-19 17:13:58 -07001127 isolated_pageblocks = has_isolate_pageblock(zone);
Mel Gorman599d0c92016-07-28 15:45:31 -07001128 nr_scanned = node_page_state(zone->zone_pgdat, NR_PAGES_SCANNED);
Mel Gorman0d5d8232014-08-06 16:07:16 -07001129 if (nr_scanned)
Mel Gorman599d0c92016-07-28 15:45:31 -07001130 __mod_node_page_state(zone->zone_pgdat, NR_PAGES_SCANNED, -nr_scanned);
Mel Gormanf2260e62009-06-16 15:32:13 -07001131
Mel Gormane5b31ac2016-05-19 17:14:24 -07001132 while (count) {
Nick Piggin48db57f2006-01-08 01:00:42 -08001133 struct page *page;
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001134 struct list_head *list;
Nick Piggin48db57f2006-01-08 01:00:42 -08001135
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001136 /*
Mel Gormana6f9edd2009-09-21 17:03:20 -07001137 * Remove pages from lists in a round-robin fashion. A
1138 * batch_free count is maintained that is incremented when an
1139 * empty list is encountered. This is so more pages are freed
1140 * off fuller lists instead of spinning excessively around empty
1141 * lists
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001142 */
1143 do {
Mel Gormana6f9edd2009-09-21 17:03:20 -07001144 batch_free++;
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001145 if (++migratetype == MIGRATE_PCPTYPES)
1146 migratetype = 0;
1147 list = &pcp->lists[migratetype];
1148 } while (list_empty(list));
1149
Namhyung Kim1d168712011-03-22 16:32:45 -07001150 /* This is the only non-empty list. Free them all. */
1151 if (batch_free == MIGRATE_PCPTYPES)
Mel Gormane5b31ac2016-05-19 17:14:24 -07001152 batch_free = count;
Namhyung Kim1d168712011-03-22 16:32:45 -07001153
Mel Gormana6f9edd2009-09-21 17:03:20 -07001154 do {
Bartlomiej Zolnierkiewicz770c8aa2012-10-08 16:31:57 -07001155 int mt; /* migratetype of the to-be-freed page */
1156
Geliang Tanga16601c2016-01-14 15:20:30 -08001157 page = list_last_entry(list, struct page, lru);
Mel Gormana6f9edd2009-09-21 17:03:20 -07001158 /* must delete as __free_one_page list manipulates */
1159 list_del(&page->lru);
Vlastimil Babkaaa016d12015-09-08 15:01:22 -07001160
Vlastimil Babkabb14c2c2015-09-08 15:01:25 -07001161 mt = get_pcppage_migratetype(page);
Vlastimil Babkaaa016d12015-09-08 15:01:22 -07001162 /* MIGRATE_ISOLATE page should not go to pcplists */
1163 VM_BUG_ON_PAGE(is_migrate_isolate(mt), page);
1164 /* Pageblock could have been isolated meanwhile */
Mel Gorman37779992016-05-19 17:13:58 -07001165 if (unlikely(isolated_pageblocks))
Joonsoo Kim51bb1a42014-11-13 15:19:14 -08001166 mt = get_pageblock_migratetype(page);
Joonsoo Kim51bb1a42014-11-13 15:19:14 -08001167
Mel Gorman4db75482016-05-19 17:14:32 -07001168 if (bulkfree_pcp_prepare(page))
1169 continue;
1170
Mel Gormandc4b0ca2014-06-04 16:10:17 -07001171 __free_one_page(page, page_to_pfn(page), zone, 0, mt);
Bartlomiej Zolnierkiewicz770c8aa2012-10-08 16:31:57 -07001172 trace_mm_page_pcpu_drain(page, 0, mt);
Mel Gormane5b31ac2016-05-19 17:14:24 -07001173 } while (--count && --batch_free && !list_empty(list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
Nick Pigginc54ad302006-01-06 00:10:56 -08001175 spin_unlock(&zone->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
1177
Mel Gormandc4b0ca2014-06-04 16:10:17 -07001178static void free_one_page(struct zone *zone,
1179 struct page *page, unsigned long pfn,
Mel Gorman7aeb09f2014-06-04 16:10:21 -07001180 unsigned int order,
Mel Gormaned0ae212009-06-16 15:32:07 -07001181 int migratetype)
Nick Piggin48db57f2006-01-08 01:00:42 -08001182{
Mel Gorman0d5d8232014-08-06 16:07:16 -07001183 unsigned long nr_scanned;
Christoph Lameter006d22d2006-09-25 23:31:48 -07001184 spin_lock(&zone->lock);
Mel Gorman599d0c92016-07-28 15:45:31 -07001185 nr_scanned = node_page_state(zone->zone_pgdat, NR_PAGES_SCANNED);
Mel Gorman0d5d8232014-08-06 16:07:16 -07001186 if (nr_scanned)
Mel Gorman599d0c92016-07-28 15:45:31 -07001187 __mod_node_page_state(zone->zone_pgdat, NR_PAGES_SCANNED, -nr_scanned);
Mel Gormanf2260e62009-06-16 15:32:13 -07001188
Joonsoo Kimad53f922014-11-13 15:19:11 -08001189 if (unlikely(has_isolate_pageblock(zone) ||
1190 is_migrate_isolate(migratetype))) {
1191 migratetype = get_pfnblock_migratetype(page, pfn);
Joonsoo Kimad53f922014-11-13 15:19:11 -08001192 }
Mel Gormandc4b0ca2014-06-04 16:10:17 -07001193 __free_one_page(page, pfn, zone, order, migratetype);
Christoph Lameter006d22d2006-09-25 23:31:48 -07001194 spin_unlock(&zone->lock);
Nick Piggin48db57f2006-01-08 01:00:42 -08001195}
1196
Robin Holt1e8ce832015-06-30 14:56:45 -07001197static void __meminit __init_single_page(struct page *page, unsigned long pfn,
1198 unsigned long zone, int nid)
1199{
Robin Holt1e8ce832015-06-30 14:56:45 -07001200 set_page_links(page, zone, nid, pfn);
Robin Holt1e8ce832015-06-30 14:56:45 -07001201 init_page_count(page);
1202 page_mapcount_reset(page);
1203 page_cpupid_reset_last(page);
Robin Holt1e8ce832015-06-30 14:56:45 -07001204
Robin Holt1e8ce832015-06-30 14:56:45 -07001205 INIT_LIST_HEAD(&page->lru);
1206#ifdef WANT_PAGE_VIRTUAL
1207 /* The shift won't overflow because ZONE_NORMAL is below 4G. */
1208 if (!is_highmem_idx(zone))
1209 set_page_address(page, __va(pfn << PAGE_SHIFT));
1210#endif
1211}
1212
1213static void __meminit __init_single_pfn(unsigned long pfn, unsigned long zone,
1214 int nid)
1215{
1216 return __init_single_page(pfn_to_page(pfn), pfn, zone, nid);
1217}
1218
Mel Gorman7e18adb2015-06-30 14:57:05 -07001219#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
1220static void init_reserved_page(unsigned long pfn)
1221{
1222 pg_data_t *pgdat;
1223 int nid, zid;
1224
1225 if (!early_page_uninitialised(pfn))
1226 return;
1227
1228 nid = early_pfn_to_nid(pfn);
1229 pgdat = NODE_DATA(nid);
1230
1231 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
1232 struct zone *zone = &pgdat->node_zones[zid];
1233
1234 if (pfn >= zone->zone_start_pfn && pfn < zone_end_pfn(zone))
1235 break;
1236 }
1237 __init_single_pfn(pfn, zid, nid);
1238}
1239#else
1240static inline void init_reserved_page(unsigned long pfn)
1241{
1242}
1243#endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
1244
Nathan Zimmer92923ca2015-06-30 14:56:48 -07001245/*
1246 * Initialised pages do not have PageReserved set. This function is
1247 * called for each range allocated by the bootmem allocator and
1248 * marks the pages PageReserved. The remaining valid pages are later
1249 * sent to the buddy page allocator.
1250 */
Stefan Bader4b50bcc2016-05-20 16:58:38 -07001251void __meminit reserve_bootmem_region(phys_addr_t start, phys_addr_t end)
Nathan Zimmer92923ca2015-06-30 14:56:48 -07001252{
1253 unsigned long start_pfn = PFN_DOWN(start);
1254 unsigned long end_pfn = PFN_UP(end);
1255
Mel Gorman7e18adb2015-06-30 14:57:05 -07001256 for (; start_pfn < end_pfn; start_pfn++) {
1257 if (pfn_valid(start_pfn)) {
1258 struct page *page = pfn_to_page(start_pfn);
1259
1260 init_reserved_page(start_pfn);
Kirill A. Shutemov1d798ca2015-11-06 16:29:54 -08001261
1262 /* Avoid false-positive PageTail() */
1263 INIT_LIST_HEAD(&page->lru);
1264
Mel Gorman7e18adb2015-06-30 14:57:05 -07001265 SetPageReserved(page);
1266 }
1267 }
Nathan Zimmer92923ca2015-06-30 14:56:48 -07001268}
1269
KOSAKI Motohiroec95f532010-05-24 14:32:38 -07001270static void __free_pages_ok(struct page *page, unsigned int order)
1271{
1272 unsigned long flags;
Minchan Kim95e34412012-10-08 16:32:11 -07001273 int migratetype;
Mel Gormandc4b0ca2014-06-04 16:10:17 -07001274 unsigned long pfn = page_to_pfn(page);
KOSAKI Motohiroec95f532010-05-24 14:32:38 -07001275
Mel Gormane2769db2016-05-19 17:14:38 -07001276 if (!free_pages_prepare(page, order, true))
KOSAKI Motohiroec95f532010-05-24 14:32:38 -07001277 return;
1278
Mel Gormancfc47a22014-06-04 16:10:19 -07001279 migratetype = get_pfnblock_migratetype(page, pfn);
Nick Pigginc54ad302006-01-06 00:10:56 -08001280 local_irq_save(flags);
Christoph Lameterf8891e52006-06-30 01:55:45 -07001281 __count_vm_events(PGFREE, 1 << order);
Mel Gormandc4b0ca2014-06-04 16:10:17 -07001282 free_one_page(page_zone(page), page, pfn, order, migratetype);
Nick Pigginc54ad302006-01-06 00:10:56 -08001283 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
1285
Li Zhang949698a2016-05-19 17:11:37 -07001286static void __init __free_pages_boot_core(struct page *page, unsigned int order)
David Howellsa226f6c2006-01-06 00:11:08 -08001287{
Johannes Weinerc3993072012-01-10 15:08:10 -08001288 unsigned int nr_pages = 1 << order;
Yinghai Lue2d0bd22013-09-11 14:20:37 -07001289 struct page *p = page;
Johannes Weinerc3993072012-01-10 15:08:10 -08001290 unsigned int loop;
David Howellsa226f6c2006-01-06 00:11:08 -08001291
Yinghai Lue2d0bd22013-09-11 14:20:37 -07001292 prefetchw(p);
1293 for (loop = 0; loop < (nr_pages - 1); loop++, p++) {
1294 prefetchw(p + 1);
Johannes Weinerc3993072012-01-10 15:08:10 -08001295 __ClearPageReserved(p);
1296 set_page_count(p, 0);
David Howellsa226f6c2006-01-06 00:11:08 -08001297 }
Yinghai Lue2d0bd22013-09-11 14:20:37 -07001298 __ClearPageReserved(p);
1299 set_page_count(p, 0);
Johannes Weinerc3993072012-01-10 15:08:10 -08001300
Yinghai Lue2d0bd22013-09-11 14:20:37 -07001301 page_zone(page)->managed_pages += nr_pages;
Johannes Weinerc3993072012-01-10 15:08:10 -08001302 set_page_refcounted(page);
1303 __free_pages(page, order);
David Howellsa226f6c2006-01-06 00:11:08 -08001304}
1305
Mel Gorman75a592a2015-06-30 14:56:59 -07001306#if defined(CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID) || \
1307 defined(CONFIG_HAVE_MEMBLOCK_NODE_MAP)
Mel Gorman7ace9912015-08-06 15:46:13 -07001308
Mel Gorman75a592a2015-06-30 14:56:59 -07001309static struct mminit_pfnnid_cache early_pfnnid_cache __meminitdata;
1310
1311int __meminit early_pfn_to_nid(unsigned long pfn)
1312{
Mel Gorman7ace9912015-08-06 15:46:13 -07001313 static DEFINE_SPINLOCK(early_pfn_lock);
Mel Gorman75a592a2015-06-30 14:56:59 -07001314 int nid;
1315
Mel Gorman7ace9912015-08-06 15:46:13 -07001316 spin_lock(&early_pfn_lock);
Mel Gorman75a592a2015-06-30 14:56:59 -07001317 nid = __early_pfn_to_nid(pfn, &early_pfnnid_cache);
Mel Gorman7ace9912015-08-06 15:46:13 -07001318 if (nid < 0)
Mel Gormane4568d32016-07-14 12:07:20 -07001319 nid = first_online_node;
Mel Gorman7ace9912015-08-06 15:46:13 -07001320 spin_unlock(&early_pfn_lock);
1321
1322 return nid;
Mel Gorman75a592a2015-06-30 14:56:59 -07001323}
1324#endif
1325
1326#ifdef CONFIG_NODES_SPAN_OTHER_NODES
1327static inline bool __meminit meminit_pfn_in_nid(unsigned long pfn, int node,
1328 struct mminit_pfnnid_cache *state)
1329{
1330 int nid;
1331
1332 nid = __early_pfn_to_nid(pfn, state);
1333 if (nid >= 0 && nid != node)
1334 return false;
1335 return true;
1336}
1337
1338/* Only safe to use early in boot when initialisation is single-threaded */
1339static inline bool __meminit early_pfn_in_nid(unsigned long pfn, int node)
1340{
1341 return meminit_pfn_in_nid(pfn, node, &early_pfnnid_cache);
1342}
1343
1344#else
1345
1346static inline bool __meminit early_pfn_in_nid(unsigned long pfn, int node)
1347{
1348 return true;
1349}
1350static inline bool __meminit meminit_pfn_in_nid(unsigned long pfn, int node,
1351 struct mminit_pfnnid_cache *state)
1352{
1353 return true;
1354}
1355#endif
1356
1357
Mel Gorman0e1cc952015-06-30 14:57:27 -07001358void __init __free_pages_bootmem(struct page *page, unsigned long pfn,
Mel Gorman3a80a7f2015-06-30 14:57:02 -07001359 unsigned int order)
1360{
1361 if (early_page_uninitialised(pfn))
1362 return;
Li Zhang949698a2016-05-19 17:11:37 -07001363 return __free_pages_boot_core(page, order);
Mel Gorman3a80a7f2015-06-30 14:57:02 -07001364}
1365
Joonsoo Kim7cf91a92016-03-15 14:57:51 -07001366/*
1367 * Check that the whole (or subset of) a pageblock given by the interval of
1368 * [start_pfn, end_pfn) is valid and within the same zone, before scanning it
1369 * with the migration of free compaction scanner. The scanners then need to
1370 * use only pfn_valid_within() check for arches that allow holes within
1371 * pageblocks.
1372 *
1373 * Return struct page pointer of start_pfn, or NULL if checks were not passed.
1374 *
1375 * It's possible on some configurations to have a setup like node0 node1 node0
1376 * i.e. it's possible that all pages within a zones range of pages do not
1377 * belong to a single zone. We assume that a border between node0 and node1
1378 * can occur within a single pageblock, but not a node0 node1 node0
1379 * interleaving within a single pageblock. It is therefore sufficient to check
1380 * the first and last page of a pageblock and avoid checking each individual
1381 * page in a pageblock.
1382 */
1383struct page *__pageblock_pfn_to_page(unsigned long start_pfn,
1384 unsigned long end_pfn, struct zone *zone)
1385{
1386 struct page *start_page;
1387 struct page *end_page;
1388
1389 /* end_pfn is one past the range we are checking */
1390 end_pfn--;
1391
1392 if (!pfn_valid(start_pfn) || !pfn_valid(end_pfn))
1393 return NULL;
1394
1395 start_page = pfn_to_page(start_pfn);
1396
1397 if (page_zone(start_page) != zone)
1398 return NULL;
1399
1400 end_page = pfn_to_page(end_pfn);
1401
1402 /* This gives a shorter code than deriving page_zone(end_page) */
1403 if (page_zone_id(start_page) != page_zone_id(end_page))
1404 return NULL;
1405
1406 return start_page;
1407}
1408
1409void set_zone_contiguous(struct zone *zone)
1410{
1411 unsigned long block_start_pfn = zone->zone_start_pfn;
1412 unsigned long block_end_pfn;
1413
1414 block_end_pfn = ALIGN(block_start_pfn + 1, pageblock_nr_pages);
1415 for (; block_start_pfn < zone_end_pfn(zone);
1416 block_start_pfn = block_end_pfn,
1417 block_end_pfn += pageblock_nr_pages) {
1418
1419 block_end_pfn = min(block_end_pfn, zone_end_pfn(zone));
1420
1421 if (!__pageblock_pfn_to_page(block_start_pfn,
1422 block_end_pfn, zone))
1423 return;
1424 }
1425
1426 /* We confirm that there is no hole */
1427 zone->contiguous = true;
1428}
1429
1430void clear_zone_contiguous(struct zone *zone)
1431{
1432 zone->contiguous = false;
1433}
1434
Mel Gorman7e18adb2015-06-30 14:57:05 -07001435#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
Mel Gorman0e1cc952015-06-30 14:57:27 -07001436static void __init deferred_free_range(struct page *page,
Mel Gormana4de83d2015-06-30 14:57:16 -07001437 unsigned long pfn, int nr_pages)
1438{
1439 int i;
1440
1441 if (!page)
1442 return;
1443
1444 /* Free a large naturally-aligned chunk if possible */
Xishi Qiue7801492016-10-07 16:58:09 -07001445 if (nr_pages == pageblock_nr_pages &&
1446 (pfn & (pageblock_nr_pages - 1)) == 0) {
Mel Gormanac5d2532015-06-30 14:57:20 -07001447 set_pageblock_migratetype(page, MIGRATE_MOVABLE);
Xishi Qiue7801492016-10-07 16:58:09 -07001448 __free_pages_boot_core(page, pageblock_order);
Mel Gormana4de83d2015-06-30 14:57:16 -07001449 return;
1450 }
1451
Xishi Qiue7801492016-10-07 16:58:09 -07001452 for (i = 0; i < nr_pages; i++, page++, pfn++) {
1453 if ((pfn & (pageblock_nr_pages - 1)) == 0)
1454 set_pageblock_migratetype(page, MIGRATE_MOVABLE);
Li Zhang949698a2016-05-19 17:11:37 -07001455 __free_pages_boot_core(page, 0);
Xishi Qiue7801492016-10-07 16:58:09 -07001456 }
Mel Gormana4de83d2015-06-30 14:57:16 -07001457}
1458
Nicolai Stanged3cd1312015-08-06 15:46:16 -07001459/* Completion tracking for deferred_init_memmap() threads */
1460static atomic_t pgdat_init_n_undone __initdata;
1461static __initdata DECLARE_COMPLETION(pgdat_init_all_done_comp);
1462
1463static inline void __init pgdat_init_report_one_done(void)
1464{
1465 if (atomic_dec_and_test(&pgdat_init_n_undone))
1466 complete(&pgdat_init_all_done_comp);
1467}
Mel Gorman0e1cc952015-06-30 14:57:27 -07001468
Mel Gorman7e18adb2015-06-30 14:57:05 -07001469/* Initialise remaining memory on a node */
Mel Gorman0e1cc952015-06-30 14:57:27 -07001470static int __init deferred_init_memmap(void *data)
Mel Gorman7e18adb2015-06-30 14:57:05 -07001471{
Mel Gorman0e1cc952015-06-30 14:57:27 -07001472 pg_data_t *pgdat = data;
1473 int nid = pgdat->node_id;
Mel Gorman7e18adb2015-06-30 14:57:05 -07001474 struct mminit_pfnnid_cache nid_init_state = { };
1475 unsigned long start = jiffies;
1476 unsigned long nr_pages = 0;
1477 unsigned long walk_start, walk_end;
1478 int i, zid;
1479 struct zone *zone;
Mel Gorman7e18adb2015-06-30 14:57:05 -07001480 unsigned long first_init_pfn = pgdat->first_deferred_pfn;
Mel Gorman0e1cc952015-06-30 14:57:27 -07001481 const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
Mel Gorman7e18adb2015-06-30 14:57:05 -07001482
Mel Gorman0e1cc952015-06-30 14:57:27 -07001483 if (first_init_pfn == ULONG_MAX) {
Nicolai Stanged3cd1312015-08-06 15:46:16 -07001484 pgdat_init_report_one_done();
Mel Gorman0e1cc952015-06-30 14:57:27 -07001485 return 0;
1486 }
1487
1488 /* Bind memory initialisation thread to a local node if possible */
1489 if (!cpumask_empty(cpumask))
1490 set_cpus_allowed_ptr(current, cpumask);
Mel Gorman7e18adb2015-06-30 14:57:05 -07001491
1492 /* Sanity check boundaries */
1493 BUG_ON(pgdat->first_deferred_pfn < pgdat->node_start_pfn);
1494 BUG_ON(pgdat->first_deferred_pfn > pgdat_end_pfn(pgdat));
1495 pgdat->first_deferred_pfn = ULONG_MAX;
1496
1497 /* Only the highest zone is deferred so find it */
1498 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
1499 zone = pgdat->node_zones + zid;
1500 if (first_init_pfn < zone_end_pfn(zone))
1501 break;
1502 }
1503
1504 for_each_mem_pfn_range(i, nid, &walk_start, &walk_end, NULL) {
1505 unsigned long pfn, end_pfn;
Mel Gorman54608c32015-06-30 14:57:09 -07001506 struct page *page = NULL;
Mel Gormana4de83d2015-06-30 14:57:16 -07001507 struct page *free_base_page = NULL;
1508 unsigned long free_base_pfn = 0;
1509 int nr_to_free = 0;
Mel Gorman7e18adb2015-06-30 14:57:05 -07001510
1511 end_pfn = min(walk_end, zone_end_pfn(zone));
1512 pfn = first_init_pfn;
1513 if (pfn < walk_start)
1514 pfn = walk_start;
1515 if (pfn < zone->zone_start_pfn)
1516 pfn = zone->zone_start_pfn;
1517
1518 for (; pfn < end_pfn; pfn++) {
Mel Gorman54608c32015-06-30 14:57:09 -07001519 if (!pfn_valid_within(pfn))
Mel Gormana4de83d2015-06-30 14:57:16 -07001520 goto free_range;
Mel Gorman7e18adb2015-06-30 14:57:05 -07001521
Mel Gorman54608c32015-06-30 14:57:09 -07001522 /*
1523 * Ensure pfn_valid is checked every
Xishi Qiue7801492016-10-07 16:58:09 -07001524 * pageblock_nr_pages for memory holes
Mel Gorman54608c32015-06-30 14:57:09 -07001525 */
Xishi Qiue7801492016-10-07 16:58:09 -07001526 if ((pfn & (pageblock_nr_pages - 1)) == 0) {
Mel Gorman54608c32015-06-30 14:57:09 -07001527 if (!pfn_valid(pfn)) {
1528 page = NULL;
Mel Gormana4de83d2015-06-30 14:57:16 -07001529 goto free_range;
Mel Gorman54608c32015-06-30 14:57:09 -07001530 }
1531 }
1532
1533 if (!meminit_pfn_in_nid(pfn, nid, &nid_init_state)) {
1534 page = NULL;
Mel Gormana4de83d2015-06-30 14:57:16 -07001535 goto free_range;
Mel Gorman54608c32015-06-30 14:57:09 -07001536 }
1537
1538 /* Minimise pfn page lookups and scheduler checks */
Xishi Qiue7801492016-10-07 16:58:09 -07001539 if (page && (pfn & (pageblock_nr_pages - 1)) != 0) {
Mel Gorman54608c32015-06-30 14:57:09 -07001540 page++;
1541 } else {
Mel Gormana4de83d2015-06-30 14:57:16 -07001542 nr_pages += nr_to_free;
1543 deferred_free_range(free_base_page,
1544 free_base_pfn, nr_to_free);
1545 free_base_page = NULL;
1546 free_base_pfn = nr_to_free = 0;
1547
Mel Gorman54608c32015-06-30 14:57:09 -07001548 page = pfn_to_page(pfn);
1549 cond_resched();
1550 }
Mel Gorman7e18adb2015-06-30 14:57:05 -07001551
1552 if (page->flags) {
1553 VM_BUG_ON(page_zone(page) != zone);
Mel Gormana4de83d2015-06-30 14:57:16 -07001554 goto free_range;
Mel Gorman7e18adb2015-06-30 14:57:05 -07001555 }
1556
1557 __init_single_page(page, pfn, zid, nid);
Mel Gormana4de83d2015-06-30 14:57:16 -07001558 if (!free_base_page) {
1559 free_base_page = page;
1560 free_base_pfn = pfn;
1561 nr_to_free = 0;
1562 }
1563 nr_to_free++;
1564
1565 /* Where possible, batch up pages for a single free */
1566 continue;
1567free_range:
1568 /* Free the current block of pages to allocator */
1569 nr_pages += nr_to_free;
1570 deferred_free_range(free_base_page, free_base_pfn,
1571 nr_to_free);
1572 free_base_page = NULL;
1573 free_base_pfn = nr_to_free = 0;
Mel Gorman7e18adb2015-06-30 14:57:05 -07001574 }
Xishi Qiue7801492016-10-07 16:58:09 -07001575 /* Free the last block of pages to allocator */
1576 nr_pages += nr_to_free;
1577 deferred_free_range(free_base_page, free_base_pfn, nr_to_free);
Mel Gormana4de83d2015-06-30 14:57:16 -07001578
Mel Gorman7e18adb2015-06-30 14:57:05 -07001579 first_init_pfn = max(end_pfn, first_init_pfn);
1580 }
1581
1582 /* Sanity check that the next zone really is unpopulated */
1583 WARN_ON(++zid < MAX_NR_ZONES && populated_zone(++zone));
1584
Mel Gorman0e1cc952015-06-30 14:57:27 -07001585 pr_info("node %d initialised, %lu pages in %ums\n", nid, nr_pages,
Mel Gorman7e18adb2015-06-30 14:57:05 -07001586 jiffies_to_msecs(jiffies - start));
Nicolai Stanged3cd1312015-08-06 15:46:16 -07001587
1588 pgdat_init_report_one_done();
Mel Gorman0e1cc952015-06-30 14:57:27 -07001589 return 0;
1590}
Joonsoo Kim7cf91a92016-03-15 14:57:51 -07001591#endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
Mel Gorman0e1cc952015-06-30 14:57:27 -07001592
1593void __init page_alloc_init_late(void)
1594{
Joonsoo Kim7cf91a92016-03-15 14:57:51 -07001595 struct zone *zone;
1596
1597#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
Mel Gorman0e1cc952015-06-30 14:57:27 -07001598 int nid;
1599
Nicolai Stanged3cd1312015-08-06 15:46:16 -07001600 /* There will be num_node_state(N_MEMORY) threads */
1601 atomic_set(&pgdat_init_n_undone, num_node_state(N_MEMORY));
Mel Gorman0e1cc952015-06-30 14:57:27 -07001602 for_each_node_state(nid, N_MEMORY) {
Mel Gorman0e1cc952015-06-30 14:57:27 -07001603 kthread_run(deferred_init_memmap, NODE_DATA(nid), "pgdatinit%d", nid);
1604 }
1605
1606 /* Block until all are initialised */
Nicolai Stanged3cd1312015-08-06 15:46:16 -07001607 wait_for_completion(&pgdat_init_all_done_comp);
Mel Gorman4248b0d2015-08-06 15:46:20 -07001608
1609 /* Reinit limits that are based on free pages after the kernel is up */
1610 files_maxfiles_init();
Joonsoo Kim7cf91a92016-03-15 14:57:51 -07001611#endif
Pavel Tatashin87395ee2017-08-18 15:16:05 -07001612#ifdef CONFIG_ARCH_DISCARD_MEMBLOCK
1613 /* Discard memblock private memory */
1614 memblock_discard();
1615#endif
Joonsoo Kim7cf91a92016-03-15 14:57:51 -07001616
1617 for_each_populated_zone(zone)
1618 set_zone_contiguous(zone);
Mel Gorman7e18adb2015-06-30 14:57:05 -07001619}
Mel Gorman7e18adb2015-06-30 14:57:05 -07001620
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001621#ifdef CONFIG_CMA
Li Zhong9cf510a2013-08-23 13:52:52 +08001622/* Free whole pageblock and set its migration type to MIGRATE_CMA. */
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001623void __init init_cma_reserved_pageblock(struct page *page)
1624{
1625 unsigned i = pageblock_nr_pages;
1626 struct page *p = page;
1627
1628 do {
1629 __ClearPageReserved(p);
1630 set_page_count(p, 0);
1631 } while (++p, --i);
1632
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001633 set_pageblock_migratetype(page, MIGRATE_CMA);
Michal Nazarewiczdc783272014-07-02 15:22:35 -07001634
1635 if (pageblock_order >= MAX_ORDER) {
1636 i = pageblock_nr_pages;
1637 p = page;
1638 do {
1639 set_page_refcounted(p);
1640 __free_pages(p, MAX_ORDER - 1);
1641 p += MAX_ORDER_NR_PAGES;
1642 } while (i -= MAX_ORDER_NR_PAGES);
1643 } else {
1644 set_page_refcounted(page);
1645 __free_pages(page, pageblock_order);
1646 }
1647
Jiang Liu3dcc0572013-07-03 15:03:21 -07001648 adjust_managed_page_count(page, pageblock_nr_pages);
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001649}
1650#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652/*
1653 * The order of subdivision here is critical for the IO subsystem.
1654 * Please do not alter this order without good reasons and regression
1655 * testing. Specifically, as large blocks of memory are subdivided,
1656 * the order in which smaller blocks are delivered depends on the order
1657 * they're subdivided in this function. This is the primary factor
1658 * influencing the order in which pages are delivered to the IO
1659 * subsystem according to empirical testing, and this is also justified
1660 * by considering the behavior of a buddy system containing a single
1661 * large block of memory acted on by a series of small allocations.
1662 * This behavior is a critical factor in sglist merging's success.
1663 *
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +01001664 * -- nyc
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 */
Nick Piggin085cc7d2006-01-06 00:11:01 -08001666static inline void expand(struct zone *zone, struct page *page,
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001667 int low, int high, struct free_area *area,
1668 int migratetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
1670 unsigned long size = 1 << high;
1671
1672 while (high > low) {
1673 area--;
1674 high--;
1675 size >>= 1;
Sasha Levin309381fea2014-01-23 15:52:54 -08001676 VM_BUG_ON_PAGE(bad_range(zone, &page[size]), &page[size]);
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -08001677
Joonsoo Kimacbc15a2016-10-07 16:58:15 -07001678 /*
1679 * Mark as guard pages (or page), that will allow to
1680 * merge back to allocator when buddy will be freed.
1681 * Corresponding page table entries will not be touched,
1682 * pages will stay not present in virtual address space
1683 */
1684 if (set_page_guard(zone, &page[size], high, migratetype))
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -08001685 continue;
Joonsoo Kimacbc15a2016-10-07 16:58:15 -07001686
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001687 list_add(&page[size].lru, &area->free_list[migratetype]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 area->nr_free++;
1689 set_page_order(&page[size], high);
1690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691}
1692
Vlastimil Babka4e611802016-05-19 17:14:41 -07001693static void check_new_page_bad(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
Vlastimil Babka4e611802016-05-19 17:14:41 -07001695 const char *bad_reason = NULL;
1696 unsigned long bad_flags = 0;
Dave Hansenf0b791a2014-01-23 15:52:49 -08001697
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08001698 if (unlikely(atomic_read(&page->_mapcount) != -1))
Dave Hansenf0b791a2014-01-23 15:52:49 -08001699 bad_reason = "nonzero mapcount";
1700 if (unlikely(page->mapping != NULL))
1701 bad_reason = "non-NULL mapping";
Joonsoo Kimfe896d12016-03-17 14:19:26 -07001702 if (unlikely(page_ref_count(page) != 0))
Dave Hansenf0b791a2014-01-23 15:52:49 -08001703 bad_reason = "nonzero _count";
Naoya Horiguchif4c18e62015-08-06 15:47:08 -07001704 if (unlikely(page->flags & __PG_HWPOISON)) {
1705 bad_reason = "HWPoisoned (hardware-corrupted)";
1706 bad_flags = __PG_HWPOISON;
Naoya Horiguchie570f562016-05-20 16:58:50 -07001707 /* Don't complain about hwpoisoned pages */
1708 page_mapcount_reset(page); /* remove PageBuddy */
1709 return;
Naoya Horiguchif4c18e62015-08-06 15:47:08 -07001710 }
Dave Hansenf0b791a2014-01-23 15:52:49 -08001711 if (unlikely(page->flags & PAGE_FLAGS_CHECK_AT_PREP)) {
1712 bad_reason = "PAGE_FLAGS_CHECK_AT_PREP flag set";
1713 bad_flags = PAGE_FLAGS_CHECK_AT_PREP;
1714 }
Johannes Weiner9edad6e2014-12-10 15:44:58 -08001715#ifdef CONFIG_MEMCG
1716 if (unlikely(page->mem_cgroup))
1717 bad_reason = "page still charged to cgroup";
1718#endif
Vlastimil Babka4e611802016-05-19 17:14:41 -07001719 bad_page(page, bad_reason, bad_flags);
1720}
1721
1722/*
1723 * This page is about to be returned from the page allocator
1724 */
1725static inline int check_new_page(struct page *page)
1726{
1727 if (likely(page_expected_state(page,
1728 PAGE_FLAGS_CHECK_AT_PREP|__PG_HWPOISON)))
1729 return 0;
1730
1731 check_new_page_bad(page);
1732 return 1;
Wu Fengguang2a7684a2009-09-16 11:50:12 +02001733}
1734
Laura Abbott1414c7f2016-03-15 14:56:30 -07001735static inline bool free_pages_prezeroed(bool poisoned)
1736{
1737 return IS_ENABLED(CONFIG_PAGE_POISONING_ZERO) &&
1738 page_poisoning_enabled() && poisoned;
1739}
1740
Mel Gorman479f8542016-05-19 17:14:35 -07001741#ifdef CONFIG_DEBUG_VM
1742static bool check_pcp_refill(struct page *page)
1743{
1744 return false;
1745}
1746
1747static bool check_new_pcp(struct page *page)
1748{
1749 return check_new_page(page);
1750}
1751#else
1752static bool check_pcp_refill(struct page *page)
1753{
1754 return check_new_page(page);
1755}
1756static bool check_new_pcp(struct page *page)
1757{
1758 return false;
1759}
1760#endif /* CONFIG_DEBUG_VM */
1761
1762static bool check_new_pages(struct page *page, unsigned int order)
1763{
1764 int i;
1765 for (i = 0; i < (1 << order); i++) {
1766 struct page *p = page + i;
1767
1768 if (unlikely(check_new_page(p)))
1769 return true;
1770 }
1771
1772 return false;
1773}
1774
Joonsoo Kim46f24fd2016-07-26 15:23:58 -07001775inline void post_alloc_hook(struct page *page, unsigned int order,
1776 gfp_t gfp_flags)
1777{
1778 set_page_private(page, 0);
1779 set_page_refcounted(page);
1780
1781 arch_alloc_page(page, order);
1782 kernel_map_pages(page, 1 << order, 1);
1783 kernel_poison_pages(page, 1 << order, 1);
1784 kasan_alloc_pages(page, order);
1785 set_page_owner(page, order, gfp_flags);
1786}
1787
Mel Gorman479f8542016-05-19 17:14:35 -07001788static void prep_new_page(struct page *page, unsigned int order, gfp_t gfp_flags,
Mel Gormanc6038442016-05-19 17:13:38 -07001789 unsigned int alloc_flags)
Wu Fengguang2a7684a2009-09-16 11:50:12 +02001790{
1791 int i;
Laura Abbott1414c7f2016-03-15 14:56:30 -07001792 bool poisoned = true;
Wu Fengguang2a7684a2009-09-16 11:50:12 +02001793
1794 for (i = 0; i < (1 << order); i++) {
1795 struct page *p = page + i;
Laura Abbott1414c7f2016-03-15 14:56:30 -07001796 if (poisoned)
1797 poisoned &= page_is_poisoned(p);
Wu Fengguang2a7684a2009-09-16 11:50:12 +02001798 }
Hugh Dickins689bceb2005-11-21 21:32:20 -08001799
Joonsoo Kim46f24fd2016-07-26 15:23:58 -07001800 post_alloc_hook(page, order, gfp_flags);
Nick Piggin17cf4402006-03-22 00:08:41 -08001801
Laura Abbott1414c7f2016-03-15 14:56:30 -07001802 if (!free_pages_prezeroed(poisoned) && (gfp_flags & __GFP_ZERO))
Anisse Astierf4d28972015-06-24 16:56:36 -07001803 for (i = 0; i < (1 << order); i++)
1804 clear_highpage(page + i);
Nick Piggin17cf4402006-03-22 00:08:41 -08001805
1806 if (order && (gfp_flags & __GFP_COMP))
1807 prep_compound_page(page, order);
1808
Vlastimil Babka75379192015-02-11 15:25:38 -08001809 /*
Michal Hocko2f064f32015-08-21 14:11:51 -07001810 * page is set pfmemalloc when ALLOC_NO_WATERMARKS was necessary to
Vlastimil Babka75379192015-02-11 15:25:38 -08001811 * allocate the page. The expectation is that the caller is taking
1812 * steps that will free more memory. The caller should avoid the page
1813 * being used for !PFMEMALLOC purposes.
1814 */
Michal Hocko2f064f32015-08-21 14:11:51 -07001815 if (alloc_flags & ALLOC_NO_WATERMARKS)
1816 set_page_pfmemalloc(page);
1817 else
1818 clear_page_pfmemalloc(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819}
1820
Mel Gorman56fd56b2007-10-16 01:25:58 -07001821/*
1822 * Go through the free lists for the given migratetype and remove
1823 * the smallest available page from the freelists
1824 */
Mel Gorman728ec982009-06-16 15:32:04 -07001825static inline
1826struct page *__rmqueue_smallest(struct zone *zone, unsigned int order,
Mel Gorman56fd56b2007-10-16 01:25:58 -07001827 int migratetype)
1828{
1829 unsigned int current_order;
Pintu Kumarb8af2942013-09-11 14:20:34 -07001830 struct free_area *area;
Mel Gorman56fd56b2007-10-16 01:25:58 -07001831 struct page *page;
1832
1833 /* Find a page of the appropriate size in the preferred list */
1834 for (current_order = order; current_order < MAX_ORDER; ++current_order) {
1835 area = &(zone->free_area[current_order]);
Geliang Tanga16601c2016-01-14 15:20:30 -08001836 page = list_first_entry_or_null(&area->free_list[migratetype],
Mel Gorman56fd56b2007-10-16 01:25:58 -07001837 struct page, lru);
Geliang Tanga16601c2016-01-14 15:20:30 -08001838 if (!page)
1839 continue;
Mel Gorman56fd56b2007-10-16 01:25:58 -07001840 list_del(&page->lru);
1841 rmv_page_order(page);
1842 area->nr_free--;
Mel Gorman56fd56b2007-10-16 01:25:58 -07001843 expand(zone, page, order, current_order, area, migratetype);
Vlastimil Babkabb14c2c2015-09-08 15:01:25 -07001844 set_pcppage_migratetype(page, migratetype);
Mel Gorman56fd56b2007-10-16 01:25:58 -07001845 return page;
1846 }
1847
1848 return NULL;
1849}
1850
1851
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001852/*
1853 * This array describes the order lists are fallen back to when
1854 * the free lists for the desirable migrate type are depleted
1855 */
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001856static int fallbacks[MIGRATE_TYPES][4] = {
Mel Gorman974a7862015-11-06 16:28:34 -08001857 [MIGRATE_UNMOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_MOVABLE, MIGRATE_TYPES },
1858 [MIGRATE_RECLAIMABLE] = { MIGRATE_UNMOVABLE, MIGRATE_MOVABLE, MIGRATE_TYPES },
1859 [MIGRATE_MOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE, MIGRATE_TYPES },
Joonsoo Kimdc676472015-04-14 15:45:15 -07001860#ifdef CONFIG_CMA
Mel Gorman974a7862015-11-06 16:28:34 -08001861 [MIGRATE_CMA] = { MIGRATE_TYPES }, /* Never used */
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001862#endif
Minchan Kim194159f2013-02-22 16:33:58 -08001863#ifdef CONFIG_MEMORY_ISOLATION
Mel Gorman974a7862015-11-06 16:28:34 -08001864 [MIGRATE_ISOLATE] = { MIGRATE_TYPES }, /* Never used */
Minchan Kim194159f2013-02-22 16:33:58 -08001865#endif
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001866};
1867
Joonsoo Kimdc676472015-04-14 15:45:15 -07001868#ifdef CONFIG_CMA
1869static struct page *__rmqueue_cma_fallback(struct zone *zone,
1870 unsigned int order)
1871{
1872 return __rmqueue_smallest(zone, order, MIGRATE_CMA);
1873}
1874#else
1875static inline struct page *__rmqueue_cma_fallback(struct zone *zone,
1876 unsigned int order) { return NULL; }
1877#endif
1878
Mel Gormanc361be52007-10-16 01:25:51 -07001879/*
1880 * Move the free pages in a range to the free lists of the requested type.
Mel Gormand9c23402007-10-16 01:26:01 -07001881 * Note that start_page and end_pages are not aligned on a pageblock
Mel Gormanc361be52007-10-16 01:25:51 -07001882 * boundary. If alignment is required, use move_freepages_block()
1883 */
Minchan Kim435b4052012-10-08 16:32:16 -07001884int move_freepages(struct zone *zone,
Adrian Bunkb69a7282008-07-23 21:28:12 -07001885 struct page *start_page, struct page *end_page,
1886 int migratetype)
Mel Gormanc361be52007-10-16 01:25:51 -07001887{
1888 struct page *page;
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08001889 unsigned int order;
Mel Gormand1003132007-10-16 01:26:00 -07001890 int pages_moved = 0;
Mel Gormanc361be52007-10-16 01:25:51 -07001891
1892#ifndef CONFIG_HOLES_IN_ZONE
1893 /*
1894 * page_zone is not safe to call in this context when
1895 * CONFIG_HOLES_IN_ZONE is set. This bug check is probably redundant
1896 * anyway as we check zone boundaries in move_freepages_block().
1897 * Remove at a later date when no bug reports exist related to
Mel Gormanac0e5b72007-10-16 01:25:58 -07001898 * grouping pages by mobility
Mel Gormanc361be52007-10-16 01:25:51 -07001899 */
Mel Gorman97ee4ba2014-10-09 15:28:28 -07001900 VM_BUG_ON(page_zone(start_page) != page_zone(end_page));
Mel Gormanc361be52007-10-16 01:25:51 -07001901#endif
1902
1903 for (page = start_page; page <= end_page;) {
1904 if (!pfn_valid_within(page_to_pfn(page))) {
1905 page++;
1906 continue;
1907 }
1908
Ard Biesheuvel22cccef2017-01-10 16:58:00 -08001909 /* Make sure we are not inadvertently changing nodes */
1910 VM_BUG_ON_PAGE(page_to_nid(page) != zone_to_nid(zone), page);
1911
Mel Gormanc361be52007-10-16 01:25:51 -07001912 if (!PageBuddy(page)) {
1913 page++;
1914 continue;
1915 }
1916
1917 order = page_order(page);
Kirill A. Shutemov84be48d2011-03-22 16:33:41 -07001918 list_move(&page->lru,
1919 &zone->free_area[order].free_list[migratetype]);
Mel Gormanc361be52007-10-16 01:25:51 -07001920 page += 1 << order;
Mel Gormand1003132007-10-16 01:26:00 -07001921 pages_moved += 1 << order;
Mel Gormanc361be52007-10-16 01:25:51 -07001922 }
1923
Mel Gormand1003132007-10-16 01:26:00 -07001924 return pages_moved;
Mel Gormanc361be52007-10-16 01:25:51 -07001925}
1926
Minchan Kimee6f5092012-07-31 16:43:50 -07001927int move_freepages_block(struct zone *zone, struct page *page,
Linus Torvalds68e3e922012-06-03 20:05:57 -07001928 int migratetype)
Mel Gormanc361be52007-10-16 01:25:51 -07001929{
1930 unsigned long start_pfn, end_pfn;
1931 struct page *start_page, *end_page;
1932
1933 start_pfn = page_to_pfn(page);
Mel Gormand9c23402007-10-16 01:26:01 -07001934 start_pfn = start_pfn & ~(pageblock_nr_pages-1);
Mel Gormanc361be52007-10-16 01:25:51 -07001935 start_page = pfn_to_page(start_pfn);
Mel Gormand9c23402007-10-16 01:26:01 -07001936 end_page = start_page + pageblock_nr_pages - 1;
1937 end_pfn = start_pfn + pageblock_nr_pages - 1;
Mel Gormanc361be52007-10-16 01:25:51 -07001938
1939 /* Do not cross zone boundaries */
Cody P Schafer108bcc92013-02-22 16:35:23 -08001940 if (!zone_spans_pfn(zone, start_pfn))
Mel Gormanc361be52007-10-16 01:25:51 -07001941 start_page = page;
Cody P Schafer108bcc92013-02-22 16:35:23 -08001942 if (!zone_spans_pfn(zone, end_pfn))
Mel Gormanc361be52007-10-16 01:25:51 -07001943 return 0;
1944
1945 return move_freepages(zone, start_page, end_page, migratetype);
1946}
1947
Mel Gorman2f66a682009-09-21 17:02:31 -07001948static void change_pageblock_range(struct page *pageblock_page,
1949 int start_order, int migratetype)
1950{
1951 int nr_pageblocks = 1 << (start_order - pageblock_order);
1952
1953 while (nr_pageblocks--) {
1954 set_pageblock_migratetype(pageblock_page, migratetype);
1955 pageblock_page += pageblock_nr_pages;
1956 }
1957}
1958
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07001959/*
Vlastimil Babka9c0415e2015-02-11 15:28:21 -08001960 * When we are falling back to another migratetype during allocation, try to
1961 * steal extra free pages from the same pageblocks to satisfy further
1962 * allocations, instead of polluting multiple pageblocks.
1963 *
1964 * If we are stealing a relatively large buddy page, it is likely there will
1965 * be more free pages in the pageblock, so try to steal them all. For
1966 * reclaimable and unmovable allocations, we steal regardless of page size,
1967 * as fragmentation caused by those allocations polluting movable pageblocks
1968 * is worse than movable allocations stealing from unmovable and reclaimable
1969 * pageblocks.
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07001970 */
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07001971static bool can_steal_fallback(unsigned int order, int start_mt)
1972{
1973 /*
1974 * Leaving this order check is intended, although there is
1975 * relaxed order check in next check. The reason is that
1976 * we can actually steal whole pageblock if this condition met,
1977 * but, below check doesn't guarantee it and that is just heuristic
1978 * so could be changed anytime.
1979 */
1980 if (order >= pageblock_order)
1981 return true;
1982
1983 if (order >= pageblock_order / 2 ||
1984 start_mt == MIGRATE_RECLAIMABLE ||
1985 start_mt == MIGRATE_UNMOVABLE ||
1986 page_group_by_mobility_disabled)
1987 return true;
1988
1989 return false;
1990}
1991
1992/*
1993 * This function implements actual steal behaviour. If order is large enough,
1994 * we can steal whole pageblock. If not, we first move freepages in this
1995 * pageblock and check whether half of pages are moved or not. If half of
1996 * pages are moved, we can change migratetype of pageblock and permanently
1997 * use it's pages as requested migratetype in the future.
1998 */
1999static void steal_suitable_fallback(struct zone *zone, struct page *page,
2000 int start_type)
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07002001{
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08002002 unsigned int current_order = page_order(page);
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002003 int pages;
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07002004
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07002005 /* Take ownership for orders >= pageblock_order */
2006 if (current_order >= pageblock_order) {
2007 change_pageblock_range(page, current_order, start_type);
Vlastimil Babka3a1086f2015-02-11 15:28:18 -08002008 return;
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07002009 }
2010
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002011 pages = move_freepages_block(zone, page, start_type);
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07002012
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002013 /* Claim the whole block if over half of it is free */
2014 if (pages >= (1 << (pageblock_order-1)) ||
2015 page_group_by_mobility_disabled)
2016 set_pageblock_migratetype(page, start_type);
2017}
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07002018
Joonsoo Kim2149cda2015-04-14 15:45:21 -07002019/*
2020 * Check whether there is a suitable fallback freepage with requested order.
2021 * If only_stealable is true, this function returns fallback_mt only if
2022 * we can steal other freepages all together. This would help to reduce
2023 * fragmentation due to mixed migratetype pages in one pageblock.
2024 */
2025int find_suitable_fallback(struct free_area *area, unsigned int order,
2026 int migratetype, bool only_stealable, bool *can_steal)
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002027{
2028 int i;
2029 int fallback_mt;
2030
2031 if (area->nr_free == 0)
2032 return -1;
2033
2034 *can_steal = false;
2035 for (i = 0;; i++) {
2036 fallback_mt = fallbacks[migratetype][i];
Mel Gorman974a7862015-11-06 16:28:34 -08002037 if (fallback_mt == MIGRATE_TYPES)
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002038 break;
2039
2040 if (list_empty(&area->free_list[fallback_mt]))
2041 continue;
2042
2043 if (can_steal_fallback(order, migratetype))
2044 *can_steal = true;
2045
Joonsoo Kim2149cda2015-04-14 15:45:21 -07002046 if (!only_stealable)
2047 return fallback_mt;
2048
2049 if (*can_steal)
2050 return fallback_mt;
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07002051 }
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002052
2053 return -1;
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07002054}
2055
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002056/*
2057 * Reserve a pageblock for exclusive use of high-order atomic allocations if
2058 * there are no empty page blocks that contain a page with a suitable order
2059 */
2060static void reserve_highatomic_pageblock(struct page *page, struct zone *zone,
2061 unsigned int alloc_order)
2062{
2063 int mt;
2064 unsigned long max_managed, flags;
2065
2066 /*
2067 * Limit the number reserved to 1 pageblock or roughly 1% of a zone.
2068 * Check is race-prone but harmless.
2069 */
2070 max_managed = (zone->managed_pages / 100) + pageblock_nr_pages;
2071 if (zone->nr_reserved_highatomic >= max_managed)
2072 return;
2073
2074 spin_lock_irqsave(&zone->lock, flags);
2075
2076 /* Recheck the nr_reserved_highatomic limit under the lock */
2077 if (zone->nr_reserved_highatomic >= max_managed)
2078 goto out_unlock;
2079
2080 /* Yoink! */
2081 mt = get_pageblock_migratetype(page);
2082 if (mt != MIGRATE_HIGHATOMIC &&
2083 !is_migrate_isolate(mt) && !is_migrate_cma(mt)) {
2084 zone->nr_reserved_highatomic += pageblock_nr_pages;
2085 set_pageblock_migratetype(page, MIGRATE_HIGHATOMIC);
2086 move_freepages_block(zone, page, MIGRATE_HIGHATOMIC);
2087 }
2088
2089out_unlock:
2090 spin_unlock_irqrestore(&zone->lock, flags);
2091}
2092
2093/*
2094 * Used when an allocation is about to fail under memory pressure. This
2095 * potentially hurts the reliability of high-order allocations when under
2096 * intense memory pressure but failed atomic allocations should be easier
2097 * to recover from than an OOM.
2098 */
2099static void unreserve_highatomic_pageblock(const struct alloc_context *ac)
2100{
2101 struct zonelist *zonelist = ac->zonelist;
2102 unsigned long flags;
2103 struct zoneref *z;
2104 struct zone *zone;
2105 struct page *page;
2106 int order;
2107
2108 for_each_zone_zonelist_nodemask(zone, z, zonelist, ac->high_zoneidx,
2109 ac->nodemask) {
2110 /* Preserve at least one pageblock */
2111 if (zone->nr_reserved_highatomic <= pageblock_nr_pages)
2112 continue;
2113
2114 spin_lock_irqsave(&zone->lock, flags);
2115 for (order = 0; order < MAX_ORDER; order++) {
2116 struct free_area *area = &(zone->free_area[order]);
2117
Geliang Tanga16601c2016-01-14 15:20:30 -08002118 page = list_first_entry_or_null(
2119 &area->free_list[MIGRATE_HIGHATOMIC],
2120 struct page, lru);
2121 if (!page)
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002122 continue;
2123
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002124 /*
Minchan Kim39f56772016-12-12 16:42:08 -08002125 * In page freeing path, migratetype change is racy so
2126 * we can counter several free pages in a pageblock
2127 * in this loop althoug we changed the pageblock type
2128 * from highatomic to ac->migratetype. So we should
2129 * adjust the count once.
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002130 */
Minchan Kim39f56772016-12-12 16:42:08 -08002131 if (get_pageblock_migratetype(page) ==
2132 MIGRATE_HIGHATOMIC) {
2133 /*
2134 * It should never happen but changes to
2135 * locking could inadvertently allow a per-cpu
2136 * drain to add pages to MIGRATE_HIGHATOMIC
2137 * while unreserving so be safe and watch for
2138 * underflows.
2139 */
2140 zone->nr_reserved_highatomic -= min(
2141 pageblock_nr_pages,
2142 zone->nr_reserved_highatomic);
2143 }
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002144
2145 /*
2146 * Convert to ac->migratetype and avoid the normal
2147 * pageblock stealing heuristics. Minimally, the caller
2148 * is doing the work and needs the pages. More
2149 * importantly, if the block was always converted to
2150 * MIGRATE_UNMOVABLE or another type then the number
2151 * of pageblocks that cannot be completely freed
2152 * may increase.
2153 */
2154 set_pageblock_migratetype(page, ac->migratetype);
2155 move_freepages_block(zone, page, ac->migratetype);
2156 spin_unlock_irqrestore(&zone->lock, flags);
2157 return;
2158 }
2159 spin_unlock_irqrestore(&zone->lock, flags);
2160 }
2161}
2162
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002163/* Remove an element from the buddy allocator from the fallback list */
Mel Gorman0ac3a402009-06-16 15:32:06 -07002164static inline struct page *
Mel Gorman7aeb09f2014-06-04 16:10:21 -07002165__rmqueue_fallback(struct zone *zone, unsigned int order, int start_migratetype)
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002166{
Pintu Kumarb8af2942013-09-11 14:20:34 -07002167 struct free_area *area;
Mel Gorman7aeb09f2014-06-04 16:10:21 -07002168 unsigned int current_order;
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002169 struct page *page;
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002170 int fallback_mt;
2171 bool can_steal;
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002172
2173 /* Find the largest possible block of pages in the other list */
Mel Gorman7aeb09f2014-06-04 16:10:21 -07002174 for (current_order = MAX_ORDER-1;
2175 current_order >= order && current_order <= MAX_ORDER-1;
2176 --current_order) {
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002177 area = &(zone->free_area[current_order]);
2178 fallback_mt = find_suitable_fallback(area, current_order,
Joonsoo Kim2149cda2015-04-14 15:45:21 -07002179 start_migratetype, false, &can_steal);
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002180 if (fallback_mt == -1)
2181 continue;
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002182
Geliang Tanga16601c2016-01-14 15:20:30 -08002183 page = list_first_entry(&area->free_list[fallback_mt],
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002184 struct page, lru);
2185 if (can_steal)
2186 steal_suitable_fallback(zone, page, start_migratetype);
Mel Gormane0104872007-10-16 01:25:53 -07002187
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002188 /* Remove the page from the freelists */
2189 area->nr_free--;
2190 list_del(&page->lru);
2191 rmv_page_order(page);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002192
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002193 expand(zone, page, order, current_order, area,
2194 start_migratetype);
2195 /*
Vlastimil Babkabb14c2c2015-09-08 15:01:25 -07002196 * The pcppage_migratetype may differ from pageblock's
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002197 * migratetype depending on the decisions in
Vlastimil Babkabb14c2c2015-09-08 15:01:25 -07002198 * find_suitable_fallback(). This is OK as long as it does not
2199 * differ for MIGRATE_CMA pageblocks. Those can be used as
2200 * fallback only via special __rmqueue_cma_fallback() function
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002201 */
Vlastimil Babkabb14c2c2015-09-08 15:01:25 -07002202 set_pcppage_migratetype(page, start_migratetype);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002203
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002204 trace_mm_page_alloc_extfrag(page, order, current_order,
2205 start_migratetype, fallback_mt);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002206
Joonsoo Kim4eb7dce2015-04-14 15:45:18 -07002207 return page;
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002208 }
2209
Mel Gorman728ec982009-06-16 15:32:04 -07002210 return NULL;
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002211}
2212
Mel Gorman56fd56b2007-10-16 01:25:58 -07002213/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 * Do the hard work of removing an element from the buddy allocator.
2215 * Call me with the zone->lock already held.
2216 */
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002217static struct page *__rmqueue(struct zone *zone, unsigned int order,
Mel Gorman6ac02062016-01-14 15:20:28 -08002218 int migratetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 struct page *page;
2221
Mel Gorman56fd56b2007-10-16 01:25:58 -07002222 page = __rmqueue_smallest(zone, order, migratetype);
Mel Gorman974a7862015-11-06 16:28:34 -08002223 if (unlikely(!page)) {
Joonsoo Kimdc676472015-04-14 15:45:15 -07002224 if (migratetype == MIGRATE_MOVABLE)
2225 page = __rmqueue_cma_fallback(zone, order);
2226
2227 if (!page)
2228 page = __rmqueue_fallback(zone, order, migratetype);
Mel Gorman728ec982009-06-16 15:32:04 -07002229 }
2230
Mel Gorman0d3d0622009-09-21 17:02:44 -07002231 trace_mm_page_alloc_zone_locked(page, order, migratetype);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002232 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233}
2234
Michal Nazarewicz5f63b722012-01-11 15:16:11 +01002235/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 * Obtain a specified number of elements from the buddy allocator, all under
2237 * a single hold of the lock, for efficiency. Add them to the supplied list.
2238 * Returns the number of new pages which were placed at *list.
2239 */
Michal Nazarewicz5f63b722012-01-11 15:16:11 +01002240static int rmqueue_bulk(struct zone *zone, unsigned int order,
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002241 unsigned long count, struct list_head *list,
Mel Gormanb745bc82014-06-04 16:10:22 -07002242 int migratetype, bool cold)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243{
Mel Gorman44919a22016-12-12 16:44:41 -08002244 int i, alloced = 0;
Michal Nazarewicz5f63b722012-01-11 15:16:11 +01002245
Nick Pigginc54ad302006-01-06 00:10:56 -08002246 spin_lock(&zone->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 for (i = 0; i < count; ++i) {
Mel Gorman6ac02062016-01-14 15:20:28 -08002248 struct page *page = __rmqueue(zone, order, migratetype);
Nick Piggin085cc7d2006-01-06 00:11:01 -08002249 if (unlikely(page == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 break;
Mel Gorman81eabcb2007-12-17 16:20:05 -08002251
Mel Gorman479f8542016-05-19 17:14:35 -07002252 if (unlikely(check_pcp_refill(page)))
2253 continue;
2254
Mel Gorman81eabcb2007-12-17 16:20:05 -08002255 /*
2256 * Split buddy pages returned by expand() are received here
2257 * in physical page order. The page is added to the callers and
2258 * list and the list head then moves forward. From the callers
2259 * perspective, the linked list is ordered by page number in
2260 * some conditions. This is useful for IO devices that can
2261 * merge IO requests if the physical pages are ordered
2262 * properly.
2263 */
Mel Gormanb745bc82014-06-04 16:10:22 -07002264 if (likely(!cold))
Mel Gormane084b2d2009-07-29 15:02:04 -07002265 list_add(&page->lru, list);
2266 else
2267 list_add_tail(&page->lru, list);
Mel Gorman81eabcb2007-12-17 16:20:05 -08002268 list = &page->lru;
Mel Gorman44919a22016-12-12 16:44:41 -08002269 alloced++;
Vlastimil Babkabb14c2c2015-09-08 15:01:25 -07002270 if (is_migrate_cma(get_pcppage_migratetype(page)))
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -07002271 __mod_zone_page_state(zone, NR_FREE_CMA_PAGES,
2272 -(1 << order));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 }
Mel Gorman44919a22016-12-12 16:44:41 -08002274
2275 /*
2276 * i pages were removed from the buddy list even if some leak due
2277 * to check_pcp_refill failing so adjust NR_FREE_PAGES based
2278 * on i. Do not confuse with 'alloced' which is the number of
2279 * pages added to the pcp list.
2280 */
Mel Gormanf2260e62009-06-16 15:32:13 -07002281 __mod_zone_page_state(zone, NR_FREE_PAGES, -(i << order));
Nick Pigginc54ad302006-01-06 00:10:56 -08002282 spin_unlock(&zone->lock);
Mel Gorman44919a22016-12-12 16:44:41 -08002283 return alloced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284}
2285
Christoph Lameter4ae7c032005-06-21 17:14:57 -07002286#ifdef CONFIG_NUMA
Christoph Lameter8fce4d82006-03-09 17:33:54 -08002287/*
Christoph Lameter4037d452007-05-09 02:35:14 -07002288 * Called from the vmstat counter updater to drain pagesets of this
2289 * currently executing processor on remote nodes after they have
2290 * expired.
2291 *
Christoph Lameter879336c2006-03-22 00:09:08 -08002292 * Note that this function must be called with the thread pinned to
2293 * a single processor.
Christoph Lameter8fce4d82006-03-09 17:33:54 -08002294 */
Christoph Lameter4037d452007-05-09 02:35:14 -07002295void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
Christoph Lameter4ae7c032005-06-21 17:14:57 -07002296{
Christoph Lameter4ae7c032005-06-21 17:14:57 -07002297 unsigned long flags;
Michal Nazarewicz7be12fc2014-08-06 16:05:15 -07002298 int to_drain, batch;
Christoph Lameter4ae7c032005-06-21 17:14:57 -07002299
Christoph Lameter4037d452007-05-09 02:35:14 -07002300 local_irq_save(flags);
Jason Low4db0c3c2015-04-15 16:14:08 -07002301 batch = READ_ONCE(pcp->batch);
Michal Nazarewicz7be12fc2014-08-06 16:05:15 -07002302 to_drain = min(pcp->count, batch);
KOSAKI Motohiro2a135152012-07-31 16:42:53 -07002303 if (to_drain > 0) {
2304 free_pcppages_bulk(zone, to_drain, pcp);
2305 pcp->count -= to_drain;
2306 }
Christoph Lameter4037d452007-05-09 02:35:14 -07002307 local_irq_restore(flags);
Christoph Lameter4ae7c032005-06-21 17:14:57 -07002308}
2309#endif
2310
Christoph Lameter9f8f2172008-02-04 22:29:11 -08002311/*
Vlastimil Babka93481ff2014-12-10 15:43:01 -08002312 * Drain pcplists of the indicated processor and zone.
2313 *
2314 * The processor must either be the current processor and the
2315 * thread pinned to the current processor or a processor that
2316 * is not online.
2317 */
2318static void drain_pages_zone(unsigned int cpu, struct zone *zone)
2319{
2320 unsigned long flags;
2321 struct per_cpu_pageset *pset;
2322 struct per_cpu_pages *pcp;
2323
2324 local_irq_save(flags);
2325 pset = per_cpu_ptr(zone->pageset, cpu);
2326
2327 pcp = &pset->pcp;
2328 if (pcp->count) {
2329 free_pcppages_bulk(zone, pcp->count, pcp);
2330 pcp->count = 0;
2331 }
2332 local_irq_restore(flags);
2333}
2334
2335/*
2336 * Drain pcplists of all zones on the indicated processor.
Christoph Lameter9f8f2172008-02-04 22:29:11 -08002337 *
2338 * The processor must either be the current processor and the
2339 * thread pinned to the current processor or a processor that
2340 * is not online.
2341 */
2342static void drain_pages(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343{
2344 struct zone *zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
KOSAKI Motohiroee99c712009-03-31 15:19:31 -07002346 for_each_populated_zone(zone) {
Vlastimil Babka93481ff2014-12-10 15:43:01 -08002347 drain_pages_zone(cpu, zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 }
2349}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
Christoph Lameter9f8f2172008-02-04 22:29:11 -08002351/*
2352 * Spill all of this CPU's per-cpu pages back into the buddy allocator.
Vlastimil Babka93481ff2014-12-10 15:43:01 -08002353 *
2354 * The CPU has to be pinned. When zone parameter is non-NULL, spill just
2355 * the single zone's pages.
Christoph Lameter9f8f2172008-02-04 22:29:11 -08002356 */
Sami Tolvanen97d5fd22017-08-16 10:45:46 -07002357void drain_local_pages(void *z)
Christoph Lameter9f8f2172008-02-04 22:29:11 -08002358{
Sami Tolvanen97d5fd22017-08-16 10:45:46 -07002359 struct zone *zone = (struct zone *)z;
Vlastimil Babka93481ff2014-12-10 15:43:01 -08002360 int cpu = smp_processor_id();
2361
2362 if (zone)
2363 drain_pages_zone(cpu, zone);
2364 else
2365 drain_pages(cpu);
Christoph Lameter9f8f2172008-02-04 22:29:11 -08002366}
2367
2368/*
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07002369 * Spill all the per-cpu pages from all CPUs back into the buddy allocator.
2370 *
Vlastimil Babka93481ff2014-12-10 15:43:01 -08002371 * When zone parameter is non-NULL, spill just the single zone's pages.
2372 *
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07002373 * Note that this code is protected against sending an IPI to an offline
2374 * CPU but does not guarantee sending an IPI to newly hotplugged CPUs:
2375 * on_each_cpu_mask() blocks hotplug and won't talk to offlined CPUs but
2376 * nothing keeps CPUs from showing up after we populated the cpumask and
2377 * before the call to on_each_cpu_mask().
Christoph Lameter9f8f2172008-02-04 22:29:11 -08002378 */
Vlastimil Babka93481ff2014-12-10 15:43:01 -08002379void drain_all_pages(struct zone *zone)
Christoph Lameter9f8f2172008-02-04 22:29:11 -08002380{
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07002381 int cpu;
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07002382
2383 /*
2384 * Allocate in the BSS so we wont require allocation in
2385 * direct reclaim path for CONFIG_CPUMASK_OFFSTACK=y
2386 */
2387 static cpumask_t cpus_with_pcps;
2388
2389 /*
2390 * We don't care about racing with CPU hotplug event
2391 * as offline notification will cause the notified
2392 * cpu to drain that CPU pcps and on_each_cpu_mask
2393 * disables preemption as part of its processing
2394 */
2395 for_each_online_cpu(cpu) {
Vlastimil Babka93481ff2014-12-10 15:43:01 -08002396 struct per_cpu_pageset *pcp;
2397 struct zone *z;
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07002398 bool has_pcps = false;
Vlastimil Babka93481ff2014-12-10 15:43:01 -08002399
2400 if (zone) {
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07002401 pcp = per_cpu_ptr(zone->pageset, cpu);
Vlastimil Babka93481ff2014-12-10 15:43:01 -08002402 if (pcp->pcp.count)
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07002403 has_pcps = true;
Vlastimil Babka93481ff2014-12-10 15:43:01 -08002404 } else {
2405 for_each_populated_zone(z) {
2406 pcp = per_cpu_ptr(z->pageset, cpu);
2407 if (pcp->pcp.count) {
2408 has_pcps = true;
2409 break;
2410 }
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07002411 }
2412 }
Vlastimil Babka93481ff2014-12-10 15:43:01 -08002413
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07002414 if (has_pcps)
2415 cpumask_set_cpu(cpu, &cpus_with_pcps);
2416 else
2417 cpumask_clear_cpu(cpu, &cpus_with_pcps);
2418 }
Sami Tolvanen97d5fd22017-08-16 10:45:46 -07002419 on_each_cpu_mask(&cpus_with_pcps, drain_local_pages, zone, 1);
Christoph Lameter9f8f2172008-02-04 22:29:11 -08002420}
2421
Rafael J. Wysocki296699d2007-07-29 23:27:18 +02002422#ifdef CONFIG_HIBERNATION
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
2424void mark_free_pages(struct zone *zone)
2425{
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07002426 unsigned long pfn, max_zone_pfn;
2427 unsigned long flags;
Mel Gorman7aeb09f2014-06-04 16:10:21 -07002428 unsigned int order, t;
Geliang Tang86760a22016-01-14 15:20:33 -08002429 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Xishi Qiu8080fc02013-09-11 14:21:45 -07002431 if (zone_is_empty(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 return;
2433
2434 spin_lock_irqsave(&zone->lock, flags);
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07002435
Cody P Schafer108bcc92013-02-22 16:35:23 -08002436 max_zone_pfn = zone_end_pfn(zone);
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07002437 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
2438 if (pfn_valid(pfn)) {
Geliang Tang86760a22016-01-14 15:20:33 -08002439 page = pfn_to_page(pfn);
Joonsoo Kimba6b0972016-05-19 17:12:16 -07002440
2441 if (page_zone(page) != zone)
2442 continue;
2443
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07002444 if (!swsusp_page_is_forbidden(page))
2445 swsusp_unset_page_free(page);
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07002446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002448 for_each_migratetype_order(order, t) {
Geliang Tang86760a22016-01-14 15:20:33 -08002449 list_for_each_entry(page,
2450 &zone->free_area[order].free_list[t], lru) {
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07002451 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
Geliang Tang86760a22016-01-14 15:20:33 -08002453 pfn = page_to_pfn(page);
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07002454 for (i = 0; i < (1UL << order); i++)
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07002455 swsusp_set_page_free(pfn_to_page(pfn + i));
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07002456 }
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 spin_unlock_irqrestore(&zone->lock, flags);
2459}
Mel Gormane2c55dc2007-10-16 01:25:50 -07002460#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
2462/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 * Free a 0-order page
Mel Gormanb745bc82014-06-04 16:10:22 -07002464 * cold == true ? free a cold page : free a hot page
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 */
Mel Gormanb745bc82014-06-04 16:10:22 -07002466void free_hot_cold_page(struct page *page, bool cold)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467{
2468 struct zone *zone = page_zone(page);
2469 struct per_cpu_pages *pcp;
2470 unsigned long flags;
Mel Gormandc4b0ca2014-06-04 16:10:17 -07002471 unsigned long pfn = page_to_pfn(page);
Mel Gorman5f8dcc22009-09-21 17:03:19 -07002472 int migratetype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Mel Gorman4db75482016-05-19 17:14:32 -07002474 if (!free_pcp_prepare(page))
Hugh Dickins689bceb2005-11-21 21:32:20 -08002475 return;
2476
Mel Gormandc4b0ca2014-06-04 16:10:17 -07002477 migratetype = get_pfnblock_migratetype(page, pfn);
Vlastimil Babkabb14c2c2015-09-08 15:01:25 -07002478 set_pcppage_migratetype(page, migratetype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 local_irq_save(flags);
Christoph Lameterf8891e52006-06-30 01:55:45 -07002480 __count_vm_event(PGFREE);
Mel Gormanda456f12009-06-16 15:32:08 -07002481
Mel Gorman5f8dcc22009-09-21 17:03:19 -07002482 /*
2483 * We only track unmovable, reclaimable and movable on pcp lists.
2484 * Free ISOLATE pages back to the allocator because they are being
2485 * offlined but treat RESERVE as movable pages so we can get those
2486 * areas back if necessary. Otherwise, we may have to free
2487 * excessively into the page allocator
2488 */
2489 if (migratetype >= MIGRATE_PCPTYPES) {
Minchan Kim194159f2013-02-22 16:33:58 -08002490 if (unlikely(is_migrate_isolate(migratetype))) {
Mel Gormandc4b0ca2014-06-04 16:10:17 -07002491 free_one_page(zone, page, pfn, 0, migratetype);
Mel Gorman5f8dcc22009-09-21 17:03:19 -07002492 goto out;
2493 }
2494 migratetype = MIGRATE_MOVABLE;
2495 }
2496
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09002497 pcp = &this_cpu_ptr(zone->pageset)->pcp;
Mel Gormanb745bc82014-06-04 16:10:22 -07002498 if (!cold)
Mel Gorman5f8dcc22009-09-21 17:03:19 -07002499 list_add(&page->lru, &pcp->lists[migratetype]);
Mel Gormanb745bc82014-06-04 16:10:22 -07002500 else
2501 list_add_tail(&page->lru, &pcp->lists[migratetype]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 pcp->count++;
Nick Piggin48db57f2006-01-08 01:00:42 -08002503 if (pcp->count >= pcp->high) {
Jason Low4db0c3c2015-04-15 16:14:08 -07002504 unsigned long batch = READ_ONCE(pcp->batch);
Cody P Schafer998d39c2013-07-03 15:01:32 -07002505 free_pcppages_bulk(zone, batch, pcp);
2506 pcp->count -= batch;
Nick Piggin48db57f2006-01-08 01:00:42 -08002507 }
Mel Gorman5f8dcc22009-09-21 17:03:19 -07002508
2509out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511}
2512
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08002513/*
Konstantin Khlebnikovcc598502012-01-10 15:07:04 -08002514 * Free a list of 0-order pages
2515 */
Mel Gormanb745bc82014-06-04 16:10:22 -07002516void free_hot_cold_page_list(struct list_head *list, bool cold)
Konstantin Khlebnikovcc598502012-01-10 15:07:04 -08002517{
2518 struct page *page, *next;
2519
2520 list_for_each_entry_safe(page, next, list, lru) {
Konstantin Khlebnikovb413d482012-01-10 15:07:09 -08002521 trace_mm_page_free_batched(page, cold);
Konstantin Khlebnikovcc598502012-01-10 15:07:04 -08002522 free_hot_cold_page(page, cold);
2523 }
2524}
2525
2526/*
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08002527 * split_page takes a non-compound higher-order page, and splits it into
2528 * n (1<<order) sub-pages: page[0..n]
2529 * Each sub-page must be freed individually.
2530 *
2531 * Note: this is probably too low level an operation for use in drivers.
2532 * Please consult with lkml before using this in your driver.
2533 */
2534void split_page(struct page *page, unsigned int order)
2535{
2536 int i;
2537
Sasha Levin309381fea2014-01-23 15:52:54 -08002538 VM_BUG_ON_PAGE(PageCompound(page), page);
2539 VM_BUG_ON_PAGE(!page_count(page), page);
Vegard Nossumb1eeab62008-11-25 16:55:53 +01002540
2541#ifdef CONFIG_KMEMCHECK
2542 /*
2543 * Split shadow pages too, because free(page[0]) would
2544 * otherwise free the whole shadow.
2545 */
2546 if (kmemcheck_page_is_tracked(page))
2547 split_page(virt_to_page(page[0].shadow), order);
2548#endif
2549
Joonsoo Kima9627bc2016-07-26 15:23:49 -07002550 for (i = 1; i < (1 << order); i++)
Nick Piggin7835e982006-03-22 00:08:40 -08002551 set_page_refcounted(page + i);
Joonsoo Kima9627bc2016-07-26 15:23:49 -07002552 split_page_owner(page, order);
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08002553}
K. Y. Srinivasan5853ff22013-03-25 15:47:38 -07002554EXPORT_SYMBOL_GPL(split_page);
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08002555
Joonsoo Kim3c605092014-11-13 15:19:21 -08002556int __isolate_free_page(struct page *page, unsigned int order)
Mel Gorman748446b2010-05-24 14:32:27 -07002557{
Mel Gorman748446b2010-05-24 14:32:27 -07002558 unsigned long watermark;
2559 struct zone *zone;
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -07002560 int mt;
Mel Gorman748446b2010-05-24 14:32:27 -07002561
2562 BUG_ON(!PageBuddy(page));
2563
2564 zone = page_zone(page);
Marek Szyprowski2e30abd2012-12-11 16:02:57 -08002565 mt = get_pageblock_migratetype(page);
Mel Gorman748446b2010-05-24 14:32:27 -07002566
Minchan Kim194159f2013-02-22 16:33:58 -08002567 if (!is_migrate_isolate(mt)) {
Vlastimil Babka8348faf2016-10-07 16:58:00 -07002568 /*
2569 * Obey watermarks as if the page was being allocated. We can
2570 * emulate a high-order watermark check with a raised order-0
2571 * watermark, because we already know our high-order page
2572 * exists.
2573 */
2574 watermark = min_wmark_pages(zone) + (1UL << order);
Vlastimil Babka984fdba2016-10-07 16:57:57 -07002575 if (!zone_watermark_ok(zone, 0, watermark, 0, ALLOC_CMA))
Marek Szyprowski2e30abd2012-12-11 16:02:57 -08002576 return 0;
2577
Mel Gorman8fb74b92013-01-11 14:32:16 -08002578 __mod_zone_freepage_state(zone, -(1UL << order), mt);
Marek Szyprowski2e30abd2012-12-11 16:02:57 -08002579 }
Mel Gorman748446b2010-05-24 14:32:27 -07002580
2581 /* Remove page from free list */
2582 list_del(&page->lru);
2583 zone->free_area[order].nr_free--;
2584 rmv_page_order(page);
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -07002585
zhong jiang400bc7f2016-07-28 15:45:07 -07002586 /*
2587 * Set the pageblock if the isolated page is at least half of a
2588 * pageblock
2589 */
Mel Gorman748446b2010-05-24 14:32:27 -07002590 if (order >= pageblock_order - 1) {
2591 struct page *endpage = page + (1 << order) - 1;
Michal Nazarewicz47118af2011-12-29 13:09:50 +01002592 for (; page < endpage; page += pageblock_nr_pages) {
2593 int mt = get_pageblock_migratetype(page);
Minchan Kim194159f2013-02-22 16:33:58 -08002594 if (!is_migrate_isolate(mt) && !is_migrate_cma(mt))
Michal Nazarewicz47118af2011-12-29 13:09:50 +01002595 set_pageblock_migratetype(page,
2596 MIGRATE_MOVABLE);
2597 }
Mel Gorman748446b2010-05-24 14:32:27 -07002598 }
2599
Joonsoo Kimf3a14ce2015-07-17 16:24:15 -07002600
Mel Gorman8fb74b92013-01-11 14:32:16 -08002601 return 1UL << order;
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07002602}
2603
2604/*
Mel Gorman060e7412016-05-19 17:13:27 -07002605 * Update NUMA hit/miss statistics
2606 *
2607 * Must be called with interrupts disabled.
Mel Gorman060e7412016-05-19 17:13:27 -07002608 */
2609static inline void zone_statistics(struct zone *preferred_zone, struct zone *z,
2610 gfp_t flags)
2611{
2612#ifdef CONFIG_NUMA
Mel Gorman060e7412016-05-19 17:13:27 -07002613 enum zone_stat_item local_stat = NUMA_LOCAL;
2614
Michal Hockofae478c2017-01-10 16:57:39 -08002615 if (z->node != numa_node_id())
Mel Gorman060e7412016-05-19 17:13:27 -07002616 local_stat = NUMA_OTHER;
Mel Gorman060e7412016-05-19 17:13:27 -07002617
Michal Hockofae478c2017-01-10 16:57:39 -08002618 if (z->node == preferred_zone->node)
Mel Gorman060e7412016-05-19 17:13:27 -07002619 __inc_zone_state(z, NUMA_HIT);
Michal Hockofae478c2017-01-10 16:57:39 -08002620 else {
Mel Gorman060e7412016-05-19 17:13:27 -07002621 __inc_zone_state(z, NUMA_MISS);
2622 __inc_zone_state(preferred_zone, NUMA_FOREIGN);
2623 }
Michal Hockofae478c2017-01-10 16:57:39 -08002624 __inc_zone_state(z, local_stat);
Mel Gorman060e7412016-05-19 17:13:27 -07002625#endif
2626}
2627
2628/*
Vlastimil Babka75379192015-02-11 15:25:38 -08002629 * Allocate a page from the given zone. Use pcplists for order-0 allocations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 */
Mel Gorman0a15c3e2009-06-16 15:32:05 -07002631static inline
2632struct page *buffered_rmqueue(struct zone *preferred_zone,
Mel Gorman7aeb09f2014-06-04 16:10:21 -07002633 struct zone *zone, unsigned int order,
Mel Gormanc6038442016-05-19 17:13:38 -07002634 gfp_t gfp_flags, unsigned int alloc_flags,
2635 int migratetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636{
2637 unsigned long flags;
Hugh Dickins689bceb2005-11-21 21:32:20 -08002638 struct page *page;
Mel Gormanb745bc82014-06-04 16:10:22 -07002639 bool cold = ((gfp_flags & __GFP_COLD) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
Nick Piggin48db57f2006-01-08 01:00:42 -08002641 if (likely(order == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 struct per_cpu_pages *pcp;
Mel Gorman5f8dcc22009-09-21 17:03:19 -07002643 struct list_head *list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 local_irq_save(flags);
Mel Gorman479f8542016-05-19 17:14:35 -07002646 do {
2647 pcp = &this_cpu_ptr(zone->pageset)->pcp;
2648 list = &pcp->lists[migratetype];
2649 if (list_empty(list)) {
2650 pcp->count += rmqueue_bulk(zone, 0,
2651 pcp->batch, list,
2652 migratetype, cold);
2653 if (unlikely(list_empty(list)))
2654 goto failed;
2655 }
Mel Gormanb92a6ed2007-10-16 01:25:50 -07002656
Mel Gorman479f8542016-05-19 17:14:35 -07002657 if (cold)
2658 page = list_last_entry(list, struct page, lru);
2659 else
2660 page = list_first_entry(list, struct page, lru);
Mel Gorman5f8dcc22009-09-21 17:03:19 -07002661
Vlastimil Babka83b93552016-06-03 14:55:52 -07002662 list_del(&page->lru);
2663 pcp->count--;
2664
2665 } while (check_new_pcp(page));
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002666 } else {
Michal Hocko0f352e52016-03-17 14:19:32 -07002667 /*
2668 * We most definitely don't want callers attempting to
2669 * allocate greater than order-1 page units with __GFP_NOFAIL.
2670 */
2671 WARN_ON_ONCE((gfp_flags & __GFP_NOFAIL) && (order > 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 spin_lock_irqsave(&zone->lock, flags);
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002673
Mel Gorman479f8542016-05-19 17:14:35 -07002674 do {
2675 page = NULL;
2676 if (alloc_flags & ALLOC_HARDER) {
2677 page = __rmqueue_smallest(zone, order, MIGRATE_HIGHATOMIC);
2678 if (page)
2679 trace_mm_page_alloc_zone_locked(page, order, migratetype);
2680 }
2681 if (!page)
2682 page = __rmqueue(zone, order, migratetype);
2683 } while (page && check_new_pages(page, order));
Nick Piggina74609f2006-01-06 00:11:20 -08002684 spin_unlock(&zone->lock);
2685 if (!page)
2686 goto failed;
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -07002687 __mod_zone_freepage_state(zone, -(1 << order),
Vlastimil Babkabb14c2c2015-09-08 15:01:25 -07002688 get_pcppage_migratetype(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 }
2690
Mel Gorman16709d12016-07-28 15:46:56 -07002691 __count_zid_vm_events(PGALLOC, page_zonenum(page), 1 << order);
Andi Kleen78afd562011-03-22 16:33:12 -07002692 zone_statistics(preferred_zone, zone, gfp_flags);
Nick Piggina74609f2006-01-06 00:11:20 -08002693 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
Sasha Levin309381fea2014-01-23 15:52:54 -08002695 VM_BUG_ON_PAGE(bad_range(zone, page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 return page;
Nick Piggina74609f2006-01-06 00:11:20 -08002697
2698failed:
2699 local_irq_restore(flags);
Nick Piggina74609f2006-01-06 00:11:20 -08002700 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701}
2702
Akinobu Mita933e3122006-12-08 02:39:45 -08002703#ifdef CONFIG_FAIL_PAGE_ALLOC
2704
Akinobu Mitab2588c42011-07-26 16:09:03 -07002705static struct {
Akinobu Mita933e3122006-12-08 02:39:45 -08002706 struct fault_attr attr;
2707
Viresh Kumar621a5f72015-09-26 15:04:07 -07002708 bool ignore_gfp_highmem;
Mel Gorman71baba42015-11-06 16:28:28 -08002709 bool ignore_gfp_reclaim;
Akinobu Mita54114992007-07-15 23:40:23 -07002710 u32 min_order;
Akinobu Mita933e3122006-12-08 02:39:45 -08002711} fail_page_alloc = {
2712 .attr = FAULT_ATTR_INITIALIZER,
Mel Gorman71baba42015-11-06 16:28:28 -08002713 .ignore_gfp_reclaim = true,
Viresh Kumar621a5f72015-09-26 15:04:07 -07002714 .ignore_gfp_highmem = true,
Akinobu Mita54114992007-07-15 23:40:23 -07002715 .min_order = 1,
Akinobu Mita933e3122006-12-08 02:39:45 -08002716};
2717
2718static int __init setup_fail_page_alloc(char *str)
2719{
2720 return setup_fault_attr(&fail_page_alloc.attr, str);
2721}
2722__setup("fail_page_alloc=", setup_fail_page_alloc);
2723
Gavin Shandeaf3862012-07-31 16:41:51 -07002724static bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
Akinobu Mita933e3122006-12-08 02:39:45 -08002725{
Akinobu Mita54114992007-07-15 23:40:23 -07002726 if (order < fail_page_alloc.min_order)
Gavin Shandeaf3862012-07-31 16:41:51 -07002727 return false;
Akinobu Mita933e3122006-12-08 02:39:45 -08002728 if (gfp_mask & __GFP_NOFAIL)
Gavin Shandeaf3862012-07-31 16:41:51 -07002729 return false;
Akinobu Mita933e3122006-12-08 02:39:45 -08002730 if (fail_page_alloc.ignore_gfp_highmem && (gfp_mask & __GFP_HIGHMEM))
Gavin Shandeaf3862012-07-31 16:41:51 -07002731 return false;
Mel Gorman71baba42015-11-06 16:28:28 -08002732 if (fail_page_alloc.ignore_gfp_reclaim &&
2733 (gfp_mask & __GFP_DIRECT_RECLAIM))
Gavin Shandeaf3862012-07-31 16:41:51 -07002734 return false;
Akinobu Mita933e3122006-12-08 02:39:45 -08002735
2736 return should_fail(&fail_page_alloc.attr, 1 << order);
2737}
2738
2739#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
2740
2741static int __init fail_page_alloc_debugfs(void)
2742{
Al Virof4ae40a2011-07-24 04:33:43 -04002743 umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
Akinobu Mita933e3122006-12-08 02:39:45 -08002744 struct dentry *dir;
Akinobu Mita933e3122006-12-08 02:39:45 -08002745
Akinobu Mitadd48c082011-08-03 16:21:01 -07002746 dir = fault_create_debugfs_attr("fail_page_alloc", NULL,
2747 &fail_page_alloc.attr);
2748 if (IS_ERR(dir))
2749 return PTR_ERR(dir);
Akinobu Mita933e3122006-12-08 02:39:45 -08002750
Akinobu Mitab2588c42011-07-26 16:09:03 -07002751 if (!debugfs_create_bool("ignore-gfp-wait", mode, dir,
Mel Gorman71baba42015-11-06 16:28:28 -08002752 &fail_page_alloc.ignore_gfp_reclaim))
Akinobu Mitab2588c42011-07-26 16:09:03 -07002753 goto fail;
2754 if (!debugfs_create_bool("ignore-gfp-highmem", mode, dir,
2755 &fail_page_alloc.ignore_gfp_highmem))
2756 goto fail;
2757 if (!debugfs_create_u32("min-order", mode, dir,
2758 &fail_page_alloc.min_order))
2759 goto fail;
Akinobu Mita933e3122006-12-08 02:39:45 -08002760
Akinobu Mitab2588c42011-07-26 16:09:03 -07002761 return 0;
2762fail:
Akinobu Mitadd48c082011-08-03 16:21:01 -07002763 debugfs_remove_recursive(dir);
Akinobu Mita933e3122006-12-08 02:39:45 -08002764
Akinobu Mitab2588c42011-07-26 16:09:03 -07002765 return -ENOMEM;
Akinobu Mita933e3122006-12-08 02:39:45 -08002766}
2767
2768late_initcall(fail_page_alloc_debugfs);
2769
2770#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
2771
2772#else /* CONFIG_FAIL_PAGE_ALLOC */
2773
Gavin Shandeaf3862012-07-31 16:41:51 -07002774static inline bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
Akinobu Mita933e3122006-12-08 02:39:45 -08002775{
Gavin Shandeaf3862012-07-31 16:41:51 -07002776 return false;
Akinobu Mita933e3122006-12-08 02:39:45 -08002777}
2778
2779#endif /* CONFIG_FAIL_PAGE_ALLOC */
2780
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781/*
Mel Gorman97a16fc2015-11-06 16:28:40 -08002782 * Return true if free base pages are above 'mark'. For high-order checks it
2783 * will return true of the order-0 watermark is reached and there is at least
2784 * one free page of a suitable size. Checking now avoids taking the zone lock
2785 * to check in the allocation paths if no pages are free.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 */
Michal Hocko86a294a2016-05-20 16:57:12 -07002787bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
2788 int classzone_idx, unsigned int alloc_flags,
2789 long free_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790{
Christoph Lameterd23ad422007-02-10 01:43:02 -08002791 long min = mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 int o;
Mel Gormanc6038442016-05-19 17:13:38 -07002793 const bool alloc_harder = (alloc_flags & ALLOC_HARDER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002795 /* free_pages may go negative - that's OK */
Michal Hockodf0a6da2012-01-10 15:08:02 -08002796 free_pages -= (1 << order) - 1;
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002797
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002798 if (alloc_flags & ALLOC_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 min -= min / 2;
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002800
2801 /*
2802 * If the caller does not have rights to ALLOC_HARDER then subtract
2803 * the high-atomic reserves. This will over-estimate the size of the
2804 * atomic reserve but it avoids a search.
2805 */
Mel Gorman97a16fc2015-11-06 16:28:40 -08002806 if (likely(!alloc_harder))
Mel Gorman0aaa29a2015-11-06 16:28:37 -08002807 free_pages -= z->nr_reserved_highatomic;
2808 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 min -= min / 4;
Mel Gormane2b19192015-11-06 16:28:09 -08002810
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07002811#ifdef CONFIG_CMA
2812 /* If allocation can't use CMA areas don't use free CMA pages */
2813 if (!(alloc_flags & ALLOC_CMA))
Mel Gorman97a16fc2015-11-06 16:28:40 -08002814 free_pages -= zone_page_state(z, NR_FREE_CMA_PAGES);
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07002815#endif
Tomasz Stanislawski026b0812013-06-12 14:05:02 -07002816
Mel Gorman97a16fc2015-11-06 16:28:40 -08002817 /*
2818 * Check watermarks for an order-0 allocation request. If these
2819 * are not met, then a high-order request also cannot go ahead
2820 * even if a suitable page happened to be free.
2821 */
2822 if (free_pages <= min + z->lowmem_reserve[classzone_idx])
Mel Gorman88f5acf2011-01-13 15:45:41 -08002823 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
Mel Gorman97a16fc2015-11-06 16:28:40 -08002825 /* If this is an order-0 request then the watermark is fine */
2826 if (!order)
2827 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
Mel Gorman97a16fc2015-11-06 16:28:40 -08002829 /* For a high-order request, check at least one suitable page is free */
2830 for (o = order; o < MAX_ORDER; o++) {
2831 struct free_area *area = &z->free_area[o];
2832 int mt;
2833
2834 if (!area->nr_free)
2835 continue;
2836
Mel Gorman97a16fc2015-11-06 16:28:40 -08002837 for (mt = 0; mt < MIGRATE_PCPTYPES; mt++) {
2838 if (!list_empty(&area->free_list[mt]))
2839 return true;
2840 }
2841
2842#ifdef CONFIG_CMA
2843 if ((alloc_flags & ALLOC_CMA) &&
2844 !list_empty(&area->free_list[MIGRATE_CMA])) {
2845 return true;
2846 }
2847#endif
Vlastimil Babka685cce52017-11-15 17:38:30 -08002848 if (alloc_harder &&
2849 !list_empty(&area->free_list[MIGRATE_HIGHATOMIC]))
2850 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 }
Mel Gorman97a16fc2015-11-06 16:28:40 -08002852 return false;
Mel Gorman88f5acf2011-01-13 15:45:41 -08002853}
2854
Mel Gorman7aeb09f2014-06-04 16:10:21 -07002855bool zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
Mel Gormanc6038442016-05-19 17:13:38 -07002856 int classzone_idx, unsigned int alloc_flags)
Mel Gorman88f5acf2011-01-13 15:45:41 -08002857{
2858 return __zone_watermark_ok(z, order, mark, classzone_idx, alloc_flags,
2859 zone_page_state(z, NR_FREE_PAGES));
2860}
2861
Mel Gorman48ee5f32016-05-19 17:14:07 -07002862static inline bool zone_watermark_fast(struct zone *z, unsigned int order,
2863 unsigned long mark, int classzone_idx, unsigned int alloc_flags)
2864{
2865 long free_pages = zone_page_state(z, NR_FREE_PAGES);
2866 long cma_pages = 0;
2867
2868#ifdef CONFIG_CMA
2869 /* If allocation can't use CMA areas don't use free CMA pages */
2870 if (!(alloc_flags & ALLOC_CMA))
2871 cma_pages = zone_page_state(z, NR_FREE_CMA_PAGES);
2872#endif
2873
2874 /*
2875 * Fast check for order-0 only. If this fails then the reserves
2876 * need to be calculated. There is a corner case where the check
2877 * passes but only the high-order atomic reserve are free. If
2878 * the caller is !atomic then it'll uselessly search the free
2879 * list. That corner case is then slower but it is harmless.
2880 */
2881 if (!order && (free_pages - cma_pages) > mark + z->lowmem_reserve[classzone_idx])
2882 return true;
2883
2884 return __zone_watermark_ok(z, order, mark, classzone_idx, alloc_flags,
2885 free_pages);
2886}
2887
Mel Gorman7aeb09f2014-06-04 16:10:21 -07002888bool zone_watermark_ok_safe(struct zone *z, unsigned int order,
Mel Gormane2b19192015-11-06 16:28:09 -08002889 unsigned long mark, int classzone_idx)
Mel Gorman88f5acf2011-01-13 15:45:41 -08002890{
2891 long free_pages = zone_page_state(z, NR_FREE_PAGES);
2892
2893 if (z->percpu_drift_mark && free_pages < z->percpu_drift_mark)
2894 free_pages = zone_page_state_snapshot(z, NR_FREE_PAGES);
2895
Mel Gormane2b19192015-11-06 16:28:09 -08002896 return __zone_watermark_ok(z, order, mark, classzone_idx, 0,
Mel Gorman88f5acf2011-01-13 15:45:41 -08002897 free_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898}
2899
Paul Jackson9276b1bc2006-12-06 20:31:48 -08002900#ifdef CONFIG_NUMA
David Rientjes957f8222012-10-08 16:33:24 -07002901static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone)
2902{
Gavin Shand1e80422017-02-24 14:59:33 -08002903 return node_distance(zone_to_nid(local_zone), zone_to_nid(zone)) <=
Mel Gorman5f7a75a2014-06-04 16:07:15 -07002904 RECLAIM_DISTANCE;
David Rientjes957f8222012-10-08 16:33:24 -07002905}
Paul Jackson9276b1bc2006-12-06 20:31:48 -08002906#else /* CONFIG_NUMA */
David Rientjes957f8222012-10-08 16:33:24 -07002907static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone)
2908{
2909 return true;
2910}
Paul Jackson9276b1bc2006-12-06 20:31:48 -08002911#endif /* CONFIG_NUMA */
2912
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002913/*
Paul Jackson0798e512006-12-06 20:31:38 -08002914 * get_page_from_freelist goes through the zonelist trying to allocate
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002915 * a page.
2916 */
2917static struct page *
Vlastimil Babkaa9263752015-02-11 15:25:41 -08002918get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
2919 const struct alloc_context *ac)
Martin Hicks753ee722005-06-21 17:14:41 -07002920{
Mel Gormanc33d6c02016-05-19 17:14:10 -07002921 struct zoneref *z = ac->preferred_zoneref;
Mel Gorman5117f452009-06-16 15:31:59 -07002922 struct zone *zone;
Mel Gorman3b8c0be2016-07-28 15:46:53 -07002923 struct pglist_data *last_pgdat_dirty_limit = NULL;
2924
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002925 /*
Paul Jackson9276b1bc2006-12-06 20:31:48 -08002926 * Scan zonelist, looking for a zone with enough free.
Vladimir Davydov344736f2014-10-20 15:50:30 +04002927 * See also __cpuset_node_allowed() comment in kernel/cpuset.c.
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002928 */
Mel Gormanc33d6c02016-05-19 17:14:10 -07002929 for_next_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
Vlastimil Babkaa9263752015-02-11 15:25:41 -08002930 ac->nodemask) {
Mel Gormanbe06af02016-05-19 17:13:47 -07002931 struct page *page;
Johannes Weinere085dbc2013-09-11 14:20:46 -07002932 unsigned long mark;
2933
Mel Gorman664eedd2014-06-04 16:10:08 -07002934 if (cpusets_enabled() &&
2935 (alloc_flags & ALLOC_CPUSET) &&
Vlastimil Babka002f2902016-05-19 17:14:30 -07002936 !__cpuset_zone_allowed(zone, gfp_mask))
Mel Gormancd38b112011-07-25 17:12:29 -07002937 continue;
Johannes Weinera756cf52012-01-10 15:07:49 -08002938 /*
2939 * When allocating a page cache page for writing, we
Mel Gorman281e3722016-07-28 15:46:11 -07002940 * want to get it from a node that is within its dirty
2941 * limit, such that no single node holds more than its
Johannes Weinera756cf52012-01-10 15:07:49 -08002942 * proportional share of globally allowed dirty pages.
Mel Gorman281e3722016-07-28 15:46:11 -07002943 * The dirty limits take into account the node's
Johannes Weinera756cf52012-01-10 15:07:49 -08002944 * lowmem reserves and high watermark so that kswapd
2945 * should be able to balance it without having to
2946 * write pages from its LRU list.
2947 *
Johannes Weinera756cf52012-01-10 15:07:49 -08002948 * XXX: For now, allow allocations to potentially
Mel Gorman281e3722016-07-28 15:46:11 -07002949 * exceed the per-node dirty limit in the slowpath
Mel Gormanc9ab0c42015-11-06 16:28:12 -08002950 * (spread_dirty_pages unset) before going into reclaim,
Johannes Weinera756cf52012-01-10 15:07:49 -08002951 * which is important when on a NUMA setup the allowed
Mel Gorman281e3722016-07-28 15:46:11 -07002952 * nodes are together not big enough to reach the
Johannes Weinera756cf52012-01-10 15:07:49 -08002953 * global limit. The proper fix for these situations
Mel Gorman281e3722016-07-28 15:46:11 -07002954 * will require awareness of nodes in the
Johannes Weinera756cf52012-01-10 15:07:49 -08002955 * dirty-throttling and the flusher threads.
2956 */
Mel Gorman3b8c0be2016-07-28 15:46:53 -07002957 if (ac->spread_dirty_pages) {
2958 if (last_pgdat_dirty_limit == zone->zone_pgdat)
2959 continue;
2960
2961 if (!node_dirty_ok(zone->zone_pgdat)) {
2962 last_pgdat_dirty_limit = zone->zone_pgdat;
2963 continue;
2964 }
2965 }
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002966
Johannes Weinere085dbc2013-09-11 14:20:46 -07002967 mark = zone->watermark[alloc_flags & ALLOC_WMARK_MASK];
Mel Gorman48ee5f32016-05-19 17:14:07 -07002968 if (!zone_watermark_fast(zone, order, mark,
Mel Gorman93ea9962016-05-19 17:14:13 -07002969 ac_classzone_idx(ac), alloc_flags)) {
Mel Gormanfa5e0842009-06-16 15:33:22 -07002970 int ret;
2971
Mel Gorman5dab2912014-06-04 16:10:14 -07002972 /* Checked here to keep the fast path fast */
2973 BUILD_BUG_ON(ALLOC_NO_WATERMARKS < NR_WMARK);
2974 if (alloc_flags & ALLOC_NO_WATERMARKS)
2975 goto try_this_zone;
2976
Mel Gormana5f5f912016-07-28 15:46:32 -07002977 if (node_reclaim_mode == 0 ||
Mel Gormanc33d6c02016-05-19 17:14:10 -07002978 !zone_allows_reclaim(ac->preferred_zoneref->zone, zone))
Mel Gormancd38b112011-07-25 17:12:29 -07002979 continue;
2980
Mel Gormana5f5f912016-07-28 15:46:32 -07002981 ret = node_reclaim(zone->zone_pgdat, gfp_mask, order);
Mel Gormanfa5e0842009-06-16 15:33:22 -07002982 switch (ret) {
Mel Gormana5f5f912016-07-28 15:46:32 -07002983 case NODE_RECLAIM_NOSCAN:
Mel Gormanfa5e0842009-06-16 15:33:22 -07002984 /* did not scan */
Mel Gormancd38b112011-07-25 17:12:29 -07002985 continue;
Mel Gormana5f5f912016-07-28 15:46:32 -07002986 case NODE_RECLAIM_FULL:
Mel Gormanfa5e0842009-06-16 15:33:22 -07002987 /* scanned but unreclaimable */
Mel Gormancd38b112011-07-25 17:12:29 -07002988 continue;
Mel Gormanfa5e0842009-06-16 15:33:22 -07002989 default:
2990 /* did we reclaim enough */
Mel Gormanfed27192013-04-29 15:07:57 -07002991 if (zone_watermark_ok(zone, order, mark,
Mel Gorman93ea9962016-05-19 17:14:13 -07002992 ac_classzone_idx(ac), alloc_flags))
Mel Gormanfed27192013-04-29 15:07:57 -07002993 goto try_this_zone;
2994
Mel Gormanfed27192013-04-29 15:07:57 -07002995 continue;
Paul Jackson0798e512006-12-06 20:31:38 -08002996 }
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002997 }
2998
Mel Gormanfa5e0842009-06-16 15:33:22 -07002999try_this_zone:
Mel Gormanc33d6c02016-05-19 17:14:10 -07003000 page = buffered_rmqueue(ac->preferred_zoneref->zone, zone, order,
Mel Gorman0aaa29a2015-11-06 16:28:37 -08003001 gfp_mask, alloc_flags, ac->migratetype);
Vlastimil Babka75379192015-02-11 15:25:38 -08003002 if (page) {
Mel Gorman479f8542016-05-19 17:14:35 -07003003 prep_new_page(page, order, gfp_mask, alloc_flags);
Mel Gorman0aaa29a2015-11-06 16:28:37 -08003004
3005 /*
3006 * If this is a high-order atomic allocation then check
3007 * if the pageblock should be reserved for the future
3008 */
3009 if (unlikely(order && (alloc_flags & ALLOC_HARDER)))
3010 reserve_highatomic_pageblock(page, zone, order);
3011
Vlastimil Babka75379192015-02-11 15:25:38 -08003012 return page;
3013 }
Mel Gorman54a6eb52008-04-28 02:12:16 -07003014 }
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003015
Mel Gorman4ffeaf32014-08-06 16:07:22 -07003016 return NULL;
Martin Hicks753ee722005-06-21 17:14:41 -07003017}
3018
David Rientjes29423e772011-03-22 16:30:47 -07003019/*
3020 * Large machines with many possible nodes should not always dump per-node
3021 * meminfo in irq context.
3022 */
3023static inline bool should_suppress_show_mem(void)
3024{
3025 bool ret = false;
3026
3027#if NODES_SHIFT > 8
3028 ret = in_interrupt();
3029#endif
3030 return ret;
3031}
3032
Dave Hansena238ab52011-05-24 17:12:16 -07003033static DEFINE_RATELIMIT_STATE(nopage_rs,
3034 DEFAULT_RATELIMIT_INTERVAL,
3035 DEFAULT_RATELIMIT_BURST);
3036
Michal Hocko7877cdc2016-10-07 17:01:55 -07003037void warn_alloc(gfp_t gfp_mask, const char *fmt, ...)
Dave Hansena238ab52011-05-24 17:12:16 -07003038{
Dave Hansena238ab52011-05-24 17:12:16 -07003039 unsigned int filter = SHOW_MEM_FILTER_NODES;
Michal Hocko7877cdc2016-10-07 17:01:55 -07003040 struct va_format vaf;
3041 va_list args;
Dave Hansena238ab52011-05-24 17:12:16 -07003042
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -08003043 if ((gfp_mask & __GFP_NOWARN) || !__ratelimit(&nopage_rs) ||
3044 debug_guardpage_minorder() > 0)
Dave Hansena238ab52011-05-24 17:12:16 -07003045 return;
3046
3047 /*
3048 * This documents exceptions given to allocations in certain
3049 * contexts that are allowed to allocate outside current's set
3050 * of allowed nodes.
3051 */
3052 if (!(gfp_mask & __GFP_NOMEMALLOC))
3053 if (test_thread_flag(TIF_MEMDIE) ||
3054 (current->flags & (PF_MEMALLOC | PF_EXITING)))
3055 filter &= ~SHOW_MEM_FILTER_NODES;
Mel Gormand0164ad2015-11-06 16:28:21 -08003056 if (in_interrupt() || !(gfp_mask & __GFP_DIRECT_RECLAIM))
Dave Hansena238ab52011-05-24 17:12:16 -07003057 filter &= ~SHOW_MEM_FILTER_NODES;
3058
Michal Hocko7877cdc2016-10-07 17:01:55 -07003059 pr_warn("%s: ", current->comm);
Joe Perches3ee9a4f2011-10-31 17:08:35 -07003060
Michal Hocko7877cdc2016-10-07 17:01:55 -07003061 va_start(args, fmt);
3062 vaf.fmt = fmt;
3063 vaf.va = &args;
3064 pr_cont("%pV", &vaf);
3065 va_end(args);
Joe Perches3ee9a4f2011-10-31 17:08:35 -07003066
Michal Hocko7877cdc2016-10-07 17:01:55 -07003067 pr_cont(", mode:%#x(%pGg)\n", gfp_mask, &gfp_mask);
Joe Perches3ee9a4f2011-10-31 17:08:35 -07003068
Dave Hansena238ab52011-05-24 17:12:16 -07003069 dump_stack();
3070 if (!should_suppress_show_mem())
3071 show_mem(filter);
3072}
3073
Mel Gorman11e33f62009-06-16 15:31:57 -07003074static inline struct page *
3075__alloc_pages_may_oom(gfp_t gfp_mask, unsigned int order,
Vlastimil Babkaa9263752015-02-11 15:25:41 -08003076 const struct alloc_context *ac, unsigned long *did_some_progress)
Mel Gorman11e33f62009-06-16 15:31:57 -07003077{
David Rientjes6e0fc462015-09-08 15:00:36 -07003078 struct oom_control oc = {
3079 .zonelist = ac->zonelist,
3080 .nodemask = ac->nodemask,
Vladimir Davydov2a966b72016-07-26 15:22:33 -07003081 .memcg = NULL,
David Rientjes6e0fc462015-09-08 15:00:36 -07003082 .gfp_mask = gfp_mask,
3083 .order = order,
David Rientjes6e0fc462015-09-08 15:00:36 -07003084 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
Johannes Weiner9879de72015-01-26 12:58:32 -08003087 *did_some_progress = 0;
3088
Johannes Weiner9879de72015-01-26 12:58:32 -08003089 /*
Johannes Weinerdc564012015-06-24 16:57:19 -07003090 * Acquire the oom lock. If that fails, somebody else is
3091 * making progress for us.
Johannes Weiner9879de72015-01-26 12:58:32 -08003092 */
Johannes Weinerdc564012015-06-24 16:57:19 -07003093 if (!mutex_trylock(&oom_lock)) {
Johannes Weiner9879de72015-01-26 12:58:32 -08003094 *did_some_progress = 1;
Mel Gorman11e33f62009-06-16 15:31:57 -07003095 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 return NULL;
3097 }
Jens Axboe6b1de912005-11-17 21:35:02 +01003098
Mel Gorman11e33f62009-06-16 15:31:57 -07003099 /*
3100 * Go through the zonelist yet one more time, keep very high watermark
3101 * here, this is only to catch a parallel oom killing, we must fail if
3102 * we're still under heavy pressure.
3103 */
Vlastimil Babkaa9263752015-02-11 15:25:41 -08003104 page = get_page_from_freelist(gfp_mask | __GFP_HARDWALL, order,
3105 ALLOC_WMARK_HIGH|ALLOC_CPUSET, ac);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08003106 if (page)
Mel Gorman11e33f62009-06-16 15:31:57 -07003107 goto out;
3108
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -08003109 if (!(gfp_mask & __GFP_NOFAIL)) {
Johannes Weiner9879de72015-01-26 12:58:32 -08003110 /* Coredumps can quickly deplete all memory reserves */
3111 if (current->flags & PF_DUMPCORE)
3112 goto out;
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -08003113 /* The OOM killer will not help higher order allocs */
3114 if (order > PAGE_ALLOC_COSTLY_ORDER)
3115 goto out;
David Rientjes03668b32010-08-09 17:18:54 -07003116 /* The OOM killer does not needlessly kill tasks for lowmem */
Vlastimil Babkaa9263752015-02-11 15:25:41 -08003117 if (ac->high_zoneidx < ZONE_NORMAL)
David Rientjes03668b32010-08-09 17:18:54 -07003118 goto out;
Johannes Weiner90839052015-06-24 16:57:21 -07003119 if (pm_suspended_storage())
3120 goto out;
Michal Hocko3da88fb2016-05-19 17:13:09 -07003121 /*
3122 * XXX: GFP_NOFS allocations should rather fail than rely on
3123 * other request to make a forward progress.
3124 * We are in an unfortunate situation where out_of_memory cannot
3125 * do much for this context but let's try it to at least get
3126 * access to memory reserved if the current task is killed (see
3127 * out_of_memory). Once filesystems are ready to handle allocation
3128 * failures more gracefully we should just bail out here.
3129 */
3130
David Rientjes4167e9b2015-04-14 15:46:55 -07003131 /* The OOM killer may not free memory on a specific node */
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -08003132 if (gfp_mask & __GFP_THISNODE)
3133 goto out;
3134 }
Mel Gorman11e33f62009-06-16 15:31:57 -07003135 /* Exhausted what can be done so it's blamo time */
Michal Hocko5020e282016-01-14 15:20:36 -08003136 if (out_of_memory(&oc) || WARN_ON_ONCE(gfp_mask & __GFP_NOFAIL)) {
Michal Hockoc32b3cb2015-02-11 15:26:24 -08003137 *did_some_progress = 1;
Michal Hocko5020e282016-01-14 15:20:36 -08003138
3139 if (gfp_mask & __GFP_NOFAIL) {
3140 page = get_page_from_freelist(gfp_mask, order,
3141 ALLOC_NO_WATERMARKS|ALLOC_CPUSET, ac);
3142 /*
3143 * fallback to ignore cpuset restriction if our nodes
3144 * are depleted
3145 */
3146 if (!page)
3147 page = get_page_from_freelist(gfp_mask, order,
3148 ALLOC_NO_WATERMARKS, ac);
3149 }
3150 }
Mel Gorman11e33f62009-06-16 15:31:57 -07003151out:
Johannes Weinerdc564012015-06-24 16:57:19 -07003152 mutex_unlock(&oom_lock);
Mel Gorman11e33f62009-06-16 15:31:57 -07003153 return page;
3154}
3155
Michal Hocko33c2d212016-05-20 16:57:06 -07003156/*
3157 * Maximum number of compaction retries wit a progress before OOM
3158 * killer is consider as the only way to move forward.
3159 */
3160#define MAX_COMPACT_RETRIES 16
3161
Mel Gorman56de7262010-05-24 14:32:30 -07003162#ifdef CONFIG_COMPACTION
3163/* Try memory compaction for high-order allocations before reclaim */
3164static struct page *
3165__alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
Mel Gormanc6038442016-05-19 17:13:38 -07003166 unsigned int alloc_flags, const struct alloc_context *ac,
Vlastimil Babkaa5508cd2016-07-28 15:49:28 -07003167 enum compact_priority prio, enum compact_result *compact_result)
Mel Gorman56de7262010-05-24 14:32:30 -07003168{
Vlastimil Babka98dd3b42014-10-09 15:27:04 -07003169 struct page *page;
Johannes Weiner3df0e592018-10-26 15:06:27 -07003170 unsigned long pflags;
Vlastimil Babka4e434d42017-05-08 15:59:46 -07003171 unsigned int noreclaim_flag = current->flags & PF_MEMALLOC;
Vlastimil Babka53853e22014-10-09 15:27:02 -07003172
Mel Gorman66199712012-01-12 17:19:41 -08003173 if (!order)
Mel Gorman56de7262010-05-24 14:32:30 -07003174 return NULL;
3175
Johannes Weiner3df0e592018-10-26 15:06:27 -07003176 psi_memstall_enter(&pflags);
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08003177 current->flags |= PF_MEMALLOC;
Johannes Weiner3df0e592018-10-26 15:06:27 -07003178
Michal Hockoc5d01d02016-05-20 16:56:53 -07003179 *compact_result = try_to_compact_pages(gfp_mask, order, alloc_flags, ac,
Vlastimil Babkac3486f52016-07-28 15:49:30 -07003180 prio);
Johannes Weiner3df0e592018-10-26 15:06:27 -07003181
Vlastimil Babka4e434d42017-05-08 15:59:46 -07003182 current->flags = (current->flags & ~PF_MEMALLOC) | noreclaim_flag;
Johannes Weiner3df0e592018-10-26 15:06:27 -07003183 psi_memstall_leave(&pflags);
Mel Gorman56de7262010-05-24 14:32:30 -07003184
Michal Hockoc5d01d02016-05-20 16:56:53 -07003185 if (*compact_result <= COMPACT_INACTIVE)
Vlastimil Babka98dd3b42014-10-09 15:27:04 -07003186 return NULL;
Mel Gorman56de7262010-05-24 14:32:30 -07003187
Vlastimil Babka98dd3b42014-10-09 15:27:04 -07003188 /*
3189 * At least in one zone compaction wasn't deferred or skipped, so let's
3190 * count a compaction stall
3191 */
3192 count_vm_event(COMPACTSTALL);
3193
Vlastimil Babka31a6c192016-07-28 15:49:13 -07003194 page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac);
Vlastimil Babka98dd3b42014-10-09 15:27:04 -07003195
3196 if (page) {
3197 struct zone *zone = page_zone(page);
3198
3199 zone->compact_blockskip_flush = false;
3200 compaction_defer_reset(zone, order, true);
3201 count_vm_event(COMPACTSUCCESS);
3202 return page;
3203 }
3204
3205 /*
Vlastimil Babka98dd3b42014-10-09 15:27:04 -07003206 * It's bad if compaction run occurs and fails. The most likely reason
3207 * is that pages exist, but not enough to satisfy watermarks.
3208 */
3209 count_vm_event(COMPACTFAIL);
3210
3211 cond_resched();
3212
Mel Gorman56de7262010-05-24 14:32:30 -07003213 return NULL;
3214}
Michal Hocko33c2d212016-05-20 16:57:06 -07003215
Vlastimil Babka32508452016-10-07 17:00:28 -07003216static inline bool
3217should_compact_retry(struct alloc_context *ac, int order, int alloc_flags,
3218 enum compact_result compact_result,
3219 enum compact_priority *compact_priority,
Vlastimil Babkad9436492016-10-07 17:00:31 -07003220 int *compaction_retries)
Vlastimil Babka32508452016-10-07 17:00:28 -07003221{
3222 int max_retries = MAX_COMPACT_RETRIES;
Vlastimil Babkac2033b02016-10-07 17:00:34 -07003223 int min_priority;
Vlastimil Babka32508452016-10-07 17:00:28 -07003224
3225 if (!order)
3226 return false;
3227
Vlastimil Babkad9436492016-10-07 17:00:31 -07003228 if (compaction_made_progress(compact_result))
3229 (*compaction_retries)++;
3230
Vlastimil Babka32508452016-10-07 17:00:28 -07003231 /*
3232 * compaction considers all the zone as desperately out of memory
3233 * so it doesn't really make much sense to retry except when the
3234 * failure could be caused by insufficient priority
3235 */
Vlastimil Babkad9436492016-10-07 17:00:31 -07003236 if (compaction_failed(compact_result))
3237 goto check_priority;
Vlastimil Babka32508452016-10-07 17:00:28 -07003238
3239 /*
3240 * make sure the compaction wasn't deferred or didn't bail out early
3241 * due to locks contention before we declare that we should give up.
3242 * But do not retry if the given zonelist is not suitable for
3243 * compaction.
3244 */
3245 if (compaction_withdrawn(compact_result))
3246 return compaction_zonelist_suitable(ac, order, alloc_flags);
3247
3248 /*
3249 * !costly requests are much more important than __GFP_REPEAT
3250 * costly ones because they are de facto nofail and invoke OOM
3251 * killer to move on while costly can fail and users are ready
3252 * to cope with that. 1/4 retries is rather arbitrary but we
3253 * would need much more detailed feedback from compaction to
3254 * make a better decision.
3255 */
3256 if (order > PAGE_ALLOC_COSTLY_ORDER)
3257 max_retries /= 4;
Vlastimil Babkad9436492016-10-07 17:00:31 -07003258 if (*compaction_retries <= max_retries)
Vlastimil Babka32508452016-10-07 17:00:28 -07003259 return true;
3260
Vlastimil Babkad9436492016-10-07 17:00:31 -07003261 /*
3262 * Make sure there are attempts at the highest priority if we exhausted
3263 * all retries or failed at the lower priorities.
3264 */
3265check_priority:
Vlastimil Babkac2033b02016-10-07 17:00:34 -07003266 min_priority = (order > PAGE_ALLOC_COSTLY_ORDER) ?
3267 MIN_COMPACT_COSTLY_PRIORITY : MIN_COMPACT_PRIORITY;
3268 if (*compact_priority > min_priority) {
Vlastimil Babkad9436492016-10-07 17:00:31 -07003269 (*compact_priority)--;
3270 *compaction_retries = 0;
3271 return true;
3272 }
Vlastimil Babka32508452016-10-07 17:00:28 -07003273 return false;
3274}
Mel Gorman56de7262010-05-24 14:32:30 -07003275#else
3276static inline struct page *
3277__alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
Mel Gormanc6038442016-05-19 17:13:38 -07003278 unsigned int alloc_flags, const struct alloc_context *ac,
Vlastimil Babkaa5508cd2016-07-28 15:49:28 -07003279 enum compact_priority prio, enum compact_result *compact_result)
Mel Gorman56de7262010-05-24 14:32:30 -07003280{
Michal Hocko33c2d212016-05-20 16:57:06 -07003281 *compact_result = COMPACT_SKIPPED;
Mel Gorman56de7262010-05-24 14:32:30 -07003282 return NULL;
3283}
Michal Hocko33c2d212016-05-20 16:57:06 -07003284
3285static inline bool
Michal Hocko86a294a2016-05-20 16:57:12 -07003286should_compact_retry(struct alloc_context *ac, unsigned int order, int alloc_flags,
3287 enum compact_result compact_result,
Vlastimil Babkaa5508cd2016-07-28 15:49:28 -07003288 enum compact_priority *compact_priority,
Vlastimil Babkad9436492016-10-07 17:00:31 -07003289 int *compaction_retries)
Michal Hocko33c2d212016-05-20 16:57:06 -07003290{
Michal Hocko31e49bf2016-05-20 16:57:15 -07003291 struct zone *zone;
3292 struct zoneref *z;
3293
3294 if (!order || order > PAGE_ALLOC_COSTLY_ORDER)
3295 return false;
3296
3297 /*
3298 * There are setups with compaction disabled which would prefer to loop
3299 * inside the allocator rather than hit the oom killer prematurely.
3300 * Let's give them a good hope and keep retrying while the order-0
3301 * watermarks are OK.
3302 */
3303 for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
3304 ac->nodemask) {
3305 if (zone_watermark_ok(zone, 0, min_wmark_pages(zone),
3306 ac_classzone_idx(ac), alloc_flags))
3307 return true;
3308 }
Michal Hocko33c2d212016-05-20 16:57:06 -07003309 return false;
3310}
Vlastimil Babka32508452016-10-07 17:00:28 -07003311#endif /* CONFIG_COMPACTION */
Mel Gorman56de7262010-05-24 14:32:30 -07003312
Marek Szyprowskibba90712012-01-25 12:09:52 +01003313/* Perform direct synchronous page reclaim */
3314static int
Vlastimil Babkaa9263752015-02-11 15:25:41 -08003315__perform_reclaim(gfp_t gfp_mask, unsigned int order,
3316 const struct alloc_context *ac)
Mel Gorman11e33f62009-06-16 15:31:57 -07003317{
Mel Gorman11e33f62009-06-16 15:31:57 -07003318 struct reclaim_state reclaim_state;
Marek Szyprowskibba90712012-01-25 12:09:52 +01003319 int progress;
Johannes Weiner3df0e592018-10-26 15:06:27 -07003320 unsigned long pflags;
Mel Gorman11e33f62009-06-16 15:31:57 -07003321
3322 cond_resched();
3323
3324 /* We now go into synchronous reclaim */
3325 cpuset_memory_pressure_bump();
Johannes Weiner3df0e592018-10-26 15:06:27 -07003326 psi_memstall_enter(&pflags);
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08003327 current->flags |= PF_MEMALLOC;
Mel Gorman11e33f62009-06-16 15:31:57 -07003328 lockdep_set_current_reclaim_state(gfp_mask);
3329 reclaim_state.reclaimed_slab = 0;
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08003330 current->reclaim_state = &reclaim_state;
Mel Gorman11e33f62009-06-16 15:31:57 -07003331
Vlastimil Babkaa9263752015-02-11 15:25:41 -08003332 progress = try_to_free_pages(ac->zonelist, order, gfp_mask,
3333 ac->nodemask);
Mel Gorman11e33f62009-06-16 15:31:57 -07003334
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08003335 current->reclaim_state = NULL;
Mel Gorman11e33f62009-06-16 15:31:57 -07003336 lockdep_clear_current_reclaim_state();
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08003337 current->flags &= ~PF_MEMALLOC;
Johannes Weiner3df0e592018-10-26 15:06:27 -07003338 psi_memstall_leave(&pflags);
Mel Gorman11e33f62009-06-16 15:31:57 -07003339
3340 cond_resched();
3341
Marek Szyprowskibba90712012-01-25 12:09:52 +01003342 return progress;
3343}
3344
3345/* The really slow allocator path where we enter direct reclaim */
3346static inline struct page *
3347__alloc_pages_direct_reclaim(gfp_t gfp_mask, unsigned int order,
Mel Gormanc6038442016-05-19 17:13:38 -07003348 unsigned int alloc_flags, const struct alloc_context *ac,
Vlastimil Babkaa9263752015-02-11 15:25:41 -08003349 unsigned long *did_some_progress)
Marek Szyprowskibba90712012-01-25 12:09:52 +01003350{
3351 struct page *page = NULL;
3352 bool drained = false;
3353
Vlastimil Babkaa9263752015-02-11 15:25:41 -08003354 *did_some_progress = __perform_reclaim(gfp_mask, order, ac);
Mel Gorman9ee493c2010-09-09 16:38:18 -07003355 if (unlikely(!(*did_some_progress)))
3356 return NULL;
Mel Gorman11e33f62009-06-16 15:31:57 -07003357
Mel Gorman9ee493c2010-09-09 16:38:18 -07003358retry:
Vlastimil Babka31a6c192016-07-28 15:49:13 -07003359 page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac);
Mel Gorman9ee493c2010-09-09 16:38:18 -07003360
3361 /*
3362 * If an allocation failed after direct reclaim, it could be because
Mel Gorman0aaa29a2015-11-06 16:28:37 -08003363 * pages are pinned on the per-cpu lists or in high alloc reserves.
3364 * Shrink them them and try again
Mel Gorman9ee493c2010-09-09 16:38:18 -07003365 */
3366 if (!page && !drained) {
Mel Gorman0aaa29a2015-11-06 16:28:37 -08003367 unreserve_highatomic_pageblock(ac);
Vlastimil Babka93481ff2014-12-10 15:43:01 -08003368 drain_all_pages(NULL);
Mel Gorman9ee493c2010-09-09 16:38:18 -07003369 drained = true;
3370 goto retry;
3371 }
3372
Mel Gorman11e33f62009-06-16 15:31:57 -07003373 return page;
3374}
3375
Vlastimil Babkaa9263752015-02-11 15:25:41 -08003376static void wake_all_kswapds(unsigned int order, const struct alloc_context *ac)
Mel Gorman11e33f62009-06-16 15:31:57 -07003377{
3378 struct zoneref *z;
3379 struct zone *zone;
Mel Gormane1a55632016-07-28 15:46:26 -07003380 pg_data_t *last_pgdat = NULL;
Mel Gorman11e33f62009-06-16 15:31:57 -07003381
Vlastimil Babkaa9263752015-02-11 15:25:41 -08003382 for_each_zone_zonelist_nodemask(zone, z, ac->zonelist,
Mel Gormane1a55632016-07-28 15:46:26 -07003383 ac->high_zoneidx, ac->nodemask) {
3384 if (last_pgdat != zone->zone_pgdat)
Mel Gorman52e9f872016-07-28 15:46:29 -07003385 wakeup_kswapd(zone, order, ac->high_zoneidx);
Mel Gormane1a55632016-07-28 15:46:26 -07003386 last_pgdat = zone->zone_pgdat;
3387 }
Mel Gorman11e33f62009-06-16 15:31:57 -07003388}
3389
Mel Gormanc6038442016-05-19 17:13:38 -07003390static inline unsigned int
Peter Zijlstra341ce062009-06-16 15:32:02 -07003391gfp_to_alloc_flags(gfp_t gfp_mask)
3392{
Mel Gormanc6038442016-05-19 17:13:38 -07003393 unsigned int alloc_flags = ALLOC_WMARK_MIN | ALLOC_CPUSET;
Peter Zijlstra341ce062009-06-16 15:32:02 -07003394
Mel Gormana56f57f2009-06-16 15:32:02 -07003395 /* __GFP_HIGH is assumed to be the same as ALLOC_HIGH to save a branch. */
Namhyung Kime6223a32010-10-26 14:21:59 -07003396 BUILD_BUG_ON(__GFP_HIGH != (__force gfp_t) ALLOC_HIGH);
Mel Gormana56f57f2009-06-16 15:32:02 -07003397
Peter Zijlstra341ce062009-06-16 15:32:02 -07003398 /*
3399 * The caller may dip into page reserves a bit more if the caller
3400 * cannot run direct reclaim, or if the caller has realtime scheduling
3401 * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will
Mel Gormand0164ad2015-11-06 16:28:21 -08003402 * set both ALLOC_HARDER (__GFP_ATOMIC) and ALLOC_HIGH (__GFP_HIGH).
Peter Zijlstra341ce062009-06-16 15:32:02 -07003403 */
Namhyung Kime6223a32010-10-26 14:21:59 -07003404 alloc_flags |= (__force int) (gfp_mask & __GFP_HIGH);
Peter Zijlstra341ce062009-06-16 15:32:02 -07003405
Mel Gormand0164ad2015-11-06 16:28:21 -08003406 if (gfp_mask & __GFP_ATOMIC) {
Andrea Arcangeli5c3240d2011-01-13 15:46:49 -08003407 /*
David Rientjesb104a352014-07-30 16:08:24 -07003408 * Not worth trying to allocate harder for __GFP_NOMEMALLOC even
3409 * if it can't schedule.
Andrea Arcangeli5c3240d2011-01-13 15:46:49 -08003410 */
David Rientjesb104a352014-07-30 16:08:24 -07003411 if (!(gfp_mask & __GFP_NOMEMALLOC))
Andrea Arcangeli5c3240d2011-01-13 15:46:49 -08003412 alloc_flags |= ALLOC_HARDER;
Peter Zijlstra341ce062009-06-16 15:32:02 -07003413 /*
David Rientjesb104a352014-07-30 16:08:24 -07003414 * Ignore cpuset mems for GFP_ATOMIC rather than fail, see the
Vladimir Davydov344736f2014-10-20 15:50:30 +04003415 * comment for __cpuset_node_allowed().
Peter Zijlstra341ce062009-06-16 15:32:02 -07003416 */
3417 alloc_flags &= ~ALLOC_CPUSET;
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08003418 } else if (unlikely(rt_task(current)) && !in_interrupt())
Peter Zijlstra341ce062009-06-16 15:32:02 -07003419 alloc_flags |= ALLOC_HARDER;
3420
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07003421#ifdef CONFIG_CMA
David Rientjes43e7a342014-10-09 15:27:25 -07003422 if (gfpflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07003423 alloc_flags |= ALLOC_CMA;
3424#endif
Peter Zijlstra341ce062009-06-16 15:32:02 -07003425 return alloc_flags;
3426}
3427
Mel Gorman072bb0a2012-07-31 16:43:58 -07003428bool gfp_pfmemalloc_allowed(gfp_t gfp_mask)
3429{
Vlastimil Babka31a6c192016-07-28 15:49:13 -07003430 if (unlikely(gfp_mask & __GFP_NOMEMALLOC))
3431 return false;
3432
3433 if (gfp_mask & __GFP_MEMALLOC)
3434 return true;
3435 if (in_serving_softirq() && (current->flags & PF_MEMALLOC))
3436 return true;
3437 if (!in_interrupt() &&
3438 ((current->flags & PF_MEMALLOC) ||
3439 unlikely(test_thread_flag(TIF_MEMDIE))))
3440 return true;
3441
3442 return false;
Mel Gorman072bb0a2012-07-31 16:43:58 -07003443}
3444
Michal Hocko0a0337e2016-05-20 16:57:00 -07003445/*
Michal Hocko0a0337e2016-05-20 16:57:00 -07003446 * Checks whether it makes sense to retry the reclaim to make a forward progress
3447 * for the given allocation request.
3448 * The reclaim feedback represented by did_some_progress (any progress during
Michal Hocko7854ea62016-05-20 16:57:09 -07003449 * the last reclaim round) and no_progress_loops (number of reclaim rounds without
3450 * any progress in a row) is considered as well as the reclaimable pages on the
3451 * applicable zone list (with a backoff mechanism which is a function of
3452 * no_progress_loops).
Michal Hocko0a0337e2016-05-20 16:57:00 -07003453 *
3454 * Returns true if a retry is viable or false to enter the oom path.
3455 */
3456static inline bool
3457should_reclaim_retry(gfp_t gfp_mask, unsigned order,
3458 struct alloc_context *ac, int alloc_flags,
Vlastimil Babka423b4522016-10-07 17:00:40 -07003459 bool did_some_progress, int *no_progress_loops)
Michal Hocko0a0337e2016-05-20 16:57:00 -07003460{
3461 struct zone *zone;
3462 struct zoneref *z;
3463
3464 /*
Vlastimil Babka423b4522016-10-07 17:00:40 -07003465 * Costly allocations might have made a progress but this doesn't mean
3466 * their order will become available due to high fragmentation so
3467 * always increment the no progress counter for them
3468 */
3469 if (did_some_progress && order <= PAGE_ALLOC_COSTLY_ORDER)
3470 *no_progress_loops = 0;
3471 else
3472 (*no_progress_loops)++;
3473
3474 /*
Michal Hocko0a0337e2016-05-20 16:57:00 -07003475 * Make sure we converge to OOM if we cannot make any progress
3476 * several times in the row.
3477 */
Vlastimil Babka423b4522016-10-07 17:00:40 -07003478 if (*no_progress_loops > MAX_RECLAIM_RETRIES)
Michal Hocko0a0337e2016-05-20 16:57:00 -07003479 return false;
3480
Michal Hocko0a0337e2016-05-20 16:57:00 -07003481 /*
Mel Gormanbca67592016-07-28 15:47:05 -07003482 * Keep reclaiming pages while there is a chance this will lead
3483 * somewhere. If none of the target zones can satisfy our allocation
3484 * request even if all reclaimable pages are considered then we are
3485 * screwed and have to go OOM.
Michal Hocko0a0337e2016-05-20 16:57:00 -07003486 */
3487 for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
3488 ac->nodemask) {
3489 unsigned long available;
Michal Hockoede37712016-05-20 16:57:03 -07003490 unsigned long reclaimable;
Michal Hocko0a0337e2016-05-20 16:57:00 -07003491
Mel Gorman5a1c84b2016-07-28 15:47:31 -07003492 available = reclaimable = zone_reclaimable_pages(zone);
Vlastimil Babka423b4522016-10-07 17:00:40 -07003493 available -= DIV_ROUND_UP((*no_progress_loops) * available,
Michal Hocko0a0337e2016-05-20 16:57:00 -07003494 MAX_RECLAIM_RETRIES);
Mel Gorman5a1c84b2016-07-28 15:47:31 -07003495 available += zone_page_state_snapshot(zone, NR_FREE_PAGES);
Michal Hocko0a0337e2016-05-20 16:57:00 -07003496
3497 /*
3498 * Would the allocation succeed if we reclaimed the whole
Mel Gorman5a1c84b2016-07-28 15:47:31 -07003499 * available?
Michal Hocko0a0337e2016-05-20 16:57:00 -07003500 */
Mel Gorman5a1c84b2016-07-28 15:47:31 -07003501 if (__zone_watermark_ok(zone, order, min_wmark_pages(zone),
3502 ac_classzone_idx(ac), alloc_flags, available)) {
Michal Hockoede37712016-05-20 16:57:03 -07003503 /*
3504 * If we didn't make any progress and have a lot of
3505 * dirty + writeback pages then we should wait for
3506 * an IO to complete to slow down the reclaim and
3507 * prevent from pre mature OOM
3508 */
3509 if (!did_some_progress) {
Mel Gorman11fb9982016-07-28 15:46:20 -07003510 unsigned long write_pending;
Michal Hockoede37712016-05-20 16:57:03 -07003511
Mel Gorman5a1c84b2016-07-28 15:47:31 -07003512 write_pending = zone_page_state_snapshot(zone,
3513 NR_ZONE_WRITE_PENDING);
Michal Hockoede37712016-05-20 16:57:03 -07003514
Mel Gorman11fb9982016-07-28 15:46:20 -07003515 if (2 * write_pending > reclaimable) {
Michal Hockoede37712016-05-20 16:57:03 -07003516 congestion_wait(BLK_RW_ASYNC, HZ/10);
3517 return true;
3518 }
3519 }
Mel Gorman5a1c84b2016-07-28 15:47:31 -07003520
Michal Hockoede37712016-05-20 16:57:03 -07003521 /*
3522 * Memory allocation/reclaim might be called from a WQ
3523 * context and the current implementation of the WQ
3524 * concurrency control doesn't recognize that
3525 * a particular WQ is congested if the worker thread is
3526 * looping without ever sleeping. Therefore we have to
3527 * do a short sleep here rather than calling
3528 * cond_resched().
3529 */
3530 if (current->flags & PF_WQ_WORKER)
3531 schedule_timeout_uninterruptible(1);
3532 else
3533 cond_resched();
3534
Michal Hocko0a0337e2016-05-20 16:57:00 -07003535 return true;
3536 }
3537 }
3538
3539 return false;
3540}
3541
Mel Gorman11e33f62009-06-16 15:31:57 -07003542static inline struct page *
3543__alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
Vlastimil Babkaa9263752015-02-11 15:25:41 -08003544 struct alloc_context *ac)
Mel Gorman11e33f62009-06-16 15:31:57 -07003545{
Mel Gormand0164ad2015-11-06 16:28:21 -08003546 bool can_direct_reclaim = gfp_mask & __GFP_DIRECT_RECLAIM;
Mel Gorman11e33f62009-06-16 15:31:57 -07003547 struct page *page = NULL;
Mel Gormanc6038442016-05-19 17:13:38 -07003548 unsigned int alloc_flags;
Mel Gorman11e33f62009-06-16 15:31:57 -07003549 unsigned long did_some_progress;
Vlastimil Babkab678e4f2017-01-24 15:18:38 -08003550 enum compact_priority compact_priority;
Michal Hockoc5d01d02016-05-20 16:56:53 -07003551 enum compact_result compact_result;
Vlastimil Babkab678e4f2017-01-24 15:18:38 -08003552 int compaction_retries;
3553 int no_progress_loops;
Vlastimil Babkab678e4f2017-01-24 15:18:38 -08003554 unsigned int cpuset_mems_cookie;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08003555
Christoph Lameter952f3b52006-12-06 20:33:26 -08003556 /*
Mel Gorman72807a72009-06-16 15:32:18 -07003557 * In the slowpath, we sanity check order to avoid ever trying to
3558 * reclaim >= MAX_ORDER areas which will never succeed. Callers may
3559 * be using allocators in order of preference for an area that is
3560 * too large.
3561 */
Mel Gorman1fc28b72009-07-29 15:04:08 -07003562 if (order >= MAX_ORDER) {
3563 WARN_ON_ONCE(!(gfp_mask & __GFP_NOWARN));
Mel Gorman72807a72009-06-16 15:32:18 -07003564 return NULL;
Mel Gorman1fc28b72009-07-29 15:04:08 -07003565 }
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08003566
Christoph Lameter952f3b52006-12-06 20:33:26 -08003567 /*
Mel Gormand0164ad2015-11-06 16:28:21 -08003568 * We also sanity check to catch abuse of atomic reserves being used by
3569 * callers that are not in atomic context.
3570 */
3571 if (WARN_ON_ONCE((gfp_mask & (__GFP_ATOMIC|__GFP_DIRECT_RECLAIM)) ==
3572 (__GFP_ATOMIC|__GFP_DIRECT_RECLAIM)))
3573 gfp_mask &= ~__GFP_ATOMIC;
3574
Vlastimil Babkab678e4f2017-01-24 15:18:38 -08003575retry_cpuset:
3576 compaction_retries = 0;
3577 no_progress_loops = 0;
3578 compact_priority = DEF_COMPACT_PRIORITY;
3579 cpuset_mems_cookie = read_mems_allowed_begin();
Vlastimil Babka96e5cec2017-01-24 15:18:41 -08003580 /*
3581 * We need to recalculate the starting point for the zonelist iterator
3582 * because we might have used different nodemask in the fast path, or
3583 * there was a cpuset modification and we are retrying - otherwise we
3584 * could end up iterating over non-eligible zones endlessly.
3585 */
3586 ac->preferred_zoneref = first_zones_zonelist(ac->zonelist,
3587 ac->high_zoneidx, ac->nodemask);
3588 if (!ac->preferred_zoneref->zone)
3589 goto nopage;
3590
Vlastimil Babkab678e4f2017-01-24 15:18:38 -08003591
Vlastimil Babka31a6c192016-07-28 15:49:13 -07003592 /*
3593 * The fast path uses conservative alloc_flags to succeed only until
3594 * kswapd needs to be woken up, and to avoid the cost of setting up
3595 * alloc_flags precisely. So we do that now.
3596 */
3597 alloc_flags = gfp_to_alloc_flags(gfp_mask);
3598
Mel Gormand0164ad2015-11-06 16:28:21 -08003599 if (gfp_mask & __GFP_KSWAPD_RECLAIM)
Vlastimil Babkaa9263752015-02-11 15:25:41 -08003600 wake_all_kswapds(order, ac);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08003601
Paul Jackson9bf22292005-09-06 15:18:12 -07003602 /*
Vlastimil Babka23771232016-07-28 15:49:16 -07003603 * The adjusted alloc_flags might result in immediate success, so try
3604 * that first
3605 */
3606 page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac);
3607 if (page)
3608 goto got_pg;
3609
Vlastimil Babkaa8161d12016-07-28 15:49:19 -07003610 /*
3611 * For costly allocations, try direct compaction first, as it's likely
3612 * that we have enough base pages and don't need to reclaim. Don't try
3613 * that for allocations that are allowed to ignore watermarks, as the
3614 * ALLOC_NO_WATERMARKS attempt didn't yet happen.
3615 */
3616 if (can_direct_reclaim && order > PAGE_ALLOC_COSTLY_ORDER &&
3617 !gfp_pfmemalloc_allowed(gfp_mask)) {
3618 page = __alloc_pages_direct_compact(gfp_mask, order,
3619 alloc_flags, ac,
Vlastimil Babkaa5508cd2016-07-28 15:49:28 -07003620 INIT_COMPACT_PRIORITY,
Vlastimil Babkaa8161d12016-07-28 15:49:19 -07003621 &compact_result);
3622 if (page)
3623 goto got_pg;
3624
Vlastimil Babka3eb27712016-07-28 15:49:22 -07003625 /*
3626 * Checks for costly allocations with __GFP_NORETRY, which
3627 * includes THP page fault allocations
3628 */
3629 if (gfp_mask & __GFP_NORETRY) {
Vlastimil Babkaa8161d12016-07-28 15:49:19 -07003630 /*
3631 * If compaction is deferred for high-order allocations,
3632 * it is because sync compaction recently failed. If
3633 * this is the case and the caller requested a THP
3634 * allocation, we do not want to heavily disrupt the
3635 * system, so we fail the allocation instead of entering
3636 * direct reclaim.
3637 */
3638 if (compact_result == COMPACT_DEFERRED)
3639 goto nopage;
3640
3641 /*
Vlastimil Babka3eb27712016-07-28 15:49:22 -07003642 * Looks like reclaim/compaction is worth trying, but
3643 * sync compaction could be very expensive, so keep
Vlastimil Babka25160352016-07-28 15:49:25 -07003644 * using async compaction.
Vlastimil Babkaa8161d12016-07-28 15:49:19 -07003645 */
Vlastimil Babkaa5508cd2016-07-28 15:49:28 -07003646 compact_priority = INIT_COMPACT_PRIORITY;
Vlastimil Babkaa8161d12016-07-28 15:49:19 -07003647 }
3648 }
Vlastimil Babka23771232016-07-28 15:49:16 -07003649
3650retry:
3651 /* Ensure kswapd doesn't accidentally go to sleep as long as we loop */
3652 if (gfp_mask & __GFP_KSWAPD_RECLAIM)
3653 wake_all_kswapds(order, ac);
3654
3655 if (gfp_pfmemalloc_allowed(gfp_mask))
3656 alloc_flags = ALLOC_NO_WATERMARKS;
3657
3658 /*
Mel Gormane46e7b72016-06-03 14:56:01 -07003659 * Reset the zonelist iterators if memory policies can be ignored.
3660 * These allocations are high priority and system rather than user
3661 * orientated.
3662 */
Vlastimil Babka23771232016-07-28 15:49:16 -07003663 if (!(alloc_flags & ALLOC_CPUSET) || (alloc_flags & ALLOC_NO_WATERMARKS)) {
Mel Gormane46e7b72016-06-03 14:56:01 -07003664 ac->preferred_zoneref = first_zones_zonelist(ac->zonelist,
3665 ac->high_zoneidx, ac->nodemask);
3666 }
3667
Vlastimil Babka23771232016-07-28 15:49:16 -07003668 /* Attempt with potentially adjusted zonelist and alloc_flags */
Vlastimil Babka31a6c192016-07-28 15:49:13 -07003669 page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08003670 if (page)
3671 goto got_pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672
Mel Gormand0164ad2015-11-06 16:28:21 -08003673 /* Caller is not willing to reclaim, we can't balance anything */
3674 if (!can_direct_reclaim) {
David Rientjesaed0a0e2014-01-21 15:51:12 -08003675 /*
Michal Hocko33d53102016-01-14 15:19:05 -08003676 * All existing users of the __GFP_NOFAIL are blockable, so warn
3677 * of any new users that actually allow this type of allocation
3678 * to fail.
David Rientjesaed0a0e2014-01-21 15:51:12 -08003679 */
3680 WARN_ON_ONCE(gfp_mask & __GFP_NOFAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 goto nopage;
David Rientjesaed0a0e2014-01-21 15:51:12 -08003682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683
Peter Zijlstra341ce062009-06-16 15:32:02 -07003684 /* Avoid recursion of direct reclaim */
Michal Hocko33d53102016-01-14 15:19:05 -08003685 if (current->flags & PF_MEMALLOC) {
3686 /*
3687 * __GFP_NOFAIL request from this context is rather bizarre
3688 * because we cannot reclaim anything and only can loop waiting
3689 * for somebody to do a work for us.
3690 */
3691 if (WARN_ON_ONCE(gfp_mask & __GFP_NOFAIL)) {
3692 cond_resched();
3693 goto retry;
3694 }
Peter Zijlstra341ce062009-06-16 15:32:02 -07003695 goto nopage;
Michal Hocko33d53102016-01-14 15:19:05 -08003696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697
David Rientjes6583bb62009-07-29 15:02:06 -07003698 /* Avoid allocations with no watermarks from looping endlessly */
3699 if (test_thread_flag(TIF_MEMDIE) && !(gfp_mask & __GFP_NOFAIL))
3700 goto nopage;
3701
David Rientjes8fe78042014-08-06 16:07:54 -07003702
Mel Gorman11e33f62009-06-16 15:31:57 -07003703 /* Try direct reclaim and then allocating */
Vlastimil Babkaa9263752015-02-11 15:25:41 -08003704 page = __alloc_pages_direct_reclaim(gfp_mask, order, alloc_flags, ac,
3705 &did_some_progress);
Mel Gorman11e33f62009-06-16 15:31:57 -07003706 if (page)
3707 goto got_pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708
Vlastimil Babkaa8161d12016-07-28 15:49:19 -07003709 /* Try direct compaction and then allocating */
3710 page = __alloc_pages_direct_compact(gfp_mask, order, alloc_flags, ac,
Vlastimil Babkaa5508cd2016-07-28 15:49:28 -07003711 compact_priority, &compact_result);
Vlastimil Babkaa8161d12016-07-28 15:49:19 -07003712 if (page)
3713 goto got_pg;
3714
Johannes Weiner90839052015-06-24 16:57:21 -07003715 /* Do not loop if specifically requested */
3716 if (gfp_mask & __GFP_NORETRY)
Vlastimil Babkaa8161d12016-07-28 15:49:19 -07003717 goto nopage;
Johannes Weiner90839052015-06-24 16:57:21 -07003718
Michal Hocko0a0337e2016-05-20 16:57:00 -07003719 /*
3720 * Do not retry costly high order allocations unless they are
3721 * __GFP_REPEAT
3722 */
3723 if (order > PAGE_ALLOC_COSTLY_ORDER && !(gfp_mask & __GFP_REPEAT))
Vlastimil Babkaa8161d12016-07-28 15:49:19 -07003724 goto nopage;
Michal Hocko0a0337e2016-05-20 16:57:00 -07003725
Michal Hocko0a0337e2016-05-20 16:57:00 -07003726 if (should_reclaim_retry(gfp_mask, order, ac, alloc_flags,
Vlastimil Babka423b4522016-10-07 17:00:40 -07003727 did_some_progress > 0, &no_progress_loops))
Michal Hocko0a0337e2016-05-20 16:57:00 -07003728 goto retry;
3729
Michal Hocko33c2d212016-05-20 16:57:06 -07003730 /*
3731 * It doesn't make any sense to retry for the compaction if the order-0
3732 * reclaim is not able to make any progress because the current
3733 * implementation of the compaction depends on the sufficient amount
3734 * of free memory (see __compaction_suitable)
3735 */
3736 if (did_some_progress > 0 &&
Michal Hocko86a294a2016-05-20 16:57:12 -07003737 should_compact_retry(ac, order, alloc_flags,
Vlastimil Babkaa5508cd2016-07-28 15:49:28 -07003738 compact_result, &compact_priority,
Vlastimil Babkad9436492016-10-07 17:00:31 -07003739 &compaction_retries))
Michal Hocko33c2d212016-05-20 16:57:06 -07003740 goto retry;
3741
Vlastimil Babka96e5cec2017-01-24 15:18:41 -08003742 /*
3743 * It's possible we raced with cpuset update so the OOM would be
3744 * premature (see below the nopage: label for full explanation).
3745 */
3746 if (read_mems_allowed_retry(cpuset_mems_cookie))
3747 goto retry_cpuset;
3748
Johannes Weiner90839052015-06-24 16:57:21 -07003749 /* Reclaim has failed us, start killing things */
3750 page = __alloc_pages_may_oom(gfp_mask, order, ac, &did_some_progress);
3751 if (page)
3752 goto got_pg;
3753
3754 /* Retry as long as the OOM killer is making progress */
Michal Hocko0a0337e2016-05-20 16:57:00 -07003755 if (did_some_progress) {
3756 no_progress_loops = 0;
Johannes Weiner90839052015-06-24 16:57:21 -07003757 goto retry;
Michal Hocko0a0337e2016-05-20 16:57:00 -07003758 }
Johannes Weiner90839052015-06-24 16:57:21 -07003759
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760nopage:
Vlastimil Babkab678e4f2017-01-24 15:18:38 -08003761 /*
Vlastimil Babka96e5cec2017-01-24 15:18:41 -08003762 * When updating a task's mems_allowed or mempolicy nodemask, it is
3763 * possible to race with parallel threads in such a way that our
3764 * allocation can fail while the mask is being updated. If we are about
3765 * to fail, check if the cpuset changed during allocation and if so,
3766 * retry.
Vlastimil Babkab678e4f2017-01-24 15:18:38 -08003767 */
3768 if (read_mems_allowed_retry(cpuset_mems_cookie))
3769 goto retry_cpuset;
3770
Michal Hocko7877cdc2016-10-07 17:01:55 -07003771 warn_alloc(gfp_mask,
3772 "page allocation failure: order:%u", order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773got_pg:
Mel Gorman072bb0a2012-07-31 16:43:58 -07003774 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775}
Mel Gorman11e33f62009-06-16 15:31:57 -07003776
3777/*
3778 * This is the 'heart' of the zoned buddy allocator.
3779 */
3780struct page *
3781__alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order,
3782 struct zonelist *zonelist, nodemask_t *nodemask)
3783{
Mel Gorman5bb1b162016-05-19 17:13:50 -07003784 struct page *page;
Mel Gormane6cbd7f2016-07-28 15:46:50 -07003785 unsigned int alloc_flags = ALLOC_WMARK_LOW;
Mel Gorman83d4ca82016-05-19 17:13:56 -07003786 gfp_t alloc_mask = gfp_mask; /* The gfp_t that was actually used for allocation */
Vlastimil Babkaa9263752015-02-11 15:25:41 -08003787 struct alloc_context ac = {
3788 .high_zoneidx = gfp_zone(gfp_mask),
Mel Gorman682a3382016-05-19 17:13:30 -07003789 .zonelist = zonelist,
Vlastimil Babkaa9263752015-02-11 15:25:41 -08003790 .nodemask = nodemask,
3791 .migratetype = gfpflags_to_migratetype(gfp_mask),
3792 };
Mel Gorman11e33f62009-06-16 15:31:57 -07003793
Mel Gorman682a3382016-05-19 17:13:30 -07003794 if (cpusets_enabled()) {
Mel Gorman83d4ca82016-05-19 17:13:56 -07003795 alloc_mask |= __GFP_HARDWALL;
Mel Gorman682a3382016-05-19 17:13:30 -07003796 alloc_flags |= ALLOC_CPUSET;
3797 if (!ac.nodemask)
3798 ac.nodemask = &cpuset_current_mems_allowed;
3799 }
3800
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +10003801 gfp_mask &= gfp_allowed_mask;
3802
Mel Gorman11e33f62009-06-16 15:31:57 -07003803 lockdep_trace_alloc(gfp_mask);
3804
Mel Gormand0164ad2015-11-06 16:28:21 -08003805 might_sleep_if(gfp_mask & __GFP_DIRECT_RECLAIM);
Mel Gorman11e33f62009-06-16 15:31:57 -07003806
3807 if (should_fail_alloc_page(gfp_mask, order))
3808 return NULL;
3809
3810 /*
3811 * Check the zones suitable for the gfp_mask contain at least one
3812 * valid zone. It's possible to have an empty zonelist as a result
David Rientjes4167e9b2015-04-14 15:46:55 -07003813 * of __GFP_THISNODE and a memoryless node
Mel Gorman11e33f62009-06-16 15:31:57 -07003814 */
3815 if (unlikely(!zonelist->_zonerefs->zone))
3816 return NULL;
3817
Vlastimil Babkaa9263752015-02-11 15:25:41 -08003818 if (IS_ENABLED(CONFIG_CMA) && ac.migratetype == MIGRATE_MOVABLE)
Vlastimil Babka21bb9bd2014-10-09 15:26:51 -07003819 alloc_flags |= ALLOC_CMA;
3820
Mel Gormanc9ab0c42015-11-06 16:28:12 -08003821 /* Dirty zone balancing only done in the fast path */
3822 ac.spread_dirty_pages = (gfp_mask & __GFP_WRITE);
3823
Mel Gormane46e7b72016-06-03 14:56:01 -07003824 /*
3825 * The preferred zone is used for statistics but crucially it is
3826 * also used as the starting point for the zonelist iterator. It
3827 * may get reset for allocations that ignore memory policies.
3828 */
Mel Gormanc33d6c02016-05-19 17:14:10 -07003829 ac.preferred_zoneref = first_zones_zonelist(ac.zonelist,
3830 ac.high_zoneidx, ac.nodemask);
Vlastimil Babkaade7afe2017-01-24 15:18:32 -08003831 if (!ac.preferred_zoneref->zone) {
Mel Gorman5bb1b162016-05-19 17:13:50 -07003832 page = NULL;
Vlastimil Babkab678e4f2017-01-24 15:18:38 -08003833 /*
3834 * This might be due to race with cpuset_current_mems_allowed
3835 * update, so make sure we retry with original nodemask in the
3836 * slow path.
3837 */
Mel Gorman4fcb0972016-05-19 17:14:01 -07003838 goto no_zone;
Mel Gorman5bb1b162016-05-19 17:13:50 -07003839 }
3840
Mel Gorman5117f452009-06-16 15:31:59 -07003841 /* First allocation attempt */
Vlastimil Babkaa9263752015-02-11 15:25:41 -08003842 page = get_page_from_freelist(alloc_mask, order, alloc_flags, &ac);
Mel Gorman4fcb0972016-05-19 17:14:01 -07003843 if (likely(page))
3844 goto out;
Andrew Morton91fbdc02015-02-11 15:25:04 -08003845
Vlastimil Babkab678e4f2017-01-24 15:18:38 -08003846no_zone:
Mel Gorman4fcb0972016-05-19 17:14:01 -07003847 /*
3848 * Runtime PM, block IO and its error handling path can deadlock
3849 * because I/O on the device might not complete.
3850 */
3851 alloc_mask = memalloc_noio_flags(gfp_mask);
3852 ac.spread_dirty_pages = false;
Mel Gorman11e33f62009-06-16 15:31:57 -07003853
Mel Gorman47415262016-05-19 17:14:44 -07003854 /*
3855 * Restore the original nodemask if it was potentially replaced with
3856 * &cpuset_current_mems_allowed to optimize the fast-path attempt.
3857 */
Vlastimil Babka96e5cec2017-01-24 15:18:41 -08003858 if (unlikely(ac.nodemask != nodemask))
Mel Gorman47415262016-05-19 17:14:44 -07003859 ac.nodemask = nodemask;
Vlastimil Babkad1656c52017-01-24 15:18:35 -08003860
Mel Gorman4fcb0972016-05-19 17:14:01 -07003861 page = __alloc_pages_slowpath(alloc_mask, order, &ac);
Xishi Qiu23f086f2015-02-11 15:25:07 -08003862
Mel Gorman4fcb0972016-05-19 17:14:01 -07003863out:
Vladimir Davydovc4159a72016-08-08 23:03:12 +03003864 if (memcg_kmem_enabled() && (gfp_mask & __GFP_ACCOUNT) && page &&
3865 unlikely(memcg_kmem_charge(page, gfp_mask, order) != 0)) {
3866 __free_pages(page, order);
3867 page = NULL;
Vladimir Davydov49491482016-07-26 15:24:24 -07003868 }
3869
Mel Gorman4fcb0972016-05-19 17:14:01 -07003870 if (kmemcheck_enabled && page)
3871 kmemcheck_pagealloc_alloc(page, order, gfp_mask);
3872
3873 trace_mm_page_alloc(page, order, alloc_mask, ac.migratetype);
3874
Mel Gorman11e33f62009-06-16 15:31:57 -07003875 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876}
Mel Gormand2391712009-06-16 15:31:52 -07003877EXPORT_SYMBOL(__alloc_pages_nodemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878
3879/*
3880 * Common helper functions.
3881 */
Harvey Harrison920c7a52008-02-04 22:29:26 -08003882unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883{
Akinobu Mita945a1112009-09-21 17:01:47 -07003884 struct page *page;
3885
3886 /*
3887 * __get_free_pages() returns a 32-bit address, which cannot represent
3888 * a highmem page
3889 */
3890 VM_BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
3891
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 page = alloc_pages(gfp_mask, order);
3893 if (!page)
3894 return 0;
3895 return (unsigned long) page_address(page);
3896}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897EXPORT_SYMBOL(__get_free_pages);
3898
Harvey Harrison920c7a52008-02-04 22:29:26 -08003899unsigned long get_zeroed_page(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900{
Akinobu Mita945a1112009-09-21 17:01:47 -07003901 return __get_free_pages(gfp_mask | __GFP_ZERO, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903EXPORT_SYMBOL(get_zeroed_page);
3904
Harvey Harrison920c7a52008-02-04 22:29:26 -08003905void __free_pages(struct page *page, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906{
Nick Pigginb5810032005-10-29 18:16:12 -07003907 if (put_page_testzero(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 if (order == 0)
Mel Gormanb745bc82014-06-04 16:10:22 -07003909 free_hot_cold_page(page, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 else
3911 __free_pages_ok(page, order);
3912 }
3913}
3914
3915EXPORT_SYMBOL(__free_pages);
3916
Harvey Harrison920c7a52008-02-04 22:29:26 -08003917void free_pages(unsigned long addr, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918{
3919 if (addr != 0) {
Nick Piggin725d7042006-09-25 23:30:55 -07003920 VM_BUG_ON(!virt_addr_valid((void *)addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 __free_pages(virt_to_page((void *)addr), order);
3922 }
3923}
3924
3925EXPORT_SYMBOL(free_pages);
3926
Glauber Costa6a1a0d32012-12-18 14:22:00 -08003927/*
Alexander Duyckb63ae8c2015-05-06 21:11:57 -07003928 * Page Fragment:
3929 * An arbitrary-length arbitrary-offset area of memory which resides
3930 * within a 0 or higher order page. Multiple fragments within that page
3931 * are individually refcounted, in the page's reference counter.
3932 *
3933 * The page_frag functions below provide a simple allocation framework for
3934 * page fragments. This is used by the network stack and network device
3935 * drivers to provide a backing region of memory for use as either an
3936 * sk_buff->head, or to be used in the "frags" portion of skb_shared_info.
3937 */
3938static struct page *__page_frag_refill(struct page_frag_cache *nc,
3939 gfp_t gfp_mask)
3940{
3941 struct page *page = NULL;
3942 gfp_t gfp = gfp_mask;
3943
3944#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
3945 gfp_mask |= __GFP_COMP | __GFP_NOWARN | __GFP_NORETRY |
3946 __GFP_NOMEMALLOC;
3947 page = alloc_pages_node(NUMA_NO_NODE, gfp_mask,
3948 PAGE_FRAG_CACHE_MAX_ORDER);
3949 nc->size = page ? PAGE_FRAG_CACHE_MAX_SIZE : PAGE_SIZE;
3950#endif
3951 if (unlikely(!page))
3952 page = alloc_pages_node(NUMA_NO_NODE, gfp, 0);
3953
3954 nc->va = page ? page_address(page) : NULL;
3955
3956 return page;
3957}
3958
3959void *__alloc_page_frag(struct page_frag_cache *nc,
3960 unsigned int fragsz, gfp_t gfp_mask)
3961{
3962 unsigned int size = PAGE_SIZE;
3963 struct page *page;
3964 int offset;
3965
3966 if (unlikely(!nc->va)) {
3967refill:
3968 page = __page_frag_refill(nc, gfp_mask);
3969 if (!page)
3970 return NULL;
3971
3972#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
3973 /* if size can vary use size else just use PAGE_SIZE */
3974 size = nc->size;
3975#endif
3976 /* Even if we own the page, we do not use atomic_set().
3977 * This would break get_page_unless_zero() users.
3978 */
Jann Horn484e89a2019-02-13 22:45:59 +01003979 page_ref_add(page, size);
Alexander Duyckb63ae8c2015-05-06 21:11:57 -07003980
3981 /* reset page count bias and offset to start of new frag */
Michal Hocko2f064f32015-08-21 14:11:51 -07003982 nc->pfmemalloc = page_is_pfmemalloc(page);
Jann Horn484e89a2019-02-13 22:45:59 +01003983 nc->pagecnt_bias = size + 1;
Alexander Duyckb63ae8c2015-05-06 21:11:57 -07003984 nc->offset = size;
3985 }
3986
3987 offset = nc->offset - fragsz;
3988 if (unlikely(offset < 0)) {
3989 page = virt_to_page(nc->va);
3990
Joonsoo Kimfe896d12016-03-17 14:19:26 -07003991 if (!page_ref_sub_and_test(page, nc->pagecnt_bias))
Alexander Duyckb63ae8c2015-05-06 21:11:57 -07003992 goto refill;
3993
3994#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
3995 /* if size can vary use size else just use PAGE_SIZE */
3996 size = nc->size;
3997#endif
3998 /* OK, page count is 0, we can safely set it */
Jann Horn484e89a2019-02-13 22:45:59 +01003999 set_page_count(page, size + 1);
Alexander Duyckb63ae8c2015-05-06 21:11:57 -07004000
4001 /* reset page count bias and offset to start of new frag */
Jann Horn484e89a2019-02-13 22:45:59 +01004002 nc->pagecnt_bias = size + 1;
Alexander Duyckb63ae8c2015-05-06 21:11:57 -07004003 offset = size - fragsz;
4004 }
4005
4006 nc->pagecnt_bias--;
4007 nc->offset = offset;
4008
4009 return nc->va + offset;
4010}
4011EXPORT_SYMBOL(__alloc_page_frag);
4012
4013/*
4014 * Frees a page fragment allocated out of either a compound or order 0 page.
4015 */
4016void __free_page_frag(void *addr)
4017{
4018 struct page *page = virt_to_head_page(addr);
4019
4020 if (unlikely(put_page_testzero(page)))
4021 __free_pages_ok(page, compound_order(page));
4022}
4023EXPORT_SYMBOL(__free_page_frag);
4024
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08004025static void *make_alloc_exact(unsigned long addr, unsigned int order,
4026 size_t size)
Andi Kleenee85c2e2011-05-11 15:13:34 -07004027{
4028 if (addr) {
4029 unsigned long alloc_end = addr + (PAGE_SIZE << order);
4030 unsigned long used = addr + PAGE_ALIGN(size);
4031
4032 split_page(virt_to_page((void *)addr), order);
4033 while (used < alloc_end) {
4034 free_page(used);
4035 used += PAGE_SIZE;
4036 }
4037 }
4038 return (void *)addr;
4039}
4040
Timur Tabi2be0ffe2008-07-23 21:28:11 -07004041/**
4042 * alloc_pages_exact - allocate an exact number physically-contiguous pages.
4043 * @size: the number of bytes to allocate
4044 * @gfp_mask: GFP flags for the allocation
4045 *
4046 * This function is similar to alloc_pages(), except that it allocates the
4047 * minimum number of pages to satisfy the request. alloc_pages() can only
4048 * allocate memory in power-of-two pages.
4049 *
4050 * This function is also limited by MAX_ORDER.
4051 *
4052 * Memory allocated by this function must be released by free_pages_exact().
4053 */
4054void *alloc_pages_exact(size_t size, gfp_t gfp_mask)
4055{
4056 unsigned int order = get_order(size);
4057 unsigned long addr;
4058
4059 addr = __get_free_pages(gfp_mask, order);
Andi Kleenee85c2e2011-05-11 15:13:34 -07004060 return make_alloc_exact(addr, order, size);
Timur Tabi2be0ffe2008-07-23 21:28:11 -07004061}
4062EXPORT_SYMBOL(alloc_pages_exact);
4063
4064/**
Andi Kleenee85c2e2011-05-11 15:13:34 -07004065 * alloc_pages_exact_nid - allocate an exact number of physically-contiguous
4066 * pages on a node.
Randy Dunlapb5e6ab52011-05-16 13:16:54 -07004067 * @nid: the preferred node ID where memory should be allocated
Andi Kleenee85c2e2011-05-11 15:13:34 -07004068 * @size: the number of bytes to allocate
4069 * @gfp_mask: GFP flags for the allocation
4070 *
4071 * Like alloc_pages_exact(), but try to allocate on node nid first before falling
4072 * back.
Andi Kleenee85c2e2011-05-11 15:13:34 -07004073 */
Fabian Fredericke1931812014-08-06 16:04:59 -07004074void * __meminit alloc_pages_exact_nid(int nid, size_t size, gfp_t gfp_mask)
Andi Kleenee85c2e2011-05-11 15:13:34 -07004075{
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08004076 unsigned int order = get_order(size);
Andi Kleenee85c2e2011-05-11 15:13:34 -07004077 struct page *p = alloc_pages_node(nid, gfp_mask, order);
4078 if (!p)
4079 return NULL;
4080 return make_alloc_exact((unsigned long)page_address(p), order, size);
4081}
Andi Kleenee85c2e2011-05-11 15:13:34 -07004082
4083/**
Timur Tabi2be0ffe2008-07-23 21:28:11 -07004084 * free_pages_exact - release memory allocated via alloc_pages_exact()
4085 * @virt: the value returned by alloc_pages_exact.
4086 * @size: size of allocation, same value as passed to alloc_pages_exact().
4087 *
4088 * Release the memory allocated by a previous call to alloc_pages_exact.
4089 */
4090void free_pages_exact(void *virt, size_t size)
4091{
4092 unsigned long addr = (unsigned long)virt;
4093 unsigned long end = addr + PAGE_ALIGN(size);
4094
4095 while (addr < end) {
4096 free_page(addr);
4097 addr += PAGE_SIZE;
4098 }
4099}
4100EXPORT_SYMBOL(free_pages_exact);
4101
Zhang Yanfeie0fb5812013-02-22 16:35:54 -08004102/**
4103 * nr_free_zone_pages - count number of pages beyond high watermark
4104 * @offset: The zone index of the highest zone
4105 *
4106 * nr_free_zone_pages() counts the number of counts pages which are beyond the
4107 * high watermark within all zones at or below a given zone index. For each
4108 * zone, the number of pages is calculated as:
Jiang Liu834405c2013-07-03 15:03:04 -07004109 * managed_pages - high_pages
Zhang Yanfeie0fb5812013-02-22 16:35:54 -08004110 */
Zhang Yanfeiebec3862013-02-22 16:35:43 -08004111static unsigned long nr_free_zone_pages(int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112{
Mel Gormandd1a2392008-04-28 02:12:17 -07004113 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07004114 struct zone *zone;
4115
Martin J. Blighe310fd42005-07-29 22:59:18 -07004116 /* Just pick one node, since fallback list is circular */
Zhang Yanfeiebec3862013-02-22 16:35:43 -08004117 unsigned long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118
Mel Gorman0e884602008-04-28 02:12:14 -07004119 struct zonelist *zonelist = node_zonelist(numa_node_id(), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120
Mel Gorman54a6eb52008-04-28 02:12:16 -07004121 for_each_zone_zonelist(zone, z, zonelist, offset) {
Jiang Liub40da042013-02-22 16:33:52 -08004122 unsigned long size = zone->managed_pages;
Mel Gorman41858962009-06-16 15:32:12 -07004123 unsigned long high = high_wmark_pages(zone);
Martin J. Blighe310fd42005-07-29 22:59:18 -07004124 if (size > high)
4125 sum += size - high;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 }
4127
4128 return sum;
4129}
4130
Zhang Yanfeie0fb5812013-02-22 16:35:54 -08004131/**
4132 * nr_free_buffer_pages - count number of pages beyond high watermark
4133 *
4134 * nr_free_buffer_pages() counts the number of pages which are beyond the high
4135 * watermark within ZONE_DMA and ZONE_NORMAL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 */
Zhang Yanfeiebec3862013-02-22 16:35:43 -08004137unsigned long nr_free_buffer_pages(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138{
Al Viroaf4ca452005-10-21 02:55:38 -04004139 return nr_free_zone_pages(gfp_zone(GFP_USER));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140}
Meelap Shahc2f1a552007-07-17 04:04:39 -07004141EXPORT_SYMBOL_GPL(nr_free_buffer_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142
Zhang Yanfeie0fb5812013-02-22 16:35:54 -08004143/**
4144 * nr_free_pagecache_pages - count number of pages beyond high watermark
4145 *
4146 * nr_free_pagecache_pages() counts the number of pages which are beyond the
4147 * high watermark within all zones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 */
Zhang Yanfeiebec3862013-02-22 16:35:43 -08004149unsigned long nr_free_pagecache_pages(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150{
Mel Gorman2a1e2742007-07-17 04:03:12 -07004151 return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER_MOVABLE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152}
Christoph Lameter08e0f6a2006-09-27 01:50:06 -07004153
4154static inline void show_node(struct zone *zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155{
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08004156 if (IS_ENABLED(CONFIG_NUMA))
Andy Whitcroft25ba77c2006-12-06 20:33:03 -08004157 printk("Node %d ", zone_to_nid(zone));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159
Igor Redkod02bd272016-03-17 14:19:05 -07004160long si_mem_available(void)
4161{
4162 long available;
4163 unsigned long pagecache;
4164 unsigned long wmark_low = 0;
4165 unsigned long pages[NR_LRU_LISTS];
4166 struct zone *zone;
4167 int lru;
4168
4169 for (lru = LRU_BASE; lru < NR_LRU_LISTS; lru++)
Mel Gorman2f95ff92016-08-11 15:32:57 -07004170 pages[lru] = global_node_page_state(NR_LRU_BASE + lru);
Igor Redkod02bd272016-03-17 14:19:05 -07004171
4172 for_each_zone(zone)
4173 wmark_low += zone->watermark[WMARK_LOW];
4174
4175 /*
4176 * Estimate the amount of memory available for userspace allocations,
4177 * without causing swapping.
4178 */
4179 available = global_page_state(NR_FREE_PAGES) - totalreserve_pages;
4180
4181 /*
4182 * Not all the page cache can be freed, otherwise the system will
4183 * start swapping. Assume at least half of the page cache, or the
4184 * low watermark worth of cache, needs to stay.
4185 */
4186 pagecache = pages[LRU_ACTIVE_FILE] + pages[LRU_INACTIVE_FILE];
4187 pagecache -= min(pagecache / 2, wmark_low);
4188 available += pagecache;
4189
4190 /*
4191 * Part of the reclaimable slab consists of items that are in use,
4192 * and cannot be freed. Cap this estimate at the low watermark.
4193 */
4194 available += global_page_state(NR_SLAB_RECLAIMABLE) -
4195 min(global_page_state(NR_SLAB_RECLAIMABLE) / 2, wmark_low);
4196
4197 if (available < 0)
4198 available = 0;
4199 return available;
4200}
4201EXPORT_SYMBOL_GPL(si_mem_available);
4202
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203void si_meminfo(struct sysinfo *val)
4204{
4205 val->totalram = totalram_pages;
Mel Gorman11fb9982016-07-28 15:46:20 -07004206 val->sharedram = global_node_page_state(NR_SHMEM);
Christoph Lameterd23ad422007-02-10 01:43:02 -08004207 val->freeram = global_page_state(NR_FREE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 val->bufferram = nr_blockdev_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 val->totalhigh = totalhigh_pages;
4210 val->freehigh = nr_free_highpages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 val->mem_unit = PAGE_SIZE;
4212}
4213
4214EXPORT_SYMBOL(si_meminfo);
4215
4216#ifdef CONFIG_NUMA
4217void si_meminfo_node(struct sysinfo *val, int nid)
4218{
Jiang Liucdd91a72013-07-03 15:03:27 -07004219 int zone_type; /* needs to be signed */
4220 unsigned long managed_pages = 0;
Joonsoo Kimfc2bd792016-05-19 17:12:23 -07004221 unsigned long managed_highpages = 0;
4222 unsigned long free_highpages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 pg_data_t *pgdat = NODE_DATA(nid);
4224
Jiang Liucdd91a72013-07-03 15:03:27 -07004225 for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++)
4226 managed_pages += pgdat->node_zones[zone_type].managed_pages;
4227 val->totalram = managed_pages;
Mel Gorman11fb9982016-07-28 15:46:20 -07004228 val->sharedram = node_page_state(pgdat, NR_SHMEM);
Mel Gorman75ef7182016-07-28 15:45:24 -07004229 val->freeram = sum_zone_node_page_state(nid, NR_FREE_PAGES);
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07004230#ifdef CONFIG_HIGHMEM
Joonsoo Kimfc2bd792016-05-19 17:12:23 -07004231 for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
4232 struct zone *zone = &pgdat->node_zones[zone_type];
4233
4234 if (is_highmem(zone)) {
4235 managed_highpages += zone->managed_pages;
4236 free_highpages += zone_page_state(zone, NR_FREE_PAGES);
4237 }
4238 }
4239 val->totalhigh = managed_highpages;
4240 val->freehigh = free_highpages;
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07004241#else
Joonsoo Kimfc2bd792016-05-19 17:12:23 -07004242 val->totalhigh = managed_highpages;
4243 val->freehigh = free_highpages;
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07004244#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 val->mem_unit = PAGE_SIZE;
4246}
4247#endif
4248
David Rientjesddd588b2011-03-22 16:30:46 -07004249/*
David Rientjes7bf02ea2011-05-24 17:11:16 -07004250 * Determine whether the node should be displayed or not, depending on whether
4251 * SHOW_MEM_FILTER_NODES was passed to show_free_areas().
David Rientjesddd588b2011-03-22 16:30:46 -07004252 */
David Rientjes7bf02ea2011-05-24 17:11:16 -07004253bool skip_free_areas_node(unsigned int flags, int nid)
David Rientjesddd588b2011-03-22 16:30:46 -07004254{
4255 bool ret = false;
Mel Gormancc9a6c82012-03-21 16:34:11 -07004256 unsigned int cpuset_mems_cookie;
David Rientjesddd588b2011-03-22 16:30:46 -07004257
4258 if (!(flags & SHOW_MEM_FILTER_NODES))
4259 goto out;
4260
Mel Gormancc9a6c82012-03-21 16:34:11 -07004261 do {
Mel Gormand26914d2014-04-03 14:47:24 -07004262 cpuset_mems_cookie = read_mems_allowed_begin();
Mel Gormancc9a6c82012-03-21 16:34:11 -07004263 ret = !node_isset(nid, cpuset_current_mems_allowed);
Mel Gormand26914d2014-04-03 14:47:24 -07004264 } while (read_mems_allowed_retry(cpuset_mems_cookie));
David Rientjesddd588b2011-03-22 16:30:46 -07004265out:
4266 return ret;
4267}
4268
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269#define K(x) ((x) << (PAGE_SHIFT-10))
4270
Rabin Vincent377e4f12012-12-11 16:00:24 -08004271static void show_migration_types(unsigned char type)
4272{
4273 static const char types[MIGRATE_TYPES] = {
4274 [MIGRATE_UNMOVABLE] = 'U',
Rabin Vincent377e4f12012-12-11 16:00:24 -08004275 [MIGRATE_MOVABLE] = 'M',
Vlastimil Babka475a2f92015-12-11 13:40:29 -08004276 [MIGRATE_RECLAIMABLE] = 'E',
4277 [MIGRATE_HIGHATOMIC] = 'H',
Rabin Vincent377e4f12012-12-11 16:00:24 -08004278#ifdef CONFIG_CMA
4279 [MIGRATE_CMA] = 'C',
4280#endif
Minchan Kim194159f2013-02-22 16:33:58 -08004281#ifdef CONFIG_MEMORY_ISOLATION
Rabin Vincent377e4f12012-12-11 16:00:24 -08004282 [MIGRATE_ISOLATE] = 'I',
Minchan Kim194159f2013-02-22 16:33:58 -08004283#endif
Rabin Vincent377e4f12012-12-11 16:00:24 -08004284 };
4285 char tmp[MIGRATE_TYPES + 1];
4286 char *p = tmp;
4287 int i;
4288
4289 for (i = 0; i < MIGRATE_TYPES; i++) {
4290 if (type & (1 << i))
4291 *p++ = types[i];
4292 }
4293
4294 *p = '\0';
Joe Perches1f84a182016-10-27 17:46:29 -07004295 printk(KERN_CONT "(%s) ", tmp);
Rabin Vincent377e4f12012-12-11 16:00:24 -08004296}
4297
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298/*
4299 * Show free area list (used inside shift_scroll-lock stuff)
4300 * We also calculate the percentage fragmentation. We do this by counting the
4301 * memory on each free list with the exception of the first item on the list.
Konstantin Khlebnikovd1bfcdb2015-04-14 15:45:30 -07004302 *
4303 * Bits in @filter:
4304 * SHOW_MEM_FILTER_NODES: suppress nodes that are not allowed by current's
4305 * cpuset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 */
David Rientjes7bf02ea2011-05-24 17:11:16 -07004307void show_free_areas(unsigned int filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308{
Konstantin Khlebnikovd1bfcdb2015-04-14 15:45:30 -07004309 unsigned long free_pcp = 0;
Jes Sorensenc7241912006-09-27 01:50:05 -07004310 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 struct zone *zone;
Mel Gorman599d0c92016-07-28 15:45:31 -07004312 pg_data_t *pgdat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313
KOSAKI Motohiroee99c712009-03-31 15:19:31 -07004314 for_each_populated_zone(zone) {
David Rientjes7bf02ea2011-05-24 17:11:16 -07004315 if (skip_free_areas_node(filter, zone_to_nid(zone)))
David Rientjesddd588b2011-03-22 16:30:46 -07004316 continue;
Konstantin Khlebnikovd1bfcdb2015-04-14 15:45:30 -07004317
Konstantin Khlebnikov761b0672015-04-14 15:45:32 -07004318 for_each_online_cpu(cpu)
4319 free_pcp += per_cpu_ptr(zone->pageset, cpu)->pcp.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 }
4321
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07004322 printk("active_anon:%lu inactive_anon:%lu isolated_anon:%lu\n"
4323 " active_file:%lu inactive_file:%lu isolated_file:%lu\n"
Konstantin Khlebnikovd1bfcdb2015-04-14 15:45:30 -07004324 " unevictable:%lu dirty:%lu writeback:%lu unstable:%lu\n"
4325 " slab_reclaimable:%lu slab_unreclaimable:%lu\n"
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -07004326 " mapped:%lu shmem:%lu pagetables:%lu bounce:%lu\n"
Konstantin Khlebnikovd1bfcdb2015-04-14 15:45:30 -07004327 " free:%lu free_pcp:%lu free_cma:%lu\n",
Mel Gorman599d0c92016-07-28 15:45:31 -07004328 global_node_page_state(NR_ACTIVE_ANON),
4329 global_node_page_state(NR_INACTIVE_ANON),
4330 global_node_page_state(NR_ISOLATED_ANON),
4331 global_node_page_state(NR_ACTIVE_FILE),
4332 global_node_page_state(NR_INACTIVE_FILE),
4333 global_node_page_state(NR_ISOLATED_FILE),
4334 global_node_page_state(NR_UNEVICTABLE),
Mel Gorman11fb9982016-07-28 15:46:20 -07004335 global_node_page_state(NR_FILE_DIRTY),
4336 global_node_page_state(NR_WRITEBACK),
4337 global_node_page_state(NR_UNSTABLE_NFS),
KOSAKI Motohiro3701b032009-09-21 17:01:29 -07004338 global_page_state(NR_SLAB_RECLAIMABLE),
4339 global_page_state(NR_SLAB_UNRECLAIMABLE),
Mel Gorman50658e22016-07-28 15:46:14 -07004340 global_node_page_state(NR_FILE_MAPPED),
Mel Gorman11fb9982016-07-28 15:46:20 -07004341 global_node_page_state(NR_SHMEM),
Andrew Mortona25700a2007-02-08 14:20:40 -08004342 global_page_state(NR_PAGETABLE),
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -07004343 global_page_state(NR_BOUNCE),
Konstantin Khlebnikovd1bfcdb2015-04-14 15:45:30 -07004344 global_page_state(NR_FREE_PAGES),
4345 free_pcp,
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -07004346 global_page_state(NR_FREE_CMA_PAGES));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347
Mel Gorman599d0c92016-07-28 15:45:31 -07004348 for_each_online_pgdat(pgdat) {
4349 printk("Node %d"
4350 " active_anon:%lukB"
4351 " inactive_anon:%lukB"
4352 " active_file:%lukB"
4353 " inactive_file:%lukB"
4354 " unevictable:%lukB"
4355 " isolated(anon):%lukB"
4356 " isolated(file):%lukB"
Mel Gorman50658e22016-07-28 15:46:14 -07004357 " mapped:%lukB"
Mel Gorman11fb9982016-07-28 15:46:20 -07004358 " dirty:%lukB"
4359 " writeback:%lukB"
4360 " shmem:%lukB"
4361#ifdef CONFIG_TRANSPARENT_HUGEPAGE
4362 " shmem_thp: %lukB"
4363 " shmem_pmdmapped: %lukB"
4364 " anon_thp: %lukB"
4365#endif
4366 " writeback_tmp:%lukB"
4367 " unstable:%lukB"
Minchan Kim33e077b2016-07-28 15:47:14 -07004368 " pages_scanned:%lu"
Mel Gorman599d0c92016-07-28 15:45:31 -07004369 " all_unreclaimable? %s"
4370 "\n",
4371 pgdat->node_id,
4372 K(node_page_state(pgdat, NR_ACTIVE_ANON)),
4373 K(node_page_state(pgdat, NR_INACTIVE_ANON)),
4374 K(node_page_state(pgdat, NR_ACTIVE_FILE)),
4375 K(node_page_state(pgdat, NR_INACTIVE_FILE)),
4376 K(node_page_state(pgdat, NR_UNEVICTABLE)),
4377 K(node_page_state(pgdat, NR_ISOLATED_ANON)),
4378 K(node_page_state(pgdat, NR_ISOLATED_FILE)),
Mel Gorman50658e22016-07-28 15:46:14 -07004379 K(node_page_state(pgdat, NR_FILE_MAPPED)),
Mel Gorman11fb9982016-07-28 15:46:20 -07004380 K(node_page_state(pgdat, NR_FILE_DIRTY)),
4381 K(node_page_state(pgdat, NR_WRITEBACK)),
Alexander Polakove3b08eb2017-04-07 16:04:45 -07004382 K(node_page_state(pgdat, NR_SHMEM)),
Mel Gorman11fb9982016-07-28 15:46:20 -07004383#ifdef CONFIG_TRANSPARENT_HUGEPAGE
4384 K(node_page_state(pgdat, NR_SHMEM_THPS) * HPAGE_PMD_NR),
4385 K(node_page_state(pgdat, NR_SHMEM_PMDMAPPED)
4386 * HPAGE_PMD_NR),
4387 K(node_page_state(pgdat, NR_ANON_THPS) * HPAGE_PMD_NR),
4388#endif
Mel Gorman11fb9982016-07-28 15:46:20 -07004389 K(node_page_state(pgdat, NR_WRITEBACK_TEMP)),
4390 K(node_page_state(pgdat, NR_UNSTABLE_NFS)),
Minchan Kim33e077b2016-07-28 15:47:14 -07004391 node_page_state(pgdat, NR_PAGES_SCANNED),
Johannes Weiner19a7db12017-05-03 14:51:51 -07004392 pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES ?
4393 "yes" : "no");
Mel Gorman599d0c92016-07-28 15:45:31 -07004394 }
4395
KOSAKI Motohiroee99c712009-03-31 15:19:31 -07004396 for_each_populated_zone(zone) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 int i;
4398
David Rientjes7bf02ea2011-05-24 17:11:16 -07004399 if (skip_free_areas_node(filter, zone_to_nid(zone)))
David Rientjesddd588b2011-03-22 16:30:46 -07004400 continue;
Konstantin Khlebnikovd1bfcdb2015-04-14 15:45:30 -07004401
4402 free_pcp = 0;
4403 for_each_online_cpu(cpu)
4404 free_pcp += per_cpu_ptr(zone->pageset, cpu)->pcp.count;
4405
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 show_node(zone);
Joe Perches1f84a182016-10-27 17:46:29 -07004407 printk(KERN_CONT
4408 "%s"
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 " free:%lukB"
4410 " min:%lukB"
4411 " low:%lukB"
4412 " high:%lukB"
Minchan Kim71c799f2016-07-28 15:47:26 -07004413 " active_anon:%lukB"
4414 " inactive_anon:%lukB"
4415 " active_file:%lukB"
4416 " inactive_file:%lukB"
4417 " unevictable:%lukB"
Mel Gorman5a1c84b2016-07-28 15:47:31 -07004418 " writepending:%lukB"
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 " present:%lukB"
Jiang Liu9feedc92012-12-12 13:52:12 -08004420 " managed:%lukB"
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07004421 " mlocked:%lukB"
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07004422 " slab_reclaimable:%lukB"
4423 " slab_unreclaimable:%lukB"
KOSAKI Motohiroc6a7f572009-09-21 17:01:32 -07004424 " kernel_stack:%lukB"
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07004425 " pagetables:%lukB"
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07004426 " bounce:%lukB"
Konstantin Khlebnikovd1bfcdb2015-04-14 15:45:30 -07004427 " free_pcp:%lukB"
4428 " local_pcp:%ukB"
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -07004429 " free_cma:%lukB"
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 "\n",
4431 zone->name,
Mel Gorman88f5acf2011-01-13 15:45:41 -08004432 K(zone_page_state(zone, NR_FREE_PAGES)),
Mel Gorman41858962009-06-16 15:32:12 -07004433 K(min_wmark_pages(zone)),
4434 K(low_wmark_pages(zone)),
4435 K(high_wmark_pages(zone)),
Minchan Kim71c799f2016-07-28 15:47:26 -07004436 K(zone_page_state(zone, NR_ZONE_ACTIVE_ANON)),
4437 K(zone_page_state(zone, NR_ZONE_INACTIVE_ANON)),
4438 K(zone_page_state(zone, NR_ZONE_ACTIVE_FILE)),
4439 K(zone_page_state(zone, NR_ZONE_INACTIVE_FILE)),
4440 K(zone_page_state(zone, NR_ZONE_UNEVICTABLE)),
Mel Gorman5a1c84b2016-07-28 15:47:31 -07004441 K(zone_page_state(zone, NR_ZONE_WRITE_PENDING)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 K(zone->present_pages),
Jiang Liu9feedc92012-12-12 13:52:12 -08004443 K(zone->managed_pages),
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07004444 K(zone_page_state(zone, NR_MLOCK)),
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07004445 K(zone_page_state(zone, NR_SLAB_RECLAIMABLE)),
4446 K(zone_page_state(zone, NR_SLAB_UNRECLAIMABLE)),
Andy Lutomirskid30dd8b2016-07-28 15:48:14 -07004447 zone_page_state(zone, NR_KERNEL_STACK_KB),
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07004448 K(zone_page_state(zone, NR_PAGETABLE)),
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07004449 K(zone_page_state(zone, NR_BOUNCE)),
Konstantin Khlebnikovd1bfcdb2015-04-14 15:45:30 -07004450 K(free_pcp),
4451 K(this_cpu_read(zone->pageset->pcp.count)),
Minchan Kim33e077b2016-07-28 15:47:14 -07004452 K(zone_page_state(zone, NR_FREE_CMA_PAGES)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 printk("lowmem_reserve[]:");
4454 for (i = 0; i < MAX_NR_ZONES; i++)
Joe Perches1f84a182016-10-27 17:46:29 -07004455 printk(KERN_CONT " %ld", zone->lowmem_reserve[i]);
4456 printk(KERN_CONT "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 }
4458
KOSAKI Motohiroee99c712009-03-31 15:19:31 -07004459 for_each_populated_zone(zone) {
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08004460 unsigned int order;
4461 unsigned long nr[MAX_ORDER], flags, total = 0;
Rabin Vincent377e4f12012-12-11 16:00:24 -08004462 unsigned char types[MAX_ORDER];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463
David Rientjes7bf02ea2011-05-24 17:11:16 -07004464 if (skip_free_areas_node(filter, zone_to_nid(zone)))
David Rientjesddd588b2011-03-22 16:30:46 -07004465 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 show_node(zone);
Joe Perches1f84a182016-10-27 17:46:29 -07004467 printk(KERN_CONT "%s: ", zone->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468
4469 spin_lock_irqsave(&zone->lock, flags);
4470 for (order = 0; order < MAX_ORDER; order++) {
Rabin Vincent377e4f12012-12-11 16:00:24 -08004471 struct free_area *area = &zone->free_area[order];
4472 int type;
4473
4474 nr[order] = area->nr_free;
Kirill Korotaev8f9de512006-06-23 02:03:50 -07004475 total += nr[order] << order;
Rabin Vincent377e4f12012-12-11 16:00:24 -08004476
4477 types[order] = 0;
4478 for (type = 0; type < MIGRATE_TYPES; type++) {
4479 if (!list_empty(&area->free_list[type]))
4480 types[order] |= 1 << type;
4481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 }
4483 spin_unlock_irqrestore(&zone->lock, flags);
Rabin Vincent377e4f12012-12-11 16:00:24 -08004484 for (order = 0; order < MAX_ORDER; order++) {
Joe Perches1f84a182016-10-27 17:46:29 -07004485 printk(KERN_CONT "%lu*%lukB ",
4486 nr[order], K(1UL) << order);
Rabin Vincent377e4f12012-12-11 16:00:24 -08004487 if (nr[order])
4488 show_migration_types(types[order]);
4489 }
Joe Perches1f84a182016-10-27 17:46:29 -07004490 printk(KERN_CONT "= %lukB\n", K(total));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 }
4492
David Rientjes949f7ec2013-04-29 15:07:48 -07004493 hugetlb_show_meminfo();
4494
Mel Gorman11fb9982016-07-28 15:46:20 -07004495 printk("%ld total pagecache pages\n", global_node_page_state(NR_FILE_PAGES));
Larry Woodmane6f36022008-02-04 22:29:30 -08004496
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 show_swap_cache_info();
4498}
4499
Mel Gorman19770b32008-04-28 02:12:18 -07004500static void zoneref_set_zone(struct zone *zone, struct zoneref *zoneref)
4501{
4502 zoneref->zone = zone;
4503 zoneref->zone_idx = zone_idx(zone);
4504}
4505
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506/*
4507 * Builds allocation fallback zone lists.
Christoph Lameter1a932052006-01-06 00:11:16 -08004508 *
4509 * Add all populated zones of a node to the zonelist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004511static int build_zonelists_node(pg_data_t *pgdat, struct zonelist *zonelist,
Zhang Yanfeibc732f12013-07-08 16:00:06 -07004512 int nr_zones)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513{
Christoph Lameter1a932052006-01-06 00:11:16 -08004514 struct zone *zone;
Zhang Yanfeibc732f12013-07-08 16:00:06 -07004515 enum zone_type zone_type = MAX_NR_ZONES;
Christoph Lameter02a68a52006-01-06 00:11:18 -08004516
4517 do {
Christoph Lameter2f6726e2006-09-25 23:31:18 -07004518 zone_type--;
Christoph Lameter070f8032006-01-06 00:11:19 -08004519 zone = pgdat->node_zones + zone_type;
Mel Gorman6aa303d2016-09-01 16:14:55 -07004520 if (managed_zone(zone)) {
Mel Gormandd1a2392008-04-28 02:12:17 -07004521 zoneref_set_zone(zone,
4522 &zonelist->_zonerefs[nr_zones++]);
Christoph Lameter070f8032006-01-06 00:11:19 -08004523 check_highest_zone(zone_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 }
Christoph Lameter2f6726e2006-09-25 23:31:18 -07004525 } while (zone_type);
Zhang Yanfeibc732f12013-07-08 16:00:06 -07004526
Christoph Lameter070f8032006-01-06 00:11:19 -08004527 return nr_zones;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528}
4529
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004530
4531/*
4532 * zonelist_order:
4533 * 0 = automatic detection of better ordering.
4534 * 1 = order by ([node] distance, -zonetype)
4535 * 2 = order by (-zonetype, [node] distance)
4536 *
4537 * If not NUMA, ZONELIST_ORDER_ZONE and ZONELIST_ORDER_NODE will create
4538 * the same zonelist. So only NUMA can configure this param.
4539 */
4540#define ZONELIST_ORDER_DEFAULT 0
4541#define ZONELIST_ORDER_NODE 1
4542#define ZONELIST_ORDER_ZONE 2
4543
4544/* zonelist order in the kernel.
4545 * set_zonelist_order() will set this to NODE or ZONE.
4546 */
4547static int current_zonelist_order = ZONELIST_ORDER_DEFAULT;
4548static char zonelist_order_name[3][8] = {"Default", "Node", "Zone"};
4549
4550
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551#ifdef CONFIG_NUMA
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004552/* The value user specified ....changed by config */
4553static int user_zonelist_order = ZONELIST_ORDER_DEFAULT;
4554/* string for sysctl */
4555#define NUMA_ZONELIST_ORDER_LEN 16
4556char numa_zonelist_order[16] = "default";
4557
4558/*
4559 * interface for configure zonelist ordering.
4560 * command line option "numa_zonelist_order"
4561 * = "[dD]efault - default, automatic configuration.
4562 * = "[nN]ode - order by node locality, then by zone within node
4563 * = "[zZ]one - order by zone, then by locality within zone
4564 */
4565
4566static int __parse_numa_zonelist_order(char *s)
4567{
4568 if (*s == 'd' || *s == 'D') {
4569 user_zonelist_order = ZONELIST_ORDER_DEFAULT;
4570 } else if (*s == 'n' || *s == 'N') {
4571 user_zonelist_order = ZONELIST_ORDER_NODE;
4572 } else if (*s == 'z' || *s == 'Z') {
4573 user_zonelist_order = ZONELIST_ORDER_ZONE;
4574 } else {
Joe Perches11705322016-03-17 14:19:50 -07004575 pr_warn("Ignoring invalid numa_zonelist_order value: %s\n", s);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004576 return -EINVAL;
4577 }
4578 return 0;
4579}
4580
4581static __init int setup_numa_zonelist_order(char *s)
4582{
Volodymyr G. Lukiianykecb256f2011-01-13 15:46:26 -08004583 int ret;
4584
4585 if (!s)
4586 return 0;
4587
4588 ret = __parse_numa_zonelist_order(s);
4589 if (ret == 0)
4590 strlcpy(numa_zonelist_order, s, NUMA_ZONELIST_ORDER_LEN);
4591
4592 return ret;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004593}
4594early_param("numa_zonelist_order", setup_numa_zonelist_order);
4595
4596/*
4597 * sysctl handler for numa_zonelist_order
4598 */
Joe Perchescccad5b2014-06-06 14:38:09 -07004599int numa_zonelist_order_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004600 void __user *buffer, size_t *length,
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004601 loff_t *ppos)
4602{
4603 char saved_string[NUMA_ZONELIST_ORDER_LEN];
4604 int ret;
Andi Kleen443c6f12009-12-23 21:00:47 +01004605 static DEFINE_MUTEX(zl_order_mutex);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004606
Andi Kleen443c6f12009-12-23 21:00:47 +01004607 mutex_lock(&zl_order_mutex);
Chen Gangdacbde02013-07-03 15:02:35 -07004608 if (write) {
4609 if (strlen((char *)table->data) >= NUMA_ZONELIST_ORDER_LEN) {
4610 ret = -EINVAL;
4611 goto out;
4612 }
4613 strcpy(saved_string, (char *)table->data);
4614 }
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004615 ret = proc_dostring(table, write, buffer, length, ppos);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004616 if (ret)
Andi Kleen443c6f12009-12-23 21:00:47 +01004617 goto out;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004618 if (write) {
4619 int oldval = user_zonelist_order;
Chen Gangdacbde02013-07-03 15:02:35 -07004620
4621 ret = __parse_numa_zonelist_order((char *)table->data);
4622 if (ret) {
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004623 /*
4624 * bogus value. restore saved string
4625 */
Chen Gangdacbde02013-07-03 15:02:35 -07004626 strncpy((char *)table->data, saved_string,
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004627 NUMA_ZONELIST_ORDER_LEN);
4628 user_zonelist_order = oldval;
Haicheng Li4eaf3f62010-05-24 14:32:52 -07004629 } else if (oldval != user_zonelist_order) {
4630 mutex_lock(&zonelists_mutex);
Jiang Liu9adb62a2012-07-31 16:43:28 -07004631 build_all_zonelists(NULL, NULL);
Haicheng Li4eaf3f62010-05-24 14:32:52 -07004632 mutex_unlock(&zonelists_mutex);
4633 }
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004634 }
Andi Kleen443c6f12009-12-23 21:00:47 +01004635out:
4636 mutex_unlock(&zl_order_mutex);
4637 return ret;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004638}
4639
4640
Christoph Lameter62bc62a2009-06-16 15:32:15 -07004641#define MAX_NODE_LOAD (nr_online_nodes)
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004642static int node_load[MAX_NUMNODES];
4643
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644/**
Pavel Pisa4dc3b162005-05-01 08:59:25 -07004645 * 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 -07004646 * @node: node whose fallback list we're appending
4647 * @used_node_mask: nodemask_t of already used nodes
4648 *
4649 * We use a number of factors to determine which is the next node that should
4650 * appear on a given node's fallback list. The node should not have appeared
4651 * already in @node's fallback list, and it should be the next closest node
4652 * according to the distance array (which contains arbitrary distance values
4653 * from each node to each node in the system), and should also prefer nodes
4654 * with no CPUs, since presumably they'll have very little allocation pressure
4655 * on them otherwise.
4656 * It returns -1 if no node is found.
4657 */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004658static int find_next_best_node(int node, nodemask_t *used_node_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659{
Linus Torvalds4cf808e2006-02-17 20:38:21 +01004660 int n, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 int min_val = INT_MAX;
David Rientjes00ef2d22013-02-22 16:35:36 -08004662 int best_node = NUMA_NO_NODE;
Rusty Russella70f7302009-03-13 14:49:46 +10304663 const struct cpumask *tmp = cpumask_of_node(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664
Linus Torvalds4cf808e2006-02-17 20:38:21 +01004665 /* Use the local node if we haven't already */
4666 if (!node_isset(node, *used_node_mask)) {
4667 node_set(node, *used_node_mask);
4668 return node;
4669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08004671 for_each_node_state(n, N_MEMORY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672
4673 /* Don't want a node to appear more than once */
4674 if (node_isset(n, *used_node_mask))
4675 continue;
4676
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 /* Use the distance array to find the distance */
4678 val = node_distance(node, n);
4679
Linus Torvalds4cf808e2006-02-17 20:38:21 +01004680 /* Penalize nodes under us ("prefer the next node") */
4681 val += (n < node);
4682
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 /* Give preference to headless and unused nodes */
Rusty Russella70f7302009-03-13 14:49:46 +10304684 tmp = cpumask_of_node(n);
4685 if (!cpumask_empty(tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 val += PENALTY_FOR_NODE_WITH_CPUS;
4687
4688 /* Slight preference for less loaded node */
4689 val *= (MAX_NODE_LOAD*MAX_NUMNODES);
4690 val += node_load[n];
4691
4692 if (val < min_val) {
4693 min_val = val;
4694 best_node = n;
4695 }
4696 }
4697
4698 if (best_node >= 0)
4699 node_set(best_node, *used_node_mask);
4700
4701 return best_node;
4702}
4703
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004704
4705/*
4706 * Build zonelists ordered by node and zones within node.
4707 * This results in maximum locality--normal zone overflows into local
4708 * DMA zone, if any--but risks exhausting DMA zone.
4709 */
4710static void build_zonelists_in_node_order(pg_data_t *pgdat, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711{
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004712 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 struct zonelist *zonelist;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004714
Aneesh Kumar K.Vc9634cf2016-10-07 16:59:12 -07004715 zonelist = &pgdat->node_zonelists[ZONELIST_FALLBACK];
Mel Gormandd1a2392008-04-28 02:12:17 -07004716 for (j = 0; zonelist->_zonerefs[j].zone != NULL; j++)
Mel Gorman54a6eb52008-04-28 02:12:16 -07004717 ;
Zhang Yanfeibc732f12013-07-08 16:00:06 -07004718 j = build_zonelists_node(NODE_DATA(node), zonelist, j);
Mel Gormandd1a2392008-04-28 02:12:17 -07004719 zonelist->_zonerefs[j].zone = NULL;
4720 zonelist->_zonerefs[j].zone_idx = 0;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004721}
4722
4723/*
Christoph Lameter523b9452007-10-16 01:25:37 -07004724 * Build gfp_thisnode zonelists
4725 */
4726static void build_thisnode_zonelists(pg_data_t *pgdat)
4727{
Christoph Lameter523b9452007-10-16 01:25:37 -07004728 int j;
4729 struct zonelist *zonelist;
4730
Aneesh Kumar K.Vc9634cf2016-10-07 16:59:12 -07004731 zonelist = &pgdat->node_zonelists[ZONELIST_NOFALLBACK];
Zhang Yanfeibc732f12013-07-08 16:00:06 -07004732 j = build_zonelists_node(pgdat, zonelist, 0);
Mel Gormandd1a2392008-04-28 02:12:17 -07004733 zonelist->_zonerefs[j].zone = NULL;
4734 zonelist->_zonerefs[j].zone_idx = 0;
Christoph Lameter523b9452007-10-16 01:25:37 -07004735}
4736
4737/*
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004738 * Build zonelists ordered by zone and nodes within zones.
4739 * This results in conserving DMA zone[s] until all Normal memory is
4740 * exhausted, but results in overflowing to remote node while memory
4741 * may still exist in local DMA zone.
4742 */
4743static int node_order[MAX_NUMNODES];
4744
4745static void build_zonelists_in_zone_order(pg_data_t *pgdat, int nr_nodes)
4746{
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004747 int pos, j, node;
4748 int zone_type; /* needs to be signed */
4749 struct zone *z;
4750 struct zonelist *zonelist;
4751
Aneesh Kumar K.Vc9634cf2016-10-07 16:59:12 -07004752 zonelist = &pgdat->node_zonelists[ZONELIST_FALLBACK];
Mel Gorman54a6eb52008-04-28 02:12:16 -07004753 pos = 0;
4754 for (zone_type = MAX_NR_ZONES - 1; zone_type >= 0; zone_type--) {
4755 for (j = 0; j < nr_nodes; j++) {
4756 node = node_order[j];
4757 z = &NODE_DATA(node)->node_zones[zone_type];
Mel Gorman6aa303d2016-09-01 16:14:55 -07004758 if (managed_zone(z)) {
Mel Gormandd1a2392008-04-28 02:12:17 -07004759 zoneref_set_zone(z,
4760 &zonelist->_zonerefs[pos++]);
Mel Gorman54a6eb52008-04-28 02:12:16 -07004761 check_highest_zone(zone_type);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004762 }
4763 }
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004764 }
Mel Gormandd1a2392008-04-28 02:12:17 -07004765 zonelist->_zonerefs[pos].zone = NULL;
4766 zonelist->_zonerefs[pos].zone_idx = 0;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004767}
4768
Mel Gorman31939132014-10-09 15:28:30 -07004769#if defined(CONFIG_64BIT)
4770/*
4771 * Devices that require DMA32/DMA are relatively rare and do not justify a
4772 * penalty to every machine in case the specialised case applies. Default
4773 * to Node-ordering on 64-bit NUMA machines
4774 */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004775static int default_zonelist_order(void)
4776{
Mel Gorman31939132014-10-09 15:28:30 -07004777 return ZONELIST_ORDER_NODE;
4778}
4779#else
4780/*
4781 * On 32-bit, the Normal zone needs to be preserved for allocations accessible
4782 * by the kernel. If processes running on node 0 deplete the low memory zone
4783 * then reclaim will occur more frequency increasing stalls and potentially
4784 * be easier to OOM if a large percentage of the zone is under writeback or
4785 * dirty. The problem is significantly worse if CONFIG_HIGHPTE is not set.
4786 * Hence, default to zone ordering on 32-bit.
4787 */
4788static int default_zonelist_order(void)
4789{
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004790 return ZONELIST_ORDER_ZONE;
4791}
Mel Gorman31939132014-10-09 15:28:30 -07004792#endif /* CONFIG_64BIT */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004793
4794static void set_zonelist_order(void)
4795{
4796 if (user_zonelist_order == ZONELIST_ORDER_DEFAULT)
4797 current_zonelist_order = default_zonelist_order();
4798 else
4799 current_zonelist_order = user_zonelist_order;
4800}
4801
4802static void build_zonelists(pg_data_t *pgdat)
4803{
Yaowei Baic00eb152016-01-14 15:19:00 -08004804 int i, node, load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 nodemask_t used_mask;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004806 int local_node, prev_node;
4807 struct zonelist *zonelist;
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08004808 unsigned int order = current_zonelist_order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
4810 /* initialize zonelists */
Christoph Lameter523b9452007-10-16 01:25:37 -07004811 for (i = 0; i < MAX_ZONELISTS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 zonelist = pgdat->node_zonelists + i;
Mel Gormandd1a2392008-04-28 02:12:17 -07004813 zonelist->_zonerefs[0].zone = NULL;
4814 zonelist->_zonerefs[0].zone_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 }
4816
4817 /* NUMA-aware ordering of nodes */
4818 local_node = pgdat->node_id;
Christoph Lameter62bc62a2009-06-16 15:32:15 -07004819 load = nr_online_nodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 prev_node = local_node;
4821 nodes_clear(used_mask);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004822
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004823 memset(node_order, 0, sizeof(node_order));
Yaowei Baic00eb152016-01-14 15:19:00 -08004824 i = 0;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004825
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
4827 /*
4828 * We don't want to pressure a particular node.
4829 * So adding penalty to the first node in same
4830 * distance group to make it round-robin.
4831 */
David Rientjes957f8222012-10-08 16:33:24 -07004832 if (node_distance(local_node, node) !=
4833 node_distance(local_node, prev_node))
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004834 node_load[node] = load;
4835
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 prev_node = node;
4837 load--;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004838 if (order == ZONELIST_ORDER_NODE)
4839 build_zonelists_in_node_order(pgdat, node);
4840 else
Yaowei Baic00eb152016-01-14 15:19:00 -08004841 node_order[i++] = node; /* remember order */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004844 if (order == ZONELIST_ORDER_ZONE) {
4845 /* calculate node order -- i.e., DMA last! */
Yaowei Baic00eb152016-01-14 15:19:00 -08004846 build_zonelists_in_zone_order(pgdat, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 }
Christoph Lameter523b9452007-10-16 01:25:37 -07004848
4849 build_thisnode_zonelists(pgdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850}
4851
Lee Schermerhorn7aac7892010-05-26 14:45:00 -07004852#ifdef CONFIG_HAVE_MEMORYLESS_NODES
4853/*
4854 * Return node id of node used for "local" allocations.
4855 * I.e., first node id of first zone in arg node's generic zonelist.
4856 * Used for initializing percpu 'numa_mem', which is used primarily
4857 * for kernel allocations, so use GFP_KERNEL flags to locate zonelist.
4858 */
4859int local_memory_node(int node)
4860{
Mel Gormanc33d6c02016-05-19 17:14:10 -07004861 struct zoneref *z;
Lee Schermerhorn7aac7892010-05-26 14:45:00 -07004862
Mel Gormanc33d6c02016-05-19 17:14:10 -07004863 z = first_zones_zonelist(node_zonelist(node, GFP_KERNEL),
Lee Schermerhorn7aac7892010-05-26 14:45:00 -07004864 gfp_zone(GFP_KERNEL),
Mel Gormanc33d6c02016-05-19 17:14:10 -07004865 NULL);
4866 return z->zone->node;
Lee Schermerhorn7aac7892010-05-26 14:45:00 -07004867}
4868#endif
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004869
Joonsoo Kim6423aa82016-08-10 16:27:49 -07004870static void setup_min_unmapped_ratio(void);
4871static void setup_min_slab_ratio(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872#else /* CONFIG_NUMA */
4873
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07004874static void set_zonelist_order(void)
4875{
4876 current_zonelist_order = ZONELIST_ORDER_ZONE;
4877}
4878
4879static void build_zonelists(pg_data_t *pgdat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880{
Christoph Lameter19655d32006-09-25 23:31:19 -07004881 int node, local_node;
Mel Gorman54a6eb52008-04-28 02:12:16 -07004882 enum zone_type j;
4883 struct zonelist *zonelist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884
4885 local_node = pgdat->node_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886
Aneesh Kumar K.Vc9634cf2016-10-07 16:59:12 -07004887 zonelist = &pgdat->node_zonelists[ZONELIST_FALLBACK];
Zhang Yanfeibc732f12013-07-08 16:00:06 -07004888 j = build_zonelists_node(pgdat, zonelist, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889
Mel Gorman54a6eb52008-04-28 02:12:16 -07004890 /*
4891 * Now we build the zonelist so that it contains the zones
4892 * of all the other nodes.
4893 * We don't want to pressure a particular node, so when
4894 * building the zones for node N, we make sure that the
4895 * zones coming right after the local ones are those from
4896 * node N+1 (modulo N)
4897 */
4898 for (node = local_node + 1; node < MAX_NUMNODES; node++) {
4899 if (!node_online(node))
4900 continue;
Zhang Yanfeibc732f12013-07-08 16:00:06 -07004901 j = build_zonelists_node(NODE_DATA(node), zonelist, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 }
Mel Gorman54a6eb52008-04-28 02:12:16 -07004903 for (node = 0; node < local_node; node++) {
4904 if (!node_online(node))
4905 continue;
Zhang Yanfeibc732f12013-07-08 16:00:06 -07004906 j = build_zonelists_node(NODE_DATA(node), zonelist, j);
Mel Gorman54a6eb52008-04-28 02:12:16 -07004907 }
4908
Mel Gormandd1a2392008-04-28 02:12:17 -07004909 zonelist->_zonerefs[j].zone = NULL;
4910 zonelist->_zonerefs[j].zone_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911}
4912
4913#endif /* CONFIG_NUMA */
4914
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09004915/*
4916 * Boot pageset table. One per cpu which is going to be used for all
4917 * zones and all nodes. The parameters will be set in such a way
4918 * that an item put on a list will immediately be handed over to
4919 * the buddy list. This is safe since pageset manipulation is done
4920 * with interrupts disabled.
4921 *
4922 * The boot_pagesets must be kept even after bootup is complete for
4923 * unused processors and/or zones. They do play a role for bootstrapping
4924 * hotplugged processors.
4925 *
4926 * zoneinfo_show() and maybe other functions do
4927 * not check if the processor is online before following the pageset pointer.
4928 * Other parts of the kernel may not check if the zone is available.
4929 */
4930static void setup_pageset(struct per_cpu_pageset *p, unsigned long batch);
4931static DEFINE_PER_CPU(struct per_cpu_pageset, boot_pageset);
Haicheng Li1f522502010-05-24 14:32:51 -07004932static void setup_zone_pageset(struct zone *zone);
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09004933
Haicheng Li4eaf3f62010-05-24 14:32:52 -07004934/*
4935 * Global mutex to protect against size modification of zonelists
4936 * as well as to serialize pageset setup for the new populated zone.
4937 */
4938DEFINE_MUTEX(zonelists_mutex);
4939
Rusty Russell9b1a4d32008-07-28 12:16:30 -05004940/* return values int ....just for stop_machine() */
Jiang Liu4ed7e022012-07-31 16:43:35 -07004941static int __build_all_zonelists(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942{
Yasunori Goto68113782006-06-23 02:03:11 -07004943 int nid;
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09004944 int cpu;
Jiang Liu9adb62a2012-07-31 16:43:28 -07004945 pg_data_t *self = data;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08004946
Bo Liu7f9cfb32009-08-18 14:11:19 -07004947#ifdef CONFIG_NUMA
4948 memset(node_load, 0, sizeof(node_load));
4949#endif
Jiang Liu9adb62a2012-07-31 16:43:28 -07004950
4951 if (self && !node_online(self->node_id)) {
4952 build_zonelists(self);
Jiang Liu9adb62a2012-07-31 16:43:28 -07004953 }
4954
Paul Jackson9276b1bc2006-12-06 20:31:48 -08004955 for_each_online_node(nid) {
Christoph Lameter7ea15302007-10-16 01:25:29 -07004956 pg_data_t *pgdat = NODE_DATA(nid);
4957
4958 build_zonelists(pgdat);
Paul Jackson9276b1bc2006-12-06 20:31:48 -08004959 }
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09004960
4961 /*
4962 * Initialize the boot_pagesets that are going to be used
4963 * for bootstrapping processors. The real pagesets for
4964 * each zone will be allocated later when the per cpu
4965 * allocator is available.
4966 *
4967 * boot_pagesets are used also for bootstrapping offline
4968 * cpus if the system is already booted because the pagesets
4969 * are needed to initialize allocators on a specific cpu too.
4970 * F.e. the percpu allocator needs the page allocator which
4971 * needs the percpu allocator in order to allocate its pagesets
4972 * (a chicken-egg dilemma).
4973 */
Lee Schermerhorn7aac7892010-05-26 14:45:00 -07004974 for_each_possible_cpu(cpu) {
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09004975 setup_pageset(&per_cpu(boot_pageset, cpu), 0);
4976
Lee Schermerhorn7aac7892010-05-26 14:45:00 -07004977#ifdef CONFIG_HAVE_MEMORYLESS_NODES
4978 /*
4979 * We now know the "local memory node" for each node--
4980 * i.e., the node of the first zone in the generic zonelist.
4981 * Set up numa_mem percpu variable for on-line cpus. During
4982 * boot, only the boot cpu should be on-line; we'll init the
4983 * secondary cpus' numa_mem as they come on-line. During
4984 * node/memory hotplug, we'll fixup all on-line cpus.
4985 */
4986 if (cpu_online(cpu))
4987 set_cpu_numa_mem(cpu, local_memory_node(cpu_to_node(cpu)));
4988#endif
4989 }
4990
Yasunori Goto68113782006-06-23 02:03:11 -07004991 return 0;
4992}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993
Rasmus Villemoes061f67b2015-02-12 15:00:06 -08004994static noinline void __init
4995build_all_zonelists_init(void)
4996{
4997 __build_all_zonelists(NULL);
4998 mminit_verify_zonelist();
4999 cpuset_init_current_mems_allowed();
5000}
5001
Haicheng Li4eaf3f62010-05-24 14:32:52 -07005002/*
5003 * Called with zonelists_mutex held always
5004 * unless system_state == SYSTEM_BOOTING.
Rasmus Villemoes061f67b2015-02-12 15:00:06 -08005005 *
5006 * __ref due to (1) call of __meminit annotated setup_zone_pageset
5007 * [we're only called with non-NULL zone through __meminit paths] and
5008 * (2) call of __init annotated helper build_all_zonelists_init
5009 * [protected by SYSTEM_BOOTING].
Haicheng Li4eaf3f62010-05-24 14:32:52 -07005010 */
Jiang Liu9adb62a2012-07-31 16:43:28 -07005011void __ref build_all_zonelists(pg_data_t *pgdat, struct zone *zone)
Yasunori Goto68113782006-06-23 02:03:11 -07005012{
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005013 set_zonelist_order();
5014
Yasunori Goto68113782006-06-23 02:03:11 -07005015 if (system_state == SYSTEM_BOOTING) {
Rasmus Villemoes061f67b2015-02-12 15:00:06 -08005016 build_all_zonelists_init();
Yasunori Goto68113782006-06-23 02:03:11 -07005017 } else {
KAMEZAWA Hiroyukie9959f02010-11-24 12:57:09 -08005018#ifdef CONFIG_MEMORY_HOTPLUG
Jiang Liu9adb62a2012-07-31 16:43:28 -07005019 if (zone)
5020 setup_zone_pageset(zone);
KAMEZAWA Hiroyukie9959f02010-11-24 12:57:09 -08005021#endif
Cody P Schaferdd1895e2013-07-03 15:01:36 -07005022 /* we have to stop all cpus to guarantee there is no user
5023 of zonelist */
Jiang Liu9adb62a2012-07-31 16:43:28 -07005024 stop_machine(__build_all_zonelists, pgdat, NULL);
Yasunori Goto68113782006-06-23 02:03:11 -07005025 /* cpuset refresh routine should be here */
5026 }
Andrew Mortonbd1e22b2006-06-23 02:03:47 -07005027 vm_total_pages = nr_free_pagecache_pages();
Mel Gorman9ef9acb2007-10-16 01:25:54 -07005028 /*
5029 * Disable grouping by mobility if the number of pages in the
5030 * system is too low to allow the mechanism to work. It would be
5031 * more accurate, but expensive to check per-zone. This check is
5032 * made on memory-hotadd so a system can start with mobility
5033 * disabled and enable it later
5034 */
Mel Gormand9c23402007-10-16 01:26:01 -07005035 if (vm_total_pages < (pageblock_nr_pages * MIGRATE_TYPES))
Mel Gorman9ef9acb2007-10-16 01:25:54 -07005036 page_group_by_mobility_disabled = 1;
5037 else
5038 page_group_by_mobility_disabled = 0;
5039
Joe Perches756a025f02016-03-17 14:19:47 -07005040 pr_info("Built %i zonelists in %s order, mobility grouping %s. Total pages: %ld\n",
5041 nr_online_nodes,
5042 zonelist_order_name[current_zonelist_order],
5043 page_group_by_mobility_disabled ? "off" : "on",
5044 vm_total_pages);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005045#ifdef CONFIG_NUMA
Anton Blanchardf88dfff2014-12-10 15:42:53 -08005046 pr_info("Policy zone: %s\n", zone_names[policy_zone]);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07005047#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048}
5049
5050/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 * Initially all pages are reserved - free ones are freed
5052 * up by free_all_bootmem() once the early boot process is
5053 * done. Non-atomic initialization, single-pass.
5054 */
Matt Tolentinoc09b4242006-01-17 07:03:44 +01005055void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
Dave Hansena2f3aa022007-01-10 23:15:30 -08005056 unsigned long start_pfn, enum memmap_context context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057{
Dan Williams4b94ffd2016-01-15 16:56:22 -08005058 struct vmem_altmap *altmap = to_vmem_altmap(__pfn_to_phys(start_pfn));
Andy Whitcroft29751f62005-06-23 00:08:00 -07005059 unsigned long end_pfn = start_pfn + size;
Dan Williams4b94ffd2016-01-15 16:56:22 -08005060 pg_data_t *pgdat = NODE_DATA(nid);
Andy Whitcroft29751f62005-06-23 00:08:00 -07005061 unsigned long pfn;
Mel Gorman3a80a7f2015-06-30 14:57:02 -07005062 unsigned long nr_initialised = 0;
Taku Izumi342332e2016-03-15 14:55:22 -07005063#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
5064 struct memblock_region *r = NULL, *tmp;
5065#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066
Hugh Dickins22b31ee2009-01-06 14:40:09 -08005067 if (highest_memmap_pfn < end_pfn - 1)
5068 highest_memmap_pfn = end_pfn - 1;
5069
Dan Williams4b94ffd2016-01-15 16:56:22 -08005070 /*
5071 * Honor reservation requested by the driver for this ZONE_DEVICE
5072 * memory
5073 */
5074 if (altmap && start_pfn == altmap->base_pfn)
5075 start_pfn += altmap->reserve;
5076
Greg Ungerercbe8dd42006-01-12 01:05:24 -08005077 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
Dave Hansena2f3aa022007-01-10 23:15:30 -08005078 /*
Andrew Mortonb72d0ff2016-03-15 14:55:25 -07005079 * There can be holes in boot-time mem_map[]s handed to this
5080 * function. They do not exist on hotplugged memory.
Dave Hansena2f3aa022007-01-10 23:15:30 -08005081 */
Andrew Mortonb72d0ff2016-03-15 14:55:25 -07005082 if (context != MEMMAP_EARLY)
5083 goto not_early;
5084
5085 if (!early_pfn_valid(pfn))
5086 continue;
5087 if (!early_pfn_in_nid(pfn, nid))
5088 continue;
5089 if (!update_defer_init(pgdat, pfn, end_pfn, &nr_initialised))
5090 break;
Taku Izumi342332e2016-03-15 14:55:22 -07005091
5092#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Andrew Mortonb72d0ff2016-03-15 14:55:25 -07005093 /*
Andrew Mortonb72d0ff2016-03-15 14:55:25 -07005094 * Check given memblock attribute by firmware which can affect
5095 * kernel memory layout. If zone==ZONE_MOVABLE but memory is
5096 * mirrored, it's an overlapped memmap init. skip it.
5097 */
5098 if (mirrored_kernelcore && zone == ZONE_MOVABLE) {
5099 if (!r || pfn >= memblock_region_memory_end_pfn(r)) {
5100 for_each_memblock(memory, tmp)
5101 if (pfn < memblock_region_memory_end_pfn(tmp))
5102 break;
5103 r = tmp;
Taku Izumi342332e2016-03-15 14:55:22 -07005104 }
Andrew Mortonb72d0ff2016-03-15 14:55:25 -07005105 if (pfn >= memblock_region_memory_base_pfn(r) &&
5106 memblock_is_mirror(r)) {
5107 /* already initialized as NORMAL */
5108 pfn = memblock_region_memory_end_pfn(r);
5109 continue;
5110 }
Dave Hansena2f3aa022007-01-10 23:15:30 -08005111 }
Andrew Mortonb72d0ff2016-03-15 14:55:25 -07005112#endif
Mel Gormanac5d2532015-06-30 14:57:20 -07005113
Andrew Mortonb72d0ff2016-03-15 14:55:25 -07005114not_early:
Mel Gormanac5d2532015-06-30 14:57:20 -07005115 /*
5116 * Mark the block movable so that blocks are reserved for
5117 * movable at startup. This will force kernel allocations
5118 * to reserve their blocks rather than leaking throughout
5119 * the address space during boot when many long-lived
Mel Gorman974a7862015-11-06 16:28:34 -08005120 * kernel allocations are made.
Mel Gormanac5d2532015-06-30 14:57:20 -07005121 *
5122 * bitmap is created for zone's valid pfn range. but memmap
5123 * can be created for invalid pages (for alignment)
5124 * check here not to call set_pageblock_migratetype() against
5125 * pfn out of zone.
5126 */
5127 if (!(pfn & (pageblock_nr_pages - 1))) {
5128 struct page *page = pfn_to_page(pfn);
5129
5130 __init_single_page(page, pfn, zone, nid);
5131 set_pageblock_migratetype(page, MIGRATE_MOVABLE);
5132 } else {
5133 __init_single_pfn(pfn, zone, nid);
5134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 }
5136}
5137
Andi Kleen1e548de2008-02-04 22:29:26 -08005138static void __meminit zone_init_free_lists(struct zone *zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139{
Mel Gorman7aeb09f2014-06-04 16:10:21 -07005140 unsigned int order, t;
Mel Gormanb2a0ac82007-10-16 01:25:48 -07005141 for_each_migratetype_order(order, t) {
5142 INIT_LIST_HEAD(&zone->free_area[order].free_list[t]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 zone->free_area[order].nr_free = 0;
5144 }
5145}
5146
5147#ifndef __HAVE_ARCH_MEMMAP_INIT
5148#define memmap_init(size, nid, zone, start_pfn) \
Dave Hansena2f3aa022007-01-10 23:15:30 -08005149 memmap_init_zone((size), (nid), (zone), (start_pfn), MEMMAP_EARLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150#endif
5151
David Rientjes7cd2b0a2014-06-23 13:22:04 -07005152static int zone_batchsize(struct zone *zone)
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07005153{
David Howells3a6be872009-05-06 16:03:03 -07005154#ifdef CONFIG_MMU
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07005155 int batch;
5156
5157 /*
5158 * The per-cpu-pages pools are set to around 1000th of the
Seth, Rohitba56e912005-10-29 18:15:47 -07005159 * size of the zone. But no more than 1/2 of a meg.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07005160 *
5161 * OK, so we don't know how big the cache is. So guess.
5162 */
Jiang Liub40da042013-02-22 16:33:52 -08005163 batch = zone->managed_pages / 1024;
Seth, Rohitba56e912005-10-29 18:15:47 -07005164 if (batch * PAGE_SIZE > 512 * 1024)
5165 batch = (512 * 1024) / PAGE_SIZE;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07005166 batch /= 4; /* We effectively *= 4 below */
5167 if (batch < 1)
5168 batch = 1;
5169
5170 /*
Nick Piggin0ceaacc2005-12-04 13:55:25 +11005171 * Clamp the batch to a 2^n - 1 value. Having a power
5172 * of 2 value was found to be more likely to have
5173 * suboptimal cache aliasing properties in some cases.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07005174 *
Nick Piggin0ceaacc2005-12-04 13:55:25 +11005175 * For example if 2 tasks are alternately allocating
5176 * batches of pages, one task can end up with a lot
5177 * of pages of one half of the possible page colors
5178 * and the other with pages of the other colors.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07005179 */
David Howells91552032009-05-06 16:03:02 -07005180 batch = rounddown_pow_of_two(batch + batch/2) - 1;
Seth, Rohitba56e912005-10-29 18:15:47 -07005181
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07005182 return batch;
David Howells3a6be872009-05-06 16:03:03 -07005183
5184#else
5185 /* The deferral and batching of frees should be suppressed under NOMMU
5186 * conditions.
5187 *
5188 * The problem is that NOMMU needs to be able to allocate large chunks
5189 * of contiguous memory as there's no hardware page translation to
5190 * assemble apparent contiguous memory from discontiguous pages.
5191 *
5192 * Queueing large contiguous runs of pages for batching, however,
5193 * causes the pages to actually be freed in smaller chunks. As there
5194 * can be a significant delay between the individual batches being
5195 * recycled, this leads to the once large chunks of space being
5196 * fragmented and becoming unavailable for high-order allocations.
5197 */
5198 return 0;
5199#endif
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07005200}
5201
Cody P Schafer8d7a8fa2013-07-03 15:01:31 -07005202/*
5203 * pcp->high and pcp->batch values are related and dependent on one another:
5204 * ->batch must never be higher then ->high.
5205 * The following function updates them in a safe manner without read side
5206 * locking.
5207 *
5208 * Any new users of pcp->batch and pcp->high should ensure they can cope with
5209 * those fields changing asynchronously (acording the the above rule).
5210 *
5211 * mutex_is_locked(&pcp_batch_high_lock) required when calling this function
5212 * outside of boot time (or some other assurance that no concurrent updaters
5213 * exist).
5214 */
5215static void pageset_update(struct per_cpu_pages *pcp, unsigned long high,
5216 unsigned long batch)
5217{
5218 /* start with a fail safe value for batch */
5219 pcp->batch = 1;
5220 smp_wmb();
5221
5222 /* Update high, then batch, in order */
5223 pcp->high = high;
5224 smp_wmb();
5225
5226 pcp->batch = batch;
5227}
5228
Cody P Schafer36640332013-07-03 15:01:40 -07005229/* a companion to pageset_set_high() */
Cody P Schafer4008bab2013-07-03 15:01:28 -07005230static void pageset_set_batch(struct per_cpu_pageset *p, unsigned long batch)
5231{
Cody P Schafer8d7a8fa2013-07-03 15:01:31 -07005232 pageset_update(&p->pcp, 6 * batch, max(1UL, 1 * batch));
Cody P Schafer4008bab2013-07-03 15:01:28 -07005233}
5234
Cody P Schafer88c90db2013-07-03 15:01:35 -07005235static void pageset_init(struct per_cpu_pageset *p)
Christoph Lameter2caaad42005-06-21 17:15:00 -07005236{
5237 struct per_cpu_pages *pcp;
Mel Gorman5f8dcc22009-09-21 17:03:19 -07005238 int migratetype;
Christoph Lameter2caaad42005-06-21 17:15:00 -07005239
Magnus Damm1c6fe942005-10-26 01:58:59 -07005240 memset(p, 0, sizeof(*p));
5241
Christoph Lameter3dfa5722008-02-04 22:29:19 -08005242 pcp = &p->pcp;
Christoph Lameter2caaad42005-06-21 17:15:00 -07005243 pcp->count = 0;
Mel Gorman5f8dcc22009-09-21 17:03:19 -07005244 for (migratetype = 0; migratetype < MIGRATE_PCPTYPES; migratetype++)
5245 INIT_LIST_HEAD(&pcp->lists[migratetype]);
Christoph Lameter2caaad42005-06-21 17:15:00 -07005246}
5247
Cody P Schafer88c90db2013-07-03 15:01:35 -07005248static void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
5249{
5250 pageset_init(p);
5251 pageset_set_batch(p, batch);
5252}
5253
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08005254/*
Cody P Schafer36640332013-07-03 15:01:40 -07005255 * pageset_set_high() sets the high water mark for hot per_cpu_pagelist
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08005256 * to the value high for the pageset p.
5257 */
Cody P Schafer36640332013-07-03 15:01:40 -07005258static void pageset_set_high(struct per_cpu_pageset *p,
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08005259 unsigned long high)
5260{
Cody P Schafer8d7a8fa2013-07-03 15:01:31 -07005261 unsigned long batch = max(1UL, high / 4);
5262 if ((high / 4) > (PAGE_SHIFT * 8))
5263 batch = PAGE_SHIFT * 8;
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08005264
Cody P Schafer8d7a8fa2013-07-03 15:01:31 -07005265 pageset_update(&p->pcp, high, batch);
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08005266}
5267
David Rientjes7cd2b0a2014-06-23 13:22:04 -07005268static void pageset_set_high_and_batch(struct zone *zone,
5269 struct per_cpu_pageset *pcp)
Cody P Schafer56cef2b2013-07-03 15:01:38 -07005270{
Cody P Schafer56cef2b2013-07-03 15:01:38 -07005271 if (percpu_pagelist_fraction)
Cody P Schafer36640332013-07-03 15:01:40 -07005272 pageset_set_high(pcp,
Cody P Schafer56cef2b2013-07-03 15:01:38 -07005273 (zone->managed_pages /
5274 percpu_pagelist_fraction));
5275 else
5276 pageset_set_batch(pcp, zone_batchsize(zone));
5277}
5278
Cody P Schafer169f6c12013-07-03 15:01:41 -07005279static void __meminit zone_pageset_init(struct zone *zone, int cpu)
5280{
5281 struct per_cpu_pageset *pcp = per_cpu_ptr(zone->pageset, cpu);
5282
5283 pageset_init(pcp);
5284 pageset_set_high_and_batch(zone, pcp);
5285}
5286
Jiang Liu4ed7e022012-07-31 16:43:35 -07005287static void __meminit setup_zone_pageset(struct zone *zone)
Wu Fengguang319774e2010-05-24 14:32:49 -07005288{
5289 int cpu;
Wu Fengguang319774e2010-05-24 14:32:49 -07005290 zone->pageset = alloc_percpu(struct per_cpu_pageset);
Cody P Schafer56cef2b2013-07-03 15:01:38 -07005291 for_each_possible_cpu(cpu)
5292 zone_pageset_init(zone, cpu);
Wu Fengguang319774e2010-05-24 14:32:49 -07005293}
5294
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07005295/*
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09005296 * Allocate per cpu pagesets and initialize them.
5297 * Before this call only boot pagesets were available.
Christoph Lameter2caaad42005-06-21 17:15:00 -07005298 */
Al Viro78d99552005-12-15 09:18:25 +00005299void __init setup_per_cpu_pageset(void)
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07005300{
Mel Gormanb4911ea2016-08-04 15:31:49 -07005301 struct pglist_data *pgdat;
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09005302 struct zone *zone;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07005303
Wu Fengguang319774e2010-05-24 14:32:49 -07005304 for_each_populated_zone(zone)
5305 setup_zone_pageset(zone);
Mel Gormanb4911ea2016-08-04 15:31:49 -07005306
5307 for_each_online_pgdat(pgdat)
5308 pgdat->per_cpu_nodestats =
5309 alloc_percpu(struct per_cpu_nodestat);
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07005310}
5311
Matt Tolentinoc09b4242006-01-17 07:03:44 +01005312static __meminit void zone_pcp_init(struct zone *zone)
Dave Hansened8ece22005-10-29 18:16:50 -07005313{
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09005314 /*
5315 * per cpu subsystem is not up at this point. The following code
5316 * relies on the ability of the linker to provide the
5317 * offset of a (static) per cpu variable into the per cpu area.
5318 */
5319 zone->pageset = &boot_pageset;
Dave Hansened8ece22005-10-29 18:16:50 -07005320
Xishi Qiub38a8722013-11-12 15:07:20 -08005321 if (populated_zone(zone))
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09005322 printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%u\n",
5323 zone->name, zone->present_pages,
5324 zone_batchsize(zone));
Dave Hansened8ece22005-10-29 18:16:50 -07005325}
5326
Jiang Liu4ed7e022012-07-31 16:43:35 -07005327int __meminit init_currently_empty_zone(struct zone *zone,
Yasunori Goto718127c2006-06-23 02:03:10 -07005328 unsigned long zone_start_pfn,
Yaowei Baib171e402015-11-05 18:47:06 -08005329 unsigned long size)
Dave Hansened8ece22005-10-29 18:16:50 -07005330{
5331 struct pglist_data *pgdat = zone->zone_pgdat;
Linus Torvalds9dcb8b62016-10-26 10:15:30 -07005332
Dave Hansened8ece22005-10-29 18:16:50 -07005333 pgdat->nr_zones = zone_idx(zone) + 1;
5334
Dave Hansened8ece22005-10-29 18:16:50 -07005335 zone->zone_start_pfn = zone_start_pfn;
5336
Mel Gorman708614e2008-07-23 21:26:51 -07005337 mminit_dprintk(MMINIT_TRACE, "memmap_init",
5338 "Initialising map node %d zone %lu pfns %lu -> %lu\n",
5339 pgdat->node_id,
5340 (unsigned long)zone_idx(zone),
5341 zone_start_pfn, (zone_start_pfn + size));
5342
Andi Kleen1e548de2008-02-04 22:29:26 -08005343 zone_init_free_lists(zone);
Linus Torvalds9dcb8b62016-10-26 10:15:30 -07005344 zone->initialized = 1;
Yasunori Goto718127c2006-06-23 02:03:10 -07005345
5346 return 0;
Dave Hansened8ece22005-10-29 18:16:50 -07005347}
5348
Tejun Heo0ee332c2011-12-08 10:22:09 -08005349#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Mel Gormanc7132162006-09-27 01:49:43 -07005350#ifndef CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID
Mel Gorman8a942fd2015-06-30 14:56:55 -07005351
Mel Gormanc7132162006-09-27 01:49:43 -07005352/*
5353 * Required by SPARSEMEM. Given a PFN, return what node the PFN is on.
Mel Gormanc7132162006-09-27 01:49:43 -07005354 */
Mel Gorman8a942fd2015-06-30 14:56:55 -07005355int __meminit __early_pfn_to_nid(unsigned long pfn,
5356 struct mminit_pfnnid_cache *state)
Mel Gormanc7132162006-09-27 01:49:43 -07005357{
Tejun Heoc13291a2011-07-12 10:46:30 +02005358 unsigned long start_pfn, end_pfn;
Yinghai Lue76b63f2013-09-11 14:22:17 -07005359 int nid;
Russ Anderson7c243c72013-04-29 15:07:59 -07005360
Mel Gorman8a942fd2015-06-30 14:56:55 -07005361 if (state->last_start <= pfn && pfn < state->last_end)
5362 return state->last_nid;
Mel Gormanc7132162006-09-27 01:49:43 -07005363
Yinghai Lue76b63f2013-09-11 14:22:17 -07005364 nid = memblock_search_pfn_nid(pfn, &start_pfn, &end_pfn);
5365 if (nid != -1) {
Mel Gorman8a942fd2015-06-30 14:56:55 -07005366 state->last_start = start_pfn;
5367 state->last_end = end_pfn;
5368 state->last_nid = nid;
Yinghai Lue76b63f2013-09-11 14:22:17 -07005369 }
5370
5371 return nid;
Mel Gormanc7132162006-09-27 01:49:43 -07005372}
5373#endif /* CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID */
5374
Mel Gormanc7132162006-09-27 01:49:43 -07005375/**
Santosh Shilimkar67828322014-01-21 15:50:25 -08005376 * free_bootmem_with_active_regions - Call memblock_free_early_nid for each active range
Randy Dunlap88ca3b92006-10-04 02:15:25 -07005377 * @nid: The node to free memory on. If MAX_NUMNODES, all nodes are freed.
Santosh Shilimkar67828322014-01-21 15:50:25 -08005378 * @max_low_pfn: The highest PFN that will be passed to memblock_free_early_nid
Mel Gormanc7132162006-09-27 01:49:43 -07005379 *
Zhang Zhen7d018172014-06-04 16:10:53 -07005380 * If an architecture guarantees that all ranges registered contain no holes
5381 * and may be freed, this this function may be used instead of calling
5382 * memblock_free_early_nid() manually.
Mel Gormanc7132162006-09-27 01:49:43 -07005383 */
Tejun Heoc13291a2011-07-12 10:46:30 +02005384void __init free_bootmem_with_active_regions(int nid, unsigned long max_low_pfn)
Mel Gormanc7132162006-09-27 01:49:43 -07005385{
Tejun Heoc13291a2011-07-12 10:46:30 +02005386 unsigned long start_pfn, end_pfn;
5387 int i, this_nid;
Mel Gormanc7132162006-09-27 01:49:43 -07005388
Tejun Heoc13291a2011-07-12 10:46:30 +02005389 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, &this_nid) {
5390 start_pfn = min(start_pfn, max_low_pfn);
5391 end_pfn = min(end_pfn, max_low_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07005392
Tejun Heoc13291a2011-07-12 10:46:30 +02005393 if (start_pfn < end_pfn)
Santosh Shilimkar67828322014-01-21 15:50:25 -08005394 memblock_free_early_nid(PFN_PHYS(start_pfn),
5395 (end_pfn - start_pfn) << PAGE_SHIFT,
5396 this_nid);
Mel Gormanc7132162006-09-27 01:49:43 -07005397 }
5398}
5399
5400/**
5401 * sparse_memory_present_with_active_regions - Call memory_present for each active range
Randy Dunlap88ca3b92006-10-04 02:15:25 -07005402 * @nid: The node to call memory_present for. If MAX_NUMNODES, all nodes will be used.
Mel Gormanc7132162006-09-27 01:49:43 -07005403 *
Zhang Zhen7d018172014-06-04 16:10:53 -07005404 * If an architecture guarantees that all ranges registered contain no holes and may
5405 * be freed, this function may be used instead of calling memory_present() manually.
Mel Gormanc7132162006-09-27 01:49:43 -07005406 */
5407void __init sparse_memory_present_with_active_regions(int nid)
5408{
Tejun Heoc13291a2011-07-12 10:46:30 +02005409 unsigned long start_pfn, end_pfn;
5410 int i, this_nid;
Mel Gormanc7132162006-09-27 01:49:43 -07005411
Tejun Heoc13291a2011-07-12 10:46:30 +02005412 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, &this_nid)
5413 memory_present(this_nid, start_pfn, end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07005414}
5415
5416/**
5417 * get_pfn_range_for_nid - Return the start and end page frames for a node
Randy Dunlap88ca3b92006-10-04 02:15:25 -07005418 * @nid: The nid to return the range for. If MAX_NUMNODES, the min and max PFN are returned.
5419 * @start_pfn: Passed by reference. On return, it will have the node start_pfn.
5420 * @end_pfn: Passed by reference. On return, it will have the node end_pfn.
Mel Gormanc7132162006-09-27 01:49:43 -07005421 *
5422 * It returns the start and end page frame of a node based on information
Zhang Zhen7d018172014-06-04 16:10:53 -07005423 * provided by memblock_set_node(). If called for a node
Mel Gormanc7132162006-09-27 01:49:43 -07005424 * with no available memory, a warning is printed and the start and end
Randy Dunlap88ca3b92006-10-04 02:15:25 -07005425 * PFNs will be 0.
Mel Gormanc7132162006-09-27 01:49:43 -07005426 */
Yasunori Gotoa3142c82007-05-08 00:23:07 -07005427void __meminit get_pfn_range_for_nid(unsigned int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07005428 unsigned long *start_pfn, unsigned long *end_pfn)
5429{
Tejun Heoc13291a2011-07-12 10:46:30 +02005430 unsigned long this_start_pfn, this_end_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07005431 int i;
Tejun Heoc13291a2011-07-12 10:46:30 +02005432
Mel Gormanc7132162006-09-27 01:49:43 -07005433 *start_pfn = -1UL;
5434 *end_pfn = 0;
5435
Tejun Heoc13291a2011-07-12 10:46:30 +02005436 for_each_mem_pfn_range(i, nid, &this_start_pfn, &this_end_pfn, NULL) {
5437 *start_pfn = min(*start_pfn, this_start_pfn);
5438 *end_pfn = max(*end_pfn, this_end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07005439 }
5440
Christoph Lameter633c0662007-10-16 01:25:37 -07005441 if (*start_pfn == -1UL)
Mel Gormanc7132162006-09-27 01:49:43 -07005442 *start_pfn = 0;
Mel Gormanc7132162006-09-27 01:49:43 -07005443}
5444
5445/*
Mel Gorman2a1e2742007-07-17 04:03:12 -07005446 * This finds a zone that can be used for ZONE_MOVABLE pages. The
5447 * assumption is made that zones within a node are ordered in monotonic
5448 * increasing memory addresses so that the "highest" populated zone is used
5449 */
Adrian Bunkb69a7282008-07-23 21:28:12 -07005450static void __init find_usable_zone_for_movable(void)
Mel Gorman2a1e2742007-07-17 04:03:12 -07005451{
5452 int zone_index;
5453 for (zone_index = MAX_NR_ZONES - 1; zone_index >= 0; zone_index--) {
5454 if (zone_index == ZONE_MOVABLE)
5455 continue;
5456
5457 if (arch_zone_highest_possible_pfn[zone_index] >
5458 arch_zone_lowest_possible_pfn[zone_index])
5459 break;
5460 }
5461
5462 VM_BUG_ON(zone_index == -1);
5463 movable_zone = zone_index;
5464}
5465
5466/*
5467 * The zone ranges provided by the architecture do not include ZONE_MOVABLE
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005468 * because it is sized independent of architecture. Unlike the other zones,
Mel Gorman2a1e2742007-07-17 04:03:12 -07005469 * the starting point for ZONE_MOVABLE is not fixed. It may be different
5470 * in each node depending on the size of each node and how evenly kernelcore
5471 * is distributed. This helper function adjusts the zone ranges
5472 * provided by the architecture for a given node by using the end of the
5473 * highest usable zone for ZONE_MOVABLE. This preserves the assumption that
5474 * zones within a node are in order of monotonic increases memory addresses
5475 */
Adrian Bunkb69a7282008-07-23 21:28:12 -07005476static void __meminit adjust_zone_range_for_zone_movable(int nid,
Mel Gorman2a1e2742007-07-17 04:03:12 -07005477 unsigned long zone_type,
5478 unsigned long node_start_pfn,
5479 unsigned long node_end_pfn,
5480 unsigned long *zone_start_pfn,
5481 unsigned long *zone_end_pfn)
5482{
5483 /* Only adjust if ZONE_MOVABLE is on this node */
5484 if (zone_movable_pfn[nid]) {
5485 /* Size ZONE_MOVABLE */
5486 if (zone_type == ZONE_MOVABLE) {
5487 *zone_start_pfn = zone_movable_pfn[nid];
5488 *zone_end_pfn = min(node_end_pfn,
5489 arch_zone_highest_possible_pfn[movable_zone]);
5490
Xishi Qiue506b992016-10-07 16:58:06 -07005491 /* Adjust for ZONE_MOVABLE starting within this range */
5492 } else if (!mirrored_kernelcore &&
5493 *zone_start_pfn < zone_movable_pfn[nid] &&
5494 *zone_end_pfn > zone_movable_pfn[nid]) {
5495 *zone_end_pfn = zone_movable_pfn[nid];
5496
Mel Gorman2a1e2742007-07-17 04:03:12 -07005497 /* Check if this whole range is within ZONE_MOVABLE */
5498 } else if (*zone_start_pfn >= zone_movable_pfn[nid])
5499 *zone_start_pfn = *zone_end_pfn;
5500 }
5501}
5502
5503/*
Mel Gormanc7132162006-09-27 01:49:43 -07005504 * Return the number of pages a zone spans in a node, including holes
5505 * present_pages = zone_spanned_pages_in_node() - zone_absent_pages_in_node()
5506 */
Paul Mundt6ea6e682007-07-15 23:38:20 -07005507static unsigned long __meminit zone_spanned_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07005508 unsigned long zone_type,
Zhang Yanfei7960aed2013-07-08 15:59:52 -07005509 unsigned long node_start_pfn,
5510 unsigned long node_end_pfn,
Taku Izumid91749c2016-03-15 14:55:18 -07005511 unsigned long *zone_start_pfn,
5512 unsigned long *zone_end_pfn,
Mel Gormanc7132162006-09-27 01:49:43 -07005513 unsigned long *ignored)
5514{
Xishi Qiub5685e92015-09-08 15:04:16 -07005515 /* When hotadd a new node from cpu_up(), the node should be empty */
Xishi Qiuf9126ab2015-08-14 15:35:16 -07005516 if (!node_start_pfn && !node_end_pfn)
5517 return 0;
5518
Zhang Yanfei7960aed2013-07-08 15:59:52 -07005519 /* Get the start and end of the zone */
Taku Izumid91749c2016-03-15 14:55:18 -07005520 *zone_start_pfn = arch_zone_lowest_possible_pfn[zone_type];
5521 *zone_end_pfn = arch_zone_highest_possible_pfn[zone_type];
Mel Gorman2a1e2742007-07-17 04:03:12 -07005522 adjust_zone_range_for_zone_movable(nid, zone_type,
5523 node_start_pfn, node_end_pfn,
Taku Izumid91749c2016-03-15 14:55:18 -07005524 zone_start_pfn, zone_end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07005525
5526 /* Check that this node has pages within the zone's required range */
Taku Izumid91749c2016-03-15 14:55:18 -07005527 if (*zone_end_pfn < node_start_pfn || *zone_start_pfn > node_end_pfn)
Mel Gormanc7132162006-09-27 01:49:43 -07005528 return 0;
5529
5530 /* Move the zone boundaries inside the node if necessary */
Taku Izumid91749c2016-03-15 14:55:18 -07005531 *zone_end_pfn = min(*zone_end_pfn, node_end_pfn);
5532 *zone_start_pfn = max(*zone_start_pfn, node_start_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07005533
5534 /* Return the spanned pages */
Taku Izumid91749c2016-03-15 14:55:18 -07005535 return *zone_end_pfn - *zone_start_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07005536}
5537
5538/*
5539 * Return the number of holes in a range on a node. If nid is MAX_NUMNODES,
Randy Dunlap88ca3b92006-10-04 02:15:25 -07005540 * then all holes in the requested range will be accounted for.
Mel Gormanc7132162006-09-27 01:49:43 -07005541 */
Yinghai Lu32996252009-12-15 17:59:02 -08005542unsigned long __meminit __absent_pages_in_range(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07005543 unsigned long range_start_pfn,
5544 unsigned long range_end_pfn)
5545{
Tejun Heo96e907d2011-07-12 10:46:29 +02005546 unsigned long nr_absent = range_end_pfn - range_start_pfn;
5547 unsigned long start_pfn, end_pfn;
5548 int i;
Mel Gormanc7132162006-09-27 01:49:43 -07005549
Tejun Heo96e907d2011-07-12 10:46:29 +02005550 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
5551 start_pfn = clamp(start_pfn, range_start_pfn, range_end_pfn);
5552 end_pfn = clamp(end_pfn, range_start_pfn, range_end_pfn);
5553 nr_absent -= end_pfn - start_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07005554 }
Tejun Heo96e907d2011-07-12 10:46:29 +02005555 return nr_absent;
Mel Gormanc7132162006-09-27 01:49:43 -07005556}
5557
5558/**
5559 * absent_pages_in_range - Return number of page frames in holes within a range
5560 * @start_pfn: The start PFN to start searching for holes
5561 * @end_pfn: The end PFN to stop searching for holes
5562 *
Randy Dunlap88ca3b92006-10-04 02:15:25 -07005563 * It returns the number of pages frames in memory holes within a range.
Mel Gormanc7132162006-09-27 01:49:43 -07005564 */
5565unsigned long __init absent_pages_in_range(unsigned long start_pfn,
5566 unsigned long end_pfn)
5567{
5568 return __absent_pages_in_range(MAX_NUMNODES, start_pfn, end_pfn);
5569}
5570
5571/* Return the number of page frames in holes in a zone on a node */
Paul Mundt6ea6e682007-07-15 23:38:20 -07005572static unsigned long __meminit zone_absent_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07005573 unsigned long zone_type,
Zhang Yanfei7960aed2013-07-08 15:59:52 -07005574 unsigned long node_start_pfn,
5575 unsigned long node_end_pfn,
Mel Gormanc7132162006-09-27 01:49:43 -07005576 unsigned long *ignored)
5577{
Tejun Heo96e907d2011-07-12 10:46:29 +02005578 unsigned long zone_low = arch_zone_lowest_possible_pfn[zone_type];
5579 unsigned long zone_high = arch_zone_highest_possible_pfn[zone_type];
Mel Gorman9c7cd682006-09-27 01:49:58 -07005580 unsigned long zone_start_pfn, zone_end_pfn;
Taku Izumi342332e2016-03-15 14:55:22 -07005581 unsigned long nr_absent;
Mel Gorman9c7cd682006-09-27 01:49:58 -07005582
Xishi Qiub5685e92015-09-08 15:04:16 -07005583 /* When hotadd a new node from cpu_up(), the node should be empty */
Xishi Qiuf9126ab2015-08-14 15:35:16 -07005584 if (!node_start_pfn && !node_end_pfn)
5585 return 0;
5586
Tejun Heo96e907d2011-07-12 10:46:29 +02005587 zone_start_pfn = clamp(node_start_pfn, zone_low, zone_high);
5588 zone_end_pfn = clamp(node_end_pfn, zone_low, zone_high);
Mel Gorman9c7cd682006-09-27 01:49:58 -07005589
Mel Gorman2a1e2742007-07-17 04:03:12 -07005590 adjust_zone_range_for_zone_movable(nid, zone_type,
5591 node_start_pfn, node_end_pfn,
5592 &zone_start_pfn, &zone_end_pfn);
Taku Izumi342332e2016-03-15 14:55:22 -07005593 nr_absent = __absent_pages_in_range(nid, zone_start_pfn, zone_end_pfn);
5594
5595 /*
5596 * ZONE_MOVABLE handling.
5597 * Treat pages to be ZONE_MOVABLE in ZONE_NORMAL as absent pages
5598 * and vice versa.
5599 */
Xishi Qiue506b992016-10-07 16:58:06 -07005600 if (mirrored_kernelcore && zone_movable_pfn[nid]) {
5601 unsigned long start_pfn, end_pfn;
5602 struct memblock_region *r;
Taku Izumi342332e2016-03-15 14:55:22 -07005603
Xishi Qiue506b992016-10-07 16:58:06 -07005604 for_each_memblock(memory, r) {
5605 start_pfn = clamp(memblock_region_memory_base_pfn(r),
5606 zone_start_pfn, zone_end_pfn);
5607 end_pfn = clamp(memblock_region_memory_end_pfn(r),
5608 zone_start_pfn, zone_end_pfn);
Taku Izumi342332e2016-03-15 14:55:22 -07005609
Xishi Qiue506b992016-10-07 16:58:06 -07005610 if (zone_type == ZONE_MOVABLE &&
5611 memblock_is_mirror(r))
5612 nr_absent += end_pfn - start_pfn;
Taku Izumi342332e2016-03-15 14:55:22 -07005613
Xishi Qiue506b992016-10-07 16:58:06 -07005614 if (zone_type == ZONE_NORMAL &&
5615 !memblock_is_mirror(r))
5616 nr_absent += end_pfn - start_pfn;
Taku Izumi342332e2016-03-15 14:55:22 -07005617 }
5618 }
5619
5620 return nr_absent;
Mel Gormanc7132162006-09-27 01:49:43 -07005621}
Mel Gorman0e0b8642006-09-27 01:49:56 -07005622
Tejun Heo0ee332c2011-12-08 10:22:09 -08005623#else /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
Paul Mundt6ea6e682007-07-15 23:38:20 -07005624static inline unsigned long __meminit zone_spanned_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07005625 unsigned long zone_type,
Zhang Yanfei7960aed2013-07-08 15:59:52 -07005626 unsigned long node_start_pfn,
5627 unsigned long node_end_pfn,
Taku Izumid91749c2016-03-15 14:55:18 -07005628 unsigned long *zone_start_pfn,
5629 unsigned long *zone_end_pfn,
Mel Gormanc7132162006-09-27 01:49:43 -07005630 unsigned long *zones_size)
5631{
Taku Izumid91749c2016-03-15 14:55:18 -07005632 unsigned int zone;
5633
5634 *zone_start_pfn = node_start_pfn;
5635 for (zone = 0; zone < zone_type; zone++)
5636 *zone_start_pfn += zones_size[zone];
5637
5638 *zone_end_pfn = *zone_start_pfn + zones_size[zone_type];
5639
Mel Gormanc7132162006-09-27 01:49:43 -07005640 return zones_size[zone_type];
5641}
5642
Paul Mundt6ea6e682007-07-15 23:38:20 -07005643static inline unsigned long __meminit zone_absent_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07005644 unsigned long zone_type,
Zhang Yanfei7960aed2013-07-08 15:59:52 -07005645 unsigned long node_start_pfn,
5646 unsigned long node_end_pfn,
Mel Gormanc7132162006-09-27 01:49:43 -07005647 unsigned long *zholes_size)
5648{
5649 if (!zholes_size)
5650 return 0;
5651
5652 return zholes_size[zone_type];
5653}
Yinghai Lu20e69262013-03-01 14:51:27 -08005654
Tejun Heo0ee332c2011-12-08 10:22:09 -08005655#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
Mel Gormanc7132162006-09-27 01:49:43 -07005656
Yasunori Gotoa3142c82007-05-08 00:23:07 -07005657static void __meminit calculate_node_totalpages(struct pglist_data *pgdat,
Zhang Yanfei7960aed2013-07-08 15:59:52 -07005658 unsigned long node_start_pfn,
5659 unsigned long node_end_pfn,
5660 unsigned long *zones_size,
5661 unsigned long *zholes_size)
Mel Gormanc7132162006-09-27 01:49:43 -07005662{
Gu Zhengfebd5942015-06-24 16:57:02 -07005663 unsigned long realtotalpages = 0, totalpages = 0;
Mel Gormanc7132162006-09-27 01:49:43 -07005664 enum zone_type i;
5665
Gu Zhengfebd5942015-06-24 16:57:02 -07005666 for (i = 0; i < MAX_NR_ZONES; i++) {
5667 struct zone *zone = pgdat->node_zones + i;
Taku Izumid91749c2016-03-15 14:55:18 -07005668 unsigned long zone_start_pfn, zone_end_pfn;
Gu Zhengfebd5942015-06-24 16:57:02 -07005669 unsigned long size, real_size;
Mel Gormanc7132162006-09-27 01:49:43 -07005670
Gu Zhengfebd5942015-06-24 16:57:02 -07005671 size = zone_spanned_pages_in_node(pgdat->node_id, i,
5672 node_start_pfn,
5673 node_end_pfn,
Taku Izumid91749c2016-03-15 14:55:18 -07005674 &zone_start_pfn,
5675 &zone_end_pfn,
Gu Zhengfebd5942015-06-24 16:57:02 -07005676 zones_size);
5677 real_size = size - zone_absent_pages_in_node(pgdat->node_id, i,
Zhang Yanfei7960aed2013-07-08 15:59:52 -07005678 node_start_pfn, node_end_pfn,
5679 zholes_size);
Taku Izumid91749c2016-03-15 14:55:18 -07005680 if (size)
5681 zone->zone_start_pfn = zone_start_pfn;
5682 else
5683 zone->zone_start_pfn = 0;
Gu Zhengfebd5942015-06-24 16:57:02 -07005684 zone->spanned_pages = size;
5685 zone->present_pages = real_size;
5686
5687 totalpages += size;
5688 realtotalpages += real_size;
5689 }
5690
5691 pgdat->node_spanned_pages = totalpages;
Mel Gormanc7132162006-09-27 01:49:43 -07005692 pgdat->node_present_pages = realtotalpages;
5693 printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id,
5694 realtotalpages);
5695}
5696
Mel Gorman835c1342007-10-16 01:25:47 -07005697#ifndef CONFIG_SPARSEMEM
5698/*
5699 * Calculate the size of the zone->blockflags rounded to an unsigned long
Mel Gormand9c23402007-10-16 01:26:01 -07005700 * Start by making sure zonesize is a multiple of pageblock_order by rounding
5701 * up. Then use 1 NR_PAGEBLOCK_BITS worth of bits per pageblock, finally
Mel Gorman835c1342007-10-16 01:25:47 -07005702 * round what is now in bits to nearest long in bits, then return it in
5703 * bytes.
5704 */
Linus Torvalds7c455122013-02-18 09:58:02 -08005705static unsigned long __init usemap_size(unsigned long zone_start_pfn, unsigned long zonesize)
Mel Gorman835c1342007-10-16 01:25:47 -07005706{
5707 unsigned long usemapsize;
5708
Linus Torvalds7c455122013-02-18 09:58:02 -08005709 zonesize += zone_start_pfn & (pageblock_nr_pages-1);
Mel Gormand9c23402007-10-16 01:26:01 -07005710 usemapsize = roundup(zonesize, pageblock_nr_pages);
5711 usemapsize = usemapsize >> pageblock_order;
Mel Gorman835c1342007-10-16 01:25:47 -07005712 usemapsize *= NR_PAGEBLOCK_BITS;
5713 usemapsize = roundup(usemapsize, 8 * sizeof(unsigned long));
5714
5715 return usemapsize / 8;
5716}
5717
5718static void __init setup_usemap(struct pglist_data *pgdat,
Linus Torvalds7c455122013-02-18 09:58:02 -08005719 struct zone *zone,
5720 unsigned long zone_start_pfn,
5721 unsigned long zonesize)
Mel Gorman835c1342007-10-16 01:25:47 -07005722{
Linus Torvalds7c455122013-02-18 09:58:02 -08005723 unsigned long usemapsize = usemap_size(zone_start_pfn, zonesize);
Mel Gorman835c1342007-10-16 01:25:47 -07005724 zone->pageblock_flags = NULL;
Julia Lawall58a01a42009-01-06 14:39:28 -08005725 if (usemapsize)
Santosh Shilimkar67828322014-01-21 15:50:25 -08005726 zone->pageblock_flags =
5727 memblock_virt_alloc_node_nopanic(usemapsize,
5728 pgdat->node_id);
Mel Gorman835c1342007-10-16 01:25:47 -07005729}
5730#else
Linus Torvalds7c455122013-02-18 09:58:02 -08005731static inline void setup_usemap(struct pglist_data *pgdat, struct zone *zone,
5732 unsigned long zone_start_pfn, unsigned long zonesize) {}
Mel Gorman835c1342007-10-16 01:25:47 -07005733#endif /* CONFIG_SPARSEMEM */
5734
Mel Gormand9c23402007-10-16 01:26:01 -07005735#ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
Mel Gormanba72cb82007-11-28 16:21:13 -08005736
Mel Gormand9c23402007-10-16 01:26:01 -07005737/* Initialise the number of pages represented by NR_PAGEBLOCK_BITS */
Chen Gang15ca2202013-09-11 14:20:27 -07005738void __paginginit set_pageblock_order(void)
Mel Gormand9c23402007-10-16 01:26:01 -07005739{
Andrew Morton955c1cd2012-05-29 15:06:31 -07005740 unsigned int order;
5741
Mel Gormand9c23402007-10-16 01:26:01 -07005742 /* Check that pageblock_nr_pages has not already been setup */
5743 if (pageblock_order)
5744 return;
5745
Andrew Morton955c1cd2012-05-29 15:06:31 -07005746 if (HPAGE_SHIFT > PAGE_SHIFT)
5747 order = HUGETLB_PAGE_ORDER;
5748 else
5749 order = MAX_ORDER - 1;
5750
Mel Gormand9c23402007-10-16 01:26:01 -07005751 /*
5752 * Assume the largest contiguous order of interest is a huge page.
Andrew Morton955c1cd2012-05-29 15:06:31 -07005753 * This value may be variable depending on boot parameters on IA64 and
5754 * powerpc.
Mel Gormand9c23402007-10-16 01:26:01 -07005755 */
5756 pageblock_order = order;
5757}
5758#else /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
5759
Mel Gormanba72cb82007-11-28 16:21:13 -08005760/*
5761 * When CONFIG_HUGETLB_PAGE_SIZE_VARIABLE is not set, set_pageblock_order()
Andrew Morton955c1cd2012-05-29 15:06:31 -07005762 * is unused as pageblock_order is set at compile-time. See
5763 * include/linux/pageblock-flags.h for the values of pageblock_order based on
5764 * the kernel config
Mel Gormanba72cb82007-11-28 16:21:13 -08005765 */
Chen Gang15ca2202013-09-11 14:20:27 -07005766void __paginginit set_pageblock_order(void)
Mel Gormanba72cb82007-11-28 16:21:13 -08005767{
Mel Gormanba72cb82007-11-28 16:21:13 -08005768}
Mel Gormand9c23402007-10-16 01:26:01 -07005769
5770#endif /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
5771
Jiang Liu01cefae2012-12-12 13:52:19 -08005772static unsigned long __paginginit calc_memmap_size(unsigned long spanned_pages,
5773 unsigned long present_pages)
5774{
5775 unsigned long pages = spanned_pages;
5776
5777 /*
5778 * Provide a more accurate estimation if there are holes within
5779 * the zone and SPARSEMEM is in use. If there are holes within the
5780 * zone, each populated memory region may cost us one or two extra
5781 * memmap pages due to alignment because memmap pages for each
5782 * populated regions may not naturally algined on page boundary.
5783 * So the (present_pages >> 4) heuristic is a tradeoff for that.
5784 */
5785 if (spanned_pages > present_pages + (present_pages >> 4) &&
5786 IS_ENABLED(CONFIG_SPARSEMEM))
5787 pages = present_pages;
5788
5789 return PAGE_ALIGN(pages * sizeof(struct page)) >> PAGE_SHIFT;
5790}
5791
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792/*
5793 * Set up the zone data structures:
5794 * - mark all pages reserved
5795 * - mark all memory queues empty
5796 * - clear the memory bitmaps
Minchan Kim6527af52012-07-31 16:46:16 -07005797 *
5798 * NOTE: pgdat should get zeroed by caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 */
Wei Yang7f3eb552015-09-08 14:59:50 -07005800static void __paginginit free_area_init_core(struct pglist_data *pgdat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801{
Christoph Lameter2f1b6242006-09-25 23:31:13 -07005802 enum zone_type j;
Dave Hansened8ece22005-10-29 18:16:50 -07005803 int nid = pgdat->node_id;
Yasunori Goto718127c2006-06-23 02:03:10 -07005804 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805
Dave Hansen208d54e2005-10-29 18:16:52 -07005806 pgdat_resize_init(pgdat);
Andrea Arcangeli8177a422012-03-23 20:56:34 +01005807#ifdef CONFIG_NUMA_BALANCING
5808 spin_lock_init(&pgdat->numabalancing_migrate_lock);
5809 pgdat->numabalancing_migrate_nr_pages = 0;
5810 pgdat->numabalancing_migrate_next_window = jiffies;
5811#endif
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08005812#ifdef CONFIG_TRANSPARENT_HUGEPAGE
5813 spin_lock_init(&pgdat->split_queue_lock);
5814 INIT_LIST_HEAD(&pgdat->split_queue);
5815 pgdat->split_queue_len = 0;
5816#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 init_waitqueue_head(&pgdat->kswapd_wait);
Mel Gorman55150612012-07-31 16:44:35 -07005818 init_waitqueue_head(&pgdat->pfmemalloc_wait);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07005819#ifdef CONFIG_COMPACTION
5820 init_waitqueue_head(&pgdat->kcompactd_wait);
5821#endif
Joonsoo Kimeefa8642014-12-12 16:55:46 -08005822 pgdat_page_ext_init(pgdat);
Mel Gormana52633d2016-07-28 15:45:28 -07005823 spin_lock_init(&pgdat->lru_lock);
Mel Gormana9dd0a82016-07-28 15:46:02 -07005824 lruvec_init(node_lruvec(pgdat));
Michal Nazarewicz5f63b722012-01-11 15:16:11 +01005825
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 for (j = 0; j < MAX_NR_ZONES; j++) {
5827 struct zone *zone = pgdat->node_zones + j;
Jiang Liu9feedc92012-12-12 13:52:12 -08005828 unsigned long size, realsize, freesize, memmap_pages;
Taku Izumid91749c2016-03-15 14:55:18 -07005829 unsigned long zone_start_pfn = zone->zone_start_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830
Gu Zhengfebd5942015-06-24 16:57:02 -07005831 size = zone->spanned_pages;
5832 realsize = freesize = zone->present_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833
Mel Gorman0e0b8642006-09-27 01:49:56 -07005834 /*
Jiang Liu9feedc92012-12-12 13:52:12 -08005835 * Adjust freesize so that it accounts for how much memory
Mel Gorman0e0b8642006-09-27 01:49:56 -07005836 * is used by this zone for memmap. This affects the watermark
5837 * and per-cpu initialisations
5838 */
Jiang Liu01cefae2012-12-12 13:52:19 -08005839 memmap_pages = calc_memmap_size(size, realsize);
Zhong Hongboba914f42014-12-12 16:56:21 -08005840 if (!is_highmem_idx(j)) {
5841 if (freesize >= memmap_pages) {
5842 freesize -= memmap_pages;
5843 if (memmap_pages)
5844 printk(KERN_DEBUG
5845 " %s zone: %lu pages used for memmap\n",
5846 zone_names[j], memmap_pages);
5847 } else
Joe Perches11705322016-03-17 14:19:50 -07005848 pr_warn(" %s zone: %lu pages exceeds freesize %lu\n",
Zhong Hongboba914f42014-12-12 16:56:21 -08005849 zone_names[j], memmap_pages, freesize);
5850 }
Mel Gorman0e0b8642006-09-27 01:49:56 -07005851
Christoph Lameter62672762007-02-10 01:43:07 -08005852 /* Account for reserved pages */
Jiang Liu9feedc92012-12-12 13:52:12 -08005853 if (j == 0 && freesize > dma_reserve) {
5854 freesize -= dma_reserve;
Yinghai Lud903ef92008-10-18 20:27:06 -07005855 printk(KERN_DEBUG " %s zone: %lu pages reserved\n",
Christoph Lameter62672762007-02-10 01:43:07 -08005856 zone_names[0], dma_reserve);
Mel Gorman0e0b8642006-09-27 01:49:56 -07005857 }
5858
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07005859 if (!is_highmem_idx(j))
Jiang Liu9feedc92012-12-12 13:52:12 -08005860 nr_kernel_pages += freesize;
Jiang Liu01cefae2012-12-12 13:52:19 -08005861 /* Charge for highmem memmap if there are enough kernel pages */
5862 else if (nr_kernel_pages > memmap_pages * 2)
5863 nr_kernel_pages -= memmap_pages;
Jiang Liu9feedc92012-12-12 13:52:12 -08005864 nr_all_pages += freesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865
Jiang Liu9feedc92012-12-12 13:52:12 -08005866 /*
5867 * Set an approximate value for lowmem here, it will be adjusted
5868 * when the bootmem allocator frees pages into the buddy system.
5869 * And all highmem pages will be managed by the buddy system.
5870 */
5871 zone->managed_pages = is_highmem_idx(j) ? realsize : freesize;
Christoph Lameter96146342006-07-03 00:24:13 -07005872#ifdef CONFIG_NUMA
Christoph Lameterd5f541e2006-09-27 01:50:08 -07005873 zone->node = nid;
Christoph Lameter96146342006-07-03 00:24:13 -07005874#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 zone->name = zone_names[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 zone->zone_pgdat = pgdat;
Mel Gormana52633d2016-07-28 15:45:28 -07005877 spin_lock_init(&zone->lock);
5878 zone_seqlock_init(zone);
Dave Hansened8ece22005-10-29 18:16:50 -07005879 zone_pcp_init(zone);
Johannes Weiner81c0a2b2013-09-11 14:20:47 -07005880
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881 if (!size)
5882 continue;
5883
Andrew Morton955c1cd2012-05-29 15:06:31 -07005884 set_pageblock_order();
Linus Torvalds7c455122013-02-18 09:58:02 -08005885 setup_usemap(pgdat, zone, zone_start_pfn, size);
Yaowei Baib171e402015-11-05 18:47:06 -08005886 ret = init_currently_empty_zone(zone, zone_start_pfn, size);
Yasunori Goto718127c2006-06-23 02:03:10 -07005887 BUG_ON(ret);
Heiko Carstens76cdd582008-05-14 16:05:52 -07005888 memmap_init(size, nid, j, zone_start_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 }
5890}
5891
Fabian Frederickbd721ea2016-08-02 14:03:33 -07005892static void __ref alloc_node_mem_map(struct pglist_data *pgdat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893{
Tony Luckb0aeba72015-11-10 10:09:47 -08005894 unsigned long __maybe_unused start = 0;
Laura Abbotta1c34a32015-11-05 18:48:46 -08005895 unsigned long __maybe_unused offset = 0;
5896
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897 /* Skip empty nodes */
5898 if (!pgdat->node_spanned_pages)
5899 return;
5900
Andy Whitcroftd41dee32005-06-23 00:07:54 -07005901#ifdef CONFIG_FLAT_NODE_MEM_MAP
Tony Luckb0aeba72015-11-10 10:09:47 -08005902 start = pgdat->node_start_pfn & ~(MAX_ORDER_NR_PAGES - 1);
5903 offset = pgdat->node_start_pfn - start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 /* ia64 gets its own node_mem_map, before this, without bootmem */
5905 if (!pgdat->node_mem_map) {
Tony Luckb0aeba72015-11-10 10:09:47 -08005906 unsigned long size, end;
Andy Whitcroftd41dee32005-06-23 00:07:54 -07005907 struct page *map;
5908
Bob Piccoe984bb42006-05-20 15:00:31 -07005909 /*
5910 * The zone's endpoints aren't required to be MAX_ORDER
5911 * aligned but the node_mem_map endpoints must be in order
5912 * for the buddy allocator to function correctly.
5913 */
Cody P Schafer108bcc92013-02-22 16:35:23 -08005914 end = pgdat_end_pfn(pgdat);
Bob Piccoe984bb42006-05-20 15:00:31 -07005915 end = ALIGN(end, MAX_ORDER_NR_PAGES);
5916 size = (end - start) * sizeof(struct page);
Dave Hansen6f167ec2005-06-23 00:07:39 -07005917 map = alloc_remap(pgdat->node_id, size);
5918 if (!map)
Santosh Shilimkar67828322014-01-21 15:50:25 -08005919 map = memblock_virt_alloc_node_nopanic(size,
5920 pgdat->node_id);
Laura Abbotta1c34a32015-11-05 18:48:46 -08005921 pgdat->node_mem_map = map + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 }
Roman Zippel12d810c2007-05-31 00:40:54 -07005923#ifndef CONFIG_NEED_MULTIPLE_NODES
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924 /*
5925 * With no DISCONTIG, the global mem_map is just set as node 0's
5926 */
Mel Gormanc7132162006-09-27 01:49:43 -07005927 if (pgdat == NODE_DATA(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928 mem_map = NODE_DATA(0)->node_mem_map;
Laura Abbotta1c34a32015-11-05 18:48:46 -08005929#if defined(CONFIG_HAVE_MEMBLOCK_NODE_MAP) || defined(CONFIG_FLATMEM)
Mel Gormanc7132162006-09-27 01:49:43 -07005930 if (page_to_pfn(mem_map) != pgdat->node_start_pfn)
Laura Abbotta1c34a32015-11-05 18:48:46 -08005931 mem_map -= offset;
Tejun Heo0ee332c2011-12-08 10:22:09 -08005932#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
Mel Gormanc7132162006-09-27 01:49:43 -07005933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934#endif
Andy Whitcroftd41dee32005-06-23 00:07:54 -07005935#endif /* CONFIG_FLAT_NODE_MEM_MAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936}
5937
Johannes Weiner9109fb72008-07-23 21:27:20 -07005938void __paginginit free_area_init_node(int nid, unsigned long *zones_size,
5939 unsigned long node_start_pfn, unsigned long *zholes_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940{
Johannes Weiner9109fb72008-07-23 21:27:20 -07005941 pg_data_t *pgdat = NODE_DATA(nid);
Zhang Yanfei7960aed2013-07-08 15:59:52 -07005942 unsigned long start_pfn = 0;
5943 unsigned long end_pfn = 0;
Johannes Weiner9109fb72008-07-23 21:27:20 -07005944
Minchan Kim88fdf752012-07-31 16:46:14 -07005945 /* pg_data_t should be reset to zero when it's allocated */
Mel Gorman38087d92016-07-28 15:45:49 -07005946 WARN_ON(pgdat->nr_zones || pgdat->kswapd_classzone_idx);
Minchan Kim88fdf752012-07-31 16:46:14 -07005947
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 pgdat->node_id = nid;
5949 pgdat->node_start_pfn = node_start_pfn;
Mel Gorman75ef7182016-07-28 15:45:24 -07005950 pgdat->per_cpu_nodestats = NULL;
Zhang Yanfei7960aed2013-07-08 15:59:52 -07005951#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
5952 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
Juergen Gross8d29e182015-02-11 15:26:01 -08005953 pr_info("Initmem setup node %d [mem %#018Lx-%#018Lx]\n", nid,
Zhen Lei4ada0c52015-09-08 15:04:19 -07005954 (u64)start_pfn << PAGE_SHIFT,
5955 end_pfn ? ((u64)end_pfn << PAGE_SHIFT) - 1 : 0);
Taku Izumid91749c2016-03-15 14:55:18 -07005956#else
5957 start_pfn = node_start_pfn;
Zhang Yanfei7960aed2013-07-08 15:59:52 -07005958#endif
5959 calculate_node_totalpages(pgdat, start_pfn, end_pfn,
5960 zones_size, zholes_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961
5962 alloc_node_mem_map(pgdat);
Yinghai Lue8c27ac2008-06-01 13:15:22 -07005963#ifdef CONFIG_FLAT_NODE_MEM_MAP
5964 printk(KERN_DEBUG "free_area_init_node: node %d, pgdat %08lx, node_mem_map %08lx\n",
5965 nid, (unsigned long)pgdat,
5966 (unsigned long)pgdat->node_mem_map);
5967#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968
Michal Hocko292f70c2017-06-02 14:46:49 -07005969 reset_deferred_meminit(pgdat);
Wei Yang7f3eb552015-09-08 14:59:50 -07005970 free_area_init_core(pgdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971}
5972
Tejun Heo0ee332c2011-12-08 10:22:09 -08005973#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Miklos Szeredi418508c2007-05-23 13:57:55 -07005974
5975#if MAX_NUMNODES > 1
5976/*
5977 * Figure out the number of possible node ids.
5978 */
Cody P Schaferf9872ca2013-04-29 15:08:01 -07005979void __init setup_nr_node_ids(void)
Miklos Szeredi418508c2007-05-23 13:57:55 -07005980{
Wei Yang904a9552015-09-08 14:59:48 -07005981 unsigned int highest;
Miklos Szeredi418508c2007-05-23 13:57:55 -07005982
Wei Yang904a9552015-09-08 14:59:48 -07005983 highest = find_last_bit(node_possible_map.bits, MAX_NUMNODES);
Miklos Szeredi418508c2007-05-23 13:57:55 -07005984 nr_node_ids = highest + 1;
5985}
Miklos Szeredi418508c2007-05-23 13:57:55 -07005986#endif
5987
Mel Gormanc7132162006-09-27 01:49:43 -07005988/**
Tejun Heo1e019792011-07-12 09:45:34 +02005989 * node_map_pfn_alignment - determine the maximum internode alignment
5990 *
5991 * This function should be called after node map is populated and sorted.
5992 * It calculates the maximum power of two alignment which can distinguish
5993 * all the nodes.
5994 *
5995 * For example, if all nodes are 1GiB and aligned to 1GiB, the return value
5996 * would indicate 1GiB alignment with (1 << (30 - PAGE_SHIFT)). If the
5997 * nodes are shifted by 256MiB, 256MiB. Note that if only the last node is
5998 * shifted, 1GiB is enough and this function will indicate so.
5999 *
6000 * This is used to test whether pfn -> nid mapping of the chosen memory
6001 * model has fine enough granularity to avoid incorrect mapping for the
6002 * populated node map.
6003 *
6004 * Returns the determined alignment in pfn's. 0 if there is no alignment
6005 * requirement (single node).
6006 */
6007unsigned long __init node_map_pfn_alignment(void)
6008{
6009 unsigned long accl_mask = 0, last_end = 0;
Tejun Heoc13291a2011-07-12 10:46:30 +02006010 unsigned long start, end, mask;
Tejun Heo1e019792011-07-12 09:45:34 +02006011 int last_nid = -1;
Tejun Heoc13291a2011-07-12 10:46:30 +02006012 int i, nid;
Tejun Heo1e019792011-07-12 09:45:34 +02006013
Tejun Heoc13291a2011-07-12 10:46:30 +02006014 for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, &nid) {
Tejun Heo1e019792011-07-12 09:45:34 +02006015 if (!start || last_nid < 0 || last_nid == nid) {
6016 last_nid = nid;
6017 last_end = end;
6018 continue;
6019 }
6020
6021 /*
6022 * Start with a mask granular enough to pin-point to the
6023 * start pfn and tick off bits one-by-one until it becomes
6024 * too coarse to separate the current node from the last.
6025 */
6026 mask = ~((1 << __ffs(start)) - 1);
6027 while (mask && last_end <= (start & (mask << 1)))
6028 mask <<= 1;
6029
6030 /* accumulate all internode masks */
6031 accl_mask |= mask;
6032 }
6033
6034 /* convert mask to number of pages */
6035 return ~accl_mask + 1;
6036}
6037
Mel Gormana6af2bc2007-02-10 01:42:57 -08006038/* Find the lowest pfn for a node */
Adrian Bunkb69a7282008-07-23 21:28:12 -07006039static unsigned long __init find_min_pfn_for_node(int nid)
Mel Gormanc7132162006-09-27 01:49:43 -07006040{
Mel Gormana6af2bc2007-02-10 01:42:57 -08006041 unsigned long min_pfn = ULONG_MAX;
Tejun Heoc13291a2011-07-12 10:46:30 +02006042 unsigned long start_pfn;
6043 int i;
Mel Gorman1abbfb42006-11-23 12:01:41 +00006044
Tejun Heoc13291a2011-07-12 10:46:30 +02006045 for_each_mem_pfn_range(i, nid, &start_pfn, NULL, NULL)
6046 min_pfn = min(min_pfn, start_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07006047
Mel Gormana6af2bc2007-02-10 01:42:57 -08006048 if (min_pfn == ULONG_MAX) {
Joe Perches11705322016-03-17 14:19:50 -07006049 pr_warn("Could not find start_pfn for node %d\n", nid);
Mel Gormana6af2bc2007-02-10 01:42:57 -08006050 return 0;
6051 }
6052
6053 return min_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07006054}
6055
6056/**
6057 * find_min_pfn_with_active_regions - Find the minimum PFN registered
6058 *
6059 * It returns the minimum PFN based on information provided via
Zhang Zhen7d018172014-06-04 16:10:53 -07006060 * memblock_set_node().
Mel Gormanc7132162006-09-27 01:49:43 -07006061 */
6062unsigned long __init find_min_pfn_with_active_regions(void)
6063{
6064 return find_min_pfn_for_node(MAX_NUMNODES);
6065}
6066
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07006067/*
6068 * early_calculate_totalpages()
6069 * Sum pages in active regions for movable zone.
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08006070 * Populate N_MEMORY for calculating usable_nodes.
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07006071 */
Adrian Bunk484f51f2007-10-16 01:26:03 -07006072static unsigned long __init early_calculate_totalpages(void)
Mel Gorman7e63efe2007-07-17 04:03:15 -07006073{
Mel Gorman7e63efe2007-07-17 04:03:15 -07006074 unsigned long totalpages = 0;
Tejun Heoc13291a2011-07-12 10:46:30 +02006075 unsigned long start_pfn, end_pfn;
6076 int i, nid;
Mel Gorman7e63efe2007-07-17 04:03:15 -07006077
Tejun Heoc13291a2011-07-12 10:46:30 +02006078 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) {
6079 unsigned long pages = end_pfn - start_pfn;
6080
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07006081 totalpages += pages;
6082 if (pages)
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08006083 node_set_state(nid, N_MEMORY);
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07006084 }
Pintu Kumarb8af2942013-09-11 14:20:34 -07006085 return totalpages;
Mel Gorman7e63efe2007-07-17 04:03:15 -07006086}
6087
Mel Gorman2a1e2742007-07-17 04:03:12 -07006088/*
6089 * Find the PFN the Movable zone begins in each node. Kernel memory
6090 * is spread evenly between nodes as long as the nodes have enough
6091 * memory. When they don't, some nodes will have more kernelcore than
6092 * others
6093 */
Kautuk Consulb224ef82012-03-21 16:34:15 -07006094static void __init find_zone_movable_pfns_for_nodes(void)
Mel Gorman2a1e2742007-07-17 04:03:12 -07006095{
6096 int i, nid;
6097 unsigned long usable_startpfn;
6098 unsigned long kernelcore_node, kernelcore_remaining;
Yinghai Lu66918dc2009-06-30 11:41:37 -07006099 /* save the state before borrow the nodemask */
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08006100 nodemask_t saved_node_state = node_states[N_MEMORY];
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07006101 unsigned long totalpages = early_calculate_totalpages();
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08006102 int usable_nodes = nodes_weight(node_states[N_MEMORY]);
Emil Medve136199f2014-04-07 15:37:52 -07006103 struct memblock_region *r;
Tang Chenb2f3eeb2014-01-21 15:49:38 -08006104
6105 /* Need to find movable_zone earlier when movable_node is specified. */
6106 find_usable_zone_for_movable();
Mel Gorman2a1e2742007-07-17 04:03:12 -07006107
Mel Gorman7e63efe2007-07-17 04:03:15 -07006108 /*
Tang Chenb2f3eeb2014-01-21 15:49:38 -08006109 * If movable_node is specified, ignore kernelcore and movablecore
6110 * options.
6111 */
6112 if (movable_node_is_enabled()) {
Emil Medve136199f2014-04-07 15:37:52 -07006113 for_each_memblock(memory, r) {
6114 if (!memblock_is_hotpluggable(r))
Tang Chenb2f3eeb2014-01-21 15:49:38 -08006115 continue;
6116
Emil Medve136199f2014-04-07 15:37:52 -07006117 nid = r->nid;
Tang Chenb2f3eeb2014-01-21 15:49:38 -08006118
Emil Medve136199f2014-04-07 15:37:52 -07006119 usable_startpfn = PFN_DOWN(r->base);
Tang Chenb2f3eeb2014-01-21 15:49:38 -08006120 zone_movable_pfn[nid] = zone_movable_pfn[nid] ?
6121 min(usable_startpfn, zone_movable_pfn[nid]) :
6122 usable_startpfn;
6123 }
6124
6125 goto out2;
6126 }
6127
6128 /*
Taku Izumi342332e2016-03-15 14:55:22 -07006129 * If kernelcore=mirror is specified, ignore movablecore option
6130 */
6131 if (mirrored_kernelcore) {
6132 bool mem_below_4gb_not_mirrored = false;
6133
6134 for_each_memblock(memory, r) {
6135 if (memblock_is_mirror(r))
6136 continue;
6137
6138 nid = r->nid;
6139
6140 usable_startpfn = memblock_region_memory_base_pfn(r);
6141
6142 if (usable_startpfn < 0x100000) {
6143 mem_below_4gb_not_mirrored = true;
6144 continue;
6145 }
6146
6147 zone_movable_pfn[nid] = zone_movable_pfn[nid] ?
6148 min(usable_startpfn, zone_movable_pfn[nid]) :
6149 usable_startpfn;
6150 }
6151
6152 if (mem_below_4gb_not_mirrored)
6153 pr_warn("This configuration results in unmirrored kernel memory.");
6154
6155 goto out2;
6156 }
6157
6158 /*
Tang Chenb2f3eeb2014-01-21 15:49:38 -08006159 * If movablecore=nn[KMG] was specified, calculate what size of
Mel Gorman7e63efe2007-07-17 04:03:15 -07006160 * kernelcore that corresponds so that memory usable for
6161 * any allocation type is evenly spread. If both kernelcore
6162 * and movablecore are specified, then the value of kernelcore
6163 * will be used for required_kernelcore if it's greater than
6164 * what movablecore would have allowed.
6165 */
6166 if (required_movablecore) {
Mel Gorman7e63efe2007-07-17 04:03:15 -07006167 unsigned long corepages;
6168
6169 /*
6170 * Round-up so that ZONE_MOVABLE is at least as large as what
6171 * was requested by the user
6172 */
6173 required_movablecore =
6174 roundup(required_movablecore, MAX_ORDER_NR_PAGES);
Xishi Qiu9fd745d2015-11-05 18:48:11 -08006175 required_movablecore = min(totalpages, required_movablecore);
Mel Gorman7e63efe2007-07-17 04:03:15 -07006176 corepages = totalpages - required_movablecore;
6177
6178 required_kernelcore = max(required_kernelcore, corepages);
6179 }
6180
Xishi Qiubde304b2015-11-05 18:48:56 -08006181 /*
6182 * If kernelcore was not specified or kernelcore size is larger
6183 * than totalpages, there is no ZONE_MOVABLE.
6184 */
6185 if (!required_kernelcore || required_kernelcore >= totalpages)
Yinghai Lu66918dc2009-06-30 11:41:37 -07006186 goto out;
Mel Gorman2a1e2742007-07-17 04:03:12 -07006187
6188 /* usable_startpfn is the lowest possible pfn ZONE_MOVABLE can be at */
Mel Gorman2a1e2742007-07-17 04:03:12 -07006189 usable_startpfn = arch_zone_lowest_possible_pfn[movable_zone];
6190
6191restart:
6192 /* Spread kernelcore memory as evenly as possible throughout nodes */
6193 kernelcore_node = required_kernelcore / usable_nodes;
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08006194 for_each_node_state(nid, N_MEMORY) {
Tejun Heoc13291a2011-07-12 10:46:30 +02006195 unsigned long start_pfn, end_pfn;
6196
Mel Gorman2a1e2742007-07-17 04:03:12 -07006197 /*
6198 * Recalculate kernelcore_node if the division per node
6199 * now exceeds what is necessary to satisfy the requested
6200 * amount of memory for the kernel
6201 */
6202 if (required_kernelcore < kernelcore_node)
6203 kernelcore_node = required_kernelcore / usable_nodes;
6204
6205 /*
6206 * As the map is walked, we track how much memory is usable
6207 * by the kernel using kernelcore_remaining. When it is
6208 * 0, the rest of the node is usable by ZONE_MOVABLE
6209 */
6210 kernelcore_remaining = kernelcore_node;
6211
6212 /* Go through each range of PFNs within this node */
Tejun Heoc13291a2011-07-12 10:46:30 +02006213 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
Mel Gorman2a1e2742007-07-17 04:03:12 -07006214 unsigned long size_pages;
6215
Tejun Heoc13291a2011-07-12 10:46:30 +02006216 start_pfn = max(start_pfn, zone_movable_pfn[nid]);
Mel Gorman2a1e2742007-07-17 04:03:12 -07006217 if (start_pfn >= end_pfn)
6218 continue;
6219
6220 /* Account for what is only usable for kernelcore */
6221 if (start_pfn < usable_startpfn) {
6222 unsigned long kernel_pages;
6223 kernel_pages = min(end_pfn, usable_startpfn)
6224 - start_pfn;
6225
6226 kernelcore_remaining -= min(kernel_pages,
6227 kernelcore_remaining);
6228 required_kernelcore -= min(kernel_pages,
6229 required_kernelcore);
6230
6231 /* Continue if range is now fully accounted */
6232 if (end_pfn <= usable_startpfn) {
6233
6234 /*
6235 * Push zone_movable_pfn to the end so
6236 * that if we have to rebalance
6237 * kernelcore across nodes, we will
6238 * not double account here
6239 */
6240 zone_movable_pfn[nid] = end_pfn;
6241 continue;
6242 }
6243 start_pfn = usable_startpfn;
6244 }
6245
6246 /*
6247 * The usable PFN range for ZONE_MOVABLE is from
6248 * start_pfn->end_pfn. Calculate size_pages as the
6249 * number of pages used as kernelcore
6250 */
6251 size_pages = end_pfn - start_pfn;
6252 if (size_pages > kernelcore_remaining)
6253 size_pages = kernelcore_remaining;
6254 zone_movable_pfn[nid] = start_pfn + size_pages;
6255
6256 /*
6257 * Some kernelcore has been met, update counts and
6258 * break if the kernelcore for this node has been
Pintu Kumarb8af2942013-09-11 14:20:34 -07006259 * satisfied
Mel Gorman2a1e2742007-07-17 04:03:12 -07006260 */
6261 required_kernelcore -= min(required_kernelcore,
6262 size_pages);
6263 kernelcore_remaining -= size_pages;
6264 if (!kernelcore_remaining)
6265 break;
6266 }
6267 }
6268
6269 /*
6270 * If there is still required_kernelcore, we do another pass with one
6271 * less node in the count. This will push zone_movable_pfn[nid] further
6272 * along on the nodes that still have memory until kernelcore is
Pintu Kumarb8af2942013-09-11 14:20:34 -07006273 * satisfied
Mel Gorman2a1e2742007-07-17 04:03:12 -07006274 */
6275 usable_nodes--;
6276 if (usable_nodes && required_kernelcore > usable_nodes)
6277 goto restart;
6278
Tang Chenb2f3eeb2014-01-21 15:49:38 -08006279out2:
Mel Gorman2a1e2742007-07-17 04:03:12 -07006280 /* Align start of ZONE_MOVABLE on all nids to MAX_ORDER_NR_PAGES */
6281 for (nid = 0; nid < MAX_NUMNODES; nid++)
6282 zone_movable_pfn[nid] =
6283 roundup(zone_movable_pfn[nid], MAX_ORDER_NR_PAGES);
Yinghai Lu66918dc2009-06-30 11:41:37 -07006284
Yinghai Lu20e69262013-03-01 14:51:27 -08006285out:
Yinghai Lu66918dc2009-06-30 11:41:37 -07006286 /* restore the node_state */
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08006287 node_states[N_MEMORY] = saved_node_state;
Mel Gorman2a1e2742007-07-17 04:03:12 -07006288}
6289
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08006290/* Any regular or high memory on that node ? */
6291static void check_for_memory(pg_data_t *pgdat, int nid)
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07006292{
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07006293 enum zone_type zone_type;
6294
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08006295 if (N_MEMORY == N_NORMAL_MEMORY)
6296 return;
6297
6298 for (zone_type = 0; zone_type <= ZONE_MOVABLE - 1; zone_type++) {
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07006299 struct zone *zone = &pgdat->node_zones[zone_type];
Xishi Qiub38a8722013-11-12 15:07:20 -08006300 if (populated_zone(zone)) {
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08006301 node_set_state(nid, N_HIGH_MEMORY);
6302 if (N_NORMAL_MEMORY != N_HIGH_MEMORY &&
6303 zone_type <= ZONE_NORMAL)
6304 node_set_state(nid, N_NORMAL_MEMORY);
Bob Liud0048b02012-01-12 17:19:07 -08006305 break;
6306 }
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07006307 }
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07006308}
6309
Mel Gormanc7132162006-09-27 01:49:43 -07006310/**
6311 * free_area_init_nodes - Initialise all pg_data_t and zone data
Randy Dunlap88ca3b92006-10-04 02:15:25 -07006312 * @max_zone_pfn: an array of max PFNs for each zone
Mel Gormanc7132162006-09-27 01:49:43 -07006313 *
6314 * This will call free_area_init_node() for each active node in the system.
Zhang Zhen7d018172014-06-04 16:10:53 -07006315 * Using the page ranges provided by memblock_set_node(), the size of each
Mel Gormanc7132162006-09-27 01:49:43 -07006316 * zone in each node and their holes is calculated. If the maximum PFN
6317 * between two adjacent zones match, it is assumed that the zone is empty.
6318 * For example, if arch_max_dma_pfn == arch_max_dma32_pfn, it is assumed
6319 * that arch_max_dma32_pfn has no pages. It is also assumed that a zone
6320 * starts where the previous one ended. For example, ZONE_DMA32 starts
6321 * at arch_max_dma_pfn.
6322 */
6323void __init free_area_init_nodes(unsigned long *max_zone_pfn)
6324{
Tejun Heoc13291a2011-07-12 10:46:30 +02006325 unsigned long start_pfn, end_pfn;
6326 int i, nid;
Mel Gormana6af2bc2007-02-10 01:42:57 -08006327
Mel Gormanc7132162006-09-27 01:49:43 -07006328 /* Record where the zone boundaries are */
6329 memset(arch_zone_lowest_possible_pfn, 0,
6330 sizeof(arch_zone_lowest_possible_pfn));
6331 memset(arch_zone_highest_possible_pfn, 0,
6332 sizeof(arch_zone_highest_possible_pfn));
Oliver O'Halloran90cae1f2016-07-26 15:22:17 -07006333
6334 start_pfn = find_min_pfn_with_active_regions();
6335
6336 for (i = 0; i < MAX_NR_ZONES; i++) {
Mel Gorman2a1e2742007-07-17 04:03:12 -07006337 if (i == ZONE_MOVABLE)
6338 continue;
Oliver O'Halloran90cae1f2016-07-26 15:22:17 -07006339
6340 end_pfn = max(max_zone_pfn[i], start_pfn);
6341 arch_zone_lowest_possible_pfn[i] = start_pfn;
6342 arch_zone_highest_possible_pfn[i] = end_pfn;
6343
6344 start_pfn = end_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07006345 }
Mel Gorman2a1e2742007-07-17 04:03:12 -07006346 arch_zone_lowest_possible_pfn[ZONE_MOVABLE] = 0;
6347 arch_zone_highest_possible_pfn[ZONE_MOVABLE] = 0;
6348
6349 /* Find the PFNs that ZONE_MOVABLE begins at in each node */
6350 memset(zone_movable_pfn, 0, sizeof(zone_movable_pfn));
Kautuk Consulb224ef82012-03-21 16:34:15 -07006351 find_zone_movable_pfns_for_nodes();
Mel Gormanc7132162006-09-27 01:49:43 -07006352
Mel Gormanc7132162006-09-27 01:49:43 -07006353 /* Print out the zone ranges */
Anton Blanchardf88dfff2014-12-10 15:42:53 -08006354 pr_info("Zone ranges:\n");
Mel Gorman2a1e2742007-07-17 04:03:12 -07006355 for (i = 0; i < MAX_NR_ZONES; i++) {
6356 if (i == ZONE_MOVABLE)
6357 continue;
Anton Blanchardf88dfff2014-12-10 15:42:53 -08006358 pr_info(" %-8s ", zone_names[i]);
David Rientjes72f0ba02010-03-05 13:42:14 -08006359 if (arch_zone_lowest_possible_pfn[i] ==
6360 arch_zone_highest_possible_pfn[i])
Anton Blanchardf88dfff2014-12-10 15:42:53 -08006361 pr_cont("empty\n");
David Rientjes72f0ba02010-03-05 13:42:14 -08006362 else
Juergen Gross8d29e182015-02-11 15:26:01 -08006363 pr_cont("[mem %#018Lx-%#018Lx]\n",
6364 (u64)arch_zone_lowest_possible_pfn[i]
6365 << PAGE_SHIFT,
6366 ((u64)arch_zone_highest_possible_pfn[i]
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07006367 << PAGE_SHIFT) - 1);
Mel Gorman2a1e2742007-07-17 04:03:12 -07006368 }
6369
6370 /* Print out the PFNs ZONE_MOVABLE begins at in each node */
Anton Blanchardf88dfff2014-12-10 15:42:53 -08006371 pr_info("Movable zone start for each node\n");
Mel Gorman2a1e2742007-07-17 04:03:12 -07006372 for (i = 0; i < MAX_NUMNODES; i++) {
6373 if (zone_movable_pfn[i])
Juergen Gross8d29e182015-02-11 15:26:01 -08006374 pr_info(" Node %d: %#018Lx\n", i,
6375 (u64)zone_movable_pfn[i] << PAGE_SHIFT);
Mel Gorman2a1e2742007-07-17 04:03:12 -07006376 }
Mel Gormanc7132162006-09-27 01:49:43 -07006377
Wanpeng Lif2d52fe2012-10-08 16:32:24 -07006378 /* Print out the early node map */
Anton Blanchardf88dfff2014-12-10 15:42:53 -08006379 pr_info("Early memory node ranges\n");
Tejun Heoc13291a2011-07-12 10:46:30 +02006380 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid)
Juergen Gross8d29e182015-02-11 15:26:01 -08006381 pr_info(" node %3d: [mem %#018Lx-%#018Lx]\n", nid,
6382 (u64)start_pfn << PAGE_SHIFT,
6383 ((u64)end_pfn << PAGE_SHIFT) - 1);
Mel Gormanc7132162006-09-27 01:49:43 -07006384
6385 /* Initialise every node */
Mel Gorman708614e2008-07-23 21:26:51 -07006386 mminit_verify_pageflags_layout();
Christoph Lameter8ef82862007-02-20 13:57:52 -08006387 setup_nr_node_ids();
Mel Gormanc7132162006-09-27 01:49:43 -07006388 for_each_online_node(nid) {
6389 pg_data_t *pgdat = NODE_DATA(nid);
Johannes Weiner9109fb72008-07-23 21:27:20 -07006390 free_area_init_node(nid, NULL,
Mel Gormanc7132162006-09-27 01:49:43 -07006391 find_min_pfn_for_node(nid), NULL);
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07006392
6393 /* Any memory on that node */
6394 if (pgdat->node_present_pages)
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08006395 node_set_state(nid, N_MEMORY);
6396 check_for_memory(pgdat, nid);
Mel Gormanc7132162006-09-27 01:49:43 -07006397 }
6398}
Mel Gorman2a1e2742007-07-17 04:03:12 -07006399
Mel Gorman7e63efe2007-07-17 04:03:15 -07006400static int __init cmdline_parse_core(char *p, unsigned long *core)
Mel Gorman2a1e2742007-07-17 04:03:12 -07006401{
6402 unsigned long long coremem;
6403 if (!p)
6404 return -EINVAL;
6405
6406 coremem = memparse(p, &p);
Mel Gorman7e63efe2007-07-17 04:03:15 -07006407 *core = coremem >> PAGE_SHIFT;
Mel Gorman2a1e2742007-07-17 04:03:12 -07006408
Mel Gorman7e63efe2007-07-17 04:03:15 -07006409 /* Paranoid check that UL is enough for the coremem value */
Mel Gorman2a1e2742007-07-17 04:03:12 -07006410 WARN_ON((coremem >> PAGE_SHIFT) > ULONG_MAX);
6411
6412 return 0;
6413}
Mel Gormaned7ed362007-07-17 04:03:14 -07006414
Mel Gorman7e63efe2007-07-17 04:03:15 -07006415/*
6416 * kernelcore=size sets the amount of memory for use for allocations that
6417 * cannot be reclaimed or migrated.
6418 */
6419static int __init cmdline_parse_kernelcore(char *p)
6420{
Taku Izumi342332e2016-03-15 14:55:22 -07006421 /* parse kernelcore=mirror */
6422 if (parse_option_str(p, "mirror")) {
6423 mirrored_kernelcore = true;
6424 return 0;
6425 }
6426
Mel Gorman7e63efe2007-07-17 04:03:15 -07006427 return cmdline_parse_core(p, &required_kernelcore);
6428}
6429
6430/*
6431 * movablecore=size sets the amount of memory for use for allocations that
6432 * can be reclaimed or migrated.
6433 */
6434static int __init cmdline_parse_movablecore(char *p)
6435{
6436 return cmdline_parse_core(p, &required_movablecore);
6437}
6438
Mel Gormaned7ed362007-07-17 04:03:14 -07006439early_param("kernelcore", cmdline_parse_kernelcore);
Mel Gorman7e63efe2007-07-17 04:03:15 -07006440early_param("movablecore", cmdline_parse_movablecore);
Mel Gormaned7ed362007-07-17 04:03:14 -07006441
Tejun Heo0ee332c2011-12-08 10:22:09 -08006442#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
Mel Gormanc7132162006-09-27 01:49:43 -07006443
Jiang Liuc3d5f5f2013-07-03 15:03:14 -07006444void adjust_managed_page_count(struct page *page, long count)
6445{
6446 spin_lock(&managed_page_count_lock);
6447 page_zone(page)->managed_pages += count;
6448 totalram_pages += count;
Jiang Liu3dcc0572013-07-03 15:03:21 -07006449#ifdef CONFIG_HIGHMEM
6450 if (PageHighMem(page))
6451 totalhigh_pages += count;
6452#endif
Jiang Liuc3d5f5f2013-07-03 15:03:14 -07006453 spin_unlock(&managed_page_count_lock);
6454}
Jiang Liu3dcc0572013-07-03 15:03:21 -07006455EXPORT_SYMBOL(adjust_managed_page_count);
Jiang Liuc3d5f5f2013-07-03 15:03:14 -07006456
Jiang Liu11199692013-07-03 15:02:48 -07006457unsigned long free_reserved_area(void *start, void *end, int poison, char *s)
Jiang Liu69afade2013-04-29 15:06:21 -07006458{
Jiang Liu11199692013-07-03 15:02:48 -07006459 void *pos;
6460 unsigned long pages = 0;
Jiang Liu69afade2013-04-29 15:06:21 -07006461
Jiang Liu11199692013-07-03 15:02:48 -07006462 start = (void *)PAGE_ALIGN((unsigned long)start);
6463 end = (void *)((unsigned long)end & PAGE_MASK);
6464 for (pos = start; pos < end; pos += PAGE_SIZE, pages++) {
Jiang Liudbe67df2013-07-03 15:02:51 -07006465 if ((unsigned int)poison <= 0xFF)
Jiang Liu11199692013-07-03 15:02:48 -07006466 memset(pos, poison, PAGE_SIZE);
6467 free_reserved_page(virt_to_page(pos));
Jiang Liu69afade2013-04-29 15:06:21 -07006468 }
6469
6470 if (pages && s)
Josh Poimboeuf7b95b742016-10-25 09:51:14 -05006471 pr_info("Freeing %s memory: %ldK\n",
6472 s, pages << (PAGE_SHIFT - 10));
Jiang Liu69afade2013-04-29 15:06:21 -07006473
6474 return pages;
6475}
Jiang Liu11199692013-07-03 15:02:48 -07006476EXPORT_SYMBOL(free_reserved_area);
Jiang Liu69afade2013-04-29 15:06:21 -07006477
Jiang Liucfa11e02013-04-29 15:07:00 -07006478#ifdef CONFIG_HIGHMEM
6479void free_highmem_page(struct page *page)
6480{
6481 __free_reserved_page(page);
6482 totalram_pages++;
Jiang Liu7b4b2a02013-07-03 15:03:11 -07006483 page_zone(page)->managed_pages++;
Jiang Liucfa11e02013-04-29 15:07:00 -07006484 totalhigh_pages++;
6485}
6486#endif
6487
Jiang Liu7ee3d4e2013-07-03 15:03:41 -07006488
6489void __init mem_init_print_info(const char *str)
6490{
6491 unsigned long physpages, codesize, datasize, rosize, bss_size;
6492 unsigned long init_code_size, init_data_size;
6493
6494 physpages = get_num_physpages();
6495 codesize = _etext - _stext;
6496 datasize = _edata - _sdata;
6497 rosize = __end_rodata - __start_rodata;
6498 bss_size = __bss_stop - __bss_start;
6499 init_data_size = __init_end - __init_begin;
6500 init_code_size = _einittext - _sinittext;
6501
6502 /*
6503 * Detect special cases and adjust section sizes accordingly:
6504 * 1) .init.* may be embedded into .data sections
6505 * 2) .init.text.* may be out of [__init_begin, __init_end],
6506 * please refer to arch/tile/kernel/vmlinux.lds.S.
6507 * 3) .rodata.* may be embedded into .text or .data sections.
6508 */
6509#define adj_init_size(start, end, size, pos, adj) \
Pintu Kumarb8af2942013-09-11 14:20:34 -07006510 do { \
6511 if (start <= pos && pos < end && size > adj) \
6512 size -= adj; \
6513 } while (0)
Jiang Liu7ee3d4e2013-07-03 15:03:41 -07006514
6515 adj_init_size(__init_begin, __init_end, init_data_size,
6516 _sinittext, init_code_size);
6517 adj_init_size(_stext, _etext, codesize, _sinittext, init_code_size);
6518 adj_init_size(_sdata, _edata, datasize, __init_begin, init_data_size);
6519 adj_init_size(_stext, _etext, codesize, __start_rodata, rosize);
6520 adj_init_size(_sdata, _edata, datasize, __start_rodata, rosize);
6521
6522#undef adj_init_size
6523
Joe Perches756a025f02016-03-17 14:19:47 -07006524 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 -07006525#ifdef CONFIG_HIGHMEM
Joe Perches756a025f02016-03-17 14:19:47 -07006526 ", %luK highmem"
Jiang Liu7ee3d4e2013-07-03 15:03:41 -07006527#endif
Joe Perches756a025f02016-03-17 14:19:47 -07006528 "%s%s)\n",
6529 nr_free_pages() << (PAGE_SHIFT - 10),
6530 physpages << (PAGE_SHIFT - 10),
6531 codesize >> 10, datasize >> 10, rosize >> 10,
6532 (init_data_size + init_code_size) >> 10, bss_size >> 10,
6533 (physpages - totalram_pages - totalcma_pages) << (PAGE_SHIFT - 10),
6534 totalcma_pages << (PAGE_SHIFT - 10),
Jiang Liu7ee3d4e2013-07-03 15:03:41 -07006535#ifdef CONFIG_HIGHMEM
Joe Perches756a025f02016-03-17 14:19:47 -07006536 totalhigh_pages << (PAGE_SHIFT - 10),
Jiang Liu7ee3d4e2013-07-03 15:03:41 -07006537#endif
Joe Perches756a025f02016-03-17 14:19:47 -07006538 str ? ", " : "", str ? str : "");
Jiang Liu7ee3d4e2013-07-03 15:03:41 -07006539}
6540
Mel Gorman0e0b8642006-09-27 01:49:56 -07006541/**
Randy Dunlap88ca3b92006-10-04 02:15:25 -07006542 * set_dma_reserve - set the specified number of pages reserved in the first zone
6543 * @new_dma_reserve: The number of pages to mark reserved
Mel Gorman0e0b8642006-09-27 01:49:56 -07006544 *
Yaowei Bai013110a2015-09-08 15:04:10 -07006545 * The per-cpu batchsize and zone watermarks are determined by managed_pages.
Mel Gorman0e0b8642006-09-27 01:49:56 -07006546 * In the DMA zone, a significant percentage may be consumed by kernel image
6547 * and other unfreeable allocations which can skew the watermarks badly. This
Randy Dunlap88ca3b92006-10-04 02:15:25 -07006548 * function may optionally be used to account for unfreeable pages in the
6549 * first zone (e.g., ZONE_DMA). The effect will be lower watermarks and
6550 * smaller per-cpu batchsize.
Mel Gorman0e0b8642006-09-27 01:49:56 -07006551 */
6552void __init set_dma_reserve(unsigned long new_dma_reserve)
6553{
6554 dma_reserve = new_dma_reserve;
6555}
6556
Linus Torvalds1da177e2005-04-16 15:20:36 -07006557void __init free_area_init(unsigned long *zones_size)
6558{
Johannes Weiner9109fb72008-07-23 21:27:20 -07006559 free_area_init_node(0, zones_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
6561}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563static int page_alloc_cpu_notify(struct notifier_block *self,
6564 unsigned long action, void *hcpu)
6565{
6566 int cpu = (unsigned long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006568 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
Konstantin Khlebnikovf0cb3c72012-03-21 16:34:06 -07006569 lru_add_drain_cpu(cpu);
Christoph Lameter9f8f2172008-02-04 22:29:11 -08006570 drain_pages(cpu);
6571
6572 /*
6573 * Spill the event counters of the dead processor
6574 * into the current processors event counters.
6575 * This artificially elevates the count of the current
6576 * processor.
6577 */
Christoph Lameterf8891e52006-06-30 01:55:45 -07006578 vm_events_fold_cpu(cpu);
Christoph Lameter9f8f2172008-02-04 22:29:11 -08006579
6580 /*
6581 * Zero the differential counters of the dead processor
6582 * so that the vm statistics are consistent.
6583 *
6584 * This is only okay since the processor is dead and cannot
6585 * race with what we are doing.
6586 */
Christoph Lameter2bb921e2013-09-11 14:21:30 -07006587 cpu_vm_stats_fold(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006588 }
6589 return NOTIFY_OK;
6590}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591
6592void __init page_alloc_init(void)
6593{
6594 hotcpu_notifier(page_alloc_cpu_notify, 0);
6595}
6596
6597/*
Yaowei Bai34b10062015-09-08 15:04:13 -07006598 * calculate_totalreserve_pages - called when sysctl_lowmem_reserve_ratio
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07006599 * or min_free_kbytes changes.
6600 */
6601static void calculate_totalreserve_pages(void)
6602{
6603 struct pglist_data *pgdat;
6604 unsigned long reserve_pages = 0;
Christoph Lameter2f6726e2006-09-25 23:31:18 -07006605 enum zone_type i, j;
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07006606
6607 for_each_online_pgdat(pgdat) {
Mel Gorman281e3722016-07-28 15:46:11 -07006608
6609 pgdat->totalreserve_pages = 0;
6610
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07006611 for (i = 0; i < MAX_NR_ZONES; i++) {
6612 struct zone *zone = pgdat->node_zones + i;
Mel Gorman3484b2d2014-08-06 16:07:14 -07006613 long max = 0;
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07006614
6615 /* Find valid and maximum lowmem_reserve in the zone */
6616 for (j = i; j < MAX_NR_ZONES; j++) {
6617 if (zone->lowmem_reserve[j] > max)
6618 max = zone->lowmem_reserve[j];
6619 }
6620
Mel Gorman41858962009-06-16 15:32:12 -07006621 /* we treat the high watermark as reserved pages. */
6622 max += high_wmark_pages(zone);
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07006623
Jiang Liub40da042013-02-22 16:33:52 -08006624 if (max > zone->managed_pages)
6625 max = zone->managed_pages;
Johannes Weinera8d01432016-01-14 15:20:15 -08006626
Mel Gorman281e3722016-07-28 15:46:11 -07006627 pgdat->totalreserve_pages += max;
Johannes Weinera8d01432016-01-14 15:20:15 -08006628
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07006629 reserve_pages += max;
6630 }
6631 }
6632 totalreserve_pages = reserve_pages;
6633}
6634
6635/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636 * setup_per_zone_lowmem_reserve - called whenever
Yaowei Bai34b10062015-09-08 15:04:13 -07006637 * sysctl_lowmem_reserve_ratio changes. Ensures that each zone
Linus Torvalds1da177e2005-04-16 15:20:36 -07006638 * has a correct pages reserved value, so an adequate number of
6639 * pages are left in the zone after a successful __alloc_pages().
6640 */
6641static void setup_per_zone_lowmem_reserve(void)
6642{
6643 struct pglist_data *pgdat;
Christoph Lameter2f6726e2006-09-25 23:31:18 -07006644 enum zone_type j, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006645
KAMEZAWA Hiroyukiec936fc2006-03-27 01:15:59 -08006646 for_each_online_pgdat(pgdat) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006647 for (j = 0; j < MAX_NR_ZONES; j++) {
6648 struct zone *zone = pgdat->node_zones + j;
Jiang Liub40da042013-02-22 16:33:52 -08006649 unsigned long managed_pages = zone->managed_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650
6651 zone->lowmem_reserve[j] = 0;
6652
Christoph Lameter2f6726e2006-09-25 23:31:18 -07006653 idx = j;
6654 while (idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006655 struct zone *lower_zone;
6656
Christoph Lameter2f6726e2006-09-25 23:31:18 -07006657 idx--;
6658
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659 if (sysctl_lowmem_reserve_ratio[idx] < 1)
6660 sysctl_lowmem_reserve_ratio[idx] = 1;
6661
6662 lower_zone = pgdat->node_zones + idx;
Jiang Liub40da042013-02-22 16:33:52 -08006663 lower_zone->lowmem_reserve[j] = managed_pages /
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664 sysctl_lowmem_reserve_ratio[idx];
Jiang Liub40da042013-02-22 16:33:52 -08006665 managed_pages += lower_zone->managed_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666 }
6667 }
6668 }
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07006669
6670 /* update totalreserve_pages */
6671 calculate_totalreserve_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672}
6673
Mel Gormancfd3da12011-04-25 21:36:42 +00006674static void __setup_per_zone_wmarks(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006675{
6676 unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
Rik van Rieldbe0f612011-09-01 15:26:50 -04006677 unsigned long pages_low = extra_free_kbytes >> (PAGE_SHIFT - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006678 unsigned long lowmem_pages = 0;
6679 struct zone *zone;
6680 unsigned long flags;
6681
6682 /* Calculate total number of !ZONE_HIGHMEM pages */
6683 for_each_zone(zone) {
6684 if (!is_highmem(zone))
Jiang Liub40da042013-02-22 16:33:52 -08006685 lowmem_pages += zone->managed_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686 }
6687
6688 for_each_zone(zone) {
Rik van Rieldbe0f612011-09-01 15:26:50 -04006689 u64 min, low;
Andrew Mortonac924c62006-05-15 09:43:59 -07006690
Gerald Schaefer1125b4e2008-10-18 20:27:11 -07006691 spin_lock_irqsave(&zone->lock, flags);
Rik van Rieldbe0f612011-09-01 15:26:50 -04006692 min = (u64)pages_min * zone->managed_pages;
6693 do_div(min, lowmem_pages);
6694 low = (u64)pages_low * zone->managed_pages;
6695 do_div(low, vm_total_pages);
6696
Linus Torvalds1da177e2005-04-16 15:20:36 -07006697 if (is_highmem(zone)) {
6698 /*
Nick Piggin669ed172005-11-13 16:06:45 -08006699 * __GFP_HIGH and PF_MEMALLOC allocations usually don't
6700 * need highmem pages, so cap pages_min to a small
6701 * value here.
6702 *
Mel Gorman41858962009-06-16 15:32:12 -07006703 * The WMARK_HIGH-WMARK_LOW and (WMARK_LOW-WMARK_MIN)
Yaowei Bai42ff2702015-04-14 15:47:14 -07006704 * deltas control asynch page reclaim, and so should
Nick Piggin669ed172005-11-13 16:06:45 -08006705 * not be capped for highmem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706 */
Andrew Morton90ae8d62013-02-22 16:32:22 -08006707 unsigned long min_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708
Jiang Liub40da042013-02-22 16:33:52 -08006709 min_pages = zone->managed_pages / 1024;
Andrew Morton90ae8d62013-02-22 16:32:22 -08006710 min_pages = clamp(min_pages, SWAP_CLUSTER_MAX, 128UL);
Mel Gorman41858962009-06-16 15:32:12 -07006711 zone->watermark[WMARK_MIN] = min_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006712 } else {
Nick Piggin669ed172005-11-13 16:06:45 -08006713 /*
6714 * If it's a lowmem zone, reserve a number of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715 * proportionate to the zone's size.
6716 */
Rik van Rieldbe0f612011-09-01 15:26:50 -04006717 zone->watermark[WMARK_MIN] = min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006718 }
6719
Johannes Weiner795ae7a2016-03-17 14:19:14 -07006720 /*
6721 * Set the kswapd watermarks distance according to the
6722 * scale factor in proportion to available memory, but
6723 * ensure a minimum size on small systems.
6724 */
Rik van Rieldbe0f612011-09-01 15:26:50 -04006725 min = max_t(u64, min >> 2,
Johannes Weiner795ae7a2016-03-17 14:19:14 -07006726 mult_frac(zone->managed_pages,
6727 watermark_scale_factor, 10000));
6728
Rik van Rieldbe0f612011-09-01 15:26:50 -04006729 zone->watermark[WMARK_LOW] = min_wmark_pages(zone) +
6730 low + min;
6731 zone->watermark[WMARK_HIGH] = min_wmark_pages(zone) +
6732 low + min * 2;
Marek Szyprowski49f223a2012-01-25 12:49:24 +01006733
Gerald Schaefer1125b4e2008-10-18 20:27:11 -07006734 spin_unlock_irqrestore(&zone->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006735 }
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07006736
6737 /* update totalreserve_pages */
6738 calculate_totalreserve_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739}
6740
Mel Gormancfd3da12011-04-25 21:36:42 +00006741/**
6742 * setup_per_zone_wmarks - called when min_free_kbytes changes
6743 * or when memory is hot-{added|removed}
6744 *
6745 * Ensures that the watermark[min,low,high] values for each zone are set
6746 * correctly with respect to min_free_kbytes.
6747 */
6748void setup_per_zone_wmarks(void)
6749{
6750 mutex_lock(&zonelists_mutex);
6751 __setup_per_zone_wmarks();
6752 mutex_unlock(&zonelists_mutex);
6753}
6754
Randy Dunlap55a44622009-09-21 17:01:20 -07006755/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756 * Initialise min_free_kbytes.
6757 *
6758 * For small machines we want it small (128k min). For large machines
6759 * we want it large (64MB max). But it is not linear, because network
6760 * bandwidth does not increase linearly with machine size. We use
6761 *
Pintu Kumarb8af2942013-09-11 14:20:34 -07006762 * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763 * min_free_kbytes = sqrt(lowmem_kbytes * 16)
6764 *
6765 * which yields
6766 *
6767 * 16MB: 512k
6768 * 32MB: 724k
6769 * 64MB: 1024k
6770 * 128MB: 1448k
6771 * 256MB: 2048k
6772 * 512MB: 2896k
6773 * 1024MB: 4096k
6774 * 2048MB: 5792k
6775 * 4096MB: 8192k
6776 * 8192MB: 11584k
6777 * 16384MB: 16384k
6778 */
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07006779int __meminit init_per_zone_wmark_min(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006780{
6781 unsigned long lowmem_kbytes;
Michal Hocko5f127332013-07-08 16:00:40 -07006782 int new_min_free_kbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783
6784 lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
Michal Hocko5f127332013-07-08 16:00:40 -07006785 new_min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786
Michal Hocko5f127332013-07-08 16:00:40 -07006787 if (new_min_free_kbytes > user_min_free_kbytes) {
6788 min_free_kbytes = new_min_free_kbytes;
6789 if (min_free_kbytes < 128)
6790 min_free_kbytes = 128;
6791 if (min_free_kbytes > 65536)
6792 min_free_kbytes = 65536;
6793 } else {
6794 pr_warn("min_free_kbytes is not updated to %d because user defined value %d is preferred\n",
6795 new_min_free_kbytes, user_min_free_kbytes);
6796 }
Minchan Kimbc75d332009-06-16 15:32:48 -07006797 setup_per_zone_wmarks();
KOSAKI Motohiroa6cccdc2011-05-24 17:11:33 -07006798 refresh_zone_stat_thresholds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006799 setup_per_zone_lowmem_reserve();
Joonsoo Kim6423aa82016-08-10 16:27:49 -07006800
6801#ifdef CONFIG_NUMA
6802 setup_min_unmapped_ratio();
6803 setup_min_slab_ratio();
6804#endif
6805
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806 return 0;
6807}
Jason Baronbc22af72016-05-05 16:22:12 -07006808core_initcall(init_per_zone_wmark_min)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809
6810/*
Pintu Kumarb8af2942013-09-11 14:20:34 -07006811 * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812 * that we can call two helper functions whenever min_free_kbytes
Rik van Rieldbe0f612011-09-01 15:26:50 -04006813 * or extra_free_kbytes changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006814 */
Joe Perchescccad5b2014-06-06 14:38:09 -07006815int min_free_kbytes_sysctl_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07006816 void __user *buffer, size_t *length, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006817{
Han Pingtianda8c7572014-01-23 15:53:17 -08006818 int rc;
6819
6820 rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
6821 if (rc)
6822 return rc;
6823
Michal Hocko5f127332013-07-08 16:00:40 -07006824 if (write) {
6825 user_min_free_kbytes = min_free_kbytes;
Minchan Kimbc75d332009-06-16 15:32:48 -07006826 setup_per_zone_wmarks();
Michal Hocko5f127332013-07-08 16:00:40 -07006827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828 return 0;
6829}
6830
Johannes Weiner795ae7a2016-03-17 14:19:14 -07006831int watermark_scale_factor_sysctl_handler(struct ctl_table *table, int write,
6832 void __user *buffer, size_t *length, loff_t *ppos)
6833{
6834 int rc;
6835
6836 rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
6837 if (rc)
6838 return rc;
6839
6840 if (write)
6841 setup_per_zone_wmarks();
6842
6843 return 0;
6844}
6845
Christoph Lameter96146342006-07-03 00:24:13 -07006846#ifdef CONFIG_NUMA
Joonsoo Kim6423aa82016-08-10 16:27:49 -07006847static void setup_min_unmapped_ratio(void)
Christoph Lameter96146342006-07-03 00:24:13 -07006848{
Joonsoo Kim6423aa82016-08-10 16:27:49 -07006849 pg_data_t *pgdat;
Christoph Lameter96146342006-07-03 00:24:13 -07006850 struct zone *zone;
Christoph Lameter96146342006-07-03 00:24:13 -07006851
Mel Gormana5f5f912016-07-28 15:46:32 -07006852 for_each_online_pgdat(pgdat)
Joonsoo Kim81cbcbc2016-08-10 16:27:46 -07006853 pgdat->min_unmapped_pages = 0;
Mel Gormana5f5f912016-07-28 15:46:32 -07006854
Christoph Lameter96146342006-07-03 00:24:13 -07006855 for_each_zone(zone)
Mel Gormana5f5f912016-07-28 15:46:32 -07006856 zone->zone_pgdat->min_unmapped_pages += (zone->managed_pages *
Christoph Lameter96146342006-07-03 00:24:13 -07006857 sysctl_min_unmapped_ratio) / 100;
Christoph Lameter96146342006-07-03 00:24:13 -07006858}
Christoph Lameter0ff38492006-09-25 23:31:52 -07006859
Joonsoo Kim6423aa82016-08-10 16:27:49 -07006860
6861int sysctl_min_unmapped_ratio_sysctl_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07006862 void __user *buffer, size_t *length, loff_t *ppos)
Christoph Lameter0ff38492006-09-25 23:31:52 -07006863{
Christoph Lameter0ff38492006-09-25 23:31:52 -07006864 int rc;
6865
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07006866 rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
Christoph Lameter0ff38492006-09-25 23:31:52 -07006867 if (rc)
6868 return rc;
6869
Joonsoo Kim6423aa82016-08-10 16:27:49 -07006870 setup_min_unmapped_ratio();
6871
6872 return 0;
6873}
6874
6875static void setup_min_slab_ratio(void)
6876{
6877 pg_data_t *pgdat;
6878 struct zone *zone;
6879
Mel Gormana5f5f912016-07-28 15:46:32 -07006880 for_each_online_pgdat(pgdat)
6881 pgdat->min_slab_pages = 0;
6882
Christoph Lameter0ff38492006-09-25 23:31:52 -07006883 for_each_zone(zone)
Mel Gormana5f5f912016-07-28 15:46:32 -07006884 zone->zone_pgdat->min_slab_pages += (zone->managed_pages *
Christoph Lameter0ff38492006-09-25 23:31:52 -07006885 sysctl_min_slab_ratio) / 100;
Joonsoo Kim6423aa82016-08-10 16:27:49 -07006886}
6887
6888int sysctl_min_slab_ratio_sysctl_handler(struct ctl_table *table, int write,
6889 void __user *buffer, size_t *length, loff_t *ppos)
6890{
6891 int rc;
6892
6893 rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
6894 if (rc)
6895 return rc;
6896
6897 setup_min_slab_ratio();
6898
Christoph Lameter0ff38492006-09-25 23:31:52 -07006899 return 0;
6900}
Christoph Lameter96146342006-07-03 00:24:13 -07006901#endif
6902
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903/*
6904 * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
6905 * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
6906 * whenever sysctl_lowmem_reserve_ratio changes.
6907 *
6908 * The reserve ratio obviously has absolutely no relation with the
Mel Gorman41858962009-06-16 15:32:12 -07006909 * minimum watermarks. The lowmem reserve ratio can only make sense
Linus Torvalds1da177e2005-04-16 15:20:36 -07006910 * if in function of the boot time zone sizes.
6911 */
Joe Perchescccad5b2014-06-06 14:38:09 -07006912int lowmem_reserve_ratio_sysctl_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07006913 void __user *buffer, size_t *length, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07006915 proc_dointvec_minmax(table, write, buffer, length, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916 setup_per_zone_lowmem_reserve();
6917 return 0;
6918}
6919
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08006920/*
6921 * percpu_pagelist_fraction - changes the pcp->high for each zone on each
Pintu Kumarb8af2942013-09-11 14:20:34 -07006922 * cpu. It is the fraction of total pages in each zone that a hot per cpu
6923 * pagelist can have before it gets flushed back to buddy allocator.
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08006924 */
Joe Perchescccad5b2014-06-06 14:38:09 -07006925int percpu_pagelist_fraction_sysctl_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07006926 void __user *buffer, size_t *length, loff_t *ppos)
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08006927{
6928 struct zone *zone;
David Rientjes7cd2b0a2014-06-23 13:22:04 -07006929 int old_percpu_pagelist_fraction;
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08006930 int ret;
6931
Cody P Schaferc8e251f2013-07-03 15:01:29 -07006932 mutex_lock(&pcp_batch_high_lock);
David Rientjes7cd2b0a2014-06-23 13:22:04 -07006933 old_percpu_pagelist_fraction = percpu_pagelist_fraction;
6934
6935 ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
6936 if (!write || ret < 0)
6937 goto out;
6938
6939 /* Sanity checking to avoid pcp imbalance */
6940 if (percpu_pagelist_fraction &&
6941 percpu_pagelist_fraction < MIN_PERCPU_PAGELIST_FRACTION) {
6942 percpu_pagelist_fraction = old_percpu_pagelist_fraction;
6943 ret = -EINVAL;
6944 goto out;
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08006945 }
David Rientjes7cd2b0a2014-06-23 13:22:04 -07006946
6947 /* No change? */
6948 if (percpu_pagelist_fraction == old_percpu_pagelist_fraction)
6949 goto out;
6950
6951 for_each_populated_zone(zone) {
6952 unsigned int cpu;
6953
6954 for_each_possible_cpu(cpu)
6955 pageset_set_high_and_batch(zone,
6956 per_cpu_ptr(zone->pageset, cpu));
6957 }
6958out:
Cody P Schaferc8e251f2013-07-03 15:01:29 -07006959 mutex_unlock(&pcp_batch_high_lock);
David Rientjes7cd2b0a2014-06-23 13:22:04 -07006960 return ret;
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08006961}
6962
Rasmus Villemoesa9919c72015-06-24 16:56:28 -07006963#ifdef CONFIG_NUMA
David S. Millerf034b5d2006-08-24 03:08:07 -07006964int hashdist = HASHDIST_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965
Linus Torvalds1da177e2005-04-16 15:20:36 -07006966static int __init set_hashdist(char *str)
6967{
6968 if (!str)
6969 return 0;
6970 hashdist = simple_strtoul(str, &str, 0);
6971 return 1;
6972}
6973__setup("hashdist=", set_hashdist);
6974#endif
6975
Srikar Dronamrajuf6f34b42016-10-07 16:59:15 -07006976#ifndef __HAVE_ARCH_RESERVED_KERNEL_PAGES
6977/*
6978 * Returns the number of pages that arch has reserved but
6979 * is not known to alloc_large_system_hash().
6980 */
6981static unsigned long __init arch_reserved_kernel_pages(void)
6982{
6983 return 0;
6984}
6985#endif
6986
Linus Torvalds1da177e2005-04-16 15:20:36 -07006987/*
6988 * allocate a large system hash table from bootmem
6989 * - it is assumed that the hash table must contain an exact power-of-2
6990 * quantity of entries
6991 * - limit is the number of hash buckets, not the total allocation size
6992 */
6993void *__init alloc_large_system_hash(const char *tablename,
6994 unsigned long bucketsize,
6995 unsigned long numentries,
6996 int scale,
6997 int flags,
6998 unsigned int *_hash_shift,
6999 unsigned int *_hash_mask,
Tim Bird31fe62b2012-05-23 13:33:35 +00007000 unsigned long low_limit,
7001 unsigned long high_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002{
Tim Bird31fe62b2012-05-23 13:33:35 +00007003 unsigned long long max = high_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007004 unsigned long log2qty, size;
7005 void *table = NULL;
7006
7007 /* allow the kernel cmdline to have a say */
7008 if (!numentries) {
7009 /* round applicable memory size up to nearest megabyte */
Andrew Morton04903662006-12-06 20:37:33 -08007010 numentries = nr_kernel_pages;
Srikar Dronamrajuf6f34b42016-10-07 16:59:15 -07007011 numentries -= arch_reserved_kernel_pages();
Jerry Zhoua7e83312013-09-11 14:20:26 -07007012
7013 /* It isn't necessary when PAGE_SIZE >= 1MB */
7014 if (PAGE_SHIFT < 20)
7015 numentries = round_up(numentries, (1<<20)/PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016
7017 /* limit to 1 bucket per 2^scale bytes of low memory */
7018 if (scale > PAGE_SHIFT)
7019 numentries >>= (scale - PAGE_SHIFT);
7020 else
7021 numentries <<= (PAGE_SHIFT - scale);
Paul Mundt9ab37b82007-01-05 16:36:30 -08007022
7023 /* Make sure we've got at least a 0-order allocation.. */
Jan Beulich2c85f512009-09-21 17:03:07 -07007024 if (unlikely(flags & HASH_SMALL)) {
7025 /* Makes no sense without HASH_EARLY */
7026 WARN_ON(!(flags & HASH_EARLY));
7027 if (!(numentries >> *_hash_shift)) {
7028 numentries = 1UL << *_hash_shift;
7029 BUG_ON(!numentries);
7030 }
7031 } else if (unlikely((numentries * bucketsize) < PAGE_SIZE))
Paul Mundt9ab37b82007-01-05 16:36:30 -08007032 numentries = PAGE_SIZE / bucketsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007033 }
John Hawkes6e692ed2006-03-25 03:08:02 -08007034 numentries = roundup_pow_of_two(numentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007035
7036 /* limit allocation size to 1/16 total memory by default */
7037 if (max == 0) {
7038 max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
7039 do_div(max, bucketsize);
7040 }
Dimitri Sivanich074b8512012-02-08 12:39:07 -08007041 max = min(max, 0x80000000ULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007042
Tim Bird31fe62b2012-05-23 13:33:35 +00007043 if (numentries < low_limit)
7044 numentries = low_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045 if (numentries > max)
7046 numentries = max;
7047
David Howellsf0d1b0b2006-12-08 02:37:49 -08007048 log2qty = ilog2(numentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049
7050 do {
7051 size = bucketsize << log2qty;
7052 if (flags & HASH_EARLY)
Santosh Shilimkar67828322014-01-21 15:50:25 -08007053 table = memblock_virt_alloc_nopanic(size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054 else if (hashdist)
7055 table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
7056 else {
Eric Dumazet1037b832007-07-15 23:38:05 -07007057 /*
7058 * If bucketsize is not a power-of-two, we may free
Mel Gormana1dd2682009-06-16 15:32:19 -07007059 * some pages at the end of hash table which
7060 * alloc_pages_exact() automatically does
Eric Dumazet1037b832007-07-15 23:38:05 -07007061 */
Catalin Marinas264ef8a2009-07-07 10:33:01 +01007062 if (get_order(size) < MAX_ORDER) {
Mel Gormana1dd2682009-06-16 15:32:19 -07007063 table = alloc_pages_exact(size, GFP_ATOMIC);
Catalin Marinas264ef8a2009-07-07 10:33:01 +01007064 kmemleak_alloc(table, size, 1, GFP_ATOMIC);
7065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007066 }
7067 } while (!table && size > PAGE_SIZE && --log2qty);
7068
7069 if (!table)
7070 panic("Failed to allocate %s hash table\n", tablename);
7071
Joe Perches11705322016-03-17 14:19:50 -07007072 pr_info("%s hash table entries: %ld (order: %d, %lu bytes)\n",
7073 tablename, 1UL << log2qty, ilog2(size) - PAGE_SHIFT, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007074
7075 if (_hash_shift)
7076 *_hash_shift = log2qty;
7077 if (_hash_mask)
7078 *_hash_mask = (1 << log2qty) - 1;
7079
7080 return table;
7081}
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -08007082
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -07007083/*
Minchan Kim80934512012-07-31 16:43:01 -07007084 * This function checks whether pageblock includes unmovable pages or not.
7085 * If @count is not zero, it is okay to include less @count unmovable pages
7086 *
Pintu Kumarb8af2942013-09-11 14:20:34 -07007087 * PageLRU check without isolation or lru_lock could race so that
Minchan Kim80934512012-07-31 16:43:01 -07007088 * MIGRATE_MOVABLE block might include unmovable pages. It means you can't
7089 * expect this function should be exact.
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -07007090 */
Wen Congyangb023f462012-12-11 16:00:45 -08007091bool has_unmovable_pages(struct zone *zone, struct page *page, int count,
7092 bool skip_hwpoisoned_pages)
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07007093{
7094 unsigned long pfn, iter, found;
Michal Nazarewicz47118af2011-12-29 13:09:50 +01007095 int mt;
7096
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07007097 /*
7098 * For avoiding noise data, lru_add_drain_all() should be called
Minchan Kim80934512012-07-31 16:43:01 -07007099 * If ZONE_MOVABLE, the zone never contains unmovable pages
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07007100 */
7101 if (zone_idx(zone) == ZONE_MOVABLE)
Minchan Kim80934512012-07-31 16:43:01 -07007102 return false;
Michal Nazarewicz47118af2011-12-29 13:09:50 +01007103 mt = get_pageblock_migratetype(page);
7104 if (mt == MIGRATE_MOVABLE || is_migrate_cma(mt))
Minchan Kim80934512012-07-31 16:43:01 -07007105 return false;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07007106
7107 pfn = page_to_pfn(page);
7108 for (found = 0, iter = 0; iter < pageblock_nr_pages; iter++) {
7109 unsigned long check = pfn + iter;
7110
Namhyung Kim29723fc2011-02-25 14:44:25 -08007111 if (!pfn_valid_within(check))
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07007112 continue;
Namhyung Kim29723fc2011-02-25 14:44:25 -08007113
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07007114 page = pfn_to_page(check);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07007115
7116 /*
7117 * Hugepages are not in LRU lists, but they're movable.
7118 * We need not scan over tail pages bacause we don't
7119 * handle each tail page individually in migration.
7120 */
7121 if (PageHuge(page)) {
7122 iter = round_up(iter + 1, 1<<compound_order(page)) - 1;
7123 continue;
7124 }
7125
Minchan Kim97d255c2012-07-31 16:42:59 -07007126 /*
7127 * We can't use page_count without pin a page
7128 * because another CPU can free compound page.
7129 * This check already skips compound tails of THP
Joonsoo Kim0139aa72016-05-19 17:10:49 -07007130 * because their page->_refcount is zero at all time.
Minchan Kim97d255c2012-07-31 16:42:59 -07007131 */
Joonsoo Kimfe896d12016-03-17 14:19:26 -07007132 if (!page_ref_count(page)) {
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07007133 if (PageBuddy(page))
7134 iter += (1 << page_order(page)) - 1;
7135 continue;
7136 }
Minchan Kim97d255c2012-07-31 16:42:59 -07007137
Wen Congyangb023f462012-12-11 16:00:45 -08007138 /*
7139 * The HWPoisoned page may be not in buddy system, and
7140 * page_count() is not 0.
7141 */
7142 if (skip_hwpoisoned_pages && PageHWPoison(page))
7143 continue;
7144
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07007145 if (!PageLRU(page))
7146 found++;
7147 /*
Johannes Weiner6b4f7792014-12-12 16:56:13 -08007148 * If there are RECLAIMABLE pages, we need to check
7149 * it. But now, memory offline itself doesn't call
7150 * shrink_node_slabs() and it still to be fixed.
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07007151 */
7152 /*
7153 * If the page is not RAM, page_count()should be 0.
7154 * we don't need more check. This is an _used_ not-movable page.
7155 *
7156 * The problematic thing here is PG_reserved pages. PG_reserved
7157 * is set to both of a memory hole page and a _used_ kernel
7158 * page at boot.
7159 */
7160 if (found > count)
Minchan Kim80934512012-07-31 16:43:01 -07007161 return true;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07007162 }
Minchan Kim80934512012-07-31 16:43:01 -07007163 return false;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07007164}
7165
7166bool is_pageblock_removable_nolock(struct page *page)
7167{
Michal Hocko656a0702012-01-20 14:33:58 -08007168 struct zone *zone;
7169 unsigned long pfn;
Michal Hocko687875f2012-01-20 14:33:55 -08007170
7171 /*
7172 * We have to be careful here because we are iterating over memory
7173 * sections which are not zone aware so we might end up outside of
7174 * the zone but still within the section.
Michal Hocko656a0702012-01-20 14:33:58 -08007175 * We have to take care about the node as well. If the node is offline
7176 * its NODE_DATA will be NULL - see page_zone.
Michal Hocko687875f2012-01-20 14:33:55 -08007177 */
Michal Hocko656a0702012-01-20 14:33:58 -08007178 if (!node_online(page_to_nid(page)))
7179 return false;
7180
7181 zone = page_zone(page);
7182 pfn = page_to_pfn(page);
Cody P Schafer108bcc92013-02-22 16:35:23 -08007183 if (!zone_spans_pfn(zone, pfn))
Michal Hocko687875f2012-01-20 14:33:55 -08007184 return false;
7185
Wen Congyangb023f462012-12-11 16:00:45 -08007186 return !has_unmovable_pages(zone, page, 0, true);
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -07007187}
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07007188
Vlastimil Babka080fe202016-02-05 15:36:41 -08007189#if (defined(CONFIG_MEMORY_ISOLATION) && defined(CONFIG_COMPACTION)) || defined(CONFIG_CMA)
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007190
7191static unsigned long pfn_max_align_down(unsigned long pfn)
7192{
7193 return pfn & ~(max_t(unsigned long, MAX_ORDER_NR_PAGES,
7194 pageblock_nr_pages) - 1);
7195}
7196
7197static unsigned long pfn_max_align_up(unsigned long pfn)
7198{
7199 return ALIGN(pfn, max_t(unsigned long, MAX_ORDER_NR_PAGES,
7200 pageblock_nr_pages));
7201}
7202
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007203/* [start, end) must belong to a single zone. */
Mel Gormanbb13ffe2012-10-08 16:32:41 -07007204static int __alloc_contig_migrate_range(struct compact_control *cc,
7205 unsigned long start, unsigned long end)
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007206{
7207 /* This function is based on compact_zone() from compaction.c. */
Minchan Kimbeb51ea2012-10-08 16:33:51 -07007208 unsigned long nr_reclaimed;
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007209 unsigned long pfn = start;
7210 unsigned int tries = 0;
7211 int ret = 0;
7212
Marek Szyprowskibe49a6e2012-12-12 13:51:19 -08007213 migrate_prep();
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007214
Mel Gormanbb13ffe2012-10-08 16:32:41 -07007215 while (pfn < end || !list_empty(&cc->migratepages)) {
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007216 if (fatal_signal_pending(current)) {
7217 ret = -EINTR;
7218 break;
7219 }
7220
Mel Gormanbb13ffe2012-10-08 16:32:41 -07007221 if (list_empty(&cc->migratepages)) {
7222 cc->nr_migratepages = 0;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07007223 pfn = isolate_migratepages_range(cc, pfn, end);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007224 if (!pfn) {
7225 ret = -EINTR;
7226 break;
7227 }
7228 tries = 0;
7229 } else if (++tries == 5) {
7230 ret = ret < 0 ? ret : -EBUSY;
7231 break;
7232 }
7233
Minchan Kimbeb51ea2012-10-08 16:33:51 -07007234 nr_reclaimed = reclaim_clean_pages_from_list(cc->zone,
7235 &cc->migratepages);
7236 cc->nr_migratepages -= nr_reclaimed;
Minchan Kim02c6de82012-10-08 16:31:55 -07007237
Hugh Dickins9c620e22013-02-22 16:35:14 -08007238 ret = migrate_pages(&cc->migratepages, alloc_migrate_target,
David Rientjese0b9dae2014-06-04 16:08:28 -07007239 NULL, 0, cc->mode, MR_CMA);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007240 }
Srinivas Pandruvada2a6f5122013-02-22 16:32:09 -08007241 if (ret < 0) {
7242 putback_movable_pages(&cc->migratepages);
7243 return ret;
7244 }
7245 return 0;
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007246}
7247
7248/**
7249 * alloc_contig_range() -- tries to allocate given range of pages
7250 * @start: start PFN to allocate
7251 * @end: one-past-the-last PFN to allocate
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02007252 * @migratetype: migratetype of the underlaying pageblocks (either
7253 * #MIGRATE_MOVABLE or #MIGRATE_CMA). All pageblocks
7254 * in range must have the same migratetype and it must
7255 * be either of the two.
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007256 *
7257 * The PFN range does not have to be pageblock or MAX_ORDER_NR_PAGES
7258 * aligned, however it's the caller's responsibility to guarantee that
7259 * we are the only thread that changes migrate type of pageblocks the
7260 * pages fall in.
7261 *
7262 * The PFN range must belong to a single zone.
7263 *
7264 * Returns zero on success or negative error code. On success all
7265 * pages which PFN is in [start, end) are allocated for the caller and
7266 * need to be freed with free_contig_range().
7267 */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02007268int alloc_contig_range(unsigned long start, unsigned long end,
7269 unsigned migratetype)
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007270{
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007271 unsigned long outer_start, outer_end;
Kirill A. Shutemovd00181b2015-11-06 16:29:57 -08007272 unsigned int order;
7273 int ret = 0;
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007274
Mel Gormanbb13ffe2012-10-08 16:32:41 -07007275 struct compact_control cc = {
7276 .nr_migratepages = 0,
7277 .order = -1,
7278 .zone = page_zone(pfn_to_page(start)),
David Rientjese0b9dae2014-06-04 16:08:28 -07007279 .mode = MIGRATE_SYNC,
Mel Gormanbb13ffe2012-10-08 16:32:41 -07007280 .ignore_skip_hint = true,
7281 };
7282 INIT_LIST_HEAD(&cc.migratepages);
7283
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007284 /*
7285 * What we do here is we mark all pageblocks in range as
7286 * MIGRATE_ISOLATE. Because pageblock and max order pages may
7287 * have different sizes, and due to the way page allocator
7288 * work, we align the range to biggest of the two pages so
7289 * that page allocator won't try to merge buddies from
7290 * different pageblocks and change MIGRATE_ISOLATE to some
7291 * other migration type.
7292 *
7293 * Once the pageblocks are marked as MIGRATE_ISOLATE, we
7294 * migrate the pages from an unaligned range (ie. pages that
7295 * we are interested in). This will put all the pages in
7296 * range back to page allocator as MIGRATE_ISOLATE.
7297 *
7298 * When this is done, we take the pages in range from page
7299 * allocator removing them from the buddy system. This way
7300 * page allocator will never consider using them.
7301 *
7302 * This lets us mark the pageblocks back as
7303 * MIGRATE_CMA/MIGRATE_MOVABLE so that free pages in the
7304 * aligned range but not in the unaligned, original range are
7305 * put back to page allocator so that buddy can use them.
7306 */
7307
7308 ret = start_isolate_page_range(pfn_max_align_down(start),
Wen Congyangb023f462012-12-11 16:00:45 -08007309 pfn_max_align_up(end), migratetype,
7310 false);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007311 if (ret)
Bob Liu86a595f2012-10-25 13:37:56 -07007312 return ret;
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007313
Joonsoo Kim8ef58492016-01-14 15:18:45 -08007314 /*
7315 * In case of -EBUSY, we'd like to know which page causes problem.
Mike Kravetz436f19a2017-11-29 16:10:01 -08007316 * So, just fall through. test_pages_isolated() has a tracepoint
7317 * which will report the busy page.
7318 *
7319 * It is possible that busy pages could become available before
7320 * the call to test_pages_isolated, and the range will actually be
7321 * allocated. So, if we fall through be sure to clear ret so that
7322 * -EBUSY is not accidentally used or returned to caller.
Joonsoo Kim8ef58492016-01-14 15:18:45 -08007323 */
Mel Gormanbb13ffe2012-10-08 16:32:41 -07007324 ret = __alloc_contig_migrate_range(&cc, start, end);
Joonsoo Kim8ef58492016-01-14 15:18:45 -08007325 if (ret && ret != -EBUSY)
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007326 goto done;
Mike Kravetz436f19a2017-11-29 16:10:01 -08007327 ret =0;
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007328
7329 /*
7330 * Pages from [start, end) are within a MAX_ORDER_NR_PAGES
7331 * aligned blocks that are marked as MIGRATE_ISOLATE. What's
7332 * more, all pages in [start, end) are free in page allocator.
7333 * What we are going to do is to allocate all pages from
7334 * [start, end) (that is remove them from page allocator).
7335 *
7336 * The only problem is that pages at the beginning and at the
7337 * end of interesting range may be not aligned with pages that
7338 * page allocator holds, ie. they can be part of higher order
7339 * pages. Because of this, we reserve the bigger range and
7340 * once this is done free the pages we are not interested in.
7341 *
7342 * We don't have to hold zone->lock here because the pages are
7343 * isolated thus they won't get removed from buddy.
7344 */
7345
7346 lru_add_drain_all();
Vlastimil Babka510f5502014-12-10 15:43:07 -08007347 drain_all_pages(cc.zone);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007348
7349 order = 0;
7350 outer_start = start;
7351 while (!PageBuddy(pfn_to_page(outer_start))) {
7352 if (++order >= MAX_ORDER) {
Joonsoo Kim8ef58492016-01-14 15:18:45 -08007353 outer_start = start;
7354 break;
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007355 }
7356 outer_start &= ~0UL << order;
7357 }
7358
Joonsoo Kim8ef58492016-01-14 15:18:45 -08007359 if (outer_start != start) {
7360 order = page_order(pfn_to_page(outer_start));
7361
7362 /*
7363 * outer_start page could be small order buddy page and
7364 * it doesn't include start page. Adjust outer_start
7365 * in this case to report failed page properly
7366 * on tracepoint in test_pages_isolated()
7367 */
7368 if (outer_start + (1UL << order) <= start)
7369 outer_start = start;
7370 }
7371
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007372 /* Make sure the range is really isolated. */
Wen Congyangb023f462012-12-11 16:00:45 -08007373 if (test_pages_isolated(outer_start, end, false)) {
Jonathan Toppinsb56cd772017-08-10 15:23:35 -07007374 pr_info_ratelimited("%s: [%lx, %lx) PFNs busy\n",
Michal Nazarewiczdae803e2014-11-13 15:19:27 -08007375 __func__, outer_start, end);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007376 ret = -EBUSY;
7377 goto done;
7378 }
7379
Marek Szyprowski49f223a2012-01-25 12:49:24 +01007380 /* Grab isolated pages from freelists. */
Mel Gormanbb13ffe2012-10-08 16:32:41 -07007381 outer_end = isolate_freepages_range(&cc, outer_start, end);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007382 if (!outer_end) {
7383 ret = -EBUSY;
7384 goto done;
7385 }
7386
7387 /* Free head and tail (if any) */
7388 if (start != outer_start)
7389 free_contig_range(outer_start, start - outer_start);
7390 if (end != outer_end)
7391 free_contig_range(end, outer_end - end);
7392
7393done:
7394 undo_isolate_page_range(pfn_max_align_down(start),
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02007395 pfn_max_align_up(end), migratetype);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007396 return ret;
7397}
7398
7399void free_contig_range(unsigned long pfn, unsigned nr_pages)
7400{
Marek Szyprowskibcc2b022012-12-20 15:05:18 -08007401 unsigned int count = 0;
7402
7403 for (; nr_pages--; pfn++) {
7404 struct page *page = pfn_to_page(pfn);
7405
7406 count += page_count(page) != 1;
7407 __free_page(page);
7408 }
7409 WARN(count != 0, "%d pages are still in use!\n", count);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01007410}
7411#endif
7412
Jiang Liu4ed7e022012-07-31 16:43:35 -07007413#ifdef CONFIG_MEMORY_HOTPLUG
Cody P Schafer0a647f32013-07-03 15:01:33 -07007414/*
7415 * The zone indicated has a new number of managed_pages; batch sizes and percpu
7416 * page high values need to be recalulated.
7417 */
Jiang Liu4ed7e022012-07-31 16:43:35 -07007418void __meminit zone_pcp_update(struct zone *zone)
7419{
Cody P Schafer0a647f32013-07-03 15:01:33 -07007420 unsigned cpu;
Cody P Schaferc8e251f2013-07-03 15:01:29 -07007421 mutex_lock(&pcp_batch_high_lock);
Cody P Schafer0a647f32013-07-03 15:01:33 -07007422 for_each_possible_cpu(cpu)
Cody P Schafer169f6c12013-07-03 15:01:41 -07007423 pageset_set_high_and_batch(zone,
7424 per_cpu_ptr(zone->pageset, cpu));
Cody P Schaferc8e251f2013-07-03 15:01:29 -07007425 mutex_unlock(&pcp_batch_high_lock);
Jiang Liu4ed7e022012-07-31 16:43:35 -07007426}
7427#endif
7428
Jiang Liu340175b2012-07-31 16:43:32 -07007429void zone_pcp_reset(struct zone *zone)
7430{
7431 unsigned long flags;
Minchan Kim5a883812012-10-08 16:33:39 -07007432 int cpu;
7433 struct per_cpu_pageset *pset;
Jiang Liu340175b2012-07-31 16:43:32 -07007434
7435 /* avoid races with drain_pages() */
7436 local_irq_save(flags);
7437 if (zone->pageset != &boot_pageset) {
Minchan Kim5a883812012-10-08 16:33:39 -07007438 for_each_online_cpu(cpu) {
7439 pset = per_cpu_ptr(zone->pageset, cpu);
7440 drain_zonestat(zone, pset);
7441 }
Jiang Liu340175b2012-07-31 16:43:32 -07007442 free_percpu(zone->pageset);
7443 zone->pageset = &boot_pageset;
7444 }
7445 local_irq_restore(flags);
7446}
7447
Wen Congyang6dcd73d2012-12-11 16:01:01 -08007448#ifdef CONFIG_MEMORY_HOTREMOVE
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07007449/*
Joonsoo Kimb9eb6312016-05-19 17:12:06 -07007450 * All pages in the range must be in a single zone and isolated
7451 * before calling this.
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07007452 */
7453void
7454__offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
7455{
7456 struct page *page;
7457 struct zone *zone;
Mel Gorman7aeb09f2014-06-04 16:10:21 -07007458 unsigned int order, i;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07007459 unsigned long pfn;
7460 unsigned long flags;
7461 /* find the first valid pfn */
7462 for (pfn = start_pfn; pfn < end_pfn; pfn++)
7463 if (pfn_valid(pfn))
7464 break;
7465 if (pfn == end_pfn)
7466 return;
7467 zone = page_zone(pfn_to_page(pfn));
7468 spin_lock_irqsave(&zone->lock, flags);
7469 pfn = start_pfn;
7470 while (pfn < end_pfn) {
7471 if (!pfn_valid(pfn)) {
7472 pfn++;
7473 continue;
7474 }
7475 page = pfn_to_page(pfn);
Wen Congyangb023f462012-12-11 16:00:45 -08007476 /*
7477 * The HWPoisoned page may be not in buddy system, and
7478 * page_count() is not 0.
7479 */
7480 if (unlikely(!PageBuddy(page) && PageHWPoison(page))) {
7481 pfn++;
7482 SetPageReserved(page);
7483 continue;
7484 }
7485
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07007486 BUG_ON(page_count(page));
7487 BUG_ON(!PageBuddy(page));
7488 order = page_order(page);
7489#ifdef CONFIG_DEBUG_VM
Joe Perches11705322016-03-17 14:19:50 -07007490 pr_info("remove from free list %lx %d %lx\n",
7491 pfn, 1 << order, end_pfn);
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07007492#endif
7493 list_del(&page->lru);
7494 rmv_page_order(page);
7495 zone->free_area[order].nr_free--;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07007496 for (i = 0; i < (1 << order); i++)
7497 SetPageReserved((page+i));
7498 pfn += (1 << order);
7499 }
7500 spin_unlock_irqrestore(&zone->lock, flags);
7501}
7502#endif
Wu Fengguang8d22ba12009-12-16 12:19:58 +01007503
Wu Fengguang8d22ba12009-12-16 12:19:58 +01007504bool is_free_buddy_page(struct page *page)
7505{
7506 struct zone *zone = page_zone(page);
7507 unsigned long pfn = page_to_pfn(page);
7508 unsigned long flags;
Mel Gorman7aeb09f2014-06-04 16:10:21 -07007509 unsigned int order;
Wu Fengguang8d22ba12009-12-16 12:19:58 +01007510
7511 spin_lock_irqsave(&zone->lock, flags);
7512 for (order = 0; order < MAX_ORDER; order++) {
7513 struct page *page_head = page - (pfn & ((1 << order) - 1));
7514
7515 if (PageBuddy(page_head) && page_order(page_head) >= order)
7516 break;
7517 }
7518 spin_unlock_irqrestore(&zone->lock, flags);
7519
7520 return order < MAX_ORDER;
7521}