blob: 3f18a14effb81cb73e9eaf7fea26702198a29a1a [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#include <asm/tlbflush.h>
Andrew Mortonac924c62006-05-15 09:43:59 -070063#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include "internal.h"
65
Lee Schermerhorn72812012010-05-26 14:44:56 -070066#ifdef CONFIG_USE_PERCPU_NUMA_NODE_ID
67DEFINE_PER_CPU(int, numa_node);
68EXPORT_PER_CPU_SYMBOL(numa_node);
69#endif
70
Lee Schermerhorn7aac7892010-05-26 14:45:00 -070071#ifdef CONFIG_HAVE_MEMORYLESS_NODES
72/*
73 * N.B., Do NOT reference the '_numa_mem_' per cpu variable directly.
74 * It will not be defined when CONFIG_HAVE_MEMORYLESS_NODES is not defined.
75 * Use the accessor functions set_numa_mem(), numa_mem_id() and cpu_to_mem()
76 * defined in <linux/topology.h>.
77 */
78DEFINE_PER_CPU(int, _numa_mem_); /* Kernel "local memory" node */
79EXPORT_PER_CPU_SYMBOL(_numa_mem_);
80#endif
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/*
Christoph Lameter13808912007-10-16 01:25:27 -070083 * Array of node states.
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 */
Christoph Lameter13808912007-10-16 01:25:27 -070085nodemask_t node_states[NR_NODE_STATES] __read_mostly = {
86 [N_POSSIBLE] = NODE_MASK_ALL,
87 [N_ONLINE] = { { [0] = 1UL } },
88#ifndef CONFIG_NUMA
89 [N_NORMAL_MEMORY] = { { [0] = 1UL } },
90#ifdef CONFIG_HIGHMEM
91 [N_HIGH_MEMORY] = { { [0] = 1UL } },
92#endif
93 [N_CPU] = { { [0] = 1UL } },
94#endif /* NUMA */
95};
96EXPORT_SYMBOL(node_states);
97
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -070098unsigned long totalram_pages __read_mostly;
Hideo AOKIcb45b0e2006-04-10 22:52:59 -070099unsigned long totalreserve_pages __read_mostly;
Johannes Weinerab8fabd2012-01-10 15:07:42 -0800100/*
101 * When calculating the number of globally allowed dirty pages, there
102 * is a certain number of per-zone reserves that should not be
103 * considered dirtyable memory. This is the sum of those reserves
104 * over all existing zones that contribute dirtyable memory.
105 */
106unsigned long dirty_balance_reserve __read_mostly;
107
Hugh Dickins1b76b022012-05-11 01:00:07 -0700108int percpu_pagelist_fraction;
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +1000109gfp_t gfp_allowed_mask __read_mostly = GFP_BOOT_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800111#ifdef CONFIG_PM_SLEEP
112/*
113 * The following functions are used by the suspend/hibernate code to temporarily
114 * change gfp_allowed_mask in order to avoid using I/O during memory allocations
115 * while devices are suspended. To avoid races with the suspend/hibernate code,
116 * they should always be called with pm_mutex held (gfp_allowed_mask also should
117 * only be modified with pm_mutex held, unless the suspend/hibernate code is
118 * guaranteed not to run in parallel with that modification).
119 */
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100120
121static gfp_t saved_gfp_mask;
122
123void pm_restore_gfp_mask(void)
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800124{
125 WARN_ON(!mutex_is_locked(&pm_mutex));
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100126 if (saved_gfp_mask) {
127 gfp_allowed_mask = saved_gfp_mask;
128 saved_gfp_mask = 0;
129 }
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800130}
131
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100132void pm_restrict_gfp_mask(void)
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800133{
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800134 WARN_ON(!mutex_is_locked(&pm_mutex));
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100135 WARN_ON(saved_gfp_mask);
136 saved_gfp_mask = gfp_allowed_mask;
137 gfp_allowed_mask &= ~GFP_IOFS;
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800138}
Mel Gormanf90ac392012-01-10 15:07:15 -0800139
140bool pm_suspended_storage(void)
141{
142 if ((gfp_allowed_mask & GFP_IOFS) == GFP_IOFS)
143 return false;
144 return true;
145}
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800146#endif /* CONFIG_PM_SLEEP */
147
Mel Gormand9c23402007-10-16 01:26:01 -0700148#ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
149int pageblock_order __read_mostly;
150#endif
151
Hugh Dickinsd98c7a02006-02-14 13:52:59 -0800152static void __free_pages_ok(struct page *page, unsigned int order);
David Howellsa226f6c2006-01-06 00:11:08 -0800153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154/*
155 * results with 256, 32 in the lowmem_reserve sysctl:
156 * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
157 * 1G machine -> (16M dma, 784M normal, 224M high)
158 * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
159 * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
160 * HIGHMEM allocation will (224M+784M)/256 of ram reserved in ZONE_DMA
Andi Kleena2f1b422005-11-05 17:25:53 +0100161 *
162 * TBD: should special case ZONE_DMA32 machines here - in those we normally
163 * don't need any ZONE_NORMAL reservation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 */
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700165int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1] = {
Christoph Lameter4b51d662007-02-10 01:43:10 -0800166#ifdef CONFIG_ZONE_DMA
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700167 256,
Christoph Lameter4b51d662007-02-10 01:43:10 -0800168#endif
Christoph Lameterfb0e7942006-09-25 23:31:13 -0700169#ifdef CONFIG_ZONE_DMA32
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700170 256,
Christoph Lameterfb0e7942006-09-25 23:31:13 -0700171#endif
Christoph Lametere53ef382006-09-25 23:31:14 -0700172#ifdef CONFIG_HIGHMEM
Mel Gorman2a1e2742007-07-17 04:03:12 -0700173 32,
Christoph Lametere53ef382006-09-25 23:31:14 -0700174#endif
Mel Gorman2a1e2742007-07-17 04:03:12 -0700175 32,
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700176};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178EXPORT_SYMBOL(totalram_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Helge Deller15ad7cd2006-12-06 20:40:36 -0800180static char * const zone_names[MAX_NR_ZONES] = {
Christoph Lameter4b51d662007-02-10 01:43:10 -0800181#ifdef CONFIG_ZONE_DMA
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700182 "DMA",
Christoph Lameter4b51d662007-02-10 01:43:10 -0800183#endif
Christoph Lameterfb0e7942006-09-25 23:31:13 -0700184#ifdef CONFIG_ZONE_DMA32
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700185 "DMA32",
Christoph Lameterfb0e7942006-09-25 23:31:13 -0700186#endif
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700187 "Normal",
Christoph Lametere53ef382006-09-25 23:31:14 -0700188#ifdef CONFIG_HIGHMEM
Mel Gorman2a1e2742007-07-17 04:03:12 -0700189 "HighMem",
Christoph Lametere53ef382006-09-25 23:31:14 -0700190#endif
Mel Gorman2a1e2742007-07-17 04:03:12 -0700191 "Movable",
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700192};
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194int min_free_kbytes = 1024;
195
Jan Beulich2c85f512009-09-21 17:03:07 -0700196static unsigned long __meminitdata nr_kernel_pages;
197static unsigned long __meminitdata nr_all_pages;
Yasunori Gotoa3142c82007-05-08 00:23:07 -0700198static unsigned long __meminitdata dma_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Tejun Heo0ee332c2011-12-08 10:22:09 -0800200#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
201static unsigned long __meminitdata arch_zone_lowest_possible_pfn[MAX_NR_ZONES];
202static unsigned long __meminitdata arch_zone_highest_possible_pfn[MAX_NR_ZONES];
203static unsigned long __initdata required_kernelcore;
204static unsigned long __initdata required_movablecore;
205static unsigned long __meminitdata zone_movable_pfn[MAX_NUMNODES];
Mel Gormanc7132162006-09-27 01:49:43 -0700206
Tejun Heo0ee332c2011-12-08 10:22:09 -0800207/* movable_zone is the "real" zone pages in ZONE_MOVABLE are taken from */
208int movable_zone;
209EXPORT_SYMBOL(movable_zone);
210#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
Mel Gormanc7132162006-09-27 01:49:43 -0700211
Miklos Szeredi418508c2007-05-23 13:57:55 -0700212#if MAX_NUMNODES > 1
213int nr_node_ids __read_mostly = MAX_NUMNODES;
Christoph Lameter62bc62a2009-06-16 15:32:15 -0700214int nr_online_nodes __read_mostly = 1;
Miklos Szeredi418508c2007-05-23 13:57:55 -0700215EXPORT_SYMBOL(nr_node_ids);
Christoph Lameter62bc62a2009-06-16 15:32:15 -0700216EXPORT_SYMBOL(nr_online_nodes);
Miklos Szeredi418508c2007-05-23 13:57:55 -0700217#endif
218
Mel Gorman9ef9acb2007-10-16 01:25:54 -0700219int page_group_by_mobility_disabled __read_mostly;
220
Minchan Kim702d1a62012-07-31 16:43:56 -0700221/*
222 * NOTE:
223 * Don't use set_pageblock_migratetype(page, MIGRATE_ISOLATE) directly.
224 * Instead, use {un}set_pageblock_isolate.
225 */
Minchan Kimee6f5092012-07-31 16:43:50 -0700226void set_pageblock_migratetype(struct page *page, int migratetype)
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700227{
Mel Gorman49255c62009-06-16 15:31:58 -0700228
229 if (unlikely(page_group_by_mobility_disabled))
230 migratetype = MIGRATE_UNMOVABLE;
231
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700232 set_pageblock_flags_group(page, (unsigned long)migratetype,
233 PB_migrate, PB_migrate_end);
234}
235
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700236bool oom_killer_disabled __read_mostly;
237
Nick Piggin13e74442006-01-06 00:10:58 -0800238#ifdef CONFIG_DEBUG_VM
Dave Hansenc6a57e12005-10-29 18:16:52 -0700239static int page_outside_zone_boundaries(struct zone *zone, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Dave Hansenbdc8cb92005-10-29 18:16:53 -0700241 int ret = 0;
242 unsigned seq;
243 unsigned long pfn = page_to_pfn(page);
Dave Hansenc6a57e12005-10-29 18:16:52 -0700244
Dave Hansenbdc8cb92005-10-29 18:16:53 -0700245 do {
246 seq = zone_span_seqbegin(zone);
247 if (pfn >= zone->zone_start_pfn + zone->spanned_pages)
248 ret = 1;
249 else if (pfn < zone->zone_start_pfn)
250 ret = 1;
251 } while (zone_span_seqretry(zone, seq));
252
253 return ret;
Dave Hansenc6a57e12005-10-29 18:16:52 -0700254}
255
256static int page_is_consistent(struct zone *zone, struct page *page)
257{
Andy Whitcroft14e07292007-05-06 14:49:14 -0700258 if (!pfn_valid_within(page_to_pfn(page)))
Dave Hansenc6a57e12005-10-29 18:16:52 -0700259 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (zone != page_zone(page))
Dave Hansenc6a57e12005-10-29 18:16:52 -0700261 return 0;
262
263 return 1;
264}
265/*
266 * Temporary debugging check for pages not lying within a given zone.
267 */
268static int bad_range(struct zone *zone, struct page *page)
269{
270 if (page_outside_zone_boundaries(zone, page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return 1;
Dave Hansenc6a57e12005-10-29 18:16:52 -0700272 if (!page_is_consistent(zone, page))
273 return 1;
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return 0;
276}
Nick Piggin13e74442006-01-06 00:10:58 -0800277#else
278static inline int bad_range(struct zone *zone, struct page *page)
279{
280 return 0;
281}
282#endif
283
Nick Piggin224abf92006-01-06 00:11:11 -0800284static void bad_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800286 static unsigned long resume;
287 static unsigned long nr_shown;
288 static unsigned long nr_unshown;
289
Wu Fengguang2a7684a2009-09-16 11:50:12 +0200290 /* Don't complain about poisoned pages */
291 if (PageHWPoison(page)) {
Andrea Arcangelief2b4b92011-03-18 00:16:35 +0100292 reset_page_mapcount(page); /* remove PageBuddy */
Wu Fengguang2a7684a2009-09-16 11:50:12 +0200293 return;
294 }
295
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800296 /*
297 * Allow a burst of 60 reports, then keep quiet for that minute;
298 * or allow a steady drip of one report per second.
299 */
300 if (nr_shown == 60) {
301 if (time_before(jiffies, resume)) {
302 nr_unshown++;
303 goto out;
304 }
305 if (nr_unshown) {
Hugh Dickins1e9e6362009-01-06 14:40:13 -0800306 printk(KERN_ALERT
307 "BUG: Bad page state: %lu messages suppressed\n",
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800308 nr_unshown);
309 nr_unshown = 0;
310 }
311 nr_shown = 0;
312 }
313 if (nr_shown++ == 0)
314 resume = jiffies + 60 * HZ;
315
Hugh Dickins1e9e6362009-01-06 14:40:13 -0800316 printk(KERN_ALERT "BUG: Bad page state in process %s pfn:%05lx\n",
Hugh Dickins3dc14742009-01-06 14:40:08 -0800317 current->comm, page_to_pfn(page));
Wu Fengguang718a3822010-03-10 15:20:43 -0800318 dump_page(page);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700319
Dave Jones4f318882011-10-31 17:07:24 -0700320 print_modules();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 dump_stack();
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800322out:
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800323 /* Leave bad fields for debug, except PageBuddy could make trouble */
Andrea Arcangelief2b4b92011-03-18 00:16:35 +0100324 reset_page_mapcount(page); /* remove PageBuddy */
Randy Dunlap9f158332005-09-13 01:25:16 -0700325 add_taint(TAINT_BAD_PAGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328/*
329 * Higher-order pages are called "compound pages". They are structured thusly:
330 *
331 * The first PAGE_SIZE page is called the "head page".
332 *
333 * The remaining PAGE_SIZE pages are called "tail pages".
334 *
Wang Sheng-Hui6416b9f2011-11-17 10:53:50 +0100335 * All pages have PG_compound set. All tail pages have their ->first_page
336 * pointing at the head page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 *
Hugh Dickins41d78ba2006-02-14 13:52:58 -0800338 * The first tail page's ->lru.next holds the address of the compound page's
339 * put_page() function. Its ->lru.prev holds the order of allocation.
340 * This usage means that zero-order pages may not be compound.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 */
Hugh Dickinsd98c7a02006-02-14 13:52:59 -0800342
343static void free_compound_page(struct page *page)
344{
Christoph Lameterd85f3382007-05-06 14:49:39 -0700345 __free_pages_ok(page, compound_order(page));
Hugh Dickinsd98c7a02006-02-14 13:52:59 -0800346}
347
Andi Kleen01ad1c02008-07-23 21:27:46 -0700348void prep_compound_page(struct page *page, unsigned long order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 int i;
351 int nr_pages = 1 << order;
352
Andy Whitcroft33f2ef82006-12-06 20:33:32 -0800353 set_compound_page_dtor(page, free_compound_page);
Christoph Lameterd85f3382007-05-06 14:49:39 -0700354 set_compound_order(page, order);
Christoph Lameter6d777952007-05-06 14:49:40 -0700355 __SetPageHead(page);
Andy Whitcroft18229df2008-11-06 12:53:27 -0800356 for (i = 1; i < nr_pages; i++) {
357 struct page *p = page + i;
Christoph Lameterd85f3382007-05-06 14:49:39 -0700358 __SetPageTail(p);
Youquan Song58a84aa2011-12-08 14:34:18 -0800359 set_page_count(p, 0);
Christoph Lameterd85f3382007-05-06 14:49:39 -0700360 p->first_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362}
363
Andrea Arcangeli59ff4212011-01-13 15:46:38 -0800364/* update __split_huge_page_refcount if you change this function */
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800365static int destroy_compound_page(struct page *page, unsigned long order)
Andy Whitcroft18229df2008-11-06 12:53:27 -0800366{
367 int i;
368 int nr_pages = 1 << order;
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800369 int bad = 0;
Andy Whitcroft18229df2008-11-06 12:53:27 -0800370
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800371 if (unlikely(compound_order(page) != order) ||
372 unlikely(!PageHead(page))) {
Nick Piggin224abf92006-01-06 00:11:11 -0800373 bad_page(page);
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800374 bad++;
375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Christoph Lameter6d777952007-05-06 14:49:40 -0700377 __ClearPageHead(page);
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800378
Andy Whitcroft18229df2008-11-06 12:53:27 -0800379 for (i = 1; i < nr_pages; i++) {
380 struct page *p = page + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Alexey Zaytseve713a212009-01-10 02:47:57 +0300382 if (unlikely(!PageTail(p) || (p->first_page != page))) {
Nick Piggin224abf92006-01-06 00:11:11 -0800383 bad_page(page);
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800384 bad++;
385 }
Christoph Lameterd85f3382007-05-06 14:49:39 -0700386 __ClearPageTail(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800388
389 return bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Nick Piggin17cf4402006-03-22 00:08:41 -0800392static inline void prep_zero_page(struct page *page, int order, gfp_t gfp_flags)
393{
394 int i;
395
Andrew Morton6626c5d2006-03-22 00:08:42 -0800396 /*
397 * clear_highpage() will use KM_USER0, so it's a bug to use __GFP_ZERO
398 * and __GFP_HIGHMEM from hard or soft interrupt context.
399 */
Nick Piggin725d7042006-09-25 23:30:55 -0700400 VM_BUG_ON((gfp_flags & __GFP_HIGHMEM) && in_interrupt());
Nick Piggin17cf4402006-03-22 00:08:41 -0800401 for (i = 0; i < (1 << order); i++)
402 clear_highpage(page + i);
403}
404
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -0800405#ifdef CONFIG_DEBUG_PAGEALLOC
406unsigned int _debug_guardpage_minorder;
407
408static int __init debug_guardpage_minorder_setup(char *buf)
409{
410 unsigned long res;
411
412 if (kstrtoul(buf, 10, &res) < 0 || res > MAX_ORDER / 2) {
413 printk(KERN_ERR "Bad debug_guardpage_minorder value\n");
414 return 0;
415 }
416 _debug_guardpage_minorder = res;
417 printk(KERN_INFO "Setting debug_guardpage_minorder to %lu\n", res);
418 return 0;
419}
420__setup("debug_guardpage_minorder=", debug_guardpage_minorder_setup);
421
422static inline void set_page_guard_flag(struct page *page)
423{
424 __set_bit(PAGE_DEBUG_FLAG_GUARD, &page->debug_flags);
425}
426
427static inline void clear_page_guard_flag(struct page *page)
428{
429 __clear_bit(PAGE_DEBUG_FLAG_GUARD, &page->debug_flags);
430}
431#else
432static inline void set_page_guard_flag(struct page *page) { }
433static inline void clear_page_guard_flag(struct page *page) { }
434#endif
435
Andrew Morton6aa3001b22006-04-18 22:20:52 -0700436static inline void set_page_order(struct page *page, int order)
437{
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700438 set_page_private(page, order);
Nick Piggin676165a2006-04-10 11:21:48 +1000439 __SetPageBuddy(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
442static inline void rmv_page_order(struct page *page)
443{
Nick Piggin676165a2006-04-10 11:21:48 +1000444 __ClearPageBuddy(page);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700445 set_page_private(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
448/*
449 * Locate the struct page for both the matching buddy in our
450 * pair (buddy1) and the combined O(n+1) page they form (page).
451 *
452 * 1) Any buddy B1 will have an order O twin B2 which satisfies
453 * the following equation:
454 * B2 = B1 ^ (1 << O)
455 * For example, if the starting buddy (buddy2) is #8 its order
456 * 1 buddy is #10:
457 * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
458 *
459 * 2) Any buddy B will have an order O+1 parent P which
460 * satisfies the following equation:
461 * P = B & ~(1 << O)
462 *
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200463 * Assumption: *_mem_map is contiguous at least up to MAX_ORDER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465static inline unsigned long
KyongHo Cho43506fa2011-01-13 15:47:24 -0800466__find_buddy_index(unsigned long page_idx, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
KyongHo Cho43506fa2011-01-13 15:47:24 -0800468 return page_idx ^ (1 << order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
471/*
472 * This function checks whether a page is free && is the buddy
473 * we can do coalesce a page and its buddy if
Nick Piggin13e74442006-01-06 00:10:58 -0800474 * (a) the buddy is not in a hole &&
Nick Piggin676165a2006-04-10 11:21:48 +1000475 * (b) the buddy is in the buddy system &&
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700476 * (c) a page and its buddy have the same order &&
477 * (d) a page and its buddy are in the same zone.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 *
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800479 * For recording whether a page is in the buddy system, we set ->_mapcount -2.
480 * Setting, clearing, and testing _mapcount -2 is serialized by zone->lock.
Nick Piggin676165a2006-04-10 11:21:48 +1000481 *
482 * For recording page's order, we use page_private(page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 */
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700484static inline int page_is_buddy(struct page *page, struct page *buddy,
485 int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Andy Whitcroft14e07292007-05-06 14:49:14 -0700487 if (!pfn_valid_within(page_to_pfn(buddy)))
Nick Piggin13e74442006-01-06 00:10:58 -0800488 return 0;
Nick Piggin13e74442006-01-06 00:10:58 -0800489
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700490 if (page_zone_id(page) != page_zone_id(buddy))
491 return 0;
492
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -0800493 if (page_is_guard(buddy) && page_order(buddy) == order) {
494 VM_BUG_ON(page_count(buddy) != 0);
495 return 1;
496 }
497
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700498 if (PageBuddy(buddy) && page_order(buddy) == order) {
Nick Piggina3af9c32009-06-16 15:32:10 -0700499 VM_BUG_ON(page_count(buddy) != 0);
Andrew Morton6aa3001b22006-04-18 22:20:52 -0700500 return 1;
Nick Piggin676165a2006-04-10 11:21:48 +1000501 }
Andrew Morton6aa3001b22006-04-18 22:20:52 -0700502 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505/*
506 * Freeing function for a buddy system allocator.
507 *
508 * The concept of a buddy system is to maintain direct-mapped table
509 * (containing bit values) for memory blocks of various "orders".
510 * The bottom level table contains the map for the smallest allocatable
511 * units of memory (here, pages), and each level above it describes
512 * pairs of units from the levels below, hence, "buddies".
513 * At a high level, all that happens here is marking the table entry
514 * at the bottom level available, and propagating the changes upward
515 * as necessary, plus some accounting needed to play nicely with other
516 * parts of the VM system.
517 * At each level, we keep a list of pages, which are heads of continuous
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800518 * free pages of length of (1 << order) and marked with _mapcount -2. Page's
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700519 * order is recorded in page_private(page) field.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 * So when we are allocating or freeing one, we can derive the state of the
Michal Nazarewicz5f63b722012-01-11 15:16:11 +0100521 * other. That is, if we allocate a small block, and both were
522 * free, the remainder of the region must be split into blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 * If a block is freed, and its buddy is also free, then this
Michal Nazarewicz5f63b722012-01-11 15:16:11 +0100524 * triggers coalescing into a block of larger size.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 *
526 * -- wli
527 */
528
Nick Piggin48db57f2006-01-08 01:00:42 -0800529static inline void __free_one_page(struct page *page,
Mel Gormaned0ae212009-06-16 15:32:07 -0700530 struct zone *zone, unsigned int order,
531 int migratetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 unsigned long page_idx;
Corrado Zoccolo6dda9d52010-05-24 14:31:54 -0700534 unsigned long combined_idx;
KyongHo Cho43506fa2011-01-13 15:47:24 -0800535 unsigned long uninitialized_var(buddy_idx);
Corrado Zoccolo6dda9d52010-05-24 14:31:54 -0700536 struct page *buddy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Nick Piggin224abf92006-01-06 00:11:11 -0800538 if (unlikely(PageCompound(page)))
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800539 if (unlikely(destroy_compound_page(page, order)))
540 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Mel Gormaned0ae212009-06-16 15:32:07 -0700542 VM_BUG_ON(migratetype == -1);
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 page_idx = page_to_pfn(page) & ((1 << MAX_ORDER) - 1);
545
Mel Gormanf2260e62009-06-16 15:32:13 -0700546 VM_BUG_ON(page_idx & ((1 << order) - 1));
Nick Piggin725d7042006-09-25 23:30:55 -0700547 VM_BUG_ON(bad_range(zone, page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 while (order < MAX_ORDER-1) {
KyongHo Cho43506fa2011-01-13 15:47:24 -0800550 buddy_idx = __find_buddy_index(page_idx, order);
551 buddy = page + (buddy_idx - page_idx);
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700552 if (!page_is_buddy(page, buddy, order))
Andy Whitcroft3c82d0c2008-07-23 21:27:11 -0700553 break;
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -0800554 /*
555 * Our buddy is free or it is CONFIG_DEBUG_PAGEALLOC guard page,
556 * merge with it and move up one order.
557 */
558 if (page_is_guard(buddy)) {
559 clear_page_guard_flag(buddy);
560 set_page_private(page, 0);
561 __mod_zone_page_state(zone, NR_FREE_PAGES, 1 << order);
562 } else {
563 list_del(&buddy->lru);
564 zone->free_area[order].nr_free--;
565 rmv_page_order(buddy);
566 }
KyongHo Cho43506fa2011-01-13 15:47:24 -0800567 combined_idx = buddy_idx & page_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 page = page + (combined_idx - page_idx);
569 page_idx = combined_idx;
570 order++;
571 }
572 set_page_order(page, order);
Corrado Zoccolo6dda9d52010-05-24 14:31:54 -0700573
574 /*
575 * If this is not the largest possible page, check if the buddy
576 * of the next-highest order is free. If it is, it's possible
577 * that pages are being freed that will coalesce soon. In case,
578 * that is happening, add the free page to the tail of the list
579 * so it's less likely to be used soon and more likely to be merged
580 * as a higher order page
581 */
Mel Gormanb7f50cf2010-10-26 14:21:11 -0700582 if ((order < MAX_ORDER-2) && pfn_valid_within(page_to_pfn(buddy))) {
Corrado Zoccolo6dda9d52010-05-24 14:31:54 -0700583 struct page *higher_page, *higher_buddy;
KyongHo Cho43506fa2011-01-13 15:47:24 -0800584 combined_idx = buddy_idx & page_idx;
585 higher_page = page + (combined_idx - page_idx);
586 buddy_idx = __find_buddy_index(combined_idx, order + 1);
Li Haifeng0ba8f2d2012-09-17 14:09:21 -0700587 higher_buddy = higher_page + (buddy_idx - combined_idx);
Corrado Zoccolo6dda9d52010-05-24 14:31:54 -0700588 if (page_is_buddy(higher_page, higher_buddy, order + 1)) {
589 list_add_tail(&page->lru,
590 &zone->free_area[order].free_list[migratetype]);
591 goto out;
592 }
593 }
594
595 list_add(&page->lru, &zone->free_area[order].free_list[migratetype]);
596out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 zone->free_area[order].nr_free++;
598}
599
KOSAKI Motohiro092cead2009-06-16 15:32:17 -0700600/*
601 * free_page_mlock() -- clean up attempts to free and mlocked() page.
602 * Page should not be on lru, so no need to fix that up.
603 * free_pages_check() will verify...
604 */
605static inline void free_page_mlock(struct page *page)
606{
KOSAKI Motohiro092cead2009-06-16 15:32:17 -0700607 __dec_zone_page_state(page, NR_MLOCK);
608 __count_vm_event(UNEVICTABLE_MLOCKFREED);
609}
KOSAKI Motohiro092cead2009-06-16 15:32:17 -0700610
Nick Piggin224abf92006-01-06 00:11:11 -0800611static inline int free_pages_check(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Nick Piggin92be2e332006-01-06 00:10:57 -0800613 if (unlikely(page_mapcount(page) |
614 (page->mapping != NULL) |
Nick Piggina3af9c32009-06-16 15:32:10 -0700615 (atomic_read(&page->_count) != 0) |
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -0700616 (page->flags & PAGE_FLAGS_CHECK_AT_FREE) |
617 (mem_cgroup_bad_page_check(page)))) {
Nick Piggin224abf92006-01-06 00:11:11 -0800618 bad_page(page);
Hugh Dickins79f4b7b2009-01-06 14:40:05 -0800619 return 1;
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800620 }
Hugh Dickins79f4b7b2009-01-06 14:40:05 -0800621 if (page->flags & PAGE_FLAGS_CHECK_AT_PREP)
622 page->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
623 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
626/*
Mel Gorman5f8dcc22009-09-21 17:03:19 -0700627 * Frees a number of pages from the PCP lists
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 * Assumes all pages on list are in same zone, and of same order.
Renaud Lienhart207f36e2005-09-10 00:26:59 -0700629 * count is the number of pages to free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 *
631 * If the zone was previously in an "all pages pinned" state then look to
632 * see if this freeing clears that state.
633 *
634 * And clear the zone's pages_scanned counter, to hold off the "all pages are
635 * pinned" detection logic.
636 */
Mel Gorman5f8dcc22009-09-21 17:03:19 -0700637static void free_pcppages_bulk(struct zone *zone, int count,
638 struct per_cpu_pages *pcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Mel Gorman5f8dcc22009-09-21 17:03:19 -0700640 int migratetype = 0;
Mel Gormana6f9edd2009-09-21 17:03:20 -0700641 int batch_free = 0;
Mel Gorman72853e22010-09-09 16:38:16 -0700642 int to_free = count;
Mel Gorman5f8dcc22009-09-21 17:03:19 -0700643
Nick Pigginc54ad302006-01-06 00:10:56 -0800644 spin_lock(&zone->lock);
KOSAKI Motohiro93e4a892010-03-05 13:41:55 -0800645 zone->all_unreclaimable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 zone->pages_scanned = 0;
Mel Gormanf2260e62009-06-16 15:32:13 -0700647
Mel Gorman72853e22010-09-09 16:38:16 -0700648 while (to_free) {
Nick Piggin48db57f2006-01-08 01:00:42 -0800649 struct page *page;
Mel Gorman5f8dcc22009-09-21 17:03:19 -0700650 struct list_head *list;
Nick Piggin48db57f2006-01-08 01:00:42 -0800651
Mel Gorman5f8dcc22009-09-21 17:03:19 -0700652 /*
Mel Gormana6f9edd2009-09-21 17:03:20 -0700653 * Remove pages from lists in a round-robin fashion. A
654 * batch_free count is maintained that is incremented when an
655 * empty list is encountered. This is so more pages are freed
656 * off fuller lists instead of spinning excessively around empty
657 * lists
Mel Gorman5f8dcc22009-09-21 17:03:19 -0700658 */
659 do {
Mel Gormana6f9edd2009-09-21 17:03:20 -0700660 batch_free++;
Mel Gorman5f8dcc22009-09-21 17:03:19 -0700661 if (++migratetype == MIGRATE_PCPTYPES)
662 migratetype = 0;
663 list = &pcp->lists[migratetype];
664 } while (list_empty(list));
665
Namhyung Kim1d168712011-03-22 16:32:45 -0700666 /* This is the only non-empty list. Free them all. */
667 if (batch_free == MIGRATE_PCPTYPES)
668 batch_free = to_free;
669
Mel Gormana6f9edd2009-09-21 17:03:20 -0700670 do {
Bartlomiej Zolnierkiewicz770c8aa2012-10-08 16:31:57 -0700671 int mt; /* migratetype of the to-be-freed page */
672
Mel Gormana6f9edd2009-09-21 17:03:20 -0700673 page = list_entry(list->prev, struct page, lru);
674 /* must delete as __free_one_page list manipulates */
675 list_del(&page->lru);
Bartlomiej Zolnierkiewicz770c8aa2012-10-08 16:31:57 -0700676 mt = page_private(page);
Hugh Dickinsa7016232010-01-29 17:46:34 +0000677 /* MIGRATE_MOVABLE list may include MIGRATE_RESERVEs */
Bartlomiej Zolnierkiewicz770c8aa2012-10-08 16:31:57 -0700678 __free_one_page(page, zone, 0, mt);
679 trace_mm_page_pcpu_drain(page, 0, mt);
Mel Gorman72853e22010-09-09 16:38:16 -0700680 } while (--to_free && --batch_free && !list_empty(list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
Mel Gorman72853e22010-09-09 16:38:16 -0700682 __mod_zone_page_state(zone, NR_FREE_PAGES, count);
Nick Pigginc54ad302006-01-06 00:10:56 -0800683 spin_unlock(&zone->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
Mel Gormaned0ae212009-06-16 15:32:07 -0700686static void free_one_page(struct zone *zone, struct page *page, int order,
687 int migratetype)
Nick Piggin48db57f2006-01-08 01:00:42 -0800688{
Christoph Lameter006d22d2006-09-25 23:31:48 -0700689 spin_lock(&zone->lock);
KOSAKI Motohiro93e4a892010-03-05 13:41:55 -0800690 zone->all_unreclaimable = 0;
Christoph Lameter006d22d2006-09-25 23:31:48 -0700691 zone->pages_scanned = 0;
Mel Gormanf2260e62009-06-16 15:32:13 -0700692
Mel Gormaned0ae212009-06-16 15:32:07 -0700693 __free_one_page(page, zone, order, migratetype);
Mel Gorman72853e22010-09-09 16:38:16 -0700694 __mod_zone_page_state(zone, NR_FREE_PAGES, 1 << order);
Christoph Lameter006d22d2006-09-25 23:31:48 -0700695 spin_unlock(&zone->lock);
Nick Piggin48db57f2006-01-08 01:00:42 -0800696}
697
KOSAKI Motohiroec95f532010-05-24 14:32:38 -0700698static bool free_pages_prepare(struct page *page, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 int i;
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800701 int bad = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Konstantin Khlebnikovb413d482012-01-10 15:07:09 -0800703 trace_mm_page_free(page, order);
Vegard Nossumb1eeab62008-11-25 16:55:53 +0100704 kmemcheck_free_shadow(page, order);
705
Andrea Arcangeli8dd60a32011-01-13 15:46:34 -0800706 if (PageAnon(page))
707 page->mapping = NULL;
708 for (i = 0; i < (1 << order); i++)
709 bad += free_pages_check(page + i);
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800710 if (bad)
KOSAKI Motohiroec95f532010-05-24 14:32:38 -0700711 return false;
Hugh Dickins689bceb2005-11-21 21:32:20 -0800712
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700713 if (!PageHighMem(page)) {
Nick Piggin9858db52006-10-11 01:21:30 -0700714 debug_check_no_locks_freed(page_address(page),PAGE_SIZE<<order);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700715 debug_check_no_obj_freed(page_address(page),
716 PAGE_SIZE << order);
717 }
Nick Piggindafb1362006-10-11 01:21:30 -0700718 arch_free_page(page, order);
Nick Piggin48db57f2006-01-08 01:00:42 -0800719 kernel_map_pages(page, 1 << order, 0);
Nick Piggindafb1362006-10-11 01:21:30 -0700720
KOSAKI Motohiroec95f532010-05-24 14:32:38 -0700721 return true;
722}
723
724static void __free_pages_ok(struct page *page, unsigned int order)
725{
726 unsigned long flags;
727 int wasMlocked = __TestClearPageMlocked(page);
728
729 if (!free_pages_prepare(page, order))
730 return;
731
Nick Pigginc54ad302006-01-06 00:10:56 -0800732 local_irq_save(flags);
Johannes Weinerc2773312009-06-19 19:30:56 +0200733 if (unlikely(wasMlocked))
Mel Gormanda456f12009-06-16 15:32:08 -0700734 free_page_mlock(page);
Christoph Lameterf8891e52006-06-30 01:55:45 -0700735 __count_vm_events(PGFREE, 1 << order);
Mel Gormaned0ae212009-06-16 15:32:07 -0700736 free_one_page(page_zone(page), page, order,
737 get_pageblock_migratetype(page));
Nick Pigginc54ad302006-01-06 00:10:56 -0800738 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
Yasunori Gotoaf370fb2008-07-23 21:28:17 -0700741void __meminit __free_pages_bootmem(struct page *page, unsigned int order)
David Howellsa226f6c2006-01-06 00:11:08 -0800742{
Johannes Weinerc3993072012-01-10 15:08:10 -0800743 unsigned int nr_pages = 1 << order;
744 unsigned int loop;
David Howellsa226f6c2006-01-06 00:11:08 -0800745
Johannes Weinerc3993072012-01-10 15:08:10 -0800746 prefetchw(page);
747 for (loop = 0; loop < nr_pages; loop++) {
748 struct page *p = &page[loop];
David Howellsa226f6c2006-01-06 00:11:08 -0800749
Johannes Weinerc3993072012-01-10 15:08:10 -0800750 if (loop + 1 < nr_pages)
751 prefetchw(p + 1);
752 __ClearPageReserved(p);
753 set_page_count(p, 0);
David Howellsa226f6c2006-01-06 00:11:08 -0800754 }
Johannes Weinerc3993072012-01-10 15:08:10 -0800755
756 set_page_refcounted(page);
757 __free_pages(page, order);
David Howellsa226f6c2006-01-06 00:11:08 -0800758}
759
Michal Nazarewicz47118af2011-12-29 13:09:50 +0100760#ifdef CONFIG_CMA
761/* Free whole pageblock and set it's migration type to MIGRATE_CMA. */
762void __init init_cma_reserved_pageblock(struct page *page)
763{
764 unsigned i = pageblock_nr_pages;
765 struct page *p = page;
766
767 do {
768 __ClearPageReserved(p);
769 set_page_count(p, 0);
770 } while (++p, --i);
771
772 set_page_refcounted(page);
773 set_pageblock_migratetype(page, MIGRATE_CMA);
774 __free_pages(page, pageblock_order);
775 totalram_pages += pageblock_nr_pages;
776}
777#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779/*
780 * The order of subdivision here is critical for the IO subsystem.
781 * Please do not alter this order without good reasons and regression
782 * testing. Specifically, as large blocks of memory are subdivided,
783 * the order in which smaller blocks are delivered depends on the order
784 * they're subdivided in this function. This is the primary factor
785 * influencing the order in which pages are delivered to the IO
786 * subsystem according to empirical testing, and this is also justified
787 * by considering the behavior of a buddy system containing a single
788 * large block of memory acted on by a series of small allocations.
789 * This behavior is a critical factor in sglist merging's success.
790 *
791 * -- wli
792 */
Nick Piggin085cc7d2006-01-06 00:11:01 -0800793static inline void expand(struct zone *zone, struct page *page,
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700794 int low, int high, struct free_area *area,
795 int migratetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 unsigned long size = 1 << high;
798
799 while (high > low) {
800 area--;
801 high--;
802 size >>= 1;
Nick Piggin725d7042006-09-25 23:30:55 -0700803 VM_BUG_ON(bad_range(zone, &page[size]));
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -0800804
805#ifdef CONFIG_DEBUG_PAGEALLOC
806 if (high < debug_guardpage_minorder()) {
807 /*
808 * Mark as guard pages (or page), that will allow to
809 * merge back to allocator when buddy will be freed.
810 * Corresponding page table entries will not be touched,
811 * pages will stay not present in virtual address space
812 */
813 INIT_LIST_HEAD(&page[size].lru);
814 set_page_guard_flag(&page[size]);
815 set_page_private(&page[size], high);
816 /* Guard pages are not available for any usage */
817 __mod_zone_page_state(zone, NR_FREE_PAGES, -(1 << high));
818 continue;
819 }
820#endif
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700821 list_add(&page[size].lru, &area->free_list[migratetype]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 area->nr_free++;
823 set_page_order(&page[size], high);
824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827/*
828 * This page is about to be returned from the page allocator
829 */
Wu Fengguang2a7684a2009-09-16 11:50:12 +0200830static inline int check_new_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Nick Piggin92be2e332006-01-06 00:10:57 -0800832 if (unlikely(page_mapcount(page) |
833 (page->mapping != NULL) |
Nick Piggina3af9c32009-06-16 15:32:10 -0700834 (atomic_read(&page->_count) != 0) |
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -0700835 (page->flags & PAGE_FLAGS_CHECK_AT_PREP) |
836 (mem_cgroup_bad_page_check(page)))) {
Nick Piggin224abf92006-01-06 00:11:11 -0800837 bad_page(page);
Hugh Dickins689bceb2005-11-21 21:32:20 -0800838 return 1;
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800839 }
Wu Fengguang2a7684a2009-09-16 11:50:12 +0200840 return 0;
841}
842
843static int prep_new_page(struct page *page, int order, gfp_t gfp_flags)
844{
845 int i;
846
847 for (i = 0; i < (1 << order); i++) {
848 struct page *p = page + i;
849 if (unlikely(check_new_page(p)))
850 return 1;
851 }
Hugh Dickins689bceb2005-11-21 21:32:20 -0800852
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700853 set_page_private(page, 0);
Nick Piggin7835e982006-03-22 00:08:40 -0800854 set_page_refcounted(page);
Nick Piggincc1025092006-12-06 20:32:00 -0800855
856 arch_alloc_page(page, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 kernel_map_pages(page, 1 << order, 1);
Nick Piggin17cf4402006-03-22 00:08:41 -0800858
859 if (gfp_flags & __GFP_ZERO)
860 prep_zero_page(page, order, gfp_flags);
861
862 if (order && (gfp_flags & __GFP_COMP))
863 prep_compound_page(page, order);
864
Hugh Dickins689bceb2005-11-21 21:32:20 -0800865 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
867
Mel Gorman56fd56b2007-10-16 01:25:58 -0700868/*
869 * Go through the free lists for the given migratetype and remove
870 * the smallest available page from the freelists
871 */
Mel Gorman728ec982009-06-16 15:32:04 -0700872static inline
873struct page *__rmqueue_smallest(struct zone *zone, unsigned int order,
Mel Gorman56fd56b2007-10-16 01:25:58 -0700874 int migratetype)
875{
876 unsigned int current_order;
877 struct free_area * area;
878 struct page *page;
879
880 /* Find a page of the appropriate size in the preferred list */
881 for (current_order = order; current_order < MAX_ORDER; ++current_order) {
882 area = &(zone->free_area[current_order]);
883 if (list_empty(&area->free_list[migratetype]))
884 continue;
885
886 page = list_entry(area->free_list[migratetype].next,
887 struct page, lru);
888 list_del(&page->lru);
889 rmv_page_order(page);
890 area->nr_free--;
Mel Gorman56fd56b2007-10-16 01:25:58 -0700891 expand(zone, page, order, current_order, area, migratetype);
892 return page;
893 }
894
895 return NULL;
896}
897
898
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700899/*
900 * This array describes the order lists are fallen back to when
901 * the free lists for the desirable migrate type are depleted
902 */
Michal Nazarewicz47118af2011-12-29 13:09:50 +0100903static int fallbacks[MIGRATE_TYPES][4] = {
904 [MIGRATE_UNMOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_MOVABLE, MIGRATE_RESERVE },
905 [MIGRATE_RECLAIMABLE] = { MIGRATE_UNMOVABLE, MIGRATE_MOVABLE, MIGRATE_RESERVE },
906#ifdef CONFIG_CMA
907 [MIGRATE_MOVABLE] = { MIGRATE_CMA, MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE, MIGRATE_RESERVE },
908 [MIGRATE_CMA] = { MIGRATE_RESERVE }, /* Never used */
909#else
910 [MIGRATE_MOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE, MIGRATE_RESERVE },
911#endif
Michal Nazarewicz6d4a4912012-01-11 15:31:33 +0100912 [MIGRATE_RESERVE] = { MIGRATE_RESERVE }, /* Never used */
913 [MIGRATE_ISOLATE] = { MIGRATE_RESERVE }, /* Never used */
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700914};
915
Mel Gormanc361be52007-10-16 01:25:51 -0700916/*
917 * Move the free pages in a range to the free lists of the requested type.
Mel Gormand9c23402007-10-16 01:26:01 -0700918 * Note that start_page and end_pages are not aligned on a pageblock
Mel Gormanc361be52007-10-16 01:25:51 -0700919 * boundary. If alignment is required, use move_freepages_block()
920 */
Adrian Bunkb69a7282008-07-23 21:28:12 -0700921static int move_freepages(struct zone *zone,
922 struct page *start_page, struct page *end_page,
923 int migratetype)
Mel Gormanc361be52007-10-16 01:25:51 -0700924{
925 struct page *page;
926 unsigned long order;
Mel Gormand1003132007-10-16 01:26:00 -0700927 int pages_moved = 0;
Mel Gormanc361be52007-10-16 01:25:51 -0700928
929#ifndef CONFIG_HOLES_IN_ZONE
930 /*
931 * page_zone is not safe to call in this context when
932 * CONFIG_HOLES_IN_ZONE is set. This bug check is probably redundant
933 * anyway as we check zone boundaries in move_freepages_block().
934 * Remove at a later date when no bug reports exist related to
Mel Gormanac0e5b72007-10-16 01:25:58 -0700935 * grouping pages by mobility
Mel Gormanc361be52007-10-16 01:25:51 -0700936 */
937 BUG_ON(page_zone(start_page) != page_zone(end_page));
938#endif
939
940 for (page = start_page; page <= end_page;) {
Adam Litke344c7902008-09-02 14:35:38 -0700941 /* Make sure we are not inadvertently changing nodes */
942 VM_BUG_ON(page_to_nid(page) != zone_to_nid(zone));
943
Mel Gormanc361be52007-10-16 01:25:51 -0700944 if (!pfn_valid_within(page_to_pfn(page))) {
945 page++;
946 continue;
947 }
948
949 if (!PageBuddy(page)) {
950 page++;
951 continue;
952 }
953
954 order = page_order(page);
Kirill A. Shutemov84be48d2011-03-22 16:33:41 -0700955 list_move(&page->lru,
956 &zone->free_area[order].free_list[migratetype]);
Mel Gormanc361be52007-10-16 01:25:51 -0700957 page += 1 << order;
Mel Gormand1003132007-10-16 01:26:00 -0700958 pages_moved += 1 << order;
Mel Gormanc361be52007-10-16 01:25:51 -0700959 }
960
Mel Gormand1003132007-10-16 01:26:00 -0700961 return pages_moved;
Mel Gormanc361be52007-10-16 01:25:51 -0700962}
963
Minchan Kimee6f5092012-07-31 16:43:50 -0700964int move_freepages_block(struct zone *zone, struct page *page,
Linus Torvalds68e3e922012-06-03 20:05:57 -0700965 int migratetype)
Mel Gormanc361be52007-10-16 01:25:51 -0700966{
967 unsigned long start_pfn, end_pfn;
968 struct page *start_page, *end_page;
969
970 start_pfn = page_to_pfn(page);
Mel Gormand9c23402007-10-16 01:26:01 -0700971 start_pfn = start_pfn & ~(pageblock_nr_pages-1);
Mel Gormanc361be52007-10-16 01:25:51 -0700972 start_page = pfn_to_page(start_pfn);
Mel Gormand9c23402007-10-16 01:26:01 -0700973 end_page = start_page + pageblock_nr_pages - 1;
974 end_pfn = start_pfn + pageblock_nr_pages - 1;
Mel Gormanc361be52007-10-16 01:25:51 -0700975
976 /* Do not cross zone boundaries */
977 if (start_pfn < zone->zone_start_pfn)
978 start_page = page;
979 if (end_pfn >= zone->zone_start_pfn + zone->spanned_pages)
980 return 0;
981
982 return move_freepages(zone, start_page, end_page, migratetype);
983}
984
Mel Gorman2f66a682009-09-21 17:02:31 -0700985static void change_pageblock_range(struct page *pageblock_page,
986 int start_order, int migratetype)
987{
988 int nr_pageblocks = 1 << (start_order - pageblock_order);
989
990 while (nr_pageblocks--) {
991 set_pageblock_migratetype(pageblock_page, migratetype);
992 pageblock_page += pageblock_nr_pages;
993 }
994}
995
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700996/* Remove an element from the buddy allocator from the fallback list */
Mel Gorman0ac3a402009-06-16 15:32:06 -0700997static inline struct page *
998__rmqueue_fallback(struct zone *zone, int order, int start_migratetype)
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700999{
1000 struct free_area * area;
1001 int current_order;
1002 struct page *page;
1003 int migratetype, i;
1004
1005 /* Find the largest possible block of pages in the other list */
1006 for (current_order = MAX_ORDER-1; current_order >= order;
1007 --current_order) {
Michal Nazarewicz6d4a4912012-01-11 15:31:33 +01001008 for (i = 0;; i++) {
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001009 migratetype = fallbacks[start_migratetype][i];
1010
Mel Gorman56fd56b2007-10-16 01:25:58 -07001011 /* MIGRATE_RESERVE handled later if necessary */
1012 if (migratetype == MIGRATE_RESERVE)
Michal Nazarewicz6d4a4912012-01-11 15:31:33 +01001013 break;
Mel Gormane0104872007-10-16 01:25:53 -07001014
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001015 area = &(zone->free_area[current_order]);
1016 if (list_empty(&area->free_list[migratetype]))
1017 continue;
1018
1019 page = list_entry(area->free_list[migratetype].next,
1020 struct page, lru);
1021 area->nr_free--;
1022
1023 /*
Mel Gormanc361be52007-10-16 01:25:51 -07001024 * If breaking a large block of pages, move all free
Mel Gorman46dafbc2007-10-16 01:25:55 -07001025 * pages to the preferred allocation list. If falling
1026 * back for a reclaimable kernel allocation, be more
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001027 * aggressive about taking ownership of free pages
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001028 *
1029 * On the other hand, never change migration
1030 * type of MIGRATE_CMA pageblocks nor move CMA
1031 * pages on different free lists. We don't
1032 * want unmovable pages to be allocated from
1033 * MIGRATE_CMA areas.
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001034 */
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001035 if (!is_migrate_cma(migratetype) &&
1036 (unlikely(current_order >= pageblock_order / 2) ||
1037 start_migratetype == MIGRATE_RECLAIMABLE ||
1038 page_group_by_mobility_disabled)) {
1039 int pages;
Mel Gorman46dafbc2007-10-16 01:25:55 -07001040 pages = move_freepages_block(zone, page,
1041 start_migratetype);
1042
1043 /* Claim the whole block if over half of it is free */
Mel Gormandd5d2412009-09-05 11:17:11 -07001044 if (pages >= (1 << (pageblock_order-1)) ||
1045 page_group_by_mobility_disabled)
Mel Gorman46dafbc2007-10-16 01:25:55 -07001046 set_pageblock_migratetype(page,
1047 start_migratetype);
1048
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001049 migratetype = start_migratetype;
Mel Gormanc361be52007-10-16 01:25:51 -07001050 }
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001051
1052 /* Remove the page from the freelists */
1053 list_del(&page->lru);
1054 rmv_page_order(page);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001055
Mel Gorman2f66a682009-09-21 17:02:31 -07001056 /* Take ownership for orders >= pageblock_order */
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001057 if (current_order >= pageblock_order &&
1058 !is_migrate_cma(migratetype))
Mel Gorman2f66a682009-09-21 17:02:31 -07001059 change_pageblock_range(page, current_order,
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001060 start_migratetype);
1061
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001062 expand(zone, page, order, current_order, area,
1063 is_migrate_cma(migratetype)
1064 ? migratetype : start_migratetype);
Mel Gormane0fff1b2009-09-21 17:02:42 -07001065
1066 trace_mm_page_alloc_extfrag(page, order, current_order,
1067 start_migratetype, migratetype);
1068
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001069 return page;
1070 }
1071 }
1072
Mel Gorman728ec982009-06-16 15:32:04 -07001073 return NULL;
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001074}
1075
Mel Gorman56fd56b2007-10-16 01:25:58 -07001076/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 * Do the hard work of removing an element from the buddy allocator.
1078 * Call me with the zone->lock already held.
1079 */
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001080static struct page *__rmqueue(struct zone *zone, unsigned int order,
1081 int migratetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 struct page *page;
1084
Mel Gorman728ec982009-06-16 15:32:04 -07001085retry_reserve:
Mel Gorman56fd56b2007-10-16 01:25:58 -07001086 page = __rmqueue_smallest(zone, order, migratetype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Mel Gorman728ec982009-06-16 15:32:04 -07001088 if (unlikely(!page) && migratetype != MIGRATE_RESERVE) {
Mel Gorman56fd56b2007-10-16 01:25:58 -07001089 page = __rmqueue_fallback(zone, order, migratetype);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001090
Mel Gorman728ec982009-06-16 15:32:04 -07001091 /*
1092 * Use MIGRATE_RESERVE rather than fail an allocation. goto
1093 * is used because __rmqueue_smallest is an inline function
1094 * and we want just one call site
1095 */
1096 if (!page) {
1097 migratetype = MIGRATE_RESERVE;
1098 goto retry_reserve;
1099 }
1100 }
1101
Mel Gorman0d3d0622009-09-21 17:02:44 -07001102 trace_mm_page_alloc_zone_locked(page, order, migratetype);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001103 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
1105
Michal Nazarewicz5f63b722012-01-11 15:16:11 +01001106/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 * Obtain a specified number of elements from the buddy allocator, all under
1108 * a single hold of the lock, for efficiency. Add them to the supplied list.
1109 * Returns the number of new pages which were placed at *list.
1110 */
Michal Nazarewicz5f63b722012-01-11 15:16:11 +01001111static int rmqueue_bulk(struct zone *zone, unsigned int order,
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001112 unsigned long count, struct list_head *list,
Mel Gormane084b2d2009-07-29 15:02:04 -07001113 int migratetype, int cold)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001115 int mt = migratetype, i;
Michal Nazarewicz5f63b722012-01-11 15:16:11 +01001116
Nick Pigginc54ad302006-01-06 00:10:56 -08001117 spin_lock(&zone->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 for (i = 0; i < count; ++i) {
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001119 struct page *page = __rmqueue(zone, order, migratetype);
Nick Piggin085cc7d2006-01-06 00:11:01 -08001120 if (unlikely(page == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 break;
Mel Gorman81eabcb2007-12-17 16:20:05 -08001122
1123 /*
1124 * Split buddy pages returned by expand() are received here
1125 * in physical page order. The page is added to the callers and
1126 * list and the list head then moves forward. From the callers
1127 * perspective, the linked list is ordered by page number in
1128 * some conditions. This is useful for IO devices that can
1129 * merge IO requests if the physical pages are ordered
1130 * properly.
1131 */
Mel Gormane084b2d2009-07-29 15:02:04 -07001132 if (likely(cold == 0))
1133 list_add(&page->lru, list);
1134 else
1135 list_add_tail(&page->lru, list);
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001136 if (IS_ENABLED(CONFIG_CMA)) {
1137 mt = get_pageblock_migratetype(page);
1138 if (!is_migrate_cma(mt) && mt != MIGRATE_ISOLATE)
1139 mt = migratetype;
1140 }
1141 set_page_private(page, mt);
Mel Gorman81eabcb2007-12-17 16:20:05 -08001142 list = &page->lru;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
Mel Gormanf2260e62009-06-16 15:32:13 -07001144 __mod_zone_page_state(zone, NR_FREE_PAGES, -(i << order));
Nick Pigginc54ad302006-01-06 00:10:56 -08001145 spin_unlock(&zone->lock);
Nick Piggin085cc7d2006-01-06 00:11:01 -08001146 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147}
1148
Christoph Lameter4ae7c032005-06-21 17:14:57 -07001149#ifdef CONFIG_NUMA
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001150/*
Christoph Lameter4037d452007-05-09 02:35:14 -07001151 * Called from the vmstat counter updater to drain pagesets of this
1152 * currently executing processor on remote nodes after they have
1153 * expired.
1154 *
Christoph Lameter879336c2006-03-22 00:09:08 -08001155 * Note that this function must be called with the thread pinned to
1156 * a single processor.
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001157 */
Christoph Lameter4037d452007-05-09 02:35:14 -07001158void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
Christoph Lameter4ae7c032005-06-21 17:14:57 -07001159{
Christoph Lameter4ae7c032005-06-21 17:14:57 -07001160 unsigned long flags;
Christoph Lameter4037d452007-05-09 02:35:14 -07001161 int to_drain;
Christoph Lameter4ae7c032005-06-21 17:14:57 -07001162
Christoph Lameter4037d452007-05-09 02:35:14 -07001163 local_irq_save(flags);
1164 if (pcp->count >= pcp->batch)
1165 to_drain = pcp->batch;
1166 else
1167 to_drain = pcp->count;
KOSAKI Motohiro2a135152012-07-31 16:42:53 -07001168 if (to_drain > 0) {
1169 free_pcppages_bulk(zone, to_drain, pcp);
1170 pcp->count -= to_drain;
1171 }
Christoph Lameter4037d452007-05-09 02:35:14 -07001172 local_irq_restore(flags);
Christoph Lameter4ae7c032005-06-21 17:14:57 -07001173}
1174#endif
1175
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001176/*
1177 * Drain pages of the indicated processor.
1178 *
1179 * The processor must either be the current processor and the
1180 * thread pinned to the current processor or a processor that
1181 * is not online.
1182 */
1183static void drain_pages(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
Nick Pigginc54ad302006-01-06 00:10:56 -08001185 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 struct zone *zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
KOSAKI Motohiroee99c712009-03-31 15:19:31 -07001188 for_each_populated_zone(zone) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 struct per_cpu_pageset *pset;
Christoph Lameter3dfa5722008-02-04 22:29:19 -08001190 struct per_cpu_pages *pcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09001192 local_irq_save(flags);
1193 pset = per_cpu_ptr(zone->pageset, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Christoph Lameter3dfa5722008-02-04 22:29:19 -08001195 pcp = &pset->pcp;
David Rientjes2ff754f2011-01-25 15:07:23 -08001196 if (pcp->count) {
1197 free_pcppages_bulk(zone, pcp->count, pcp);
1198 pcp->count = 0;
1199 }
Christoph Lameter3dfa5722008-02-04 22:29:19 -08001200 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
1202}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001204/*
1205 * Spill all of this CPU's per-cpu pages back into the buddy allocator.
1206 */
1207void drain_local_pages(void *arg)
1208{
1209 drain_pages(smp_processor_id());
1210}
1211
1212/*
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07001213 * Spill all the per-cpu pages from all CPUs back into the buddy allocator.
1214 *
1215 * Note that this code is protected against sending an IPI to an offline
1216 * CPU but does not guarantee sending an IPI to newly hotplugged CPUs:
1217 * on_each_cpu_mask() blocks hotplug and won't talk to offlined CPUs but
1218 * nothing keeps CPUs from showing up after we populated the cpumask and
1219 * before the call to on_each_cpu_mask().
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001220 */
1221void drain_all_pages(void)
1222{
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07001223 int cpu;
1224 struct per_cpu_pageset *pcp;
1225 struct zone *zone;
1226
1227 /*
1228 * Allocate in the BSS so we wont require allocation in
1229 * direct reclaim path for CONFIG_CPUMASK_OFFSTACK=y
1230 */
1231 static cpumask_t cpus_with_pcps;
1232
1233 /*
1234 * We don't care about racing with CPU hotplug event
1235 * as offline notification will cause the notified
1236 * cpu to drain that CPU pcps and on_each_cpu_mask
1237 * disables preemption as part of its processing
1238 */
1239 for_each_online_cpu(cpu) {
1240 bool has_pcps = false;
1241 for_each_populated_zone(zone) {
1242 pcp = per_cpu_ptr(zone->pageset, cpu);
1243 if (pcp->pcp.count) {
1244 has_pcps = true;
1245 break;
1246 }
1247 }
1248 if (has_pcps)
1249 cpumask_set_cpu(cpu, &cpus_with_pcps);
1250 else
1251 cpumask_clear_cpu(cpu, &cpus_with_pcps);
1252 }
1253 on_each_cpu_mask(&cpus_with_pcps, drain_local_pages, NULL, 1);
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001254}
1255
Rafael J. Wysocki296699d2007-07-29 23:27:18 +02001256#ifdef CONFIG_HIBERNATION
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258void mark_free_pages(struct zone *zone)
1259{
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07001260 unsigned long pfn, max_zone_pfn;
1261 unsigned long flags;
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001262 int order, t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 struct list_head *curr;
1264
1265 if (!zone->spanned_pages)
1266 return;
1267
1268 spin_lock_irqsave(&zone->lock, flags);
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07001269
1270 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
1271 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
1272 if (pfn_valid(pfn)) {
1273 struct page *page = pfn_to_page(pfn);
1274
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001275 if (!swsusp_page_is_forbidden(page))
1276 swsusp_unset_page_free(page);
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07001277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001279 for_each_migratetype_order(order, t) {
1280 list_for_each(curr, &zone->free_area[order].free_list[t]) {
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07001281 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07001283 pfn = page_to_pfn(list_entry(curr, struct page, lru));
1284 for (i = 0; i < (1UL << order); i++)
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001285 swsusp_set_page_free(pfn_to_page(pfn + i));
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07001286 }
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 spin_unlock_irqrestore(&zone->lock, flags);
1289}
Mel Gormane2c55dc2007-10-16 01:25:50 -07001290#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 * Free a 0-order page
Li Hongfc916682010-03-05 13:41:54 -08001294 * cold == 1 ? free a cold page : free a hot page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 */
Li Hongfc916682010-03-05 13:41:54 -08001296void free_hot_cold_page(struct page *page, int cold)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
1298 struct zone *zone = page_zone(page);
1299 struct per_cpu_pages *pcp;
1300 unsigned long flags;
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001301 int migratetype;
Johannes Weiner451ea252009-09-21 17:01:48 -07001302 int wasMlocked = __TestClearPageMlocked(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
KOSAKI Motohiroec95f532010-05-24 14:32:38 -07001304 if (!free_pages_prepare(page, 0))
Hugh Dickins689bceb2005-11-21 21:32:20 -08001305 return;
1306
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001307 migratetype = get_pageblock_migratetype(page);
1308 set_page_private(page, migratetype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 local_irq_save(flags);
Johannes Weinerc2773312009-06-19 19:30:56 +02001310 if (unlikely(wasMlocked))
Mel Gormanda456f12009-06-16 15:32:08 -07001311 free_page_mlock(page);
Christoph Lameterf8891e52006-06-30 01:55:45 -07001312 __count_vm_event(PGFREE);
Mel Gormanda456f12009-06-16 15:32:08 -07001313
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001314 /*
1315 * We only track unmovable, reclaimable and movable on pcp lists.
1316 * Free ISOLATE pages back to the allocator because they are being
1317 * offlined but treat RESERVE as movable pages so we can get those
1318 * areas back if necessary. Otherwise, we may have to free
1319 * excessively into the page allocator
1320 */
1321 if (migratetype >= MIGRATE_PCPTYPES) {
1322 if (unlikely(migratetype == MIGRATE_ISOLATE)) {
1323 free_one_page(zone, page, 0, migratetype);
1324 goto out;
1325 }
1326 migratetype = MIGRATE_MOVABLE;
1327 }
1328
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09001329 pcp = &this_cpu_ptr(zone->pageset)->pcp;
Christoph Lameter3dfa5722008-02-04 22:29:19 -08001330 if (cold)
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001331 list_add_tail(&page->lru, &pcp->lists[migratetype]);
Christoph Lameter3dfa5722008-02-04 22:29:19 -08001332 else
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001333 list_add(&page->lru, &pcp->lists[migratetype]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 pcp->count++;
Nick Piggin48db57f2006-01-08 01:00:42 -08001335 if (pcp->count >= pcp->high) {
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001336 free_pcppages_bulk(zone, pcp->batch, pcp);
Nick Piggin48db57f2006-01-08 01:00:42 -08001337 pcp->count -= pcp->batch;
1338 }
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001339
1340out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342}
1343
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08001344/*
Konstantin Khlebnikovcc598502012-01-10 15:07:04 -08001345 * Free a list of 0-order pages
1346 */
1347void free_hot_cold_page_list(struct list_head *list, int cold)
1348{
1349 struct page *page, *next;
1350
1351 list_for_each_entry_safe(page, next, list, lru) {
Konstantin Khlebnikovb413d482012-01-10 15:07:09 -08001352 trace_mm_page_free_batched(page, cold);
Konstantin Khlebnikovcc598502012-01-10 15:07:04 -08001353 free_hot_cold_page(page, cold);
1354 }
1355}
1356
1357/*
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08001358 * split_page takes a non-compound higher-order page, and splits it into
1359 * n (1<<order) sub-pages: page[0..n]
1360 * Each sub-page must be freed individually.
1361 *
1362 * Note: this is probably too low level an operation for use in drivers.
1363 * Please consult with lkml before using this in your driver.
1364 */
1365void split_page(struct page *page, unsigned int order)
1366{
1367 int i;
1368
Nick Piggin725d7042006-09-25 23:30:55 -07001369 VM_BUG_ON(PageCompound(page));
1370 VM_BUG_ON(!page_count(page));
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001371
1372#ifdef CONFIG_KMEMCHECK
1373 /*
1374 * Split shadow pages too, because free(page[0]) would
1375 * otherwise free the whole shadow.
1376 */
1377 if (kmemcheck_page_is_tracked(page))
1378 split_page(virt_to_page(page[0].shadow), order);
1379#endif
1380
Nick Piggin7835e982006-03-22 00:08:40 -08001381 for (i = 1; i < (1 << order); i++)
1382 set_page_refcounted(page + i);
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08001383}
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08001384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385/*
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001386 * Similar to the split_page family of functions except that the page
1387 * required at the given order and being isolated now to prevent races
1388 * with parallel allocators
Mel Gorman748446b2010-05-24 14:32:27 -07001389 */
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001390int capture_free_page(struct page *page, int alloc_order, int migratetype)
Mel Gorman748446b2010-05-24 14:32:27 -07001391{
1392 unsigned int order;
1393 unsigned long watermark;
1394 struct zone *zone;
1395
1396 BUG_ON(!PageBuddy(page));
1397
1398 zone = page_zone(page);
1399 order = page_order(page);
1400
1401 /* Obey watermarks as if the page was being allocated */
1402 watermark = low_wmark_pages(zone) + (1 << order);
1403 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
1404 return 0;
1405
1406 /* Remove page from free list */
1407 list_del(&page->lru);
1408 zone->free_area[order].nr_free--;
1409 rmv_page_order(page);
1410 __mod_zone_page_state(zone, NR_FREE_PAGES, -(1UL << order));
1411
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001412 if (alloc_order != order)
1413 expand(zone, page, alloc_order, order,
1414 &zone->free_area[order], migratetype);
Mel Gorman748446b2010-05-24 14:32:27 -07001415
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001416 /* Set the pageblock if the captured page is at least a pageblock */
Mel Gorman748446b2010-05-24 14:32:27 -07001417 if (order >= pageblock_order - 1) {
1418 struct page *endpage = page + (1 << order) - 1;
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001419 for (; page < endpage; page += pageblock_nr_pages) {
1420 int mt = get_pageblock_migratetype(page);
1421 if (mt != MIGRATE_ISOLATE && !is_migrate_cma(mt))
1422 set_pageblock_migratetype(page,
1423 MIGRATE_MOVABLE);
1424 }
Mel Gorman748446b2010-05-24 14:32:27 -07001425 }
1426
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001427 return 1UL << order;
1428}
1429
1430/*
1431 * Similar to split_page except the page is already free. As this is only
1432 * being used for migration, the migratetype of the block also changes.
1433 * As this is called with interrupts disabled, the caller is responsible
1434 * for calling arch_alloc_page() and kernel_map_page() after interrupts
1435 * are enabled.
1436 *
1437 * Note: this is probably too low level an operation for use in drivers.
1438 * Please consult with lkml before using this in your driver.
1439 */
1440int split_free_page(struct page *page)
1441{
1442 unsigned int order;
1443 int nr_pages;
1444
1445 BUG_ON(!PageBuddy(page));
1446 order = page_order(page);
1447
1448 nr_pages = capture_free_page(page, order, 0);
1449 if (!nr_pages)
1450 return 0;
1451
1452 /* Split into individual pages */
1453 set_page_refcounted(page);
1454 split_page(page, order);
1455 return nr_pages;
Mel Gorman748446b2010-05-24 14:32:27 -07001456}
1457
1458/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
1460 * we cheat by calling it from here, in the order > 0 path. Saves a branch
1461 * or two.
1462 */
Mel Gorman0a15c3e2009-06-16 15:32:05 -07001463static inline
1464struct page *buffered_rmqueue(struct zone *preferred_zone,
Mel Gorman3dd28262009-06-16 15:32:00 -07001465 struct zone *zone, int order, gfp_t gfp_flags,
1466 int migratetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
1468 unsigned long flags;
Hugh Dickins689bceb2005-11-21 21:32:20 -08001469 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 int cold = !!(gfp_flags & __GFP_COLD);
1471
Hugh Dickins689bceb2005-11-21 21:32:20 -08001472again:
Nick Piggin48db57f2006-01-08 01:00:42 -08001473 if (likely(order == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 struct per_cpu_pages *pcp;
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001475 struct list_head *list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 local_irq_save(flags);
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09001478 pcp = &this_cpu_ptr(zone->pageset)->pcp;
1479 list = &pcp->lists[migratetype];
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001480 if (list_empty(list)) {
Mel Gorman535131e62007-10-16 01:25:49 -07001481 pcp->count += rmqueue_bulk(zone, 0,
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001482 pcp->batch, list,
Mel Gormane084b2d2009-07-29 15:02:04 -07001483 migratetype, cold);
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001484 if (unlikely(list_empty(list)))
Shaohua Li6fb332f2009-09-21 17:01:17 -07001485 goto failed;
Mel Gorman535131e62007-10-16 01:25:49 -07001486 }
Mel Gormanb92a6ed2007-10-16 01:25:50 -07001487
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001488 if (cold)
1489 page = list_entry(list->prev, struct page, lru);
1490 else
1491 page = list_entry(list->next, struct page, lru);
1492
Mel Gormanb92a6ed2007-10-16 01:25:50 -07001493 list_del(&page->lru);
1494 pcp->count--;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001495 } else {
Andrew Mortondab48da2009-06-16 15:32:37 -07001496 if (unlikely(gfp_flags & __GFP_NOFAIL)) {
1497 /*
1498 * __GFP_NOFAIL is not to be used in new code.
1499 *
1500 * All __GFP_NOFAIL callers should be fixed so that they
1501 * properly detect and handle allocation failures.
1502 *
1503 * We most definitely don't want callers attempting to
Linus Torvalds4923abf2009-06-24 12:16:49 -07001504 * allocate greater than order-1 page units with
Andrew Mortondab48da2009-06-16 15:32:37 -07001505 * __GFP_NOFAIL.
1506 */
Linus Torvalds4923abf2009-06-24 12:16:49 -07001507 WARN_ON_ONCE(order > 1);
Andrew Mortondab48da2009-06-16 15:32:37 -07001508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 spin_lock_irqsave(&zone->lock, flags);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001510 page = __rmqueue(zone, order, migratetype);
Nick Piggina74609f2006-01-06 00:11:20 -08001511 spin_unlock(&zone->lock);
1512 if (!page)
1513 goto failed;
KOSAKI Motohiro6ccf80e2010-01-15 17:01:18 -08001514 __mod_zone_page_state(zone, NR_FREE_PAGES, -(1 << order));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 }
1516
Christoph Lameterf8891e52006-06-30 01:55:45 -07001517 __count_zone_vm_events(PGALLOC, zone, 1 << order);
Andi Kleen78afd562011-03-22 16:33:12 -07001518 zone_statistics(preferred_zone, zone, gfp_flags);
Nick Piggina74609f2006-01-06 00:11:20 -08001519 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Nick Piggin725d7042006-09-25 23:30:55 -07001521 VM_BUG_ON(bad_range(zone, page));
Nick Piggin17cf4402006-03-22 00:08:41 -08001522 if (prep_new_page(page, order, gfp_flags))
Nick Piggina74609f2006-01-06 00:11:20 -08001523 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return page;
Nick Piggina74609f2006-01-06 00:11:20 -08001525
1526failed:
1527 local_irq_restore(flags);
Nick Piggina74609f2006-01-06 00:11:20 -08001528 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529}
1530
Mel Gorman41858962009-06-16 15:32:12 -07001531/* The ALLOC_WMARK bits are used as an index to zone->watermark */
1532#define ALLOC_WMARK_MIN WMARK_MIN
1533#define ALLOC_WMARK_LOW WMARK_LOW
1534#define ALLOC_WMARK_HIGH WMARK_HIGH
1535#define ALLOC_NO_WATERMARKS 0x04 /* don't check watermarks at all */
1536
1537/* Mask to get the watermark bits */
1538#define ALLOC_WMARK_MASK (ALLOC_NO_WATERMARKS-1)
1539
Nick Piggin31488902005-11-28 13:44:03 -08001540#define ALLOC_HARDER 0x10 /* try to alloc harder */
1541#define ALLOC_HIGH 0x20 /* __GFP_HIGH set */
1542#define ALLOC_CPUSET 0x40 /* check for correct cpuset */
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001543
Akinobu Mita933e3122006-12-08 02:39:45 -08001544#ifdef CONFIG_FAIL_PAGE_ALLOC
1545
Akinobu Mitab2588c42011-07-26 16:09:03 -07001546static struct {
Akinobu Mita933e3122006-12-08 02:39:45 -08001547 struct fault_attr attr;
1548
1549 u32 ignore_gfp_highmem;
1550 u32 ignore_gfp_wait;
Akinobu Mita54114992007-07-15 23:40:23 -07001551 u32 min_order;
Akinobu Mita933e3122006-12-08 02:39:45 -08001552} fail_page_alloc = {
1553 .attr = FAULT_ATTR_INITIALIZER,
Don Mullis6b1b60f2006-12-08 02:39:53 -08001554 .ignore_gfp_wait = 1,
1555 .ignore_gfp_highmem = 1,
Akinobu Mita54114992007-07-15 23:40:23 -07001556 .min_order = 1,
Akinobu Mita933e3122006-12-08 02:39:45 -08001557};
1558
1559static int __init setup_fail_page_alloc(char *str)
1560{
1561 return setup_fault_attr(&fail_page_alloc.attr, str);
1562}
1563__setup("fail_page_alloc=", setup_fail_page_alloc);
1564
Gavin Shandeaf3862012-07-31 16:41:51 -07001565static bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
Akinobu Mita933e3122006-12-08 02:39:45 -08001566{
Akinobu Mita54114992007-07-15 23:40:23 -07001567 if (order < fail_page_alloc.min_order)
Gavin Shandeaf3862012-07-31 16:41:51 -07001568 return false;
Akinobu Mita933e3122006-12-08 02:39:45 -08001569 if (gfp_mask & __GFP_NOFAIL)
Gavin Shandeaf3862012-07-31 16:41:51 -07001570 return false;
Akinobu Mita933e3122006-12-08 02:39:45 -08001571 if (fail_page_alloc.ignore_gfp_highmem && (gfp_mask & __GFP_HIGHMEM))
Gavin Shandeaf3862012-07-31 16:41:51 -07001572 return false;
Akinobu Mita933e3122006-12-08 02:39:45 -08001573 if (fail_page_alloc.ignore_gfp_wait && (gfp_mask & __GFP_WAIT))
Gavin Shandeaf3862012-07-31 16:41:51 -07001574 return false;
Akinobu Mita933e3122006-12-08 02:39:45 -08001575
1576 return should_fail(&fail_page_alloc.attr, 1 << order);
1577}
1578
1579#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
1580
1581static int __init fail_page_alloc_debugfs(void)
1582{
Al Virof4ae40a2011-07-24 04:33:43 -04001583 umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
Akinobu Mita933e3122006-12-08 02:39:45 -08001584 struct dentry *dir;
Akinobu Mita933e3122006-12-08 02:39:45 -08001585
Akinobu Mitadd48c082011-08-03 16:21:01 -07001586 dir = fault_create_debugfs_attr("fail_page_alloc", NULL,
1587 &fail_page_alloc.attr);
1588 if (IS_ERR(dir))
1589 return PTR_ERR(dir);
Akinobu Mita933e3122006-12-08 02:39:45 -08001590
Akinobu Mitab2588c42011-07-26 16:09:03 -07001591 if (!debugfs_create_bool("ignore-gfp-wait", mode, dir,
1592 &fail_page_alloc.ignore_gfp_wait))
1593 goto fail;
1594 if (!debugfs_create_bool("ignore-gfp-highmem", mode, dir,
1595 &fail_page_alloc.ignore_gfp_highmem))
1596 goto fail;
1597 if (!debugfs_create_u32("min-order", mode, dir,
1598 &fail_page_alloc.min_order))
1599 goto fail;
Akinobu Mita933e3122006-12-08 02:39:45 -08001600
Akinobu Mitab2588c42011-07-26 16:09:03 -07001601 return 0;
1602fail:
Akinobu Mitadd48c082011-08-03 16:21:01 -07001603 debugfs_remove_recursive(dir);
Akinobu Mita933e3122006-12-08 02:39:45 -08001604
Akinobu Mitab2588c42011-07-26 16:09:03 -07001605 return -ENOMEM;
Akinobu Mita933e3122006-12-08 02:39:45 -08001606}
1607
1608late_initcall(fail_page_alloc_debugfs);
1609
1610#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
1611
1612#else /* CONFIG_FAIL_PAGE_ALLOC */
1613
Gavin Shandeaf3862012-07-31 16:41:51 -07001614static inline bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
Akinobu Mita933e3122006-12-08 02:39:45 -08001615{
Gavin Shandeaf3862012-07-31 16:41:51 -07001616 return false;
Akinobu Mita933e3122006-12-08 02:39:45 -08001617}
1618
1619#endif /* CONFIG_FAIL_PAGE_ALLOC */
1620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621/*
Mel Gorman88f5acf2011-01-13 15:45:41 -08001622 * Return true if free pages are above 'mark'. This takes into account the order
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 * of the allocation.
1624 */
Mel Gorman88f5acf2011-01-13 15:45:41 -08001625static bool __zone_watermark_ok(struct zone *z, int order, unsigned long mark,
1626 int classzone_idx, int alloc_flags, long free_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627{
1628 /* free_pages my go negative - that's OK */
Christoph Lameterd23ad422007-02-10 01:43:02 -08001629 long min = mark;
Minchan Kim2cfed072012-07-31 16:43:53 -07001630 long lowmem_reserve = z->lowmem_reserve[classzone_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 int o;
1632
Michal Hockodf0a6da2012-01-10 15:08:02 -08001633 free_pages -= (1 << order) - 1;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001634 if (alloc_flags & ALLOC_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 min -= min / 2;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001636 if (alloc_flags & ALLOC_HARDER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 min -= min / 4;
1638
Minchan Kim2cfed072012-07-31 16:43:53 -07001639 if (free_pages <= min + lowmem_reserve)
Mel Gorman88f5acf2011-01-13 15:45:41 -08001640 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 for (o = 0; o < order; o++) {
1642 /* At the next order, this order's pages become unavailable */
1643 free_pages -= z->free_area[o].nr_free << o;
1644
1645 /* Require fewer higher order pages to be free */
1646 min >>= 1;
1647
1648 if (free_pages <= min)
Mel Gorman88f5acf2011-01-13 15:45:41 -08001649 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 }
Mel Gorman88f5acf2011-01-13 15:45:41 -08001651 return true;
1652}
1653
Minchan Kim702d1a62012-07-31 16:43:56 -07001654#ifdef CONFIG_MEMORY_ISOLATION
1655static inline unsigned long nr_zone_isolate_freepages(struct zone *zone)
1656{
1657 if (unlikely(zone->nr_pageblock_isolate))
1658 return zone->nr_pageblock_isolate * pageblock_nr_pages;
1659 return 0;
1660}
1661#else
1662static inline unsigned long nr_zone_isolate_freepages(struct zone *zone)
1663{
1664 return 0;
1665}
1666#endif
1667
Mel Gorman88f5acf2011-01-13 15:45:41 -08001668bool zone_watermark_ok(struct zone *z, int order, unsigned long mark,
1669 int classzone_idx, int alloc_flags)
1670{
1671 return __zone_watermark_ok(z, order, mark, classzone_idx, alloc_flags,
1672 zone_page_state(z, NR_FREE_PAGES));
1673}
1674
1675bool zone_watermark_ok_safe(struct zone *z, int order, unsigned long mark,
1676 int classzone_idx, int alloc_flags)
1677{
1678 long free_pages = zone_page_state(z, NR_FREE_PAGES);
1679
1680 if (z->percpu_drift_mark && free_pages < z->percpu_drift_mark)
1681 free_pages = zone_page_state_snapshot(z, NR_FREE_PAGES);
1682
Minchan Kim702d1a62012-07-31 16:43:56 -07001683 /*
1684 * If the zone has MIGRATE_ISOLATE type free pages, we should consider
1685 * it. nr_zone_isolate_freepages is never accurate so kswapd might not
1686 * sleep although it could do so. But this is more desirable for memory
1687 * hotplug than sleeping which can cause a livelock in the direct
1688 * reclaim path.
1689 */
1690 free_pages -= nr_zone_isolate_freepages(z);
Mel Gorman88f5acf2011-01-13 15:45:41 -08001691 return __zone_watermark_ok(z, order, mark, classzone_idx, alloc_flags,
1692 free_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693}
1694
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001695#ifdef CONFIG_NUMA
1696/*
1697 * zlc_setup - Setup for "zonelist cache". Uses cached zone data to
1698 * skip over zones that are not allowed by the cpuset, or that have
1699 * been recently (in last second) found to be nearly full. See further
1700 * comments in mmzone.h. Reduces cache footprint of zonelist scans
Simon Arlott183ff222007-10-20 01:27:18 +02001701 * that have to skip over a lot of full or unallowed zones.
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001702 *
1703 * If the zonelist cache is present in the passed in zonelist, then
1704 * returns a pointer to the allowed node mask (either the current
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07001705 * tasks mems_allowed, or node_states[N_HIGH_MEMORY].)
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001706 *
1707 * If the zonelist cache is not available for this zonelist, does
1708 * nothing and returns NULL.
1709 *
1710 * If the fullzones BITMAP in the zonelist cache is stale (more than
1711 * a second since last zap'd) then we zap it out (clear its bits.)
1712 *
1713 * We hold off even calling zlc_setup, until after we've checked the
1714 * first zone in the zonelist, on the theory that most allocations will
1715 * be satisfied from that first zone, so best to examine that zone as
1716 * quickly as we can.
1717 */
1718static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
1719{
1720 struct zonelist_cache *zlc; /* cached zonelist speedup info */
1721 nodemask_t *allowednodes; /* zonelist_cache approximation */
1722
1723 zlc = zonelist->zlcache_ptr;
1724 if (!zlc)
1725 return NULL;
1726
S.Caglar Onurf05111f2008-04-28 02:12:38 -07001727 if (time_after(jiffies, zlc->last_full_zap + HZ)) {
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001728 bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
1729 zlc->last_full_zap = jiffies;
1730 }
1731
1732 allowednodes = !in_interrupt() && (alloc_flags & ALLOC_CPUSET) ?
1733 &cpuset_current_mems_allowed :
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07001734 &node_states[N_HIGH_MEMORY];
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001735 return allowednodes;
1736}
1737
1738/*
1739 * Given 'z' scanning a zonelist, run a couple of quick checks to see
1740 * if it is worth looking at further for free memory:
1741 * 1) Check that the zone isn't thought to be full (doesn't have its
1742 * bit set in the zonelist_cache fullzones BITMAP).
1743 * 2) Check that the zones node (obtained from the zonelist_cache
1744 * z_to_n[] mapping) is allowed in the passed in allowednodes mask.
1745 * Return true (non-zero) if zone is worth looking at further, or
1746 * else return false (zero) if it is not.
1747 *
1748 * This check -ignores- the distinction between various watermarks,
1749 * such as GFP_HIGH, GFP_ATOMIC, PF_MEMALLOC, ... If a zone is
1750 * found to be full for any variation of these watermarks, it will
1751 * be considered full for up to one second by all requests, unless
1752 * we are so low on memory on all allowed nodes that we are forced
1753 * into the second scan of the zonelist.
1754 *
1755 * In the second scan we ignore this zonelist cache and exactly
1756 * apply the watermarks to all zones, even it is slower to do so.
1757 * We are low on memory in the second scan, and should leave no stone
1758 * unturned looking for a free page.
1759 */
Mel Gormandd1a2392008-04-28 02:12:17 -07001760static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zoneref *z,
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001761 nodemask_t *allowednodes)
1762{
1763 struct zonelist_cache *zlc; /* cached zonelist speedup info */
1764 int i; /* index of *z in zonelist zones */
1765 int n; /* node that zone *z is on */
1766
1767 zlc = zonelist->zlcache_ptr;
1768 if (!zlc)
1769 return 1;
1770
Mel Gormandd1a2392008-04-28 02:12:17 -07001771 i = z - zonelist->_zonerefs;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001772 n = zlc->z_to_n[i];
1773
1774 /* This zone is worth trying if it is allowed but not full */
1775 return node_isset(n, *allowednodes) && !test_bit(i, zlc->fullzones);
1776}
1777
1778/*
1779 * Given 'z' scanning a zonelist, set the corresponding bit in
1780 * zlc->fullzones, so that subsequent attempts to allocate a page
1781 * from that zone don't waste time re-examining it.
1782 */
Mel Gormandd1a2392008-04-28 02:12:17 -07001783static void zlc_mark_zone_full(struct zonelist *zonelist, struct zoneref *z)
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001784{
1785 struct zonelist_cache *zlc; /* cached zonelist speedup info */
1786 int i; /* index of *z in zonelist zones */
1787
1788 zlc = zonelist->zlcache_ptr;
1789 if (!zlc)
1790 return;
1791
Mel Gormandd1a2392008-04-28 02:12:17 -07001792 i = z - zonelist->_zonerefs;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001793
1794 set_bit(i, zlc->fullzones);
1795}
1796
Mel Gorman76d3fbf2011-07-25 17:12:30 -07001797/*
1798 * clear all zones full, called after direct reclaim makes progress so that
1799 * a zone that was recently full is not skipped over for up to a second
1800 */
1801static void zlc_clear_zones_full(struct zonelist *zonelist)
1802{
1803 struct zonelist_cache *zlc; /* cached zonelist speedup info */
1804
1805 zlc = zonelist->zlcache_ptr;
1806 if (!zlc)
1807 return;
1808
1809 bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
1810}
1811
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001812#else /* CONFIG_NUMA */
1813
1814static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
1815{
1816 return NULL;
1817}
1818
Mel Gormandd1a2392008-04-28 02:12:17 -07001819static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zoneref *z,
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001820 nodemask_t *allowednodes)
1821{
1822 return 1;
1823}
1824
Mel Gormandd1a2392008-04-28 02:12:17 -07001825static void zlc_mark_zone_full(struct zonelist *zonelist, struct zoneref *z)
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001826{
1827}
Mel Gorman76d3fbf2011-07-25 17:12:30 -07001828
1829static void zlc_clear_zones_full(struct zonelist *zonelist)
1830{
1831}
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001832#endif /* CONFIG_NUMA */
1833
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001834/*
Paul Jackson0798e512006-12-06 20:31:38 -08001835 * get_page_from_freelist goes through the zonelist trying to allocate
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001836 * a page.
1837 */
1838static struct page *
Mel Gorman19770b32008-04-28 02:12:18 -07001839get_page_from_freelist(gfp_t gfp_mask, nodemask_t *nodemask, unsigned int order,
Mel Gorman5117f452009-06-16 15:31:59 -07001840 struct zonelist *zonelist, int high_zoneidx, int alloc_flags,
Mel Gorman3dd28262009-06-16 15:32:00 -07001841 struct zone *preferred_zone, int migratetype)
Martin Hicks753ee722005-06-21 17:14:41 -07001842{
Mel Gormandd1a2392008-04-28 02:12:17 -07001843 struct zoneref *z;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001844 struct page *page = NULL;
Mel Gorman54a6eb52008-04-28 02:12:16 -07001845 int classzone_idx;
Mel Gorman5117f452009-06-16 15:31:59 -07001846 struct zone *zone;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001847 nodemask_t *allowednodes = NULL;/* zonelist_cache approximation */
1848 int zlc_active = 0; /* set if using zonelist_cache */
1849 int did_zlc_setup = 0; /* just call zlc_setup() one time */
Mel Gorman54a6eb52008-04-28 02:12:16 -07001850
Mel Gorman19770b32008-04-28 02:12:18 -07001851 classzone_idx = zone_idx(preferred_zone);
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001852zonelist_scan:
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001853 /*
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001854 * Scan zonelist, looking for a zone with enough free.
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001855 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
1856 */
Mel Gorman19770b32008-04-28 02:12:18 -07001857 for_each_zone_zonelist_nodemask(zone, z, zonelist,
1858 high_zoneidx, nodemask) {
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001859 if (NUMA_BUILD && zlc_active &&
1860 !zlc_zone_worth_trying(zonelist, z, allowednodes))
1861 continue;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001862 if ((alloc_flags & ALLOC_CPUSET) &&
Paul Jackson02a0e532006-12-13 00:34:25 -08001863 !cpuset_zone_allowed_softwall(zone, gfp_mask))
Mel Gormancd38b112011-07-25 17:12:29 -07001864 continue;
Johannes Weinera756cf52012-01-10 15:07:49 -08001865 /*
1866 * When allocating a page cache page for writing, we
1867 * want to get it from a zone that is within its dirty
1868 * limit, such that no single zone holds more than its
1869 * proportional share of globally allowed dirty pages.
1870 * The dirty limits take into account the zone's
1871 * lowmem reserves and high watermark so that kswapd
1872 * should be able to balance it without having to
1873 * write pages from its LRU list.
1874 *
1875 * This may look like it could increase pressure on
1876 * lower zones by failing allocations in higher zones
1877 * before they are full. But the pages that do spill
1878 * over are limited as the lower zones are protected
1879 * by this very same mechanism. It should not become
1880 * a practical burden to them.
1881 *
1882 * XXX: For now, allow allocations to potentially
1883 * exceed the per-zone dirty limit in the slowpath
1884 * (ALLOC_WMARK_LOW unset) before going into reclaim,
1885 * which is important when on a NUMA setup the allowed
1886 * zones are together not big enough to reach the
1887 * global limit. The proper fix for these situations
1888 * will require awareness of zones in the
1889 * dirty-throttling and the flusher threads.
1890 */
1891 if ((alloc_flags & ALLOC_WMARK_LOW) &&
1892 (gfp_mask & __GFP_WRITE) && !zone_dirty_ok(zone))
1893 goto this_zone_full;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001894
Mel Gorman41858962009-06-16 15:32:12 -07001895 BUILD_BUG_ON(ALLOC_NO_WATERMARKS < NR_WMARK);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001896 if (!(alloc_flags & ALLOC_NO_WATERMARKS)) {
Nick Piggin31488902005-11-28 13:44:03 -08001897 unsigned long mark;
Mel Gormanfa5e0842009-06-16 15:33:22 -07001898 int ret;
1899
Mel Gorman41858962009-06-16 15:32:12 -07001900 mark = zone->watermark[alloc_flags & ALLOC_WMARK_MASK];
Mel Gormanfa5e0842009-06-16 15:33:22 -07001901 if (zone_watermark_ok(zone, order, mark,
1902 classzone_idx, alloc_flags))
1903 goto try_this_zone;
1904
Mel Gormancd38b112011-07-25 17:12:29 -07001905 if (NUMA_BUILD && !did_zlc_setup && nr_online_nodes > 1) {
1906 /*
1907 * we do zlc_setup if there are multiple nodes
1908 * and before considering the first zone allowed
1909 * by the cpuset.
1910 */
1911 allowednodes = zlc_setup(zonelist, alloc_flags);
1912 zlc_active = 1;
1913 did_zlc_setup = 1;
1914 }
1915
Mel Gormanfa5e0842009-06-16 15:33:22 -07001916 if (zone_reclaim_mode == 0)
1917 goto this_zone_full;
1918
Mel Gormancd38b112011-07-25 17:12:29 -07001919 /*
1920 * As we may have just activated ZLC, check if the first
1921 * eligible zone has failed zone_reclaim recently.
1922 */
1923 if (NUMA_BUILD && zlc_active &&
1924 !zlc_zone_worth_trying(zonelist, z, allowednodes))
1925 continue;
1926
Mel Gormanfa5e0842009-06-16 15:33:22 -07001927 ret = zone_reclaim(zone, gfp_mask, order);
1928 switch (ret) {
1929 case ZONE_RECLAIM_NOSCAN:
1930 /* did not scan */
Mel Gormancd38b112011-07-25 17:12:29 -07001931 continue;
Mel Gormanfa5e0842009-06-16 15:33:22 -07001932 case ZONE_RECLAIM_FULL:
1933 /* scanned but unreclaimable */
Mel Gormancd38b112011-07-25 17:12:29 -07001934 continue;
Mel Gormanfa5e0842009-06-16 15:33:22 -07001935 default:
1936 /* did we reclaim enough */
1937 if (!zone_watermark_ok(zone, order, mark,
1938 classzone_idx, alloc_flags))
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001939 goto this_zone_full;
Paul Jackson0798e512006-12-06 20:31:38 -08001940 }
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001941 }
1942
Mel Gormanfa5e0842009-06-16 15:33:22 -07001943try_this_zone:
Mel Gorman3dd28262009-06-16 15:32:00 -07001944 page = buffered_rmqueue(preferred_zone, zone, order,
1945 gfp_mask, migratetype);
Paul Jackson0798e512006-12-06 20:31:38 -08001946 if (page)
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001947 break;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001948this_zone_full:
1949 if (NUMA_BUILD)
1950 zlc_mark_zone_full(zonelist, z);
Mel Gorman54a6eb52008-04-28 02:12:16 -07001951 }
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001952
1953 if (unlikely(NUMA_BUILD && page == NULL && zlc_active)) {
1954 /* Disable zlc cache for second zonelist scan */
1955 zlc_active = 0;
1956 goto zonelist_scan;
1957 }
Alex Shib1211862012-08-21 16:16:08 -07001958
1959 if (page)
1960 /*
1961 * page->pfmemalloc is set when ALLOC_NO_WATERMARKS was
1962 * necessary to allocate the page. The expectation is
1963 * that the caller is taking steps that will free more
1964 * memory. The caller should avoid the page being used
1965 * for !PFMEMALLOC purposes.
1966 */
1967 page->pfmemalloc = !!(alloc_flags & ALLOC_NO_WATERMARKS);
1968
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001969 return page;
Martin Hicks753ee722005-06-21 17:14:41 -07001970}
1971
David Rientjes29423e772011-03-22 16:30:47 -07001972/*
1973 * Large machines with many possible nodes should not always dump per-node
1974 * meminfo in irq context.
1975 */
1976static inline bool should_suppress_show_mem(void)
1977{
1978 bool ret = false;
1979
1980#if NODES_SHIFT > 8
1981 ret = in_interrupt();
1982#endif
1983 return ret;
1984}
1985
Dave Hansena238ab52011-05-24 17:12:16 -07001986static DEFINE_RATELIMIT_STATE(nopage_rs,
1987 DEFAULT_RATELIMIT_INTERVAL,
1988 DEFAULT_RATELIMIT_BURST);
1989
1990void warn_alloc_failed(gfp_t gfp_mask, int order, const char *fmt, ...)
1991{
Dave Hansena238ab52011-05-24 17:12:16 -07001992 unsigned int filter = SHOW_MEM_FILTER_NODES;
1993
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -08001994 if ((gfp_mask & __GFP_NOWARN) || !__ratelimit(&nopage_rs) ||
1995 debug_guardpage_minorder() > 0)
Dave Hansena238ab52011-05-24 17:12:16 -07001996 return;
1997
1998 /*
1999 * This documents exceptions given to allocations in certain
2000 * contexts that are allowed to allocate outside current's set
2001 * of allowed nodes.
2002 */
2003 if (!(gfp_mask & __GFP_NOMEMALLOC))
2004 if (test_thread_flag(TIF_MEMDIE) ||
2005 (current->flags & (PF_MEMALLOC | PF_EXITING)))
2006 filter &= ~SHOW_MEM_FILTER_NODES;
2007 if (in_interrupt() || !(gfp_mask & __GFP_WAIT))
2008 filter &= ~SHOW_MEM_FILTER_NODES;
2009
2010 if (fmt) {
Joe Perches3ee9a4f2011-10-31 17:08:35 -07002011 struct va_format vaf;
2012 va_list args;
2013
Dave Hansena238ab52011-05-24 17:12:16 -07002014 va_start(args, fmt);
Joe Perches3ee9a4f2011-10-31 17:08:35 -07002015
2016 vaf.fmt = fmt;
2017 vaf.va = &args;
2018
2019 pr_warn("%pV", &vaf);
2020
Dave Hansena238ab52011-05-24 17:12:16 -07002021 va_end(args);
2022 }
2023
Joe Perches3ee9a4f2011-10-31 17:08:35 -07002024 pr_warn("%s: page allocation failure: order:%d, mode:0x%x\n",
2025 current->comm, order, gfp_mask);
Dave Hansena238ab52011-05-24 17:12:16 -07002026
2027 dump_stack();
2028 if (!should_suppress_show_mem())
2029 show_mem(filter);
2030}
2031
Mel Gorman11e33f62009-06-16 15:31:57 -07002032static inline int
2033should_alloc_retry(gfp_t gfp_mask, unsigned int order,
Mel Gormanf90ac392012-01-10 15:07:15 -08002034 unsigned long did_some_progress,
Mel Gorman11e33f62009-06-16 15:31:57 -07002035 unsigned long pages_reclaimed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036{
Mel Gorman11e33f62009-06-16 15:31:57 -07002037 /* Do not loop if specifically requested */
2038 if (gfp_mask & __GFP_NORETRY)
2039 return 0;
2040
Mel Gormanf90ac392012-01-10 15:07:15 -08002041 /* Always retry if specifically requested */
2042 if (gfp_mask & __GFP_NOFAIL)
2043 return 1;
2044
2045 /*
2046 * Suspend converts GFP_KERNEL to __GFP_WAIT which can prevent reclaim
2047 * making forward progress without invoking OOM. Suspend also disables
2048 * storage devices so kswapd will not help. Bail if we are suspending.
2049 */
2050 if (!did_some_progress && pm_suspended_storage())
2051 return 0;
2052
Mel Gorman11e33f62009-06-16 15:31:57 -07002053 /*
2054 * In this implementation, order <= PAGE_ALLOC_COSTLY_ORDER
2055 * means __GFP_NOFAIL, but that may not be true in other
2056 * implementations.
2057 */
2058 if (order <= PAGE_ALLOC_COSTLY_ORDER)
2059 return 1;
2060
2061 /*
2062 * For order > PAGE_ALLOC_COSTLY_ORDER, if __GFP_REPEAT is
2063 * specified, then we retry until we no longer reclaim any pages
2064 * (above), or we've reclaimed an order of pages at least as
2065 * large as the allocation's order. In both cases, if the
2066 * allocation still fails, we stop retrying.
2067 */
2068 if (gfp_mask & __GFP_REPEAT && pages_reclaimed < (1 << order))
2069 return 1;
2070
Mel Gorman11e33f62009-06-16 15:31:57 -07002071 return 0;
2072}
2073
2074static inline struct page *
2075__alloc_pages_may_oom(gfp_t gfp_mask, unsigned int order,
2076 struct zonelist *zonelist, enum zone_type high_zoneidx,
Mel Gorman3dd28262009-06-16 15:32:00 -07002077 nodemask_t *nodemask, struct zone *preferred_zone,
2078 int migratetype)
Mel Gorman11e33f62009-06-16 15:31:57 -07002079{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Mel Gorman11e33f62009-06-16 15:31:57 -07002082 /* Acquire the OOM killer lock for the zones in zonelist */
Minchan Kimff321fe2010-08-09 17:18:57 -07002083 if (!try_set_zonelist_oom(zonelist, gfp_mask)) {
Mel Gorman11e33f62009-06-16 15:31:57 -07002084 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 return NULL;
2086 }
Jens Axboe6b1de912005-11-17 21:35:02 +01002087
Mel Gorman11e33f62009-06-16 15:31:57 -07002088 /*
2089 * Go through the zonelist yet one more time, keep very high watermark
2090 * here, this is only to catch a parallel oom killing, we must fail if
2091 * we're still under heavy pressure.
2092 */
2093 page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, nodemask,
2094 order, zonelist, high_zoneidx,
Mel Gorman5117f452009-06-16 15:31:59 -07002095 ALLOC_WMARK_HIGH|ALLOC_CPUSET,
Mel Gorman3dd28262009-06-16 15:32:00 -07002096 preferred_zone, migratetype);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002097 if (page)
Mel Gorman11e33f62009-06-16 15:31:57 -07002098 goto out;
2099
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -08002100 if (!(gfp_mask & __GFP_NOFAIL)) {
2101 /* The OOM killer will not help higher order allocs */
2102 if (order > PAGE_ALLOC_COSTLY_ORDER)
2103 goto out;
David Rientjes03668b32010-08-09 17:18:54 -07002104 /* The OOM killer does not needlessly kill tasks for lowmem */
2105 if (high_zoneidx < ZONE_NORMAL)
2106 goto out;
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -08002107 /*
2108 * GFP_THISNODE contains __GFP_NORETRY and we never hit this.
2109 * Sanity check for bare calls of __GFP_THISNODE, not real OOM.
2110 * The caller should handle page allocation failure by itself if
2111 * it specifies __GFP_THISNODE.
2112 * Note: Hugepage uses it but will hit PAGE_ALLOC_COSTLY_ORDER.
2113 */
2114 if (gfp_mask & __GFP_THISNODE)
2115 goto out;
2116 }
Mel Gorman11e33f62009-06-16 15:31:57 -07002117 /* Exhausted what can be done so it's blamo time */
David Rientjes08ab9b12012-03-21 16:34:04 -07002118 out_of_memory(zonelist, gfp_mask, order, nodemask, false);
Mel Gorman11e33f62009-06-16 15:31:57 -07002119
2120out:
2121 clear_zonelist_oom(zonelist, gfp_mask);
2122 return page;
2123}
2124
Mel Gorman56de7262010-05-24 14:32:30 -07002125#ifdef CONFIG_COMPACTION
2126/* Try memory compaction for high-order allocations before reclaim */
2127static struct page *
2128__alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
2129 struct zonelist *zonelist, enum zone_type high_zoneidx,
2130 nodemask_t *nodemask, int alloc_flags, struct zone *preferred_zone,
Mel Gorman66199712012-01-12 17:19:41 -08002131 int migratetype, bool sync_migration,
Mel Gormanc67fe372012-08-21 16:16:17 -07002132 bool *contended_compaction, bool *deferred_compaction,
Mel Gorman66199712012-01-12 17:19:41 -08002133 unsigned long *did_some_progress)
Mel Gorman56de7262010-05-24 14:32:30 -07002134{
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07002135 struct page *page = NULL;
Mel Gorman56de7262010-05-24 14:32:30 -07002136
Mel Gorman66199712012-01-12 17:19:41 -08002137 if (!order)
Mel Gorman56de7262010-05-24 14:32:30 -07002138 return NULL;
2139
Rik van Rielaff62242012-03-21 16:33:52 -07002140 if (compaction_deferred(preferred_zone, order)) {
Mel Gorman66199712012-01-12 17:19:41 -08002141 *deferred_compaction = true;
2142 return NULL;
2143 }
2144
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08002145 current->flags |= PF_MEMALLOC;
Mel Gorman56de7262010-05-24 14:32:30 -07002146 *did_some_progress = try_to_compact_pages(zonelist, order, gfp_mask,
Mel Gormanc67fe372012-08-21 16:16:17 -07002147 nodemask, sync_migration,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07002148 contended_compaction, &page);
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08002149 current->flags &= ~PF_MEMALLOC;
Mel Gorman56de7262010-05-24 14:32:30 -07002150
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07002151 /* If compaction captured a page, prep and use it */
2152 if (page) {
2153 prep_new_page(page, order, gfp_mask);
2154 goto got_page;
2155 }
2156
2157 if (*did_some_progress != COMPACT_SKIPPED) {
Mel Gorman56de7262010-05-24 14:32:30 -07002158 /* Page migration frees to the PCP lists but we want merging */
2159 drain_pages(get_cpu());
2160 put_cpu();
2161
2162 page = get_page_from_freelist(gfp_mask, nodemask,
2163 order, zonelist, high_zoneidx,
Mel Gormancfd19c52012-07-31 16:44:10 -07002164 alloc_flags & ~ALLOC_NO_WATERMARKS,
2165 preferred_zone, migratetype);
Mel Gorman56de7262010-05-24 14:32:30 -07002166 if (page) {
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07002167got_page:
Mel Gorman4f92e252010-05-24 14:32:32 -07002168 preferred_zone->compact_considered = 0;
2169 preferred_zone->compact_defer_shift = 0;
Rik van Rielaff62242012-03-21 16:33:52 -07002170 if (order >= preferred_zone->compact_order_failed)
2171 preferred_zone->compact_order_failed = order + 1;
Mel Gorman56de7262010-05-24 14:32:30 -07002172 count_vm_event(COMPACTSUCCESS);
2173 return page;
2174 }
2175
2176 /*
2177 * It's bad if compaction run occurs and fails.
2178 * The most likely reason is that pages exist,
2179 * but not enough to satisfy watermarks.
2180 */
2181 count_vm_event(COMPACTFAIL);
Mel Gorman66199712012-01-12 17:19:41 -08002182
2183 /*
2184 * As async compaction considers a subset of pageblocks, only
2185 * defer if the failure was a sync compaction failure.
2186 */
2187 if (sync_migration)
Rik van Rielaff62242012-03-21 16:33:52 -07002188 defer_compaction(preferred_zone, order);
Mel Gorman56de7262010-05-24 14:32:30 -07002189
2190 cond_resched();
2191 }
2192
2193 return NULL;
2194}
2195#else
2196static inline struct page *
2197__alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
2198 struct zonelist *zonelist, enum zone_type high_zoneidx,
2199 nodemask_t *nodemask, int alloc_flags, struct zone *preferred_zone,
Mel Gorman66199712012-01-12 17:19:41 -08002200 int migratetype, bool sync_migration,
Mel Gormanc67fe372012-08-21 16:16:17 -07002201 bool *contended_compaction, bool *deferred_compaction,
Mel Gorman66199712012-01-12 17:19:41 -08002202 unsigned long *did_some_progress)
Mel Gorman56de7262010-05-24 14:32:30 -07002203{
2204 return NULL;
2205}
2206#endif /* CONFIG_COMPACTION */
2207
Marek Szyprowskibba90712012-01-25 12:09:52 +01002208/* Perform direct synchronous page reclaim */
2209static int
2210__perform_reclaim(gfp_t gfp_mask, unsigned int order, struct zonelist *zonelist,
2211 nodemask_t *nodemask)
Mel Gorman11e33f62009-06-16 15:31:57 -07002212{
Mel Gorman11e33f62009-06-16 15:31:57 -07002213 struct reclaim_state reclaim_state;
Marek Szyprowskibba90712012-01-25 12:09:52 +01002214 int progress;
Mel Gorman11e33f62009-06-16 15:31:57 -07002215
2216 cond_resched();
2217
2218 /* We now go into synchronous reclaim */
2219 cpuset_memory_pressure_bump();
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08002220 current->flags |= PF_MEMALLOC;
Mel Gorman11e33f62009-06-16 15:31:57 -07002221 lockdep_set_current_reclaim_state(gfp_mask);
2222 reclaim_state.reclaimed_slab = 0;
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08002223 current->reclaim_state = &reclaim_state;
Mel Gorman11e33f62009-06-16 15:31:57 -07002224
Marek Szyprowskibba90712012-01-25 12:09:52 +01002225 progress = try_to_free_pages(zonelist, order, gfp_mask, nodemask);
Mel Gorman11e33f62009-06-16 15:31:57 -07002226
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08002227 current->reclaim_state = NULL;
Mel Gorman11e33f62009-06-16 15:31:57 -07002228 lockdep_clear_current_reclaim_state();
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08002229 current->flags &= ~PF_MEMALLOC;
Mel Gorman11e33f62009-06-16 15:31:57 -07002230
2231 cond_resched();
2232
Marek Szyprowskibba90712012-01-25 12:09:52 +01002233 return progress;
2234}
2235
2236/* The really slow allocator path where we enter direct reclaim */
2237static inline struct page *
2238__alloc_pages_direct_reclaim(gfp_t gfp_mask, unsigned int order,
2239 struct zonelist *zonelist, enum zone_type high_zoneidx,
2240 nodemask_t *nodemask, int alloc_flags, struct zone *preferred_zone,
2241 int migratetype, unsigned long *did_some_progress)
2242{
2243 struct page *page = NULL;
2244 bool drained = false;
2245
2246 *did_some_progress = __perform_reclaim(gfp_mask, order, zonelist,
2247 nodemask);
Mel Gorman9ee493c2010-09-09 16:38:18 -07002248 if (unlikely(!(*did_some_progress)))
2249 return NULL;
Mel Gorman11e33f62009-06-16 15:31:57 -07002250
Mel Gorman76d3fbf2011-07-25 17:12:30 -07002251 /* After successful reclaim, reconsider all zones for allocation */
2252 if (NUMA_BUILD)
2253 zlc_clear_zones_full(zonelist);
2254
Mel Gorman9ee493c2010-09-09 16:38:18 -07002255retry:
2256 page = get_page_from_freelist(gfp_mask, nodemask, order,
Mel Gorman5117f452009-06-16 15:31:59 -07002257 zonelist, high_zoneidx,
Mel Gormancfd19c52012-07-31 16:44:10 -07002258 alloc_flags & ~ALLOC_NO_WATERMARKS,
2259 preferred_zone, migratetype);
Mel Gorman9ee493c2010-09-09 16:38:18 -07002260
2261 /*
2262 * If an allocation failed after direct reclaim, it could be because
2263 * pages are pinned on the per-cpu lists. Drain them and try again
2264 */
2265 if (!page && !drained) {
2266 drain_all_pages();
2267 drained = true;
2268 goto retry;
2269 }
2270
Mel Gorman11e33f62009-06-16 15:31:57 -07002271 return page;
2272}
2273
Mel Gorman11e33f62009-06-16 15:31:57 -07002274/*
2275 * This is called in the allocator slow-path if the allocation request is of
2276 * sufficient urgency to ignore watermarks and take other desperate measures
2277 */
2278static inline struct page *
2279__alloc_pages_high_priority(gfp_t gfp_mask, unsigned int order,
2280 struct zonelist *zonelist, enum zone_type high_zoneidx,
Mel Gorman3dd28262009-06-16 15:32:00 -07002281 nodemask_t *nodemask, struct zone *preferred_zone,
2282 int migratetype)
Mel Gorman11e33f62009-06-16 15:31:57 -07002283{
2284 struct page *page;
2285
2286 do {
2287 page = get_page_from_freelist(gfp_mask, nodemask, order,
Mel Gorman5117f452009-06-16 15:31:59 -07002288 zonelist, high_zoneidx, ALLOC_NO_WATERMARKS,
Mel Gorman3dd28262009-06-16 15:32:00 -07002289 preferred_zone, migratetype);
Mel Gorman11e33f62009-06-16 15:31:57 -07002290
2291 if (!page && gfp_mask & __GFP_NOFAIL)
Mel Gorman0e093d992010-10-26 14:21:45 -07002292 wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/50);
Mel Gorman11e33f62009-06-16 15:31:57 -07002293 } while (!page && (gfp_mask & __GFP_NOFAIL));
2294
2295 return page;
2296}
2297
2298static inline
2299void wake_all_kswapd(unsigned int order, struct zonelist *zonelist,
Mel Gorman99504742011-01-13 15:46:20 -08002300 enum zone_type high_zoneidx,
2301 enum zone_type classzone_idx)
Mel Gorman11e33f62009-06-16 15:31:57 -07002302{
2303 struct zoneref *z;
2304 struct zone *zone;
2305
2306 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx)
Mel Gorman99504742011-01-13 15:46:20 -08002307 wakeup_kswapd(zone, order, classzone_idx);
Mel Gorman11e33f62009-06-16 15:31:57 -07002308}
2309
Peter Zijlstra341ce062009-06-16 15:32:02 -07002310static inline int
2311gfp_to_alloc_flags(gfp_t gfp_mask)
2312{
Peter Zijlstra341ce062009-06-16 15:32:02 -07002313 int alloc_flags = ALLOC_WMARK_MIN | ALLOC_CPUSET;
2314 const gfp_t wait = gfp_mask & __GFP_WAIT;
2315
Mel Gormana56f57f2009-06-16 15:32:02 -07002316 /* __GFP_HIGH is assumed to be the same as ALLOC_HIGH to save a branch. */
Namhyung Kime6223a32010-10-26 14:21:59 -07002317 BUILD_BUG_ON(__GFP_HIGH != (__force gfp_t) ALLOC_HIGH);
Mel Gormana56f57f2009-06-16 15:32:02 -07002318
Peter Zijlstra341ce062009-06-16 15:32:02 -07002319 /*
2320 * The caller may dip into page reserves a bit more if the caller
2321 * cannot run direct reclaim, or if the caller has realtime scheduling
2322 * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will
2323 * set both ALLOC_HARDER (!wait) and ALLOC_HIGH (__GFP_HIGH).
2324 */
Namhyung Kime6223a32010-10-26 14:21:59 -07002325 alloc_flags |= (__force int) (gfp_mask & __GFP_HIGH);
Peter Zijlstra341ce062009-06-16 15:32:02 -07002326
2327 if (!wait) {
Andrea Arcangeli5c3240d2011-01-13 15:46:49 -08002328 /*
2329 * Not worth trying to allocate harder for
2330 * __GFP_NOMEMALLOC even if it can't schedule.
2331 */
2332 if (!(gfp_mask & __GFP_NOMEMALLOC))
2333 alloc_flags |= ALLOC_HARDER;
Peter Zijlstra341ce062009-06-16 15:32:02 -07002334 /*
2335 * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
2336 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
2337 */
2338 alloc_flags &= ~ALLOC_CPUSET;
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08002339 } else if (unlikely(rt_task(current)) && !in_interrupt())
Peter Zijlstra341ce062009-06-16 15:32:02 -07002340 alloc_flags |= ALLOC_HARDER;
2341
Mel Gormanb37f1dd2012-07-31 16:44:03 -07002342 if (likely(!(gfp_mask & __GFP_NOMEMALLOC))) {
2343 if (gfp_mask & __GFP_MEMALLOC)
2344 alloc_flags |= ALLOC_NO_WATERMARKS;
Mel Gorman907aed42012-07-31 16:44:07 -07002345 else if (in_serving_softirq() && (current->flags & PF_MEMALLOC))
2346 alloc_flags |= ALLOC_NO_WATERMARKS;
2347 else if (!in_interrupt() &&
2348 ((current->flags & PF_MEMALLOC) ||
2349 unlikely(test_thread_flag(TIF_MEMDIE))))
Peter Zijlstra341ce062009-06-16 15:32:02 -07002350 alloc_flags |= ALLOC_NO_WATERMARKS;
2351 }
2352
2353 return alloc_flags;
2354}
2355
Mel Gorman072bb0a2012-07-31 16:43:58 -07002356bool gfp_pfmemalloc_allowed(gfp_t gfp_mask)
2357{
Mel Gormanb37f1dd2012-07-31 16:44:03 -07002358 return !!(gfp_to_alloc_flags(gfp_mask) & ALLOC_NO_WATERMARKS);
Mel Gorman072bb0a2012-07-31 16:43:58 -07002359}
2360
Mel Gorman11e33f62009-06-16 15:31:57 -07002361static inline struct page *
2362__alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
2363 struct zonelist *zonelist, enum zone_type high_zoneidx,
Mel Gorman3dd28262009-06-16 15:32:00 -07002364 nodemask_t *nodemask, struct zone *preferred_zone,
2365 int migratetype)
Mel Gorman11e33f62009-06-16 15:31:57 -07002366{
2367 const gfp_t wait = gfp_mask & __GFP_WAIT;
2368 struct page *page = NULL;
2369 int alloc_flags;
2370 unsigned long pages_reclaimed = 0;
2371 unsigned long did_some_progress;
Mel Gorman77f1fe62011-01-13 15:45:57 -08002372 bool sync_migration = false;
Mel Gorman66199712012-01-12 17:19:41 -08002373 bool deferred_compaction = false;
Mel Gormanc67fe372012-08-21 16:16:17 -07002374 bool contended_compaction = false;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002375
Christoph Lameter952f3b52006-12-06 20:33:26 -08002376 /*
Mel Gorman72807a72009-06-16 15:32:18 -07002377 * In the slowpath, we sanity check order to avoid ever trying to
2378 * reclaim >= MAX_ORDER areas which will never succeed. Callers may
2379 * be using allocators in order of preference for an area that is
2380 * too large.
2381 */
Mel Gorman1fc28b72009-07-29 15:04:08 -07002382 if (order >= MAX_ORDER) {
2383 WARN_ON_ONCE(!(gfp_mask & __GFP_NOWARN));
Mel Gorman72807a72009-06-16 15:32:18 -07002384 return NULL;
Mel Gorman1fc28b72009-07-29 15:04:08 -07002385 }
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002386
Christoph Lameter952f3b52006-12-06 20:33:26 -08002387 /*
2388 * GFP_THISNODE (meaning __GFP_THISNODE, __GFP_NORETRY and
2389 * __GFP_NOWARN set) should not cause reclaim since the subsystem
2390 * (f.e. slab) using GFP_THISNODE may choose to trigger reclaim
2391 * using a larger set of nodes after it has established that the
2392 * allowed per node queues are empty and that nodes are
2393 * over allocated.
2394 */
2395 if (NUMA_BUILD && (gfp_mask & GFP_THISNODE) == GFP_THISNODE)
2396 goto nopage;
2397
Mel Gormancc4a6852009-11-11 14:26:14 -08002398restart:
Rik van Rielc6543452012-10-08 16:28:21 -07002399 wake_all_kswapd(order, zonelist, high_zoneidx,
2400 zone_idx(preferred_zone));
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002401
Paul Jackson9bf22292005-09-06 15:18:12 -07002402 /*
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002403 * OK, we're below the kswapd watermark and have kicked background
2404 * reclaim. Now things get more complex, so set up alloc_flags according
2405 * to how we want to proceed.
Paul Jackson9bf22292005-09-06 15:18:12 -07002406 */
Peter Zijlstra341ce062009-06-16 15:32:02 -07002407 alloc_flags = gfp_to_alloc_flags(gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
David Rientjesf33261d2011-01-25 15:07:20 -08002409 /*
2410 * Find the true preferred zone if the allocation is unconstrained by
2411 * cpusets.
2412 */
2413 if (!(alloc_flags & ALLOC_CPUSET) && !nodemask)
2414 first_zones_zonelist(zonelist, high_zoneidx, NULL,
2415 &preferred_zone);
2416
Andrew Barrycfa54a02011-05-24 17:12:52 -07002417rebalance:
Peter Zijlstra341ce062009-06-16 15:32:02 -07002418 /* This is the last chance, in general, before the goto nopage. */
Mel Gorman19770b32008-04-28 02:12:18 -07002419 page = get_page_from_freelist(gfp_mask, nodemask, order, zonelist,
Peter Zijlstra341ce062009-06-16 15:32:02 -07002420 high_zoneidx, alloc_flags & ~ALLOC_NO_WATERMARKS,
2421 preferred_zone, migratetype);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002422 if (page)
2423 goto got_pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
Mel Gorman11e33f62009-06-16 15:31:57 -07002425 /* Allocate without watermarks if the context allows */
Peter Zijlstra341ce062009-06-16 15:32:02 -07002426 if (alloc_flags & ALLOC_NO_WATERMARKS) {
Mel Gorman183f6372012-07-31 16:44:12 -07002427 /*
2428 * Ignore mempolicies if ALLOC_NO_WATERMARKS on the grounds
2429 * the allocation is high priority and these type of
2430 * allocations are system rather than user orientated
2431 */
2432 zonelist = node_zonelist(numa_node_id(), gfp_mask);
2433
Peter Zijlstra341ce062009-06-16 15:32:02 -07002434 page = __alloc_pages_high_priority(gfp_mask, order,
2435 zonelist, high_zoneidx, nodemask,
2436 preferred_zone, migratetype);
Mel Gormancfd19c52012-07-31 16:44:10 -07002437 if (page) {
Peter Zijlstra341ce062009-06-16 15:32:02 -07002438 goto got_pg;
Mel Gormancfd19c52012-07-31 16:44:10 -07002439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 }
2441
2442 /* Atomic allocations - we can't balance anything */
2443 if (!wait)
2444 goto nopage;
2445
Peter Zijlstra341ce062009-06-16 15:32:02 -07002446 /* Avoid recursion of direct reclaim */
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08002447 if (current->flags & PF_MEMALLOC)
Peter Zijlstra341ce062009-06-16 15:32:02 -07002448 goto nopage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
David Rientjes6583bb62009-07-29 15:02:06 -07002450 /* Avoid allocations with no watermarks from looping endlessly */
2451 if (test_thread_flag(TIF_MEMDIE) && !(gfp_mask & __GFP_NOFAIL))
2452 goto nopage;
2453
Mel Gorman77f1fe62011-01-13 15:45:57 -08002454 /*
2455 * Try direct compaction. The first pass is asynchronous. Subsequent
2456 * attempts after direct reclaim are synchronous
2457 */
Mel Gorman56de7262010-05-24 14:32:30 -07002458 page = __alloc_pages_direct_compact(gfp_mask, order,
2459 zonelist, high_zoneidx,
2460 nodemask,
2461 alloc_flags, preferred_zone,
Mel Gorman66199712012-01-12 17:19:41 -08002462 migratetype, sync_migration,
Mel Gormanc67fe372012-08-21 16:16:17 -07002463 &contended_compaction,
Mel Gorman66199712012-01-12 17:19:41 -08002464 &deferred_compaction,
2465 &did_some_progress);
Mel Gorman56de7262010-05-24 14:32:30 -07002466 if (page)
2467 goto got_pg;
Andrea Arcangelic6a140b2011-05-24 17:11:38 -07002468 sync_migration = true;
Mel Gorman56de7262010-05-24 14:32:30 -07002469
Mel Gorman66199712012-01-12 17:19:41 -08002470 /*
2471 * If compaction is deferred for high-order allocations, it is because
2472 * sync compaction recently failed. In this is the case and the caller
Mel Gormanc67fe372012-08-21 16:16:17 -07002473 * requested a movable allocation that does not heavily disrupt the
2474 * system then fail the allocation instead of entering direct reclaim.
Mel Gorman66199712012-01-12 17:19:41 -08002475 */
Mel Gormanc67fe372012-08-21 16:16:17 -07002476 if ((deferred_compaction || contended_compaction) &&
Rik van Rielc6543452012-10-08 16:28:21 -07002477 (gfp_mask & (__GFP_MOVABLE|__GFP_REPEAT)) == __GFP_MOVABLE)
Mel Gorman66199712012-01-12 17:19:41 -08002478 goto nopage;
2479
Mel Gorman11e33f62009-06-16 15:31:57 -07002480 /* Try direct reclaim and then allocating */
2481 page = __alloc_pages_direct_reclaim(gfp_mask, order,
2482 zonelist, high_zoneidx,
2483 nodemask,
Mel Gorman5117f452009-06-16 15:31:59 -07002484 alloc_flags, preferred_zone,
Mel Gorman3dd28262009-06-16 15:32:00 -07002485 migratetype, &did_some_progress);
Mel Gorman11e33f62009-06-16 15:31:57 -07002486 if (page)
2487 goto got_pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 /*
Mel Gorman11e33f62009-06-16 15:31:57 -07002490 * If we failed to make any progress reclaiming, then we are
2491 * running out of options and have to consider going OOM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 */
Mel Gorman11e33f62009-06-16 15:31:57 -07002493 if (!did_some_progress) {
2494 if ((gfp_mask & __GFP_FS) && !(gfp_mask & __GFP_NORETRY)) {
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -07002495 if (oom_killer_disabled)
2496 goto nopage;
David Rientjes29fd66d2012-03-28 14:42:41 -07002497 /* Coredumps can quickly deplete all memory reserves */
2498 if ((current->flags & PF_DUMPCORE) &&
2499 !(gfp_mask & __GFP_NOFAIL))
2500 goto nopage;
Mel Gorman11e33f62009-06-16 15:31:57 -07002501 page = __alloc_pages_may_oom(gfp_mask, order,
2502 zonelist, high_zoneidx,
Mel Gorman3dd28262009-06-16 15:32:00 -07002503 nodemask, preferred_zone,
2504 migratetype);
Mel Gorman11e33f62009-06-16 15:31:57 -07002505 if (page)
2506 goto got_pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
David Rientjes03668b32010-08-09 17:18:54 -07002508 if (!(gfp_mask & __GFP_NOFAIL)) {
2509 /*
2510 * The oom killer is not called for high-order
2511 * allocations that may fail, so if no progress
2512 * is being made, there are no other options and
2513 * retrying is unlikely to help.
2514 */
2515 if (order > PAGE_ALLOC_COSTLY_ORDER)
2516 goto nopage;
2517 /*
2518 * The oom killer is not called for lowmem
2519 * allocations to prevent needlessly killing
2520 * innocent tasks.
2521 */
2522 if (high_zoneidx < ZONE_NORMAL)
2523 goto nopage;
2524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 goto restart;
2527 }
2528 }
2529
Mel Gorman11e33f62009-06-16 15:31:57 -07002530 /* Check if we should retry the allocation */
Nishanth Aravamudana41f24e2008-04-29 00:58:25 -07002531 pages_reclaimed += did_some_progress;
Mel Gormanf90ac392012-01-10 15:07:15 -08002532 if (should_alloc_retry(gfp_mask, order, did_some_progress,
2533 pages_reclaimed)) {
Mel Gorman11e33f62009-06-16 15:31:57 -07002534 /* Wait for some write requests to complete then retry */
Mel Gorman0e093d992010-10-26 14:21:45 -07002535 wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 goto rebalance;
Mel Gorman3e7d3442011-01-13 15:45:56 -08002537 } else {
2538 /*
2539 * High-order allocations do not necessarily loop after
2540 * direct reclaim and reclaim/compaction depends on compaction
2541 * being called after reclaim so call directly if necessary
2542 */
2543 page = __alloc_pages_direct_compact(gfp_mask, order,
2544 zonelist, high_zoneidx,
2545 nodemask,
2546 alloc_flags, preferred_zone,
Mel Gorman66199712012-01-12 17:19:41 -08002547 migratetype, sync_migration,
Mel Gormanc67fe372012-08-21 16:16:17 -07002548 &contended_compaction,
Mel Gorman66199712012-01-12 17:19:41 -08002549 &deferred_compaction,
2550 &did_some_progress);
Mel Gorman3e7d3442011-01-13 15:45:56 -08002551 if (page)
2552 goto got_pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 }
2554
2555nopage:
Dave Hansena238ab52011-05-24 17:12:16 -07002556 warn_alloc_failed(gfp_mask, order, NULL);
Vegard Nossumb1eeab62008-11-25 16:55:53 +01002557 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558got_pg:
Vegard Nossumb1eeab62008-11-25 16:55:53 +01002559 if (kmemcheck_enabled)
2560 kmemcheck_pagealloc_alloc(page, order, gfp_mask);
Mel Gorman11e33f62009-06-16 15:31:57 -07002561
Mel Gorman072bb0a2012-07-31 16:43:58 -07002562 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563}
Mel Gorman11e33f62009-06-16 15:31:57 -07002564
2565/*
2566 * This is the 'heart' of the zoned buddy allocator.
2567 */
2568struct page *
2569__alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order,
2570 struct zonelist *zonelist, nodemask_t *nodemask)
2571{
2572 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
Mel Gorman5117f452009-06-16 15:31:59 -07002573 struct zone *preferred_zone;
Mel Gormancc9a6c82012-03-21 16:34:11 -07002574 struct page *page = NULL;
Mel Gorman3dd28262009-06-16 15:32:00 -07002575 int migratetype = allocflags_to_migratetype(gfp_mask);
Mel Gormancc9a6c82012-03-21 16:34:11 -07002576 unsigned int cpuset_mems_cookie;
Mel Gorman11e33f62009-06-16 15:31:57 -07002577
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +10002578 gfp_mask &= gfp_allowed_mask;
2579
Mel Gorman11e33f62009-06-16 15:31:57 -07002580 lockdep_trace_alloc(gfp_mask);
2581
2582 might_sleep_if(gfp_mask & __GFP_WAIT);
2583
2584 if (should_fail_alloc_page(gfp_mask, order))
2585 return NULL;
2586
2587 /*
2588 * Check the zones suitable for the gfp_mask contain at least one
2589 * valid zone. It's possible to have an empty zonelist as a result
2590 * of GFP_THISNODE and a memoryless node
2591 */
2592 if (unlikely(!zonelist->_zonerefs->zone))
2593 return NULL;
2594
Mel Gormancc9a6c82012-03-21 16:34:11 -07002595retry_cpuset:
2596 cpuset_mems_cookie = get_mems_allowed();
2597
Mel Gorman5117f452009-06-16 15:31:59 -07002598 /* The preferred zone is used for statistics later */
David Rientjesf33261d2011-01-25 15:07:20 -08002599 first_zones_zonelist(zonelist, high_zoneidx,
2600 nodemask ? : &cpuset_current_mems_allowed,
2601 &preferred_zone);
Mel Gormancc9a6c82012-03-21 16:34:11 -07002602 if (!preferred_zone)
2603 goto out;
Mel Gorman5117f452009-06-16 15:31:59 -07002604
2605 /* First allocation attempt */
Mel Gorman11e33f62009-06-16 15:31:57 -07002606 page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, nodemask, order,
Mel Gorman5117f452009-06-16 15:31:59 -07002607 zonelist, high_zoneidx, ALLOC_WMARK_LOW|ALLOC_CPUSET,
Mel Gorman3dd28262009-06-16 15:32:00 -07002608 preferred_zone, migratetype);
Mel Gorman11e33f62009-06-16 15:31:57 -07002609 if (unlikely(!page))
2610 page = __alloc_pages_slowpath(gfp_mask, order,
Mel Gorman5117f452009-06-16 15:31:59 -07002611 zonelist, high_zoneidx, nodemask,
Mel Gorman3dd28262009-06-16 15:32:00 -07002612 preferred_zone, migratetype);
Mel Gorman11e33f62009-06-16 15:31:57 -07002613
Mel Gorman4b4f2782009-09-21 17:02:41 -07002614 trace_mm_page_alloc(page, order, gfp_mask, migratetype);
Mel Gormancc9a6c82012-03-21 16:34:11 -07002615
2616out:
2617 /*
2618 * When updating a task's mems_allowed, it is possible to race with
2619 * parallel threads in such a way that an allocation can fail while
2620 * the mask is being updated. If a page allocation is about to fail,
2621 * check if the cpuset changed during allocation and if so, retry.
2622 */
2623 if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !page))
2624 goto retry_cpuset;
2625
Mel Gorman11e33f62009-06-16 15:31:57 -07002626 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627}
Mel Gormand2391712009-06-16 15:31:52 -07002628EXPORT_SYMBOL(__alloc_pages_nodemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
2630/*
2631 * Common helper functions.
2632 */
Harvey Harrison920c7a52008-02-04 22:29:26 -08002633unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634{
Akinobu Mita945a1112009-09-21 17:01:47 -07002635 struct page *page;
2636
2637 /*
2638 * __get_free_pages() returns a 32-bit address, which cannot represent
2639 * a highmem page
2640 */
2641 VM_BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
2642
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 page = alloc_pages(gfp_mask, order);
2644 if (!page)
2645 return 0;
2646 return (unsigned long) page_address(page);
2647}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648EXPORT_SYMBOL(__get_free_pages);
2649
Harvey Harrison920c7a52008-02-04 22:29:26 -08002650unsigned long get_zeroed_page(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651{
Akinobu Mita945a1112009-09-21 17:01:47 -07002652 return __get_free_pages(gfp_mask | __GFP_ZERO, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654EXPORT_SYMBOL(get_zeroed_page);
2655
Harvey Harrison920c7a52008-02-04 22:29:26 -08002656void __free_pages(struct page *page, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657{
Nick Pigginb5810032005-10-29 18:16:12 -07002658 if (put_page_testzero(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 if (order == 0)
Li Hongfc916682010-03-05 13:41:54 -08002660 free_hot_cold_page(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 else
2662 __free_pages_ok(page, order);
2663 }
2664}
2665
2666EXPORT_SYMBOL(__free_pages);
2667
Harvey Harrison920c7a52008-02-04 22:29:26 -08002668void free_pages(unsigned long addr, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669{
2670 if (addr != 0) {
Nick Piggin725d7042006-09-25 23:30:55 -07002671 VM_BUG_ON(!virt_addr_valid((void *)addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 __free_pages(virt_to_page((void *)addr), order);
2673 }
2674}
2675
2676EXPORT_SYMBOL(free_pages);
2677
Andi Kleenee85c2e2011-05-11 15:13:34 -07002678static void *make_alloc_exact(unsigned long addr, unsigned order, size_t size)
2679{
2680 if (addr) {
2681 unsigned long alloc_end = addr + (PAGE_SIZE << order);
2682 unsigned long used = addr + PAGE_ALIGN(size);
2683
2684 split_page(virt_to_page((void *)addr), order);
2685 while (used < alloc_end) {
2686 free_page(used);
2687 used += PAGE_SIZE;
2688 }
2689 }
2690 return (void *)addr;
2691}
2692
Timur Tabi2be0ffe2008-07-23 21:28:11 -07002693/**
2694 * alloc_pages_exact - allocate an exact number physically-contiguous pages.
2695 * @size: the number of bytes to allocate
2696 * @gfp_mask: GFP flags for the allocation
2697 *
2698 * This function is similar to alloc_pages(), except that it allocates the
2699 * minimum number of pages to satisfy the request. alloc_pages() can only
2700 * allocate memory in power-of-two pages.
2701 *
2702 * This function is also limited by MAX_ORDER.
2703 *
2704 * Memory allocated by this function must be released by free_pages_exact().
2705 */
2706void *alloc_pages_exact(size_t size, gfp_t gfp_mask)
2707{
2708 unsigned int order = get_order(size);
2709 unsigned long addr;
2710
2711 addr = __get_free_pages(gfp_mask, order);
Andi Kleenee85c2e2011-05-11 15:13:34 -07002712 return make_alloc_exact(addr, order, size);
Timur Tabi2be0ffe2008-07-23 21:28:11 -07002713}
2714EXPORT_SYMBOL(alloc_pages_exact);
2715
2716/**
Andi Kleenee85c2e2011-05-11 15:13:34 -07002717 * alloc_pages_exact_nid - allocate an exact number of physically-contiguous
2718 * pages on a node.
Randy Dunlapb5e6ab52011-05-16 13:16:54 -07002719 * @nid: the preferred node ID where memory should be allocated
Andi Kleenee85c2e2011-05-11 15:13:34 -07002720 * @size: the number of bytes to allocate
2721 * @gfp_mask: GFP flags for the allocation
2722 *
2723 * Like alloc_pages_exact(), but try to allocate on node nid first before falling
2724 * back.
2725 * Note this is not alloc_pages_exact_node() which allocates on a specific node,
2726 * but is not exact.
2727 */
2728void *alloc_pages_exact_nid(int nid, size_t size, gfp_t gfp_mask)
2729{
2730 unsigned order = get_order(size);
2731 struct page *p = alloc_pages_node(nid, gfp_mask, order);
2732 if (!p)
2733 return NULL;
2734 return make_alloc_exact((unsigned long)page_address(p), order, size);
2735}
2736EXPORT_SYMBOL(alloc_pages_exact_nid);
2737
2738/**
Timur Tabi2be0ffe2008-07-23 21:28:11 -07002739 * free_pages_exact - release memory allocated via alloc_pages_exact()
2740 * @virt: the value returned by alloc_pages_exact.
2741 * @size: size of allocation, same value as passed to alloc_pages_exact().
2742 *
2743 * Release the memory allocated by a previous call to alloc_pages_exact.
2744 */
2745void free_pages_exact(void *virt, size_t size)
2746{
2747 unsigned long addr = (unsigned long)virt;
2748 unsigned long end = addr + PAGE_ALIGN(size);
2749
2750 while (addr < end) {
2751 free_page(addr);
2752 addr += PAGE_SIZE;
2753 }
2754}
2755EXPORT_SYMBOL(free_pages_exact);
2756
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757static unsigned int nr_free_zone_pages(int offset)
2758{
Mel Gormandd1a2392008-04-28 02:12:17 -07002759 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07002760 struct zone *zone;
2761
Martin J. Blighe310fd42005-07-29 22:59:18 -07002762 /* Just pick one node, since fallback list is circular */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 unsigned int sum = 0;
2764
Mel Gorman0e884602008-04-28 02:12:14 -07002765 struct zonelist *zonelist = node_zonelist(numa_node_id(), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
Mel Gorman54a6eb52008-04-28 02:12:16 -07002767 for_each_zone_zonelist(zone, z, zonelist, offset) {
Martin J. Blighe310fd42005-07-29 22:59:18 -07002768 unsigned long size = zone->present_pages;
Mel Gorman41858962009-06-16 15:32:12 -07002769 unsigned long high = high_wmark_pages(zone);
Martin J. Blighe310fd42005-07-29 22:59:18 -07002770 if (size > high)
2771 sum += size - high;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 }
2773
2774 return sum;
2775}
2776
2777/*
2778 * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
2779 */
2780unsigned int nr_free_buffer_pages(void)
2781{
Al Viroaf4ca452005-10-21 02:55:38 -04002782 return nr_free_zone_pages(gfp_zone(GFP_USER));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783}
Meelap Shahc2f1a552007-07-17 04:04:39 -07002784EXPORT_SYMBOL_GPL(nr_free_buffer_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
2786/*
2787 * Amount of free RAM allocatable within all zones
2788 */
2789unsigned int nr_free_pagecache_pages(void)
2790{
Mel Gorman2a1e2742007-07-17 04:03:12 -07002791 return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER_MOVABLE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792}
Christoph Lameter08e0f6a2006-09-27 01:50:06 -07002793
2794static inline void show_node(struct zone *zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795{
Christoph Lameter08e0f6a2006-09-27 01:50:06 -07002796 if (NUMA_BUILD)
Andy Whitcroft25ba77c2006-12-06 20:33:03 -08002797 printk("Node %d ", zone_to_nid(zone));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800void si_meminfo(struct sysinfo *val)
2801{
2802 val->totalram = totalram_pages;
2803 val->sharedram = 0;
Christoph Lameterd23ad422007-02-10 01:43:02 -08002804 val->freeram = global_page_state(NR_FREE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 val->bufferram = nr_blockdev_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 val->totalhigh = totalhigh_pages;
2807 val->freehigh = nr_free_highpages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 val->mem_unit = PAGE_SIZE;
2809}
2810
2811EXPORT_SYMBOL(si_meminfo);
2812
2813#ifdef CONFIG_NUMA
2814void si_meminfo_node(struct sysinfo *val, int nid)
2815{
2816 pg_data_t *pgdat = NODE_DATA(nid);
2817
2818 val->totalram = pgdat->node_present_pages;
Christoph Lameterd23ad422007-02-10 01:43:02 -08002819 val->freeram = node_page_state(nid, NR_FREE_PAGES);
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07002820#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].present_pages;
Christoph Lameterd23ad422007-02-10 01:43:02 -08002822 val->freehigh = zone_page_state(&pgdat->node_zones[ZONE_HIGHMEM],
2823 NR_FREE_PAGES);
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07002824#else
2825 val->totalhigh = 0;
2826 val->freehigh = 0;
2827#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 val->mem_unit = PAGE_SIZE;
2829}
2830#endif
2831
David Rientjesddd588b2011-03-22 16:30:46 -07002832/*
David Rientjes7bf02ea2011-05-24 17:11:16 -07002833 * Determine whether the node should be displayed or not, depending on whether
2834 * SHOW_MEM_FILTER_NODES was passed to show_free_areas().
David Rientjesddd588b2011-03-22 16:30:46 -07002835 */
David Rientjes7bf02ea2011-05-24 17:11:16 -07002836bool skip_free_areas_node(unsigned int flags, int nid)
David Rientjesddd588b2011-03-22 16:30:46 -07002837{
2838 bool ret = false;
Mel Gormancc9a6c82012-03-21 16:34:11 -07002839 unsigned int cpuset_mems_cookie;
David Rientjesddd588b2011-03-22 16:30:46 -07002840
2841 if (!(flags & SHOW_MEM_FILTER_NODES))
2842 goto out;
2843
Mel Gormancc9a6c82012-03-21 16:34:11 -07002844 do {
2845 cpuset_mems_cookie = get_mems_allowed();
2846 ret = !node_isset(nid, cpuset_current_mems_allowed);
2847 } while (!put_mems_allowed(cpuset_mems_cookie));
David Rientjesddd588b2011-03-22 16:30:46 -07002848out:
2849 return ret;
2850}
2851
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852#define K(x) ((x) << (PAGE_SHIFT-10))
2853
2854/*
2855 * Show free area list (used inside shift_scroll-lock stuff)
2856 * We also calculate the percentage fragmentation. We do this by counting the
2857 * memory on each free list with the exception of the first item on the list.
David Rientjesddd588b2011-03-22 16:30:46 -07002858 * Suppresses nodes that are not allowed by current's cpuset if
2859 * SHOW_MEM_FILTER_NODES is passed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 */
David Rientjes7bf02ea2011-05-24 17:11:16 -07002861void show_free_areas(unsigned int filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862{
Jes Sorensenc7241912006-09-27 01:50:05 -07002863 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 struct zone *zone;
2865
KOSAKI Motohiroee99c712009-03-31 15:19:31 -07002866 for_each_populated_zone(zone) {
David Rientjes7bf02ea2011-05-24 17:11:16 -07002867 if (skip_free_areas_node(filter, zone_to_nid(zone)))
David Rientjesddd588b2011-03-22 16:30:46 -07002868 continue;
Jes Sorensenc7241912006-09-27 01:50:05 -07002869 show_node(zone);
2870 printk("%s per-cpu:\n", zone->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
Dave Jones6b482c62005-11-10 15:45:56 -05002872 for_each_online_cpu(cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 struct per_cpu_pageset *pageset;
2874
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09002875 pageset = per_cpu_ptr(zone->pageset, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
Christoph Lameter3dfa5722008-02-04 22:29:19 -08002877 printk("CPU %4d: hi:%5d, btch:%4d usd:%4d\n",
2878 cpu, pageset->pcp.high,
2879 pageset->pcp.batch, pageset->pcp.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 }
2881 }
2882
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07002883 printk("active_anon:%lu inactive_anon:%lu isolated_anon:%lu\n"
2884 " active_file:%lu inactive_file:%lu isolated_file:%lu\n"
Lee Schermerhorn7b854122008-10-18 20:26:40 -07002885 " unevictable:%lu"
Andrew Mortonb76146e2009-10-26 16:49:52 -07002886 " dirty:%lu writeback:%lu unstable:%lu\n"
KOSAKI Motohiro3701b032009-09-21 17:01:29 -07002887 " free:%lu slab_reclaimable:%lu slab_unreclaimable:%lu\n"
KOSAKI Motohiro4b021082009-09-21 17:01:33 -07002888 " mapped:%lu shmem:%lu pagetables:%lu bounce:%lu\n",
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002889 global_page_state(NR_ACTIVE_ANON),
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002890 global_page_state(NR_INACTIVE_ANON),
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07002891 global_page_state(NR_ISOLATED_ANON),
2892 global_page_state(NR_ACTIVE_FILE),
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002893 global_page_state(NR_INACTIVE_FILE),
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07002894 global_page_state(NR_ISOLATED_FILE),
Lee Schermerhorn7b854122008-10-18 20:26:40 -07002895 global_page_state(NR_UNEVICTABLE),
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -07002896 global_page_state(NR_FILE_DIRTY),
Christoph Lameterce866b32006-06-30 01:55:40 -07002897 global_page_state(NR_WRITEBACK),
Christoph Lameterfd39fc82006-06-30 01:55:40 -07002898 global_page_state(NR_UNSTABLE_NFS),
Christoph Lameterd23ad422007-02-10 01:43:02 -08002899 global_page_state(NR_FREE_PAGES),
KOSAKI Motohiro3701b032009-09-21 17:01:29 -07002900 global_page_state(NR_SLAB_RECLAIMABLE),
2901 global_page_state(NR_SLAB_UNRECLAIMABLE),
Christoph Lameter65ba55f2006-06-30 01:55:34 -07002902 global_page_state(NR_FILE_MAPPED),
KOSAKI Motohiro4b021082009-09-21 17:01:33 -07002903 global_page_state(NR_SHMEM),
Andrew Mortona25700a2007-02-08 14:20:40 -08002904 global_page_state(NR_PAGETABLE),
2905 global_page_state(NR_BOUNCE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
KOSAKI Motohiroee99c712009-03-31 15:19:31 -07002907 for_each_populated_zone(zone) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 int i;
2909
David Rientjes7bf02ea2011-05-24 17:11:16 -07002910 if (skip_free_areas_node(filter, zone_to_nid(zone)))
David Rientjesddd588b2011-03-22 16:30:46 -07002911 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 show_node(zone);
2913 printk("%s"
2914 " free:%lukB"
2915 " min:%lukB"
2916 " low:%lukB"
2917 " high:%lukB"
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002918 " active_anon:%lukB"
2919 " inactive_anon:%lukB"
2920 " active_file:%lukB"
2921 " inactive_file:%lukB"
Lee Schermerhorn7b854122008-10-18 20:26:40 -07002922 " unevictable:%lukB"
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07002923 " isolated(anon):%lukB"
2924 " isolated(file):%lukB"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 " present:%lukB"
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07002926 " mlocked:%lukB"
2927 " dirty:%lukB"
2928 " writeback:%lukB"
2929 " mapped:%lukB"
KOSAKI Motohiro4b021082009-09-21 17:01:33 -07002930 " shmem:%lukB"
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07002931 " slab_reclaimable:%lukB"
2932 " slab_unreclaimable:%lukB"
KOSAKI Motohiroc6a7f572009-09-21 17:01:32 -07002933 " kernel_stack:%lukB"
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07002934 " pagetables:%lukB"
2935 " unstable:%lukB"
2936 " bounce:%lukB"
2937 " writeback_tmp:%lukB"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 " pages_scanned:%lu"
2939 " all_unreclaimable? %s"
2940 "\n",
2941 zone->name,
Mel Gorman88f5acf2011-01-13 15:45:41 -08002942 K(zone_page_state(zone, NR_FREE_PAGES)),
Mel Gorman41858962009-06-16 15:32:12 -07002943 K(min_wmark_pages(zone)),
2944 K(low_wmark_pages(zone)),
2945 K(high_wmark_pages(zone)),
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002946 K(zone_page_state(zone, NR_ACTIVE_ANON)),
2947 K(zone_page_state(zone, NR_INACTIVE_ANON)),
2948 K(zone_page_state(zone, NR_ACTIVE_FILE)),
2949 K(zone_page_state(zone, NR_INACTIVE_FILE)),
Lee Schermerhorn7b854122008-10-18 20:26:40 -07002950 K(zone_page_state(zone, NR_UNEVICTABLE)),
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07002951 K(zone_page_state(zone, NR_ISOLATED_ANON)),
2952 K(zone_page_state(zone, NR_ISOLATED_FILE)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 K(zone->present_pages),
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07002954 K(zone_page_state(zone, NR_MLOCK)),
2955 K(zone_page_state(zone, NR_FILE_DIRTY)),
2956 K(zone_page_state(zone, NR_WRITEBACK)),
2957 K(zone_page_state(zone, NR_FILE_MAPPED)),
KOSAKI Motohiro4b021082009-09-21 17:01:33 -07002958 K(zone_page_state(zone, NR_SHMEM)),
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07002959 K(zone_page_state(zone, NR_SLAB_RECLAIMABLE)),
2960 K(zone_page_state(zone, NR_SLAB_UNRECLAIMABLE)),
KOSAKI Motohiroc6a7f572009-09-21 17:01:32 -07002961 zone_page_state(zone, NR_KERNEL_STACK) *
2962 THREAD_SIZE / 1024,
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07002963 K(zone_page_state(zone, NR_PAGETABLE)),
2964 K(zone_page_state(zone, NR_UNSTABLE_NFS)),
2965 K(zone_page_state(zone, NR_BOUNCE)),
2966 K(zone_page_state(zone, NR_WRITEBACK_TEMP)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 zone->pages_scanned,
KOSAKI Motohiro93e4a892010-03-05 13:41:55 -08002968 (zone->all_unreclaimable ? "yes" : "no")
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 );
2970 printk("lowmem_reserve[]:");
2971 for (i = 0; i < MAX_NR_ZONES; i++)
2972 printk(" %lu", zone->lowmem_reserve[i]);
2973 printk("\n");
2974 }
2975
KOSAKI Motohiroee99c712009-03-31 15:19:31 -07002976 for_each_populated_zone(zone) {
Kirill Korotaev8f9de512006-06-23 02:03:50 -07002977 unsigned long nr[MAX_ORDER], flags, order, total = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
David Rientjes7bf02ea2011-05-24 17:11:16 -07002979 if (skip_free_areas_node(filter, zone_to_nid(zone)))
David Rientjesddd588b2011-03-22 16:30:46 -07002980 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 show_node(zone);
2982 printk("%s: ", zone->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
2984 spin_lock_irqsave(&zone->lock, flags);
2985 for (order = 0; order < MAX_ORDER; order++) {
Kirill Korotaev8f9de512006-06-23 02:03:50 -07002986 nr[order] = zone->free_area[order].nr_free;
2987 total += nr[order] << order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 }
2989 spin_unlock_irqrestore(&zone->lock, flags);
Kirill Korotaev8f9de512006-06-23 02:03:50 -07002990 for (order = 0; order < MAX_ORDER; order++)
2991 printk("%lu*%lukB ", nr[order], K(1UL) << order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 printk("= %lukB\n", K(total));
2993 }
2994
Larry Woodmane6f36022008-02-04 22:29:30 -08002995 printk("%ld total pagecache pages\n", global_page_state(NR_FILE_PAGES));
2996
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 show_swap_cache_info();
2998}
2999
Mel Gorman19770b32008-04-28 02:12:18 -07003000static void zoneref_set_zone(struct zone *zone, struct zoneref *zoneref)
3001{
3002 zoneref->zone = zone;
3003 zoneref->zone_idx = zone_idx(zone);
3004}
3005
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006/*
3007 * Builds allocation fallback zone lists.
Christoph Lameter1a932052006-01-06 00:11:16 -08003008 *
3009 * Add all populated zones of a node to the zonelist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003011static int build_zonelists_node(pg_data_t *pgdat, struct zonelist *zonelist,
3012 int nr_zones, enum zone_type zone_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013{
Christoph Lameter1a932052006-01-06 00:11:16 -08003014 struct zone *zone;
3015
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07003016 BUG_ON(zone_type >= MAX_NR_ZONES);
Christoph Lameter2f6726e2006-09-25 23:31:18 -07003017 zone_type++;
Christoph Lameter02a68a52006-01-06 00:11:18 -08003018
3019 do {
Christoph Lameter2f6726e2006-09-25 23:31:18 -07003020 zone_type--;
Christoph Lameter070f8032006-01-06 00:11:19 -08003021 zone = pgdat->node_zones + zone_type;
Christoph Lameter1a932052006-01-06 00:11:16 -08003022 if (populated_zone(zone)) {
Mel Gormandd1a2392008-04-28 02:12:17 -07003023 zoneref_set_zone(zone,
3024 &zonelist->_zonerefs[nr_zones++]);
Christoph Lameter070f8032006-01-06 00:11:19 -08003025 check_highest_zone(zone_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 }
Christoph Lameter02a68a52006-01-06 00:11:18 -08003027
Christoph Lameter2f6726e2006-09-25 23:31:18 -07003028 } while (zone_type);
Christoph Lameter070f8032006-01-06 00:11:19 -08003029 return nr_zones;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030}
3031
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003032
3033/*
3034 * zonelist_order:
3035 * 0 = automatic detection of better ordering.
3036 * 1 = order by ([node] distance, -zonetype)
3037 * 2 = order by (-zonetype, [node] distance)
3038 *
3039 * If not NUMA, ZONELIST_ORDER_ZONE and ZONELIST_ORDER_NODE will create
3040 * the same zonelist. So only NUMA can configure this param.
3041 */
3042#define ZONELIST_ORDER_DEFAULT 0
3043#define ZONELIST_ORDER_NODE 1
3044#define ZONELIST_ORDER_ZONE 2
3045
3046/* zonelist order in the kernel.
3047 * set_zonelist_order() will set this to NODE or ZONE.
3048 */
3049static int current_zonelist_order = ZONELIST_ORDER_DEFAULT;
3050static char zonelist_order_name[3][8] = {"Default", "Node", "Zone"};
3051
3052
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053#ifdef CONFIG_NUMA
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003054/* The value user specified ....changed by config */
3055static int user_zonelist_order = ZONELIST_ORDER_DEFAULT;
3056/* string for sysctl */
3057#define NUMA_ZONELIST_ORDER_LEN 16
3058char numa_zonelist_order[16] = "default";
3059
3060/*
3061 * interface for configure zonelist ordering.
3062 * command line option "numa_zonelist_order"
3063 * = "[dD]efault - default, automatic configuration.
3064 * = "[nN]ode - order by node locality, then by zone within node
3065 * = "[zZ]one - order by zone, then by locality within zone
3066 */
3067
3068static int __parse_numa_zonelist_order(char *s)
3069{
3070 if (*s == 'd' || *s == 'D') {
3071 user_zonelist_order = ZONELIST_ORDER_DEFAULT;
3072 } else if (*s == 'n' || *s == 'N') {
3073 user_zonelist_order = ZONELIST_ORDER_NODE;
3074 } else if (*s == 'z' || *s == 'Z') {
3075 user_zonelist_order = ZONELIST_ORDER_ZONE;
3076 } else {
3077 printk(KERN_WARNING
3078 "Ignoring invalid numa_zonelist_order value: "
3079 "%s\n", s);
3080 return -EINVAL;
3081 }
3082 return 0;
3083}
3084
3085static __init int setup_numa_zonelist_order(char *s)
3086{
Volodymyr G. Lukiianykecb256f2011-01-13 15:46:26 -08003087 int ret;
3088
3089 if (!s)
3090 return 0;
3091
3092 ret = __parse_numa_zonelist_order(s);
3093 if (ret == 0)
3094 strlcpy(numa_zonelist_order, s, NUMA_ZONELIST_ORDER_LEN);
3095
3096 return ret;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003097}
3098early_param("numa_zonelist_order", setup_numa_zonelist_order);
3099
3100/*
3101 * sysctl handler for numa_zonelist_order
3102 */
3103int numa_zonelist_order_handler(ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003104 void __user *buffer, size_t *length,
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003105 loff_t *ppos)
3106{
3107 char saved_string[NUMA_ZONELIST_ORDER_LEN];
3108 int ret;
Andi Kleen443c6f12009-12-23 21:00:47 +01003109 static DEFINE_MUTEX(zl_order_mutex);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003110
Andi Kleen443c6f12009-12-23 21:00:47 +01003111 mutex_lock(&zl_order_mutex);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003112 if (write)
Andi Kleen443c6f12009-12-23 21:00:47 +01003113 strcpy(saved_string, (char*)table->data);
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003114 ret = proc_dostring(table, write, buffer, length, ppos);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003115 if (ret)
Andi Kleen443c6f12009-12-23 21:00:47 +01003116 goto out;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003117 if (write) {
3118 int oldval = user_zonelist_order;
3119 if (__parse_numa_zonelist_order((char*)table->data)) {
3120 /*
3121 * bogus value. restore saved string
3122 */
3123 strncpy((char*)table->data, saved_string,
3124 NUMA_ZONELIST_ORDER_LEN);
3125 user_zonelist_order = oldval;
Haicheng Li4eaf3f62010-05-24 14:32:52 -07003126 } else if (oldval != user_zonelist_order) {
3127 mutex_lock(&zonelists_mutex);
Jiang Liu9adb62a2012-07-31 16:43:28 -07003128 build_all_zonelists(NULL, NULL);
Haicheng Li4eaf3f62010-05-24 14:32:52 -07003129 mutex_unlock(&zonelists_mutex);
3130 }
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003131 }
Andi Kleen443c6f12009-12-23 21:00:47 +01003132out:
3133 mutex_unlock(&zl_order_mutex);
3134 return ret;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003135}
3136
3137
Christoph Lameter62bc62a2009-06-16 15:32:15 -07003138#define MAX_NODE_LOAD (nr_online_nodes)
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003139static int node_load[MAX_NUMNODES];
3140
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141/**
Pavel Pisa4dc3b162005-05-01 08:59:25 -07003142 * 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 -07003143 * @node: node whose fallback list we're appending
3144 * @used_node_mask: nodemask_t of already used nodes
3145 *
3146 * We use a number of factors to determine which is the next node that should
3147 * appear on a given node's fallback list. The node should not have appeared
3148 * already in @node's fallback list, and it should be the next closest node
3149 * according to the distance array (which contains arbitrary distance values
3150 * from each node to each node in the system), and should also prefer nodes
3151 * with no CPUs, since presumably they'll have very little allocation pressure
3152 * on them otherwise.
3153 * It returns -1 if no node is found.
3154 */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003155static int find_next_best_node(int node, nodemask_t *used_node_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156{
Linus Torvalds4cf808e2006-02-17 20:38:21 +01003157 int n, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 int min_val = INT_MAX;
3159 int best_node = -1;
Rusty Russella70f7302009-03-13 14:49:46 +10303160 const struct cpumask *tmp = cpumask_of_node(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
Linus Torvalds4cf808e2006-02-17 20:38:21 +01003162 /* Use the local node if we haven't already */
3163 if (!node_isset(node, *used_node_mask)) {
3164 node_set(node, *used_node_mask);
3165 return node;
3166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07003168 for_each_node_state(n, N_HIGH_MEMORY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169
3170 /* Don't want a node to appear more than once */
3171 if (node_isset(n, *used_node_mask))
3172 continue;
3173
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 /* Use the distance array to find the distance */
3175 val = node_distance(node, n);
3176
Linus Torvalds4cf808e2006-02-17 20:38:21 +01003177 /* Penalize nodes under us ("prefer the next node") */
3178 val += (n < node);
3179
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 /* Give preference to headless and unused nodes */
Rusty Russella70f7302009-03-13 14:49:46 +10303181 tmp = cpumask_of_node(n);
3182 if (!cpumask_empty(tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 val += PENALTY_FOR_NODE_WITH_CPUS;
3184
3185 /* Slight preference for less loaded node */
3186 val *= (MAX_NODE_LOAD*MAX_NUMNODES);
3187 val += node_load[n];
3188
3189 if (val < min_val) {
3190 min_val = val;
3191 best_node = n;
3192 }
3193 }
3194
3195 if (best_node >= 0)
3196 node_set(best_node, *used_node_mask);
3197
3198 return best_node;
3199}
3200
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003201
3202/*
3203 * Build zonelists ordered by node and zones within node.
3204 * This results in maximum locality--normal zone overflows into local
3205 * DMA zone, if any--but risks exhausting DMA zone.
3206 */
3207static void build_zonelists_in_node_order(pg_data_t *pgdat, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208{
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003209 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 struct zonelist *zonelist;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003211
Mel Gorman54a6eb52008-04-28 02:12:16 -07003212 zonelist = &pgdat->node_zonelists[0];
Mel Gormandd1a2392008-04-28 02:12:17 -07003213 for (j = 0; zonelist->_zonerefs[j].zone != NULL; j++)
Mel Gorman54a6eb52008-04-28 02:12:16 -07003214 ;
3215 j = build_zonelists_node(NODE_DATA(node), zonelist, j,
3216 MAX_NR_ZONES - 1);
Mel Gormandd1a2392008-04-28 02:12:17 -07003217 zonelist->_zonerefs[j].zone = NULL;
3218 zonelist->_zonerefs[j].zone_idx = 0;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003219}
3220
3221/*
Christoph Lameter523b9452007-10-16 01:25:37 -07003222 * Build gfp_thisnode zonelists
3223 */
3224static void build_thisnode_zonelists(pg_data_t *pgdat)
3225{
Christoph Lameter523b9452007-10-16 01:25:37 -07003226 int j;
3227 struct zonelist *zonelist;
3228
Mel Gorman54a6eb52008-04-28 02:12:16 -07003229 zonelist = &pgdat->node_zonelists[1];
3230 j = build_zonelists_node(pgdat, zonelist, 0, MAX_NR_ZONES - 1);
Mel Gormandd1a2392008-04-28 02:12:17 -07003231 zonelist->_zonerefs[j].zone = NULL;
3232 zonelist->_zonerefs[j].zone_idx = 0;
Christoph Lameter523b9452007-10-16 01:25:37 -07003233}
3234
3235/*
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003236 * Build zonelists ordered by zone and nodes within zones.
3237 * This results in conserving DMA zone[s] until all Normal memory is
3238 * exhausted, but results in overflowing to remote node while memory
3239 * may still exist in local DMA zone.
3240 */
3241static int node_order[MAX_NUMNODES];
3242
3243static void build_zonelists_in_zone_order(pg_data_t *pgdat, int nr_nodes)
3244{
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003245 int pos, j, node;
3246 int zone_type; /* needs to be signed */
3247 struct zone *z;
3248 struct zonelist *zonelist;
3249
Mel Gorman54a6eb52008-04-28 02:12:16 -07003250 zonelist = &pgdat->node_zonelists[0];
3251 pos = 0;
3252 for (zone_type = MAX_NR_ZONES - 1; zone_type >= 0; zone_type--) {
3253 for (j = 0; j < nr_nodes; j++) {
3254 node = node_order[j];
3255 z = &NODE_DATA(node)->node_zones[zone_type];
3256 if (populated_zone(z)) {
Mel Gormandd1a2392008-04-28 02:12:17 -07003257 zoneref_set_zone(z,
3258 &zonelist->_zonerefs[pos++]);
Mel Gorman54a6eb52008-04-28 02:12:16 -07003259 check_highest_zone(zone_type);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003260 }
3261 }
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003262 }
Mel Gormandd1a2392008-04-28 02:12:17 -07003263 zonelist->_zonerefs[pos].zone = NULL;
3264 zonelist->_zonerefs[pos].zone_idx = 0;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003265}
3266
3267static int default_zonelist_order(void)
3268{
3269 int nid, zone_type;
3270 unsigned long low_kmem_size,total_size;
3271 struct zone *z;
3272 int average_size;
3273 /*
Thomas Weber88393162010-03-16 11:47:56 +01003274 * ZONE_DMA and ZONE_DMA32 can be very small area in the system.
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003275 * If they are really small and used heavily, the system can fall
3276 * into OOM very easily.
David Rientjese325c902010-05-24 14:32:13 -07003277 * This function detect ZONE_DMA/DMA32 size and configures zone order.
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003278 */
3279 /* Is there ZONE_NORMAL ? (ex. ppc has only DMA zone..) */
3280 low_kmem_size = 0;
3281 total_size = 0;
3282 for_each_online_node(nid) {
3283 for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
3284 z = &NODE_DATA(nid)->node_zones[zone_type];
3285 if (populated_zone(z)) {
3286 if (zone_type < ZONE_NORMAL)
3287 low_kmem_size += z->present_pages;
3288 total_size += z->present_pages;
David Rientjese325c902010-05-24 14:32:13 -07003289 } else if (zone_type == ZONE_NORMAL) {
3290 /*
3291 * If any node has only lowmem, then node order
3292 * is preferred to allow kernel allocations
3293 * locally; otherwise, they can easily infringe
3294 * on other nodes when there is an abundance of
3295 * lowmem available to allocate from.
3296 */
3297 return ZONELIST_ORDER_NODE;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003298 }
3299 }
3300 }
3301 if (!low_kmem_size || /* there are no DMA area. */
3302 low_kmem_size > total_size/2) /* DMA/DMA32 is big. */
3303 return ZONELIST_ORDER_NODE;
3304 /*
3305 * look into each node's config.
3306 * If there is a node whose DMA/DMA32 memory is very big area on
3307 * local memory, NODE_ORDER may be suitable.
3308 */
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07003309 average_size = total_size /
3310 (nodes_weight(node_states[N_HIGH_MEMORY]) + 1);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003311 for_each_online_node(nid) {
3312 low_kmem_size = 0;
3313 total_size = 0;
3314 for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
3315 z = &NODE_DATA(nid)->node_zones[zone_type];
3316 if (populated_zone(z)) {
3317 if (zone_type < ZONE_NORMAL)
3318 low_kmem_size += z->present_pages;
3319 total_size += z->present_pages;
3320 }
3321 }
3322 if (low_kmem_size &&
3323 total_size > average_size && /* ignore small node */
3324 low_kmem_size > total_size * 70/100)
3325 return ZONELIST_ORDER_NODE;
3326 }
3327 return ZONELIST_ORDER_ZONE;
3328}
3329
3330static void set_zonelist_order(void)
3331{
3332 if (user_zonelist_order == ZONELIST_ORDER_DEFAULT)
3333 current_zonelist_order = default_zonelist_order();
3334 else
3335 current_zonelist_order = user_zonelist_order;
3336}
3337
3338static void build_zonelists(pg_data_t *pgdat)
3339{
3340 int j, node, load;
3341 enum zone_type i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 nodemask_t used_mask;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003343 int local_node, prev_node;
3344 struct zonelist *zonelist;
3345 int order = current_zonelist_order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346
3347 /* initialize zonelists */
Christoph Lameter523b9452007-10-16 01:25:37 -07003348 for (i = 0; i < MAX_ZONELISTS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 zonelist = pgdat->node_zonelists + i;
Mel Gormandd1a2392008-04-28 02:12:17 -07003350 zonelist->_zonerefs[0].zone = NULL;
3351 zonelist->_zonerefs[0].zone_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 }
3353
3354 /* NUMA-aware ordering of nodes */
3355 local_node = pgdat->node_id;
Christoph Lameter62bc62a2009-06-16 15:32:15 -07003356 load = nr_online_nodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 prev_node = local_node;
3358 nodes_clear(used_mask);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003359
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003360 memset(node_order, 0, sizeof(node_order));
3361 j = 0;
3362
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
Christoph Lameter9eeff232006-01-18 17:42:31 -08003364 int distance = node_distance(local_node, node);
3365
3366 /*
3367 * If another node is sufficiently far away then it is better
3368 * to reclaim pages in a zone before going off node.
3369 */
3370 if (distance > RECLAIM_DISTANCE)
3371 zone_reclaim_mode = 1;
3372
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 /*
3374 * We don't want to pressure a particular node.
3375 * So adding penalty to the first node in same
3376 * distance group to make it round-robin.
3377 */
Christoph Lameter9eeff232006-01-18 17:42:31 -08003378 if (distance != node_distance(local_node, prev_node))
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003379 node_load[node] = load;
3380
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 prev_node = node;
3382 load--;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003383 if (order == ZONELIST_ORDER_NODE)
3384 build_zonelists_in_node_order(pgdat, node);
3385 else
3386 node_order[j++] = node; /* remember order */
3387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003389 if (order == ZONELIST_ORDER_ZONE) {
3390 /* calculate node order -- i.e., DMA last! */
3391 build_zonelists_in_zone_order(pgdat, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 }
Christoph Lameter523b9452007-10-16 01:25:37 -07003393
3394 build_thisnode_zonelists(pgdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395}
3396
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003397/* Construct the zonelist performance cache - see further mmzone.h */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003398static void build_zonelist_cache(pg_data_t *pgdat)
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003399{
Mel Gorman54a6eb52008-04-28 02:12:16 -07003400 struct zonelist *zonelist;
3401 struct zonelist_cache *zlc;
Mel Gormandd1a2392008-04-28 02:12:17 -07003402 struct zoneref *z;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003403
Mel Gorman54a6eb52008-04-28 02:12:16 -07003404 zonelist = &pgdat->node_zonelists[0];
3405 zonelist->zlcache_ptr = zlc = &zonelist->zlcache;
3406 bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
Mel Gormandd1a2392008-04-28 02:12:17 -07003407 for (z = zonelist->_zonerefs; z->zone; z++)
3408 zlc->z_to_n[z - zonelist->_zonerefs] = zonelist_node_idx(z);
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003409}
3410
Lee Schermerhorn7aac7892010-05-26 14:45:00 -07003411#ifdef CONFIG_HAVE_MEMORYLESS_NODES
3412/*
3413 * Return node id of node used for "local" allocations.
3414 * I.e., first node id of first zone in arg node's generic zonelist.
3415 * Used for initializing percpu 'numa_mem', which is used primarily
3416 * for kernel allocations, so use GFP_KERNEL flags to locate zonelist.
3417 */
3418int local_memory_node(int node)
3419{
3420 struct zone *zone;
3421
3422 (void)first_zones_zonelist(node_zonelist(node, GFP_KERNEL),
3423 gfp_zone(GFP_KERNEL),
3424 NULL,
3425 &zone);
3426 return zone->node;
3427}
3428#endif
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003429
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430#else /* CONFIG_NUMA */
3431
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003432static void set_zonelist_order(void)
3433{
3434 current_zonelist_order = ZONELIST_ORDER_ZONE;
3435}
3436
3437static void build_zonelists(pg_data_t *pgdat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438{
Christoph Lameter19655d32006-09-25 23:31:19 -07003439 int node, local_node;
Mel Gorman54a6eb52008-04-28 02:12:16 -07003440 enum zone_type j;
3441 struct zonelist *zonelist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442
3443 local_node = pgdat->node_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
Mel Gorman54a6eb52008-04-28 02:12:16 -07003445 zonelist = &pgdat->node_zonelists[0];
3446 j = build_zonelists_node(pgdat, zonelist, 0, MAX_NR_ZONES - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447
Mel Gorman54a6eb52008-04-28 02:12:16 -07003448 /*
3449 * Now we build the zonelist so that it contains the zones
3450 * of all the other nodes.
3451 * We don't want to pressure a particular node, so when
3452 * building the zones for node N, we make sure that the
3453 * zones coming right after the local ones are those from
3454 * node N+1 (modulo N)
3455 */
3456 for (node = local_node + 1; node < MAX_NUMNODES; node++) {
3457 if (!node_online(node))
3458 continue;
3459 j = build_zonelists_node(NODE_DATA(node), zonelist, j,
3460 MAX_NR_ZONES - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 }
Mel Gorman54a6eb52008-04-28 02:12:16 -07003462 for (node = 0; node < local_node; node++) {
3463 if (!node_online(node))
3464 continue;
3465 j = build_zonelists_node(NODE_DATA(node), zonelist, j,
3466 MAX_NR_ZONES - 1);
3467 }
3468
Mel Gormandd1a2392008-04-28 02:12:17 -07003469 zonelist->_zonerefs[j].zone = NULL;
3470 zonelist->_zonerefs[j].zone_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471}
3472
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003473/* non-NUMA variant of zonelist performance cache - just NULL zlcache_ptr */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003474static void build_zonelist_cache(pg_data_t *pgdat)
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003475{
Mel Gorman54a6eb52008-04-28 02:12:16 -07003476 pgdat->node_zonelists[0].zlcache_ptr = NULL;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003477}
3478
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479#endif /* CONFIG_NUMA */
3480
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09003481/*
3482 * Boot pageset table. One per cpu which is going to be used for all
3483 * zones and all nodes. The parameters will be set in such a way
3484 * that an item put on a list will immediately be handed over to
3485 * the buddy list. This is safe since pageset manipulation is done
3486 * with interrupts disabled.
3487 *
3488 * The boot_pagesets must be kept even after bootup is complete for
3489 * unused processors and/or zones. They do play a role for bootstrapping
3490 * hotplugged processors.
3491 *
3492 * zoneinfo_show() and maybe other functions do
3493 * not check if the processor is online before following the pageset pointer.
3494 * Other parts of the kernel may not check if the zone is available.
3495 */
3496static void setup_pageset(struct per_cpu_pageset *p, unsigned long batch);
3497static DEFINE_PER_CPU(struct per_cpu_pageset, boot_pageset);
Haicheng Li1f522502010-05-24 14:32:51 -07003498static void setup_zone_pageset(struct zone *zone);
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09003499
Haicheng Li4eaf3f62010-05-24 14:32:52 -07003500/*
3501 * Global mutex to protect against size modification of zonelists
3502 * as well as to serialize pageset setup for the new populated zone.
3503 */
3504DEFINE_MUTEX(zonelists_mutex);
3505
Rusty Russell9b1a4d32008-07-28 12:16:30 -05003506/* return values int ....just for stop_machine() */
Jiang Liu4ed7e022012-07-31 16:43:35 -07003507static int __build_all_zonelists(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508{
Yasunori Goto68113782006-06-23 02:03:11 -07003509 int nid;
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09003510 int cpu;
Jiang Liu9adb62a2012-07-31 16:43:28 -07003511 pg_data_t *self = data;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003512
Bo Liu7f9cfb32009-08-18 14:11:19 -07003513#ifdef CONFIG_NUMA
3514 memset(node_load, 0, sizeof(node_load));
3515#endif
Jiang Liu9adb62a2012-07-31 16:43:28 -07003516
3517 if (self && !node_online(self->node_id)) {
3518 build_zonelists(self);
3519 build_zonelist_cache(self);
3520 }
3521
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003522 for_each_online_node(nid) {
Christoph Lameter7ea15302007-10-16 01:25:29 -07003523 pg_data_t *pgdat = NODE_DATA(nid);
3524
3525 build_zonelists(pgdat);
3526 build_zonelist_cache(pgdat);
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003527 }
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09003528
3529 /*
3530 * Initialize the boot_pagesets that are going to be used
3531 * for bootstrapping processors. The real pagesets for
3532 * each zone will be allocated later when the per cpu
3533 * allocator is available.
3534 *
3535 * boot_pagesets are used also for bootstrapping offline
3536 * cpus if the system is already booted because the pagesets
3537 * are needed to initialize allocators on a specific cpu too.
3538 * F.e. the percpu allocator needs the page allocator which
3539 * needs the percpu allocator in order to allocate its pagesets
3540 * (a chicken-egg dilemma).
3541 */
Lee Schermerhorn7aac7892010-05-26 14:45:00 -07003542 for_each_possible_cpu(cpu) {
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09003543 setup_pageset(&per_cpu(boot_pageset, cpu), 0);
3544
Lee Schermerhorn7aac7892010-05-26 14:45:00 -07003545#ifdef CONFIG_HAVE_MEMORYLESS_NODES
3546 /*
3547 * We now know the "local memory node" for each node--
3548 * i.e., the node of the first zone in the generic zonelist.
3549 * Set up numa_mem percpu variable for on-line cpus. During
3550 * boot, only the boot cpu should be on-line; we'll init the
3551 * secondary cpus' numa_mem as they come on-line. During
3552 * node/memory hotplug, we'll fixup all on-line cpus.
3553 */
3554 if (cpu_online(cpu))
3555 set_cpu_numa_mem(cpu, local_memory_node(cpu_to_node(cpu)));
3556#endif
3557 }
3558
Yasunori Goto68113782006-06-23 02:03:11 -07003559 return 0;
3560}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561
Haicheng Li4eaf3f62010-05-24 14:32:52 -07003562/*
3563 * Called with zonelists_mutex held always
3564 * unless system_state == SYSTEM_BOOTING.
3565 */
Jiang Liu9adb62a2012-07-31 16:43:28 -07003566void __ref build_all_zonelists(pg_data_t *pgdat, struct zone *zone)
Yasunori Goto68113782006-06-23 02:03:11 -07003567{
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003568 set_zonelist_order();
3569
Yasunori Goto68113782006-06-23 02:03:11 -07003570 if (system_state == SYSTEM_BOOTING) {
Randy Dunlap423b41d2006-09-27 01:50:12 -07003571 __build_all_zonelists(NULL);
Mel Gorman68ad8df2008-07-23 21:26:52 -07003572 mminit_verify_zonelist();
Yasunori Goto68113782006-06-23 02:03:11 -07003573 cpuset_init_current_mems_allowed();
3574 } else {
Simon Arlott183ff222007-10-20 01:27:18 +02003575 /* we have to stop all cpus to guarantee there is no user
Yasunori Goto68113782006-06-23 02:03:11 -07003576 of zonelist */
KAMEZAWA Hiroyukie9959f02010-11-24 12:57:09 -08003577#ifdef CONFIG_MEMORY_HOTPLUG
Jiang Liu9adb62a2012-07-31 16:43:28 -07003578 if (zone)
3579 setup_zone_pageset(zone);
KAMEZAWA Hiroyukie9959f02010-11-24 12:57:09 -08003580#endif
Jiang Liu9adb62a2012-07-31 16:43:28 -07003581 stop_machine(__build_all_zonelists, pgdat, NULL);
Yasunori Goto68113782006-06-23 02:03:11 -07003582 /* cpuset refresh routine should be here */
3583 }
Andrew Mortonbd1e22b2006-06-23 02:03:47 -07003584 vm_total_pages = nr_free_pagecache_pages();
Mel Gorman9ef9acb2007-10-16 01:25:54 -07003585 /*
3586 * Disable grouping by mobility if the number of pages in the
3587 * system is too low to allow the mechanism to work. It would be
3588 * more accurate, but expensive to check per-zone. This check is
3589 * made on memory-hotadd so a system can start with mobility
3590 * disabled and enable it later
3591 */
Mel Gormand9c23402007-10-16 01:26:01 -07003592 if (vm_total_pages < (pageblock_nr_pages * MIGRATE_TYPES))
Mel Gorman9ef9acb2007-10-16 01:25:54 -07003593 page_group_by_mobility_disabled = 1;
3594 else
3595 page_group_by_mobility_disabled = 0;
3596
3597 printk("Built %i zonelists in %s order, mobility grouping %s. "
3598 "Total pages: %ld\n",
Christoph Lameter62bc62a2009-06-16 15:32:15 -07003599 nr_online_nodes,
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003600 zonelist_order_name[current_zonelist_order],
Mel Gorman9ef9acb2007-10-16 01:25:54 -07003601 page_group_by_mobility_disabled ? "off" : "on",
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003602 vm_total_pages);
3603#ifdef CONFIG_NUMA
3604 printk("Policy zone: %s\n", zone_names[policy_zone]);
3605#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606}
3607
3608/*
3609 * Helper functions to size the waitqueue hash table.
3610 * Essentially these want to choose hash table sizes sufficiently
3611 * large so that collisions trying to wait on pages are rare.
3612 * But in fact, the number of active page waitqueues on typical
3613 * systems is ridiculously low, less than 200. So this is even
3614 * conservative, even though it seems large.
3615 *
3616 * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
3617 * waitqueues, i.e. the size of the waitq table given the number of pages.
3618 */
3619#define PAGES_PER_WAITQUEUE 256
3620
Yasunori Gotocca448f2006-06-23 02:03:10 -07003621#ifndef CONFIG_MEMORY_HOTPLUG
Yasunori Goto02b694d2006-06-23 02:03:08 -07003622static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623{
3624 unsigned long size = 1;
3625
3626 pages /= PAGES_PER_WAITQUEUE;
3627
3628 while (size < pages)
3629 size <<= 1;
3630
3631 /*
3632 * Once we have dozens or even hundreds of threads sleeping
3633 * on IO we've got bigger problems than wait queue collision.
3634 * Limit the size of the wait table to a reasonable size.
3635 */
3636 size = min(size, 4096UL);
3637
3638 return max(size, 4UL);
3639}
Yasunori Gotocca448f2006-06-23 02:03:10 -07003640#else
3641/*
3642 * A zone's size might be changed by hot-add, so it is not possible to determine
3643 * a suitable size for its wait_table. So we use the maximum size now.
3644 *
3645 * The max wait table size = 4096 x sizeof(wait_queue_head_t). ie:
3646 *
3647 * i386 (preemption config) : 4096 x 16 = 64Kbyte.
3648 * ia64, x86-64 (no preemption): 4096 x 20 = 80Kbyte.
3649 * ia64, x86-64 (preemption) : 4096 x 24 = 96Kbyte.
3650 *
3651 * The maximum entries are prepared when a zone's memory is (512K + 256) pages
3652 * or more by the traditional way. (See above). It equals:
3653 *
3654 * i386, x86-64, powerpc(4K page size) : = ( 2G + 1M)byte.
3655 * ia64(16K page size) : = ( 8G + 4M)byte.
3656 * powerpc (64K page size) : = (32G +16M)byte.
3657 */
3658static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
3659{
3660 return 4096UL;
3661}
3662#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663
3664/*
3665 * This is an integer logarithm so that shifts can be used later
3666 * to extract the more random high bits from the multiplicative
3667 * hash function before the remainder is taken.
3668 */
3669static inline unsigned long wait_table_bits(unsigned long size)
3670{
3671 return ffz(~size);
3672}
3673
3674#define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
3675
Mel Gorman56fd56b2007-10-16 01:25:58 -07003676/*
Arve Hjønnevåg6d3163c2011-05-24 17:12:24 -07003677 * Check if a pageblock contains reserved pages
3678 */
3679static int pageblock_is_reserved(unsigned long start_pfn, unsigned long end_pfn)
3680{
3681 unsigned long pfn;
3682
3683 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
3684 if (!pfn_valid_within(pfn) || PageReserved(pfn_to_page(pfn)))
3685 return 1;
3686 }
3687 return 0;
3688}
3689
3690/*
Mel Gormand9c23402007-10-16 01:26:01 -07003691 * Mark a number of pageblocks as MIGRATE_RESERVE. The number
Mel Gorman41858962009-06-16 15:32:12 -07003692 * of blocks reserved is based on min_wmark_pages(zone). The memory within
3693 * the reserve will tend to store contiguous free pages. Setting min_free_kbytes
Mel Gorman56fd56b2007-10-16 01:25:58 -07003694 * higher will lead to a bigger reserve which will get freed as contiguous
3695 * blocks as reclaim kicks in
3696 */
3697static void setup_zone_migrate_reserve(struct zone *zone)
3698{
Arve Hjønnevåg6d3163c2011-05-24 17:12:24 -07003699 unsigned long start_pfn, pfn, end_pfn, block_end_pfn;
Mel Gorman56fd56b2007-10-16 01:25:58 -07003700 struct page *page;
Mel Gorman78986a62009-09-21 17:03:02 -07003701 unsigned long block_migratetype;
3702 int reserve;
Mel Gorman56fd56b2007-10-16 01:25:58 -07003703
Michal Hockod02156382011-12-08 14:34:27 -08003704 /*
3705 * Get the start pfn, end pfn and the number of blocks to reserve
3706 * We have to be careful to be aligned to pageblock_nr_pages to
3707 * make sure that we always check pfn_valid for the first page in
3708 * the block.
3709 */
Mel Gorman56fd56b2007-10-16 01:25:58 -07003710 start_pfn = zone->zone_start_pfn;
3711 end_pfn = start_pfn + zone->spanned_pages;
Michal Hockod02156382011-12-08 14:34:27 -08003712 start_pfn = roundup(start_pfn, pageblock_nr_pages);
Mel Gorman41858962009-06-16 15:32:12 -07003713 reserve = roundup(min_wmark_pages(zone), pageblock_nr_pages) >>
Mel Gormand9c23402007-10-16 01:26:01 -07003714 pageblock_order;
Mel Gorman56fd56b2007-10-16 01:25:58 -07003715
Mel Gorman78986a62009-09-21 17:03:02 -07003716 /*
3717 * Reserve blocks are generally in place to help high-order atomic
3718 * allocations that are short-lived. A min_free_kbytes value that
3719 * would result in more than 2 reserve blocks for atomic allocations
3720 * is assumed to be in place to help anti-fragmentation for the
3721 * future allocation of hugepages at runtime.
3722 */
3723 reserve = min(2, reserve);
3724
Mel Gormand9c23402007-10-16 01:26:01 -07003725 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
Mel Gorman56fd56b2007-10-16 01:25:58 -07003726 if (!pfn_valid(pfn))
3727 continue;
3728 page = pfn_to_page(pfn);
3729
Adam Litke344c7902008-09-02 14:35:38 -07003730 /* Watch out for overlapping nodes */
3731 if (page_to_nid(page) != zone_to_nid(zone))
3732 continue;
3733
Mel Gorman56fd56b2007-10-16 01:25:58 -07003734 block_migratetype = get_pageblock_migratetype(page);
3735
Mel Gorman938929f2012-01-10 15:07:14 -08003736 /* Only test what is necessary when the reserves are not met */
3737 if (reserve > 0) {
3738 /*
3739 * Blocks with reserved pages will never free, skip
3740 * them.
3741 */
3742 block_end_pfn = min(pfn + pageblock_nr_pages, end_pfn);
3743 if (pageblock_is_reserved(pfn, block_end_pfn))
3744 continue;
Mel Gorman56fd56b2007-10-16 01:25:58 -07003745
Mel Gorman938929f2012-01-10 15:07:14 -08003746 /* If this block is reserved, account for it */
3747 if (block_migratetype == MIGRATE_RESERVE) {
3748 reserve--;
3749 continue;
3750 }
3751
3752 /* Suitable for reserving if this block is movable */
3753 if (block_migratetype == MIGRATE_MOVABLE) {
3754 set_pageblock_migratetype(page,
3755 MIGRATE_RESERVE);
3756 move_freepages_block(zone, page,
3757 MIGRATE_RESERVE);
3758 reserve--;
3759 continue;
3760 }
Mel Gorman56fd56b2007-10-16 01:25:58 -07003761 }
3762
3763 /*
3764 * If the reserve is met and this is a previous reserved block,
3765 * take it back
3766 */
3767 if (block_migratetype == MIGRATE_RESERVE) {
3768 set_pageblock_migratetype(page, MIGRATE_MOVABLE);
3769 move_freepages_block(zone, page, MIGRATE_MOVABLE);
3770 }
3771 }
3772}
Mel Gormanac0e5b72007-10-16 01:25:58 -07003773
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774/*
3775 * Initially all pages are reserved - free ones are freed
3776 * up by free_all_bootmem() once the early boot process is
3777 * done. Non-atomic initialization, single-pass.
3778 */
Matt Tolentinoc09b4242006-01-17 07:03:44 +01003779void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
Dave Hansena2f3aa022007-01-10 23:15:30 -08003780 unsigned long start_pfn, enum memmap_context context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 struct page *page;
Andy Whitcroft29751f62005-06-23 00:08:00 -07003783 unsigned long end_pfn = start_pfn + size;
3784 unsigned long pfn;
KAMEZAWA Hiroyuki86051ca2008-04-29 00:58:21 -07003785 struct zone *z;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786
Hugh Dickins22b31ee2009-01-06 14:40:09 -08003787 if (highest_memmap_pfn < end_pfn - 1)
3788 highest_memmap_pfn = end_pfn - 1;
3789
KAMEZAWA Hiroyuki86051ca2008-04-29 00:58:21 -07003790 z = &NODE_DATA(nid)->node_zones[zone];
Greg Ungerercbe8dd42006-01-12 01:05:24 -08003791 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
Dave Hansena2f3aa022007-01-10 23:15:30 -08003792 /*
3793 * There can be holes in boot-time mem_map[]s
3794 * handed to this function. They do not
3795 * exist on hotplugged memory.
3796 */
3797 if (context == MEMMAP_EARLY) {
3798 if (!early_pfn_valid(pfn))
3799 continue;
3800 if (!early_pfn_in_nid(pfn, nid))
3801 continue;
3802 }
Andy Whitcroftd41dee32005-06-23 00:07:54 -07003803 page = pfn_to_page(pfn);
3804 set_page_links(page, zone, nid, pfn);
Mel Gorman708614e2008-07-23 21:26:51 -07003805 mminit_verify_page_links(page, zone, nid, pfn);
Nick Piggin7835e982006-03-22 00:08:40 -08003806 init_page_count(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 reset_page_mapcount(page);
3808 SetPageReserved(page);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07003809 /*
3810 * Mark the block movable so that blocks are reserved for
3811 * movable at startup. This will force kernel allocations
3812 * to reserve their blocks rather than leaking throughout
3813 * the address space during boot when many long-lived
Mel Gorman56fd56b2007-10-16 01:25:58 -07003814 * kernel allocations are made. Later some blocks near
3815 * the start are marked MIGRATE_RESERVE by
3816 * setup_zone_migrate_reserve()
KAMEZAWA Hiroyuki86051ca2008-04-29 00:58:21 -07003817 *
3818 * bitmap is created for zone's valid pfn range. but memmap
3819 * can be created for invalid pages (for alignment)
3820 * check here not to call set_pageblock_migratetype() against
3821 * pfn out of zone.
Mel Gormanb2a0ac82007-10-16 01:25:48 -07003822 */
KAMEZAWA Hiroyuki86051ca2008-04-29 00:58:21 -07003823 if ((z->zone_start_pfn <= pfn)
3824 && (pfn < z->zone_start_pfn + z->spanned_pages)
3825 && !(pfn & (pageblock_nr_pages - 1)))
Mel Gorman56fd56b2007-10-16 01:25:58 -07003826 set_pageblock_migratetype(page, MIGRATE_MOVABLE);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07003827
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 INIT_LIST_HEAD(&page->lru);
3829#ifdef WANT_PAGE_VIRTUAL
3830 /* The shift won't overflow because ZONE_NORMAL is below 4G. */
3831 if (!is_highmem_idx(zone))
Bob Picco3212c6b2005-06-27 14:36:28 -07003832 set_page_address(page, __va(pfn << PAGE_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 }
3835}
3836
Andi Kleen1e548de2008-02-04 22:29:26 -08003837static void __meminit zone_init_free_lists(struct zone *zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838{
Mel Gormanb2a0ac82007-10-16 01:25:48 -07003839 int order, t;
3840 for_each_migratetype_order(order, t) {
3841 INIT_LIST_HEAD(&zone->free_area[order].free_list[t]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 zone->free_area[order].nr_free = 0;
3843 }
3844}
3845
3846#ifndef __HAVE_ARCH_MEMMAP_INIT
3847#define memmap_init(size, nid, zone, start_pfn) \
Dave Hansena2f3aa022007-01-10 23:15:30 -08003848 memmap_init_zone((size), (nid), (zone), (start_pfn), MEMMAP_EARLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849#endif
3850
Jiang Liu4ed7e022012-07-31 16:43:35 -07003851static int __meminit zone_batchsize(struct zone *zone)
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07003852{
David Howells3a6be872009-05-06 16:03:03 -07003853#ifdef CONFIG_MMU
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07003854 int batch;
3855
3856 /*
3857 * The per-cpu-pages pools are set to around 1000th of the
Seth, Rohitba56e912005-10-29 18:15:47 -07003858 * size of the zone. But no more than 1/2 of a meg.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07003859 *
3860 * OK, so we don't know how big the cache is. So guess.
3861 */
3862 batch = zone->present_pages / 1024;
Seth, Rohitba56e912005-10-29 18:15:47 -07003863 if (batch * PAGE_SIZE > 512 * 1024)
3864 batch = (512 * 1024) / PAGE_SIZE;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07003865 batch /= 4; /* We effectively *= 4 below */
3866 if (batch < 1)
3867 batch = 1;
3868
3869 /*
Nick Piggin0ceaacc2005-12-04 13:55:25 +11003870 * Clamp the batch to a 2^n - 1 value. Having a power
3871 * of 2 value was found to be more likely to have
3872 * suboptimal cache aliasing properties in some cases.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07003873 *
Nick Piggin0ceaacc2005-12-04 13:55:25 +11003874 * For example if 2 tasks are alternately allocating
3875 * batches of pages, one task can end up with a lot
3876 * of pages of one half of the possible page colors
3877 * and the other with pages of the other colors.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07003878 */
David Howells91552032009-05-06 16:03:02 -07003879 batch = rounddown_pow_of_two(batch + batch/2) - 1;
Seth, Rohitba56e912005-10-29 18:15:47 -07003880
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07003881 return batch;
David Howells3a6be872009-05-06 16:03:03 -07003882
3883#else
3884 /* The deferral and batching of frees should be suppressed under NOMMU
3885 * conditions.
3886 *
3887 * The problem is that NOMMU needs to be able to allocate large chunks
3888 * of contiguous memory as there's no hardware page translation to
3889 * assemble apparent contiguous memory from discontiguous pages.
3890 *
3891 * Queueing large contiguous runs of pages for batching, however,
3892 * causes the pages to actually be freed in smaller chunks. As there
3893 * can be a significant delay between the individual batches being
3894 * recycled, this leads to the once large chunks of space being
3895 * fragmented and becoming unavailable for high-order allocations.
3896 */
3897 return 0;
3898#endif
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07003899}
3900
Adrian Bunkb69a7282008-07-23 21:28:12 -07003901static void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
Christoph Lameter2caaad42005-06-21 17:15:00 -07003902{
3903 struct per_cpu_pages *pcp;
Mel Gorman5f8dcc22009-09-21 17:03:19 -07003904 int migratetype;
Christoph Lameter2caaad42005-06-21 17:15:00 -07003905
Magnus Damm1c6fe942005-10-26 01:58:59 -07003906 memset(p, 0, sizeof(*p));
3907
Christoph Lameter3dfa5722008-02-04 22:29:19 -08003908 pcp = &p->pcp;
Christoph Lameter2caaad42005-06-21 17:15:00 -07003909 pcp->count = 0;
Christoph Lameter2caaad42005-06-21 17:15:00 -07003910 pcp->high = 6 * batch;
3911 pcp->batch = max(1UL, 1 * batch);
Mel Gorman5f8dcc22009-09-21 17:03:19 -07003912 for (migratetype = 0; migratetype < MIGRATE_PCPTYPES; migratetype++)
3913 INIT_LIST_HEAD(&pcp->lists[migratetype]);
Christoph Lameter2caaad42005-06-21 17:15:00 -07003914}
3915
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08003916/*
3917 * setup_pagelist_highmark() sets the high water mark for hot per_cpu_pagelist
3918 * to the value high for the pageset p.
3919 */
3920
3921static void setup_pagelist_highmark(struct per_cpu_pageset *p,
3922 unsigned long high)
3923{
3924 struct per_cpu_pages *pcp;
3925
Christoph Lameter3dfa5722008-02-04 22:29:19 -08003926 pcp = &p->pcp;
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08003927 pcp->high = high;
3928 pcp->batch = max(1UL, high/4);
3929 if ((high/4) > (PAGE_SHIFT * 8))
3930 pcp->batch = PAGE_SHIFT * 8;
3931}
3932
Jiang Liu4ed7e022012-07-31 16:43:35 -07003933static void __meminit setup_zone_pageset(struct zone *zone)
Wu Fengguang319774e2010-05-24 14:32:49 -07003934{
3935 int cpu;
3936
3937 zone->pageset = alloc_percpu(struct per_cpu_pageset);
3938
3939 for_each_possible_cpu(cpu) {
3940 struct per_cpu_pageset *pcp = per_cpu_ptr(zone->pageset, cpu);
3941
3942 setup_pageset(pcp, zone_batchsize(zone));
3943
3944 if (percpu_pagelist_fraction)
3945 setup_pagelist_highmark(pcp,
3946 (zone->present_pages /
3947 percpu_pagelist_fraction));
3948 }
3949}
3950
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07003951/*
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09003952 * Allocate per cpu pagesets and initialize them.
3953 * Before this call only boot pagesets were available.
Christoph Lameter2caaad42005-06-21 17:15:00 -07003954 */
Al Viro78d99552005-12-15 09:18:25 +00003955void __init setup_per_cpu_pageset(void)
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07003956{
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09003957 struct zone *zone;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07003958
Wu Fengguang319774e2010-05-24 14:32:49 -07003959 for_each_populated_zone(zone)
3960 setup_zone_pageset(zone);
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07003961}
3962
Sam Ravnborg577a32f2007-05-17 23:29:25 +02003963static noinline __init_refok
Yasunori Gotocca448f2006-06-23 02:03:10 -07003964int zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages)
Dave Hansened8ece22005-10-29 18:16:50 -07003965{
3966 int i;
3967 struct pglist_data *pgdat = zone->zone_pgdat;
Yasunori Gotocca448f2006-06-23 02:03:10 -07003968 size_t alloc_size;
Dave Hansened8ece22005-10-29 18:16:50 -07003969
3970 /*
3971 * The per-page waitqueue mechanism uses hashed waitqueues
3972 * per zone.
3973 */
Yasunori Goto02b694d2006-06-23 02:03:08 -07003974 zone->wait_table_hash_nr_entries =
3975 wait_table_hash_nr_entries(zone_size_pages);
3976 zone->wait_table_bits =
3977 wait_table_bits(zone->wait_table_hash_nr_entries);
Yasunori Gotocca448f2006-06-23 02:03:10 -07003978 alloc_size = zone->wait_table_hash_nr_entries
3979 * sizeof(wait_queue_head_t);
3980
Heiko Carstenscd94b9d2008-05-23 13:04:52 -07003981 if (!slab_is_available()) {
Yasunori Gotocca448f2006-06-23 02:03:10 -07003982 zone->wait_table = (wait_queue_head_t *)
Yinghai Lu8f389a992011-05-11 15:13:32 -07003983 alloc_bootmem_node_nopanic(pgdat, alloc_size);
Yasunori Gotocca448f2006-06-23 02:03:10 -07003984 } else {
3985 /*
3986 * This case means that a zone whose size was 0 gets new memory
3987 * via memory hot-add.
3988 * But it may be the case that a new node was hot-added. In
3989 * this case vmalloc() will not be able to use this new node's
3990 * memory - this wait_table must be initialized to use this new
3991 * node itself as well.
3992 * To use this new node's memory, further consideration will be
3993 * necessary.
3994 */
Jesper Juhl8691f3a2007-10-16 01:24:49 -07003995 zone->wait_table = vmalloc(alloc_size);
Yasunori Gotocca448f2006-06-23 02:03:10 -07003996 }
3997 if (!zone->wait_table)
3998 return -ENOMEM;
Dave Hansened8ece22005-10-29 18:16:50 -07003999
Yasunori Goto02b694d2006-06-23 02:03:08 -07004000 for(i = 0; i < zone->wait_table_hash_nr_entries; ++i)
Dave Hansened8ece22005-10-29 18:16:50 -07004001 init_waitqueue_head(zone->wait_table + i);
Yasunori Gotocca448f2006-06-23 02:03:10 -07004002
4003 return 0;
Dave Hansened8ece22005-10-29 18:16:50 -07004004}
4005
Matt Tolentinoc09b4242006-01-17 07:03:44 +01004006static __meminit void zone_pcp_init(struct zone *zone)
Dave Hansened8ece22005-10-29 18:16:50 -07004007{
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09004008 /*
4009 * per cpu subsystem is not up at this point. The following code
4010 * relies on the ability of the linker to provide the
4011 * offset of a (static) per cpu variable into the per cpu area.
4012 */
4013 zone->pageset = &boot_pageset;
Dave Hansened8ece22005-10-29 18:16:50 -07004014
Anton Blanchardf5335c02006-03-25 03:06:49 -08004015 if (zone->present_pages)
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09004016 printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%u\n",
4017 zone->name, zone->present_pages,
4018 zone_batchsize(zone));
Dave Hansened8ece22005-10-29 18:16:50 -07004019}
4020
Jiang Liu4ed7e022012-07-31 16:43:35 -07004021int __meminit init_currently_empty_zone(struct zone *zone,
Yasunori Goto718127c2006-06-23 02:03:10 -07004022 unsigned long zone_start_pfn,
Dave Hansena2f3aa022007-01-10 23:15:30 -08004023 unsigned long size,
4024 enum memmap_context context)
Dave Hansened8ece22005-10-29 18:16:50 -07004025{
4026 struct pglist_data *pgdat = zone->zone_pgdat;
Yasunori Gotocca448f2006-06-23 02:03:10 -07004027 int ret;
4028 ret = zone_wait_table_init(zone, size);
4029 if (ret)
4030 return ret;
Dave Hansened8ece22005-10-29 18:16:50 -07004031 pgdat->nr_zones = zone_idx(zone) + 1;
4032
Dave Hansened8ece22005-10-29 18:16:50 -07004033 zone->zone_start_pfn = zone_start_pfn;
4034
Mel Gorman708614e2008-07-23 21:26:51 -07004035 mminit_dprintk(MMINIT_TRACE, "memmap_init",
4036 "Initialising map node %d zone %lu pfns %lu -> %lu\n",
4037 pgdat->node_id,
4038 (unsigned long)zone_idx(zone),
4039 zone_start_pfn, (zone_start_pfn + size));
4040
Andi Kleen1e548de2008-02-04 22:29:26 -08004041 zone_init_free_lists(zone);
Yasunori Goto718127c2006-06-23 02:03:10 -07004042
4043 return 0;
Dave Hansened8ece22005-10-29 18:16:50 -07004044}
4045
Tejun Heo0ee332c2011-12-08 10:22:09 -08004046#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Mel Gormanc7132162006-09-27 01:49:43 -07004047#ifndef CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID
4048/*
4049 * Required by SPARSEMEM. Given a PFN, return what node the PFN is on.
4050 * Architectures may implement their own version but if add_active_range()
4051 * was used and there are no special requirements, this is a convenient
4052 * alternative
4053 */
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -08004054int __meminit __early_pfn_to_nid(unsigned long pfn)
Mel Gormanc7132162006-09-27 01:49:43 -07004055{
Tejun Heoc13291a2011-07-12 10:46:30 +02004056 unsigned long start_pfn, end_pfn;
4057 int i, nid;
Mel Gormanc7132162006-09-27 01:49:43 -07004058
Tejun Heoc13291a2011-07-12 10:46:30 +02004059 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid)
Mel Gormanc7132162006-09-27 01:49:43 -07004060 if (start_pfn <= pfn && pfn < end_pfn)
Tejun Heoc13291a2011-07-12 10:46:30 +02004061 return nid;
KAMEZAWA Hiroyukicc2559b2009-02-18 14:48:33 -08004062 /* This is a memory hole */
4063 return -1;
Mel Gormanc7132162006-09-27 01:49:43 -07004064}
4065#endif /* CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID */
4066
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -08004067int __meminit early_pfn_to_nid(unsigned long pfn)
4068{
KAMEZAWA Hiroyukicc2559b2009-02-18 14:48:33 -08004069 int nid;
4070
4071 nid = __early_pfn_to_nid(pfn);
4072 if (nid >= 0)
4073 return nid;
4074 /* just returns 0 */
4075 return 0;
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -08004076}
4077
KAMEZAWA Hiroyukicc2559b2009-02-18 14:48:33 -08004078#ifdef CONFIG_NODES_SPAN_OTHER_NODES
4079bool __meminit early_pfn_in_nid(unsigned long pfn, int node)
4080{
4081 int nid;
4082
4083 nid = __early_pfn_to_nid(pfn);
4084 if (nid >= 0 && nid != node)
4085 return false;
4086 return true;
4087}
4088#endif
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -08004089
Mel Gormanc7132162006-09-27 01:49:43 -07004090/**
4091 * free_bootmem_with_active_regions - Call free_bootmem_node for each active range
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004092 * @nid: The node to free memory on. If MAX_NUMNODES, all nodes are freed.
4093 * @max_low_pfn: The highest PFN that will be passed to free_bootmem_node
Mel Gormanc7132162006-09-27 01:49:43 -07004094 *
4095 * If an architecture guarantees that all ranges registered with
4096 * add_active_ranges() contain no holes and may be freed, this
4097 * this function may be used instead of calling free_bootmem() manually.
4098 */
Tejun Heoc13291a2011-07-12 10:46:30 +02004099void __init free_bootmem_with_active_regions(int nid, unsigned long max_low_pfn)
Mel Gormanc7132162006-09-27 01:49:43 -07004100{
Tejun Heoc13291a2011-07-12 10:46:30 +02004101 unsigned long start_pfn, end_pfn;
4102 int i, this_nid;
Mel Gormanc7132162006-09-27 01:49:43 -07004103
Tejun Heoc13291a2011-07-12 10:46:30 +02004104 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, &this_nid) {
4105 start_pfn = min(start_pfn, max_low_pfn);
4106 end_pfn = min(end_pfn, max_low_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07004107
Tejun Heoc13291a2011-07-12 10:46:30 +02004108 if (start_pfn < end_pfn)
4109 free_bootmem_node(NODE_DATA(this_nid),
4110 PFN_PHYS(start_pfn),
4111 (end_pfn - start_pfn) << PAGE_SHIFT);
Mel Gormanc7132162006-09-27 01:49:43 -07004112 }
4113}
4114
4115/**
4116 * sparse_memory_present_with_active_regions - Call memory_present for each active range
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004117 * @nid: The node to call memory_present for. If MAX_NUMNODES, all nodes will be used.
Mel Gormanc7132162006-09-27 01:49:43 -07004118 *
4119 * If an architecture guarantees that all ranges registered with
4120 * add_active_ranges() contain no holes and may be freed, this
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004121 * function may be used instead of calling memory_present() manually.
Mel Gormanc7132162006-09-27 01:49:43 -07004122 */
4123void __init sparse_memory_present_with_active_regions(int nid)
4124{
Tejun Heoc13291a2011-07-12 10:46:30 +02004125 unsigned long start_pfn, end_pfn;
4126 int i, this_nid;
Mel Gormanc7132162006-09-27 01:49:43 -07004127
Tejun Heoc13291a2011-07-12 10:46:30 +02004128 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, &this_nid)
4129 memory_present(this_nid, start_pfn, end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07004130}
4131
4132/**
4133 * get_pfn_range_for_nid - Return the start and end page frames for a node
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004134 * @nid: The nid to return the range for. If MAX_NUMNODES, the min and max PFN are returned.
4135 * @start_pfn: Passed by reference. On return, it will have the node start_pfn.
4136 * @end_pfn: Passed by reference. On return, it will have the node end_pfn.
Mel Gormanc7132162006-09-27 01:49:43 -07004137 *
4138 * It returns the start and end page frame of a node based on information
4139 * provided by an arch calling add_active_range(). If called for a node
4140 * with no available memory, a warning is printed and the start and end
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004141 * PFNs will be 0.
Mel Gormanc7132162006-09-27 01:49:43 -07004142 */
Yasunori Gotoa3142c82007-05-08 00:23:07 -07004143void __meminit get_pfn_range_for_nid(unsigned int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07004144 unsigned long *start_pfn, unsigned long *end_pfn)
4145{
Tejun Heoc13291a2011-07-12 10:46:30 +02004146 unsigned long this_start_pfn, this_end_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07004147 int i;
Tejun Heoc13291a2011-07-12 10:46:30 +02004148
Mel Gormanc7132162006-09-27 01:49:43 -07004149 *start_pfn = -1UL;
4150 *end_pfn = 0;
4151
Tejun Heoc13291a2011-07-12 10:46:30 +02004152 for_each_mem_pfn_range(i, nid, &this_start_pfn, &this_end_pfn, NULL) {
4153 *start_pfn = min(*start_pfn, this_start_pfn);
4154 *end_pfn = max(*end_pfn, this_end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07004155 }
4156
Christoph Lameter633c0662007-10-16 01:25:37 -07004157 if (*start_pfn == -1UL)
Mel Gormanc7132162006-09-27 01:49:43 -07004158 *start_pfn = 0;
Mel Gormanc7132162006-09-27 01:49:43 -07004159}
4160
4161/*
Mel Gorman2a1e2742007-07-17 04:03:12 -07004162 * This finds a zone that can be used for ZONE_MOVABLE pages. The
4163 * assumption is made that zones within a node are ordered in monotonic
4164 * increasing memory addresses so that the "highest" populated zone is used
4165 */
Adrian Bunkb69a7282008-07-23 21:28:12 -07004166static void __init find_usable_zone_for_movable(void)
Mel Gorman2a1e2742007-07-17 04:03:12 -07004167{
4168 int zone_index;
4169 for (zone_index = MAX_NR_ZONES - 1; zone_index >= 0; zone_index--) {
4170 if (zone_index == ZONE_MOVABLE)
4171 continue;
4172
4173 if (arch_zone_highest_possible_pfn[zone_index] >
4174 arch_zone_lowest_possible_pfn[zone_index])
4175 break;
4176 }
4177
4178 VM_BUG_ON(zone_index == -1);
4179 movable_zone = zone_index;
4180}
4181
4182/*
4183 * The zone ranges provided by the architecture do not include ZONE_MOVABLE
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004184 * because it is sized independent of architecture. Unlike the other zones,
Mel Gorman2a1e2742007-07-17 04:03:12 -07004185 * the starting point for ZONE_MOVABLE is not fixed. It may be different
4186 * in each node depending on the size of each node and how evenly kernelcore
4187 * is distributed. This helper function adjusts the zone ranges
4188 * provided by the architecture for a given node by using the end of the
4189 * highest usable zone for ZONE_MOVABLE. This preserves the assumption that
4190 * zones within a node are in order of monotonic increases memory addresses
4191 */
Adrian Bunkb69a7282008-07-23 21:28:12 -07004192static void __meminit adjust_zone_range_for_zone_movable(int nid,
Mel Gorman2a1e2742007-07-17 04:03:12 -07004193 unsigned long zone_type,
4194 unsigned long node_start_pfn,
4195 unsigned long node_end_pfn,
4196 unsigned long *zone_start_pfn,
4197 unsigned long *zone_end_pfn)
4198{
4199 /* Only adjust if ZONE_MOVABLE is on this node */
4200 if (zone_movable_pfn[nid]) {
4201 /* Size ZONE_MOVABLE */
4202 if (zone_type == ZONE_MOVABLE) {
4203 *zone_start_pfn = zone_movable_pfn[nid];
4204 *zone_end_pfn = min(node_end_pfn,
4205 arch_zone_highest_possible_pfn[movable_zone]);
4206
4207 /* Adjust for ZONE_MOVABLE starting within this range */
4208 } else if (*zone_start_pfn < zone_movable_pfn[nid] &&
4209 *zone_end_pfn > zone_movable_pfn[nid]) {
4210 *zone_end_pfn = zone_movable_pfn[nid];
4211
4212 /* Check if this whole range is within ZONE_MOVABLE */
4213 } else if (*zone_start_pfn >= zone_movable_pfn[nid])
4214 *zone_start_pfn = *zone_end_pfn;
4215 }
4216}
4217
4218/*
Mel Gormanc7132162006-09-27 01:49:43 -07004219 * Return the number of pages a zone spans in a node, including holes
4220 * present_pages = zone_spanned_pages_in_node() - zone_absent_pages_in_node()
4221 */
Paul Mundt6ea6e682007-07-15 23:38:20 -07004222static unsigned long __meminit zone_spanned_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07004223 unsigned long zone_type,
4224 unsigned long *ignored)
4225{
4226 unsigned long node_start_pfn, node_end_pfn;
4227 unsigned long zone_start_pfn, zone_end_pfn;
4228
4229 /* Get the start and end of the node and zone */
4230 get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
4231 zone_start_pfn = arch_zone_lowest_possible_pfn[zone_type];
4232 zone_end_pfn = arch_zone_highest_possible_pfn[zone_type];
Mel Gorman2a1e2742007-07-17 04:03:12 -07004233 adjust_zone_range_for_zone_movable(nid, zone_type,
4234 node_start_pfn, node_end_pfn,
4235 &zone_start_pfn, &zone_end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07004236
4237 /* Check that this node has pages within the zone's required range */
4238 if (zone_end_pfn < node_start_pfn || zone_start_pfn > node_end_pfn)
4239 return 0;
4240
4241 /* Move the zone boundaries inside the node if necessary */
4242 zone_end_pfn = min(zone_end_pfn, node_end_pfn);
4243 zone_start_pfn = max(zone_start_pfn, node_start_pfn);
4244
4245 /* Return the spanned pages */
4246 return zone_end_pfn - zone_start_pfn;
4247}
4248
4249/*
4250 * Return the number of holes in a range on a node. If nid is MAX_NUMNODES,
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004251 * then all holes in the requested range will be accounted for.
Mel Gormanc7132162006-09-27 01:49:43 -07004252 */
Yinghai Lu32996252009-12-15 17:59:02 -08004253unsigned long __meminit __absent_pages_in_range(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07004254 unsigned long range_start_pfn,
4255 unsigned long range_end_pfn)
4256{
Tejun Heo96e907d2011-07-12 10:46:29 +02004257 unsigned long nr_absent = range_end_pfn - range_start_pfn;
4258 unsigned long start_pfn, end_pfn;
4259 int i;
Mel Gormanc7132162006-09-27 01:49:43 -07004260
Tejun Heo96e907d2011-07-12 10:46:29 +02004261 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
4262 start_pfn = clamp(start_pfn, range_start_pfn, range_end_pfn);
4263 end_pfn = clamp(end_pfn, range_start_pfn, range_end_pfn);
4264 nr_absent -= end_pfn - start_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07004265 }
Tejun Heo96e907d2011-07-12 10:46:29 +02004266 return nr_absent;
Mel Gormanc7132162006-09-27 01:49:43 -07004267}
4268
4269/**
4270 * absent_pages_in_range - Return number of page frames in holes within a range
4271 * @start_pfn: The start PFN to start searching for holes
4272 * @end_pfn: The end PFN to stop searching for holes
4273 *
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004274 * It returns the number of pages frames in memory holes within a range.
Mel Gormanc7132162006-09-27 01:49:43 -07004275 */
4276unsigned long __init absent_pages_in_range(unsigned long start_pfn,
4277 unsigned long end_pfn)
4278{
4279 return __absent_pages_in_range(MAX_NUMNODES, start_pfn, end_pfn);
4280}
4281
4282/* Return the number of page frames in holes in a zone on a node */
Paul Mundt6ea6e682007-07-15 23:38:20 -07004283static unsigned long __meminit zone_absent_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07004284 unsigned long zone_type,
4285 unsigned long *ignored)
4286{
Tejun Heo96e907d2011-07-12 10:46:29 +02004287 unsigned long zone_low = arch_zone_lowest_possible_pfn[zone_type];
4288 unsigned long zone_high = arch_zone_highest_possible_pfn[zone_type];
Mel Gorman9c7cd682006-09-27 01:49:58 -07004289 unsigned long node_start_pfn, node_end_pfn;
4290 unsigned long zone_start_pfn, zone_end_pfn;
4291
4292 get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
Tejun Heo96e907d2011-07-12 10:46:29 +02004293 zone_start_pfn = clamp(node_start_pfn, zone_low, zone_high);
4294 zone_end_pfn = clamp(node_end_pfn, zone_low, zone_high);
Mel Gorman9c7cd682006-09-27 01:49:58 -07004295
Mel Gorman2a1e2742007-07-17 04:03:12 -07004296 adjust_zone_range_for_zone_movable(nid, zone_type,
4297 node_start_pfn, node_end_pfn,
4298 &zone_start_pfn, &zone_end_pfn);
Mel Gorman9c7cd682006-09-27 01:49:58 -07004299 return __absent_pages_in_range(nid, zone_start_pfn, zone_end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07004300}
Mel Gorman0e0b8642006-09-27 01:49:56 -07004301
Tejun Heo0ee332c2011-12-08 10:22:09 -08004302#else /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
Paul Mundt6ea6e682007-07-15 23:38:20 -07004303static inline unsigned long __meminit zone_spanned_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07004304 unsigned long zone_type,
4305 unsigned long *zones_size)
4306{
4307 return zones_size[zone_type];
4308}
4309
Paul Mundt6ea6e682007-07-15 23:38:20 -07004310static inline unsigned long __meminit zone_absent_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07004311 unsigned long zone_type,
4312 unsigned long *zholes_size)
4313{
4314 if (!zholes_size)
4315 return 0;
4316
4317 return zholes_size[zone_type];
4318}
Mel Gorman0e0b8642006-09-27 01:49:56 -07004319
Tejun Heo0ee332c2011-12-08 10:22:09 -08004320#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
Mel Gormanc7132162006-09-27 01:49:43 -07004321
Yasunori Gotoa3142c82007-05-08 00:23:07 -07004322static void __meminit calculate_node_totalpages(struct pglist_data *pgdat,
Mel Gormanc7132162006-09-27 01:49:43 -07004323 unsigned long *zones_size, unsigned long *zholes_size)
4324{
4325 unsigned long realtotalpages, totalpages = 0;
4326 enum zone_type i;
4327
4328 for (i = 0; i < MAX_NR_ZONES; i++)
4329 totalpages += zone_spanned_pages_in_node(pgdat->node_id, i,
4330 zones_size);
4331 pgdat->node_spanned_pages = totalpages;
4332
4333 realtotalpages = totalpages;
4334 for (i = 0; i < MAX_NR_ZONES; i++)
4335 realtotalpages -=
4336 zone_absent_pages_in_node(pgdat->node_id, i,
4337 zholes_size);
4338 pgdat->node_present_pages = realtotalpages;
4339 printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id,
4340 realtotalpages);
4341}
4342
Mel Gorman835c1342007-10-16 01:25:47 -07004343#ifndef CONFIG_SPARSEMEM
4344/*
4345 * Calculate the size of the zone->blockflags rounded to an unsigned long
Mel Gormand9c23402007-10-16 01:26:01 -07004346 * Start by making sure zonesize is a multiple of pageblock_order by rounding
4347 * up. Then use 1 NR_PAGEBLOCK_BITS worth of bits per pageblock, finally
Mel Gorman835c1342007-10-16 01:25:47 -07004348 * round what is now in bits to nearest long in bits, then return it in
4349 * bytes.
4350 */
4351static unsigned long __init usemap_size(unsigned long zonesize)
4352{
4353 unsigned long usemapsize;
4354
Mel Gormand9c23402007-10-16 01:26:01 -07004355 usemapsize = roundup(zonesize, pageblock_nr_pages);
4356 usemapsize = usemapsize >> pageblock_order;
Mel Gorman835c1342007-10-16 01:25:47 -07004357 usemapsize *= NR_PAGEBLOCK_BITS;
4358 usemapsize = roundup(usemapsize, 8 * sizeof(unsigned long));
4359
4360 return usemapsize / 8;
4361}
4362
4363static void __init setup_usemap(struct pglist_data *pgdat,
4364 struct zone *zone, unsigned long zonesize)
4365{
4366 unsigned long usemapsize = usemap_size(zonesize);
4367 zone->pageblock_flags = NULL;
Julia Lawall58a01a42009-01-06 14:39:28 -08004368 if (usemapsize)
Yinghai Lu8f389a992011-05-11 15:13:32 -07004369 zone->pageblock_flags = alloc_bootmem_node_nopanic(pgdat,
4370 usemapsize);
Mel Gorman835c1342007-10-16 01:25:47 -07004371}
4372#else
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01004373static inline void setup_usemap(struct pglist_data *pgdat,
Mel Gorman835c1342007-10-16 01:25:47 -07004374 struct zone *zone, unsigned long zonesize) {}
4375#endif /* CONFIG_SPARSEMEM */
4376
Mel Gormand9c23402007-10-16 01:26:01 -07004377#ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
Mel Gormanba72cb82007-11-28 16:21:13 -08004378
Mel Gormand9c23402007-10-16 01:26:01 -07004379/* Initialise the number of pages represented by NR_PAGEBLOCK_BITS */
Xishi Qiuca57df72012-07-31 16:43:19 -07004380void __init set_pageblock_order(void)
Mel Gormand9c23402007-10-16 01:26:01 -07004381{
Andrew Morton955c1cd2012-05-29 15:06:31 -07004382 unsigned int order;
4383
Mel Gormand9c23402007-10-16 01:26:01 -07004384 /* Check that pageblock_nr_pages has not already been setup */
4385 if (pageblock_order)
4386 return;
4387
Andrew Morton955c1cd2012-05-29 15:06:31 -07004388 if (HPAGE_SHIFT > PAGE_SHIFT)
4389 order = HUGETLB_PAGE_ORDER;
4390 else
4391 order = MAX_ORDER - 1;
4392
Mel Gormand9c23402007-10-16 01:26:01 -07004393 /*
4394 * Assume the largest contiguous order of interest is a huge page.
Andrew Morton955c1cd2012-05-29 15:06:31 -07004395 * This value may be variable depending on boot parameters on IA64 and
4396 * powerpc.
Mel Gormand9c23402007-10-16 01:26:01 -07004397 */
4398 pageblock_order = order;
4399}
4400#else /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
4401
Mel Gormanba72cb82007-11-28 16:21:13 -08004402/*
4403 * When CONFIG_HUGETLB_PAGE_SIZE_VARIABLE is not set, set_pageblock_order()
Andrew Morton955c1cd2012-05-29 15:06:31 -07004404 * is unused as pageblock_order is set at compile-time. See
4405 * include/linux/pageblock-flags.h for the values of pageblock_order based on
4406 * the kernel config
Mel Gormanba72cb82007-11-28 16:21:13 -08004407 */
Xishi Qiuca57df72012-07-31 16:43:19 -07004408void __init set_pageblock_order(void)
Mel Gormanba72cb82007-11-28 16:21:13 -08004409{
Mel Gormanba72cb82007-11-28 16:21:13 -08004410}
Mel Gormand9c23402007-10-16 01:26:01 -07004411
4412#endif /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
4413
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414/*
4415 * Set up the zone data structures:
4416 * - mark all pages reserved
4417 * - mark all memory queues empty
4418 * - clear the memory bitmaps
Minchan Kim6527af52012-07-31 16:46:16 -07004419 *
4420 * NOTE: pgdat should get zeroed by caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 */
Alexander van Heukelumb5a0e012008-02-23 15:24:06 -08004422static void __paginginit free_area_init_core(struct pglist_data *pgdat,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 unsigned long *zones_size, unsigned long *zholes_size)
4424{
Christoph Lameter2f1b6242006-09-25 23:31:13 -07004425 enum zone_type j;
Dave Hansened8ece22005-10-29 18:16:50 -07004426 int nid = pgdat->node_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 unsigned long zone_start_pfn = pgdat->node_start_pfn;
Yasunori Goto718127c2006-06-23 02:03:10 -07004428 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429
Dave Hansen208d54e2005-10-29 18:16:52 -07004430 pgdat_resize_init(pgdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 init_waitqueue_head(&pgdat->kswapd_wait);
Mel Gorman55150612012-07-31 16:44:35 -07004432 init_waitqueue_head(&pgdat->pfmemalloc_wait);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004433 pgdat_page_cgroup_init(pgdat);
Michal Nazarewicz5f63b722012-01-11 15:16:11 +01004434
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 for (j = 0; j < MAX_NR_ZONES; j++) {
4436 struct zone *zone = pgdat->node_zones + j;
Mel Gorman0e0b8642006-09-27 01:49:56 -07004437 unsigned long size, realsize, memmap_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438
Mel Gormanc7132162006-09-27 01:49:43 -07004439 size = zone_spanned_pages_in_node(nid, j, zones_size);
4440 realsize = size - zone_absent_pages_in_node(nid, j,
4441 zholes_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442
Mel Gorman0e0b8642006-09-27 01:49:56 -07004443 /*
4444 * Adjust realsize so that it accounts for how much memory
4445 * is used by this zone for memmap. This affects the watermark
4446 * and per-cpu initialisations
4447 */
Johannes Weinerf7232152008-05-23 13:04:21 -07004448 memmap_pages =
4449 PAGE_ALIGN(size * sizeof(struct page)) >> PAGE_SHIFT;
Mel Gorman0e0b8642006-09-27 01:49:56 -07004450 if (realsize >= memmap_pages) {
4451 realsize -= memmap_pages;
Yinghai Lu5594c8c2009-01-06 14:39:14 -08004452 if (memmap_pages)
4453 printk(KERN_DEBUG
4454 " %s zone: %lu pages used for memmap\n",
4455 zone_names[j], memmap_pages);
Mel Gorman0e0b8642006-09-27 01:49:56 -07004456 } else
4457 printk(KERN_WARNING
4458 " %s zone: %lu pages exceeds realsize %lu\n",
4459 zone_names[j], memmap_pages, realsize);
4460
Christoph Lameter62672762007-02-10 01:43:07 -08004461 /* Account for reserved pages */
4462 if (j == 0 && realsize > dma_reserve) {
Mel Gorman0e0b8642006-09-27 01:49:56 -07004463 realsize -= dma_reserve;
Yinghai Lud903ef92008-10-18 20:27:06 -07004464 printk(KERN_DEBUG " %s zone: %lu pages reserved\n",
Christoph Lameter62672762007-02-10 01:43:07 -08004465 zone_names[0], dma_reserve);
Mel Gorman0e0b8642006-09-27 01:49:56 -07004466 }
4467
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07004468 if (!is_highmem_idx(j))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 nr_kernel_pages += realsize;
4470 nr_all_pages += realsize;
4471
4472 zone->spanned_pages = size;
4473 zone->present_pages = realsize;
Rik van Riel7db88892012-07-31 16:43:12 -07004474#if defined CONFIG_COMPACTION || defined CONFIG_CMA
4475 zone->compact_cached_free_pfn = zone->zone_start_pfn +
4476 zone->spanned_pages;
4477 zone->compact_cached_free_pfn &= ~(pageblock_nr_pages-1);
4478#endif
Christoph Lameter96146342006-07-03 00:24:13 -07004479#ifdef CONFIG_NUMA
Christoph Lameterd5f541e2006-09-27 01:50:08 -07004480 zone->node = nid;
Christoph Lameter8417bba2006-09-25 23:31:51 -07004481 zone->min_unmapped_pages = (realsize*sysctl_min_unmapped_ratio)
Christoph Lameter96146342006-07-03 00:24:13 -07004482 / 100;
Christoph Lameter0ff38492006-09-25 23:31:52 -07004483 zone->min_slab_pages = (realsize * sysctl_min_slab_ratio) / 100;
Christoph Lameter96146342006-07-03 00:24:13 -07004484#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 zone->name = zone_names[j];
4486 spin_lock_init(&zone->lock);
4487 spin_lock_init(&zone->lru_lock);
Dave Hansenbdc8cb92005-10-29 18:16:53 -07004488 zone_seqlock_init(zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 zone->zone_pgdat = pgdat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490
Dave Hansened8ece22005-10-29 18:16:50 -07004491 zone_pcp_init(zone);
Konstantin Khlebnikov7f5e86c2012-05-29 15:06:58 -07004492 lruvec_init(&zone->lruvec, zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 if (!size)
4494 continue;
4495
Andrew Morton955c1cd2012-05-29 15:06:31 -07004496 set_pageblock_order();
Mel Gorman835c1342007-10-16 01:25:47 -07004497 setup_usemap(pgdat, zone, size);
Dave Hansena2f3aa022007-01-10 23:15:30 -08004498 ret = init_currently_empty_zone(zone, zone_start_pfn,
4499 size, MEMMAP_EARLY);
Yasunori Goto718127c2006-06-23 02:03:10 -07004500 BUG_ON(ret);
Heiko Carstens76cdd582008-05-14 16:05:52 -07004501 memmap_init(size, nid, j, zone_start_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 zone_start_pfn += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 }
4504}
4505
Sam Ravnborg577a32f2007-05-17 23:29:25 +02004506static void __init_refok alloc_node_mem_map(struct pglist_data *pgdat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 /* Skip empty nodes */
4509 if (!pgdat->node_spanned_pages)
4510 return;
4511
Andy Whitcroftd41dee32005-06-23 00:07:54 -07004512#ifdef CONFIG_FLAT_NODE_MEM_MAP
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 /* ia64 gets its own node_mem_map, before this, without bootmem */
4514 if (!pgdat->node_mem_map) {
Bob Piccoe984bb42006-05-20 15:00:31 -07004515 unsigned long size, start, end;
Andy Whitcroftd41dee32005-06-23 00:07:54 -07004516 struct page *map;
4517
Bob Piccoe984bb42006-05-20 15:00:31 -07004518 /*
4519 * The zone's endpoints aren't required to be MAX_ORDER
4520 * aligned but the node_mem_map endpoints must be in order
4521 * for the buddy allocator to function correctly.
4522 */
4523 start = pgdat->node_start_pfn & ~(MAX_ORDER_NR_PAGES - 1);
4524 end = pgdat->node_start_pfn + pgdat->node_spanned_pages;
4525 end = ALIGN(end, MAX_ORDER_NR_PAGES);
4526 size = (end - start) * sizeof(struct page);
Dave Hansen6f167ec2005-06-23 00:07:39 -07004527 map = alloc_remap(pgdat->node_id, size);
4528 if (!map)
Yinghai Lu8f389a992011-05-11 15:13:32 -07004529 map = alloc_bootmem_node_nopanic(pgdat, size);
Bob Piccoe984bb42006-05-20 15:00:31 -07004530 pgdat->node_mem_map = map + (pgdat->node_start_pfn - start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 }
Roman Zippel12d810c2007-05-31 00:40:54 -07004532#ifndef CONFIG_NEED_MULTIPLE_NODES
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 /*
4534 * With no DISCONTIG, the global mem_map is just set as node 0's
4535 */
Mel Gormanc7132162006-09-27 01:49:43 -07004536 if (pgdat == NODE_DATA(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 mem_map = NODE_DATA(0)->node_mem_map;
Tejun Heo0ee332c2011-12-08 10:22:09 -08004538#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Mel Gormanc7132162006-09-27 01:49:43 -07004539 if (page_to_pfn(mem_map) != pgdat->node_start_pfn)
Thomas Bogendoerfer467bc462008-01-08 15:33:11 -08004540 mem_map -= (pgdat->node_start_pfn - ARCH_PFN_OFFSET);
Tejun Heo0ee332c2011-12-08 10:22:09 -08004541#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
Mel Gormanc7132162006-09-27 01:49:43 -07004542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543#endif
Andy Whitcroftd41dee32005-06-23 00:07:54 -07004544#endif /* CONFIG_FLAT_NODE_MEM_MAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545}
4546
Johannes Weiner9109fb72008-07-23 21:27:20 -07004547void __paginginit free_area_init_node(int nid, unsigned long *zones_size,
4548 unsigned long node_start_pfn, unsigned long *zholes_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549{
Johannes Weiner9109fb72008-07-23 21:27:20 -07004550 pg_data_t *pgdat = NODE_DATA(nid);
4551
Minchan Kim88fdf752012-07-31 16:46:14 -07004552 /* pg_data_t should be reset to zero when it's allocated */
Linus Torvalds8783b6e2012-08-02 10:37:03 -07004553 WARN_ON(pgdat->nr_zones || pgdat->classzone_idx);
Minchan Kim88fdf752012-07-31 16:46:14 -07004554
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 pgdat->node_id = nid;
4556 pgdat->node_start_pfn = node_start_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07004557 calculate_node_totalpages(pgdat, zones_size, zholes_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558
4559 alloc_node_mem_map(pgdat);
Yinghai Lue8c27ac2008-06-01 13:15:22 -07004560#ifdef CONFIG_FLAT_NODE_MEM_MAP
4561 printk(KERN_DEBUG "free_area_init_node: node %d, pgdat %08lx, node_mem_map %08lx\n",
4562 nid, (unsigned long)pgdat,
4563 (unsigned long)pgdat->node_mem_map);
4564#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565
4566 free_area_init_core(pgdat, zones_size, zholes_size);
4567}
4568
Tejun Heo0ee332c2011-12-08 10:22:09 -08004569#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Miklos Szeredi418508c2007-05-23 13:57:55 -07004570
4571#if MAX_NUMNODES > 1
4572/*
4573 * Figure out the number of possible node ids.
4574 */
4575static void __init setup_nr_node_ids(void)
4576{
4577 unsigned int node;
4578 unsigned int highest = 0;
4579
4580 for_each_node_mask(node, node_possible_map)
4581 highest = node;
4582 nr_node_ids = highest + 1;
4583}
4584#else
4585static inline void setup_nr_node_ids(void)
4586{
4587}
4588#endif
4589
Mel Gormanc7132162006-09-27 01:49:43 -07004590/**
Tejun Heo1e019792011-07-12 09:45:34 +02004591 * node_map_pfn_alignment - determine the maximum internode alignment
4592 *
4593 * This function should be called after node map is populated and sorted.
4594 * It calculates the maximum power of two alignment which can distinguish
4595 * all the nodes.
4596 *
4597 * For example, if all nodes are 1GiB and aligned to 1GiB, the return value
4598 * would indicate 1GiB alignment with (1 << (30 - PAGE_SHIFT)). If the
4599 * nodes are shifted by 256MiB, 256MiB. Note that if only the last node is
4600 * shifted, 1GiB is enough and this function will indicate so.
4601 *
4602 * This is used to test whether pfn -> nid mapping of the chosen memory
4603 * model has fine enough granularity to avoid incorrect mapping for the
4604 * populated node map.
4605 *
4606 * Returns the determined alignment in pfn's. 0 if there is no alignment
4607 * requirement (single node).
4608 */
4609unsigned long __init node_map_pfn_alignment(void)
4610{
4611 unsigned long accl_mask = 0, last_end = 0;
Tejun Heoc13291a2011-07-12 10:46:30 +02004612 unsigned long start, end, mask;
Tejun Heo1e019792011-07-12 09:45:34 +02004613 int last_nid = -1;
Tejun Heoc13291a2011-07-12 10:46:30 +02004614 int i, nid;
Tejun Heo1e019792011-07-12 09:45:34 +02004615
Tejun Heoc13291a2011-07-12 10:46:30 +02004616 for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, &nid) {
Tejun Heo1e019792011-07-12 09:45:34 +02004617 if (!start || last_nid < 0 || last_nid == nid) {
4618 last_nid = nid;
4619 last_end = end;
4620 continue;
4621 }
4622
4623 /*
4624 * Start with a mask granular enough to pin-point to the
4625 * start pfn and tick off bits one-by-one until it becomes
4626 * too coarse to separate the current node from the last.
4627 */
4628 mask = ~((1 << __ffs(start)) - 1);
4629 while (mask && last_end <= (start & (mask << 1)))
4630 mask <<= 1;
4631
4632 /* accumulate all internode masks */
4633 accl_mask |= mask;
4634 }
4635
4636 /* convert mask to number of pages */
4637 return ~accl_mask + 1;
4638}
4639
Mel Gormana6af2bc2007-02-10 01:42:57 -08004640/* Find the lowest pfn for a node */
Adrian Bunkb69a7282008-07-23 21:28:12 -07004641static unsigned long __init find_min_pfn_for_node(int nid)
Mel Gormanc7132162006-09-27 01:49:43 -07004642{
Mel Gormana6af2bc2007-02-10 01:42:57 -08004643 unsigned long min_pfn = ULONG_MAX;
Tejun Heoc13291a2011-07-12 10:46:30 +02004644 unsigned long start_pfn;
4645 int i;
Mel Gorman1abbfb42006-11-23 12:01:41 +00004646
Tejun Heoc13291a2011-07-12 10:46:30 +02004647 for_each_mem_pfn_range(i, nid, &start_pfn, NULL, NULL)
4648 min_pfn = min(min_pfn, start_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07004649
Mel Gormana6af2bc2007-02-10 01:42:57 -08004650 if (min_pfn == ULONG_MAX) {
4651 printk(KERN_WARNING
Paul Jackson2bc0d262008-06-22 07:22:17 -07004652 "Could not find start_pfn for node %d\n", nid);
Mel Gormana6af2bc2007-02-10 01:42:57 -08004653 return 0;
4654 }
4655
4656 return min_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07004657}
4658
4659/**
4660 * find_min_pfn_with_active_regions - Find the minimum PFN registered
4661 *
4662 * It returns the minimum PFN based on information provided via
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004663 * add_active_range().
Mel Gormanc7132162006-09-27 01:49:43 -07004664 */
4665unsigned long __init find_min_pfn_with_active_regions(void)
4666{
4667 return find_min_pfn_for_node(MAX_NUMNODES);
4668}
4669
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07004670/*
4671 * early_calculate_totalpages()
4672 * Sum pages in active regions for movable zone.
4673 * Populate N_HIGH_MEMORY for calculating usable_nodes.
4674 */
Adrian Bunk484f51f2007-10-16 01:26:03 -07004675static unsigned long __init early_calculate_totalpages(void)
Mel Gorman7e63efe2007-07-17 04:03:15 -07004676{
Mel Gorman7e63efe2007-07-17 04:03:15 -07004677 unsigned long totalpages = 0;
Tejun Heoc13291a2011-07-12 10:46:30 +02004678 unsigned long start_pfn, end_pfn;
4679 int i, nid;
Mel Gorman7e63efe2007-07-17 04:03:15 -07004680
Tejun Heoc13291a2011-07-12 10:46:30 +02004681 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) {
4682 unsigned long pages = end_pfn - start_pfn;
4683
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07004684 totalpages += pages;
4685 if (pages)
Tejun Heoc13291a2011-07-12 10:46:30 +02004686 node_set_state(nid, N_HIGH_MEMORY);
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07004687 }
4688 return totalpages;
Mel Gorman7e63efe2007-07-17 04:03:15 -07004689}
4690
Mel Gorman2a1e2742007-07-17 04:03:12 -07004691/*
4692 * Find the PFN the Movable zone begins in each node. Kernel memory
4693 * is spread evenly between nodes as long as the nodes have enough
4694 * memory. When they don't, some nodes will have more kernelcore than
4695 * others
4696 */
Kautuk Consulb224ef82012-03-21 16:34:15 -07004697static void __init find_zone_movable_pfns_for_nodes(void)
Mel Gorman2a1e2742007-07-17 04:03:12 -07004698{
4699 int i, nid;
4700 unsigned long usable_startpfn;
4701 unsigned long kernelcore_node, kernelcore_remaining;
Yinghai Lu66918dc2009-06-30 11:41:37 -07004702 /* save the state before borrow the nodemask */
4703 nodemask_t saved_node_state = node_states[N_HIGH_MEMORY];
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07004704 unsigned long totalpages = early_calculate_totalpages();
4705 int usable_nodes = nodes_weight(node_states[N_HIGH_MEMORY]);
Mel Gorman2a1e2742007-07-17 04:03:12 -07004706
Mel Gorman7e63efe2007-07-17 04:03:15 -07004707 /*
4708 * If movablecore was specified, calculate what size of
4709 * kernelcore that corresponds so that memory usable for
4710 * any allocation type is evenly spread. If both kernelcore
4711 * and movablecore are specified, then the value of kernelcore
4712 * will be used for required_kernelcore if it's greater than
4713 * what movablecore would have allowed.
4714 */
4715 if (required_movablecore) {
Mel Gorman7e63efe2007-07-17 04:03:15 -07004716 unsigned long corepages;
4717
4718 /*
4719 * Round-up so that ZONE_MOVABLE is at least as large as what
4720 * was requested by the user
4721 */
4722 required_movablecore =
4723 roundup(required_movablecore, MAX_ORDER_NR_PAGES);
4724 corepages = totalpages - required_movablecore;
4725
4726 required_kernelcore = max(required_kernelcore, corepages);
4727 }
4728
Mel Gorman2a1e2742007-07-17 04:03:12 -07004729 /* If kernelcore was not specified, there is no ZONE_MOVABLE */
4730 if (!required_kernelcore)
Yinghai Lu66918dc2009-06-30 11:41:37 -07004731 goto out;
Mel Gorman2a1e2742007-07-17 04:03:12 -07004732
4733 /* usable_startpfn is the lowest possible pfn ZONE_MOVABLE can be at */
4734 find_usable_zone_for_movable();
4735 usable_startpfn = arch_zone_lowest_possible_pfn[movable_zone];
4736
4737restart:
4738 /* Spread kernelcore memory as evenly as possible throughout nodes */
4739 kernelcore_node = required_kernelcore / usable_nodes;
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07004740 for_each_node_state(nid, N_HIGH_MEMORY) {
Tejun Heoc13291a2011-07-12 10:46:30 +02004741 unsigned long start_pfn, end_pfn;
4742
Mel Gorman2a1e2742007-07-17 04:03:12 -07004743 /*
4744 * Recalculate kernelcore_node if the division per node
4745 * now exceeds what is necessary to satisfy the requested
4746 * amount of memory for the kernel
4747 */
4748 if (required_kernelcore < kernelcore_node)
4749 kernelcore_node = required_kernelcore / usable_nodes;
4750
4751 /*
4752 * As the map is walked, we track how much memory is usable
4753 * by the kernel using kernelcore_remaining. When it is
4754 * 0, the rest of the node is usable by ZONE_MOVABLE
4755 */
4756 kernelcore_remaining = kernelcore_node;
4757
4758 /* Go through each range of PFNs within this node */
Tejun Heoc13291a2011-07-12 10:46:30 +02004759 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
Mel Gorman2a1e2742007-07-17 04:03:12 -07004760 unsigned long size_pages;
4761
Tejun Heoc13291a2011-07-12 10:46:30 +02004762 start_pfn = max(start_pfn, zone_movable_pfn[nid]);
Mel Gorman2a1e2742007-07-17 04:03:12 -07004763 if (start_pfn >= end_pfn)
4764 continue;
4765
4766 /* Account for what is only usable for kernelcore */
4767 if (start_pfn < usable_startpfn) {
4768 unsigned long kernel_pages;
4769 kernel_pages = min(end_pfn, usable_startpfn)
4770 - start_pfn;
4771
4772 kernelcore_remaining -= min(kernel_pages,
4773 kernelcore_remaining);
4774 required_kernelcore -= min(kernel_pages,
4775 required_kernelcore);
4776
4777 /* Continue if range is now fully accounted */
4778 if (end_pfn <= usable_startpfn) {
4779
4780 /*
4781 * Push zone_movable_pfn to the end so
4782 * that if we have to rebalance
4783 * kernelcore across nodes, we will
4784 * not double account here
4785 */
4786 zone_movable_pfn[nid] = end_pfn;
4787 continue;
4788 }
4789 start_pfn = usable_startpfn;
4790 }
4791
4792 /*
4793 * The usable PFN range for ZONE_MOVABLE is from
4794 * start_pfn->end_pfn. Calculate size_pages as the
4795 * number of pages used as kernelcore
4796 */
4797 size_pages = end_pfn - start_pfn;
4798 if (size_pages > kernelcore_remaining)
4799 size_pages = kernelcore_remaining;
4800 zone_movable_pfn[nid] = start_pfn + size_pages;
4801
4802 /*
4803 * Some kernelcore has been met, update counts and
4804 * break if the kernelcore for this node has been
4805 * satisified
4806 */
4807 required_kernelcore -= min(required_kernelcore,
4808 size_pages);
4809 kernelcore_remaining -= size_pages;
4810 if (!kernelcore_remaining)
4811 break;
4812 }
4813 }
4814
4815 /*
4816 * If there is still required_kernelcore, we do another pass with one
4817 * less node in the count. This will push zone_movable_pfn[nid] further
4818 * along on the nodes that still have memory until kernelcore is
4819 * satisified
4820 */
4821 usable_nodes--;
4822 if (usable_nodes && required_kernelcore > usable_nodes)
4823 goto restart;
4824
4825 /* Align start of ZONE_MOVABLE on all nids to MAX_ORDER_NR_PAGES */
4826 for (nid = 0; nid < MAX_NUMNODES; nid++)
4827 zone_movable_pfn[nid] =
4828 roundup(zone_movable_pfn[nid], MAX_ORDER_NR_PAGES);
Yinghai Lu66918dc2009-06-30 11:41:37 -07004829
4830out:
4831 /* restore the node_state */
4832 node_states[N_HIGH_MEMORY] = saved_node_state;
Mel Gorman2a1e2742007-07-17 04:03:12 -07004833}
4834
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07004835/* Any regular memory on that node ? */
Jiang Liu4ed7e022012-07-31 16:43:35 -07004836static void __init check_for_regular_memory(pg_data_t *pgdat)
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07004837{
4838#ifdef CONFIG_HIGHMEM
4839 enum zone_type zone_type;
4840
4841 for (zone_type = 0; zone_type <= ZONE_NORMAL; zone_type++) {
4842 struct zone *zone = &pgdat->node_zones[zone_type];
Bob Liud0048b02012-01-12 17:19:07 -08004843 if (zone->present_pages) {
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07004844 node_set_state(zone_to_nid(zone), N_NORMAL_MEMORY);
Bob Liud0048b02012-01-12 17:19:07 -08004845 break;
4846 }
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07004847 }
4848#endif
4849}
4850
Mel Gormanc7132162006-09-27 01:49:43 -07004851/**
4852 * free_area_init_nodes - Initialise all pg_data_t and zone data
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004853 * @max_zone_pfn: an array of max PFNs for each zone
Mel Gormanc7132162006-09-27 01:49:43 -07004854 *
4855 * This will call free_area_init_node() for each active node in the system.
4856 * Using the page ranges provided by add_active_range(), the size of each
4857 * zone in each node and their holes is calculated. If the maximum PFN
4858 * between two adjacent zones match, it is assumed that the zone is empty.
4859 * For example, if arch_max_dma_pfn == arch_max_dma32_pfn, it is assumed
4860 * that arch_max_dma32_pfn has no pages. It is also assumed that a zone
4861 * starts where the previous one ended. For example, ZONE_DMA32 starts
4862 * at arch_max_dma_pfn.
4863 */
4864void __init free_area_init_nodes(unsigned long *max_zone_pfn)
4865{
Tejun Heoc13291a2011-07-12 10:46:30 +02004866 unsigned long start_pfn, end_pfn;
4867 int i, nid;
Mel Gormana6af2bc2007-02-10 01:42:57 -08004868
Mel Gormanc7132162006-09-27 01:49:43 -07004869 /* Record where the zone boundaries are */
4870 memset(arch_zone_lowest_possible_pfn, 0,
4871 sizeof(arch_zone_lowest_possible_pfn));
4872 memset(arch_zone_highest_possible_pfn, 0,
4873 sizeof(arch_zone_highest_possible_pfn));
4874 arch_zone_lowest_possible_pfn[0] = find_min_pfn_with_active_regions();
4875 arch_zone_highest_possible_pfn[0] = max_zone_pfn[0];
4876 for (i = 1; i < MAX_NR_ZONES; i++) {
Mel Gorman2a1e2742007-07-17 04:03:12 -07004877 if (i == ZONE_MOVABLE)
4878 continue;
Mel Gormanc7132162006-09-27 01:49:43 -07004879 arch_zone_lowest_possible_pfn[i] =
4880 arch_zone_highest_possible_pfn[i-1];
4881 arch_zone_highest_possible_pfn[i] =
4882 max(max_zone_pfn[i], arch_zone_lowest_possible_pfn[i]);
4883 }
Mel Gorman2a1e2742007-07-17 04:03:12 -07004884 arch_zone_lowest_possible_pfn[ZONE_MOVABLE] = 0;
4885 arch_zone_highest_possible_pfn[ZONE_MOVABLE] = 0;
4886
4887 /* Find the PFNs that ZONE_MOVABLE begins at in each node */
4888 memset(zone_movable_pfn, 0, sizeof(zone_movable_pfn));
Kautuk Consulb224ef82012-03-21 16:34:15 -07004889 find_zone_movable_pfns_for_nodes();
Mel Gormanc7132162006-09-27 01:49:43 -07004890
Mel Gormanc7132162006-09-27 01:49:43 -07004891 /* Print out the zone ranges */
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07004892 printk("Zone ranges:\n");
Mel Gorman2a1e2742007-07-17 04:03:12 -07004893 for (i = 0; i < MAX_NR_ZONES; i++) {
4894 if (i == ZONE_MOVABLE)
4895 continue;
Kay Sievers155cbfc2012-05-08 17:24:14 +02004896 printk(KERN_CONT " %-8s ", zone_names[i]);
David Rientjes72f0ba02010-03-05 13:42:14 -08004897 if (arch_zone_lowest_possible_pfn[i] ==
4898 arch_zone_highest_possible_pfn[i])
Kay Sievers155cbfc2012-05-08 17:24:14 +02004899 printk(KERN_CONT "empty\n");
David Rientjes72f0ba02010-03-05 13:42:14 -08004900 else
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07004901 printk(KERN_CONT "[mem %0#10lx-%0#10lx]\n",
4902 arch_zone_lowest_possible_pfn[i] << PAGE_SHIFT,
4903 (arch_zone_highest_possible_pfn[i]
4904 << PAGE_SHIFT) - 1);
Mel Gorman2a1e2742007-07-17 04:03:12 -07004905 }
4906
4907 /* Print out the PFNs ZONE_MOVABLE begins at in each node */
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07004908 printk("Movable zone start for each node\n");
Mel Gorman2a1e2742007-07-17 04:03:12 -07004909 for (i = 0; i < MAX_NUMNODES; i++) {
4910 if (zone_movable_pfn[i])
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07004911 printk(" Node %d: %#010lx\n", i,
4912 zone_movable_pfn[i] << PAGE_SHIFT);
Mel Gorman2a1e2742007-07-17 04:03:12 -07004913 }
Mel Gormanc7132162006-09-27 01:49:43 -07004914
4915 /* Print out the early_node_map[] */
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07004916 printk("Early memory node ranges\n");
Tejun Heoc13291a2011-07-12 10:46:30 +02004917 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid)
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07004918 printk(" node %3d: [mem %#010lx-%#010lx]\n", nid,
4919 start_pfn << PAGE_SHIFT, (end_pfn << PAGE_SHIFT) - 1);
Mel Gormanc7132162006-09-27 01:49:43 -07004920
4921 /* Initialise every node */
Mel Gorman708614e2008-07-23 21:26:51 -07004922 mminit_verify_pageflags_layout();
Christoph Lameter8ef82862007-02-20 13:57:52 -08004923 setup_nr_node_ids();
Mel Gormanc7132162006-09-27 01:49:43 -07004924 for_each_online_node(nid) {
4925 pg_data_t *pgdat = NODE_DATA(nid);
Johannes Weiner9109fb72008-07-23 21:27:20 -07004926 free_area_init_node(nid, NULL,
Mel Gormanc7132162006-09-27 01:49:43 -07004927 find_min_pfn_for_node(nid), NULL);
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07004928
4929 /* Any memory on that node */
4930 if (pgdat->node_present_pages)
4931 node_set_state(nid, N_HIGH_MEMORY);
4932 check_for_regular_memory(pgdat);
Mel Gormanc7132162006-09-27 01:49:43 -07004933 }
4934}
Mel Gorman2a1e2742007-07-17 04:03:12 -07004935
Mel Gorman7e63efe2007-07-17 04:03:15 -07004936static int __init cmdline_parse_core(char *p, unsigned long *core)
Mel Gorman2a1e2742007-07-17 04:03:12 -07004937{
4938 unsigned long long coremem;
4939 if (!p)
4940 return -EINVAL;
4941
4942 coremem = memparse(p, &p);
Mel Gorman7e63efe2007-07-17 04:03:15 -07004943 *core = coremem >> PAGE_SHIFT;
Mel Gorman2a1e2742007-07-17 04:03:12 -07004944
Mel Gorman7e63efe2007-07-17 04:03:15 -07004945 /* Paranoid check that UL is enough for the coremem value */
Mel Gorman2a1e2742007-07-17 04:03:12 -07004946 WARN_ON((coremem >> PAGE_SHIFT) > ULONG_MAX);
4947
4948 return 0;
4949}
Mel Gormaned7ed362007-07-17 04:03:14 -07004950
Mel Gorman7e63efe2007-07-17 04:03:15 -07004951/*
4952 * kernelcore=size sets the amount of memory for use for allocations that
4953 * cannot be reclaimed or migrated.
4954 */
4955static int __init cmdline_parse_kernelcore(char *p)
4956{
4957 return cmdline_parse_core(p, &required_kernelcore);
4958}
4959
4960/*
4961 * movablecore=size sets the amount of memory for use for allocations that
4962 * can be reclaimed or migrated.
4963 */
4964static int __init cmdline_parse_movablecore(char *p)
4965{
4966 return cmdline_parse_core(p, &required_movablecore);
4967}
4968
Mel Gormaned7ed362007-07-17 04:03:14 -07004969early_param("kernelcore", cmdline_parse_kernelcore);
Mel Gorman7e63efe2007-07-17 04:03:15 -07004970early_param("movablecore", cmdline_parse_movablecore);
Mel Gormaned7ed362007-07-17 04:03:14 -07004971
Tejun Heo0ee332c2011-12-08 10:22:09 -08004972#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
Mel Gormanc7132162006-09-27 01:49:43 -07004973
Mel Gorman0e0b8642006-09-27 01:49:56 -07004974/**
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004975 * set_dma_reserve - set the specified number of pages reserved in the first zone
4976 * @new_dma_reserve: The number of pages to mark reserved
Mel Gorman0e0b8642006-09-27 01:49:56 -07004977 *
4978 * The per-cpu batchsize and zone watermarks are determined by present_pages.
4979 * In the DMA zone, a significant percentage may be consumed by kernel image
4980 * and other unfreeable allocations which can skew the watermarks badly. This
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004981 * function may optionally be used to account for unfreeable pages in the
4982 * first zone (e.g., ZONE_DMA). The effect will be lower watermarks and
4983 * smaller per-cpu batchsize.
Mel Gorman0e0b8642006-09-27 01:49:56 -07004984 */
4985void __init set_dma_reserve(unsigned long new_dma_reserve)
4986{
4987 dma_reserve = new_dma_reserve;
4988}
4989
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990void __init free_area_init(unsigned long *zones_size)
4991{
Johannes Weiner9109fb72008-07-23 21:27:20 -07004992 free_area_init_node(0, zones_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
4994}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996static int page_alloc_cpu_notify(struct notifier_block *self,
4997 unsigned long action, void *hcpu)
4998{
4999 int cpu = (unsigned long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005001 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
Konstantin Khlebnikovf0cb3c72012-03-21 16:34:06 -07005002 lru_add_drain_cpu(cpu);
Christoph Lameter9f8f2172008-02-04 22:29:11 -08005003 drain_pages(cpu);
5004
5005 /*
5006 * Spill the event counters of the dead processor
5007 * into the current processors event counters.
5008 * This artificially elevates the count of the current
5009 * processor.
5010 */
Christoph Lameterf8891e52006-06-30 01:55:45 -07005011 vm_events_fold_cpu(cpu);
Christoph Lameter9f8f2172008-02-04 22:29:11 -08005012
5013 /*
5014 * Zero the differential counters of the dead processor
5015 * so that the vm statistics are consistent.
5016 *
5017 * This is only okay since the processor is dead and cannot
5018 * race with what we are doing.
5019 */
Christoph Lameter2244b952006-06-30 01:55:33 -07005020 refresh_cpu_vm_stats(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 }
5022 return NOTIFY_OK;
5023}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024
5025void __init page_alloc_init(void)
5026{
5027 hotcpu_notifier(page_alloc_cpu_notify, 0);
5028}
5029
5030/*
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07005031 * calculate_totalreserve_pages - called when sysctl_lower_zone_reserve_ratio
5032 * or min_free_kbytes changes.
5033 */
5034static void calculate_totalreserve_pages(void)
5035{
5036 struct pglist_data *pgdat;
5037 unsigned long reserve_pages = 0;
Christoph Lameter2f6726e2006-09-25 23:31:18 -07005038 enum zone_type i, j;
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07005039
5040 for_each_online_pgdat(pgdat) {
5041 for (i = 0; i < MAX_NR_ZONES; i++) {
5042 struct zone *zone = pgdat->node_zones + i;
5043 unsigned long max = 0;
5044
5045 /* Find valid and maximum lowmem_reserve in the zone */
5046 for (j = i; j < MAX_NR_ZONES; j++) {
5047 if (zone->lowmem_reserve[j] > max)
5048 max = zone->lowmem_reserve[j];
5049 }
5050
Mel Gorman41858962009-06-16 15:32:12 -07005051 /* we treat the high watermark as reserved pages. */
5052 max += high_wmark_pages(zone);
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07005053
5054 if (max > zone->present_pages)
5055 max = zone->present_pages;
5056 reserve_pages += max;
Johannes Weinerab8fabd2012-01-10 15:07:42 -08005057 /*
5058 * Lowmem reserves are not available to
5059 * GFP_HIGHUSER page cache allocations and
5060 * kswapd tries to balance zones to their high
5061 * watermark. As a result, neither should be
5062 * regarded as dirtyable memory, to prevent a
5063 * situation where reclaim has to clean pages
5064 * in order to balance the zones.
5065 */
5066 zone->dirty_balance_reserve = max;
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07005067 }
5068 }
Johannes Weinerab8fabd2012-01-10 15:07:42 -08005069 dirty_balance_reserve = reserve_pages;
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07005070 totalreserve_pages = reserve_pages;
5071}
5072
5073/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 * setup_per_zone_lowmem_reserve - called whenever
5075 * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
5076 * has a correct pages reserved value, so an adequate number of
5077 * pages are left in the zone after a successful __alloc_pages().
5078 */
5079static void setup_per_zone_lowmem_reserve(void)
5080{
5081 struct pglist_data *pgdat;
Christoph Lameter2f6726e2006-09-25 23:31:18 -07005082 enum zone_type j, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083
KAMEZAWA Hiroyukiec936fc2006-03-27 01:15:59 -08005084 for_each_online_pgdat(pgdat) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 for (j = 0; j < MAX_NR_ZONES; j++) {
5086 struct zone *zone = pgdat->node_zones + j;
5087 unsigned long present_pages = zone->present_pages;
5088
5089 zone->lowmem_reserve[j] = 0;
5090
Christoph Lameter2f6726e2006-09-25 23:31:18 -07005091 idx = j;
5092 while (idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 struct zone *lower_zone;
5094
Christoph Lameter2f6726e2006-09-25 23:31:18 -07005095 idx--;
5096
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 if (sysctl_lowmem_reserve_ratio[idx] < 1)
5098 sysctl_lowmem_reserve_ratio[idx] = 1;
5099
5100 lower_zone = pgdat->node_zones + idx;
5101 lower_zone->lowmem_reserve[j] = present_pages /
5102 sysctl_lowmem_reserve_ratio[idx];
5103 present_pages += lower_zone->present_pages;
5104 }
5105 }
5106 }
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07005107
5108 /* update totalreserve_pages */
5109 calculate_totalreserve_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110}
5111
Mel Gormancfd3da12011-04-25 21:36:42 +00005112static void __setup_per_zone_wmarks(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113{
5114 unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
5115 unsigned long lowmem_pages = 0;
5116 struct zone *zone;
5117 unsigned long flags;
5118
5119 /* Calculate total number of !ZONE_HIGHMEM pages */
5120 for_each_zone(zone) {
5121 if (!is_highmem(zone))
5122 lowmem_pages += zone->present_pages;
5123 }
5124
5125 for_each_zone(zone) {
Andrew Mortonac924c62006-05-15 09:43:59 -07005126 u64 tmp;
5127
Gerald Schaefer1125b4e2008-10-18 20:27:11 -07005128 spin_lock_irqsave(&zone->lock, flags);
Andrew Mortonac924c62006-05-15 09:43:59 -07005129 tmp = (u64)pages_min * zone->present_pages;
5130 do_div(tmp, lowmem_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 if (is_highmem(zone)) {
5132 /*
Nick Piggin669ed172005-11-13 16:06:45 -08005133 * __GFP_HIGH and PF_MEMALLOC allocations usually don't
5134 * need highmem pages, so cap pages_min to a small
5135 * value here.
5136 *
Mel Gorman41858962009-06-16 15:32:12 -07005137 * The WMARK_HIGH-WMARK_LOW and (WMARK_LOW-WMARK_MIN)
Nick Piggin669ed172005-11-13 16:06:45 -08005138 * deltas controls asynch page reclaim, and so should
5139 * not be capped for highmem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 */
5141 int min_pages;
5142
5143 min_pages = zone->present_pages / 1024;
5144 if (min_pages < SWAP_CLUSTER_MAX)
5145 min_pages = SWAP_CLUSTER_MAX;
5146 if (min_pages > 128)
5147 min_pages = 128;
Mel Gorman41858962009-06-16 15:32:12 -07005148 zone->watermark[WMARK_MIN] = min_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 } else {
Nick Piggin669ed172005-11-13 16:06:45 -08005150 /*
5151 * If it's a lowmem zone, reserve a number of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 * proportionate to the zone's size.
5153 */
Mel Gorman41858962009-06-16 15:32:12 -07005154 zone->watermark[WMARK_MIN] = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 }
5156
Mel Gorman41858962009-06-16 15:32:12 -07005157 zone->watermark[WMARK_LOW] = min_wmark_pages(zone) + (tmp >> 2);
5158 zone->watermark[WMARK_HIGH] = min_wmark_pages(zone) + (tmp >> 1);
Marek Szyprowski49f223a2012-01-25 12:49:24 +01005159
5160 zone->watermark[WMARK_MIN] += cma_wmark_pages(zone);
5161 zone->watermark[WMARK_LOW] += cma_wmark_pages(zone);
5162 zone->watermark[WMARK_HIGH] += cma_wmark_pages(zone);
5163
Mel Gorman56fd56b2007-10-16 01:25:58 -07005164 setup_zone_migrate_reserve(zone);
Gerald Schaefer1125b4e2008-10-18 20:27:11 -07005165 spin_unlock_irqrestore(&zone->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 }
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07005167
5168 /* update totalreserve_pages */
5169 calculate_totalreserve_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170}
5171
Mel Gormancfd3da12011-04-25 21:36:42 +00005172/**
5173 * setup_per_zone_wmarks - called when min_free_kbytes changes
5174 * or when memory is hot-{added|removed}
5175 *
5176 * Ensures that the watermark[min,low,high] values for each zone are set
5177 * correctly with respect to min_free_kbytes.
5178 */
5179void setup_per_zone_wmarks(void)
5180{
5181 mutex_lock(&zonelists_mutex);
5182 __setup_per_zone_wmarks();
5183 mutex_unlock(&zonelists_mutex);
5184}
5185
Randy Dunlap55a44622009-09-21 17:01:20 -07005186/*
Rik van Riel556adec2008-10-18 20:26:34 -07005187 * The inactive anon list should be small enough that the VM never has to
5188 * do too much work, but large enough that each inactive page has a chance
5189 * to be referenced again before it is swapped out.
5190 *
5191 * The inactive_anon ratio is the target ratio of ACTIVE_ANON to
5192 * INACTIVE_ANON pages on this zone's LRU, maintained by the
5193 * pageout code. A zone->inactive_ratio of 3 means 3:1 or 25% of
5194 * the anonymous pages are kept on the inactive list.
5195 *
5196 * total target max
5197 * memory ratio inactive anon
5198 * -------------------------------------
5199 * 10MB 1 5MB
5200 * 100MB 1 50MB
5201 * 1GB 3 250MB
5202 * 10GB 10 0.9GB
5203 * 100GB 31 3GB
5204 * 1TB 101 10GB
5205 * 10TB 320 32GB
5206 */
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07005207static void __meminit calculate_zone_inactive_ratio(struct zone *zone)
Minchan Kim96cb4df2009-06-16 15:32:49 -07005208{
5209 unsigned int gb, ratio;
5210
5211 /* Zone size in gigabytes */
5212 gb = zone->present_pages >> (30 - PAGE_SHIFT);
5213 if (gb)
5214 ratio = int_sqrt(10 * gb);
5215 else
5216 ratio = 1;
5217
5218 zone->inactive_ratio = ratio;
5219}
5220
KOSAKI Motohiro839a4fc2011-05-24 17:11:31 -07005221static void __meminit setup_per_zone_inactive_ratio(void)
Rik van Riel556adec2008-10-18 20:26:34 -07005222{
5223 struct zone *zone;
5224
Minchan Kim96cb4df2009-06-16 15:32:49 -07005225 for_each_zone(zone)
5226 calculate_zone_inactive_ratio(zone);
Rik van Riel556adec2008-10-18 20:26:34 -07005227}
5228
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229/*
5230 * Initialise min_free_kbytes.
5231 *
5232 * For small machines we want it small (128k min). For large machines
5233 * we want it large (64MB max). But it is not linear, because network
5234 * bandwidth does not increase linearly with machine size. We use
5235 *
5236 * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
5237 * min_free_kbytes = sqrt(lowmem_kbytes * 16)
5238 *
5239 * which yields
5240 *
5241 * 16MB: 512k
5242 * 32MB: 724k
5243 * 64MB: 1024k
5244 * 128MB: 1448k
5245 * 256MB: 2048k
5246 * 512MB: 2896k
5247 * 1024MB: 4096k
5248 * 2048MB: 5792k
5249 * 4096MB: 8192k
5250 * 8192MB: 11584k
5251 * 16384MB: 16384k
5252 */
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07005253int __meminit init_per_zone_wmark_min(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254{
5255 unsigned long lowmem_kbytes;
5256
5257 lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
5258
5259 min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
5260 if (min_free_kbytes < 128)
5261 min_free_kbytes = 128;
5262 if (min_free_kbytes > 65536)
5263 min_free_kbytes = 65536;
Minchan Kimbc75d332009-06-16 15:32:48 -07005264 setup_per_zone_wmarks();
KOSAKI Motohiroa6cccdc2011-05-24 17:11:33 -07005265 refresh_zone_stat_thresholds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 setup_per_zone_lowmem_reserve();
Rik van Riel556adec2008-10-18 20:26:34 -07005267 setup_per_zone_inactive_ratio();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 return 0;
5269}
Minchan Kimbc75d332009-06-16 15:32:48 -07005270module_init(init_per_zone_wmark_min)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271
5272/*
5273 * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
5274 * that we can call two helper functions whenever min_free_kbytes
5275 * changes.
5276 */
5277int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005278 void __user *buffer, size_t *length, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005280 proc_dointvec(table, write, buffer, length, ppos);
Mel Gorman3b1d92c2007-05-06 14:49:30 -07005281 if (write)
Minchan Kimbc75d332009-06-16 15:32:48 -07005282 setup_per_zone_wmarks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 return 0;
5284}
5285
Christoph Lameter96146342006-07-03 00:24:13 -07005286#ifdef CONFIG_NUMA
5287int sysctl_min_unmapped_ratio_sysctl_handler(ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005288 void __user *buffer, size_t *length, loff_t *ppos)
Christoph Lameter96146342006-07-03 00:24:13 -07005289{
5290 struct zone *zone;
5291 int rc;
5292
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005293 rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
Christoph Lameter96146342006-07-03 00:24:13 -07005294 if (rc)
5295 return rc;
5296
5297 for_each_zone(zone)
Christoph Lameter8417bba2006-09-25 23:31:51 -07005298 zone->min_unmapped_pages = (zone->present_pages *
Christoph Lameter96146342006-07-03 00:24:13 -07005299 sysctl_min_unmapped_ratio) / 100;
5300 return 0;
5301}
Christoph Lameter0ff38492006-09-25 23:31:52 -07005302
5303int sysctl_min_slab_ratio_sysctl_handler(ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005304 void __user *buffer, size_t *length, loff_t *ppos)
Christoph Lameter0ff38492006-09-25 23:31:52 -07005305{
5306 struct zone *zone;
5307 int rc;
5308
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005309 rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
Christoph Lameter0ff38492006-09-25 23:31:52 -07005310 if (rc)
5311 return rc;
5312
5313 for_each_zone(zone)
5314 zone->min_slab_pages = (zone->present_pages *
5315 sysctl_min_slab_ratio) / 100;
5316 return 0;
5317}
Christoph Lameter96146342006-07-03 00:24:13 -07005318#endif
5319
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320/*
5321 * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
5322 * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
5323 * whenever sysctl_lowmem_reserve_ratio changes.
5324 *
5325 * The reserve ratio obviously has absolutely no relation with the
Mel Gorman41858962009-06-16 15:32:12 -07005326 * minimum watermarks. The lowmem reserve ratio can only make sense
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 * if in function of the boot time zone sizes.
5328 */
5329int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005330 void __user *buffer, size_t *length, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005332 proc_dointvec_minmax(table, write, buffer, length, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 setup_per_zone_lowmem_reserve();
5334 return 0;
5335}
5336
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08005337/*
5338 * percpu_pagelist_fraction - changes the pcp->high for each zone on each
5339 * cpu. It is the fraction of total pages in each zone that a hot per cpu pagelist
5340 * can have before it gets flushed back to buddy allocator.
5341 */
5342
5343int percpu_pagelist_fraction_sysctl_handler(ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005344 void __user *buffer, size_t *length, loff_t *ppos)
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08005345{
5346 struct zone *zone;
5347 unsigned int cpu;
5348 int ret;
5349
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005350 ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
Sasha Levin93278812012-05-10 13:01:44 -07005351 if (!write || (ret < 0))
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08005352 return ret;
Dimitri Sivanich364df0e2009-06-23 12:37:04 -07005353 for_each_populated_zone(zone) {
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09005354 for_each_possible_cpu(cpu) {
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08005355 unsigned long high;
5356 high = zone->present_pages / percpu_pagelist_fraction;
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09005357 setup_pagelist_highmark(
5358 per_cpu_ptr(zone->pageset, cpu), high);
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08005359 }
5360 }
5361 return 0;
5362}
5363
David S. Millerf034b5d2006-08-24 03:08:07 -07005364int hashdist = HASHDIST_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365
5366#ifdef CONFIG_NUMA
5367static int __init set_hashdist(char *str)
5368{
5369 if (!str)
5370 return 0;
5371 hashdist = simple_strtoul(str, &str, 0);
5372 return 1;
5373}
5374__setup("hashdist=", set_hashdist);
5375#endif
5376
5377/*
5378 * allocate a large system hash table from bootmem
5379 * - it is assumed that the hash table must contain an exact power-of-2
5380 * quantity of entries
5381 * - limit is the number of hash buckets, not the total allocation size
5382 */
5383void *__init alloc_large_system_hash(const char *tablename,
5384 unsigned long bucketsize,
5385 unsigned long numentries,
5386 int scale,
5387 int flags,
5388 unsigned int *_hash_shift,
5389 unsigned int *_hash_mask,
Tim Bird31fe62b2012-05-23 13:33:35 +00005390 unsigned long low_limit,
5391 unsigned long high_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392{
Tim Bird31fe62b2012-05-23 13:33:35 +00005393 unsigned long long max = high_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 unsigned long log2qty, size;
5395 void *table = NULL;
5396
5397 /* allow the kernel cmdline to have a say */
5398 if (!numentries) {
5399 /* round applicable memory size up to nearest megabyte */
Andrew Morton04903662006-12-06 20:37:33 -08005400 numentries = nr_kernel_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
5402 numentries >>= 20 - PAGE_SHIFT;
5403 numentries <<= 20 - PAGE_SHIFT;
5404
5405 /* limit to 1 bucket per 2^scale bytes of low memory */
5406 if (scale > PAGE_SHIFT)
5407 numentries >>= (scale - PAGE_SHIFT);
5408 else
5409 numentries <<= (PAGE_SHIFT - scale);
Paul Mundt9ab37b82007-01-05 16:36:30 -08005410
5411 /* Make sure we've got at least a 0-order allocation.. */
Jan Beulich2c85f512009-09-21 17:03:07 -07005412 if (unlikely(flags & HASH_SMALL)) {
5413 /* Makes no sense without HASH_EARLY */
5414 WARN_ON(!(flags & HASH_EARLY));
5415 if (!(numentries >> *_hash_shift)) {
5416 numentries = 1UL << *_hash_shift;
5417 BUG_ON(!numentries);
5418 }
5419 } else if (unlikely((numentries * bucketsize) < PAGE_SIZE))
Paul Mundt9ab37b82007-01-05 16:36:30 -08005420 numentries = PAGE_SIZE / bucketsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 }
John Hawkes6e692ed2006-03-25 03:08:02 -08005422 numentries = roundup_pow_of_two(numentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423
5424 /* limit allocation size to 1/16 total memory by default */
5425 if (max == 0) {
5426 max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
5427 do_div(max, bucketsize);
5428 }
Dimitri Sivanich074b8512012-02-08 12:39:07 -08005429 max = min(max, 0x80000000ULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430
Tim Bird31fe62b2012-05-23 13:33:35 +00005431 if (numentries < low_limit)
5432 numentries = low_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 if (numentries > max)
5434 numentries = max;
5435
David Howellsf0d1b0b2006-12-08 02:37:49 -08005436 log2qty = ilog2(numentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437
5438 do {
5439 size = bucketsize << log2qty;
5440 if (flags & HASH_EARLY)
Jan Beulich74768ed2008-08-12 15:08:39 -07005441 table = alloc_bootmem_nopanic(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 else if (hashdist)
5443 table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
5444 else {
Eric Dumazet1037b832007-07-15 23:38:05 -07005445 /*
5446 * If bucketsize is not a power-of-two, we may free
Mel Gormana1dd2682009-06-16 15:32:19 -07005447 * some pages at the end of hash table which
5448 * alloc_pages_exact() automatically does
Eric Dumazet1037b832007-07-15 23:38:05 -07005449 */
Catalin Marinas264ef8a2009-07-07 10:33:01 +01005450 if (get_order(size) < MAX_ORDER) {
Mel Gormana1dd2682009-06-16 15:32:19 -07005451 table = alloc_pages_exact(size, GFP_ATOMIC);
Catalin Marinas264ef8a2009-07-07 10:33:01 +01005452 kmemleak_alloc(table, size, 1, GFP_ATOMIC);
5453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 }
5455 } while (!table && size > PAGE_SIZE && --log2qty);
5456
5457 if (!table)
5458 panic("Failed to allocate %s hash table\n", tablename);
5459
Robin Holtf241e6602010-10-07 12:59:26 -07005460 printk(KERN_INFO "%s hash table entries: %ld (order: %d, %lu bytes)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 tablename,
Robin Holtf241e6602010-10-07 12:59:26 -07005462 (1UL << log2qty),
David Howellsf0d1b0b2006-12-08 02:37:49 -08005463 ilog2(size) - PAGE_SHIFT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 size);
5465
5466 if (_hash_shift)
5467 *_hash_shift = log2qty;
5468 if (_hash_mask)
5469 *_hash_mask = (1 << log2qty) - 1;
5470
5471 return table;
5472}
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -08005473
Mel Gorman835c1342007-10-16 01:25:47 -07005474/* Return a pointer to the bitmap storing bits affecting a block of pages */
5475static inline unsigned long *get_pageblock_bitmap(struct zone *zone,
5476 unsigned long pfn)
5477{
5478#ifdef CONFIG_SPARSEMEM
5479 return __pfn_to_section(pfn)->pageblock_flags;
5480#else
5481 return zone->pageblock_flags;
5482#endif /* CONFIG_SPARSEMEM */
5483}
Andrew Morton6220ec72006-10-19 23:29:05 -07005484
Mel Gorman835c1342007-10-16 01:25:47 -07005485static inline int pfn_to_bitidx(struct zone *zone, unsigned long pfn)
5486{
5487#ifdef CONFIG_SPARSEMEM
5488 pfn &= (PAGES_PER_SECTION-1);
Mel Gormand9c23402007-10-16 01:26:01 -07005489 return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
Mel Gorman835c1342007-10-16 01:25:47 -07005490#else
5491 pfn = pfn - zone->zone_start_pfn;
Mel Gormand9c23402007-10-16 01:26:01 -07005492 return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
Mel Gorman835c1342007-10-16 01:25:47 -07005493#endif /* CONFIG_SPARSEMEM */
5494}
5495
5496/**
Mel Gormand9c23402007-10-16 01:26:01 -07005497 * 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 -07005498 * @page: The page within the block of interest
5499 * @start_bitidx: The first bit of interest to retrieve
5500 * @end_bitidx: The last bit of interest
5501 * returns pageblock_bits flags
5502 */
5503unsigned long get_pageblock_flags_group(struct page *page,
5504 int start_bitidx, int end_bitidx)
5505{
5506 struct zone *zone;
5507 unsigned long *bitmap;
5508 unsigned long pfn, bitidx;
5509 unsigned long flags = 0;
5510 unsigned long value = 1;
5511
5512 zone = page_zone(page);
5513 pfn = page_to_pfn(page);
5514 bitmap = get_pageblock_bitmap(zone, pfn);
5515 bitidx = pfn_to_bitidx(zone, pfn);
5516
5517 for (; start_bitidx <= end_bitidx; start_bitidx++, value <<= 1)
5518 if (test_bit(bitidx + start_bitidx, bitmap))
5519 flags |= value;
5520
5521 return flags;
5522}
5523
5524/**
Mel Gormand9c23402007-10-16 01:26:01 -07005525 * 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 -07005526 * @page: The page within the block of interest
5527 * @start_bitidx: The first bit of interest
5528 * @end_bitidx: The last bit of interest
5529 * @flags: The flags to set
5530 */
5531void set_pageblock_flags_group(struct page *page, unsigned long flags,
5532 int start_bitidx, int end_bitidx)
5533{
5534 struct zone *zone;
5535 unsigned long *bitmap;
5536 unsigned long pfn, bitidx;
5537 unsigned long value = 1;
5538
5539 zone = page_zone(page);
5540 pfn = page_to_pfn(page);
5541 bitmap = get_pageblock_bitmap(zone, pfn);
5542 bitidx = pfn_to_bitidx(zone, pfn);
KAMEZAWA Hiroyuki86051ca2008-04-29 00:58:21 -07005543 VM_BUG_ON(pfn < zone->zone_start_pfn);
5544 VM_BUG_ON(pfn >= zone->zone_start_pfn + zone->spanned_pages);
Mel Gorman835c1342007-10-16 01:25:47 -07005545
5546 for (; start_bitidx <= end_bitidx; start_bitidx++, value <<= 1)
5547 if (flags & value)
5548 __set_bit(bitidx + start_bitidx, bitmap);
5549 else
5550 __clear_bit(bitidx + start_bitidx, bitmap);
5551}
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -07005552
5553/*
Minchan Kim80934512012-07-31 16:43:01 -07005554 * This function checks whether pageblock includes unmovable pages or not.
5555 * If @count is not zero, it is okay to include less @count unmovable pages
5556 *
5557 * PageLRU check wihtout isolation or lru_lock could race so that
5558 * MIGRATE_MOVABLE block might include unmovable pages. It means you can't
5559 * expect this function should be exact.
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -07005560 */
Minchan Kimee6f5092012-07-31 16:43:50 -07005561bool has_unmovable_pages(struct zone *zone, struct page *page, int count)
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005562{
5563 unsigned long pfn, iter, found;
Michal Nazarewicz47118af2011-12-29 13:09:50 +01005564 int mt;
5565
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005566 /*
5567 * For avoiding noise data, lru_add_drain_all() should be called
Minchan Kim80934512012-07-31 16:43:01 -07005568 * If ZONE_MOVABLE, the zone never contains unmovable pages
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005569 */
5570 if (zone_idx(zone) == ZONE_MOVABLE)
Minchan Kim80934512012-07-31 16:43:01 -07005571 return false;
Michal Nazarewicz47118af2011-12-29 13:09:50 +01005572 mt = get_pageblock_migratetype(page);
5573 if (mt == MIGRATE_MOVABLE || is_migrate_cma(mt))
Minchan Kim80934512012-07-31 16:43:01 -07005574 return false;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005575
5576 pfn = page_to_pfn(page);
5577 for (found = 0, iter = 0; iter < pageblock_nr_pages; iter++) {
5578 unsigned long check = pfn + iter;
5579
Namhyung Kim29723fc2011-02-25 14:44:25 -08005580 if (!pfn_valid_within(check))
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005581 continue;
Namhyung Kim29723fc2011-02-25 14:44:25 -08005582
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005583 page = pfn_to_page(check);
Minchan Kim97d255c2012-07-31 16:42:59 -07005584 /*
5585 * We can't use page_count without pin a page
5586 * because another CPU can free compound page.
5587 * This check already skips compound tails of THP
5588 * because their page->_count is zero at all time.
5589 */
5590 if (!atomic_read(&page->_count)) {
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005591 if (PageBuddy(page))
5592 iter += (1 << page_order(page)) - 1;
5593 continue;
5594 }
Minchan Kim97d255c2012-07-31 16:42:59 -07005595
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005596 if (!PageLRU(page))
5597 found++;
5598 /*
5599 * If there are RECLAIMABLE pages, we need to check it.
5600 * But now, memory offline itself doesn't call shrink_slab()
5601 * and it still to be fixed.
5602 */
5603 /*
5604 * If the page is not RAM, page_count()should be 0.
5605 * we don't need more check. This is an _used_ not-movable page.
5606 *
5607 * The problematic thing here is PG_reserved pages. PG_reserved
5608 * is set to both of a memory hole page and a _used_ kernel
5609 * page at boot.
5610 */
5611 if (found > count)
Minchan Kim80934512012-07-31 16:43:01 -07005612 return true;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005613 }
Minchan Kim80934512012-07-31 16:43:01 -07005614 return false;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005615}
5616
5617bool is_pageblock_removable_nolock(struct page *page)
5618{
Michal Hocko656a0702012-01-20 14:33:58 -08005619 struct zone *zone;
5620 unsigned long pfn;
Michal Hocko687875f2012-01-20 14:33:55 -08005621
5622 /*
5623 * We have to be careful here because we are iterating over memory
5624 * sections which are not zone aware so we might end up outside of
5625 * the zone but still within the section.
Michal Hocko656a0702012-01-20 14:33:58 -08005626 * We have to take care about the node as well. If the node is offline
5627 * its NODE_DATA will be NULL - see page_zone.
Michal Hocko687875f2012-01-20 14:33:55 -08005628 */
Michal Hocko656a0702012-01-20 14:33:58 -08005629 if (!node_online(page_to_nid(page)))
5630 return false;
5631
5632 zone = page_zone(page);
5633 pfn = page_to_pfn(page);
5634 if (zone->zone_start_pfn > pfn ||
Michal Hocko687875f2012-01-20 14:33:55 -08005635 zone->zone_start_pfn + zone->spanned_pages <= pfn)
5636 return false;
5637
Minchan Kimee6f5092012-07-31 16:43:50 -07005638 return !has_unmovable_pages(zone, page, 0);
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -07005639}
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07005640
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01005641#ifdef CONFIG_CMA
5642
5643static unsigned long pfn_max_align_down(unsigned long pfn)
5644{
5645 return pfn & ~(max_t(unsigned long, MAX_ORDER_NR_PAGES,
5646 pageblock_nr_pages) - 1);
5647}
5648
5649static unsigned long pfn_max_align_up(unsigned long pfn)
5650{
5651 return ALIGN(pfn, max_t(unsigned long, MAX_ORDER_NR_PAGES,
5652 pageblock_nr_pages));
5653}
5654
5655static struct page *
5656__alloc_contig_migrate_alloc(struct page *page, unsigned long private,
5657 int **resultp)
5658{
Rabin Vincent6a6dccb2012-07-05 15:52:23 +05305659 gfp_t gfp_mask = GFP_USER | __GFP_MOVABLE;
5660
5661 if (PageHighMem(page))
5662 gfp_mask |= __GFP_HIGHMEM;
5663
5664 return alloc_page(gfp_mask);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01005665}
5666
5667/* [start, end) must belong to a single zone. */
5668static int __alloc_contig_migrate_range(unsigned long start, unsigned long end)
5669{
5670 /* This function is based on compact_zone() from compaction.c. */
5671
5672 unsigned long pfn = start;
5673 unsigned int tries = 0;
5674 int ret = 0;
5675
5676 struct compact_control cc = {
5677 .nr_migratepages = 0,
5678 .order = -1,
5679 .zone = page_zone(pfn_to_page(start)),
Linus Torvalds68e3e922012-06-03 20:05:57 -07005680 .sync = true,
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01005681 };
5682 INIT_LIST_HEAD(&cc.migratepages);
5683
5684 migrate_prep_local();
5685
5686 while (pfn < end || !list_empty(&cc.migratepages)) {
5687 if (fatal_signal_pending(current)) {
5688 ret = -EINTR;
5689 break;
5690 }
5691
5692 if (list_empty(&cc.migratepages)) {
5693 cc.nr_migratepages = 0;
5694 pfn = isolate_migratepages_range(cc.zone, &cc,
5695 pfn, end);
5696 if (!pfn) {
5697 ret = -EINTR;
5698 break;
5699 }
5700 tries = 0;
5701 } else if (++tries == 5) {
5702 ret = ret < 0 ? ret : -EBUSY;
5703 break;
5704 }
5705
Minchan Kim02c6de82012-10-08 16:31:55 -07005706 reclaim_clean_pages_from_list(cc.zone, &cc.migratepages);
5707
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01005708 ret = migrate_pages(&cc.migratepages,
5709 __alloc_contig_migrate_alloc,
Minchan Kim58f42fd2012-05-11 09:37:13 +02005710 0, false, MIGRATE_SYNC);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01005711 }
5712
5713 putback_lru_pages(&cc.migratepages);
5714 return ret > 0 ? 0 : ret;
5715}
5716
Marek Szyprowski49f223a2012-01-25 12:49:24 +01005717/*
5718 * Update zone's cma pages counter used for watermark level calculation.
5719 */
5720static inline void __update_cma_watermarks(struct zone *zone, int count)
5721{
5722 unsigned long flags;
5723 spin_lock_irqsave(&zone->lock, flags);
5724 zone->min_cma_pages += count;
5725 spin_unlock_irqrestore(&zone->lock, flags);
5726 setup_per_zone_wmarks();
5727}
5728
5729/*
5730 * Trigger memory pressure bump to reclaim some pages in order to be able to
5731 * allocate 'count' pages in single page units. Does similar work as
5732 *__alloc_pages_slowpath() function.
5733 */
5734static int __reclaim_pages(struct zone *zone, gfp_t gfp_mask, int count)
5735{
5736 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
5737 struct zonelist *zonelist = node_zonelist(0, gfp_mask);
5738 int did_some_progress = 0;
5739 int order = 1;
5740
5741 /*
5742 * Increase level of watermarks to force kswapd do his job
5743 * to stabilise at new watermark level.
5744 */
5745 __update_cma_watermarks(zone, count);
5746
5747 /* Obey watermarks as if the page was being allocated */
5748 while (!zone_watermark_ok(zone, 0, low_wmark_pages(zone), 0, 0)) {
5749 wake_all_kswapd(order, zonelist, high_zoneidx, zone_idx(zone));
5750
5751 did_some_progress = __perform_reclaim(gfp_mask, order, zonelist,
5752 NULL);
5753 if (!did_some_progress) {
5754 /* Exhausted what can be done so it's blamo time */
5755 out_of_memory(zonelist, gfp_mask, order, NULL, false);
5756 }
5757 }
5758
5759 /* Restore original watermark levels. */
5760 __update_cma_watermarks(zone, -count);
5761
5762 return count;
5763}
5764
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01005765/**
5766 * alloc_contig_range() -- tries to allocate given range of pages
5767 * @start: start PFN to allocate
5768 * @end: one-past-the-last PFN to allocate
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02005769 * @migratetype: migratetype of the underlaying pageblocks (either
5770 * #MIGRATE_MOVABLE or #MIGRATE_CMA). All pageblocks
5771 * in range must have the same migratetype and it must
5772 * be either of the two.
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01005773 *
5774 * The PFN range does not have to be pageblock or MAX_ORDER_NR_PAGES
5775 * aligned, however it's the caller's responsibility to guarantee that
5776 * we are the only thread that changes migrate type of pageblocks the
5777 * pages fall in.
5778 *
5779 * The PFN range must belong to a single zone.
5780 *
5781 * Returns zero on success or negative error code. On success all
5782 * pages which PFN is in [start, end) are allocated for the caller and
5783 * need to be freed with free_contig_range().
5784 */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02005785int alloc_contig_range(unsigned long start, unsigned long end,
5786 unsigned migratetype)
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01005787{
5788 struct zone *zone = page_zone(pfn_to_page(start));
5789 unsigned long outer_start, outer_end;
5790 int ret = 0, order;
5791
5792 /*
5793 * What we do here is we mark all pageblocks in range as
5794 * MIGRATE_ISOLATE. Because pageblock and max order pages may
5795 * have different sizes, and due to the way page allocator
5796 * work, we align the range to biggest of the two pages so
5797 * that page allocator won't try to merge buddies from
5798 * different pageblocks and change MIGRATE_ISOLATE to some
5799 * other migration type.
5800 *
5801 * Once the pageblocks are marked as MIGRATE_ISOLATE, we
5802 * migrate the pages from an unaligned range (ie. pages that
5803 * we are interested in). This will put all the pages in
5804 * range back to page allocator as MIGRATE_ISOLATE.
5805 *
5806 * When this is done, we take the pages in range from page
5807 * allocator removing them from the buddy system. This way
5808 * page allocator will never consider using them.
5809 *
5810 * This lets us mark the pageblocks back as
5811 * MIGRATE_CMA/MIGRATE_MOVABLE so that free pages in the
5812 * aligned range but not in the unaligned, original range are
5813 * put back to page allocator so that buddy can use them.
5814 */
5815
5816 ret = start_isolate_page_range(pfn_max_align_down(start),
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02005817 pfn_max_align_up(end), migratetype);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01005818 if (ret)
5819 goto done;
5820
5821 ret = __alloc_contig_migrate_range(start, end);
5822 if (ret)
5823 goto done;
5824
5825 /*
5826 * Pages from [start, end) are within a MAX_ORDER_NR_PAGES
5827 * aligned blocks that are marked as MIGRATE_ISOLATE. What's
5828 * more, all pages in [start, end) are free in page allocator.
5829 * What we are going to do is to allocate all pages from
5830 * [start, end) (that is remove them from page allocator).
5831 *
5832 * The only problem is that pages at the beginning and at the
5833 * end of interesting range may be not aligned with pages that
5834 * page allocator holds, ie. they can be part of higher order
5835 * pages. Because of this, we reserve the bigger range and
5836 * once this is done free the pages we are not interested in.
5837 *
5838 * We don't have to hold zone->lock here because the pages are
5839 * isolated thus they won't get removed from buddy.
5840 */
5841
5842 lru_add_drain_all();
5843 drain_all_pages();
5844
5845 order = 0;
5846 outer_start = start;
5847 while (!PageBuddy(pfn_to_page(outer_start))) {
5848 if (++order >= MAX_ORDER) {
5849 ret = -EBUSY;
5850 goto done;
5851 }
5852 outer_start &= ~0UL << order;
5853 }
5854
5855 /* Make sure the range is really isolated. */
5856 if (test_pages_isolated(outer_start, end)) {
5857 pr_warn("alloc_contig_range test_pages_isolated(%lx, %lx) failed\n",
5858 outer_start, end);
5859 ret = -EBUSY;
5860 goto done;
5861 }
5862
Marek Szyprowski49f223a2012-01-25 12:49:24 +01005863 /*
5864 * Reclaim enough pages to make sure that contiguous allocation
5865 * will not starve the system.
5866 */
5867 __reclaim_pages(zone, GFP_HIGHUSER_MOVABLE, end-start);
5868
5869 /* Grab isolated pages from freelists. */
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01005870 outer_end = isolate_freepages_range(outer_start, end);
5871 if (!outer_end) {
5872 ret = -EBUSY;
5873 goto done;
5874 }
5875
5876 /* Free head and tail (if any) */
5877 if (start != outer_start)
5878 free_contig_range(outer_start, start - outer_start);
5879 if (end != outer_end)
5880 free_contig_range(end, outer_end - end);
5881
5882done:
5883 undo_isolate_page_range(pfn_max_align_down(start),
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02005884 pfn_max_align_up(end), migratetype);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01005885 return ret;
5886}
5887
5888void free_contig_range(unsigned long pfn, unsigned nr_pages)
5889{
5890 for (; nr_pages--; ++pfn)
5891 __free_page(pfn_to_page(pfn));
5892}
5893#endif
5894
Jiang Liu4ed7e022012-07-31 16:43:35 -07005895#ifdef CONFIG_MEMORY_HOTPLUG
5896static int __meminit __zone_pcp_update(void *data)
5897{
5898 struct zone *zone = data;
5899 int cpu;
5900 unsigned long batch = zone_batchsize(zone), flags;
5901
5902 for_each_possible_cpu(cpu) {
5903 struct per_cpu_pageset *pset;
5904 struct per_cpu_pages *pcp;
5905
5906 pset = per_cpu_ptr(zone->pageset, cpu);
5907 pcp = &pset->pcp;
5908
5909 local_irq_save(flags);
5910 if (pcp->count > 0)
5911 free_pcppages_bulk(zone, pcp->count, pcp);
5912 setup_pageset(pset, batch);
5913 local_irq_restore(flags);
5914 }
5915 return 0;
5916}
5917
5918void __meminit zone_pcp_update(struct zone *zone)
5919{
5920 stop_machine(__zone_pcp_update, zone, NULL);
5921}
5922#endif
5923
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07005924#ifdef CONFIG_MEMORY_HOTREMOVE
Jiang Liu340175b2012-07-31 16:43:32 -07005925void zone_pcp_reset(struct zone *zone)
5926{
5927 unsigned long flags;
5928
5929 /* avoid races with drain_pages() */
5930 local_irq_save(flags);
5931 if (zone->pageset != &boot_pageset) {
5932 free_percpu(zone->pageset);
5933 zone->pageset = &boot_pageset;
5934 }
5935 local_irq_restore(flags);
5936}
5937
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07005938/*
5939 * All pages in the range must be isolated before calling this.
5940 */
5941void
5942__offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
5943{
5944 struct page *page;
5945 struct zone *zone;
5946 int order, i;
5947 unsigned long pfn;
5948 unsigned long flags;
5949 /* find the first valid pfn */
5950 for (pfn = start_pfn; pfn < end_pfn; pfn++)
5951 if (pfn_valid(pfn))
5952 break;
5953 if (pfn == end_pfn)
5954 return;
5955 zone = page_zone(pfn_to_page(pfn));
5956 spin_lock_irqsave(&zone->lock, flags);
5957 pfn = start_pfn;
5958 while (pfn < end_pfn) {
5959 if (!pfn_valid(pfn)) {
5960 pfn++;
5961 continue;
5962 }
5963 page = pfn_to_page(pfn);
5964 BUG_ON(page_count(page));
5965 BUG_ON(!PageBuddy(page));
5966 order = page_order(page);
5967#ifdef CONFIG_DEBUG_VM
5968 printk(KERN_INFO "remove from free list %lx %d %lx\n",
5969 pfn, 1 << order, end_pfn);
5970#endif
5971 list_del(&page->lru);
5972 rmv_page_order(page);
5973 zone->free_area[order].nr_free--;
5974 __mod_zone_page_state(zone, NR_FREE_PAGES,
5975 - (1UL << order));
5976 for (i = 0; i < (1 << order); i++)
5977 SetPageReserved((page+i));
5978 pfn += (1 << order);
5979 }
5980 spin_unlock_irqrestore(&zone->lock, flags);
5981}
5982#endif
Wu Fengguang8d22ba12009-12-16 12:19:58 +01005983
5984#ifdef CONFIG_MEMORY_FAILURE
5985bool is_free_buddy_page(struct page *page)
5986{
5987 struct zone *zone = page_zone(page);
5988 unsigned long pfn = page_to_pfn(page);
5989 unsigned long flags;
5990 int order;
5991
5992 spin_lock_irqsave(&zone->lock, flags);
5993 for (order = 0; order < MAX_ORDER; order++) {
5994 struct page *page_head = page - (pfn & ((1 << order) - 1));
5995
5996 if (PageBuddy(page_head) && page_order(page_head) >= order)
5997 break;
5998 }
5999 spin_unlock_irqrestore(&zone->lock, flags);
6000
6001 return order < MAX_ORDER;
6002}
6003#endif
Wu Fengguang718a3822010-03-10 15:20:43 -08006004
Andrew Morton51300ce2012-05-29 15:06:44 -07006005static const struct trace_print_flags pageflag_names[] = {
Wu Fengguang718a3822010-03-10 15:20:43 -08006006 {1UL << PG_locked, "locked" },
6007 {1UL << PG_error, "error" },
6008 {1UL << PG_referenced, "referenced" },
6009 {1UL << PG_uptodate, "uptodate" },
6010 {1UL << PG_dirty, "dirty" },
6011 {1UL << PG_lru, "lru" },
6012 {1UL << PG_active, "active" },
6013 {1UL << PG_slab, "slab" },
6014 {1UL << PG_owner_priv_1, "owner_priv_1" },
6015 {1UL << PG_arch_1, "arch_1" },
6016 {1UL << PG_reserved, "reserved" },
6017 {1UL << PG_private, "private" },
6018 {1UL << PG_private_2, "private_2" },
6019 {1UL << PG_writeback, "writeback" },
6020#ifdef CONFIG_PAGEFLAGS_EXTENDED
6021 {1UL << PG_head, "head" },
6022 {1UL << PG_tail, "tail" },
6023#else
6024 {1UL << PG_compound, "compound" },
6025#endif
6026 {1UL << PG_swapcache, "swapcache" },
6027 {1UL << PG_mappedtodisk, "mappedtodisk" },
6028 {1UL << PG_reclaim, "reclaim" },
Wu Fengguang718a3822010-03-10 15:20:43 -08006029 {1UL << PG_swapbacked, "swapbacked" },
6030 {1UL << PG_unevictable, "unevictable" },
6031#ifdef CONFIG_MMU
6032 {1UL << PG_mlocked, "mlocked" },
6033#endif
6034#ifdef CONFIG_ARCH_USES_PG_UNCACHED
6035 {1UL << PG_uncached, "uncached" },
6036#endif
6037#ifdef CONFIG_MEMORY_FAILURE
6038 {1UL << PG_hwpoison, "hwpoison" },
6039#endif
Gavin Shanbe9cd872012-05-29 15:06:44 -07006040#ifdef CONFIG_TRANSPARENT_HUGEPAGE
6041 {1UL << PG_compound_lock, "compound_lock" },
6042#endif
Wu Fengguang718a3822010-03-10 15:20:43 -08006043};
6044
6045static void dump_page_flags(unsigned long flags)
6046{
6047 const char *delim = "";
6048 unsigned long mask;
6049 int i;
6050
Andrew Morton51300ce2012-05-29 15:06:44 -07006051 BUILD_BUG_ON(ARRAY_SIZE(pageflag_names) != __NR_PAGEFLAGS);
Johannes Weineracc50c12012-05-29 15:06:44 -07006052
Wu Fengguang718a3822010-03-10 15:20:43 -08006053 printk(KERN_ALERT "page flags: %#lx(", flags);
6054
6055 /* remove zone id */
6056 flags &= (1UL << NR_PAGEFLAGS) - 1;
6057
Andrew Morton51300ce2012-05-29 15:06:44 -07006058 for (i = 0; i < ARRAY_SIZE(pageflag_names) && flags; i++) {
Wu Fengguang718a3822010-03-10 15:20:43 -08006059
6060 mask = pageflag_names[i].mask;
6061 if ((flags & mask) != mask)
6062 continue;
6063
6064 flags &= ~mask;
6065 printk("%s%s", delim, pageflag_names[i].name);
6066 delim = "|";
6067 }
6068
6069 /* check for left over flags */
6070 if (flags)
6071 printk("%s%#lx", delim, flags);
6072
6073 printk(")\n");
6074}
6075
6076void dump_page(struct page *page)
6077{
6078 printk(KERN_ALERT
6079 "page:%p count:%d mapcount:%d mapping:%p index:%#lx\n",
Andrea Arcangeli4e9f64c2011-01-13 15:46:29 -08006080 page, atomic_read(&page->_count), page_mapcount(page),
Wu Fengguang718a3822010-03-10 15:20:43 -08006081 page->mapping, page->index);
6082 dump_page_flags(page->flags);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07006083 mem_cgroup_print_bad_page(page);
Wu Fengguang718a3822010-03-10 15:20:43 -08006084}