blob: b09ce5fe0cd225ffbcf1e63817bef142b7d00654 [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 Hiroyukia5d76b52007-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 Gruszkac0a32fc52012-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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Jiang Liu7ee3d4e2013-07-03 15:03:41 -070064#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#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
Cody P Schaferc8e251f2013-07-03 15:01:29 -070069/* prevent >1 _updater_ of zone percpu pageset ->high and ->batch fields */
70static DEFINE_MUTEX(pcp_batch_high_lock);
71
Lee Schermerhorn72812012010-05-26 14:44:56 -070072#ifdef CONFIG_USE_PERCPU_NUMA_NODE_ID
73DEFINE_PER_CPU(int, numa_node);
74EXPORT_PER_CPU_SYMBOL(numa_node);
75#endif
76
Lee Schermerhorn7aac7892010-05-26 14:45:00 -070077#ifdef CONFIG_HAVE_MEMORYLESS_NODES
78/*
79 * N.B., Do NOT reference the '_numa_mem_' per cpu variable directly.
80 * It will not be defined when CONFIG_HAVE_MEMORYLESS_NODES is not defined.
81 * Use the accessor functions set_numa_mem(), numa_mem_id() and cpu_to_mem()
82 * defined in <linux/topology.h>.
83 */
84DEFINE_PER_CPU(int, _numa_mem_); /* Kernel "local memory" node */
85EXPORT_PER_CPU_SYMBOL(_numa_mem_);
86#endif
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/*
Christoph Lameter13808912007-10-16 01:25:27 -070089 * Array of node states.
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 */
Christoph Lameter13808912007-10-16 01:25:27 -070091nodemask_t node_states[NR_NODE_STATES] __read_mostly = {
92 [N_POSSIBLE] = NODE_MASK_ALL,
93 [N_ONLINE] = { { [0] = 1UL } },
94#ifndef CONFIG_NUMA
95 [N_NORMAL_MEMORY] = { { [0] = 1UL } },
96#ifdef CONFIG_HIGHMEM
97 [N_HIGH_MEMORY] = { { [0] = 1UL } },
98#endif
Lai Jiangshan20b2f522012-12-12 13:52:00 -080099#ifdef CONFIG_MOVABLE_NODE
100 [N_MEMORY] = { { [0] = 1UL } },
101#endif
Christoph Lameter13808912007-10-16 01:25:27 -0700102 [N_CPU] = { { [0] = 1UL } },
103#endif /* NUMA */
104};
105EXPORT_SYMBOL(node_states);
106
Jiang Liuc3d5f5f2013-07-03 15:03:14 -0700107/* Protect totalram_pages and zone->managed_pages */
108static DEFINE_SPINLOCK(managed_page_count_lock);
109
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -0700110unsigned long totalram_pages __read_mostly;
Hideo AOKIcb45b0e2006-04-10 22:52:59 -0700111unsigned long totalreserve_pages __read_mostly;
Johannes Weinerab8fabd2012-01-10 15:07:42 -0800112/*
113 * When calculating the number of globally allowed dirty pages, there
114 * is a certain number of per-zone reserves that should not be
115 * considered dirtyable memory. This is the sum of those reserves
116 * over all existing zones that contribute dirtyable memory.
117 */
118unsigned long dirty_balance_reserve __read_mostly;
119
Hugh Dickins1b76b022012-05-11 01:00:07 -0700120int percpu_pagelist_fraction;
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +1000121gfp_t gfp_allowed_mask __read_mostly = GFP_BOOT_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800123#ifdef CONFIG_PM_SLEEP
124/*
125 * The following functions are used by the suspend/hibernate code to temporarily
126 * change gfp_allowed_mask in order to avoid using I/O during memory allocations
127 * while devices are suspended. To avoid races with the suspend/hibernate code,
128 * they should always be called with pm_mutex held (gfp_allowed_mask also should
129 * only be modified with pm_mutex held, unless the suspend/hibernate code is
130 * guaranteed not to run in parallel with that modification).
131 */
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100132
133static gfp_t saved_gfp_mask;
134
135void pm_restore_gfp_mask(void)
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800136{
137 WARN_ON(!mutex_is_locked(&pm_mutex));
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100138 if (saved_gfp_mask) {
139 gfp_allowed_mask = saved_gfp_mask;
140 saved_gfp_mask = 0;
141 }
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800142}
143
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100144void pm_restrict_gfp_mask(void)
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800145{
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800146 WARN_ON(!mutex_is_locked(&pm_mutex));
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100147 WARN_ON(saved_gfp_mask);
148 saved_gfp_mask = gfp_allowed_mask;
149 gfp_allowed_mask &= ~GFP_IOFS;
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800150}
Mel Gormanf90ac392012-01-10 15:07:15 -0800151
152bool pm_suspended_storage(void)
153{
154 if ((gfp_allowed_mask & GFP_IOFS) == GFP_IOFS)
155 return false;
156 return true;
157}
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800158#endif /* CONFIG_PM_SLEEP */
159
Mel Gormand9c23402007-10-16 01:26:01 -0700160#ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
161int pageblock_order __read_mostly;
162#endif
163
Hugh Dickinsd98c7a02006-02-14 13:52:59 -0800164static void __free_pages_ok(struct page *page, unsigned int order);
David Howellsa226f6c2006-01-06 00:11:08 -0800165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/*
167 * results with 256, 32 in the lowmem_reserve sysctl:
168 * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
169 * 1G machine -> (16M dma, 784M normal, 224M high)
170 * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
171 * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
172 * HIGHMEM allocation will (224M+784M)/256 of ram reserved in ZONE_DMA
Andi Kleena2f1b422005-11-05 17:25:53 +0100173 *
174 * TBD: should special case ZONE_DMA32 machines here - in those we normally
175 * don't need any ZONE_NORMAL reservation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 */
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700177int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1] = {
Christoph Lameter4b51d662007-02-10 01:43:10 -0800178#ifdef CONFIG_ZONE_DMA
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700179 256,
Christoph Lameter4b51d662007-02-10 01:43:10 -0800180#endif
Christoph Lameterfb0e7942006-09-25 23:31:13 -0700181#ifdef CONFIG_ZONE_DMA32
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700182 256,
Christoph Lameterfb0e7942006-09-25 23:31:13 -0700183#endif
Christoph Lametere53ef382006-09-25 23:31:14 -0700184#ifdef CONFIG_HIGHMEM
Mel Gorman2a1e2742007-07-17 04:03:12 -0700185 32,
Christoph Lametere53ef382006-09-25 23:31:14 -0700186#endif
Mel Gorman2a1e2742007-07-17 04:03:12 -0700187 32,
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700188};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190EXPORT_SYMBOL(totalram_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Helge Deller15ad7cd2006-12-06 20:40:36 -0800192static char * const zone_names[MAX_NR_ZONES] = {
Christoph Lameter4b51d662007-02-10 01:43:10 -0800193#ifdef CONFIG_ZONE_DMA
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700194 "DMA",
Christoph Lameter4b51d662007-02-10 01:43:10 -0800195#endif
Christoph Lameterfb0e7942006-09-25 23:31:13 -0700196#ifdef CONFIG_ZONE_DMA32
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700197 "DMA32",
Christoph Lameterfb0e7942006-09-25 23:31:13 -0700198#endif
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700199 "Normal",
Christoph Lametere53ef382006-09-25 23:31:14 -0700200#ifdef CONFIG_HIGHMEM
Mel Gorman2a1e2742007-07-17 04:03:12 -0700201 "HighMem",
Christoph Lametere53ef382006-09-25 23:31:14 -0700202#endif
Mel Gorman2a1e2742007-07-17 04:03:12 -0700203 "Movable",
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700204};
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206int min_free_kbytes = 1024;
Michal Hocko5f127332013-07-08 16:00:40 -0700207int user_min_free_kbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Jan Beulich2c85f512009-09-21 17:03:07 -0700209static unsigned long __meminitdata nr_kernel_pages;
210static unsigned long __meminitdata nr_all_pages;
Yasunori Gotoa3142c82007-05-08 00:23:07 -0700211static unsigned long __meminitdata dma_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Tejun Heo0ee332c2011-12-08 10:22:09 -0800213#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
214static unsigned long __meminitdata arch_zone_lowest_possible_pfn[MAX_NR_ZONES];
215static unsigned long __meminitdata arch_zone_highest_possible_pfn[MAX_NR_ZONES];
216static unsigned long __initdata required_kernelcore;
217static unsigned long __initdata required_movablecore;
218static unsigned long __meminitdata zone_movable_pfn[MAX_NUMNODES];
Mel Gormanc7132162006-09-27 01:49:43 -0700219
Tejun Heo0ee332c2011-12-08 10:22:09 -0800220/* movable_zone is the "real" zone pages in ZONE_MOVABLE are taken from */
221int movable_zone;
222EXPORT_SYMBOL(movable_zone);
223#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
Mel Gormanc7132162006-09-27 01:49:43 -0700224
Miklos Szeredi418508c2007-05-23 13:57:55 -0700225#if MAX_NUMNODES > 1
226int nr_node_ids __read_mostly = MAX_NUMNODES;
Christoph Lameter62bc62a2009-06-16 15:32:15 -0700227int nr_online_nodes __read_mostly = 1;
Miklos Szeredi418508c2007-05-23 13:57:55 -0700228EXPORT_SYMBOL(nr_node_ids);
Christoph Lameter62bc62a2009-06-16 15:32:15 -0700229EXPORT_SYMBOL(nr_online_nodes);
Miklos Szeredi418508c2007-05-23 13:57:55 -0700230#endif
231
Mel Gorman9ef9acb2007-10-16 01:25:54 -0700232int page_group_by_mobility_disabled __read_mostly;
233
Minchan Kimee6f5092012-07-31 16:43:50 -0700234void set_pageblock_migratetype(struct page *page, int migratetype)
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700235{
Mel Gorman49255c62009-06-16 15:31:58 -0700236
237 if (unlikely(page_group_by_mobility_disabled))
238 migratetype = MIGRATE_UNMOVABLE;
239
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700240 set_pageblock_flags_group(page, (unsigned long)migratetype,
241 PB_migrate, PB_migrate_end);
242}
243
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700244bool oom_killer_disabled __read_mostly;
245
Nick Piggin13e74442006-01-06 00:10:58 -0800246#ifdef CONFIG_DEBUG_VM
Dave Hansenc6a57e12005-10-29 18:16:52 -0700247static int page_outside_zone_boundaries(struct zone *zone, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Dave Hansenbdc8cb92005-10-29 18:16:53 -0700249 int ret = 0;
250 unsigned seq;
251 unsigned long pfn = page_to_pfn(page);
Cody P Schaferb5e6a5a2013-02-22 16:35:28 -0800252 unsigned long sp, start_pfn;
Dave Hansenc6a57e12005-10-29 18:16:52 -0700253
Dave Hansenbdc8cb92005-10-29 18:16:53 -0700254 do {
255 seq = zone_span_seqbegin(zone);
Cody P Schaferb5e6a5a2013-02-22 16:35:28 -0800256 start_pfn = zone->zone_start_pfn;
257 sp = zone->spanned_pages;
Cody P Schafer108bcc92013-02-22 16:35:23 -0800258 if (!zone_spans_pfn(zone, pfn))
Dave Hansenbdc8cb92005-10-29 18:16:53 -0700259 ret = 1;
260 } while (zone_span_seqretry(zone, seq));
261
Cody P Schaferb5e6a5a2013-02-22 16:35:28 -0800262 if (ret)
263 pr_err("page %lu outside zone [ %lu - %lu ]\n",
264 pfn, start_pfn, start_pfn + sp);
265
Dave Hansenbdc8cb92005-10-29 18:16:53 -0700266 return ret;
Dave Hansenc6a57e12005-10-29 18:16:52 -0700267}
268
269static int page_is_consistent(struct zone *zone, struct page *page)
270{
Andy Whitcroft14e07292007-05-06 14:49:14 -0700271 if (!pfn_valid_within(page_to_pfn(page)))
Dave Hansenc6a57e12005-10-29 18:16:52 -0700272 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (zone != page_zone(page))
Dave Hansenc6a57e12005-10-29 18:16:52 -0700274 return 0;
275
276 return 1;
277}
278/*
279 * Temporary debugging check for pages not lying within a given zone.
280 */
281static int bad_range(struct zone *zone, struct page *page)
282{
283 if (page_outside_zone_boundaries(zone, page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return 1;
Dave Hansenc6a57e12005-10-29 18:16:52 -0700285 if (!page_is_consistent(zone, page))
286 return 1;
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return 0;
289}
Nick Piggin13e74442006-01-06 00:10:58 -0800290#else
291static inline int bad_range(struct zone *zone, struct page *page)
292{
293 return 0;
294}
295#endif
296
Nick Piggin224abf92006-01-06 00:11:11 -0800297static void bad_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800299 static unsigned long resume;
300 static unsigned long nr_shown;
301 static unsigned long nr_unshown;
302
Wu Fengguang2a7684a2009-09-16 11:50:12 +0200303 /* Don't complain about poisoned pages */
304 if (PageHWPoison(page)) {
Mel Gorman22b751c2013-02-22 16:34:59 -0800305 page_mapcount_reset(page); /* remove PageBuddy */
Wu Fengguang2a7684a2009-09-16 11:50:12 +0200306 return;
307 }
308
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800309 /*
310 * Allow a burst of 60 reports, then keep quiet for that minute;
311 * or allow a steady drip of one report per second.
312 */
313 if (nr_shown == 60) {
314 if (time_before(jiffies, resume)) {
315 nr_unshown++;
316 goto out;
317 }
318 if (nr_unshown) {
Hugh Dickins1e9e6362009-01-06 14:40:13 -0800319 printk(KERN_ALERT
320 "BUG: Bad page state: %lu messages suppressed\n",
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800321 nr_unshown);
322 nr_unshown = 0;
323 }
324 nr_shown = 0;
325 }
326 if (nr_shown++ == 0)
327 resume = jiffies + 60 * HZ;
328
Hugh Dickins1e9e6362009-01-06 14:40:13 -0800329 printk(KERN_ALERT "BUG: Bad page state in process %s pfn:%05lx\n",
Hugh Dickins3dc14742009-01-06 14:40:08 -0800330 current->comm, page_to_pfn(page));
Wu Fengguang718a3822010-03-10 15:20:43 -0800331 dump_page(page);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700332
Dave Jones4f318882011-10-31 17:07:24 -0700333 print_modules();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 dump_stack();
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800335out:
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800336 /* Leave bad fields for debug, except PageBuddy could make trouble */
Mel Gorman22b751c2013-02-22 16:34:59 -0800337 page_mapcount_reset(page); /* remove PageBuddy */
Rusty Russell373d4d02013-01-21 17:17:39 +1030338 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341/*
342 * Higher-order pages are called "compound pages". They are structured thusly:
343 *
344 * The first PAGE_SIZE page is called the "head page".
345 *
346 * The remaining PAGE_SIZE pages are called "tail pages".
347 *
Wang Sheng-Hui6416b9f2011-11-17 10:53:50 +0100348 * All pages have PG_compound set. All tail pages have their ->first_page
349 * pointing at the head page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 *
Hugh Dickins41d78ba2006-02-14 13:52:58 -0800351 * The first tail page's ->lru.next holds the address of the compound page's
352 * put_page() function. Its ->lru.prev holds the order of allocation.
353 * This usage means that zero-order pages may not be compound.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 */
Hugh Dickinsd98c7a02006-02-14 13:52:59 -0800355
356static void free_compound_page(struct page *page)
357{
Christoph Lameterd85f3382007-05-06 14:49:39 -0700358 __free_pages_ok(page, compound_order(page));
Hugh Dickinsd98c7a02006-02-14 13:52:59 -0800359}
360
Andi Kleen01ad1c02008-07-23 21:27:46 -0700361void prep_compound_page(struct page *page, unsigned long order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 int i;
364 int nr_pages = 1 << order;
365
Andy Whitcroft33f2ef82006-12-06 20:33:32 -0800366 set_compound_page_dtor(page, free_compound_page);
Christoph Lameterd85f3382007-05-06 14:49:39 -0700367 set_compound_order(page, order);
Christoph Lameter6d777952007-05-06 14:49:40 -0700368 __SetPageHead(page);
Andy Whitcroft18229df2008-11-06 12:53:27 -0800369 for (i = 1; i < nr_pages; i++) {
370 struct page *p = page + i;
Christoph Lameterd85f3382007-05-06 14:49:39 -0700371 __SetPageTail(p);
Youquan Song58a84aa2011-12-08 14:34:18 -0800372 set_page_count(p, 0);
Christoph Lameterd85f3382007-05-06 14:49:39 -0700373 p->first_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375}
376
Andrea Arcangeli59ff4212011-01-13 15:46:38 -0800377/* update __split_huge_page_refcount if you change this function */
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800378static int destroy_compound_page(struct page *page, unsigned long order)
Andy Whitcroft18229df2008-11-06 12:53:27 -0800379{
380 int i;
381 int nr_pages = 1 << order;
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800382 int bad = 0;
Andy Whitcroft18229df2008-11-06 12:53:27 -0800383
Gavin Shan0bb2c762012-12-18 14:21:32 -0800384 if (unlikely(compound_order(page) != order)) {
Nick Piggin224abf92006-01-06 00:11:11 -0800385 bad_page(page);
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800386 bad++;
387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Christoph Lameter6d777952007-05-06 14:49:40 -0700389 __ClearPageHead(page);
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800390
Andy Whitcroft18229df2008-11-06 12:53:27 -0800391 for (i = 1; i < nr_pages; i++) {
392 struct page *p = page + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Alexey Zaytseve713a212009-01-10 02:47:57 +0300394 if (unlikely(!PageTail(p) || (p->first_page != page))) {
Nick Piggin224abf92006-01-06 00:11:11 -0800395 bad_page(page);
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800396 bad++;
397 }
Christoph Lameterd85f3382007-05-06 14:49:39 -0700398 __ClearPageTail(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800400
401 return bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Nick Piggin17cf4402006-03-22 00:08:41 -0800404static inline void prep_zero_page(struct page *page, int order, gfp_t gfp_flags)
405{
406 int i;
407
Andrew Morton6626c5d2006-03-22 00:08:42 -0800408 /*
409 * clear_highpage() will use KM_USER0, so it's a bug to use __GFP_ZERO
410 * and __GFP_HIGHMEM from hard or soft interrupt context.
411 */
Nick Piggin725d7042006-09-25 23:30:55 -0700412 VM_BUG_ON((gfp_flags & __GFP_HIGHMEM) && in_interrupt());
Nick Piggin17cf4402006-03-22 00:08:41 -0800413 for (i = 0; i < (1 << order); i++)
414 clear_highpage(page + i);
415}
416
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -0800417#ifdef CONFIG_DEBUG_PAGEALLOC
418unsigned int _debug_guardpage_minorder;
419
420static int __init debug_guardpage_minorder_setup(char *buf)
421{
422 unsigned long res;
423
424 if (kstrtoul(buf, 10, &res) < 0 || res > MAX_ORDER / 2) {
425 printk(KERN_ERR "Bad debug_guardpage_minorder value\n");
426 return 0;
427 }
428 _debug_guardpage_minorder = res;
429 printk(KERN_INFO "Setting debug_guardpage_minorder to %lu\n", res);
430 return 0;
431}
432__setup("debug_guardpage_minorder=", debug_guardpage_minorder_setup);
433
434static inline void set_page_guard_flag(struct page *page)
435{
436 __set_bit(PAGE_DEBUG_FLAG_GUARD, &page->debug_flags);
437}
438
439static inline void clear_page_guard_flag(struct page *page)
440{
441 __clear_bit(PAGE_DEBUG_FLAG_GUARD, &page->debug_flags);
442}
443#else
444static inline void set_page_guard_flag(struct page *page) { }
445static inline void clear_page_guard_flag(struct page *page) { }
446#endif
447
Andrew Morton6aa3001b22006-04-18 22:20:52 -0700448static inline void set_page_order(struct page *page, int order)
449{
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700450 set_page_private(page, order);
Nick Piggin676165a2006-04-10 11:21:48 +1000451 __SetPageBuddy(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
454static inline void rmv_page_order(struct page *page)
455{
Nick Piggin676165a2006-04-10 11:21:48 +1000456 __ClearPageBuddy(page);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700457 set_page_private(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
460/*
461 * Locate the struct page for both the matching buddy in our
462 * pair (buddy1) and the combined O(n+1) page they form (page).
463 *
464 * 1) Any buddy B1 will have an order O twin B2 which satisfies
465 * the following equation:
466 * B2 = B1 ^ (1 << O)
467 * For example, if the starting buddy (buddy2) is #8 its order
468 * 1 buddy is #10:
469 * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
470 *
471 * 2) Any buddy B will have an order O+1 parent P which
472 * satisfies the following equation:
473 * P = B & ~(1 << O)
474 *
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200475 * Assumption: *_mem_map is contiguous at least up to MAX_ORDER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477static inline unsigned long
KyongHo Cho43506fa2011-01-13 15:47:24 -0800478__find_buddy_index(unsigned long page_idx, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
KyongHo Cho43506fa2011-01-13 15:47:24 -0800480 return page_idx ^ (1 << order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
483/*
484 * This function checks whether a page is free && is the buddy
485 * we can do coalesce a page and its buddy if
Nick Piggin13e74442006-01-06 00:10:58 -0800486 * (a) the buddy is not in a hole &&
Nick Piggin676165a2006-04-10 11:21:48 +1000487 * (b) the buddy is in the buddy system &&
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700488 * (c) a page and its buddy have the same order &&
489 * (d) a page and its buddy are in the same zone.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 *
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800491 * For recording whether a page is in the buddy system, we set ->_mapcount -2.
492 * Setting, clearing, and testing _mapcount -2 is serialized by zone->lock.
Nick Piggin676165a2006-04-10 11:21:48 +1000493 *
494 * For recording page's order, we use page_private(page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 */
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700496static inline int page_is_buddy(struct page *page, struct page *buddy,
497 int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Andy Whitcroft14e07292007-05-06 14:49:14 -0700499 if (!pfn_valid_within(page_to_pfn(buddy)))
Nick Piggin13e74442006-01-06 00:10:58 -0800500 return 0;
Nick Piggin13e74442006-01-06 00:10:58 -0800501
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700502 if (page_zone_id(page) != page_zone_id(buddy))
503 return 0;
504
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -0800505 if (page_is_guard(buddy) && page_order(buddy) == order) {
506 VM_BUG_ON(page_count(buddy) != 0);
507 return 1;
508 }
509
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700510 if (PageBuddy(buddy) && page_order(buddy) == order) {
Nick Piggina3af9c32009-06-16 15:32:10 -0700511 VM_BUG_ON(page_count(buddy) != 0);
Andrew Morton6aa3001b22006-04-18 22:20:52 -0700512 return 1;
Nick Piggin676165a2006-04-10 11:21:48 +1000513 }
Andrew Morton6aa3001b22006-04-18 22:20:52 -0700514 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
517/*
518 * Freeing function for a buddy system allocator.
519 *
520 * The concept of a buddy system is to maintain direct-mapped table
521 * (containing bit values) for memory blocks of various "orders".
522 * The bottom level table contains the map for the smallest allocatable
523 * units of memory (here, pages), and each level above it describes
524 * pairs of units from the levels below, hence, "buddies".
525 * At a high level, all that happens here is marking the table entry
526 * at the bottom level available, and propagating the changes upward
527 * as necessary, plus some accounting needed to play nicely with other
528 * parts of the VM system.
529 * At each level, we keep a list of pages, which are heads of continuous
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800530 * free pages of length of (1 << order) and marked with _mapcount -2. Page's
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700531 * order is recorded in page_private(page) field.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 * So when we are allocating or freeing one, we can derive the state of the
Michal Nazarewicz5f63b722012-01-11 15:16:11 +0100533 * other. That is, if we allocate a small block, and both were
534 * free, the remainder of the region must be split into blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 * If a block is freed, and its buddy is also free, then this
Michal Nazarewicz5f63b722012-01-11 15:16:11 +0100536 * triggers coalescing into a block of larger size.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 *
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +0100538 * -- nyc
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 */
540
Nick Piggin48db57f2006-01-08 01:00:42 -0800541static inline void __free_one_page(struct page *page,
Mel Gormaned0ae212009-06-16 15:32:07 -0700542 struct zone *zone, unsigned int order,
543 int migratetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 unsigned long page_idx;
Corrado Zoccolo6dda9d52010-05-24 14:31:54 -0700546 unsigned long combined_idx;
KyongHo Cho43506fa2011-01-13 15:47:24 -0800547 unsigned long uninitialized_var(buddy_idx);
Corrado Zoccolo6dda9d52010-05-24 14:31:54 -0700548 struct page *buddy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Cody P Schaferd29bb972013-02-22 16:35:25 -0800550 VM_BUG_ON(!zone_is_initialized(zone));
551
Nick Piggin224abf92006-01-06 00:11:11 -0800552 if (unlikely(PageCompound(page)))
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800553 if (unlikely(destroy_compound_page(page, order)))
554 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Mel Gormaned0ae212009-06-16 15:32:07 -0700556 VM_BUG_ON(migratetype == -1);
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 page_idx = page_to_pfn(page) & ((1 << MAX_ORDER) - 1);
559
Mel Gormanf2260e62009-06-16 15:32:13 -0700560 VM_BUG_ON(page_idx & ((1 << order) - 1));
Nick Piggin725d7042006-09-25 23:30:55 -0700561 VM_BUG_ON(bad_range(zone, page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 while (order < MAX_ORDER-1) {
KyongHo Cho43506fa2011-01-13 15:47:24 -0800564 buddy_idx = __find_buddy_index(page_idx, order);
565 buddy = page + (buddy_idx - page_idx);
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700566 if (!page_is_buddy(page, buddy, order))
Andy Whitcroft3c82d0c2008-07-23 21:27:11 -0700567 break;
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -0800568 /*
569 * Our buddy is free or it is CONFIG_DEBUG_PAGEALLOC guard page,
570 * merge with it and move up one order.
571 */
572 if (page_is_guard(buddy)) {
573 clear_page_guard_flag(buddy);
574 set_page_private(page, 0);
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -0700575 __mod_zone_freepage_state(zone, 1 << order,
576 migratetype);
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -0800577 } else {
578 list_del(&buddy->lru);
579 zone->free_area[order].nr_free--;
580 rmv_page_order(buddy);
581 }
KyongHo Cho43506fa2011-01-13 15:47:24 -0800582 combined_idx = buddy_idx & page_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 page = page + (combined_idx - page_idx);
584 page_idx = combined_idx;
585 order++;
586 }
587 set_page_order(page, order);
Corrado Zoccolo6dda9d52010-05-24 14:31:54 -0700588
589 /*
590 * If this is not the largest possible page, check if the buddy
591 * of the next-highest order is free. If it is, it's possible
592 * that pages are being freed that will coalesce soon. In case,
593 * that is happening, add the free page to the tail of the list
594 * so it's less likely to be used soon and more likely to be merged
595 * as a higher order page
596 */
Mel Gormanb7f50cf2010-10-26 14:21:11 -0700597 if ((order < MAX_ORDER-2) && pfn_valid_within(page_to_pfn(buddy))) {
Corrado Zoccolo6dda9d52010-05-24 14:31:54 -0700598 struct page *higher_page, *higher_buddy;
KyongHo Cho43506fa2011-01-13 15:47:24 -0800599 combined_idx = buddy_idx & page_idx;
600 higher_page = page + (combined_idx - page_idx);
601 buddy_idx = __find_buddy_index(combined_idx, order + 1);
Li Haifeng0ba8f2d2012-09-17 14:09:21 -0700602 higher_buddy = higher_page + (buddy_idx - combined_idx);
Corrado Zoccolo6dda9d52010-05-24 14:31:54 -0700603 if (page_is_buddy(higher_page, higher_buddy, order + 1)) {
604 list_add_tail(&page->lru,
605 &zone->free_area[order].free_list[migratetype]);
606 goto out;
607 }
608 }
609
610 list_add(&page->lru, &zone->free_area[order].free_list[migratetype]);
611out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 zone->free_area[order].nr_free++;
613}
614
Nick Piggin224abf92006-01-06 00:11:11 -0800615static inline int free_pages_check(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Nick Piggin92be2e332006-01-06 00:10:57 -0800617 if (unlikely(page_mapcount(page) |
618 (page->mapping != NULL) |
Nick Piggina3af9c32009-06-16 15:32:10 -0700619 (atomic_read(&page->_count) != 0) |
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -0700620 (page->flags & PAGE_FLAGS_CHECK_AT_FREE) |
621 (mem_cgroup_bad_page_check(page)))) {
Nick Piggin224abf92006-01-06 00:11:11 -0800622 bad_page(page);
Hugh Dickins79f4b7b2009-01-06 14:40:05 -0800623 return 1;
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800624 }
Mel Gorman22b751c2013-02-22 16:34:59 -0800625 page_nid_reset_last(page);
Hugh Dickins79f4b7b2009-01-06 14:40:05 -0800626 if (page->flags & PAGE_FLAGS_CHECK_AT_PREP)
627 page->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
628 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
631/*
Mel Gorman5f8dcc22009-09-21 17:03:19 -0700632 * Frees a number of pages from the PCP lists
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 * Assumes all pages on list are in same zone, and of same order.
Renaud Lienhart207f36e2005-09-10 00:26:59 -0700634 * count is the number of pages to free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 *
636 * If the zone was previously in an "all pages pinned" state then look to
637 * see if this freeing clears that state.
638 *
639 * And clear the zone's pages_scanned counter, to hold off the "all pages are
640 * pinned" detection logic.
641 */
Mel Gorman5f8dcc22009-09-21 17:03:19 -0700642static void free_pcppages_bulk(struct zone *zone, int count,
643 struct per_cpu_pages *pcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Mel Gorman5f8dcc22009-09-21 17:03:19 -0700645 int migratetype = 0;
Mel Gormana6f9edd2009-09-21 17:03:20 -0700646 int batch_free = 0;
Mel Gorman72853e22010-09-09 16:38:16 -0700647 int to_free = count;
Mel Gorman5f8dcc22009-09-21 17:03:19 -0700648
Nick Pigginc54ad302006-01-06 00:10:56 -0800649 spin_lock(&zone->lock);
KOSAKI Motohiro93e4a892010-03-05 13:41:55 -0800650 zone->all_unreclaimable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 zone->pages_scanned = 0;
Mel Gormanf2260e62009-06-16 15:32:13 -0700652
Mel Gorman72853e22010-09-09 16:38:16 -0700653 while (to_free) {
Nick Piggin48db57f2006-01-08 01:00:42 -0800654 struct page *page;
Mel Gorman5f8dcc22009-09-21 17:03:19 -0700655 struct list_head *list;
Nick Piggin48db57f2006-01-08 01:00:42 -0800656
Mel Gorman5f8dcc22009-09-21 17:03:19 -0700657 /*
Mel Gormana6f9edd2009-09-21 17:03:20 -0700658 * Remove pages from lists in a round-robin fashion. A
659 * batch_free count is maintained that is incremented when an
660 * empty list is encountered. This is so more pages are freed
661 * off fuller lists instead of spinning excessively around empty
662 * lists
Mel Gorman5f8dcc22009-09-21 17:03:19 -0700663 */
664 do {
Mel Gormana6f9edd2009-09-21 17:03:20 -0700665 batch_free++;
Mel Gorman5f8dcc22009-09-21 17:03:19 -0700666 if (++migratetype == MIGRATE_PCPTYPES)
667 migratetype = 0;
668 list = &pcp->lists[migratetype];
669 } while (list_empty(list));
670
Namhyung Kim1d168712011-03-22 16:32:45 -0700671 /* This is the only non-empty list. Free them all. */
672 if (batch_free == MIGRATE_PCPTYPES)
673 batch_free = to_free;
674
Mel Gormana6f9edd2009-09-21 17:03:20 -0700675 do {
Bartlomiej Zolnierkiewicz770c8aa2012-10-08 16:31:57 -0700676 int mt; /* migratetype of the to-be-freed page */
677
Mel Gormana6f9edd2009-09-21 17:03:20 -0700678 page = list_entry(list->prev, struct page, lru);
679 /* must delete as __free_one_page list manipulates */
680 list_del(&page->lru);
Minchan Kimb12c4ad2012-10-08 16:32:08 -0700681 mt = get_freepage_migratetype(page);
Hugh Dickinsa7016232010-01-29 17:46:34 +0000682 /* MIGRATE_MOVABLE list may include MIGRATE_RESERVEs */
Bartlomiej Zolnierkiewicz770c8aa2012-10-08 16:31:57 -0700683 __free_one_page(page, zone, 0, mt);
684 trace_mm_page_pcpu_drain(page, 0, mt);
Minchan Kim194159f2013-02-22 16:33:58 -0800685 if (likely(!is_migrate_isolate_page(page))) {
Wen Congyang97d0da22012-12-11 16:00:52 -0800686 __mod_zone_page_state(zone, NR_FREE_PAGES, 1);
687 if (is_migrate_cma(mt))
688 __mod_zone_page_state(zone, NR_FREE_CMA_PAGES, 1);
689 }
Mel Gorman72853e22010-09-09 16:38:16 -0700690 } while (--to_free && --batch_free && !list_empty(list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
Nick Pigginc54ad302006-01-06 00:10:56 -0800692 spin_unlock(&zone->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
Mel Gormaned0ae212009-06-16 15:32:07 -0700695static void free_one_page(struct zone *zone, struct page *page, int order,
696 int migratetype)
Nick Piggin48db57f2006-01-08 01:00:42 -0800697{
Christoph Lameter006d22d2006-09-25 23:31:48 -0700698 spin_lock(&zone->lock);
KOSAKI Motohiro93e4a892010-03-05 13:41:55 -0800699 zone->all_unreclaimable = 0;
Christoph Lameter006d22d2006-09-25 23:31:48 -0700700 zone->pages_scanned = 0;
Mel Gormanf2260e62009-06-16 15:32:13 -0700701
Mel Gormaned0ae212009-06-16 15:32:07 -0700702 __free_one_page(page, zone, order, migratetype);
Minchan Kim194159f2013-02-22 16:33:58 -0800703 if (unlikely(!is_migrate_isolate(migratetype)))
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -0700704 __mod_zone_freepage_state(zone, 1 << order, migratetype);
Christoph Lameter006d22d2006-09-25 23:31:48 -0700705 spin_unlock(&zone->lock);
Nick Piggin48db57f2006-01-08 01:00:42 -0800706}
707
KOSAKI Motohiroec95f532010-05-24 14:32:38 -0700708static bool free_pages_prepare(struct page *page, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 int i;
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800711 int bad = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Konstantin Khlebnikovb413d482012-01-10 15:07:09 -0800713 trace_mm_page_free(page, order);
Vegard Nossumb1eeab62008-11-25 16:55:53 +0100714 kmemcheck_free_shadow(page, order);
715
Andrea Arcangeli8dd60a32011-01-13 15:46:34 -0800716 if (PageAnon(page))
717 page->mapping = NULL;
718 for (i = 0; i < (1 << order); i++)
719 bad += free_pages_check(page + i);
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800720 if (bad)
KOSAKI Motohiroec95f532010-05-24 14:32:38 -0700721 return false;
Hugh Dickins689bceb2005-11-21 21:32:20 -0800722
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700723 if (!PageHighMem(page)) {
Pintu Kumarb8af2942013-09-11 14:20:34 -0700724 debug_check_no_locks_freed(page_address(page),
725 PAGE_SIZE << order);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700726 debug_check_no_obj_freed(page_address(page),
727 PAGE_SIZE << order);
728 }
Nick Piggindafb1362006-10-11 01:21:30 -0700729 arch_free_page(page, order);
Nick Piggin48db57f2006-01-08 01:00:42 -0800730 kernel_map_pages(page, 1 << order, 0);
Nick Piggindafb1362006-10-11 01:21:30 -0700731
KOSAKI Motohiroec95f532010-05-24 14:32:38 -0700732 return true;
733}
734
735static void __free_pages_ok(struct page *page, unsigned int order)
736{
737 unsigned long flags;
Minchan Kim95e34412012-10-08 16:32:11 -0700738 int migratetype;
KOSAKI Motohiroec95f532010-05-24 14:32:38 -0700739
740 if (!free_pages_prepare(page, order))
741 return;
742
Nick Pigginc54ad302006-01-06 00:10:56 -0800743 local_irq_save(flags);
Christoph Lameterf8891e52006-06-30 01:55:45 -0700744 __count_vm_events(PGFREE, 1 << order);
Minchan Kim95e34412012-10-08 16:32:11 -0700745 migratetype = get_pageblock_migratetype(page);
746 set_freepage_migratetype(page, migratetype);
747 free_one_page(page_zone(page), page, order, migratetype);
Nick Pigginc54ad302006-01-06 00:10:56 -0800748 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
Jiang Liu170a5a72013-07-03 15:03:17 -0700751void __init __free_pages_bootmem(struct page *page, unsigned int order)
David Howellsa226f6c2006-01-06 00:11:08 -0800752{
Johannes Weinerc3993072012-01-10 15:08:10 -0800753 unsigned int nr_pages = 1 << order;
754 unsigned int loop;
David Howellsa226f6c2006-01-06 00:11:08 -0800755
Johannes Weinerc3993072012-01-10 15:08:10 -0800756 prefetchw(page);
757 for (loop = 0; loop < nr_pages; loop++) {
758 struct page *p = &page[loop];
David Howellsa226f6c2006-01-06 00:11:08 -0800759
Johannes Weinerc3993072012-01-10 15:08:10 -0800760 if (loop + 1 < nr_pages)
761 prefetchw(p + 1);
762 __ClearPageReserved(p);
763 set_page_count(p, 0);
David Howellsa226f6c2006-01-06 00:11:08 -0800764 }
Johannes Weinerc3993072012-01-10 15:08:10 -0800765
Jiang Liu9feedc92012-12-12 13:52:12 -0800766 page_zone(page)->managed_pages += 1 << order;
Johannes Weinerc3993072012-01-10 15:08:10 -0800767 set_page_refcounted(page);
768 __free_pages(page, order);
David Howellsa226f6c2006-01-06 00:11:08 -0800769}
770
Michal Nazarewicz47118af2011-12-29 13:09:50 +0100771#ifdef CONFIG_CMA
Li Zhong9cf510a2013-08-23 13:52:52 +0800772/* Free whole pageblock and set its migration type to MIGRATE_CMA. */
Michal Nazarewicz47118af2011-12-29 13:09:50 +0100773void __init init_cma_reserved_pageblock(struct page *page)
774{
775 unsigned i = pageblock_nr_pages;
776 struct page *p = page;
777
778 do {
779 __ClearPageReserved(p);
780 set_page_count(p, 0);
781 } while (++p, --i);
782
783 set_page_refcounted(page);
784 set_pageblock_migratetype(page, MIGRATE_CMA);
785 __free_pages(page, pageblock_order);
Jiang Liu3dcc0572013-07-03 15:03:21 -0700786 adjust_managed_page_count(page, pageblock_nr_pages);
Michal Nazarewicz47118af2011-12-29 13:09:50 +0100787}
788#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790/*
791 * The order of subdivision here is critical for the IO subsystem.
792 * Please do not alter this order without good reasons and regression
793 * testing. Specifically, as large blocks of memory are subdivided,
794 * the order in which smaller blocks are delivered depends on the order
795 * they're subdivided in this function. This is the primary factor
796 * influencing the order in which pages are delivered to the IO
797 * subsystem according to empirical testing, and this is also justified
798 * by considering the behavior of a buddy system containing a single
799 * large block of memory acted on by a series of small allocations.
800 * This behavior is a critical factor in sglist merging's success.
801 *
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +0100802 * -- nyc
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 */
Nick Piggin085cc7d2006-01-06 00:11:01 -0800804static inline void expand(struct zone *zone, struct page *page,
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700805 int low, int high, struct free_area *area,
806 int migratetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 unsigned long size = 1 << high;
809
810 while (high > low) {
811 area--;
812 high--;
813 size >>= 1;
Nick Piggin725d7042006-09-25 23:30:55 -0700814 VM_BUG_ON(bad_range(zone, &page[size]));
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -0800815
816#ifdef CONFIG_DEBUG_PAGEALLOC
817 if (high < debug_guardpage_minorder()) {
818 /*
819 * Mark as guard pages (or page), that will allow to
820 * merge back to allocator when buddy will be freed.
821 * Corresponding page table entries will not be touched,
822 * pages will stay not present in virtual address space
823 */
824 INIT_LIST_HEAD(&page[size].lru);
825 set_page_guard_flag(&page[size]);
826 set_page_private(&page[size], high);
827 /* Guard pages are not available for any usage */
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -0700828 __mod_zone_freepage_state(zone, -(1 << high),
829 migratetype);
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -0800830 continue;
831 }
832#endif
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700833 list_add(&page[size].lru, &area->free_list[migratetype]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 area->nr_free++;
835 set_page_order(&page[size], high);
836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839/*
840 * This page is about to be returned from the page allocator
841 */
Wu Fengguang2a7684a2009-09-16 11:50:12 +0200842static inline int check_new_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Nick Piggin92be2e332006-01-06 00:10:57 -0800844 if (unlikely(page_mapcount(page) |
845 (page->mapping != NULL) |
Nick Piggina3af9c32009-06-16 15:32:10 -0700846 (atomic_read(&page->_count) != 0) |
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -0700847 (page->flags & PAGE_FLAGS_CHECK_AT_PREP) |
848 (mem_cgroup_bad_page_check(page)))) {
Nick Piggin224abf92006-01-06 00:11:11 -0800849 bad_page(page);
Hugh Dickins689bceb2005-11-21 21:32:20 -0800850 return 1;
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800851 }
Wu Fengguang2a7684a2009-09-16 11:50:12 +0200852 return 0;
853}
854
855static int prep_new_page(struct page *page, int order, gfp_t gfp_flags)
856{
857 int i;
858
859 for (i = 0; i < (1 << order); i++) {
860 struct page *p = page + i;
861 if (unlikely(check_new_page(p)))
862 return 1;
863 }
Hugh Dickins689bceb2005-11-21 21:32:20 -0800864
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700865 set_page_private(page, 0);
Nick Piggin7835e982006-03-22 00:08:40 -0800866 set_page_refcounted(page);
Nick Piggincc1025092006-12-06 20:32:00 -0800867
868 arch_alloc_page(page, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 kernel_map_pages(page, 1 << order, 1);
Nick Piggin17cf4402006-03-22 00:08:41 -0800870
871 if (gfp_flags & __GFP_ZERO)
872 prep_zero_page(page, order, gfp_flags);
873
874 if (order && (gfp_flags & __GFP_COMP))
875 prep_compound_page(page, order);
876
Hugh Dickins689bceb2005-11-21 21:32:20 -0800877 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
Mel Gorman56fd56b2007-10-16 01:25:58 -0700880/*
881 * Go through the free lists for the given migratetype and remove
882 * the smallest available page from the freelists
883 */
Mel Gorman728ec982009-06-16 15:32:04 -0700884static inline
885struct page *__rmqueue_smallest(struct zone *zone, unsigned int order,
Mel Gorman56fd56b2007-10-16 01:25:58 -0700886 int migratetype)
887{
888 unsigned int current_order;
Pintu Kumarb8af2942013-09-11 14:20:34 -0700889 struct free_area *area;
Mel Gorman56fd56b2007-10-16 01:25:58 -0700890 struct page *page;
891
892 /* Find a page of the appropriate size in the preferred list */
893 for (current_order = order; current_order < MAX_ORDER; ++current_order) {
894 area = &(zone->free_area[current_order]);
895 if (list_empty(&area->free_list[migratetype]))
896 continue;
897
898 page = list_entry(area->free_list[migratetype].next,
899 struct page, lru);
900 list_del(&page->lru);
901 rmv_page_order(page);
902 area->nr_free--;
Mel Gorman56fd56b2007-10-16 01:25:58 -0700903 expand(zone, page, order, current_order, area, migratetype);
904 return page;
905 }
906
907 return NULL;
908}
909
910
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700911/*
912 * This array describes the order lists are fallen back to when
913 * the free lists for the desirable migrate type are depleted
914 */
Michal Nazarewicz47118af2011-12-29 13:09:50 +0100915static int fallbacks[MIGRATE_TYPES][4] = {
916 [MIGRATE_UNMOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_MOVABLE, MIGRATE_RESERVE },
917 [MIGRATE_RECLAIMABLE] = { MIGRATE_UNMOVABLE, MIGRATE_MOVABLE, MIGRATE_RESERVE },
918#ifdef CONFIG_CMA
919 [MIGRATE_MOVABLE] = { MIGRATE_CMA, MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE, MIGRATE_RESERVE },
920 [MIGRATE_CMA] = { MIGRATE_RESERVE }, /* Never used */
921#else
922 [MIGRATE_MOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE, MIGRATE_RESERVE },
923#endif
Michal Nazarewicz6d4a4912012-01-11 15:31:33 +0100924 [MIGRATE_RESERVE] = { MIGRATE_RESERVE }, /* Never used */
Minchan Kim194159f2013-02-22 16:33:58 -0800925#ifdef CONFIG_MEMORY_ISOLATION
Michal Nazarewicz6d4a4912012-01-11 15:31:33 +0100926 [MIGRATE_ISOLATE] = { MIGRATE_RESERVE }, /* Never used */
Minchan Kim194159f2013-02-22 16:33:58 -0800927#endif
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700928};
929
Mel Gormanc361be52007-10-16 01:25:51 -0700930/*
931 * Move the free pages in a range to the free lists of the requested type.
Mel Gormand9c23402007-10-16 01:26:01 -0700932 * Note that start_page and end_pages are not aligned on a pageblock
Mel Gormanc361be52007-10-16 01:25:51 -0700933 * boundary. If alignment is required, use move_freepages_block()
934 */
Minchan Kim435b4052012-10-08 16:32:16 -0700935int move_freepages(struct zone *zone,
Adrian Bunkb69a7282008-07-23 21:28:12 -0700936 struct page *start_page, struct page *end_page,
937 int migratetype)
Mel Gormanc361be52007-10-16 01:25:51 -0700938{
939 struct page *page;
940 unsigned long order;
Mel Gormand1003132007-10-16 01:26:00 -0700941 int pages_moved = 0;
Mel Gormanc361be52007-10-16 01:25:51 -0700942
943#ifndef CONFIG_HOLES_IN_ZONE
944 /*
945 * page_zone is not safe to call in this context when
946 * CONFIG_HOLES_IN_ZONE is set. This bug check is probably redundant
947 * anyway as we check zone boundaries in move_freepages_block().
948 * Remove at a later date when no bug reports exist related to
Mel Gormanac0e5b72007-10-16 01:25:58 -0700949 * grouping pages by mobility
Mel Gormanc361be52007-10-16 01:25:51 -0700950 */
951 BUG_ON(page_zone(start_page) != page_zone(end_page));
952#endif
953
954 for (page = start_page; page <= end_page;) {
Adam Litke344c7902008-09-02 14:35:38 -0700955 /* Make sure we are not inadvertently changing nodes */
956 VM_BUG_ON(page_to_nid(page) != zone_to_nid(zone));
957
Mel Gormanc361be52007-10-16 01:25:51 -0700958 if (!pfn_valid_within(page_to_pfn(page))) {
959 page++;
960 continue;
961 }
962
963 if (!PageBuddy(page)) {
964 page++;
965 continue;
966 }
967
968 order = page_order(page);
Kirill A. Shutemov84be48d2011-03-22 16:33:41 -0700969 list_move(&page->lru,
970 &zone->free_area[order].free_list[migratetype]);
Minchan Kim95e34412012-10-08 16:32:11 -0700971 set_freepage_migratetype(page, migratetype);
Mel Gormanc361be52007-10-16 01:25:51 -0700972 page += 1 << order;
Mel Gormand1003132007-10-16 01:26:00 -0700973 pages_moved += 1 << order;
Mel Gormanc361be52007-10-16 01:25:51 -0700974 }
975
Mel Gormand1003132007-10-16 01:26:00 -0700976 return pages_moved;
Mel Gormanc361be52007-10-16 01:25:51 -0700977}
978
Minchan Kimee6f5092012-07-31 16:43:50 -0700979int move_freepages_block(struct zone *zone, struct page *page,
Linus Torvalds68e3e922012-06-03 20:05:57 -0700980 int migratetype)
Mel Gormanc361be52007-10-16 01:25:51 -0700981{
982 unsigned long start_pfn, end_pfn;
983 struct page *start_page, *end_page;
984
985 start_pfn = page_to_pfn(page);
Mel Gormand9c23402007-10-16 01:26:01 -0700986 start_pfn = start_pfn & ~(pageblock_nr_pages-1);
Mel Gormanc361be52007-10-16 01:25:51 -0700987 start_page = pfn_to_page(start_pfn);
Mel Gormand9c23402007-10-16 01:26:01 -0700988 end_page = start_page + pageblock_nr_pages - 1;
989 end_pfn = start_pfn + pageblock_nr_pages - 1;
Mel Gormanc361be52007-10-16 01:25:51 -0700990
991 /* Do not cross zone boundaries */
Cody P Schafer108bcc92013-02-22 16:35:23 -0800992 if (!zone_spans_pfn(zone, start_pfn))
Mel Gormanc361be52007-10-16 01:25:51 -0700993 start_page = page;
Cody P Schafer108bcc92013-02-22 16:35:23 -0800994 if (!zone_spans_pfn(zone, end_pfn))
Mel Gormanc361be52007-10-16 01:25:51 -0700995 return 0;
996
997 return move_freepages(zone, start_page, end_page, migratetype);
998}
999
Mel Gorman2f66a682009-09-21 17:02:31 -07001000static void change_pageblock_range(struct page *pageblock_page,
1001 int start_order, int migratetype)
1002{
1003 int nr_pageblocks = 1 << (start_order - pageblock_order);
1004
1005 while (nr_pageblocks--) {
1006 set_pageblock_migratetype(pageblock_page, migratetype);
1007 pageblock_page += pageblock_nr_pages;
1008 }
1009}
1010
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07001011/*
1012 * If breaking a large block of pages, move all free pages to the preferred
1013 * allocation list. If falling back for a reclaimable kernel allocation, be
1014 * more aggressive about taking ownership of free pages.
1015 *
1016 * On the other hand, never change migration type of MIGRATE_CMA pageblocks
1017 * nor move CMA pages to different free lists. We don't want unmovable pages
1018 * to be allocated from MIGRATE_CMA areas.
1019 *
1020 * Returns the new migratetype of the pageblock (or the same old migratetype
1021 * if it was unchanged).
1022 */
1023static int try_to_steal_freepages(struct zone *zone, struct page *page,
1024 int start_type, int fallback_type)
1025{
1026 int current_order = page_order(page);
1027
1028 if (is_migrate_cma(fallback_type))
1029 return fallback_type;
1030
1031 /* Take ownership for orders >= pageblock_order */
1032 if (current_order >= pageblock_order) {
1033 change_pageblock_range(page, current_order, start_type);
1034 return start_type;
1035 }
1036
1037 if (current_order >= pageblock_order / 2 ||
1038 start_type == MIGRATE_RECLAIMABLE ||
1039 page_group_by_mobility_disabled) {
1040 int pages;
1041
1042 pages = move_freepages_block(zone, page, start_type);
1043
1044 /* Claim the whole block if over half of it is free */
1045 if (pages >= (1 << (pageblock_order-1)) ||
1046 page_group_by_mobility_disabled) {
1047
1048 set_pageblock_migratetype(page, start_type);
1049 return start_type;
1050 }
1051
1052 }
1053
1054 return fallback_type;
1055}
1056
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001057/* Remove an element from the buddy allocator from the fallback list */
Mel Gorman0ac3a402009-06-16 15:32:06 -07001058static inline struct page *
1059__rmqueue_fallback(struct zone *zone, int order, int start_migratetype)
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001060{
Pintu Kumarb8af2942013-09-11 14:20:34 -07001061 struct free_area *area;
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001062 int current_order;
1063 struct page *page;
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07001064 int migratetype, new_type, i;
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001065
1066 /* Find the largest possible block of pages in the other list */
1067 for (current_order = MAX_ORDER-1; current_order >= order;
1068 --current_order) {
Michal Nazarewicz6d4a4912012-01-11 15:31:33 +01001069 for (i = 0;; i++) {
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001070 migratetype = fallbacks[start_migratetype][i];
1071
Mel Gorman56fd56b2007-10-16 01:25:58 -07001072 /* MIGRATE_RESERVE handled later if necessary */
1073 if (migratetype == MIGRATE_RESERVE)
Michal Nazarewicz6d4a4912012-01-11 15:31:33 +01001074 break;
Mel Gormane0104872007-10-16 01:25:53 -07001075
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001076 area = &(zone->free_area[current_order]);
1077 if (list_empty(&area->free_list[migratetype]))
1078 continue;
1079
1080 page = list_entry(area->free_list[migratetype].next,
1081 struct page, lru);
1082 area->nr_free--;
1083
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07001084 new_type = try_to_steal_freepages(zone, page,
1085 start_migratetype,
1086 migratetype);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001087
1088 /* Remove the page from the freelists */
1089 list_del(&page->lru);
1090 rmv_page_order(page);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001091
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07001092 /*
1093 * Borrow the excess buddy pages as well, irrespective
1094 * of whether we stole freepages, or took ownership of
1095 * the pageblock or not.
1096 *
1097 * Exception: When borrowing from MIGRATE_CMA, release
1098 * the excess buddy pages to CMA itself.
1099 */
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001100 expand(zone, page, order, current_order, area,
1101 is_migrate_cma(migratetype)
1102 ? migratetype : start_migratetype);
Mel Gormane0fff1b2009-09-21 17:02:42 -07001103
1104 trace_mm_page_alloc_extfrag(page, order, current_order,
Srivatsa S. Bhatfef903e2013-09-11 14:20:35 -07001105 start_migratetype, new_type);
Mel Gormane0fff1b2009-09-21 17:02:42 -07001106
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001107 return page;
1108 }
1109 }
1110
Mel Gorman728ec982009-06-16 15:32:04 -07001111 return NULL;
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001112}
1113
Mel Gorman56fd56b2007-10-16 01:25:58 -07001114/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 * Do the hard work of removing an element from the buddy allocator.
1116 * Call me with the zone->lock already held.
1117 */
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001118static struct page *__rmqueue(struct zone *zone, unsigned int order,
1119 int migratetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 struct page *page;
1122
Mel Gorman728ec982009-06-16 15:32:04 -07001123retry_reserve:
Mel Gorman56fd56b2007-10-16 01:25:58 -07001124 page = __rmqueue_smallest(zone, order, migratetype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Mel Gorman728ec982009-06-16 15:32:04 -07001126 if (unlikely(!page) && migratetype != MIGRATE_RESERVE) {
Mel Gorman56fd56b2007-10-16 01:25:58 -07001127 page = __rmqueue_fallback(zone, order, migratetype);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001128
Mel Gorman728ec982009-06-16 15:32:04 -07001129 /*
1130 * Use MIGRATE_RESERVE rather than fail an allocation. goto
1131 * is used because __rmqueue_smallest is an inline function
1132 * and we want just one call site
1133 */
1134 if (!page) {
1135 migratetype = MIGRATE_RESERVE;
1136 goto retry_reserve;
1137 }
1138 }
1139
Mel Gorman0d3d0622009-09-21 17:02:44 -07001140 trace_mm_page_alloc_zone_locked(page, order, migratetype);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001141 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
1143
Michal Nazarewicz5f63b722012-01-11 15:16:11 +01001144/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 * Obtain a specified number of elements from the buddy allocator, all under
1146 * a single hold of the lock, for efficiency. Add them to the supplied list.
1147 * Returns the number of new pages which were placed at *list.
1148 */
Michal Nazarewicz5f63b722012-01-11 15:16:11 +01001149static int rmqueue_bulk(struct zone *zone, unsigned int order,
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001150 unsigned long count, struct list_head *list,
Mel Gormane084b2d2009-07-29 15:02:04 -07001151 int migratetype, int cold)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001153 int mt = migratetype, i;
Michal Nazarewicz5f63b722012-01-11 15:16:11 +01001154
Nick Pigginc54ad302006-01-06 00:10:56 -08001155 spin_lock(&zone->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 for (i = 0; i < count; ++i) {
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001157 struct page *page = __rmqueue(zone, order, migratetype);
Nick Piggin085cc7d2006-01-06 00:11:01 -08001158 if (unlikely(page == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 break;
Mel Gorman81eabcb2007-12-17 16:20:05 -08001160
1161 /*
1162 * Split buddy pages returned by expand() are received here
1163 * in physical page order. The page is added to the callers and
1164 * list and the list head then moves forward. From the callers
1165 * perspective, the linked list is ordered by page number in
1166 * some conditions. This is useful for IO devices that can
1167 * merge IO requests if the physical pages are ordered
1168 * properly.
1169 */
Mel Gormane084b2d2009-07-29 15:02:04 -07001170 if (likely(cold == 0))
1171 list_add(&page->lru, list);
1172 else
1173 list_add_tail(&page->lru, list);
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001174 if (IS_ENABLED(CONFIG_CMA)) {
1175 mt = get_pageblock_migratetype(page);
Minchan Kim194159f2013-02-22 16:33:58 -08001176 if (!is_migrate_cma(mt) && !is_migrate_isolate(mt))
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001177 mt = migratetype;
1178 }
Minchan Kimb12c4ad2012-10-08 16:32:08 -07001179 set_freepage_migratetype(page, mt);
Mel Gorman81eabcb2007-12-17 16:20:05 -08001180 list = &page->lru;
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -07001181 if (is_migrate_cma(mt))
1182 __mod_zone_page_state(zone, NR_FREE_CMA_PAGES,
1183 -(1 << order));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
Mel Gormanf2260e62009-06-16 15:32:13 -07001185 __mod_zone_page_state(zone, NR_FREE_PAGES, -(i << order));
Nick Pigginc54ad302006-01-06 00:10:56 -08001186 spin_unlock(&zone->lock);
Nick Piggin085cc7d2006-01-06 00:11:01 -08001187 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
1189
Christoph Lameter4ae7c032005-06-21 17:14:57 -07001190#ifdef CONFIG_NUMA
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001191/*
Christoph Lameter4037d452007-05-09 02:35:14 -07001192 * Called from the vmstat counter updater to drain pagesets of this
1193 * currently executing processor on remote nodes after they have
1194 * expired.
1195 *
Christoph Lameter879336c2006-03-22 00:09:08 -08001196 * Note that this function must be called with the thread pinned to
1197 * a single processor.
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001198 */
Christoph Lameter4037d452007-05-09 02:35:14 -07001199void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
Christoph Lameter4ae7c032005-06-21 17:14:57 -07001200{
Christoph Lameter4ae7c032005-06-21 17:14:57 -07001201 unsigned long flags;
Christoph Lameter4037d452007-05-09 02:35:14 -07001202 int to_drain;
Cody P Schafer998d39c2013-07-03 15:01:32 -07001203 unsigned long batch;
Christoph Lameter4ae7c032005-06-21 17:14:57 -07001204
Christoph Lameter4037d452007-05-09 02:35:14 -07001205 local_irq_save(flags);
Cody P Schafer998d39c2013-07-03 15:01:32 -07001206 batch = ACCESS_ONCE(pcp->batch);
1207 if (pcp->count >= batch)
1208 to_drain = batch;
Christoph Lameter4037d452007-05-09 02:35:14 -07001209 else
1210 to_drain = pcp->count;
KOSAKI Motohiro2a135152012-07-31 16:42:53 -07001211 if (to_drain > 0) {
1212 free_pcppages_bulk(zone, to_drain, pcp);
1213 pcp->count -= to_drain;
1214 }
Christoph Lameter4037d452007-05-09 02:35:14 -07001215 local_irq_restore(flags);
Christoph Lameter4ae7c032005-06-21 17:14:57 -07001216}
1217#endif
1218
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001219/*
1220 * Drain pages of the indicated processor.
1221 *
1222 * The processor must either be the current processor and the
1223 * thread pinned to the current processor or a processor that
1224 * is not online.
1225 */
1226static void drain_pages(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
Nick Pigginc54ad302006-01-06 00:10:56 -08001228 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 struct zone *zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
KOSAKI Motohiroee99c712009-03-31 15:19:31 -07001231 for_each_populated_zone(zone) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 struct per_cpu_pageset *pset;
Christoph Lameter3dfa5722008-02-04 22:29:19 -08001233 struct per_cpu_pages *pcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09001235 local_irq_save(flags);
1236 pset = per_cpu_ptr(zone->pageset, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Christoph Lameter3dfa5722008-02-04 22:29:19 -08001238 pcp = &pset->pcp;
David Rientjes2ff754f2011-01-25 15:07:23 -08001239 if (pcp->count) {
1240 free_pcppages_bulk(zone, pcp->count, pcp);
1241 pcp->count = 0;
1242 }
Christoph Lameter3dfa5722008-02-04 22:29:19 -08001243 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 }
1245}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001247/*
1248 * Spill all of this CPU's per-cpu pages back into the buddy allocator.
1249 */
1250void drain_local_pages(void *arg)
1251{
1252 drain_pages(smp_processor_id());
1253}
1254
1255/*
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07001256 * Spill all the per-cpu pages from all CPUs back into the buddy allocator.
1257 *
1258 * Note that this code is protected against sending an IPI to an offline
1259 * CPU but does not guarantee sending an IPI to newly hotplugged CPUs:
1260 * on_each_cpu_mask() blocks hotplug and won't talk to offlined CPUs but
1261 * nothing keeps CPUs from showing up after we populated the cpumask and
1262 * before the call to on_each_cpu_mask().
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001263 */
1264void drain_all_pages(void)
1265{
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07001266 int cpu;
1267 struct per_cpu_pageset *pcp;
1268 struct zone *zone;
1269
1270 /*
1271 * Allocate in the BSS so we wont require allocation in
1272 * direct reclaim path for CONFIG_CPUMASK_OFFSTACK=y
1273 */
1274 static cpumask_t cpus_with_pcps;
1275
1276 /*
1277 * We don't care about racing with CPU hotplug event
1278 * as offline notification will cause the notified
1279 * cpu to drain that CPU pcps and on_each_cpu_mask
1280 * disables preemption as part of its processing
1281 */
1282 for_each_online_cpu(cpu) {
1283 bool has_pcps = false;
1284 for_each_populated_zone(zone) {
1285 pcp = per_cpu_ptr(zone->pageset, cpu);
1286 if (pcp->pcp.count) {
1287 has_pcps = true;
1288 break;
1289 }
1290 }
1291 if (has_pcps)
1292 cpumask_set_cpu(cpu, &cpus_with_pcps);
1293 else
1294 cpumask_clear_cpu(cpu, &cpus_with_pcps);
1295 }
1296 on_each_cpu_mask(&cpus_with_pcps, drain_local_pages, NULL, 1);
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001297}
1298
Rafael J. Wysocki296699d2007-07-29 23:27:18 +02001299#ifdef CONFIG_HIBERNATION
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301void mark_free_pages(struct zone *zone)
1302{
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07001303 unsigned long pfn, max_zone_pfn;
1304 unsigned long flags;
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001305 int order, t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 struct list_head *curr;
1307
1308 if (!zone->spanned_pages)
1309 return;
1310
1311 spin_lock_irqsave(&zone->lock, flags);
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07001312
Cody P Schafer108bcc92013-02-22 16:35:23 -08001313 max_zone_pfn = zone_end_pfn(zone);
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07001314 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
1315 if (pfn_valid(pfn)) {
1316 struct page *page = pfn_to_page(pfn);
1317
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001318 if (!swsusp_page_is_forbidden(page))
1319 swsusp_unset_page_free(page);
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07001320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001322 for_each_migratetype_order(order, t) {
1323 list_for_each(curr, &zone->free_area[order].free_list[t]) {
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07001324 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07001326 pfn = page_to_pfn(list_entry(curr, struct page, lru));
1327 for (i = 0; i < (1UL << order); i++)
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001328 swsusp_set_page_free(pfn_to_page(pfn + i));
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07001329 }
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 spin_unlock_irqrestore(&zone->lock, flags);
1332}
Mel Gormane2c55dc2007-10-16 01:25:50 -07001333#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 * Free a 0-order page
Li Hongfc916682010-03-05 13:41:54 -08001337 * cold == 1 ? free a cold page : free a hot page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 */
Li Hongfc916682010-03-05 13:41:54 -08001339void free_hot_cold_page(struct page *page, int cold)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
1341 struct zone *zone = page_zone(page);
1342 struct per_cpu_pages *pcp;
1343 unsigned long flags;
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001344 int migratetype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
KOSAKI Motohiroec95f532010-05-24 14:32:38 -07001346 if (!free_pages_prepare(page, 0))
Hugh Dickins689bceb2005-11-21 21:32:20 -08001347 return;
1348
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001349 migratetype = get_pageblock_migratetype(page);
Minchan Kimb12c4ad2012-10-08 16:32:08 -07001350 set_freepage_migratetype(page, migratetype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 local_irq_save(flags);
Christoph Lameterf8891e52006-06-30 01:55:45 -07001352 __count_vm_event(PGFREE);
Mel Gormanda456f12009-06-16 15:32:08 -07001353
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001354 /*
1355 * We only track unmovable, reclaimable and movable on pcp lists.
1356 * Free ISOLATE pages back to the allocator because they are being
1357 * offlined but treat RESERVE as movable pages so we can get those
1358 * areas back if necessary. Otherwise, we may have to free
1359 * excessively into the page allocator
1360 */
1361 if (migratetype >= MIGRATE_PCPTYPES) {
Minchan Kim194159f2013-02-22 16:33:58 -08001362 if (unlikely(is_migrate_isolate(migratetype))) {
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001363 free_one_page(zone, page, 0, migratetype);
1364 goto out;
1365 }
1366 migratetype = MIGRATE_MOVABLE;
1367 }
1368
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09001369 pcp = &this_cpu_ptr(zone->pageset)->pcp;
Christoph Lameter3dfa5722008-02-04 22:29:19 -08001370 if (cold)
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001371 list_add_tail(&page->lru, &pcp->lists[migratetype]);
Christoph Lameter3dfa5722008-02-04 22:29:19 -08001372 else
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001373 list_add(&page->lru, &pcp->lists[migratetype]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 pcp->count++;
Nick Piggin48db57f2006-01-08 01:00:42 -08001375 if (pcp->count >= pcp->high) {
Cody P Schafer998d39c2013-07-03 15:01:32 -07001376 unsigned long batch = ACCESS_ONCE(pcp->batch);
1377 free_pcppages_bulk(zone, batch, pcp);
1378 pcp->count -= batch;
Nick Piggin48db57f2006-01-08 01:00:42 -08001379 }
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001380
1381out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383}
1384
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08001385/*
Konstantin Khlebnikovcc598502012-01-10 15:07:04 -08001386 * Free a list of 0-order pages
1387 */
1388void free_hot_cold_page_list(struct list_head *list, int cold)
1389{
1390 struct page *page, *next;
1391
1392 list_for_each_entry_safe(page, next, list, lru) {
Konstantin Khlebnikovb413d482012-01-10 15:07:09 -08001393 trace_mm_page_free_batched(page, cold);
Konstantin Khlebnikovcc598502012-01-10 15:07:04 -08001394 free_hot_cold_page(page, cold);
1395 }
1396}
1397
1398/*
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08001399 * split_page takes a non-compound higher-order page, and splits it into
1400 * n (1<<order) sub-pages: page[0..n]
1401 * Each sub-page must be freed individually.
1402 *
1403 * Note: this is probably too low level an operation for use in drivers.
1404 * Please consult with lkml before using this in your driver.
1405 */
1406void split_page(struct page *page, unsigned int order)
1407{
1408 int i;
1409
Nick Piggin725d7042006-09-25 23:30:55 -07001410 VM_BUG_ON(PageCompound(page));
1411 VM_BUG_ON(!page_count(page));
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001412
1413#ifdef CONFIG_KMEMCHECK
1414 /*
1415 * Split shadow pages too, because free(page[0]) would
1416 * otherwise free the whole shadow.
1417 */
1418 if (kmemcheck_page_is_tracked(page))
1419 split_page(virt_to_page(page[0].shadow), order);
1420#endif
1421
Nick Piggin7835e982006-03-22 00:08:40 -08001422 for (i = 1; i < (1 << order); i++)
1423 set_page_refcounted(page + i);
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08001424}
K. Y. Srinivasan5853ff22013-03-25 15:47:38 -07001425EXPORT_SYMBOL_GPL(split_page);
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08001426
Mel Gorman8fb74b92013-01-11 14:32:16 -08001427static int __isolate_free_page(struct page *page, unsigned int order)
Mel Gorman748446b2010-05-24 14:32:27 -07001428{
Mel Gorman748446b2010-05-24 14:32:27 -07001429 unsigned long watermark;
1430 struct zone *zone;
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -07001431 int mt;
Mel Gorman748446b2010-05-24 14:32:27 -07001432
1433 BUG_ON(!PageBuddy(page));
1434
1435 zone = page_zone(page);
Marek Szyprowski2e30abd2012-12-11 16:02:57 -08001436 mt = get_pageblock_migratetype(page);
Mel Gorman748446b2010-05-24 14:32:27 -07001437
Minchan Kim194159f2013-02-22 16:33:58 -08001438 if (!is_migrate_isolate(mt)) {
Marek Szyprowski2e30abd2012-12-11 16:02:57 -08001439 /* Obey watermarks as if the page was being allocated */
1440 watermark = low_wmark_pages(zone) + (1 << order);
1441 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
1442 return 0;
1443
Mel Gorman8fb74b92013-01-11 14:32:16 -08001444 __mod_zone_freepage_state(zone, -(1UL << order), mt);
Marek Szyprowski2e30abd2012-12-11 16:02:57 -08001445 }
Mel Gorman748446b2010-05-24 14:32:27 -07001446
1447 /* Remove page from free list */
1448 list_del(&page->lru);
1449 zone->free_area[order].nr_free--;
1450 rmv_page_order(page);
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -07001451
Mel Gorman8fb74b92013-01-11 14:32:16 -08001452 /* Set the pageblock if the isolated page is at least a pageblock */
Mel Gorman748446b2010-05-24 14:32:27 -07001453 if (order >= pageblock_order - 1) {
1454 struct page *endpage = page + (1 << order) - 1;
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001455 for (; page < endpage; page += pageblock_nr_pages) {
1456 int mt = get_pageblock_migratetype(page);
Minchan Kim194159f2013-02-22 16:33:58 -08001457 if (!is_migrate_isolate(mt) && !is_migrate_cma(mt))
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001458 set_pageblock_migratetype(page,
1459 MIGRATE_MOVABLE);
1460 }
Mel Gorman748446b2010-05-24 14:32:27 -07001461 }
1462
Mel Gorman8fb74b92013-01-11 14:32:16 -08001463 return 1UL << order;
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001464}
1465
1466/*
1467 * Similar to split_page except the page is already free. As this is only
1468 * being used for migration, the migratetype of the block also changes.
1469 * As this is called with interrupts disabled, the caller is responsible
1470 * for calling arch_alloc_page() and kernel_map_page() after interrupts
1471 * are enabled.
1472 *
1473 * Note: this is probably too low level an operation for use in drivers.
1474 * Please consult with lkml before using this in your driver.
1475 */
1476int split_free_page(struct page *page)
1477{
1478 unsigned int order;
1479 int nr_pages;
1480
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001481 order = page_order(page);
1482
Mel Gorman8fb74b92013-01-11 14:32:16 -08001483 nr_pages = __isolate_free_page(page, order);
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001484 if (!nr_pages)
1485 return 0;
1486
1487 /* Split into individual pages */
1488 set_page_refcounted(page);
1489 split_page(page, order);
1490 return nr_pages;
Mel Gorman748446b2010-05-24 14:32:27 -07001491}
1492
1493/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
1495 * we cheat by calling it from here, in the order > 0 path. Saves a branch
1496 * or two.
1497 */
Mel Gorman0a15c3e2009-06-16 15:32:05 -07001498static inline
1499struct page *buffered_rmqueue(struct zone *preferred_zone,
Mel Gorman3dd28262009-06-16 15:32:00 -07001500 struct zone *zone, int order, gfp_t gfp_flags,
1501 int migratetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
1503 unsigned long flags;
Hugh Dickins689bceb2005-11-21 21:32:20 -08001504 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 int cold = !!(gfp_flags & __GFP_COLD);
1506
Hugh Dickins689bceb2005-11-21 21:32:20 -08001507again:
Nick Piggin48db57f2006-01-08 01:00:42 -08001508 if (likely(order == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 struct per_cpu_pages *pcp;
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001510 struct list_head *list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 local_irq_save(flags);
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09001513 pcp = &this_cpu_ptr(zone->pageset)->pcp;
1514 list = &pcp->lists[migratetype];
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001515 if (list_empty(list)) {
Mel Gorman535131e62007-10-16 01:25:49 -07001516 pcp->count += rmqueue_bulk(zone, 0,
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001517 pcp->batch, list,
Mel Gormane084b2d2009-07-29 15:02:04 -07001518 migratetype, cold);
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001519 if (unlikely(list_empty(list)))
Shaohua Li6fb332f2009-09-21 17:01:17 -07001520 goto failed;
Mel Gorman535131e62007-10-16 01:25:49 -07001521 }
Mel Gormanb92a6ed2007-10-16 01:25:50 -07001522
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001523 if (cold)
1524 page = list_entry(list->prev, struct page, lru);
1525 else
1526 page = list_entry(list->next, struct page, lru);
1527
Mel Gormanb92a6ed2007-10-16 01:25:50 -07001528 list_del(&page->lru);
1529 pcp->count--;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001530 } else {
Andrew Mortondab48da2009-06-16 15:32:37 -07001531 if (unlikely(gfp_flags & __GFP_NOFAIL)) {
1532 /*
1533 * __GFP_NOFAIL is not to be used in new code.
1534 *
1535 * All __GFP_NOFAIL callers should be fixed so that they
1536 * properly detect and handle allocation failures.
1537 *
1538 * We most definitely don't want callers attempting to
Linus Torvalds4923abf2009-06-24 12:16:49 -07001539 * allocate greater than order-1 page units with
Andrew Mortondab48da2009-06-16 15:32:37 -07001540 * __GFP_NOFAIL.
1541 */
Linus Torvalds4923abf2009-06-24 12:16:49 -07001542 WARN_ON_ONCE(order > 1);
Andrew Mortondab48da2009-06-16 15:32:37 -07001543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 spin_lock_irqsave(&zone->lock, flags);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001545 page = __rmqueue(zone, order, migratetype);
Nick Piggina74609f2006-01-06 00:11:20 -08001546 spin_unlock(&zone->lock);
1547 if (!page)
1548 goto failed;
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -07001549 __mod_zone_freepage_state(zone, -(1 << order),
1550 get_pageblock_migratetype(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 }
1552
Christoph Lameterf8891e52006-06-30 01:55:45 -07001553 __count_zone_vm_events(PGALLOC, zone, 1 << order);
Andi Kleen78afd562011-03-22 16:33:12 -07001554 zone_statistics(preferred_zone, zone, gfp_flags);
Nick Piggina74609f2006-01-06 00:11:20 -08001555 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Nick Piggin725d7042006-09-25 23:30:55 -07001557 VM_BUG_ON(bad_range(zone, page));
Nick Piggin17cf4402006-03-22 00:08:41 -08001558 if (prep_new_page(page, order, gfp_flags))
Nick Piggina74609f2006-01-06 00:11:20 -08001559 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 return page;
Nick Piggina74609f2006-01-06 00:11:20 -08001561
1562failed:
1563 local_irq_restore(flags);
Nick Piggina74609f2006-01-06 00:11:20 -08001564 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566
Akinobu Mita933e3122006-12-08 02:39:45 -08001567#ifdef CONFIG_FAIL_PAGE_ALLOC
1568
Akinobu Mitab2588c42011-07-26 16:09:03 -07001569static struct {
Akinobu Mita933e3122006-12-08 02:39:45 -08001570 struct fault_attr attr;
1571
1572 u32 ignore_gfp_highmem;
1573 u32 ignore_gfp_wait;
Akinobu Mita54114992007-07-15 23:40:23 -07001574 u32 min_order;
Akinobu Mita933e3122006-12-08 02:39:45 -08001575} fail_page_alloc = {
1576 .attr = FAULT_ATTR_INITIALIZER,
Don Mullis6b1b60f2006-12-08 02:39:53 -08001577 .ignore_gfp_wait = 1,
1578 .ignore_gfp_highmem = 1,
Akinobu Mita54114992007-07-15 23:40:23 -07001579 .min_order = 1,
Akinobu Mita933e3122006-12-08 02:39:45 -08001580};
1581
1582static int __init setup_fail_page_alloc(char *str)
1583{
1584 return setup_fault_attr(&fail_page_alloc.attr, str);
1585}
1586__setup("fail_page_alloc=", setup_fail_page_alloc);
1587
Gavin Shandeaf3862012-07-31 16:41:51 -07001588static bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
Akinobu Mita933e3122006-12-08 02:39:45 -08001589{
Akinobu Mita54114992007-07-15 23:40:23 -07001590 if (order < fail_page_alloc.min_order)
Gavin Shandeaf3862012-07-31 16:41:51 -07001591 return false;
Akinobu Mita933e3122006-12-08 02:39:45 -08001592 if (gfp_mask & __GFP_NOFAIL)
Gavin Shandeaf3862012-07-31 16:41:51 -07001593 return false;
Akinobu Mita933e3122006-12-08 02:39:45 -08001594 if (fail_page_alloc.ignore_gfp_highmem && (gfp_mask & __GFP_HIGHMEM))
Gavin Shandeaf3862012-07-31 16:41:51 -07001595 return false;
Akinobu Mita933e3122006-12-08 02:39:45 -08001596 if (fail_page_alloc.ignore_gfp_wait && (gfp_mask & __GFP_WAIT))
Gavin Shandeaf3862012-07-31 16:41:51 -07001597 return false;
Akinobu Mita933e3122006-12-08 02:39:45 -08001598
1599 return should_fail(&fail_page_alloc.attr, 1 << order);
1600}
1601
1602#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
1603
1604static int __init fail_page_alloc_debugfs(void)
1605{
Al Virof4ae40a2011-07-24 04:33:43 -04001606 umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
Akinobu Mita933e3122006-12-08 02:39:45 -08001607 struct dentry *dir;
Akinobu Mita933e3122006-12-08 02:39:45 -08001608
Akinobu Mitadd48c082011-08-03 16:21:01 -07001609 dir = fault_create_debugfs_attr("fail_page_alloc", NULL,
1610 &fail_page_alloc.attr);
1611 if (IS_ERR(dir))
1612 return PTR_ERR(dir);
Akinobu Mita933e3122006-12-08 02:39:45 -08001613
Akinobu Mitab2588c42011-07-26 16:09:03 -07001614 if (!debugfs_create_bool("ignore-gfp-wait", mode, dir,
1615 &fail_page_alloc.ignore_gfp_wait))
1616 goto fail;
1617 if (!debugfs_create_bool("ignore-gfp-highmem", mode, dir,
1618 &fail_page_alloc.ignore_gfp_highmem))
1619 goto fail;
1620 if (!debugfs_create_u32("min-order", mode, dir,
1621 &fail_page_alloc.min_order))
1622 goto fail;
Akinobu Mita933e3122006-12-08 02:39:45 -08001623
Akinobu Mitab2588c42011-07-26 16:09:03 -07001624 return 0;
1625fail:
Akinobu Mitadd48c082011-08-03 16:21:01 -07001626 debugfs_remove_recursive(dir);
Akinobu Mita933e3122006-12-08 02:39:45 -08001627
Akinobu Mitab2588c42011-07-26 16:09:03 -07001628 return -ENOMEM;
Akinobu Mita933e3122006-12-08 02:39:45 -08001629}
1630
1631late_initcall(fail_page_alloc_debugfs);
1632
1633#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
1634
1635#else /* CONFIG_FAIL_PAGE_ALLOC */
1636
Gavin Shandeaf3862012-07-31 16:41:51 -07001637static inline bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
Akinobu Mita933e3122006-12-08 02:39:45 -08001638{
Gavin Shandeaf3862012-07-31 16:41:51 -07001639 return false;
Akinobu Mita933e3122006-12-08 02:39:45 -08001640}
1641
1642#endif /* CONFIG_FAIL_PAGE_ALLOC */
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644/*
Mel Gorman88f5acf2011-01-13 15:45:41 -08001645 * Return true if free pages are above 'mark'. This takes into account the order
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 * of the allocation.
1647 */
Mel Gorman88f5acf2011-01-13 15:45:41 -08001648static bool __zone_watermark_ok(struct zone *z, int order, unsigned long mark,
1649 int classzone_idx, int alloc_flags, long free_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
1651 /* free_pages my go negative - that's OK */
Christoph Lameterd23ad422007-02-10 01:43:02 -08001652 long min = mark;
Minchan Kim2cfed072012-07-31 16:43:53 -07001653 long lowmem_reserve = z->lowmem_reserve[classzone_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 int o;
Tomasz Stanislawski026b0812013-06-12 14:05:02 -07001655 long free_cma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Michal Hockodf0a6da2012-01-10 15:08:02 -08001657 free_pages -= (1 << order) - 1;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001658 if (alloc_flags & ALLOC_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 min -= min / 2;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001660 if (alloc_flags & ALLOC_HARDER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 min -= min / 4;
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001662#ifdef CONFIG_CMA
1663 /* If allocation can't use CMA areas don't use free CMA pages */
1664 if (!(alloc_flags & ALLOC_CMA))
Tomasz Stanislawski026b0812013-06-12 14:05:02 -07001665 free_cma = zone_page_state(z, NR_FREE_CMA_PAGES);
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001666#endif
Tomasz Stanislawski026b0812013-06-12 14:05:02 -07001667
1668 if (free_pages - free_cma <= min + lowmem_reserve)
Mel Gorman88f5acf2011-01-13 15:45:41 -08001669 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 for (o = 0; o < order; o++) {
1671 /* At the next order, this order's pages become unavailable */
1672 free_pages -= z->free_area[o].nr_free << o;
1673
1674 /* Require fewer higher order pages to be free */
1675 min >>= 1;
1676
1677 if (free_pages <= min)
Mel Gorman88f5acf2011-01-13 15:45:41 -08001678 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 }
Mel Gorman88f5acf2011-01-13 15:45:41 -08001680 return true;
1681}
1682
1683bool zone_watermark_ok(struct zone *z, int order, unsigned long mark,
1684 int classzone_idx, int alloc_flags)
1685{
1686 return __zone_watermark_ok(z, order, mark, classzone_idx, alloc_flags,
1687 zone_page_state(z, NR_FREE_PAGES));
1688}
1689
1690bool zone_watermark_ok_safe(struct zone *z, int order, unsigned long mark,
1691 int classzone_idx, int alloc_flags)
1692{
1693 long free_pages = zone_page_state(z, NR_FREE_PAGES);
1694
1695 if (z->percpu_drift_mark && free_pages < z->percpu_drift_mark)
1696 free_pages = zone_page_state_snapshot(z, NR_FREE_PAGES);
1697
1698 return __zone_watermark_ok(z, order, mark, classzone_idx, alloc_flags,
1699 free_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700}
1701
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001702#ifdef CONFIG_NUMA
1703/*
1704 * zlc_setup - Setup for "zonelist cache". Uses cached zone data to
1705 * skip over zones that are not allowed by the cpuset, or that have
1706 * been recently (in last second) found to be nearly full. See further
1707 * comments in mmzone.h. Reduces cache footprint of zonelist scans
Simon Arlott183ff222007-10-20 01:27:18 +02001708 * that have to skip over a lot of full or unallowed zones.
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001709 *
1710 * If the zonelist cache is present in the passed in zonelist, then
1711 * returns a pointer to the allowed node mask (either the current
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08001712 * tasks mems_allowed, or node_states[N_MEMORY].)
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001713 *
1714 * If the zonelist cache is not available for this zonelist, does
1715 * nothing and returns NULL.
1716 *
1717 * If the fullzones BITMAP in the zonelist cache is stale (more than
1718 * a second since last zap'd) then we zap it out (clear its bits.)
1719 *
1720 * We hold off even calling zlc_setup, until after we've checked the
1721 * first zone in the zonelist, on the theory that most allocations will
1722 * be satisfied from that first zone, so best to examine that zone as
1723 * quickly as we can.
1724 */
1725static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
1726{
1727 struct zonelist_cache *zlc; /* cached zonelist speedup info */
1728 nodemask_t *allowednodes; /* zonelist_cache approximation */
1729
1730 zlc = zonelist->zlcache_ptr;
1731 if (!zlc)
1732 return NULL;
1733
S.Caglar Onurf05111f2008-04-28 02:12:38 -07001734 if (time_after(jiffies, zlc->last_full_zap + HZ)) {
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001735 bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
1736 zlc->last_full_zap = jiffies;
1737 }
1738
1739 allowednodes = !in_interrupt() && (alloc_flags & ALLOC_CPUSET) ?
1740 &cpuset_current_mems_allowed :
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08001741 &node_states[N_MEMORY];
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001742 return allowednodes;
1743}
1744
1745/*
1746 * Given 'z' scanning a zonelist, run a couple of quick checks to see
1747 * if it is worth looking at further for free memory:
1748 * 1) Check that the zone isn't thought to be full (doesn't have its
1749 * bit set in the zonelist_cache fullzones BITMAP).
1750 * 2) Check that the zones node (obtained from the zonelist_cache
1751 * z_to_n[] mapping) is allowed in the passed in allowednodes mask.
1752 * Return true (non-zero) if zone is worth looking at further, or
1753 * else return false (zero) if it is not.
1754 *
1755 * This check -ignores- the distinction between various watermarks,
1756 * such as GFP_HIGH, GFP_ATOMIC, PF_MEMALLOC, ... If a zone is
1757 * found to be full for any variation of these watermarks, it will
1758 * be considered full for up to one second by all requests, unless
1759 * we are so low on memory on all allowed nodes that we are forced
1760 * into the second scan of the zonelist.
1761 *
1762 * In the second scan we ignore this zonelist cache and exactly
1763 * apply the watermarks to all zones, even it is slower to do so.
1764 * We are low on memory in the second scan, and should leave no stone
1765 * unturned looking for a free page.
1766 */
Mel Gormandd1a2392008-04-28 02:12:17 -07001767static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zoneref *z,
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001768 nodemask_t *allowednodes)
1769{
1770 struct zonelist_cache *zlc; /* cached zonelist speedup info */
1771 int i; /* index of *z in zonelist zones */
1772 int n; /* node that zone *z is on */
1773
1774 zlc = zonelist->zlcache_ptr;
1775 if (!zlc)
1776 return 1;
1777
Mel Gormandd1a2392008-04-28 02:12:17 -07001778 i = z - zonelist->_zonerefs;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001779 n = zlc->z_to_n[i];
1780
1781 /* This zone is worth trying if it is allowed but not full */
1782 return node_isset(n, *allowednodes) && !test_bit(i, zlc->fullzones);
1783}
1784
1785/*
1786 * Given 'z' scanning a zonelist, set the corresponding bit in
1787 * zlc->fullzones, so that subsequent attempts to allocate a page
1788 * from that zone don't waste time re-examining it.
1789 */
Mel Gormandd1a2392008-04-28 02:12:17 -07001790static void zlc_mark_zone_full(struct zonelist *zonelist, struct zoneref *z)
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001791{
1792 struct zonelist_cache *zlc; /* cached zonelist speedup info */
1793 int i; /* index of *z in zonelist zones */
1794
1795 zlc = zonelist->zlcache_ptr;
1796 if (!zlc)
1797 return;
1798
Mel Gormandd1a2392008-04-28 02:12:17 -07001799 i = z - zonelist->_zonerefs;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001800
1801 set_bit(i, zlc->fullzones);
1802}
1803
Mel Gorman76d3fbf2011-07-25 17:12:30 -07001804/*
1805 * clear all zones full, called after direct reclaim makes progress so that
1806 * a zone that was recently full is not skipped over for up to a second
1807 */
1808static void zlc_clear_zones_full(struct zonelist *zonelist)
1809{
1810 struct zonelist_cache *zlc; /* cached zonelist speedup info */
1811
1812 zlc = zonelist->zlcache_ptr;
1813 if (!zlc)
1814 return;
1815
1816 bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
1817}
1818
David Rientjes957f8222012-10-08 16:33:24 -07001819static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone)
1820{
1821 return node_isset(local_zone->node, zone->zone_pgdat->reclaim_nodes);
1822}
1823
1824static void __paginginit init_zone_allows_reclaim(int nid)
1825{
1826 int i;
1827
1828 for_each_online_node(i)
David Rientjes6b187d022012-10-25 13:38:08 -07001829 if (node_distance(nid, i) <= RECLAIM_DISTANCE)
David Rientjes957f8222012-10-08 16:33:24 -07001830 node_set(i, NODE_DATA(nid)->reclaim_nodes);
David Rientjes6b187d022012-10-25 13:38:08 -07001831 else
David Rientjes957f8222012-10-08 16:33:24 -07001832 zone_reclaim_mode = 1;
David Rientjes957f8222012-10-08 16:33:24 -07001833}
1834
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001835#else /* CONFIG_NUMA */
1836
1837static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
1838{
1839 return NULL;
1840}
1841
Mel Gormandd1a2392008-04-28 02:12:17 -07001842static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zoneref *z,
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001843 nodemask_t *allowednodes)
1844{
1845 return 1;
1846}
1847
Mel Gormandd1a2392008-04-28 02:12:17 -07001848static void zlc_mark_zone_full(struct zonelist *zonelist, struct zoneref *z)
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001849{
1850}
Mel Gorman76d3fbf2011-07-25 17:12:30 -07001851
1852static void zlc_clear_zones_full(struct zonelist *zonelist)
1853{
1854}
David Rientjes957f8222012-10-08 16:33:24 -07001855
1856static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone)
1857{
1858 return true;
1859}
1860
1861static inline void init_zone_allows_reclaim(int nid)
1862{
1863}
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001864#endif /* CONFIG_NUMA */
1865
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001866/*
Paul Jackson0798e512006-12-06 20:31:38 -08001867 * get_page_from_freelist goes through the zonelist trying to allocate
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001868 * a page.
1869 */
1870static struct page *
Mel Gorman19770b32008-04-28 02:12:18 -07001871get_page_from_freelist(gfp_t gfp_mask, nodemask_t *nodemask, unsigned int order,
Mel Gorman5117f452009-06-16 15:31:59 -07001872 struct zonelist *zonelist, int high_zoneidx, int alloc_flags,
Mel Gorman3dd28262009-06-16 15:32:00 -07001873 struct zone *preferred_zone, int migratetype)
Martin Hicks753ee722005-06-21 17:14:41 -07001874{
Mel Gormandd1a2392008-04-28 02:12:17 -07001875 struct zoneref *z;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001876 struct page *page = NULL;
Mel Gorman54a6eb52008-04-28 02:12:16 -07001877 int classzone_idx;
Mel Gorman5117f452009-06-16 15:31:59 -07001878 struct zone *zone;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001879 nodemask_t *allowednodes = NULL;/* zonelist_cache approximation */
1880 int zlc_active = 0; /* set if using zonelist_cache */
1881 int did_zlc_setup = 0; /* just call zlc_setup() one time */
Mel Gorman54a6eb52008-04-28 02:12:16 -07001882
Mel Gorman19770b32008-04-28 02:12:18 -07001883 classzone_idx = zone_idx(preferred_zone);
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001884zonelist_scan:
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001885 /*
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001886 * Scan zonelist, looking for a zone with enough free.
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001887 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
1888 */
Mel Gorman19770b32008-04-28 02:12:18 -07001889 for_each_zone_zonelist_nodemask(zone, z, zonelist,
1890 high_zoneidx, nodemask) {
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08001891 if (IS_ENABLED(CONFIG_NUMA) && zlc_active &&
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001892 !zlc_zone_worth_trying(zonelist, z, allowednodes))
1893 continue;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001894 if ((alloc_flags & ALLOC_CPUSET) &&
Paul Jackson02a0e532006-12-13 00:34:25 -08001895 !cpuset_zone_allowed_softwall(zone, gfp_mask))
Mel Gormancd38b112011-07-25 17:12:29 -07001896 continue;
Johannes Weinera756cf52012-01-10 15:07:49 -08001897 /*
1898 * When allocating a page cache page for writing, we
1899 * want to get it from a zone that is within its dirty
1900 * limit, such that no single zone holds more than its
1901 * proportional share of globally allowed dirty pages.
1902 * The dirty limits take into account the zone's
1903 * lowmem reserves and high watermark so that kswapd
1904 * should be able to balance it without having to
1905 * write pages from its LRU list.
1906 *
1907 * This may look like it could increase pressure on
1908 * lower zones by failing allocations in higher zones
1909 * before they are full. But the pages that do spill
1910 * over are limited as the lower zones are protected
1911 * by this very same mechanism. It should not become
1912 * a practical burden to them.
1913 *
1914 * XXX: For now, allow allocations to potentially
1915 * exceed the per-zone dirty limit in the slowpath
1916 * (ALLOC_WMARK_LOW unset) before going into reclaim,
1917 * which is important when on a NUMA setup the allowed
1918 * zones are together not big enough to reach the
1919 * global limit. The proper fix for these situations
1920 * will require awareness of zones in the
1921 * dirty-throttling and the flusher threads.
1922 */
1923 if ((alloc_flags & ALLOC_WMARK_LOW) &&
1924 (gfp_mask & __GFP_WRITE) && !zone_dirty_ok(zone))
1925 goto this_zone_full;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001926
Mel Gorman41858962009-06-16 15:32:12 -07001927 BUILD_BUG_ON(ALLOC_NO_WATERMARKS < NR_WMARK);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001928 if (!(alloc_flags & ALLOC_NO_WATERMARKS)) {
Nick Piggin31488902005-11-28 13:44:03 -08001929 unsigned long mark;
Mel Gormanfa5e0842009-06-16 15:33:22 -07001930 int ret;
1931
Mel Gorman41858962009-06-16 15:32:12 -07001932 mark = zone->watermark[alloc_flags & ALLOC_WMARK_MASK];
Mel Gormanfa5e0842009-06-16 15:33:22 -07001933 if (zone_watermark_ok(zone, order, mark,
1934 classzone_idx, alloc_flags))
1935 goto try_this_zone;
1936
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08001937 if (IS_ENABLED(CONFIG_NUMA) &&
1938 !did_zlc_setup && nr_online_nodes > 1) {
Mel Gormancd38b112011-07-25 17:12:29 -07001939 /*
1940 * we do zlc_setup if there are multiple nodes
1941 * and before considering the first zone allowed
1942 * by the cpuset.
1943 */
1944 allowednodes = zlc_setup(zonelist, alloc_flags);
1945 zlc_active = 1;
1946 did_zlc_setup = 1;
1947 }
1948
David Rientjes957f8222012-10-08 16:33:24 -07001949 if (zone_reclaim_mode == 0 ||
1950 !zone_allows_reclaim(preferred_zone, zone))
Mel Gormanfa5e0842009-06-16 15:33:22 -07001951 goto this_zone_full;
1952
Mel Gormancd38b112011-07-25 17:12:29 -07001953 /*
1954 * As we may have just activated ZLC, check if the first
1955 * eligible zone has failed zone_reclaim recently.
1956 */
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08001957 if (IS_ENABLED(CONFIG_NUMA) && zlc_active &&
Mel Gormancd38b112011-07-25 17:12:29 -07001958 !zlc_zone_worth_trying(zonelist, z, allowednodes))
1959 continue;
1960
Mel Gormanfa5e0842009-06-16 15:33:22 -07001961 ret = zone_reclaim(zone, gfp_mask, order);
1962 switch (ret) {
1963 case ZONE_RECLAIM_NOSCAN:
1964 /* did not scan */
Mel Gormancd38b112011-07-25 17:12:29 -07001965 continue;
Mel Gormanfa5e0842009-06-16 15:33:22 -07001966 case ZONE_RECLAIM_FULL:
1967 /* scanned but unreclaimable */
Mel Gormancd38b112011-07-25 17:12:29 -07001968 continue;
Mel Gormanfa5e0842009-06-16 15:33:22 -07001969 default:
1970 /* did we reclaim enough */
Mel Gormanfed27192013-04-29 15:07:57 -07001971 if (zone_watermark_ok(zone, order, mark,
Mel Gormanfa5e0842009-06-16 15:33:22 -07001972 classzone_idx, alloc_flags))
Mel Gormanfed27192013-04-29 15:07:57 -07001973 goto try_this_zone;
1974
1975 /*
1976 * Failed to reclaim enough to meet watermark.
1977 * Only mark the zone full if checking the min
1978 * watermark or if we failed to reclaim just
1979 * 1<<order pages or else the page allocator
1980 * fastpath will prematurely mark zones full
1981 * when the watermark is between the low and
1982 * min watermarks.
1983 */
1984 if (((alloc_flags & ALLOC_WMARK_MASK) == ALLOC_WMARK_MIN) ||
1985 ret == ZONE_RECLAIM_SOME)
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001986 goto this_zone_full;
Mel Gormanfed27192013-04-29 15:07:57 -07001987
1988 continue;
Paul Jackson0798e512006-12-06 20:31:38 -08001989 }
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001990 }
1991
Mel Gormanfa5e0842009-06-16 15:33:22 -07001992try_this_zone:
Mel Gorman3dd28262009-06-16 15:32:00 -07001993 page = buffered_rmqueue(preferred_zone, zone, order,
1994 gfp_mask, migratetype);
Paul Jackson0798e512006-12-06 20:31:38 -08001995 if (page)
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001996 break;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001997this_zone_full:
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08001998 if (IS_ENABLED(CONFIG_NUMA))
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001999 zlc_mark_zone_full(zonelist, z);
Mel Gorman54a6eb52008-04-28 02:12:16 -07002000 }
Paul Jackson9276b1bc2006-12-06 20:31:48 -08002001
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08002002 if (unlikely(IS_ENABLED(CONFIG_NUMA) && page == NULL && zlc_active)) {
Paul Jackson9276b1bc2006-12-06 20:31:48 -08002003 /* Disable zlc cache for second zonelist scan */
2004 zlc_active = 0;
2005 goto zonelist_scan;
2006 }
Alex Shib1211862012-08-21 16:16:08 -07002007
2008 if (page)
2009 /*
2010 * page->pfmemalloc is set when ALLOC_NO_WATERMARKS was
2011 * necessary to allocate the page. The expectation is
2012 * that the caller is taking steps that will free more
2013 * memory. The caller should avoid the page being used
2014 * for !PFMEMALLOC purposes.
2015 */
2016 page->pfmemalloc = !!(alloc_flags & ALLOC_NO_WATERMARKS);
2017
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002018 return page;
Martin Hicks753ee722005-06-21 17:14:41 -07002019}
2020
David Rientjes29423e772011-03-22 16:30:47 -07002021/*
2022 * Large machines with many possible nodes should not always dump per-node
2023 * meminfo in irq context.
2024 */
2025static inline bool should_suppress_show_mem(void)
2026{
2027 bool ret = false;
2028
2029#if NODES_SHIFT > 8
2030 ret = in_interrupt();
2031#endif
2032 return ret;
2033}
2034
Dave Hansena238ab52011-05-24 17:12:16 -07002035static DEFINE_RATELIMIT_STATE(nopage_rs,
2036 DEFAULT_RATELIMIT_INTERVAL,
2037 DEFAULT_RATELIMIT_BURST);
2038
2039void warn_alloc_failed(gfp_t gfp_mask, int order, const char *fmt, ...)
2040{
Dave Hansena238ab52011-05-24 17:12:16 -07002041 unsigned int filter = SHOW_MEM_FILTER_NODES;
2042
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -08002043 if ((gfp_mask & __GFP_NOWARN) || !__ratelimit(&nopage_rs) ||
2044 debug_guardpage_minorder() > 0)
Dave Hansena238ab52011-05-24 17:12:16 -07002045 return;
2046
2047 /*
David Rientjes4b59e6c2013-04-29 15:06:11 -07002048 * Walking all memory to count page types is very expensive and should
2049 * be inhibited in non-blockable contexts.
2050 */
2051 if (!(gfp_mask & __GFP_WAIT))
2052 filter |= SHOW_MEM_FILTER_PAGE_COUNT;
2053
2054 /*
Dave Hansena238ab52011-05-24 17:12:16 -07002055 * This documents exceptions given to allocations in certain
2056 * contexts that are allowed to allocate outside current's set
2057 * of allowed nodes.
2058 */
2059 if (!(gfp_mask & __GFP_NOMEMALLOC))
2060 if (test_thread_flag(TIF_MEMDIE) ||
2061 (current->flags & (PF_MEMALLOC | PF_EXITING)))
2062 filter &= ~SHOW_MEM_FILTER_NODES;
2063 if (in_interrupt() || !(gfp_mask & __GFP_WAIT))
2064 filter &= ~SHOW_MEM_FILTER_NODES;
2065
2066 if (fmt) {
Joe Perches3ee9a4f2011-10-31 17:08:35 -07002067 struct va_format vaf;
2068 va_list args;
2069
Dave Hansena238ab52011-05-24 17:12:16 -07002070 va_start(args, fmt);
Joe Perches3ee9a4f2011-10-31 17:08:35 -07002071
2072 vaf.fmt = fmt;
2073 vaf.va = &args;
2074
2075 pr_warn("%pV", &vaf);
2076
Dave Hansena238ab52011-05-24 17:12:16 -07002077 va_end(args);
2078 }
2079
Joe Perches3ee9a4f2011-10-31 17:08:35 -07002080 pr_warn("%s: page allocation failure: order:%d, mode:0x%x\n",
2081 current->comm, order, gfp_mask);
Dave Hansena238ab52011-05-24 17:12:16 -07002082
2083 dump_stack();
2084 if (!should_suppress_show_mem())
2085 show_mem(filter);
2086}
2087
Mel Gorman11e33f62009-06-16 15:31:57 -07002088static inline int
2089should_alloc_retry(gfp_t gfp_mask, unsigned int order,
Mel Gormanf90ac392012-01-10 15:07:15 -08002090 unsigned long did_some_progress,
Mel Gorman11e33f62009-06-16 15:31:57 -07002091 unsigned long pages_reclaimed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
Mel Gorman11e33f62009-06-16 15:31:57 -07002093 /* Do not loop if specifically requested */
2094 if (gfp_mask & __GFP_NORETRY)
2095 return 0;
2096
Mel Gormanf90ac392012-01-10 15:07:15 -08002097 /* Always retry if specifically requested */
2098 if (gfp_mask & __GFP_NOFAIL)
2099 return 1;
2100
2101 /*
2102 * Suspend converts GFP_KERNEL to __GFP_WAIT which can prevent reclaim
2103 * making forward progress without invoking OOM. Suspend also disables
2104 * storage devices so kswapd will not help. Bail if we are suspending.
2105 */
2106 if (!did_some_progress && pm_suspended_storage())
2107 return 0;
2108
Mel Gorman11e33f62009-06-16 15:31:57 -07002109 /*
2110 * In this implementation, order <= PAGE_ALLOC_COSTLY_ORDER
2111 * means __GFP_NOFAIL, but that may not be true in other
2112 * implementations.
2113 */
2114 if (order <= PAGE_ALLOC_COSTLY_ORDER)
2115 return 1;
2116
2117 /*
2118 * For order > PAGE_ALLOC_COSTLY_ORDER, if __GFP_REPEAT is
2119 * specified, then we retry until we no longer reclaim any pages
2120 * (above), or we've reclaimed an order of pages at least as
2121 * large as the allocation's order. In both cases, if the
2122 * allocation still fails, we stop retrying.
2123 */
2124 if (gfp_mask & __GFP_REPEAT && pages_reclaimed < (1 << order))
2125 return 1;
2126
Mel Gorman11e33f62009-06-16 15:31:57 -07002127 return 0;
2128}
2129
2130static inline struct page *
2131__alloc_pages_may_oom(gfp_t gfp_mask, unsigned int order,
2132 struct zonelist *zonelist, enum zone_type high_zoneidx,
Mel Gorman3dd28262009-06-16 15:32:00 -07002133 nodemask_t *nodemask, struct zone *preferred_zone,
2134 int migratetype)
Mel Gorman11e33f62009-06-16 15:31:57 -07002135{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Mel Gorman11e33f62009-06-16 15:31:57 -07002138 /* Acquire the OOM killer lock for the zones in zonelist */
Minchan Kimff321fe2010-08-09 17:18:57 -07002139 if (!try_set_zonelist_oom(zonelist, gfp_mask)) {
Mel Gorman11e33f62009-06-16 15:31:57 -07002140 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 return NULL;
2142 }
Jens Axboe6b1de912005-11-17 21:35:02 +01002143
Mel Gorman11e33f62009-06-16 15:31:57 -07002144 /*
2145 * Go through the zonelist yet one more time, keep very high watermark
2146 * here, this is only to catch a parallel oom killing, we must fail if
2147 * we're still under heavy pressure.
2148 */
2149 page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, nodemask,
2150 order, zonelist, high_zoneidx,
Mel Gorman5117f452009-06-16 15:31:59 -07002151 ALLOC_WMARK_HIGH|ALLOC_CPUSET,
Mel Gorman3dd28262009-06-16 15:32:00 -07002152 preferred_zone, migratetype);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002153 if (page)
Mel Gorman11e33f62009-06-16 15:31:57 -07002154 goto out;
2155
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -08002156 if (!(gfp_mask & __GFP_NOFAIL)) {
2157 /* The OOM killer will not help higher order allocs */
2158 if (order > PAGE_ALLOC_COSTLY_ORDER)
2159 goto out;
David Rientjes03668b32010-08-09 17:18:54 -07002160 /* The OOM killer does not needlessly kill tasks for lowmem */
2161 if (high_zoneidx < ZONE_NORMAL)
2162 goto out;
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -08002163 /*
2164 * GFP_THISNODE contains __GFP_NORETRY and we never hit this.
2165 * Sanity check for bare calls of __GFP_THISNODE, not real OOM.
2166 * The caller should handle page allocation failure by itself if
2167 * it specifies __GFP_THISNODE.
2168 * Note: Hugepage uses it but will hit PAGE_ALLOC_COSTLY_ORDER.
2169 */
2170 if (gfp_mask & __GFP_THISNODE)
2171 goto out;
2172 }
Mel Gorman11e33f62009-06-16 15:31:57 -07002173 /* Exhausted what can be done so it's blamo time */
David Rientjes08ab9b12012-03-21 16:34:04 -07002174 out_of_memory(zonelist, gfp_mask, order, nodemask, false);
Mel Gorman11e33f62009-06-16 15:31:57 -07002175
2176out:
2177 clear_zonelist_oom(zonelist, gfp_mask);
2178 return page;
2179}
2180
Mel Gorman56de7262010-05-24 14:32:30 -07002181#ifdef CONFIG_COMPACTION
2182/* Try memory compaction for high-order allocations before reclaim */
2183static struct page *
2184__alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
2185 struct zonelist *zonelist, enum zone_type high_zoneidx,
2186 nodemask_t *nodemask, int alloc_flags, struct zone *preferred_zone,
Mel Gorman66199712012-01-12 17:19:41 -08002187 int migratetype, bool sync_migration,
Mel Gormanc67fe372012-08-21 16:16:17 -07002188 bool *contended_compaction, bool *deferred_compaction,
Mel Gorman66199712012-01-12 17:19:41 -08002189 unsigned long *did_some_progress)
Mel Gorman56de7262010-05-24 14:32:30 -07002190{
Mel Gorman66199712012-01-12 17:19:41 -08002191 if (!order)
Mel Gorman56de7262010-05-24 14:32:30 -07002192 return NULL;
2193
Rik van Rielaff62242012-03-21 16:33:52 -07002194 if (compaction_deferred(preferred_zone, order)) {
Mel Gorman66199712012-01-12 17:19:41 -08002195 *deferred_compaction = true;
2196 return NULL;
2197 }
2198
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08002199 current->flags |= PF_MEMALLOC;
Mel Gorman56de7262010-05-24 14:32:30 -07002200 *did_some_progress = try_to_compact_pages(zonelist, order, gfp_mask,
Mel Gormanc67fe372012-08-21 16:16:17 -07002201 nodemask, sync_migration,
Mel Gorman8fb74b92013-01-11 14:32:16 -08002202 contended_compaction);
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08002203 current->flags &= ~PF_MEMALLOC;
Mel Gorman56de7262010-05-24 14:32:30 -07002204
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07002205 if (*did_some_progress != COMPACT_SKIPPED) {
Mel Gorman8fb74b92013-01-11 14:32:16 -08002206 struct page *page;
2207
Mel Gorman56de7262010-05-24 14:32:30 -07002208 /* Page migration frees to the PCP lists but we want merging */
2209 drain_pages(get_cpu());
2210 put_cpu();
2211
2212 page = get_page_from_freelist(gfp_mask, nodemask,
2213 order, zonelist, high_zoneidx,
Mel Gormancfd19c52012-07-31 16:44:10 -07002214 alloc_flags & ~ALLOC_NO_WATERMARKS,
2215 preferred_zone, migratetype);
Mel Gorman56de7262010-05-24 14:32:30 -07002216 if (page) {
Mel Gorman62997022012-10-08 16:32:47 -07002217 preferred_zone->compact_blockskip_flush = false;
Mel Gorman4f92e252010-05-24 14:32:32 -07002218 preferred_zone->compact_considered = 0;
2219 preferred_zone->compact_defer_shift = 0;
Rik van Rielaff62242012-03-21 16:33:52 -07002220 if (order >= preferred_zone->compact_order_failed)
2221 preferred_zone->compact_order_failed = order + 1;
Mel Gorman56de7262010-05-24 14:32:30 -07002222 count_vm_event(COMPACTSUCCESS);
2223 return page;
2224 }
2225
2226 /*
2227 * It's bad if compaction run occurs and fails.
2228 * The most likely reason is that pages exist,
2229 * but not enough to satisfy watermarks.
2230 */
2231 count_vm_event(COMPACTFAIL);
Mel Gorman66199712012-01-12 17:19:41 -08002232
2233 /*
2234 * As async compaction considers a subset of pageblocks, only
2235 * defer if the failure was a sync compaction failure.
2236 */
2237 if (sync_migration)
Rik van Rielaff62242012-03-21 16:33:52 -07002238 defer_compaction(preferred_zone, order);
Mel Gorman56de7262010-05-24 14:32:30 -07002239
2240 cond_resched();
2241 }
2242
2243 return NULL;
2244}
2245#else
2246static inline struct page *
2247__alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
2248 struct zonelist *zonelist, enum zone_type high_zoneidx,
2249 nodemask_t *nodemask, int alloc_flags, struct zone *preferred_zone,
Mel Gorman66199712012-01-12 17:19:41 -08002250 int migratetype, bool sync_migration,
Mel Gormanc67fe372012-08-21 16:16:17 -07002251 bool *contended_compaction, bool *deferred_compaction,
Mel Gorman66199712012-01-12 17:19:41 -08002252 unsigned long *did_some_progress)
Mel Gorman56de7262010-05-24 14:32:30 -07002253{
2254 return NULL;
2255}
2256#endif /* CONFIG_COMPACTION */
2257
Marek Szyprowskibba90712012-01-25 12:09:52 +01002258/* Perform direct synchronous page reclaim */
2259static int
2260__perform_reclaim(gfp_t gfp_mask, unsigned int order, struct zonelist *zonelist,
2261 nodemask_t *nodemask)
Mel Gorman11e33f62009-06-16 15:31:57 -07002262{
Mel Gorman11e33f62009-06-16 15:31:57 -07002263 struct reclaim_state reclaim_state;
Marek Szyprowskibba90712012-01-25 12:09:52 +01002264 int progress;
Mel Gorman11e33f62009-06-16 15:31:57 -07002265
2266 cond_resched();
2267
2268 /* We now go into synchronous reclaim */
2269 cpuset_memory_pressure_bump();
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08002270 current->flags |= PF_MEMALLOC;
Mel Gorman11e33f62009-06-16 15:31:57 -07002271 lockdep_set_current_reclaim_state(gfp_mask);
2272 reclaim_state.reclaimed_slab = 0;
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08002273 current->reclaim_state = &reclaim_state;
Mel Gorman11e33f62009-06-16 15:31:57 -07002274
Marek Szyprowskibba90712012-01-25 12:09:52 +01002275 progress = try_to_free_pages(zonelist, order, gfp_mask, nodemask);
Mel Gorman11e33f62009-06-16 15:31:57 -07002276
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08002277 current->reclaim_state = NULL;
Mel Gorman11e33f62009-06-16 15:31:57 -07002278 lockdep_clear_current_reclaim_state();
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08002279 current->flags &= ~PF_MEMALLOC;
Mel Gorman11e33f62009-06-16 15:31:57 -07002280
2281 cond_resched();
2282
Marek Szyprowskibba90712012-01-25 12:09:52 +01002283 return progress;
2284}
2285
2286/* The really slow allocator path where we enter direct reclaim */
2287static inline struct page *
2288__alloc_pages_direct_reclaim(gfp_t gfp_mask, unsigned int order,
2289 struct zonelist *zonelist, enum zone_type high_zoneidx,
2290 nodemask_t *nodemask, int alloc_flags, struct zone *preferred_zone,
2291 int migratetype, unsigned long *did_some_progress)
2292{
2293 struct page *page = NULL;
2294 bool drained = false;
2295
2296 *did_some_progress = __perform_reclaim(gfp_mask, order, zonelist,
2297 nodemask);
Mel Gorman9ee493c2010-09-09 16:38:18 -07002298 if (unlikely(!(*did_some_progress)))
2299 return NULL;
Mel Gorman11e33f62009-06-16 15:31:57 -07002300
Mel Gorman76d3fbf2011-07-25 17:12:30 -07002301 /* After successful reclaim, reconsider all zones for allocation */
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08002302 if (IS_ENABLED(CONFIG_NUMA))
Mel Gorman76d3fbf2011-07-25 17:12:30 -07002303 zlc_clear_zones_full(zonelist);
2304
Mel Gorman9ee493c2010-09-09 16:38:18 -07002305retry:
2306 page = get_page_from_freelist(gfp_mask, nodemask, order,
Mel Gorman5117f452009-06-16 15:31:59 -07002307 zonelist, high_zoneidx,
Mel Gormancfd19c52012-07-31 16:44:10 -07002308 alloc_flags & ~ALLOC_NO_WATERMARKS,
2309 preferred_zone, migratetype);
Mel Gorman9ee493c2010-09-09 16:38:18 -07002310
2311 /*
2312 * If an allocation failed after direct reclaim, it could be because
2313 * pages are pinned on the per-cpu lists. Drain them and try again
2314 */
2315 if (!page && !drained) {
2316 drain_all_pages();
2317 drained = true;
2318 goto retry;
2319 }
2320
Mel Gorman11e33f62009-06-16 15:31:57 -07002321 return page;
2322}
2323
Mel Gorman11e33f62009-06-16 15:31:57 -07002324/*
2325 * This is called in the allocator slow-path if the allocation request is of
2326 * sufficient urgency to ignore watermarks and take other desperate measures
2327 */
2328static inline struct page *
2329__alloc_pages_high_priority(gfp_t gfp_mask, unsigned int order,
2330 struct zonelist *zonelist, enum zone_type high_zoneidx,
Mel Gorman3dd28262009-06-16 15:32:00 -07002331 nodemask_t *nodemask, struct zone *preferred_zone,
2332 int migratetype)
Mel Gorman11e33f62009-06-16 15:31:57 -07002333{
2334 struct page *page;
2335
2336 do {
2337 page = get_page_from_freelist(gfp_mask, nodemask, order,
Mel Gorman5117f452009-06-16 15:31:59 -07002338 zonelist, high_zoneidx, ALLOC_NO_WATERMARKS,
Mel Gorman3dd28262009-06-16 15:32:00 -07002339 preferred_zone, migratetype);
Mel Gorman11e33f62009-06-16 15:31:57 -07002340
2341 if (!page && gfp_mask & __GFP_NOFAIL)
Mel Gorman0e093d992010-10-26 14:21:45 -07002342 wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/50);
Mel Gorman11e33f62009-06-16 15:31:57 -07002343 } while (!page && (gfp_mask & __GFP_NOFAIL));
2344
2345 return page;
2346}
2347
2348static inline
2349void wake_all_kswapd(unsigned int order, struct zonelist *zonelist,
Mel Gorman99504742011-01-13 15:46:20 -08002350 enum zone_type high_zoneidx,
2351 enum zone_type classzone_idx)
Mel Gorman11e33f62009-06-16 15:31:57 -07002352{
2353 struct zoneref *z;
2354 struct zone *zone;
2355
2356 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx)
Mel Gorman99504742011-01-13 15:46:20 -08002357 wakeup_kswapd(zone, order, classzone_idx);
Mel Gorman11e33f62009-06-16 15:31:57 -07002358}
2359
Peter Zijlstra341ce062009-06-16 15:32:02 -07002360static inline int
2361gfp_to_alloc_flags(gfp_t gfp_mask)
2362{
Peter Zijlstra341ce062009-06-16 15:32:02 -07002363 int alloc_flags = ALLOC_WMARK_MIN | ALLOC_CPUSET;
2364 const gfp_t wait = gfp_mask & __GFP_WAIT;
2365
Mel Gormana56f57f2009-06-16 15:32:02 -07002366 /* __GFP_HIGH is assumed to be the same as ALLOC_HIGH to save a branch. */
Namhyung Kime6223a32010-10-26 14:21:59 -07002367 BUILD_BUG_ON(__GFP_HIGH != (__force gfp_t) ALLOC_HIGH);
Mel Gormana56f57f2009-06-16 15:32:02 -07002368
Peter Zijlstra341ce062009-06-16 15:32:02 -07002369 /*
2370 * The caller may dip into page reserves a bit more if the caller
2371 * cannot run direct reclaim, or if the caller has realtime scheduling
2372 * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will
2373 * set both ALLOC_HARDER (!wait) and ALLOC_HIGH (__GFP_HIGH).
2374 */
Namhyung Kime6223a32010-10-26 14:21:59 -07002375 alloc_flags |= (__force int) (gfp_mask & __GFP_HIGH);
Peter Zijlstra341ce062009-06-16 15:32:02 -07002376
2377 if (!wait) {
Andrea Arcangeli5c3240d2011-01-13 15:46:49 -08002378 /*
2379 * Not worth trying to allocate harder for
2380 * __GFP_NOMEMALLOC even if it can't schedule.
2381 */
2382 if (!(gfp_mask & __GFP_NOMEMALLOC))
2383 alloc_flags |= ALLOC_HARDER;
Peter Zijlstra341ce062009-06-16 15:32:02 -07002384 /*
2385 * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
2386 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
2387 */
2388 alloc_flags &= ~ALLOC_CPUSET;
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08002389 } else if (unlikely(rt_task(current)) && !in_interrupt())
Peter Zijlstra341ce062009-06-16 15:32:02 -07002390 alloc_flags |= ALLOC_HARDER;
2391
Mel Gormanb37f1dd2012-07-31 16:44:03 -07002392 if (likely(!(gfp_mask & __GFP_NOMEMALLOC))) {
2393 if (gfp_mask & __GFP_MEMALLOC)
2394 alloc_flags |= ALLOC_NO_WATERMARKS;
Mel Gorman907aed42012-07-31 16:44:07 -07002395 else if (in_serving_softirq() && (current->flags & PF_MEMALLOC))
2396 alloc_flags |= ALLOC_NO_WATERMARKS;
2397 else if (!in_interrupt() &&
2398 ((current->flags & PF_MEMALLOC) ||
2399 unlikely(test_thread_flag(TIF_MEMDIE))))
Peter Zijlstra341ce062009-06-16 15:32:02 -07002400 alloc_flags |= ALLOC_NO_WATERMARKS;
2401 }
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07002402#ifdef CONFIG_CMA
2403 if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
2404 alloc_flags |= ALLOC_CMA;
2405#endif
Peter Zijlstra341ce062009-06-16 15:32:02 -07002406 return alloc_flags;
2407}
2408
Mel Gorman072bb0a2012-07-31 16:43:58 -07002409bool gfp_pfmemalloc_allowed(gfp_t gfp_mask)
2410{
Mel Gormanb37f1dd2012-07-31 16:44:03 -07002411 return !!(gfp_to_alloc_flags(gfp_mask) & ALLOC_NO_WATERMARKS);
Mel Gorman072bb0a2012-07-31 16:43:58 -07002412}
2413
Mel Gorman11e33f62009-06-16 15:31:57 -07002414static inline struct page *
2415__alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
2416 struct zonelist *zonelist, enum zone_type high_zoneidx,
Mel Gorman3dd28262009-06-16 15:32:00 -07002417 nodemask_t *nodemask, struct zone *preferred_zone,
2418 int migratetype)
Mel Gorman11e33f62009-06-16 15:31:57 -07002419{
2420 const gfp_t wait = gfp_mask & __GFP_WAIT;
2421 struct page *page = NULL;
2422 int alloc_flags;
2423 unsigned long pages_reclaimed = 0;
2424 unsigned long did_some_progress;
Mel Gorman77f1fe62011-01-13 15:45:57 -08002425 bool sync_migration = false;
Mel Gorman66199712012-01-12 17:19:41 -08002426 bool deferred_compaction = false;
Mel Gormanc67fe372012-08-21 16:16:17 -07002427 bool contended_compaction = false;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002428
Christoph Lameter952f3b52006-12-06 20:33:26 -08002429 /*
Mel Gorman72807a72009-06-16 15:32:18 -07002430 * In the slowpath, we sanity check order to avoid ever trying to
2431 * reclaim >= MAX_ORDER areas which will never succeed. Callers may
2432 * be using allocators in order of preference for an area that is
2433 * too large.
2434 */
Mel Gorman1fc28b72009-07-29 15:04:08 -07002435 if (order >= MAX_ORDER) {
2436 WARN_ON_ONCE(!(gfp_mask & __GFP_NOWARN));
Mel Gorman72807a72009-06-16 15:32:18 -07002437 return NULL;
Mel Gorman1fc28b72009-07-29 15:04:08 -07002438 }
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002439
Christoph Lameter952f3b52006-12-06 20:33:26 -08002440 /*
2441 * GFP_THISNODE (meaning __GFP_THISNODE, __GFP_NORETRY and
2442 * __GFP_NOWARN set) should not cause reclaim since the subsystem
2443 * (f.e. slab) using GFP_THISNODE may choose to trigger reclaim
2444 * using a larger set of nodes after it has established that the
2445 * allowed per node queues are empty and that nodes are
2446 * over allocated.
2447 */
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08002448 if (IS_ENABLED(CONFIG_NUMA) &&
2449 (gfp_mask & GFP_THISNODE) == GFP_THISNODE)
Christoph Lameter952f3b52006-12-06 20:33:26 -08002450 goto nopage;
2451
Mel Gormancc4a6852009-11-11 14:26:14 -08002452restart:
Linus Torvaldscaf49192012-12-10 10:51:16 -08002453 if (!(gfp_mask & __GFP_NO_KSWAPD))
2454 wake_all_kswapd(order, zonelist, high_zoneidx,
2455 zone_idx(preferred_zone));
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002456
Paul Jackson9bf22292005-09-06 15:18:12 -07002457 /*
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002458 * OK, we're below the kswapd watermark and have kicked background
2459 * reclaim. Now things get more complex, so set up alloc_flags according
2460 * to how we want to proceed.
Paul Jackson9bf22292005-09-06 15:18:12 -07002461 */
Peter Zijlstra341ce062009-06-16 15:32:02 -07002462 alloc_flags = gfp_to_alloc_flags(gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
David Rientjesf33261d2011-01-25 15:07:20 -08002464 /*
2465 * Find the true preferred zone if the allocation is unconstrained by
2466 * cpusets.
2467 */
2468 if (!(alloc_flags & ALLOC_CPUSET) && !nodemask)
2469 first_zones_zonelist(zonelist, high_zoneidx, NULL,
2470 &preferred_zone);
2471
Andrew Barrycfa54a02011-05-24 17:12:52 -07002472rebalance:
Peter Zijlstra341ce062009-06-16 15:32:02 -07002473 /* This is the last chance, in general, before the goto nopage. */
Mel Gorman19770b32008-04-28 02:12:18 -07002474 page = get_page_from_freelist(gfp_mask, nodemask, order, zonelist,
Peter Zijlstra341ce062009-06-16 15:32:02 -07002475 high_zoneidx, alloc_flags & ~ALLOC_NO_WATERMARKS,
2476 preferred_zone, migratetype);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002477 if (page)
2478 goto got_pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
Mel Gorman11e33f62009-06-16 15:31:57 -07002480 /* Allocate without watermarks if the context allows */
Peter Zijlstra341ce062009-06-16 15:32:02 -07002481 if (alloc_flags & ALLOC_NO_WATERMARKS) {
Mel Gorman183f6372012-07-31 16:44:12 -07002482 /*
2483 * Ignore mempolicies if ALLOC_NO_WATERMARKS on the grounds
2484 * the allocation is high priority and these type of
2485 * allocations are system rather than user orientated
2486 */
2487 zonelist = node_zonelist(numa_node_id(), gfp_mask);
2488
Peter Zijlstra341ce062009-06-16 15:32:02 -07002489 page = __alloc_pages_high_priority(gfp_mask, order,
2490 zonelist, high_zoneidx, nodemask,
2491 preferred_zone, migratetype);
Mel Gormancfd19c52012-07-31 16:44:10 -07002492 if (page) {
Peter Zijlstra341ce062009-06-16 15:32:02 -07002493 goto got_pg;
Mel Gormancfd19c52012-07-31 16:44:10 -07002494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 }
2496
2497 /* Atomic allocations - we can't balance anything */
2498 if (!wait)
2499 goto nopage;
2500
Peter Zijlstra341ce062009-06-16 15:32:02 -07002501 /* Avoid recursion of direct reclaim */
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08002502 if (current->flags & PF_MEMALLOC)
Peter Zijlstra341ce062009-06-16 15:32:02 -07002503 goto nopage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
David Rientjes6583bb62009-07-29 15:02:06 -07002505 /* Avoid allocations with no watermarks from looping endlessly */
2506 if (test_thread_flag(TIF_MEMDIE) && !(gfp_mask & __GFP_NOFAIL))
2507 goto nopage;
2508
Mel Gorman77f1fe62011-01-13 15:45:57 -08002509 /*
2510 * Try direct compaction. The first pass is asynchronous. Subsequent
2511 * attempts after direct reclaim are synchronous
2512 */
Mel Gorman56de7262010-05-24 14:32:30 -07002513 page = __alloc_pages_direct_compact(gfp_mask, order,
2514 zonelist, high_zoneidx,
2515 nodemask,
2516 alloc_flags, preferred_zone,
Mel Gorman66199712012-01-12 17:19:41 -08002517 migratetype, sync_migration,
Mel Gormanc67fe372012-08-21 16:16:17 -07002518 &contended_compaction,
Mel Gorman66199712012-01-12 17:19:41 -08002519 &deferred_compaction,
2520 &did_some_progress);
Mel Gorman56de7262010-05-24 14:32:30 -07002521 if (page)
2522 goto got_pg;
Andrea Arcangelic6a140b2011-05-24 17:11:38 -07002523 sync_migration = true;
Mel Gorman56de7262010-05-24 14:32:30 -07002524
Linus Torvalds31f8d422012-12-10 10:47:45 -08002525 /*
2526 * If compaction is deferred for high-order allocations, it is because
2527 * sync compaction recently failed. In this is the case and the caller
2528 * requested a movable allocation that does not heavily disrupt the
2529 * system then fail the allocation instead of entering direct reclaim.
2530 */
2531 if ((deferred_compaction || contended_compaction) &&
Linus Torvaldscaf49192012-12-10 10:51:16 -08002532 (gfp_mask & __GFP_NO_KSWAPD))
Linus Torvalds31f8d422012-12-10 10:47:45 -08002533 goto nopage;
Mel Gorman66199712012-01-12 17:19:41 -08002534
Mel Gorman11e33f62009-06-16 15:31:57 -07002535 /* Try direct reclaim and then allocating */
2536 page = __alloc_pages_direct_reclaim(gfp_mask, order,
2537 zonelist, high_zoneidx,
2538 nodemask,
Mel Gorman5117f452009-06-16 15:31:59 -07002539 alloc_flags, preferred_zone,
Mel Gorman3dd28262009-06-16 15:32:00 -07002540 migratetype, &did_some_progress);
Mel Gorman11e33f62009-06-16 15:31:57 -07002541 if (page)
2542 goto got_pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 /*
Mel Gorman11e33f62009-06-16 15:31:57 -07002545 * If we failed to make any progress reclaiming, then we are
2546 * running out of options and have to consider going OOM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 */
Mel Gorman11e33f62009-06-16 15:31:57 -07002548 if (!did_some_progress) {
2549 if ((gfp_mask & __GFP_FS) && !(gfp_mask & __GFP_NORETRY)) {
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -07002550 if (oom_killer_disabled)
2551 goto nopage;
David Rientjes29fd66d2012-03-28 14:42:41 -07002552 /* Coredumps can quickly deplete all memory reserves */
2553 if ((current->flags & PF_DUMPCORE) &&
2554 !(gfp_mask & __GFP_NOFAIL))
2555 goto nopage;
Mel Gorman11e33f62009-06-16 15:31:57 -07002556 page = __alloc_pages_may_oom(gfp_mask, order,
2557 zonelist, high_zoneidx,
Mel Gorman3dd28262009-06-16 15:32:00 -07002558 nodemask, preferred_zone,
2559 migratetype);
Mel Gorman11e33f62009-06-16 15:31:57 -07002560 if (page)
2561 goto got_pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
David Rientjes03668b32010-08-09 17:18:54 -07002563 if (!(gfp_mask & __GFP_NOFAIL)) {
2564 /*
2565 * The oom killer is not called for high-order
2566 * allocations that may fail, so if no progress
2567 * is being made, there are no other options and
2568 * retrying is unlikely to help.
2569 */
2570 if (order > PAGE_ALLOC_COSTLY_ORDER)
2571 goto nopage;
2572 /*
2573 * The oom killer is not called for lowmem
2574 * allocations to prevent needlessly killing
2575 * innocent tasks.
2576 */
2577 if (high_zoneidx < ZONE_NORMAL)
2578 goto nopage;
2579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 goto restart;
2582 }
2583 }
2584
Mel Gorman11e33f62009-06-16 15:31:57 -07002585 /* Check if we should retry the allocation */
Nishanth Aravamudana41f24e2008-04-29 00:58:25 -07002586 pages_reclaimed += did_some_progress;
Mel Gormanf90ac392012-01-10 15:07:15 -08002587 if (should_alloc_retry(gfp_mask, order, did_some_progress,
2588 pages_reclaimed)) {
Mel Gorman11e33f62009-06-16 15:31:57 -07002589 /* Wait for some write requests to complete then retry */
Mel Gorman0e093d992010-10-26 14:21:45 -07002590 wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 goto rebalance;
Mel Gorman3e7d3442011-01-13 15:45:56 -08002592 } else {
2593 /*
2594 * High-order allocations do not necessarily loop after
2595 * direct reclaim and reclaim/compaction depends on compaction
2596 * being called after reclaim so call directly if necessary
2597 */
2598 page = __alloc_pages_direct_compact(gfp_mask, order,
2599 zonelist, high_zoneidx,
2600 nodemask,
2601 alloc_flags, preferred_zone,
Mel Gorman66199712012-01-12 17:19:41 -08002602 migratetype, sync_migration,
Mel Gormanc67fe372012-08-21 16:16:17 -07002603 &contended_compaction,
Mel Gorman66199712012-01-12 17:19:41 -08002604 &deferred_compaction,
2605 &did_some_progress);
Mel Gorman3e7d3442011-01-13 15:45:56 -08002606 if (page)
2607 goto got_pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 }
2609
2610nopage:
Dave Hansena238ab52011-05-24 17:12:16 -07002611 warn_alloc_failed(gfp_mask, order, NULL);
Vegard Nossumb1eeab62008-11-25 16:55:53 +01002612 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613got_pg:
Vegard Nossumb1eeab62008-11-25 16:55:53 +01002614 if (kmemcheck_enabled)
2615 kmemcheck_pagealloc_alloc(page, order, gfp_mask);
Mel Gorman11e33f62009-06-16 15:31:57 -07002616
Mel Gorman072bb0a2012-07-31 16:43:58 -07002617 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618}
Mel Gorman11e33f62009-06-16 15:31:57 -07002619
2620/*
2621 * This is the 'heart' of the zoned buddy allocator.
2622 */
2623struct page *
2624__alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order,
2625 struct zonelist *zonelist, nodemask_t *nodemask)
2626{
2627 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
Mel Gorman5117f452009-06-16 15:31:59 -07002628 struct zone *preferred_zone;
Mel Gormancc9a6c82012-03-21 16:34:11 -07002629 struct page *page = NULL;
Mel Gorman3dd28262009-06-16 15:32:00 -07002630 int migratetype = allocflags_to_migratetype(gfp_mask);
Mel Gormancc9a6c82012-03-21 16:34:11 -07002631 unsigned int cpuset_mems_cookie;
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07002632 int alloc_flags = ALLOC_WMARK_LOW|ALLOC_CPUSET;
Glauber Costa6a1a0d32012-12-18 14:22:00 -08002633 struct mem_cgroup *memcg = NULL;
Mel Gorman11e33f62009-06-16 15:31:57 -07002634
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +10002635 gfp_mask &= gfp_allowed_mask;
2636
Mel Gorman11e33f62009-06-16 15:31:57 -07002637 lockdep_trace_alloc(gfp_mask);
2638
2639 might_sleep_if(gfp_mask & __GFP_WAIT);
2640
2641 if (should_fail_alloc_page(gfp_mask, order))
2642 return NULL;
2643
2644 /*
2645 * Check the zones suitable for the gfp_mask contain at least one
2646 * valid zone. It's possible to have an empty zonelist as a result
2647 * of GFP_THISNODE and a memoryless node
2648 */
2649 if (unlikely(!zonelist->_zonerefs->zone))
2650 return NULL;
2651
Glauber Costa6a1a0d32012-12-18 14:22:00 -08002652 /*
2653 * Will only have any effect when __GFP_KMEMCG is set. This is
2654 * verified in the (always inline) callee
2655 */
2656 if (!memcg_kmem_newpage_charge(gfp_mask, &memcg, order))
2657 return NULL;
2658
Mel Gormancc9a6c82012-03-21 16:34:11 -07002659retry_cpuset:
2660 cpuset_mems_cookie = get_mems_allowed();
2661
Mel Gorman5117f452009-06-16 15:31:59 -07002662 /* The preferred zone is used for statistics later */
David Rientjesf33261d2011-01-25 15:07:20 -08002663 first_zones_zonelist(zonelist, high_zoneidx,
2664 nodemask ? : &cpuset_current_mems_allowed,
2665 &preferred_zone);
Mel Gormancc9a6c82012-03-21 16:34:11 -07002666 if (!preferred_zone)
2667 goto out;
Mel Gorman5117f452009-06-16 15:31:59 -07002668
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07002669#ifdef CONFIG_CMA
2670 if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
2671 alloc_flags |= ALLOC_CMA;
2672#endif
Mel Gorman5117f452009-06-16 15:31:59 -07002673 /* First allocation attempt */
Mel Gorman11e33f62009-06-16 15:31:57 -07002674 page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, nodemask, order,
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07002675 zonelist, high_zoneidx, alloc_flags,
Mel Gorman3dd28262009-06-16 15:32:00 -07002676 preferred_zone, migratetype);
Ming Lei21caf2f2013-02-22 16:34:08 -08002677 if (unlikely(!page)) {
2678 /*
2679 * Runtime PM, block IO and its error handling path
2680 * can deadlock because I/O on the device might not
2681 * complete.
2682 */
2683 gfp_mask = memalloc_noio_flags(gfp_mask);
Mel Gorman11e33f62009-06-16 15:31:57 -07002684 page = __alloc_pages_slowpath(gfp_mask, order,
Mel Gorman5117f452009-06-16 15:31:59 -07002685 zonelist, high_zoneidx, nodemask,
Mel Gorman3dd28262009-06-16 15:32:00 -07002686 preferred_zone, migratetype);
Ming Lei21caf2f2013-02-22 16:34:08 -08002687 }
Mel Gorman11e33f62009-06-16 15:31:57 -07002688
Mel Gorman4b4f2782009-09-21 17:02:41 -07002689 trace_mm_page_alloc(page, order, gfp_mask, migratetype);
Mel Gormancc9a6c82012-03-21 16:34:11 -07002690
2691out:
2692 /*
2693 * When updating a task's mems_allowed, it is possible to race with
2694 * parallel threads in such a way that an allocation can fail while
2695 * the mask is being updated. If a page allocation is about to fail,
2696 * check if the cpuset changed during allocation and if so, retry.
2697 */
2698 if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !page))
2699 goto retry_cpuset;
2700
Glauber Costa6a1a0d32012-12-18 14:22:00 -08002701 memcg_kmem_commit_charge(page, memcg, order);
2702
Mel Gorman11e33f62009-06-16 15:31:57 -07002703 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704}
Mel Gormand2391712009-06-16 15:31:52 -07002705EXPORT_SYMBOL(__alloc_pages_nodemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
2707/*
2708 * Common helper functions.
2709 */
Harvey Harrison920c7a52008-02-04 22:29:26 -08002710unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711{
Akinobu Mita945a1112009-09-21 17:01:47 -07002712 struct page *page;
2713
2714 /*
2715 * __get_free_pages() returns a 32-bit address, which cannot represent
2716 * a highmem page
2717 */
2718 VM_BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
2719
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 page = alloc_pages(gfp_mask, order);
2721 if (!page)
2722 return 0;
2723 return (unsigned long) page_address(page);
2724}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725EXPORT_SYMBOL(__get_free_pages);
2726
Harvey Harrison920c7a52008-02-04 22:29:26 -08002727unsigned long get_zeroed_page(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728{
Akinobu Mita945a1112009-09-21 17:01:47 -07002729 return __get_free_pages(gfp_mask | __GFP_ZERO, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731EXPORT_SYMBOL(get_zeroed_page);
2732
Harvey Harrison920c7a52008-02-04 22:29:26 -08002733void __free_pages(struct page *page, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734{
Nick Pigginb5810032005-10-29 18:16:12 -07002735 if (put_page_testzero(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 if (order == 0)
Li Hongfc916682010-03-05 13:41:54 -08002737 free_hot_cold_page(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 else
2739 __free_pages_ok(page, order);
2740 }
2741}
2742
2743EXPORT_SYMBOL(__free_pages);
2744
Harvey Harrison920c7a52008-02-04 22:29:26 -08002745void free_pages(unsigned long addr, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746{
2747 if (addr != 0) {
Nick Piggin725d7042006-09-25 23:30:55 -07002748 VM_BUG_ON(!virt_addr_valid((void *)addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 __free_pages(virt_to_page((void *)addr), order);
2750 }
2751}
2752
2753EXPORT_SYMBOL(free_pages);
2754
Glauber Costa6a1a0d32012-12-18 14:22:00 -08002755/*
2756 * __free_memcg_kmem_pages and free_memcg_kmem_pages will free
2757 * pages allocated with __GFP_KMEMCG.
2758 *
2759 * Those pages are accounted to a particular memcg, embedded in the
2760 * corresponding page_cgroup. To avoid adding a hit in the allocator to search
2761 * for that information only to find out that it is NULL for users who have no
2762 * interest in that whatsoever, we provide these functions.
2763 *
2764 * The caller knows better which flags it relies on.
2765 */
2766void __free_memcg_kmem_pages(struct page *page, unsigned int order)
2767{
2768 memcg_kmem_uncharge_pages(page, order);
2769 __free_pages(page, order);
2770}
2771
2772void free_memcg_kmem_pages(unsigned long addr, unsigned int order)
2773{
2774 if (addr != 0) {
2775 VM_BUG_ON(!virt_addr_valid((void *)addr));
2776 __free_memcg_kmem_pages(virt_to_page((void *)addr), order);
2777 }
2778}
2779
Andi Kleenee85c2e2011-05-11 15:13:34 -07002780static void *make_alloc_exact(unsigned long addr, unsigned order, size_t size)
2781{
2782 if (addr) {
2783 unsigned long alloc_end = addr + (PAGE_SIZE << order);
2784 unsigned long used = addr + PAGE_ALIGN(size);
2785
2786 split_page(virt_to_page((void *)addr), order);
2787 while (used < alloc_end) {
2788 free_page(used);
2789 used += PAGE_SIZE;
2790 }
2791 }
2792 return (void *)addr;
2793}
2794
Timur Tabi2be0ffe2008-07-23 21:28:11 -07002795/**
2796 * alloc_pages_exact - allocate an exact number physically-contiguous pages.
2797 * @size: the number of bytes to allocate
2798 * @gfp_mask: GFP flags for the allocation
2799 *
2800 * This function is similar to alloc_pages(), except that it allocates the
2801 * minimum number of pages to satisfy the request. alloc_pages() can only
2802 * allocate memory in power-of-two pages.
2803 *
2804 * This function is also limited by MAX_ORDER.
2805 *
2806 * Memory allocated by this function must be released by free_pages_exact().
2807 */
2808void *alloc_pages_exact(size_t size, gfp_t gfp_mask)
2809{
2810 unsigned int order = get_order(size);
2811 unsigned long addr;
2812
2813 addr = __get_free_pages(gfp_mask, order);
Andi Kleenee85c2e2011-05-11 15:13:34 -07002814 return make_alloc_exact(addr, order, size);
Timur Tabi2be0ffe2008-07-23 21:28:11 -07002815}
2816EXPORT_SYMBOL(alloc_pages_exact);
2817
2818/**
Andi Kleenee85c2e2011-05-11 15:13:34 -07002819 * alloc_pages_exact_nid - allocate an exact number of physically-contiguous
2820 * pages on a node.
Randy Dunlapb5e6ab52011-05-16 13:16:54 -07002821 * @nid: the preferred node ID where memory should be allocated
Andi Kleenee85c2e2011-05-11 15:13:34 -07002822 * @size: the number of bytes to allocate
2823 * @gfp_mask: GFP flags for the allocation
2824 *
2825 * Like alloc_pages_exact(), but try to allocate on node nid first before falling
2826 * back.
2827 * Note this is not alloc_pages_exact_node() which allocates on a specific node,
2828 * but is not exact.
2829 */
2830void *alloc_pages_exact_nid(int nid, size_t size, gfp_t gfp_mask)
2831{
2832 unsigned order = get_order(size);
2833 struct page *p = alloc_pages_node(nid, gfp_mask, order);
2834 if (!p)
2835 return NULL;
2836 return make_alloc_exact((unsigned long)page_address(p), order, size);
2837}
2838EXPORT_SYMBOL(alloc_pages_exact_nid);
2839
2840/**
Timur Tabi2be0ffe2008-07-23 21:28:11 -07002841 * free_pages_exact - release memory allocated via alloc_pages_exact()
2842 * @virt: the value returned by alloc_pages_exact.
2843 * @size: size of allocation, same value as passed to alloc_pages_exact().
2844 *
2845 * Release the memory allocated by a previous call to alloc_pages_exact.
2846 */
2847void free_pages_exact(void *virt, size_t size)
2848{
2849 unsigned long addr = (unsigned long)virt;
2850 unsigned long end = addr + PAGE_ALIGN(size);
2851
2852 while (addr < end) {
2853 free_page(addr);
2854 addr += PAGE_SIZE;
2855 }
2856}
2857EXPORT_SYMBOL(free_pages_exact);
2858
Zhang Yanfeie0fb5812013-02-22 16:35:54 -08002859/**
2860 * nr_free_zone_pages - count number of pages beyond high watermark
2861 * @offset: The zone index of the highest zone
2862 *
2863 * nr_free_zone_pages() counts the number of counts pages which are beyond the
2864 * high watermark within all zones at or below a given zone index. For each
2865 * zone, the number of pages is calculated as:
Jiang Liu834405c2013-07-03 15:03:04 -07002866 * managed_pages - high_pages
Zhang Yanfeie0fb5812013-02-22 16:35:54 -08002867 */
Zhang Yanfeiebec3862013-02-22 16:35:43 -08002868static unsigned long nr_free_zone_pages(int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869{
Mel Gormandd1a2392008-04-28 02:12:17 -07002870 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07002871 struct zone *zone;
2872
Martin J. Blighe310fd42005-07-29 22:59:18 -07002873 /* Just pick one node, since fallback list is circular */
Zhang Yanfeiebec3862013-02-22 16:35:43 -08002874 unsigned long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
Mel Gorman0e884602008-04-28 02:12:14 -07002876 struct zonelist *zonelist = node_zonelist(numa_node_id(), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
Mel Gorman54a6eb52008-04-28 02:12:16 -07002878 for_each_zone_zonelist(zone, z, zonelist, offset) {
Jiang Liub40da042013-02-22 16:33:52 -08002879 unsigned long size = zone->managed_pages;
Mel Gorman41858962009-06-16 15:32:12 -07002880 unsigned long high = high_wmark_pages(zone);
Martin J. Blighe310fd42005-07-29 22:59:18 -07002881 if (size > high)
2882 sum += size - high;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 }
2884
2885 return sum;
2886}
2887
Zhang Yanfeie0fb5812013-02-22 16:35:54 -08002888/**
2889 * nr_free_buffer_pages - count number of pages beyond high watermark
2890 *
2891 * nr_free_buffer_pages() counts the number of pages which are beyond the high
2892 * watermark within ZONE_DMA and ZONE_NORMAL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 */
Zhang Yanfeiebec3862013-02-22 16:35:43 -08002894unsigned long nr_free_buffer_pages(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895{
Al Viroaf4ca452005-10-21 02:55:38 -04002896 return nr_free_zone_pages(gfp_zone(GFP_USER));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897}
Meelap Shahc2f1a552007-07-17 04:04:39 -07002898EXPORT_SYMBOL_GPL(nr_free_buffer_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
Zhang Yanfeie0fb5812013-02-22 16:35:54 -08002900/**
2901 * nr_free_pagecache_pages - count number of pages beyond high watermark
2902 *
2903 * nr_free_pagecache_pages() counts the number of pages which are beyond the
2904 * high watermark within all zones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 */
Zhang Yanfeiebec3862013-02-22 16:35:43 -08002906unsigned long nr_free_pagecache_pages(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907{
Mel Gorman2a1e2742007-07-17 04:03:12 -07002908 return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER_MOVABLE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909}
Christoph Lameter08e0f6a2006-09-27 01:50:06 -07002910
2911static inline void show_node(struct zone *zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912{
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08002913 if (IS_ENABLED(CONFIG_NUMA))
Andy Whitcroft25ba77c2006-12-06 20:33:03 -08002914 printk("Node %d ", zone_to_nid(zone));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917void si_meminfo(struct sysinfo *val)
2918{
2919 val->totalram = totalram_pages;
2920 val->sharedram = 0;
Christoph Lameterd23ad422007-02-10 01:43:02 -08002921 val->freeram = global_page_state(NR_FREE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 val->bufferram = nr_blockdev_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 val->totalhigh = totalhigh_pages;
2924 val->freehigh = nr_free_highpages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 val->mem_unit = PAGE_SIZE;
2926}
2927
2928EXPORT_SYMBOL(si_meminfo);
2929
2930#ifdef CONFIG_NUMA
2931void si_meminfo_node(struct sysinfo *val, int nid)
2932{
Jiang Liucdd91a72013-07-03 15:03:27 -07002933 int zone_type; /* needs to be signed */
2934 unsigned long managed_pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 pg_data_t *pgdat = NODE_DATA(nid);
2936
Jiang Liucdd91a72013-07-03 15:03:27 -07002937 for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++)
2938 managed_pages += pgdat->node_zones[zone_type].managed_pages;
2939 val->totalram = managed_pages;
Christoph Lameterd23ad422007-02-10 01:43:02 -08002940 val->freeram = node_page_state(nid, NR_FREE_PAGES);
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07002941#ifdef CONFIG_HIGHMEM
Jiang Liub40da042013-02-22 16:33:52 -08002942 val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].managed_pages;
Christoph Lameterd23ad422007-02-10 01:43:02 -08002943 val->freehigh = zone_page_state(&pgdat->node_zones[ZONE_HIGHMEM],
2944 NR_FREE_PAGES);
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07002945#else
2946 val->totalhigh = 0;
2947 val->freehigh = 0;
2948#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 val->mem_unit = PAGE_SIZE;
2950}
2951#endif
2952
David Rientjesddd588b2011-03-22 16:30:46 -07002953/*
David Rientjes7bf02ea2011-05-24 17:11:16 -07002954 * Determine whether the node should be displayed or not, depending on whether
2955 * SHOW_MEM_FILTER_NODES was passed to show_free_areas().
David Rientjesddd588b2011-03-22 16:30:46 -07002956 */
David Rientjes7bf02ea2011-05-24 17:11:16 -07002957bool skip_free_areas_node(unsigned int flags, int nid)
David Rientjesddd588b2011-03-22 16:30:46 -07002958{
2959 bool ret = false;
Mel Gormancc9a6c82012-03-21 16:34:11 -07002960 unsigned int cpuset_mems_cookie;
David Rientjesddd588b2011-03-22 16:30:46 -07002961
2962 if (!(flags & SHOW_MEM_FILTER_NODES))
2963 goto out;
2964
Mel Gormancc9a6c82012-03-21 16:34:11 -07002965 do {
2966 cpuset_mems_cookie = get_mems_allowed();
2967 ret = !node_isset(nid, cpuset_current_mems_allowed);
2968 } while (!put_mems_allowed(cpuset_mems_cookie));
David Rientjesddd588b2011-03-22 16:30:46 -07002969out:
2970 return ret;
2971}
2972
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973#define K(x) ((x) << (PAGE_SHIFT-10))
2974
Rabin Vincent377e4f12012-12-11 16:00:24 -08002975static void show_migration_types(unsigned char type)
2976{
2977 static const char types[MIGRATE_TYPES] = {
2978 [MIGRATE_UNMOVABLE] = 'U',
2979 [MIGRATE_RECLAIMABLE] = 'E',
2980 [MIGRATE_MOVABLE] = 'M',
2981 [MIGRATE_RESERVE] = 'R',
2982#ifdef CONFIG_CMA
2983 [MIGRATE_CMA] = 'C',
2984#endif
Minchan Kim194159f2013-02-22 16:33:58 -08002985#ifdef CONFIG_MEMORY_ISOLATION
Rabin Vincent377e4f12012-12-11 16:00:24 -08002986 [MIGRATE_ISOLATE] = 'I',
Minchan Kim194159f2013-02-22 16:33:58 -08002987#endif
Rabin Vincent377e4f12012-12-11 16:00:24 -08002988 };
2989 char tmp[MIGRATE_TYPES + 1];
2990 char *p = tmp;
2991 int i;
2992
2993 for (i = 0; i < MIGRATE_TYPES; i++) {
2994 if (type & (1 << i))
2995 *p++ = types[i];
2996 }
2997
2998 *p = '\0';
2999 printk("(%s) ", tmp);
3000}
3001
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002/*
3003 * Show free area list (used inside shift_scroll-lock stuff)
3004 * We also calculate the percentage fragmentation. We do this by counting the
3005 * memory on each free list with the exception of the first item on the list.
David Rientjesddd588b2011-03-22 16:30:46 -07003006 * Suppresses nodes that are not allowed by current's cpuset if
3007 * SHOW_MEM_FILTER_NODES is passed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 */
David Rientjes7bf02ea2011-05-24 17:11:16 -07003009void show_free_areas(unsigned int filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010{
Jes Sorensenc7241912006-09-27 01:50:05 -07003011 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 struct zone *zone;
3013
KOSAKI Motohiroee99c712009-03-31 15:19:31 -07003014 for_each_populated_zone(zone) {
David Rientjes7bf02ea2011-05-24 17:11:16 -07003015 if (skip_free_areas_node(filter, zone_to_nid(zone)))
David Rientjesddd588b2011-03-22 16:30:46 -07003016 continue;
Jes Sorensenc7241912006-09-27 01:50:05 -07003017 show_node(zone);
3018 printk("%s per-cpu:\n", zone->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
Dave Jones6b482c62005-11-10 15:45:56 -05003020 for_each_online_cpu(cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 struct per_cpu_pageset *pageset;
3022
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09003023 pageset = per_cpu_ptr(zone->pageset, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
Christoph Lameter3dfa5722008-02-04 22:29:19 -08003025 printk("CPU %4d: hi:%5d, btch:%4d usd:%4d\n",
3026 cpu, pageset->pcp.high,
3027 pageset->pcp.batch, pageset->pcp.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 }
3029 }
3030
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07003031 printk("active_anon:%lu inactive_anon:%lu isolated_anon:%lu\n"
3032 " active_file:%lu inactive_file:%lu isolated_file:%lu\n"
Lee Schermerhorn7b854122008-10-18 20:26:40 -07003033 " unevictable:%lu"
Andrew Mortonb76146e2009-10-26 16:49:52 -07003034 " dirty:%lu writeback:%lu unstable:%lu\n"
KOSAKI Motohiro3701b032009-09-21 17:01:29 -07003035 " free:%lu slab_reclaimable:%lu slab_unreclaimable:%lu\n"
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -07003036 " mapped:%lu shmem:%lu pagetables:%lu bounce:%lu\n"
3037 " free_cma:%lu\n",
Rik van Riel4f98a2f2008-10-18 20:26:32 -07003038 global_page_state(NR_ACTIVE_ANON),
Rik van Riel4f98a2f2008-10-18 20:26:32 -07003039 global_page_state(NR_INACTIVE_ANON),
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07003040 global_page_state(NR_ISOLATED_ANON),
3041 global_page_state(NR_ACTIVE_FILE),
Rik van Riel4f98a2f2008-10-18 20:26:32 -07003042 global_page_state(NR_INACTIVE_FILE),
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07003043 global_page_state(NR_ISOLATED_FILE),
Lee Schermerhorn7b854122008-10-18 20:26:40 -07003044 global_page_state(NR_UNEVICTABLE),
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -07003045 global_page_state(NR_FILE_DIRTY),
Christoph Lameterce866b32006-06-30 01:55:40 -07003046 global_page_state(NR_WRITEBACK),
Christoph Lameterfd39fc82006-06-30 01:55:40 -07003047 global_page_state(NR_UNSTABLE_NFS),
Christoph Lameterd23ad422007-02-10 01:43:02 -08003048 global_page_state(NR_FREE_PAGES),
KOSAKI Motohiro3701b032009-09-21 17:01:29 -07003049 global_page_state(NR_SLAB_RECLAIMABLE),
3050 global_page_state(NR_SLAB_UNRECLAIMABLE),
Christoph Lameter65ba55f2006-06-30 01:55:34 -07003051 global_page_state(NR_FILE_MAPPED),
KOSAKI Motohiro4b021082009-09-21 17:01:33 -07003052 global_page_state(NR_SHMEM),
Andrew Mortona25700a2007-02-08 14:20:40 -08003053 global_page_state(NR_PAGETABLE),
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -07003054 global_page_state(NR_BOUNCE),
3055 global_page_state(NR_FREE_CMA_PAGES));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
KOSAKI Motohiroee99c712009-03-31 15:19:31 -07003057 for_each_populated_zone(zone) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 int i;
3059
David Rientjes7bf02ea2011-05-24 17:11:16 -07003060 if (skip_free_areas_node(filter, zone_to_nid(zone)))
David Rientjesddd588b2011-03-22 16:30:46 -07003061 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 show_node(zone);
3063 printk("%s"
3064 " free:%lukB"
3065 " min:%lukB"
3066 " low:%lukB"
3067 " high:%lukB"
Rik van Riel4f98a2f2008-10-18 20:26:32 -07003068 " active_anon:%lukB"
3069 " inactive_anon:%lukB"
3070 " active_file:%lukB"
3071 " inactive_file:%lukB"
Lee Schermerhorn7b854122008-10-18 20:26:40 -07003072 " unevictable:%lukB"
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07003073 " isolated(anon):%lukB"
3074 " isolated(file):%lukB"
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 " present:%lukB"
Jiang Liu9feedc92012-12-12 13:52:12 -08003076 " managed:%lukB"
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07003077 " mlocked:%lukB"
3078 " dirty:%lukB"
3079 " writeback:%lukB"
3080 " mapped:%lukB"
KOSAKI Motohiro4b021082009-09-21 17:01:33 -07003081 " shmem:%lukB"
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07003082 " slab_reclaimable:%lukB"
3083 " slab_unreclaimable:%lukB"
KOSAKI Motohiroc6a7f572009-09-21 17:01:32 -07003084 " kernel_stack:%lukB"
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07003085 " pagetables:%lukB"
3086 " unstable:%lukB"
3087 " bounce:%lukB"
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -07003088 " free_cma:%lukB"
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07003089 " writeback_tmp:%lukB"
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 " pages_scanned:%lu"
3091 " all_unreclaimable? %s"
3092 "\n",
3093 zone->name,
Mel Gorman88f5acf2011-01-13 15:45:41 -08003094 K(zone_page_state(zone, NR_FREE_PAGES)),
Mel Gorman41858962009-06-16 15:32:12 -07003095 K(min_wmark_pages(zone)),
3096 K(low_wmark_pages(zone)),
3097 K(high_wmark_pages(zone)),
Rik van Riel4f98a2f2008-10-18 20:26:32 -07003098 K(zone_page_state(zone, NR_ACTIVE_ANON)),
3099 K(zone_page_state(zone, NR_INACTIVE_ANON)),
3100 K(zone_page_state(zone, NR_ACTIVE_FILE)),
3101 K(zone_page_state(zone, NR_INACTIVE_FILE)),
Lee Schermerhorn7b854122008-10-18 20:26:40 -07003102 K(zone_page_state(zone, NR_UNEVICTABLE)),
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07003103 K(zone_page_state(zone, NR_ISOLATED_ANON)),
3104 K(zone_page_state(zone, NR_ISOLATED_FILE)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 K(zone->present_pages),
Jiang Liu9feedc92012-12-12 13:52:12 -08003106 K(zone->managed_pages),
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07003107 K(zone_page_state(zone, NR_MLOCK)),
3108 K(zone_page_state(zone, NR_FILE_DIRTY)),
3109 K(zone_page_state(zone, NR_WRITEBACK)),
3110 K(zone_page_state(zone, NR_FILE_MAPPED)),
KOSAKI Motohiro4b021082009-09-21 17:01:33 -07003111 K(zone_page_state(zone, NR_SHMEM)),
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07003112 K(zone_page_state(zone, NR_SLAB_RECLAIMABLE)),
3113 K(zone_page_state(zone, NR_SLAB_UNRECLAIMABLE)),
KOSAKI Motohiroc6a7f572009-09-21 17:01:32 -07003114 zone_page_state(zone, NR_KERNEL_STACK) *
3115 THREAD_SIZE / 1024,
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07003116 K(zone_page_state(zone, NR_PAGETABLE)),
3117 K(zone_page_state(zone, NR_UNSTABLE_NFS)),
3118 K(zone_page_state(zone, NR_BOUNCE)),
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -07003119 K(zone_page_state(zone, NR_FREE_CMA_PAGES)),
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07003120 K(zone_page_state(zone, NR_WRITEBACK_TEMP)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 zone->pages_scanned,
KOSAKI Motohiro93e4a892010-03-05 13:41:55 -08003122 (zone->all_unreclaimable ? "yes" : "no")
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 );
3124 printk("lowmem_reserve[]:");
3125 for (i = 0; i < MAX_NR_ZONES; i++)
3126 printk(" %lu", zone->lowmem_reserve[i]);
3127 printk("\n");
3128 }
3129
KOSAKI Motohiroee99c712009-03-31 15:19:31 -07003130 for_each_populated_zone(zone) {
Pintu Kumarb8af2942013-09-11 14:20:34 -07003131 unsigned long nr[MAX_ORDER], flags, order, total = 0;
Rabin Vincent377e4f12012-12-11 16:00:24 -08003132 unsigned char types[MAX_ORDER];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
David Rientjes7bf02ea2011-05-24 17:11:16 -07003134 if (skip_free_areas_node(filter, zone_to_nid(zone)))
David Rientjesddd588b2011-03-22 16:30:46 -07003135 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 show_node(zone);
3137 printk("%s: ", zone->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138
3139 spin_lock_irqsave(&zone->lock, flags);
3140 for (order = 0; order < MAX_ORDER; order++) {
Rabin Vincent377e4f12012-12-11 16:00:24 -08003141 struct free_area *area = &zone->free_area[order];
3142 int type;
3143
3144 nr[order] = area->nr_free;
Kirill Korotaev8f9de512006-06-23 02:03:50 -07003145 total += nr[order] << order;
Rabin Vincent377e4f12012-12-11 16:00:24 -08003146
3147 types[order] = 0;
3148 for (type = 0; type < MIGRATE_TYPES; type++) {
3149 if (!list_empty(&area->free_list[type]))
3150 types[order] |= 1 << type;
3151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 }
3153 spin_unlock_irqrestore(&zone->lock, flags);
Rabin Vincent377e4f12012-12-11 16:00:24 -08003154 for (order = 0; order < MAX_ORDER; order++) {
Kirill Korotaev8f9de512006-06-23 02:03:50 -07003155 printk("%lu*%lukB ", nr[order], K(1UL) << order);
Rabin Vincent377e4f12012-12-11 16:00:24 -08003156 if (nr[order])
3157 show_migration_types(types[order]);
3158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 printk("= %lukB\n", K(total));
3160 }
3161
David Rientjes949f7ec2013-04-29 15:07:48 -07003162 hugetlb_show_meminfo();
3163
Larry Woodmane6f36022008-02-04 22:29:30 -08003164 printk("%ld total pagecache pages\n", global_page_state(NR_FILE_PAGES));
3165
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 show_swap_cache_info();
3167}
3168
Mel Gorman19770b32008-04-28 02:12:18 -07003169static void zoneref_set_zone(struct zone *zone, struct zoneref *zoneref)
3170{
3171 zoneref->zone = zone;
3172 zoneref->zone_idx = zone_idx(zone);
3173}
3174
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175/*
3176 * Builds allocation fallback zone lists.
Christoph Lameter1a932052006-01-06 00:11:16 -08003177 *
3178 * Add all populated zones of a node to the zonelist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003180static int build_zonelists_node(pg_data_t *pgdat, struct zonelist *zonelist,
Zhang Yanfeibc732f12013-07-08 16:00:06 -07003181 int nr_zones)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182{
Christoph Lameter1a932052006-01-06 00:11:16 -08003183 struct zone *zone;
Zhang Yanfeibc732f12013-07-08 16:00:06 -07003184 enum zone_type zone_type = MAX_NR_ZONES;
Christoph Lameter02a68a52006-01-06 00:11:18 -08003185
3186 do {
Christoph Lameter2f6726e2006-09-25 23:31:18 -07003187 zone_type--;
Christoph Lameter070f8032006-01-06 00:11:19 -08003188 zone = pgdat->node_zones + zone_type;
Christoph Lameter1a932052006-01-06 00:11:16 -08003189 if (populated_zone(zone)) {
Mel Gormandd1a2392008-04-28 02:12:17 -07003190 zoneref_set_zone(zone,
3191 &zonelist->_zonerefs[nr_zones++]);
Christoph Lameter070f8032006-01-06 00:11:19 -08003192 check_highest_zone(zone_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 }
Christoph Lameter2f6726e2006-09-25 23:31:18 -07003194 } while (zone_type);
Zhang Yanfeibc732f12013-07-08 16:00:06 -07003195
Christoph Lameter070f8032006-01-06 00:11:19 -08003196 return nr_zones;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197}
3198
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003199
3200/*
3201 * zonelist_order:
3202 * 0 = automatic detection of better ordering.
3203 * 1 = order by ([node] distance, -zonetype)
3204 * 2 = order by (-zonetype, [node] distance)
3205 *
3206 * If not NUMA, ZONELIST_ORDER_ZONE and ZONELIST_ORDER_NODE will create
3207 * the same zonelist. So only NUMA can configure this param.
3208 */
3209#define ZONELIST_ORDER_DEFAULT 0
3210#define ZONELIST_ORDER_NODE 1
3211#define ZONELIST_ORDER_ZONE 2
3212
3213/* zonelist order in the kernel.
3214 * set_zonelist_order() will set this to NODE or ZONE.
3215 */
3216static int current_zonelist_order = ZONELIST_ORDER_DEFAULT;
3217static char zonelist_order_name[3][8] = {"Default", "Node", "Zone"};
3218
3219
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220#ifdef CONFIG_NUMA
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003221/* The value user specified ....changed by config */
3222static int user_zonelist_order = ZONELIST_ORDER_DEFAULT;
3223/* string for sysctl */
3224#define NUMA_ZONELIST_ORDER_LEN 16
3225char numa_zonelist_order[16] = "default";
3226
3227/*
3228 * interface for configure zonelist ordering.
3229 * command line option "numa_zonelist_order"
3230 * = "[dD]efault - default, automatic configuration.
3231 * = "[nN]ode - order by node locality, then by zone within node
3232 * = "[zZ]one - order by zone, then by locality within zone
3233 */
3234
3235static int __parse_numa_zonelist_order(char *s)
3236{
3237 if (*s == 'd' || *s == 'D') {
3238 user_zonelist_order = ZONELIST_ORDER_DEFAULT;
3239 } else if (*s == 'n' || *s == 'N') {
3240 user_zonelist_order = ZONELIST_ORDER_NODE;
3241 } else if (*s == 'z' || *s == 'Z') {
3242 user_zonelist_order = ZONELIST_ORDER_ZONE;
3243 } else {
3244 printk(KERN_WARNING
3245 "Ignoring invalid numa_zonelist_order value: "
3246 "%s\n", s);
3247 return -EINVAL;
3248 }
3249 return 0;
3250}
3251
3252static __init int setup_numa_zonelist_order(char *s)
3253{
Volodymyr G. Lukiianykecb256f2011-01-13 15:46:26 -08003254 int ret;
3255
3256 if (!s)
3257 return 0;
3258
3259 ret = __parse_numa_zonelist_order(s);
3260 if (ret == 0)
3261 strlcpy(numa_zonelist_order, s, NUMA_ZONELIST_ORDER_LEN);
3262
3263 return ret;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003264}
3265early_param("numa_zonelist_order", setup_numa_zonelist_order);
3266
3267/*
3268 * sysctl handler for numa_zonelist_order
3269 */
3270int numa_zonelist_order_handler(ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003271 void __user *buffer, size_t *length,
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003272 loff_t *ppos)
3273{
3274 char saved_string[NUMA_ZONELIST_ORDER_LEN];
3275 int ret;
Andi Kleen443c6f12009-12-23 21:00:47 +01003276 static DEFINE_MUTEX(zl_order_mutex);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003277
Andi Kleen443c6f12009-12-23 21:00:47 +01003278 mutex_lock(&zl_order_mutex);
Chen Gangdacbde02013-07-03 15:02:35 -07003279 if (write) {
3280 if (strlen((char *)table->data) >= NUMA_ZONELIST_ORDER_LEN) {
3281 ret = -EINVAL;
3282 goto out;
3283 }
3284 strcpy(saved_string, (char *)table->data);
3285 }
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003286 ret = proc_dostring(table, write, buffer, length, ppos);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003287 if (ret)
Andi Kleen443c6f12009-12-23 21:00:47 +01003288 goto out;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003289 if (write) {
3290 int oldval = user_zonelist_order;
Chen Gangdacbde02013-07-03 15:02:35 -07003291
3292 ret = __parse_numa_zonelist_order((char *)table->data);
3293 if (ret) {
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003294 /*
3295 * bogus value. restore saved string
3296 */
Chen Gangdacbde02013-07-03 15:02:35 -07003297 strncpy((char *)table->data, saved_string,
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003298 NUMA_ZONELIST_ORDER_LEN);
3299 user_zonelist_order = oldval;
Haicheng Li4eaf3f62010-05-24 14:32:52 -07003300 } else if (oldval != user_zonelist_order) {
3301 mutex_lock(&zonelists_mutex);
Jiang Liu9adb62a2012-07-31 16:43:28 -07003302 build_all_zonelists(NULL, NULL);
Haicheng Li4eaf3f62010-05-24 14:32:52 -07003303 mutex_unlock(&zonelists_mutex);
3304 }
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003305 }
Andi Kleen443c6f12009-12-23 21:00:47 +01003306out:
3307 mutex_unlock(&zl_order_mutex);
3308 return ret;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003309}
3310
3311
Christoph Lameter62bc62a2009-06-16 15:32:15 -07003312#define MAX_NODE_LOAD (nr_online_nodes)
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003313static int node_load[MAX_NUMNODES];
3314
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315/**
Pavel Pisa4dc3b162005-05-01 08:59:25 -07003316 * 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 -07003317 * @node: node whose fallback list we're appending
3318 * @used_node_mask: nodemask_t of already used nodes
3319 *
3320 * We use a number of factors to determine which is the next node that should
3321 * appear on a given node's fallback list. The node should not have appeared
3322 * already in @node's fallback list, and it should be the next closest node
3323 * according to the distance array (which contains arbitrary distance values
3324 * from each node to each node in the system), and should also prefer nodes
3325 * with no CPUs, since presumably they'll have very little allocation pressure
3326 * on them otherwise.
3327 * It returns -1 if no node is found.
3328 */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003329static int find_next_best_node(int node, nodemask_t *used_node_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330{
Linus Torvalds4cf808e2006-02-17 20:38:21 +01003331 int n, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 int min_val = INT_MAX;
David Rientjes00ef2d22013-02-22 16:35:36 -08003333 int best_node = NUMA_NO_NODE;
Rusty Russella70f7302009-03-13 14:49:46 +10303334 const struct cpumask *tmp = cpumask_of_node(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335
Linus Torvalds4cf808e2006-02-17 20:38:21 +01003336 /* Use the local node if we haven't already */
3337 if (!node_isset(node, *used_node_mask)) {
3338 node_set(node, *used_node_mask);
3339 return node;
3340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08003342 for_each_node_state(n, N_MEMORY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343
3344 /* Don't want a node to appear more than once */
3345 if (node_isset(n, *used_node_mask))
3346 continue;
3347
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 /* Use the distance array to find the distance */
3349 val = node_distance(node, n);
3350
Linus Torvalds4cf808e2006-02-17 20:38:21 +01003351 /* Penalize nodes under us ("prefer the next node") */
3352 val += (n < node);
3353
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 /* Give preference to headless and unused nodes */
Rusty Russella70f7302009-03-13 14:49:46 +10303355 tmp = cpumask_of_node(n);
3356 if (!cpumask_empty(tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 val += PENALTY_FOR_NODE_WITH_CPUS;
3358
3359 /* Slight preference for less loaded node */
3360 val *= (MAX_NODE_LOAD*MAX_NUMNODES);
3361 val += node_load[n];
3362
3363 if (val < min_val) {
3364 min_val = val;
3365 best_node = n;
3366 }
3367 }
3368
3369 if (best_node >= 0)
3370 node_set(best_node, *used_node_mask);
3371
3372 return best_node;
3373}
3374
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003375
3376/*
3377 * Build zonelists ordered by node and zones within node.
3378 * This results in maximum locality--normal zone overflows into local
3379 * DMA zone, if any--but risks exhausting DMA zone.
3380 */
3381static void build_zonelists_in_node_order(pg_data_t *pgdat, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382{
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003383 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 struct zonelist *zonelist;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003385
Mel Gorman54a6eb52008-04-28 02:12:16 -07003386 zonelist = &pgdat->node_zonelists[0];
Mel Gormandd1a2392008-04-28 02:12:17 -07003387 for (j = 0; zonelist->_zonerefs[j].zone != NULL; j++)
Mel Gorman54a6eb52008-04-28 02:12:16 -07003388 ;
Zhang Yanfeibc732f12013-07-08 16:00:06 -07003389 j = build_zonelists_node(NODE_DATA(node), zonelist, j);
Mel Gormandd1a2392008-04-28 02:12:17 -07003390 zonelist->_zonerefs[j].zone = NULL;
3391 zonelist->_zonerefs[j].zone_idx = 0;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003392}
3393
3394/*
Christoph Lameter523b9452007-10-16 01:25:37 -07003395 * Build gfp_thisnode zonelists
3396 */
3397static void build_thisnode_zonelists(pg_data_t *pgdat)
3398{
Christoph Lameter523b9452007-10-16 01:25:37 -07003399 int j;
3400 struct zonelist *zonelist;
3401
Mel Gorman54a6eb52008-04-28 02:12:16 -07003402 zonelist = &pgdat->node_zonelists[1];
Zhang Yanfeibc732f12013-07-08 16:00:06 -07003403 j = build_zonelists_node(pgdat, zonelist, 0);
Mel Gormandd1a2392008-04-28 02:12:17 -07003404 zonelist->_zonerefs[j].zone = NULL;
3405 zonelist->_zonerefs[j].zone_idx = 0;
Christoph Lameter523b9452007-10-16 01:25:37 -07003406}
3407
3408/*
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003409 * Build zonelists ordered by zone and nodes within zones.
3410 * This results in conserving DMA zone[s] until all Normal memory is
3411 * exhausted, but results in overflowing to remote node while memory
3412 * may still exist in local DMA zone.
3413 */
3414static int node_order[MAX_NUMNODES];
3415
3416static void build_zonelists_in_zone_order(pg_data_t *pgdat, int nr_nodes)
3417{
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003418 int pos, j, node;
3419 int zone_type; /* needs to be signed */
3420 struct zone *z;
3421 struct zonelist *zonelist;
3422
Mel Gorman54a6eb52008-04-28 02:12:16 -07003423 zonelist = &pgdat->node_zonelists[0];
3424 pos = 0;
3425 for (zone_type = MAX_NR_ZONES - 1; zone_type >= 0; zone_type--) {
3426 for (j = 0; j < nr_nodes; j++) {
3427 node = node_order[j];
3428 z = &NODE_DATA(node)->node_zones[zone_type];
3429 if (populated_zone(z)) {
Mel Gormandd1a2392008-04-28 02:12:17 -07003430 zoneref_set_zone(z,
3431 &zonelist->_zonerefs[pos++]);
Mel Gorman54a6eb52008-04-28 02:12:16 -07003432 check_highest_zone(zone_type);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003433 }
3434 }
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003435 }
Mel Gormandd1a2392008-04-28 02:12:17 -07003436 zonelist->_zonerefs[pos].zone = NULL;
3437 zonelist->_zonerefs[pos].zone_idx = 0;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003438}
3439
3440static int default_zonelist_order(void)
3441{
3442 int nid, zone_type;
Pintu Kumarb8af2942013-09-11 14:20:34 -07003443 unsigned long low_kmem_size, total_size;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003444 struct zone *z;
3445 int average_size;
3446 /*
Pintu Kumarb8af2942013-09-11 14:20:34 -07003447 * ZONE_DMA and ZONE_DMA32 can be very small area in the system.
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003448 * If they are really small and used heavily, the system can fall
3449 * into OOM very easily.
David Rientjese325c902010-05-24 14:32:13 -07003450 * This function detect ZONE_DMA/DMA32 size and configures zone order.
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003451 */
3452 /* Is there ZONE_NORMAL ? (ex. ppc has only DMA zone..) */
3453 low_kmem_size = 0;
3454 total_size = 0;
3455 for_each_online_node(nid) {
3456 for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
3457 z = &NODE_DATA(nid)->node_zones[zone_type];
3458 if (populated_zone(z)) {
3459 if (zone_type < ZONE_NORMAL)
Jiang Liu4f9f4772013-07-03 15:03:07 -07003460 low_kmem_size += z->managed_pages;
3461 total_size += z->managed_pages;
David Rientjese325c902010-05-24 14:32:13 -07003462 } else if (zone_type == ZONE_NORMAL) {
3463 /*
3464 * If any node has only lowmem, then node order
3465 * is preferred to allow kernel allocations
3466 * locally; otherwise, they can easily infringe
3467 * on other nodes when there is an abundance of
3468 * lowmem available to allocate from.
3469 */
3470 return ZONELIST_ORDER_NODE;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003471 }
3472 }
3473 }
3474 if (!low_kmem_size || /* there are no DMA area. */
3475 low_kmem_size > total_size/2) /* DMA/DMA32 is big. */
3476 return ZONELIST_ORDER_NODE;
3477 /*
3478 * look into each node's config.
Pintu Kumarb8af2942013-09-11 14:20:34 -07003479 * If there is a node whose DMA/DMA32 memory is very big area on
3480 * local memory, NODE_ORDER may be suitable.
3481 */
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07003482 average_size = total_size /
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08003483 (nodes_weight(node_states[N_MEMORY]) + 1);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003484 for_each_online_node(nid) {
3485 low_kmem_size = 0;
3486 total_size = 0;
3487 for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
3488 z = &NODE_DATA(nid)->node_zones[zone_type];
3489 if (populated_zone(z)) {
3490 if (zone_type < ZONE_NORMAL)
3491 low_kmem_size += z->present_pages;
3492 total_size += z->present_pages;
3493 }
3494 }
3495 if (low_kmem_size &&
3496 total_size > average_size && /* ignore small node */
3497 low_kmem_size > total_size * 70/100)
3498 return ZONELIST_ORDER_NODE;
3499 }
3500 return ZONELIST_ORDER_ZONE;
3501}
3502
3503static void set_zonelist_order(void)
3504{
3505 if (user_zonelist_order == ZONELIST_ORDER_DEFAULT)
3506 current_zonelist_order = default_zonelist_order();
3507 else
3508 current_zonelist_order = user_zonelist_order;
3509}
3510
3511static void build_zonelists(pg_data_t *pgdat)
3512{
3513 int j, node, load;
3514 enum zone_type i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 nodemask_t used_mask;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003516 int local_node, prev_node;
3517 struct zonelist *zonelist;
3518 int order = current_zonelist_order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519
3520 /* initialize zonelists */
Christoph Lameter523b9452007-10-16 01:25:37 -07003521 for (i = 0; i < MAX_ZONELISTS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 zonelist = pgdat->node_zonelists + i;
Mel Gormandd1a2392008-04-28 02:12:17 -07003523 zonelist->_zonerefs[0].zone = NULL;
3524 zonelist->_zonerefs[0].zone_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 }
3526
3527 /* NUMA-aware ordering of nodes */
3528 local_node = pgdat->node_id;
Christoph Lameter62bc62a2009-06-16 15:32:15 -07003529 load = nr_online_nodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 prev_node = local_node;
3531 nodes_clear(used_mask);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003532
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003533 memset(node_order, 0, sizeof(node_order));
3534 j = 0;
3535
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
3537 /*
3538 * We don't want to pressure a particular node.
3539 * So adding penalty to the first node in same
3540 * distance group to make it round-robin.
3541 */
David Rientjes957f8222012-10-08 16:33:24 -07003542 if (node_distance(local_node, node) !=
3543 node_distance(local_node, prev_node))
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003544 node_load[node] = load;
3545
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 prev_node = node;
3547 load--;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003548 if (order == ZONELIST_ORDER_NODE)
3549 build_zonelists_in_node_order(pgdat, node);
3550 else
3551 node_order[j++] = node; /* remember order */
3552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003554 if (order == ZONELIST_ORDER_ZONE) {
3555 /* calculate node order -- i.e., DMA last! */
3556 build_zonelists_in_zone_order(pgdat, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 }
Christoph Lameter523b9452007-10-16 01:25:37 -07003558
3559 build_thisnode_zonelists(pgdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560}
3561
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003562/* Construct the zonelist performance cache - see further mmzone.h */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003563static void build_zonelist_cache(pg_data_t *pgdat)
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003564{
Mel Gorman54a6eb52008-04-28 02:12:16 -07003565 struct zonelist *zonelist;
3566 struct zonelist_cache *zlc;
Mel Gormandd1a2392008-04-28 02:12:17 -07003567 struct zoneref *z;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003568
Mel Gorman54a6eb52008-04-28 02:12:16 -07003569 zonelist = &pgdat->node_zonelists[0];
3570 zonelist->zlcache_ptr = zlc = &zonelist->zlcache;
3571 bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
Mel Gormandd1a2392008-04-28 02:12:17 -07003572 for (z = zonelist->_zonerefs; z->zone; z++)
3573 zlc->z_to_n[z - zonelist->_zonerefs] = zonelist_node_idx(z);
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003574}
3575
Lee Schermerhorn7aac7892010-05-26 14:45:00 -07003576#ifdef CONFIG_HAVE_MEMORYLESS_NODES
3577/*
3578 * Return node id of node used for "local" allocations.
3579 * I.e., first node id of first zone in arg node's generic zonelist.
3580 * Used for initializing percpu 'numa_mem', which is used primarily
3581 * for kernel allocations, so use GFP_KERNEL flags to locate zonelist.
3582 */
3583int local_memory_node(int node)
3584{
3585 struct zone *zone;
3586
3587 (void)first_zones_zonelist(node_zonelist(node, GFP_KERNEL),
3588 gfp_zone(GFP_KERNEL),
3589 NULL,
3590 &zone);
3591 return zone->node;
3592}
3593#endif
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003594
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595#else /* CONFIG_NUMA */
3596
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003597static void set_zonelist_order(void)
3598{
3599 current_zonelist_order = ZONELIST_ORDER_ZONE;
3600}
3601
3602static void build_zonelists(pg_data_t *pgdat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603{
Christoph Lameter19655d32006-09-25 23:31:19 -07003604 int node, local_node;
Mel Gorman54a6eb52008-04-28 02:12:16 -07003605 enum zone_type j;
3606 struct zonelist *zonelist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607
3608 local_node = pgdat->node_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609
Mel Gorman54a6eb52008-04-28 02:12:16 -07003610 zonelist = &pgdat->node_zonelists[0];
Zhang Yanfeibc732f12013-07-08 16:00:06 -07003611 j = build_zonelists_node(pgdat, zonelist, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612
Mel Gorman54a6eb52008-04-28 02:12:16 -07003613 /*
3614 * Now we build the zonelist so that it contains the zones
3615 * of all the other nodes.
3616 * We don't want to pressure a particular node, so when
3617 * building the zones for node N, we make sure that the
3618 * zones coming right after the local ones are those from
3619 * node N+1 (modulo N)
3620 */
3621 for (node = local_node + 1; node < MAX_NUMNODES; node++) {
3622 if (!node_online(node))
3623 continue;
Zhang Yanfeibc732f12013-07-08 16:00:06 -07003624 j = build_zonelists_node(NODE_DATA(node), zonelist, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 }
Mel Gorman54a6eb52008-04-28 02:12:16 -07003626 for (node = 0; node < local_node; node++) {
3627 if (!node_online(node))
3628 continue;
Zhang Yanfeibc732f12013-07-08 16:00:06 -07003629 j = build_zonelists_node(NODE_DATA(node), zonelist, j);
Mel Gorman54a6eb52008-04-28 02:12:16 -07003630 }
3631
Mel Gormandd1a2392008-04-28 02:12:17 -07003632 zonelist->_zonerefs[j].zone = NULL;
3633 zonelist->_zonerefs[j].zone_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634}
3635
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003636/* non-NUMA variant of zonelist performance cache - just NULL zlcache_ptr */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003637static void build_zonelist_cache(pg_data_t *pgdat)
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003638{
Mel Gorman54a6eb52008-04-28 02:12:16 -07003639 pgdat->node_zonelists[0].zlcache_ptr = NULL;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003640}
3641
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642#endif /* CONFIG_NUMA */
3643
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09003644/*
3645 * Boot pageset table. One per cpu which is going to be used for all
3646 * zones and all nodes. The parameters will be set in such a way
3647 * that an item put on a list will immediately be handed over to
3648 * the buddy list. This is safe since pageset manipulation is done
3649 * with interrupts disabled.
3650 *
3651 * The boot_pagesets must be kept even after bootup is complete for
3652 * unused processors and/or zones. They do play a role for bootstrapping
3653 * hotplugged processors.
3654 *
3655 * zoneinfo_show() and maybe other functions do
3656 * not check if the processor is online before following the pageset pointer.
3657 * Other parts of the kernel may not check if the zone is available.
3658 */
3659static void setup_pageset(struct per_cpu_pageset *p, unsigned long batch);
3660static DEFINE_PER_CPU(struct per_cpu_pageset, boot_pageset);
Haicheng Li1f522502010-05-24 14:32:51 -07003661static void setup_zone_pageset(struct zone *zone);
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09003662
Haicheng Li4eaf3f62010-05-24 14:32:52 -07003663/*
3664 * Global mutex to protect against size modification of zonelists
3665 * as well as to serialize pageset setup for the new populated zone.
3666 */
3667DEFINE_MUTEX(zonelists_mutex);
3668
Rusty Russell9b1a4d32008-07-28 12:16:30 -05003669/* return values int ....just for stop_machine() */
Jiang Liu4ed7e022012-07-31 16:43:35 -07003670static int __build_all_zonelists(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671{
Yasunori Goto68113782006-06-23 02:03:11 -07003672 int nid;
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09003673 int cpu;
Jiang Liu9adb62a2012-07-31 16:43:28 -07003674 pg_data_t *self = data;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003675
Bo Liu7f9cfb32009-08-18 14:11:19 -07003676#ifdef CONFIG_NUMA
3677 memset(node_load, 0, sizeof(node_load));
3678#endif
Jiang Liu9adb62a2012-07-31 16:43:28 -07003679
3680 if (self && !node_online(self->node_id)) {
3681 build_zonelists(self);
3682 build_zonelist_cache(self);
3683 }
3684
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003685 for_each_online_node(nid) {
Christoph Lameter7ea15302007-10-16 01:25:29 -07003686 pg_data_t *pgdat = NODE_DATA(nid);
3687
3688 build_zonelists(pgdat);
3689 build_zonelist_cache(pgdat);
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003690 }
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09003691
3692 /*
3693 * Initialize the boot_pagesets that are going to be used
3694 * for bootstrapping processors. The real pagesets for
3695 * each zone will be allocated later when the per cpu
3696 * allocator is available.
3697 *
3698 * boot_pagesets are used also for bootstrapping offline
3699 * cpus if the system is already booted because the pagesets
3700 * are needed to initialize allocators on a specific cpu too.
3701 * F.e. the percpu allocator needs the page allocator which
3702 * needs the percpu allocator in order to allocate its pagesets
3703 * (a chicken-egg dilemma).
3704 */
Lee Schermerhorn7aac7892010-05-26 14:45:00 -07003705 for_each_possible_cpu(cpu) {
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09003706 setup_pageset(&per_cpu(boot_pageset, cpu), 0);
3707
Lee Schermerhorn7aac7892010-05-26 14:45:00 -07003708#ifdef CONFIG_HAVE_MEMORYLESS_NODES
3709 /*
3710 * We now know the "local memory node" for each node--
3711 * i.e., the node of the first zone in the generic zonelist.
3712 * Set up numa_mem percpu variable for on-line cpus. During
3713 * boot, only the boot cpu should be on-line; we'll init the
3714 * secondary cpus' numa_mem as they come on-line. During
3715 * node/memory hotplug, we'll fixup all on-line cpus.
3716 */
3717 if (cpu_online(cpu))
3718 set_cpu_numa_mem(cpu, local_memory_node(cpu_to_node(cpu)));
3719#endif
3720 }
3721
Yasunori Goto68113782006-06-23 02:03:11 -07003722 return 0;
3723}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724
Haicheng Li4eaf3f62010-05-24 14:32:52 -07003725/*
3726 * Called with zonelists_mutex held always
3727 * unless system_state == SYSTEM_BOOTING.
3728 */
Jiang Liu9adb62a2012-07-31 16:43:28 -07003729void __ref build_all_zonelists(pg_data_t *pgdat, struct zone *zone)
Yasunori Goto68113782006-06-23 02:03:11 -07003730{
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003731 set_zonelist_order();
3732
Yasunori Goto68113782006-06-23 02:03:11 -07003733 if (system_state == SYSTEM_BOOTING) {
Randy Dunlap423b41d2006-09-27 01:50:12 -07003734 __build_all_zonelists(NULL);
Mel Gorman68ad8df2008-07-23 21:26:52 -07003735 mminit_verify_zonelist();
Yasunori Goto68113782006-06-23 02:03:11 -07003736 cpuset_init_current_mems_allowed();
3737 } else {
KAMEZAWA Hiroyukie9959f02010-11-24 12:57:09 -08003738#ifdef CONFIG_MEMORY_HOTPLUG
Jiang Liu9adb62a2012-07-31 16:43:28 -07003739 if (zone)
3740 setup_zone_pageset(zone);
KAMEZAWA Hiroyukie9959f02010-11-24 12:57:09 -08003741#endif
Cody P Schaferdd1895e2013-07-03 15:01:36 -07003742 /* we have to stop all cpus to guarantee there is no user
3743 of zonelist */
Jiang Liu9adb62a2012-07-31 16:43:28 -07003744 stop_machine(__build_all_zonelists, pgdat, NULL);
Yasunori Goto68113782006-06-23 02:03:11 -07003745 /* cpuset refresh routine should be here */
3746 }
Andrew Mortonbd1e22b2006-06-23 02:03:47 -07003747 vm_total_pages = nr_free_pagecache_pages();
Mel Gorman9ef9acb2007-10-16 01:25:54 -07003748 /*
3749 * Disable grouping by mobility if the number of pages in the
3750 * system is too low to allow the mechanism to work. It would be
3751 * more accurate, but expensive to check per-zone. This check is
3752 * made on memory-hotadd so a system can start with mobility
3753 * disabled and enable it later
3754 */
Mel Gormand9c23402007-10-16 01:26:01 -07003755 if (vm_total_pages < (pageblock_nr_pages * MIGRATE_TYPES))
Mel Gorman9ef9acb2007-10-16 01:25:54 -07003756 page_group_by_mobility_disabled = 1;
3757 else
3758 page_group_by_mobility_disabled = 0;
3759
3760 printk("Built %i zonelists in %s order, mobility grouping %s. "
3761 "Total pages: %ld\n",
Christoph Lameter62bc62a2009-06-16 15:32:15 -07003762 nr_online_nodes,
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003763 zonelist_order_name[current_zonelist_order],
Mel Gorman9ef9acb2007-10-16 01:25:54 -07003764 page_group_by_mobility_disabled ? "off" : "on",
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003765 vm_total_pages);
3766#ifdef CONFIG_NUMA
3767 printk("Policy zone: %s\n", zone_names[policy_zone]);
3768#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769}
3770
3771/*
3772 * Helper functions to size the waitqueue hash table.
3773 * Essentially these want to choose hash table sizes sufficiently
3774 * large so that collisions trying to wait on pages are rare.
3775 * But in fact, the number of active page waitqueues on typical
3776 * systems is ridiculously low, less than 200. So this is even
3777 * conservative, even though it seems large.
3778 *
3779 * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
3780 * waitqueues, i.e. the size of the waitq table given the number of pages.
3781 */
3782#define PAGES_PER_WAITQUEUE 256
3783
Yasunori Gotocca448f2006-06-23 02:03:10 -07003784#ifndef CONFIG_MEMORY_HOTPLUG
Yasunori Goto02b694d2006-06-23 02:03:08 -07003785static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786{
3787 unsigned long size = 1;
3788
3789 pages /= PAGES_PER_WAITQUEUE;
3790
3791 while (size < pages)
3792 size <<= 1;
3793
3794 /*
3795 * Once we have dozens or even hundreds of threads sleeping
3796 * on IO we've got bigger problems than wait queue collision.
3797 * Limit the size of the wait table to a reasonable size.
3798 */
3799 size = min(size, 4096UL);
3800
3801 return max(size, 4UL);
3802}
Yasunori Gotocca448f2006-06-23 02:03:10 -07003803#else
3804/*
3805 * A zone's size might be changed by hot-add, so it is not possible to determine
3806 * a suitable size for its wait_table. So we use the maximum size now.
3807 *
3808 * The max wait table size = 4096 x sizeof(wait_queue_head_t). ie:
3809 *
3810 * i386 (preemption config) : 4096 x 16 = 64Kbyte.
3811 * ia64, x86-64 (no preemption): 4096 x 20 = 80Kbyte.
3812 * ia64, x86-64 (preemption) : 4096 x 24 = 96Kbyte.
3813 *
3814 * The maximum entries are prepared when a zone's memory is (512K + 256) pages
3815 * or more by the traditional way. (See above). It equals:
3816 *
3817 * i386, x86-64, powerpc(4K page size) : = ( 2G + 1M)byte.
3818 * ia64(16K page size) : = ( 8G + 4M)byte.
3819 * powerpc (64K page size) : = (32G +16M)byte.
3820 */
3821static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
3822{
3823 return 4096UL;
3824}
3825#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826
3827/*
3828 * This is an integer logarithm so that shifts can be used later
3829 * to extract the more random high bits from the multiplicative
3830 * hash function before the remainder is taken.
3831 */
3832static inline unsigned long wait_table_bits(unsigned long size)
3833{
3834 return ffz(~size);
3835}
3836
3837#define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
3838
Mel Gorman56fd56b2007-10-16 01:25:58 -07003839/*
Arve Hjønnevåg6d3163c2011-05-24 17:12:24 -07003840 * Check if a pageblock contains reserved pages
3841 */
3842static int pageblock_is_reserved(unsigned long start_pfn, unsigned long end_pfn)
3843{
3844 unsigned long pfn;
3845
3846 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
3847 if (!pfn_valid_within(pfn) || PageReserved(pfn_to_page(pfn)))
3848 return 1;
3849 }
3850 return 0;
3851}
3852
3853/*
Mel Gormand9c23402007-10-16 01:26:01 -07003854 * Mark a number of pageblocks as MIGRATE_RESERVE. The number
Mel Gorman41858962009-06-16 15:32:12 -07003855 * of blocks reserved is based on min_wmark_pages(zone). The memory within
3856 * the reserve will tend to store contiguous free pages. Setting min_free_kbytes
Mel Gorman56fd56b2007-10-16 01:25:58 -07003857 * higher will lead to a bigger reserve which will get freed as contiguous
3858 * blocks as reclaim kicks in
3859 */
3860static void setup_zone_migrate_reserve(struct zone *zone)
3861{
Arve Hjønnevåg6d3163c2011-05-24 17:12:24 -07003862 unsigned long start_pfn, pfn, end_pfn, block_end_pfn;
Mel Gorman56fd56b2007-10-16 01:25:58 -07003863 struct page *page;
Mel Gorman78986a62009-09-21 17:03:02 -07003864 unsigned long block_migratetype;
3865 int reserve;
Mel Gorman56fd56b2007-10-16 01:25:58 -07003866
Michal Hockod02156382011-12-08 14:34:27 -08003867 /*
3868 * Get the start pfn, end pfn and the number of blocks to reserve
3869 * We have to be careful to be aligned to pageblock_nr_pages to
3870 * make sure that we always check pfn_valid for the first page in
3871 * the block.
3872 */
Mel Gorman56fd56b2007-10-16 01:25:58 -07003873 start_pfn = zone->zone_start_pfn;
Cody P Schafer108bcc92013-02-22 16:35:23 -08003874 end_pfn = zone_end_pfn(zone);
Michal Hockod02156382011-12-08 14:34:27 -08003875 start_pfn = roundup(start_pfn, pageblock_nr_pages);
Mel Gorman41858962009-06-16 15:32:12 -07003876 reserve = roundup(min_wmark_pages(zone), pageblock_nr_pages) >>
Mel Gormand9c23402007-10-16 01:26:01 -07003877 pageblock_order;
Mel Gorman56fd56b2007-10-16 01:25:58 -07003878
Mel Gorman78986a62009-09-21 17:03:02 -07003879 /*
3880 * Reserve blocks are generally in place to help high-order atomic
3881 * allocations that are short-lived. A min_free_kbytes value that
3882 * would result in more than 2 reserve blocks for atomic allocations
3883 * is assumed to be in place to help anti-fragmentation for the
3884 * future allocation of hugepages at runtime.
3885 */
3886 reserve = min(2, reserve);
3887
Mel Gormand9c23402007-10-16 01:26:01 -07003888 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
Mel Gorman56fd56b2007-10-16 01:25:58 -07003889 if (!pfn_valid(pfn))
3890 continue;
3891 page = pfn_to_page(pfn);
3892
Adam Litke344c7902008-09-02 14:35:38 -07003893 /* Watch out for overlapping nodes */
3894 if (page_to_nid(page) != zone_to_nid(zone))
3895 continue;
3896
Mel Gorman56fd56b2007-10-16 01:25:58 -07003897 block_migratetype = get_pageblock_migratetype(page);
3898
Mel Gorman938929f2012-01-10 15:07:14 -08003899 /* Only test what is necessary when the reserves are not met */
3900 if (reserve > 0) {
3901 /*
3902 * Blocks with reserved pages will never free, skip
3903 * them.
3904 */
3905 block_end_pfn = min(pfn + pageblock_nr_pages, end_pfn);
3906 if (pageblock_is_reserved(pfn, block_end_pfn))
3907 continue;
Mel Gorman56fd56b2007-10-16 01:25:58 -07003908
Mel Gorman938929f2012-01-10 15:07:14 -08003909 /* If this block is reserved, account for it */
3910 if (block_migratetype == MIGRATE_RESERVE) {
3911 reserve--;
3912 continue;
3913 }
3914
3915 /* Suitable for reserving if this block is movable */
3916 if (block_migratetype == MIGRATE_MOVABLE) {
3917 set_pageblock_migratetype(page,
3918 MIGRATE_RESERVE);
3919 move_freepages_block(zone, page,
3920 MIGRATE_RESERVE);
3921 reserve--;
3922 continue;
3923 }
Mel Gorman56fd56b2007-10-16 01:25:58 -07003924 }
3925
3926 /*
3927 * If the reserve is met and this is a previous reserved block,
3928 * take it back
3929 */
3930 if (block_migratetype == MIGRATE_RESERVE) {
3931 set_pageblock_migratetype(page, MIGRATE_MOVABLE);
3932 move_freepages_block(zone, page, MIGRATE_MOVABLE);
3933 }
3934 }
3935}
Mel Gormanac0e5b72007-10-16 01:25:58 -07003936
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937/*
3938 * Initially all pages are reserved - free ones are freed
3939 * up by free_all_bootmem() once the early boot process is
3940 * done. Non-atomic initialization, single-pass.
3941 */
Matt Tolentinoc09b4242006-01-17 07:03:44 +01003942void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
Dave Hansena2f3aa022007-01-10 23:15:30 -08003943 unsigned long start_pfn, enum memmap_context context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 struct page *page;
Andy Whitcroft29751f62005-06-23 00:08:00 -07003946 unsigned long end_pfn = start_pfn + size;
3947 unsigned long pfn;
KAMEZAWA Hiroyuki86051ca2008-04-29 00:58:21 -07003948 struct zone *z;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949
Hugh Dickins22b31ee2009-01-06 14:40:09 -08003950 if (highest_memmap_pfn < end_pfn - 1)
3951 highest_memmap_pfn = end_pfn - 1;
3952
KAMEZAWA Hiroyuki86051ca2008-04-29 00:58:21 -07003953 z = &NODE_DATA(nid)->node_zones[zone];
Greg Ungerercbe8dd42006-01-12 01:05:24 -08003954 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
Dave Hansena2f3aa022007-01-10 23:15:30 -08003955 /*
3956 * There can be holes in boot-time mem_map[]s
3957 * handed to this function. They do not
3958 * exist on hotplugged memory.
3959 */
3960 if (context == MEMMAP_EARLY) {
3961 if (!early_pfn_valid(pfn))
3962 continue;
3963 if (!early_pfn_in_nid(pfn, nid))
3964 continue;
3965 }
Andy Whitcroftd41dee32005-06-23 00:07:54 -07003966 page = pfn_to_page(pfn);
3967 set_page_links(page, zone, nid, pfn);
Mel Gorman708614e2008-07-23 21:26:51 -07003968 mminit_verify_page_links(page, zone, nid, pfn);
Nick Piggin7835e982006-03-22 00:08:40 -08003969 init_page_count(page);
Mel Gorman22b751c2013-02-22 16:34:59 -08003970 page_mapcount_reset(page);
3971 page_nid_reset_last(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 SetPageReserved(page);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07003973 /*
3974 * Mark the block movable so that blocks are reserved for
3975 * movable at startup. This will force kernel allocations
3976 * to reserve their blocks rather than leaking throughout
3977 * the address space during boot when many long-lived
Mel Gorman56fd56b2007-10-16 01:25:58 -07003978 * kernel allocations are made. Later some blocks near
3979 * the start are marked MIGRATE_RESERVE by
3980 * setup_zone_migrate_reserve()
KAMEZAWA Hiroyuki86051ca2008-04-29 00:58:21 -07003981 *
3982 * bitmap is created for zone's valid pfn range. but memmap
3983 * can be created for invalid pages (for alignment)
3984 * check here not to call set_pageblock_migratetype() against
3985 * pfn out of zone.
Mel Gormanb2a0ac82007-10-16 01:25:48 -07003986 */
KAMEZAWA Hiroyuki86051ca2008-04-29 00:58:21 -07003987 if ((z->zone_start_pfn <= pfn)
Cody P Schafer108bcc92013-02-22 16:35:23 -08003988 && (pfn < zone_end_pfn(z))
KAMEZAWA Hiroyuki86051ca2008-04-29 00:58:21 -07003989 && !(pfn & (pageblock_nr_pages - 1)))
Mel Gorman56fd56b2007-10-16 01:25:58 -07003990 set_pageblock_migratetype(page, MIGRATE_MOVABLE);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07003991
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 INIT_LIST_HEAD(&page->lru);
3993#ifdef WANT_PAGE_VIRTUAL
3994 /* The shift won't overflow because ZONE_NORMAL is below 4G. */
3995 if (!is_highmem_idx(zone))
Bob Picco3212c6b2005-06-27 14:36:28 -07003996 set_page_address(page, __va(pfn << PAGE_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 }
3999}
4000
Andi Kleen1e548de2008-02-04 22:29:26 -08004001static void __meminit zone_init_free_lists(struct zone *zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002{
Mel Gormanb2a0ac82007-10-16 01:25:48 -07004003 int order, t;
4004 for_each_migratetype_order(order, t) {
4005 INIT_LIST_HEAD(&zone->free_area[order].free_list[t]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 zone->free_area[order].nr_free = 0;
4007 }
4008}
4009
4010#ifndef __HAVE_ARCH_MEMMAP_INIT
4011#define memmap_init(size, nid, zone, start_pfn) \
Dave Hansena2f3aa022007-01-10 23:15:30 -08004012 memmap_init_zone((size), (nid), (zone), (start_pfn), MEMMAP_EARLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013#endif
4014
Jiang Liu4ed7e022012-07-31 16:43:35 -07004015static int __meminit zone_batchsize(struct zone *zone)
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07004016{
David Howells3a6be872009-05-06 16:03:03 -07004017#ifdef CONFIG_MMU
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07004018 int batch;
4019
4020 /*
4021 * The per-cpu-pages pools are set to around 1000th of the
Seth, Rohitba56e912005-10-29 18:15:47 -07004022 * size of the zone. But no more than 1/2 of a meg.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07004023 *
4024 * OK, so we don't know how big the cache is. So guess.
4025 */
Jiang Liub40da042013-02-22 16:33:52 -08004026 batch = zone->managed_pages / 1024;
Seth, Rohitba56e912005-10-29 18:15:47 -07004027 if (batch * PAGE_SIZE > 512 * 1024)
4028 batch = (512 * 1024) / PAGE_SIZE;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07004029 batch /= 4; /* We effectively *= 4 below */
4030 if (batch < 1)
4031 batch = 1;
4032
4033 /*
Nick Piggin0ceaacc2005-12-04 13:55:25 +11004034 * Clamp the batch to a 2^n - 1 value. Having a power
4035 * of 2 value was found to be more likely to have
4036 * suboptimal cache aliasing properties in some cases.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07004037 *
Nick Piggin0ceaacc2005-12-04 13:55:25 +11004038 * For example if 2 tasks are alternately allocating
4039 * batches of pages, one task can end up with a lot
4040 * of pages of one half of the possible page colors
4041 * and the other with pages of the other colors.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07004042 */
David Howells91552032009-05-06 16:03:02 -07004043 batch = rounddown_pow_of_two(batch + batch/2) - 1;
Seth, Rohitba56e912005-10-29 18:15:47 -07004044
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07004045 return batch;
David Howells3a6be872009-05-06 16:03:03 -07004046
4047#else
4048 /* The deferral and batching of frees should be suppressed under NOMMU
4049 * conditions.
4050 *
4051 * The problem is that NOMMU needs to be able to allocate large chunks
4052 * of contiguous memory as there's no hardware page translation to
4053 * assemble apparent contiguous memory from discontiguous pages.
4054 *
4055 * Queueing large contiguous runs of pages for batching, however,
4056 * causes the pages to actually be freed in smaller chunks. As there
4057 * can be a significant delay between the individual batches being
4058 * recycled, this leads to the once large chunks of space being
4059 * fragmented and becoming unavailable for high-order allocations.
4060 */
4061 return 0;
4062#endif
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07004063}
4064
Cody P Schafer8d7a8fa2013-07-03 15:01:31 -07004065/*
4066 * pcp->high and pcp->batch values are related and dependent on one another:
4067 * ->batch must never be higher then ->high.
4068 * The following function updates them in a safe manner without read side
4069 * locking.
4070 *
4071 * Any new users of pcp->batch and pcp->high should ensure they can cope with
4072 * those fields changing asynchronously (acording the the above rule).
4073 *
4074 * mutex_is_locked(&pcp_batch_high_lock) required when calling this function
4075 * outside of boot time (or some other assurance that no concurrent updaters
4076 * exist).
4077 */
4078static void pageset_update(struct per_cpu_pages *pcp, unsigned long high,
4079 unsigned long batch)
4080{
4081 /* start with a fail safe value for batch */
4082 pcp->batch = 1;
4083 smp_wmb();
4084
4085 /* Update high, then batch, in order */
4086 pcp->high = high;
4087 smp_wmb();
4088
4089 pcp->batch = batch;
4090}
4091
Cody P Schafer36640332013-07-03 15:01:40 -07004092/* a companion to pageset_set_high() */
Cody P Schafer4008bab2013-07-03 15:01:28 -07004093static void pageset_set_batch(struct per_cpu_pageset *p, unsigned long batch)
4094{
Cody P Schafer8d7a8fa2013-07-03 15:01:31 -07004095 pageset_update(&p->pcp, 6 * batch, max(1UL, 1 * batch));
Cody P Schafer4008bab2013-07-03 15:01:28 -07004096}
4097
Cody P Schafer88c90db2013-07-03 15:01:35 -07004098static void pageset_init(struct per_cpu_pageset *p)
Christoph Lameter2caaad42005-06-21 17:15:00 -07004099{
4100 struct per_cpu_pages *pcp;
Mel Gorman5f8dcc22009-09-21 17:03:19 -07004101 int migratetype;
Christoph Lameter2caaad42005-06-21 17:15:00 -07004102
Magnus Damm1c6fe942005-10-26 01:58:59 -07004103 memset(p, 0, sizeof(*p));
4104
Christoph Lameter3dfa5722008-02-04 22:29:19 -08004105 pcp = &p->pcp;
Christoph Lameter2caaad42005-06-21 17:15:00 -07004106 pcp->count = 0;
Mel Gorman5f8dcc22009-09-21 17:03:19 -07004107 for (migratetype = 0; migratetype < MIGRATE_PCPTYPES; migratetype++)
4108 INIT_LIST_HEAD(&pcp->lists[migratetype]);
Christoph Lameter2caaad42005-06-21 17:15:00 -07004109}
4110
Cody P Schafer88c90db2013-07-03 15:01:35 -07004111static void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
4112{
4113 pageset_init(p);
4114 pageset_set_batch(p, batch);
4115}
4116
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08004117/*
Cody P Schafer36640332013-07-03 15:01:40 -07004118 * pageset_set_high() sets the high water mark for hot per_cpu_pagelist
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08004119 * to the value high for the pageset p.
4120 */
Cody P Schafer36640332013-07-03 15:01:40 -07004121static void pageset_set_high(struct per_cpu_pageset *p,
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08004122 unsigned long high)
4123{
Cody P Schafer8d7a8fa2013-07-03 15:01:31 -07004124 unsigned long batch = max(1UL, high / 4);
4125 if ((high / 4) > (PAGE_SHIFT * 8))
4126 batch = PAGE_SHIFT * 8;
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08004127
Cody P Schafer8d7a8fa2013-07-03 15:01:31 -07004128 pageset_update(&p->pcp, high, batch);
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08004129}
4130
Cody P Schafer169f6c12013-07-03 15:01:41 -07004131static void __meminit pageset_set_high_and_batch(struct zone *zone,
4132 struct per_cpu_pageset *pcp)
Cody P Schafer56cef2b2013-07-03 15:01:38 -07004133{
Cody P Schafer56cef2b2013-07-03 15:01:38 -07004134 if (percpu_pagelist_fraction)
Cody P Schafer36640332013-07-03 15:01:40 -07004135 pageset_set_high(pcp,
Cody P Schafer56cef2b2013-07-03 15:01:38 -07004136 (zone->managed_pages /
4137 percpu_pagelist_fraction));
4138 else
4139 pageset_set_batch(pcp, zone_batchsize(zone));
4140}
4141
Cody P Schafer169f6c12013-07-03 15:01:41 -07004142static void __meminit zone_pageset_init(struct zone *zone, int cpu)
4143{
4144 struct per_cpu_pageset *pcp = per_cpu_ptr(zone->pageset, cpu);
4145
4146 pageset_init(pcp);
4147 pageset_set_high_and_batch(zone, pcp);
4148}
4149
Jiang Liu4ed7e022012-07-31 16:43:35 -07004150static void __meminit setup_zone_pageset(struct zone *zone)
Wu Fengguang319774e2010-05-24 14:32:49 -07004151{
4152 int cpu;
Wu Fengguang319774e2010-05-24 14:32:49 -07004153 zone->pageset = alloc_percpu(struct per_cpu_pageset);
Cody P Schafer56cef2b2013-07-03 15:01:38 -07004154 for_each_possible_cpu(cpu)
4155 zone_pageset_init(zone, cpu);
Wu Fengguang319774e2010-05-24 14:32:49 -07004156}
4157
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07004158/*
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09004159 * Allocate per cpu pagesets and initialize them.
4160 * Before this call only boot pagesets were available.
Christoph Lameter2caaad42005-06-21 17:15:00 -07004161 */
Al Viro78d99552005-12-15 09:18:25 +00004162void __init setup_per_cpu_pageset(void)
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07004163{
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09004164 struct zone *zone;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07004165
Wu Fengguang319774e2010-05-24 14:32:49 -07004166 for_each_populated_zone(zone)
4167 setup_zone_pageset(zone);
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07004168}
4169
Sam Ravnborg577a32f2007-05-17 23:29:25 +02004170static noinline __init_refok
Yasunori Gotocca448f2006-06-23 02:03:10 -07004171int zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages)
Dave Hansened8ece22005-10-29 18:16:50 -07004172{
4173 int i;
4174 struct pglist_data *pgdat = zone->zone_pgdat;
Yasunori Gotocca448f2006-06-23 02:03:10 -07004175 size_t alloc_size;
Dave Hansened8ece22005-10-29 18:16:50 -07004176
4177 /*
4178 * The per-page waitqueue mechanism uses hashed waitqueues
4179 * per zone.
4180 */
Yasunori Goto02b694d2006-06-23 02:03:08 -07004181 zone->wait_table_hash_nr_entries =
4182 wait_table_hash_nr_entries(zone_size_pages);
4183 zone->wait_table_bits =
4184 wait_table_bits(zone->wait_table_hash_nr_entries);
Yasunori Gotocca448f2006-06-23 02:03:10 -07004185 alloc_size = zone->wait_table_hash_nr_entries
4186 * sizeof(wait_queue_head_t);
4187
Heiko Carstenscd94b9d2008-05-23 13:04:52 -07004188 if (!slab_is_available()) {
Yasunori Gotocca448f2006-06-23 02:03:10 -07004189 zone->wait_table = (wait_queue_head_t *)
Yinghai Lu8f389a992011-05-11 15:13:32 -07004190 alloc_bootmem_node_nopanic(pgdat, alloc_size);
Yasunori Gotocca448f2006-06-23 02:03:10 -07004191 } else {
4192 /*
4193 * This case means that a zone whose size was 0 gets new memory
4194 * via memory hot-add.
4195 * But it may be the case that a new node was hot-added. In
4196 * this case vmalloc() will not be able to use this new node's
4197 * memory - this wait_table must be initialized to use this new
4198 * node itself as well.
4199 * To use this new node's memory, further consideration will be
4200 * necessary.
4201 */
Jesper Juhl8691f3a2007-10-16 01:24:49 -07004202 zone->wait_table = vmalloc(alloc_size);
Yasunori Gotocca448f2006-06-23 02:03:10 -07004203 }
4204 if (!zone->wait_table)
4205 return -ENOMEM;
Dave Hansened8ece22005-10-29 18:16:50 -07004206
Pintu Kumarb8af2942013-09-11 14:20:34 -07004207 for (i = 0; i < zone->wait_table_hash_nr_entries; ++i)
Dave Hansened8ece22005-10-29 18:16:50 -07004208 init_waitqueue_head(zone->wait_table + i);
Yasunori Gotocca448f2006-06-23 02:03:10 -07004209
4210 return 0;
Dave Hansened8ece22005-10-29 18:16:50 -07004211}
4212
Matt Tolentinoc09b4242006-01-17 07:03:44 +01004213static __meminit void zone_pcp_init(struct zone *zone)
Dave Hansened8ece22005-10-29 18:16:50 -07004214{
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09004215 /*
4216 * per cpu subsystem is not up at this point. The following code
4217 * relies on the ability of the linker to provide the
4218 * offset of a (static) per cpu variable into the per cpu area.
4219 */
4220 zone->pageset = &boot_pageset;
Dave Hansened8ece22005-10-29 18:16:50 -07004221
Anton Blanchardf5335c02006-03-25 03:06:49 -08004222 if (zone->present_pages)
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09004223 printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%u\n",
4224 zone->name, zone->present_pages,
4225 zone_batchsize(zone));
Dave Hansened8ece22005-10-29 18:16:50 -07004226}
4227
Jiang Liu4ed7e022012-07-31 16:43:35 -07004228int __meminit init_currently_empty_zone(struct zone *zone,
Yasunori Goto718127c2006-06-23 02:03:10 -07004229 unsigned long zone_start_pfn,
Dave Hansena2f3aa022007-01-10 23:15:30 -08004230 unsigned long size,
4231 enum memmap_context context)
Dave Hansened8ece22005-10-29 18:16:50 -07004232{
4233 struct pglist_data *pgdat = zone->zone_pgdat;
Yasunori Gotocca448f2006-06-23 02:03:10 -07004234 int ret;
4235 ret = zone_wait_table_init(zone, size);
4236 if (ret)
4237 return ret;
Dave Hansened8ece22005-10-29 18:16:50 -07004238 pgdat->nr_zones = zone_idx(zone) + 1;
4239
Dave Hansened8ece22005-10-29 18:16:50 -07004240 zone->zone_start_pfn = zone_start_pfn;
4241
Mel Gorman708614e2008-07-23 21:26:51 -07004242 mminit_dprintk(MMINIT_TRACE, "memmap_init",
4243 "Initialising map node %d zone %lu pfns %lu -> %lu\n",
4244 pgdat->node_id,
4245 (unsigned long)zone_idx(zone),
4246 zone_start_pfn, (zone_start_pfn + size));
4247
Andi Kleen1e548de2008-02-04 22:29:26 -08004248 zone_init_free_lists(zone);
Yasunori Goto718127c2006-06-23 02:03:10 -07004249
4250 return 0;
Dave Hansened8ece22005-10-29 18:16:50 -07004251}
4252
Tejun Heo0ee332c2011-12-08 10:22:09 -08004253#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Mel Gormanc7132162006-09-27 01:49:43 -07004254#ifndef CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID
4255/*
4256 * Required by SPARSEMEM. Given a PFN, return what node the PFN is on.
4257 * Architectures may implement their own version but if add_active_range()
4258 * was used and there are no special requirements, this is a convenient
4259 * alternative
4260 */
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -08004261int __meminit __early_pfn_to_nid(unsigned long pfn)
Mel Gormanc7132162006-09-27 01:49:43 -07004262{
Tejun Heoc13291a2011-07-12 10:46:30 +02004263 unsigned long start_pfn, end_pfn;
4264 int i, nid;
Russ Anderson7c243c72013-04-29 15:07:59 -07004265 /*
4266 * NOTE: The following SMP-unsafe globals are only used early in boot
4267 * when the kernel is running single-threaded.
4268 */
4269 static unsigned long __meminitdata last_start_pfn, last_end_pfn;
4270 static int __meminitdata last_nid;
4271
4272 if (last_start_pfn <= pfn && pfn < last_end_pfn)
4273 return last_nid;
Mel Gormanc7132162006-09-27 01:49:43 -07004274
Tejun Heoc13291a2011-07-12 10:46:30 +02004275 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid)
Russ Anderson7c243c72013-04-29 15:07:59 -07004276 if (start_pfn <= pfn && pfn < end_pfn) {
4277 last_start_pfn = start_pfn;
4278 last_end_pfn = end_pfn;
4279 last_nid = nid;
Tejun Heoc13291a2011-07-12 10:46:30 +02004280 return nid;
Russ Anderson7c243c72013-04-29 15:07:59 -07004281 }
KAMEZAWA Hiroyukicc2559b2009-02-18 14:48:33 -08004282 /* This is a memory hole */
4283 return -1;
Mel Gormanc7132162006-09-27 01:49:43 -07004284}
4285#endif /* CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID */
4286
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -08004287int __meminit early_pfn_to_nid(unsigned long pfn)
4288{
KAMEZAWA Hiroyukicc2559b2009-02-18 14:48:33 -08004289 int nid;
4290
4291 nid = __early_pfn_to_nid(pfn);
4292 if (nid >= 0)
4293 return nid;
4294 /* just returns 0 */
4295 return 0;
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -08004296}
4297
KAMEZAWA Hiroyukicc2559b2009-02-18 14:48:33 -08004298#ifdef CONFIG_NODES_SPAN_OTHER_NODES
4299bool __meminit early_pfn_in_nid(unsigned long pfn, int node)
4300{
4301 int nid;
4302
4303 nid = __early_pfn_to_nid(pfn);
4304 if (nid >= 0 && nid != node)
4305 return false;
4306 return true;
4307}
4308#endif
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -08004309
Mel Gormanc7132162006-09-27 01:49:43 -07004310/**
4311 * free_bootmem_with_active_regions - Call free_bootmem_node for each active range
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004312 * @nid: The node to free memory on. If MAX_NUMNODES, all nodes are freed.
4313 * @max_low_pfn: The highest PFN that will be passed to free_bootmem_node
Mel Gormanc7132162006-09-27 01:49:43 -07004314 *
4315 * If an architecture guarantees that all ranges registered with
4316 * add_active_ranges() contain no holes and may be freed, this
4317 * this function may be used instead of calling free_bootmem() manually.
4318 */
Tejun Heoc13291a2011-07-12 10:46:30 +02004319void __init free_bootmem_with_active_regions(int nid, unsigned long max_low_pfn)
Mel Gormanc7132162006-09-27 01:49:43 -07004320{
Tejun Heoc13291a2011-07-12 10:46:30 +02004321 unsigned long start_pfn, end_pfn;
4322 int i, this_nid;
Mel Gormanc7132162006-09-27 01:49:43 -07004323
Tejun Heoc13291a2011-07-12 10:46:30 +02004324 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, &this_nid) {
4325 start_pfn = min(start_pfn, max_low_pfn);
4326 end_pfn = min(end_pfn, max_low_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07004327
Tejun Heoc13291a2011-07-12 10:46:30 +02004328 if (start_pfn < end_pfn)
4329 free_bootmem_node(NODE_DATA(this_nid),
4330 PFN_PHYS(start_pfn),
4331 (end_pfn - start_pfn) << PAGE_SHIFT);
Mel Gormanc7132162006-09-27 01:49:43 -07004332 }
4333}
4334
4335/**
4336 * sparse_memory_present_with_active_regions - Call memory_present for each active range
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004337 * @nid: The node to call memory_present for. If MAX_NUMNODES, all nodes will be used.
Mel Gormanc7132162006-09-27 01:49:43 -07004338 *
4339 * If an architecture guarantees that all ranges registered with
4340 * add_active_ranges() contain no holes and may be freed, this
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004341 * function may be used instead of calling memory_present() manually.
Mel Gormanc7132162006-09-27 01:49:43 -07004342 */
4343void __init sparse_memory_present_with_active_regions(int nid)
4344{
Tejun Heoc13291a2011-07-12 10:46:30 +02004345 unsigned long start_pfn, end_pfn;
4346 int i, this_nid;
Mel Gormanc7132162006-09-27 01:49:43 -07004347
Tejun Heoc13291a2011-07-12 10:46:30 +02004348 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, &this_nid)
4349 memory_present(this_nid, start_pfn, end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07004350}
4351
4352/**
4353 * get_pfn_range_for_nid - Return the start and end page frames for a node
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004354 * @nid: The nid to return the range for. If MAX_NUMNODES, the min and max PFN are returned.
4355 * @start_pfn: Passed by reference. On return, it will have the node start_pfn.
4356 * @end_pfn: Passed by reference. On return, it will have the node end_pfn.
Mel Gormanc7132162006-09-27 01:49:43 -07004357 *
4358 * It returns the start and end page frame of a node based on information
4359 * provided by an arch calling add_active_range(). If called for a node
4360 * with no available memory, a warning is printed and the start and end
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004361 * PFNs will be 0.
Mel Gormanc7132162006-09-27 01:49:43 -07004362 */
Yasunori Gotoa3142c82007-05-08 00:23:07 -07004363void __meminit get_pfn_range_for_nid(unsigned int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07004364 unsigned long *start_pfn, unsigned long *end_pfn)
4365{
Tejun Heoc13291a2011-07-12 10:46:30 +02004366 unsigned long this_start_pfn, this_end_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07004367 int i;
Tejun Heoc13291a2011-07-12 10:46:30 +02004368
Mel Gormanc7132162006-09-27 01:49:43 -07004369 *start_pfn = -1UL;
4370 *end_pfn = 0;
4371
Tejun Heoc13291a2011-07-12 10:46:30 +02004372 for_each_mem_pfn_range(i, nid, &this_start_pfn, &this_end_pfn, NULL) {
4373 *start_pfn = min(*start_pfn, this_start_pfn);
4374 *end_pfn = max(*end_pfn, this_end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07004375 }
4376
Christoph Lameter633c0662007-10-16 01:25:37 -07004377 if (*start_pfn == -1UL)
Mel Gormanc7132162006-09-27 01:49:43 -07004378 *start_pfn = 0;
Mel Gormanc7132162006-09-27 01:49:43 -07004379}
4380
4381/*
Mel Gorman2a1e2742007-07-17 04:03:12 -07004382 * This finds a zone that can be used for ZONE_MOVABLE pages. The
4383 * assumption is made that zones within a node are ordered in monotonic
4384 * increasing memory addresses so that the "highest" populated zone is used
4385 */
Adrian Bunkb69a7282008-07-23 21:28:12 -07004386static void __init find_usable_zone_for_movable(void)
Mel Gorman2a1e2742007-07-17 04:03:12 -07004387{
4388 int zone_index;
4389 for (zone_index = MAX_NR_ZONES - 1; zone_index >= 0; zone_index--) {
4390 if (zone_index == ZONE_MOVABLE)
4391 continue;
4392
4393 if (arch_zone_highest_possible_pfn[zone_index] >
4394 arch_zone_lowest_possible_pfn[zone_index])
4395 break;
4396 }
4397
4398 VM_BUG_ON(zone_index == -1);
4399 movable_zone = zone_index;
4400}
4401
4402/*
4403 * The zone ranges provided by the architecture do not include ZONE_MOVABLE
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004404 * because it is sized independent of architecture. Unlike the other zones,
Mel Gorman2a1e2742007-07-17 04:03:12 -07004405 * the starting point for ZONE_MOVABLE is not fixed. It may be different
4406 * in each node depending on the size of each node and how evenly kernelcore
4407 * is distributed. This helper function adjusts the zone ranges
4408 * provided by the architecture for a given node by using the end of the
4409 * highest usable zone for ZONE_MOVABLE. This preserves the assumption that
4410 * zones within a node are in order of monotonic increases memory addresses
4411 */
Adrian Bunkb69a7282008-07-23 21:28:12 -07004412static void __meminit adjust_zone_range_for_zone_movable(int nid,
Mel Gorman2a1e2742007-07-17 04:03:12 -07004413 unsigned long zone_type,
4414 unsigned long node_start_pfn,
4415 unsigned long node_end_pfn,
4416 unsigned long *zone_start_pfn,
4417 unsigned long *zone_end_pfn)
4418{
4419 /* Only adjust if ZONE_MOVABLE is on this node */
4420 if (zone_movable_pfn[nid]) {
4421 /* Size ZONE_MOVABLE */
4422 if (zone_type == ZONE_MOVABLE) {
4423 *zone_start_pfn = zone_movable_pfn[nid];
4424 *zone_end_pfn = min(node_end_pfn,
4425 arch_zone_highest_possible_pfn[movable_zone]);
4426
4427 /* Adjust for ZONE_MOVABLE starting within this range */
4428 } else if (*zone_start_pfn < zone_movable_pfn[nid] &&
4429 *zone_end_pfn > zone_movable_pfn[nid]) {
4430 *zone_end_pfn = zone_movable_pfn[nid];
4431
4432 /* Check if this whole range is within ZONE_MOVABLE */
4433 } else if (*zone_start_pfn >= zone_movable_pfn[nid])
4434 *zone_start_pfn = *zone_end_pfn;
4435 }
4436}
4437
4438/*
Mel Gormanc7132162006-09-27 01:49:43 -07004439 * Return the number of pages a zone spans in a node, including holes
4440 * present_pages = zone_spanned_pages_in_node() - zone_absent_pages_in_node()
4441 */
Paul Mundt6ea6e682007-07-15 23:38:20 -07004442static unsigned long __meminit zone_spanned_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07004443 unsigned long zone_type,
Zhang Yanfei7960aed2013-07-08 15:59:52 -07004444 unsigned long node_start_pfn,
4445 unsigned long node_end_pfn,
Mel Gormanc7132162006-09-27 01:49:43 -07004446 unsigned long *ignored)
4447{
Mel Gormanc7132162006-09-27 01:49:43 -07004448 unsigned long zone_start_pfn, zone_end_pfn;
4449
Zhang Yanfei7960aed2013-07-08 15:59:52 -07004450 /* Get the start and end of the zone */
Mel Gormanc7132162006-09-27 01:49:43 -07004451 zone_start_pfn = arch_zone_lowest_possible_pfn[zone_type];
4452 zone_end_pfn = arch_zone_highest_possible_pfn[zone_type];
Mel Gorman2a1e2742007-07-17 04:03:12 -07004453 adjust_zone_range_for_zone_movable(nid, zone_type,
4454 node_start_pfn, node_end_pfn,
4455 &zone_start_pfn, &zone_end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07004456
4457 /* Check that this node has pages within the zone's required range */
4458 if (zone_end_pfn < node_start_pfn || zone_start_pfn > node_end_pfn)
4459 return 0;
4460
4461 /* Move the zone boundaries inside the node if necessary */
4462 zone_end_pfn = min(zone_end_pfn, node_end_pfn);
4463 zone_start_pfn = max(zone_start_pfn, node_start_pfn);
4464
4465 /* Return the spanned pages */
4466 return zone_end_pfn - zone_start_pfn;
4467}
4468
4469/*
4470 * Return the number of holes in a range on a node. If nid is MAX_NUMNODES,
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004471 * then all holes in the requested range will be accounted for.
Mel Gormanc7132162006-09-27 01:49:43 -07004472 */
Yinghai Lu32996252009-12-15 17:59:02 -08004473unsigned long __meminit __absent_pages_in_range(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07004474 unsigned long range_start_pfn,
4475 unsigned long range_end_pfn)
4476{
Tejun Heo96e907d2011-07-12 10:46:29 +02004477 unsigned long nr_absent = range_end_pfn - range_start_pfn;
4478 unsigned long start_pfn, end_pfn;
4479 int i;
Mel Gormanc7132162006-09-27 01:49:43 -07004480
Tejun Heo96e907d2011-07-12 10:46:29 +02004481 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
4482 start_pfn = clamp(start_pfn, range_start_pfn, range_end_pfn);
4483 end_pfn = clamp(end_pfn, range_start_pfn, range_end_pfn);
4484 nr_absent -= end_pfn - start_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07004485 }
Tejun Heo96e907d2011-07-12 10:46:29 +02004486 return nr_absent;
Mel Gormanc7132162006-09-27 01:49:43 -07004487}
4488
4489/**
4490 * absent_pages_in_range - Return number of page frames in holes within a range
4491 * @start_pfn: The start PFN to start searching for holes
4492 * @end_pfn: The end PFN to stop searching for holes
4493 *
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004494 * It returns the number of pages frames in memory holes within a range.
Mel Gormanc7132162006-09-27 01:49:43 -07004495 */
4496unsigned long __init absent_pages_in_range(unsigned long start_pfn,
4497 unsigned long end_pfn)
4498{
4499 return __absent_pages_in_range(MAX_NUMNODES, start_pfn, end_pfn);
4500}
4501
4502/* Return the number of page frames in holes in a zone on a node */
Paul Mundt6ea6e682007-07-15 23:38:20 -07004503static unsigned long __meminit zone_absent_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07004504 unsigned long zone_type,
Zhang Yanfei7960aed2013-07-08 15:59:52 -07004505 unsigned long node_start_pfn,
4506 unsigned long node_end_pfn,
Mel Gormanc7132162006-09-27 01:49:43 -07004507 unsigned long *ignored)
4508{
Tejun Heo96e907d2011-07-12 10:46:29 +02004509 unsigned long zone_low = arch_zone_lowest_possible_pfn[zone_type];
4510 unsigned long zone_high = arch_zone_highest_possible_pfn[zone_type];
Mel Gorman9c7cd682006-09-27 01:49:58 -07004511 unsigned long zone_start_pfn, zone_end_pfn;
4512
Tejun Heo96e907d2011-07-12 10:46:29 +02004513 zone_start_pfn = clamp(node_start_pfn, zone_low, zone_high);
4514 zone_end_pfn = clamp(node_end_pfn, zone_low, zone_high);
Mel Gorman9c7cd682006-09-27 01:49:58 -07004515
Mel Gorman2a1e2742007-07-17 04:03:12 -07004516 adjust_zone_range_for_zone_movable(nid, zone_type,
4517 node_start_pfn, node_end_pfn,
4518 &zone_start_pfn, &zone_end_pfn);
Mel Gorman9c7cd682006-09-27 01:49:58 -07004519 return __absent_pages_in_range(nid, zone_start_pfn, zone_end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07004520}
Mel Gorman0e0b8642006-09-27 01:49:56 -07004521
Tejun Heo0ee332c2011-12-08 10:22:09 -08004522#else /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
Paul Mundt6ea6e682007-07-15 23:38:20 -07004523static inline unsigned long __meminit zone_spanned_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07004524 unsigned long zone_type,
Zhang Yanfei7960aed2013-07-08 15:59:52 -07004525 unsigned long node_start_pfn,
4526 unsigned long node_end_pfn,
Mel Gormanc7132162006-09-27 01:49:43 -07004527 unsigned long *zones_size)
4528{
4529 return zones_size[zone_type];
4530}
4531
Paul Mundt6ea6e682007-07-15 23:38:20 -07004532static inline unsigned long __meminit zone_absent_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07004533 unsigned long zone_type,
Zhang Yanfei7960aed2013-07-08 15:59:52 -07004534 unsigned long node_start_pfn,
4535 unsigned long node_end_pfn,
Mel Gormanc7132162006-09-27 01:49:43 -07004536 unsigned long *zholes_size)
4537{
4538 if (!zholes_size)
4539 return 0;
4540
4541 return zholes_size[zone_type];
4542}
Yinghai Lu20e69262013-03-01 14:51:27 -08004543
Tejun Heo0ee332c2011-12-08 10:22:09 -08004544#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
Mel Gormanc7132162006-09-27 01:49:43 -07004545
Yasunori Gotoa3142c82007-05-08 00:23:07 -07004546static void __meminit calculate_node_totalpages(struct pglist_data *pgdat,
Zhang Yanfei7960aed2013-07-08 15:59:52 -07004547 unsigned long node_start_pfn,
4548 unsigned long node_end_pfn,
4549 unsigned long *zones_size,
4550 unsigned long *zholes_size)
Mel Gormanc7132162006-09-27 01:49:43 -07004551{
4552 unsigned long realtotalpages, totalpages = 0;
4553 enum zone_type i;
4554
4555 for (i = 0; i < MAX_NR_ZONES; i++)
4556 totalpages += zone_spanned_pages_in_node(pgdat->node_id, i,
Zhang Yanfei7960aed2013-07-08 15:59:52 -07004557 node_start_pfn,
4558 node_end_pfn,
4559 zones_size);
Mel Gormanc7132162006-09-27 01:49:43 -07004560 pgdat->node_spanned_pages = totalpages;
4561
4562 realtotalpages = totalpages;
4563 for (i = 0; i < MAX_NR_ZONES; i++)
4564 realtotalpages -=
4565 zone_absent_pages_in_node(pgdat->node_id, i,
Zhang Yanfei7960aed2013-07-08 15:59:52 -07004566 node_start_pfn, node_end_pfn,
4567 zholes_size);
Mel Gormanc7132162006-09-27 01:49:43 -07004568 pgdat->node_present_pages = realtotalpages;
4569 printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id,
4570 realtotalpages);
4571}
4572
Mel Gorman835c1342007-10-16 01:25:47 -07004573#ifndef CONFIG_SPARSEMEM
4574/*
4575 * Calculate the size of the zone->blockflags rounded to an unsigned long
Mel Gormand9c23402007-10-16 01:26:01 -07004576 * Start by making sure zonesize is a multiple of pageblock_order by rounding
4577 * up. Then use 1 NR_PAGEBLOCK_BITS worth of bits per pageblock, finally
Mel Gorman835c1342007-10-16 01:25:47 -07004578 * round what is now in bits to nearest long in bits, then return it in
4579 * bytes.
4580 */
Linus Torvalds7c455122013-02-18 09:58:02 -08004581static unsigned long __init usemap_size(unsigned long zone_start_pfn, unsigned long zonesize)
Mel Gorman835c1342007-10-16 01:25:47 -07004582{
4583 unsigned long usemapsize;
4584
Linus Torvalds7c455122013-02-18 09:58:02 -08004585 zonesize += zone_start_pfn & (pageblock_nr_pages-1);
Mel Gormand9c23402007-10-16 01:26:01 -07004586 usemapsize = roundup(zonesize, pageblock_nr_pages);
4587 usemapsize = usemapsize >> pageblock_order;
Mel Gorman835c1342007-10-16 01:25:47 -07004588 usemapsize *= NR_PAGEBLOCK_BITS;
4589 usemapsize = roundup(usemapsize, 8 * sizeof(unsigned long));
4590
4591 return usemapsize / 8;
4592}
4593
4594static void __init setup_usemap(struct pglist_data *pgdat,
Linus Torvalds7c455122013-02-18 09:58:02 -08004595 struct zone *zone,
4596 unsigned long zone_start_pfn,
4597 unsigned long zonesize)
Mel Gorman835c1342007-10-16 01:25:47 -07004598{
Linus Torvalds7c455122013-02-18 09:58:02 -08004599 unsigned long usemapsize = usemap_size(zone_start_pfn, zonesize);
Mel Gorman835c1342007-10-16 01:25:47 -07004600 zone->pageblock_flags = NULL;
Julia Lawall58a01a42009-01-06 14:39:28 -08004601 if (usemapsize)
Yinghai Lu8f389a992011-05-11 15:13:32 -07004602 zone->pageblock_flags = alloc_bootmem_node_nopanic(pgdat,
4603 usemapsize);
Mel Gorman835c1342007-10-16 01:25:47 -07004604}
4605#else
Linus Torvalds7c455122013-02-18 09:58:02 -08004606static inline void setup_usemap(struct pglist_data *pgdat, struct zone *zone,
4607 unsigned long zone_start_pfn, unsigned long zonesize) {}
Mel Gorman835c1342007-10-16 01:25:47 -07004608#endif /* CONFIG_SPARSEMEM */
4609
Mel Gormand9c23402007-10-16 01:26:01 -07004610#ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
Mel Gormanba72cb82007-11-28 16:21:13 -08004611
Mel Gormand9c23402007-10-16 01:26:01 -07004612/* Initialise the number of pages represented by NR_PAGEBLOCK_BITS */
Chen Gang15ca2202013-09-11 14:20:27 -07004613void __paginginit set_pageblock_order(void)
Mel Gormand9c23402007-10-16 01:26:01 -07004614{
Andrew Morton955c1cd2012-05-29 15:06:31 -07004615 unsigned int order;
4616
Mel Gormand9c23402007-10-16 01:26:01 -07004617 /* Check that pageblock_nr_pages has not already been setup */
4618 if (pageblock_order)
4619 return;
4620
Andrew Morton955c1cd2012-05-29 15:06:31 -07004621 if (HPAGE_SHIFT > PAGE_SHIFT)
4622 order = HUGETLB_PAGE_ORDER;
4623 else
4624 order = MAX_ORDER - 1;
4625
Mel Gormand9c23402007-10-16 01:26:01 -07004626 /*
4627 * Assume the largest contiguous order of interest is a huge page.
Andrew Morton955c1cd2012-05-29 15:06:31 -07004628 * This value may be variable depending on boot parameters on IA64 and
4629 * powerpc.
Mel Gormand9c23402007-10-16 01:26:01 -07004630 */
4631 pageblock_order = order;
4632}
4633#else /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
4634
Mel Gormanba72cb82007-11-28 16:21:13 -08004635/*
4636 * When CONFIG_HUGETLB_PAGE_SIZE_VARIABLE is not set, set_pageblock_order()
Andrew Morton955c1cd2012-05-29 15:06:31 -07004637 * is unused as pageblock_order is set at compile-time. See
4638 * include/linux/pageblock-flags.h for the values of pageblock_order based on
4639 * the kernel config
Mel Gormanba72cb82007-11-28 16:21:13 -08004640 */
Chen Gang15ca2202013-09-11 14:20:27 -07004641void __paginginit set_pageblock_order(void)
Mel Gormanba72cb82007-11-28 16:21:13 -08004642{
Mel Gormanba72cb82007-11-28 16:21:13 -08004643}
Mel Gormand9c23402007-10-16 01:26:01 -07004644
4645#endif /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
4646
Jiang Liu01cefae2012-12-12 13:52:19 -08004647static unsigned long __paginginit calc_memmap_size(unsigned long spanned_pages,
4648 unsigned long present_pages)
4649{
4650 unsigned long pages = spanned_pages;
4651
4652 /*
4653 * Provide a more accurate estimation if there are holes within
4654 * the zone and SPARSEMEM is in use. If there are holes within the
4655 * zone, each populated memory region may cost us one or two extra
4656 * memmap pages due to alignment because memmap pages for each
4657 * populated regions may not naturally algined on page boundary.
4658 * So the (present_pages >> 4) heuristic is a tradeoff for that.
4659 */
4660 if (spanned_pages > present_pages + (present_pages >> 4) &&
4661 IS_ENABLED(CONFIG_SPARSEMEM))
4662 pages = present_pages;
4663
4664 return PAGE_ALIGN(pages * sizeof(struct page)) >> PAGE_SHIFT;
4665}
4666
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667/*
4668 * Set up the zone data structures:
4669 * - mark all pages reserved
4670 * - mark all memory queues empty
4671 * - clear the memory bitmaps
Minchan Kim6527af52012-07-31 16:46:16 -07004672 *
4673 * NOTE: pgdat should get zeroed by caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 */
Alexander van Heukelumb5a0e012008-02-23 15:24:06 -08004675static void __paginginit free_area_init_core(struct pglist_data *pgdat,
Zhang Yanfei7960aed2013-07-08 15:59:52 -07004676 unsigned long node_start_pfn, unsigned long node_end_pfn,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 unsigned long *zones_size, unsigned long *zholes_size)
4678{
Christoph Lameter2f1b6242006-09-25 23:31:13 -07004679 enum zone_type j;
Dave Hansened8ece22005-10-29 18:16:50 -07004680 int nid = pgdat->node_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 unsigned long zone_start_pfn = pgdat->node_start_pfn;
Yasunori Goto718127c2006-06-23 02:03:10 -07004682 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683
Dave Hansen208d54e2005-10-29 18:16:52 -07004684 pgdat_resize_init(pgdat);
Andrea Arcangeli8177a422012-03-23 20:56:34 +01004685#ifdef CONFIG_NUMA_BALANCING
4686 spin_lock_init(&pgdat->numabalancing_migrate_lock);
4687 pgdat->numabalancing_migrate_nr_pages = 0;
4688 pgdat->numabalancing_migrate_next_window = jiffies;
4689#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 init_waitqueue_head(&pgdat->kswapd_wait);
Mel Gorman55150612012-07-31 16:44:35 -07004691 init_waitqueue_head(&pgdat->pfmemalloc_wait);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004692 pgdat_page_cgroup_init(pgdat);
Michal Nazarewicz5f63b722012-01-11 15:16:11 +01004693
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 for (j = 0; j < MAX_NR_ZONES; j++) {
4695 struct zone *zone = pgdat->node_zones + j;
Jiang Liu9feedc92012-12-12 13:52:12 -08004696 unsigned long size, realsize, freesize, memmap_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697
Zhang Yanfei7960aed2013-07-08 15:59:52 -07004698 size = zone_spanned_pages_in_node(nid, j, node_start_pfn,
4699 node_end_pfn, zones_size);
Jiang Liu9feedc92012-12-12 13:52:12 -08004700 realsize = freesize = size - zone_absent_pages_in_node(nid, j,
Zhang Yanfei7960aed2013-07-08 15:59:52 -07004701 node_start_pfn,
4702 node_end_pfn,
Mel Gormanc7132162006-09-27 01:49:43 -07004703 zholes_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704
Mel Gorman0e0b8642006-09-27 01:49:56 -07004705 /*
Jiang Liu9feedc92012-12-12 13:52:12 -08004706 * Adjust freesize so that it accounts for how much memory
Mel Gorman0e0b8642006-09-27 01:49:56 -07004707 * is used by this zone for memmap. This affects the watermark
4708 * and per-cpu initialisations
4709 */
Jiang Liu01cefae2012-12-12 13:52:19 -08004710 memmap_pages = calc_memmap_size(size, realsize);
Jiang Liu9feedc92012-12-12 13:52:12 -08004711 if (freesize >= memmap_pages) {
4712 freesize -= memmap_pages;
Yinghai Lu5594c8c2009-01-06 14:39:14 -08004713 if (memmap_pages)
4714 printk(KERN_DEBUG
4715 " %s zone: %lu pages used for memmap\n",
4716 zone_names[j], memmap_pages);
Mel Gorman0e0b8642006-09-27 01:49:56 -07004717 } else
4718 printk(KERN_WARNING
Jiang Liu9feedc92012-12-12 13:52:12 -08004719 " %s zone: %lu pages exceeds freesize %lu\n",
4720 zone_names[j], memmap_pages, freesize);
Mel Gorman0e0b8642006-09-27 01:49:56 -07004721
Christoph Lameter62672762007-02-10 01:43:07 -08004722 /* Account for reserved pages */
Jiang Liu9feedc92012-12-12 13:52:12 -08004723 if (j == 0 && freesize > dma_reserve) {
4724 freesize -= dma_reserve;
Yinghai Lud903ef92008-10-18 20:27:06 -07004725 printk(KERN_DEBUG " %s zone: %lu pages reserved\n",
Christoph Lameter62672762007-02-10 01:43:07 -08004726 zone_names[0], dma_reserve);
Mel Gorman0e0b8642006-09-27 01:49:56 -07004727 }
4728
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07004729 if (!is_highmem_idx(j))
Jiang Liu9feedc92012-12-12 13:52:12 -08004730 nr_kernel_pages += freesize;
Jiang Liu01cefae2012-12-12 13:52:19 -08004731 /* Charge for highmem memmap if there are enough kernel pages */
4732 else if (nr_kernel_pages > memmap_pages * 2)
4733 nr_kernel_pages -= memmap_pages;
Jiang Liu9feedc92012-12-12 13:52:12 -08004734 nr_all_pages += freesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735
4736 zone->spanned_pages = size;
Jiang Liu306f2e92013-02-22 16:33:54 -08004737 zone->present_pages = realsize;
Jiang Liu9feedc92012-12-12 13:52:12 -08004738 /*
4739 * Set an approximate value for lowmem here, it will be adjusted
4740 * when the bootmem allocator frees pages into the buddy system.
4741 * And all highmem pages will be managed by the buddy system.
4742 */
4743 zone->managed_pages = is_highmem_idx(j) ? realsize : freesize;
Christoph Lameter96146342006-07-03 00:24:13 -07004744#ifdef CONFIG_NUMA
Christoph Lameterd5f541e2006-09-27 01:50:08 -07004745 zone->node = nid;
Jiang Liu9feedc92012-12-12 13:52:12 -08004746 zone->min_unmapped_pages = (freesize*sysctl_min_unmapped_ratio)
Christoph Lameter96146342006-07-03 00:24:13 -07004747 / 100;
Jiang Liu9feedc92012-12-12 13:52:12 -08004748 zone->min_slab_pages = (freesize * sysctl_min_slab_ratio) / 100;
Christoph Lameter96146342006-07-03 00:24:13 -07004749#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 zone->name = zone_names[j];
4751 spin_lock_init(&zone->lock);
4752 spin_lock_init(&zone->lru_lock);
Dave Hansenbdc8cb92005-10-29 18:16:53 -07004753 zone_seqlock_init(zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 zone->zone_pgdat = pgdat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755
Dave Hansened8ece22005-10-29 18:16:50 -07004756 zone_pcp_init(zone);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004757 lruvec_init(&zone->lruvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 if (!size)
4759 continue;
4760
Andrew Morton955c1cd2012-05-29 15:06:31 -07004761 set_pageblock_order();
Linus Torvalds7c455122013-02-18 09:58:02 -08004762 setup_usemap(pgdat, zone, zone_start_pfn, size);
Dave Hansena2f3aa022007-01-10 23:15:30 -08004763 ret = init_currently_empty_zone(zone, zone_start_pfn,
4764 size, MEMMAP_EARLY);
Yasunori Goto718127c2006-06-23 02:03:10 -07004765 BUG_ON(ret);
Heiko Carstens76cdd582008-05-14 16:05:52 -07004766 memmap_init(size, nid, j, zone_start_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 zone_start_pfn += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 }
4769}
4770
Sam Ravnborg577a32f2007-05-17 23:29:25 +02004771static void __init_refok alloc_node_mem_map(struct pglist_data *pgdat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 /* Skip empty nodes */
4774 if (!pgdat->node_spanned_pages)
4775 return;
4776
Andy Whitcroftd41dee32005-06-23 00:07:54 -07004777#ifdef CONFIG_FLAT_NODE_MEM_MAP
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 /* ia64 gets its own node_mem_map, before this, without bootmem */
4779 if (!pgdat->node_mem_map) {
Bob Piccoe984bb42006-05-20 15:00:31 -07004780 unsigned long size, start, end;
Andy Whitcroftd41dee32005-06-23 00:07:54 -07004781 struct page *map;
4782
Bob Piccoe984bb42006-05-20 15:00:31 -07004783 /*
4784 * The zone's endpoints aren't required to be MAX_ORDER
4785 * aligned but the node_mem_map endpoints must be in order
4786 * for the buddy allocator to function correctly.
4787 */
4788 start = pgdat->node_start_pfn & ~(MAX_ORDER_NR_PAGES - 1);
Cody P Schafer108bcc92013-02-22 16:35:23 -08004789 end = pgdat_end_pfn(pgdat);
Bob Piccoe984bb42006-05-20 15:00:31 -07004790 end = ALIGN(end, MAX_ORDER_NR_PAGES);
4791 size = (end - start) * sizeof(struct page);
Dave Hansen6f167ec2005-06-23 00:07:39 -07004792 map = alloc_remap(pgdat->node_id, size);
4793 if (!map)
Yinghai Lu8f389a992011-05-11 15:13:32 -07004794 map = alloc_bootmem_node_nopanic(pgdat, size);
Bob Piccoe984bb42006-05-20 15:00:31 -07004795 pgdat->node_mem_map = map + (pgdat->node_start_pfn - start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 }
Roman Zippel12d810c2007-05-31 00:40:54 -07004797#ifndef CONFIG_NEED_MULTIPLE_NODES
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 /*
4799 * With no DISCONTIG, the global mem_map is just set as node 0's
4800 */
Mel Gormanc7132162006-09-27 01:49:43 -07004801 if (pgdat == NODE_DATA(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 mem_map = NODE_DATA(0)->node_mem_map;
Tejun Heo0ee332c2011-12-08 10:22:09 -08004803#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Mel Gormanc7132162006-09-27 01:49:43 -07004804 if (page_to_pfn(mem_map) != pgdat->node_start_pfn)
Thomas Bogendoerfer467bc462008-01-08 15:33:11 -08004805 mem_map -= (pgdat->node_start_pfn - ARCH_PFN_OFFSET);
Tejun Heo0ee332c2011-12-08 10:22:09 -08004806#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
Mel Gormanc7132162006-09-27 01:49:43 -07004807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808#endif
Andy Whitcroftd41dee32005-06-23 00:07:54 -07004809#endif /* CONFIG_FLAT_NODE_MEM_MAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810}
4811
Johannes Weiner9109fb72008-07-23 21:27:20 -07004812void __paginginit free_area_init_node(int nid, unsigned long *zones_size,
4813 unsigned long node_start_pfn, unsigned long *zholes_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814{
Johannes Weiner9109fb72008-07-23 21:27:20 -07004815 pg_data_t *pgdat = NODE_DATA(nid);
Zhang Yanfei7960aed2013-07-08 15:59:52 -07004816 unsigned long start_pfn = 0;
4817 unsigned long end_pfn = 0;
Johannes Weiner9109fb72008-07-23 21:27:20 -07004818
Minchan Kim88fdf752012-07-31 16:46:14 -07004819 /* pg_data_t should be reset to zero when it's allocated */
Linus Torvalds8783b6e2012-08-02 10:37:03 -07004820 WARN_ON(pgdat->nr_zones || pgdat->classzone_idx);
Minchan Kim88fdf752012-07-31 16:46:14 -07004821
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 pgdat->node_id = nid;
4823 pgdat->node_start_pfn = node_start_pfn;
David Rientjes957f8222012-10-08 16:33:24 -07004824 init_zone_allows_reclaim(nid);
Zhang Yanfei7960aed2013-07-08 15:59:52 -07004825#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
4826 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
4827#endif
4828 calculate_node_totalpages(pgdat, start_pfn, end_pfn,
4829 zones_size, zholes_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830
4831 alloc_node_mem_map(pgdat);
Yinghai Lue8c27ac2008-06-01 13:15:22 -07004832#ifdef CONFIG_FLAT_NODE_MEM_MAP
4833 printk(KERN_DEBUG "free_area_init_node: node %d, pgdat %08lx, node_mem_map %08lx\n",
4834 nid, (unsigned long)pgdat,
4835 (unsigned long)pgdat->node_mem_map);
4836#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837
Zhang Yanfei7960aed2013-07-08 15:59:52 -07004838 free_area_init_core(pgdat, start_pfn, end_pfn,
4839 zones_size, zholes_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840}
4841
Tejun Heo0ee332c2011-12-08 10:22:09 -08004842#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Miklos Szeredi418508c2007-05-23 13:57:55 -07004843
4844#if MAX_NUMNODES > 1
4845/*
4846 * Figure out the number of possible node ids.
4847 */
Cody P Schaferf9872ca2013-04-29 15:08:01 -07004848void __init setup_nr_node_ids(void)
Miklos Szeredi418508c2007-05-23 13:57:55 -07004849{
4850 unsigned int node;
4851 unsigned int highest = 0;
4852
4853 for_each_node_mask(node, node_possible_map)
4854 highest = node;
4855 nr_node_ids = highest + 1;
4856}
Miklos Szeredi418508c2007-05-23 13:57:55 -07004857#endif
4858
Mel Gormanc7132162006-09-27 01:49:43 -07004859/**
Tejun Heo1e019792011-07-12 09:45:34 +02004860 * node_map_pfn_alignment - determine the maximum internode alignment
4861 *
4862 * This function should be called after node map is populated and sorted.
4863 * It calculates the maximum power of two alignment which can distinguish
4864 * all the nodes.
4865 *
4866 * For example, if all nodes are 1GiB and aligned to 1GiB, the return value
4867 * would indicate 1GiB alignment with (1 << (30 - PAGE_SHIFT)). If the
4868 * nodes are shifted by 256MiB, 256MiB. Note that if only the last node is
4869 * shifted, 1GiB is enough and this function will indicate so.
4870 *
4871 * This is used to test whether pfn -> nid mapping of the chosen memory
4872 * model has fine enough granularity to avoid incorrect mapping for the
4873 * populated node map.
4874 *
4875 * Returns the determined alignment in pfn's. 0 if there is no alignment
4876 * requirement (single node).
4877 */
4878unsigned long __init node_map_pfn_alignment(void)
4879{
4880 unsigned long accl_mask = 0, last_end = 0;
Tejun Heoc13291a2011-07-12 10:46:30 +02004881 unsigned long start, end, mask;
Tejun Heo1e019792011-07-12 09:45:34 +02004882 int last_nid = -1;
Tejun Heoc13291a2011-07-12 10:46:30 +02004883 int i, nid;
Tejun Heo1e019792011-07-12 09:45:34 +02004884
Tejun Heoc13291a2011-07-12 10:46:30 +02004885 for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, &nid) {
Tejun Heo1e019792011-07-12 09:45:34 +02004886 if (!start || last_nid < 0 || last_nid == nid) {
4887 last_nid = nid;
4888 last_end = end;
4889 continue;
4890 }
4891
4892 /*
4893 * Start with a mask granular enough to pin-point to the
4894 * start pfn and tick off bits one-by-one until it becomes
4895 * too coarse to separate the current node from the last.
4896 */
4897 mask = ~((1 << __ffs(start)) - 1);
4898 while (mask && last_end <= (start & (mask << 1)))
4899 mask <<= 1;
4900
4901 /* accumulate all internode masks */
4902 accl_mask |= mask;
4903 }
4904
4905 /* convert mask to number of pages */
4906 return ~accl_mask + 1;
4907}
4908
Mel Gormana6af2bc2007-02-10 01:42:57 -08004909/* Find the lowest pfn for a node */
Adrian Bunkb69a7282008-07-23 21:28:12 -07004910static unsigned long __init find_min_pfn_for_node(int nid)
Mel Gormanc7132162006-09-27 01:49:43 -07004911{
Mel Gormana6af2bc2007-02-10 01:42:57 -08004912 unsigned long min_pfn = ULONG_MAX;
Tejun Heoc13291a2011-07-12 10:46:30 +02004913 unsigned long start_pfn;
4914 int i;
Mel Gorman1abbfb42006-11-23 12:01:41 +00004915
Tejun Heoc13291a2011-07-12 10:46:30 +02004916 for_each_mem_pfn_range(i, nid, &start_pfn, NULL, NULL)
4917 min_pfn = min(min_pfn, start_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07004918
Mel Gormana6af2bc2007-02-10 01:42:57 -08004919 if (min_pfn == ULONG_MAX) {
4920 printk(KERN_WARNING
Paul Jackson2bc0d262008-06-22 07:22:17 -07004921 "Could not find start_pfn for node %d\n", nid);
Mel Gormana6af2bc2007-02-10 01:42:57 -08004922 return 0;
4923 }
4924
4925 return min_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07004926}
4927
4928/**
4929 * find_min_pfn_with_active_regions - Find the minimum PFN registered
4930 *
4931 * It returns the minimum PFN based on information provided via
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004932 * add_active_range().
Mel Gormanc7132162006-09-27 01:49:43 -07004933 */
4934unsigned long __init find_min_pfn_with_active_regions(void)
4935{
4936 return find_min_pfn_for_node(MAX_NUMNODES);
4937}
4938
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07004939/*
4940 * early_calculate_totalpages()
4941 * Sum pages in active regions for movable zone.
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08004942 * Populate N_MEMORY for calculating usable_nodes.
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07004943 */
Adrian Bunk484f51f2007-10-16 01:26:03 -07004944static unsigned long __init early_calculate_totalpages(void)
Mel Gorman7e63efe2007-07-17 04:03:15 -07004945{
Mel Gorman7e63efe2007-07-17 04:03:15 -07004946 unsigned long totalpages = 0;
Tejun Heoc13291a2011-07-12 10:46:30 +02004947 unsigned long start_pfn, end_pfn;
4948 int i, nid;
Mel Gorman7e63efe2007-07-17 04:03:15 -07004949
Tejun Heoc13291a2011-07-12 10:46:30 +02004950 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) {
4951 unsigned long pages = end_pfn - start_pfn;
4952
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07004953 totalpages += pages;
4954 if (pages)
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08004955 node_set_state(nid, N_MEMORY);
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07004956 }
Pintu Kumarb8af2942013-09-11 14:20:34 -07004957 return totalpages;
Mel Gorman7e63efe2007-07-17 04:03:15 -07004958}
4959
Mel Gorman2a1e2742007-07-17 04:03:12 -07004960/*
4961 * Find the PFN the Movable zone begins in each node. Kernel memory
4962 * is spread evenly between nodes as long as the nodes have enough
4963 * memory. When they don't, some nodes will have more kernelcore than
4964 * others
4965 */
Kautuk Consulb224ef82012-03-21 16:34:15 -07004966static void __init find_zone_movable_pfns_for_nodes(void)
Mel Gorman2a1e2742007-07-17 04:03:12 -07004967{
4968 int i, nid;
4969 unsigned long usable_startpfn;
4970 unsigned long kernelcore_node, kernelcore_remaining;
Yinghai Lu66918dc2009-06-30 11:41:37 -07004971 /* save the state before borrow the nodemask */
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08004972 nodemask_t saved_node_state = node_states[N_MEMORY];
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07004973 unsigned long totalpages = early_calculate_totalpages();
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08004974 int usable_nodes = nodes_weight(node_states[N_MEMORY]);
Mel Gorman2a1e2742007-07-17 04:03:12 -07004975
Mel Gorman7e63efe2007-07-17 04:03:15 -07004976 /*
4977 * If movablecore was specified, calculate what size of
4978 * kernelcore that corresponds so that memory usable for
4979 * any allocation type is evenly spread. If both kernelcore
4980 * and movablecore are specified, then the value of kernelcore
4981 * will be used for required_kernelcore if it's greater than
4982 * what movablecore would have allowed.
4983 */
4984 if (required_movablecore) {
Mel Gorman7e63efe2007-07-17 04:03:15 -07004985 unsigned long corepages;
4986
4987 /*
4988 * Round-up so that ZONE_MOVABLE is at least as large as what
4989 * was requested by the user
4990 */
4991 required_movablecore =
4992 roundup(required_movablecore, MAX_ORDER_NR_PAGES);
4993 corepages = totalpages - required_movablecore;
4994
4995 required_kernelcore = max(required_kernelcore, corepages);
4996 }
4997
Yinghai Lu20e69262013-03-01 14:51:27 -08004998 /* If kernelcore was not specified, there is no ZONE_MOVABLE */
4999 if (!required_kernelcore)
Yinghai Lu66918dc2009-06-30 11:41:37 -07005000 goto out;
Mel Gorman2a1e2742007-07-17 04:03:12 -07005001
5002 /* usable_startpfn is the lowest possible pfn ZONE_MOVABLE can be at */
Yinghai Lu20e69262013-03-01 14:51:27 -08005003 find_usable_zone_for_movable();
Mel Gorman2a1e2742007-07-17 04:03:12 -07005004 usable_startpfn = arch_zone_lowest_possible_pfn[movable_zone];
5005
5006restart:
5007 /* Spread kernelcore memory as evenly as possible throughout nodes */
5008 kernelcore_node = required_kernelcore / usable_nodes;
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08005009 for_each_node_state(nid, N_MEMORY) {
Tejun Heoc13291a2011-07-12 10:46:30 +02005010 unsigned long start_pfn, end_pfn;
5011
Mel Gorman2a1e2742007-07-17 04:03:12 -07005012 /*
5013 * Recalculate kernelcore_node if the division per node
5014 * now exceeds what is necessary to satisfy the requested
5015 * amount of memory for the kernel
5016 */
5017 if (required_kernelcore < kernelcore_node)
5018 kernelcore_node = required_kernelcore / usable_nodes;
5019
5020 /*
5021 * As the map is walked, we track how much memory is usable
5022 * by the kernel using kernelcore_remaining. When it is
5023 * 0, the rest of the node is usable by ZONE_MOVABLE
5024 */
5025 kernelcore_remaining = kernelcore_node;
5026
5027 /* Go through each range of PFNs within this node */
Tejun Heoc13291a2011-07-12 10:46:30 +02005028 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
Mel Gorman2a1e2742007-07-17 04:03:12 -07005029 unsigned long size_pages;
5030
Tejun Heoc13291a2011-07-12 10:46:30 +02005031 start_pfn = max(start_pfn, zone_movable_pfn[nid]);
Mel Gorman2a1e2742007-07-17 04:03:12 -07005032 if (start_pfn >= end_pfn)
5033 continue;
5034
5035 /* Account for what is only usable for kernelcore */
5036 if (start_pfn < usable_startpfn) {
5037 unsigned long kernel_pages;
5038 kernel_pages = min(end_pfn, usable_startpfn)
5039 - start_pfn;
5040
5041 kernelcore_remaining -= min(kernel_pages,
5042 kernelcore_remaining);
5043 required_kernelcore -= min(kernel_pages,
5044 required_kernelcore);
5045
5046 /* Continue if range is now fully accounted */
5047 if (end_pfn <= usable_startpfn) {
5048
5049 /*
5050 * Push zone_movable_pfn to the end so
5051 * that if we have to rebalance
5052 * kernelcore across nodes, we will
5053 * not double account here
5054 */
5055 zone_movable_pfn[nid] = end_pfn;
5056 continue;
5057 }
5058 start_pfn = usable_startpfn;
5059 }
5060
5061 /*
5062 * The usable PFN range for ZONE_MOVABLE is from
5063 * start_pfn->end_pfn. Calculate size_pages as the
5064 * number of pages used as kernelcore
5065 */
5066 size_pages = end_pfn - start_pfn;
5067 if (size_pages > kernelcore_remaining)
5068 size_pages = kernelcore_remaining;
5069 zone_movable_pfn[nid] = start_pfn + size_pages;
5070
5071 /*
5072 * Some kernelcore has been met, update counts and
5073 * break if the kernelcore for this node has been
Pintu Kumarb8af2942013-09-11 14:20:34 -07005074 * satisfied
Mel Gorman2a1e2742007-07-17 04:03:12 -07005075 */
5076 required_kernelcore -= min(required_kernelcore,
5077 size_pages);
5078 kernelcore_remaining -= size_pages;
5079 if (!kernelcore_remaining)
5080 break;
5081 }
5082 }
5083
5084 /*
5085 * If there is still required_kernelcore, we do another pass with one
5086 * less node in the count. This will push zone_movable_pfn[nid] further
5087 * along on the nodes that still have memory until kernelcore is
Pintu Kumarb8af2942013-09-11 14:20:34 -07005088 * satisfied
Mel Gorman2a1e2742007-07-17 04:03:12 -07005089 */
5090 usable_nodes--;
5091 if (usable_nodes && required_kernelcore > usable_nodes)
5092 goto restart;
5093
5094 /* Align start of ZONE_MOVABLE on all nids to MAX_ORDER_NR_PAGES */
5095 for (nid = 0; nid < MAX_NUMNODES; nid++)
5096 zone_movable_pfn[nid] =
5097 roundup(zone_movable_pfn[nid], MAX_ORDER_NR_PAGES);
Yinghai Lu66918dc2009-06-30 11:41:37 -07005098
Yinghai Lu20e69262013-03-01 14:51:27 -08005099out:
Yinghai Lu66918dc2009-06-30 11:41:37 -07005100 /* restore the node_state */
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08005101 node_states[N_MEMORY] = saved_node_state;
Mel Gorman2a1e2742007-07-17 04:03:12 -07005102}
5103
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08005104/* Any regular or high memory on that node ? */
5105static void check_for_memory(pg_data_t *pgdat, int nid)
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07005106{
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07005107 enum zone_type zone_type;
5108
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08005109 if (N_MEMORY == N_NORMAL_MEMORY)
5110 return;
5111
5112 for (zone_type = 0; zone_type <= ZONE_MOVABLE - 1; zone_type++) {
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07005113 struct zone *zone = &pgdat->node_zones[zone_type];
Bob Liud0048b02012-01-12 17:19:07 -08005114 if (zone->present_pages) {
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08005115 node_set_state(nid, N_HIGH_MEMORY);
5116 if (N_NORMAL_MEMORY != N_HIGH_MEMORY &&
5117 zone_type <= ZONE_NORMAL)
5118 node_set_state(nid, N_NORMAL_MEMORY);
Bob Liud0048b02012-01-12 17:19:07 -08005119 break;
5120 }
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07005121 }
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07005122}
5123
Mel Gormanc7132162006-09-27 01:49:43 -07005124/**
5125 * free_area_init_nodes - Initialise all pg_data_t and zone data
Randy Dunlap88ca3b92006-10-04 02:15:25 -07005126 * @max_zone_pfn: an array of max PFNs for each zone
Mel Gormanc7132162006-09-27 01:49:43 -07005127 *
5128 * This will call free_area_init_node() for each active node in the system.
5129 * Using the page ranges provided by add_active_range(), the size of each
5130 * zone in each node and their holes is calculated. If the maximum PFN
5131 * between two adjacent zones match, it is assumed that the zone is empty.
5132 * For example, if arch_max_dma_pfn == arch_max_dma32_pfn, it is assumed
5133 * that arch_max_dma32_pfn has no pages. It is also assumed that a zone
5134 * starts where the previous one ended. For example, ZONE_DMA32 starts
5135 * at arch_max_dma_pfn.
5136 */
5137void __init free_area_init_nodes(unsigned long *max_zone_pfn)
5138{
Tejun Heoc13291a2011-07-12 10:46:30 +02005139 unsigned long start_pfn, end_pfn;
5140 int i, nid;
Mel Gormana6af2bc2007-02-10 01:42:57 -08005141
Mel Gormanc7132162006-09-27 01:49:43 -07005142 /* Record where the zone boundaries are */
5143 memset(arch_zone_lowest_possible_pfn, 0,
5144 sizeof(arch_zone_lowest_possible_pfn));
5145 memset(arch_zone_highest_possible_pfn, 0,
5146 sizeof(arch_zone_highest_possible_pfn));
5147 arch_zone_lowest_possible_pfn[0] = find_min_pfn_with_active_regions();
5148 arch_zone_highest_possible_pfn[0] = max_zone_pfn[0];
5149 for (i = 1; i < MAX_NR_ZONES; i++) {
Mel Gorman2a1e2742007-07-17 04:03:12 -07005150 if (i == ZONE_MOVABLE)
5151 continue;
Mel Gormanc7132162006-09-27 01:49:43 -07005152 arch_zone_lowest_possible_pfn[i] =
5153 arch_zone_highest_possible_pfn[i-1];
5154 arch_zone_highest_possible_pfn[i] =
5155 max(max_zone_pfn[i], arch_zone_lowest_possible_pfn[i]);
5156 }
Mel Gorman2a1e2742007-07-17 04:03:12 -07005157 arch_zone_lowest_possible_pfn[ZONE_MOVABLE] = 0;
5158 arch_zone_highest_possible_pfn[ZONE_MOVABLE] = 0;
5159
5160 /* Find the PFNs that ZONE_MOVABLE begins at in each node */
5161 memset(zone_movable_pfn, 0, sizeof(zone_movable_pfn));
Kautuk Consulb224ef82012-03-21 16:34:15 -07005162 find_zone_movable_pfns_for_nodes();
Mel Gormanc7132162006-09-27 01:49:43 -07005163
Mel Gormanc7132162006-09-27 01:49:43 -07005164 /* Print out the zone ranges */
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07005165 printk("Zone ranges:\n");
Mel Gorman2a1e2742007-07-17 04:03:12 -07005166 for (i = 0; i < MAX_NR_ZONES; i++) {
5167 if (i == ZONE_MOVABLE)
5168 continue;
Kay Sievers155cbfc2012-05-08 17:24:14 +02005169 printk(KERN_CONT " %-8s ", zone_names[i]);
David Rientjes72f0ba02010-03-05 13:42:14 -08005170 if (arch_zone_lowest_possible_pfn[i] ==
5171 arch_zone_highest_possible_pfn[i])
Kay Sievers155cbfc2012-05-08 17:24:14 +02005172 printk(KERN_CONT "empty\n");
David Rientjes72f0ba02010-03-05 13:42:14 -08005173 else
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07005174 printk(KERN_CONT "[mem %0#10lx-%0#10lx]\n",
5175 arch_zone_lowest_possible_pfn[i] << PAGE_SHIFT,
5176 (arch_zone_highest_possible_pfn[i]
5177 << PAGE_SHIFT) - 1);
Mel Gorman2a1e2742007-07-17 04:03:12 -07005178 }
5179
5180 /* Print out the PFNs ZONE_MOVABLE begins at in each node */
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07005181 printk("Movable zone start for each node\n");
Mel Gorman2a1e2742007-07-17 04:03:12 -07005182 for (i = 0; i < MAX_NUMNODES; i++) {
5183 if (zone_movable_pfn[i])
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07005184 printk(" Node %d: %#010lx\n", i,
5185 zone_movable_pfn[i] << PAGE_SHIFT);
Mel Gorman2a1e2742007-07-17 04:03:12 -07005186 }
Mel Gormanc7132162006-09-27 01:49:43 -07005187
Wanpeng Lif2d52fe2012-10-08 16:32:24 -07005188 /* Print out the early node map */
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07005189 printk("Early memory node ranges\n");
Tejun Heoc13291a2011-07-12 10:46:30 +02005190 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid)
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07005191 printk(" node %3d: [mem %#010lx-%#010lx]\n", nid,
5192 start_pfn << PAGE_SHIFT, (end_pfn << PAGE_SHIFT) - 1);
Mel Gormanc7132162006-09-27 01:49:43 -07005193
5194 /* Initialise every node */
Mel Gorman708614e2008-07-23 21:26:51 -07005195 mminit_verify_pageflags_layout();
Christoph Lameter8ef82862007-02-20 13:57:52 -08005196 setup_nr_node_ids();
Mel Gormanc7132162006-09-27 01:49:43 -07005197 for_each_online_node(nid) {
5198 pg_data_t *pgdat = NODE_DATA(nid);
Johannes Weiner9109fb72008-07-23 21:27:20 -07005199 free_area_init_node(nid, NULL,
Mel Gormanc7132162006-09-27 01:49:43 -07005200 find_min_pfn_for_node(nid), NULL);
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07005201
5202 /* Any memory on that node */
5203 if (pgdat->node_present_pages)
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08005204 node_set_state(nid, N_MEMORY);
5205 check_for_memory(pgdat, nid);
Mel Gormanc7132162006-09-27 01:49:43 -07005206 }
5207}
Mel Gorman2a1e2742007-07-17 04:03:12 -07005208
Mel Gorman7e63efe2007-07-17 04:03:15 -07005209static int __init cmdline_parse_core(char *p, unsigned long *core)
Mel Gorman2a1e2742007-07-17 04:03:12 -07005210{
5211 unsigned long long coremem;
5212 if (!p)
5213 return -EINVAL;
5214
5215 coremem = memparse(p, &p);
Mel Gorman7e63efe2007-07-17 04:03:15 -07005216 *core = coremem >> PAGE_SHIFT;
Mel Gorman2a1e2742007-07-17 04:03:12 -07005217
Mel Gorman7e63efe2007-07-17 04:03:15 -07005218 /* Paranoid check that UL is enough for the coremem value */
Mel Gorman2a1e2742007-07-17 04:03:12 -07005219 WARN_ON((coremem >> PAGE_SHIFT) > ULONG_MAX);
5220
5221 return 0;
5222}
Mel Gormaned7ed362007-07-17 04:03:14 -07005223
Mel Gorman7e63efe2007-07-17 04:03:15 -07005224/*
5225 * kernelcore=size sets the amount of memory for use for allocations that
5226 * cannot be reclaimed or migrated.
5227 */
5228static int __init cmdline_parse_kernelcore(char *p)
5229{
5230 return cmdline_parse_core(p, &required_kernelcore);
5231}
5232
5233/*
5234 * movablecore=size sets the amount of memory for use for allocations that
5235 * can be reclaimed or migrated.
5236 */
5237static int __init cmdline_parse_movablecore(char *p)
5238{
5239 return cmdline_parse_core(p, &required_movablecore);
5240}
5241
Mel Gormaned7ed362007-07-17 04:03:14 -07005242early_param("kernelcore", cmdline_parse_kernelcore);
Mel Gorman7e63efe2007-07-17 04:03:15 -07005243early_param("movablecore", cmdline_parse_movablecore);
Mel Gormaned7ed362007-07-17 04:03:14 -07005244
Tejun Heo0ee332c2011-12-08 10:22:09 -08005245#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
Mel Gormanc7132162006-09-27 01:49:43 -07005246
Jiang Liuc3d5f5f2013-07-03 15:03:14 -07005247void adjust_managed_page_count(struct page *page, long count)
5248{
5249 spin_lock(&managed_page_count_lock);
5250 page_zone(page)->managed_pages += count;
5251 totalram_pages += count;
Jiang Liu3dcc0572013-07-03 15:03:21 -07005252#ifdef CONFIG_HIGHMEM
5253 if (PageHighMem(page))
5254 totalhigh_pages += count;
5255#endif
Jiang Liuc3d5f5f2013-07-03 15:03:14 -07005256 spin_unlock(&managed_page_count_lock);
5257}
Jiang Liu3dcc0572013-07-03 15:03:21 -07005258EXPORT_SYMBOL(adjust_managed_page_count);
Jiang Liuc3d5f5f2013-07-03 15:03:14 -07005259
Jiang Liu11199692013-07-03 15:02:48 -07005260unsigned long free_reserved_area(void *start, void *end, int poison, char *s)
Jiang Liu69afade2013-04-29 15:06:21 -07005261{
Jiang Liu11199692013-07-03 15:02:48 -07005262 void *pos;
5263 unsigned long pages = 0;
Jiang Liu69afade2013-04-29 15:06:21 -07005264
Jiang Liu11199692013-07-03 15:02:48 -07005265 start = (void *)PAGE_ALIGN((unsigned long)start);
5266 end = (void *)((unsigned long)end & PAGE_MASK);
5267 for (pos = start; pos < end; pos += PAGE_SIZE, pages++) {
Jiang Liudbe67df2013-07-03 15:02:51 -07005268 if ((unsigned int)poison <= 0xFF)
Jiang Liu11199692013-07-03 15:02:48 -07005269 memset(pos, poison, PAGE_SIZE);
5270 free_reserved_page(virt_to_page(pos));
Jiang Liu69afade2013-04-29 15:06:21 -07005271 }
5272
5273 if (pages && s)
Jiang Liu11199692013-07-03 15:02:48 -07005274 pr_info("Freeing %s memory: %ldK (%p - %p)\n",
Jiang Liu69afade2013-04-29 15:06:21 -07005275 s, pages << (PAGE_SHIFT - 10), start, end);
5276
5277 return pages;
5278}
Jiang Liu11199692013-07-03 15:02:48 -07005279EXPORT_SYMBOL(free_reserved_area);
Jiang Liu69afade2013-04-29 15:06:21 -07005280
Jiang Liucfa11e02013-04-29 15:07:00 -07005281#ifdef CONFIG_HIGHMEM
5282void free_highmem_page(struct page *page)
5283{
5284 __free_reserved_page(page);
5285 totalram_pages++;
Jiang Liu7b4b2a02013-07-03 15:03:11 -07005286 page_zone(page)->managed_pages++;
Jiang Liucfa11e02013-04-29 15:07:00 -07005287 totalhigh_pages++;
5288}
5289#endif
5290
Jiang Liu7ee3d4e2013-07-03 15:03:41 -07005291
5292void __init mem_init_print_info(const char *str)
5293{
5294 unsigned long physpages, codesize, datasize, rosize, bss_size;
5295 unsigned long init_code_size, init_data_size;
5296
5297 physpages = get_num_physpages();
5298 codesize = _etext - _stext;
5299 datasize = _edata - _sdata;
5300 rosize = __end_rodata - __start_rodata;
5301 bss_size = __bss_stop - __bss_start;
5302 init_data_size = __init_end - __init_begin;
5303 init_code_size = _einittext - _sinittext;
5304
5305 /*
5306 * Detect special cases and adjust section sizes accordingly:
5307 * 1) .init.* may be embedded into .data sections
5308 * 2) .init.text.* may be out of [__init_begin, __init_end],
5309 * please refer to arch/tile/kernel/vmlinux.lds.S.
5310 * 3) .rodata.* may be embedded into .text or .data sections.
5311 */
5312#define adj_init_size(start, end, size, pos, adj) \
Pintu Kumarb8af2942013-09-11 14:20:34 -07005313 do { \
5314 if (start <= pos && pos < end && size > adj) \
5315 size -= adj; \
5316 } while (0)
Jiang Liu7ee3d4e2013-07-03 15:03:41 -07005317
5318 adj_init_size(__init_begin, __init_end, init_data_size,
5319 _sinittext, init_code_size);
5320 adj_init_size(_stext, _etext, codesize, _sinittext, init_code_size);
5321 adj_init_size(_sdata, _edata, datasize, __init_begin, init_data_size);
5322 adj_init_size(_stext, _etext, codesize, __start_rodata, rosize);
5323 adj_init_size(_sdata, _edata, datasize, __start_rodata, rosize);
5324
5325#undef adj_init_size
5326
5327 printk("Memory: %luK/%luK available "
5328 "(%luK kernel code, %luK rwdata, %luK rodata, "
5329 "%luK init, %luK bss, %luK reserved"
5330#ifdef CONFIG_HIGHMEM
5331 ", %luK highmem"
5332#endif
5333 "%s%s)\n",
5334 nr_free_pages() << (PAGE_SHIFT-10), physpages << (PAGE_SHIFT-10),
5335 codesize >> 10, datasize >> 10, rosize >> 10,
5336 (init_data_size + init_code_size) >> 10, bss_size >> 10,
5337 (physpages - totalram_pages) << (PAGE_SHIFT-10),
5338#ifdef CONFIG_HIGHMEM
5339 totalhigh_pages << (PAGE_SHIFT-10),
5340#endif
5341 str ? ", " : "", str ? str : "");
5342}
5343
Mel Gorman0e0b8642006-09-27 01:49:56 -07005344/**
Randy Dunlap88ca3b92006-10-04 02:15:25 -07005345 * set_dma_reserve - set the specified number of pages reserved in the first zone
5346 * @new_dma_reserve: The number of pages to mark reserved
Mel Gorman0e0b8642006-09-27 01:49:56 -07005347 *
5348 * The per-cpu batchsize and zone watermarks are determined by present_pages.
5349 * In the DMA zone, a significant percentage may be consumed by kernel image
5350 * and other unfreeable allocations which can skew the watermarks badly. This
Randy Dunlap88ca3b92006-10-04 02:15:25 -07005351 * function may optionally be used to account for unfreeable pages in the
5352 * first zone (e.g., ZONE_DMA). The effect will be lower watermarks and
5353 * smaller per-cpu batchsize.
Mel Gorman0e0b8642006-09-27 01:49:56 -07005354 */
5355void __init set_dma_reserve(unsigned long new_dma_reserve)
5356{
5357 dma_reserve = new_dma_reserve;
5358}
5359
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360void __init free_area_init(unsigned long *zones_size)
5361{
Johannes Weiner9109fb72008-07-23 21:27:20 -07005362 free_area_init_node(0, zones_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
5364}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366static int page_alloc_cpu_notify(struct notifier_block *self,
5367 unsigned long action, void *hcpu)
5368{
5369 int cpu = (unsigned long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005371 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
Konstantin Khlebnikovf0cb3c72012-03-21 16:34:06 -07005372 lru_add_drain_cpu(cpu);
Christoph Lameter9f8f2172008-02-04 22:29:11 -08005373 drain_pages(cpu);
5374
5375 /*
5376 * Spill the event counters of the dead processor
5377 * into the current processors event counters.
5378 * This artificially elevates the count of the current
5379 * processor.
5380 */
Christoph Lameterf8891e52006-06-30 01:55:45 -07005381 vm_events_fold_cpu(cpu);
Christoph Lameter9f8f2172008-02-04 22:29:11 -08005382
5383 /*
5384 * Zero the differential counters of the dead processor
5385 * so that the vm statistics are consistent.
5386 *
5387 * This is only okay since the processor is dead and cannot
5388 * race with what we are doing.
5389 */
Christoph Lameter2244b952006-06-30 01:55:33 -07005390 refresh_cpu_vm_stats(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 }
5392 return NOTIFY_OK;
5393}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394
5395void __init page_alloc_init(void)
5396{
5397 hotcpu_notifier(page_alloc_cpu_notify, 0);
5398}
5399
5400/*
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07005401 * calculate_totalreserve_pages - called when sysctl_lower_zone_reserve_ratio
5402 * or min_free_kbytes changes.
5403 */
5404static void calculate_totalreserve_pages(void)
5405{
5406 struct pglist_data *pgdat;
5407 unsigned long reserve_pages = 0;
Christoph Lameter2f6726e2006-09-25 23:31:18 -07005408 enum zone_type i, j;
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07005409
5410 for_each_online_pgdat(pgdat) {
5411 for (i = 0; i < MAX_NR_ZONES; i++) {
5412 struct zone *zone = pgdat->node_zones + i;
5413 unsigned long max = 0;
5414
5415 /* Find valid and maximum lowmem_reserve in the zone */
5416 for (j = i; j < MAX_NR_ZONES; j++) {
5417 if (zone->lowmem_reserve[j] > max)
5418 max = zone->lowmem_reserve[j];
5419 }
5420
Mel Gorman41858962009-06-16 15:32:12 -07005421 /* we treat the high watermark as reserved pages. */
5422 max += high_wmark_pages(zone);
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07005423
Jiang Liub40da042013-02-22 16:33:52 -08005424 if (max > zone->managed_pages)
5425 max = zone->managed_pages;
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07005426 reserve_pages += max;
Johannes Weinerab8fabd2012-01-10 15:07:42 -08005427 /*
5428 * Lowmem reserves are not available to
5429 * GFP_HIGHUSER page cache allocations and
5430 * kswapd tries to balance zones to their high
5431 * watermark. As a result, neither should be
5432 * regarded as dirtyable memory, to prevent a
5433 * situation where reclaim has to clean pages
5434 * in order to balance the zones.
5435 */
5436 zone->dirty_balance_reserve = max;
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07005437 }
5438 }
Johannes Weinerab8fabd2012-01-10 15:07:42 -08005439 dirty_balance_reserve = reserve_pages;
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07005440 totalreserve_pages = reserve_pages;
5441}
5442
5443/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 * setup_per_zone_lowmem_reserve - called whenever
5445 * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
5446 * has a correct pages reserved value, so an adequate number of
5447 * pages are left in the zone after a successful __alloc_pages().
5448 */
5449static void setup_per_zone_lowmem_reserve(void)
5450{
5451 struct pglist_data *pgdat;
Christoph Lameter2f6726e2006-09-25 23:31:18 -07005452 enum zone_type j, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453
KAMEZAWA Hiroyukiec936fc2006-03-27 01:15:59 -08005454 for_each_online_pgdat(pgdat) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 for (j = 0; j < MAX_NR_ZONES; j++) {
5456 struct zone *zone = pgdat->node_zones + j;
Jiang Liub40da042013-02-22 16:33:52 -08005457 unsigned long managed_pages = zone->managed_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458
5459 zone->lowmem_reserve[j] = 0;
5460
Christoph Lameter2f6726e2006-09-25 23:31:18 -07005461 idx = j;
5462 while (idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 struct zone *lower_zone;
5464
Christoph Lameter2f6726e2006-09-25 23:31:18 -07005465 idx--;
5466
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 if (sysctl_lowmem_reserve_ratio[idx] < 1)
5468 sysctl_lowmem_reserve_ratio[idx] = 1;
5469
5470 lower_zone = pgdat->node_zones + idx;
Jiang Liub40da042013-02-22 16:33:52 -08005471 lower_zone->lowmem_reserve[j] = managed_pages /
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 sysctl_lowmem_reserve_ratio[idx];
Jiang Liub40da042013-02-22 16:33:52 -08005473 managed_pages += lower_zone->managed_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 }
5475 }
5476 }
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07005477
5478 /* update totalreserve_pages */
5479 calculate_totalreserve_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480}
5481
Mel Gormancfd3da12011-04-25 21:36:42 +00005482static void __setup_per_zone_wmarks(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483{
5484 unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
5485 unsigned long lowmem_pages = 0;
5486 struct zone *zone;
5487 unsigned long flags;
5488
5489 /* Calculate total number of !ZONE_HIGHMEM pages */
5490 for_each_zone(zone) {
5491 if (!is_highmem(zone))
Jiang Liub40da042013-02-22 16:33:52 -08005492 lowmem_pages += zone->managed_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 }
5494
5495 for_each_zone(zone) {
Andrew Mortonac924c62006-05-15 09:43:59 -07005496 u64 tmp;
5497
Gerald Schaefer1125b4e2008-10-18 20:27:11 -07005498 spin_lock_irqsave(&zone->lock, flags);
Jiang Liub40da042013-02-22 16:33:52 -08005499 tmp = (u64)pages_min * zone->managed_pages;
Andrew Mortonac924c62006-05-15 09:43:59 -07005500 do_div(tmp, lowmem_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 if (is_highmem(zone)) {
5502 /*
Nick Piggin669ed172005-11-13 16:06:45 -08005503 * __GFP_HIGH and PF_MEMALLOC allocations usually don't
5504 * need highmem pages, so cap pages_min to a small
5505 * value here.
5506 *
Mel Gorman41858962009-06-16 15:32:12 -07005507 * The WMARK_HIGH-WMARK_LOW and (WMARK_LOW-WMARK_MIN)
Nick Piggin669ed172005-11-13 16:06:45 -08005508 * deltas controls asynch page reclaim, and so should
5509 * not be capped for highmem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 */
Andrew Morton90ae8d62013-02-22 16:32:22 -08005511 unsigned long min_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512
Jiang Liub40da042013-02-22 16:33:52 -08005513 min_pages = zone->managed_pages / 1024;
Andrew Morton90ae8d62013-02-22 16:32:22 -08005514 min_pages = clamp(min_pages, SWAP_CLUSTER_MAX, 128UL);
Mel Gorman41858962009-06-16 15:32:12 -07005515 zone->watermark[WMARK_MIN] = min_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 } else {
Nick Piggin669ed172005-11-13 16:06:45 -08005517 /*
5518 * If it's a lowmem zone, reserve a number of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 * proportionate to the zone's size.
5520 */
Mel Gorman41858962009-06-16 15:32:12 -07005521 zone->watermark[WMARK_MIN] = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 }
5523
Mel Gorman41858962009-06-16 15:32:12 -07005524 zone->watermark[WMARK_LOW] = min_wmark_pages(zone) + (tmp >> 2);
5525 zone->watermark[WMARK_HIGH] = min_wmark_pages(zone) + (tmp >> 1);
Marek Szyprowski49f223a2012-01-25 12:49:24 +01005526
Mel Gorman56fd56b2007-10-16 01:25:58 -07005527 setup_zone_migrate_reserve(zone);
Gerald Schaefer1125b4e2008-10-18 20:27:11 -07005528 spin_unlock_irqrestore(&zone->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 }
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07005530
5531 /* update totalreserve_pages */
5532 calculate_totalreserve_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533}
5534
Mel Gormancfd3da12011-04-25 21:36:42 +00005535/**
5536 * setup_per_zone_wmarks - called when min_free_kbytes changes
5537 * or when memory is hot-{added|removed}
5538 *
5539 * Ensures that the watermark[min,low,high] values for each zone are set
5540 * correctly with respect to min_free_kbytes.
5541 */
5542void setup_per_zone_wmarks(void)
5543{
5544 mutex_lock(&zonelists_mutex);
5545 __setup_per_zone_wmarks();
5546 mutex_unlock(&zonelists_mutex);
5547}
5548
Randy Dunlap55a44622009-09-21 17:01:20 -07005549/*
Rik van Riel556adec2008-10-18 20:26:34 -07005550 * The inactive anon list should be small enough that the VM never has to
5551 * do too much work, but large enough that each inactive page has a chance
5552 * to be referenced again before it is swapped out.
5553 *
5554 * The inactive_anon ratio is the target ratio of ACTIVE_ANON to
5555 * INACTIVE_ANON pages on this zone's LRU, maintained by the
5556 * pageout code. A zone->inactive_ratio of 3 means 3:1 or 25% of
5557 * the anonymous pages are kept on the inactive list.
5558 *
5559 * total target max
5560 * memory ratio inactive anon
5561 * -------------------------------------
5562 * 10MB 1 5MB
5563 * 100MB 1 50MB
5564 * 1GB 3 250MB
5565 * 10GB 10 0.9GB
5566 * 100GB 31 3GB
5567 * 1TB 101 10GB
5568 * 10TB 320 32GB
5569 */
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07005570static void __meminit calculate_zone_inactive_ratio(struct zone *zone)
Minchan Kim96cb4df2009-06-16 15:32:49 -07005571{
5572 unsigned int gb, ratio;
5573
5574 /* Zone size in gigabytes */
Jiang Liub40da042013-02-22 16:33:52 -08005575 gb = zone->managed_pages >> (30 - PAGE_SHIFT);
Minchan Kim96cb4df2009-06-16 15:32:49 -07005576 if (gb)
5577 ratio = int_sqrt(10 * gb);
5578 else
5579 ratio = 1;
5580
5581 zone->inactive_ratio = ratio;
5582}
5583
KOSAKI Motohiro839a4fc2011-05-24 17:11:31 -07005584static void __meminit setup_per_zone_inactive_ratio(void)
Rik van Riel556adec2008-10-18 20:26:34 -07005585{
5586 struct zone *zone;
5587
Minchan Kim96cb4df2009-06-16 15:32:49 -07005588 for_each_zone(zone)
5589 calculate_zone_inactive_ratio(zone);
Rik van Riel556adec2008-10-18 20:26:34 -07005590}
5591
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592/*
5593 * Initialise min_free_kbytes.
5594 *
5595 * For small machines we want it small (128k min). For large machines
5596 * we want it large (64MB max). But it is not linear, because network
5597 * bandwidth does not increase linearly with machine size. We use
5598 *
Pintu Kumarb8af2942013-09-11 14:20:34 -07005599 * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 * min_free_kbytes = sqrt(lowmem_kbytes * 16)
5601 *
5602 * which yields
5603 *
5604 * 16MB: 512k
5605 * 32MB: 724k
5606 * 64MB: 1024k
5607 * 128MB: 1448k
5608 * 256MB: 2048k
5609 * 512MB: 2896k
5610 * 1024MB: 4096k
5611 * 2048MB: 5792k
5612 * 4096MB: 8192k
5613 * 8192MB: 11584k
5614 * 16384MB: 16384k
5615 */
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07005616int __meminit init_per_zone_wmark_min(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617{
5618 unsigned long lowmem_kbytes;
Michal Hocko5f127332013-07-08 16:00:40 -07005619 int new_min_free_kbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620
5621 lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
Michal Hocko5f127332013-07-08 16:00:40 -07005622 new_min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623
Michal Hocko5f127332013-07-08 16:00:40 -07005624 if (new_min_free_kbytes > user_min_free_kbytes) {
5625 min_free_kbytes = new_min_free_kbytes;
5626 if (min_free_kbytes < 128)
5627 min_free_kbytes = 128;
5628 if (min_free_kbytes > 65536)
5629 min_free_kbytes = 65536;
5630 } else {
5631 pr_warn("min_free_kbytes is not updated to %d because user defined value %d is preferred\n",
5632 new_min_free_kbytes, user_min_free_kbytes);
5633 }
Minchan Kimbc75d332009-06-16 15:32:48 -07005634 setup_per_zone_wmarks();
KOSAKI Motohiroa6cccdc2011-05-24 17:11:33 -07005635 refresh_zone_stat_thresholds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 setup_per_zone_lowmem_reserve();
Rik van Riel556adec2008-10-18 20:26:34 -07005637 setup_per_zone_inactive_ratio();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 return 0;
5639}
Minchan Kimbc75d332009-06-16 15:32:48 -07005640module_init(init_per_zone_wmark_min)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641
5642/*
Pintu Kumarb8af2942013-09-11 14:20:34 -07005643 * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 * that we can call two helper functions whenever min_free_kbytes
5645 * changes.
5646 */
Pintu Kumarb8af2942013-09-11 14:20:34 -07005647int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005648 void __user *buffer, size_t *length, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005650 proc_dointvec(table, write, buffer, length, ppos);
Michal Hocko5f127332013-07-08 16:00:40 -07005651 if (write) {
5652 user_min_free_kbytes = min_free_kbytes;
Minchan Kimbc75d332009-06-16 15:32:48 -07005653 setup_per_zone_wmarks();
Michal Hocko5f127332013-07-08 16:00:40 -07005654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 return 0;
5656}
5657
Christoph Lameter96146342006-07-03 00:24:13 -07005658#ifdef CONFIG_NUMA
5659int sysctl_min_unmapped_ratio_sysctl_handler(ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005660 void __user *buffer, size_t *length, loff_t *ppos)
Christoph Lameter96146342006-07-03 00:24:13 -07005661{
5662 struct zone *zone;
5663 int rc;
5664
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005665 rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
Christoph Lameter96146342006-07-03 00:24:13 -07005666 if (rc)
5667 return rc;
5668
5669 for_each_zone(zone)
Jiang Liub40da042013-02-22 16:33:52 -08005670 zone->min_unmapped_pages = (zone->managed_pages *
Christoph Lameter96146342006-07-03 00:24:13 -07005671 sysctl_min_unmapped_ratio) / 100;
5672 return 0;
5673}
Christoph Lameter0ff38492006-09-25 23:31:52 -07005674
5675int sysctl_min_slab_ratio_sysctl_handler(ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005676 void __user *buffer, size_t *length, loff_t *ppos)
Christoph Lameter0ff38492006-09-25 23:31:52 -07005677{
5678 struct zone *zone;
5679 int rc;
5680
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005681 rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
Christoph Lameter0ff38492006-09-25 23:31:52 -07005682 if (rc)
5683 return rc;
5684
5685 for_each_zone(zone)
Jiang Liub40da042013-02-22 16:33:52 -08005686 zone->min_slab_pages = (zone->managed_pages *
Christoph Lameter0ff38492006-09-25 23:31:52 -07005687 sysctl_min_slab_ratio) / 100;
5688 return 0;
5689}
Christoph Lameter96146342006-07-03 00:24:13 -07005690#endif
5691
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692/*
5693 * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
5694 * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
5695 * whenever sysctl_lowmem_reserve_ratio changes.
5696 *
5697 * The reserve ratio obviously has absolutely no relation with the
Mel Gorman41858962009-06-16 15:32:12 -07005698 * minimum watermarks. The lowmem reserve ratio can only make sense
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 * if in function of the boot time zone sizes.
5700 */
5701int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005702 void __user *buffer, size_t *length, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005704 proc_dointvec_minmax(table, write, buffer, length, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 setup_per_zone_lowmem_reserve();
5706 return 0;
5707}
5708
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08005709/*
5710 * percpu_pagelist_fraction - changes the pcp->high for each zone on each
Pintu Kumarb8af2942013-09-11 14:20:34 -07005711 * cpu. It is the fraction of total pages in each zone that a hot per cpu
5712 * pagelist can have before it gets flushed back to buddy allocator.
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08005713 */
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08005714int percpu_pagelist_fraction_sysctl_handler(ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005715 void __user *buffer, size_t *length, loff_t *ppos)
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08005716{
5717 struct zone *zone;
5718 unsigned int cpu;
5719 int ret;
5720
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005721 ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
Sasha Levin93278812012-05-10 13:01:44 -07005722 if (!write || (ret < 0))
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08005723 return ret;
Cody P Schaferc8e251f2013-07-03 15:01:29 -07005724
5725 mutex_lock(&pcp_batch_high_lock);
Dimitri Sivanich364df0e2009-06-23 12:37:04 -07005726 for_each_populated_zone(zone) {
Cody P Schafer22a7f122013-07-03 15:01:34 -07005727 unsigned long high;
5728 high = zone->managed_pages / percpu_pagelist_fraction;
5729 for_each_possible_cpu(cpu)
Cody P Schafer36640332013-07-03 15:01:40 -07005730 pageset_set_high(per_cpu_ptr(zone->pageset, cpu),
5731 high);
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08005732 }
Cody P Schaferc8e251f2013-07-03 15:01:29 -07005733 mutex_unlock(&pcp_batch_high_lock);
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08005734 return 0;
5735}
5736
David S. Millerf034b5d2006-08-24 03:08:07 -07005737int hashdist = HASHDIST_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738
5739#ifdef CONFIG_NUMA
5740static int __init set_hashdist(char *str)
5741{
5742 if (!str)
5743 return 0;
5744 hashdist = simple_strtoul(str, &str, 0);
5745 return 1;
5746}
5747__setup("hashdist=", set_hashdist);
5748#endif
5749
5750/*
5751 * allocate a large system hash table from bootmem
5752 * - it is assumed that the hash table must contain an exact power-of-2
5753 * quantity of entries
5754 * - limit is the number of hash buckets, not the total allocation size
5755 */
5756void *__init alloc_large_system_hash(const char *tablename,
5757 unsigned long bucketsize,
5758 unsigned long numentries,
5759 int scale,
5760 int flags,
5761 unsigned int *_hash_shift,
5762 unsigned int *_hash_mask,
Tim Bird31fe62b2012-05-23 13:33:35 +00005763 unsigned long low_limit,
5764 unsigned long high_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765{
Tim Bird31fe62b2012-05-23 13:33:35 +00005766 unsigned long long max = high_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 unsigned long log2qty, size;
5768 void *table = NULL;
5769
5770 /* allow the kernel cmdline to have a say */
5771 if (!numentries) {
5772 /* round applicable memory size up to nearest megabyte */
Andrew Morton04903662006-12-06 20:37:33 -08005773 numentries = nr_kernel_pages;
Jerry Zhoua7e83312013-09-11 14:20:26 -07005774
5775 /* It isn't necessary when PAGE_SIZE >= 1MB */
5776 if (PAGE_SHIFT < 20)
5777 numentries = round_up(numentries, (1<<20)/PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778
5779 /* limit to 1 bucket per 2^scale bytes of low memory */
5780 if (scale > PAGE_SHIFT)
5781 numentries >>= (scale - PAGE_SHIFT);
5782 else
5783 numentries <<= (PAGE_SHIFT - scale);
Paul Mundt9ab37b82007-01-05 16:36:30 -08005784
5785 /* Make sure we've got at least a 0-order allocation.. */
Jan Beulich2c85f512009-09-21 17:03:07 -07005786 if (unlikely(flags & HASH_SMALL)) {
5787 /* Makes no sense without HASH_EARLY */
5788 WARN_ON(!(flags & HASH_EARLY));
5789 if (!(numentries >> *_hash_shift)) {
5790 numentries = 1UL << *_hash_shift;
5791 BUG_ON(!numentries);
5792 }
5793 } else if (unlikely((numentries * bucketsize) < PAGE_SIZE))
Paul Mundt9ab37b82007-01-05 16:36:30 -08005794 numentries = PAGE_SIZE / bucketsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795 }
John Hawkes6e692ed2006-03-25 03:08:02 -08005796 numentries = roundup_pow_of_two(numentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797
5798 /* limit allocation size to 1/16 total memory by default */
5799 if (max == 0) {
5800 max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
5801 do_div(max, bucketsize);
5802 }
Dimitri Sivanich074b8512012-02-08 12:39:07 -08005803 max = min(max, 0x80000000ULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804
Tim Bird31fe62b2012-05-23 13:33:35 +00005805 if (numentries < low_limit)
5806 numentries = low_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807 if (numentries > max)
5808 numentries = max;
5809
David Howellsf0d1b0b2006-12-08 02:37:49 -08005810 log2qty = ilog2(numentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811
5812 do {
5813 size = bucketsize << log2qty;
5814 if (flags & HASH_EARLY)
Jan Beulich74768ed2008-08-12 15:08:39 -07005815 table = alloc_bootmem_nopanic(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816 else if (hashdist)
5817 table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
5818 else {
Eric Dumazet1037b832007-07-15 23:38:05 -07005819 /*
5820 * If bucketsize is not a power-of-two, we may free
Mel Gormana1dd2682009-06-16 15:32:19 -07005821 * some pages at the end of hash table which
5822 * alloc_pages_exact() automatically does
Eric Dumazet1037b832007-07-15 23:38:05 -07005823 */
Catalin Marinas264ef8a2009-07-07 10:33:01 +01005824 if (get_order(size) < MAX_ORDER) {
Mel Gormana1dd2682009-06-16 15:32:19 -07005825 table = alloc_pages_exact(size, GFP_ATOMIC);
Catalin Marinas264ef8a2009-07-07 10:33:01 +01005826 kmemleak_alloc(table, size, 1, GFP_ATOMIC);
5827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 }
5829 } while (!table && size > PAGE_SIZE && --log2qty);
5830
5831 if (!table)
5832 panic("Failed to allocate %s hash table\n", tablename);
5833
Robin Holtf241e6602010-10-07 12:59:26 -07005834 printk(KERN_INFO "%s hash table entries: %ld (order: %d, %lu bytes)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 tablename,
Robin Holtf241e6602010-10-07 12:59:26 -07005836 (1UL << log2qty),
David Howellsf0d1b0b2006-12-08 02:37:49 -08005837 ilog2(size) - PAGE_SHIFT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 size);
5839
5840 if (_hash_shift)
5841 *_hash_shift = log2qty;
5842 if (_hash_mask)
5843 *_hash_mask = (1 << log2qty) - 1;
5844
5845 return table;
5846}
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -08005847
Mel Gorman835c1342007-10-16 01:25:47 -07005848/* Return a pointer to the bitmap storing bits affecting a block of pages */
5849static inline unsigned long *get_pageblock_bitmap(struct zone *zone,
5850 unsigned long pfn)
5851{
5852#ifdef CONFIG_SPARSEMEM
5853 return __pfn_to_section(pfn)->pageblock_flags;
5854#else
5855 return zone->pageblock_flags;
5856#endif /* CONFIG_SPARSEMEM */
5857}
Andrew Morton6220ec72006-10-19 23:29:05 -07005858
Mel Gorman835c1342007-10-16 01:25:47 -07005859static inline int pfn_to_bitidx(struct zone *zone, unsigned long pfn)
5860{
5861#ifdef CONFIG_SPARSEMEM
5862 pfn &= (PAGES_PER_SECTION-1);
Mel Gormand9c23402007-10-16 01:26:01 -07005863 return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
Mel Gorman835c1342007-10-16 01:25:47 -07005864#else
Laura Abbottc060f942013-01-11 14:31:51 -08005865 pfn = pfn - round_down(zone->zone_start_pfn, pageblock_nr_pages);
Mel Gormand9c23402007-10-16 01:26:01 -07005866 return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
Mel Gorman835c1342007-10-16 01:25:47 -07005867#endif /* CONFIG_SPARSEMEM */
5868}
5869
5870/**
Mel Gormand9c23402007-10-16 01:26:01 -07005871 * 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 -07005872 * @page: The page within the block of interest
5873 * @start_bitidx: The first bit of interest to retrieve
5874 * @end_bitidx: The last bit of interest
5875 * returns pageblock_bits flags
5876 */
5877unsigned long get_pageblock_flags_group(struct page *page,
5878 int start_bitidx, int end_bitidx)
5879{
5880 struct zone *zone;
5881 unsigned long *bitmap;
5882 unsigned long pfn, bitidx;
5883 unsigned long flags = 0;
5884 unsigned long value = 1;
5885
5886 zone = page_zone(page);
5887 pfn = page_to_pfn(page);
5888 bitmap = get_pageblock_bitmap(zone, pfn);
5889 bitidx = pfn_to_bitidx(zone, pfn);
5890
5891 for (; start_bitidx <= end_bitidx; start_bitidx++, value <<= 1)
5892 if (test_bit(bitidx + start_bitidx, bitmap))
5893 flags |= value;
5894
5895 return flags;
5896}
5897
5898/**
Mel Gormand9c23402007-10-16 01:26:01 -07005899 * 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 -07005900 * @page: The page within the block of interest
5901 * @start_bitidx: The first bit of interest
5902 * @end_bitidx: The last bit of interest
5903 * @flags: The flags to set
5904 */
5905void set_pageblock_flags_group(struct page *page, unsigned long flags,
5906 int start_bitidx, int end_bitidx)
5907{
5908 struct zone *zone;
5909 unsigned long *bitmap;
5910 unsigned long pfn, bitidx;
5911 unsigned long value = 1;
5912
5913 zone = page_zone(page);
5914 pfn = page_to_pfn(page);
5915 bitmap = get_pageblock_bitmap(zone, pfn);
5916 bitidx = pfn_to_bitidx(zone, pfn);
Cody P Schafer108bcc92013-02-22 16:35:23 -08005917 VM_BUG_ON(!zone_spans_pfn(zone, pfn));
Mel Gorman835c1342007-10-16 01:25:47 -07005918
5919 for (; start_bitidx <= end_bitidx; start_bitidx++, value <<= 1)
5920 if (flags & value)
5921 __set_bit(bitidx + start_bitidx, bitmap);
5922 else
5923 __clear_bit(bitidx + start_bitidx, bitmap);
5924}
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -07005925
5926/*
Minchan Kim80934512012-07-31 16:43:01 -07005927 * This function checks whether pageblock includes unmovable pages or not.
5928 * If @count is not zero, it is okay to include less @count unmovable pages
5929 *
Pintu Kumarb8af2942013-09-11 14:20:34 -07005930 * PageLRU check without isolation or lru_lock could race so that
Minchan Kim80934512012-07-31 16:43:01 -07005931 * MIGRATE_MOVABLE block might include unmovable pages. It means you can't
5932 * expect this function should be exact.
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -07005933 */
Wen Congyangb023f462012-12-11 16:00:45 -08005934bool has_unmovable_pages(struct zone *zone, struct page *page, int count,
5935 bool skip_hwpoisoned_pages)
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005936{
5937 unsigned long pfn, iter, found;
Michal Nazarewicz47118af2011-12-29 13:09:50 +01005938 int mt;
5939
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005940 /*
5941 * For avoiding noise data, lru_add_drain_all() should be called
Minchan Kim80934512012-07-31 16:43:01 -07005942 * If ZONE_MOVABLE, the zone never contains unmovable pages
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005943 */
5944 if (zone_idx(zone) == ZONE_MOVABLE)
Minchan Kim80934512012-07-31 16:43:01 -07005945 return false;
Michal Nazarewicz47118af2011-12-29 13:09:50 +01005946 mt = get_pageblock_migratetype(page);
5947 if (mt == MIGRATE_MOVABLE || is_migrate_cma(mt))
Minchan Kim80934512012-07-31 16:43:01 -07005948 return false;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005949
5950 pfn = page_to_pfn(page);
5951 for (found = 0, iter = 0; iter < pageblock_nr_pages; iter++) {
5952 unsigned long check = pfn + iter;
5953
Namhyung Kim29723fc2011-02-25 14:44:25 -08005954 if (!pfn_valid_within(check))
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005955 continue;
Namhyung Kim29723fc2011-02-25 14:44:25 -08005956
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005957 page = pfn_to_page(check);
Minchan Kim97d255c2012-07-31 16:42:59 -07005958 /*
5959 * We can't use page_count without pin a page
5960 * because another CPU can free compound page.
5961 * This check already skips compound tails of THP
5962 * because their page->_count is zero at all time.
5963 */
5964 if (!atomic_read(&page->_count)) {
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005965 if (PageBuddy(page))
5966 iter += (1 << page_order(page)) - 1;
5967 continue;
5968 }
Minchan Kim97d255c2012-07-31 16:42:59 -07005969
Wen Congyangb023f462012-12-11 16:00:45 -08005970 /*
5971 * The HWPoisoned page may be not in buddy system, and
5972 * page_count() is not 0.
5973 */
5974 if (skip_hwpoisoned_pages && PageHWPoison(page))
5975 continue;
5976
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005977 if (!PageLRU(page))
5978 found++;
5979 /*
5980 * If there are RECLAIMABLE pages, we need to check it.
5981 * But now, memory offline itself doesn't call shrink_slab()
5982 * and it still to be fixed.
5983 */
5984 /*
5985 * If the page is not RAM, page_count()should be 0.
5986 * we don't need more check. This is an _used_ not-movable page.
5987 *
5988 * The problematic thing here is PG_reserved pages. PG_reserved
5989 * is set to both of a memory hole page and a _used_ kernel
5990 * page at boot.
5991 */
5992 if (found > count)
Minchan Kim80934512012-07-31 16:43:01 -07005993 return true;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005994 }
Minchan Kim80934512012-07-31 16:43:01 -07005995 return false;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005996}
5997
5998bool is_pageblock_removable_nolock(struct page *page)
5999{
Michal Hocko656a0702012-01-20 14:33:58 -08006000 struct zone *zone;
6001 unsigned long pfn;
Michal Hocko687875f2012-01-20 14:33:55 -08006002
6003 /*
6004 * We have to be careful here because we are iterating over memory
6005 * sections which are not zone aware so we might end up outside of
6006 * the zone but still within the section.
Michal Hocko656a0702012-01-20 14:33:58 -08006007 * We have to take care about the node as well. If the node is offline
6008 * its NODE_DATA will be NULL - see page_zone.
Michal Hocko687875f2012-01-20 14:33:55 -08006009 */
Michal Hocko656a0702012-01-20 14:33:58 -08006010 if (!node_online(page_to_nid(page)))
6011 return false;
6012
6013 zone = page_zone(page);
6014 pfn = page_to_pfn(page);
Cody P Schafer108bcc92013-02-22 16:35:23 -08006015 if (!zone_spans_pfn(zone, pfn))
Michal Hocko687875f2012-01-20 14:33:55 -08006016 return false;
6017
Wen Congyangb023f462012-12-11 16:00:45 -08006018 return !has_unmovable_pages(zone, page, 0, true);
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -07006019}
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07006020
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006021#ifdef CONFIG_CMA
6022
6023static unsigned long pfn_max_align_down(unsigned long pfn)
6024{
6025 return pfn & ~(max_t(unsigned long, MAX_ORDER_NR_PAGES,
6026 pageblock_nr_pages) - 1);
6027}
6028
6029static unsigned long pfn_max_align_up(unsigned long pfn)
6030{
6031 return ALIGN(pfn, max_t(unsigned long, MAX_ORDER_NR_PAGES,
6032 pageblock_nr_pages));
6033}
6034
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006035/* [start, end) must belong to a single zone. */
Mel Gormanbb13ffe2012-10-08 16:32:41 -07006036static int __alloc_contig_migrate_range(struct compact_control *cc,
6037 unsigned long start, unsigned long end)
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006038{
6039 /* This function is based on compact_zone() from compaction.c. */
Minchan Kimbeb51ea2012-10-08 16:33:51 -07006040 unsigned long nr_reclaimed;
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006041 unsigned long pfn = start;
6042 unsigned int tries = 0;
6043 int ret = 0;
6044
Marek Szyprowskibe49a6e2012-12-12 13:51:19 -08006045 migrate_prep();
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006046
Mel Gormanbb13ffe2012-10-08 16:32:41 -07006047 while (pfn < end || !list_empty(&cc->migratepages)) {
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006048 if (fatal_signal_pending(current)) {
6049 ret = -EINTR;
6050 break;
6051 }
6052
Mel Gormanbb13ffe2012-10-08 16:32:41 -07006053 if (list_empty(&cc->migratepages)) {
6054 cc->nr_migratepages = 0;
6055 pfn = isolate_migratepages_range(cc->zone, cc,
Minchan Kime46a2872012-10-08 16:33:48 -07006056 pfn, end, true);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006057 if (!pfn) {
6058 ret = -EINTR;
6059 break;
6060 }
6061 tries = 0;
6062 } else if (++tries == 5) {
6063 ret = ret < 0 ? ret : -EBUSY;
6064 break;
6065 }
6066
Minchan Kimbeb51ea2012-10-08 16:33:51 -07006067 nr_reclaimed = reclaim_clean_pages_from_list(cc->zone,
6068 &cc->migratepages);
6069 cc->nr_migratepages -= nr_reclaimed;
Minchan Kim02c6de82012-10-08 16:31:55 -07006070
Hugh Dickins9c620e22013-02-22 16:35:14 -08006071 ret = migrate_pages(&cc->migratepages, alloc_migrate_target,
6072 0, MIGRATE_SYNC, MR_CMA);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006073 }
Srinivas Pandruvada2a6f5122013-02-22 16:32:09 -08006074 if (ret < 0) {
6075 putback_movable_pages(&cc->migratepages);
6076 return ret;
6077 }
6078 return 0;
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006079}
6080
6081/**
6082 * alloc_contig_range() -- tries to allocate given range of pages
6083 * @start: start PFN to allocate
6084 * @end: one-past-the-last PFN to allocate
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02006085 * @migratetype: migratetype of the underlaying pageblocks (either
6086 * #MIGRATE_MOVABLE or #MIGRATE_CMA). All pageblocks
6087 * in range must have the same migratetype and it must
6088 * be either of the two.
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006089 *
6090 * The PFN range does not have to be pageblock or MAX_ORDER_NR_PAGES
6091 * aligned, however it's the caller's responsibility to guarantee that
6092 * we are the only thread that changes migrate type of pageblocks the
6093 * pages fall in.
6094 *
6095 * The PFN range must belong to a single zone.
6096 *
6097 * Returns zero on success or negative error code. On success all
6098 * pages which PFN is in [start, end) are allocated for the caller and
6099 * need to be freed with free_contig_range().
6100 */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02006101int alloc_contig_range(unsigned long start, unsigned long end,
6102 unsigned migratetype)
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006103{
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006104 unsigned long outer_start, outer_end;
6105 int ret = 0, order;
6106
Mel Gormanbb13ffe2012-10-08 16:32:41 -07006107 struct compact_control cc = {
6108 .nr_migratepages = 0,
6109 .order = -1,
6110 .zone = page_zone(pfn_to_page(start)),
6111 .sync = true,
6112 .ignore_skip_hint = true,
6113 };
6114 INIT_LIST_HEAD(&cc.migratepages);
6115
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006116 /*
6117 * What we do here is we mark all pageblocks in range as
6118 * MIGRATE_ISOLATE. Because pageblock and max order pages may
6119 * have different sizes, and due to the way page allocator
6120 * work, we align the range to biggest of the two pages so
6121 * that page allocator won't try to merge buddies from
6122 * different pageblocks and change MIGRATE_ISOLATE to some
6123 * other migration type.
6124 *
6125 * Once the pageblocks are marked as MIGRATE_ISOLATE, we
6126 * migrate the pages from an unaligned range (ie. pages that
6127 * we are interested in). This will put all the pages in
6128 * range back to page allocator as MIGRATE_ISOLATE.
6129 *
6130 * When this is done, we take the pages in range from page
6131 * allocator removing them from the buddy system. This way
6132 * page allocator will never consider using them.
6133 *
6134 * This lets us mark the pageblocks back as
6135 * MIGRATE_CMA/MIGRATE_MOVABLE so that free pages in the
6136 * aligned range but not in the unaligned, original range are
6137 * put back to page allocator so that buddy can use them.
6138 */
6139
6140 ret = start_isolate_page_range(pfn_max_align_down(start),
Wen Congyangb023f462012-12-11 16:00:45 -08006141 pfn_max_align_up(end), migratetype,
6142 false);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006143 if (ret)
Bob Liu86a595f2012-10-25 13:37:56 -07006144 return ret;
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006145
Mel Gormanbb13ffe2012-10-08 16:32:41 -07006146 ret = __alloc_contig_migrate_range(&cc, start, end);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006147 if (ret)
6148 goto done;
6149
6150 /*
6151 * Pages from [start, end) are within a MAX_ORDER_NR_PAGES
6152 * aligned blocks that are marked as MIGRATE_ISOLATE. What's
6153 * more, all pages in [start, end) are free in page allocator.
6154 * What we are going to do is to allocate all pages from
6155 * [start, end) (that is remove them from page allocator).
6156 *
6157 * The only problem is that pages at the beginning and at the
6158 * end of interesting range may be not aligned with pages that
6159 * page allocator holds, ie. they can be part of higher order
6160 * pages. Because of this, we reserve the bigger range and
6161 * once this is done free the pages we are not interested in.
6162 *
6163 * We don't have to hold zone->lock here because the pages are
6164 * isolated thus they won't get removed from buddy.
6165 */
6166
6167 lru_add_drain_all();
6168 drain_all_pages();
6169
6170 order = 0;
6171 outer_start = start;
6172 while (!PageBuddy(pfn_to_page(outer_start))) {
6173 if (++order >= MAX_ORDER) {
6174 ret = -EBUSY;
6175 goto done;
6176 }
6177 outer_start &= ~0UL << order;
6178 }
6179
6180 /* Make sure the range is really isolated. */
Wen Congyangb023f462012-12-11 16:00:45 -08006181 if (test_pages_isolated(outer_start, end, false)) {
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006182 pr_warn("alloc_contig_range test_pages_isolated(%lx, %lx) failed\n",
6183 outer_start, end);
6184 ret = -EBUSY;
6185 goto done;
6186 }
6187
Marek Szyprowski49f223a2012-01-25 12:49:24 +01006188
6189 /* Grab isolated pages from freelists. */
Mel Gormanbb13ffe2012-10-08 16:32:41 -07006190 outer_end = isolate_freepages_range(&cc, outer_start, end);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006191 if (!outer_end) {
6192 ret = -EBUSY;
6193 goto done;
6194 }
6195
6196 /* Free head and tail (if any) */
6197 if (start != outer_start)
6198 free_contig_range(outer_start, start - outer_start);
6199 if (end != outer_end)
6200 free_contig_range(end, outer_end - end);
6201
6202done:
6203 undo_isolate_page_range(pfn_max_align_down(start),
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02006204 pfn_max_align_up(end), migratetype);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006205 return ret;
6206}
6207
6208void free_contig_range(unsigned long pfn, unsigned nr_pages)
6209{
Marek Szyprowskibcc2b022012-12-20 15:05:18 -08006210 unsigned int count = 0;
6211
6212 for (; nr_pages--; pfn++) {
6213 struct page *page = pfn_to_page(pfn);
6214
6215 count += page_count(page) != 1;
6216 __free_page(page);
6217 }
6218 WARN(count != 0, "%d pages are still in use!\n", count);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006219}
6220#endif
6221
Jiang Liu4ed7e022012-07-31 16:43:35 -07006222#ifdef CONFIG_MEMORY_HOTPLUG
Cody P Schafer0a647f32013-07-03 15:01:33 -07006223/*
6224 * The zone indicated has a new number of managed_pages; batch sizes and percpu
6225 * page high values need to be recalulated.
6226 */
Jiang Liu4ed7e022012-07-31 16:43:35 -07006227void __meminit zone_pcp_update(struct zone *zone)
6228{
Cody P Schafer0a647f32013-07-03 15:01:33 -07006229 unsigned cpu;
Cody P Schaferc8e251f2013-07-03 15:01:29 -07006230 mutex_lock(&pcp_batch_high_lock);
Cody P Schafer0a647f32013-07-03 15:01:33 -07006231 for_each_possible_cpu(cpu)
Cody P Schafer169f6c12013-07-03 15:01:41 -07006232 pageset_set_high_and_batch(zone,
6233 per_cpu_ptr(zone->pageset, cpu));
Cody P Schaferc8e251f2013-07-03 15:01:29 -07006234 mutex_unlock(&pcp_batch_high_lock);
Jiang Liu4ed7e022012-07-31 16:43:35 -07006235}
6236#endif
6237
Jiang Liu340175b2012-07-31 16:43:32 -07006238void zone_pcp_reset(struct zone *zone)
6239{
6240 unsigned long flags;
Minchan Kim5a883812012-10-08 16:33:39 -07006241 int cpu;
6242 struct per_cpu_pageset *pset;
Jiang Liu340175b2012-07-31 16:43:32 -07006243
6244 /* avoid races with drain_pages() */
6245 local_irq_save(flags);
6246 if (zone->pageset != &boot_pageset) {
Minchan Kim5a883812012-10-08 16:33:39 -07006247 for_each_online_cpu(cpu) {
6248 pset = per_cpu_ptr(zone->pageset, cpu);
6249 drain_zonestat(zone, pset);
6250 }
Jiang Liu340175b2012-07-31 16:43:32 -07006251 free_percpu(zone->pageset);
6252 zone->pageset = &boot_pageset;
6253 }
6254 local_irq_restore(flags);
6255}
6256
Wen Congyang6dcd73d2012-12-11 16:01:01 -08006257#ifdef CONFIG_MEMORY_HOTREMOVE
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07006258/*
6259 * All pages in the range must be isolated before calling this.
6260 */
6261void
6262__offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
6263{
6264 struct page *page;
6265 struct zone *zone;
6266 int order, i;
6267 unsigned long pfn;
6268 unsigned long flags;
6269 /* find the first valid pfn */
6270 for (pfn = start_pfn; pfn < end_pfn; pfn++)
6271 if (pfn_valid(pfn))
6272 break;
6273 if (pfn == end_pfn)
6274 return;
6275 zone = page_zone(pfn_to_page(pfn));
6276 spin_lock_irqsave(&zone->lock, flags);
6277 pfn = start_pfn;
6278 while (pfn < end_pfn) {
6279 if (!pfn_valid(pfn)) {
6280 pfn++;
6281 continue;
6282 }
6283 page = pfn_to_page(pfn);
Wen Congyangb023f462012-12-11 16:00:45 -08006284 /*
6285 * The HWPoisoned page may be not in buddy system, and
6286 * page_count() is not 0.
6287 */
6288 if (unlikely(!PageBuddy(page) && PageHWPoison(page))) {
6289 pfn++;
6290 SetPageReserved(page);
6291 continue;
6292 }
6293
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07006294 BUG_ON(page_count(page));
6295 BUG_ON(!PageBuddy(page));
6296 order = page_order(page);
6297#ifdef CONFIG_DEBUG_VM
6298 printk(KERN_INFO "remove from free list %lx %d %lx\n",
6299 pfn, 1 << order, end_pfn);
6300#endif
6301 list_del(&page->lru);
6302 rmv_page_order(page);
6303 zone->free_area[order].nr_free--;
Wanpeng Licea27eb2013-07-03 15:02:40 -07006304#ifdef CONFIG_HIGHMEM
6305 if (PageHighMem(page))
6306 totalhigh_pages -= 1 << order;
6307#endif
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07006308 for (i = 0; i < (1 << order); i++)
6309 SetPageReserved((page+i));
6310 pfn += (1 << order);
6311 }
6312 spin_unlock_irqrestore(&zone->lock, flags);
6313}
6314#endif
Wu Fengguang8d22ba12009-12-16 12:19:58 +01006315
6316#ifdef CONFIG_MEMORY_FAILURE
6317bool is_free_buddy_page(struct page *page)
6318{
6319 struct zone *zone = page_zone(page);
6320 unsigned long pfn = page_to_pfn(page);
6321 unsigned long flags;
6322 int order;
6323
6324 spin_lock_irqsave(&zone->lock, flags);
6325 for (order = 0; order < MAX_ORDER; order++) {
6326 struct page *page_head = page - (pfn & ((1 << order) - 1));
6327
6328 if (PageBuddy(page_head) && page_order(page_head) >= order)
6329 break;
6330 }
6331 spin_unlock_irqrestore(&zone->lock, flags);
6332
6333 return order < MAX_ORDER;
6334}
6335#endif
Wu Fengguang718a3822010-03-10 15:20:43 -08006336
Andrew Morton51300ce2012-05-29 15:06:44 -07006337static const struct trace_print_flags pageflag_names[] = {
Wu Fengguang718a3822010-03-10 15:20:43 -08006338 {1UL << PG_locked, "locked" },
6339 {1UL << PG_error, "error" },
6340 {1UL << PG_referenced, "referenced" },
6341 {1UL << PG_uptodate, "uptodate" },
6342 {1UL << PG_dirty, "dirty" },
6343 {1UL << PG_lru, "lru" },
6344 {1UL << PG_active, "active" },
6345 {1UL << PG_slab, "slab" },
6346 {1UL << PG_owner_priv_1, "owner_priv_1" },
6347 {1UL << PG_arch_1, "arch_1" },
6348 {1UL << PG_reserved, "reserved" },
6349 {1UL << PG_private, "private" },
6350 {1UL << PG_private_2, "private_2" },
6351 {1UL << PG_writeback, "writeback" },
6352#ifdef CONFIG_PAGEFLAGS_EXTENDED
6353 {1UL << PG_head, "head" },
6354 {1UL << PG_tail, "tail" },
6355#else
6356 {1UL << PG_compound, "compound" },
6357#endif
6358 {1UL << PG_swapcache, "swapcache" },
6359 {1UL << PG_mappedtodisk, "mappedtodisk" },
6360 {1UL << PG_reclaim, "reclaim" },
Wu Fengguang718a3822010-03-10 15:20:43 -08006361 {1UL << PG_swapbacked, "swapbacked" },
6362 {1UL << PG_unevictable, "unevictable" },
6363#ifdef CONFIG_MMU
6364 {1UL << PG_mlocked, "mlocked" },
6365#endif
6366#ifdef CONFIG_ARCH_USES_PG_UNCACHED
6367 {1UL << PG_uncached, "uncached" },
6368#endif
6369#ifdef CONFIG_MEMORY_FAILURE
6370 {1UL << PG_hwpoison, "hwpoison" },
6371#endif
Gavin Shanbe9cd872012-05-29 15:06:44 -07006372#ifdef CONFIG_TRANSPARENT_HUGEPAGE
6373 {1UL << PG_compound_lock, "compound_lock" },
6374#endif
Wu Fengguang718a3822010-03-10 15:20:43 -08006375};
6376
6377static void dump_page_flags(unsigned long flags)
6378{
6379 const char *delim = "";
6380 unsigned long mask;
6381 int i;
6382
Andrew Morton51300ce2012-05-29 15:06:44 -07006383 BUILD_BUG_ON(ARRAY_SIZE(pageflag_names) != __NR_PAGEFLAGS);
Johannes Weineracc50c12012-05-29 15:06:44 -07006384
Wu Fengguang718a3822010-03-10 15:20:43 -08006385 printk(KERN_ALERT "page flags: %#lx(", flags);
6386
6387 /* remove zone id */
6388 flags &= (1UL << NR_PAGEFLAGS) - 1;
6389
Andrew Morton51300ce2012-05-29 15:06:44 -07006390 for (i = 0; i < ARRAY_SIZE(pageflag_names) && flags; i++) {
Wu Fengguang718a3822010-03-10 15:20:43 -08006391
6392 mask = pageflag_names[i].mask;
6393 if ((flags & mask) != mask)
6394 continue;
6395
6396 flags &= ~mask;
6397 printk("%s%s", delim, pageflag_names[i].name);
6398 delim = "|";
6399 }
6400
6401 /* check for left over flags */
6402 if (flags)
6403 printk("%s%#lx", delim, flags);
6404
6405 printk(")\n");
6406}
6407
6408void dump_page(struct page *page)
6409{
6410 printk(KERN_ALERT
6411 "page:%p count:%d mapcount:%d mapping:%p index:%#lx\n",
Andrea Arcangeli4e9f64c2011-01-13 15:46:29 -08006412 page, atomic_read(&page->_count), page_mapcount(page),
Wu Fengguang718a3822010-03-10 15:20:43 -08006413 page->mapping, page->index);
6414 dump_page_flags(page->flags);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07006415 mem_cgroup_print_bad_page(page);
Wu Fengguang718a3822010-03-10 15:20:43 -08006416}