blob: b94a58e576da219b55d62157c3a7099277afd578 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
29#include <linux/suspend.h>
30#include <linux/pagevec.h>
31#include <linux/blkdev.h>
32#include <linux/slab.h>
Dave Hansena238ab52011-05-24 17:12:16 -070033#include <linux/ratelimit.h>
David Rientjes5a3135c22007-10-16 23:25:53 -070034#include <linux/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/notifier.h>
36#include <linux/topology.h>
37#include <linux/sysctl.h>
38#include <linux/cpu.h>
39#include <linux/cpuset.h>
Dave Hansenbdc8cb92005-10-29 18:16:53 -070040#include <linux/memory_hotplug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/nodemask.h>
42#include <linux/vmalloc.h>
KOSAKI Motohiroa6cccdc2011-05-24 17:11:33 -070043#include <linux/vmstat.h>
Christoph Lameter4be38e32006-01-06 00:11:17 -080044#include <linux/mempolicy.h>
Yasunori Goto68113782006-06-23 02:03:11 -070045#include <linux/stop_machine.h>
Mel Gormanc7132162006-09-27 01:49:43 -070046#include <linux/sort.h>
47#include <linux/pfn.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070048#include <linux/backing-dev.h>
Akinobu Mita933e3122006-12-08 02:39:45 -080049#include <linux/fault-inject.h>
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -070050#include <linux/page-isolation.h>
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070051#include <linux/page_cgroup.h>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070052#include <linux/debugobjects.h>
Catalin Marinasdbb1f812009-06-11 13:23:19 +010053#include <linux/kmemleak.h>
Mel Gorman56de7262010-05-24 14:32:30 -070054#include <linux/compaction.h>
Mel Gorman0d3d0622009-09-21 17:02:44 -070055#include <trace/events/kmem.h>
Wu Fengguang718a3822010-03-10 15:20:43 -080056#include <linux/ftrace_event.h>
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -070057#include <linux/memcontrol.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070058#include <linux/prefetch.h>
Michal Nazarewicz041d3a82011-12-29 13:09:50 +010059#include <linux/migrate.h>
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -080060#include <linux/page-debug-flags.h>
David Rientjes949f7ec2013-04-29 15:07:48 -070061#include <linux/hugetlb.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060062#include <linux/sched/rt.h>
Ingo Molnarbc7d7492009-07-03 08:29:37 -050063#include <linux/locallock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65#include <asm/tlbflush.h>
Andrew Mortonac924c62006-05-15 09:43:59 -070066#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include "internal.h"
68
Lee Schermerhorn72812012010-05-26 14:44:56 -070069#ifdef CONFIG_USE_PERCPU_NUMA_NODE_ID
70DEFINE_PER_CPU(int, numa_node);
71EXPORT_PER_CPU_SYMBOL(numa_node);
72#endif
73
Lee Schermerhorn7aac7892010-05-26 14:45:00 -070074#ifdef CONFIG_HAVE_MEMORYLESS_NODES
75/*
76 * N.B., Do NOT reference the '_numa_mem_' per cpu variable directly.
77 * It will not be defined when CONFIG_HAVE_MEMORYLESS_NODES is not defined.
78 * Use the accessor functions set_numa_mem(), numa_mem_id() and cpu_to_mem()
79 * defined in <linux/topology.h>.
80 */
81DEFINE_PER_CPU(int, _numa_mem_); /* Kernel "local memory" node */
82EXPORT_PER_CPU_SYMBOL(_numa_mem_);
83#endif
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/*
Christoph Lameter13808912007-10-16 01:25:27 -070086 * Array of node states.
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 */
Christoph Lameter13808912007-10-16 01:25:27 -070088nodemask_t node_states[NR_NODE_STATES] __read_mostly = {
89 [N_POSSIBLE] = NODE_MASK_ALL,
90 [N_ONLINE] = { { [0] = 1UL } },
91#ifndef CONFIG_NUMA
92 [N_NORMAL_MEMORY] = { { [0] = 1UL } },
93#ifdef CONFIG_HIGHMEM
94 [N_HIGH_MEMORY] = { { [0] = 1UL } },
95#endif
Lai Jiangshan20b2f522012-12-12 13:52:00 -080096#ifdef CONFIG_MOVABLE_NODE
97 [N_MEMORY] = { { [0] = 1UL } },
98#endif
Christoph Lameter13808912007-10-16 01:25:27 -070099 [N_CPU] = { { [0] = 1UL } },
100#endif /* NUMA */
101};
102EXPORT_SYMBOL(node_states);
103
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -0700104unsigned long totalram_pages __read_mostly;
Hideo AOKIcb45b0e2006-04-10 22:52:59 -0700105unsigned long totalreserve_pages __read_mostly;
Johannes Weinerab8fabd2012-01-10 15:07:42 -0800106/*
107 * When calculating the number of globally allowed dirty pages, there
108 * is a certain number of per-zone reserves that should not be
109 * considered dirtyable memory. This is the sum of those reserves
110 * over all existing zones that contribute dirtyable memory.
111 */
112unsigned long dirty_balance_reserve __read_mostly;
113
Hugh Dickins1b76b022012-05-11 01:00:07 -0700114int percpu_pagelist_fraction;
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +1000115gfp_t gfp_allowed_mask __read_mostly = GFP_BOOT_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800117#ifdef CONFIG_PM_SLEEP
118/*
119 * The following functions are used by the suspend/hibernate code to temporarily
120 * change gfp_allowed_mask in order to avoid using I/O during memory allocations
121 * while devices are suspended. To avoid races with the suspend/hibernate code,
122 * they should always be called with pm_mutex held (gfp_allowed_mask also should
123 * only be modified with pm_mutex held, unless the suspend/hibernate code is
124 * guaranteed not to run in parallel with that modification).
125 */
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100126
127static gfp_t saved_gfp_mask;
128
129void pm_restore_gfp_mask(void)
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800130{
131 WARN_ON(!mutex_is_locked(&pm_mutex));
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100132 if (saved_gfp_mask) {
133 gfp_allowed_mask = saved_gfp_mask;
134 saved_gfp_mask = 0;
135 }
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800136}
137
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100138void pm_restrict_gfp_mask(void)
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800139{
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800140 WARN_ON(!mutex_is_locked(&pm_mutex));
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100141 WARN_ON(saved_gfp_mask);
142 saved_gfp_mask = gfp_allowed_mask;
143 gfp_allowed_mask &= ~GFP_IOFS;
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800144}
Mel Gormanf90ac392012-01-10 15:07:15 -0800145
146bool pm_suspended_storage(void)
147{
148 if ((gfp_allowed_mask & GFP_IOFS) == GFP_IOFS)
149 return false;
150 return true;
151}
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800152#endif /* CONFIG_PM_SLEEP */
153
Mel Gormand9c23402007-10-16 01:26:01 -0700154#ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
155int pageblock_order __read_mostly;
156#endif
157
Hugh Dickinsd98c7a02006-02-14 13:52:59 -0800158static void __free_pages_ok(struct page *page, unsigned int order);
David Howellsa226f6c2006-01-06 00:11:08 -0800159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160/*
161 * results with 256, 32 in the lowmem_reserve sysctl:
162 * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
163 * 1G machine -> (16M dma, 784M normal, 224M high)
164 * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
165 * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
166 * HIGHMEM allocation will (224M+784M)/256 of ram reserved in ZONE_DMA
Andi Kleena2f1b422005-11-05 17:25:53 +0100167 *
168 * TBD: should special case ZONE_DMA32 machines here - in those we normally
169 * don't need any ZONE_NORMAL reservation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 */
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700171int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1] = {
Christoph Lameter4b51d662007-02-10 01:43:10 -0800172#ifdef CONFIG_ZONE_DMA
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700173 256,
Christoph Lameter4b51d662007-02-10 01:43:10 -0800174#endif
Christoph Lameterfb0e7942006-09-25 23:31:13 -0700175#ifdef CONFIG_ZONE_DMA32
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700176 256,
Christoph Lameterfb0e7942006-09-25 23:31:13 -0700177#endif
Christoph Lametere53ef382006-09-25 23:31:14 -0700178#ifdef CONFIG_HIGHMEM
Mel Gorman2a1e2742007-07-17 04:03:12 -0700179 32,
Christoph Lametere53ef382006-09-25 23:31:14 -0700180#endif
Mel Gorman2a1e2742007-07-17 04:03:12 -0700181 32,
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700182};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184EXPORT_SYMBOL(totalram_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Helge Deller15ad7cd2006-12-06 20:40:36 -0800186static char * const zone_names[MAX_NR_ZONES] = {
Christoph Lameter4b51d662007-02-10 01:43:10 -0800187#ifdef CONFIG_ZONE_DMA
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700188 "DMA",
Christoph Lameter4b51d662007-02-10 01:43:10 -0800189#endif
Christoph Lameterfb0e7942006-09-25 23:31:13 -0700190#ifdef CONFIG_ZONE_DMA32
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700191 "DMA32",
Christoph Lameterfb0e7942006-09-25 23:31:13 -0700192#endif
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700193 "Normal",
Christoph Lametere53ef382006-09-25 23:31:14 -0700194#ifdef CONFIG_HIGHMEM
Mel Gorman2a1e2742007-07-17 04:03:12 -0700195 "HighMem",
Christoph Lametere53ef382006-09-25 23:31:14 -0700196#endif
Mel Gorman2a1e2742007-07-17 04:03:12 -0700197 "Movable",
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700198};
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200int min_free_kbytes = 1024;
201
Jan Beulich2c85f512009-09-21 17:03:07 -0700202static unsigned long __meminitdata nr_kernel_pages;
203static unsigned long __meminitdata nr_all_pages;
Yasunori Gotoa3142c82007-05-08 00:23:07 -0700204static unsigned long __meminitdata dma_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Tejun Heo0ee332c2011-12-08 10:22:09 -0800206#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
207static unsigned long __meminitdata arch_zone_lowest_possible_pfn[MAX_NR_ZONES];
208static unsigned long __meminitdata arch_zone_highest_possible_pfn[MAX_NR_ZONES];
209static unsigned long __initdata required_kernelcore;
210static unsigned long __initdata required_movablecore;
211static unsigned long __meminitdata zone_movable_pfn[MAX_NUMNODES];
Mel Gormanc7132162006-09-27 01:49:43 -0700212
Tejun Heo0ee332c2011-12-08 10:22:09 -0800213/* movable_zone is the "real" zone pages in ZONE_MOVABLE are taken from */
214int movable_zone;
215EXPORT_SYMBOL(movable_zone);
216#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
Mel Gormanc7132162006-09-27 01:49:43 -0700217
Miklos Szeredi418508c2007-05-23 13:57:55 -0700218#if MAX_NUMNODES > 1
219int nr_node_ids __read_mostly = MAX_NUMNODES;
Christoph Lameter62bc62a2009-06-16 15:32:15 -0700220int nr_online_nodes __read_mostly = 1;
Miklos Szeredi418508c2007-05-23 13:57:55 -0700221EXPORT_SYMBOL(nr_node_ids);
Christoph Lameter62bc62a2009-06-16 15:32:15 -0700222EXPORT_SYMBOL(nr_online_nodes);
Miklos Szeredi418508c2007-05-23 13:57:55 -0700223#endif
224
Ingo Molnarbc7d7492009-07-03 08:29:37 -0500225static DEFINE_LOCAL_IRQ_LOCK(pa_lock);
226
227#ifdef CONFIG_PREEMPT_RT_BASE
228# define cpu_lock_irqsave(cpu, flags) \
Thomas Gleixnerb4c2e1b2012-09-27 11:11:46 +0200229 local_lock_irqsave_on(pa_lock, flags, cpu)
Ingo Molnarbc7d7492009-07-03 08:29:37 -0500230# define cpu_unlock_irqrestore(cpu, flags) \
Thomas Gleixnerb4c2e1b2012-09-27 11:11:46 +0200231 local_unlock_irqrestore_on(pa_lock, flags, cpu)
Ingo Molnarbc7d7492009-07-03 08:29:37 -0500232#else
233# define cpu_lock_irqsave(cpu, flags) local_irq_save(flags)
234# define cpu_unlock_irqrestore(cpu, flags) local_irq_restore(flags)
235#endif
236
Mel Gorman9ef9acb2007-10-16 01:25:54 -0700237int page_group_by_mobility_disabled __read_mostly;
238
Minchan Kimee6f5092012-07-31 16:43:50 -0700239void set_pageblock_migratetype(struct page *page, int migratetype)
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700240{
Mel Gorman49255c62009-06-16 15:31:58 -0700241
242 if (unlikely(page_group_by_mobility_disabled))
243 migratetype = MIGRATE_UNMOVABLE;
244
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700245 set_pageblock_flags_group(page, (unsigned long)migratetype,
246 PB_migrate, PB_migrate_end);
247}
248
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700249bool oom_killer_disabled __read_mostly;
250
Nick Piggin13e74442006-01-06 00:10:58 -0800251#ifdef CONFIG_DEBUG_VM
Dave Hansenc6a57e12005-10-29 18:16:52 -0700252static int page_outside_zone_boundaries(struct zone *zone, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Dave Hansenbdc8cb92005-10-29 18:16:53 -0700254 int ret = 0;
255 unsigned seq;
256 unsigned long pfn = page_to_pfn(page);
Cody P Schaferb5e6a5a2013-02-22 16:35:28 -0800257 unsigned long sp, start_pfn;
Dave Hansenc6a57e12005-10-29 18:16:52 -0700258
Dave Hansenbdc8cb92005-10-29 18:16:53 -0700259 do {
260 seq = zone_span_seqbegin(zone);
Cody P Schaferb5e6a5a2013-02-22 16:35:28 -0800261 start_pfn = zone->zone_start_pfn;
262 sp = zone->spanned_pages;
Cody P Schafer108bcc92013-02-22 16:35:23 -0800263 if (!zone_spans_pfn(zone, pfn))
Dave Hansenbdc8cb92005-10-29 18:16:53 -0700264 ret = 1;
265 } while (zone_span_seqretry(zone, seq));
266
Cody P Schaferb5e6a5a2013-02-22 16:35:28 -0800267 if (ret)
268 pr_err("page %lu outside zone [ %lu - %lu ]\n",
269 pfn, start_pfn, start_pfn + sp);
270
Dave Hansenbdc8cb92005-10-29 18:16:53 -0700271 return ret;
Dave Hansenc6a57e12005-10-29 18:16:52 -0700272}
273
274static int page_is_consistent(struct zone *zone, struct page *page)
275{
Andy Whitcroft14e07292007-05-06 14:49:14 -0700276 if (!pfn_valid_within(page_to_pfn(page)))
Dave Hansenc6a57e12005-10-29 18:16:52 -0700277 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (zone != page_zone(page))
Dave Hansenc6a57e12005-10-29 18:16:52 -0700279 return 0;
280
281 return 1;
282}
283/*
284 * Temporary debugging check for pages not lying within a given zone.
285 */
286static int bad_range(struct zone *zone, struct page *page)
287{
288 if (page_outside_zone_boundaries(zone, page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return 1;
Dave Hansenc6a57e12005-10-29 18:16:52 -0700290 if (!page_is_consistent(zone, page))
291 return 1;
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return 0;
294}
Nick Piggin13e74442006-01-06 00:10:58 -0800295#else
296static inline int bad_range(struct zone *zone, struct page *page)
297{
298 return 0;
299}
300#endif
301
Nick Piggin224abf92006-01-06 00:11:11 -0800302static void bad_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800304 static unsigned long resume;
305 static unsigned long nr_shown;
306 static unsigned long nr_unshown;
307
Wu Fengguang2a7684a2009-09-16 11:50:12 +0200308 /* Don't complain about poisoned pages */
309 if (PageHWPoison(page)) {
Mel Gorman22b751c2013-02-22 16:34:59 -0800310 page_mapcount_reset(page); /* remove PageBuddy */
Wu Fengguang2a7684a2009-09-16 11:50:12 +0200311 return;
312 }
313
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800314 /*
315 * Allow a burst of 60 reports, then keep quiet for that minute;
316 * or allow a steady drip of one report per second.
317 */
318 if (nr_shown == 60) {
319 if (time_before(jiffies, resume)) {
320 nr_unshown++;
321 goto out;
322 }
323 if (nr_unshown) {
Hugh Dickins1e9e6362009-01-06 14:40:13 -0800324 printk(KERN_ALERT
325 "BUG: Bad page state: %lu messages suppressed\n",
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800326 nr_unshown);
327 nr_unshown = 0;
328 }
329 nr_shown = 0;
330 }
331 if (nr_shown++ == 0)
332 resume = jiffies + 60 * HZ;
333
Hugh Dickins1e9e6362009-01-06 14:40:13 -0800334 printk(KERN_ALERT "BUG: Bad page state in process %s pfn:%05lx\n",
Hugh Dickins3dc14742009-01-06 14:40:08 -0800335 current->comm, page_to_pfn(page));
Wu Fengguang718a3822010-03-10 15:20:43 -0800336 dump_page(page);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700337
Dave Jones4f318882011-10-31 17:07:24 -0700338 print_modules();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 dump_stack();
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800340out:
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800341 /* Leave bad fields for debug, except PageBuddy could make trouble */
Mel Gorman22b751c2013-02-22 16:34:59 -0800342 page_mapcount_reset(page); /* remove PageBuddy */
Rusty Russell373d4d02013-01-21 17:17:39 +1030343 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346/*
347 * Higher-order pages are called "compound pages". They are structured thusly:
348 *
349 * The first PAGE_SIZE page is called the "head page".
350 *
351 * The remaining PAGE_SIZE pages are called "tail pages".
352 *
Wang Sheng-Hui6416b9f2011-11-17 10:53:50 +0100353 * All pages have PG_compound set. All tail pages have their ->first_page
354 * pointing at the head page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 *
Hugh Dickins41d78ba2006-02-14 13:52:58 -0800356 * The first tail page's ->lru.next holds the address of the compound page's
357 * put_page() function. Its ->lru.prev holds the order of allocation.
358 * This usage means that zero-order pages may not be compound.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 */
Hugh Dickinsd98c7a02006-02-14 13:52:59 -0800360
361static void free_compound_page(struct page *page)
362{
Christoph Lameterd85f3382007-05-06 14:49:39 -0700363 __free_pages_ok(page, compound_order(page));
Hugh Dickinsd98c7a02006-02-14 13:52:59 -0800364}
365
Andi Kleen01ad1c02008-07-23 21:27:46 -0700366void prep_compound_page(struct page *page, unsigned long order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 int i;
369 int nr_pages = 1 << order;
370
Andy Whitcroft33f2ef82006-12-06 20:33:32 -0800371 set_compound_page_dtor(page, free_compound_page);
Christoph Lameterd85f3382007-05-06 14:49:39 -0700372 set_compound_order(page, order);
Christoph Lameter6d777952007-05-06 14:49:40 -0700373 __SetPageHead(page);
Andy Whitcroft18229df2008-11-06 12:53:27 -0800374 for (i = 1; i < nr_pages; i++) {
375 struct page *p = page + i;
Christoph Lameterd85f3382007-05-06 14:49:39 -0700376 __SetPageTail(p);
Youquan Song58a84aa2011-12-08 14:34:18 -0800377 set_page_count(p, 0);
Christoph Lameterd85f3382007-05-06 14:49:39 -0700378 p->first_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380}
381
Andrea Arcangeli59ff4212011-01-13 15:46:38 -0800382/* update __split_huge_page_refcount if you change this function */
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800383static int destroy_compound_page(struct page *page, unsigned long order)
Andy Whitcroft18229df2008-11-06 12:53:27 -0800384{
385 int i;
386 int nr_pages = 1 << order;
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800387 int bad = 0;
Andy Whitcroft18229df2008-11-06 12:53:27 -0800388
Gavin Shan0bb2c762012-12-18 14:21:32 -0800389 if (unlikely(compound_order(page) != order)) {
Nick Piggin224abf92006-01-06 00:11:11 -0800390 bad_page(page);
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800391 bad++;
392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Christoph Lameter6d777952007-05-06 14:49:40 -0700394 __ClearPageHead(page);
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800395
Andy Whitcroft18229df2008-11-06 12:53:27 -0800396 for (i = 1; i < nr_pages; i++) {
397 struct page *p = page + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Alexey Zaytseve713a212009-01-10 02:47:57 +0300399 if (unlikely(!PageTail(p) || (p->first_page != page))) {
Nick Piggin224abf92006-01-06 00:11:11 -0800400 bad_page(page);
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800401 bad++;
402 }
Christoph Lameterd85f3382007-05-06 14:49:39 -0700403 __ClearPageTail(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800405
406 return bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Nick Piggin17cf4402006-03-22 00:08:41 -0800409static inline void prep_zero_page(struct page *page, int order, gfp_t gfp_flags)
410{
411 int i;
412
Andrew Morton6626c5d2006-03-22 00:08:42 -0800413 /*
414 * clear_highpage() will use KM_USER0, so it's a bug to use __GFP_ZERO
415 * and __GFP_HIGHMEM from hard or soft interrupt context.
416 */
Nick Piggin725d7042006-09-25 23:30:55 -0700417 VM_BUG_ON((gfp_flags & __GFP_HIGHMEM) && in_interrupt());
Nick Piggin17cf4402006-03-22 00:08:41 -0800418 for (i = 0; i < (1 << order); i++)
419 clear_highpage(page + i);
420}
421
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -0800422#ifdef CONFIG_DEBUG_PAGEALLOC
423unsigned int _debug_guardpage_minorder;
424
425static int __init debug_guardpage_minorder_setup(char *buf)
426{
427 unsigned long res;
428
429 if (kstrtoul(buf, 10, &res) < 0 || res > MAX_ORDER / 2) {
430 printk(KERN_ERR "Bad debug_guardpage_minorder value\n");
431 return 0;
432 }
433 _debug_guardpage_minorder = res;
434 printk(KERN_INFO "Setting debug_guardpage_minorder to %lu\n", res);
435 return 0;
436}
437__setup("debug_guardpage_minorder=", debug_guardpage_minorder_setup);
438
439static inline void set_page_guard_flag(struct page *page)
440{
441 __set_bit(PAGE_DEBUG_FLAG_GUARD, &page->debug_flags);
442}
443
444static inline void clear_page_guard_flag(struct page *page)
445{
446 __clear_bit(PAGE_DEBUG_FLAG_GUARD, &page->debug_flags);
447}
448#else
449static inline void set_page_guard_flag(struct page *page) { }
450static inline void clear_page_guard_flag(struct page *page) { }
451#endif
452
Andrew Morton6aa3001b22006-04-18 22:20:52 -0700453static inline void set_page_order(struct page *page, int order)
454{
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700455 set_page_private(page, order);
Nick Piggin676165a2006-04-10 11:21:48 +1000456 __SetPageBuddy(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
459static inline void rmv_page_order(struct page *page)
460{
Nick Piggin676165a2006-04-10 11:21:48 +1000461 __ClearPageBuddy(page);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700462 set_page_private(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
465/*
466 * Locate the struct page for both the matching buddy in our
467 * pair (buddy1) and the combined O(n+1) page they form (page).
468 *
469 * 1) Any buddy B1 will have an order O twin B2 which satisfies
470 * the following equation:
471 * B2 = B1 ^ (1 << O)
472 * For example, if the starting buddy (buddy2) is #8 its order
473 * 1 buddy is #10:
474 * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
475 *
476 * 2) Any buddy B will have an order O+1 parent P which
477 * satisfies the following equation:
478 * P = B & ~(1 << O)
479 *
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200480 * Assumption: *_mem_map is contiguous at least up to MAX_ORDER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482static inline unsigned long
KyongHo Cho43506fa2011-01-13 15:47:24 -0800483__find_buddy_index(unsigned long page_idx, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
KyongHo Cho43506fa2011-01-13 15:47:24 -0800485 return page_idx ^ (1 << order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
488/*
489 * This function checks whether a page is free && is the buddy
490 * we can do coalesce a page and its buddy if
Nick Piggin13e74442006-01-06 00:10:58 -0800491 * (a) the buddy is not in a hole &&
Nick Piggin676165a2006-04-10 11:21:48 +1000492 * (b) the buddy is in the buddy system &&
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700493 * (c) a page and its buddy have the same order &&
494 * (d) a page and its buddy are in the same zone.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 *
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800496 * For recording whether a page is in the buddy system, we set ->_mapcount -2.
497 * Setting, clearing, and testing _mapcount -2 is serialized by zone->lock.
Nick Piggin676165a2006-04-10 11:21:48 +1000498 *
499 * For recording page's order, we use page_private(page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 */
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700501static inline int page_is_buddy(struct page *page, struct page *buddy,
502 int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Andy Whitcroft14e07292007-05-06 14:49:14 -0700504 if (!pfn_valid_within(page_to_pfn(buddy)))
Nick Piggin13e74442006-01-06 00:10:58 -0800505 return 0;
Nick Piggin13e74442006-01-06 00:10:58 -0800506
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700507 if (page_zone_id(page) != page_zone_id(buddy))
508 return 0;
509
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -0800510 if (page_is_guard(buddy) && page_order(buddy) == order) {
511 VM_BUG_ON(page_count(buddy) != 0);
512 return 1;
513 }
514
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700515 if (PageBuddy(buddy) && page_order(buddy) == order) {
Nick Piggina3af9c32009-06-16 15:32:10 -0700516 VM_BUG_ON(page_count(buddy) != 0);
Andrew Morton6aa3001b22006-04-18 22:20:52 -0700517 return 1;
Nick Piggin676165a2006-04-10 11:21:48 +1000518 }
Andrew Morton6aa3001b22006-04-18 22:20:52 -0700519 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
522/*
523 * Freeing function for a buddy system allocator.
524 *
525 * The concept of a buddy system is to maintain direct-mapped table
526 * (containing bit values) for memory blocks of various "orders".
527 * The bottom level table contains the map for the smallest allocatable
528 * units of memory (here, pages), and each level above it describes
529 * pairs of units from the levels below, hence, "buddies".
530 * At a high level, all that happens here is marking the table entry
531 * at the bottom level available, and propagating the changes upward
532 * as necessary, plus some accounting needed to play nicely with other
533 * parts of the VM system.
534 * At each level, we keep a list of pages, which are heads of continuous
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800535 * free pages of length of (1 << order) and marked with _mapcount -2. Page's
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700536 * order is recorded in page_private(page) field.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 * So when we are allocating or freeing one, we can derive the state of the
Michal Nazarewicz5f63b722012-01-11 15:16:11 +0100538 * other. That is, if we allocate a small block, and both were
539 * free, the remainder of the region must be split into blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 * If a block is freed, and its buddy is also free, then this
Michal Nazarewicz5f63b722012-01-11 15:16:11 +0100541 * triggers coalescing into a block of larger size.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 *
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +0100543 * -- nyc
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 */
545
Nick Piggin48db57f2006-01-08 01:00:42 -0800546static inline void __free_one_page(struct page *page,
Mel Gormaned0ae212009-06-16 15:32:07 -0700547 struct zone *zone, unsigned int order,
548 int migratetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 unsigned long page_idx;
Corrado Zoccolo6dda9d52010-05-24 14:31:54 -0700551 unsigned long combined_idx;
KyongHo Cho43506fa2011-01-13 15:47:24 -0800552 unsigned long uninitialized_var(buddy_idx);
Corrado Zoccolo6dda9d52010-05-24 14:31:54 -0700553 struct page *buddy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Cody P Schaferd29bb972013-02-22 16:35:25 -0800555 VM_BUG_ON(!zone_is_initialized(zone));
556
Nick Piggin224abf92006-01-06 00:11:11 -0800557 if (unlikely(PageCompound(page)))
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800558 if (unlikely(destroy_compound_page(page, order)))
559 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Mel Gormaned0ae212009-06-16 15:32:07 -0700561 VM_BUG_ON(migratetype == -1);
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 page_idx = page_to_pfn(page) & ((1 << MAX_ORDER) - 1);
564
Mel Gormanf2260e62009-06-16 15:32:13 -0700565 VM_BUG_ON(page_idx & ((1 << order) - 1));
Nick Piggin725d7042006-09-25 23:30:55 -0700566 VM_BUG_ON(bad_range(zone, page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 while (order < MAX_ORDER-1) {
KyongHo Cho43506fa2011-01-13 15:47:24 -0800569 buddy_idx = __find_buddy_index(page_idx, order);
570 buddy = page + (buddy_idx - page_idx);
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700571 if (!page_is_buddy(page, buddy, order))
Andy Whitcroft3c82d0c2008-07-23 21:27:11 -0700572 break;
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -0800573 /*
574 * Our buddy is free or it is CONFIG_DEBUG_PAGEALLOC guard page,
575 * merge with it and move up one order.
576 */
577 if (page_is_guard(buddy)) {
578 clear_page_guard_flag(buddy);
579 set_page_private(page, 0);
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -0700580 __mod_zone_freepage_state(zone, 1 << order,
581 migratetype);
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -0800582 } else {
583 list_del(&buddy->lru);
584 zone->free_area[order].nr_free--;
585 rmv_page_order(buddy);
586 }
KyongHo Cho43506fa2011-01-13 15:47:24 -0800587 combined_idx = buddy_idx & page_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 page = page + (combined_idx - page_idx);
589 page_idx = combined_idx;
590 order++;
591 }
592 set_page_order(page, order);
Corrado Zoccolo6dda9d52010-05-24 14:31:54 -0700593
594 /*
595 * If this is not the largest possible page, check if the buddy
596 * of the next-highest order is free. If it is, it's possible
597 * that pages are being freed that will coalesce soon. In case,
598 * that is happening, add the free page to the tail of the list
599 * so it's less likely to be used soon and more likely to be merged
600 * as a higher order page
601 */
Mel Gormanb7f50cf2010-10-26 14:21:11 -0700602 if ((order < MAX_ORDER-2) && pfn_valid_within(page_to_pfn(buddy))) {
Corrado Zoccolo6dda9d52010-05-24 14:31:54 -0700603 struct page *higher_page, *higher_buddy;
KyongHo Cho43506fa2011-01-13 15:47:24 -0800604 combined_idx = buddy_idx & page_idx;
605 higher_page = page + (combined_idx - page_idx);
606 buddy_idx = __find_buddy_index(combined_idx, order + 1);
Li Haifeng0ba8f2d2012-09-17 14:09:21 -0700607 higher_buddy = higher_page + (buddy_idx - combined_idx);
Corrado Zoccolo6dda9d52010-05-24 14:31:54 -0700608 if (page_is_buddy(higher_page, higher_buddy, order + 1)) {
609 list_add_tail(&page->lru,
610 &zone->free_area[order].free_list[migratetype]);
611 goto out;
612 }
613 }
614
615 list_add(&page->lru, &zone->free_area[order].free_list[migratetype]);
616out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 zone->free_area[order].nr_free++;
618}
619
Nick Piggin224abf92006-01-06 00:11:11 -0800620static inline int free_pages_check(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Nick Piggin92be2e332006-01-06 00:10:57 -0800622 if (unlikely(page_mapcount(page) |
623 (page->mapping != NULL) |
Nick Piggina3af9c32009-06-16 15:32:10 -0700624 (atomic_read(&page->_count) != 0) |
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -0700625 (page->flags & PAGE_FLAGS_CHECK_AT_FREE) |
626 (mem_cgroup_bad_page_check(page)))) {
Nick Piggin224abf92006-01-06 00:11:11 -0800627 bad_page(page);
Hugh Dickins79f4b7b2009-01-06 14:40:05 -0800628 return 1;
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800629 }
Mel Gorman22b751c2013-02-22 16:34:59 -0800630 page_nid_reset_last(page);
Hugh Dickins79f4b7b2009-01-06 14:40:05 -0800631 if (page->flags & PAGE_FLAGS_CHECK_AT_PREP)
632 page->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
633 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
635
636/*
Peter Zijlstra2b2430e2009-07-03 08:44:37 -0500637 * Frees a number of pages which have been collected from the pcp lists.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 * Assumes all pages on list are in same zone, and of same order.
Renaud Lienhart207f36e2005-09-10 00:26:59 -0700639 * count is the number of pages to free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 *
641 * If the zone was previously in an "all pages pinned" state then look to
642 * see if this freeing clears that state.
643 *
644 * And clear the zone's pages_scanned counter, to hold off the "all pages are
645 * pinned" detection logic.
646 */
Mel Gorman5f8dcc22009-09-21 17:03:19 -0700647static void free_pcppages_bulk(struct zone *zone, int count,
Peter Zijlstra2b2430e2009-07-03 08:44:37 -0500648 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Mel Gorman72853e22010-09-09 16:38:16 -0700650 int to_free = count;
Peter Zijlstra2b2430e2009-07-03 08:44:37 -0500651 unsigned long flags;
Mel Gorman5f8dcc22009-09-21 17:03:19 -0700652
Peter Zijlstra2b2430e2009-07-03 08:44:37 -0500653 spin_lock_irqsave(&zone->lock, flags);
KOSAKI Motohiro93e4a892010-03-05 13:41:55 -0800654 zone->all_unreclaimable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 zone->pages_scanned = 0;
Mel Gormanf2260e62009-06-16 15:32:13 -0700656
Peter Zijlstra2b2430e2009-07-03 08:44:37 -0500657 while (!list_empty(list)) {
658 struct page *page = list_first_entry(list, struct page, lru);
659 int mt; /* migratetype of the to-be-freed page */
660
661 /* must delete as __free_one_page list manipulates */
662 list_del(&page->lru);
663
664 mt = get_freepage_migratetype(page);
665 /* MIGRATE_MOVABLE list may include MIGRATE_RESERVEs */
666 __free_one_page(page, zone, 0, mt);
667 trace_mm_page_pcpu_drain(page, 0, mt);
668 if (likely(!is_migrate_isolate_page(page))) {
669 __mod_zone_page_state(zone, NR_FREE_PAGES, 1);
670 if (is_migrate_cma(mt))
671 __mod_zone_page_state(zone, NR_FREE_CMA_PAGES, 1);
672 }
673
674 to_free--;
675 }
676 WARN_ON(to_free != 0);
677 spin_unlock_irqrestore(&zone->lock, flags);
678}
679
680/*
681 * Moves a number of pages from the PCP lists to free list which
682 * is freed outside of the locked region.
683 *
684 * Assumes all pages on list are in same zone, and of same order.
685 * count is the number of pages to free.
686 */
687static void isolate_pcp_pages(int to_free, struct per_cpu_pages *src,
688 struct list_head *dst)
689{
690 int migratetype = 0, batch_free = 0;
691
Mel Gorman72853e22010-09-09 16:38:16 -0700692 while (to_free) {
Nick Piggin48db57f2006-01-08 01:00:42 -0800693 struct page *page;
Mel Gorman5f8dcc22009-09-21 17:03:19 -0700694 struct list_head *list;
Nick Piggin48db57f2006-01-08 01:00:42 -0800695
Mel Gorman5f8dcc22009-09-21 17:03:19 -0700696 /*
Mel Gormana6f9edd62009-09-21 17:03:20 -0700697 * Remove pages from lists in a round-robin fashion. A
698 * batch_free count is maintained that is incremented when an
699 * empty list is encountered. This is so more pages are freed
700 * off fuller lists instead of spinning excessively around empty
701 * lists
Mel Gorman5f8dcc22009-09-21 17:03:19 -0700702 */
703 do {
Mel Gormana6f9edd62009-09-21 17:03:20 -0700704 batch_free++;
Mel Gorman5f8dcc22009-09-21 17:03:19 -0700705 if (++migratetype == MIGRATE_PCPTYPES)
706 migratetype = 0;
Peter Zijlstra2b2430e2009-07-03 08:44:37 -0500707 list = &src->lists[migratetype];
Mel Gorman5f8dcc22009-09-21 17:03:19 -0700708 } while (list_empty(list));
709
Namhyung Kim1d168712011-03-22 16:32:45 -0700710 /* This is the only non-empty list. Free them all. */
711 if (batch_free == MIGRATE_PCPTYPES)
712 batch_free = to_free;
713
Mel Gormana6f9edd62009-09-21 17:03:20 -0700714 do {
Peter Zijlstra7ed54142011-06-21 11:24:35 +0200715 page = list_last_entry(list, struct page, lru);
Mel Gormana6f9edd62009-09-21 17:03:20 -0700716 list_del(&page->lru);
Peter Zijlstra2b2430e2009-07-03 08:44:37 -0500717 list_add(&page->lru, dst);
Mel Gorman72853e22010-09-09 16:38:16 -0700718 } while (--to_free && --batch_free && !list_empty(list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
Mel Gormaned0ae212009-06-16 15:32:07 -0700722static void free_one_page(struct zone *zone, struct page *page, int order,
723 int migratetype)
Nick Piggin48db57f2006-01-08 01:00:42 -0800724{
Peter Zijlstra2b2430e2009-07-03 08:44:37 -0500725 unsigned long flags;
726
727 spin_lock_irqsave(&zone->lock, flags);
KOSAKI Motohiro93e4a892010-03-05 13:41:55 -0800728 zone->all_unreclaimable = 0;
Christoph Lameter006d22d2006-09-25 23:31:48 -0700729 zone->pages_scanned = 0;
Mel Gormanf2260e62009-06-16 15:32:13 -0700730
Mel Gormaned0ae212009-06-16 15:32:07 -0700731 __free_one_page(page, zone, order, migratetype);
Minchan Kim194159f2013-02-22 16:33:58 -0800732 if (unlikely(!is_migrate_isolate(migratetype)))
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -0700733 __mod_zone_freepage_state(zone, 1 << order, migratetype);
Peter Zijlstra2b2430e2009-07-03 08:44:37 -0500734 spin_unlock_irqrestore(&zone->lock, flags);
Nick Piggin48db57f2006-01-08 01:00:42 -0800735}
736
KOSAKI Motohiroec95f532010-05-24 14:32:38 -0700737static bool free_pages_prepare(struct page *page, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 int i;
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800740 int bad = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Konstantin Khlebnikovb413d482012-01-10 15:07:09 -0800742 trace_mm_page_free(page, order);
Vegard Nossumb1eeab62008-11-25 16:55:53 +0100743 kmemcheck_free_shadow(page, order);
744
Andrea Arcangeli8dd60a32011-01-13 15:46:34 -0800745 if (PageAnon(page))
746 page->mapping = NULL;
747 for (i = 0; i < (1 << order); i++)
748 bad += free_pages_check(page + i);
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800749 if (bad)
KOSAKI Motohiroec95f532010-05-24 14:32:38 -0700750 return false;
Hugh Dickins689bceb2005-11-21 21:32:20 -0800751
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700752 if (!PageHighMem(page)) {
Nick Piggin9858db52006-10-11 01:21:30 -0700753 debug_check_no_locks_freed(page_address(page),PAGE_SIZE<<order);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700754 debug_check_no_obj_freed(page_address(page),
755 PAGE_SIZE << order);
756 }
Nick Piggindafb1362006-10-11 01:21:30 -0700757 arch_free_page(page, order);
Nick Piggin48db57f2006-01-08 01:00:42 -0800758 kernel_map_pages(page, 1 << order, 0);
Nick Piggindafb1362006-10-11 01:21:30 -0700759
KOSAKI Motohiroec95f532010-05-24 14:32:38 -0700760 return true;
761}
762
763static void __free_pages_ok(struct page *page, unsigned int order)
764{
765 unsigned long flags;
Minchan Kim95e34412012-10-08 16:32:11 -0700766 int migratetype;
KOSAKI Motohiroec95f532010-05-24 14:32:38 -0700767
768 if (!free_pages_prepare(page, order))
769 return;
770
Ingo Molnarbc7d7492009-07-03 08:29:37 -0500771 local_lock_irqsave(pa_lock, flags);
Christoph Lameterf8891e52006-06-30 01:55:45 -0700772 __count_vm_events(PGFREE, 1 << order);
Minchan Kim95e34412012-10-08 16:32:11 -0700773 migratetype = get_pageblock_migratetype(page);
774 set_freepage_migratetype(page, migratetype);
775 free_one_page(page_zone(page), page, order, migratetype);
Ingo Molnarbc7d7492009-07-03 08:29:37 -0500776 local_unlock_irqrestore(pa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778
Jiang Liu9feedc92012-12-12 13:52:12 -0800779/*
780 * Read access to zone->managed_pages is safe because it's unsigned long,
781 * but we still need to serialize writers. Currently all callers of
782 * __free_pages_bootmem() except put_page_bootmem() should only be used
783 * at boot time. So for shorter boot time, we shift the burden to
784 * put_page_bootmem() to serialize writers.
785 */
Yasunori Gotoaf370fb2008-07-23 21:28:17 -0700786void __meminit __free_pages_bootmem(struct page *page, unsigned int order)
David Howellsa226f6c2006-01-06 00:11:08 -0800787{
Johannes Weinerc3993072012-01-10 15:08:10 -0800788 unsigned int nr_pages = 1 << order;
789 unsigned int loop;
David Howellsa226f6c2006-01-06 00:11:08 -0800790
Johannes Weinerc3993072012-01-10 15:08:10 -0800791 prefetchw(page);
792 for (loop = 0; loop < nr_pages; loop++) {
793 struct page *p = &page[loop];
David Howellsa226f6c2006-01-06 00:11:08 -0800794
Johannes Weinerc3993072012-01-10 15:08:10 -0800795 if (loop + 1 < nr_pages)
796 prefetchw(p + 1);
797 __ClearPageReserved(p);
798 set_page_count(p, 0);
David Howellsa226f6c2006-01-06 00:11:08 -0800799 }
Johannes Weinerc3993072012-01-10 15:08:10 -0800800
Jiang Liu9feedc92012-12-12 13:52:12 -0800801 page_zone(page)->managed_pages += 1 << order;
Johannes Weinerc3993072012-01-10 15:08:10 -0800802 set_page_refcounted(page);
803 __free_pages(page, order);
David Howellsa226f6c2006-01-06 00:11:08 -0800804}
805
Michal Nazarewicz47118af2011-12-29 13:09:50 +0100806#ifdef CONFIG_CMA
807/* Free whole pageblock and set it's migration type to MIGRATE_CMA. */
808void __init init_cma_reserved_pageblock(struct page *page)
809{
810 unsigned i = pageblock_nr_pages;
811 struct page *p = page;
812
813 do {
814 __ClearPageReserved(p);
815 set_page_count(p, 0);
816 } while (++p, --i);
817
818 set_page_refcounted(page);
819 set_pageblock_migratetype(page, MIGRATE_CMA);
820 __free_pages(page, pageblock_order);
821 totalram_pages += pageblock_nr_pages;
Marek Szyprowski41a79732013-02-12 13:46:24 -0800822#ifdef CONFIG_HIGHMEM
823 if (PageHighMem(page))
824 totalhigh_pages += pageblock_nr_pages;
825#endif
Michal Nazarewicz47118af2011-12-29 13:09:50 +0100826}
827#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829/*
830 * The order of subdivision here is critical for the IO subsystem.
831 * Please do not alter this order without good reasons and regression
832 * testing. Specifically, as large blocks of memory are subdivided,
833 * the order in which smaller blocks are delivered depends on the order
834 * they're subdivided in this function. This is the primary factor
835 * influencing the order in which pages are delivered to the IO
836 * subsystem according to empirical testing, and this is also justified
837 * by considering the behavior of a buddy system containing a single
838 * large block of memory acted on by a series of small allocations.
839 * This behavior is a critical factor in sglist merging's success.
840 *
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +0100841 * -- nyc
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 */
Nick Piggin085cc7d2006-01-06 00:11:01 -0800843static inline void expand(struct zone *zone, struct page *page,
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700844 int low, int high, struct free_area *area,
845 int migratetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
847 unsigned long size = 1 << high;
848
849 while (high > low) {
850 area--;
851 high--;
852 size >>= 1;
Nick Piggin725d7042006-09-25 23:30:55 -0700853 VM_BUG_ON(bad_range(zone, &page[size]));
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -0800854
855#ifdef CONFIG_DEBUG_PAGEALLOC
856 if (high < debug_guardpage_minorder()) {
857 /*
858 * Mark as guard pages (or page), that will allow to
859 * merge back to allocator when buddy will be freed.
860 * Corresponding page table entries will not be touched,
861 * pages will stay not present in virtual address space
862 */
863 INIT_LIST_HEAD(&page[size].lru);
864 set_page_guard_flag(&page[size]);
865 set_page_private(&page[size], high);
866 /* Guard pages are not available for any usage */
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -0700867 __mod_zone_freepage_state(zone, -(1 << high),
868 migratetype);
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -0800869 continue;
870 }
871#endif
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700872 list_add(&page[size].lru, &area->free_list[migratetype]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 area->nr_free++;
874 set_page_order(&page[size], high);
875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878/*
879 * This page is about to be returned from the page allocator
880 */
Wu Fengguang2a7684a2009-09-16 11:50:12 +0200881static inline int check_new_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Nick Piggin92be2e332006-01-06 00:10:57 -0800883 if (unlikely(page_mapcount(page) |
884 (page->mapping != NULL) |
Nick Piggina3af9c32009-06-16 15:32:10 -0700885 (atomic_read(&page->_count) != 0) |
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -0700886 (page->flags & PAGE_FLAGS_CHECK_AT_PREP) |
887 (mem_cgroup_bad_page_check(page)))) {
Nick Piggin224abf92006-01-06 00:11:11 -0800888 bad_page(page);
Hugh Dickins689bceb2005-11-21 21:32:20 -0800889 return 1;
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800890 }
Wu Fengguang2a7684a2009-09-16 11:50:12 +0200891 return 0;
892}
893
894static int prep_new_page(struct page *page, int order, gfp_t gfp_flags)
895{
896 int i;
897
898 for (i = 0; i < (1 << order); i++) {
899 struct page *p = page + i;
900 if (unlikely(check_new_page(p)))
901 return 1;
902 }
Hugh Dickins689bceb2005-11-21 21:32:20 -0800903
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700904 set_page_private(page, 0);
Nick Piggin7835e982006-03-22 00:08:40 -0800905 set_page_refcounted(page);
Nick Piggincc1025092006-12-06 20:32:00 -0800906
907 arch_alloc_page(page, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 kernel_map_pages(page, 1 << order, 1);
Nick Piggin17cf4402006-03-22 00:08:41 -0800909
910 if (gfp_flags & __GFP_ZERO)
911 prep_zero_page(page, order, gfp_flags);
912
913 if (order && (gfp_flags & __GFP_COMP))
914 prep_compound_page(page, order);
915
Hugh Dickins689bceb2005-11-21 21:32:20 -0800916 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
918
Mel Gorman56fd56b2007-10-16 01:25:58 -0700919/*
920 * Go through the free lists for the given migratetype and remove
921 * the smallest available page from the freelists
922 */
Mel Gorman728ec982009-06-16 15:32:04 -0700923static inline
924struct page *__rmqueue_smallest(struct zone *zone, unsigned int order,
Mel Gorman56fd56b2007-10-16 01:25:58 -0700925 int migratetype)
926{
927 unsigned int current_order;
928 struct free_area * area;
929 struct page *page;
930
931 /* Find a page of the appropriate size in the preferred list */
932 for (current_order = order; current_order < MAX_ORDER; ++current_order) {
933 area = &(zone->free_area[current_order]);
934 if (list_empty(&area->free_list[migratetype]))
935 continue;
936
937 page = list_entry(area->free_list[migratetype].next,
938 struct page, lru);
939 list_del(&page->lru);
940 rmv_page_order(page);
941 area->nr_free--;
Mel Gorman56fd56b2007-10-16 01:25:58 -0700942 expand(zone, page, order, current_order, area, migratetype);
943 return page;
944 }
945
946 return NULL;
947}
948
949
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700950/*
951 * This array describes the order lists are fallen back to when
952 * the free lists for the desirable migrate type are depleted
953 */
Michal Nazarewicz47118af2011-12-29 13:09:50 +0100954static int fallbacks[MIGRATE_TYPES][4] = {
955 [MIGRATE_UNMOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_MOVABLE, MIGRATE_RESERVE },
956 [MIGRATE_RECLAIMABLE] = { MIGRATE_UNMOVABLE, MIGRATE_MOVABLE, MIGRATE_RESERVE },
957#ifdef CONFIG_CMA
958 [MIGRATE_MOVABLE] = { MIGRATE_CMA, MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE, MIGRATE_RESERVE },
959 [MIGRATE_CMA] = { MIGRATE_RESERVE }, /* Never used */
960#else
961 [MIGRATE_MOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE, MIGRATE_RESERVE },
962#endif
Michal Nazarewicz6d4a4912012-01-11 15:31:33 +0100963 [MIGRATE_RESERVE] = { MIGRATE_RESERVE }, /* Never used */
Minchan Kim194159f2013-02-22 16:33:58 -0800964#ifdef CONFIG_MEMORY_ISOLATION
Michal Nazarewicz6d4a4912012-01-11 15:31:33 +0100965 [MIGRATE_ISOLATE] = { MIGRATE_RESERVE }, /* Never used */
Minchan Kim194159f2013-02-22 16:33:58 -0800966#endif
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700967};
968
Mel Gormanc361be52007-10-16 01:25:51 -0700969/*
970 * Move the free pages in a range to the free lists of the requested type.
Mel Gormand9c23402007-10-16 01:26:01 -0700971 * Note that start_page and end_pages are not aligned on a pageblock
Mel Gormanc361be52007-10-16 01:25:51 -0700972 * boundary. If alignment is required, use move_freepages_block()
973 */
Minchan Kim435b4052012-10-08 16:32:16 -0700974int move_freepages(struct zone *zone,
Adrian Bunkb69a7282008-07-23 21:28:12 -0700975 struct page *start_page, struct page *end_page,
976 int migratetype)
Mel Gormanc361be52007-10-16 01:25:51 -0700977{
978 struct page *page;
979 unsigned long order;
Mel Gormand1003132007-10-16 01:26:00 -0700980 int pages_moved = 0;
Mel Gormanc361be52007-10-16 01:25:51 -0700981
982#ifndef CONFIG_HOLES_IN_ZONE
983 /*
984 * page_zone is not safe to call in this context when
985 * CONFIG_HOLES_IN_ZONE is set. This bug check is probably redundant
986 * anyway as we check zone boundaries in move_freepages_block().
987 * Remove at a later date when no bug reports exist related to
Mel Gormanac0e5b72007-10-16 01:25:58 -0700988 * grouping pages by mobility
Mel Gormanc361be52007-10-16 01:25:51 -0700989 */
990 BUG_ON(page_zone(start_page) != page_zone(end_page));
991#endif
992
993 for (page = start_page; page <= end_page;) {
Adam Litke344c7902008-09-02 14:35:38 -0700994 /* Make sure we are not inadvertently changing nodes */
995 VM_BUG_ON(page_to_nid(page) != zone_to_nid(zone));
996
Mel Gormanc361be52007-10-16 01:25:51 -0700997 if (!pfn_valid_within(page_to_pfn(page))) {
998 page++;
999 continue;
1000 }
1001
1002 if (!PageBuddy(page)) {
1003 page++;
1004 continue;
1005 }
1006
1007 order = page_order(page);
Kirill A. Shutemov84be48d2011-03-22 16:33:41 -07001008 list_move(&page->lru,
1009 &zone->free_area[order].free_list[migratetype]);
Minchan Kim95e34412012-10-08 16:32:11 -07001010 set_freepage_migratetype(page, migratetype);
Mel Gormanc361be52007-10-16 01:25:51 -07001011 page += 1 << order;
Mel Gormand1003132007-10-16 01:26:00 -07001012 pages_moved += 1 << order;
Mel Gormanc361be52007-10-16 01:25:51 -07001013 }
1014
Mel Gormand1003132007-10-16 01:26:00 -07001015 return pages_moved;
Mel Gormanc361be52007-10-16 01:25:51 -07001016}
1017
Minchan Kimee6f5092012-07-31 16:43:50 -07001018int move_freepages_block(struct zone *zone, struct page *page,
Linus Torvalds68e3e922012-06-03 20:05:57 -07001019 int migratetype)
Mel Gormanc361be52007-10-16 01:25:51 -07001020{
1021 unsigned long start_pfn, end_pfn;
1022 struct page *start_page, *end_page;
1023
1024 start_pfn = page_to_pfn(page);
Mel Gormand9c23402007-10-16 01:26:01 -07001025 start_pfn = start_pfn & ~(pageblock_nr_pages-1);
Mel Gormanc361be52007-10-16 01:25:51 -07001026 start_page = pfn_to_page(start_pfn);
Mel Gormand9c23402007-10-16 01:26:01 -07001027 end_page = start_page + pageblock_nr_pages - 1;
1028 end_pfn = start_pfn + pageblock_nr_pages - 1;
Mel Gormanc361be52007-10-16 01:25:51 -07001029
1030 /* Do not cross zone boundaries */
Cody P Schafer108bcc92013-02-22 16:35:23 -08001031 if (!zone_spans_pfn(zone, start_pfn))
Mel Gormanc361be52007-10-16 01:25:51 -07001032 start_page = page;
Cody P Schafer108bcc92013-02-22 16:35:23 -08001033 if (!zone_spans_pfn(zone, end_pfn))
Mel Gormanc361be52007-10-16 01:25:51 -07001034 return 0;
1035
1036 return move_freepages(zone, start_page, end_page, migratetype);
1037}
1038
Mel Gorman2f66a682009-09-21 17:02:31 -07001039static void change_pageblock_range(struct page *pageblock_page,
1040 int start_order, int migratetype)
1041{
1042 int nr_pageblocks = 1 << (start_order - pageblock_order);
1043
1044 while (nr_pageblocks--) {
1045 set_pageblock_migratetype(pageblock_page, migratetype);
1046 pageblock_page += pageblock_nr_pages;
1047 }
1048}
1049
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001050/* Remove an element from the buddy allocator from the fallback list */
Mel Gorman0ac3a402009-06-16 15:32:06 -07001051static inline struct page *
1052__rmqueue_fallback(struct zone *zone, int order, int start_migratetype)
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001053{
1054 struct free_area * area;
1055 int current_order;
1056 struct page *page;
1057 int migratetype, i;
1058
1059 /* Find the largest possible block of pages in the other list */
1060 for (current_order = MAX_ORDER-1; current_order >= order;
1061 --current_order) {
Michal Nazarewicz6d4a4912012-01-11 15:31:33 +01001062 for (i = 0;; i++) {
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001063 migratetype = fallbacks[start_migratetype][i];
1064
Mel Gorman56fd56b2007-10-16 01:25:58 -07001065 /* MIGRATE_RESERVE handled later if necessary */
1066 if (migratetype == MIGRATE_RESERVE)
Michal Nazarewicz6d4a4912012-01-11 15:31:33 +01001067 break;
Mel Gormane0104872007-10-16 01:25:53 -07001068
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001069 area = &(zone->free_area[current_order]);
1070 if (list_empty(&area->free_list[migratetype]))
1071 continue;
1072
1073 page = list_entry(area->free_list[migratetype].next,
1074 struct page, lru);
1075 area->nr_free--;
1076
1077 /*
Mel Gormanc361be52007-10-16 01:25:51 -07001078 * If breaking a large block of pages, move all free
Mel Gorman46dafbc2007-10-16 01:25:55 -07001079 * pages to the preferred allocation list. If falling
1080 * back for a reclaimable kernel allocation, be more
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001081 * aggressive about taking ownership of free pages
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001082 *
1083 * On the other hand, never change migration
1084 * type of MIGRATE_CMA pageblocks nor move CMA
1085 * pages on different free lists. We don't
1086 * want unmovable pages to be allocated from
1087 * MIGRATE_CMA areas.
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001088 */
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001089 if (!is_migrate_cma(migratetype) &&
1090 (unlikely(current_order >= pageblock_order / 2) ||
1091 start_migratetype == MIGRATE_RECLAIMABLE ||
1092 page_group_by_mobility_disabled)) {
1093 int pages;
Mel Gorman46dafbc2007-10-16 01:25:55 -07001094 pages = move_freepages_block(zone, page,
1095 start_migratetype);
1096
1097 /* Claim the whole block if over half of it is free */
Mel Gormandd5d2412009-09-05 11:17:11 -07001098 if (pages >= (1 << (pageblock_order-1)) ||
1099 page_group_by_mobility_disabled)
Mel Gorman46dafbc2007-10-16 01:25:55 -07001100 set_pageblock_migratetype(page,
1101 start_migratetype);
1102
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001103 migratetype = start_migratetype;
Mel Gormanc361be52007-10-16 01:25:51 -07001104 }
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001105
1106 /* Remove the page from the freelists */
1107 list_del(&page->lru);
1108 rmv_page_order(page);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001109
Mel Gorman2f66a682009-09-21 17:02:31 -07001110 /* Take ownership for orders >= pageblock_order */
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001111 if (current_order >= pageblock_order &&
1112 !is_migrate_cma(migratetype))
Mel Gorman2f66a682009-09-21 17:02:31 -07001113 change_pageblock_range(page, current_order,
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001114 start_migratetype);
1115
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001116 expand(zone, page, order, current_order, area,
1117 is_migrate_cma(migratetype)
1118 ? migratetype : start_migratetype);
Mel Gormane0fff1b2009-09-21 17:02:42 -07001119
1120 trace_mm_page_alloc_extfrag(page, order, current_order,
1121 start_migratetype, migratetype);
1122
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001123 return page;
1124 }
1125 }
1126
Mel Gorman728ec982009-06-16 15:32:04 -07001127 return NULL;
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001128}
1129
Mel Gorman56fd56b2007-10-16 01:25:58 -07001130/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 * Do the hard work of removing an element from the buddy allocator.
1132 * Call me with the zone->lock already held.
1133 */
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001134static struct page *__rmqueue(struct zone *zone, unsigned int order,
1135 int migratetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 struct page *page;
1138
Mel Gorman728ec982009-06-16 15:32:04 -07001139retry_reserve:
Mel Gorman56fd56b2007-10-16 01:25:58 -07001140 page = __rmqueue_smallest(zone, order, migratetype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Mel Gorman728ec982009-06-16 15:32:04 -07001142 if (unlikely(!page) && migratetype != MIGRATE_RESERVE) {
Mel Gorman56fd56b2007-10-16 01:25:58 -07001143 page = __rmqueue_fallback(zone, order, migratetype);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001144
Mel Gorman728ec982009-06-16 15:32:04 -07001145 /*
1146 * Use MIGRATE_RESERVE rather than fail an allocation. goto
1147 * is used because __rmqueue_smallest is an inline function
1148 * and we want just one call site
1149 */
1150 if (!page) {
1151 migratetype = MIGRATE_RESERVE;
1152 goto retry_reserve;
1153 }
1154 }
1155
Mel Gorman0d3d0622009-09-21 17:02:44 -07001156 trace_mm_page_alloc_zone_locked(page, order, migratetype);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001157 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158}
1159
Michal Nazarewicz5f63b722012-01-11 15:16:11 +01001160/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 * Obtain a specified number of elements from the buddy allocator, all under
1162 * a single hold of the lock, for efficiency. Add them to the supplied list.
1163 * Returns the number of new pages which were placed at *list.
1164 */
Michal Nazarewicz5f63b722012-01-11 15:16:11 +01001165static int rmqueue_bulk(struct zone *zone, unsigned int order,
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001166 unsigned long count, struct list_head *list,
Mel Gormane084b2d2009-07-29 15:02:04 -07001167 int migratetype, int cold)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001169 int mt = migratetype, i;
Michal Nazarewicz5f63b722012-01-11 15:16:11 +01001170
Nick Pigginc54ad302006-01-06 00:10:56 -08001171 spin_lock(&zone->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 for (i = 0; i < count; ++i) {
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001173 struct page *page = __rmqueue(zone, order, migratetype);
Nick Piggin085cc7d2006-01-06 00:11:01 -08001174 if (unlikely(page == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 break;
Mel Gorman81eabcb2007-12-17 16:20:05 -08001176
1177 /*
1178 * Split buddy pages returned by expand() are received here
1179 * in physical page order. The page is added to the callers and
1180 * list and the list head then moves forward. From the callers
1181 * perspective, the linked list is ordered by page number in
1182 * some conditions. This is useful for IO devices that can
1183 * merge IO requests if the physical pages are ordered
1184 * properly.
1185 */
Mel Gormane084b2d2009-07-29 15:02:04 -07001186 if (likely(cold == 0))
1187 list_add(&page->lru, list);
1188 else
1189 list_add_tail(&page->lru, list);
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001190 if (IS_ENABLED(CONFIG_CMA)) {
1191 mt = get_pageblock_migratetype(page);
Minchan Kim194159f2013-02-22 16:33:58 -08001192 if (!is_migrate_cma(mt) && !is_migrate_isolate(mt))
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001193 mt = migratetype;
1194 }
Minchan Kimb12c4ad2012-10-08 16:32:08 -07001195 set_freepage_migratetype(page, mt);
Mel Gorman81eabcb2007-12-17 16:20:05 -08001196 list = &page->lru;
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -07001197 if (is_migrate_cma(mt))
1198 __mod_zone_page_state(zone, NR_FREE_CMA_PAGES,
1199 -(1 << order));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
Mel Gormanf2260e62009-06-16 15:32:13 -07001201 __mod_zone_page_state(zone, NR_FREE_PAGES, -(i << order));
Nick Pigginc54ad302006-01-06 00:10:56 -08001202 spin_unlock(&zone->lock);
Nick Piggin085cc7d2006-01-06 00:11:01 -08001203 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
1205
Christoph Lameter4ae7c032005-06-21 17:14:57 -07001206#ifdef CONFIG_NUMA
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001207/*
Christoph Lameter4037d452007-05-09 02:35:14 -07001208 * Called from the vmstat counter updater to drain pagesets of this
1209 * currently executing processor on remote nodes after they have
1210 * expired.
1211 *
Christoph Lameter879336c2006-03-22 00:09:08 -08001212 * Note that this function must be called with the thread pinned to
1213 * a single processor.
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001214 */
Christoph Lameter4037d452007-05-09 02:35:14 -07001215void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
Christoph Lameter4ae7c032005-06-21 17:14:57 -07001216{
Christoph Lameter4ae7c032005-06-21 17:14:57 -07001217 unsigned long flags;
Peter Zijlstra2b2430e2009-07-03 08:44:37 -05001218 LIST_HEAD(dst);
Christoph Lameter4037d452007-05-09 02:35:14 -07001219 int to_drain;
Christoph Lameter4ae7c032005-06-21 17:14:57 -07001220
Ingo Molnarbc7d7492009-07-03 08:29:37 -05001221 local_lock_irqsave(pa_lock, flags);
Christoph Lameter4037d452007-05-09 02:35:14 -07001222 if (pcp->count >= pcp->batch)
1223 to_drain = pcp->batch;
1224 else
1225 to_drain = pcp->count;
KOSAKI Motohiro2a135152012-07-31 16:42:53 -07001226 if (to_drain > 0) {
Peter Zijlstra2b2430e2009-07-03 08:44:37 -05001227 isolate_pcp_pages(to_drain, pcp, &dst);
KOSAKI Motohiro2a135152012-07-31 16:42:53 -07001228 pcp->count -= to_drain;
1229 }
Ingo Molnarbc7d7492009-07-03 08:29:37 -05001230 local_unlock_irqrestore(pa_lock, flags);
Peter Zijlstra2b2430e2009-07-03 08:44:37 -05001231 free_pcppages_bulk(zone, to_drain, &dst);
Christoph Lameter4ae7c032005-06-21 17:14:57 -07001232}
1233#endif
1234
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001235/*
1236 * Drain pages of the indicated processor.
1237 *
1238 * The processor must either be the current processor and the
1239 * thread pinned to the current processor or a processor that
1240 * is not online.
1241 */
1242static void drain_pages(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
Nick Pigginc54ad302006-01-06 00:10:56 -08001244 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 struct zone *zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
KOSAKI Motohiroee99c712009-03-31 15:19:31 -07001247 for_each_populated_zone(zone) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 struct per_cpu_pageset *pset;
Christoph Lameter3dfa5722008-02-04 22:29:19 -08001249 struct per_cpu_pages *pcp;
Peter Zijlstra2b2430e2009-07-03 08:44:37 -05001250 LIST_HEAD(dst);
1251 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Ingo Molnarbc7d7492009-07-03 08:29:37 -05001253 cpu_lock_irqsave(cpu, flags);
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09001254 pset = per_cpu_ptr(zone->pageset, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Christoph Lameter3dfa5722008-02-04 22:29:19 -08001256 pcp = &pset->pcp;
Peter Zijlstra2b2430e2009-07-03 08:44:37 -05001257 count = pcp->count;
1258 if (count) {
1259 isolate_pcp_pages(count, pcp, &dst);
David Rientjes2ff754f2011-01-25 15:07:23 -08001260 pcp->count = 0;
1261 }
Ingo Molnarbc7d7492009-07-03 08:29:37 -05001262 cpu_unlock_irqrestore(cpu, flags);
Peter Zijlstra2b2430e2009-07-03 08:44:37 -05001263 if (count)
1264 free_pcppages_bulk(zone, count, &dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
1266}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001268/*
1269 * Spill all of this CPU's per-cpu pages back into the buddy allocator.
1270 */
1271void drain_local_pages(void *arg)
1272{
1273 drain_pages(smp_processor_id());
1274}
1275
1276/*
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07001277 * Spill all the per-cpu pages from all CPUs back into the buddy allocator.
1278 *
1279 * Note that this code is protected against sending an IPI to an offline
1280 * CPU but does not guarantee sending an IPI to newly hotplugged CPUs:
1281 * on_each_cpu_mask() blocks hotplug and won't talk to offlined CPUs but
1282 * nothing keeps CPUs from showing up after we populated the cpumask and
1283 * before the call to on_each_cpu_mask().
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001284 */
1285void drain_all_pages(void)
1286{
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07001287 int cpu;
1288 struct per_cpu_pageset *pcp;
1289 struct zone *zone;
1290
1291 /*
1292 * Allocate in the BSS so we wont require allocation in
1293 * direct reclaim path for CONFIG_CPUMASK_OFFSTACK=y
1294 */
1295 static cpumask_t cpus_with_pcps;
1296
1297 /*
1298 * We don't care about racing with CPU hotplug event
1299 * as offline notification will cause the notified
1300 * cpu to drain that CPU pcps and on_each_cpu_mask
1301 * disables preemption as part of its processing
1302 */
1303 for_each_online_cpu(cpu) {
1304 bool has_pcps = false;
1305 for_each_populated_zone(zone) {
1306 pcp = per_cpu_ptr(zone->pageset, cpu);
1307 if (pcp->pcp.count) {
1308 has_pcps = true;
1309 break;
1310 }
1311 }
1312 if (has_pcps)
1313 cpumask_set_cpu(cpu, &cpus_with_pcps);
1314 else
1315 cpumask_clear_cpu(cpu, &cpus_with_pcps);
1316 }
Ingo Molnarbc7d7492009-07-03 08:29:37 -05001317#ifndef CONFIG_PREEMPT_RT_BASE
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07001318 on_each_cpu_mask(&cpus_with_pcps, drain_local_pages, NULL, 1);
Ingo Molnarbc7d7492009-07-03 08:29:37 -05001319#else
1320 for_each_cpu(cpu, &cpus_with_pcps)
1321 drain_pages(cpu);
1322#endif
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001323}
1324
Rafael J. Wysocki296699d2007-07-29 23:27:18 +02001325#ifdef CONFIG_HIBERNATION
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327void mark_free_pages(struct zone *zone)
1328{
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07001329 unsigned long pfn, max_zone_pfn;
1330 unsigned long flags;
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001331 int order, t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 struct list_head *curr;
1333
1334 if (!zone->spanned_pages)
1335 return;
1336
1337 spin_lock_irqsave(&zone->lock, flags);
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07001338
Cody P Schafer108bcc92013-02-22 16:35:23 -08001339 max_zone_pfn = zone_end_pfn(zone);
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07001340 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
1341 if (pfn_valid(pfn)) {
1342 struct page *page = pfn_to_page(pfn);
1343
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001344 if (!swsusp_page_is_forbidden(page))
1345 swsusp_unset_page_free(page);
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07001346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001348 for_each_migratetype_order(order, t) {
1349 list_for_each(curr, &zone->free_area[order].free_list[t]) {
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07001350 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07001352 pfn = page_to_pfn(list_entry(curr, struct page, lru));
1353 for (i = 0; i < (1UL << order); i++)
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001354 swsusp_set_page_free(pfn_to_page(pfn + i));
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07001355 }
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 spin_unlock_irqrestore(&zone->lock, flags);
1358}
Mel Gormane2c55dc2007-10-16 01:25:50 -07001359#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 * Free a 0-order page
Li Hongfc916682010-03-05 13:41:54 -08001363 * cold == 1 ? free a cold page : free a hot page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 */
Li Hongfc916682010-03-05 13:41:54 -08001365void free_hot_cold_page(struct page *page, int cold)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
1367 struct zone *zone = page_zone(page);
1368 struct per_cpu_pages *pcp;
1369 unsigned long flags;
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001370 int migratetype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
KOSAKI Motohiroec95f532010-05-24 14:32:38 -07001372 if (!free_pages_prepare(page, 0))
Hugh Dickins689bceb2005-11-21 21:32:20 -08001373 return;
1374
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001375 migratetype = get_pageblock_migratetype(page);
Minchan Kimb12c4ad2012-10-08 16:32:08 -07001376 set_freepage_migratetype(page, migratetype);
Ingo Molnarbc7d7492009-07-03 08:29:37 -05001377 local_lock_irqsave(pa_lock, flags);
Christoph Lameterf8891e52006-06-30 01:55:45 -07001378 __count_vm_event(PGFREE);
Mel Gormanda456f12009-06-16 15:32:08 -07001379
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001380 /*
1381 * We only track unmovable, reclaimable and movable on pcp lists.
1382 * Free ISOLATE pages back to the allocator because they are being
1383 * offlined but treat RESERVE as movable pages so we can get those
1384 * areas back if necessary. Otherwise, we may have to free
1385 * excessively into the page allocator
1386 */
1387 if (migratetype >= MIGRATE_PCPTYPES) {
Minchan Kim194159f2013-02-22 16:33:58 -08001388 if (unlikely(is_migrate_isolate(migratetype))) {
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001389 free_one_page(zone, page, 0, migratetype);
1390 goto out;
1391 }
1392 migratetype = MIGRATE_MOVABLE;
1393 }
1394
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09001395 pcp = &this_cpu_ptr(zone->pageset)->pcp;
Christoph Lameter3dfa5722008-02-04 22:29:19 -08001396 if (cold)
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001397 list_add_tail(&page->lru, &pcp->lists[migratetype]);
Christoph Lameter3dfa5722008-02-04 22:29:19 -08001398 else
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001399 list_add(&page->lru, &pcp->lists[migratetype]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 pcp->count++;
Nick Piggin48db57f2006-01-08 01:00:42 -08001401 if (pcp->count >= pcp->high) {
Peter Zijlstra2b2430e2009-07-03 08:44:37 -05001402 LIST_HEAD(dst);
1403 int count;
1404
1405 isolate_pcp_pages(pcp->batch, pcp, &dst);
Nick Piggin48db57f2006-01-08 01:00:42 -08001406 pcp->count -= pcp->batch;
Peter Zijlstra2b2430e2009-07-03 08:44:37 -05001407 count = pcp->batch;
1408 local_unlock_irqrestore(pa_lock, flags);
1409 free_pcppages_bulk(zone, count, &dst);
1410 return;
Nick Piggin48db57f2006-01-08 01:00:42 -08001411 }
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001412
1413out:
Ingo Molnarbc7d7492009-07-03 08:29:37 -05001414 local_unlock_irqrestore(pa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08001417/*
Konstantin Khlebnikovcc598502012-01-10 15:07:04 -08001418 * Free a list of 0-order pages
1419 */
1420void free_hot_cold_page_list(struct list_head *list, int cold)
1421{
1422 struct page *page, *next;
1423
1424 list_for_each_entry_safe(page, next, list, lru) {
Konstantin Khlebnikovb413d482012-01-10 15:07:09 -08001425 trace_mm_page_free_batched(page, cold);
Konstantin Khlebnikovcc598502012-01-10 15:07:04 -08001426 free_hot_cold_page(page, cold);
1427 }
1428}
1429
1430/*
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08001431 * split_page takes a non-compound higher-order page, and splits it into
1432 * n (1<<order) sub-pages: page[0..n]
1433 * Each sub-page must be freed individually.
1434 *
1435 * Note: this is probably too low level an operation for use in drivers.
1436 * Please consult with lkml before using this in your driver.
1437 */
1438void split_page(struct page *page, unsigned int order)
1439{
1440 int i;
1441
Nick Piggin725d7042006-09-25 23:30:55 -07001442 VM_BUG_ON(PageCompound(page));
1443 VM_BUG_ON(!page_count(page));
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001444
1445#ifdef CONFIG_KMEMCHECK
1446 /*
1447 * Split shadow pages too, because free(page[0]) would
1448 * otherwise free the whole shadow.
1449 */
1450 if (kmemcheck_page_is_tracked(page))
1451 split_page(virt_to_page(page[0].shadow), order);
1452#endif
1453
Nick Piggin7835e982006-03-22 00:08:40 -08001454 for (i = 1; i < (1 << order); i++)
1455 set_page_refcounted(page + i);
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08001456}
K. Y. Srinivasan5853ff22013-03-25 15:47:38 -07001457EXPORT_SYMBOL_GPL(split_page);
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08001458
Mel Gorman8fb74b92013-01-11 14:32:16 -08001459static int __isolate_free_page(struct page *page, unsigned int order)
Mel Gorman748446b2010-05-24 14:32:27 -07001460{
Mel Gorman748446b2010-05-24 14:32:27 -07001461 unsigned long watermark;
1462 struct zone *zone;
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -07001463 int mt;
Mel Gorman748446b2010-05-24 14:32:27 -07001464
1465 BUG_ON(!PageBuddy(page));
1466
1467 zone = page_zone(page);
Marek Szyprowski2e30abd2012-12-11 16:02:57 -08001468 mt = get_pageblock_migratetype(page);
Mel Gorman748446b2010-05-24 14:32:27 -07001469
Minchan Kim194159f2013-02-22 16:33:58 -08001470 if (!is_migrate_isolate(mt)) {
Marek Szyprowski2e30abd2012-12-11 16:02:57 -08001471 /* Obey watermarks as if the page was being allocated */
1472 watermark = low_wmark_pages(zone) + (1 << order);
1473 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
1474 return 0;
1475
Mel Gorman8fb74b92013-01-11 14:32:16 -08001476 __mod_zone_freepage_state(zone, -(1UL << order), mt);
Marek Szyprowski2e30abd2012-12-11 16:02:57 -08001477 }
Mel Gorman748446b2010-05-24 14:32:27 -07001478
1479 /* Remove page from free list */
1480 list_del(&page->lru);
1481 zone->free_area[order].nr_free--;
1482 rmv_page_order(page);
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -07001483
Mel Gorman8fb74b92013-01-11 14:32:16 -08001484 /* Set the pageblock if the isolated page is at least a pageblock */
Mel Gorman748446b2010-05-24 14:32:27 -07001485 if (order >= pageblock_order - 1) {
1486 struct page *endpage = page + (1 << order) - 1;
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001487 for (; page < endpage; page += pageblock_nr_pages) {
1488 int mt = get_pageblock_migratetype(page);
Minchan Kim194159f2013-02-22 16:33:58 -08001489 if (!is_migrate_isolate(mt) && !is_migrate_cma(mt))
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001490 set_pageblock_migratetype(page,
1491 MIGRATE_MOVABLE);
1492 }
Mel Gorman748446b2010-05-24 14:32:27 -07001493 }
1494
Mel Gorman8fb74b92013-01-11 14:32:16 -08001495 return 1UL << order;
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001496}
1497
1498/*
1499 * Similar to split_page except the page is already free. As this is only
1500 * being used for migration, the migratetype of the block also changes.
1501 * As this is called with interrupts disabled, the caller is responsible
1502 * for calling arch_alloc_page() and kernel_map_page() after interrupts
1503 * are enabled.
1504 *
1505 * Note: this is probably too low level an operation for use in drivers.
1506 * Please consult with lkml before using this in your driver.
1507 */
1508int split_free_page(struct page *page)
1509{
1510 unsigned int order;
1511 int nr_pages;
1512
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001513 order = page_order(page);
1514
Mel Gorman8fb74b92013-01-11 14:32:16 -08001515 nr_pages = __isolate_free_page(page, order);
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001516 if (!nr_pages)
1517 return 0;
1518
1519 /* Split into individual pages */
1520 set_page_refcounted(page);
1521 split_page(page, order);
1522 return nr_pages;
Mel Gorman748446b2010-05-24 14:32:27 -07001523}
1524
1525/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
1527 * we cheat by calling it from here, in the order > 0 path. Saves a branch
1528 * or two.
1529 */
Mel Gorman0a15c3e2009-06-16 15:32:05 -07001530static inline
1531struct page *buffered_rmqueue(struct zone *preferred_zone,
Mel Gorman3dd28262009-06-16 15:32:00 -07001532 struct zone *zone, int order, gfp_t gfp_flags,
1533 int migratetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
1535 unsigned long flags;
Hugh Dickins689bceb2005-11-21 21:32:20 -08001536 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 int cold = !!(gfp_flags & __GFP_COLD);
1538
Hugh Dickins689bceb2005-11-21 21:32:20 -08001539again:
Nick Piggin48db57f2006-01-08 01:00:42 -08001540 if (likely(order == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 struct per_cpu_pages *pcp;
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001542 struct list_head *list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Ingo Molnarbc7d7492009-07-03 08:29:37 -05001544 local_lock_irqsave(pa_lock, flags);
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09001545 pcp = &this_cpu_ptr(zone->pageset)->pcp;
1546 list = &pcp->lists[migratetype];
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001547 if (list_empty(list)) {
Mel Gorman535131e62007-10-16 01:25:49 -07001548 pcp->count += rmqueue_bulk(zone, 0,
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001549 pcp->batch, list,
Mel Gormane084b2d2009-07-29 15:02:04 -07001550 migratetype, cold);
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001551 if (unlikely(list_empty(list)))
Shaohua Li6fb332f2009-09-21 17:01:17 -07001552 goto failed;
Mel Gorman535131e62007-10-16 01:25:49 -07001553 }
Mel Gormanb92a6ed2007-10-16 01:25:50 -07001554
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001555 if (cold)
1556 page = list_entry(list->prev, struct page, lru);
1557 else
1558 page = list_entry(list->next, struct page, lru);
1559
Mel Gormanb92a6ed2007-10-16 01:25:50 -07001560 list_del(&page->lru);
1561 pcp->count--;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001562 } else {
Andrew Mortondab48da2009-06-16 15:32:37 -07001563 if (unlikely(gfp_flags & __GFP_NOFAIL)) {
1564 /*
1565 * __GFP_NOFAIL is not to be used in new code.
1566 *
1567 * All __GFP_NOFAIL callers should be fixed so that they
1568 * properly detect and handle allocation failures.
1569 *
1570 * We most definitely don't want callers attempting to
Linus Torvalds4923abf2009-06-24 12:16:49 -07001571 * allocate greater than order-1 page units with
Andrew Mortondab48da2009-06-16 15:32:37 -07001572 * __GFP_NOFAIL.
1573 */
Linus Torvalds4923abf2009-06-24 12:16:49 -07001574 WARN_ON_ONCE(order > 1);
Andrew Mortondab48da2009-06-16 15:32:37 -07001575 }
Ingo Molnarbc7d7492009-07-03 08:29:37 -05001576 local_spin_lock_irqsave(pa_lock, &zone->lock, flags);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001577 page = __rmqueue(zone, order, migratetype);
Ingo Molnarbc7d7492009-07-03 08:29:37 -05001578 if (!page) {
1579 spin_unlock(&zone->lock);
Nick Piggina74609f2006-01-06 00:11:20 -08001580 goto failed;
Ingo Molnarbc7d7492009-07-03 08:29:37 -05001581 }
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -07001582 __mod_zone_freepage_state(zone, -(1 << order),
1583 get_pageblock_migratetype(page));
Ingo Molnarbc7d7492009-07-03 08:29:37 -05001584 spin_unlock(&zone->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
1586
Christoph Lameterf8891e52006-06-30 01:55:45 -07001587 __count_zone_vm_events(PGALLOC, zone, 1 << order);
Andi Kleen78afd562011-03-22 16:33:12 -07001588 zone_statistics(preferred_zone, zone, gfp_flags);
Ingo Molnarbc7d7492009-07-03 08:29:37 -05001589 local_unlock_irqrestore(pa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Nick Piggin725d7042006-09-25 23:30:55 -07001591 VM_BUG_ON(bad_range(zone, page));
Nick Piggin17cf4402006-03-22 00:08:41 -08001592 if (prep_new_page(page, order, gfp_flags))
Nick Piggina74609f2006-01-06 00:11:20 -08001593 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 return page;
Nick Piggina74609f2006-01-06 00:11:20 -08001595
1596failed:
Ingo Molnarbc7d7492009-07-03 08:29:37 -05001597 local_unlock_irqrestore(pa_lock, flags);
Nick Piggina74609f2006-01-06 00:11:20 -08001598 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599}
1600
Akinobu Mita933e3122006-12-08 02:39:45 -08001601#ifdef CONFIG_FAIL_PAGE_ALLOC
1602
Akinobu Mitab2588c42011-07-26 16:09:03 -07001603static struct {
Akinobu Mita933e3122006-12-08 02:39:45 -08001604 struct fault_attr attr;
1605
1606 u32 ignore_gfp_highmem;
1607 u32 ignore_gfp_wait;
Akinobu Mita54114992007-07-15 23:40:23 -07001608 u32 min_order;
Akinobu Mita933e3122006-12-08 02:39:45 -08001609} fail_page_alloc = {
1610 .attr = FAULT_ATTR_INITIALIZER,
Don Mullis6b1b60f2006-12-08 02:39:53 -08001611 .ignore_gfp_wait = 1,
1612 .ignore_gfp_highmem = 1,
Akinobu Mita54114992007-07-15 23:40:23 -07001613 .min_order = 1,
Akinobu Mita933e3122006-12-08 02:39:45 -08001614};
1615
1616static int __init setup_fail_page_alloc(char *str)
1617{
1618 return setup_fault_attr(&fail_page_alloc.attr, str);
1619}
1620__setup("fail_page_alloc=", setup_fail_page_alloc);
1621
Gavin Shandeaf3862012-07-31 16:41:51 -07001622static bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
Akinobu Mita933e3122006-12-08 02:39:45 -08001623{
Akinobu Mita54114992007-07-15 23:40:23 -07001624 if (order < fail_page_alloc.min_order)
Gavin Shandeaf3862012-07-31 16:41:51 -07001625 return false;
Akinobu Mita933e3122006-12-08 02:39:45 -08001626 if (gfp_mask & __GFP_NOFAIL)
Gavin Shandeaf3862012-07-31 16:41:51 -07001627 return false;
Akinobu Mita933e3122006-12-08 02:39:45 -08001628 if (fail_page_alloc.ignore_gfp_highmem && (gfp_mask & __GFP_HIGHMEM))
Gavin Shandeaf3862012-07-31 16:41:51 -07001629 return false;
Akinobu Mita933e3122006-12-08 02:39:45 -08001630 if (fail_page_alloc.ignore_gfp_wait && (gfp_mask & __GFP_WAIT))
Gavin Shandeaf3862012-07-31 16:41:51 -07001631 return false;
Akinobu Mita933e3122006-12-08 02:39:45 -08001632
1633 return should_fail(&fail_page_alloc.attr, 1 << order);
1634}
1635
1636#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
1637
1638static int __init fail_page_alloc_debugfs(void)
1639{
Al Virof4ae40a2011-07-24 04:33:43 -04001640 umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
Akinobu Mita933e3122006-12-08 02:39:45 -08001641 struct dentry *dir;
Akinobu Mita933e3122006-12-08 02:39:45 -08001642
Akinobu Mitadd48c082011-08-03 16:21:01 -07001643 dir = fault_create_debugfs_attr("fail_page_alloc", NULL,
1644 &fail_page_alloc.attr);
1645 if (IS_ERR(dir))
1646 return PTR_ERR(dir);
Akinobu Mita933e3122006-12-08 02:39:45 -08001647
Akinobu Mitab2588c42011-07-26 16:09:03 -07001648 if (!debugfs_create_bool("ignore-gfp-wait", mode, dir,
1649 &fail_page_alloc.ignore_gfp_wait))
1650 goto fail;
1651 if (!debugfs_create_bool("ignore-gfp-highmem", mode, dir,
1652 &fail_page_alloc.ignore_gfp_highmem))
1653 goto fail;
1654 if (!debugfs_create_u32("min-order", mode, dir,
1655 &fail_page_alloc.min_order))
1656 goto fail;
Akinobu Mita933e3122006-12-08 02:39:45 -08001657
Akinobu Mitab2588c42011-07-26 16:09:03 -07001658 return 0;
1659fail:
Akinobu Mitadd48c082011-08-03 16:21:01 -07001660 debugfs_remove_recursive(dir);
Akinobu Mita933e3122006-12-08 02:39:45 -08001661
Akinobu Mitab2588c42011-07-26 16:09:03 -07001662 return -ENOMEM;
Akinobu Mita933e3122006-12-08 02:39:45 -08001663}
1664
1665late_initcall(fail_page_alloc_debugfs);
1666
1667#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
1668
1669#else /* CONFIG_FAIL_PAGE_ALLOC */
1670
Gavin Shandeaf3862012-07-31 16:41:51 -07001671static inline bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
Akinobu Mita933e3122006-12-08 02:39:45 -08001672{
Gavin Shandeaf3862012-07-31 16:41:51 -07001673 return false;
Akinobu Mita933e3122006-12-08 02:39:45 -08001674}
1675
1676#endif /* CONFIG_FAIL_PAGE_ALLOC */
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678/*
Mel Gorman88f5acf2011-01-13 15:45:41 -08001679 * Return true if free pages are above 'mark'. This takes into account the order
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 * of the allocation.
1681 */
Mel Gorman88f5acf2011-01-13 15:45:41 -08001682static bool __zone_watermark_ok(struct zone *z, int order, unsigned long mark,
1683 int classzone_idx, int alloc_flags, long free_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
1685 /* free_pages my go negative - that's OK */
Christoph Lameterd23ad422007-02-10 01:43:02 -08001686 long min = mark;
Minchan Kim2cfed072012-07-31 16:43:53 -07001687 long lowmem_reserve = z->lowmem_reserve[classzone_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 int o;
Tomasz Stanislawski026b0812013-06-12 14:05:02 -07001689 long free_cma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Michal Hockodf0a6da2012-01-10 15:08:02 -08001691 free_pages -= (1 << order) - 1;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001692 if (alloc_flags & ALLOC_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 min -= min / 2;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001694 if (alloc_flags & ALLOC_HARDER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 min -= min / 4;
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001696#ifdef CONFIG_CMA
1697 /* If allocation can't use CMA areas don't use free CMA pages */
1698 if (!(alloc_flags & ALLOC_CMA))
Tomasz Stanislawski026b0812013-06-12 14:05:02 -07001699 free_cma = zone_page_state(z, NR_FREE_CMA_PAGES);
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001700#endif
Tomasz Stanislawski026b0812013-06-12 14:05:02 -07001701
1702 if (free_pages - free_cma <= min + lowmem_reserve)
Mel Gorman88f5acf2011-01-13 15:45:41 -08001703 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 for (o = 0; o < order; o++) {
1705 /* At the next order, this order's pages become unavailable */
1706 free_pages -= z->free_area[o].nr_free << o;
1707
1708 /* Require fewer higher order pages to be free */
1709 min >>= 1;
1710
1711 if (free_pages <= min)
Mel Gorman88f5acf2011-01-13 15:45:41 -08001712 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 }
Mel Gorman88f5acf2011-01-13 15:45:41 -08001714 return true;
1715}
1716
1717bool zone_watermark_ok(struct zone *z, int order, unsigned long mark,
1718 int classzone_idx, int alloc_flags)
1719{
1720 return __zone_watermark_ok(z, order, mark, classzone_idx, alloc_flags,
1721 zone_page_state(z, NR_FREE_PAGES));
1722}
1723
1724bool zone_watermark_ok_safe(struct zone *z, int order, unsigned long mark,
1725 int classzone_idx, int alloc_flags)
1726{
1727 long free_pages = zone_page_state(z, NR_FREE_PAGES);
1728
1729 if (z->percpu_drift_mark && free_pages < z->percpu_drift_mark)
1730 free_pages = zone_page_state_snapshot(z, NR_FREE_PAGES);
1731
1732 return __zone_watermark_ok(z, order, mark, classzone_idx, alloc_flags,
1733 free_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734}
1735
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001736#ifdef CONFIG_NUMA
1737/*
1738 * zlc_setup - Setup for "zonelist cache". Uses cached zone data to
1739 * skip over zones that are not allowed by the cpuset, or that have
1740 * been recently (in last second) found to be nearly full. See further
1741 * comments in mmzone.h. Reduces cache footprint of zonelist scans
Simon Arlott183ff222007-10-20 01:27:18 +02001742 * that have to skip over a lot of full or unallowed zones.
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001743 *
1744 * If the zonelist cache is present in the passed in zonelist, then
1745 * returns a pointer to the allowed node mask (either the current
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08001746 * tasks mems_allowed, or node_states[N_MEMORY].)
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001747 *
1748 * If the zonelist cache is not available for this zonelist, does
1749 * nothing and returns NULL.
1750 *
1751 * If the fullzones BITMAP in the zonelist cache is stale (more than
1752 * a second since last zap'd) then we zap it out (clear its bits.)
1753 *
1754 * We hold off even calling zlc_setup, until after we've checked the
1755 * first zone in the zonelist, on the theory that most allocations will
1756 * be satisfied from that first zone, so best to examine that zone as
1757 * quickly as we can.
1758 */
1759static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
1760{
1761 struct zonelist_cache *zlc; /* cached zonelist speedup info */
1762 nodemask_t *allowednodes; /* zonelist_cache approximation */
1763
1764 zlc = zonelist->zlcache_ptr;
1765 if (!zlc)
1766 return NULL;
1767
S.Caglar Onurf05111f2008-04-28 02:12:38 -07001768 if (time_after(jiffies, zlc->last_full_zap + HZ)) {
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001769 bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
1770 zlc->last_full_zap = jiffies;
1771 }
1772
1773 allowednodes = !in_interrupt() && (alloc_flags & ALLOC_CPUSET) ?
1774 &cpuset_current_mems_allowed :
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08001775 &node_states[N_MEMORY];
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001776 return allowednodes;
1777}
1778
1779/*
1780 * Given 'z' scanning a zonelist, run a couple of quick checks to see
1781 * if it is worth looking at further for free memory:
1782 * 1) Check that the zone isn't thought to be full (doesn't have its
1783 * bit set in the zonelist_cache fullzones BITMAP).
1784 * 2) Check that the zones node (obtained from the zonelist_cache
1785 * z_to_n[] mapping) is allowed in the passed in allowednodes mask.
1786 * Return true (non-zero) if zone is worth looking at further, or
1787 * else return false (zero) if it is not.
1788 *
1789 * This check -ignores- the distinction between various watermarks,
1790 * such as GFP_HIGH, GFP_ATOMIC, PF_MEMALLOC, ... If a zone is
1791 * found to be full for any variation of these watermarks, it will
1792 * be considered full for up to one second by all requests, unless
1793 * we are so low on memory on all allowed nodes that we are forced
1794 * into the second scan of the zonelist.
1795 *
1796 * In the second scan we ignore this zonelist cache and exactly
1797 * apply the watermarks to all zones, even it is slower to do so.
1798 * We are low on memory in the second scan, and should leave no stone
1799 * unturned looking for a free page.
1800 */
Mel Gormandd1a2392008-04-28 02:12:17 -07001801static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zoneref *z,
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001802 nodemask_t *allowednodes)
1803{
1804 struct zonelist_cache *zlc; /* cached zonelist speedup info */
1805 int i; /* index of *z in zonelist zones */
1806 int n; /* node that zone *z is on */
1807
1808 zlc = zonelist->zlcache_ptr;
1809 if (!zlc)
1810 return 1;
1811
Mel Gormandd1a2392008-04-28 02:12:17 -07001812 i = z - zonelist->_zonerefs;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001813 n = zlc->z_to_n[i];
1814
1815 /* This zone is worth trying if it is allowed but not full */
1816 return node_isset(n, *allowednodes) && !test_bit(i, zlc->fullzones);
1817}
1818
1819/*
1820 * Given 'z' scanning a zonelist, set the corresponding bit in
1821 * zlc->fullzones, so that subsequent attempts to allocate a page
1822 * from that zone don't waste time re-examining it.
1823 */
Mel Gormandd1a2392008-04-28 02:12:17 -07001824static void zlc_mark_zone_full(struct zonelist *zonelist, struct zoneref *z)
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001825{
1826 struct zonelist_cache *zlc; /* cached zonelist speedup info */
1827 int i; /* index of *z in zonelist zones */
1828
1829 zlc = zonelist->zlcache_ptr;
1830 if (!zlc)
1831 return;
1832
Mel Gormandd1a2392008-04-28 02:12:17 -07001833 i = z - zonelist->_zonerefs;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001834
1835 set_bit(i, zlc->fullzones);
1836}
1837
Mel Gorman76d3fbf2011-07-25 17:12:30 -07001838/*
1839 * clear all zones full, called after direct reclaim makes progress so that
1840 * a zone that was recently full is not skipped over for up to a second
1841 */
1842static void zlc_clear_zones_full(struct zonelist *zonelist)
1843{
1844 struct zonelist_cache *zlc; /* cached zonelist speedup info */
1845
1846 zlc = zonelist->zlcache_ptr;
1847 if (!zlc)
1848 return;
1849
1850 bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
1851}
1852
David Rientjes957f8222012-10-08 16:33:24 -07001853static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone)
1854{
1855 return node_isset(local_zone->node, zone->zone_pgdat->reclaim_nodes);
1856}
1857
1858static void __paginginit init_zone_allows_reclaim(int nid)
1859{
1860 int i;
1861
1862 for_each_online_node(i)
David Rientjes6b187d022012-10-25 13:38:08 -07001863 if (node_distance(nid, i) <= RECLAIM_DISTANCE)
David Rientjes957f8222012-10-08 16:33:24 -07001864 node_set(i, NODE_DATA(nid)->reclaim_nodes);
David Rientjes6b187d022012-10-25 13:38:08 -07001865 else
David Rientjes957f8222012-10-08 16:33:24 -07001866 zone_reclaim_mode = 1;
David Rientjes957f8222012-10-08 16:33:24 -07001867}
1868
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001869#else /* CONFIG_NUMA */
1870
1871static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
1872{
1873 return NULL;
1874}
1875
Mel Gormandd1a2392008-04-28 02:12:17 -07001876static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zoneref *z,
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001877 nodemask_t *allowednodes)
1878{
1879 return 1;
1880}
1881
Mel Gormandd1a2392008-04-28 02:12:17 -07001882static void zlc_mark_zone_full(struct zonelist *zonelist, struct zoneref *z)
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001883{
1884}
Mel Gorman76d3fbf2011-07-25 17:12:30 -07001885
1886static void zlc_clear_zones_full(struct zonelist *zonelist)
1887{
1888}
David Rientjes957f8222012-10-08 16:33:24 -07001889
1890static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone)
1891{
1892 return true;
1893}
1894
1895static inline void init_zone_allows_reclaim(int nid)
1896{
1897}
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001898#endif /* CONFIG_NUMA */
1899
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001900/*
Paul Jackson0798e512006-12-06 20:31:38 -08001901 * get_page_from_freelist goes through the zonelist trying to allocate
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001902 * a page.
1903 */
1904static struct page *
Mel Gorman19770b32008-04-28 02:12:18 -07001905get_page_from_freelist(gfp_t gfp_mask, nodemask_t *nodemask, unsigned int order,
Mel Gorman5117f452009-06-16 15:31:59 -07001906 struct zonelist *zonelist, int high_zoneidx, int alloc_flags,
Mel Gorman3dd28262009-06-16 15:32:00 -07001907 struct zone *preferred_zone, int migratetype)
Martin Hicks753ee722005-06-21 17:14:41 -07001908{
Mel Gormandd1a2392008-04-28 02:12:17 -07001909 struct zoneref *z;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001910 struct page *page = NULL;
Mel Gorman54a6eb52008-04-28 02:12:16 -07001911 int classzone_idx;
Mel Gorman5117f452009-06-16 15:31:59 -07001912 struct zone *zone;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001913 nodemask_t *allowednodes = NULL;/* zonelist_cache approximation */
1914 int zlc_active = 0; /* set if using zonelist_cache */
1915 int did_zlc_setup = 0; /* just call zlc_setup() one time */
Mel Gorman54a6eb52008-04-28 02:12:16 -07001916
Mel Gorman19770b32008-04-28 02:12:18 -07001917 classzone_idx = zone_idx(preferred_zone);
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001918zonelist_scan:
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001919 /*
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001920 * Scan zonelist, looking for a zone with enough free.
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001921 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
1922 */
Mel Gorman19770b32008-04-28 02:12:18 -07001923 for_each_zone_zonelist_nodemask(zone, z, zonelist,
1924 high_zoneidx, nodemask) {
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08001925 if (IS_ENABLED(CONFIG_NUMA) && zlc_active &&
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001926 !zlc_zone_worth_trying(zonelist, z, allowednodes))
1927 continue;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001928 if ((alloc_flags & ALLOC_CPUSET) &&
Paul Jackson02a0e532006-12-13 00:34:25 -08001929 !cpuset_zone_allowed_softwall(zone, gfp_mask))
Mel Gormancd38b112011-07-25 17:12:29 -07001930 continue;
Johannes Weinera756cf52012-01-10 15:07:49 -08001931 /*
1932 * When allocating a page cache page for writing, we
1933 * want to get it from a zone that is within its dirty
1934 * limit, such that no single zone holds more than its
1935 * proportional share of globally allowed dirty pages.
1936 * The dirty limits take into account the zone's
1937 * lowmem reserves and high watermark so that kswapd
1938 * should be able to balance it without having to
1939 * write pages from its LRU list.
1940 *
1941 * This may look like it could increase pressure on
1942 * lower zones by failing allocations in higher zones
1943 * before they are full. But the pages that do spill
1944 * over are limited as the lower zones are protected
1945 * by this very same mechanism. It should not become
1946 * a practical burden to them.
1947 *
1948 * XXX: For now, allow allocations to potentially
1949 * exceed the per-zone dirty limit in the slowpath
1950 * (ALLOC_WMARK_LOW unset) before going into reclaim,
1951 * which is important when on a NUMA setup the allowed
1952 * zones are together not big enough to reach the
1953 * global limit. The proper fix for these situations
1954 * will require awareness of zones in the
1955 * dirty-throttling and the flusher threads.
1956 */
1957 if ((alloc_flags & ALLOC_WMARK_LOW) &&
1958 (gfp_mask & __GFP_WRITE) && !zone_dirty_ok(zone))
1959 goto this_zone_full;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001960
Mel Gorman41858962009-06-16 15:32:12 -07001961 BUILD_BUG_ON(ALLOC_NO_WATERMARKS < NR_WMARK);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001962 if (!(alloc_flags & ALLOC_NO_WATERMARKS)) {
Nick Piggin31488902005-11-28 13:44:03 -08001963 unsigned long mark;
Mel Gormanfa5e0842009-06-16 15:33:22 -07001964 int ret;
1965
Mel Gorman41858962009-06-16 15:32:12 -07001966 mark = zone->watermark[alloc_flags & ALLOC_WMARK_MASK];
Mel Gormanfa5e0842009-06-16 15:33:22 -07001967 if (zone_watermark_ok(zone, order, mark,
1968 classzone_idx, alloc_flags))
1969 goto try_this_zone;
1970
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08001971 if (IS_ENABLED(CONFIG_NUMA) &&
1972 !did_zlc_setup && nr_online_nodes > 1) {
Mel Gormancd38b112011-07-25 17:12:29 -07001973 /*
1974 * we do zlc_setup if there are multiple nodes
1975 * and before considering the first zone allowed
1976 * by the cpuset.
1977 */
1978 allowednodes = zlc_setup(zonelist, alloc_flags);
1979 zlc_active = 1;
1980 did_zlc_setup = 1;
1981 }
1982
David Rientjes957f8222012-10-08 16:33:24 -07001983 if (zone_reclaim_mode == 0 ||
1984 !zone_allows_reclaim(preferred_zone, zone))
Mel Gormanfa5e0842009-06-16 15:33:22 -07001985 goto this_zone_full;
1986
Mel Gormancd38b112011-07-25 17:12:29 -07001987 /*
1988 * As we may have just activated ZLC, check if the first
1989 * eligible zone has failed zone_reclaim recently.
1990 */
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08001991 if (IS_ENABLED(CONFIG_NUMA) && zlc_active &&
Mel Gormancd38b112011-07-25 17:12:29 -07001992 !zlc_zone_worth_trying(zonelist, z, allowednodes))
1993 continue;
1994
Mel Gormanfa5e0842009-06-16 15:33:22 -07001995 ret = zone_reclaim(zone, gfp_mask, order);
1996 switch (ret) {
1997 case ZONE_RECLAIM_NOSCAN:
1998 /* did not scan */
Mel Gormancd38b112011-07-25 17:12:29 -07001999 continue;
Mel Gormanfa5e0842009-06-16 15:33:22 -07002000 case ZONE_RECLAIM_FULL:
2001 /* scanned but unreclaimable */
Mel Gormancd38b112011-07-25 17:12:29 -07002002 continue;
Mel Gormanfa5e0842009-06-16 15:33:22 -07002003 default:
2004 /* did we reclaim enough */
Mel Gormanfed27192013-04-29 15:07:57 -07002005 if (zone_watermark_ok(zone, order, mark,
Mel Gormanfa5e0842009-06-16 15:33:22 -07002006 classzone_idx, alloc_flags))
Mel Gormanfed27192013-04-29 15:07:57 -07002007 goto try_this_zone;
2008
2009 /*
2010 * Failed to reclaim enough to meet watermark.
2011 * Only mark the zone full if checking the min
2012 * watermark or if we failed to reclaim just
2013 * 1<<order pages or else the page allocator
2014 * fastpath will prematurely mark zones full
2015 * when the watermark is between the low and
2016 * min watermarks.
2017 */
2018 if (((alloc_flags & ALLOC_WMARK_MASK) == ALLOC_WMARK_MIN) ||
2019 ret == ZONE_RECLAIM_SOME)
Paul Jackson9276b1bc2006-12-06 20:31:48 -08002020 goto this_zone_full;
Mel Gormanfed27192013-04-29 15:07:57 -07002021
2022 continue;
Paul Jackson0798e512006-12-06 20:31:38 -08002023 }
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002024 }
2025
Mel Gormanfa5e0842009-06-16 15:33:22 -07002026try_this_zone:
Mel Gorman3dd28262009-06-16 15:32:00 -07002027 page = buffered_rmqueue(preferred_zone, zone, order,
2028 gfp_mask, migratetype);
Paul Jackson0798e512006-12-06 20:31:38 -08002029 if (page)
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002030 break;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08002031this_zone_full:
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08002032 if (IS_ENABLED(CONFIG_NUMA))
Paul Jackson9276b1bc2006-12-06 20:31:48 -08002033 zlc_mark_zone_full(zonelist, z);
Mel Gorman54a6eb52008-04-28 02:12:16 -07002034 }
Paul Jackson9276b1bc2006-12-06 20:31:48 -08002035
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08002036 if (unlikely(IS_ENABLED(CONFIG_NUMA) && page == NULL && zlc_active)) {
Paul Jackson9276b1bc2006-12-06 20:31:48 -08002037 /* Disable zlc cache for second zonelist scan */
2038 zlc_active = 0;
2039 goto zonelist_scan;
2040 }
Alex Shib1211862012-08-21 16:16:08 -07002041
2042 if (page)
2043 /*
2044 * page->pfmemalloc is set when ALLOC_NO_WATERMARKS was
2045 * necessary to allocate the page. The expectation is
2046 * that the caller is taking steps that will free more
2047 * memory. The caller should avoid the page being used
2048 * for !PFMEMALLOC purposes.
2049 */
2050 page->pfmemalloc = !!(alloc_flags & ALLOC_NO_WATERMARKS);
2051
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002052 return page;
Martin Hicks753ee722005-06-21 17:14:41 -07002053}
2054
David Rientjes29423e772011-03-22 16:30:47 -07002055/*
2056 * Large machines with many possible nodes should not always dump per-node
2057 * meminfo in irq context.
2058 */
2059static inline bool should_suppress_show_mem(void)
2060{
2061 bool ret = false;
2062
2063#if NODES_SHIFT > 8
2064 ret = in_interrupt();
2065#endif
2066 return ret;
2067}
2068
Dave Hansena238ab52011-05-24 17:12:16 -07002069static DEFINE_RATELIMIT_STATE(nopage_rs,
2070 DEFAULT_RATELIMIT_INTERVAL,
2071 DEFAULT_RATELIMIT_BURST);
2072
2073void warn_alloc_failed(gfp_t gfp_mask, int order, const char *fmt, ...)
2074{
Dave Hansena238ab52011-05-24 17:12:16 -07002075 unsigned int filter = SHOW_MEM_FILTER_NODES;
2076
Stanislaw Gruszkac0a32fc2012-01-10 15:07:28 -08002077 if ((gfp_mask & __GFP_NOWARN) || !__ratelimit(&nopage_rs) ||
2078 debug_guardpage_minorder() > 0)
Dave Hansena238ab52011-05-24 17:12:16 -07002079 return;
2080
2081 /*
David Rientjes4b59e6c2013-04-29 15:06:11 -07002082 * Walking all memory to count page types is very expensive and should
2083 * be inhibited in non-blockable contexts.
2084 */
2085 if (!(gfp_mask & __GFP_WAIT))
2086 filter |= SHOW_MEM_FILTER_PAGE_COUNT;
2087
2088 /*
Dave Hansena238ab52011-05-24 17:12:16 -07002089 * This documents exceptions given to allocations in certain
2090 * contexts that are allowed to allocate outside current's set
2091 * of allowed nodes.
2092 */
2093 if (!(gfp_mask & __GFP_NOMEMALLOC))
2094 if (test_thread_flag(TIF_MEMDIE) ||
2095 (current->flags & (PF_MEMALLOC | PF_EXITING)))
2096 filter &= ~SHOW_MEM_FILTER_NODES;
2097 if (in_interrupt() || !(gfp_mask & __GFP_WAIT))
2098 filter &= ~SHOW_MEM_FILTER_NODES;
2099
2100 if (fmt) {
Joe Perches3ee9a4f2011-10-31 17:08:35 -07002101 struct va_format vaf;
2102 va_list args;
2103
Dave Hansena238ab52011-05-24 17:12:16 -07002104 va_start(args, fmt);
Joe Perches3ee9a4f2011-10-31 17:08:35 -07002105
2106 vaf.fmt = fmt;
2107 vaf.va = &args;
2108
2109 pr_warn("%pV", &vaf);
2110
Dave Hansena238ab52011-05-24 17:12:16 -07002111 va_end(args);
2112 }
2113
Joe Perches3ee9a4f2011-10-31 17:08:35 -07002114 pr_warn("%s: page allocation failure: order:%d, mode:0x%x\n",
2115 current->comm, order, gfp_mask);
Dave Hansena238ab52011-05-24 17:12:16 -07002116
2117 dump_stack();
2118 if (!should_suppress_show_mem())
2119 show_mem(filter);
2120}
2121
Mel Gorman11e33f62009-06-16 15:31:57 -07002122static inline int
2123should_alloc_retry(gfp_t gfp_mask, unsigned int order,
Mel Gormanf90ac392012-01-10 15:07:15 -08002124 unsigned long did_some_progress,
Mel Gorman11e33f62009-06-16 15:31:57 -07002125 unsigned long pages_reclaimed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
Mel Gorman11e33f62009-06-16 15:31:57 -07002127 /* Do not loop if specifically requested */
2128 if (gfp_mask & __GFP_NORETRY)
2129 return 0;
2130
Mel Gormanf90ac392012-01-10 15:07:15 -08002131 /* Always retry if specifically requested */
2132 if (gfp_mask & __GFP_NOFAIL)
2133 return 1;
2134
2135 /*
2136 * Suspend converts GFP_KERNEL to __GFP_WAIT which can prevent reclaim
2137 * making forward progress without invoking OOM. Suspend also disables
2138 * storage devices so kswapd will not help. Bail if we are suspending.
2139 */
2140 if (!did_some_progress && pm_suspended_storage())
2141 return 0;
2142
Mel Gorman11e33f62009-06-16 15:31:57 -07002143 /*
2144 * In this implementation, order <= PAGE_ALLOC_COSTLY_ORDER
2145 * means __GFP_NOFAIL, but that may not be true in other
2146 * implementations.
2147 */
2148 if (order <= PAGE_ALLOC_COSTLY_ORDER)
2149 return 1;
2150
2151 /*
2152 * For order > PAGE_ALLOC_COSTLY_ORDER, if __GFP_REPEAT is
2153 * specified, then we retry until we no longer reclaim any pages
2154 * (above), or we've reclaimed an order of pages at least as
2155 * large as the allocation's order. In both cases, if the
2156 * allocation still fails, we stop retrying.
2157 */
2158 if (gfp_mask & __GFP_REPEAT && pages_reclaimed < (1 << order))
2159 return 1;
2160
Mel Gorman11e33f62009-06-16 15:31:57 -07002161 return 0;
2162}
2163
2164static inline struct page *
2165__alloc_pages_may_oom(gfp_t gfp_mask, unsigned int order,
2166 struct zonelist *zonelist, enum zone_type high_zoneidx,
Mel Gorman3dd28262009-06-16 15:32:00 -07002167 nodemask_t *nodemask, struct zone *preferred_zone,
2168 int migratetype)
Mel Gorman11e33f62009-06-16 15:31:57 -07002169{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Mel Gorman11e33f62009-06-16 15:31:57 -07002172 /* Acquire the OOM killer lock for the zones in zonelist */
Minchan Kimff321fe2010-08-09 17:18:57 -07002173 if (!try_set_zonelist_oom(zonelist, gfp_mask)) {
Mel Gorman11e33f62009-06-16 15:31:57 -07002174 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 return NULL;
2176 }
Jens Axboe6b1de912005-11-17 21:35:02 +01002177
Mel Gorman11e33f62009-06-16 15:31:57 -07002178 /*
2179 * Go through the zonelist yet one more time, keep very high watermark
2180 * here, this is only to catch a parallel oom killing, we must fail if
2181 * we're still under heavy pressure.
2182 */
2183 page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, nodemask,
2184 order, zonelist, high_zoneidx,
Mel Gorman5117f452009-06-16 15:31:59 -07002185 ALLOC_WMARK_HIGH|ALLOC_CPUSET,
Mel Gorman3dd28262009-06-16 15:32:00 -07002186 preferred_zone, migratetype);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002187 if (page)
Mel Gorman11e33f62009-06-16 15:31:57 -07002188 goto out;
2189
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -08002190 if (!(gfp_mask & __GFP_NOFAIL)) {
2191 /* The OOM killer will not help higher order allocs */
2192 if (order > PAGE_ALLOC_COSTLY_ORDER)
2193 goto out;
David Rientjes03668b32010-08-09 17:18:54 -07002194 /* The OOM killer does not needlessly kill tasks for lowmem */
2195 if (high_zoneidx < ZONE_NORMAL)
2196 goto out;
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -08002197 /*
2198 * GFP_THISNODE contains __GFP_NORETRY and we never hit this.
2199 * Sanity check for bare calls of __GFP_THISNODE, not real OOM.
2200 * The caller should handle page allocation failure by itself if
2201 * it specifies __GFP_THISNODE.
2202 * Note: Hugepage uses it but will hit PAGE_ALLOC_COSTLY_ORDER.
2203 */
2204 if (gfp_mask & __GFP_THISNODE)
2205 goto out;
2206 }
Mel Gorman11e33f62009-06-16 15:31:57 -07002207 /* Exhausted what can be done so it's blamo time */
David Rientjes08ab9b12012-03-21 16:34:04 -07002208 out_of_memory(zonelist, gfp_mask, order, nodemask, false);
Mel Gorman11e33f62009-06-16 15:31:57 -07002209
2210out:
2211 clear_zonelist_oom(zonelist, gfp_mask);
2212 return page;
2213}
2214
Mel Gorman56de7262010-05-24 14:32:30 -07002215#ifdef CONFIG_COMPACTION
2216/* Try memory compaction for high-order allocations before reclaim */
2217static struct page *
2218__alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
2219 struct zonelist *zonelist, enum zone_type high_zoneidx,
2220 nodemask_t *nodemask, int alloc_flags, struct zone *preferred_zone,
Mel Gorman66199712012-01-12 17:19:41 -08002221 int migratetype, bool sync_migration,
Mel Gormanc67fe372012-08-21 16:16:17 -07002222 bool *contended_compaction, bool *deferred_compaction,
Mel Gorman66199712012-01-12 17:19:41 -08002223 unsigned long *did_some_progress)
Mel Gorman56de7262010-05-24 14:32:30 -07002224{
Mel Gorman66199712012-01-12 17:19:41 -08002225 if (!order)
Mel Gorman56de7262010-05-24 14:32:30 -07002226 return NULL;
2227
Rik van Rielaff62242012-03-21 16:33:52 -07002228 if (compaction_deferred(preferred_zone, order)) {
Mel Gorman66199712012-01-12 17:19:41 -08002229 *deferred_compaction = true;
2230 return NULL;
2231 }
2232
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08002233 current->flags |= PF_MEMALLOC;
Mel Gorman56de7262010-05-24 14:32:30 -07002234 *did_some_progress = try_to_compact_pages(zonelist, order, gfp_mask,
Mel Gormanc67fe372012-08-21 16:16:17 -07002235 nodemask, sync_migration,
Mel Gorman8fb74b92013-01-11 14:32:16 -08002236 contended_compaction);
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08002237 current->flags &= ~PF_MEMALLOC;
Mel Gorman56de7262010-05-24 14:32:30 -07002238
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07002239 if (*did_some_progress != COMPACT_SKIPPED) {
Mel Gorman8fb74b92013-01-11 14:32:16 -08002240 struct page *page;
2241
Mel Gorman56de7262010-05-24 14:32:30 -07002242 /* Page migration frees to the PCP lists but we want merging */
Thomas Gleixner762f1a52011-07-21 16:47:49 +02002243 drain_pages(get_cpu_light());
2244 put_cpu_light();
Mel Gorman56de7262010-05-24 14:32:30 -07002245
2246 page = get_page_from_freelist(gfp_mask, nodemask,
2247 order, zonelist, high_zoneidx,
Mel Gormancfd19c52012-07-31 16:44:10 -07002248 alloc_flags & ~ALLOC_NO_WATERMARKS,
2249 preferred_zone, migratetype);
Mel Gorman56de7262010-05-24 14:32:30 -07002250 if (page) {
Mel Gorman62997022012-10-08 16:32:47 -07002251 preferred_zone->compact_blockskip_flush = false;
Mel Gorman4f92e252010-05-24 14:32:32 -07002252 preferred_zone->compact_considered = 0;
2253 preferred_zone->compact_defer_shift = 0;
Rik van Rielaff62242012-03-21 16:33:52 -07002254 if (order >= preferred_zone->compact_order_failed)
2255 preferred_zone->compact_order_failed = order + 1;
Mel Gorman56de7262010-05-24 14:32:30 -07002256 count_vm_event(COMPACTSUCCESS);
2257 return page;
2258 }
2259
2260 /*
2261 * It's bad if compaction run occurs and fails.
2262 * The most likely reason is that pages exist,
2263 * but not enough to satisfy watermarks.
2264 */
2265 count_vm_event(COMPACTFAIL);
Mel Gorman66199712012-01-12 17:19:41 -08002266
2267 /*
2268 * As async compaction considers a subset of pageblocks, only
2269 * defer if the failure was a sync compaction failure.
2270 */
2271 if (sync_migration)
Rik van Rielaff62242012-03-21 16:33:52 -07002272 defer_compaction(preferred_zone, order);
Mel Gorman56de7262010-05-24 14:32:30 -07002273
2274 cond_resched();
2275 }
2276
2277 return NULL;
2278}
2279#else
2280static inline struct page *
2281__alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
2282 struct zonelist *zonelist, enum zone_type high_zoneidx,
2283 nodemask_t *nodemask, int alloc_flags, struct zone *preferred_zone,
Mel Gorman66199712012-01-12 17:19:41 -08002284 int migratetype, bool sync_migration,
Mel Gormanc67fe372012-08-21 16:16:17 -07002285 bool *contended_compaction, bool *deferred_compaction,
Mel Gorman66199712012-01-12 17:19:41 -08002286 unsigned long *did_some_progress)
Mel Gorman56de7262010-05-24 14:32:30 -07002287{
2288 return NULL;
2289}
2290#endif /* CONFIG_COMPACTION */
2291
Marek Szyprowskibba90712012-01-25 12:09:52 +01002292/* Perform direct synchronous page reclaim */
2293static int
2294__perform_reclaim(gfp_t gfp_mask, unsigned int order, struct zonelist *zonelist,
2295 nodemask_t *nodemask)
Mel Gorman11e33f62009-06-16 15:31:57 -07002296{
Mel Gorman11e33f62009-06-16 15:31:57 -07002297 struct reclaim_state reclaim_state;
Marek Szyprowskibba90712012-01-25 12:09:52 +01002298 int progress;
Mel Gorman11e33f62009-06-16 15:31:57 -07002299
2300 cond_resched();
2301
2302 /* We now go into synchronous reclaim */
2303 cpuset_memory_pressure_bump();
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08002304 current->flags |= PF_MEMALLOC;
Mel Gorman11e33f62009-06-16 15:31:57 -07002305 lockdep_set_current_reclaim_state(gfp_mask);
2306 reclaim_state.reclaimed_slab = 0;
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08002307 current->reclaim_state = &reclaim_state;
Mel Gorman11e33f62009-06-16 15:31:57 -07002308
Marek Szyprowskibba90712012-01-25 12:09:52 +01002309 progress = try_to_free_pages(zonelist, order, gfp_mask, nodemask);
Mel Gorman11e33f62009-06-16 15:31:57 -07002310
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08002311 current->reclaim_state = NULL;
Mel Gorman11e33f62009-06-16 15:31:57 -07002312 lockdep_clear_current_reclaim_state();
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08002313 current->flags &= ~PF_MEMALLOC;
Mel Gorman11e33f62009-06-16 15:31:57 -07002314
2315 cond_resched();
2316
Marek Szyprowskibba90712012-01-25 12:09:52 +01002317 return progress;
2318}
2319
2320/* The really slow allocator path where we enter direct reclaim */
2321static inline struct page *
2322__alloc_pages_direct_reclaim(gfp_t gfp_mask, unsigned int order,
2323 struct zonelist *zonelist, enum zone_type high_zoneidx,
2324 nodemask_t *nodemask, int alloc_flags, struct zone *preferred_zone,
2325 int migratetype, unsigned long *did_some_progress)
2326{
2327 struct page *page = NULL;
2328 bool drained = false;
2329
2330 *did_some_progress = __perform_reclaim(gfp_mask, order, zonelist,
2331 nodemask);
Mel Gorman9ee493c2010-09-09 16:38:18 -07002332 if (unlikely(!(*did_some_progress)))
2333 return NULL;
Mel Gorman11e33f62009-06-16 15:31:57 -07002334
Mel Gorman76d3fbf2011-07-25 17:12:30 -07002335 /* After successful reclaim, reconsider all zones for allocation */
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08002336 if (IS_ENABLED(CONFIG_NUMA))
Mel Gorman76d3fbf2011-07-25 17:12:30 -07002337 zlc_clear_zones_full(zonelist);
2338
Mel Gorman9ee493c2010-09-09 16:38:18 -07002339retry:
2340 page = get_page_from_freelist(gfp_mask, nodemask, order,
Mel Gorman5117f452009-06-16 15:31:59 -07002341 zonelist, high_zoneidx,
Mel Gormancfd19c52012-07-31 16:44:10 -07002342 alloc_flags & ~ALLOC_NO_WATERMARKS,
2343 preferred_zone, migratetype);
Mel Gorman9ee493c2010-09-09 16:38:18 -07002344
2345 /*
2346 * If an allocation failed after direct reclaim, it could be because
2347 * pages are pinned on the per-cpu lists. Drain them and try again
2348 */
2349 if (!page && !drained) {
2350 drain_all_pages();
2351 drained = true;
2352 goto retry;
2353 }
2354
Mel Gorman11e33f62009-06-16 15:31:57 -07002355 return page;
2356}
2357
Mel Gorman11e33f62009-06-16 15:31:57 -07002358/*
2359 * This is called in the allocator slow-path if the allocation request is of
2360 * sufficient urgency to ignore watermarks and take other desperate measures
2361 */
2362static inline struct page *
2363__alloc_pages_high_priority(gfp_t gfp_mask, unsigned int order,
2364 struct zonelist *zonelist, enum zone_type high_zoneidx,
Mel Gorman3dd28262009-06-16 15:32:00 -07002365 nodemask_t *nodemask, struct zone *preferred_zone,
2366 int migratetype)
Mel Gorman11e33f62009-06-16 15:31:57 -07002367{
2368 struct page *page;
2369
2370 do {
2371 page = get_page_from_freelist(gfp_mask, nodemask, order,
Mel Gorman5117f452009-06-16 15:31:59 -07002372 zonelist, high_zoneidx, ALLOC_NO_WATERMARKS,
Mel Gorman3dd28262009-06-16 15:32:00 -07002373 preferred_zone, migratetype);
Mel Gorman11e33f62009-06-16 15:31:57 -07002374
2375 if (!page && gfp_mask & __GFP_NOFAIL)
Mel Gorman0e093d992010-10-26 14:21:45 -07002376 wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/50);
Mel Gorman11e33f62009-06-16 15:31:57 -07002377 } while (!page && (gfp_mask & __GFP_NOFAIL));
2378
2379 return page;
2380}
2381
2382static inline
2383void wake_all_kswapd(unsigned int order, struct zonelist *zonelist,
Mel Gorman99504742011-01-13 15:46:20 -08002384 enum zone_type high_zoneidx,
2385 enum zone_type classzone_idx)
Mel Gorman11e33f62009-06-16 15:31:57 -07002386{
2387 struct zoneref *z;
2388 struct zone *zone;
2389
2390 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx)
Mel Gorman99504742011-01-13 15:46:20 -08002391 wakeup_kswapd(zone, order, classzone_idx);
Mel Gorman11e33f62009-06-16 15:31:57 -07002392}
2393
Peter Zijlstra341ce062009-06-16 15:32:02 -07002394static inline int
2395gfp_to_alloc_flags(gfp_t gfp_mask)
2396{
Peter Zijlstra341ce062009-06-16 15:32:02 -07002397 int alloc_flags = ALLOC_WMARK_MIN | ALLOC_CPUSET;
2398 const gfp_t wait = gfp_mask & __GFP_WAIT;
2399
Mel Gormana56f57f2009-06-16 15:32:02 -07002400 /* __GFP_HIGH is assumed to be the same as ALLOC_HIGH to save a branch. */
Namhyung Kime6223a32010-10-26 14:21:59 -07002401 BUILD_BUG_ON(__GFP_HIGH != (__force gfp_t) ALLOC_HIGH);
Mel Gormana56f57f2009-06-16 15:32:02 -07002402
Peter Zijlstra341ce062009-06-16 15:32:02 -07002403 /*
2404 * The caller may dip into page reserves a bit more if the caller
2405 * cannot run direct reclaim, or if the caller has realtime scheduling
2406 * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will
2407 * set both ALLOC_HARDER (!wait) and ALLOC_HIGH (__GFP_HIGH).
2408 */
Namhyung Kime6223a32010-10-26 14:21:59 -07002409 alloc_flags |= (__force int) (gfp_mask & __GFP_HIGH);
Peter Zijlstra341ce062009-06-16 15:32:02 -07002410
2411 if (!wait) {
Andrea Arcangeli5c3240d2011-01-13 15:46:49 -08002412 /*
2413 * Not worth trying to allocate harder for
2414 * __GFP_NOMEMALLOC even if it can't schedule.
2415 */
2416 if (!(gfp_mask & __GFP_NOMEMALLOC))
2417 alloc_flags |= ALLOC_HARDER;
Peter Zijlstra341ce062009-06-16 15:32:02 -07002418 /*
2419 * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
2420 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
2421 */
2422 alloc_flags &= ~ALLOC_CPUSET;
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08002423 } else if (unlikely(rt_task(current)) && !in_interrupt())
Peter Zijlstra341ce062009-06-16 15:32:02 -07002424 alloc_flags |= ALLOC_HARDER;
2425
Mel Gormanb37f1dd2012-07-31 16:44:03 -07002426 if (likely(!(gfp_mask & __GFP_NOMEMALLOC))) {
2427 if (gfp_mask & __GFP_MEMALLOC)
2428 alloc_flags |= ALLOC_NO_WATERMARKS;
Mel Gorman907aed42012-07-31 16:44:07 -07002429 else if (in_serving_softirq() && (current->flags & PF_MEMALLOC))
2430 alloc_flags |= ALLOC_NO_WATERMARKS;
2431 else if (!in_interrupt() &&
2432 ((current->flags & PF_MEMALLOC) ||
2433 unlikely(test_thread_flag(TIF_MEMDIE))))
Peter Zijlstra341ce062009-06-16 15:32:02 -07002434 alloc_flags |= ALLOC_NO_WATERMARKS;
2435 }
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07002436#ifdef CONFIG_CMA
2437 if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
2438 alloc_flags |= ALLOC_CMA;
2439#endif
Peter Zijlstra341ce062009-06-16 15:32:02 -07002440 return alloc_flags;
2441}
2442
Mel Gorman072bb0a2012-07-31 16:43:58 -07002443bool gfp_pfmemalloc_allowed(gfp_t gfp_mask)
2444{
Mel Gormanb37f1dd2012-07-31 16:44:03 -07002445 return !!(gfp_to_alloc_flags(gfp_mask) & ALLOC_NO_WATERMARKS);
Mel Gorman072bb0a2012-07-31 16:43:58 -07002446}
2447
Mel Gorman11e33f62009-06-16 15:31:57 -07002448static inline struct page *
2449__alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
2450 struct zonelist *zonelist, enum zone_type high_zoneidx,
Mel Gorman3dd28262009-06-16 15:32:00 -07002451 nodemask_t *nodemask, struct zone *preferred_zone,
2452 int migratetype)
Mel Gorman11e33f62009-06-16 15:31:57 -07002453{
2454 const gfp_t wait = gfp_mask & __GFP_WAIT;
2455 struct page *page = NULL;
2456 int alloc_flags;
2457 unsigned long pages_reclaimed = 0;
2458 unsigned long did_some_progress;
Mel Gorman77f1fe62011-01-13 15:45:57 -08002459 bool sync_migration = false;
Mel Gorman66199712012-01-12 17:19:41 -08002460 bool deferred_compaction = false;
Mel Gormanc67fe372012-08-21 16:16:17 -07002461 bool contended_compaction = false;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002462
Christoph Lameter952f3b52006-12-06 20:33:26 -08002463 /*
Mel Gorman72807a72009-06-16 15:32:18 -07002464 * In the slowpath, we sanity check order to avoid ever trying to
2465 * reclaim >= MAX_ORDER areas which will never succeed. Callers may
2466 * be using allocators in order of preference for an area that is
2467 * too large.
2468 */
Mel Gorman1fc28b72009-07-29 15:04:08 -07002469 if (order >= MAX_ORDER) {
2470 WARN_ON_ONCE(!(gfp_mask & __GFP_NOWARN));
Mel Gorman72807a72009-06-16 15:32:18 -07002471 return NULL;
Mel Gorman1fc28b72009-07-29 15:04:08 -07002472 }
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002473
Christoph Lameter952f3b52006-12-06 20:33:26 -08002474 /*
2475 * GFP_THISNODE (meaning __GFP_THISNODE, __GFP_NORETRY and
2476 * __GFP_NOWARN set) should not cause reclaim since the subsystem
2477 * (f.e. slab) using GFP_THISNODE may choose to trigger reclaim
2478 * using a larger set of nodes after it has established that the
2479 * allowed per node queues are empty and that nodes are
2480 * over allocated.
2481 */
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08002482 if (IS_ENABLED(CONFIG_NUMA) &&
2483 (gfp_mask & GFP_THISNODE) == GFP_THISNODE)
Christoph Lameter952f3b52006-12-06 20:33:26 -08002484 goto nopage;
2485
Mel Gormancc4a6852009-11-11 14:26:14 -08002486restart:
Linus Torvaldscaf49192012-12-10 10:51:16 -08002487 if (!(gfp_mask & __GFP_NO_KSWAPD))
2488 wake_all_kswapd(order, zonelist, high_zoneidx,
2489 zone_idx(preferred_zone));
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002490
Paul Jackson9bf22292005-09-06 15:18:12 -07002491 /*
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002492 * OK, we're below the kswapd watermark and have kicked background
2493 * reclaim. Now things get more complex, so set up alloc_flags according
2494 * to how we want to proceed.
Paul Jackson9bf22292005-09-06 15:18:12 -07002495 */
Peter Zijlstra341ce062009-06-16 15:32:02 -07002496 alloc_flags = gfp_to_alloc_flags(gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
David Rientjesf33261d2011-01-25 15:07:20 -08002498 /*
2499 * Find the true preferred zone if the allocation is unconstrained by
2500 * cpusets.
2501 */
2502 if (!(alloc_flags & ALLOC_CPUSET) && !nodemask)
2503 first_zones_zonelist(zonelist, high_zoneidx, NULL,
2504 &preferred_zone);
2505
Andrew Barrycfa54a02011-05-24 17:12:52 -07002506rebalance:
Peter Zijlstra341ce062009-06-16 15:32:02 -07002507 /* This is the last chance, in general, before the goto nopage. */
Mel Gorman19770b32008-04-28 02:12:18 -07002508 page = get_page_from_freelist(gfp_mask, nodemask, order, zonelist,
Peter Zijlstra341ce062009-06-16 15:32:02 -07002509 high_zoneidx, alloc_flags & ~ALLOC_NO_WATERMARKS,
2510 preferred_zone, migratetype);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002511 if (page)
2512 goto got_pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
Mel Gorman11e33f62009-06-16 15:31:57 -07002514 /* Allocate without watermarks if the context allows */
Peter Zijlstra341ce062009-06-16 15:32:02 -07002515 if (alloc_flags & ALLOC_NO_WATERMARKS) {
Mel Gorman183f6372012-07-31 16:44:12 -07002516 /*
2517 * Ignore mempolicies if ALLOC_NO_WATERMARKS on the grounds
2518 * the allocation is high priority and these type of
2519 * allocations are system rather than user orientated
2520 */
2521 zonelist = node_zonelist(numa_node_id(), gfp_mask);
2522
Peter Zijlstra341ce062009-06-16 15:32:02 -07002523 page = __alloc_pages_high_priority(gfp_mask, order,
2524 zonelist, high_zoneidx, nodemask,
2525 preferred_zone, migratetype);
Mel Gormancfd19c52012-07-31 16:44:10 -07002526 if (page) {
Peter Zijlstra341ce062009-06-16 15:32:02 -07002527 goto got_pg;
Mel Gormancfd19c52012-07-31 16:44:10 -07002528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 }
2530
2531 /* Atomic allocations - we can't balance anything */
2532 if (!wait)
2533 goto nopage;
2534
Peter Zijlstra341ce062009-06-16 15:32:02 -07002535 /* Avoid recursion of direct reclaim */
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08002536 if (current->flags & PF_MEMALLOC)
Peter Zijlstra341ce062009-06-16 15:32:02 -07002537 goto nopage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
David Rientjes6583bb62009-07-29 15:02:06 -07002539 /* Avoid allocations with no watermarks from looping endlessly */
2540 if (test_thread_flag(TIF_MEMDIE) && !(gfp_mask & __GFP_NOFAIL))
2541 goto nopage;
2542
Mel Gorman77f1fe62011-01-13 15:45:57 -08002543 /*
2544 * Try direct compaction. The first pass is asynchronous. Subsequent
2545 * attempts after direct reclaim are synchronous
2546 */
Mel Gorman56de7262010-05-24 14:32:30 -07002547 page = __alloc_pages_direct_compact(gfp_mask, order,
2548 zonelist, high_zoneidx,
2549 nodemask,
2550 alloc_flags, preferred_zone,
Mel Gorman66199712012-01-12 17:19:41 -08002551 migratetype, sync_migration,
Mel Gormanc67fe372012-08-21 16:16:17 -07002552 &contended_compaction,
Mel Gorman66199712012-01-12 17:19:41 -08002553 &deferred_compaction,
2554 &did_some_progress);
Mel Gorman56de7262010-05-24 14:32:30 -07002555 if (page)
2556 goto got_pg;
Andrea Arcangelic6a140b2011-05-24 17:11:38 -07002557 sync_migration = true;
Mel Gorman56de7262010-05-24 14:32:30 -07002558
Linus Torvalds31f8d422012-12-10 10:47:45 -08002559 /*
2560 * If compaction is deferred for high-order allocations, it is because
2561 * sync compaction recently failed. In this is the case and the caller
2562 * requested a movable allocation that does not heavily disrupt the
2563 * system then fail the allocation instead of entering direct reclaim.
2564 */
2565 if ((deferred_compaction || contended_compaction) &&
Linus Torvaldscaf49192012-12-10 10:51:16 -08002566 (gfp_mask & __GFP_NO_KSWAPD))
Linus Torvalds31f8d422012-12-10 10:47:45 -08002567 goto nopage;
Mel Gorman66199712012-01-12 17:19:41 -08002568
Mel Gorman11e33f62009-06-16 15:31:57 -07002569 /* Try direct reclaim and then allocating */
2570 page = __alloc_pages_direct_reclaim(gfp_mask, order,
2571 zonelist, high_zoneidx,
2572 nodemask,
Mel Gorman5117f452009-06-16 15:31:59 -07002573 alloc_flags, preferred_zone,
Mel Gorman3dd28262009-06-16 15:32:00 -07002574 migratetype, &did_some_progress);
Mel Gorman11e33f62009-06-16 15:31:57 -07002575 if (page)
2576 goto got_pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 /*
Mel Gorman11e33f62009-06-16 15:31:57 -07002579 * If we failed to make any progress reclaiming, then we are
2580 * running out of options and have to consider going OOM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 */
Mel Gorman11e33f62009-06-16 15:31:57 -07002582 if (!did_some_progress) {
2583 if ((gfp_mask & __GFP_FS) && !(gfp_mask & __GFP_NORETRY)) {
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -07002584 if (oom_killer_disabled)
2585 goto nopage;
David Rientjes29fd66d2012-03-28 14:42:41 -07002586 /* Coredumps can quickly deplete all memory reserves */
2587 if ((current->flags & PF_DUMPCORE) &&
2588 !(gfp_mask & __GFP_NOFAIL))
2589 goto nopage;
Mel Gorman11e33f62009-06-16 15:31:57 -07002590 page = __alloc_pages_may_oom(gfp_mask, order,
2591 zonelist, high_zoneidx,
Mel Gorman3dd28262009-06-16 15:32:00 -07002592 nodemask, preferred_zone,
2593 migratetype);
Mel Gorman11e33f62009-06-16 15:31:57 -07002594 if (page)
2595 goto got_pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
David Rientjes03668b32010-08-09 17:18:54 -07002597 if (!(gfp_mask & __GFP_NOFAIL)) {
2598 /*
2599 * The oom killer is not called for high-order
2600 * allocations that may fail, so if no progress
2601 * is being made, there are no other options and
2602 * retrying is unlikely to help.
2603 */
2604 if (order > PAGE_ALLOC_COSTLY_ORDER)
2605 goto nopage;
2606 /*
2607 * The oom killer is not called for lowmem
2608 * allocations to prevent needlessly killing
2609 * innocent tasks.
2610 */
2611 if (high_zoneidx < ZONE_NORMAL)
2612 goto nopage;
2613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 goto restart;
2616 }
2617 }
2618
Mel Gorman11e33f62009-06-16 15:31:57 -07002619 /* Check if we should retry the allocation */
Nishanth Aravamudana41f24e2008-04-29 00:58:25 -07002620 pages_reclaimed += did_some_progress;
Mel Gormanf90ac392012-01-10 15:07:15 -08002621 if (should_alloc_retry(gfp_mask, order, did_some_progress,
2622 pages_reclaimed)) {
Mel Gorman11e33f62009-06-16 15:31:57 -07002623 /* Wait for some write requests to complete then retry */
Mel Gorman0e093d992010-10-26 14:21:45 -07002624 wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 goto rebalance;
Mel Gorman3e7d3442011-01-13 15:45:56 -08002626 } else {
2627 /*
2628 * High-order allocations do not necessarily loop after
2629 * direct reclaim and reclaim/compaction depends on compaction
2630 * being called after reclaim so call directly if necessary
2631 */
2632 page = __alloc_pages_direct_compact(gfp_mask, order,
2633 zonelist, high_zoneidx,
2634 nodemask,
2635 alloc_flags, preferred_zone,
Mel Gorman66199712012-01-12 17:19:41 -08002636 migratetype, sync_migration,
Mel Gormanc67fe372012-08-21 16:16:17 -07002637 &contended_compaction,
Mel Gorman66199712012-01-12 17:19:41 -08002638 &deferred_compaction,
2639 &did_some_progress);
Mel Gorman3e7d3442011-01-13 15:45:56 -08002640 if (page)
2641 goto got_pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 }
2643
2644nopage:
Dave Hansena238ab52011-05-24 17:12:16 -07002645 warn_alloc_failed(gfp_mask, order, NULL);
Vegard Nossumb1eeab62008-11-25 16:55:53 +01002646 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647got_pg:
Vegard Nossumb1eeab62008-11-25 16:55:53 +01002648 if (kmemcheck_enabled)
2649 kmemcheck_pagealloc_alloc(page, order, gfp_mask);
Mel Gorman11e33f62009-06-16 15:31:57 -07002650
Mel Gorman072bb0a2012-07-31 16:43:58 -07002651 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652}
Mel Gorman11e33f62009-06-16 15:31:57 -07002653
2654/*
2655 * This is the 'heart' of the zoned buddy allocator.
2656 */
2657struct page *
2658__alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order,
2659 struct zonelist *zonelist, nodemask_t *nodemask)
2660{
2661 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
Mel Gorman5117f452009-06-16 15:31:59 -07002662 struct zone *preferred_zone;
Mel Gormancc9a6c82012-03-21 16:34:11 -07002663 struct page *page = NULL;
Mel Gorman3dd28262009-06-16 15:32:00 -07002664 int migratetype = allocflags_to_migratetype(gfp_mask);
Mel Gormancc9a6c82012-03-21 16:34:11 -07002665 unsigned int cpuset_mems_cookie;
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07002666 int alloc_flags = ALLOC_WMARK_LOW|ALLOC_CPUSET;
Glauber Costa6a1a0d32012-12-18 14:22:00 -08002667 struct mem_cgroup *memcg = NULL;
Mel Gorman11e33f62009-06-16 15:31:57 -07002668
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +10002669 gfp_mask &= gfp_allowed_mask;
2670
Mel Gorman11e33f62009-06-16 15:31:57 -07002671 lockdep_trace_alloc(gfp_mask);
2672
2673 might_sleep_if(gfp_mask & __GFP_WAIT);
2674
2675 if (should_fail_alloc_page(gfp_mask, order))
2676 return NULL;
2677
2678 /*
2679 * Check the zones suitable for the gfp_mask contain at least one
2680 * valid zone. It's possible to have an empty zonelist as a result
2681 * of GFP_THISNODE and a memoryless node
2682 */
2683 if (unlikely(!zonelist->_zonerefs->zone))
2684 return NULL;
2685
Glauber Costa6a1a0d32012-12-18 14:22:00 -08002686 /*
2687 * Will only have any effect when __GFP_KMEMCG is set. This is
2688 * verified in the (always inline) callee
2689 */
2690 if (!memcg_kmem_newpage_charge(gfp_mask, &memcg, order))
2691 return NULL;
2692
Mel Gormancc9a6c82012-03-21 16:34:11 -07002693retry_cpuset:
2694 cpuset_mems_cookie = get_mems_allowed();
2695
Mel Gorman5117f452009-06-16 15:31:59 -07002696 /* The preferred zone is used for statistics later */
David Rientjesf33261d2011-01-25 15:07:20 -08002697 first_zones_zonelist(zonelist, high_zoneidx,
2698 nodemask ? : &cpuset_current_mems_allowed,
2699 &preferred_zone);
Mel Gormancc9a6c82012-03-21 16:34:11 -07002700 if (!preferred_zone)
2701 goto out;
Mel Gorman5117f452009-06-16 15:31:59 -07002702
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07002703#ifdef CONFIG_CMA
2704 if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
2705 alloc_flags |= ALLOC_CMA;
2706#endif
Mel Gorman5117f452009-06-16 15:31:59 -07002707 /* First allocation attempt */
Mel Gorman11e33f62009-06-16 15:31:57 -07002708 page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, nodemask, order,
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07002709 zonelist, high_zoneidx, alloc_flags,
Mel Gorman3dd28262009-06-16 15:32:00 -07002710 preferred_zone, migratetype);
Ming Lei21caf2f2013-02-22 16:34:08 -08002711 if (unlikely(!page)) {
2712 /*
2713 * Runtime PM, block IO and its error handling path
2714 * can deadlock because I/O on the device might not
2715 * complete.
2716 */
2717 gfp_mask = memalloc_noio_flags(gfp_mask);
Mel Gorman11e33f62009-06-16 15:31:57 -07002718 page = __alloc_pages_slowpath(gfp_mask, order,
Mel Gorman5117f452009-06-16 15:31:59 -07002719 zonelist, high_zoneidx, nodemask,
Mel Gorman3dd28262009-06-16 15:32:00 -07002720 preferred_zone, migratetype);
Ming Lei21caf2f2013-02-22 16:34:08 -08002721 }
Mel Gorman11e33f62009-06-16 15:31:57 -07002722
Mel Gorman4b4f2782009-09-21 17:02:41 -07002723 trace_mm_page_alloc(page, order, gfp_mask, migratetype);
Mel Gormancc9a6c82012-03-21 16:34:11 -07002724
2725out:
2726 /*
2727 * When updating a task's mems_allowed, it is possible to race with
2728 * parallel threads in such a way that an allocation can fail while
2729 * the mask is being updated. If a page allocation is about to fail,
2730 * check if the cpuset changed during allocation and if so, retry.
2731 */
2732 if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !page))
2733 goto retry_cpuset;
2734
Glauber Costa6a1a0d32012-12-18 14:22:00 -08002735 memcg_kmem_commit_charge(page, memcg, order);
2736
Mel Gorman11e33f62009-06-16 15:31:57 -07002737 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738}
Mel Gormand2391712009-06-16 15:31:52 -07002739EXPORT_SYMBOL(__alloc_pages_nodemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
2741/*
2742 * Common helper functions.
2743 */
Harvey Harrison920c7a52008-02-04 22:29:26 -08002744unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745{
Akinobu Mita945a1112009-09-21 17:01:47 -07002746 struct page *page;
2747
2748 /*
2749 * __get_free_pages() returns a 32-bit address, which cannot represent
2750 * a highmem page
2751 */
2752 VM_BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
2753
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 page = alloc_pages(gfp_mask, order);
2755 if (!page)
2756 return 0;
2757 return (unsigned long) page_address(page);
2758}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759EXPORT_SYMBOL(__get_free_pages);
2760
Harvey Harrison920c7a52008-02-04 22:29:26 -08002761unsigned long get_zeroed_page(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762{
Akinobu Mita945a1112009-09-21 17:01:47 -07002763 return __get_free_pages(gfp_mask | __GFP_ZERO, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765EXPORT_SYMBOL(get_zeroed_page);
2766
Harvey Harrison920c7a52008-02-04 22:29:26 -08002767void __free_pages(struct page *page, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768{
Nick Pigginb5810032005-10-29 18:16:12 -07002769 if (put_page_testzero(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 if (order == 0)
Li Hongfc916682010-03-05 13:41:54 -08002771 free_hot_cold_page(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 else
2773 __free_pages_ok(page, order);
2774 }
2775}
2776
2777EXPORT_SYMBOL(__free_pages);
2778
Harvey Harrison920c7a52008-02-04 22:29:26 -08002779void free_pages(unsigned long addr, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780{
2781 if (addr != 0) {
Nick Piggin725d7042006-09-25 23:30:55 -07002782 VM_BUG_ON(!virt_addr_valid((void *)addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 __free_pages(virt_to_page((void *)addr), order);
2784 }
2785}
2786
2787EXPORT_SYMBOL(free_pages);
2788
Glauber Costa6a1a0d32012-12-18 14:22:00 -08002789/*
2790 * __free_memcg_kmem_pages and free_memcg_kmem_pages will free
2791 * pages allocated with __GFP_KMEMCG.
2792 *
2793 * Those pages are accounted to a particular memcg, embedded in the
2794 * corresponding page_cgroup. To avoid adding a hit in the allocator to search
2795 * for that information only to find out that it is NULL for users who have no
2796 * interest in that whatsoever, we provide these functions.
2797 *
2798 * The caller knows better which flags it relies on.
2799 */
2800void __free_memcg_kmem_pages(struct page *page, unsigned int order)
2801{
2802 memcg_kmem_uncharge_pages(page, order);
2803 __free_pages(page, order);
2804}
2805
2806void free_memcg_kmem_pages(unsigned long addr, unsigned int order)
2807{
2808 if (addr != 0) {
2809 VM_BUG_ON(!virt_addr_valid((void *)addr));
2810 __free_memcg_kmem_pages(virt_to_page((void *)addr), order);
2811 }
2812}
2813
Andi Kleenee85c2e2011-05-11 15:13:34 -07002814static void *make_alloc_exact(unsigned long addr, unsigned order, size_t size)
2815{
2816 if (addr) {
2817 unsigned long alloc_end = addr + (PAGE_SIZE << order);
2818 unsigned long used = addr + PAGE_ALIGN(size);
2819
2820 split_page(virt_to_page((void *)addr), order);
2821 while (used < alloc_end) {
2822 free_page(used);
2823 used += PAGE_SIZE;
2824 }
2825 }
2826 return (void *)addr;
2827}
2828
Timur Tabi2be0ffe2008-07-23 21:28:11 -07002829/**
2830 * alloc_pages_exact - allocate an exact number physically-contiguous pages.
2831 * @size: the number of bytes to allocate
2832 * @gfp_mask: GFP flags for the allocation
2833 *
2834 * This function is similar to alloc_pages(), except that it allocates the
2835 * minimum number of pages to satisfy the request. alloc_pages() can only
2836 * allocate memory in power-of-two pages.
2837 *
2838 * This function is also limited by MAX_ORDER.
2839 *
2840 * Memory allocated by this function must be released by free_pages_exact().
2841 */
2842void *alloc_pages_exact(size_t size, gfp_t gfp_mask)
2843{
2844 unsigned int order = get_order(size);
2845 unsigned long addr;
2846
2847 addr = __get_free_pages(gfp_mask, order);
Andi Kleenee85c2e2011-05-11 15:13:34 -07002848 return make_alloc_exact(addr, order, size);
Timur Tabi2be0ffe2008-07-23 21:28:11 -07002849}
2850EXPORT_SYMBOL(alloc_pages_exact);
2851
2852/**
Andi Kleenee85c2e2011-05-11 15:13:34 -07002853 * alloc_pages_exact_nid - allocate an exact number of physically-contiguous
2854 * pages on a node.
Randy Dunlapb5e6ab52011-05-16 13:16:54 -07002855 * @nid: the preferred node ID where memory should be allocated
Andi Kleenee85c2e2011-05-11 15:13:34 -07002856 * @size: the number of bytes to allocate
2857 * @gfp_mask: GFP flags for the allocation
2858 *
2859 * Like alloc_pages_exact(), but try to allocate on node nid first before falling
2860 * back.
2861 * Note this is not alloc_pages_exact_node() which allocates on a specific node,
2862 * but is not exact.
2863 */
2864void *alloc_pages_exact_nid(int nid, size_t size, gfp_t gfp_mask)
2865{
2866 unsigned order = get_order(size);
2867 struct page *p = alloc_pages_node(nid, gfp_mask, order);
2868 if (!p)
2869 return NULL;
2870 return make_alloc_exact((unsigned long)page_address(p), order, size);
2871}
2872EXPORT_SYMBOL(alloc_pages_exact_nid);
2873
2874/**
Timur Tabi2be0ffe2008-07-23 21:28:11 -07002875 * free_pages_exact - release memory allocated via alloc_pages_exact()
2876 * @virt: the value returned by alloc_pages_exact.
2877 * @size: size of allocation, same value as passed to alloc_pages_exact().
2878 *
2879 * Release the memory allocated by a previous call to alloc_pages_exact.
2880 */
2881void free_pages_exact(void *virt, size_t size)
2882{
2883 unsigned long addr = (unsigned long)virt;
2884 unsigned long end = addr + PAGE_ALIGN(size);
2885
2886 while (addr < end) {
2887 free_page(addr);
2888 addr += PAGE_SIZE;
2889 }
2890}
2891EXPORT_SYMBOL(free_pages_exact);
2892
Zhang Yanfeie0fb5812013-02-22 16:35:54 -08002893/**
2894 * nr_free_zone_pages - count number of pages beyond high watermark
2895 * @offset: The zone index of the highest zone
2896 *
2897 * nr_free_zone_pages() counts the number of counts pages which are beyond the
2898 * high watermark within all zones at or below a given zone index. For each
2899 * zone, the number of pages is calculated as:
2900 * present_pages - high_pages
2901 */
Zhang Yanfeiebec3862013-02-22 16:35:43 -08002902static unsigned long nr_free_zone_pages(int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903{
Mel Gormandd1a2392008-04-28 02:12:17 -07002904 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07002905 struct zone *zone;
2906
Martin J. Blighe310fd42005-07-29 22:59:18 -07002907 /* Just pick one node, since fallback list is circular */
Zhang Yanfeiebec3862013-02-22 16:35:43 -08002908 unsigned long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
Mel Gorman0e884602008-04-28 02:12:14 -07002910 struct zonelist *zonelist = node_zonelist(numa_node_id(), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
Mel Gorman54a6eb52008-04-28 02:12:16 -07002912 for_each_zone_zonelist(zone, z, zonelist, offset) {
Jiang Liub40da042013-02-22 16:33:52 -08002913 unsigned long size = zone->managed_pages;
Mel Gorman41858962009-06-16 15:32:12 -07002914 unsigned long high = high_wmark_pages(zone);
Martin J. Blighe310fd42005-07-29 22:59:18 -07002915 if (size > high)
2916 sum += size - high;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 }
2918
2919 return sum;
2920}
2921
Zhang Yanfeie0fb5812013-02-22 16:35:54 -08002922/**
2923 * nr_free_buffer_pages - count number of pages beyond high watermark
2924 *
2925 * nr_free_buffer_pages() counts the number of pages which are beyond the high
2926 * watermark within ZONE_DMA and ZONE_NORMAL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 */
Zhang Yanfeiebec3862013-02-22 16:35:43 -08002928unsigned long nr_free_buffer_pages(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929{
Al Viroaf4ca452005-10-21 02:55:38 -04002930 return nr_free_zone_pages(gfp_zone(GFP_USER));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931}
Meelap Shahc2f1a552007-07-17 04:04:39 -07002932EXPORT_SYMBOL_GPL(nr_free_buffer_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
Zhang Yanfeie0fb5812013-02-22 16:35:54 -08002934/**
2935 * nr_free_pagecache_pages - count number of pages beyond high watermark
2936 *
2937 * nr_free_pagecache_pages() counts the number of pages which are beyond the
2938 * high watermark within all zones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 */
Zhang Yanfeiebec3862013-02-22 16:35:43 -08002940unsigned long nr_free_pagecache_pages(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941{
Mel Gorman2a1e2742007-07-17 04:03:12 -07002942 return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER_MOVABLE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943}
Christoph Lameter08e0f6a2006-09-27 01:50:06 -07002944
2945static inline void show_node(struct zone *zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946{
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08002947 if (IS_ENABLED(CONFIG_NUMA))
Andy Whitcroft25ba77c2006-12-06 20:33:03 -08002948 printk("Node %d ", zone_to_nid(zone));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951void si_meminfo(struct sysinfo *val)
2952{
2953 val->totalram = totalram_pages;
2954 val->sharedram = 0;
Christoph Lameterd23ad422007-02-10 01:43:02 -08002955 val->freeram = global_page_state(NR_FREE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 val->bufferram = nr_blockdev_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 val->totalhigh = totalhigh_pages;
2958 val->freehigh = nr_free_highpages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 val->mem_unit = PAGE_SIZE;
2960}
2961
2962EXPORT_SYMBOL(si_meminfo);
2963
2964#ifdef CONFIG_NUMA
2965void si_meminfo_node(struct sysinfo *val, int nid)
2966{
2967 pg_data_t *pgdat = NODE_DATA(nid);
2968
2969 val->totalram = pgdat->node_present_pages;
Christoph Lameterd23ad422007-02-10 01:43:02 -08002970 val->freeram = node_page_state(nid, NR_FREE_PAGES);
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07002971#ifdef CONFIG_HIGHMEM
Jiang Liub40da042013-02-22 16:33:52 -08002972 val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].managed_pages;
Christoph Lameterd23ad422007-02-10 01:43:02 -08002973 val->freehigh = zone_page_state(&pgdat->node_zones[ZONE_HIGHMEM],
2974 NR_FREE_PAGES);
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07002975#else
2976 val->totalhigh = 0;
2977 val->freehigh = 0;
2978#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 val->mem_unit = PAGE_SIZE;
2980}
2981#endif
2982
David Rientjesddd588b2011-03-22 16:30:46 -07002983/*
David Rientjes7bf02ea2011-05-24 17:11:16 -07002984 * Determine whether the node should be displayed or not, depending on whether
2985 * SHOW_MEM_FILTER_NODES was passed to show_free_areas().
David Rientjesddd588b2011-03-22 16:30:46 -07002986 */
David Rientjes7bf02ea2011-05-24 17:11:16 -07002987bool skip_free_areas_node(unsigned int flags, int nid)
David Rientjesddd588b2011-03-22 16:30:46 -07002988{
2989 bool ret = false;
Mel Gormancc9a6c82012-03-21 16:34:11 -07002990 unsigned int cpuset_mems_cookie;
David Rientjesddd588b2011-03-22 16:30:46 -07002991
2992 if (!(flags & SHOW_MEM_FILTER_NODES))
2993 goto out;
2994
Mel Gormancc9a6c82012-03-21 16:34:11 -07002995 do {
2996 cpuset_mems_cookie = get_mems_allowed();
2997 ret = !node_isset(nid, cpuset_current_mems_allowed);
2998 } while (!put_mems_allowed(cpuset_mems_cookie));
David Rientjesddd588b2011-03-22 16:30:46 -07002999out:
3000 return ret;
3001}
3002
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003#define K(x) ((x) << (PAGE_SHIFT-10))
3004
Rabin Vincent377e4f12012-12-11 16:00:24 -08003005static void show_migration_types(unsigned char type)
3006{
3007 static const char types[MIGRATE_TYPES] = {
3008 [MIGRATE_UNMOVABLE] = 'U',
3009 [MIGRATE_RECLAIMABLE] = 'E',
3010 [MIGRATE_MOVABLE] = 'M',
3011 [MIGRATE_RESERVE] = 'R',
3012#ifdef CONFIG_CMA
3013 [MIGRATE_CMA] = 'C',
3014#endif
Minchan Kim194159f2013-02-22 16:33:58 -08003015#ifdef CONFIG_MEMORY_ISOLATION
Rabin Vincent377e4f12012-12-11 16:00:24 -08003016 [MIGRATE_ISOLATE] = 'I',
Minchan Kim194159f2013-02-22 16:33:58 -08003017#endif
Rabin Vincent377e4f12012-12-11 16:00:24 -08003018 };
3019 char tmp[MIGRATE_TYPES + 1];
3020 char *p = tmp;
3021 int i;
3022
3023 for (i = 0; i < MIGRATE_TYPES; i++) {
3024 if (type & (1 << i))
3025 *p++ = types[i];
3026 }
3027
3028 *p = '\0';
3029 printk("(%s) ", tmp);
3030}
3031
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032/*
3033 * Show free area list (used inside shift_scroll-lock stuff)
3034 * We also calculate the percentage fragmentation. We do this by counting the
3035 * memory on each free list with the exception of the first item on the list.
David Rientjesddd588b2011-03-22 16:30:46 -07003036 * Suppresses nodes that are not allowed by current's cpuset if
3037 * SHOW_MEM_FILTER_NODES is passed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 */
David Rientjes7bf02ea2011-05-24 17:11:16 -07003039void show_free_areas(unsigned int filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040{
Jes Sorensenc7241912006-09-27 01:50:05 -07003041 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 struct zone *zone;
3043
KOSAKI Motohiroee99c712009-03-31 15:19:31 -07003044 for_each_populated_zone(zone) {
David Rientjes7bf02ea2011-05-24 17:11:16 -07003045 if (skip_free_areas_node(filter, zone_to_nid(zone)))
David Rientjesddd588b2011-03-22 16:30:46 -07003046 continue;
Jes Sorensenc7241912006-09-27 01:50:05 -07003047 show_node(zone);
3048 printk("%s per-cpu:\n", zone->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049
Dave Jones6b482c62005-11-10 15:45:56 -05003050 for_each_online_cpu(cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 struct per_cpu_pageset *pageset;
3052
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09003053 pageset = per_cpu_ptr(zone->pageset, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
Christoph Lameter3dfa5722008-02-04 22:29:19 -08003055 printk("CPU %4d: hi:%5d, btch:%4d usd:%4d\n",
3056 cpu, pageset->pcp.high,
3057 pageset->pcp.batch, pageset->pcp.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 }
3059 }
3060
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07003061 printk("active_anon:%lu inactive_anon:%lu isolated_anon:%lu\n"
3062 " active_file:%lu inactive_file:%lu isolated_file:%lu\n"
Lee Schermerhorn7b854122008-10-18 20:26:40 -07003063 " unevictable:%lu"
Andrew Mortonb76146e2009-10-26 16:49:52 -07003064 " dirty:%lu writeback:%lu unstable:%lu\n"
KOSAKI Motohiro3701b032009-09-21 17:01:29 -07003065 " free:%lu slab_reclaimable:%lu slab_unreclaimable:%lu\n"
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -07003066 " mapped:%lu shmem:%lu pagetables:%lu bounce:%lu\n"
3067 " free_cma:%lu\n",
Rik van Riel4f98a2f2008-10-18 20:26:32 -07003068 global_page_state(NR_ACTIVE_ANON),
Rik van Riel4f98a2f2008-10-18 20:26:32 -07003069 global_page_state(NR_INACTIVE_ANON),
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07003070 global_page_state(NR_ISOLATED_ANON),
3071 global_page_state(NR_ACTIVE_FILE),
Rik van Riel4f98a2f2008-10-18 20:26:32 -07003072 global_page_state(NR_INACTIVE_FILE),
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07003073 global_page_state(NR_ISOLATED_FILE),
Lee Schermerhorn7b854122008-10-18 20:26:40 -07003074 global_page_state(NR_UNEVICTABLE),
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -07003075 global_page_state(NR_FILE_DIRTY),
Christoph Lameterce866b32006-06-30 01:55:40 -07003076 global_page_state(NR_WRITEBACK),
Christoph Lameterfd39fc82006-06-30 01:55:40 -07003077 global_page_state(NR_UNSTABLE_NFS),
Christoph Lameterd23ad422007-02-10 01:43:02 -08003078 global_page_state(NR_FREE_PAGES),
KOSAKI Motohiro3701b032009-09-21 17:01:29 -07003079 global_page_state(NR_SLAB_RECLAIMABLE),
3080 global_page_state(NR_SLAB_UNRECLAIMABLE),
Christoph Lameter65ba55f2006-06-30 01:55:34 -07003081 global_page_state(NR_FILE_MAPPED),
KOSAKI Motohiro4b021082009-09-21 17:01:33 -07003082 global_page_state(NR_SHMEM),
Andrew Mortona25700a2007-02-08 14:20:40 -08003083 global_page_state(NR_PAGETABLE),
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -07003084 global_page_state(NR_BOUNCE),
3085 global_page_state(NR_FREE_CMA_PAGES));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
KOSAKI Motohiroee99c712009-03-31 15:19:31 -07003087 for_each_populated_zone(zone) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 int i;
3089
David Rientjes7bf02ea2011-05-24 17:11:16 -07003090 if (skip_free_areas_node(filter, zone_to_nid(zone)))
David Rientjesddd588b2011-03-22 16:30:46 -07003091 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 show_node(zone);
3093 printk("%s"
3094 " free:%lukB"
3095 " min:%lukB"
3096 " low:%lukB"
3097 " high:%lukB"
Rik van Riel4f98a2f2008-10-18 20:26:32 -07003098 " active_anon:%lukB"
3099 " inactive_anon:%lukB"
3100 " active_file:%lukB"
3101 " inactive_file:%lukB"
Lee Schermerhorn7b854122008-10-18 20:26:40 -07003102 " unevictable:%lukB"
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07003103 " isolated(anon):%lukB"
3104 " isolated(file):%lukB"
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 " present:%lukB"
Jiang Liu9feedc92012-12-12 13:52:12 -08003106 " managed:%lukB"
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07003107 " mlocked:%lukB"
3108 " dirty:%lukB"
3109 " writeback:%lukB"
3110 " mapped:%lukB"
KOSAKI Motohiro4b021082009-09-21 17:01:33 -07003111 " shmem:%lukB"
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07003112 " slab_reclaimable:%lukB"
3113 " slab_unreclaimable:%lukB"
KOSAKI Motohiroc6a7f572009-09-21 17:01:32 -07003114 " kernel_stack:%lukB"
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07003115 " pagetables:%lukB"
3116 " unstable:%lukB"
3117 " bounce:%lukB"
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -07003118 " free_cma:%lukB"
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07003119 " writeback_tmp:%lukB"
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 " pages_scanned:%lu"
3121 " all_unreclaimable? %s"
3122 "\n",
3123 zone->name,
Mel Gorman88f5acf2011-01-13 15:45:41 -08003124 K(zone_page_state(zone, NR_FREE_PAGES)),
Mel Gorman41858962009-06-16 15:32:12 -07003125 K(min_wmark_pages(zone)),
3126 K(low_wmark_pages(zone)),
3127 K(high_wmark_pages(zone)),
Rik van Riel4f98a2f2008-10-18 20:26:32 -07003128 K(zone_page_state(zone, NR_ACTIVE_ANON)),
3129 K(zone_page_state(zone, NR_INACTIVE_ANON)),
3130 K(zone_page_state(zone, NR_ACTIVE_FILE)),
3131 K(zone_page_state(zone, NR_INACTIVE_FILE)),
Lee Schermerhorn7b854122008-10-18 20:26:40 -07003132 K(zone_page_state(zone, NR_UNEVICTABLE)),
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07003133 K(zone_page_state(zone, NR_ISOLATED_ANON)),
3134 K(zone_page_state(zone, NR_ISOLATED_FILE)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 K(zone->present_pages),
Jiang Liu9feedc92012-12-12 13:52:12 -08003136 K(zone->managed_pages),
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07003137 K(zone_page_state(zone, NR_MLOCK)),
3138 K(zone_page_state(zone, NR_FILE_DIRTY)),
3139 K(zone_page_state(zone, NR_WRITEBACK)),
3140 K(zone_page_state(zone, NR_FILE_MAPPED)),
KOSAKI Motohiro4b021082009-09-21 17:01:33 -07003141 K(zone_page_state(zone, NR_SHMEM)),
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07003142 K(zone_page_state(zone, NR_SLAB_RECLAIMABLE)),
3143 K(zone_page_state(zone, NR_SLAB_UNRECLAIMABLE)),
KOSAKI Motohiroc6a7f572009-09-21 17:01:32 -07003144 zone_page_state(zone, NR_KERNEL_STACK) *
3145 THREAD_SIZE / 1024,
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07003146 K(zone_page_state(zone, NR_PAGETABLE)),
3147 K(zone_page_state(zone, NR_UNSTABLE_NFS)),
3148 K(zone_page_state(zone, NR_BOUNCE)),
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -07003149 K(zone_page_state(zone, NR_FREE_CMA_PAGES)),
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07003150 K(zone_page_state(zone, NR_WRITEBACK_TEMP)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 zone->pages_scanned,
KOSAKI Motohiro93e4a892010-03-05 13:41:55 -08003152 (zone->all_unreclaimable ? "yes" : "no")
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 );
3154 printk("lowmem_reserve[]:");
3155 for (i = 0; i < MAX_NR_ZONES; i++)
3156 printk(" %lu", zone->lowmem_reserve[i]);
3157 printk("\n");
3158 }
3159
KOSAKI Motohiroee99c712009-03-31 15:19:31 -07003160 for_each_populated_zone(zone) {
Kirill Korotaev8f9de512006-06-23 02:03:50 -07003161 unsigned long nr[MAX_ORDER], flags, order, total = 0;
Rabin Vincent377e4f12012-12-11 16:00:24 -08003162 unsigned char types[MAX_ORDER];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
David Rientjes7bf02ea2011-05-24 17:11:16 -07003164 if (skip_free_areas_node(filter, zone_to_nid(zone)))
David Rientjesddd588b2011-03-22 16:30:46 -07003165 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 show_node(zone);
3167 printk("%s: ", zone->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
3169 spin_lock_irqsave(&zone->lock, flags);
3170 for (order = 0; order < MAX_ORDER; order++) {
Rabin Vincent377e4f12012-12-11 16:00:24 -08003171 struct free_area *area = &zone->free_area[order];
3172 int type;
3173
3174 nr[order] = area->nr_free;
Kirill Korotaev8f9de512006-06-23 02:03:50 -07003175 total += nr[order] << order;
Rabin Vincent377e4f12012-12-11 16:00:24 -08003176
3177 types[order] = 0;
3178 for (type = 0; type < MIGRATE_TYPES; type++) {
3179 if (!list_empty(&area->free_list[type]))
3180 types[order] |= 1 << type;
3181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 }
3183 spin_unlock_irqrestore(&zone->lock, flags);
Rabin Vincent377e4f12012-12-11 16:00:24 -08003184 for (order = 0; order < MAX_ORDER; order++) {
Kirill Korotaev8f9de512006-06-23 02:03:50 -07003185 printk("%lu*%lukB ", nr[order], K(1UL) << order);
Rabin Vincent377e4f12012-12-11 16:00:24 -08003186 if (nr[order])
3187 show_migration_types(types[order]);
3188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 printk("= %lukB\n", K(total));
3190 }
3191
David Rientjes949f7ec2013-04-29 15:07:48 -07003192 hugetlb_show_meminfo();
3193
Larry Woodmane6f36022008-02-04 22:29:30 -08003194 printk("%ld total pagecache pages\n", global_page_state(NR_FILE_PAGES));
3195
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 show_swap_cache_info();
3197}
3198
Mel Gorman19770b32008-04-28 02:12:18 -07003199static void zoneref_set_zone(struct zone *zone, struct zoneref *zoneref)
3200{
3201 zoneref->zone = zone;
3202 zoneref->zone_idx = zone_idx(zone);
3203}
3204
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205/*
3206 * Builds allocation fallback zone lists.
Christoph Lameter1a932052006-01-06 00:11:16 -08003207 *
3208 * Add all populated zones of a node to the zonelist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003210static int build_zonelists_node(pg_data_t *pgdat, struct zonelist *zonelist,
3211 int nr_zones, enum zone_type zone_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212{
Christoph Lameter1a932052006-01-06 00:11:16 -08003213 struct zone *zone;
3214
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07003215 BUG_ON(zone_type >= MAX_NR_ZONES);
Christoph Lameter2f6726e2006-09-25 23:31:18 -07003216 zone_type++;
Christoph Lameter02a68a52006-01-06 00:11:18 -08003217
3218 do {
Christoph Lameter2f6726e2006-09-25 23:31:18 -07003219 zone_type--;
Christoph Lameter070f8032006-01-06 00:11:19 -08003220 zone = pgdat->node_zones + zone_type;
Christoph Lameter1a932052006-01-06 00:11:16 -08003221 if (populated_zone(zone)) {
Mel Gormandd1a2392008-04-28 02:12:17 -07003222 zoneref_set_zone(zone,
3223 &zonelist->_zonerefs[nr_zones++]);
Christoph Lameter070f8032006-01-06 00:11:19 -08003224 check_highest_zone(zone_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 }
Christoph Lameter02a68a52006-01-06 00:11:18 -08003226
Christoph Lameter2f6726e2006-09-25 23:31:18 -07003227 } while (zone_type);
Christoph Lameter070f8032006-01-06 00:11:19 -08003228 return nr_zones;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229}
3230
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003231
3232/*
3233 * zonelist_order:
3234 * 0 = automatic detection of better ordering.
3235 * 1 = order by ([node] distance, -zonetype)
3236 * 2 = order by (-zonetype, [node] distance)
3237 *
3238 * If not NUMA, ZONELIST_ORDER_ZONE and ZONELIST_ORDER_NODE will create
3239 * the same zonelist. So only NUMA can configure this param.
3240 */
3241#define ZONELIST_ORDER_DEFAULT 0
3242#define ZONELIST_ORDER_NODE 1
3243#define ZONELIST_ORDER_ZONE 2
3244
3245/* zonelist order in the kernel.
3246 * set_zonelist_order() will set this to NODE or ZONE.
3247 */
3248static int current_zonelist_order = ZONELIST_ORDER_DEFAULT;
3249static char zonelist_order_name[3][8] = {"Default", "Node", "Zone"};
3250
3251
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252#ifdef CONFIG_NUMA
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003253/* The value user specified ....changed by config */
3254static int user_zonelist_order = ZONELIST_ORDER_DEFAULT;
3255/* string for sysctl */
3256#define NUMA_ZONELIST_ORDER_LEN 16
3257char numa_zonelist_order[16] = "default";
3258
3259/*
3260 * interface for configure zonelist ordering.
3261 * command line option "numa_zonelist_order"
3262 * = "[dD]efault - default, automatic configuration.
3263 * = "[nN]ode - order by node locality, then by zone within node
3264 * = "[zZ]one - order by zone, then by locality within zone
3265 */
3266
3267static int __parse_numa_zonelist_order(char *s)
3268{
3269 if (*s == 'd' || *s == 'D') {
3270 user_zonelist_order = ZONELIST_ORDER_DEFAULT;
3271 } else if (*s == 'n' || *s == 'N') {
3272 user_zonelist_order = ZONELIST_ORDER_NODE;
3273 } else if (*s == 'z' || *s == 'Z') {
3274 user_zonelist_order = ZONELIST_ORDER_ZONE;
3275 } else {
3276 printk(KERN_WARNING
3277 "Ignoring invalid numa_zonelist_order value: "
3278 "%s\n", s);
3279 return -EINVAL;
3280 }
3281 return 0;
3282}
3283
3284static __init int setup_numa_zonelist_order(char *s)
3285{
Volodymyr G. Lukiianykecb256f2011-01-13 15:46:26 -08003286 int ret;
3287
3288 if (!s)
3289 return 0;
3290
3291 ret = __parse_numa_zonelist_order(s);
3292 if (ret == 0)
3293 strlcpy(numa_zonelist_order, s, NUMA_ZONELIST_ORDER_LEN);
3294
3295 return ret;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003296}
3297early_param("numa_zonelist_order", setup_numa_zonelist_order);
3298
3299/*
3300 * sysctl handler for numa_zonelist_order
3301 */
3302int numa_zonelist_order_handler(ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003303 void __user *buffer, size_t *length,
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003304 loff_t *ppos)
3305{
3306 char saved_string[NUMA_ZONELIST_ORDER_LEN];
3307 int ret;
Andi Kleen443c6f12009-12-23 21:00:47 +01003308 static DEFINE_MUTEX(zl_order_mutex);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003309
Andi Kleen443c6f12009-12-23 21:00:47 +01003310 mutex_lock(&zl_order_mutex);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003311 if (write)
Andi Kleen443c6f12009-12-23 21:00:47 +01003312 strcpy(saved_string, (char*)table->data);
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003313 ret = proc_dostring(table, write, buffer, length, ppos);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003314 if (ret)
Andi Kleen443c6f12009-12-23 21:00:47 +01003315 goto out;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003316 if (write) {
3317 int oldval = user_zonelist_order;
3318 if (__parse_numa_zonelist_order((char*)table->data)) {
3319 /*
3320 * bogus value. restore saved string
3321 */
3322 strncpy((char*)table->data, saved_string,
3323 NUMA_ZONELIST_ORDER_LEN);
3324 user_zonelist_order = oldval;
Haicheng Li4eaf3f62010-05-24 14:32:52 -07003325 } else if (oldval != user_zonelist_order) {
3326 mutex_lock(&zonelists_mutex);
Jiang Liu9adb62a2012-07-31 16:43:28 -07003327 build_all_zonelists(NULL, NULL);
Haicheng Li4eaf3f62010-05-24 14:32:52 -07003328 mutex_unlock(&zonelists_mutex);
3329 }
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003330 }
Andi Kleen443c6f12009-12-23 21:00:47 +01003331out:
3332 mutex_unlock(&zl_order_mutex);
3333 return ret;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003334}
3335
3336
Christoph Lameter62bc62a2009-06-16 15:32:15 -07003337#define MAX_NODE_LOAD (nr_online_nodes)
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003338static int node_load[MAX_NUMNODES];
3339
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340/**
Pavel Pisa4dc3b162005-05-01 08:59:25 -07003341 * 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 -07003342 * @node: node whose fallback list we're appending
3343 * @used_node_mask: nodemask_t of already used nodes
3344 *
3345 * We use a number of factors to determine which is the next node that should
3346 * appear on a given node's fallback list. The node should not have appeared
3347 * already in @node's fallback list, and it should be the next closest node
3348 * according to the distance array (which contains arbitrary distance values
3349 * from each node to each node in the system), and should also prefer nodes
3350 * with no CPUs, since presumably they'll have very little allocation pressure
3351 * on them otherwise.
3352 * It returns -1 if no node is found.
3353 */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003354static int find_next_best_node(int node, nodemask_t *used_node_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355{
Linus Torvalds4cf808eb2006-02-17 20:38:21 +01003356 int n, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 int min_val = INT_MAX;
David Rientjes00ef2d22013-02-22 16:35:36 -08003358 int best_node = NUMA_NO_NODE;
Rusty Russella70f7302009-03-13 14:49:46 +10303359 const struct cpumask *tmp = cpumask_of_node(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360
Linus Torvalds4cf808eb2006-02-17 20:38:21 +01003361 /* Use the local node if we haven't already */
3362 if (!node_isset(node, *used_node_mask)) {
3363 node_set(node, *used_node_mask);
3364 return node;
3365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08003367 for_each_node_state(n, N_MEMORY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
3369 /* Don't want a node to appear more than once */
3370 if (node_isset(n, *used_node_mask))
3371 continue;
3372
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 /* Use the distance array to find the distance */
3374 val = node_distance(node, n);
3375
Linus Torvalds4cf808eb2006-02-17 20:38:21 +01003376 /* Penalize nodes under us ("prefer the next node") */
3377 val += (n < node);
3378
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 /* Give preference to headless and unused nodes */
Rusty Russella70f7302009-03-13 14:49:46 +10303380 tmp = cpumask_of_node(n);
3381 if (!cpumask_empty(tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 val += PENALTY_FOR_NODE_WITH_CPUS;
3383
3384 /* Slight preference for less loaded node */
3385 val *= (MAX_NODE_LOAD*MAX_NUMNODES);
3386 val += node_load[n];
3387
3388 if (val < min_val) {
3389 min_val = val;
3390 best_node = n;
3391 }
3392 }
3393
3394 if (best_node >= 0)
3395 node_set(best_node, *used_node_mask);
3396
3397 return best_node;
3398}
3399
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003400
3401/*
3402 * Build zonelists ordered by node and zones within node.
3403 * This results in maximum locality--normal zone overflows into local
3404 * DMA zone, if any--but risks exhausting DMA zone.
3405 */
3406static void build_zonelists_in_node_order(pg_data_t *pgdat, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407{
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003408 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 struct zonelist *zonelist;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003410
Mel Gorman54a6eb52008-04-28 02:12:16 -07003411 zonelist = &pgdat->node_zonelists[0];
Mel Gormandd1a2392008-04-28 02:12:17 -07003412 for (j = 0; zonelist->_zonerefs[j].zone != NULL; j++)
Mel Gorman54a6eb52008-04-28 02:12:16 -07003413 ;
3414 j = build_zonelists_node(NODE_DATA(node), zonelist, j,
3415 MAX_NR_ZONES - 1);
Mel Gormandd1a2392008-04-28 02:12:17 -07003416 zonelist->_zonerefs[j].zone = NULL;
3417 zonelist->_zonerefs[j].zone_idx = 0;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003418}
3419
3420/*
Christoph Lameter523b9452007-10-16 01:25:37 -07003421 * Build gfp_thisnode zonelists
3422 */
3423static void build_thisnode_zonelists(pg_data_t *pgdat)
3424{
Christoph Lameter523b9452007-10-16 01:25:37 -07003425 int j;
3426 struct zonelist *zonelist;
3427
Mel Gorman54a6eb52008-04-28 02:12:16 -07003428 zonelist = &pgdat->node_zonelists[1];
3429 j = build_zonelists_node(pgdat, zonelist, 0, MAX_NR_ZONES - 1);
Mel Gormandd1a2392008-04-28 02:12:17 -07003430 zonelist->_zonerefs[j].zone = NULL;
3431 zonelist->_zonerefs[j].zone_idx = 0;
Christoph Lameter523b9452007-10-16 01:25:37 -07003432}
3433
3434/*
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003435 * Build zonelists ordered by zone and nodes within zones.
3436 * This results in conserving DMA zone[s] until all Normal memory is
3437 * exhausted, but results in overflowing to remote node while memory
3438 * may still exist in local DMA zone.
3439 */
3440static int node_order[MAX_NUMNODES];
3441
3442static void build_zonelists_in_zone_order(pg_data_t *pgdat, int nr_nodes)
3443{
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003444 int pos, j, node;
3445 int zone_type; /* needs to be signed */
3446 struct zone *z;
3447 struct zonelist *zonelist;
3448
Mel Gorman54a6eb52008-04-28 02:12:16 -07003449 zonelist = &pgdat->node_zonelists[0];
3450 pos = 0;
3451 for (zone_type = MAX_NR_ZONES - 1; zone_type >= 0; zone_type--) {
3452 for (j = 0; j < nr_nodes; j++) {
3453 node = node_order[j];
3454 z = &NODE_DATA(node)->node_zones[zone_type];
3455 if (populated_zone(z)) {
Mel Gormandd1a2392008-04-28 02:12:17 -07003456 zoneref_set_zone(z,
3457 &zonelist->_zonerefs[pos++]);
Mel Gorman54a6eb52008-04-28 02:12:16 -07003458 check_highest_zone(zone_type);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003459 }
3460 }
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003461 }
Mel Gormandd1a2392008-04-28 02:12:17 -07003462 zonelist->_zonerefs[pos].zone = NULL;
3463 zonelist->_zonerefs[pos].zone_idx = 0;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003464}
3465
3466static int default_zonelist_order(void)
3467{
3468 int nid, zone_type;
3469 unsigned long low_kmem_size,total_size;
3470 struct zone *z;
3471 int average_size;
3472 /*
Thomas Weber88393162010-03-16 11:47:56 +01003473 * ZONE_DMA and ZONE_DMA32 can be very small area in the system.
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003474 * If they are really small and used heavily, the system can fall
3475 * into OOM very easily.
David Rientjese325c902010-05-24 14:32:13 -07003476 * This function detect ZONE_DMA/DMA32 size and configures zone order.
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003477 */
3478 /* Is there ZONE_NORMAL ? (ex. ppc has only DMA zone..) */
3479 low_kmem_size = 0;
3480 total_size = 0;
3481 for_each_online_node(nid) {
3482 for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
3483 z = &NODE_DATA(nid)->node_zones[zone_type];
3484 if (populated_zone(z)) {
3485 if (zone_type < ZONE_NORMAL)
3486 low_kmem_size += z->present_pages;
3487 total_size += z->present_pages;
David Rientjese325c902010-05-24 14:32:13 -07003488 } else if (zone_type == ZONE_NORMAL) {
3489 /*
3490 * If any node has only lowmem, then node order
3491 * is preferred to allow kernel allocations
3492 * locally; otherwise, they can easily infringe
3493 * on other nodes when there is an abundance of
3494 * lowmem available to allocate from.
3495 */
3496 return ZONELIST_ORDER_NODE;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003497 }
3498 }
3499 }
3500 if (!low_kmem_size || /* there are no DMA area. */
3501 low_kmem_size > total_size/2) /* DMA/DMA32 is big. */
3502 return ZONELIST_ORDER_NODE;
3503 /*
3504 * look into each node's config.
3505 * If there is a node whose DMA/DMA32 memory is very big area on
3506 * local memory, NODE_ORDER may be suitable.
3507 */
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07003508 average_size = total_size /
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08003509 (nodes_weight(node_states[N_MEMORY]) + 1);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003510 for_each_online_node(nid) {
3511 low_kmem_size = 0;
3512 total_size = 0;
3513 for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
3514 z = &NODE_DATA(nid)->node_zones[zone_type];
3515 if (populated_zone(z)) {
3516 if (zone_type < ZONE_NORMAL)
3517 low_kmem_size += z->present_pages;
3518 total_size += z->present_pages;
3519 }
3520 }
3521 if (low_kmem_size &&
3522 total_size > average_size && /* ignore small node */
3523 low_kmem_size > total_size * 70/100)
3524 return ZONELIST_ORDER_NODE;
3525 }
3526 return ZONELIST_ORDER_ZONE;
3527}
3528
3529static void set_zonelist_order(void)
3530{
3531 if (user_zonelist_order == ZONELIST_ORDER_DEFAULT)
3532 current_zonelist_order = default_zonelist_order();
3533 else
3534 current_zonelist_order = user_zonelist_order;
3535}
3536
3537static void build_zonelists(pg_data_t *pgdat)
3538{
3539 int j, node, load;
3540 enum zone_type i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 nodemask_t used_mask;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003542 int local_node, prev_node;
3543 struct zonelist *zonelist;
3544 int order = current_zonelist_order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
3546 /* initialize zonelists */
Christoph Lameter523b9452007-10-16 01:25:37 -07003547 for (i = 0; i < MAX_ZONELISTS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 zonelist = pgdat->node_zonelists + i;
Mel Gormandd1a2392008-04-28 02:12:17 -07003549 zonelist->_zonerefs[0].zone = NULL;
3550 zonelist->_zonerefs[0].zone_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 }
3552
3553 /* NUMA-aware ordering of nodes */
3554 local_node = pgdat->node_id;
Christoph Lameter62bc62a2009-06-16 15:32:15 -07003555 load = nr_online_nodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 prev_node = local_node;
3557 nodes_clear(used_mask);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003558
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003559 memset(node_order, 0, sizeof(node_order));
3560 j = 0;
3561
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
3563 /*
3564 * We don't want to pressure a particular node.
3565 * So adding penalty to the first node in same
3566 * distance group to make it round-robin.
3567 */
David Rientjes957f8222012-10-08 16:33:24 -07003568 if (node_distance(local_node, node) !=
3569 node_distance(local_node, prev_node))
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003570 node_load[node] = load;
3571
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 prev_node = node;
3573 load--;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003574 if (order == ZONELIST_ORDER_NODE)
3575 build_zonelists_in_node_order(pgdat, node);
3576 else
3577 node_order[j++] = node; /* remember order */
3578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003580 if (order == ZONELIST_ORDER_ZONE) {
3581 /* calculate node order -- i.e., DMA last! */
3582 build_zonelists_in_zone_order(pgdat, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 }
Christoph Lameter523b9452007-10-16 01:25:37 -07003584
3585 build_thisnode_zonelists(pgdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586}
3587
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003588/* Construct the zonelist performance cache - see further mmzone.h */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003589static void build_zonelist_cache(pg_data_t *pgdat)
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003590{
Mel Gorman54a6eb52008-04-28 02:12:16 -07003591 struct zonelist *zonelist;
3592 struct zonelist_cache *zlc;
Mel Gormandd1a2392008-04-28 02:12:17 -07003593 struct zoneref *z;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003594
Mel Gorman54a6eb52008-04-28 02:12:16 -07003595 zonelist = &pgdat->node_zonelists[0];
3596 zonelist->zlcache_ptr = zlc = &zonelist->zlcache;
3597 bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
Mel Gormandd1a2392008-04-28 02:12:17 -07003598 for (z = zonelist->_zonerefs; z->zone; z++)
3599 zlc->z_to_n[z - zonelist->_zonerefs] = zonelist_node_idx(z);
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003600}
3601
Lee Schermerhorn7aac7892010-05-26 14:45:00 -07003602#ifdef CONFIG_HAVE_MEMORYLESS_NODES
3603/*
3604 * Return node id of node used for "local" allocations.
3605 * I.e., first node id of first zone in arg node's generic zonelist.
3606 * Used for initializing percpu 'numa_mem', which is used primarily
3607 * for kernel allocations, so use GFP_KERNEL flags to locate zonelist.
3608 */
3609int local_memory_node(int node)
3610{
3611 struct zone *zone;
3612
3613 (void)first_zones_zonelist(node_zonelist(node, GFP_KERNEL),
3614 gfp_zone(GFP_KERNEL),
3615 NULL,
3616 &zone);
3617 return zone->node;
3618}
3619#endif
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003620
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621#else /* CONFIG_NUMA */
3622
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003623static void set_zonelist_order(void)
3624{
3625 current_zonelist_order = ZONELIST_ORDER_ZONE;
3626}
3627
3628static void build_zonelists(pg_data_t *pgdat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629{
Christoph Lameter19655d32006-09-25 23:31:19 -07003630 int node, local_node;
Mel Gorman54a6eb52008-04-28 02:12:16 -07003631 enum zone_type j;
3632 struct zonelist *zonelist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
3634 local_node = pgdat->node_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635
Mel Gorman54a6eb52008-04-28 02:12:16 -07003636 zonelist = &pgdat->node_zonelists[0];
3637 j = build_zonelists_node(pgdat, zonelist, 0, MAX_NR_ZONES - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638
Mel Gorman54a6eb52008-04-28 02:12:16 -07003639 /*
3640 * Now we build the zonelist so that it contains the zones
3641 * of all the other nodes.
3642 * We don't want to pressure a particular node, so when
3643 * building the zones for node N, we make sure that the
3644 * zones coming right after the local ones are those from
3645 * node N+1 (modulo N)
3646 */
3647 for (node = local_node + 1; node < MAX_NUMNODES; node++) {
3648 if (!node_online(node))
3649 continue;
3650 j = build_zonelists_node(NODE_DATA(node), zonelist, j,
3651 MAX_NR_ZONES - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 }
Mel Gorman54a6eb52008-04-28 02:12:16 -07003653 for (node = 0; node < local_node; node++) {
3654 if (!node_online(node))
3655 continue;
3656 j = build_zonelists_node(NODE_DATA(node), zonelist, j,
3657 MAX_NR_ZONES - 1);
3658 }
3659
Mel Gormandd1a2392008-04-28 02:12:17 -07003660 zonelist->_zonerefs[j].zone = NULL;
3661 zonelist->_zonerefs[j].zone_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662}
3663
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003664/* non-NUMA variant of zonelist performance cache - just NULL zlcache_ptr */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003665static void build_zonelist_cache(pg_data_t *pgdat)
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003666{
Mel Gorman54a6eb52008-04-28 02:12:16 -07003667 pgdat->node_zonelists[0].zlcache_ptr = NULL;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003668}
3669
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670#endif /* CONFIG_NUMA */
3671
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09003672/*
3673 * Boot pageset table. One per cpu which is going to be used for all
3674 * zones and all nodes. The parameters will be set in such a way
3675 * that an item put on a list will immediately be handed over to
3676 * the buddy list. This is safe since pageset manipulation is done
3677 * with interrupts disabled.
3678 *
3679 * The boot_pagesets must be kept even after bootup is complete for
3680 * unused processors and/or zones. They do play a role for bootstrapping
3681 * hotplugged processors.
3682 *
3683 * zoneinfo_show() and maybe other functions do
3684 * not check if the processor is online before following the pageset pointer.
3685 * Other parts of the kernel may not check if the zone is available.
3686 */
3687static void setup_pageset(struct per_cpu_pageset *p, unsigned long batch);
3688static DEFINE_PER_CPU(struct per_cpu_pageset, boot_pageset);
Haicheng Li1f522502010-05-24 14:32:51 -07003689static void setup_zone_pageset(struct zone *zone);
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09003690
Haicheng Li4eaf3f62010-05-24 14:32:52 -07003691/*
3692 * Global mutex to protect against size modification of zonelists
3693 * as well as to serialize pageset setup for the new populated zone.
3694 */
3695DEFINE_MUTEX(zonelists_mutex);
3696
Rusty Russell9b1a4d32008-07-28 12:16:30 -05003697/* return values int ....just for stop_machine() */
Jiang Liu4ed7e022012-07-31 16:43:35 -07003698static int __build_all_zonelists(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699{
Yasunori Goto68113782006-06-23 02:03:11 -07003700 int nid;
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09003701 int cpu;
Jiang Liu9adb62a2012-07-31 16:43:28 -07003702 pg_data_t *self = data;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003703
Bo Liu7f9cfb32009-08-18 14:11:19 -07003704#ifdef CONFIG_NUMA
3705 memset(node_load, 0, sizeof(node_load));
3706#endif
Jiang Liu9adb62a2012-07-31 16:43:28 -07003707
3708 if (self && !node_online(self->node_id)) {
3709 build_zonelists(self);
3710 build_zonelist_cache(self);
3711 }
3712
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003713 for_each_online_node(nid) {
Christoph Lameter7ea15302007-10-16 01:25:29 -07003714 pg_data_t *pgdat = NODE_DATA(nid);
3715
3716 build_zonelists(pgdat);
3717 build_zonelist_cache(pgdat);
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003718 }
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09003719
3720 /*
3721 * Initialize the boot_pagesets that are going to be used
3722 * for bootstrapping processors. The real pagesets for
3723 * each zone will be allocated later when the per cpu
3724 * allocator is available.
3725 *
3726 * boot_pagesets are used also for bootstrapping offline
3727 * cpus if the system is already booted because the pagesets
3728 * are needed to initialize allocators on a specific cpu too.
3729 * F.e. the percpu allocator needs the page allocator which
3730 * needs the percpu allocator in order to allocate its pagesets
3731 * (a chicken-egg dilemma).
3732 */
Lee Schermerhorn7aac7892010-05-26 14:45:00 -07003733 for_each_possible_cpu(cpu) {
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09003734 setup_pageset(&per_cpu(boot_pageset, cpu), 0);
3735
Lee Schermerhorn7aac7892010-05-26 14:45:00 -07003736#ifdef CONFIG_HAVE_MEMORYLESS_NODES
3737 /*
3738 * We now know the "local memory node" for each node--
3739 * i.e., the node of the first zone in the generic zonelist.
3740 * Set up numa_mem percpu variable for on-line cpus. During
3741 * boot, only the boot cpu should be on-line; we'll init the
3742 * secondary cpus' numa_mem as they come on-line. During
3743 * node/memory hotplug, we'll fixup all on-line cpus.
3744 */
3745 if (cpu_online(cpu))
3746 set_cpu_numa_mem(cpu, local_memory_node(cpu_to_node(cpu)));
3747#endif
3748 }
3749
Yasunori Goto68113782006-06-23 02:03:11 -07003750 return 0;
3751}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752
Haicheng Li4eaf3f62010-05-24 14:32:52 -07003753/*
3754 * Called with zonelists_mutex held always
3755 * unless system_state == SYSTEM_BOOTING.
3756 */
Jiang Liu9adb62a2012-07-31 16:43:28 -07003757void __ref build_all_zonelists(pg_data_t *pgdat, struct zone *zone)
Yasunori Goto68113782006-06-23 02:03:11 -07003758{
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003759 set_zonelist_order();
3760
Yasunori Goto68113782006-06-23 02:03:11 -07003761 if (system_state == SYSTEM_BOOTING) {
Randy Dunlap423b41d2006-09-27 01:50:12 -07003762 __build_all_zonelists(NULL);
Mel Gorman68ad8df2008-07-23 21:26:52 -07003763 mminit_verify_zonelist();
Yasunori Goto68113782006-06-23 02:03:11 -07003764 cpuset_init_current_mems_allowed();
3765 } else {
Simon Arlott183ff222007-10-20 01:27:18 +02003766 /* we have to stop all cpus to guarantee there is no user
Yasunori Goto68113782006-06-23 02:03:11 -07003767 of zonelist */
KAMEZAWA Hiroyukie9959f02010-11-24 12:57:09 -08003768#ifdef CONFIG_MEMORY_HOTPLUG
Jiang Liu9adb62a2012-07-31 16:43:28 -07003769 if (zone)
3770 setup_zone_pageset(zone);
KAMEZAWA Hiroyukie9959f02010-11-24 12:57:09 -08003771#endif
Jiang Liu9adb62a2012-07-31 16:43:28 -07003772 stop_machine(__build_all_zonelists, pgdat, NULL);
Yasunori Goto68113782006-06-23 02:03:11 -07003773 /* cpuset refresh routine should be here */
3774 }
Andrew Mortonbd1e22b2006-06-23 02:03:47 -07003775 vm_total_pages = nr_free_pagecache_pages();
Mel Gorman9ef9acb2007-10-16 01:25:54 -07003776 /*
3777 * Disable grouping by mobility if the number of pages in the
3778 * system is too low to allow the mechanism to work. It would be
3779 * more accurate, but expensive to check per-zone. This check is
3780 * made on memory-hotadd so a system can start with mobility
3781 * disabled and enable it later
3782 */
Mel Gormand9c23402007-10-16 01:26:01 -07003783 if (vm_total_pages < (pageblock_nr_pages * MIGRATE_TYPES))
Mel Gorman9ef9acb2007-10-16 01:25:54 -07003784 page_group_by_mobility_disabled = 1;
3785 else
3786 page_group_by_mobility_disabled = 0;
3787
3788 printk("Built %i zonelists in %s order, mobility grouping %s. "
3789 "Total pages: %ld\n",
Christoph Lameter62bc62a2009-06-16 15:32:15 -07003790 nr_online_nodes,
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003791 zonelist_order_name[current_zonelist_order],
Mel Gorman9ef9acb2007-10-16 01:25:54 -07003792 page_group_by_mobility_disabled ? "off" : "on",
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003793 vm_total_pages);
3794#ifdef CONFIG_NUMA
3795 printk("Policy zone: %s\n", zone_names[policy_zone]);
3796#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797}
3798
3799/*
3800 * Helper functions to size the waitqueue hash table.
3801 * Essentially these want to choose hash table sizes sufficiently
3802 * large so that collisions trying to wait on pages are rare.
3803 * But in fact, the number of active page waitqueues on typical
3804 * systems is ridiculously low, less than 200. So this is even
3805 * conservative, even though it seems large.
3806 *
3807 * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
3808 * waitqueues, i.e. the size of the waitq table given the number of pages.
3809 */
3810#define PAGES_PER_WAITQUEUE 256
3811
Yasunori Gotocca448f2006-06-23 02:03:10 -07003812#ifndef CONFIG_MEMORY_HOTPLUG
Yasunori Goto02b694d2006-06-23 02:03:08 -07003813static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814{
3815 unsigned long size = 1;
3816
3817 pages /= PAGES_PER_WAITQUEUE;
3818
3819 while (size < pages)
3820 size <<= 1;
3821
3822 /*
3823 * Once we have dozens or even hundreds of threads sleeping
3824 * on IO we've got bigger problems than wait queue collision.
3825 * Limit the size of the wait table to a reasonable size.
3826 */
3827 size = min(size, 4096UL);
3828
3829 return max(size, 4UL);
3830}
Yasunori Gotocca448f2006-06-23 02:03:10 -07003831#else
3832/*
3833 * A zone's size might be changed by hot-add, so it is not possible to determine
3834 * a suitable size for its wait_table. So we use the maximum size now.
3835 *
3836 * The max wait table size = 4096 x sizeof(wait_queue_head_t). ie:
3837 *
3838 * i386 (preemption config) : 4096 x 16 = 64Kbyte.
3839 * ia64, x86-64 (no preemption): 4096 x 20 = 80Kbyte.
3840 * ia64, x86-64 (preemption) : 4096 x 24 = 96Kbyte.
3841 *
3842 * The maximum entries are prepared when a zone's memory is (512K + 256) pages
3843 * or more by the traditional way. (See above). It equals:
3844 *
3845 * i386, x86-64, powerpc(4K page size) : = ( 2G + 1M)byte.
3846 * ia64(16K page size) : = ( 8G + 4M)byte.
3847 * powerpc (64K page size) : = (32G +16M)byte.
3848 */
3849static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
3850{
3851 return 4096UL;
3852}
3853#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854
3855/*
3856 * This is an integer logarithm so that shifts can be used later
3857 * to extract the more random high bits from the multiplicative
3858 * hash function before the remainder is taken.
3859 */
3860static inline unsigned long wait_table_bits(unsigned long size)
3861{
3862 return ffz(~size);
3863}
3864
3865#define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
3866
Mel Gorman56fd56b2007-10-16 01:25:58 -07003867/*
Arve Hjønnevåg6d3163c2011-05-24 17:12:24 -07003868 * Check if a pageblock contains reserved pages
3869 */
3870static int pageblock_is_reserved(unsigned long start_pfn, unsigned long end_pfn)
3871{
3872 unsigned long pfn;
3873
3874 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
3875 if (!pfn_valid_within(pfn) || PageReserved(pfn_to_page(pfn)))
3876 return 1;
3877 }
3878 return 0;
3879}
3880
3881/*
Mel Gormand9c23402007-10-16 01:26:01 -07003882 * Mark a number of pageblocks as MIGRATE_RESERVE. The number
Mel Gorman41858962009-06-16 15:32:12 -07003883 * of blocks reserved is based on min_wmark_pages(zone). The memory within
3884 * the reserve will tend to store contiguous free pages. Setting min_free_kbytes
Mel Gorman56fd56b2007-10-16 01:25:58 -07003885 * higher will lead to a bigger reserve which will get freed as contiguous
3886 * blocks as reclaim kicks in
3887 */
3888static void setup_zone_migrate_reserve(struct zone *zone)
3889{
Arve Hjønnevåg6d3163c2011-05-24 17:12:24 -07003890 unsigned long start_pfn, pfn, end_pfn, block_end_pfn;
Mel Gorman56fd56b2007-10-16 01:25:58 -07003891 struct page *page;
Mel Gorman78986a62009-09-21 17:03:02 -07003892 unsigned long block_migratetype;
3893 int reserve;
Mel Gorman56fd56b2007-10-16 01:25:58 -07003894
Michal Hockod02156382011-12-08 14:34:27 -08003895 /*
3896 * Get the start pfn, end pfn and the number of blocks to reserve
3897 * We have to be careful to be aligned to pageblock_nr_pages to
3898 * make sure that we always check pfn_valid for the first page in
3899 * the block.
3900 */
Mel Gorman56fd56b2007-10-16 01:25:58 -07003901 start_pfn = zone->zone_start_pfn;
Cody P Schafer108bcc92013-02-22 16:35:23 -08003902 end_pfn = zone_end_pfn(zone);
Michal Hockod02156382011-12-08 14:34:27 -08003903 start_pfn = roundup(start_pfn, pageblock_nr_pages);
Mel Gorman41858962009-06-16 15:32:12 -07003904 reserve = roundup(min_wmark_pages(zone), pageblock_nr_pages) >>
Mel Gormand9c23402007-10-16 01:26:01 -07003905 pageblock_order;
Mel Gorman56fd56b2007-10-16 01:25:58 -07003906
Mel Gorman78986a62009-09-21 17:03:02 -07003907 /*
3908 * Reserve blocks are generally in place to help high-order atomic
3909 * allocations that are short-lived. A min_free_kbytes value that
3910 * would result in more than 2 reserve blocks for atomic allocations
3911 * is assumed to be in place to help anti-fragmentation for the
3912 * future allocation of hugepages at runtime.
3913 */
3914 reserve = min(2, reserve);
3915
Mel Gormand9c23402007-10-16 01:26:01 -07003916 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
Mel Gorman56fd56b2007-10-16 01:25:58 -07003917 if (!pfn_valid(pfn))
3918 continue;
3919 page = pfn_to_page(pfn);
3920
Adam Litke344c7902008-09-02 14:35:38 -07003921 /* Watch out for overlapping nodes */
3922 if (page_to_nid(page) != zone_to_nid(zone))
3923 continue;
3924
Mel Gorman56fd56b2007-10-16 01:25:58 -07003925 block_migratetype = get_pageblock_migratetype(page);
3926
Mel Gorman938929f2012-01-10 15:07:14 -08003927 /* Only test what is necessary when the reserves are not met */
3928 if (reserve > 0) {
3929 /*
3930 * Blocks with reserved pages will never free, skip
3931 * them.
3932 */
3933 block_end_pfn = min(pfn + pageblock_nr_pages, end_pfn);
3934 if (pageblock_is_reserved(pfn, block_end_pfn))
3935 continue;
Mel Gorman56fd56b2007-10-16 01:25:58 -07003936
Mel Gorman938929f2012-01-10 15:07:14 -08003937 /* If this block is reserved, account for it */
3938 if (block_migratetype == MIGRATE_RESERVE) {
3939 reserve--;
3940 continue;
3941 }
3942
3943 /* Suitable for reserving if this block is movable */
3944 if (block_migratetype == MIGRATE_MOVABLE) {
3945 set_pageblock_migratetype(page,
3946 MIGRATE_RESERVE);
3947 move_freepages_block(zone, page,
3948 MIGRATE_RESERVE);
3949 reserve--;
3950 continue;
3951 }
Mel Gorman56fd56b2007-10-16 01:25:58 -07003952 }
3953
3954 /*
3955 * If the reserve is met and this is a previous reserved block,
3956 * take it back
3957 */
3958 if (block_migratetype == MIGRATE_RESERVE) {
3959 set_pageblock_migratetype(page, MIGRATE_MOVABLE);
3960 move_freepages_block(zone, page, MIGRATE_MOVABLE);
3961 }
3962 }
3963}
Mel Gormanac0e5b72007-10-16 01:25:58 -07003964
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965/*
3966 * Initially all pages are reserved - free ones are freed
3967 * up by free_all_bootmem() once the early boot process is
3968 * done. Non-atomic initialization, single-pass.
3969 */
Matt Tolentinoc09b4242006-01-17 07:03:44 +01003970void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
Dave Hansena2f3aa022007-01-10 23:15:30 -08003971 unsigned long start_pfn, enum memmap_context context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 struct page *page;
Andy Whitcroft29751f62005-06-23 00:08:00 -07003974 unsigned long end_pfn = start_pfn + size;
3975 unsigned long pfn;
KAMEZAWA Hiroyuki86051ca2008-04-29 00:58:21 -07003976 struct zone *z;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977
Hugh Dickins22b31ee2009-01-06 14:40:09 -08003978 if (highest_memmap_pfn < end_pfn - 1)
3979 highest_memmap_pfn = end_pfn - 1;
3980
KAMEZAWA Hiroyuki86051ca2008-04-29 00:58:21 -07003981 z = &NODE_DATA(nid)->node_zones[zone];
Greg Ungerercbe8dd42006-01-12 01:05:24 -08003982 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
Dave Hansena2f3aa022007-01-10 23:15:30 -08003983 /*
3984 * There can be holes in boot-time mem_map[]s
3985 * handed to this function. They do not
3986 * exist on hotplugged memory.
3987 */
3988 if (context == MEMMAP_EARLY) {
3989 if (!early_pfn_valid(pfn))
3990 continue;
3991 if (!early_pfn_in_nid(pfn, nid))
3992 continue;
3993 }
Andy Whitcroftd41dee32005-06-23 00:07:54 -07003994 page = pfn_to_page(pfn);
3995 set_page_links(page, zone, nid, pfn);
Mel Gorman708614e2008-07-23 21:26:51 -07003996 mminit_verify_page_links(page, zone, nid, pfn);
Nick Piggin7835e982006-03-22 00:08:40 -08003997 init_page_count(page);
Mel Gorman22b751c2013-02-22 16:34:59 -08003998 page_mapcount_reset(page);
3999 page_nid_reset_last(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 SetPageReserved(page);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07004001 /*
4002 * Mark the block movable so that blocks are reserved for
4003 * movable at startup. This will force kernel allocations
4004 * to reserve their blocks rather than leaking throughout
4005 * the address space during boot when many long-lived
Mel Gorman56fd56b2007-10-16 01:25:58 -07004006 * kernel allocations are made. Later some blocks near
4007 * the start are marked MIGRATE_RESERVE by
4008 * setup_zone_migrate_reserve()
KAMEZAWA Hiroyuki86051ca2008-04-29 00:58:21 -07004009 *
4010 * bitmap is created for zone's valid pfn range. but memmap
4011 * can be created for invalid pages (for alignment)
4012 * check here not to call set_pageblock_migratetype() against
4013 * pfn out of zone.
Mel Gormanb2a0ac82007-10-16 01:25:48 -07004014 */
KAMEZAWA Hiroyuki86051ca2008-04-29 00:58:21 -07004015 if ((z->zone_start_pfn <= pfn)
Cody P Schafer108bcc92013-02-22 16:35:23 -08004016 && (pfn < zone_end_pfn(z))
KAMEZAWA Hiroyuki86051ca2008-04-29 00:58:21 -07004017 && !(pfn & (pageblock_nr_pages - 1)))
Mel Gorman56fd56b2007-10-16 01:25:58 -07004018 set_pageblock_migratetype(page, MIGRATE_MOVABLE);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07004019
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 INIT_LIST_HEAD(&page->lru);
4021#ifdef WANT_PAGE_VIRTUAL
4022 /* The shift won't overflow because ZONE_NORMAL is below 4G. */
4023 if (!is_highmem_idx(zone))
Bob Picco3212c6b2005-06-27 14:36:28 -07004024 set_page_address(page, __va(pfn << PAGE_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 }
4027}
4028
Andi Kleen1e548de2008-02-04 22:29:26 -08004029static void __meminit zone_init_free_lists(struct zone *zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030{
Mel Gormanb2a0ac82007-10-16 01:25:48 -07004031 int order, t;
4032 for_each_migratetype_order(order, t) {
4033 INIT_LIST_HEAD(&zone->free_area[order].free_list[t]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 zone->free_area[order].nr_free = 0;
4035 }
4036}
4037
4038#ifndef __HAVE_ARCH_MEMMAP_INIT
4039#define memmap_init(size, nid, zone, start_pfn) \
Dave Hansena2f3aa022007-01-10 23:15:30 -08004040 memmap_init_zone((size), (nid), (zone), (start_pfn), MEMMAP_EARLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041#endif
4042
Jiang Liu4ed7e022012-07-31 16:43:35 -07004043static int __meminit zone_batchsize(struct zone *zone)
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07004044{
David Howells3a6be872009-05-06 16:03:03 -07004045#ifdef CONFIG_MMU
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07004046 int batch;
4047
4048 /*
4049 * The per-cpu-pages pools are set to around 1000th of the
Seth, Rohitba56e912005-10-29 18:15:47 -07004050 * size of the zone. But no more than 1/2 of a meg.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07004051 *
4052 * OK, so we don't know how big the cache is. So guess.
4053 */
Jiang Liub40da042013-02-22 16:33:52 -08004054 batch = zone->managed_pages / 1024;
Seth, Rohitba56e912005-10-29 18:15:47 -07004055 if (batch * PAGE_SIZE > 512 * 1024)
4056 batch = (512 * 1024) / PAGE_SIZE;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07004057 batch /= 4; /* We effectively *= 4 below */
4058 if (batch < 1)
4059 batch = 1;
4060
4061 /*
Nick Piggin0ceaacc2005-12-04 13:55:25 +11004062 * Clamp the batch to a 2^n - 1 value. Having a power
4063 * of 2 value was found to be more likely to have
4064 * suboptimal cache aliasing properties in some cases.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07004065 *
Nick Piggin0ceaacc2005-12-04 13:55:25 +11004066 * For example if 2 tasks are alternately allocating
4067 * batches of pages, one task can end up with a lot
4068 * of pages of one half of the possible page colors
4069 * and the other with pages of the other colors.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07004070 */
David Howells91552032009-05-06 16:03:02 -07004071 batch = rounddown_pow_of_two(batch + batch/2) - 1;
Seth, Rohitba56e912005-10-29 18:15:47 -07004072
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07004073 return batch;
David Howells3a6be872009-05-06 16:03:03 -07004074
4075#else
4076 /* The deferral and batching of frees should be suppressed under NOMMU
4077 * conditions.
4078 *
4079 * The problem is that NOMMU needs to be able to allocate large chunks
4080 * of contiguous memory as there's no hardware page translation to
4081 * assemble apparent contiguous memory from discontiguous pages.
4082 *
4083 * Queueing large contiguous runs of pages for batching, however,
4084 * causes the pages to actually be freed in smaller chunks. As there
4085 * can be a significant delay between the individual batches being
4086 * recycled, this leads to the once large chunks of space being
4087 * fragmented and becoming unavailable for high-order allocations.
4088 */
4089 return 0;
4090#endif
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07004091}
4092
Adrian Bunkb69a7282008-07-23 21:28:12 -07004093static void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
Christoph Lameter2caaad42005-06-21 17:15:00 -07004094{
4095 struct per_cpu_pages *pcp;
Mel Gorman5f8dcc22009-09-21 17:03:19 -07004096 int migratetype;
Christoph Lameter2caaad42005-06-21 17:15:00 -07004097
Magnus Damm1c6fe942005-10-26 01:58:59 -07004098 memset(p, 0, sizeof(*p));
4099
Christoph Lameter3dfa5722008-02-04 22:29:19 -08004100 pcp = &p->pcp;
Christoph Lameter2caaad42005-06-21 17:15:00 -07004101 pcp->count = 0;
Christoph Lameter2caaad42005-06-21 17:15:00 -07004102 pcp->high = 6 * batch;
4103 pcp->batch = max(1UL, 1 * batch);
Mel Gorman5f8dcc22009-09-21 17:03:19 -07004104 for (migratetype = 0; migratetype < MIGRATE_PCPTYPES; migratetype++)
4105 INIT_LIST_HEAD(&pcp->lists[migratetype]);
Christoph Lameter2caaad42005-06-21 17:15:00 -07004106}
4107
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08004108/*
4109 * setup_pagelist_highmark() sets the high water mark for hot per_cpu_pagelist
4110 * to the value high for the pageset p.
4111 */
4112
4113static void setup_pagelist_highmark(struct per_cpu_pageset *p,
4114 unsigned long high)
4115{
4116 struct per_cpu_pages *pcp;
4117
Christoph Lameter3dfa5722008-02-04 22:29:19 -08004118 pcp = &p->pcp;
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08004119 pcp->high = high;
4120 pcp->batch = max(1UL, high/4);
4121 if ((high/4) > (PAGE_SHIFT * 8))
4122 pcp->batch = PAGE_SHIFT * 8;
4123}
4124
Jiang Liu4ed7e022012-07-31 16:43:35 -07004125static void __meminit setup_zone_pageset(struct zone *zone)
Wu Fengguang319774e2010-05-24 14:32:49 -07004126{
4127 int cpu;
4128
4129 zone->pageset = alloc_percpu(struct per_cpu_pageset);
4130
4131 for_each_possible_cpu(cpu) {
4132 struct per_cpu_pageset *pcp = per_cpu_ptr(zone->pageset, cpu);
4133
4134 setup_pageset(pcp, zone_batchsize(zone));
4135
4136 if (percpu_pagelist_fraction)
4137 setup_pagelist_highmark(pcp,
Jiang Liub40da042013-02-22 16:33:52 -08004138 (zone->managed_pages /
Wu Fengguang319774e2010-05-24 14:32:49 -07004139 percpu_pagelist_fraction));
4140 }
4141}
4142
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07004143/*
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09004144 * Allocate per cpu pagesets and initialize them.
4145 * Before this call only boot pagesets were available.
Christoph Lameter2caaad42005-06-21 17:15:00 -07004146 */
Al Viro78d99552005-12-15 09:18:25 +00004147void __init setup_per_cpu_pageset(void)
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07004148{
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09004149 struct zone *zone;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07004150
Wu Fengguang319774e2010-05-24 14:32:49 -07004151 for_each_populated_zone(zone)
4152 setup_zone_pageset(zone);
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07004153}
4154
Sam Ravnborg577a32f2007-05-17 23:29:25 +02004155static noinline __init_refok
Yasunori Gotocca448f2006-06-23 02:03:10 -07004156int zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages)
Dave Hansened8ece22005-10-29 18:16:50 -07004157{
4158 int i;
4159 struct pglist_data *pgdat = zone->zone_pgdat;
Yasunori Gotocca448f2006-06-23 02:03:10 -07004160 size_t alloc_size;
Dave Hansened8ece22005-10-29 18:16:50 -07004161
4162 /*
4163 * The per-page waitqueue mechanism uses hashed waitqueues
4164 * per zone.
4165 */
Yasunori Goto02b694d2006-06-23 02:03:08 -07004166 zone->wait_table_hash_nr_entries =
4167 wait_table_hash_nr_entries(zone_size_pages);
4168 zone->wait_table_bits =
4169 wait_table_bits(zone->wait_table_hash_nr_entries);
Yasunori Gotocca448f2006-06-23 02:03:10 -07004170 alloc_size = zone->wait_table_hash_nr_entries
4171 * sizeof(wait_queue_head_t);
4172
Heiko Carstenscd94b9d2008-05-23 13:04:52 -07004173 if (!slab_is_available()) {
Yasunori Gotocca448f2006-06-23 02:03:10 -07004174 zone->wait_table = (wait_queue_head_t *)
Yinghai Lu8f389a992011-05-11 15:13:32 -07004175 alloc_bootmem_node_nopanic(pgdat, alloc_size);
Yasunori Gotocca448f2006-06-23 02:03:10 -07004176 } else {
4177 /*
4178 * This case means that a zone whose size was 0 gets new memory
4179 * via memory hot-add.
4180 * But it may be the case that a new node was hot-added. In
4181 * this case vmalloc() will not be able to use this new node's
4182 * memory - this wait_table must be initialized to use this new
4183 * node itself as well.
4184 * To use this new node's memory, further consideration will be
4185 * necessary.
4186 */
Jesper Juhl8691f3a2007-10-16 01:24:49 -07004187 zone->wait_table = vmalloc(alloc_size);
Yasunori Gotocca448f2006-06-23 02:03:10 -07004188 }
4189 if (!zone->wait_table)
4190 return -ENOMEM;
Dave Hansened8ece22005-10-29 18:16:50 -07004191
Yasunori Goto02b694d2006-06-23 02:03:08 -07004192 for(i = 0; i < zone->wait_table_hash_nr_entries; ++i)
Dave Hansened8ece22005-10-29 18:16:50 -07004193 init_waitqueue_head(zone->wait_table + i);
Yasunori Gotocca448f2006-06-23 02:03:10 -07004194
4195 return 0;
Dave Hansened8ece22005-10-29 18:16:50 -07004196}
4197
Matt Tolentinoc09b4242006-01-17 07:03:44 +01004198static __meminit void zone_pcp_init(struct zone *zone)
Dave Hansened8ece22005-10-29 18:16:50 -07004199{
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09004200 /*
4201 * per cpu subsystem is not up at this point. The following code
4202 * relies on the ability of the linker to provide the
4203 * offset of a (static) per cpu variable into the per cpu area.
4204 */
4205 zone->pageset = &boot_pageset;
Dave Hansened8ece22005-10-29 18:16:50 -07004206
Anton Blanchardf5335c02006-03-25 03:06:49 -08004207 if (zone->present_pages)
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09004208 printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%u\n",
4209 zone->name, zone->present_pages,
4210 zone_batchsize(zone));
Dave Hansened8ece22005-10-29 18:16:50 -07004211}
4212
Jiang Liu4ed7e022012-07-31 16:43:35 -07004213int __meminit init_currently_empty_zone(struct zone *zone,
Yasunori Goto718127c2006-06-23 02:03:10 -07004214 unsigned long zone_start_pfn,
Dave Hansena2f3aa022007-01-10 23:15:30 -08004215 unsigned long size,
4216 enum memmap_context context)
Dave Hansened8ece22005-10-29 18:16:50 -07004217{
4218 struct pglist_data *pgdat = zone->zone_pgdat;
Yasunori Gotocca448f2006-06-23 02:03:10 -07004219 int ret;
4220 ret = zone_wait_table_init(zone, size);
4221 if (ret)
4222 return ret;
Dave Hansened8ece22005-10-29 18:16:50 -07004223 pgdat->nr_zones = zone_idx(zone) + 1;
4224
Dave Hansened8ece22005-10-29 18:16:50 -07004225 zone->zone_start_pfn = zone_start_pfn;
4226
Mel Gorman708614e2008-07-23 21:26:51 -07004227 mminit_dprintk(MMINIT_TRACE, "memmap_init",
4228 "Initialising map node %d zone %lu pfns %lu -> %lu\n",
4229 pgdat->node_id,
4230 (unsigned long)zone_idx(zone),
4231 zone_start_pfn, (zone_start_pfn + size));
4232
Andi Kleen1e548de2008-02-04 22:29:26 -08004233 zone_init_free_lists(zone);
Yasunori Goto718127c2006-06-23 02:03:10 -07004234
4235 return 0;
Dave Hansened8ece22005-10-29 18:16:50 -07004236}
4237
Tejun Heo0ee332c2011-12-08 10:22:09 -08004238#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Mel Gormanc7132162006-09-27 01:49:43 -07004239#ifndef CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID
4240/*
4241 * Required by SPARSEMEM. Given a PFN, return what node the PFN is on.
4242 * Architectures may implement their own version but if add_active_range()
4243 * was used and there are no special requirements, this is a convenient
4244 * alternative
4245 */
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -08004246int __meminit __early_pfn_to_nid(unsigned long pfn)
Mel Gormanc7132162006-09-27 01:49:43 -07004247{
Tejun Heoc13291a2011-07-12 10:46:30 +02004248 unsigned long start_pfn, end_pfn;
4249 int i, nid;
Russ Anderson7c243c72013-04-29 15:07:59 -07004250 /*
4251 * NOTE: The following SMP-unsafe globals are only used early in boot
4252 * when the kernel is running single-threaded.
4253 */
4254 static unsigned long __meminitdata last_start_pfn, last_end_pfn;
4255 static int __meminitdata last_nid;
4256
4257 if (last_start_pfn <= pfn && pfn < last_end_pfn)
4258 return last_nid;
Mel Gormanc7132162006-09-27 01:49:43 -07004259
Tejun Heoc13291a2011-07-12 10:46:30 +02004260 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid)
Russ Anderson7c243c72013-04-29 15:07:59 -07004261 if (start_pfn <= pfn && pfn < end_pfn) {
4262 last_start_pfn = start_pfn;
4263 last_end_pfn = end_pfn;
4264 last_nid = nid;
Tejun Heoc13291a2011-07-12 10:46:30 +02004265 return nid;
Russ Anderson7c243c72013-04-29 15:07:59 -07004266 }
KAMEZAWA Hiroyukicc2559b2009-02-18 14:48:33 -08004267 /* This is a memory hole */
4268 return -1;
Mel Gormanc7132162006-09-27 01:49:43 -07004269}
4270#endif /* CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID */
4271
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -08004272int __meminit early_pfn_to_nid(unsigned long pfn)
4273{
KAMEZAWA Hiroyukicc2559b2009-02-18 14:48:33 -08004274 int nid;
4275
4276 nid = __early_pfn_to_nid(pfn);
4277 if (nid >= 0)
4278 return nid;
4279 /* just returns 0 */
4280 return 0;
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -08004281}
4282
KAMEZAWA Hiroyukicc2559b2009-02-18 14:48:33 -08004283#ifdef CONFIG_NODES_SPAN_OTHER_NODES
4284bool __meminit early_pfn_in_nid(unsigned long pfn, int node)
4285{
4286 int nid;
4287
4288 nid = __early_pfn_to_nid(pfn);
4289 if (nid >= 0 && nid != node)
4290 return false;
4291 return true;
4292}
4293#endif
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -08004294
Mel Gormanc7132162006-09-27 01:49:43 -07004295/**
4296 * free_bootmem_with_active_regions - Call free_bootmem_node for each active range
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004297 * @nid: The node to free memory on. If MAX_NUMNODES, all nodes are freed.
4298 * @max_low_pfn: The highest PFN that will be passed to free_bootmem_node
Mel Gormanc7132162006-09-27 01:49:43 -07004299 *
4300 * If an architecture guarantees that all ranges registered with
4301 * add_active_ranges() contain no holes and may be freed, this
4302 * this function may be used instead of calling free_bootmem() manually.
4303 */
Tejun Heoc13291a2011-07-12 10:46:30 +02004304void __init free_bootmem_with_active_regions(int nid, unsigned long max_low_pfn)
Mel Gormanc7132162006-09-27 01:49:43 -07004305{
Tejun Heoc13291a2011-07-12 10:46:30 +02004306 unsigned long start_pfn, end_pfn;
4307 int i, this_nid;
Mel Gormanc7132162006-09-27 01:49:43 -07004308
Tejun Heoc13291a2011-07-12 10:46:30 +02004309 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, &this_nid) {
4310 start_pfn = min(start_pfn, max_low_pfn);
4311 end_pfn = min(end_pfn, max_low_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07004312
Tejun Heoc13291a2011-07-12 10:46:30 +02004313 if (start_pfn < end_pfn)
4314 free_bootmem_node(NODE_DATA(this_nid),
4315 PFN_PHYS(start_pfn),
4316 (end_pfn - start_pfn) << PAGE_SHIFT);
Mel Gormanc7132162006-09-27 01:49:43 -07004317 }
4318}
4319
4320/**
4321 * sparse_memory_present_with_active_regions - Call memory_present for each active range
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004322 * @nid: The node to call memory_present for. If MAX_NUMNODES, all nodes will be used.
Mel Gormanc7132162006-09-27 01:49:43 -07004323 *
4324 * If an architecture guarantees that all ranges registered with
4325 * add_active_ranges() contain no holes and may be freed, this
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004326 * function may be used instead of calling memory_present() manually.
Mel Gormanc7132162006-09-27 01:49:43 -07004327 */
4328void __init sparse_memory_present_with_active_regions(int nid)
4329{
Tejun Heoc13291a2011-07-12 10:46:30 +02004330 unsigned long start_pfn, end_pfn;
4331 int i, this_nid;
Mel Gormanc7132162006-09-27 01:49:43 -07004332
Tejun Heoc13291a2011-07-12 10:46:30 +02004333 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, &this_nid)
4334 memory_present(this_nid, start_pfn, end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07004335}
4336
4337/**
4338 * get_pfn_range_for_nid - Return the start and end page frames for a node
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004339 * @nid: The nid to return the range for. If MAX_NUMNODES, the min and max PFN are returned.
4340 * @start_pfn: Passed by reference. On return, it will have the node start_pfn.
4341 * @end_pfn: Passed by reference. On return, it will have the node end_pfn.
Mel Gormanc7132162006-09-27 01:49:43 -07004342 *
4343 * It returns the start and end page frame of a node based on information
4344 * provided by an arch calling add_active_range(). If called for a node
4345 * with no available memory, a warning is printed and the start and end
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004346 * PFNs will be 0.
Mel Gormanc7132162006-09-27 01:49:43 -07004347 */
Yasunori Gotoa3142c82007-05-08 00:23:07 -07004348void __meminit get_pfn_range_for_nid(unsigned int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07004349 unsigned long *start_pfn, unsigned long *end_pfn)
4350{
Tejun Heoc13291a2011-07-12 10:46:30 +02004351 unsigned long this_start_pfn, this_end_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07004352 int i;
Tejun Heoc13291a2011-07-12 10:46:30 +02004353
Mel Gormanc7132162006-09-27 01:49:43 -07004354 *start_pfn = -1UL;
4355 *end_pfn = 0;
4356
Tejun Heoc13291a2011-07-12 10:46:30 +02004357 for_each_mem_pfn_range(i, nid, &this_start_pfn, &this_end_pfn, NULL) {
4358 *start_pfn = min(*start_pfn, this_start_pfn);
4359 *end_pfn = max(*end_pfn, this_end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07004360 }
4361
Christoph Lameter633c0662007-10-16 01:25:37 -07004362 if (*start_pfn == -1UL)
Mel Gormanc7132162006-09-27 01:49:43 -07004363 *start_pfn = 0;
Mel Gormanc7132162006-09-27 01:49:43 -07004364}
4365
4366/*
Mel Gorman2a1e2742007-07-17 04:03:12 -07004367 * This finds a zone that can be used for ZONE_MOVABLE pages. The
4368 * assumption is made that zones within a node are ordered in monotonic
4369 * increasing memory addresses so that the "highest" populated zone is used
4370 */
Adrian Bunkb69a7282008-07-23 21:28:12 -07004371static void __init find_usable_zone_for_movable(void)
Mel Gorman2a1e2742007-07-17 04:03:12 -07004372{
4373 int zone_index;
4374 for (zone_index = MAX_NR_ZONES - 1; zone_index >= 0; zone_index--) {
4375 if (zone_index == ZONE_MOVABLE)
4376 continue;
4377
4378 if (arch_zone_highest_possible_pfn[zone_index] >
4379 arch_zone_lowest_possible_pfn[zone_index])
4380 break;
4381 }
4382
4383 VM_BUG_ON(zone_index == -1);
4384 movable_zone = zone_index;
4385}
4386
4387/*
4388 * The zone ranges provided by the architecture do not include ZONE_MOVABLE
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004389 * because it is sized independent of architecture. Unlike the other zones,
Mel Gorman2a1e2742007-07-17 04:03:12 -07004390 * the starting point for ZONE_MOVABLE is not fixed. It may be different
4391 * in each node depending on the size of each node and how evenly kernelcore
4392 * is distributed. This helper function adjusts the zone ranges
4393 * provided by the architecture for a given node by using the end of the
4394 * highest usable zone for ZONE_MOVABLE. This preserves the assumption that
4395 * zones within a node are in order of monotonic increases memory addresses
4396 */
Adrian Bunkb69a7282008-07-23 21:28:12 -07004397static void __meminit adjust_zone_range_for_zone_movable(int nid,
Mel Gorman2a1e2742007-07-17 04:03:12 -07004398 unsigned long zone_type,
4399 unsigned long node_start_pfn,
4400 unsigned long node_end_pfn,
4401 unsigned long *zone_start_pfn,
4402 unsigned long *zone_end_pfn)
4403{
4404 /* Only adjust if ZONE_MOVABLE is on this node */
4405 if (zone_movable_pfn[nid]) {
4406 /* Size ZONE_MOVABLE */
4407 if (zone_type == ZONE_MOVABLE) {
4408 *zone_start_pfn = zone_movable_pfn[nid];
4409 *zone_end_pfn = min(node_end_pfn,
4410 arch_zone_highest_possible_pfn[movable_zone]);
4411
4412 /* Adjust for ZONE_MOVABLE starting within this range */
4413 } else if (*zone_start_pfn < zone_movable_pfn[nid] &&
4414 *zone_end_pfn > zone_movable_pfn[nid]) {
4415 *zone_end_pfn = zone_movable_pfn[nid];
4416
4417 /* Check if this whole range is within ZONE_MOVABLE */
4418 } else if (*zone_start_pfn >= zone_movable_pfn[nid])
4419 *zone_start_pfn = *zone_end_pfn;
4420 }
4421}
4422
4423/*
Mel Gormanc7132162006-09-27 01:49:43 -07004424 * Return the number of pages a zone spans in a node, including holes
4425 * present_pages = zone_spanned_pages_in_node() - zone_absent_pages_in_node()
4426 */
Paul Mundt6ea6e682007-07-15 23:38:20 -07004427static unsigned long __meminit zone_spanned_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07004428 unsigned long zone_type,
4429 unsigned long *ignored)
4430{
4431 unsigned long node_start_pfn, node_end_pfn;
4432 unsigned long zone_start_pfn, zone_end_pfn;
4433
4434 /* Get the start and end of the node and zone */
4435 get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
4436 zone_start_pfn = arch_zone_lowest_possible_pfn[zone_type];
4437 zone_end_pfn = arch_zone_highest_possible_pfn[zone_type];
Mel Gorman2a1e2742007-07-17 04:03:12 -07004438 adjust_zone_range_for_zone_movable(nid, zone_type,
4439 node_start_pfn, node_end_pfn,
4440 &zone_start_pfn, &zone_end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07004441
4442 /* Check that this node has pages within the zone's required range */
4443 if (zone_end_pfn < node_start_pfn || zone_start_pfn > node_end_pfn)
4444 return 0;
4445
4446 /* Move the zone boundaries inside the node if necessary */
4447 zone_end_pfn = min(zone_end_pfn, node_end_pfn);
4448 zone_start_pfn = max(zone_start_pfn, node_start_pfn);
4449
4450 /* Return the spanned pages */
4451 return zone_end_pfn - zone_start_pfn;
4452}
4453
4454/*
4455 * Return the number of holes in a range on a node. If nid is MAX_NUMNODES,
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004456 * then all holes in the requested range will be accounted for.
Mel Gormanc7132162006-09-27 01:49:43 -07004457 */
Yinghai Lu32996252009-12-15 17:59:02 -08004458unsigned long __meminit __absent_pages_in_range(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07004459 unsigned long range_start_pfn,
4460 unsigned long range_end_pfn)
4461{
Tejun Heo96e907d2011-07-12 10:46:29 +02004462 unsigned long nr_absent = range_end_pfn - range_start_pfn;
4463 unsigned long start_pfn, end_pfn;
4464 int i;
Mel Gormanc7132162006-09-27 01:49:43 -07004465
Tejun Heo96e907d2011-07-12 10:46:29 +02004466 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
4467 start_pfn = clamp(start_pfn, range_start_pfn, range_end_pfn);
4468 end_pfn = clamp(end_pfn, range_start_pfn, range_end_pfn);
4469 nr_absent -= end_pfn - start_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07004470 }
Tejun Heo96e907d2011-07-12 10:46:29 +02004471 return nr_absent;
Mel Gormanc7132162006-09-27 01:49:43 -07004472}
4473
4474/**
4475 * absent_pages_in_range - Return number of page frames in holes within a range
4476 * @start_pfn: The start PFN to start searching for holes
4477 * @end_pfn: The end PFN to stop searching for holes
4478 *
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004479 * It returns the number of pages frames in memory holes within a range.
Mel Gormanc7132162006-09-27 01:49:43 -07004480 */
4481unsigned long __init absent_pages_in_range(unsigned long start_pfn,
4482 unsigned long end_pfn)
4483{
4484 return __absent_pages_in_range(MAX_NUMNODES, start_pfn, end_pfn);
4485}
4486
4487/* Return the number of page frames in holes in a zone on a node */
Paul Mundt6ea6e682007-07-15 23:38:20 -07004488static unsigned long __meminit zone_absent_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07004489 unsigned long zone_type,
4490 unsigned long *ignored)
4491{
Tejun Heo96e907d2011-07-12 10:46:29 +02004492 unsigned long zone_low = arch_zone_lowest_possible_pfn[zone_type];
4493 unsigned long zone_high = arch_zone_highest_possible_pfn[zone_type];
Mel Gorman9c7cd682006-09-27 01:49:58 -07004494 unsigned long node_start_pfn, node_end_pfn;
4495 unsigned long zone_start_pfn, zone_end_pfn;
4496
4497 get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
Tejun Heo96e907d2011-07-12 10:46:29 +02004498 zone_start_pfn = clamp(node_start_pfn, zone_low, zone_high);
4499 zone_end_pfn = clamp(node_end_pfn, zone_low, zone_high);
Mel Gorman9c7cd682006-09-27 01:49:58 -07004500
Mel Gorman2a1e2742007-07-17 04:03:12 -07004501 adjust_zone_range_for_zone_movable(nid, zone_type,
4502 node_start_pfn, node_end_pfn,
4503 &zone_start_pfn, &zone_end_pfn);
Mel Gorman9c7cd682006-09-27 01:49:58 -07004504 return __absent_pages_in_range(nid, zone_start_pfn, zone_end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07004505}
Mel Gorman0e0b8642006-09-27 01:49:56 -07004506
Tejun Heo0ee332c2011-12-08 10:22:09 -08004507#else /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
Paul Mundt6ea6e682007-07-15 23:38:20 -07004508static inline unsigned long __meminit zone_spanned_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07004509 unsigned long zone_type,
4510 unsigned long *zones_size)
4511{
4512 return zones_size[zone_type];
4513}
4514
Paul Mundt6ea6e682007-07-15 23:38:20 -07004515static inline unsigned long __meminit zone_absent_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07004516 unsigned long zone_type,
4517 unsigned long *zholes_size)
4518{
4519 if (!zholes_size)
4520 return 0;
4521
4522 return zholes_size[zone_type];
4523}
Yinghai Lu20e69262013-03-01 14:51:27 -08004524
Tejun Heo0ee332c2011-12-08 10:22:09 -08004525#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
Mel Gormanc7132162006-09-27 01:49:43 -07004526
Yasunori Gotoa3142c82007-05-08 00:23:07 -07004527static void __meminit calculate_node_totalpages(struct pglist_data *pgdat,
Mel Gormanc7132162006-09-27 01:49:43 -07004528 unsigned long *zones_size, unsigned long *zholes_size)
4529{
4530 unsigned long realtotalpages, totalpages = 0;
4531 enum zone_type i;
4532
4533 for (i = 0; i < MAX_NR_ZONES; i++)
4534 totalpages += zone_spanned_pages_in_node(pgdat->node_id, i,
4535 zones_size);
4536 pgdat->node_spanned_pages = totalpages;
4537
4538 realtotalpages = totalpages;
4539 for (i = 0; i < MAX_NR_ZONES; i++)
4540 realtotalpages -=
4541 zone_absent_pages_in_node(pgdat->node_id, i,
4542 zholes_size);
4543 pgdat->node_present_pages = realtotalpages;
4544 printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id,
4545 realtotalpages);
4546}
4547
Mel Gorman835c1342007-10-16 01:25:47 -07004548#ifndef CONFIG_SPARSEMEM
4549/*
4550 * Calculate the size of the zone->blockflags rounded to an unsigned long
Mel Gormand9c23402007-10-16 01:26:01 -07004551 * Start by making sure zonesize is a multiple of pageblock_order by rounding
4552 * up. Then use 1 NR_PAGEBLOCK_BITS worth of bits per pageblock, finally
Mel Gorman835c1342007-10-16 01:25:47 -07004553 * round what is now in bits to nearest long in bits, then return it in
4554 * bytes.
4555 */
Linus Torvalds7c455122013-02-18 09:58:02 -08004556static unsigned long __init usemap_size(unsigned long zone_start_pfn, unsigned long zonesize)
Mel Gorman835c1342007-10-16 01:25:47 -07004557{
4558 unsigned long usemapsize;
4559
Linus Torvalds7c455122013-02-18 09:58:02 -08004560 zonesize += zone_start_pfn & (pageblock_nr_pages-1);
Mel Gormand9c23402007-10-16 01:26:01 -07004561 usemapsize = roundup(zonesize, pageblock_nr_pages);
4562 usemapsize = usemapsize >> pageblock_order;
Mel Gorman835c1342007-10-16 01:25:47 -07004563 usemapsize *= NR_PAGEBLOCK_BITS;
4564 usemapsize = roundup(usemapsize, 8 * sizeof(unsigned long));
4565
4566 return usemapsize / 8;
4567}
4568
4569static void __init setup_usemap(struct pglist_data *pgdat,
Linus Torvalds7c455122013-02-18 09:58:02 -08004570 struct zone *zone,
4571 unsigned long zone_start_pfn,
4572 unsigned long zonesize)
Mel Gorman835c1342007-10-16 01:25:47 -07004573{
Linus Torvalds7c455122013-02-18 09:58:02 -08004574 unsigned long usemapsize = usemap_size(zone_start_pfn, zonesize);
Mel Gorman835c1342007-10-16 01:25:47 -07004575 zone->pageblock_flags = NULL;
Julia Lawall58a01a42009-01-06 14:39:28 -08004576 if (usemapsize)
Yinghai Lu8f389a992011-05-11 15:13:32 -07004577 zone->pageblock_flags = alloc_bootmem_node_nopanic(pgdat,
4578 usemapsize);
Mel Gorman835c1342007-10-16 01:25:47 -07004579}
4580#else
Linus Torvalds7c455122013-02-18 09:58:02 -08004581static inline void setup_usemap(struct pglist_data *pgdat, struct zone *zone,
4582 unsigned long zone_start_pfn, unsigned long zonesize) {}
Mel Gorman835c1342007-10-16 01:25:47 -07004583#endif /* CONFIG_SPARSEMEM */
4584
Mel Gormand9c23402007-10-16 01:26:01 -07004585#ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
Mel Gormanba72cb82007-11-28 16:21:13 -08004586
Mel Gormand9c23402007-10-16 01:26:01 -07004587/* Initialise the number of pages represented by NR_PAGEBLOCK_BITS */
Xishi Qiuca57df72012-07-31 16:43:19 -07004588void __init set_pageblock_order(void)
Mel Gormand9c23402007-10-16 01:26:01 -07004589{
Andrew Morton955c1cd2012-05-29 15:06:31 -07004590 unsigned int order;
4591
Mel Gormand9c23402007-10-16 01:26:01 -07004592 /* Check that pageblock_nr_pages has not already been setup */
4593 if (pageblock_order)
4594 return;
4595
Andrew Morton955c1cd2012-05-29 15:06:31 -07004596 if (HPAGE_SHIFT > PAGE_SHIFT)
4597 order = HUGETLB_PAGE_ORDER;
4598 else
4599 order = MAX_ORDER - 1;
4600
Mel Gormand9c23402007-10-16 01:26:01 -07004601 /*
4602 * Assume the largest contiguous order of interest is a huge page.
Andrew Morton955c1cd2012-05-29 15:06:31 -07004603 * This value may be variable depending on boot parameters on IA64 and
4604 * powerpc.
Mel Gormand9c23402007-10-16 01:26:01 -07004605 */
4606 pageblock_order = order;
4607}
4608#else /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
4609
Mel Gormanba72cb82007-11-28 16:21:13 -08004610/*
4611 * When CONFIG_HUGETLB_PAGE_SIZE_VARIABLE is not set, set_pageblock_order()
Andrew Morton955c1cd2012-05-29 15:06:31 -07004612 * is unused as pageblock_order is set at compile-time. See
4613 * include/linux/pageblock-flags.h for the values of pageblock_order based on
4614 * the kernel config
Mel Gormanba72cb82007-11-28 16:21:13 -08004615 */
Xishi Qiuca57df72012-07-31 16:43:19 -07004616void __init set_pageblock_order(void)
Mel Gormanba72cb82007-11-28 16:21:13 -08004617{
Mel Gormanba72cb82007-11-28 16:21:13 -08004618}
Mel Gormand9c23402007-10-16 01:26:01 -07004619
4620#endif /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
4621
Jiang Liu01cefae2012-12-12 13:52:19 -08004622static unsigned long __paginginit calc_memmap_size(unsigned long spanned_pages,
4623 unsigned long present_pages)
4624{
4625 unsigned long pages = spanned_pages;
4626
4627 /*
4628 * Provide a more accurate estimation if there are holes within
4629 * the zone and SPARSEMEM is in use. If there are holes within the
4630 * zone, each populated memory region may cost us one or two extra
4631 * memmap pages due to alignment because memmap pages for each
4632 * populated regions may not naturally algined on page boundary.
4633 * So the (present_pages >> 4) heuristic is a tradeoff for that.
4634 */
4635 if (spanned_pages > present_pages + (present_pages >> 4) &&
4636 IS_ENABLED(CONFIG_SPARSEMEM))
4637 pages = present_pages;
4638
4639 return PAGE_ALIGN(pages * sizeof(struct page)) >> PAGE_SHIFT;
4640}
4641
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642/*
4643 * Set up the zone data structures:
4644 * - mark all pages reserved
4645 * - mark all memory queues empty
4646 * - clear the memory bitmaps
Minchan Kim6527af52012-07-31 16:46:16 -07004647 *
4648 * NOTE: pgdat should get zeroed by caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 */
Alexander van Heukelumb5a0e012008-02-23 15:24:06 -08004650static void __paginginit free_area_init_core(struct pglist_data *pgdat,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 unsigned long *zones_size, unsigned long *zholes_size)
4652{
Christoph Lameter2f1b6242006-09-25 23:31:13 -07004653 enum zone_type j;
Dave Hansened8ece22005-10-29 18:16:50 -07004654 int nid = pgdat->node_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 unsigned long zone_start_pfn = pgdat->node_start_pfn;
Yasunori Goto718127c2006-06-23 02:03:10 -07004656 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657
Dave Hansen208d54e2005-10-29 18:16:52 -07004658 pgdat_resize_init(pgdat);
Andrea Arcangeli8177a422012-03-23 20:56:34 +01004659#ifdef CONFIG_NUMA_BALANCING
4660 spin_lock_init(&pgdat->numabalancing_migrate_lock);
4661 pgdat->numabalancing_migrate_nr_pages = 0;
4662 pgdat->numabalancing_migrate_next_window = jiffies;
4663#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 init_waitqueue_head(&pgdat->kswapd_wait);
Mel Gorman55150612012-07-31 16:44:35 -07004665 init_waitqueue_head(&pgdat->pfmemalloc_wait);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004666 pgdat_page_cgroup_init(pgdat);
Michal Nazarewicz5f63b722012-01-11 15:16:11 +01004667
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 for (j = 0; j < MAX_NR_ZONES; j++) {
4669 struct zone *zone = pgdat->node_zones + j;
Jiang Liu9feedc92012-12-12 13:52:12 -08004670 unsigned long size, realsize, freesize, memmap_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671
Mel Gormanc7132162006-09-27 01:49:43 -07004672 size = zone_spanned_pages_in_node(nid, j, zones_size);
Jiang Liu9feedc92012-12-12 13:52:12 -08004673 realsize = freesize = size - zone_absent_pages_in_node(nid, j,
Mel Gormanc7132162006-09-27 01:49:43 -07004674 zholes_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675
Mel Gorman0e0b8642006-09-27 01:49:56 -07004676 /*
Jiang Liu9feedc92012-12-12 13:52:12 -08004677 * Adjust freesize so that it accounts for how much memory
Mel Gorman0e0b8642006-09-27 01:49:56 -07004678 * is used by this zone for memmap. This affects the watermark
4679 * and per-cpu initialisations
4680 */
Jiang Liu01cefae2012-12-12 13:52:19 -08004681 memmap_pages = calc_memmap_size(size, realsize);
Jiang Liu9feedc92012-12-12 13:52:12 -08004682 if (freesize >= memmap_pages) {
4683 freesize -= memmap_pages;
Yinghai Lu5594c8c2009-01-06 14:39:14 -08004684 if (memmap_pages)
4685 printk(KERN_DEBUG
4686 " %s zone: %lu pages used for memmap\n",
4687 zone_names[j], memmap_pages);
Mel Gorman0e0b8642006-09-27 01:49:56 -07004688 } else
4689 printk(KERN_WARNING
Jiang Liu9feedc92012-12-12 13:52:12 -08004690 " %s zone: %lu pages exceeds freesize %lu\n",
4691 zone_names[j], memmap_pages, freesize);
Mel Gorman0e0b8642006-09-27 01:49:56 -07004692
Christoph Lameter62672762007-02-10 01:43:07 -08004693 /* Account for reserved pages */
Jiang Liu9feedc92012-12-12 13:52:12 -08004694 if (j == 0 && freesize > dma_reserve) {
4695 freesize -= dma_reserve;
Yinghai Lud903ef92008-10-18 20:27:06 -07004696 printk(KERN_DEBUG " %s zone: %lu pages reserved\n",
Christoph Lameter62672762007-02-10 01:43:07 -08004697 zone_names[0], dma_reserve);
Mel Gorman0e0b8642006-09-27 01:49:56 -07004698 }
4699
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07004700 if (!is_highmem_idx(j))
Jiang Liu9feedc92012-12-12 13:52:12 -08004701 nr_kernel_pages += freesize;
Jiang Liu01cefae2012-12-12 13:52:19 -08004702 /* Charge for highmem memmap if there are enough kernel pages */
4703 else if (nr_kernel_pages > memmap_pages * 2)
4704 nr_kernel_pages -= memmap_pages;
Jiang Liu9feedc92012-12-12 13:52:12 -08004705 nr_all_pages += freesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706
4707 zone->spanned_pages = size;
Jiang Liu306f2e92013-02-22 16:33:54 -08004708 zone->present_pages = realsize;
Jiang Liu9feedc92012-12-12 13:52:12 -08004709 /*
4710 * Set an approximate value for lowmem here, it will be adjusted
4711 * when the bootmem allocator frees pages into the buddy system.
4712 * And all highmem pages will be managed by the buddy system.
4713 */
4714 zone->managed_pages = is_highmem_idx(j) ? realsize : freesize;
Christoph Lameter96146342006-07-03 00:24:13 -07004715#ifdef CONFIG_NUMA
Christoph Lameterd5f541e2006-09-27 01:50:08 -07004716 zone->node = nid;
Jiang Liu9feedc92012-12-12 13:52:12 -08004717 zone->min_unmapped_pages = (freesize*sysctl_min_unmapped_ratio)
Christoph Lameter96146342006-07-03 00:24:13 -07004718 / 100;
Jiang Liu9feedc92012-12-12 13:52:12 -08004719 zone->min_slab_pages = (freesize * sysctl_min_slab_ratio) / 100;
Christoph Lameter96146342006-07-03 00:24:13 -07004720#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 zone->name = zone_names[j];
4722 spin_lock_init(&zone->lock);
4723 spin_lock_init(&zone->lru_lock);
Dave Hansenbdc8cb92005-10-29 18:16:53 -07004724 zone_seqlock_init(zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 zone->zone_pgdat = pgdat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726
Dave Hansened8ece22005-10-29 18:16:50 -07004727 zone_pcp_init(zone);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004728 lruvec_init(&zone->lruvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 if (!size)
4730 continue;
4731
Andrew Morton955c1cd2012-05-29 15:06:31 -07004732 set_pageblock_order();
Linus Torvalds7c455122013-02-18 09:58:02 -08004733 setup_usemap(pgdat, zone, zone_start_pfn, size);
Dave Hansena2f3aa022007-01-10 23:15:30 -08004734 ret = init_currently_empty_zone(zone, zone_start_pfn,
4735 size, MEMMAP_EARLY);
Yasunori Goto718127c2006-06-23 02:03:10 -07004736 BUG_ON(ret);
Heiko Carstens76cdd582008-05-14 16:05:52 -07004737 memmap_init(size, nid, j, zone_start_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 zone_start_pfn += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 }
4740}
4741
Sam Ravnborg577a32f2007-05-17 23:29:25 +02004742static void __init_refok alloc_node_mem_map(struct pglist_data *pgdat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 /* Skip empty nodes */
4745 if (!pgdat->node_spanned_pages)
4746 return;
4747
Andy Whitcroftd41dee32005-06-23 00:07:54 -07004748#ifdef CONFIG_FLAT_NODE_MEM_MAP
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 /* ia64 gets its own node_mem_map, before this, without bootmem */
4750 if (!pgdat->node_mem_map) {
Bob Piccoe984bb42006-05-20 15:00:31 -07004751 unsigned long size, start, end;
Andy Whitcroftd41dee32005-06-23 00:07:54 -07004752 struct page *map;
4753
Bob Piccoe984bb42006-05-20 15:00:31 -07004754 /*
4755 * The zone's endpoints aren't required to be MAX_ORDER
4756 * aligned but the node_mem_map endpoints must be in order
4757 * for the buddy allocator to function correctly.
4758 */
4759 start = pgdat->node_start_pfn & ~(MAX_ORDER_NR_PAGES - 1);
Cody P Schafer108bcc92013-02-22 16:35:23 -08004760 end = pgdat_end_pfn(pgdat);
Bob Piccoe984bb42006-05-20 15:00:31 -07004761 end = ALIGN(end, MAX_ORDER_NR_PAGES);
4762 size = (end - start) * sizeof(struct page);
Dave Hansen6f167ec2005-06-23 00:07:39 -07004763 map = alloc_remap(pgdat->node_id, size);
4764 if (!map)
Yinghai Lu8f389a992011-05-11 15:13:32 -07004765 map = alloc_bootmem_node_nopanic(pgdat, size);
Bob Piccoe984bb42006-05-20 15:00:31 -07004766 pgdat->node_mem_map = map + (pgdat->node_start_pfn - start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 }
Roman Zippel12d810c2007-05-31 00:40:54 -07004768#ifndef CONFIG_NEED_MULTIPLE_NODES
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 /*
4770 * With no DISCONTIG, the global mem_map is just set as node 0's
4771 */
Mel Gormanc7132162006-09-27 01:49:43 -07004772 if (pgdat == NODE_DATA(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 mem_map = NODE_DATA(0)->node_mem_map;
Tejun Heo0ee332c2011-12-08 10:22:09 -08004774#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Mel Gormanc7132162006-09-27 01:49:43 -07004775 if (page_to_pfn(mem_map) != pgdat->node_start_pfn)
Thomas Bogendoerfer467bc462008-01-08 15:33:11 -08004776 mem_map -= (pgdat->node_start_pfn - ARCH_PFN_OFFSET);
Tejun Heo0ee332c2011-12-08 10:22:09 -08004777#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
Mel Gormanc7132162006-09-27 01:49:43 -07004778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779#endif
Andy Whitcroftd41dee32005-06-23 00:07:54 -07004780#endif /* CONFIG_FLAT_NODE_MEM_MAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781}
4782
Johannes Weiner9109fb72008-07-23 21:27:20 -07004783void __paginginit free_area_init_node(int nid, unsigned long *zones_size,
4784 unsigned long node_start_pfn, unsigned long *zholes_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785{
Johannes Weiner9109fb72008-07-23 21:27:20 -07004786 pg_data_t *pgdat = NODE_DATA(nid);
4787
Minchan Kim88fdf752012-07-31 16:46:14 -07004788 /* pg_data_t should be reset to zero when it's allocated */
Linus Torvalds8783b6e2012-08-02 10:37:03 -07004789 WARN_ON(pgdat->nr_zones || pgdat->classzone_idx);
Minchan Kim88fdf752012-07-31 16:46:14 -07004790
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 pgdat->node_id = nid;
4792 pgdat->node_start_pfn = node_start_pfn;
David Rientjes957f8222012-10-08 16:33:24 -07004793 init_zone_allows_reclaim(nid);
Mel Gormanc7132162006-09-27 01:49:43 -07004794 calculate_node_totalpages(pgdat, zones_size, zholes_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795
4796 alloc_node_mem_map(pgdat);
Yinghai Lue8c27ac2008-06-01 13:15:22 -07004797#ifdef CONFIG_FLAT_NODE_MEM_MAP
4798 printk(KERN_DEBUG "free_area_init_node: node %d, pgdat %08lx, node_mem_map %08lx\n",
4799 nid, (unsigned long)pgdat,
4800 (unsigned long)pgdat->node_mem_map);
4801#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802
4803 free_area_init_core(pgdat, zones_size, zholes_size);
4804}
4805
Tejun Heo0ee332c2011-12-08 10:22:09 -08004806#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Miklos Szeredi418508c2007-05-23 13:57:55 -07004807
4808#if MAX_NUMNODES > 1
4809/*
4810 * Figure out the number of possible node ids.
4811 */
Cody P Schaferf9872ca2013-04-29 15:08:01 -07004812void __init setup_nr_node_ids(void)
Miklos Szeredi418508c2007-05-23 13:57:55 -07004813{
4814 unsigned int node;
4815 unsigned int highest = 0;
4816
4817 for_each_node_mask(node, node_possible_map)
4818 highest = node;
4819 nr_node_ids = highest + 1;
4820}
Miklos Szeredi418508c2007-05-23 13:57:55 -07004821#endif
4822
Mel Gormanc7132162006-09-27 01:49:43 -07004823/**
Tejun Heo1e019792011-07-12 09:45:34 +02004824 * node_map_pfn_alignment - determine the maximum internode alignment
4825 *
4826 * This function should be called after node map is populated and sorted.
4827 * It calculates the maximum power of two alignment which can distinguish
4828 * all the nodes.
4829 *
4830 * For example, if all nodes are 1GiB and aligned to 1GiB, the return value
4831 * would indicate 1GiB alignment with (1 << (30 - PAGE_SHIFT)). If the
4832 * nodes are shifted by 256MiB, 256MiB. Note that if only the last node is
4833 * shifted, 1GiB is enough and this function will indicate so.
4834 *
4835 * This is used to test whether pfn -> nid mapping of the chosen memory
4836 * model has fine enough granularity to avoid incorrect mapping for the
4837 * populated node map.
4838 *
4839 * Returns the determined alignment in pfn's. 0 if there is no alignment
4840 * requirement (single node).
4841 */
4842unsigned long __init node_map_pfn_alignment(void)
4843{
4844 unsigned long accl_mask = 0, last_end = 0;
Tejun Heoc13291a2011-07-12 10:46:30 +02004845 unsigned long start, end, mask;
Tejun Heo1e019792011-07-12 09:45:34 +02004846 int last_nid = -1;
Tejun Heoc13291a2011-07-12 10:46:30 +02004847 int i, nid;
Tejun Heo1e019792011-07-12 09:45:34 +02004848
Tejun Heoc13291a2011-07-12 10:46:30 +02004849 for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, &nid) {
Tejun Heo1e019792011-07-12 09:45:34 +02004850 if (!start || last_nid < 0 || last_nid == nid) {
4851 last_nid = nid;
4852 last_end = end;
4853 continue;
4854 }
4855
4856 /*
4857 * Start with a mask granular enough to pin-point to the
4858 * start pfn and tick off bits one-by-one until it becomes
4859 * too coarse to separate the current node from the last.
4860 */
4861 mask = ~((1 << __ffs(start)) - 1);
4862 while (mask && last_end <= (start & (mask << 1)))
4863 mask <<= 1;
4864
4865 /* accumulate all internode masks */
4866 accl_mask |= mask;
4867 }
4868
4869 /* convert mask to number of pages */
4870 return ~accl_mask + 1;
4871}
4872
Mel Gormana6af2bc2007-02-10 01:42:57 -08004873/* Find the lowest pfn for a node */
Adrian Bunkb69a7282008-07-23 21:28:12 -07004874static unsigned long __init find_min_pfn_for_node(int nid)
Mel Gormanc7132162006-09-27 01:49:43 -07004875{
Mel Gormana6af2bc2007-02-10 01:42:57 -08004876 unsigned long min_pfn = ULONG_MAX;
Tejun Heoc13291a2011-07-12 10:46:30 +02004877 unsigned long start_pfn;
4878 int i;
Mel Gorman1abbfb42006-11-23 12:01:41 +00004879
Tejun Heoc13291a2011-07-12 10:46:30 +02004880 for_each_mem_pfn_range(i, nid, &start_pfn, NULL, NULL)
4881 min_pfn = min(min_pfn, start_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07004882
Mel Gormana6af2bc2007-02-10 01:42:57 -08004883 if (min_pfn == ULONG_MAX) {
4884 printk(KERN_WARNING
Paul Jackson2bc0d2612008-06-22 07:22:17 -07004885 "Could not find start_pfn for node %d\n", nid);
Mel Gormana6af2bc2007-02-10 01:42:57 -08004886 return 0;
4887 }
4888
4889 return min_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07004890}
4891
4892/**
4893 * find_min_pfn_with_active_regions - Find the minimum PFN registered
4894 *
4895 * It returns the minimum PFN based on information provided via
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004896 * add_active_range().
Mel Gormanc7132162006-09-27 01:49:43 -07004897 */
4898unsigned long __init find_min_pfn_with_active_regions(void)
4899{
4900 return find_min_pfn_for_node(MAX_NUMNODES);
4901}
4902
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07004903/*
4904 * early_calculate_totalpages()
4905 * Sum pages in active regions for movable zone.
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08004906 * Populate N_MEMORY for calculating usable_nodes.
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07004907 */
Adrian Bunk484f51f2007-10-16 01:26:03 -07004908static unsigned long __init early_calculate_totalpages(void)
Mel Gorman7e63efe2007-07-17 04:03:15 -07004909{
Mel Gorman7e63efe2007-07-17 04:03:15 -07004910 unsigned long totalpages = 0;
Tejun Heoc13291a2011-07-12 10:46:30 +02004911 unsigned long start_pfn, end_pfn;
4912 int i, nid;
Mel Gorman7e63efe2007-07-17 04:03:15 -07004913
Tejun Heoc13291a2011-07-12 10:46:30 +02004914 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) {
4915 unsigned long pages = end_pfn - start_pfn;
4916
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07004917 totalpages += pages;
4918 if (pages)
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08004919 node_set_state(nid, N_MEMORY);
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07004920 }
4921 return totalpages;
Mel Gorman7e63efe2007-07-17 04:03:15 -07004922}
4923
Mel Gorman2a1e2742007-07-17 04:03:12 -07004924/*
4925 * Find the PFN the Movable zone begins in each node. Kernel memory
4926 * is spread evenly between nodes as long as the nodes have enough
4927 * memory. When they don't, some nodes will have more kernelcore than
4928 * others
4929 */
Kautuk Consulb224ef82012-03-21 16:34:15 -07004930static void __init find_zone_movable_pfns_for_nodes(void)
Mel Gorman2a1e2742007-07-17 04:03:12 -07004931{
4932 int i, nid;
4933 unsigned long usable_startpfn;
4934 unsigned long kernelcore_node, kernelcore_remaining;
Yinghai Lu66918dc2009-06-30 11:41:37 -07004935 /* save the state before borrow the nodemask */
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08004936 nodemask_t saved_node_state = node_states[N_MEMORY];
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07004937 unsigned long totalpages = early_calculate_totalpages();
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08004938 int usable_nodes = nodes_weight(node_states[N_MEMORY]);
Mel Gorman2a1e2742007-07-17 04:03:12 -07004939
Mel Gorman7e63efe2007-07-17 04:03:15 -07004940 /*
4941 * If movablecore was specified, calculate what size of
4942 * kernelcore that corresponds so that memory usable for
4943 * any allocation type is evenly spread. If both kernelcore
4944 * and movablecore are specified, then the value of kernelcore
4945 * will be used for required_kernelcore if it's greater than
4946 * what movablecore would have allowed.
4947 */
4948 if (required_movablecore) {
Mel Gorman7e63efe2007-07-17 04:03:15 -07004949 unsigned long corepages;
4950
4951 /*
4952 * Round-up so that ZONE_MOVABLE is at least as large as what
4953 * was requested by the user
4954 */
4955 required_movablecore =
4956 roundup(required_movablecore, MAX_ORDER_NR_PAGES);
4957 corepages = totalpages - required_movablecore;
4958
4959 required_kernelcore = max(required_kernelcore, corepages);
4960 }
4961
Yinghai Lu20e69262013-03-01 14:51:27 -08004962 /* If kernelcore was not specified, there is no ZONE_MOVABLE */
4963 if (!required_kernelcore)
Yinghai Lu66918dc2009-06-30 11:41:37 -07004964 goto out;
Mel Gorman2a1e2742007-07-17 04:03:12 -07004965
4966 /* usable_startpfn is the lowest possible pfn ZONE_MOVABLE can be at */
Yinghai Lu20e69262013-03-01 14:51:27 -08004967 find_usable_zone_for_movable();
Mel Gorman2a1e2742007-07-17 04:03:12 -07004968 usable_startpfn = arch_zone_lowest_possible_pfn[movable_zone];
4969
4970restart:
4971 /* Spread kernelcore memory as evenly as possible throughout nodes */
4972 kernelcore_node = required_kernelcore / usable_nodes;
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08004973 for_each_node_state(nid, N_MEMORY) {
Tejun Heoc13291a2011-07-12 10:46:30 +02004974 unsigned long start_pfn, end_pfn;
4975
Mel Gorman2a1e2742007-07-17 04:03:12 -07004976 /*
4977 * Recalculate kernelcore_node if the division per node
4978 * now exceeds what is necessary to satisfy the requested
4979 * amount of memory for the kernel
4980 */
4981 if (required_kernelcore < kernelcore_node)
4982 kernelcore_node = required_kernelcore / usable_nodes;
4983
4984 /*
4985 * As the map is walked, we track how much memory is usable
4986 * by the kernel using kernelcore_remaining. When it is
4987 * 0, the rest of the node is usable by ZONE_MOVABLE
4988 */
4989 kernelcore_remaining = kernelcore_node;
4990
4991 /* Go through each range of PFNs within this node */
Tejun Heoc13291a2011-07-12 10:46:30 +02004992 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
Mel Gorman2a1e2742007-07-17 04:03:12 -07004993 unsigned long size_pages;
4994
Tejun Heoc13291a2011-07-12 10:46:30 +02004995 start_pfn = max(start_pfn, zone_movable_pfn[nid]);
Mel Gorman2a1e2742007-07-17 04:03:12 -07004996 if (start_pfn >= end_pfn)
4997 continue;
4998
4999 /* Account for what is only usable for kernelcore */
5000 if (start_pfn < usable_startpfn) {
5001 unsigned long kernel_pages;
5002 kernel_pages = min(end_pfn, usable_startpfn)
5003 - start_pfn;
5004
5005 kernelcore_remaining -= min(kernel_pages,
5006 kernelcore_remaining);
5007 required_kernelcore -= min(kernel_pages,
5008 required_kernelcore);
5009
5010 /* Continue if range is now fully accounted */
5011 if (end_pfn <= usable_startpfn) {
5012
5013 /*
5014 * Push zone_movable_pfn to the end so
5015 * that if we have to rebalance
5016 * kernelcore across nodes, we will
5017 * not double account here
5018 */
5019 zone_movable_pfn[nid] = end_pfn;
5020 continue;
5021 }
5022 start_pfn = usable_startpfn;
5023 }
5024
5025 /*
5026 * The usable PFN range for ZONE_MOVABLE is from
5027 * start_pfn->end_pfn. Calculate size_pages as the
5028 * number of pages used as kernelcore
5029 */
5030 size_pages = end_pfn - start_pfn;
5031 if (size_pages > kernelcore_remaining)
5032 size_pages = kernelcore_remaining;
5033 zone_movable_pfn[nid] = start_pfn + size_pages;
5034
5035 /*
5036 * Some kernelcore has been met, update counts and
5037 * break if the kernelcore for this node has been
5038 * satisified
5039 */
5040 required_kernelcore -= min(required_kernelcore,
5041 size_pages);
5042 kernelcore_remaining -= size_pages;
5043 if (!kernelcore_remaining)
5044 break;
5045 }
5046 }
5047
5048 /*
5049 * If there is still required_kernelcore, we do another pass with one
5050 * less node in the count. This will push zone_movable_pfn[nid] further
5051 * along on the nodes that still have memory until kernelcore is
5052 * satisified
5053 */
5054 usable_nodes--;
5055 if (usable_nodes && required_kernelcore > usable_nodes)
5056 goto restart;
5057
5058 /* Align start of ZONE_MOVABLE on all nids to MAX_ORDER_NR_PAGES */
5059 for (nid = 0; nid < MAX_NUMNODES; nid++)
5060 zone_movable_pfn[nid] =
5061 roundup(zone_movable_pfn[nid], MAX_ORDER_NR_PAGES);
Yinghai Lu66918dc2009-06-30 11:41:37 -07005062
Yinghai Lu20e69262013-03-01 14:51:27 -08005063out:
Yinghai Lu66918dc2009-06-30 11:41:37 -07005064 /* restore the node_state */
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08005065 node_states[N_MEMORY] = saved_node_state;
Mel Gorman2a1e2742007-07-17 04:03:12 -07005066}
5067
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08005068/* Any regular or high memory on that node ? */
5069static void check_for_memory(pg_data_t *pgdat, int nid)
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07005070{
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07005071 enum zone_type zone_type;
5072
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08005073 if (N_MEMORY == N_NORMAL_MEMORY)
5074 return;
5075
5076 for (zone_type = 0; zone_type <= ZONE_MOVABLE - 1; zone_type++) {
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07005077 struct zone *zone = &pgdat->node_zones[zone_type];
Bob Liud0048b02012-01-12 17:19:07 -08005078 if (zone->present_pages) {
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08005079 node_set_state(nid, N_HIGH_MEMORY);
5080 if (N_NORMAL_MEMORY != N_HIGH_MEMORY &&
5081 zone_type <= ZONE_NORMAL)
5082 node_set_state(nid, N_NORMAL_MEMORY);
Bob Liud0048b02012-01-12 17:19:07 -08005083 break;
5084 }
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07005085 }
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07005086}
5087
Mel Gormanc7132162006-09-27 01:49:43 -07005088/**
5089 * free_area_init_nodes - Initialise all pg_data_t and zone data
Randy Dunlap88ca3b92006-10-04 02:15:25 -07005090 * @max_zone_pfn: an array of max PFNs for each zone
Mel Gormanc7132162006-09-27 01:49:43 -07005091 *
5092 * This will call free_area_init_node() for each active node in the system.
5093 * Using the page ranges provided by add_active_range(), the size of each
5094 * zone in each node and their holes is calculated. If the maximum PFN
5095 * between two adjacent zones match, it is assumed that the zone is empty.
5096 * For example, if arch_max_dma_pfn == arch_max_dma32_pfn, it is assumed
5097 * that arch_max_dma32_pfn has no pages. It is also assumed that a zone
5098 * starts where the previous one ended. For example, ZONE_DMA32 starts
5099 * at arch_max_dma_pfn.
5100 */
5101void __init free_area_init_nodes(unsigned long *max_zone_pfn)
5102{
Tejun Heoc13291a2011-07-12 10:46:30 +02005103 unsigned long start_pfn, end_pfn;
5104 int i, nid;
Mel Gormana6af2bc2007-02-10 01:42:57 -08005105
Mel Gormanc7132162006-09-27 01:49:43 -07005106 /* Record where the zone boundaries are */
5107 memset(arch_zone_lowest_possible_pfn, 0,
5108 sizeof(arch_zone_lowest_possible_pfn));
5109 memset(arch_zone_highest_possible_pfn, 0,
5110 sizeof(arch_zone_highest_possible_pfn));
5111 arch_zone_lowest_possible_pfn[0] = find_min_pfn_with_active_regions();
5112 arch_zone_highest_possible_pfn[0] = max_zone_pfn[0];
5113 for (i = 1; i < MAX_NR_ZONES; i++) {
Mel Gorman2a1e2742007-07-17 04:03:12 -07005114 if (i == ZONE_MOVABLE)
5115 continue;
Mel Gormanc7132162006-09-27 01:49:43 -07005116 arch_zone_lowest_possible_pfn[i] =
5117 arch_zone_highest_possible_pfn[i-1];
5118 arch_zone_highest_possible_pfn[i] =
5119 max(max_zone_pfn[i], arch_zone_lowest_possible_pfn[i]);
5120 }
Mel Gorman2a1e2742007-07-17 04:03:12 -07005121 arch_zone_lowest_possible_pfn[ZONE_MOVABLE] = 0;
5122 arch_zone_highest_possible_pfn[ZONE_MOVABLE] = 0;
5123
5124 /* Find the PFNs that ZONE_MOVABLE begins at in each node */
5125 memset(zone_movable_pfn, 0, sizeof(zone_movable_pfn));
Kautuk Consulb224ef82012-03-21 16:34:15 -07005126 find_zone_movable_pfns_for_nodes();
Mel Gormanc7132162006-09-27 01:49:43 -07005127
Mel Gormanc7132162006-09-27 01:49:43 -07005128 /* Print out the zone ranges */
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07005129 printk("Zone ranges:\n");
Mel Gorman2a1e2742007-07-17 04:03:12 -07005130 for (i = 0; i < MAX_NR_ZONES; i++) {
5131 if (i == ZONE_MOVABLE)
5132 continue;
Kay Sievers155cbfc2012-05-08 17:24:14 +02005133 printk(KERN_CONT " %-8s ", zone_names[i]);
David Rientjes72f0ba02010-03-05 13:42:14 -08005134 if (arch_zone_lowest_possible_pfn[i] ==
5135 arch_zone_highest_possible_pfn[i])
Kay Sievers155cbfc2012-05-08 17:24:14 +02005136 printk(KERN_CONT "empty\n");
David Rientjes72f0ba02010-03-05 13:42:14 -08005137 else
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07005138 printk(KERN_CONT "[mem %0#10lx-%0#10lx]\n",
5139 arch_zone_lowest_possible_pfn[i] << PAGE_SHIFT,
5140 (arch_zone_highest_possible_pfn[i]
5141 << PAGE_SHIFT) - 1);
Mel Gorman2a1e2742007-07-17 04:03:12 -07005142 }
5143
5144 /* Print out the PFNs ZONE_MOVABLE begins at in each node */
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07005145 printk("Movable zone start for each node\n");
Mel Gorman2a1e2742007-07-17 04:03:12 -07005146 for (i = 0; i < MAX_NUMNODES; i++) {
5147 if (zone_movable_pfn[i])
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07005148 printk(" Node %d: %#010lx\n", i,
5149 zone_movable_pfn[i] << PAGE_SHIFT);
Mel Gorman2a1e2742007-07-17 04:03:12 -07005150 }
Mel Gormanc7132162006-09-27 01:49:43 -07005151
Wanpeng Lif2d52fe2012-10-08 16:32:24 -07005152 /* Print out the early node map */
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07005153 printk("Early memory node ranges\n");
Tejun Heoc13291a2011-07-12 10:46:30 +02005154 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid)
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07005155 printk(" node %3d: [mem %#010lx-%#010lx]\n", nid,
5156 start_pfn << PAGE_SHIFT, (end_pfn << PAGE_SHIFT) - 1);
Mel Gormanc7132162006-09-27 01:49:43 -07005157
5158 /* Initialise every node */
Mel Gorman708614e2008-07-23 21:26:51 -07005159 mminit_verify_pageflags_layout();
Christoph Lameter8ef82862007-02-20 13:57:52 -08005160 setup_nr_node_ids();
Mel Gormanc7132162006-09-27 01:49:43 -07005161 for_each_online_node(nid) {
5162 pg_data_t *pgdat = NODE_DATA(nid);
Johannes Weiner9109fb72008-07-23 21:27:20 -07005163 free_area_init_node(nid, NULL,
Mel Gormanc7132162006-09-27 01:49:43 -07005164 find_min_pfn_for_node(nid), NULL);
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07005165
5166 /* Any memory on that node */
5167 if (pgdat->node_present_pages)
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08005168 node_set_state(nid, N_MEMORY);
5169 check_for_memory(pgdat, nid);
Mel Gormanc7132162006-09-27 01:49:43 -07005170 }
5171}
Mel Gorman2a1e2742007-07-17 04:03:12 -07005172
Mel Gorman7e63efe2007-07-17 04:03:15 -07005173static int __init cmdline_parse_core(char *p, unsigned long *core)
Mel Gorman2a1e2742007-07-17 04:03:12 -07005174{
5175 unsigned long long coremem;
5176 if (!p)
5177 return -EINVAL;
5178
5179 coremem = memparse(p, &p);
Mel Gorman7e63efe2007-07-17 04:03:15 -07005180 *core = coremem >> PAGE_SHIFT;
Mel Gorman2a1e2742007-07-17 04:03:12 -07005181
Mel Gorman7e63efe2007-07-17 04:03:15 -07005182 /* Paranoid check that UL is enough for the coremem value */
Mel Gorman2a1e2742007-07-17 04:03:12 -07005183 WARN_ON((coremem >> PAGE_SHIFT) > ULONG_MAX);
5184
5185 return 0;
5186}
Mel Gormaned7ed362007-07-17 04:03:14 -07005187
Mel Gorman7e63efe2007-07-17 04:03:15 -07005188/*
5189 * kernelcore=size sets the amount of memory for use for allocations that
5190 * cannot be reclaimed or migrated.
5191 */
5192static int __init cmdline_parse_kernelcore(char *p)
5193{
5194 return cmdline_parse_core(p, &required_kernelcore);
5195}
5196
5197/*
5198 * movablecore=size sets the amount of memory for use for allocations that
5199 * can be reclaimed or migrated.
5200 */
5201static int __init cmdline_parse_movablecore(char *p)
5202{
5203 return cmdline_parse_core(p, &required_movablecore);
5204}
5205
Mel Gormaned7ed362007-07-17 04:03:14 -07005206early_param("kernelcore", cmdline_parse_kernelcore);
Mel Gorman7e63efe2007-07-17 04:03:15 -07005207early_param("movablecore", cmdline_parse_movablecore);
Mel Gormaned7ed362007-07-17 04:03:14 -07005208
Tejun Heo0ee332c2011-12-08 10:22:09 -08005209#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
Mel Gormanc7132162006-09-27 01:49:43 -07005210
Jiang Liu69afade2013-04-29 15:06:21 -07005211unsigned long free_reserved_area(unsigned long start, unsigned long end,
5212 int poison, char *s)
5213{
5214 unsigned long pages, pos;
5215
5216 pos = start = PAGE_ALIGN(start);
5217 end &= PAGE_MASK;
5218 for (pages = 0; pos < end; pos += PAGE_SIZE, pages++) {
5219 if (poison)
5220 memset((void *)pos, poison, PAGE_SIZE);
Ralf Baechlebb3ec6b2013-05-22 12:18:47 +02005221 free_reserved_page(virt_to_page((void *)pos));
Jiang Liu69afade2013-04-29 15:06:21 -07005222 }
5223
5224 if (pages && s)
5225 pr_info("Freeing %s memory: %ldK (%lx - %lx)\n",
5226 s, pages << (PAGE_SHIFT - 10), start, end);
5227
5228 return pages;
5229}
5230
Jiang Liucfa11e02013-04-29 15:07:00 -07005231#ifdef CONFIG_HIGHMEM
5232void free_highmem_page(struct page *page)
5233{
5234 __free_reserved_page(page);
5235 totalram_pages++;
5236 totalhigh_pages++;
5237}
5238#endif
5239
Mel Gorman0e0b8642006-09-27 01:49:56 -07005240/**
Randy Dunlap88ca3b92006-10-04 02:15:25 -07005241 * set_dma_reserve - set the specified number of pages reserved in the first zone
5242 * @new_dma_reserve: The number of pages to mark reserved
Mel Gorman0e0b8642006-09-27 01:49:56 -07005243 *
5244 * The per-cpu batchsize and zone watermarks are determined by present_pages.
5245 * In the DMA zone, a significant percentage may be consumed by kernel image
5246 * and other unfreeable allocations which can skew the watermarks badly. This
Randy Dunlap88ca3b92006-10-04 02:15:25 -07005247 * function may optionally be used to account for unfreeable pages in the
5248 * first zone (e.g., ZONE_DMA). The effect will be lower watermarks and
5249 * smaller per-cpu batchsize.
Mel Gorman0e0b8642006-09-27 01:49:56 -07005250 */
5251void __init set_dma_reserve(unsigned long new_dma_reserve)
5252{
5253 dma_reserve = new_dma_reserve;
5254}
5255
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256void __init free_area_init(unsigned long *zones_size)
5257{
Johannes Weiner9109fb72008-07-23 21:27:20 -07005258 free_area_init_node(0, zones_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
5260}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262static int page_alloc_cpu_notify(struct notifier_block *self,
5263 unsigned long action, void *hcpu)
5264{
5265 int cpu = (unsigned long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005267 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
Konstantin Khlebnikovf0cb3c72012-03-21 16:34:06 -07005268 lru_add_drain_cpu(cpu);
Christoph Lameter9f8f2172008-02-04 22:29:11 -08005269 drain_pages(cpu);
5270
5271 /*
5272 * Spill the event counters of the dead processor
5273 * into the current processors event counters.
5274 * This artificially elevates the count of the current
5275 * processor.
5276 */
Christoph Lameterf8891e52006-06-30 01:55:45 -07005277 vm_events_fold_cpu(cpu);
Christoph Lameter9f8f2172008-02-04 22:29:11 -08005278
5279 /*
5280 * Zero the differential counters of the dead processor
5281 * so that the vm statistics are consistent.
5282 *
5283 * This is only okay since the processor is dead and cannot
5284 * race with what we are doing.
5285 */
Christoph Lameter2244b952006-06-30 01:55:33 -07005286 refresh_cpu_vm_stats(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 }
5288 return NOTIFY_OK;
5289}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290
5291void __init page_alloc_init(void)
5292{
5293 hotcpu_notifier(page_alloc_cpu_notify, 0);
Ingo Molnarbc7d7492009-07-03 08:29:37 -05005294 local_irq_lock_init(pa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295}
5296
5297/*
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07005298 * calculate_totalreserve_pages - called when sysctl_lower_zone_reserve_ratio
5299 * or min_free_kbytes changes.
5300 */
5301static void calculate_totalreserve_pages(void)
5302{
5303 struct pglist_data *pgdat;
5304 unsigned long reserve_pages = 0;
Christoph Lameter2f6726e2006-09-25 23:31:18 -07005305 enum zone_type i, j;
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07005306
5307 for_each_online_pgdat(pgdat) {
5308 for (i = 0; i < MAX_NR_ZONES; i++) {
5309 struct zone *zone = pgdat->node_zones + i;
5310 unsigned long max = 0;
5311
5312 /* Find valid and maximum lowmem_reserve in the zone */
5313 for (j = i; j < MAX_NR_ZONES; j++) {
5314 if (zone->lowmem_reserve[j] > max)
5315 max = zone->lowmem_reserve[j];
5316 }
5317
Mel Gorman41858962009-06-16 15:32:12 -07005318 /* we treat the high watermark as reserved pages. */
5319 max += high_wmark_pages(zone);
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07005320
Jiang Liub40da042013-02-22 16:33:52 -08005321 if (max > zone->managed_pages)
5322 max = zone->managed_pages;
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07005323 reserve_pages += max;
Johannes Weinerab8fabd2012-01-10 15:07:42 -08005324 /*
5325 * Lowmem reserves are not available to
5326 * GFP_HIGHUSER page cache allocations and
5327 * kswapd tries to balance zones to their high
5328 * watermark. As a result, neither should be
5329 * regarded as dirtyable memory, to prevent a
5330 * situation where reclaim has to clean pages
5331 * in order to balance the zones.
5332 */
5333 zone->dirty_balance_reserve = max;
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07005334 }
5335 }
Johannes Weinerab8fabd2012-01-10 15:07:42 -08005336 dirty_balance_reserve = reserve_pages;
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07005337 totalreserve_pages = reserve_pages;
5338}
5339
5340/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 * setup_per_zone_lowmem_reserve - called whenever
5342 * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
5343 * has a correct pages reserved value, so an adequate number of
5344 * pages are left in the zone after a successful __alloc_pages().
5345 */
5346static void setup_per_zone_lowmem_reserve(void)
5347{
5348 struct pglist_data *pgdat;
Christoph Lameter2f6726e2006-09-25 23:31:18 -07005349 enum zone_type j, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350
KAMEZAWA Hiroyukiec936fc2006-03-27 01:15:59 -08005351 for_each_online_pgdat(pgdat) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 for (j = 0; j < MAX_NR_ZONES; j++) {
5353 struct zone *zone = pgdat->node_zones + j;
Jiang Liub40da042013-02-22 16:33:52 -08005354 unsigned long managed_pages = zone->managed_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355
5356 zone->lowmem_reserve[j] = 0;
5357
Christoph Lameter2f6726e2006-09-25 23:31:18 -07005358 idx = j;
5359 while (idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 struct zone *lower_zone;
5361
Christoph Lameter2f6726e2006-09-25 23:31:18 -07005362 idx--;
5363
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 if (sysctl_lowmem_reserve_ratio[idx] < 1)
5365 sysctl_lowmem_reserve_ratio[idx] = 1;
5366
5367 lower_zone = pgdat->node_zones + idx;
Jiang Liub40da042013-02-22 16:33:52 -08005368 lower_zone->lowmem_reserve[j] = managed_pages /
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 sysctl_lowmem_reserve_ratio[idx];
Jiang Liub40da042013-02-22 16:33:52 -08005370 managed_pages += lower_zone->managed_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 }
5372 }
5373 }
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07005374
5375 /* update totalreserve_pages */
5376 calculate_totalreserve_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377}
5378
Mel Gormancfd3da12011-04-25 21:36:42 +00005379static void __setup_per_zone_wmarks(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380{
5381 unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
5382 unsigned long lowmem_pages = 0;
5383 struct zone *zone;
5384 unsigned long flags;
5385
5386 /* Calculate total number of !ZONE_HIGHMEM pages */
5387 for_each_zone(zone) {
5388 if (!is_highmem(zone))
Jiang Liub40da042013-02-22 16:33:52 -08005389 lowmem_pages += zone->managed_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 }
5391
5392 for_each_zone(zone) {
Andrew Mortonac924c62006-05-15 09:43:59 -07005393 u64 tmp;
5394
Gerald Schaefer1125b4e2008-10-18 20:27:11 -07005395 spin_lock_irqsave(&zone->lock, flags);
Jiang Liub40da042013-02-22 16:33:52 -08005396 tmp = (u64)pages_min * zone->managed_pages;
Andrew Mortonac924c62006-05-15 09:43:59 -07005397 do_div(tmp, lowmem_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 if (is_highmem(zone)) {
5399 /*
Nick Piggin669ed172005-11-13 16:06:45 -08005400 * __GFP_HIGH and PF_MEMALLOC allocations usually don't
5401 * need highmem pages, so cap pages_min to a small
5402 * value here.
5403 *
Mel Gorman41858962009-06-16 15:32:12 -07005404 * The WMARK_HIGH-WMARK_LOW and (WMARK_LOW-WMARK_MIN)
Nick Piggin669ed172005-11-13 16:06:45 -08005405 * deltas controls asynch page reclaim, and so should
5406 * not be capped for highmem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 */
Andrew Morton90ae8d62013-02-22 16:32:22 -08005408 unsigned long min_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409
Jiang Liub40da042013-02-22 16:33:52 -08005410 min_pages = zone->managed_pages / 1024;
Andrew Morton90ae8d62013-02-22 16:32:22 -08005411 min_pages = clamp(min_pages, SWAP_CLUSTER_MAX, 128UL);
Mel Gorman41858962009-06-16 15:32:12 -07005412 zone->watermark[WMARK_MIN] = min_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 } else {
Nick Piggin669ed172005-11-13 16:06:45 -08005414 /*
5415 * If it's a lowmem zone, reserve a number of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 * proportionate to the zone's size.
5417 */
Mel Gorman41858962009-06-16 15:32:12 -07005418 zone->watermark[WMARK_MIN] = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 }
5420
Mel Gorman41858962009-06-16 15:32:12 -07005421 zone->watermark[WMARK_LOW] = min_wmark_pages(zone) + (tmp >> 2);
5422 zone->watermark[WMARK_HIGH] = min_wmark_pages(zone) + (tmp >> 1);
Marek Szyprowski49f223a2012-01-25 12:49:24 +01005423
Mel Gorman56fd56b2007-10-16 01:25:58 -07005424 setup_zone_migrate_reserve(zone);
Gerald Schaefer1125b4e2008-10-18 20:27:11 -07005425 spin_unlock_irqrestore(&zone->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 }
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07005427
5428 /* update totalreserve_pages */
5429 calculate_totalreserve_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430}
5431
Mel Gormancfd3da12011-04-25 21:36:42 +00005432/**
5433 * setup_per_zone_wmarks - called when min_free_kbytes changes
5434 * or when memory is hot-{added|removed}
5435 *
5436 * Ensures that the watermark[min,low,high] values for each zone are set
5437 * correctly with respect to min_free_kbytes.
5438 */
5439void setup_per_zone_wmarks(void)
5440{
5441 mutex_lock(&zonelists_mutex);
5442 __setup_per_zone_wmarks();
5443 mutex_unlock(&zonelists_mutex);
5444}
5445
Randy Dunlap55a44622009-09-21 17:01:20 -07005446/*
Rik van Riel556adec2008-10-18 20:26:34 -07005447 * The inactive anon list should be small enough that the VM never has to
5448 * do too much work, but large enough that each inactive page has a chance
5449 * to be referenced again before it is swapped out.
5450 *
5451 * The inactive_anon ratio is the target ratio of ACTIVE_ANON to
5452 * INACTIVE_ANON pages on this zone's LRU, maintained by the
5453 * pageout code. A zone->inactive_ratio of 3 means 3:1 or 25% of
5454 * the anonymous pages are kept on the inactive list.
5455 *
5456 * total target max
5457 * memory ratio inactive anon
5458 * -------------------------------------
5459 * 10MB 1 5MB
5460 * 100MB 1 50MB
5461 * 1GB 3 250MB
5462 * 10GB 10 0.9GB
5463 * 100GB 31 3GB
5464 * 1TB 101 10GB
5465 * 10TB 320 32GB
5466 */
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07005467static void __meminit calculate_zone_inactive_ratio(struct zone *zone)
Minchan Kim96cb4df2009-06-16 15:32:49 -07005468{
5469 unsigned int gb, ratio;
5470
5471 /* Zone size in gigabytes */
Jiang Liub40da042013-02-22 16:33:52 -08005472 gb = zone->managed_pages >> (30 - PAGE_SHIFT);
Minchan Kim96cb4df2009-06-16 15:32:49 -07005473 if (gb)
5474 ratio = int_sqrt(10 * gb);
5475 else
5476 ratio = 1;
5477
5478 zone->inactive_ratio = ratio;
5479}
5480
KOSAKI Motohiro839a4fc2011-05-24 17:11:31 -07005481static void __meminit setup_per_zone_inactive_ratio(void)
Rik van Riel556adec2008-10-18 20:26:34 -07005482{
5483 struct zone *zone;
5484
Minchan Kim96cb4df2009-06-16 15:32:49 -07005485 for_each_zone(zone)
5486 calculate_zone_inactive_ratio(zone);
Rik van Riel556adec2008-10-18 20:26:34 -07005487}
5488
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489/*
5490 * Initialise min_free_kbytes.
5491 *
5492 * For small machines we want it small (128k min). For large machines
5493 * we want it large (64MB max). But it is not linear, because network
5494 * bandwidth does not increase linearly with machine size. We use
5495 *
5496 * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
5497 * min_free_kbytes = sqrt(lowmem_kbytes * 16)
5498 *
5499 * which yields
5500 *
5501 * 16MB: 512k
5502 * 32MB: 724k
5503 * 64MB: 1024k
5504 * 128MB: 1448k
5505 * 256MB: 2048k
5506 * 512MB: 2896k
5507 * 1024MB: 4096k
5508 * 2048MB: 5792k
5509 * 4096MB: 8192k
5510 * 8192MB: 11584k
5511 * 16384MB: 16384k
5512 */
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07005513int __meminit init_per_zone_wmark_min(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514{
5515 unsigned long lowmem_kbytes;
5516
5517 lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
5518
5519 min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
5520 if (min_free_kbytes < 128)
5521 min_free_kbytes = 128;
5522 if (min_free_kbytes > 65536)
5523 min_free_kbytes = 65536;
Minchan Kimbc75d332009-06-16 15:32:48 -07005524 setup_per_zone_wmarks();
KOSAKI Motohiroa6cccdc2011-05-24 17:11:33 -07005525 refresh_zone_stat_thresholds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 setup_per_zone_lowmem_reserve();
Rik van Riel556adec2008-10-18 20:26:34 -07005527 setup_per_zone_inactive_ratio();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 return 0;
5529}
Minchan Kimbc75d332009-06-16 15:32:48 -07005530module_init(init_per_zone_wmark_min)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531
5532/*
Ingo Molnarbc7d7492009-07-03 08:29:37 -05005533 * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 * that we can call two helper functions whenever min_free_kbytes
5535 * changes.
5536 */
Ingo Molnarbc7d7492009-07-03 08:29:37 -05005537int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005538 void __user *buffer, size_t *length, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005540 proc_dointvec(table, write, buffer, length, ppos);
Mel Gorman3b1d92c2007-05-06 14:49:30 -07005541 if (write)
Minchan Kimbc75d332009-06-16 15:32:48 -07005542 setup_per_zone_wmarks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 return 0;
5544}
5545
Christoph Lameter96146342006-07-03 00:24:13 -07005546#ifdef CONFIG_NUMA
5547int sysctl_min_unmapped_ratio_sysctl_handler(ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005548 void __user *buffer, size_t *length, loff_t *ppos)
Christoph Lameter96146342006-07-03 00:24:13 -07005549{
5550 struct zone *zone;
5551 int rc;
5552
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005553 rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
Christoph Lameter96146342006-07-03 00:24:13 -07005554 if (rc)
5555 return rc;
5556
5557 for_each_zone(zone)
Jiang Liub40da042013-02-22 16:33:52 -08005558 zone->min_unmapped_pages = (zone->managed_pages *
Christoph Lameter96146342006-07-03 00:24:13 -07005559 sysctl_min_unmapped_ratio) / 100;
5560 return 0;
5561}
Christoph Lameter0ff38492006-09-25 23:31:52 -07005562
5563int sysctl_min_slab_ratio_sysctl_handler(ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005564 void __user *buffer, size_t *length, loff_t *ppos)
Christoph Lameter0ff38492006-09-25 23:31:52 -07005565{
5566 struct zone *zone;
5567 int rc;
5568
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005569 rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
Christoph Lameter0ff38492006-09-25 23:31:52 -07005570 if (rc)
5571 return rc;
5572
5573 for_each_zone(zone)
Jiang Liub40da042013-02-22 16:33:52 -08005574 zone->min_slab_pages = (zone->managed_pages *
Christoph Lameter0ff38492006-09-25 23:31:52 -07005575 sysctl_min_slab_ratio) / 100;
5576 return 0;
5577}
Christoph Lameter96146342006-07-03 00:24:13 -07005578#endif
5579
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580/*
5581 * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
5582 * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
5583 * whenever sysctl_lowmem_reserve_ratio changes.
5584 *
5585 * The reserve ratio obviously has absolutely no relation with the
Mel Gorman41858962009-06-16 15:32:12 -07005586 * minimum watermarks. The lowmem reserve ratio can only make sense
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 * if in function of the boot time zone sizes.
5588 */
5589int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005590 void __user *buffer, size_t *length, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005592 proc_dointvec_minmax(table, write, buffer, length, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 setup_per_zone_lowmem_reserve();
5594 return 0;
5595}
5596
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08005597/*
5598 * percpu_pagelist_fraction - changes the pcp->high for each zone on each
5599 * cpu. It is the fraction of total pages in each zone that a hot per cpu pagelist
5600 * can have before it gets flushed back to buddy allocator.
5601 */
5602
5603int percpu_pagelist_fraction_sysctl_handler(ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005604 void __user *buffer, size_t *length, loff_t *ppos)
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08005605{
5606 struct zone *zone;
5607 unsigned int cpu;
5608 int ret;
5609
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005610 ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
Sasha Levin93278812012-05-10 13:01:44 -07005611 if (!write || (ret < 0))
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08005612 return ret;
Dimitri Sivanich364df0e2009-06-23 12:37:04 -07005613 for_each_populated_zone(zone) {
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09005614 for_each_possible_cpu(cpu) {
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08005615 unsigned long high;
Jiang Liub40da042013-02-22 16:33:52 -08005616 high = zone->managed_pages / percpu_pagelist_fraction;
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09005617 setup_pagelist_highmark(
5618 per_cpu_ptr(zone->pageset, cpu), high);
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08005619 }
5620 }
5621 return 0;
5622}
5623
David S. Millerf034b5d2006-08-24 03:08:07 -07005624int hashdist = HASHDIST_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625
5626#ifdef CONFIG_NUMA
5627static int __init set_hashdist(char *str)
5628{
5629 if (!str)
5630 return 0;
5631 hashdist = simple_strtoul(str, &str, 0);
5632 return 1;
5633}
5634__setup("hashdist=", set_hashdist);
5635#endif
5636
5637/*
5638 * allocate a large system hash table from bootmem
5639 * - it is assumed that the hash table must contain an exact power-of-2
5640 * quantity of entries
5641 * - limit is the number of hash buckets, not the total allocation size
5642 */
5643void *__init alloc_large_system_hash(const char *tablename,
5644 unsigned long bucketsize,
5645 unsigned long numentries,
5646 int scale,
5647 int flags,
5648 unsigned int *_hash_shift,
5649 unsigned int *_hash_mask,
Tim Bird31fe62b2012-05-23 13:33:35 +00005650 unsigned long low_limit,
5651 unsigned long high_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652{
Tim Bird31fe62b2012-05-23 13:33:35 +00005653 unsigned long long max = high_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 unsigned long log2qty, size;
5655 void *table = NULL;
5656
5657 /* allow the kernel cmdline to have a say */
5658 if (!numentries) {
5659 /* round applicable memory size up to nearest megabyte */
Andrew Morton04903662006-12-06 20:37:33 -08005660 numentries = nr_kernel_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
5662 numentries >>= 20 - PAGE_SHIFT;
5663 numentries <<= 20 - PAGE_SHIFT;
5664
5665 /* limit to 1 bucket per 2^scale bytes of low memory */
5666 if (scale > PAGE_SHIFT)
5667 numentries >>= (scale - PAGE_SHIFT);
5668 else
5669 numentries <<= (PAGE_SHIFT - scale);
Paul Mundt9ab37b82007-01-05 16:36:30 -08005670
5671 /* Make sure we've got at least a 0-order allocation.. */
Jan Beulich2c85f512009-09-21 17:03:07 -07005672 if (unlikely(flags & HASH_SMALL)) {
5673 /* Makes no sense without HASH_EARLY */
5674 WARN_ON(!(flags & HASH_EARLY));
5675 if (!(numentries >> *_hash_shift)) {
5676 numentries = 1UL << *_hash_shift;
5677 BUG_ON(!numentries);
5678 }
5679 } else if (unlikely((numentries * bucketsize) < PAGE_SIZE))
Paul Mundt9ab37b82007-01-05 16:36:30 -08005680 numentries = PAGE_SIZE / bucketsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 }
John Hawkes6e692ed2006-03-25 03:08:02 -08005682 numentries = roundup_pow_of_two(numentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683
5684 /* limit allocation size to 1/16 total memory by default */
5685 if (max == 0) {
5686 max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
5687 do_div(max, bucketsize);
5688 }
Dimitri Sivanich074b8512012-02-08 12:39:07 -08005689 max = min(max, 0x80000000ULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690
Tim Bird31fe62b2012-05-23 13:33:35 +00005691 if (numentries < low_limit)
5692 numentries = low_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 if (numentries > max)
5694 numentries = max;
5695
David Howellsf0d1b0b2006-12-08 02:37:49 -08005696 log2qty = ilog2(numentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697
5698 do {
5699 size = bucketsize << log2qty;
5700 if (flags & HASH_EARLY)
Jan Beulich74768ed2008-08-12 15:08:39 -07005701 table = alloc_bootmem_nopanic(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 else if (hashdist)
5703 table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
5704 else {
Eric Dumazet1037b832007-07-15 23:38:05 -07005705 /*
5706 * If bucketsize is not a power-of-two, we may free
Mel Gormana1dd2682009-06-16 15:32:19 -07005707 * some pages at the end of hash table which
5708 * alloc_pages_exact() automatically does
Eric Dumazet1037b832007-07-15 23:38:05 -07005709 */
Catalin Marinas264ef8a2009-07-07 10:33:01 +01005710 if (get_order(size) < MAX_ORDER) {
Mel Gormana1dd2682009-06-16 15:32:19 -07005711 table = alloc_pages_exact(size, GFP_ATOMIC);
Catalin Marinas264ef8a2009-07-07 10:33:01 +01005712 kmemleak_alloc(table, size, 1, GFP_ATOMIC);
5713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 }
5715 } while (!table && size > PAGE_SIZE && --log2qty);
5716
5717 if (!table)
5718 panic("Failed to allocate %s hash table\n", tablename);
5719
Robin Holtf241e6602010-10-07 12:59:26 -07005720 printk(KERN_INFO "%s hash table entries: %ld (order: %d, %lu bytes)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 tablename,
Robin Holtf241e6602010-10-07 12:59:26 -07005722 (1UL << log2qty),
David Howellsf0d1b0b2006-12-08 02:37:49 -08005723 ilog2(size) - PAGE_SHIFT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 size);
5725
5726 if (_hash_shift)
5727 *_hash_shift = log2qty;
5728 if (_hash_mask)
5729 *_hash_mask = (1 << log2qty) - 1;
5730
5731 return table;
5732}
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -08005733
Mel Gorman835c1342007-10-16 01:25:47 -07005734/* Return a pointer to the bitmap storing bits affecting a block of pages */
5735static inline unsigned long *get_pageblock_bitmap(struct zone *zone,
5736 unsigned long pfn)
5737{
5738#ifdef CONFIG_SPARSEMEM
5739 return __pfn_to_section(pfn)->pageblock_flags;
5740#else
5741 return zone->pageblock_flags;
5742#endif /* CONFIG_SPARSEMEM */
5743}
Andrew Morton6220ec72006-10-19 23:29:05 -07005744
Mel Gorman835c1342007-10-16 01:25:47 -07005745static inline int pfn_to_bitidx(struct zone *zone, unsigned long pfn)
5746{
5747#ifdef CONFIG_SPARSEMEM
5748 pfn &= (PAGES_PER_SECTION-1);
Mel Gormand9c23402007-10-16 01:26:01 -07005749 return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
Mel Gorman835c1342007-10-16 01:25:47 -07005750#else
Laura Abbottc060f942013-01-11 14:31:51 -08005751 pfn = pfn - round_down(zone->zone_start_pfn, pageblock_nr_pages);
Mel Gormand9c23402007-10-16 01:26:01 -07005752 return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
Mel Gorman835c1342007-10-16 01:25:47 -07005753#endif /* CONFIG_SPARSEMEM */
5754}
5755
5756/**
Mel Gormand9c23402007-10-16 01:26:01 -07005757 * get_pageblock_flags_group - Return the requested group of flags for the pageblock_nr_pages block of pages
Mel Gorman835c1342007-10-16 01:25:47 -07005758 * @page: The page within the block of interest
5759 * @start_bitidx: The first bit of interest to retrieve
5760 * @end_bitidx: The last bit of interest
5761 * returns pageblock_bits flags
5762 */
5763unsigned long get_pageblock_flags_group(struct page *page,
5764 int start_bitidx, int end_bitidx)
5765{
5766 struct zone *zone;
5767 unsigned long *bitmap;
5768 unsigned long pfn, bitidx;
5769 unsigned long flags = 0;
5770 unsigned long value = 1;
5771
5772 zone = page_zone(page);
5773 pfn = page_to_pfn(page);
5774 bitmap = get_pageblock_bitmap(zone, pfn);
5775 bitidx = pfn_to_bitidx(zone, pfn);
5776
5777 for (; start_bitidx <= end_bitidx; start_bitidx++, value <<= 1)
5778 if (test_bit(bitidx + start_bitidx, bitmap))
5779 flags |= value;
5780
5781 return flags;
5782}
5783
5784/**
Mel Gormand9c23402007-10-16 01:26:01 -07005785 * set_pageblock_flags_group - Set the requested group of flags for a pageblock_nr_pages block of pages
Mel Gorman835c1342007-10-16 01:25:47 -07005786 * @page: The page within the block of interest
5787 * @start_bitidx: The first bit of interest
5788 * @end_bitidx: The last bit of interest
5789 * @flags: The flags to set
5790 */
5791void set_pageblock_flags_group(struct page *page, unsigned long flags,
5792 int start_bitidx, int end_bitidx)
5793{
5794 struct zone *zone;
5795 unsigned long *bitmap;
5796 unsigned long pfn, bitidx;
5797 unsigned long value = 1;
5798
5799 zone = page_zone(page);
5800 pfn = page_to_pfn(page);
5801 bitmap = get_pageblock_bitmap(zone, pfn);
5802 bitidx = pfn_to_bitidx(zone, pfn);
Cody P Schafer108bcc92013-02-22 16:35:23 -08005803 VM_BUG_ON(!zone_spans_pfn(zone, pfn));
Mel Gorman835c1342007-10-16 01:25:47 -07005804
5805 for (; start_bitidx <= end_bitidx; start_bitidx++, value <<= 1)
5806 if (flags & value)
5807 __set_bit(bitidx + start_bitidx, bitmap);
5808 else
5809 __clear_bit(bitidx + start_bitidx, bitmap);
5810}
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -07005811
5812/*
Minchan Kim80934512012-07-31 16:43:01 -07005813 * This function checks whether pageblock includes unmovable pages or not.
5814 * If @count is not zero, it is okay to include less @count unmovable pages
5815 *
5816 * PageLRU check wihtout isolation or lru_lock could race so that
5817 * MIGRATE_MOVABLE block might include unmovable pages. It means you can't
5818 * expect this function should be exact.
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -07005819 */
Wen Congyangb023f462012-12-11 16:00:45 -08005820bool has_unmovable_pages(struct zone *zone, struct page *page, int count,
5821 bool skip_hwpoisoned_pages)
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005822{
5823 unsigned long pfn, iter, found;
Michal Nazarewicz47118af2011-12-29 13:09:50 +01005824 int mt;
5825
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005826 /*
5827 * For avoiding noise data, lru_add_drain_all() should be called
Minchan Kim80934512012-07-31 16:43:01 -07005828 * If ZONE_MOVABLE, the zone never contains unmovable pages
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005829 */
5830 if (zone_idx(zone) == ZONE_MOVABLE)
Minchan Kim80934512012-07-31 16:43:01 -07005831 return false;
Michal Nazarewicz47118af2011-12-29 13:09:50 +01005832 mt = get_pageblock_migratetype(page);
5833 if (mt == MIGRATE_MOVABLE || is_migrate_cma(mt))
Minchan Kim80934512012-07-31 16:43:01 -07005834 return false;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005835
5836 pfn = page_to_pfn(page);
5837 for (found = 0, iter = 0; iter < pageblock_nr_pages; iter++) {
5838 unsigned long check = pfn + iter;
5839
Namhyung Kim29723fc2011-02-25 14:44:25 -08005840 if (!pfn_valid_within(check))
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005841 continue;
Namhyung Kim29723fc2011-02-25 14:44:25 -08005842
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005843 page = pfn_to_page(check);
Minchan Kim97d255c2012-07-31 16:42:59 -07005844 /*
5845 * We can't use page_count without pin a page
5846 * because another CPU can free compound page.
5847 * This check already skips compound tails of THP
5848 * because their page->_count is zero at all time.
5849 */
5850 if (!atomic_read(&page->_count)) {
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005851 if (PageBuddy(page))
5852 iter += (1 << page_order(page)) - 1;
5853 continue;
5854 }
Minchan Kim97d255c2012-07-31 16:42:59 -07005855
Wen Congyangb023f462012-12-11 16:00:45 -08005856 /*
5857 * The HWPoisoned page may be not in buddy system, and
5858 * page_count() is not 0.
5859 */
5860 if (skip_hwpoisoned_pages && PageHWPoison(page))
5861 continue;
5862
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005863 if (!PageLRU(page))
5864 found++;
5865 /*
5866 * If there are RECLAIMABLE pages, we need to check it.
5867 * But now, memory offline itself doesn't call shrink_slab()
5868 * and it still to be fixed.
5869 */
5870 /*
5871 * If the page is not RAM, page_count()should be 0.
5872 * we don't need more check. This is an _used_ not-movable page.
5873 *
5874 * The problematic thing here is PG_reserved pages. PG_reserved
5875 * is set to both of a memory hole page and a _used_ kernel
5876 * page at boot.
5877 */
5878 if (found > count)
Minchan Kim80934512012-07-31 16:43:01 -07005879 return true;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005880 }
Minchan Kim80934512012-07-31 16:43:01 -07005881 return false;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005882}
5883
5884bool is_pageblock_removable_nolock(struct page *page)
5885{
Michal Hocko656a0702012-01-20 14:33:58 -08005886 struct zone *zone;
5887 unsigned long pfn;
Michal Hocko687875f2012-01-20 14:33:55 -08005888
5889 /*
5890 * We have to be careful here because we are iterating over memory
5891 * sections which are not zone aware so we might end up outside of
5892 * the zone but still within the section.
Michal Hocko656a0702012-01-20 14:33:58 -08005893 * We have to take care about the node as well. If the node is offline
5894 * its NODE_DATA will be NULL - see page_zone.
Michal Hocko687875f2012-01-20 14:33:55 -08005895 */
Michal Hocko656a0702012-01-20 14:33:58 -08005896 if (!node_online(page_to_nid(page)))
5897 return false;
5898
5899 zone = page_zone(page);
5900 pfn = page_to_pfn(page);
Cody P Schafer108bcc92013-02-22 16:35:23 -08005901 if (!zone_spans_pfn(zone, pfn))
Michal Hocko687875f2012-01-20 14:33:55 -08005902 return false;
5903
Wen Congyangb023f462012-12-11 16:00:45 -08005904 return !has_unmovable_pages(zone, page, 0, true);
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -07005905}
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07005906
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01005907#ifdef CONFIG_CMA
5908
5909static unsigned long pfn_max_align_down(unsigned long pfn)
5910{
5911 return pfn & ~(max_t(unsigned long, MAX_ORDER_NR_PAGES,
5912 pageblock_nr_pages) - 1);
5913}
5914
5915static unsigned long pfn_max_align_up(unsigned long pfn)
5916{
5917 return ALIGN(pfn, max_t(unsigned long, MAX_ORDER_NR_PAGES,
5918 pageblock_nr_pages));
5919}
5920
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01005921/* [start, end) must belong to a single zone. */
Mel Gormanbb13ffe2012-10-08 16:32:41 -07005922static int __alloc_contig_migrate_range(struct compact_control *cc,
5923 unsigned long start, unsigned long end)
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01005924{
5925 /* This function is based on compact_zone() from compaction.c. */
Minchan Kimbeb51ea2012-10-08 16:33:51 -07005926 unsigned long nr_reclaimed;
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01005927 unsigned long pfn = start;
5928 unsigned int tries = 0;
5929 int ret = 0;
5930
Marek Szyprowskibe49a6e2012-12-12 13:51:19 -08005931 migrate_prep();
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01005932
Mel Gormanbb13ffe2012-10-08 16:32:41 -07005933 while (pfn < end || !list_empty(&cc->migratepages)) {
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01005934 if (fatal_signal_pending(current)) {
5935 ret = -EINTR;
5936 break;
5937 }
5938
Mel Gormanbb13ffe2012-10-08 16:32:41 -07005939 if (list_empty(&cc->migratepages)) {
5940 cc->nr_migratepages = 0;
5941 pfn = isolate_migratepages_range(cc->zone, cc,
Minchan Kime46a2872012-10-08 16:33:48 -07005942 pfn, end, true);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01005943 if (!pfn) {
5944 ret = -EINTR;
5945 break;
5946 }
5947 tries = 0;
5948 } else if (++tries == 5) {
5949 ret = ret < 0 ? ret : -EBUSY;
5950 break;
5951 }
5952
Minchan Kimbeb51ea2012-10-08 16:33:51 -07005953 nr_reclaimed = reclaim_clean_pages_from_list(cc->zone,
5954 &cc->migratepages);
5955 cc->nr_migratepages -= nr_reclaimed;
Minchan Kim02c6de82012-10-08 16:31:55 -07005956
Hugh Dickins9c620e22013-02-22 16:35:14 -08005957 ret = migrate_pages(&cc->migratepages, alloc_migrate_target,
5958 0, MIGRATE_SYNC, MR_CMA);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01005959 }
Srinivas Pandruvada2a6f5122013-02-22 16:32:09 -08005960 if (ret < 0) {
5961 putback_movable_pages(&cc->migratepages);
5962 return ret;
5963 }
5964 return 0;
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01005965}
5966
5967/**
5968 * alloc_contig_range() -- tries to allocate given range of pages
5969 * @start: start PFN to allocate
5970 * @end: one-past-the-last PFN to allocate
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02005971 * @migratetype: migratetype of the underlaying pageblocks (either
5972 * #MIGRATE_MOVABLE or #MIGRATE_CMA). All pageblocks
5973 * in range must have the same migratetype and it must
5974 * be either of the two.
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01005975 *
5976 * The PFN range does not have to be pageblock or MAX_ORDER_NR_PAGES
5977 * aligned, however it's the caller's responsibility to guarantee that
5978 * we are the only thread that changes migrate type of pageblocks the
5979 * pages fall in.
5980 *
5981 * The PFN range must belong to a single zone.
5982 *
5983 * Returns zero on success or negative error code. On success all
5984 * pages which PFN is in [start, end) are allocated for the caller and
5985 * need to be freed with free_contig_range().
5986 */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02005987int alloc_contig_range(unsigned long start, unsigned long end,
5988 unsigned migratetype)
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01005989{
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01005990 unsigned long outer_start, outer_end;
5991 int ret = 0, order;
5992
Mel Gormanbb13ffe2012-10-08 16:32:41 -07005993 struct compact_control cc = {
5994 .nr_migratepages = 0,
5995 .order = -1,
5996 .zone = page_zone(pfn_to_page(start)),
5997 .sync = true,
5998 .ignore_skip_hint = true,
5999 };
6000 INIT_LIST_HEAD(&cc.migratepages);
6001
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006002 /*
6003 * What we do here is we mark all pageblocks in range as
6004 * MIGRATE_ISOLATE. Because pageblock and max order pages may
6005 * have different sizes, and due to the way page allocator
6006 * work, we align the range to biggest of the two pages so
6007 * that page allocator won't try to merge buddies from
6008 * different pageblocks and change MIGRATE_ISOLATE to some
6009 * other migration type.
6010 *
6011 * Once the pageblocks are marked as MIGRATE_ISOLATE, we
6012 * migrate the pages from an unaligned range (ie. pages that
6013 * we are interested in). This will put all the pages in
6014 * range back to page allocator as MIGRATE_ISOLATE.
6015 *
6016 * When this is done, we take the pages in range from page
6017 * allocator removing them from the buddy system. This way
6018 * page allocator will never consider using them.
6019 *
6020 * This lets us mark the pageblocks back as
6021 * MIGRATE_CMA/MIGRATE_MOVABLE so that free pages in the
6022 * aligned range but not in the unaligned, original range are
6023 * put back to page allocator so that buddy can use them.
6024 */
6025
6026 ret = start_isolate_page_range(pfn_max_align_down(start),
Wen Congyangb023f462012-12-11 16:00:45 -08006027 pfn_max_align_up(end), migratetype,
6028 false);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006029 if (ret)
Bob Liu86a595f2012-10-25 13:37:56 -07006030 return ret;
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006031
Mel Gormanbb13ffe2012-10-08 16:32:41 -07006032 ret = __alloc_contig_migrate_range(&cc, start, end);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006033 if (ret)
6034 goto done;
6035
6036 /*
6037 * Pages from [start, end) are within a MAX_ORDER_NR_PAGES
6038 * aligned blocks that are marked as MIGRATE_ISOLATE. What's
6039 * more, all pages in [start, end) are free in page allocator.
6040 * What we are going to do is to allocate all pages from
6041 * [start, end) (that is remove them from page allocator).
6042 *
6043 * The only problem is that pages at the beginning and at the
6044 * end of interesting range may be not aligned with pages that
6045 * page allocator holds, ie. they can be part of higher order
6046 * pages. Because of this, we reserve the bigger range and
6047 * once this is done free the pages we are not interested in.
6048 *
6049 * We don't have to hold zone->lock here because the pages are
6050 * isolated thus they won't get removed from buddy.
6051 */
6052
6053 lru_add_drain_all();
6054 drain_all_pages();
6055
6056 order = 0;
6057 outer_start = start;
6058 while (!PageBuddy(pfn_to_page(outer_start))) {
6059 if (++order >= MAX_ORDER) {
6060 ret = -EBUSY;
6061 goto done;
6062 }
6063 outer_start &= ~0UL << order;
6064 }
6065
6066 /* Make sure the range is really isolated. */
Wen Congyangb023f462012-12-11 16:00:45 -08006067 if (test_pages_isolated(outer_start, end, false)) {
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006068 pr_warn("alloc_contig_range test_pages_isolated(%lx, %lx) failed\n",
6069 outer_start, end);
6070 ret = -EBUSY;
6071 goto done;
6072 }
6073
Marek Szyprowski49f223a2012-01-25 12:49:24 +01006074
6075 /* Grab isolated pages from freelists. */
Mel Gormanbb13ffe2012-10-08 16:32:41 -07006076 outer_end = isolate_freepages_range(&cc, outer_start, end);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006077 if (!outer_end) {
6078 ret = -EBUSY;
6079 goto done;
6080 }
6081
6082 /* Free head and tail (if any) */
6083 if (start != outer_start)
6084 free_contig_range(outer_start, start - outer_start);
6085 if (end != outer_end)
6086 free_contig_range(end, outer_end - end);
6087
6088done:
6089 undo_isolate_page_range(pfn_max_align_down(start),
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02006090 pfn_max_align_up(end), migratetype);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006091 return ret;
6092}
6093
6094void free_contig_range(unsigned long pfn, unsigned nr_pages)
6095{
Marek Szyprowskibcc2b022012-12-20 15:05:18 -08006096 unsigned int count = 0;
6097
6098 for (; nr_pages--; pfn++) {
6099 struct page *page = pfn_to_page(pfn);
6100
6101 count += page_count(page) != 1;
6102 __free_page(page);
6103 }
6104 WARN(count != 0, "%d pages are still in use!\n", count);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006105}
6106#endif
6107
Jiang Liu4ed7e022012-07-31 16:43:35 -07006108#ifdef CONFIG_MEMORY_HOTPLUG
6109static int __meminit __zone_pcp_update(void *data)
6110{
6111 struct zone *zone = data;
6112 int cpu;
Peter Zijlstra2b2430e2009-07-03 08:44:37 -05006113 unsigned long flags;
Jiang Liu4ed7e022012-07-31 16:43:35 -07006114
6115 for_each_possible_cpu(cpu) {
6116 struct per_cpu_pageset *pset;
6117 struct per_cpu_pages *pcp;
Peter Zijlstra2b2430e2009-07-03 08:44:37 -05006118 LIST_HEAD(dst);
Jiang Liu4ed7e022012-07-31 16:43:35 -07006119
6120 pset = per_cpu_ptr(zone->pageset, cpu);
6121 pcp = &pset->pcp;
6122
Ingo Molnarbc7d7492009-07-03 08:29:37 -05006123 cpu_lock_irqsave(cpu, flags);
Peter Zijlstra2b2430e2009-07-03 08:44:37 -05006124 if (pcp->count > 0) {
6125 isolate_pcp_pages(pcp->count, pcp, &dst);
6126 free_pcppages_bulk(zone, pcp->count, &dst);
6127 }
Minchan Kim5a883812012-10-08 16:33:39 -07006128 drain_zonestat(zone, pset);
Ingo Molnarbc7d7492009-07-03 08:29:37 -05006129 cpu_unlock_irqrestore(cpu, flags);
Jiang Liu4ed7e022012-07-31 16:43:35 -07006130 }
6131 return 0;
6132}
6133
6134void __meminit zone_pcp_update(struct zone *zone)
6135{
6136 stop_machine(__zone_pcp_update, zone, NULL);
6137}
6138#endif
6139
Jiang Liu340175b2012-07-31 16:43:32 -07006140void zone_pcp_reset(struct zone *zone)
6141{
6142 unsigned long flags;
Minchan Kim5a883812012-10-08 16:33:39 -07006143 int cpu;
6144 struct per_cpu_pageset *pset;
Jiang Liu340175b2012-07-31 16:43:32 -07006145
6146 /* avoid races with drain_pages() */
Ingo Molnarbc7d7492009-07-03 08:29:37 -05006147 local_lock_irqsave(pa_lock, flags);
Jiang Liu340175b2012-07-31 16:43:32 -07006148 if (zone->pageset != &boot_pageset) {
Minchan Kim5a883812012-10-08 16:33:39 -07006149 for_each_online_cpu(cpu) {
6150 pset = per_cpu_ptr(zone->pageset, cpu);
6151 drain_zonestat(zone, pset);
6152 }
Jiang Liu340175b2012-07-31 16:43:32 -07006153 free_percpu(zone->pageset);
6154 zone->pageset = &boot_pageset;
6155 }
Ingo Molnarbc7d7492009-07-03 08:29:37 -05006156 local_unlock_irqrestore(pa_lock, flags);
Jiang Liu340175b2012-07-31 16:43:32 -07006157}
6158
Wen Congyang6dcd73d2012-12-11 16:01:01 -08006159#ifdef CONFIG_MEMORY_HOTREMOVE
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07006160/*
6161 * All pages in the range must be isolated before calling this.
6162 */
6163void
6164__offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
6165{
6166 struct page *page;
6167 struct zone *zone;
6168 int order, i;
6169 unsigned long pfn;
6170 unsigned long flags;
6171 /* find the first valid pfn */
6172 for (pfn = start_pfn; pfn < end_pfn; pfn++)
6173 if (pfn_valid(pfn))
6174 break;
6175 if (pfn == end_pfn)
6176 return;
6177 zone = page_zone(pfn_to_page(pfn));
6178 spin_lock_irqsave(&zone->lock, flags);
6179 pfn = start_pfn;
6180 while (pfn < end_pfn) {
6181 if (!pfn_valid(pfn)) {
6182 pfn++;
6183 continue;
6184 }
6185 page = pfn_to_page(pfn);
Wen Congyangb023f462012-12-11 16:00:45 -08006186 /*
6187 * The HWPoisoned page may be not in buddy system, and
6188 * page_count() is not 0.
6189 */
6190 if (unlikely(!PageBuddy(page) && PageHWPoison(page))) {
6191 pfn++;
6192 SetPageReserved(page);
6193 continue;
6194 }
6195
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07006196 BUG_ON(page_count(page));
6197 BUG_ON(!PageBuddy(page));
6198 order = page_order(page);
6199#ifdef CONFIG_DEBUG_VM
6200 printk(KERN_INFO "remove from free list %lx %d %lx\n",
6201 pfn, 1 << order, end_pfn);
6202#endif
6203 list_del(&page->lru);
6204 rmv_page_order(page);
6205 zone->free_area[order].nr_free--;
Wanpeng Li76422572013-07-03 15:02:40 -07006206#ifdef CONFIG_HIGHMEM
6207 if (PageHighMem(page))
6208 totalhigh_pages -= 1 << order;
6209#endif
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07006210 for (i = 0; i < (1 << order); i++)
6211 SetPageReserved((page+i));
6212 pfn += (1 << order);
6213 }
6214 spin_unlock_irqrestore(&zone->lock, flags);
6215}
6216#endif
Wu Fengguang8d22ba12009-12-16 12:19:58 +01006217
6218#ifdef CONFIG_MEMORY_FAILURE
6219bool is_free_buddy_page(struct page *page)
6220{
6221 struct zone *zone = page_zone(page);
6222 unsigned long pfn = page_to_pfn(page);
6223 unsigned long flags;
6224 int order;
6225
6226 spin_lock_irqsave(&zone->lock, flags);
6227 for (order = 0; order < MAX_ORDER; order++) {
6228 struct page *page_head = page - (pfn & ((1 << order) - 1));
6229
6230 if (PageBuddy(page_head) && page_order(page_head) >= order)
6231 break;
6232 }
6233 spin_unlock_irqrestore(&zone->lock, flags);
6234
6235 return order < MAX_ORDER;
6236}
6237#endif
Wu Fengguang718a3822010-03-10 15:20:43 -08006238
Andrew Morton51300ce2012-05-29 15:06:44 -07006239static const struct trace_print_flags pageflag_names[] = {
Wu Fengguang718a3822010-03-10 15:20:43 -08006240 {1UL << PG_locked, "locked" },
6241 {1UL << PG_error, "error" },
6242 {1UL << PG_referenced, "referenced" },
6243 {1UL << PG_uptodate, "uptodate" },
6244 {1UL << PG_dirty, "dirty" },
6245 {1UL << PG_lru, "lru" },
6246 {1UL << PG_active, "active" },
6247 {1UL << PG_slab, "slab" },
6248 {1UL << PG_owner_priv_1, "owner_priv_1" },
6249 {1UL << PG_arch_1, "arch_1" },
6250 {1UL << PG_reserved, "reserved" },
6251 {1UL << PG_private, "private" },
6252 {1UL << PG_private_2, "private_2" },
6253 {1UL << PG_writeback, "writeback" },
6254#ifdef CONFIG_PAGEFLAGS_EXTENDED
6255 {1UL << PG_head, "head" },
6256 {1UL << PG_tail, "tail" },
6257#else
6258 {1UL << PG_compound, "compound" },
6259#endif
6260 {1UL << PG_swapcache, "swapcache" },
6261 {1UL << PG_mappedtodisk, "mappedtodisk" },
6262 {1UL << PG_reclaim, "reclaim" },
Wu Fengguang718a3822010-03-10 15:20:43 -08006263 {1UL << PG_swapbacked, "swapbacked" },
6264 {1UL << PG_unevictable, "unevictable" },
6265#ifdef CONFIG_MMU
6266 {1UL << PG_mlocked, "mlocked" },
6267#endif
6268#ifdef CONFIG_ARCH_USES_PG_UNCACHED
6269 {1UL << PG_uncached, "uncached" },
6270#endif
6271#ifdef CONFIG_MEMORY_FAILURE
6272 {1UL << PG_hwpoison, "hwpoison" },
6273#endif
Gavin Shanbe9cd872012-05-29 15:06:44 -07006274#ifdef CONFIG_TRANSPARENT_HUGEPAGE
6275 {1UL << PG_compound_lock, "compound_lock" },
6276#endif
Wu Fengguang718a3822010-03-10 15:20:43 -08006277};
6278
6279static void dump_page_flags(unsigned long flags)
6280{
6281 const char *delim = "";
6282 unsigned long mask;
6283 int i;
6284
Andrew Morton51300ce2012-05-29 15:06:44 -07006285 BUILD_BUG_ON(ARRAY_SIZE(pageflag_names) != __NR_PAGEFLAGS);
Johannes Weineracc50c12012-05-29 15:06:44 -07006286
Wu Fengguang718a3822010-03-10 15:20:43 -08006287 printk(KERN_ALERT "page flags: %#lx(", flags);
6288
6289 /* remove zone id */
6290 flags &= (1UL << NR_PAGEFLAGS) - 1;
6291
Andrew Morton51300ce2012-05-29 15:06:44 -07006292 for (i = 0; i < ARRAY_SIZE(pageflag_names) && flags; i++) {
Wu Fengguang718a3822010-03-10 15:20:43 -08006293
6294 mask = pageflag_names[i].mask;
6295 if ((flags & mask) != mask)
6296 continue;
6297
6298 flags &= ~mask;
6299 printk("%s%s", delim, pageflag_names[i].name);
6300 delim = "|";
6301 }
6302
6303 /* check for left over flags */
6304 if (flags)
6305 printk("%s%#lx", delim, flags);
6306
6307 printk(")\n");
6308}
6309
6310void dump_page(struct page *page)
6311{
6312 printk(KERN_ALERT
6313 "page:%p count:%d mapcount:%d mapping:%p index:%#lx\n",
Andrea Arcangeli4e9f64c2011-01-13 15:46:29 -08006314 page, atomic_read(&page->_count), page_mapcount(page),
Wu Fengguang718a3822010-03-10 15:20:43 -08006315 page->mapping, page->index);
6316 dump_page_flags(page->flags);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07006317 mem_cgroup_print_bad_page(page);
Wu Fengguang718a3822010-03-10 15:20:43 -08006318}