blob: 88b9962c99b3fa282211c1048ab5f588e9ff12c4 [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>
Clark Williams8bd75c72013-02-07 09:47:07 -060061#include <linux/sched/rt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63#include <asm/tlbflush.h>
Andrew Mortonac924c62006-05-15 09:43:59 -070064#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include "internal.h"
66
Lee Schermerhorn72812012010-05-26 14:44:56 -070067#ifdef CONFIG_USE_PERCPU_NUMA_NODE_ID
68DEFINE_PER_CPU(int, numa_node);
69EXPORT_PER_CPU_SYMBOL(numa_node);
70#endif
71
Lee Schermerhorn7aac7892010-05-26 14:45:00 -070072#ifdef CONFIG_HAVE_MEMORYLESS_NODES
73/*
74 * N.B., Do NOT reference the '_numa_mem_' per cpu variable directly.
75 * It will not be defined when CONFIG_HAVE_MEMORYLESS_NODES is not defined.
76 * Use the accessor functions set_numa_mem(), numa_mem_id() and cpu_to_mem()
77 * defined in <linux/topology.h>.
78 */
79DEFINE_PER_CPU(int, _numa_mem_); /* Kernel "local memory" node */
80EXPORT_PER_CPU_SYMBOL(_numa_mem_);
81#endif
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/*
Christoph Lameter13808912007-10-16 01:25:27 -070084 * Array of node states.
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 */
Christoph Lameter13808912007-10-16 01:25:27 -070086nodemask_t node_states[NR_NODE_STATES] __read_mostly = {
87 [N_POSSIBLE] = NODE_MASK_ALL,
88 [N_ONLINE] = { { [0] = 1UL } },
89#ifndef CONFIG_NUMA
90 [N_NORMAL_MEMORY] = { { [0] = 1UL } },
91#ifdef CONFIG_HIGHMEM
92 [N_HIGH_MEMORY] = { { [0] = 1UL } },
93#endif
Lai Jiangshan20b2f522012-12-12 13:52:00 -080094#ifdef CONFIG_MOVABLE_NODE
95 [N_MEMORY] = { { [0] = 1UL } },
96#endif
Christoph Lameter13808912007-10-16 01:25:27 -070097 [N_CPU] = { { [0] = 1UL } },
98#endif /* NUMA */
99};
100EXPORT_SYMBOL(node_states);
101
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -0700102unsigned long totalram_pages __read_mostly;
Hideo AOKIcb45b0e2006-04-10 22:52:59 -0700103unsigned long totalreserve_pages __read_mostly;
Johannes Weinerab8fabd2012-01-10 15:07:42 -0800104/*
105 * When calculating the number of globally allowed dirty pages, there
106 * is a certain number of per-zone reserves that should not be
107 * considered dirtyable memory. This is the sum of those reserves
108 * over all existing zones that contribute dirtyable memory.
109 */
110unsigned long dirty_balance_reserve __read_mostly;
111
Hugh Dickins1b76b022012-05-11 01:00:07 -0700112int percpu_pagelist_fraction;
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +1000113gfp_t gfp_allowed_mask __read_mostly = GFP_BOOT_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800115#ifdef CONFIG_PM_SLEEP
116/*
117 * The following functions are used by the suspend/hibernate code to temporarily
118 * change gfp_allowed_mask in order to avoid using I/O during memory allocations
119 * while devices are suspended. To avoid races with the suspend/hibernate code,
120 * they should always be called with pm_mutex held (gfp_allowed_mask also should
121 * only be modified with pm_mutex held, unless the suspend/hibernate code is
122 * guaranteed not to run in parallel with that modification).
123 */
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100124
125static gfp_t saved_gfp_mask;
126
127void pm_restore_gfp_mask(void)
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800128{
129 WARN_ON(!mutex_is_locked(&pm_mutex));
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100130 if (saved_gfp_mask) {
131 gfp_allowed_mask = saved_gfp_mask;
132 saved_gfp_mask = 0;
133 }
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800134}
135
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100136void pm_restrict_gfp_mask(void)
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800137{
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800138 WARN_ON(!mutex_is_locked(&pm_mutex));
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100139 WARN_ON(saved_gfp_mask);
140 saved_gfp_mask = gfp_allowed_mask;
141 gfp_allowed_mask &= ~GFP_IOFS;
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800142}
Mel Gormanf90ac392012-01-10 15:07:15 -0800143
144bool pm_suspended_storage(void)
145{
146 if ((gfp_allowed_mask & GFP_IOFS) == GFP_IOFS)
147 return false;
148 return true;
149}
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800150#endif /* CONFIG_PM_SLEEP */
151
Mel Gormand9c23402007-10-16 01:26:01 -0700152#ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
153int pageblock_order __read_mostly;
154#endif
155
Hugh Dickinsd98c7a02006-02-14 13:52:59 -0800156static void __free_pages_ok(struct page *page, unsigned int order);
David Howellsa226f6c2006-01-06 00:11:08 -0800157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/*
159 * results with 256, 32 in the lowmem_reserve sysctl:
160 * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
161 * 1G machine -> (16M dma, 784M normal, 224M high)
162 * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
163 * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
164 * HIGHMEM allocation will (224M+784M)/256 of ram reserved in ZONE_DMA
Andi Kleena2f1b422005-11-05 17:25:53 +0100165 *
166 * TBD: should special case ZONE_DMA32 machines here - in those we normally
167 * don't need any ZONE_NORMAL reservation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 */
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700169int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1] = {
Christoph Lameter4b51d662007-02-10 01:43:10 -0800170#ifdef CONFIG_ZONE_DMA
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700171 256,
Christoph Lameter4b51d662007-02-10 01:43:10 -0800172#endif
Christoph Lameterfb0e7942006-09-25 23:31:13 -0700173#ifdef CONFIG_ZONE_DMA32
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700174 256,
Christoph Lameterfb0e7942006-09-25 23:31:13 -0700175#endif
Christoph Lametere53ef382006-09-25 23:31:14 -0700176#ifdef CONFIG_HIGHMEM
Mel Gorman2a1e2742007-07-17 04:03:12 -0700177 32,
Christoph Lametere53ef382006-09-25 23:31:14 -0700178#endif
Mel Gorman2a1e2742007-07-17 04:03:12 -0700179 32,
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700180};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182EXPORT_SYMBOL(totalram_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Helge Deller15ad7cd2006-12-06 20:40:36 -0800184static char * const zone_names[MAX_NR_ZONES] = {
Christoph Lameter4b51d662007-02-10 01:43:10 -0800185#ifdef CONFIG_ZONE_DMA
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700186 "DMA",
Christoph Lameter4b51d662007-02-10 01:43:10 -0800187#endif
Christoph Lameterfb0e7942006-09-25 23:31:13 -0700188#ifdef CONFIG_ZONE_DMA32
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700189 "DMA32",
Christoph Lameterfb0e7942006-09-25 23:31:13 -0700190#endif
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700191 "Normal",
Christoph Lametere53ef382006-09-25 23:31:14 -0700192#ifdef CONFIG_HIGHMEM
Mel Gorman2a1e2742007-07-17 04:03:12 -0700193 "HighMem",
Christoph Lametere53ef382006-09-25 23:31:14 -0700194#endif
Mel Gorman2a1e2742007-07-17 04:03:12 -0700195 "Movable",
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700196};
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198int min_free_kbytes = 1024;
199
Jan Beulich2c85f512009-09-21 17:03:07 -0700200static unsigned long __meminitdata nr_kernel_pages;
201static unsigned long __meminitdata nr_all_pages;
Yasunori Gotoa3142c82007-05-08 00:23:07 -0700202static unsigned long __meminitdata dma_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Tejun Heo0ee332c2011-12-08 10:22:09 -0800204#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Tang Chen34b71f12013-02-22 16:33:37 -0800205/* Movable memory ranges, will also be used by memblock subsystem. */
206struct movablemem_map movablemem_map;
207
Tejun Heo0ee332c2011-12-08 10:22:09 -0800208static unsigned long __meminitdata arch_zone_lowest_possible_pfn[MAX_NR_ZONES];
209static unsigned long __meminitdata arch_zone_highest_possible_pfn[MAX_NR_ZONES];
210static unsigned long __initdata required_kernelcore;
211static unsigned long __initdata required_movablecore;
212static unsigned long __meminitdata zone_movable_pfn[MAX_NUMNODES];
Tang Chen6981ec32013-02-22 16:33:39 -0800213static unsigned long __meminitdata zone_movable_limit[MAX_NUMNODES];
Mel Gormanc7132162006-09-27 01:49:43 -0700214
Tejun Heo0ee332c2011-12-08 10:22:09 -0800215/* movable_zone is the "real" zone pages in ZONE_MOVABLE are taken from */
216int movable_zone;
217EXPORT_SYMBOL(movable_zone);
218#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
Mel Gormanc7132162006-09-27 01:49:43 -0700219
Miklos Szeredi418508c2007-05-23 13:57:55 -0700220#if MAX_NUMNODES > 1
221int nr_node_ids __read_mostly = MAX_NUMNODES;
Christoph Lameter62bc62a2009-06-16 15:32:15 -0700222int nr_online_nodes __read_mostly = 1;
Miklos Szeredi418508c2007-05-23 13:57:55 -0700223EXPORT_SYMBOL(nr_node_ids);
Christoph Lameter62bc62a2009-06-16 15:32:15 -0700224EXPORT_SYMBOL(nr_online_nodes);
Miklos Szeredi418508c2007-05-23 13:57:55 -0700225#endif
226
Mel Gorman9ef9acb2007-10-16 01:25:54 -0700227int page_group_by_mobility_disabled __read_mostly;
228
Minchan Kimee6f5092012-07-31 16:43:50 -0700229void set_pageblock_migratetype(struct page *page, int migratetype)
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700230{
Mel Gorman49255c62009-06-16 15:31:58 -0700231
232 if (unlikely(page_group_by_mobility_disabled))
233 migratetype = MIGRATE_UNMOVABLE;
234
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700235 set_pageblock_flags_group(page, (unsigned long)migratetype,
236 PB_migrate, PB_migrate_end);
237}
238
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700239bool oom_killer_disabled __read_mostly;
240
Nick Piggin13e74442006-01-06 00:10:58 -0800241#ifdef CONFIG_DEBUG_VM
Dave Hansenc6a57e12005-10-29 18:16:52 -0700242static int page_outside_zone_boundaries(struct zone *zone, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Dave Hansenbdc8cb92005-10-29 18:16:53 -0700244 int ret = 0;
245 unsigned seq;
246 unsigned long pfn = page_to_pfn(page);
Dave Hansenc6a57e12005-10-29 18:16:52 -0700247
Dave Hansenbdc8cb92005-10-29 18:16:53 -0700248 do {
249 seq = zone_span_seqbegin(zone);
250 if (pfn >= zone->zone_start_pfn + zone->spanned_pages)
251 ret = 1;
252 else if (pfn < zone->zone_start_pfn)
253 ret = 1;
254 } while (zone_span_seqretry(zone, seq));
255
256 return ret;
Dave Hansenc6a57e12005-10-29 18:16:52 -0700257}
258
259static int page_is_consistent(struct zone *zone, struct page *page)
260{
Andy Whitcroft14e07292007-05-06 14:49:14 -0700261 if (!pfn_valid_within(page_to_pfn(page)))
Dave Hansenc6a57e12005-10-29 18:16:52 -0700262 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (zone != page_zone(page))
Dave Hansenc6a57e12005-10-29 18:16:52 -0700264 return 0;
265
266 return 1;
267}
268/*
269 * Temporary debugging check for pages not lying within a given zone.
270 */
271static int bad_range(struct zone *zone, struct page *page)
272{
273 if (page_outside_zone_boundaries(zone, page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return 1;
Dave Hansenc6a57e12005-10-29 18:16:52 -0700275 if (!page_is_consistent(zone, page))
276 return 1;
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return 0;
279}
Nick Piggin13e74442006-01-06 00:10:58 -0800280#else
281static inline int bad_range(struct zone *zone, struct page *page)
282{
283 return 0;
284}
285#endif
286
Nick Piggin224abf92006-01-06 00:11:11 -0800287static void bad_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800289 static unsigned long resume;
290 static unsigned long nr_shown;
291 static unsigned long nr_unshown;
292
Wu Fengguang2a7684a2009-09-16 11:50:12 +0200293 /* Don't complain about poisoned pages */
294 if (PageHWPoison(page)) {
Andrea Arcangelief2b4b92011-03-18 00:16:35 +0100295 reset_page_mapcount(page); /* remove PageBuddy */
Wu Fengguang2a7684a2009-09-16 11:50:12 +0200296 return;
297 }
298
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800299 /*
300 * Allow a burst of 60 reports, then keep quiet for that minute;
301 * or allow a steady drip of one report per second.
302 */
303 if (nr_shown == 60) {
304 if (time_before(jiffies, resume)) {
305 nr_unshown++;
306 goto out;
307 }
308 if (nr_unshown) {
Hugh Dickins1e9e6362009-01-06 14:40:13 -0800309 printk(KERN_ALERT
310 "BUG: Bad page state: %lu messages suppressed\n",
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800311 nr_unshown);
312 nr_unshown = 0;
313 }
314 nr_shown = 0;
315 }
316 if (nr_shown++ == 0)
317 resume = jiffies + 60 * HZ;
318
Hugh Dickins1e9e6362009-01-06 14:40:13 -0800319 printk(KERN_ALERT "BUG: Bad page state in process %s pfn:%05lx\n",
Hugh Dickins3dc14742009-01-06 14:40:08 -0800320 current->comm, page_to_pfn(page));
Wu Fengguang718a3822010-03-10 15:20:43 -0800321 dump_page(page);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700322
Dave Jones4f318882011-10-31 17:07:24 -0700323 print_modules();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 dump_stack();
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800325out:
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800326 /* Leave bad fields for debug, except PageBuddy could make trouble */
Andrea Arcangelief2b4b92011-03-18 00:16:35 +0100327 reset_page_mapcount(page); /* remove PageBuddy */
Randy Dunlap9f158332005-09-13 01:25:16 -0700328 add_taint(TAINT_BAD_PAGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331/*
332 * Higher-order pages are called "compound pages". They are structured thusly:
333 *
334 * The first PAGE_SIZE page is called the "head page".
335 *
336 * The remaining PAGE_SIZE pages are called "tail pages".
337 *
Wang Sheng-Hui6416b9f2011-11-17 10:53:50 +0100338 * All pages have PG_compound set. All tail pages have their ->first_page
339 * pointing at the head page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 *
Hugh Dickins41d78ba2006-02-14 13:52:58 -0800341 * The first tail page's ->lru.next holds the address of the compound page's
342 * put_page() function. Its ->lru.prev holds the order of allocation.
343 * This usage means that zero-order pages may not be compound.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 */
Hugh Dickinsd98c7a02006-02-14 13:52:59 -0800345
346static void free_compound_page(struct page *page)
347{
Christoph Lameterd85f3382007-05-06 14:49:39 -0700348 __free_pages_ok(page, compound_order(page));
Hugh Dickinsd98c7a02006-02-14 13:52:59 -0800349}
350
Andi Kleen01ad1c02008-07-23 21:27:46 -0700351void prep_compound_page(struct page *page, unsigned long order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 int i;
354 int nr_pages = 1 << order;
355
Andy Whitcroft33f2ef82006-12-06 20:33:32 -0800356 set_compound_page_dtor(page, free_compound_page);
Christoph Lameterd85f3382007-05-06 14:49:39 -0700357 set_compound_order(page, order);
Christoph Lameter6d777952007-05-06 14:49:40 -0700358 __SetPageHead(page);
Andy Whitcroft18229df2008-11-06 12:53:27 -0800359 for (i = 1; i < nr_pages; i++) {
360 struct page *p = page + i;
Christoph Lameterd85f3382007-05-06 14:49:39 -0700361 __SetPageTail(p);
Youquan Song58a84aa2011-12-08 14:34:18 -0800362 set_page_count(p, 0);
Christoph Lameterd85f3382007-05-06 14:49:39 -0700363 p->first_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365}
366
Andrea Arcangeli59ff4212011-01-13 15:46:38 -0800367/* update __split_huge_page_refcount if you change this function */
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800368static int destroy_compound_page(struct page *page, unsigned long order)
Andy Whitcroft18229df2008-11-06 12:53:27 -0800369{
370 int i;
371 int nr_pages = 1 << order;
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800372 int bad = 0;
Andy Whitcroft18229df2008-11-06 12:53:27 -0800373
Gavin Shan0bb2c762012-12-18 14:21:32 -0800374 if (unlikely(compound_order(page) != order)) {
Nick Piggin224abf92006-01-06 00:11:11 -0800375 bad_page(page);
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800376 bad++;
377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Christoph Lameter6d777952007-05-06 14:49:40 -0700379 __ClearPageHead(page);
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800380
Andy Whitcroft18229df2008-11-06 12:53:27 -0800381 for (i = 1; i < nr_pages; i++) {
382 struct page *p = page + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Alexey Zaytseve713a212009-01-10 02:47:57 +0300384 if (unlikely(!PageTail(p) || (p->first_page != page))) {
Nick Piggin224abf92006-01-06 00:11:11 -0800385 bad_page(page);
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800386 bad++;
387 }
Christoph Lameterd85f3382007-05-06 14:49:39 -0700388 __ClearPageTail(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800390
391 return bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Nick Piggin17cf4402006-03-22 00:08:41 -0800394static inline void prep_zero_page(struct page *page, int order, gfp_t gfp_flags)
395{
396 int i;
397
Andrew Morton6626c5d2006-03-22 00:08:42 -0800398 /*
399 * clear_highpage() will use KM_USER0, so it's a bug to use __GFP_ZERO
400 * and __GFP_HIGHMEM from hard or soft interrupt context.
401 */
Nick Piggin725d7042006-09-25 23:30:55 -0700402 VM_BUG_ON((gfp_flags & __GFP_HIGHMEM) && in_interrupt());
Nick Piggin17cf4402006-03-22 00:08:41 -0800403 for (i = 0; i < (1 << order); i++)
404 clear_highpage(page + i);
405}
406
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -0800407#ifdef CONFIG_DEBUG_PAGEALLOC
408unsigned int _debug_guardpage_minorder;
409
410static int __init debug_guardpage_minorder_setup(char *buf)
411{
412 unsigned long res;
413
414 if (kstrtoul(buf, 10, &res) < 0 || res > MAX_ORDER / 2) {
415 printk(KERN_ERR "Bad debug_guardpage_minorder value\n");
416 return 0;
417 }
418 _debug_guardpage_minorder = res;
419 printk(KERN_INFO "Setting debug_guardpage_minorder to %lu\n", res);
420 return 0;
421}
422__setup("debug_guardpage_minorder=", debug_guardpage_minorder_setup);
423
424static inline void set_page_guard_flag(struct page *page)
425{
426 __set_bit(PAGE_DEBUG_FLAG_GUARD, &page->debug_flags);
427}
428
429static inline void clear_page_guard_flag(struct page *page)
430{
431 __clear_bit(PAGE_DEBUG_FLAG_GUARD, &page->debug_flags);
432}
433#else
434static inline void set_page_guard_flag(struct page *page) { }
435static inline void clear_page_guard_flag(struct page *page) { }
436#endif
437
Andrew Morton6aa3001b22006-04-18 22:20:52 -0700438static inline void set_page_order(struct page *page, int order)
439{
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700440 set_page_private(page, order);
Nick Piggin676165a2006-04-10 11:21:48 +1000441 __SetPageBuddy(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
444static inline void rmv_page_order(struct page *page)
445{
Nick Piggin676165a2006-04-10 11:21:48 +1000446 __ClearPageBuddy(page);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700447 set_page_private(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
450/*
451 * Locate the struct page for both the matching buddy in our
452 * pair (buddy1) and the combined O(n+1) page they form (page).
453 *
454 * 1) Any buddy B1 will have an order O twin B2 which satisfies
455 * the following equation:
456 * B2 = B1 ^ (1 << O)
457 * For example, if the starting buddy (buddy2) is #8 its order
458 * 1 buddy is #10:
459 * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
460 *
461 * 2) Any buddy B will have an order O+1 parent P which
462 * satisfies the following equation:
463 * P = B & ~(1 << O)
464 *
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200465 * Assumption: *_mem_map is contiguous at least up to MAX_ORDER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467static inline unsigned long
KyongHo Cho43506fa2011-01-13 15:47:24 -0800468__find_buddy_index(unsigned long page_idx, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
KyongHo Cho43506fa2011-01-13 15:47:24 -0800470 return page_idx ^ (1 << order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
473/*
474 * This function checks whether a page is free && is the buddy
475 * we can do coalesce a page and its buddy if
Nick Piggin13e74442006-01-06 00:10:58 -0800476 * (a) the buddy is not in a hole &&
Nick Piggin676165a2006-04-10 11:21:48 +1000477 * (b) the buddy is in the buddy system &&
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700478 * (c) a page and its buddy have the same order &&
479 * (d) a page and its buddy are in the same zone.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 *
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800481 * For recording whether a page is in the buddy system, we set ->_mapcount -2.
482 * Setting, clearing, and testing _mapcount -2 is serialized by zone->lock.
Nick Piggin676165a2006-04-10 11:21:48 +1000483 *
484 * For recording page's order, we use page_private(page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 */
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700486static inline int page_is_buddy(struct page *page, struct page *buddy,
487 int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Andy Whitcroft14e07292007-05-06 14:49:14 -0700489 if (!pfn_valid_within(page_to_pfn(buddy)))
Nick Piggin13e74442006-01-06 00:10:58 -0800490 return 0;
Nick Piggin13e74442006-01-06 00:10:58 -0800491
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700492 if (page_zone_id(page) != page_zone_id(buddy))
493 return 0;
494
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -0800495 if (page_is_guard(buddy) && page_order(buddy) == order) {
496 VM_BUG_ON(page_count(buddy) != 0);
497 return 1;
498 }
499
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700500 if (PageBuddy(buddy) && page_order(buddy) == order) {
Nick Piggina3af9c32009-06-16 15:32:10 -0700501 VM_BUG_ON(page_count(buddy) != 0);
Andrew Morton6aa3001b22006-04-18 22:20:52 -0700502 return 1;
Nick Piggin676165a2006-04-10 11:21:48 +1000503 }
Andrew Morton6aa3001b22006-04-18 22:20:52 -0700504 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
507/*
508 * Freeing function for a buddy system allocator.
509 *
510 * The concept of a buddy system is to maintain direct-mapped table
511 * (containing bit values) for memory blocks of various "orders".
512 * The bottom level table contains the map for the smallest allocatable
513 * units of memory (here, pages), and each level above it describes
514 * pairs of units from the levels below, hence, "buddies".
515 * At a high level, all that happens here is marking the table entry
516 * at the bottom level available, and propagating the changes upward
517 * as necessary, plus some accounting needed to play nicely with other
518 * parts of the VM system.
519 * At each level, we keep a list of pages, which are heads of continuous
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800520 * free pages of length of (1 << order) and marked with _mapcount -2. Page's
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700521 * order is recorded in page_private(page) field.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 * So when we are allocating or freeing one, we can derive the state of the
Michal Nazarewicz5f63b722012-01-11 15:16:11 +0100523 * other. That is, if we allocate a small block, and both were
524 * free, the remainder of the region must be split into blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 * If a block is freed, and its buddy is also free, then this
Michal Nazarewicz5f63b722012-01-11 15:16:11 +0100526 * triggers coalescing into a block of larger size.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 *
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +0100528 * -- nyc
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 */
530
Nick Piggin48db57f2006-01-08 01:00:42 -0800531static inline void __free_one_page(struct page *page,
Mel Gormaned0ae212009-06-16 15:32:07 -0700532 struct zone *zone, unsigned int order,
533 int migratetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 unsigned long page_idx;
Corrado Zoccolo6dda9d52010-05-24 14:31:54 -0700536 unsigned long combined_idx;
KyongHo Cho43506fa2011-01-13 15:47:24 -0800537 unsigned long uninitialized_var(buddy_idx);
Corrado Zoccolo6dda9d52010-05-24 14:31:54 -0700538 struct page *buddy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Nick Piggin224abf92006-01-06 00:11:11 -0800540 if (unlikely(PageCompound(page)))
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800541 if (unlikely(destroy_compound_page(page, order)))
542 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Mel Gormaned0ae212009-06-16 15:32:07 -0700544 VM_BUG_ON(migratetype == -1);
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 page_idx = page_to_pfn(page) & ((1 << MAX_ORDER) - 1);
547
Mel Gormanf2260e62009-06-16 15:32:13 -0700548 VM_BUG_ON(page_idx & ((1 << order) - 1));
Nick Piggin725d7042006-09-25 23:30:55 -0700549 VM_BUG_ON(bad_range(zone, page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 while (order < MAX_ORDER-1) {
KyongHo Cho43506fa2011-01-13 15:47:24 -0800552 buddy_idx = __find_buddy_index(page_idx, order);
553 buddy = page + (buddy_idx - page_idx);
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700554 if (!page_is_buddy(page, buddy, order))
Andy Whitcroft3c82d0c2008-07-23 21:27:11 -0700555 break;
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -0800556 /*
557 * Our buddy is free or it is CONFIG_DEBUG_PAGEALLOC guard page,
558 * merge with it and move up one order.
559 */
560 if (page_is_guard(buddy)) {
561 clear_page_guard_flag(buddy);
562 set_page_private(page, 0);
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -0700563 __mod_zone_freepage_state(zone, 1 << order,
564 migratetype);
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -0800565 } else {
566 list_del(&buddy->lru);
567 zone->free_area[order].nr_free--;
568 rmv_page_order(buddy);
569 }
KyongHo Cho43506fa2011-01-13 15:47:24 -0800570 combined_idx = buddy_idx & page_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 page = page + (combined_idx - page_idx);
572 page_idx = combined_idx;
573 order++;
574 }
575 set_page_order(page, order);
Corrado Zoccolo6dda9d52010-05-24 14:31:54 -0700576
577 /*
578 * If this is not the largest possible page, check if the buddy
579 * of the next-highest order is free. If it is, it's possible
580 * that pages are being freed that will coalesce soon. In case,
581 * that is happening, add the free page to the tail of the list
582 * so it's less likely to be used soon and more likely to be merged
583 * as a higher order page
584 */
Mel Gormanb7f50cf2010-10-26 14:21:11 -0700585 if ((order < MAX_ORDER-2) && pfn_valid_within(page_to_pfn(buddy))) {
Corrado Zoccolo6dda9d52010-05-24 14:31:54 -0700586 struct page *higher_page, *higher_buddy;
KyongHo Cho43506fa2011-01-13 15:47:24 -0800587 combined_idx = buddy_idx & page_idx;
588 higher_page = page + (combined_idx - page_idx);
589 buddy_idx = __find_buddy_index(combined_idx, order + 1);
Li Haifeng0ba8f2d2012-09-17 14:09:21 -0700590 higher_buddy = higher_page + (buddy_idx - combined_idx);
Corrado Zoccolo6dda9d52010-05-24 14:31:54 -0700591 if (page_is_buddy(higher_page, higher_buddy, order + 1)) {
592 list_add_tail(&page->lru,
593 &zone->free_area[order].free_list[migratetype]);
594 goto out;
595 }
596 }
597
598 list_add(&page->lru, &zone->free_area[order].free_list[migratetype]);
599out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 zone->free_area[order].nr_free++;
601}
602
Nick Piggin224abf92006-01-06 00:11:11 -0800603static inline int free_pages_check(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Nick Piggin92be2e332006-01-06 00:10:57 -0800605 if (unlikely(page_mapcount(page) |
606 (page->mapping != NULL) |
Nick Piggina3af9c32009-06-16 15:32:10 -0700607 (atomic_read(&page->_count) != 0) |
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -0700608 (page->flags & PAGE_FLAGS_CHECK_AT_FREE) |
609 (mem_cgroup_bad_page_check(page)))) {
Nick Piggin224abf92006-01-06 00:11:11 -0800610 bad_page(page);
Hugh Dickins79f4b7b2009-01-06 14:40:05 -0800611 return 1;
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800612 }
Mel Gorman57e0a032012-11-12 09:06:20 +0000613 reset_page_last_nid(page);
Hugh Dickins79f4b7b2009-01-06 14:40:05 -0800614 if (page->flags & PAGE_FLAGS_CHECK_AT_PREP)
615 page->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
616 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
619/*
Mel Gorman5f8dcc22009-09-21 17:03:19 -0700620 * Frees a number of pages from the PCP lists
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 * Assumes all pages on list are in same zone, and of same order.
Renaud Lienhart207f36e2005-09-10 00:26:59 -0700622 * count is the number of pages to free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 *
624 * If the zone was previously in an "all pages pinned" state then look to
625 * see if this freeing clears that state.
626 *
627 * And clear the zone's pages_scanned counter, to hold off the "all pages are
628 * pinned" detection logic.
629 */
Mel Gorman5f8dcc22009-09-21 17:03:19 -0700630static void free_pcppages_bulk(struct zone *zone, int count,
631 struct per_cpu_pages *pcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Mel Gorman5f8dcc22009-09-21 17:03:19 -0700633 int migratetype = 0;
Mel Gormana6f9edd2009-09-21 17:03:20 -0700634 int batch_free = 0;
Mel Gorman72853e22010-09-09 16:38:16 -0700635 int to_free = count;
Mel Gorman5f8dcc22009-09-21 17:03:19 -0700636
Nick Pigginc54ad302006-01-06 00:10:56 -0800637 spin_lock(&zone->lock);
KOSAKI Motohiro93e4a892010-03-05 13:41:55 -0800638 zone->all_unreclaimable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 zone->pages_scanned = 0;
Mel Gormanf2260e62009-06-16 15:32:13 -0700640
Mel Gorman72853e22010-09-09 16:38:16 -0700641 while (to_free) {
Nick Piggin48db57f2006-01-08 01:00:42 -0800642 struct page *page;
Mel Gorman5f8dcc22009-09-21 17:03:19 -0700643 struct list_head *list;
Nick Piggin48db57f2006-01-08 01:00:42 -0800644
Mel Gorman5f8dcc22009-09-21 17:03:19 -0700645 /*
Mel Gormana6f9edd2009-09-21 17:03:20 -0700646 * Remove pages from lists in a round-robin fashion. A
647 * batch_free count is maintained that is incremented when an
648 * empty list is encountered. This is so more pages are freed
649 * off fuller lists instead of spinning excessively around empty
650 * lists
Mel Gorman5f8dcc22009-09-21 17:03:19 -0700651 */
652 do {
Mel Gormana6f9edd2009-09-21 17:03:20 -0700653 batch_free++;
Mel Gorman5f8dcc22009-09-21 17:03:19 -0700654 if (++migratetype == MIGRATE_PCPTYPES)
655 migratetype = 0;
656 list = &pcp->lists[migratetype];
657 } while (list_empty(list));
658
Namhyung Kim1d168712011-03-22 16:32:45 -0700659 /* This is the only non-empty list. Free them all. */
660 if (batch_free == MIGRATE_PCPTYPES)
661 batch_free = to_free;
662
Mel Gormana6f9edd2009-09-21 17:03:20 -0700663 do {
Bartlomiej Zolnierkiewicz770c8aa2012-10-08 16:31:57 -0700664 int mt; /* migratetype of the to-be-freed page */
665
Mel Gormana6f9edd2009-09-21 17:03:20 -0700666 page = list_entry(list->prev, struct page, lru);
667 /* must delete as __free_one_page list manipulates */
668 list_del(&page->lru);
Minchan Kimb12c4ad2012-10-08 16:32:08 -0700669 mt = get_freepage_migratetype(page);
Hugh Dickinsa7016232010-01-29 17:46:34 +0000670 /* MIGRATE_MOVABLE list may include MIGRATE_RESERVEs */
Bartlomiej Zolnierkiewicz770c8aa2012-10-08 16:31:57 -0700671 __free_one_page(page, zone, 0, mt);
672 trace_mm_page_pcpu_drain(page, 0, mt);
Wen Congyang97d0da22012-12-11 16:00:52 -0800673 if (likely(get_pageblock_migratetype(page) != MIGRATE_ISOLATE)) {
674 __mod_zone_page_state(zone, NR_FREE_PAGES, 1);
675 if (is_migrate_cma(mt))
676 __mod_zone_page_state(zone, NR_FREE_CMA_PAGES, 1);
677 }
Mel Gorman72853e22010-09-09 16:38:16 -0700678 } while (--to_free && --batch_free && !list_empty(list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
Nick Pigginc54ad302006-01-06 00:10:56 -0800680 spin_unlock(&zone->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
Mel Gormaned0ae212009-06-16 15:32:07 -0700683static void free_one_page(struct zone *zone, struct page *page, int order,
684 int migratetype)
Nick Piggin48db57f2006-01-08 01:00:42 -0800685{
Christoph Lameter006d22d2006-09-25 23:31:48 -0700686 spin_lock(&zone->lock);
KOSAKI Motohiro93e4a892010-03-05 13:41:55 -0800687 zone->all_unreclaimable = 0;
Christoph Lameter006d22d2006-09-25 23:31:48 -0700688 zone->pages_scanned = 0;
Mel Gormanf2260e62009-06-16 15:32:13 -0700689
Mel Gormaned0ae212009-06-16 15:32:07 -0700690 __free_one_page(page, zone, order, migratetype);
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -0700691 if (unlikely(migratetype != MIGRATE_ISOLATE))
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -0700692 __mod_zone_freepage_state(zone, 1 << order, migratetype);
Christoph Lameter006d22d2006-09-25 23:31:48 -0700693 spin_unlock(&zone->lock);
Nick Piggin48db57f2006-01-08 01:00:42 -0800694}
695
KOSAKI Motohiroec95f532010-05-24 14:32:38 -0700696static bool free_pages_prepare(struct page *page, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 int i;
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800699 int bad = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Konstantin Khlebnikovb413d482012-01-10 15:07:09 -0800701 trace_mm_page_free(page, order);
Vegard Nossumb1eeab62008-11-25 16:55:53 +0100702 kmemcheck_free_shadow(page, order);
703
Andrea Arcangeli8dd60a32011-01-13 15:46:34 -0800704 if (PageAnon(page))
705 page->mapping = NULL;
706 for (i = 0; i < (1 << order); i++)
707 bad += free_pages_check(page + i);
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800708 if (bad)
KOSAKI Motohiroec95f532010-05-24 14:32:38 -0700709 return false;
Hugh Dickins689bceb2005-11-21 21:32:20 -0800710
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700711 if (!PageHighMem(page)) {
Nick Piggin9858db52006-10-11 01:21:30 -0700712 debug_check_no_locks_freed(page_address(page),PAGE_SIZE<<order);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700713 debug_check_no_obj_freed(page_address(page),
714 PAGE_SIZE << order);
715 }
Nick Piggindafb1362006-10-11 01:21:30 -0700716 arch_free_page(page, order);
Nick Piggin48db57f2006-01-08 01:00:42 -0800717 kernel_map_pages(page, 1 << order, 0);
Nick Piggindafb1362006-10-11 01:21:30 -0700718
KOSAKI Motohiroec95f532010-05-24 14:32:38 -0700719 return true;
720}
721
722static void __free_pages_ok(struct page *page, unsigned int order)
723{
724 unsigned long flags;
Minchan Kim95e34412012-10-08 16:32:11 -0700725 int migratetype;
KOSAKI Motohiroec95f532010-05-24 14:32:38 -0700726
727 if (!free_pages_prepare(page, order))
728 return;
729
Nick Pigginc54ad302006-01-06 00:10:56 -0800730 local_irq_save(flags);
Christoph Lameterf8891e52006-06-30 01:55:45 -0700731 __count_vm_events(PGFREE, 1 << order);
Minchan Kim95e34412012-10-08 16:32:11 -0700732 migratetype = get_pageblock_migratetype(page);
733 set_freepage_migratetype(page, migratetype);
734 free_one_page(page_zone(page), page, order, migratetype);
Nick Pigginc54ad302006-01-06 00:10:56 -0800735 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
Jiang Liu9feedc92012-12-12 13:52:12 -0800738/*
739 * Read access to zone->managed_pages is safe because it's unsigned long,
740 * but we still need to serialize writers. Currently all callers of
741 * __free_pages_bootmem() except put_page_bootmem() should only be used
742 * at boot time. So for shorter boot time, we shift the burden to
743 * put_page_bootmem() to serialize writers.
744 */
Yasunori Gotoaf370fb2008-07-23 21:28:17 -0700745void __meminit __free_pages_bootmem(struct page *page, unsigned int order)
David Howellsa226f6c2006-01-06 00:11:08 -0800746{
Johannes Weinerc3993072012-01-10 15:08:10 -0800747 unsigned int nr_pages = 1 << order;
748 unsigned int loop;
David Howellsa226f6c2006-01-06 00:11:08 -0800749
Johannes Weinerc3993072012-01-10 15:08:10 -0800750 prefetchw(page);
751 for (loop = 0; loop < nr_pages; loop++) {
752 struct page *p = &page[loop];
David Howellsa226f6c2006-01-06 00:11:08 -0800753
Johannes Weinerc3993072012-01-10 15:08:10 -0800754 if (loop + 1 < nr_pages)
755 prefetchw(p + 1);
756 __ClearPageReserved(p);
757 set_page_count(p, 0);
David Howellsa226f6c2006-01-06 00:11:08 -0800758 }
Johannes Weinerc3993072012-01-10 15:08:10 -0800759
Jiang Liu9feedc92012-12-12 13:52:12 -0800760 page_zone(page)->managed_pages += 1 << order;
Johannes Weinerc3993072012-01-10 15:08:10 -0800761 set_page_refcounted(page);
762 __free_pages(page, order);
David Howellsa226f6c2006-01-06 00:11:08 -0800763}
764
Michal Nazarewicz47118af2011-12-29 13:09:50 +0100765#ifdef CONFIG_CMA
766/* Free whole pageblock and set it's migration type to MIGRATE_CMA. */
767void __init init_cma_reserved_pageblock(struct page *page)
768{
769 unsigned i = pageblock_nr_pages;
770 struct page *p = page;
771
772 do {
773 __ClearPageReserved(p);
774 set_page_count(p, 0);
775 } while (++p, --i);
776
777 set_page_refcounted(page);
778 set_pageblock_migratetype(page, MIGRATE_CMA);
779 __free_pages(page, pageblock_order);
780 totalram_pages += pageblock_nr_pages;
Marek Szyprowski41a79732013-02-12 13:46:24 -0800781#ifdef CONFIG_HIGHMEM
782 if (PageHighMem(page))
783 totalhigh_pages += pageblock_nr_pages;
784#endif
Michal Nazarewicz47118af2011-12-29 13:09:50 +0100785}
786#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788/*
789 * The order of subdivision here is critical for the IO subsystem.
790 * Please do not alter this order without good reasons and regression
791 * testing. Specifically, as large blocks of memory are subdivided,
792 * the order in which smaller blocks are delivered depends on the order
793 * they're subdivided in this function. This is the primary factor
794 * influencing the order in which pages are delivered to the IO
795 * subsystem according to empirical testing, and this is also justified
796 * by considering the behavior of a buddy system containing a single
797 * large block of memory acted on by a series of small allocations.
798 * This behavior is a critical factor in sglist merging's success.
799 *
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +0100800 * -- nyc
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 */
Nick Piggin085cc7d2006-01-06 00:11:01 -0800802static inline void expand(struct zone *zone, struct page *page,
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700803 int low, int high, struct free_area *area,
804 int migratetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
806 unsigned long size = 1 << high;
807
808 while (high > low) {
809 area--;
810 high--;
811 size >>= 1;
Nick Piggin725d7042006-09-25 23:30:55 -0700812 VM_BUG_ON(bad_range(zone, &page[size]));
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -0800813
814#ifdef CONFIG_DEBUG_PAGEALLOC
815 if (high < debug_guardpage_minorder()) {
816 /*
817 * Mark as guard pages (or page), that will allow to
818 * merge back to allocator when buddy will be freed.
819 * Corresponding page table entries will not be touched,
820 * pages will stay not present in virtual address space
821 */
822 INIT_LIST_HEAD(&page[size].lru);
823 set_page_guard_flag(&page[size]);
824 set_page_private(&page[size], high);
825 /* Guard pages are not available for any usage */
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -0700826 __mod_zone_freepage_state(zone, -(1 << high),
827 migratetype);
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -0800828 continue;
829 }
830#endif
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700831 list_add(&page[size].lru, &area->free_list[migratetype]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 area->nr_free++;
833 set_page_order(&page[size], high);
834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837/*
838 * This page is about to be returned from the page allocator
839 */
Wu Fengguang2a7684a2009-09-16 11:50:12 +0200840static inline int check_new_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Nick Piggin92be2e332006-01-06 00:10:57 -0800842 if (unlikely(page_mapcount(page) |
843 (page->mapping != NULL) |
Nick Piggina3af9c32009-06-16 15:32:10 -0700844 (atomic_read(&page->_count) != 0) |
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -0700845 (page->flags & PAGE_FLAGS_CHECK_AT_PREP) |
846 (mem_cgroup_bad_page_check(page)))) {
Nick Piggin224abf92006-01-06 00:11:11 -0800847 bad_page(page);
Hugh Dickins689bceb2005-11-21 21:32:20 -0800848 return 1;
Hugh Dickins8cc3b392009-01-06 14:40:06 -0800849 }
Wu Fengguang2a7684a2009-09-16 11:50:12 +0200850 return 0;
851}
852
853static int prep_new_page(struct page *page, int order, gfp_t gfp_flags)
854{
855 int i;
856
857 for (i = 0; i < (1 << order); i++) {
858 struct page *p = page + i;
859 if (unlikely(check_new_page(p)))
860 return 1;
861 }
Hugh Dickins689bceb2005-11-21 21:32:20 -0800862
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700863 set_page_private(page, 0);
Nick Piggin7835e982006-03-22 00:08:40 -0800864 set_page_refcounted(page);
Nick Piggincc1025092006-12-06 20:32:00 -0800865
866 arch_alloc_page(page, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 kernel_map_pages(page, 1 << order, 1);
Nick Piggin17cf4402006-03-22 00:08:41 -0800868
869 if (gfp_flags & __GFP_ZERO)
870 prep_zero_page(page, order, gfp_flags);
871
872 if (order && (gfp_flags & __GFP_COMP))
873 prep_compound_page(page, order);
874
Hugh Dickins689bceb2005-11-21 21:32:20 -0800875 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
Mel Gorman56fd56b2007-10-16 01:25:58 -0700878/*
879 * Go through the free lists for the given migratetype and remove
880 * the smallest available page from the freelists
881 */
Mel Gorman728ec982009-06-16 15:32:04 -0700882static inline
883struct page *__rmqueue_smallest(struct zone *zone, unsigned int order,
Mel Gorman56fd56b2007-10-16 01:25:58 -0700884 int migratetype)
885{
886 unsigned int current_order;
887 struct free_area * area;
888 struct page *page;
889
890 /* Find a page of the appropriate size in the preferred list */
891 for (current_order = order; current_order < MAX_ORDER; ++current_order) {
892 area = &(zone->free_area[current_order]);
893 if (list_empty(&area->free_list[migratetype]))
894 continue;
895
896 page = list_entry(area->free_list[migratetype].next,
897 struct page, lru);
898 list_del(&page->lru);
899 rmv_page_order(page);
900 area->nr_free--;
Mel Gorman56fd56b2007-10-16 01:25:58 -0700901 expand(zone, page, order, current_order, area, migratetype);
902 return page;
903 }
904
905 return NULL;
906}
907
908
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700909/*
910 * This array describes the order lists are fallen back to when
911 * the free lists for the desirable migrate type are depleted
912 */
Michal Nazarewicz47118af2011-12-29 13:09:50 +0100913static int fallbacks[MIGRATE_TYPES][4] = {
914 [MIGRATE_UNMOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_MOVABLE, MIGRATE_RESERVE },
915 [MIGRATE_RECLAIMABLE] = { MIGRATE_UNMOVABLE, MIGRATE_MOVABLE, MIGRATE_RESERVE },
916#ifdef CONFIG_CMA
917 [MIGRATE_MOVABLE] = { MIGRATE_CMA, MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE, MIGRATE_RESERVE },
918 [MIGRATE_CMA] = { MIGRATE_RESERVE }, /* Never used */
919#else
920 [MIGRATE_MOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE, MIGRATE_RESERVE },
921#endif
Michal Nazarewicz6d4a4912012-01-11 15:31:33 +0100922 [MIGRATE_RESERVE] = { MIGRATE_RESERVE }, /* Never used */
923 [MIGRATE_ISOLATE] = { MIGRATE_RESERVE }, /* Never used */
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700924};
925
Mel Gormanc361be52007-10-16 01:25:51 -0700926/*
927 * Move the free pages in a range to the free lists of the requested type.
Mel Gormand9c23402007-10-16 01:26:01 -0700928 * Note that start_page and end_pages are not aligned on a pageblock
Mel Gormanc361be52007-10-16 01:25:51 -0700929 * boundary. If alignment is required, use move_freepages_block()
930 */
Minchan Kim435b4052012-10-08 16:32:16 -0700931int move_freepages(struct zone *zone,
Adrian Bunkb69a7282008-07-23 21:28:12 -0700932 struct page *start_page, struct page *end_page,
933 int migratetype)
Mel Gormanc361be52007-10-16 01:25:51 -0700934{
935 struct page *page;
936 unsigned long order;
Mel Gormand1003132007-10-16 01:26:00 -0700937 int pages_moved = 0;
Mel Gormanc361be52007-10-16 01:25:51 -0700938
939#ifndef CONFIG_HOLES_IN_ZONE
940 /*
941 * page_zone is not safe to call in this context when
942 * CONFIG_HOLES_IN_ZONE is set. This bug check is probably redundant
943 * anyway as we check zone boundaries in move_freepages_block().
944 * Remove at a later date when no bug reports exist related to
Mel Gormanac0e5b72007-10-16 01:25:58 -0700945 * grouping pages by mobility
Mel Gormanc361be52007-10-16 01:25:51 -0700946 */
947 BUG_ON(page_zone(start_page) != page_zone(end_page));
948#endif
949
950 for (page = start_page; page <= end_page;) {
Adam Litke344c7902008-09-02 14:35:38 -0700951 /* Make sure we are not inadvertently changing nodes */
952 VM_BUG_ON(page_to_nid(page) != zone_to_nid(zone));
953
Mel Gormanc361be52007-10-16 01:25:51 -0700954 if (!pfn_valid_within(page_to_pfn(page))) {
955 page++;
956 continue;
957 }
958
959 if (!PageBuddy(page)) {
960 page++;
961 continue;
962 }
963
964 order = page_order(page);
Kirill A. Shutemov84be48d2011-03-22 16:33:41 -0700965 list_move(&page->lru,
966 &zone->free_area[order].free_list[migratetype]);
Minchan Kim95e34412012-10-08 16:32:11 -0700967 set_freepage_migratetype(page, migratetype);
Mel Gormanc361be52007-10-16 01:25:51 -0700968 page += 1 << order;
Mel Gormand1003132007-10-16 01:26:00 -0700969 pages_moved += 1 << order;
Mel Gormanc361be52007-10-16 01:25:51 -0700970 }
971
Mel Gormand1003132007-10-16 01:26:00 -0700972 return pages_moved;
Mel Gormanc361be52007-10-16 01:25:51 -0700973}
974
Minchan Kimee6f5092012-07-31 16:43:50 -0700975int move_freepages_block(struct zone *zone, struct page *page,
Linus Torvalds68e3e922012-06-03 20:05:57 -0700976 int migratetype)
Mel Gormanc361be52007-10-16 01:25:51 -0700977{
978 unsigned long start_pfn, end_pfn;
979 struct page *start_page, *end_page;
980
981 start_pfn = page_to_pfn(page);
Mel Gormand9c23402007-10-16 01:26:01 -0700982 start_pfn = start_pfn & ~(pageblock_nr_pages-1);
Mel Gormanc361be52007-10-16 01:25:51 -0700983 start_page = pfn_to_page(start_pfn);
Mel Gormand9c23402007-10-16 01:26:01 -0700984 end_page = start_page + pageblock_nr_pages - 1;
985 end_pfn = start_pfn + pageblock_nr_pages - 1;
Mel Gormanc361be52007-10-16 01:25:51 -0700986
987 /* Do not cross zone boundaries */
988 if (start_pfn < zone->zone_start_pfn)
989 start_page = page;
990 if (end_pfn >= zone->zone_start_pfn + zone->spanned_pages)
991 return 0;
992
993 return move_freepages(zone, start_page, end_page, migratetype);
994}
995
Mel Gorman2f66a682009-09-21 17:02:31 -0700996static void change_pageblock_range(struct page *pageblock_page,
997 int start_order, int migratetype)
998{
999 int nr_pageblocks = 1 << (start_order - pageblock_order);
1000
1001 while (nr_pageblocks--) {
1002 set_pageblock_migratetype(pageblock_page, migratetype);
1003 pageblock_page += pageblock_nr_pages;
1004 }
1005}
1006
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001007/* Remove an element from the buddy allocator from the fallback list */
Mel Gorman0ac3a402009-06-16 15:32:06 -07001008static inline struct page *
1009__rmqueue_fallback(struct zone *zone, int order, int start_migratetype)
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001010{
1011 struct free_area * area;
1012 int current_order;
1013 struct page *page;
1014 int migratetype, i;
1015
1016 /* Find the largest possible block of pages in the other list */
1017 for (current_order = MAX_ORDER-1; current_order >= order;
1018 --current_order) {
Michal Nazarewicz6d4a4912012-01-11 15:31:33 +01001019 for (i = 0;; i++) {
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001020 migratetype = fallbacks[start_migratetype][i];
1021
Mel Gorman56fd56b2007-10-16 01:25:58 -07001022 /* MIGRATE_RESERVE handled later if necessary */
1023 if (migratetype == MIGRATE_RESERVE)
Michal Nazarewicz6d4a4912012-01-11 15:31:33 +01001024 break;
Mel Gormane0104872007-10-16 01:25:53 -07001025
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001026 area = &(zone->free_area[current_order]);
1027 if (list_empty(&area->free_list[migratetype]))
1028 continue;
1029
1030 page = list_entry(area->free_list[migratetype].next,
1031 struct page, lru);
1032 area->nr_free--;
1033
1034 /*
Mel Gormanc361be52007-10-16 01:25:51 -07001035 * If breaking a large block of pages, move all free
Mel Gorman46dafbc2007-10-16 01:25:55 -07001036 * pages to the preferred allocation list. If falling
1037 * back for a reclaimable kernel allocation, be more
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001038 * aggressive about taking ownership of free pages
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001039 *
1040 * On the other hand, never change migration
1041 * type of MIGRATE_CMA pageblocks nor move CMA
1042 * pages on different free lists. We don't
1043 * want unmovable pages to be allocated from
1044 * MIGRATE_CMA areas.
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001045 */
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001046 if (!is_migrate_cma(migratetype) &&
1047 (unlikely(current_order >= pageblock_order / 2) ||
1048 start_migratetype == MIGRATE_RECLAIMABLE ||
1049 page_group_by_mobility_disabled)) {
1050 int pages;
Mel Gorman46dafbc2007-10-16 01:25:55 -07001051 pages = move_freepages_block(zone, page,
1052 start_migratetype);
1053
1054 /* Claim the whole block if over half of it is free */
Mel Gormandd5d2412009-09-05 11:17:11 -07001055 if (pages >= (1 << (pageblock_order-1)) ||
1056 page_group_by_mobility_disabled)
Mel Gorman46dafbc2007-10-16 01:25:55 -07001057 set_pageblock_migratetype(page,
1058 start_migratetype);
1059
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001060 migratetype = start_migratetype;
Mel Gormanc361be52007-10-16 01:25:51 -07001061 }
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001062
1063 /* Remove the page from the freelists */
1064 list_del(&page->lru);
1065 rmv_page_order(page);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001066
Mel Gorman2f66a682009-09-21 17:02:31 -07001067 /* Take ownership for orders >= pageblock_order */
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001068 if (current_order >= pageblock_order &&
1069 !is_migrate_cma(migratetype))
Mel Gorman2f66a682009-09-21 17:02:31 -07001070 change_pageblock_range(page, current_order,
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001071 start_migratetype);
1072
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001073 expand(zone, page, order, current_order, area,
1074 is_migrate_cma(migratetype)
1075 ? migratetype : start_migratetype);
Mel Gormane0fff1b2009-09-21 17:02:42 -07001076
1077 trace_mm_page_alloc_extfrag(page, order, current_order,
1078 start_migratetype, migratetype);
1079
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001080 return page;
1081 }
1082 }
1083
Mel Gorman728ec982009-06-16 15:32:04 -07001084 return NULL;
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001085}
1086
Mel Gorman56fd56b2007-10-16 01:25:58 -07001087/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 * Do the hard work of removing an element from the buddy allocator.
1089 * Call me with the zone->lock already held.
1090 */
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001091static struct page *__rmqueue(struct zone *zone, unsigned int order,
1092 int migratetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 struct page *page;
1095
Mel Gorman728ec982009-06-16 15:32:04 -07001096retry_reserve:
Mel Gorman56fd56b2007-10-16 01:25:58 -07001097 page = __rmqueue_smallest(zone, order, migratetype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Mel Gorman728ec982009-06-16 15:32:04 -07001099 if (unlikely(!page) && migratetype != MIGRATE_RESERVE) {
Mel Gorman56fd56b2007-10-16 01:25:58 -07001100 page = __rmqueue_fallback(zone, order, migratetype);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001101
Mel Gorman728ec982009-06-16 15:32:04 -07001102 /*
1103 * Use MIGRATE_RESERVE rather than fail an allocation. goto
1104 * is used because __rmqueue_smallest is an inline function
1105 * and we want just one call site
1106 */
1107 if (!page) {
1108 migratetype = MIGRATE_RESERVE;
1109 goto retry_reserve;
1110 }
1111 }
1112
Mel Gorman0d3d0622009-09-21 17:02:44 -07001113 trace_mm_page_alloc_zone_locked(page, order, migratetype);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001114 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
1116
Michal Nazarewicz5f63b722012-01-11 15:16:11 +01001117/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 * Obtain a specified number of elements from the buddy allocator, all under
1119 * a single hold of the lock, for efficiency. Add them to the supplied list.
1120 * Returns the number of new pages which were placed at *list.
1121 */
Michal Nazarewicz5f63b722012-01-11 15:16:11 +01001122static int rmqueue_bulk(struct zone *zone, unsigned int order,
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001123 unsigned long count, struct list_head *list,
Mel Gormane084b2d2009-07-29 15:02:04 -07001124 int migratetype, int cold)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001126 int mt = migratetype, i;
Michal Nazarewicz5f63b722012-01-11 15:16:11 +01001127
Nick Pigginc54ad302006-01-06 00:10:56 -08001128 spin_lock(&zone->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 for (i = 0; i < count; ++i) {
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001130 struct page *page = __rmqueue(zone, order, migratetype);
Nick Piggin085cc7d2006-01-06 00:11:01 -08001131 if (unlikely(page == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 break;
Mel Gorman81eabcb2007-12-17 16:20:05 -08001133
1134 /*
1135 * Split buddy pages returned by expand() are received here
1136 * in physical page order. The page is added to the callers and
1137 * list and the list head then moves forward. From the callers
1138 * perspective, the linked list is ordered by page number in
1139 * some conditions. This is useful for IO devices that can
1140 * merge IO requests if the physical pages are ordered
1141 * properly.
1142 */
Mel Gormane084b2d2009-07-29 15:02:04 -07001143 if (likely(cold == 0))
1144 list_add(&page->lru, list);
1145 else
1146 list_add_tail(&page->lru, list);
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001147 if (IS_ENABLED(CONFIG_CMA)) {
1148 mt = get_pageblock_migratetype(page);
1149 if (!is_migrate_cma(mt) && mt != MIGRATE_ISOLATE)
1150 mt = migratetype;
1151 }
Minchan Kimb12c4ad2012-10-08 16:32:08 -07001152 set_freepage_migratetype(page, mt);
Mel Gorman81eabcb2007-12-17 16:20:05 -08001153 list = &page->lru;
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -07001154 if (is_migrate_cma(mt))
1155 __mod_zone_page_state(zone, NR_FREE_CMA_PAGES,
1156 -(1 << order));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
Mel Gormanf2260e62009-06-16 15:32:13 -07001158 __mod_zone_page_state(zone, NR_FREE_PAGES, -(i << order));
Nick Pigginc54ad302006-01-06 00:10:56 -08001159 spin_unlock(&zone->lock);
Nick Piggin085cc7d2006-01-06 00:11:01 -08001160 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
Christoph Lameter4ae7c032005-06-21 17:14:57 -07001163#ifdef CONFIG_NUMA
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001164/*
Christoph Lameter4037d452007-05-09 02:35:14 -07001165 * Called from the vmstat counter updater to drain pagesets of this
1166 * currently executing processor on remote nodes after they have
1167 * expired.
1168 *
Christoph Lameter879336c2006-03-22 00:09:08 -08001169 * Note that this function must be called with the thread pinned to
1170 * a single processor.
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001171 */
Christoph Lameter4037d452007-05-09 02:35:14 -07001172void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
Christoph Lameter4ae7c032005-06-21 17:14:57 -07001173{
Christoph Lameter4ae7c032005-06-21 17:14:57 -07001174 unsigned long flags;
Christoph Lameter4037d452007-05-09 02:35:14 -07001175 int to_drain;
Christoph Lameter4ae7c032005-06-21 17:14:57 -07001176
Christoph Lameter4037d452007-05-09 02:35:14 -07001177 local_irq_save(flags);
1178 if (pcp->count >= pcp->batch)
1179 to_drain = pcp->batch;
1180 else
1181 to_drain = pcp->count;
KOSAKI Motohiro2a135152012-07-31 16:42:53 -07001182 if (to_drain > 0) {
1183 free_pcppages_bulk(zone, to_drain, pcp);
1184 pcp->count -= to_drain;
1185 }
Christoph Lameter4037d452007-05-09 02:35:14 -07001186 local_irq_restore(flags);
Christoph Lameter4ae7c032005-06-21 17:14:57 -07001187}
1188#endif
1189
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001190/*
1191 * Drain pages of the indicated processor.
1192 *
1193 * The processor must either be the current processor and the
1194 * thread pinned to the current processor or a processor that
1195 * is not online.
1196 */
1197static void drain_pages(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
Nick Pigginc54ad302006-01-06 00:10:56 -08001199 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 struct zone *zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
KOSAKI Motohiroee99c712009-03-31 15:19:31 -07001202 for_each_populated_zone(zone) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 struct per_cpu_pageset *pset;
Christoph Lameter3dfa5722008-02-04 22:29:19 -08001204 struct per_cpu_pages *pcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09001206 local_irq_save(flags);
1207 pset = per_cpu_ptr(zone->pageset, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Christoph Lameter3dfa5722008-02-04 22:29:19 -08001209 pcp = &pset->pcp;
David Rientjes2ff754f2011-01-25 15:07:23 -08001210 if (pcp->count) {
1211 free_pcppages_bulk(zone, pcp->count, pcp);
1212 pcp->count = 0;
1213 }
Christoph Lameter3dfa5722008-02-04 22:29:19 -08001214 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
1216}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001218/*
1219 * Spill all of this CPU's per-cpu pages back into the buddy allocator.
1220 */
1221void drain_local_pages(void *arg)
1222{
1223 drain_pages(smp_processor_id());
1224}
1225
1226/*
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07001227 * Spill all the per-cpu pages from all CPUs back into the buddy allocator.
1228 *
1229 * Note that this code is protected against sending an IPI to an offline
1230 * CPU but does not guarantee sending an IPI to newly hotplugged CPUs:
1231 * on_each_cpu_mask() blocks hotplug and won't talk to offlined CPUs but
1232 * nothing keeps CPUs from showing up after we populated the cpumask and
1233 * before the call to on_each_cpu_mask().
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001234 */
1235void drain_all_pages(void)
1236{
Gilad Ben-Yossef74046492012-03-28 14:42:45 -07001237 int cpu;
1238 struct per_cpu_pageset *pcp;
1239 struct zone *zone;
1240
1241 /*
1242 * Allocate in the BSS so we wont require allocation in
1243 * direct reclaim path for CONFIG_CPUMASK_OFFSTACK=y
1244 */
1245 static cpumask_t cpus_with_pcps;
1246
1247 /*
1248 * We don't care about racing with CPU hotplug event
1249 * as offline notification will cause the notified
1250 * cpu to drain that CPU pcps and on_each_cpu_mask
1251 * disables preemption as part of its processing
1252 */
1253 for_each_online_cpu(cpu) {
1254 bool has_pcps = false;
1255 for_each_populated_zone(zone) {
1256 pcp = per_cpu_ptr(zone->pageset, cpu);
1257 if (pcp->pcp.count) {
1258 has_pcps = true;
1259 break;
1260 }
1261 }
1262 if (has_pcps)
1263 cpumask_set_cpu(cpu, &cpus_with_pcps);
1264 else
1265 cpumask_clear_cpu(cpu, &cpus_with_pcps);
1266 }
1267 on_each_cpu_mask(&cpus_with_pcps, drain_local_pages, NULL, 1);
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001268}
1269
Rafael J. Wysocki296699d2007-07-29 23:27:18 +02001270#ifdef CONFIG_HIBERNATION
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272void mark_free_pages(struct zone *zone)
1273{
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07001274 unsigned long pfn, max_zone_pfn;
1275 unsigned long flags;
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001276 int order, t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 struct list_head *curr;
1278
1279 if (!zone->spanned_pages)
1280 return;
1281
1282 spin_lock_irqsave(&zone->lock, flags);
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07001283
1284 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
1285 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
1286 if (pfn_valid(pfn)) {
1287 struct page *page = pfn_to_page(pfn);
1288
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001289 if (!swsusp_page_is_forbidden(page))
1290 swsusp_unset_page_free(page);
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07001291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001293 for_each_migratetype_order(order, t) {
1294 list_for_each(curr, &zone->free_area[order].free_list[t]) {
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07001295 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07001297 pfn = page_to_pfn(list_entry(curr, struct page, lru));
1298 for (i = 0; i < (1UL << order); i++)
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001299 swsusp_set_page_free(pfn_to_page(pfn + i));
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07001300 }
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 spin_unlock_irqrestore(&zone->lock, flags);
1303}
Mel Gormane2c55dc2007-10-16 01:25:50 -07001304#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 * Free a 0-order page
Li Hongfc916682010-03-05 13:41:54 -08001308 * cold == 1 ? free a cold page : free a hot page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 */
Li Hongfc916682010-03-05 13:41:54 -08001310void free_hot_cold_page(struct page *page, int cold)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
1312 struct zone *zone = page_zone(page);
1313 struct per_cpu_pages *pcp;
1314 unsigned long flags;
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001315 int migratetype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
KOSAKI Motohiroec95f532010-05-24 14:32:38 -07001317 if (!free_pages_prepare(page, 0))
Hugh Dickins689bceb2005-11-21 21:32:20 -08001318 return;
1319
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001320 migratetype = get_pageblock_migratetype(page);
Minchan Kimb12c4ad2012-10-08 16:32:08 -07001321 set_freepage_migratetype(page, migratetype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 local_irq_save(flags);
Christoph Lameterf8891e52006-06-30 01:55:45 -07001323 __count_vm_event(PGFREE);
Mel Gormanda456f12009-06-16 15:32:08 -07001324
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001325 /*
1326 * We only track unmovable, reclaimable and movable on pcp lists.
1327 * Free ISOLATE pages back to the allocator because they are being
1328 * offlined but treat RESERVE as movable pages so we can get those
1329 * areas back if necessary. Otherwise, we may have to free
1330 * excessively into the page allocator
1331 */
1332 if (migratetype >= MIGRATE_PCPTYPES) {
1333 if (unlikely(migratetype == MIGRATE_ISOLATE)) {
1334 free_one_page(zone, page, 0, migratetype);
1335 goto out;
1336 }
1337 migratetype = MIGRATE_MOVABLE;
1338 }
1339
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09001340 pcp = &this_cpu_ptr(zone->pageset)->pcp;
Christoph Lameter3dfa5722008-02-04 22:29:19 -08001341 if (cold)
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001342 list_add_tail(&page->lru, &pcp->lists[migratetype]);
Christoph Lameter3dfa5722008-02-04 22:29:19 -08001343 else
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001344 list_add(&page->lru, &pcp->lists[migratetype]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 pcp->count++;
Nick Piggin48db57f2006-01-08 01:00:42 -08001346 if (pcp->count >= pcp->high) {
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001347 free_pcppages_bulk(zone, pcp->batch, pcp);
Nick Piggin48db57f2006-01-08 01:00:42 -08001348 pcp->count -= pcp->batch;
1349 }
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001350
1351out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353}
1354
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08001355/*
Konstantin Khlebnikovcc598502012-01-10 15:07:04 -08001356 * Free a list of 0-order pages
1357 */
1358void free_hot_cold_page_list(struct list_head *list, int cold)
1359{
1360 struct page *page, *next;
1361
1362 list_for_each_entry_safe(page, next, list, lru) {
Konstantin Khlebnikovb413d482012-01-10 15:07:09 -08001363 trace_mm_page_free_batched(page, cold);
Konstantin Khlebnikovcc598502012-01-10 15:07:04 -08001364 free_hot_cold_page(page, cold);
1365 }
1366}
1367
1368/*
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08001369 * split_page takes a non-compound higher-order page, and splits it into
1370 * n (1<<order) sub-pages: page[0..n]
1371 * Each sub-page must be freed individually.
1372 *
1373 * Note: this is probably too low level an operation for use in drivers.
1374 * Please consult with lkml before using this in your driver.
1375 */
1376void split_page(struct page *page, unsigned int order)
1377{
1378 int i;
1379
Nick Piggin725d7042006-09-25 23:30:55 -07001380 VM_BUG_ON(PageCompound(page));
1381 VM_BUG_ON(!page_count(page));
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001382
1383#ifdef CONFIG_KMEMCHECK
1384 /*
1385 * Split shadow pages too, because free(page[0]) would
1386 * otherwise free the whole shadow.
1387 */
1388 if (kmemcheck_page_is_tracked(page))
1389 split_page(virt_to_page(page[0].shadow), order);
1390#endif
1391
Nick Piggin7835e982006-03-22 00:08:40 -08001392 for (i = 1; i < (1 << order); i++)
1393 set_page_refcounted(page + i);
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08001394}
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08001395
Mel Gorman8fb74b92013-01-11 14:32:16 -08001396static int __isolate_free_page(struct page *page, unsigned int order)
Mel Gorman748446b2010-05-24 14:32:27 -07001397{
Mel Gorman748446b2010-05-24 14:32:27 -07001398 unsigned long watermark;
1399 struct zone *zone;
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -07001400 int mt;
Mel Gorman748446b2010-05-24 14:32:27 -07001401
1402 BUG_ON(!PageBuddy(page));
1403
1404 zone = page_zone(page);
Marek Szyprowski2e30abd2012-12-11 16:02:57 -08001405 mt = get_pageblock_migratetype(page);
Mel Gorman748446b2010-05-24 14:32:27 -07001406
Marek Szyprowski2e30abd2012-12-11 16:02:57 -08001407 if (mt != MIGRATE_ISOLATE) {
1408 /* Obey watermarks as if the page was being allocated */
1409 watermark = low_wmark_pages(zone) + (1 << order);
1410 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
1411 return 0;
1412
Mel Gorman8fb74b92013-01-11 14:32:16 -08001413 __mod_zone_freepage_state(zone, -(1UL << order), mt);
Marek Szyprowski2e30abd2012-12-11 16:02:57 -08001414 }
Mel Gorman748446b2010-05-24 14:32:27 -07001415
1416 /* Remove page from free list */
1417 list_del(&page->lru);
1418 zone->free_area[order].nr_free--;
1419 rmv_page_order(page);
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -07001420
Mel Gorman8fb74b92013-01-11 14:32:16 -08001421 /* Set the pageblock if the isolated page is at least a pageblock */
Mel Gorman748446b2010-05-24 14:32:27 -07001422 if (order >= pageblock_order - 1) {
1423 struct page *endpage = page + (1 << order) - 1;
Michal Nazarewicz47118af2011-12-29 13:09:50 +01001424 for (; page < endpage; page += pageblock_nr_pages) {
1425 int mt = get_pageblock_migratetype(page);
1426 if (mt != MIGRATE_ISOLATE && !is_migrate_cma(mt))
1427 set_pageblock_migratetype(page,
1428 MIGRATE_MOVABLE);
1429 }
Mel Gorman748446b2010-05-24 14:32:27 -07001430 }
1431
Mel Gorman8fb74b92013-01-11 14:32:16 -08001432 return 1UL << order;
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001433}
1434
1435/*
1436 * Similar to split_page except the page is already free. As this is only
1437 * being used for migration, the migratetype of the block also changes.
1438 * As this is called with interrupts disabled, the caller is responsible
1439 * for calling arch_alloc_page() and kernel_map_page() after interrupts
1440 * are enabled.
1441 *
1442 * Note: this is probably too low level an operation for use in drivers.
1443 * Please consult with lkml before using this in your driver.
1444 */
1445int split_free_page(struct page *page)
1446{
1447 unsigned int order;
1448 int nr_pages;
1449
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001450 order = page_order(page);
1451
Mel Gorman8fb74b92013-01-11 14:32:16 -08001452 nr_pages = __isolate_free_page(page, order);
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001453 if (!nr_pages)
1454 return 0;
1455
1456 /* Split into individual pages */
1457 set_page_refcounted(page);
1458 split_page(page, order);
1459 return nr_pages;
Mel Gorman748446b2010-05-24 14:32:27 -07001460}
1461
1462/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
1464 * we cheat by calling it from here, in the order > 0 path. Saves a branch
1465 * or two.
1466 */
Mel Gorman0a15c3e2009-06-16 15:32:05 -07001467static inline
1468struct page *buffered_rmqueue(struct zone *preferred_zone,
Mel Gorman3dd28262009-06-16 15:32:00 -07001469 struct zone *zone, int order, gfp_t gfp_flags,
1470 int migratetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
1472 unsigned long flags;
Hugh Dickins689bceb2005-11-21 21:32:20 -08001473 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 int cold = !!(gfp_flags & __GFP_COLD);
1475
Hugh Dickins689bceb2005-11-21 21:32:20 -08001476again:
Nick Piggin48db57f2006-01-08 01:00:42 -08001477 if (likely(order == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 struct per_cpu_pages *pcp;
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001479 struct list_head *list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 local_irq_save(flags);
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09001482 pcp = &this_cpu_ptr(zone->pageset)->pcp;
1483 list = &pcp->lists[migratetype];
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001484 if (list_empty(list)) {
Mel Gorman535131e62007-10-16 01:25:49 -07001485 pcp->count += rmqueue_bulk(zone, 0,
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001486 pcp->batch, list,
Mel Gormane084b2d2009-07-29 15:02:04 -07001487 migratetype, cold);
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001488 if (unlikely(list_empty(list)))
Shaohua Li6fb332f2009-09-21 17:01:17 -07001489 goto failed;
Mel Gorman535131e62007-10-16 01:25:49 -07001490 }
Mel Gormanb92a6ed2007-10-16 01:25:50 -07001491
Mel Gorman5f8dcc22009-09-21 17:03:19 -07001492 if (cold)
1493 page = list_entry(list->prev, struct page, lru);
1494 else
1495 page = list_entry(list->next, struct page, lru);
1496
Mel Gormanb92a6ed2007-10-16 01:25:50 -07001497 list_del(&page->lru);
1498 pcp->count--;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001499 } else {
Andrew Mortondab48da2009-06-16 15:32:37 -07001500 if (unlikely(gfp_flags & __GFP_NOFAIL)) {
1501 /*
1502 * __GFP_NOFAIL is not to be used in new code.
1503 *
1504 * All __GFP_NOFAIL callers should be fixed so that they
1505 * properly detect and handle allocation failures.
1506 *
1507 * We most definitely don't want callers attempting to
Linus Torvalds4923abf2009-06-24 12:16:49 -07001508 * allocate greater than order-1 page units with
Andrew Mortondab48da2009-06-16 15:32:37 -07001509 * __GFP_NOFAIL.
1510 */
Linus Torvalds4923abf2009-06-24 12:16:49 -07001511 WARN_ON_ONCE(order > 1);
Andrew Mortondab48da2009-06-16 15:32:37 -07001512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 spin_lock_irqsave(&zone->lock, flags);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001514 page = __rmqueue(zone, order, migratetype);
Nick Piggina74609f2006-01-06 00:11:20 -08001515 spin_unlock(&zone->lock);
1516 if (!page)
1517 goto failed;
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -07001518 __mod_zone_freepage_state(zone, -(1 << order),
1519 get_pageblock_migratetype(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 }
1521
Christoph Lameterf8891e52006-06-30 01:55:45 -07001522 __count_zone_vm_events(PGALLOC, zone, 1 << order);
Andi Kleen78afd562011-03-22 16:33:12 -07001523 zone_statistics(preferred_zone, zone, gfp_flags);
Nick Piggina74609f2006-01-06 00:11:20 -08001524 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Nick Piggin725d7042006-09-25 23:30:55 -07001526 VM_BUG_ON(bad_range(zone, page));
Nick Piggin17cf4402006-03-22 00:08:41 -08001527 if (prep_new_page(page, order, gfp_flags))
Nick Piggina74609f2006-01-06 00:11:20 -08001528 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 return page;
Nick Piggina74609f2006-01-06 00:11:20 -08001530
1531failed:
1532 local_irq_restore(flags);
Nick Piggina74609f2006-01-06 00:11:20 -08001533 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534}
1535
Akinobu Mita933e3122006-12-08 02:39:45 -08001536#ifdef CONFIG_FAIL_PAGE_ALLOC
1537
Akinobu Mitab2588c42011-07-26 16:09:03 -07001538static struct {
Akinobu Mita933e3122006-12-08 02:39:45 -08001539 struct fault_attr attr;
1540
1541 u32 ignore_gfp_highmem;
1542 u32 ignore_gfp_wait;
Akinobu Mita54114992007-07-15 23:40:23 -07001543 u32 min_order;
Akinobu Mita933e3122006-12-08 02:39:45 -08001544} fail_page_alloc = {
1545 .attr = FAULT_ATTR_INITIALIZER,
Don Mullis6b1b60f2006-12-08 02:39:53 -08001546 .ignore_gfp_wait = 1,
1547 .ignore_gfp_highmem = 1,
Akinobu Mita54114992007-07-15 23:40:23 -07001548 .min_order = 1,
Akinobu Mita933e3122006-12-08 02:39:45 -08001549};
1550
1551static int __init setup_fail_page_alloc(char *str)
1552{
1553 return setup_fault_attr(&fail_page_alloc.attr, str);
1554}
1555__setup("fail_page_alloc=", setup_fail_page_alloc);
1556
Gavin Shandeaf3862012-07-31 16:41:51 -07001557static bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
Akinobu Mita933e3122006-12-08 02:39:45 -08001558{
Akinobu Mita54114992007-07-15 23:40:23 -07001559 if (order < fail_page_alloc.min_order)
Gavin Shandeaf3862012-07-31 16:41:51 -07001560 return false;
Akinobu Mita933e3122006-12-08 02:39:45 -08001561 if (gfp_mask & __GFP_NOFAIL)
Gavin Shandeaf3862012-07-31 16:41:51 -07001562 return false;
Akinobu Mita933e3122006-12-08 02:39:45 -08001563 if (fail_page_alloc.ignore_gfp_highmem && (gfp_mask & __GFP_HIGHMEM))
Gavin Shandeaf3862012-07-31 16:41:51 -07001564 return false;
Akinobu Mita933e3122006-12-08 02:39:45 -08001565 if (fail_page_alloc.ignore_gfp_wait && (gfp_mask & __GFP_WAIT))
Gavin Shandeaf3862012-07-31 16:41:51 -07001566 return false;
Akinobu Mita933e3122006-12-08 02:39:45 -08001567
1568 return should_fail(&fail_page_alloc.attr, 1 << order);
1569}
1570
1571#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
1572
1573static int __init fail_page_alloc_debugfs(void)
1574{
Al Virof4ae40a2011-07-24 04:33:43 -04001575 umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
Akinobu Mita933e3122006-12-08 02:39:45 -08001576 struct dentry *dir;
Akinobu Mita933e3122006-12-08 02:39:45 -08001577
Akinobu Mitadd48c082011-08-03 16:21:01 -07001578 dir = fault_create_debugfs_attr("fail_page_alloc", NULL,
1579 &fail_page_alloc.attr);
1580 if (IS_ERR(dir))
1581 return PTR_ERR(dir);
Akinobu Mita933e3122006-12-08 02:39:45 -08001582
Akinobu Mitab2588c42011-07-26 16:09:03 -07001583 if (!debugfs_create_bool("ignore-gfp-wait", mode, dir,
1584 &fail_page_alloc.ignore_gfp_wait))
1585 goto fail;
1586 if (!debugfs_create_bool("ignore-gfp-highmem", mode, dir,
1587 &fail_page_alloc.ignore_gfp_highmem))
1588 goto fail;
1589 if (!debugfs_create_u32("min-order", mode, dir,
1590 &fail_page_alloc.min_order))
1591 goto fail;
Akinobu Mita933e3122006-12-08 02:39:45 -08001592
Akinobu Mitab2588c42011-07-26 16:09:03 -07001593 return 0;
1594fail:
Akinobu Mitadd48c082011-08-03 16:21:01 -07001595 debugfs_remove_recursive(dir);
Akinobu Mita933e3122006-12-08 02:39:45 -08001596
Akinobu Mitab2588c42011-07-26 16:09:03 -07001597 return -ENOMEM;
Akinobu Mita933e3122006-12-08 02:39:45 -08001598}
1599
1600late_initcall(fail_page_alloc_debugfs);
1601
1602#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
1603
1604#else /* CONFIG_FAIL_PAGE_ALLOC */
1605
Gavin Shandeaf3862012-07-31 16:41:51 -07001606static inline bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
Akinobu Mita933e3122006-12-08 02:39:45 -08001607{
Gavin Shandeaf3862012-07-31 16:41:51 -07001608 return false;
Akinobu Mita933e3122006-12-08 02:39:45 -08001609}
1610
1611#endif /* CONFIG_FAIL_PAGE_ALLOC */
1612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613/*
Mel Gorman88f5acf2011-01-13 15:45:41 -08001614 * Return true if free pages are above 'mark'. This takes into account the order
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 * of the allocation.
1616 */
Mel Gorman88f5acf2011-01-13 15:45:41 -08001617static bool __zone_watermark_ok(struct zone *z, int order, unsigned long mark,
1618 int classzone_idx, int alloc_flags, long free_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
1620 /* free_pages my go negative - that's OK */
Christoph Lameterd23ad422007-02-10 01:43:02 -08001621 long min = mark;
Minchan Kim2cfed072012-07-31 16:43:53 -07001622 long lowmem_reserve = z->lowmem_reserve[classzone_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 int o;
1624
Michal Hockodf0a6da2012-01-10 15:08:02 -08001625 free_pages -= (1 << order) - 1;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001626 if (alloc_flags & ALLOC_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 min -= min / 2;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001628 if (alloc_flags & ALLOC_HARDER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 min -= min / 4;
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001630#ifdef CONFIG_CMA
1631 /* If allocation can't use CMA areas don't use free CMA pages */
1632 if (!(alloc_flags & ALLOC_CMA))
1633 free_pages -= zone_page_state(z, NR_FREE_CMA_PAGES);
1634#endif
Minchan Kim2cfed072012-07-31 16:43:53 -07001635 if (free_pages <= min + lowmem_reserve)
Mel Gorman88f5acf2011-01-13 15:45:41 -08001636 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 for (o = 0; o < order; o++) {
1638 /* At the next order, this order's pages become unavailable */
1639 free_pages -= z->free_area[o].nr_free << o;
1640
1641 /* Require fewer higher order pages to be free */
1642 min >>= 1;
1643
1644 if (free_pages <= min)
Mel Gorman88f5acf2011-01-13 15:45:41 -08001645 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 }
Mel Gorman88f5acf2011-01-13 15:45:41 -08001647 return true;
1648}
1649
1650bool zone_watermark_ok(struct zone *z, int order, unsigned long mark,
1651 int classzone_idx, int alloc_flags)
1652{
1653 return __zone_watermark_ok(z, order, mark, classzone_idx, alloc_flags,
1654 zone_page_state(z, NR_FREE_PAGES));
1655}
1656
1657bool zone_watermark_ok_safe(struct zone *z, int order, unsigned long mark,
1658 int classzone_idx, int alloc_flags)
1659{
1660 long free_pages = zone_page_state(z, NR_FREE_PAGES);
1661
1662 if (z->percpu_drift_mark && free_pages < z->percpu_drift_mark)
1663 free_pages = zone_page_state_snapshot(z, NR_FREE_PAGES);
1664
1665 return __zone_watermark_ok(z, order, mark, classzone_idx, alloc_flags,
1666 free_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667}
1668
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001669#ifdef CONFIG_NUMA
1670/*
1671 * zlc_setup - Setup for "zonelist cache". Uses cached zone data to
1672 * skip over zones that are not allowed by the cpuset, or that have
1673 * been recently (in last second) found to be nearly full. See further
1674 * comments in mmzone.h. Reduces cache footprint of zonelist scans
Simon Arlott183ff222007-10-20 01:27:18 +02001675 * that have to skip over a lot of full or unallowed zones.
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001676 *
1677 * If the zonelist cache is present in the passed in zonelist, then
1678 * returns a pointer to the allowed node mask (either the current
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08001679 * tasks mems_allowed, or node_states[N_MEMORY].)
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001680 *
1681 * If the zonelist cache is not available for this zonelist, does
1682 * nothing and returns NULL.
1683 *
1684 * If the fullzones BITMAP in the zonelist cache is stale (more than
1685 * a second since last zap'd) then we zap it out (clear its bits.)
1686 *
1687 * We hold off even calling zlc_setup, until after we've checked the
1688 * first zone in the zonelist, on the theory that most allocations will
1689 * be satisfied from that first zone, so best to examine that zone as
1690 * quickly as we can.
1691 */
1692static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
1693{
1694 struct zonelist_cache *zlc; /* cached zonelist speedup info */
1695 nodemask_t *allowednodes; /* zonelist_cache approximation */
1696
1697 zlc = zonelist->zlcache_ptr;
1698 if (!zlc)
1699 return NULL;
1700
S.Caglar Onurf05111f2008-04-28 02:12:38 -07001701 if (time_after(jiffies, zlc->last_full_zap + HZ)) {
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001702 bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
1703 zlc->last_full_zap = jiffies;
1704 }
1705
1706 allowednodes = !in_interrupt() && (alloc_flags & ALLOC_CPUSET) ?
1707 &cpuset_current_mems_allowed :
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08001708 &node_states[N_MEMORY];
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001709 return allowednodes;
1710}
1711
1712/*
1713 * Given 'z' scanning a zonelist, run a couple of quick checks to see
1714 * if it is worth looking at further for free memory:
1715 * 1) Check that the zone isn't thought to be full (doesn't have its
1716 * bit set in the zonelist_cache fullzones BITMAP).
1717 * 2) Check that the zones node (obtained from the zonelist_cache
1718 * z_to_n[] mapping) is allowed in the passed in allowednodes mask.
1719 * Return true (non-zero) if zone is worth looking at further, or
1720 * else return false (zero) if it is not.
1721 *
1722 * This check -ignores- the distinction between various watermarks,
1723 * such as GFP_HIGH, GFP_ATOMIC, PF_MEMALLOC, ... If a zone is
1724 * found to be full for any variation of these watermarks, it will
1725 * be considered full for up to one second by all requests, unless
1726 * we are so low on memory on all allowed nodes that we are forced
1727 * into the second scan of the zonelist.
1728 *
1729 * In the second scan we ignore this zonelist cache and exactly
1730 * apply the watermarks to all zones, even it is slower to do so.
1731 * We are low on memory in the second scan, and should leave no stone
1732 * unturned looking for a free page.
1733 */
Mel Gormandd1a2392008-04-28 02:12:17 -07001734static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zoneref *z,
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001735 nodemask_t *allowednodes)
1736{
1737 struct zonelist_cache *zlc; /* cached zonelist speedup info */
1738 int i; /* index of *z in zonelist zones */
1739 int n; /* node that zone *z is on */
1740
1741 zlc = zonelist->zlcache_ptr;
1742 if (!zlc)
1743 return 1;
1744
Mel Gormandd1a2392008-04-28 02:12:17 -07001745 i = z - zonelist->_zonerefs;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001746 n = zlc->z_to_n[i];
1747
1748 /* This zone is worth trying if it is allowed but not full */
1749 return node_isset(n, *allowednodes) && !test_bit(i, zlc->fullzones);
1750}
1751
1752/*
1753 * Given 'z' scanning a zonelist, set the corresponding bit in
1754 * zlc->fullzones, so that subsequent attempts to allocate a page
1755 * from that zone don't waste time re-examining it.
1756 */
Mel Gormandd1a2392008-04-28 02:12:17 -07001757static void zlc_mark_zone_full(struct zonelist *zonelist, struct zoneref *z)
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001758{
1759 struct zonelist_cache *zlc; /* cached zonelist speedup info */
1760 int i; /* index of *z in zonelist zones */
1761
1762 zlc = zonelist->zlcache_ptr;
1763 if (!zlc)
1764 return;
1765
Mel Gormandd1a2392008-04-28 02:12:17 -07001766 i = z - zonelist->_zonerefs;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001767
1768 set_bit(i, zlc->fullzones);
1769}
1770
Mel Gorman76d3fbf2011-07-25 17:12:30 -07001771/*
1772 * clear all zones full, called after direct reclaim makes progress so that
1773 * a zone that was recently full is not skipped over for up to a second
1774 */
1775static void zlc_clear_zones_full(struct zonelist *zonelist)
1776{
1777 struct zonelist_cache *zlc; /* cached zonelist speedup info */
1778
1779 zlc = zonelist->zlcache_ptr;
1780 if (!zlc)
1781 return;
1782
1783 bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
1784}
1785
David Rientjes957f8222012-10-08 16:33:24 -07001786static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone)
1787{
1788 return node_isset(local_zone->node, zone->zone_pgdat->reclaim_nodes);
1789}
1790
1791static void __paginginit init_zone_allows_reclaim(int nid)
1792{
1793 int i;
1794
1795 for_each_online_node(i)
David Rientjes6b187d022012-10-25 13:38:08 -07001796 if (node_distance(nid, i) <= RECLAIM_DISTANCE)
David Rientjes957f8222012-10-08 16:33:24 -07001797 node_set(i, NODE_DATA(nid)->reclaim_nodes);
David Rientjes6b187d022012-10-25 13:38:08 -07001798 else
David Rientjes957f8222012-10-08 16:33:24 -07001799 zone_reclaim_mode = 1;
David Rientjes957f8222012-10-08 16:33:24 -07001800}
1801
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001802#else /* CONFIG_NUMA */
1803
1804static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
1805{
1806 return NULL;
1807}
1808
Mel Gormandd1a2392008-04-28 02:12:17 -07001809static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zoneref *z,
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001810 nodemask_t *allowednodes)
1811{
1812 return 1;
1813}
1814
Mel Gormandd1a2392008-04-28 02:12:17 -07001815static void zlc_mark_zone_full(struct zonelist *zonelist, struct zoneref *z)
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001816{
1817}
Mel Gorman76d3fbf2011-07-25 17:12:30 -07001818
1819static void zlc_clear_zones_full(struct zonelist *zonelist)
1820{
1821}
David Rientjes957f8222012-10-08 16:33:24 -07001822
1823static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone)
1824{
1825 return true;
1826}
1827
1828static inline void init_zone_allows_reclaim(int nid)
1829{
1830}
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001831#endif /* CONFIG_NUMA */
1832
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001833/*
Paul Jackson0798e512006-12-06 20:31:38 -08001834 * get_page_from_freelist goes through the zonelist trying to allocate
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001835 * a page.
1836 */
1837static struct page *
Mel Gorman19770b32008-04-28 02:12:18 -07001838get_page_from_freelist(gfp_t gfp_mask, nodemask_t *nodemask, unsigned int order,
Mel Gorman5117f452009-06-16 15:31:59 -07001839 struct zonelist *zonelist, int high_zoneidx, int alloc_flags,
Mel Gorman3dd28262009-06-16 15:32:00 -07001840 struct zone *preferred_zone, int migratetype)
Martin Hicks753ee722005-06-21 17:14:41 -07001841{
Mel Gormandd1a2392008-04-28 02:12:17 -07001842 struct zoneref *z;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001843 struct page *page = NULL;
Mel Gorman54a6eb52008-04-28 02:12:16 -07001844 int classzone_idx;
Mel Gorman5117f452009-06-16 15:31:59 -07001845 struct zone *zone;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001846 nodemask_t *allowednodes = NULL;/* zonelist_cache approximation */
1847 int zlc_active = 0; /* set if using zonelist_cache */
1848 int did_zlc_setup = 0; /* just call zlc_setup() one time */
Mel Gorman54a6eb52008-04-28 02:12:16 -07001849
Mel Gorman19770b32008-04-28 02:12:18 -07001850 classzone_idx = zone_idx(preferred_zone);
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001851zonelist_scan:
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001852 /*
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001853 * Scan zonelist, looking for a zone with enough free.
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001854 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
1855 */
Mel Gorman19770b32008-04-28 02:12:18 -07001856 for_each_zone_zonelist_nodemask(zone, z, zonelist,
1857 high_zoneidx, nodemask) {
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08001858 if (IS_ENABLED(CONFIG_NUMA) && zlc_active &&
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001859 !zlc_zone_worth_trying(zonelist, z, allowednodes))
1860 continue;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001861 if ((alloc_flags & ALLOC_CPUSET) &&
Paul Jackson02a0e532006-12-13 00:34:25 -08001862 !cpuset_zone_allowed_softwall(zone, gfp_mask))
Mel Gormancd38b112011-07-25 17:12:29 -07001863 continue;
Johannes Weinera756cf52012-01-10 15:07:49 -08001864 /*
1865 * When allocating a page cache page for writing, we
1866 * want to get it from a zone that is within its dirty
1867 * limit, such that no single zone holds more than its
1868 * proportional share of globally allowed dirty pages.
1869 * The dirty limits take into account the zone's
1870 * lowmem reserves and high watermark so that kswapd
1871 * should be able to balance it without having to
1872 * write pages from its LRU list.
1873 *
1874 * This may look like it could increase pressure on
1875 * lower zones by failing allocations in higher zones
1876 * before they are full. But the pages that do spill
1877 * over are limited as the lower zones are protected
1878 * by this very same mechanism. It should not become
1879 * a practical burden to them.
1880 *
1881 * XXX: For now, allow allocations to potentially
1882 * exceed the per-zone dirty limit in the slowpath
1883 * (ALLOC_WMARK_LOW unset) before going into reclaim,
1884 * which is important when on a NUMA setup the allowed
1885 * zones are together not big enough to reach the
1886 * global limit. The proper fix for these situations
1887 * will require awareness of zones in the
1888 * dirty-throttling and the flusher threads.
1889 */
1890 if ((alloc_flags & ALLOC_WMARK_LOW) &&
1891 (gfp_mask & __GFP_WRITE) && !zone_dirty_ok(zone))
1892 goto this_zone_full;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001893
Mel Gorman41858962009-06-16 15:32:12 -07001894 BUILD_BUG_ON(ALLOC_NO_WATERMARKS < NR_WMARK);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001895 if (!(alloc_flags & ALLOC_NO_WATERMARKS)) {
Nick Piggin31488902005-11-28 13:44:03 -08001896 unsigned long mark;
Mel Gormanfa5e0842009-06-16 15:33:22 -07001897 int ret;
1898
Mel Gorman41858962009-06-16 15:32:12 -07001899 mark = zone->watermark[alloc_flags & ALLOC_WMARK_MASK];
Mel Gormanfa5e0842009-06-16 15:33:22 -07001900 if (zone_watermark_ok(zone, order, mark,
1901 classzone_idx, alloc_flags))
1902 goto try_this_zone;
1903
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08001904 if (IS_ENABLED(CONFIG_NUMA) &&
1905 !did_zlc_setup && nr_online_nodes > 1) {
Mel Gormancd38b112011-07-25 17:12:29 -07001906 /*
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
David Rientjes957f8222012-10-08 16:33:24 -07001916 if (zone_reclaim_mode == 0 ||
1917 !zone_allows_reclaim(preferred_zone, zone))
Mel Gormanfa5e0842009-06-16 15:33:22 -07001918 goto this_zone_full;
1919
Mel Gormancd38b112011-07-25 17:12:29 -07001920 /*
1921 * As we may have just activated ZLC, check if the first
1922 * eligible zone has failed zone_reclaim recently.
1923 */
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08001924 if (IS_ENABLED(CONFIG_NUMA) && zlc_active &&
Mel Gormancd38b112011-07-25 17:12:29 -07001925 !zlc_zone_worth_trying(zonelist, z, allowednodes))
1926 continue;
1927
Mel Gormanfa5e0842009-06-16 15:33:22 -07001928 ret = zone_reclaim(zone, gfp_mask, order);
1929 switch (ret) {
1930 case ZONE_RECLAIM_NOSCAN:
1931 /* did not scan */
Mel Gormancd38b112011-07-25 17:12:29 -07001932 continue;
Mel Gormanfa5e0842009-06-16 15:33:22 -07001933 case ZONE_RECLAIM_FULL:
1934 /* scanned but unreclaimable */
Mel Gormancd38b112011-07-25 17:12:29 -07001935 continue;
Mel Gormanfa5e0842009-06-16 15:33:22 -07001936 default:
1937 /* did we reclaim enough */
1938 if (!zone_watermark_ok(zone, order, mark,
1939 classzone_idx, alloc_flags))
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001940 goto this_zone_full;
Paul Jackson0798e512006-12-06 20:31:38 -08001941 }
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001942 }
1943
Mel Gormanfa5e0842009-06-16 15:33:22 -07001944try_this_zone:
Mel Gorman3dd28262009-06-16 15:32:00 -07001945 page = buffered_rmqueue(preferred_zone, zone, order,
1946 gfp_mask, migratetype);
Paul Jackson0798e512006-12-06 20:31:38 -08001947 if (page)
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001948 break;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001949this_zone_full:
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08001950 if (IS_ENABLED(CONFIG_NUMA))
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001951 zlc_mark_zone_full(zonelist, z);
Mel Gorman54a6eb52008-04-28 02:12:16 -07001952 }
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001953
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08001954 if (unlikely(IS_ENABLED(CONFIG_NUMA) && page == NULL && zlc_active)) {
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001955 /* Disable zlc cache for second zonelist scan */
1956 zlc_active = 0;
1957 goto zonelist_scan;
1958 }
Alex Shib1211862012-08-21 16:16:08 -07001959
1960 if (page)
1961 /*
1962 * page->pfmemalloc is set when ALLOC_NO_WATERMARKS was
1963 * necessary to allocate the page. The expectation is
1964 * that the caller is taking steps that will free more
1965 * memory. The caller should avoid the page being used
1966 * for !PFMEMALLOC purposes.
1967 */
1968 page->pfmemalloc = !!(alloc_flags & ALLOC_NO_WATERMARKS);
1969
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001970 return page;
Martin Hicks753ee722005-06-21 17:14:41 -07001971}
1972
David Rientjes29423e772011-03-22 16:30:47 -07001973/*
1974 * Large machines with many possible nodes should not always dump per-node
1975 * meminfo in irq context.
1976 */
1977static inline bool should_suppress_show_mem(void)
1978{
1979 bool ret = false;
1980
1981#if NODES_SHIFT > 8
1982 ret = in_interrupt();
1983#endif
1984 return ret;
1985}
1986
Dave Hansena238ab52011-05-24 17:12:16 -07001987static DEFINE_RATELIMIT_STATE(nopage_rs,
1988 DEFAULT_RATELIMIT_INTERVAL,
1989 DEFAULT_RATELIMIT_BURST);
1990
1991void warn_alloc_failed(gfp_t gfp_mask, int order, const char *fmt, ...)
1992{
Dave Hansena238ab52011-05-24 17:12:16 -07001993 unsigned int filter = SHOW_MEM_FILTER_NODES;
1994
Stanislaw Gruszkac0a32fc52012-01-10 15:07:28 -08001995 if ((gfp_mask & __GFP_NOWARN) || !__ratelimit(&nopage_rs) ||
1996 debug_guardpage_minorder() > 0)
Dave Hansena238ab52011-05-24 17:12:16 -07001997 return;
1998
1999 /*
2000 * This documents exceptions given to allocations in certain
2001 * contexts that are allowed to allocate outside current's set
2002 * of allowed nodes.
2003 */
2004 if (!(gfp_mask & __GFP_NOMEMALLOC))
2005 if (test_thread_flag(TIF_MEMDIE) ||
2006 (current->flags & (PF_MEMALLOC | PF_EXITING)))
2007 filter &= ~SHOW_MEM_FILTER_NODES;
2008 if (in_interrupt() || !(gfp_mask & __GFP_WAIT))
2009 filter &= ~SHOW_MEM_FILTER_NODES;
2010
2011 if (fmt) {
Joe Perches3ee9a4f2011-10-31 17:08:35 -07002012 struct va_format vaf;
2013 va_list args;
2014
Dave Hansena238ab52011-05-24 17:12:16 -07002015 va_start(args, fmt);
Joe Perches3ee9a4f2011-10-31 17:08:35 -07002016
2017 vaf.fmt = fmt;
2018 vaf.va = &args;
2019
2020 pr_warn("%pV", &vaf);
2021
Dave Hansena238ab52011-05-24 17:12:16 -07002022 va_end(args);
2023 }
2024
Joe Perches3ee9a4f2011-10-31 17:08:35 -07002025 pr_warn("%s: page allocation failure: order:%d, mode:0x%x\n",
2026 current->comm, order, gfp_mask);
Dave Hansena238ab52011-05-24 17:12:16 -07002027
2028 dump_stack();
2029 if (!should_suppress_show_mem())
2030 show_mem(filter);
2031}
2032
Mel Gorman11e33f62009-06-16 15:31:57 -07002033static inline int
2034should_alloc_retry(gfp_t gfp_mask, unsigned int order,
Mel Gormanf90ac392012-01-10 15:07:15 -08002035 unsigned long did_some_progress,
Mel Gorman11e33f62009-06-16 15:31:57 -07002036 unsigned long pages_reclaimed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
Mel Gorman11e33f62009-06-16 15:31:57 -07002038 /* Do not loop if specifically requested */
2039 if (gfp_mask & __GFP_NORETRY)
2040 return 0;
2041
Mel Gormanf90ac392012-01-10 15:07:15 -08002042 /* Always retry if specifically requested */
2043 if (gfp_mask & __GFP_NOFAIL)
2044 return 1;
2045
2046 /*
2047 * Suspend converts GFP_KERNEL to __GFP_WAIT which can prevent reclaim
2048 * making forward progress without invoking OOM. Suspend also disables
2049 * storage devices so kswapd will not help. Bail if we are suspending.
2050 */
2051 if (!did_some_progress && pm_suspended_storage())
2052 return 0;
2053
Mel Gorman11e33f62009-06-16 15:31:57 -07002054 /*
2055 * In this implementation, order <= PAGE_ALLOC_COSTLY_ORDER
2056 * means __GFP_NOFAIL, but that may not be true in other
2057 * implementations.
2058 */
2059 if (order <= PAGE_ALLOC_COSTLY_ORDER)
2060 return 1;
2061
2062 /*
2063 * For order > PAGE_ALLOC_COSTLY_ORDER, if __GFP_REPEAT is
2064 * specified, then we retry until we no longer reclaim any pages
2065 * (above), or we've reclaimed an order of pages at least as
2066 * large as the allocation's order. In both cases, if the
2067 * allocation still fails, we stop retrying.
2068 */
2069 if (gfp_mask & __GFP_REPEAT && pages_reclaimed < (1 << order))
2070 return 1;
2071
Mel Gorman11e33f62009-06-16 15:31:57 -07002072 return 0;
2073}
2074
2075static inline struct page *
2076__alloc_pages_may_oom(gfp_t gfp_mask, unsigned int order,
2077 struct zonelist *zonelist, enum zone_type high_zoneidx,
Mel Gorman3dd28262009-06-16 15:32:00 -07002078 nodemask_t *nodemask, struct zone *preferred_zone,
2079 int migratetype)
Mel Gorman11e33f62009-06-16 15:31:57 -07002080{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Mel Gorman11e33f62009-06-16 15:31:57 -07002083 /* Acquire the OOM killer lock for the zones in zonelist */
Minchan Kimff321fe2010-08-09 17:18:57 -07002084 if (!try_set_zonelist_oom(zonelist, gfp_mask)) {
Mel Gorman11e33f62009-06-16 15:31:57 -07002085 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 return NULL;
2087 }
Jens Axboe6b1de912005-11-17 21:35:02 +01002088
Mel Gorman11e33f62009-06-16 15:31:57 -07002089 /*
2090 * Go through the zonelist yet one more time, keep very high watermark
2091 * here, this is only to catch a parallel oom killing, we must fail if
2092 * we're still under heavy pressure.
2093 */
2094 page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, nodemask,
2095 order, zonelist, high_zoneidx,
Mel Gorman5117f452009-06-16 15:31:59 -07002096 ALLOC_WMARK_HIGH|ALLOC_CPUSET,
Mel Gorman3dd28262009-06-16 15:32:00 -07002097 preferred_zone, migratetype);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002098 if (page)
Mel Gorman11e33f62009-06-16 15:31:57 -07002099 goto out;
2100
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -08002101 if (!(gfp_mask & __GFP_NOFAIL)) {
2102 /* The OOM killer will not help higher order allocs */
2103 if (order > PAGE_ALLOC_COSTLY_ORDER)
2104 goto out;
David Rientjes03668b32010-08-09 17:18:54 -07002105 /* The OOM killer does not needlessly kill tasks for lowmem */
2106 if (high_zoneidx < ZONE_NORMAL)
2107 goto out;
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -08002108 /*
2109 * GFP_THISNODE contains __GFP_NORETRY and we never hit this.
2110 * Sanity check for bare calls of __GFP_THISNODE, not real OOM.
2111 * The caller should handle page allocation failure by itself if
2112 * it specifies __GFP_THISNODE.
2113 * Note: Hugepage uses it but will hit PAGE_ALLOC_COSTLY_ORDER.
2114 */
2115 if (gfp_mask & __GFP_THISNODE)
2116 goto out;
2117 }
Mel Gorman11e33f62009-06-16 15:31:57 -07002118 /* Exhausted what can be done so it's blamo time */
David Rientjes08ab9b12012-03-21 16:34:04 -07002119 out_of_memory(zonelist, gfp_mask, order, nodemask, false);
Mel Gorman11e33f62009-06-16 15:31:57 -07002120
2121out:
2122 clear_zonelist_oom(zonelist, gfp_mask);
2123 return page;
2124}
2125
Mel Gorman56de7262010-05-24 14:32:30 -07002126#ifdef CONFIG_COMPACTION
2127/* Try memory compaction for high-order allocations before reclaim */
2128static struct page *
2129__alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
2130 struct zonelist *zonelist, enum zone_type high_zoneidx,
2131 nodemask_t *nodemask, int alloc_flags, struct zone *preferred_zone,
Mel Gorman66199712012-01-12 17:19:41 -08002132 int migratetype, bool sync_migration,
Mel Gormanc67fe372012-08-21 16:16:17 -07002133 bool *contended_compaction, bool *deferred_compaction,
Mel Gorman66199712012-01-12 17:19:41 -08002134 unsigned long *did_some_progress)
Mel Gorman56de7262010-05-24 14:32:30 -07002135{
Mel Gorman66199712012-01-12 17:19:41 -08002136 if (!order)
Mel Gorman56de7262010-05-24 14:32:30 -07002137 return NULL;
2138
Rik van Rielaff62242012-03-21 16:33:52 -07002139 if (compaction_deferred(preferred_zone, order)) {
Mel Gorman66199712012-01-12 17:19:41 -08002140 *deferred_compaction = true;
2141 return NULL;
2142 }
2143
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08002144 current->flags |= PF_MEMALLOC;
Mel Gorman56de7262010-05-24 14:32:30 -07002145 *did_some_progress = try_to_compact_pages(zonelist, order, gfp_mask,
Mel Gormanc67fe372012-08-21 16:16:17 -07002146 nodemask, sync_migration,
Mel Gorman8fb74b92013-01-11 14:32:16 -08002147 contended_compaction);
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08002148 current->flags &= ~PF_MEMALLOC;
Mel Gorman56de7262010-05-24 14:32:30 -07002149
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07002150 if (*did_some_progress != COMPACT_SKIPPED) {
Mel Gorman8fb74b92013-01-11 14:32:16 -08002151 struct page *page;
2152
Mel Gorman56de7262010-05-24 14:32:30 -07002153 /* Page migration frees to the PCP lists but we want merging */
2154 drain_pages(get_cpu());
2155 put_cpu();
2156
2157 page = get_page_from_freelist(gfp_mask, nodemask,
2158 order, zonelist, high_zoneidx,
Mel Gormancfd19c52012-07-31 16:44:10 -07002159 alloc_flags & ~ALLOC_NO_WATERMARKS,
2160 preferred_zone, migratetype);
Mel Gorman56de7262010-05-24 14:32:30 -07002161 if (page) {
Mel Gorman62997022012-10-08 16:32:47 -07002162 preferred_zone->compact_blockskip_flush = false;
Mel Gorman4f92e252010-05-24 14:32:32 -07002163 preferred_zone->compact_considered = 0;
2164 preferred_zone->compact_defer_shift = 0;
Rik van Rielaff62242012-03-21 16:33:52 -07002165 if (order >= preferred_zone->compact_order_failed)
2166 preferred_zone->compact_order_failed = order + 1;
Mel Gorman56de7262010-05-24 14:32:30 -07002167 count_vm_event(COMPACTSUCCESS);
2168 return page;
2169 }
2170
2171 /*
2172 * It's bad if compaction run occurs and fails.
2173 * The most likely reason is that pages exist,
2174 * but not enough to satisfy watermarks.
2175 */
2176 count_vm_event(COMPACTFAIL);
Mel Gorman66199712012-01-12 17:19:41 -08002177
2178 /*
2179 * As async compaction considers a subset of pageblocks, only
2180 * defer if the failure was a sync compaction failure.
2181 */
2182 if (sync_migration)
Rik van Rielaff62242012-03-21 16:33:52 -07002183 defer_compaction(preferred_zone, order);
Mel Gorman56de7262010-05-24 14:32:30 -07002184
2185 cond_resched();
2186 }
2187
2188 return NULL;
2189}
2190#else
2191static inline struct page *
2192__alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
2193 struct zonelist *zonelist, enum zone_type high_zoneidx,
2194 nodemask_t *nodemask, int alloc_flags, struct zone *preferred_zone,
Mel Gorman66199712012-01-12 17:19:41 -08002195 int migratetype, bool sync_migration,
Mel Gormanc67fe372012-08-21 16:16:17 -07002196 bool *contended_compaction, bool *deferred_compaction,
Mel Gorman66199712012-01-12 17:19:41 -08002197 unsigned long *did_some_progress)
Mel Gorman56de7262010-05-24 14:32:30 -07002198{
2199 return NULL;
2200}
2201#endif /* CONFIG_COMPACTION */
2202
Marek Szyprowskibba90712012-01-25 12:09:52 +01002203/* Perform direct synchronous page reclaim */
2204static int
2205__perform_reclaim(gfp_t gfp_mask, unsigned int order, struct zonelist *zonelist,
2206 nodemask_t *nodemask)
Mel Gorman11e33f62009-06-16 15:31:57 -07002207{
Mel Gorman11e33f62009-06-16 15:31:57 -07002208 struct reclaim_state reclaim_state;
Marek Szyprowskibba90712012-01-25 12:09:52 +01002209 int progress;
Mel Gorman11e33f62009-06-16 15:31:57 -07002210
2211 cond_resched();
2212
2213 /* We now go into synchronous reclaim */
2214 cpuset_memory_pressure_bump();
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08002215 current->flags |= PF_MEMALLOC;
Mel Gorman11e33f62009-06-16 15:31:57 -07002216 lockdep_set_current_reclaim_state(gfp_mask);
2217 reclaim_state.reclaimed_slab = 0;
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08002218 current->reclaim_state = &reclaim_state;
Mel Gorman11e33f62009-06-16 15:31:57 -07002219
Marek Szyprowskibba90712012-01-25 12:09:52 +01002220 progress = try_to_free_pages(zonelist, order, gfp_mask, nodemask);
Mel Gorman11e33f62009-06-16 15:31:57 -07002221
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08002222 current->reclaim_state = NULL;
Mel Gorman11e33f62009-06-16 15:31:57 -07002223 lockdep_clear_current_reclaim_state();
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08002224 current->flags &= ~PF_MEMALLOC;
Mel Gorman11e33f62009-06-16 15:31:57 -07002225
2226 cond_resched();
2227
Marek Szyprowskibba90712012-01-25 12:09:52 +01002228 return progress;
2229}
2230
2231/* The really slow allocator path where we enter direct reclaim */
2232static inline struct page *
2233__alloc_pages_direct_reclaim(gfp_t gfp_mask, unsigned int order,
2234 struct zonelist *zonelist, enum zone_type high_zoneidx,
2235 nodemask_t *nodemask, int alloc_flags, struct zone *preferred_zone,
2236 int migratetype, unsigned long *did_some_progress)
2237{
2238 struct page *page = NULL;
2239 bool drained = false;
2240
2241 *did_some_progress = __perform_reclaim(gfp_mask, order, zonelist,
2242 nodemask);
Mel Gorman9ee493c2010-09-09 16:38:18 -07002243 if (unlikely(!(*did_some_progress)))
2244 return NULL;
Mel Gorman11e33f62009-06-16 15:31:57 -07002245
Mel Gorman76d3fbf2011-07-25 17:12:30 -07002246 /* After successful reclaim, reconsider all zones for allocation */
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08002247 if (IS_ENABLED(CONFIG_NUMA))
Mel Gorman76d3fbf2011-07-25 17:12:30 -07002248 zlc_clear_zones_full(zonelist);
2249
Mel Gorman9ee493c2010-09-09 16:38:18 -07002250retry:
2251 page = get_page_from_freelist(gfp_mask, nodemask, order,
Mel Gorman5117f452009-06-16 15:31:59 -07002252 zonelist, high_zoneidx,
Mel Gormancfd19c52012-07-31 16:44:10 -07002253 alloc_flags & ~ALLOC_NO_WATERMARKS,
2254 preferred_zone, migratetype);
Mel Gorman9ee493c2010-09-09 16:38:18 -07002255
2256 /*
2257 * If an allocation failed after direct reclaim, it could be because
2258 * pages are pinned on the per-cpu lists. Drain them and try again
2259 */
2260 if (!page && !drained) {
2261 drain_all_pages();
2262 drained = true;
2263 goto retry;
2264 }
2265
Mel Gorman11e33f62009-06-16 15:31:57 -07002266 return page;
2267}
2268
Mel Gorman11e33f62009-06-16 15:31:57 -07002269/*
2270 * This is called in the allocator slow-path if the allocation request is of
2271 * sufficient urgency to ignore watermarks and take other desperate measures
2272 */
2273static inline struct page *
2274__alloc_pages_high_priority(gfp_t gfp_mask, unsigned int order,
2275 struct zonelist *zonelist, enum zone_type high_zoneidx,
Mel Gorman3dd28262009-06-16 15:32:00 -07002276 nodemask_t *nodemask, struct zone *preferred_zone,
2277 int migratetype)
Mel Gorman11e33f62009-06-16 15:31:57 -07002278{
2279 struct page *page;
2280
2281 do {
2282 page = get_page_from_freelist(gfp_mask, nodemask, order,
Mel Gorman5117f452009-06-16 15:31:59 -07002283 zonelist, high_zoneidx, ALLOC_NO_WATERMARKS,
Mel Gorman3dd28262009-06-16 15:32:00 -07002284 preferred_zone, migratetype);
Mel Gorman11e33f62009-06-16 15:31:57 -07002285
2286 if (!page && gfp_mask & __GFP_NOFAIL)
Mel Gorman0e093d992010-10-26 14:21:45 -07002287 wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/50);
Mel Gorman11e33f62009-06-16 15:31:57 -07002288 } while (!page && (gfp_mask & __GFP_NOFAIL));
2289
2290 return page;
2291}
2292
2293static inline
2294void wake_all_kswapd(unsigned int order, struct zonelist *zonelist,
Mel Gorman99504742011-01-13 15:46:20 -08002295 enum zone_type high_zoneidx,
2296 enum zone_type classzone_idx)
Mel Gorman11e33f62009-06-16 15:31:57 -07002297{
2298 struct zoneref *z;
2299 struct zone *zone;
2300
2301 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx)
Mel Gorman99504742011-01-13 15:46:20 -08002302 wakeup_kswapd(zone, order, classzone_idx);
Mel Gorman11e33f62009-06-16 15:31:57 -07002303}
2304
Peter Zijlstra341ce062009-06-16 15:32:02 -07002305static inline int
2306gfp_to_alloc_flags(gfp_t gfp_mask)
2307{
Peter Zijlstra341ce062009-06-16 15:32:02 -07002308 int alloc_flags = ALLOC_WMARK_MIN | ALLOC_CPUSET;
2309 const gfp_t wait = gfp_mask & __GFP_WAIT;
2310
Mel Gormana56f57f2009-06-16 15:32:02 -07002311 /* __GFP_HIGH is assumed to be the same as ALLOC_HIGH to save a branch. */
Namhyung Kime6223a32010-10-26 14:21:59 -07002312 BUILD_BUG_ON(__GFP_HIGH != (__force gfp_t) ALLOC_HIGH);
Mel Gormana56f57f2009-06-16 15:32:02 -07002313
Peter Zijlstra341ce062009-06-16 15:32:02 -07002314 /*
2315 * The caller may dip into page reserves a bit more if the caller
2316 * cannot run direct reclaim, or if the caller has realtime scheduling
2317 * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will
2318 * set both ALLOC_HARDER (!wait) and ALLOC_HIGH (__GFP_HIGH).
2319 */
Namhyung Kime6223a32010-10-26 14:21:59 -07002320 alloc_flags |= (__force int) (gfp_mask & __GFP_HIGH);
Peter Zijlstra341ce062009-06-16 15:32:02 -07002321
2322 if (!wait) {
Andrea Arcangeli5c3240d2011-01-13 15:46:49 -08002323 /*
2324 * Not worth trying to allocate harder for
2325 * __GFP_NOMEMALLOC even if it can't schedule.
2326 */
2327 if (!(gfp_mask & __GFP_NOMEMALLOC))
2328 alloc_flags |= ALLOC_HARDER;
Peter Zijlstra341ce062009-06-16 15:32:02 -07002329 /*
2330 * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
2331 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
2332 */
2333 alloc_flags &= ~ALLOC_CPUSET;
Andrew Mortonc06b1fc2011-01-13 15:47:32 -08002334 } else if (unlikely(rt_task(current)) && !in_interrupt())
Peter Zijlstra341ce062009-06-16 15:32:02 -07002335 alloc_flags |= ALLOC_HARDER;
2336
Mel Gormanb37f1dd2012-07-31 16:44:03 -07002337 if (likely(!(gfp_mask & __GFP_NOMEMALLOC))) {
2338 if (gfp_mask & __GFP_MEMALLOC)
2339 alloc_flags |= ALLOC_NO_WATERMARKS;
Mel Gorman907aed42012-07-31 16:44:07 -07002340 else if (in_serving_softirq() && (current->flags & PF_MEMALLOC))
2341 alloc_flags |= ALLOC_NO_WATERMARKS;
2342 else if (!in_interrupt() &&
2343 ((current->flags & PF_MEMALLOC) ||
2344 unlikely(test_thread_flag(TIF_MEMDIE))))
Peter Zijlstra341ce062009-06-16 15:32:02 -07002345 alloc_flags |= ALLOC_NO_WATERMARKS;
2346 }
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07002347#ifdef CONFIG_CMA
2348 if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
2349 alloc_flags |= ALLOC_CMA;
2350#endif
Peter Zijlstra341ce062009-06-16 15:32:02 -07002351 return alloc_flags;
2352}
2353
Mel Gorman072bb0a2012-07-31 16:43:58 -07002354bool gfp_pfmemalloc_allowed(gfp_t gfp_mask)
2355{
Mel Gormanb37f1dd2012-07-31 16:44:03 -07002356 return !!(gfp_to_alloc_flags(gfp_mask) & ALLOC_NO_WATERMARKS);
Mel Gorman072bb0a2012-07-31 16:43:58 -07002357}
2358
Mel Gorman11e33f62009-06-16 15:31:57 -07002359static inline struct page *
2360__alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
2361 struct zonelist *zonelist, enum zone_type high_zoneidx,
Mel Gorman3dd28262009-06-16 15:32:00 -07002362 nodemask_t *nodemask, struct zone *preferred_zone,
2363 int migratetype)
Mel Gorman11e33f62009-06-16 15:31:57 -07002364{
2365 const gfp_t wait = gfp_mask & __GFP_WAIT;
2366 struct page *page = NULL;
2367 int alloc_flags;
2368 unsigned long pages_reclaimed = 0;
2369 unsigned long did_some_progress;
Mel Gorman77f1fe62011-01-13 15:45:57 -08002370 bool sync_migration = false;
Mel Gorman66199712012-01-12 17:19:41 -08002371 bool deferred_compaction = false;
Mel Gormanc67fe372012-08-21 16:16:17 -07002372 bool contended_compaction = false;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002373
Christoph Lameter952f3b52006-12-06 20:33:26 -08002374 /*
Mel Gorman72807a72009-06-16 15:32:18 -07002375 * In the slowpath, we sanity check order to avoid ever trying to
2376 * reclaim >= MAX_ORDER areas which will never succeed. Callers may
2377 * be using allocators in order of preference for an area that is
2378 * too large.
2379 */
Mel Gorman1fc28b72009-07-29 15:04:08 -07002380 if (order >= MAX_ORDER) {
2381 WARN_ON_ONCE(!(gfp_mask & __GFP_NOWARN));
Mel Gorman72807a72009-06-16 15:32:18 -07002382 return NULL;
Mel Gorman1fc28b72009-07-29 15:04:08 -07002383 }
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08002384
Christoph Lameter952f3b52006-12-06 20:33:26 -08002385 /*
2386 * GFP_THISNODE (meaning __GFP_THISNODE, __GFP_NORETRY and
2387 * __GFP_NOWARN set) should not cause reclaim since the subsystem
2388 * (f.e. slab) using GFP_THISNODE may choose to trigger reclaim
2389 * using a larger set of nodes after it has established that the
2390 * allowed per node queues are empty and that nodes are
2391 * over allocated.
2392 */
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08002393 if (IS_ENABLED(CONFIG_NUMA) &&
2394 (gfp_mask & GFP_THISNODE) == GFP_THISNODE)
Christoph Lameter952f3b52006-12-06 20:33:26 -08002395 goto nopage;
2396
Mel Gormancc4a6852009-11-11 14:26:14 -08002397restart:
Linus Torvaldscaf49192012-12-10 10:51:16 -08002398 if (!(gfp_mask & __GFP_NO_KSWAPD))
2399 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
Linus Torvalds31f8d422012-12-10 10:47:45 -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
2473 * requested a movable allocation that does not heavily disrupt the
2474 * system then fail the allocation instead of entering direct reclaim.
2475 */
2476 if ((deferred_compaction || contended_compaction) &&
Linus Torvaldscaf49192012-12-10 10:51:16 -08002477 (gfp_mask & __GFP_NO_KSWAPD))
Linus Torvalds31f8d422012-12-10 10:47:45 -08002478 goto nopage;
Mel Gorman66199712012-01-12 17:19:41 -08002479
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;
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07002577 int alloc_flags = ALLOC_WMARK_LOW|ALLOC_CPUSET;
Glauber Costa6a1a0d32012-12-18 14:22:00 -08002578 struct mem_cgroup *memcg = NULL;
Mel Gorman11e33f62009-06-16 15:31:57 -07002579
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +10002580 gfp_mask &= gfp_allowed_mask;
2581
Mel Gorman11e33f62009-06-16 15:31:57 -07002582 lockdep_trace_alloc(gfp_mask);
2583
2584 might_sleep_if(gfp_mask & __GFP_WAIT);
2585
2586 if (should_fail_alloc_page(gfp_mask, order))
2587 return NULL;
2588
2589 /*
2590 * Check the zones suitable for the gfp_mask contain at least one
2591 * valid zone. It's possible to have an empty zonelist as a result
2592 * of GFP_THISNODE and a memoryless node
2593 */
2594 if (unlikely(!zonelist->_zonerefs->zone))
2595 return NULL;
2596
Glauber Costa6a1a0d32012-12-18 14:22:00 -08002597 /*
2598 * Will only have any effect when __GFP_KMEMCG is set. This is
2599 * verified in the (always inline) callee
2600 */
2601 if (!memcg_kmem_newpage_charge(gfp_mask, &memcg, order))
2602 return NULL;
2603
Mel Gormancc9a6c82012-03-21 16:34:11 -07002604retry_cpuset:
2605 cpuset_mems_cookie = get_mems_allowed();
2606
Mel Gorman5117f452009-06-16 15:31:59 -07002607 /* The preferred zone is used for statistics later */
David Rientjesf33261d2011-01-25 15:07:20 -08002608 first_zones_zonelist(zonelist, high_zoneidx,
2609 nodemask ? : &cpuset_current_mems_allowed,
2610 &preferred_zone);
Mel Gormancc9a6c82012-03-21 16:34:11 -07002611 if (!preferred_zone)
2612 goto out;
Mel Gorman5117f452009-06-16 15:31:59 -07002613
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07002614#ifdef CONFIG_CMA
2615 if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
2616 alloc_flags |= ALLOC_CMA;
2617#endif
Mel Gorman5117f452009-06-16 15:31:59 -07002618 /* First allocation attempt */
Mel Gorman11e33f62009-06-16 15:31:57 -07002619 page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, nodemask, order,
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07002620 zonelist, high_zoneidx, alloc_flags,
Mel Gorman3dd28262009-06-16 15:32:00 -07002621 preferred_zone, migratetype);
Mel Gorman11e33f62009-06-16 15:31:57 -07002622 if (unlikely(!page))
2623 page = __alloc_pages_slowpath(gfp_mask, order,
Mel Gorman5117f452009-06-16 15:31:59 -07002624 zonelist, high_zoneidx, nodemask,
Mel Gorman3dd28262009-06-16 15:32:00 -07002625 preferred_zone, migratetype);
Mel Gorman11e33f62009-06-16 15:31:57 -07002626
Mel Gorman4b4f2782009-09-21 17:02:41 -07002627 trace_mm_page_alloc(page, order, gfp_mask, migratetype);
Mel Gormancc9a6c82012-03-21 16:34:11 -07002628
2629out:
2630 /*
2631 * When updating a task's mems_allowed, it is possible to race with
2632 * parallel threads in such a way that an allocation can fail while
2633 * the mask is being updated. If a page allocation is about to fail,
2634 * check if the cpuset changed during allocation and if so, retry.
2635 */
2636 if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !page))
2637 goto retry_cpuset;
2638
Glauber Costa6a1a0d32012-12-18 14:22:00 -08002639 memcg_kmem_commit_charge(page, memcg, order);
2640
Mel Gorman11e33f62009-06-16 15:31:57 -07002641 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642}
Mel Gormand2391712009-06-16 15:31:52 -07002643EXPORT_SYMBOL(__alloc_pages_nodemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644
2645/*
2646 * Common helper functions.
2647 */
Harvey Harrison920c7a52008-02-04 22:29:26 -08002648unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649{
Akinobu Mita945a1112009-09-21 17:01:47 -07002650 struct page *page;
2651
2652 /*
2653 * __get_free_pages() returns a 32-bit address, which cannot represent
2654 * a highmem page
2655 */
2656 VM_BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
2657
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 page = alloc_pages(gfp_mask, order);
2659 if (!page)
2660 return 0;
2661 return (unsigned long) page_address(page);
2662}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663EXPORT_SYMBOL(__get_free_pages);
2664
Harvey Harrison920c7a52008-02-04 22:29:26 -08002665unsigned long get_zeroed_page(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666{
Akinobu Mita945a1112009-09-21 17:01:47 -07002667 return __get_free_pages(gfp_mask | __GFP_ZERO, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669EXPORT_SYMBOL(get_zeroed_page);
2670
Harvey Harrison920c7a52008-02-04 22:29:26 -08002671void __free_pages(struct page *page, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672{
Nick Pigginb5810032005-10-29 18:16:12 -07002673 if (put_page_testzero(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 if (order == 0)
Li Hongfc916682010-03-05 13:41:54 -08002675 free_hot_cold_page(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 else
2677 __free_pages_ok(page, order);
2678 }
2679}
2680
2681EXPORT_SYMBOL(__free_pages);
2682
Harvey Harrison920c7a52008-02-04 22:29:26 -08002683void free_pages(unsigned long addr, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684{
2685 if (addr != 0) {
Nick Piggin725d7042006-09-25 23:30:55 -07002686 VM_BUG_ON(!virt_addr_valid((void *)addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 __free_pages(virt_to_page((void *)addr), order);
2688 }
2689}
2690
2691EXPORT_SYMBOL(free_pages);
2692
Glauber Costa6a1a0d32012-12-18 14:22:00 -08002693/*
2694 * __free_memcg_kmem_pages and free_memcg_kmem_pages will free
2695 * pages allocated with __GFP_KMEMCG.
2696 *
2697 * Those pages are accounted to a particular memcg, embedded in the
2698 * corresponding page_cgroup. To avoid adding a hit in the allocator to search
2699 * for that information only to find out that it is NULL for users who have no
2700 * interest in that whatsoever, we provide these functions.
2701 *
2702 * The caller knows better which flags it relies on.
2703 */
2704void __free_memcg_kmem_pages(struct page *page, unsigned int order)
2705{
2706 memcg_kmem_uncharge_pages(page, order);
2707 __free_pages(page, order);
2708}
2709
2710void free_memcg_kmem_pages(unsigned long addr, unsigned int order)
2711{
2712 if (addr != 0) {
2713 VM_BUG_ON(!virt_addr_valid((void *)addr));
2714 __free_memcg_kmem_pages(virt_to_page((void *)addr), order);
2715 }
2716}
2717
Andi Kleenee85c2e2011-05-11 15:13:34 -07002718static void *make_alloc_exact(unsigned long addr, unsigned order, size_t size)
2719{
2720 if (addr) {
2721 unsigned long alloc_end = addr + (PAGE_SIZE << order);
2722 unsigned long used = addr + PAGE_ALIGN(size);
2723
2724 split_page(virt_to_page((void *)addr), order);
2725 while (used < alloc_end) {
2726 free_page(used);
2727 used += PAGE_SIZE;
2728 }
2729 }
2730 return (void *)addr;
2731}
2732
Timur Tabi2be0ffe2008-07-23 21:28:11 -07002733/**
2734 * alloc_pages_exact - allocate an exact number physically-contiguous pages.
2735 * @size: the number of bytes to allocate
2736 * @gfp_mask: GFP flags for the allocation
2737 *
2738 * This function is similar to alloc_pages(), except that it allocates the
2739 * minimum number of pages to satisfy the request. alloc_pages() can only
2740 * allocate memory in power-of-two pages.
2741 *
2742 * This function is also limited by MAX_ORDER.
2743 *
2744 * Memory allocated by this function must be released by free_pages_exact().
2745 */
2746void *alloc_pages_exact(size_t size, gfp_t gfp_mask)
2747{
2748 unsigned int order = get_order(size);
2749 unsigned long addr;
2750
2751 addr = __get_free_pages(gfp_mask, order);
Andi Kleenee85c2e2011-05-11 15:13:34 -07002752 return make_alloc_exact(addr, order, size);
Timur Tabi2be0ffe2008-07-23 21:28:11 -07002753}
2754EXPORT_SYMBOL(alloc_pages_exact);
2755
2756/**
Andi Kleenee85c2e2011-05-11 15:13:34 -07002757 * alloc_pages_exact_nid - allocate an exact number of physically-contiguous
2758 * pages on a node.
Randy Dunlapb5e6ab52011-05-16 13:16:54 -07002759 * @nid: the preferred node ID where memory should be allocated
Andi Kleenee85c2e2011-05-11 15:13:34 -07002760 * @size: the number of bytes to allocate
2761 * @gfp_mask: GFP flags for the allocation
2762 *
2763 * Like alloc_pages_exact(), but try to allocate on node nid first before falling
2764 * back.
2765 * Note this is not alloc_pages_exact_node() which allocates on a specific node,
2766 * but is not exact.
2767 */
2768void *alloc_pages_exact_nid(int nid, size_t size, gfp_t gfp_mask)
2769{
2770 unsigned order = get_order(size);
2771 struct page *p = alloc_pages_node(nid, gfp_mask, order);
2772 if (!p)
2773 return NULL;
2774 return make_alloc_exact((unsigned long)page_address(p), order, size);
2775}
2776EXPORT_SYMBOL(alloc_pages_exact_nid);
2777
2778/**
Timur Tabi2be0ffe2008-07-23 21:28:11 -07002779 * free_pages_exact - release memory allocated via alloc_pages_exact()
2780 * @virt: the value returned by alloc_pages_exact.
2781 * @size: size of allocation, same value as passed to alloc_pages_exact().
2782 *
2783 * Release the memory allocated by a previous call to alloc_pages_exact.
2784 */
2785void free_pages_exact(void *virt, size_t size)
2786{
2787 unsigned long addr = (unsigned long)virt;
2788 unsigned long end = addr + PAGE_ALIGN(size);
2789
2790 while (addr < end) {
2791 free_page(addr);
2792 addr += PAGE_SIZE;
2793 }
2794}
2795EXPORT_SYMBOL(free_pages_exact);
2796
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797static unsigned int nr_free_zone_pages(int offset)
2798{
Mel Gormandd1a2392008-04-28 02:12:17 -07002799 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07002800 struct zone *zone;
2801
Martin J. Blighe310fd42005-07-29 22:59:18 -07002802 /* Just pick one node, since fallback list is circular */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 unsigned int sum = 0;
2804
Mel Gorman0e884602008-04-28 02:12:14 -07002805 struct zonelist *zonelist = node_zonelist(numa_node_id(), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806
Mel Gorman54a6eb52008-04-28 02:12:16 -07002807 for_each_zone_zonelist(zone, z, zonelist, offset) {
Martin J. Blighe310fd42005-07-29 22:59:18 -07002808 unsigned long size = zone->present_pages;
Mel Gorman41858962009-06-16 15:32:12 -07002809 unsigned long high = high_wmark_pages(zone);
Martin J. Blighe310fd42005-07-29 22:59:18 -07002810 if (size > high)
2811 sum += size - high;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 }
2813
2814 return sum;
2815}
2816
2817/*
2818 * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
2819 */
2820unsigned int nr_free_buffer_pages(void)
2821{
Al Viroaf4ca452005-10-21 02:55:38 -04002822 return nr_free_zone_pages(gfp_zone(GFP_USER));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823}
Meelap Shahc2f1a552007-07-17 04:04:39 -07002824EXPORT_SYMBOL_GPL(nr_free_buffer_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
2826/*
2827 * Amount of free RAM allocatable within all zones
2828 */
2829unsigned int nr_free_pagecache_pages(void)
2830{
Mel Gorman2a1e2742007-07-17 04:03:12 -07002831 return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER_MOVABLE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832}
Christoph Lameter08e0f6a2006-09-27 01:50:06 -07002833
2834static inline void show_node(struct zone *zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835{
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08002836 if (IS_ENABLED(CONFIG_NUMA))
Andy Whitcroft25ba77c2006-12-06 20:33:03 -08002837 printk("Node %d ", zone_to_nid(zone));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840void si_meminfo(struct sysinfo *val)
2841{
2842 val->totalram = totalram_pages;
2843 val->sharedram = 0;
Christoph Lameterd23ad422007-02-10 01:43:02 -08002844 val->freeram = global_page_state(NR_FREE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 val->bufferram = nr_blockdev_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 val->totalhigh = totalhigh_pages;
2847 val->freehigh = nr_free_highpages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 val->mem_unit = PAGE_SIZE;
2849}
2850
2851EXPORT_SYMBOL(si_meminfo);
2852
2853#ifdef CONFIG_NUMA
2854void si_meminfo_node(struct sysinfo *val, int nid)
2855{
2856 pg_data_t *pgdat = NODE_DATA(nid);
2857
2858 val->totalram = pgdat->node_present_pages;
Christoph Lameterd23ad422007-02-10 01:43:02 -08002859 val->freeram = node_page_state(nid, NR_FREE_PAGES);
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07002860#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].present_pages;
Christoph Lameterd23ad422007-02-10 01:43:02 -08002862 val->freehigh = zone_page_state(&pgdat->node_zones[ZONE_HIGHMEM],
2863 NR_FREE_PAGES);
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07002864#else
2865 val->totalhigh = 0;
2866 val->freehigh = 0;
2867#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 val->mem_unit = PAGE_SIZE;
2869}
2870#endif
2871
David Rientjesddd588b2011-03-22 16:30:46 -07002872/*
David Rientjes7bf02ea2011-05-24 17:11:16 -07002873 * Determine whether the node should be displayed or not, depending on whether
2874 * SHOW_MEM_FILTER_NODES was passed to show_free_areas().
David Rientjesddd588b2011-03-22 16:30:46 -07002875 */
David Rientjes7bf02ea2011-05-24 17:11:16 -07002876bool skip_free_areas_node(unsigned int flags, int nid)
David Rientjesddd588b2011-03-22 16:30:46 -07002877{
2878 bool ret = false;
Mel Gormancc9a6c82012-03-21 16:34:11 -07002879 unsigned int cpuset_mems_cookie;
David Rientjesddd588b2011-03-22 16:30:46 -07002880
2881 if (!(flags & SHOW_MEM_FILTER_NODES))
2882 goto out;
2883
Mel Gormancc9a6c82012-03-21 16:34:11 -07002884 do {
2885 cpuset_mems_cookie = get_mems_allowed();
2886 ret = !node_isset(nid, cpuset_current_mems_allowed);
2887 } while (!put_mems_allowed(cpuset_mems_cookie));
David Rientjesddd588b2011-03-22 16:30:46 -07002888out:
2889 return ret;
2890}
2891
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892#define K(x) ((x) << (PAGE_SHIFT-10))
2893
Rabin Vincent377e4f12012-12-11 16:00:24 -08002894static void show_migration_types(unsigned char type)
2895{
2896 static const char types[MIGRATE_TYPES] = {
2897 [MIGRATE_UNMOVABLE] = 'U',
2898 [MIGRATE_RECLAIMABLE] = 'E',
2899 [MIGRATE_MOVABLE] = 'M',
2900 [MIGRATE_RESERVE] = 'R',
2901#ifdef CONFIG_CMA
2902 [MIGRATE_CMA] = 'C',
2903#endif
2904 [MIGRATE_ISOLATE] = 'I',
2905 };
2906 char tmp[MIGRATE_TYPES + 1];
2907 char *p = tmp;
2908 int i;
2909
2910 for (i = 0; i < MIGRATE_TYPES; i++) {
2911 if (type & (1 << i))
2912 *p++ = types[i];
2913 }
2914
2915 *p = '\0';
2916 printk("(%s) ", tmp);
2917}
2918
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919/*
2920 * Show free area list (used inside shift_scroll-lock stuff)
2921 * We also calculate the percentage fragmentation. We do this by counting the
2922 * memory on each free list with the exception of the first item on the list.
David Rientjesddd588b2011-03-22 16:30:46 -07002923 * Suppresses nodes that are not allowed by current's cpuset if
2924 * SHOW_MEM_FILTER_NODES is passed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 */
David Rientjes7bf02ea2011-05-24 17:11:16 -07002926void show_free_areas(unsigned int filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927{
Jes Sorensenc7241912006-09-27 01:50:05 -07002928 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 struct zone *zone;
2930
KOSAKI Motohiroee99c712009-03-31 15:19:31 -07002931 for_each_populated_zone(zone) {
David Rientjes7bf02ea2011-05-24 17:11:16 -07002932 if (skip_free_areas_node(filter, zone_to_nid(zone)))
David Rientjesddd588b2011-03-22 16:30:46 -07002933 continue;
Jes Sorensenc7241912006-09-27 01:50:05 -07002934 show_node(zone);
2935 printk("%s per-cpu:\n", zone->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
Dave Jones6b482c62005-11-10 15:45:56 -05002937 for_each_online_cpu(cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 struct per_cpu_pageset *pageset;
2939
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09002940 pageset = per_cpu_ptr(zone->pageset, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
Christoph Lameter3dfa5722008-02-04 22:29:19 -08002942 printk("CPU %4d: hi:%5d, btch:%4d usd:%4d\n",
2943 cpu, pageset->pcp.high,
2944 pageset->pcp.batch, pageset->pcp.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 }
2946 }
2947
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07002948 printk("active_anon:%lu inactive_anon:%lu isolated_anon:%lu\n"
2949 " active_file:%lu inactive_file:%lu isolated_file:%lu\n"
Lee Schermerhorn7b854122008-10-18 20:26:40 -07002950 " unevictable:%lu"
Andrew Mortonb76146e2009-10-26 16:49:52 -07002951 " dirty:%lu writeback:%lu unstable:%lu\n"
KOSAKI Motohiro3701b032009-09-21 17:01:29 -07002952 " free:%lu slab_reclaimable:%lu slab_unreclaimable:%lu\n"
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -07002953 " mapped:%lu shmem:%lu pagetables:%lu bounce:%lu\n"
2954 " free_cma:%lu\n",
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002955 global_page_state(NR_ACTIVE_ANON),
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002956 global_page_state(NR_INACTIVE_ANON),
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07002957 global_page_state(NR_ISOLATED_ANON),
2958 global_page_state(NR_ACTIVE_FILE),
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002959 global_page_state(NR_INACTIVE_FILE),
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07002960 global_page_state(NR_ISOLATED_FILE),
Lee Schermerhorn7b854122008-10-18 20:26:40 -07002961 global_page_state(NR_UNEVICTABLE),
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -07002962 global_page_state(NR_FILE_DIRTY),
Christoph Lameterce866b32006-06-30 01:55:40 -07002963 global_page_state(NR_WRITEBACK),
Christoph Lameterfd39fc82006-06-30 01:55:40 -07002964 global_page_state(NR_UNSTABLE_NFS),
Christoph Lameterd23ad422007-02-10 01:43:02 -08002965 global_page_state(NR_FREE_PAGES),
KOSAKI Motohiro3701b032009-09-21 17:01:29 -07002966 global_page_state(NR_SLAB_RECLAIMABLE),
2967 global_page_state(NR_SLAB_UNRECLAIMABLE),
Christoph Lameter65ba55f2006-06-30 01:55:34 -07002968 global_page_state(NR_FILE_MAPPED),
KOSAKI Motohiro4b021082009-09-21 17:01:33 -07002969 global_page_state(NR_SHMEM),
Andrew Mortona25700a2007-02-08 14:20:40 -08002970 global_page_state(NR_PAGETABLE),
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -07002971 global_page_state(NR_BOUNCE),
2972 global_page_state(NR_FREE_CMA_PAGES));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
KOSAKI Motohiroee99c712009-03-31 15:19:31 -07002974 for_each_populated_zone(zone) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 int i;
2976
David Rientjes7bf02ea2011-05-24 17:11:16 -07002977 if (skip_free_areas_node(filter, zone_to_nid(zone)))
David Rientjesddd588b2011-03-22 16:30:46 -07002978 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 show_node(zone);
2980 printk("%s"
2981 " free:%lukB"
2982 " min:%lukB"
2983 " low:%lukB"
2984 " high:%lukB"
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002985 " active_anon:%lukB"
2986 " inactive_anon:%lukB"
2987 " active_file:%lukB"
2988 " inactive_file:%lukB"
Lee Schermerhorn7b854122008-10-18 20:26:40 -07002989 " unevictable:%lukB"
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07002990 " isolated(anon):%lukB"
2991 " isolated(file):%lukB"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 " present:%lukB"
Jiang Liu9feedc92012-12-12 13:52:12 -08002993 " managed:%lukB"
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07002994 " mlocked:%lukB"
2995 " dirty:%lukB"
2996 " writeback:%lukB"
2997 " mapped:%lukB"
KOSAKI Motohiro4b021082009-09-21 17:01:33 -07002998 " shmem:%lukB"
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07002999 " slab_reclaimable:%lukB"
3000 " slab_unreclaimable:%lukB"
KOSAKI Motohiroc6a7f572009-09-21 17:01:32 -07003001 " kernel_stack:%lukB"
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07003002 " pagetables:%lukB"
3003 " unstable:%lukB"
3004 " bounce:%lukB"
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -07003005 " free_cma:%lukB"
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07003006 " writeback_tmp:%lukB"
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 " pages_scanned:%lu"
3008 " all_unreclaimable? %s"
3009 "\n",
3010 zone->name,
Mel Gorman88f5acf2011-01-13 15:45:41 -08003011 K(zone_page_state(zone, NR_FREE_PAGES)),
Mel Gorman41858962009-06-16 15:32:12 -07003012 K(min_wmark_pages(zone)),
3013 K(low_wmark_pages(zone)),
3014 K(high_wmark_pages(zone)),
Rik van Riel4f98a2f2008-10-18 20:26:32 -07003015 K(zone_page_state(zone, NR_ACTIVE_ANON)),
3016 K(zone_page_state(zone, NR_INACTIVE_ANON)),
3017 K(zone_page_state(zone, NR_ACTIVE_FILE)),
3018 K(zone_page_state(zone, NR_INACTIVE_FILE)),
Lee Schermerhorn7b854122008-10-18 20:26:40 -07003019 K(zone_page_state(zone, NR_UNEVICTABLE)),
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07003020 K(zone_page_state(zone, NR_ISOLATED_ANON)),
3021 K(zone_page_state(zone, NR_ISOLATED_FILE)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 K(zone->present_pages),
Jiang Liu9feedc92012-12-12 13:52:12 -08003023 K(zone->managed_pages),
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07003024 K(zone_page_state(zone, NR_MLOCK)),
3025 K(zone_page_state(zone, NR_FILE_DIRTY)),
3026 K(zone_page_state(zone, NR_WRITEBACK)),
3027 K(zone_page_state(zone, NR_FILE_MAPPED)),
KOSAKI Motohiro4b021082009-09-21 17:01:33 -07003028 K(zone_page_state(zone, NR_SHMEM)),
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07003029 K(zone_page_state(zone, NR_SLAB_RECLAIMABLE)),
3030 K(zone_page_state(zone, NR_SLAB_UNRECLAIMABLE)),
KOSAKI Motohiroc6a7f572009-09-21 17:01:32 -07003031 zone_page_state(zone, NR_KERNEL_STACK) *
3032 THREAD_SIZE / 1024,
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07003033 K(zone_page_state(zone, NR_PAGETABLE)),
3034 K(zone_page_state(zone, NR_UNSTABLE_NFS)),
3035 K(zone_page_state(zone, NR_BOUNCE)),
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -07003036 K(zone_page_state(zone, NR_FREE_CMA_PAGES)),
KOSAKI Motohiro4a0aa732009-09-21 17:01:30 -07003037 K(zone_page_state(zone, NR_WRITEBACK_TEMP)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 zone->pages_scanned,
KOSAKI Motohiro93e4a892010-03-05 13:41:55 -08003039 (zone->all_unreclaimable ? "yes" : "no")
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 );
3041 printk("lowmem_reserve[]:");
3042 for (i = 0; i < MAX_NR_ZONES; i++)
3043 printk(" %lu", zone->lowmem_reserve[i]);
3044 printk("\n");
3045 }
3046
KOSAKI Motohiroee99c712009-03-31 15:19:31 -07003047 for_each_populated_zone(zone) {
Kirill Korotaev8f9de512006-06-23 02:03:50 -07003048 unsigned long nr[MAX_ORDER], flags, order, total = 0;
Rabin Vincent377e4f12012-12-11 16:00:24 -08003049 unsigned char types[MAX_ORDER];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
David Rientjes7bf02ea2011-05-24 17:11:16 -07003051 if (skip_free_areas_node(filter, zone_to_nid(zone)))
David Rientjesddd588b2011-03-22 16:30:46 -07003052 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 show_node(zone);
3054 printk("%s: ", zone->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055
3056 spin_lock_irqsave(&zone->lock, flags);
3057 for (order = 0; order < MAX_ORDER; order++) {
Rabin Vincent377e4f12012-12-11 16:00:24 -08003058 struct free_area *area = &zone->free_area[order];
3059 int type;
3060
3061 nr[order] = area->nr_free;
Kirill Korotaev8f9de512006-06-23 02:03:50 -07003062 total += nr[order] << order;
Rabin Vincent377e4f12012-12-11 16:00:24 -08003063
3064 types[order] = 0;
3065 for (type = 0; type < MIGRATE_TYPES; type++) {
3066 if (!list_empty(&area->free_list[type]))
3067 types[order] |= 1 << type;
3068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 }
3070 spin_unlock_irqrestore(&zone->lock, flags);
Rabin Vincent377e4f12012-12-11 16:00:24 -08003071 for (order = 0; order < MAX_ORDER; order++) {
Kirill Korotaev8f9de512006-06-23 02:03:50 -07003072 printk("%lu*%lukB ", nr[order], K(1UL) << order);
Rabin Vincent377e4f12012-12-11 16:00:24 -08003073 if (nr[order])
3074 show_migration_types(types[order]);
3075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 printk("= %lukB\n", K(total));
3077 }
3078
Larry Woodmane6f36022008-02-04 22:29:30 -08003079 printk("%ld total pagecache pages\n", global_page_state(NR_FILE_PAGES));
3080
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 show_swap_cache_info();
3082}
3083
Mel Gorman19770b32008-04-28 02:12:18 -07003084static void zoneref_set_zone(struct zone *zone, struct zoneref *zoneref)
3085{
3086 zoneref->zone = zone;
3087 zoneref->zone_idx = zone_idx(zone);
3088}
3089
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090/*
3091 * Builds allocation fallback zone lists.
Christoph Lameter1a932052006-01-06 00:11:16 -08003092 *
3093 * Add all populated zones of a node to the zonelist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003095static int build_zonelists_node(pg_data_t *pgdat, struct zonelist *zonelist,
3096 int nr_zones, enum zone_type zone_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097{
Christoph Lameter1a932052006-01-06 00:11:16 -08003098 struct zone *zone;
3099
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07003100 BUG_ON(zone_type >= MAX_NR_ZONES);
Christoph Lameter2f6726e2006-09-25 23:31:18 -07003101 zone_type++;
Christoph Lameter02a68a52006-01-06 00:11:18 -08003102
3103 do {
Christoph Lameter2f6726e2006-09-25 23:31:18 -07003104 zone_type--;
Christoph Lameter070f8032006-01-06 00:11:19 -08003105 zone = pgdat->node_zones + zone_type;
Christoph Lameter1a932052006-01-06 00:11:16 -08003106 if (populated_zone(zone)) {
Mel Gormandd1a2392008-04-28 02:12:17 -07003107 zoneref_set_zone(zone,
3108 &zonelist->_zonerefs[nr_zones++]);
Christoph Lameter070f8032006-01-06 00:11:19 -08003109 check_highest_zone(zone_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 }
Christoph Lameter02a68a52006-01-06 00:11:18 -08003111
Christoph Lameter2f6726e2006-09-25 23:31:18 -07003112 } while (zone_type);
Christoph Lameter070f8032006-01-06 00:11:19 -08003113 return nr_zones;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114}
3115
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003116
3117/*
3118 * zonelist_order:
3119 * 0 = automatic detection of better ordering.
3120 * 1 = order by ([node] distance, -zonetype)
3121 * 2 = order by (-zonetype, [node] distance)
3122 *
3123 * If not NUMA, ZONELIST_ORDER_ZONE and ZONELIST_ORDER_NODE will create
3124 * the same zonelist. So only NUMA can configure this param.
3125 */
3126#define ZONELIST_ORDER_DEFAULT 0
3127#define ZONELIST_ORDER_NODE 1
3128#define ZONELIST_ORDER_ZONE 2
3129
3130/* zonelist order in the kernel.
3131 * set_zonelist_order() will set this to NODE or ZONE.
3132 */
3133static int current_zonelist_order = ZONELIST_ORDER_DEFAULT;
3134static char zonelist_order_name[3][8] = {"Default", "Node", "Zone"};
3135
3136
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137#ifdef CONFIG_NUMA
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003138/* The value user specified ....changed by config */
3139static int user_zonelist_order = ZONELIST_ORDER_DEFAULT;
3140/* string for sysctl */
3141#define NUMA_ZONELIST_ORDER_LEN 16
3142char numa_zonelist_order[16] = "default";
3143
3144/*
3145 * interface for configure zonelist ordering.
3146 * command line option "numa_zonelist_order"
3147 * = "[dD]efault - default, automatic configuration.
3148 * = "[nN]ode - order by node locality, then by zone within node
3149 * = "[zZ]one - order by zone, then by locality within zone
3150 */
3151
3152static int __parse_numa_zonelist_order(char *s)
3153{
3154 if (*s == 'd' || *s == 'D') {
3155 user_zonelist_order = ZONELIST_ORDER_DEFAULT;
3156 } else if (*s == 'n' || *s == 'N') {
3157 user_zonelist_order = ZONELIST_ORDER_NODE;
3158 } else if (*s == 'z' || *s == 'Z') {
3159 user_zonelist_order = ZONELIST_ORDER_ZONE;
3160 } else {
3161 printk(KERN_WARNING
3162 "Ignoring invalid numa_zonelist_order value: "
3163 "%s\n", s);
3164 return -EINVAL;
3165 }
3166 return 0;
3167}
3168
3169static __init int setup_numa_zonelist_order(char *s)
3170{
Volodymyr G. Lukiianykecb256f2011-01-13 15:46:26 -08003171 int ret;
3172
3173 if (!s)
3174 return 0;
3175
3176 ret = __parse_numa_zonelist_order(s);
3177 if (ret == 0)
3178 strlcpy(numa_zonelist_order, s, NUMA_ZONELIST_ORDER_LEN);
3179
3180 return ret;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003181}
3182early_param("numa_zonelist_order", setup_numa_zonelist_order);
3183
3184/*
3185 * sysctl handler for numa_zonelist_order
3186 */
3187int numa_zonelist_order_handler(ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003188 void __user *buffer, size_t *length,
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003189 loff_t *ppos)
3190{
3191 char saved_string[NUMA_ZONELIST_ORDER_LEN];
3192 int ret;
Andi Kleen443c6f12009-12-23 21:00:47 +01003193 static DEFINE_MUTEX(zl_order_mutex);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003194
Andi Kleen443c6f12009-12-23 21:00:47 +01003195 mutex_lock(&zl_order_mutex);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003196 if (write)
Andi Kleen443c6f12009-12-23 21:00:47 +01003197 strcpy(saved_string, (char*)table->data);
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003198 ret = proc_dostring(table, write, buffer, length, ppos);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003199 if (ret)
Andi Kleen443c6f12009-12-23 21:00:47 +01003200 goto out;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003201 if (write) {
3202 int oldval = user_zonelist_order;
3203 if (__parse_numa_zonelist_order((char*)table->data)) {
3204 /*
3205 * bogus value. restore saved string
3206 */
3207 strncpy((char*)table->data, saved_string,
3208 NUMA_ZONELIST_ORDER_LEN);
3209 user_zonelist_order = oldval;
Haicheng Li4eaf3f62010-05-24 14:32:52 -07003210 } else if (oldval != user_zonelist_order) {
3211 mutex_lock(&zonelists_mutex);
Jiang Liu9adb62a2012-07-31 16:43:28 -07003212 build_all_zonelists(NULL, NULL);
Haicheng Li4eaf3f62010-05-24 14:32:52 -07003213 mutex_unlock(&zonelists_mutex);
3214 }
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003215 }
Andi Kleen443c6f12009-12-23 21:00:47 +01003216out:
3217 mutex_unlock(&zl_order_mutex);
3218 return ret;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003219}
3220
3221
Christoph Lameter62bc62a2009-06-16 15:32:15 -07003222#define MAX_NODE_LOAD (nr_online_nodes)
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003223static int node_load[MAX_NUMNODES];
3224
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225/**
Pavel Pisa4dc3b162005-05-01 08:59:25 -07003226 * 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 -07003227 * @node: node whose fallback list we're appending
3228 * @used_node_mask: nodemask_t of already used nodes
3229 *
3230 * We use a number of factors to determine which is the next node that should
3231 * appear on a given node's fallback list. The node should not have appeared
3232 * already in @node's fallback list, and it should be the next closest node
3233 * according to the distance array (which contains arbitrary distance values
3234 * from each node to each node in the system), and should also prefer nodes
3235 * with no CPUs, since presumably they'll have very little allocation pressure
3236 * on them otherwise.
3237 * It returns -1 if no node is found.
3238 */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003239static int find_next_best_node(int node, nodemask_t *used_node_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240{
Linus Torvalds4cf808e2006-02-17 20:38:21 +01003241 int n, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 int min_val = INT_MAX;
3243 int best_node = -1;
Rusty Russella70f7302009-03-13 14:49:46 +10303244 const struct cpumask *tmp = cpumask_of_node(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245
Linus Torvalds4cf808e2006-02-17 20:38:21 +01003246 /* Use the local node if we haven't already */
3247 if (!node_isset(node, *used_node_mask)) {
3248 node_set(node, *used_node_mask);
3249 return node;
3250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08003252 for_each_node_state(n, N_MEMORY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
3254 /* Don't want a node to appear more than once */
3255 if (node_isset(n, *used_node_mask))
3256 continue;
3257
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 /* Use the distance array to find the distance */
3259 val = node_distance(node, n);
3260
Linus Torvalds4cf808e2006-02-17 20:38:21 +01003261 /* Penalize nodes under us ("prefer the next node") */
3262 val += (n < node);
3263
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 /* Give preference to headless and unused nodes */
Rusty Russella70f7302009-03-13 14:49:46 +10303265 tmp = cpumask_of_node(n);
3266 if (!cpumask_empty(tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 val += PENALTY_FOR_NODE_WITH_CPUS;
3268
3269 /* Slight preference for less loaded node */
3270 val *= (MAX_NODE_LOAD*MAX_NUMNODES);
3271 val += node_load[n];
3272
3273 if (val < min_val) {
3274 min_val = val;
3275 best_node = n;
3276 }
3277 }
3278
3279 if (best_node >= 0)
3280 node_set(best_node, *used_node_mask);
3281
3282 return best_node;
3283}
3284
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003285
3286/*
3287 * Build zonelists ordered by node and zones within node.
3288 * This results in maximum locality--normal zone overflows into local
3289 * DMA zone, if any--but risks exhausting DMA zone.
3290 */
3291static void build_zonelists_in_node_order(pg_data_t *pgdat, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292{
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003293 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 struct zonelist *zonelist;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003295
Mel Gorman54a6eb52008-04-28 02:12:16 -07003296 zonelist = &pgdat->node_zonelists[0];
Mel Gormandd1a2392008-04-28 02:12:17 -07003297 for (j = 0; zonelist->_zonerefs[j].zone != NULL; j++)
Mel Gorman54a6eb52008-04-28 02:12:16 -07003298 ;
3299 j = build_zonelists_node(NODE_DATA(node), zonelist, j,
3300 MAX_NR_ZONES - 1);
Mel Gormandd1a2392008-04-28 02:12:17 -07003301 zonelist->_zonerefs[j].zone = NULL;
3302 zonelist->_zonerefs[j].zone_idx = 0;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003303}
3304
3305/*
Christoph Lameter523b9452007-10-16 01:25:37 -07003306 * Build gfp_thisnode zonelists
3307 */
3308static void build_thisnode_zonelists(pg_data_t *pgdat)
3309{
Christoph Lameter523b9452007-10-16 01:25:37 -07003310 int j;
3311 struct zonelist *zonelist;
3312
Mel Gorman54a6eb52008-04-28 02:12:16 -07003313 zonelist = &pgdat->node_zonelists[1];
3314 j = build_zonelists_node(pgdat, zonelist, 0, MAX_NR_ZONES - 1);
Mel Gormandd1a2392008-04-28 02:12:17 -07003315 zonelist->_zonerefs[j].zone = NULL;
3316 zonelist->_zonerefs[j].zone_idx = 0;
Christoph Lameter523b9452007-10-16 01:25:37 -07003317}
3318
3319/*
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003320 * Build zonelists ordered by zone and nodes within zones.
3321 * This results in conserving DMA zone[s] until all Normal memory is
3322 * exhausted, but results in overflowing to remote node while memory
3323 * may still exist in local DMA zone.
3324 */
3325static int node_order[MAX_NUMNODES];
3326
3327static void build_zonelists_in_zone_order(pg_data_t *pgdat, int nr_nodes)
3328{
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003329 int pos, j, node;
3330 int zone_type; /* needs to be signed */
3331 struct zone *z;
3332 struct zonelist *zonelist;
3333
Mel Gorman54a6eb52008-04-28 02:12:16 -07003334 zonelist = &pgdat->node_zonelists[0];
3335 pos = 0;
3336 for (zone_type = MAX_NR_ZONES - 1; zone_type >= 0; zone_type--) {
3337 for (j = 0; j < nr_nodes; j++) {
3338 node = node_order[j];
3339 z = &NODE_DATA(node)->node_zones[zone_type];
3340 if (populated_zone(z)) {
Mel Gormandd1a2392008-04-28 02:12:17 -07003341 zoneref_set_zone(z,
3342 &zonelist->_zonerefs[pos++]);
Mel Gorman54a6eb52008-04-28 02:12:16 -07003343 check_highest_zone(zone_type);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003344 }
3345 }
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003346 }
Mel Gormandd1a2392008-04-28 02:12:17 -07003347 zonelist->_zonerefs[pos].zone = NULL;
3348 zonelist->_zonerefs[pos].zone_idx = 0;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003349}
3350
3351static int default_zonelist_order(void)
3352{
3353 int nid, zone_type;
3354 unsigned long low_kmem_size,total_size;
3355 struct zone *z;
3356 int average_size;
3357 /*
Thomas Weber88393162010-03-16 11:47:56 +01003358 * ZONE_DMA and ZONE_DMA32 can be very small area in the system.
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003359 * If they are really small and used heavily, the system can fall
3360 * into OOM very easily.
David Rientjese325c902010-05-24 14:32:13 -07003361 * This function detect ZONE_DMA/DMA32 size and configures zone order.
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003362 */
3363 /* Is there ZONE_NORMAL ? (ex. ppc has only DMA zone..) */
3364 low_kmem_size = 0;
3365 total_size = 0;
3366 for_each_online_node(nid) {
3367 for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
3368 z = &NODE_DATA(nid)->node_zones[zone_type];
3369 if (populated_zone(z)) {
3370 if (zone_type < ZONE_NORMAL)
3371 low_kmem_size += z->present_pages;
3372 total_size += z->present_pages;
David Rientjese325c902010-05-24 14:32:13 -07003373 } else if (zone_type == ZONE_NORMAL) {
3374 /*
3375 * If any node has only lowmem, then node order
3376 * is preferred to allow kernel allocations
3377 * locally; otherwise, they can easily infringe
3378 * on other nodes when there is an abundance of
3379 * lowmem available to allocate from.
3380 */
3381 return ZONELIST_ORDER_NODE;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003382 }
3383 }
3384 }
3385 if (!low_kmem_size || /* there are no DMA area. */
3386 low_kmem_size > total_size/2) /* DMA/DMA32 is big. */
3387 return ZONELIST_ORDER_NODE;
3388 /*
3389 * look into each node's config.
3390 * If there is a node whose DMA/DMA32 memory is very big area on
3391 * local memory, NODE_ORDER may be suitable.
3392 */
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07003393 average_size = total_size /
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08003394 (nodes_weight(node_states[N_MEMORY]) + 1);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003395 for_each_online_node(nid) {
3396 low_kmem_size = 0;
3397 total_size = 0;
3398 for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
3399 z = &NODE_DATA(nid)->node_zones[zone_type];
3400 if (populated_zone(z)) {
3401 if (zone_type < ZONE_NORMAL)
3402 low_kmem_size += z->present_pages;
3403 total_size += z->present_pages;
3404 }
3405 }
3406 if (low_kmem_size &&
3407 total_size > average_size && /* ignore small node */
3408 low_kmem_size > total_size * 70/100)
3409 return ZONELIST_ORDER_NODE;
3410 }
3411 return ZONELIST_ORDER_ZONE;
3412}
3413
3414static void set_zonelist_order(void)
3415{
3416 if (user_zonelist_order == ZONELIST_ORDER_DEFAULT)
3417 current_zonelist_order = default_zonelist_order();
3418 else
3419 current_zonelist_order = user_zonelist_order;
3420}
3421
3422static void build_zonelists(pg_data_t *pgdat)
3423{
3424 int j, node, load;
3425 enum zone_type i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 nodemask_t used_mask;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003427 int local_node, prev_node;
3428 struct zonelist *zonelist;
3429 int order = current_zonelist_order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
3431 /* initialize zonelists */
Christoph Lameter523b9452007-10-16 01:25:37 -07003432 for (i = 0; i < MAX_ZONELISTS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 zonelist = pgdat->node_zonelists + i;
Mel Gormandd1a2392008-04-28 02:12:17 -07003434 zonelist->_zonerefs[0].zone = NULL;
3435 zonelist->_zonerefs[0].zone_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 }
3437
3438 /* NUMA-aware ordering of nodes */
3439 local_node = pgdat->node_id;
Christoph Lameter62bc62a2009-06-16 15:32:15 -07003440 load = nr_online_nodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 prev_node = local_node;
3442 nodes_clear(used_mask);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003443
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003444 memset(node_order, 0, sizeof(node_order));
3445 j = 0;
3446
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
3448 /*
3449 * We don't want to pressure a particular node.
3450 * So adding penalty to the first node in same
3451 * distance group to make it round-robin.
3452 */
David Rientjes957f8222012-10-08 16:33:24 -07003453 if (node_distance(local_node, node) !=
3454 node_distance(local_node, prev_node))
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003455 node_load[node] = load;
3456
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 prev_node = node;
3458 load--;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003459 if (order == ZONELIST_ORDER_NODE)
3460 build_zonelists_in_node_order(pgdat, node);
3461 else
3462 node_order[j++] = node; /* remember order */
3463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003465 if (order == ZONELIST_ORDER_ZONE) {
3466 /* calculate node order -- i.e., DMA last! */
3467 build_zonelists_in_zone_order(pgdat, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 }
Christoph Lameter523b9452007-10-16 01:25:37 -07003469
3470 build_thisnode_zonelists(pgdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471}
3472
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003473/* Construct the zonelist performance cache - see further mmzone.h */
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 struct zonelist *zonelist;
3477 struct zonelist_cache *zlc;
Mel Gormandd1a2392008-04-28 02:12:17 -07003478 struct zoneref *z;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003479
Mel Gorman54a6eb52008-04-28 02:12:16 -07003480 zonelist = &pgdat->node_zonelists[0];
3481 zonelist->zlcache_ptr = zlc = &zonelist->zlcache;
3482 bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
Mel Gormandd1a2392008-04-28 02:12:17 -07003483 for (z = zonelist->_zonerefs; z->zone; z++)
3484 zlc->z_to_n[z - zonelist->_zonerefs] = zonelist_node_idx(z);
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003485}
3486
Lee Schermerhorn7aac7892010-05-26 14:45:00 -07003487#ifdef CONFIG_HAVE_MEMORYLESS_NODES
3488/*
3489 * Return node id of node used for "local" allocations.
3490 * I.e., first node id of first zone in arg node's generic zonelist.
3491 * Used for initializing percpu 'numa_mem', which is used primarily
3492 * for kernel allocations, so use GFP_KERNEL flags to locate zonelist.
3493 */
3494int local_memory_node(int node)
3495{
3496 struct zone *zone;
3497
3498 (void)first_zones_zonelist(node_zonelist(node, GFP_KERNEL),
3499 gfp_zone(GFP_KERNEL),
3500 NULL,
3501 &zone);
3502 return zone->node;
3503}
3504#endif
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003505
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506#else /* CONFIG_NUMA */
3507
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003508static void set_zonelist_order(void)
3509{
3510 current_zonelist_order = ZONELIST_ORDER_ZONE;
3511}
3512
3513static void build_zonelists(pg_data_t *pgdat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514{
Christoph Lameter19655d32006-09-25 23:31:19 -07003515 int node, local_node;
Mel Gorman54a6eb52008-04-28 02:12:16 -07003516 enum zone_type j;
3517 struct zonelist *zonelist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518
3519 local_node = pgdat->node_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520
Mel Gorman54a6eb52008-04-28 02:12:16 -07003521 zonelist = &pgdat->node_zonelists[0];
3522 j = build_zonelists_node(pgdat, zonelist, 0, MAX_NR_ZONES - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523
Mel Gorman54a6eb52008-04-28 02:12:16 -07003524 /*
3525 * Now we build the zonelist so that it contains the zones
3526 * of all the other nodes.
3527 * We don't want to pressure a particular node, so when
3528 * building the zones for node N, we make sure that the
3529 * zones coming right after the local ones are those from
3530 * node N+1 (modulo N)
3531 */
3532 for (node = local_node + 1; node < MAX_NUMNODES; node++) {
3533 if (!node_online(node))
3534 continue;
3535 j = build_zonelists_node(NODE_DATA(node), zonelist, j,
3536 MAX_NR_ZONES - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 }
Mel Gorman54a6eb52008-04-28 02:12:16 -07003538 for (node = 0; node < local_node; node++) {
3539 if (!node_online(node))
3540 continue;
3541 j = build_zonelists_node(NODE_DATA(node), zonelist, j,
3542 MAX_NR_ZONES - 1);
3543 }
3544
Mel Gormandd1a2392008-04-28 02:12:17 -07003545 zonelist->_zonerefs[j].zone = NULL;
3546 zonelist->_zonerefs[j].zone_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547}
3548
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003549/* non-NUMA variant of zonelist performance cache - just NULL zlcache_ptr */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003550static void build_zonelist_cache(pg_data_t *pgdat)
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003551{
Mel Gorman54a6eb52008-04-28 02:12:16 -07003552 pgdat->node_zonelists[0].zlcache_ptr = NULL;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003553}
3554
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555#endif /* CONFIG_NUMA */
3556
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09003557/*
3558 * Boot pageset table. One per cpu which is going to be used for all
3559 * zones and all nodes. The parameters will be set in such a way
3560 * that an item put on a list will immediately be handed over to
3561 * the buddy list. This is safe since pageset manipulation is done
3562 * with interrupts disabled.
3563 *
3564 * The boot_pagesets must be kept even after bootup is complete for
3565 * unused processors and/or zones. They do play a role for bootstrapping
3566 * hotplugged processors.
3567 *
3568 * zoneinfo_show() and maybe other functions do
3569 * not check if the processor is online before following the pageset pointer.
3570 * Other parts of the kernel may not check if the zone is available.
3571 */
3572static void setup_pageset(struct per_cpu_pageset *p, unsigned long batch);
3573static DEFINE_PER_CPU(struct per_cpu_pageset, boot_pageset);
Haicheng Li1f522502010-05-24 14:32:51 -07003574static void setup_zone_pageset(struct zone *zone);
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09003575
Haicheng Li4eaf3f62010-05-24 14:32:52 -07003576/*
3577 * Global mutex to protect against size modification of zonelists
3578 * as well as to serialize pageset setup for the new populated zone.
3579 */
3580DEFINE_MUTEX(zonelists_mutex);
3581
Rusty Russell9b1a4d32008-07-28 12:16:30 -05003582/* return values int ....just for stop_machine() */
Jiang Liu4ed7e022012-07-31 16:43:35 -07003583static int __build_all_zonelists(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584{
Yasunori Goto68113782006-06-23 02:03:11 -07003585 int nid;
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09003586 int cpu;
Jiang Liu9adb62a2012-07-31 16:43:28 -07003587 pg_data_t *self = data;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003588
Bo Liu7f9cfb32009-08-18 14:11:19 -07003589#ifdef CONFIG_NUMA
3590 memset(node_load, 0, sizeof(node_load));
3591#endif
Jiang Liu9adb62a2012-07-31 16:43:28 -07003592
3593 if (self && !node_online(self->node_id)) {
3594 build_zonelists(self);
3595 build_zonelist_cache(self);
3596 }
3597
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003598 for_each_online_node(nid) {
Christoph Lameter7ea15302007-10-16 01:25:29 -07003599 pg_data_t *pgdat = NODE_DATA(nid);
3600
3601 build_zonelists(pgdat);
3602 build_zonelist_cache(pgdat);
Paul Jackson9276b1bc2006-12-06 20:31:48 -08003603 }
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09003604
3605 /*
3606 * Initialize the boot_pagesets that are going to be used
3607 * for bootstrapping processors. The real pagesets for
3608 * each zone will be allocated later when the per cpu
3609 * allocator is available.
3610 *
3611 * boot_pagesets are used also for bootstrapping offline
3612 * cpus if the system is already booted because the pagesets
3613 * are needed to initialize allocators on a specific cpu too.
3614 * F.e. the percpu allocator needs the page allocator which
3615 * needs the percpu allocator in order to allocate its pagesets
3616 * (a chicken-egg dilemma).
3617 */
Lee Schermerhorn7aac7892010-05-26 14:45:00 -07003618 for_each_possible_cpu(cpu) {
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09003619 setup_pageset(&per_cpu(boot_pageset, cpu), 0);
3620
Lee Schermerhorn7aac7892010-05-26 14:45:00 -07003621#ifdef CONFIG_HAVE_MEMORYLESS_NODES
3622 /*
3623 * We now know the "local memory node" for each node--
3624 * i.e., the node of the first zone in the generic zonelist.
3625 * Set up numa_mem percpu variable for on-line cpus. During
3626 * boot, only the boot cpu should be on-line; we'll init the
3627 * secondary cpus' numa_mem as they come on-line. During
3628 * node/memory hotplug, we'll fixup all on-line cpus.
3629 */
3630 if (cpu_online(cpu))
3631 set_cpu_numa_mem(cpu, local_memory_node(cpu_to_node(cpu)));
3632#endif
3633 }
3634
Yasunori Goto68113782006-06-23 02:03:11 -07003635 return 0;
3636}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637
Haicheng Li4eaf3f62010-05-24 14:32:52 -07003638/*
3639 * Called with zonelists_mutex held always
3640 * unless system_state == SYSTEM_BOOTING.
3641 */
Jiang Liu9adb62a2012-07-31 16:43:28 -07003642void __ref build_all_zonelists(pg_data_t *pgdat, struct zone *zone)
Yasunori Goto68113782006-06-23 02:03:11 -07003643{
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003644 set_zonelist_order();
3645
Yasunori Goto68113782006-06-23 02:03:11 -07003646 if (system_state == SYSTEM_BOOTING) {
Randy Dunlap423b41d2006-09-27 01:50:12 -07003647 __build_all_zonelists(NULL);
Mel Gorman68ad8df2008-07-23 21:26:52 -07003648 mminit_verify_zonelist();
Yasunori Goto68113782006-06-23 02:03:11 -07003649 cpuset_init_current_mems_allowed();
3650 } else {
Simon Arlott183ff222007-10-20 01:27:18 +02003651 /* we have to stop all cpus to guarantee there is no user
Yasunori Goto68113782006-06-23 02:03:11 -07003652 of zonelist */
KAMEZAWA Hiroyukie9959f02010-11-24 12:57:09 -08003653#ifdef CONFIG_MEMORY_HOTPLUG
Jiang Liu9adb62a2012-07-31 16:43:28 -07003654 if (zone)
3655 setup_zone_pageset(zone);
KAMEZAWA Hiroyukie9959f02010-11-24 12:57:09 -08003656#endif
Jiang Liu9adb62a2012-07-31 16:43:28 -07003657 stop_machine(__build_all_zonelists, pgdat, NULL);
Yasunori Goto68113782006-06-23 02:03:11 -07003658 /* cpuset refresh routine should be here */
3659 }
Andrew Mortonbd1e22b2006-06-23 02:03:47 -07003660 vm_total_pages = nr_free_pagecache_pages();
Mel Gorman9ef9acb2007-10-16 01:25:54 -07003661 /*
3662 * Disable grouping by mobility if the number of pages in the
3663 * system is too low to allow the mechanism to work. It would be
3664 * more accurate, but expensive to check per-zone. This check is
3665 * made on memory-hotadd so a system can start with mobility
3666 * disabled and enable it later
3667 */
Mel Gormand9c23402007-10-16 01:26:01 -07003668 if (vm_total_pages < (pageblock_nr_pages * MIGRATE_TYPES))
Mel Gorman9ef9acb2007-10-16 01:25:54 -07003669 page_group_by_mobility_disabled = 1;
3670 else
3671 page_group_by_mobility_disabled = 0;
3672
3673 printk("Built %i zonelists in %s order, mobility grouping %s. "
3674 "Total pages: %ld\n",
Christoph Lameter62bc62a2009-06-16 15:32:15 -07003675 nr_online_nodes,
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003676 zonelist_order_name[current_zonelist_order],
Mel Gorman9ef9acb2007-10-16 01:25:54 -07003677 page_group_by_mobility_disabled ? "off" : "on",
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07003678 vm_total_pages);
3679#ifdef CONFIG_NUMA
3680 printk("Policy zone: %s\n", zone_names[policy_zone]);
3681#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682}
3683
3684/*
3685 * Helper functions to size the waitqueue hash table.
3686 * Essentially these want to choose hash table sizes sufficiently
3687 * large so that collisions trying to wait on pages are rare.
3688 * But in fact, the number of active page waitqueues on typical
3689 * systems is ridiculously low, less than 200. So this is even
3690 * conservative, even though it seems large.
3691 *
3692 * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
3693 * waitqueues, i.e. the size of the waitq table given the number of pages.
3694 */
3695#define PAGES_PER_WAITQUEUE 256
3696
Yasunori Gotocca448f2006-06-23 02:03:10 -07003697#ifndef CONFIG_MEMORY_HOTPLUG
Yasunori Goto02b694d2006-06-23 02:03:08 -07003698static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699{
3700 unsigned long size = 1;
3701
3702 pages /= PAGES_PER_WAITQUEUE;
3703
3704 while (size < pages)
3705 size <<= 1;
3706
3707 /*
3708 * Once we have dozens or even hundreds of threads sleeping
3709 * on IO we've got bigger problems than wait queue collision.
3710 * Limit the size of the wait table to a reasonable size.
3711 */
3712 size = min(size, 4096UL);
3713
3714 return max(size, 4UL);
3715}
Yasunori Gotocca448f2006-06-23 02:03:10 -07003716#else
3717/*
3718 * A zone's size might be changed by hot-add, so it is not possible to determine
3719 * a suitable size for its wait_table. So we use the maximum size now.
3720 *
3721 * The max wait table size = 4096 x sizeof(wait_queue_head_t). ie:
3722 *
3723 * i386 (preemption config) : 4096 x 16 = 64Kbyte.
3724 * ia64, x86-64 (no preemption): 4096 x 20 = 80Kbyte.
3725 * ia64, x86-64 (preemption) : 4096 x 24 = 96Kbyte.
3726 *
3727 * The maximum entries are prepared when a zone's memory is (512K + 256) pages
3728 * or more by the traditional way. (See above). It equals:
3729 *
3730 * i386, x86-64, powerpc(4K page size) : = ( 2G + 1M)byte.
3731 * ia64(16K page size) : = ( 8G + 4M)byte.
3732 * powerpc (64K page size) : = (32G +16M)byte.
3733 */
3734static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
3735{
3736 return 4096UL;
3737}
3738#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739
3740/*
3741 * This is an integer logarithm so that shifts can be used later
3742 * to extract the more random high bits from the multiplicative
3743 * hash function before the remainder is taken.
3744 */
3745static inline unsigned long wait_table_bits(unsigned long size)
3746{
3747 return ffz(~size);
3748}
3749
3750#define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
3751
Mel Gorman56fd56b2007-10-16 01:25:58 -07003752/*
Arve Hjønnevåg6d3163c2011-05-24 17:12:24 -07003753 * Check if a pageblock contains reserved pages
3754 */
3755static int pageblock_is_reserved(unsigned long start_pfn, unsigned long end_pfn)
3756{
3757 unsigned long pfn;
3758
3759 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
3760 if (!pfn_valid_within(pfn) || PageReserved(pfn_to_page(pfn)))
3761 return 1;
3762 }
3763 return 0;
3764}
3765
3766/*
Mel Gormand9c23402007-10-16 01:26:01 -07003767 * Mark a number of pageblocks as MIGRATE_RESERVE. The number
Mel Gorman41858962009-06-16 15:32:12 -07003768 * of blocks reserved is based on min_wmark_pages(zone). The memory within
3769 * the reserve will tend to store contiguous free pages. Setting min_free_kbytes
Mel Gorman56fd56b2007-10-16 01:25:58 -07003770 * higher will lead to a bigger reserve which will get freed as contiguous
3771 * blocks as reclaim kicks in
3772 */
3773static void setup_zone_migrate_reserve(struct zone *zone)
3774{
Arve Hjønnevåg6d3163c2011-05-24 17:12:24 -07003775 unsigned long start_pfn, pfn, end_pfn, block_end_pfn;
Mel Gorman56fd56b2007-10-16 01:25:58 -07003776 struct page *page;
Mel Gorman78986a62009-09-21 17:03:02 -07003777 unsigned long block_migratetype;
3778 int reserve;
Mel Gorman56fd56b2007-10-16 01:25:58 -07003779
Michal Hockod02156382011-12-08 14:34:27 -08003780 /*
3781 * Get the start pfn, end pfn and the number of blocks to reserve
3782 * We have to be careful to be aligned to pageblock_nr_pages to
3783 * make sure that we always check pfn_valid for the first page in
3784 * the block.
3785 */
Mel Gorman56fd56b2007-10-16 01:25:58 -07003786 start_pfn = zone->zone_start_pfn;
3787 end_pfn = start_pfn + zone->spanned_pages;
Michal Hockod02156382011-12-08 14:34:27 -08003788 start_pfn = roundup(start_pfn, pageblock_nr_pages);
Mel Gorman41858962009-06-16 15:32:12 -07003789 reserve = roundup(min_wmark_pages(zone), pageblock_nr_pages) >>
Mel Gormand9c23402007-10-16 01:26:01 -07003790 pageblock_order;
Mel Gorman56fd56b2007-10-16 01:25:58 -07003791
Mel Gorman78986a62009-09-21 17:03:02 -07003792 /*
3793 * Reserve blocks are generally in place to help high-order atomic
3794 * allocations that are short-lived. A min_free_kbytes value that
3795 * would result in more than 2 reserve blocks for atomic allocations
3796 * is assumed to be in place to help anti-fragmentation for the
3797 * future allocation of hugepages at runtime.
3798 */
3799 reserve = min(2, reserve);
3800
Mel Gormand9c23402007-10-16 01:26:01 -07003801 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
Mel Gorman56fd56b2007-10-16 01:25:58 -07003802 if (!pfn_valid(pfn))
3803 continue;
3804 page = pfn_to_page(pfn);
3805
Adam Litke344c7902008-09-02 14:35:38 -07003806 /* Watch out for overlapping nodes */
3807 if (page_to_nid(page) != zone_to_nid(zone))
3808 continue;
3809
Mel Gorman56fd56b2007-10-16 01:25:58 -07003810 block_migratetype = get_pageblock_migratetype(page);
3811
Mel Gorman938929f2012-01-10 15:07:14 -08003812 /* Only test what is necessary when the reserves are not met */
3813 if (reserve > 0) {
3814 /*
3815 * Blocks with reserved pages will never free, skip
3816 * them.
3817 */
3818 block_end_pfn = min(pfn + pageblock_nr_pages, end_pfn);
3819 if (pageblock_is_reserved(pfn, block_end_pfn))
3820 continue;
Mel Gorman56fd56b2007-10-16 01:25:58 -07003821
Mel Gorman938929f2012-01-10 15:07:14 -08003822 /* If this block is reserved, account for it */
3823 if (block_migratetype == MIGRATE_RESERVE) {
3824 reserve--;
3825 continue;
3826 }
3827
3828 /* Suitable for reserving if this block is movable */
3829 if (block_migratetype == MIGRATE_MOVABLE) {
3830 set_pageblock_migratetype(page,
3831 MIGRATE_RESERVE);
3832 move_freepages_block(zone, page,
3833 MIGRATE_RESERVE);
3834 reserve--;
3835 continue;
3836 }
Mel Gorman56fd56b2007-10-16 01:25:58 -07003837 }
3838
3839 /*
3840 * If the reserve is met and this is a previous reserved block,
3841 * take it back
3842 */
3843 if (block_migratetype == MIGRATE_RESERVE) {
3844 set_pageblock_migratetype(page, MIGRATE_MOVABLE);
3845 move_freepages_block(zone, page, MIGRATE_MOVABLE);
3846 }
3847 }
3848}
Mel Gormanac0e5b72007-10-16 01:25:58 -07003849
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850/*
3851 * Initially all pages are reserved - free ones are freed
3852 * up by free_all_bootmem() once the early boot process is
3853 * done. Non-atomic initialization, single-pass.
3854 */
Matt Tolentinoc09b4242006-01-17 07:03:44 +01003855void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
Dave Hansena2f3aa022007-01-10 23:15:30 -08003856 unsigned long start_pfn, enum memmap_context context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 struct page *page;
Andy Whitcroft29751f62005-06-23 00:08:00 -07003859 unsigned long end_pfn = start_pfn + size;
3860 unsigned long pfn;
KAMEZAWA Hiroyuki86051ca2008-04-29 00:58:21 -07003861 struct zone *z;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862
Hugh Dickins22b31ee2009-01-06 14:40:09 -08003863 if (highest_memmap_pfn < end_pfn - 1)
3864 highest_memmap_pfn = end_pfn - 1;
3865
KAMEZAWA Hiroyuki86051ca2008-04-29 00:58:21 -07003866 z = &NODE_DATA(nid)->node_zones[zone];
Greg Ungerercbe8dd42006-01-12 01:05:24 -08003867 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
Dave Hansena2f3aa022007-01-10 23:15:30 -08003868 /*
3869 * There can be holes in boot-time mem_map[]s
3870 * handed to this function. They do not
3871 * exist on hotplugged memory.
3872 */
3873 if (context == MEMMAP_EARLY) {
3874 if (!early_pfn_valid(pfn))
3875 continue;
3876 if (!early_pfn_in_nid(pfn, nid))
3877 continue;
3878 }
Andy Whitcroftd41dee32005-06-23 00:07:54 -07003879 page = pfn_to_page(pfn);
3880 set_page_links(page, zone, nid, pfn);
Mel Gorman708614e2008-07-23 21:26:51 -07003881 mminit_verify_page_links(page, zone, nid, pfn);
Nick Piggin7835e982006-03-22 00:08:40 -08003882 init_page_count(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 reset_page_mapcount(page);
Mel Gorman57e0a032012-11-12 09:06:20 +00003884 reset_page_last_nid(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 SetPageReserved(page);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07003886 /*
3887 * Mark the block movable so that blocks are reserved for
3888 * movable at startup. This will force kernel allocations
3889 * to reserve their blocks rather than leaking throughout
3890 * the address space during boot when many long-lived
Mel Gorman56fd56b2007-10-16 01:25:58 -07003891 * kernel allocations are made. Later some blocks near
3892 * the start are marked MIGRATE_RESERVE by
3893 * setup_zone_migrate_reserve()
KAMEZAWA Hiroyuki86051ca2008-04-29 00:58:21 -07003894 *
3895 * bitmap is created for zone's valid pfn range. but memmap
3896 * can be created for invalid pages (for alignment)
3897 * check here not to call set_pageblock_migratetype() against
3898 * pfn out of zone.
Mel Gormanb2a0ac82007-10-16 01:25:48 -07003899 */
KAMEZAWA Hiroyuki86051ca2008-04-29 00:58:21 -07003900 if ((z->zone_start_pfn <= pfn)
3901 && (pfn < z->zone_start_pfn + z->spanned_pages)
3902 && !(pfn & (pageblock_nr_pages - 1)))
Mel Gorman56fd56b2007-10-16 01:25:58 -07003903 set_pageblock_migratetype(page, MIGRATE_MOVABLE);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07003904
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 INIT_LIST_HEAD(&page->lru);
3906#ifdef WANT_PAGE_VIRTUAL
3907 /* The shift won't overflow because ZONE_NORMAL is below 4G. */
3908 if (!is_highmem_idx(zone))
Bob Picco3212c6b2005-06-27 14:36:28 -07003909 set_page_address(page, __va(pfn << PAGE_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 }
3912}
3913
Andi Kleen1e548de2008-02-04 22:29:26 -08003914static void __meminit zone_init_free_lists(struct zone *zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915{
Mel Gormanb2a0ac82007-10-16 01:25:48 -07003916 int order, t;
3917 for_each_migratetype_order(order, t) {
3918 INIT_LIST_HEAD(&zone->free_area[order].free_list[t]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 zone->free_area[order].nr_free = 0;
3920 }
3921}
3922
3923#ifndef __HAVE_ARCH_MEMMAP_INIT
3924#define memmap_init(size, nid, zone, start_pfn) \
Dave Hansena2f3aa022007-01-10 23:15:30 -08003925 memmap_init_zone((size), (nid), (zone), (start_pfn), MEMMAP_EARLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926#endif
3927
Jiang Liu4ed7e022012-07-31 16:43:35 -07003928static int __meminit zone_batchsize(struct zone *zone)
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07003929{
David Howells3a6be872009-05-06 16:03:03 -07003930#ifdef CONFIG_MMU
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07003931 int batch;
3932
3933 /*
3934 * The per-cpu-pages pools are set to around 1000th of the
Seth, Rohitba56e912005-10-29 18:15:47 -07003935 * size of the zone. But no more than 1/2 of a meg.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07003936 *
3937 * OK, so we don't know how big the cache is. So guess.
3938 */
3939 batch = zone->present_pages / 1024;
Seth, Rohitba56e912005-10-29 18:15:47 -07003940 if (batch * PAGE_SIZE > 512 * 1024)
3941 batch = (512 * 1024) / PAGE_SIZE;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07003942 batch /= 4; /* We effectively *= 4 below */
3943 if (batch < 1)
3944 batch = 1;
3945
3946 /*
Nick Piggin0ceaacc2005-12-04 13:55:25 +11003947 * Clamp the batch to a 2^n - 1 value. Having a power
3948 * of 2 value was found to be more likely to have
3949 * suboptimal cache aliasing properties in some cases.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07003950 *
Nick Piggin0ceaacc2005-12-04 13:55:25 +11003951 * For example if 2 tasks are alternately allocating
3952 * batches of pages, one task can end up with a lot
3953 * of pages of one half of the possible page colors
3954 * and the other with pages of the other colors.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07003955 */
David Howells91552032009-05-06 16:03:02 -07003956 batch = rounddown_pow_of_two(batch + batch/2) - 1;
Seth, Rohitba56e912005-10-29 18:15:47 -07003957
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07003958 return batch;
David Howells3a6be872009-05-06 16:03:03 -07003959
3960#else
3961 /* The deferral and batching of frees should be suppressed under NOMMU
3962 * conditions.
3963 *
3964 * The problem is that NOMMU needs to be able to allocate large chunks
3965 * of contiguous memory as there's no hardware page translation to
3966 * assemble apparent contiguous memory from discontiguous pages.
3967 *
3968 * Queueing large contiguous runs of pages for batching, however,
3969 * causes the pages to actually be freed in smaller chunks. As there
3970 * can be a significant delay between the individual batches being
3971 * recycled, this leads to the once large chunks of space being
3972 * fragmented and becoming unavailable for high-order allocations.
3973 */
3974 return 0;
3975#endif
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07003976}
3977
Adrian Bunkb69a7282008-07-23 21:28:12 -07003978static void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
Christoph Lameter2caaad42005-06-21 17:15:00 -07003979{
3980 struct per_cpu_pages *pcp;
Mel Gorman5f8dcc22009-09-21 17:03:19 -07003981 int migratetype;
Christoph Lameter2caaad42005-06-21 17:15:00 -07003982
Magnus Damm1c6fe942005-10-26 01:58:59 -07003983 memset(p, 0, sizeof(*p));
3984
Christoph Lameter3dfa5722008-02-04 22:29:19 -08003985 pcp = &p->pcp;
Christoph Lameter2caaad42005-06-21 17:15:00 -07003986 pcp->count = 0;
Christoph Lameter2caaad42005-06-21 17:15:00 -07003987 pcp->high = 6 * batch;
3988 pcp->batch = max(1UL, 1 * batch);
Mel Gorman5f8dcc22009-09-21 17:03:19 -07003989 for (migratetype = 0; migratetype < MIGRATE_PCPTYPES; migratetype++)
3990 INIT_LIST_HEAD(&pcp->lists[migratetype]);
Christoph Lameter2caaad42005-06-21 17:15:00 -07003991}
3992
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08003993/*
3994 * setup_pagelist_highmark() sets the high water mark for hot per_cpu_pagelist
3995 * to the value high for the pageset p.
3996 */
3997
3998static void setup_pagelist_highmark(struct per_cpu_pageset *p,
3999 unsigned long high)
4000{
4001 struct per_cpu_pages *pcp;
4002
Christoph Lameter3dfa5722008-02-04 22:29:19 -08004003 pcp = &p->pcp;
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08004004 pcp->high = high;
4005 pcp->batch = max(1UL, high/4);
4006 if ((high/4) > (PAGE_SHIFT * 8))
4007 pcp->batch = PAGE_SHIFT * 8;
4008}
4009
Jiang Liu4ed7e022012-07-31 16:43:35 -07004010static void __meminit setup_zone_pageset(struct zone *zone)
Wu Fengguang319774e2010-05-24 14:32:49 -07004011{
4012 int cpu;
4013
4014 zone->pageset = alloc_percpu(struct per_cpu_pageset);
4015
4016 for_each_possible_cpu(cpu) {
4017 struct per_cpu_pageset *pcp = per_cpu_ptr(zone->pageset, cpu);
4018
4019 setup_pageset(pcp, zone_batchsize(zone));
4020
4021 if (percpu_pagelist_fraction)
4022 setup_pagelist_highmark(pcp,
4023 (zone->present_pages /
4024 percpu_pagelist_fraction));
4025 }
4026}
4027
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07004028/*
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09004029 * Allocate per cpu pagesets and initialize them.
4030 * Before this call only boot pagesets were available.
Christoph Lameter2caaad42005-06-21 17:15:00 -07004031 */
Al Viro78d99552005-12-15 09:18:25 +00004032void __init setup_per_cpu_pageset(void)
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07004033{
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09004034 struct zone *zone;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07004035
Wu Fengguang319774e2010-05-24 14:32:49 -07004036 for_each_populated_zone(zone)
4037 setup_zone_pageset(zone);
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07004038}
4039
Sam Ravnborg577a32f2007-05-17 23:29:25 +02004040static noinline __init_refok
Yasunori Gotocca448f2006-06-23 02:03:10 -07004041int zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages)
Dave Hansened8ece22005-10-29 18:16:50 -07004042{
4043 int i;
4044 struct pglist_data *pgdat = zone->zone_pgdat;
Yasunori Gotocca448f2006-06-23 02:03:10 -07004045 size_t alloc_size;
Dave Hansened8ece22005-10-29 18:16:50 -07004046
4047 /*
4048 * The per-page waitqueue mechanism uses hashed waitqueues
4049 * per zone.
4050 */
Yasunori Goto02b694d2006-06-23 02:03:08 -07004051 zone->wait_table_hash_nr_entries =
4052 wait_table_hash_nr_entries(zone_size_pages);
4053 zone->wait_table_bits =
4054 wait_table_bits(zone->wait_table_hash_nr_entries);
Yasunori Gotocca448f2006-06-23 02:03:10 -07004055 alloc_size = zone->wait_table_hash_nr_entries
4056 * sizeof(wait_queue_head_t);
4057
Heiko Carstenscd94b9d2008-05-23 13:04:52 -07004058 if (!slab_is_available()) {
Yasunori Gotocca448f2006-06-23 02:03:10 -07004059 zone->wait_table = (wait_queue_head_t *)
Yinghai Lu8f389a992011-05-11 15:13:32 -07004060 alloc_bootmem_node_nopanic(pgdat, alloc_size);
Yasunori Gotocca448f2006-06-23 02:03:10 -07004061 } else {
4062 /*
4063 * This case means that a zone whose size was 0 gets new memory
4064 * via memory hot-add.
4065 * But it may be the case that a new node was hot-added. In
4066 * this case vmalloc() will not be able to use this new node's
4067 * memory - this wait_table must be initialized to use this new
4068 * node itself as well.
4069 * To use this new node's memory, further consideration will be
4070 * necessary.
4071 */
Jesper Juhl8691f3a2007-10-16 01:24:49 -07004072 zone->wait_table = vmalloc(alloc_size);
Yasunori Gotocca448f2006-06-23 02:03:10 -07004073 }
4074 if (!zone->wait_table)
4075 return -ENOMEM;
Dave Hansened8ece22005-10-29 18:16:50 -07004076
Yasunori Goto02b694d2006-06-23 02:03:08 -07004077 for(i = 0; i < zone->wait_table_hash_nr_entries; ++i)
Dave Hansened8ece22005-10-29 18:16:50 -07004078 init_waitqueue_head(zone->wait_table + i);
Yasunori Gotocca448f2006-06-23 02:03:10 -07004079
4080 return 0;
Dave Hansened8ece22005-10-29 18:16:50 -07004081}
4082
Matt Tolentinoc09b4242006-01-17 07:03:44 +01004083static __meminit void zone_pcp_init(struct zone *zone)
Dave Hansened8ece22005-10-29 18:16:50 -07004084{
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09004085 /*
4086 * per cpu subsystem is not up at this point. The following code
4087 * relies on the ability of the linker to provide the
4088 * offset of a (static) per cpu variable into the per cpu area.
4089 */
4090 zone->pageset = &boot_pageset;
Dave Hansened8ece22005-10-29 18:16:50 -07004091
Anton Blanchardf5335c02006-03-25 03:06:49 -08004092 if (zone->present_pages)
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09004093 printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%u\n",
4094 zone->name, zone->present_pages,
4095 zone_batchsize(zone));
Dave Hansened8ece22005-10-29 18:16:50 -07004096}
4097
Jiang Liu4ed7e022012-07-31 16:43:35 -07004098int __meminit init_currently_empty_zone(struct zone *zone,
Yasunori Goto718127c2006-06-23 02:03:10 -07004099 unsigned long zone_start_pfn,
Dave Hansena2f3aa022007-01-10 23:15:30 -08004100 unsigned long size,
4101 enum memmap_context context)
Dave Hansened8ece22005-10-29 18:16:50 -07004102{
4103 struct pglist_data *pgdat = zone->zone_pgdat;
Yasunori Gotocca448f2006-06-23 02:03:10 -07004104 int ret;
4105 ret = zone_wait_table_init(zone, size);
4106 if (ret)
4107 return ret;
Dave Hansened8ece22005-10-29 18:16:50 -07004108 pgdat->nr_zones = zone_idx(zone) + 1;
4109
Dave Hansened8ece22005-10-29 18:16:50 -07004110 zone->zone_start_pfn = zone_start_pfn;
4111
Mel Gorman708614e2008-07-23 21:26:51 -07004112 mminit_dprintk(MMINIT_TRACE, "memmap_init",
4113 "Initialising map node %d zone %lu pfns %lu -> %lu\n",
4114 pgdat->node_id,
4115 (unsigned long)zone_idx(zone),
4116 zone_start_pfn, (zone_start_pfn + size));
4117
Andi Kleen1e548de2008-02-04 22:29:26 -08004118 zone_init_free_lists(zone);
Yasunori Goto718127c2006-06-23 02:03:10 -07004119
4120 return 0;
Dave Hansened8ece22005-10-29 18:16:50 -07004121}
4122
Tejun Heo0ee332c2011-12-08 10:22:09 -08004123#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Mel Gormanc7132162006-09-27 01:49:43 -07004124#ifndef CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID
4125/*
4126 * Required by SPARSEMEM. Given a PFN, return what node the PFN is on.
4127 * Architectures may implement their own version but if add_active_range()
4128 * was used and there are no special requirements, this is a convenient
4129 * alternative
4130 */
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -08004131int __meminit __early_pfn_to_nid(unsigned long pfn)
Mel Gormanc7132162006-09-27 01:49:43 -07004132{
Tejun Heoc13291a2011-07-12 10:46:30 +02004133 unsigned long start_pfn, end_pfn;
4134 int i, nid;
Mel Gormanc7132162006-09-27 01:49:43 -07004135
Tejun Heoc13291a2011-07-12 10:46:30 +02004136 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid)
Mel Gormanc7132162006-09-27 01:49:43 -07004137 if (start_pfn <= pfn && pfn < end_pfn)
Tejun Heoc13291a2011-07-12 10:46:30 +02004138 return nid;
KAMEZAWA Hiroyukicc2559b2009-02-18 14:48:33 -08004139 /* This is a memory hole */
4140 return -1;
Mel Gormanc7132162006-09-27 01:49:43 -07004141}
4142#endif /* CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID */
4143
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -08004144int __meminit early_pfn_to_nid(unsigned long pfn)
4145{
KAMEZAWA Hiroyukicc2559b2009-02-18 14:48:33 -08004146 int nid;
4147
4148 nid = __early_pfn_to_nid(pfn);
4149 if (nid >= 0)
4150 return nid;
4151 /* just returns 0 */
4152 return 0;
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -08004153}
4154
KAMEZAWA Hiroyukicc2559b2009-02-18 14:48:33 -08004155#ifdef CONFIG_NODES_SPAN_OTHER_NODES
4156bool __meminit early_pfn_in_nid(unsigned long pfn, int node)
4157{
4158 int nid;
4159
4160 nid = __early_pfn_to_nid(pfn);
4161 if (nid >= 0 && nid != node)
4162 return false;
4163 return true;
4164}
4165#endif
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -08004166
Mel Gormanc7132162006-09-27 01:49:43 -07004167/**
4168 * free_bootmem_with_active_regions - Call free_bootmem_node for each active range
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004169 * @nid: The node to free memory on. If MAX_NUMNODES, all nodes are freed.
4170 * @max_low_pfn: The highest PFN that will be passed to free_bootmem_node
Mel Gormanc7132162006-09-27 01:49:43 -07004171 *
4172 * If an architecture guarantees that all ranges registered with
4173 * add_active_ranges() contain no holes and may be freed, this
4174 * this function may be used instead of calling free_bootmem() manually.
4175 */
Tejun Heoc13291a2011-07-12 10:46:30 +02004176void __init free_bootmem_with_active_regions(int nid, unsigned long max_low_pfn)
Mel Gormanc7132162006-09-27 01:49:43 -07004177{
Tejun Heoc13291a2011-07-12 10:46:30 +02004178 unsigned long start_pfn, end_pfn;
4179 int i, this_nid;
Mel Gormanc7132162006-09-27 01:49:43 -07004180
Tejun Heoc13291a2011-07-12 10:46:30 +02004181 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, &this_nid) {
4182 start_pfn = min(start_pfn, max_low_pfn);
4183 end_pfn = min(end_pfn, max_low_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07004184
Tejun Heoc13291a2011-07-12 10:46:30 +02004185 if (start_pfn < end_pfn)
4186 free_bootmem_node(NODE_DATA(this_nid),
4187 PFN_PHYS(start_pfn),
4188 (end_pfn - start_pfn) << PAGE_SHIFT);
Mel Gormanc7132162006-09-27 01:49:43 -07004189 }
4190}
4191
4192/**
4193 * sparse_memory_present_with_active_regions - Call memory_present for each active range
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004194 * @nid: The node to call memory_present for. If MAX_NUMNODES, all nodes will be used.
Mel Gormanc7132162006-09-27 01:49:43 -07004195 *
4196 * If an architecture guarantees that all ranges registered with
4197 * add_active_ranges() contain no holes and may be freed, this
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004198 * function may be used instead of calling memory_present() manually.
Mel Gormanc7132162006-09-27 01:49:43 -07004199 */
4200void __init sparse_memory_present_with_active_regions(int nid)
4201{
Tejun Heoc13291a2011-07-12 10:46:30 +02004202 unsigned long start_pfn, end_pfn;
4203 int i, this_nid;
Mel Gormanc7132162006-09-27 01:49:43 -07004204
Tejun Heoc13291a2011-07-12 10:46:30 +02004205 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, &this_nid)
4206 memory_present(this_nid, start_pfn, end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07004207}
4208
4209/**
4210 * get_pfn_range_for_nid - Return the start and end page frames for a node
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004211 * @nid: The nid to return the range for. If MAX_NUMNODES, the min and max PFN are returned.
4212 * @start_pfn: Passed by reference. On return, it will have the node start_pfn.
4213 * @end_pfn: Passed by reference. On return, it will have the node end_pfn.
Mel Gormanc7132162006-09-27 01:49:43 -07004214 *
4215 * It returns the start and end page frame of a node based on information
4216 * provided by an arch calling add_active_range(). If called for a node
4217 * with no available memory, a warning is printed and the start and end
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004218 * PFNs will be 0.
Mel Gormanc7132162006-09-27 01:49:43 -07004219 */
Yasunori Gotoa3142c82007-05-08 00:23:07 -07004220void __meminit get_pfn_range_for_nid(unsigned int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07004221 unsigned long *start_pfn, unsigned long *end_pfn)
4222{
Tejun Heoc13291a2011-07-12 10:46:30 +02004223 unsigned long this_start_pfn, this_end_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07004224 int i;
Tejun Heoc13291a2011-07-12 10:46:30 +02004225
Mel Gormanc7132162006-09-27 01:49:43 -07004226 *start_pfn = -1UL;
4227 *end_pfn = 0;
4228
Tejun Heoc13291a2011-07-12 10:46:30 +02004229 for_each_mem_pfn_range(i, nid, &this_start_pfn, &this_end_pfn, NULL) {
4230 *start_pfn = min(*start_pfn, this_start_pfn);
4231 *end_pfn = max(*end_pfn, this_end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07004232 }
4233
Christoph Lameter633c0662007-10-16 01:25:37 -07004234 if (*start_pfn == -1UL)
Mel Gormanc7132162006-09-27 01:49:43 -07004235 *start_pfn = 0;
Mel Gormanc7132162006-09-27 01:49:43 -07004236}
4237
4238/*
Mel Gorman2a1e2742007-07-17 04:03:12 -07004239 * This finds a zone that can be used for ZONE_MOVABLE pages. The
4240 * assumption is made that zones within a node are ordered in monotonic
4241 * increasing memory addresses so that the "highest" populated zone is used
4242 */
Adrian Bunkb69a7282008-07-23 21:28:12 -07004243static void __init find_usable_zone_for_movable(void)
Mel Gorman2a1e2742007-07-17 04:03:12 -07004244{
4245 int zone_index;
4246 for (zone_index = MAX_NR_ZONES - 1; zone_index >= 0; zone_index--) {
4247 if (zone_index == ZONE_MOVABLE)
4248 continue;
4249
4250 if (arch_zone_highest_possible_pfn[zone_index] >
4251 arch_zone_lowest_possible_pfn[zone_index])
4252 break;
4253 }
4254
4255 VM_BUG_ON(zone_index == -1);
4256 movable_zone = zone_index;
4257}
4258
4259/*
4260 * The zone ranges provided by the architecture do not include ZONE_MOVABLE
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004261 * because it is sized independent of architecture. Unlike the other zones,
Mel Gorman2a1e2742007-07-17 04:03:12 -07004262 * the starting point for ZONE_MOVABLE is not fixed. It may be different
4263 * in each node depending on the size of each node and how evenly kernelcore
4264 * is distributed. This helper function adjusts the zone ranges
4265 * provided by the architecture for a given node by using the end of the
4266 * highest usable zone for ZONE_MOVABLE. This preserves the assumption that
4267 * zones within a node are in order of monotonic increases memory addresses
4268 */
Adrian Bunkb69a7282008-07-23 21:28:12 -07004269static void __meminit adjust_zone_range_for_zone_movable(int nid,
Mel Gorman2a1e2742007-07-17 04:03:12 -07004270 unsigned long zone_type,
4271 unsigned long node_start_pfn,
4272 unsigned long node_end_pfn,
4273 unsigned long *zone_start_pfn,
4274 unsigned long *zone_end_pfn)
4275{
4276 /* Only adjust if ZONE_MOVABLE is on this node */
4277 if (zone_movable_pfn[nid]) {
4278 /* Size ZONE_MOVABLE */
4279 if (zone_type == ZONE_MOVABLE) {
4280 *zone_start_pfn = zone_movable_pfn[nid];
4281 *zone_end_pfn = min(node_end_pfn,
4282 arch_zone_highest_possible_pfn[movable_zone]);
4283
4284 /* Adjust for ZONE_MOVABLE starting within this range */
4285 } else if (*zone_start_pfn < zone_movable_pfn[nid] &&
4286 *zone_end_pfn > zone_movable_pfn[nid]) {
4287 *zone_end_pfn = zone_movable_pfn[nid];
4288
4289 /* Check if this whole range is within ZONE_MOVABLE */
4290 } else if (*zone_start_pfn >= zone_movable_pfn[nid])
4291 *zone_start_pfn = *zone_end_pfn;
4292 }
4293}
4294
4295/*
Mel Gormanc7132162006-09-27 01:49:43 -07004296 * Return the number of pages a zone spans in a node, including holes
4297 * present_pages = zone_spanned_pages_in_node() - zone_absent_pages_in_node()
4298 */
Paul Mundt6ea6e682007-07-15 23:38:20 -07004299static unsigned long __meminit zone_spanned_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07004300 unsigned long zone_type,
4301 unsigned long *ignored)
4302{
4303 unsigned long node_start_pfn, node_end_pfn;
4304 unsigned long zone_start_pfn, zone_end_pfn;
4305
4306 /* Get the start and end of the node and zone */
4307 get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
4308 zone_start_pfn = arch_zone_lowest_possible_pfn[zone_type];
4309 zone_end_pfn = arch_zone_highest_possible_pfn[zone_type];
Mel Gorman2a1e2742007-07-17 04:03:12 -07004310 adjust_zone_range_for_zone_movable(nid, zone_type,
4311 node_start_pfn, node_end_pfn,
4312 &zone_start_pfn, &zone_end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07004313
4314 /* Check that this node has pages within the zone's required range */
4315 if (zone_end_pfn < node_start_pfn || zone_start_pfn > node_end_pfn)
4316 return 0;
4317
4318 /* Move the zone boundaries inside the node if necessary */
4319 zone_end_pfn = min(zone_end_pfn, node_end_pfn);
4320 zone_start_pfn = max(zone_start_pfn, node_start_pfn);
4321
4322 /* Return the spanned pages */
4323 return zone_end_pfn - zone_start_pfn;
4324}
4325
4326/*
4327 * Return the number of holes in a range on a node. If nid is MAX_NUMNODES,
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004328 * then all holes in the requested range will be accounted for.
Mel Gormanc7132162006-09-27 01:49:43 -07004329 */
Yinghai Lu32996252009-12-15 17:59:02 -08004330unsigned long __meminit __absent_pages_in_range(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07004331 unsigned long range_start_pfn,
4332 unsigned long range_end_pfn)
4333{
Tejun Heo96e907d2011-07-12 10:46:29 +02004334 unsigned long nr_absent = range_end_pfn - range_start_pfn;
4335 unsigned long start_pfn, end_pfn;
4336 int i;
Mel Gormanc7132162006-09-27 01:49:43 -07004337
Tejun Heo96e907d2011-07-12 10:46:29 +02004338 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
4339 start_pfn = clamp(start_pfn, range_start_pfn, range_end_pfn);
4340 end_pfn = clamp(end_pfn, range_start_pfn, range_end_pfn);
4341 nr_absent -= end_pfn - start_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07004342 }
Tejun Heo96e907d2011-07-12 10:46:29 +02004343 return nr_absent;
Mel Gormanc7132162006-09-27 01:49:43 -07004344}
4345
4346/**
4347 * absent_pages_in_range - Return number of page frames in holes within a range
4348 * @start_pfn: The start PFN to start searching for holes
4349 * @end_pfn: The end PFN to stop searching for holes
4350 *
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004351 * It returns the number of pages frames in memory holes within a range.
Mel Gormanc7132162006-09-27 01:49:43 -07004352 */
4353unsigned long __init absent_pages_in_range(unsigned long start_pfn,
4354 unsigned long end_pfn)
4355{
4356 return __absent_pages_in_range(MAX_NUMNODES, start_pfn, end_pfn);
4357}
4358
4359/* Return the number of page frames in holes in a zone on a node */
Paul Mundt6ea6e682007-07-15 23:38:20 -07004360static unsigned long __meminit zone_absent_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07004361 unsigned long zone_type,
4362 unsigned long *ignored)
4363{
Tejun Heo96e907d2011-07-12 10:46:29 +02004364 unsigned long zone_low = arch_zone_lowest_possible_pfn[zone_type];
4365 unsigned long zone_high = arch_zone_highest_possible_pfn[zone_type];
Mel Gorman9c7cd682006-09-27 01:49:58 -07004366 unsigned long node_start_pfn, node_end_pfn;
4367 unsigned long zone_start_pfn, zone_end_pfn;
4368
4369 get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
Tejun Heo96e907d2011-07-12 10:46:29 +02004370 zone_start_pfn = clamp(node_start_pfn, zone_low, zone_high);
4371 zone_end_pfn = clamp(node_end_pfn, zone_low, zone_high);
Mel Gorman9c7cd682006-09-27 01:49:58 -07004372
Mel Gorman2a1e2742007-07-17 04:03:12 -07004373 adjust_zone_range_for_zone_movable(nid, zone_type,
4374 node_start_pfn, node_end_pfn,
4375 &zone_start_pfn, &zone_end_pfn);
Mel Gorman9c7cd682006-09-27 01:49:58 -07004376 return __absent_pages_in_range(nid, zone_start_pfn, zone_end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07004377}
Mel Gorman0e0b8642006-09-27 01:49:56 -07004378
Tang Chen6981ec32013-02-22 16:33:39 -08004379/**
4380 * sanitize_zone_movable_limit - Sanitize the zone_movable_limit array.
4381 *
4382 * zone_movable_limit is initialized as 0. This function will try to get
4383 * the first ZONE_MOVABLE pfn of each node from movablemem_map, and
4384 * assigne them to zone_movable_limit.
4385 * zone_movable_limit[nid] == 0 means no limit for the node.
4386 *
4387 * Note: Each range is represented as [start_pfn, end_pfn)
4388 */
4389static void __meminit sanitize_zone_movable_limit(void)
4390{
4391 int map_pos = 0, i, nid;
4392 unsigned long start_pfn, end_pfn;
4393
4394 if (!movablemem_map.nr_map)
4395 return;
4396
4397 /* Iterate all ranges from minimum to maximum */
4398 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) {
4399 /*
4400 * If we have found lowest pfn of ZONE_MOVABLE of the node
4401 * specified by user, just go on to check next range.
4402 */
4403 if (zone_movable_limit[nid])
4404 continue;
4405
4406#ifdef CONFIG_ZONE_DMA
4407 /* Skip DMA memory. */
4408 if (start_pfn < arch_zone_highest_possible_pfn[ZONE_DMA])
4409 start_pfn = arch_zone_highest_possible_pfn[ZONE_DMA];
4410#endif
4411
4412#ifdef CONFIG_ZONE_DMA32
4413 /* Skip DMA32 memory. */
4414 if (start_pfn < arch_zone_highest_possible_pfn[ZONE_DMA32])
4415 start_pfn = arch_zone_highest_possible_pfn[ZONE_DMA32];
4416#endif
4417
4418#ifdef CONFIG_HIGHMEM
4419 /* Skip lowmem if ZONE_MOVABLE is highmem. */
4420 if (zone_movable_is_highmem() &&
4421 start_pfn < arch_zone_lowest_possible_pfn[ZONE_HIGHMEM])
4422 start_pfn = arch_zone_lowest_possible_pfn[ZONE_HIGHMEM];
4423#endif
4424
4425 if (start_pfn >= end_pfn)
4426 continue;
4427
4428 while (map_pos < movablemem_map.nr_map) {
4429 if (end_pfn <= movablemem_map.map[map_pos].start_pfn)
4430 break;
4431
4432 if (start_pfn >= movablemem_map.map[map_pos].end_pfn) {
4433 map_pos++;
4434 continue;
4435 }
4436
4437 /*
4438 * The start_pfn of ZONE_MOVABLE is either the minimum
4439 * pfn specified by movablemem_map, or 0, which means
4440 * the node has no ZONE_MOVABLE.
4441 */
4442 zone_movable_limit[nid] = max(start_pfn,
4443 movablemem_map.map[map_pos].start_pfn);
4444
4445 break;
4446 }
4447 }
4448}
4449
Tejun Heo0ee332c2011-12-08 10:22:09 -08004450#else /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
Paul Mundt6ea6e682007-07-15 23:38:20 -07004451static inline unsigned long __meminit zone_spanned_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07004452 unsigned long zone_type,
4453 unsigned long *zones_size)
4454{
4455 return zones_size[zone_type];
4456}
4457
Paul Mundt6ea6e682007-07-15 23:38:20 -07004458static inline unsigned long __meminit zone_absent_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07004459 unsigned long zone_type,
4460 unsigned long *zholes_size)
4461{
4462 if (!zholes_size)
4463 return 0;
4464
4465 return zholes_size[zone_type];
4466}
Tejun Heo0ee332c2011-12-08 10:22:09 -08004467#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
Mel Gormanc7132162006-09-27 01:49:43 -07004468
Yasunori Gotoa3142c82007-05-08 00:23:07 -07004469static void __meminit calculate_node_totalpages(struct pglist_data *pgdat,
Mel Gormanc7132162006-09-27 01:49:43 -07004470 unsigned long *zones_size, unsigned long *zholes_size)
4471{
4472 unsigned long realtotalpages, totalpages = 0;
4473 enum zone_type i;
4474
4475 for (i = 0; i < MAX_NR_ZONES; i++)
4476 totalpages += zone_spanned_pages_in_node(pgdat->node_id, i,
4477 zones_size);
4478 pgdat->node_spanned_pages = totalpages;
4479
4480 realtotalpages = totalpages;
4481 for (i = 0; i < MAX_NR_ZONES; i++)
4482 realtotalpages -=
4483 zone_absent_pages_in_node(pgdat->node_id, i,
4484 zholes_size);
4485 pgdat->node_present_pages = realtotalpages;
4486 printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id,
4487 realtotalpages);
4488}
4489
Mel Gorman835c1342007-10-16 01:25:47 -07004490#ifndef CONFIG_SPARSEMEM
4491/*
4492 * Calculate the size of the zone->blockflags rounded to an unsigned long
Mel Gormand9c23402007-10-16 01:26:01 -07004493 * Start by making sure zonesize is a multiple of pageblock_order by rounding
4494 * up. Then use 1 NR_PAGEBLOCK_BITS worth of bits per pageblock, finally
Mel Gorman835c1342007-10-16 01:25:47 -07004495 * round what is now in bits to nearest long in bits, then return it in
4496 * bytes.
4497 */
Linus Torvalds7c455122013-02-18 09:58:02 -08004498static unsigned long __init usemap_size(unsigned long zone_start_pfn, unsigned long zonesize)
Mel Gorman835c1342007-10-16 01:25:47 -07004499{
4500 unsigned long usemapsize;
4501
Linus Torvalds7c455122013-02-18 09:58:02 -08004502 zonesize += zone_start_pfn & (pageblock_nr_pages-1);
Mel Gormand9c23402007-10-16 01:26:01 -07004503 usemapsize = roundup(zonesize, pageblock_nr_pages);
4504 usemapsize = usemapsize >> pageblock_order;
Mel Gorman835c1342007-10-16 01:25:47 -07004505 usemapsize *= NR_PAGEBLOCK_BITS;
4506 usemapsize = roundup(usemapsize, 8 * sizeof(unsigned long));
4507
4508 return usemapsize / 8;
4509}
4510
4511static void __init setup_usemap(struct pglist_data *pgdat,
Linus Torvalds7c455122013-02-18 09:58:02 -08004512 struct zone *zone,
4513 unsigned long zone_start_pfn,
4514 unsigned long zonesize)
Mel Gorman835c1342007-10-16 01:25:47 -07004515{
Linus Torvalds7c455122013-02-18 09:58:02 -08004516 unsigned long usemapsize = usemap_size(zone_start_pfn, zonesize);
Mel Gorman835c1342007-10-16 01:25:47 -07004517 zone->pageblock_flags = NULL;
Julia Lawall58a01a42009-01-06 14:39:28 -08004518 if (usemapsize)
Yinghai Lu8f389a992011-05-11 15:13:32 -07004519 zone->pageblock_flags = alloc_bootmem_node_nopanic(pgdat,
4520 usemapsize);
Mel Gorman835c1342007-10-16 01:25:47 -07004521}
4522#else
Linus Torvalds7c455122013-02-18 09:58:02 -08004523static inline void setup_usemap(struct pglist_data *pgdat, struct zone *zone,
4524 unsigned long zone_start_pfn, unsigned long zonesize) {}
Mel Gorman835c1342007-10-16 01:25:47 -07004525#endif /* CONFIG_SPARSEMEM */
4526
Mel Gormand9c23402007-10-16 01:26:01 -07004527#ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
Mel Gormanba72cb82007-11-28 16:21:13 -08004528
Mel Gormand9c23402007-10-16 01:26:01 -07004529/* Initialise the number of pages represented by NR_PAGEBLOCK_BITS */
Xishi Qiuca57df72012-07-31 16:43:19 -07004530void __init set_pageblock_order(void)
Mel Gormand9c23402007-10-16 01:26:01 -07004531{
Andrew Morton955c1cd2012-05-29 15:06:31 -07004532 unsigned int order;
4533
Mel Gormand9c23402007-10-16 01:26:01 -07004534 /* Check that pageblock_nr_pages has not already been setup */
4535 if (pageblock_order)
4536 return;
4537
Andrew Morton955c1cd2012-05-29 15:06:31 -07004538 if (HPAGE_SHIFT > PAGE_SHIFT)
4539 order = HUGETLB_PAGE_ORDER;
4540 else
4541 order = MAX_ORDER - 1;
4542
Mel Gormand9c23402007-10-16 01:26:01 -07004543 /*
4544 * Assume the largest contiguous order of interest is a huge page.
Andrew Morton955c1cd2012-05-29 15:06:31 -07004545 * This value may be variable depending on boot parameters on IA64 and
4546 * powerpc.
Mel Gormand9c23402007-10-16 01:26:01 -07004547 */
4548 pageblock_order = order;
4549}
4550#else /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
4551
Mel Gormanba72cb82007-11-28 16:21:13 -08004552/*
4553 * When CONFIG_HUGETLB_PAGE_SIZE_VARIABLE is not set, set_pageblock_order()
Andrew Morton955c1cd2012-05-29 15:06:31 -07004554 * is unused as pageblock_order is set at compile-time. See
4555 * include/linux/pageblock-flags.h for the values of pageblock_order based on
4556 * the kernel config
Mel Gormanba72cb82007-11-28 16:21:13 -08004557 */
Xishi Qiuca57df72012-07-31 16:43:19 -07004558void __init set_pageblock_order(void)
Mel Gormanba72cb82007-11-28 16:21:13 -08004559{
Mel Gormanba72cb82007-11-28 16:21:13 -08004560}
Mel Gormand9c23402007-10-16 01:26:01 -07004561
4562#endif /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
4563
Jiang Liu01cefae2012-12-12 13:52:19 -08004564static unsigned long __paginginit calc_memmap_size(unsigned long spanned_pages,
4565 unsigned long present_pages)
4566{
4567 unsigned long pages = spanned_pages;
4568
4569 /*
4570 * Provide a more accurate estimation if there are holes within
4571 * the zone and SPARSEMEM is in use. If there are holes within the
4572 * zone, each populated memory region may cost us one or two extra
4573 * memmap pages due to alignment because memmap pages for each
4574 * populated regions may not naturally algined on page boundary.
4575 * So the (present_pages >> 4) heuristic is a tradeoff for that.
4576 */
4577 if (spanned_pages > present_pages + (present_pages >> 4) &&
4578 IS_ENABLED(CONFIG_SPARSEMEM))
4579 pages = present_pages;
4580
4581 return PAGE_ALIGN(pages * sizeof(struct page)) >> PAGE_SHIFT;
4582}
4583
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584/*
4585 * Set up the zone data structures:
4586 * - mark all pages reserved
4587 * - mark all memory queues empty
4588 * - clear the memory bitmaps
Minchan Kim6527af52012-07-31 16:46:16 -07004589 *
4590 * NOTE: pgdat should get zeroed by caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 */
Alexander van Heukelumb5a0e012008-02-23 15:24:06 -08004592static void __paginginit free_area_init_core(struct pglist_data *pgdat,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 unsigned long *zones_size, unsigned long *zholes_size)
4594{
Christoph Lameter2f1b6242006-09-25 23:31:13 -07004595 enum zone_type j;
Dave Hansened8ece22005-10-29 18:16:50 -07004596 int nid = pgdat->node_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 unsigned long zone_start_pfn = pgdat->node_start_pfn;
Yasunori Goto718127c2006-06-23 02:03:10 -07004598 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599
Dave Hansen208d54e2005-10-29 18:16:52 -07004600 pgdat_resize_init(pgdat);
Andrea Arcangeli8177a422012-03-23 20:56:34 +01004601#ifdef CONFIG_NUMA_BALANCING
4602 spin_lock_init(&pgdat->numabalancing_migrate_lock);
4603 pgdat->numabalancing_migrate_nr_pages = 0;
4604 pgdat->numabalancing_migrate_next_window = jiffies;
4605#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 init_waitqueue_head(&pgdat->kswapd_wait);
Mel Gorman55150612012-07-31 16:44:35 -07004607 init_waitqueue_head(&pgdat->pfmemalloc_wait);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004608 pgdat_page_cgroup_init(pgdat);
Michal Nazarewicz5f63b722012-01-11 15:16:11 +01004609
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 for (j = 0; j < MAX_NR_ZONES; j++) {
4611 struct zone *zone = pgdat->node_zones + j;
Jiang Liu9feedc92012-12-12 13:52:12 -08004612 unsigned long size, realsize, freesize, memmap_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613
Mel Gormanc7132162006-09-27 01:49:43 -07004614 size = zone_spanned_pages_in_node(nid, j, zones_size);
Jiang Liu9feedc92012-12-12 13:52:12 -08004615 realsize = freesize = size - zone_absent_pages_in_node(nid, j,
Mel Gormanc7132162006-09-27 01:49:43 -07004616 zholes_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617
Mel Gorman0e0b8642006-09-27 01:49:56 -07004618 /*
Jiang Liu9feedc92012-12-12 13:52:12 -08004619 * Adjust freesize so that it accounts for how much memory
Mel Gorman0e0b8642006-09-27 01:49:56 -07004620 * is used by this zone for memmap. This affects the watermark
4621 * and per-cpu initialisations
4622 */
Jiang Liu01cefae2012-12-12 13:52:19 -08004623 memmap_pages = calc_memmap_size(size, realsize);
Jiang Liu9feedc92012-12-12 13:52:12 -08004624 if (freesize >= memmap_pages) {
4625 freesize -= memmap_pages;
Yinghai Lu5594c8c2009-01-06 14:39:14 -08004626 if (memmap_pages)
4627 printk(KERN_DEBUG
4628 " %s zone: %lu pages used for memmap\n",
4629 zone_names[j], memmap_pages);
Mel Gorman0e0b8642006-09-27 01:49:56 -07004630 } else
4631 printk(KERN_WARNING
Jiang Liu9feedc92012-12-12 13:52:12 -08004632 " %s zone: %lu pages exceeds freesize %lu\n",
4633 zone_names[j], memmap_pages, freesize);
Mel Gorman0e0b8642006-09-27 01:49:56 -07004634
Christoph Lameter62672762007-02-10 01:43:07 -08004635 /* Account for reserved pages */
Jiang Liu9feedc92012-12-12 13:52:12 -08004636 if (j == 0 && freesize > dma_reserve) {
4637 freesize -= dma_reserve;
Yinghai Lud903ef92008-10-18 20:27:06 -07004638 printk(KERN_DEBUG " %s zone: %lu pages reserved\n",
Christoph Lameter62672762007-02-10 01:43:07 -08004639 zone_names[0], dma_reserve);
Mel Gorman0e0b8642006-09-27 01:49:56 -07004640 }
4641
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07004642 if (!is_highmem_idx(j))
Jiang Liu9feedc92012-12-12 13:52:12 -08004643 nr_kernel_pages += freesize;
Jiang Liu01cefae2012-12-12 13:52:19 -08004644 /* Charge for highmem memmap if there are enough kernel pages */
4645 else if (nr_kernel_pages > memmap_pages * 2)
4646 nr_kernel_pages -= memmap_pages;
Jiang Liu9feedc92012-12-12 13:52:12 -08004647 nr_all_pages += freesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648
4649 zone->spanned_pages = size;
Jiang Liu9feedc92012-12-12 13:52:12 -08004650 zone->present_pages = freesize;
4651 /*
4652 * Set an approximate value for lowmem here, it will be adjusted
4653 * when the bootmem allocator frees pages into the buddy system.
4654 * And all highmem pages will be managed by the buddy system.
4655 */
4656 zone->managed_pages = is_highmem_idx(j) ? realsize : freesize;
Christoph Lameter96146342006-07-03 00:24:13 -07004657#ifdef CONFIG_NUMA
Christoph Lameterd5f541e2006-09-27 01:50:08 -07004658 zone->node = nid;
Jiang Liu9feedc92012-12-12 13:52:12 -08004659 zone->min_unmapped_pages = (freesize*sysctl_min_unmapped_ratio)
Christoph Lameter96146342006-07-03 00:24:13 -07004660 / 100;
Jiang Liu9feedc92012-12-12 13:52:12 -08004661 zone->min_slab_pages = (freesize * sysctl_min_slab_ratio) / 100;
Christoph Lameter96146342006-07-03 00:24:13 -07004662#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 zone->name = zone_names[j];
4664 spin_lock_init(&zone->lock);
4665 spin_lock_init(&zone->lru_lock);
Dave Hansenbdc8cb92005-10-29 18:16:53 -07004666 zone_seqlock_init(zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 zone->zone_pgdat = pgdat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668
Dave Hansened8ece22005-10-29 18:16:50 -07004669 zone_pcp_init(zone);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004670 lruvec_init(&zone->lruvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 if (!size)
4672 continue;
4673
Andrew Morton955c1cd2012-05-29 15:06:31 -07004674 set_pageblock_order();
Linus Torvalds7c455122013-02-18 09:58:02 -08004675 setup_usemap(pgdat, zone, zone_start_pfn, size);
Dave Hansena2f3aa022007-01-10 23:15:30 -08004676 ret = init_currently_empty_zone(zone, zone_start_pfn,
4677 size, MEMMAP_EARLY);
Yasunori Goto718127c2006-06-23 02:03:10 -07004678 BUG_ON(ret);
Heiko Carstens76cdd582008-05-14 16:05:52 -07004679 memmap_init(size, nid, j, zone_start_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 zone_start_pfn += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 }
4682}
4683
Sam Ravnborg577a32f2007-05-17 23:29:25 +02004684static void __init_refok alloc_node_mem_map(struct pglist_data *pgdat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 /* Skip empty nodes */
4687 if (!pgdat->node_spanned_pages)
4688 return;
4689
Andy Whitcroftd41dee32005-06-23 00:07:54 -07004690#ifdef CONFIG_FLAT_NODE_MEM_MAP
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 /* ia64 gets its own node_mem_map, before this, without bootmem */
4692 if (!pgdat->node_mem_map) {
Bob Piccoe984bb42006-05-20 15:00:31 -07004693 unsigned long size, start, end;
Andy Whitcroftd41dee32005-06-23 00:07:54 -07004694 struct page *map;
4695
Bob Piccoe984bb42006-05-20 15:00:31 -07004696 /*
4697 * The zone's endpoints aren't required to be MAX_ORDER
4698 * aligned but the node_mem_map endpoints must be in order
4699 * for the buddy allocator to function correctly.
4700 */
4701 start = pgdat->node_start_pfn & ~(MAX_ORDER_NR_PAGES - 1);
4702 end = pgdat->node_start_pfn + pgdat->node_spanned_pages;
4703 end = ALIGN(end, MAX_ORDER_NR_PAGES);
4704 size = (end - start) * sizeof(struct page);
Dave Hansen6f167ec2005-06-23 00:07:39 -07004705 map = alloc_remap(pgdat->node_id, size);
4706 if (!map)
Yinghai Lu8f389a992011-05-11 15:13:32 -07004707 map = alloc_bootmem_node_nopanic(pgdat, size);
Bob Piccoe984bb42006-05-20 15:00:31 -07004708 pgdat->node_mem_map = map + (pgdat->node_start_pfn - start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 }
Roman Zippel12d810c2007-05-31 00:40:54 -07004710#ifndef CONFIG_NEED_MULTIPLE_NODES
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 /*
4712 * With no DISCONTIG, the global mem_map is just set as node 0's
4713 */
Mel Gormanc7132162006-09-27 01:49:43 -07004714 if (pgdat == NODE_DATA(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 mem_map = NODE_DATA(0)->node_mem_map;
Tejun Heo0ee332c2011-12-08 10:22:09 -08004716#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Mel Gormanc7132162006-09-27 01:49:43 -07004717 if (page_to_pfn(mem_map) != pgdat->node_start_pfn)
Thomas Bogendoerfer467bc462008-01-08 15:33:11 -08004718 mem_map -= (pgdat->node_start_pfn - ARCH_PFN_OFFSET);
Tejun Heo0ee332c2011-12-08 10:22:09 -08004719#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
Mel Gormanc7132162006-09-27 01:49:43 -07004720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721#endif
Andy Whitcroftd41dee32005-06-23 00:07:54 -07004722#endif /* CONFIG_FLAT_NODE_MEM_MAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723}
4724
Johannes Weiner9109fb72008-07-23 21:27:20 -07004725void __paginginit free_area_init_node(int nid, unsigned long *zones_size,
4726 unsigned long node_start_pfn, unsigned long *zholes_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727{
Johannes Weiner9109fb72008-07-23 21:27:20 -07004728 pg_data_t *pgdat = NODE_DATA(nid);
4729
Minchan Kim88fdf752012-07-31 16:46:14 -07004730 /* pg_data_t should be reset to zero when it's allocated */
Linus Torvalds8783b6e2012-08-02 10:37:03 -07004731 WARN_ON(pgdat->nr_zones || pgdat->classzone_idx);
Minchan Kim88fdf752012-07-31 16:46:14 -07004732
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 pgdat->node_id = nid;
4734 pgdat->node_start_pfn = node_start_pfn;
David Rientjes957f8222012-10-08 16:33:24 -07004735 init_zone_allows_reclaim(nid);
Mel Gormanc7132162006-09-27 01:49:43 -07004736 calculate_node_totalpages(pgdat, zones_size, zholes_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737
4738 alloc_node_mem_map(pgdat);
Yinghai Lue8c27ac2008-06-01 13:15:22 -07004739#ifdef CONFIG_FLAT_NODE_MEM_MAP
4740 printk(KERN_DEBUG "free_area_init_node: node %d, pgdat %08lx, node_mem_map %08lx\n",
4741 nid, (unsigned long)pgdat,
4742 (unsigned long)pgdat->node_mem_map);
4743#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744
4745 free_area_init_core(pgdat, zones_size, zholes_size);
4746}
4747
Tejun Heo0ee332c2011-12-08 10:22:09 -08004748#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Miklos Szeredi418508c2007-05-23 13:57:55 -07004749
4750#if MAX_NUMNODES > 1
4751/*
4752 * Figure out the number of possible node ids.
4753 */
4754static void __init setup_nr_node_ids(void)
4755{
4756 unsigned int node;
4757 unsigned int highest = 0;
4758
4759 for_each_node_mask(node, node_possible_map)
4760 highest = node;
4761 nr_node_ids = highest + 1;
4762}
4763#else
4764static inline void setup_nr_node_ids(void)
4765{
4766}
4767#endif
4768
Mel Gormanc7132162006-09-27 01:49:43 -07004769/**
Tejun Heo1e019792011-07-12 09:45:34 +02004770 * node_map_pfn_alignment - determine the maximum internode alignment
4771 *
4772 * This function should be called after node map is populated and sorted.
4773 * It calculates the maximum power of two alignment which can distinguish
4774 * all the nodes.
4775 *
4776 * For example, if all nodes are 1GiB and aligned to 1GiB, the return value
4777 * would indicate 1GiB alignment with (1 << (30 - PAGE_SHIFT)). If the
4778 * nodes are shifted by 256MiB, 256MiB. Note that if only the last node is
4779 * shifted, 1GiB is enough and this function will indicate so.
4780 *
4781 * This is used to test whether pfn -> nid mapping of the chosen memory
4782 * model has fine enough granularity to avoid incorrect mapping for the
4783 * populated node map.
4784 *
4785 * Returns the determined alignment in pfn's. 0 if there is no alignment
4786 * requirement (single node).
4787 */
4788unsigned long __init node_map_pfn_alignment(void)
4789{
4790 unsigned long accl_mask = 0, last_end = 0;
Tejun Heoc13291a2011-07-12 10:46:30 +02004791 unsigned long start, end, mask;
Tejun Heo1e019792011-07-12 09:45:34 +02004792 int last_nid = -1;
Tejun Heoc13291a2011-07-12 10:46:30 +02004793 int i, nid;
Tejun Heo1e019792011-07-12 09:45:34 +02004794
Tejun Heoc13291a2011-07-12 10:46:30 +02004795 for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, &nid) {
Tejun Heo1e019792011-07-12 09:45:34 +02004796 if (!start || last_nid < 0 || last_nid == nid) {
4797 last_nid = nid;
4798 last_end = end;
4799 continue;
4800 }
4801
4802 /*
4803 * Start with a mask granular enough to pin-point to the
4804 * start pfn and tick off bits one-by-one until it becomes
4805 * too coarse to separate the current node from the last.
4806 */
4807 mask = ~((1 << __ffs(start)) - 1);
4808 while (mask && last_end <= (start & (mask << 1)))
4809 mask <<= 1;
4810
4811 /* accumulate all internode masks */
4812 accl_mask |= mask;
4813 }
4814
4815 /* convert mask to number of pages */
4816 return ~accl_mask + 1;
4817}
4818
Mel Gormana6af2bc2007-02-10 01:42:57 -08004819/* Find the lowest pfn for a node */
Adrian Bunkb69a7282008-07-23 21:28:12 -07004820static unsigned long __init find_min_pfn_for_node(int nid)
Mel Gormanc7132162006-09-27 01:49:43 -07004821{
Mel Gormana6af2bc2007-02-10 01:42:57 -08004822 unsigned long min_pfn = ULONG_MAX;
Tejun Heoc13291a2011-07-12 10:46:30 +02004823 unsigned long start_pfn;
4824 int i;
Mel Gorman1abbfb42006-11-23 12:01:41 +00004825
Tejun Heoc13291a2011-07-12 10:46:30 +02004826 for_each_mem_pfn_range(i, nid, &start_pfn, NULL, NULL)
4827 min_pfn = min(min_pfn, start_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07004828
Mel Gormana6af2bc2007-02-10 01:42:57 -08004829 if (min_pfn == ULONG_MAX) {
4830 printk(KERN_WARNING
Paul Jackson2bc0d262008-06-22 07:22:17 -07004831 "Could not find start_pfn for node %d\n", nid);
Mel Gormana6af2bc2007-02-10 01:42:57 -08004832 return 0;
4833 }
4834
4835 return min_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07004836}
4837
4838/**
4839 * find_min_pfn_with_active_regions - Find the minimum PFN registered
4840 *
4841 * It returns the minimum PFN based on information provided via
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004842 * add_active_range().
Mel Gormanc7132162006-09-27 01:49:43 -07004843 */
4844unsigned long __init find_min_pfn_with_active_regions(void)
4845{
4846 return find_min_pfn_for_node(MAX_NUMNODES);
4847}
4848
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07004849/*
4850 * early_calculate_totalpages()
4851 * Sum pages in active regions for movable zone.
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08004852 * Populate N_MEMORY for calculating usable_nodes.
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07004853 */
Adrian Bunk484f51f2007-10-16 01:26:03 -07004854static unsigned long __init early_calculate_totalpages(void)
Mel Gorman7e63efe2007-07-17 04:03:15 -07004855{
Mel Gorman7e63efe2007-07-17 04:03:15 -07004856 unsigned long totalpages = 0;
Tejun Heoc13291a2011-07-12 10:46:30 +02004857 unsigned long start_pfn, end_pfn;
4858 int i, nid;
Mel Gorman7e63efe2007-07-17 04:03:15 -07004859
Tejun Heoc13291a2011-07-12 10:46:30 +02004860 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) {
4861 unsigned long pages = end_pfn - start_pfn;
4862
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07004863 totalpages += pages;
4864 if (pages)
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08004865 node_set_state(nid, N_MEMORY);
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07004866 }
4867 return totalpages;
Mel Gorman7e63efe2007-07-17 04:03:15 -07004868}
4869
Mel Gorman2a1e2742007-07-17 04:03:12 -07004870/*
4871 * Find the PFN the Movable zone begins in each node. Kernel memory
4872 * is spread evenly between nodes as long as the nodes have enough
4873 * memory. When they don't, some nodes will have more kernelcore than
4874 * others
4875 */
Kautuk Consulb224ef82012-03-21 16:34:15 -07004876static void __init find_zone_movable_pfns_for_nodes(void)
Mel Gorman2a1e2742007-07-17 04:03:12 -07004877{
4878 int i, nid;
4879 unsigned long usable_startpfn;
4880 unsigned long kernelcore_node, kernelcore_remaining;
Yinghai Lu66918dc2009-06-30 11:41:37 -07004881 /* save the state before borrow the nodemask */
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08004882 nodemask_t saved_node_state = node_states[N_MEMORY];
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07004883 unsigned long totalpages = early_calculate_totalpages();
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08004884 int usable_nodes = nodes_weight(node_states[N_MEMORY]);
Mel Gorman2a1e2742007-07-17 04:03:12 -07004885
Mel Gorman7e63efe2007-07-17 04:03:15 -07004886 /*
4887 * If movablecore was specified, calculate what size of
4888 * kernelcore that corresponds so that memory usable for
4889 * any allocation type is evenly spread. If both kernelcore
4890 * and movablecore are specified, then the value of kernelcore
4891 * will be used for required_kernelcore if it's greater than
4892 * what movablecore would have allowed.
4893 */
4894 if (required_movablecore) {
Mel Gorman7e63efe2007-07-17 04:03:15 -07004895 unsigned long corepages;
4896
4897 /*
4898 * Round-up so that ZONE_MOVABLE is at least as large as what
4899 * was requested by the user
4900 */
4901 required_movablecore =
4902 roundup(required_movablecore, MAX_ORDER_NR_PAGES);
4903 corepages = totalpages - required_movablecore;
4904
4905 required_kernelcore = max(required_kernelcore, corepages);
4906 }
4907
Tang Chen42f47e22013-02-22 16:33:40 -08004908 /*
4909 * If neither kernelcore/movablecore nor movablemem_map is specified,
4910 * there is no ZONE_MOVABLE. But if movablemem_map is specified, the
4911 * start pfn of ZONE_MOVABLE has been stored in zone_movable_limit[].
4912 */
4913 if (!required_kernelcore) {
4914 if (movablemem_map.nr_map)
4915 memcpy(zone_movable_pfn, zone_movable_limit,
4916 sizeof(zone_movable_pfn));
Yinghai Lu66918dc2009-06-30 11:41:37 -07004917 goto out;
Tang Chen42f47e22013-02-22 16:33:40 -08004918 }
Mel Gorman2a1e2742007-07-17 04:03:12 -07004919
4920 /* usable_startpfn is the lowest possible pfn ZONE_MOVABLE can be at */
Mel Gorman2a1e2742007-07-17 04:03:12 -07004921 usable_startpfn = arch_zone_lowest_possible_pfn[movable_zone];
4922
4923restart:
4924 /* Spread kernelcore memory as evenly as possible throughout nodes */
4925 kernelcore_node = required_kernelcore / usable_nodes;
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08004926 for_each_node_state(nid, N_MEMORY) {
Tejun Heoc13291a2011-07-12 10:46:30 +02004927 unsigned long start_pfn, end_pfn;
4928
Mel Gorman2a1e2742007-07-17 04:03:12 -07004929 /*
4930 * Recalculate kernelcore_node if the division per node
4931 * now exceeds what is necessary to satisfy the requested
4932 * amount of memory for the kernel
4933 */
4934 if (required_kernelcore < kernelcore_node)
4935 kernelcore_node = required_kernelcore / usable_nodes;
4936
4937 /*
4938 * As the map is walked, we track how much memory is usable
4939 * by the kernel using kernelcore_remaining. When it is
4940 * 0, the rest of the node is usable by ZONE_MOVABLE
4941 */
4942 kernelcore_remaining = kernelcore_node;
4943
4944 /* Go through each range of PFNs within this node */
Tejun Heoc13291a2011-07-12 10:46:30 +02004945 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
Mel Gorman2a1e2742007-07-17 04:03:12 -07004946 unsigned long size_pages;
4947
Tang Chen42f47e22013-02-22 16:33:40 -08004948 /*
4949 * Find more memory for kernelcore in
4950 * [zone_movable_pfn[nid], zone_movable_limit[nid]).
4951 */
Tejun Heoc13291a2011-07-12 10:46:30 +02004952 start_pfn = max(start_pfn, zone_movable_pfn[nid]);
Mel Gorman2a1e2742007-07-17 04:03:12 -07004953 if (start_pfn >= end_pfn)
4954 continue;
4955
Tang Chen42f47e22013-02-22 16:33:40 -08004956 if (zone_movable_limit[nid]) {
4957 end_pfn = min(end_pfn, zone_movable_limit[nid]);
4958 /* No range left for kernelcore in this node */
4959 if (start_pfn >= end_pfn) {
4960 zone_movable_pfn[nid] =
4961 zone_movable_limit[nid];
4962 break;
4963 }
4964 }
4965
Mel Gorman2a1e2742007-07-17 04:03:12 -07004966 /* Account for what is only usable for kernelcore */
4967 if (start_pfn < usable_startpfn) {
4968 unsigned long kernel_pages;
4969 kernel_pages = min(end_pfn, usable_startpfn)
4970 - start_pfn;
4971
4972 kernelcore_remaining -= min(kernel_pages,
4973 kernelcore_remaining);
4974 required_kernelcore -= min(kernel_pages,
4975 required_kernelcore);
4976
4977 /* Continue if range is now fully accounted */
4978 if (end_pfn <= usable_startpfn) {
4979
4980 /*
4981 * Push zone_movable_pfn to the end so
4982 * that if we have to rebalance
4983 * kernelcore across nodes, we will
4984 * not double account here
4985 */
4986 zone_movable_pfn[nid] = end_pfn;
4987 continue;
4988 }
4989 start_pfn = usable_startpfn;
4990 }
4991
4992 /*
4993 * The usable PFN range for ZONE_MOVABLE is from
4994 * start_pfn->end_pfn. Calculate size_pages as the
4995 * number of pages used as kernelcore
4996 */
4997 size_pages = end_pfn - start_pfn;
4998 if (size_pages > kernelcore_remaining)
4999 size_pages = kernelcore_remaining;
5000 zone_movable_pfn[nid] = start_pfn + size_pages;
5001
5002 /*
5003 * Some kernelcore has been met, update counts and
5004 * break if the kernelcore for this node has been
5005 * satisified
5006 */
5007 required_kernelcore -= min(required_kernelcore,
5008 size_pages);
5009 kernelcore_remaining -= size_pages;
5010 if (!kernelcore_remaining)
5011 break;
5012 }
5013 }
5014
5015 /*
5016 * If there is still required_kernelcore, we do another pass with one
5017 * less node in the count. This will push zone_movable_pfn[nid] further
5018 * along on the nodes that still have memory until kernelcore is
5019 * satisified
5020 */
5021 usable_nodes--;
5022 if (usable_nodes && required_kernelcore > usable_nodes)
5023 goto restart;
5024
Tang Chen42f47e22013-02-22 16:33:40 -08005025out:
Mel Gorman2a1e2742007-07-17 04:03:12 -07005026 /* Align start of ZONE_MOVABLE on all nids to MAX_ORDER_NR_PAGES */
5027 for (nid = 0; nid < MAX_NUMNODES; nid++)
5028 zone_movable_pfn[nid] =
5029 roundup(zone_movable_pfn[nid], MAX_ORDER_NR_PAGES);
Yinghai Lu66918dc2009-06-30 11:41:37 -07005030
Yinghai Lu66918dc2009-06-30 11:41:37 -07005031 /* restore the node_state */
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08005032 node_states[N_MEMORY] = saved_node_state;
Mel Gorman2a1e2742007-07-17 04:03:12 -07005033}
5034
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08005035/* Any regular or high memory on that node ? */
5036static void check_for_memory(pg_data_t *pgdat, int nid)
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07005037{
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07005038 enum zone_type zone_type;
5039
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08005040 if (N_MEMORY == N_NORMAL_MEMORY)
5041 return;
5042
5043 for (zone_type = 0; zone_type <= ZONE_MOVABLE - 1; zone_type++) {
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07005044 struct zone *zone = &pgdat->node_zones[zone_type];
Bob Liud0048b02012-01-12 17:19:07 -08005045 if (zone->present_pages) {
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08005046 node_set_state(nid, N_HIGH_MEMORY);
5047 if (N_NORMAL_MEMORY != N_HIGH_MEMORY &&
5048 zone_type <= ZONE_NORMAL)
5049 node_set_state(nid, N_NORMAL_MEMORY);
Bob Liud0048b02012-01-12 17:19:07 -08005050 break;
5051 }
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07005052 }
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07005053}
5054
Mel Gormanc7132162006-09-27 01:49:43 -07005055/**
5056 * free_area_init_nodes - Initialise all pg_data_t and zone data
Randy Dunlap88ca3b92006-10-04 02:15:25 -07005057 * @max_zone_pfn: an array of max PFNs for each zone
Mel Gormanc7132162006-09-27 01:49:43 -07005058 *
5059 * This will call free_area_init_node() for each active node in the system.
5060 * Using the page ranges provided by add_active_range(), the size of each
5061 * zone in each node and their holes is calculated. If the maximum PFN
5062 * between two adjacent zones match, it is assumed that the zone is empty.
5063 * For example, if arch_max_dma_pfn == arch_max_dma32_pfn, it is assumed
5064 * that arch_max_dma32_pfn has no pages. It is also assumed that a zone
5065 * starts where the previous one ended. For example, ZONE_DMA32 starts
5066 * at arch_max_dma_pfn.
5067 */
5068void __init free_area_init_nodes(unsigned long *max_zone_pfn)
5069{
Tejun Heoc13291a2011-07-12 10:46:30 +02005070 unsigned long start_pfn, end_pfn;
5071 int i, nid;
Mel Gormana6af2bc2007-02-10 01:42:57 -08005072
Mel Gormanc7132162006-09-27 01:49:43 -07005073 /* Record where the zone boundaries are */
5074 memset(arch_zone_lowest_possible_pfn, 0,
5075 sizeof(arch_zone_lowest_possible_pfn));
5076 memset(arch_zone_highest_possible_pfn, 0,
5077 sizeof(arch_zone_highest_possible_pfn));
5078 arch_zone_lowest_possible_pfn[0] = find_min_pfn_with_active_regions();
5079 arch_zone_highest_possible_pfn[0] = max_zone_pfn[0];
5080 for (i = 1; i < MAX_NR_ZONES; i++) {
Mel Gorman2a1e2742007-07-17 04:03:12 -07005081 if (i == ZONE_MOVABLE)
5082 continue;
Mel Gormanc7132162006-09-27 01:49:43 -07005083 arch_zone_lowest_possible_pfn[i] =
5084 arch_zone_highest_possible_pfn[i-1];
5085 arch_zone_highest_possible_pfn[i] =
5086 max(max_zone_pfn[i], arch_zone_lowest_possible_pfn[i]);
5087 }
Mel Gorman2a1e2742007-07-17 04:03:12 -07005088 arch_zone_lowest_possible_pfn[ZONE_MOVABLE] = 0;
5089 arch_zone_highest_possible_pfn[ZONE_MOVABLE] = 0;
5090
5091 /* Find the PFNs that ZONE_MOVABLE begins at in each node */
5092 memset(zone_movable_pfn, 0, sizeof(zone_movable_pfn));
Tang Chen6981ec32013-02-22 16:33:39 -08005093 find_usable_zone_for_movable();
5094 sanitize_zone_movable_limit();
Kautuk Consulb224ef82012-03-21 16:34:15 -07005095 find_zone_movable_pfns_for_nodes();
Mel Gormanc7132162006-09-27 01:49:43 -07005096
Mel Gormanc7132162006-09-27 01:49:43 -07005097 /* Print out the zone ranges */
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07005098 printk("Zone ranges:\n");
Mel Gorman2a1e2742007-07-17 04:03:12 -07005099 for (i = 0; i < MAX_NR_ZONES; i++) {
5100 if (i == ZONE_MOVABLE)
5101 continue;
Kay Sievers155cbfc2012-05-08 17:24:14 +02005102 printk(KERN_CONT " %-8s ", zone_names[i]);
David Rientjes72f0ba02010-03-05 13:42:14 -08005103 if (arch_zone_lowest_possible_pfn[i] ==
5104 arch_zone_highest_possible_pfn[i])
Kay Sievers155cbfc2012-05-08 17:24:14 +02005105 printk(KERN_CONT "empty\n");
David Rientjes72f0ba02010-03-05 13:42:14 -08005106 else
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07005107 printk(KERN_CONT "[mem %0#10lx-%0#10lx]\n",
5108 arch_zone_lowest_possible_pfn[i] << PAGE_SHIFT,
5109 (arch_zone_highest_possible_pfn[i]
5110 << PAGE_SHIFT) - 1);
Mel Gorman2a1e2742007-07-17 04:03:12 -07005111 }
5112
5113 /* Print out the PFNs ZONE_MOVABLE begins at in each node */
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07005114 printk("Movable zone start for each node\n");
Mel Gorman2a1e2742007-07-17 04:03:12 -07005115 for (i = 0; i < MAX_NUMNODES; i++) {
5116 if (zone_movable_pfn[i])
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07005117 printk(" Node %d: %#010lx\n", i,
5118 zone_movable_pfn[i] << PAGE_SHIFT);
Mel Gorman2a1e2742007-07-17 04:03:12 -07005119 }
Mel Gormanc7132162006-09-27 01:49:43 -07005120
Wanpeng Lif2d52fe2012-10-08 16:32:24 -07005121 /* Print out the early node map */
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07005122 printk("Early memory node ranges\n");
Tejun Heoc13291a2011-07-12 10:46:30 +02005123 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid)
Bjorn Helgaasa62e2f42012-05-29 15:06:30 -07005124 printk(" node %3d: [mem %#010lx-%#010lx]\n", nid,
5125 start_pfn << PAGE_SHIFT, (end_pfn << PAGE_SHIFT) - 1);
Mel Gormanc7132162006-09-27 01:49:43 -07005126
5127 /* Initialise every node */
Mel Gorman708614e2008-07-23 21:26:51 -07005128 mminit_verify_pageflags_layout();
Christoph Lameter8ef82862007-02-20 13:57:52 -08005129 setup_nr_node_ids();
Mel Gormanc7132162006-09-27 01:49:43 -07005130 for_each_online_node(nid) {
5131 pg_data_t *pgdat = NODE_DATA(nid);
Johannes Weiner9109fb72008-07-23 21:27:20 -07005132 free_area_init_node(nid, NULL,
Mel Gormanc7132162006-09-27 01:49:43 -07005133 find_min_pfn_for_node(nid), NULL);
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07005134
5135 /* Any memory on that node */
5136 if (pgdat->node_present_pages)
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -08005137 node_set_state(nid, N_MEMORY);
5138 check_for_memory(pgdat, nid);
Mel Gormanc7132162006-09-27 01:49:43 -07005139 }
5140}
Mel Gorman2a1e2742007-07-17 04:03:12 -07005141
Mel Gorman7e63efe2007-07-17 04:03:15 -07005142static int __init cmdline_parse_core(char *p, unsigned long *core)
Mel Gorman2a1e2742007-07-17 04:03:12 -07005143{
5144 unsigned long long coremem;
5145 if (!p)
5146 return -EINVAL;
5147
5148 coremem = memparse(p, &p);
Mel Gorman7e63efe2007-07-17 04:03:15 -07005149 *core = coremem >> PAGE_SHIFT;
Mel Gorman2a1e2742007-07-17 04:03:12 -07005150
Mel Gorman7e63efe2007-07-17 04:03:15 -07005151 /* Paranoid check that UL is enough for the coremem value */
Mel Gorman2a1e2742007-07-17 04:03:12 -07005152 WARN_ON((coremem >> PAGE_SHIFT) > ULONG_MAX);
5153
5154 return 0;
5155}
Mel Gormaned7ed362007-07-17 04:03:14 -07005156
Mel Gorman7e63efe2007-07-17 04:03:15 -07005157/*
5158 * kernelcore=size sets the amount of memory for use for allocations that
5159 * cannot be reclaimed or migrated.
5160 */
5161static int __init cmdline_parse_kernelcore(char *p)
5162{
5163 return cmdline_parse_core(p, &required_kernelcore);
5164}
5165
5166/*
5167 * movablecore=size sets the amount of memory for use for allocations that
5168 * can be reclaimed or migrated.
5169 */
5170static int __init cmdline_parse_movablecore(char *p)
5171{
5172 return cmdline_parse_core(p, &required_movablecore);
5173}
5174
Mel Gormaned7ed362007-07-17 04:03:14 -07005175early_param("kernelcore", cmdline_parse_kernelcore);
Mel Gorman7e63efe2007-07-17 04:03:15 -07005176early_param("movablecore", cmdline_parse_movablecore);
Mel Gormaned7ed362007-07-17 04:03:14 -07005177
Tang Chen34b71f12013-02-22 16:33:37 -08005178/**
5179 * insert_movablemem_map - Insert a memory range in to movablemem_map.map.
5180 * @start_pfn: start pfn of the range
5181 * @end_pfn: end pfn of the range
5182 *
5183 * This function will also merge the overlapped ranges, and sort the array
5184 * by start_pfn in monotonic increasing order.
5185 */
5186static void __init insert_movablemem_map(unsigned long start_pfn,
5187 unsigned long end_pfn)
5188{
5189 int pos, overlap;
5190
5191 /*
5192 * pos will be at the 1st overlapped range, or the position
5193 * where the element should be inserted.
5194 */
5195 for (pos = 0; pos < movablemem_map.nr_map; pos++)
5196 if (start_pfn <= movablemem_map.map[pos].end_pfn)
5197 break;
5198
5199 /* If there is no overlapped range, just insert the element. */
5200 if (pos == movablemem_map.nr_map ||
5201 end_pfn < movablemem_map.map[pos].start_pfn) {
5202 /*
5203 * If pos is not the end of array, we need to move all
5204 * the rest elements backward.
5205 */
5206 if (pos < movablemem_map.nr_map)
5207 memmove(&movablemem_map.map[pos+1],
5208 &movablemem_map.map[pos],
5209 sizeof(struct movablemem_entry) *
5210 (movablemem_map.nr_map - pos));
5211 movablemem_map.map[pos].start_pfn = start_pfn;
5212 movablemem_map.map[pos].end_pfn = end_pfn;
5213 movablemem_map.nr_map++;
5214 return;
5215 }
5216
5217 /* overlap will be at the last overlapped range */
5218 for (overlap = pos + 1; overlap < movablemem_map.nr_map; overlap++)
5219 if (end_pfn < movablemem_map.map[overlap].start_pfn)
5220 break;
5221
5222 /*
5223 * If there are more ranges overlapped, we need to merge them,
5224 * and move the rest elements forward.
5225 */
5226 overlap--;
5227 movablemem_map.map[pos].start_pfn = min(start_pfn,
5228 movablemem_map.map[pos].start_pfn);
5229 movablemem_map.map[pos].end_pfn = max(end_pfn,
5230 movablemem_map.map[overlap].end_pfn);
5231
5232 if (pos != overlap && overlap + 1 != movablemem_map.nr_map)
5233 memmove(&movablemem_map.map[pos+1],
5234 &movablemem_map.map[overlap+1],
5235 sizeof(struct movablemem_entry) *
5236 (movablemem_map.nr_map - overlap - 1));
5237
5238 movablemem_map.nr_map -= overlap - pos;
5239}
5240
5241/**
5242 * movablemem_map_add_region - Add a memory range into movablemem_map.
5243 * @start: physical start address of range
5244 * @end: physical end address of range
5245 *
5246 * This function transform the physical address into pfn, and then add the
5247 * range into movablemem_map by calling insert_movablemem_map().
5248 */
5249static void __init movablemem_map_add_region(u64 start, u64 size)
5250{
5251 unsigned long start_pfn, end_pfn;
5252
5253 /* In case size == 0 or start + size overflows */
5254 if (start + size <= start)
5255 return;
5256
5257 if (movablemem_map.nr_map >= ARRAY_SIZE(movablemem_map.map)) {
5258 pr_err("movablemem_map: too many entries;"
5259 " ignoring [mem %#010llx-%#010llx]\n",
5260 (unsigned long long) start,
5261 (unsigned long long) (start + size - 1));
5262 return;
5263 }
5264
5265 start_pfn = PFN_DOWN(start);
5266 end_pfn = PFN_UP(start + size);
5267 insert_movablemem_map(start_pfn, end_pfn);
5268}
5269
5270/*
5271 * cmdline_parse_movablemem_map - Parse boot option movablemem_map.
5272 * @p: The boot option of the following format:
5273 * movablemem_map=nn[KMG]@ss[KMG]
5274 *
5275 * This option sets the memory range [ss, ss+nn) to be used as movable memory.
5276 *
5277 * Return: 0 on success or -EINVAL on failure.
5278 */
5279static int __init cmdline_parse_movablemem_map(char *p)
5280{
5281 char *oldp;
5282 u64 start_at, mem_size;
5283
5284 if (!p)
5285 goto err;
5286
5287 oldp = p;
5288 mem_size = memparse(p, &p);
5289 if (p == oldp)
5290 goto err;
5291
5292 if (*p == '@') {
5293 oldp = ++p;
5294 start_at = memparse(p, &p);
5295 if (p == oldp || *p != '\0')
5296 goto err;
5297
5298 movablemem_map_add_region(start_at, mem_size);
5299 return 0;
5300 }
5301err:
5302 return -EINVAL;
5303}
5304early_param("movablemem_map", cmdline_parse_movablemem_map);
5305
Tejun Heo0ee332c2011-12-08 10:22:09 -08005306#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
Mel Gormanc7132162006-09-27 01:49:43 -07005307
Mel Gorman0e0b8642006-09-27 01:49:56 -07005308/**
Randy Dunlap88ca3b92006-10-04 02:15:25 -07005309 * set_dma_reserve - set the specified number of pages reserved in the first zone
5310 * @new_dma_reserve: The number of pages to mark reserved
Mel Gorman0e0b8642006-09-27 01:49:56 -07005311 *
5312 * The per-cpu batchsize and zone watermarks are determined by present_pages.
5313 * In the DMA zone, a significant percentage may be consumed by kernel image
5314 * and other unfreeable allocations which can skew the watermarks badly. This
Randy Dunlap88ca3b92006-10-04 02:15:25 -07005315 * function may optionally be used to account for unfreeable pages in the
5316 * first zone (e.g., ZONE_DMA). The effect will be lower watermarks and
5317 * smaller per-cpu batchsize.
Mel Gorman0e0b8642006-09-27 01:49:56 -07005318 */
5319void __init set_dma_reserve(unsigned long new_dma_reserve)
5320{
5321 dma_reserve = new_dma_reserve;
5322}
5323
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324void __init free_area_init(unsigned long *zones_size)
5325{
Johannes Weiner9109fb72008-07-23 21:27:20 -07005326 free_area_init_node(0, zones_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
5328}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330static int page_alloc_cpu_notify(struct notifier_block *self,
5331 unsigned long action, void *hcpu)
5332{
5333 int cpu = (unsigned long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005335 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
Konstantin Khlebnikovf0cb3c72012-03-21 16:34:06 -07005336 lru_add_drain_cpu(cpu);
Christoph Lameter9f8f2172008-02-04 22:29:11 -08005337 drain_pages(cpu);
5338
5339 /*
5340 * Spill the event counters of the dead processor
5341 * into the current processors event counters.
5342 * This artificially elevates the count of the current
5343 * processor.
5344 */
Christoph Lameterf8891e52006-06-30 01:55:45 -07005345 vm_events_fold_cpu(cpu);
Christoph Lameter9f8f2172008-02-04 22:29:11 -08005346
5347 /*
5348 * Zero the differential counters of the dead processor
5349 * so that the vm statistics are consistent.
5350 *
5351 * This is only okay since the processor is dead and cannot
5352 * race with what we are doing.
5353 */
Christoph Lameter2244b952006-06-30 01:55:33 -07005354 refresh_cpu_vm_stats(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 }
5356 return NOTIFY_OK;
5357}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358
5359void __init page_alloc_init(void)
5360{
5361 hotcpu_notifier(page_alloc_cpu_notify, 0);
5362}
5363
5364/*
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07005365 * calculate_totalreserve_pages - called when sysctl_lower_zone_reserve_ratio
5366 * or min_free_kbytes changes.
5367 */
5368static void calculate_totalreserve_pages(void)
5369{
5370 struct pglist_data *pgdat;
5371 unsigned long reserve_pages = 0;
Christoph Lameter2f6726e2006-09-25 23:31:18 -07005372 enum zone_type i, j;
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07005373
5374 for_each_online_pgdat(pgdat) {
5375 for (i = 0; i < MAX_NR_ZONES; i++) {
5376 struct zone *zone = pgdat->node_zones + i;
5377 unsigned long max = 0;
5378
5379 /* Find valid and maximum lowmem_reserve in the zone */
5380 for (j = i; j < MAX_NR_ZONES; j++) {
5381 if (zone->lowmem_reserve[j] > max)
5382 max = zone->lowmem_reserve[j];
5383 }
5384
Mel Gorman41858962009-06-16 15:32:12 -07005385 /* we treat the high watermark as reserved pages. */
5386 max += high_wmark_pages(zone);
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07005387
5388 if (max > zone->present_pages)
5389 max = zone->present_pages;
5390 reserve_pages += max;
Johannes Weinerab8fabd2012-01-10 15:07:42 -08005391 /*
5392 * Lowmem reserves are not available to
5393 * GFP_HIGHUSER page cache allocations and
5394 * kswapd tries to balance zones to their high
5395 * watermark. As a result, neither should be
5396 * regarded as dirtyable memory, to prevent a
5397 * situation where reclaim has to clean pages
5398 * in order to balance the zones.
5399 */
5400 zone->dirty_balance_reserve = max;
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07005401 }
5402 }
Johannes Weinerab8fabd2012-01-10 15:07:42 -08005403 dirty_balance_reserve = reserve_pages;
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07005404 totalreserve_pages = reserve_pages;
5405}
5406
5407/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 * setup_per_zone_lowmem_reserve - called whenever
5409 * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
5410 * has a correct pages reserved value, so an adequate number of
5411 * pages are left in the zone after a successful __alloc_pages().
5412 */
5413static void setup_per_zone_lowmem_reserve(void)
5414{
5415 struct pglist_data *pgdat;
Christoph Lameter2f6726e2006-09-25 23:31:18 -07005416 enum zone_type j, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417
KAMEZAWA Hiroyukiec936fc2006-03-27 01:15:59 -08005418 for_each_online_pgdat(pgdat) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 for (j = 0; j < MAX_NR_ZONES; j++) {
5420 struct zone *zone = pgdat->node_zones + j;
5421 unsigned long present_pages = zone->present_pages;
5422
5423 zone->lowmem_reserve[j] = 0;
5424
Christoph Lameter2f6726e2006-09-25 23:31:18 -07005425 idx = j;
5426 while (idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 struct zone *lower_zone;
5428
Christoph Lameter2f6726e2006-09-25 23:31:18 -07005429 idx--;
5430
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 if (sysctl_lowmem_reserve_ratio[idx] < 1)
5432 sysctl_lowmem_reserve_ratio[idx] = 1;
5433
5434 lower_zone = pgdat->node_zones + idx;
5435 lower_zone->lowmem_reserve[j] = present_pages /
5436 sysctl_lowmem_reserve_ratio[idx];
5437 present_pages += lower_zone->present_pages;
5438 }
5439 }
5440 }
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07005441
5442 /* update totalreserve_pages */
5443 calculate_totalreserve_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444}
5445
Mel Gormancfd3da12011-04-25 21:36:42 +00005446static void __setup_per_zone_wmarks(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447{
5448 unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
5449 unsigned long lowmem_pages = 0;
5450 struct zone *zone;
5451 unsigned long flags;
5452
5453 /* Calculate total number of !ZONE_HIGHMEM pages */
5454 for_each_zone(zone) {
5455 if (!is_highmem(zone))
5456 lowmem_pages += zone->present_pages;
5457 }
5458
5459 for_each_zone(zone) {
Andrew Mortonac924c62006-05-15 09:43:59 -07005460 u64 tmp;
5461
Gerald Schaefer1125b4e2008-10-18 20:27:11 -07005462 spin_lock_irqsave(&zone->lock, flags);
Andrew Mortonac924c62006-05-15 09:43:59 -07005463 tmp = (u64)pages_min * zone->present_pages;
5464 do_div(tmp, lowmem_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 if (is_highmem(zone)) {
5466 /*
Nick Piggin669ed172005-11-13 16:06:45 -08005467 * __GFP_HIGH and PF_MEMALLOC allocations usually don't
5468 * need highmem pages, so cap pages_min to a small
5469 * value here.
5470 *
Mel Gorman41858962009-06-16 15:32:12 -07005471 * The WMARK_HIGH-WMARK_LOW and (WMARK_LOW-WMARK_MIN)
Nick Piggin669ed172005-11-13 16:06:45 -08005472 * deltas controls asynch page reclaim, and so should
5473 * not be capped for highmem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 */
Andrew Morton90ae8d62013-02-22 16:32:22 -08005475 unsigned long min_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476
5477 min_pages = zone->present_pages / 1024;
Andrew Morton90ae8d62013-02-22 16:32:22 -08005478 min_pages = clamp(min_pages, SWAP_CLUSTER_MAX, 128UL);
Mel Gorman41858962009-06-16 15:32:12 -07005479 zone->watermark[WMARK_MIN] = min_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 } else {
Nick Piggin669ed172005-11-13 16:06:45 -08005481 /*
5482 * If it's a lowmem zone, reserve a number of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 * proportionate to the zone's size.
5484 */
Mel Gorman41858962009-06-16 15:32:12 -07005485 zone->watermark[WMARK_MIN] = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 }
5487
Mel Gorman41858962009-06-16 15:32:12 -07005488 zone->watermark[WMARK_LOW] = min_wmark_pages(zone) + (tmp >> 2);
5489 zone->watermark[WMARK_HIGH] = min_wmark_pages(zone) + (tmp >> 1);
Marek Szyprowski49f223a2012-01-25 12:49:24 +01005490
Mel Gorman56fd56b2007-10-16 01:25:58 -07005491 setup_zone_migrate_reserve(zone);
Gerald Schaefer1125b4e2008-10-18 20:27:11 -07005492 spin_unlock_irqrestore(&zone->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 }
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07005494
5495 /* update totalreserve_pages */
5496 calculate_totalreserve_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497}
5498
Mel Gormancfd3da12011-04-25 21:36:42 +00005499/**
5500 * setup_per_zone_wmarks - called when min_free_kbytes changes
5501 * or when memory is hot-{added|removed}
5502 *
5503 * Ensures that the watermark[min,low,high] values for each zone are set
5504 * correctly with respect to min_free_kbytes.
5505 */
5506void setup_per_zone_wmarks(void)
5507{
5508 mutex_lock(&zonelists_mutex);
5509 __setup_per_zone_wmarks();
5510 mutex_unlock(&zonelists_mutex);
5511}
5512
Randy Dunlap55a44622009-09-21 17:01:20 -07005513/*
Rik van Riel556adec2008-10-18 20:26:34 -07005514 * The inactive anon list should be small enough that the VM never has to
5515 * do too much work, but large enough that each inactive page has a chance
5516 * to be referenced again before it is swapped out.
5517 *
5518 * The inactive_anon ratio is the target ratio of ACTIVE_ANON to
5519 * INACTIVE_ANON pages on this zone's LRU, maintained by the
5520 * pageout code. A zone->inactive_ratio of 3 means 3:1 or 25% of
5521 * the anonymous pages are kept on the inactive list.
5522 *
5523 * total target max
5524 * memory ratio inactive anon
5525 * -------------------------------------
5526 * 10MB 1 5MB
5527 * 100MB 1 50MB
5528 * 1GB 3 250MB
5529 * 10GB 10 0.9GB
5530 * 100GB 31 3GB
5531 * 1TB 101 10GB
5532 * 10TB 320 32GB
5533 */
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07005534static void __meminit calculate_zone_inactive_ratio(struct zone *zone)
Minchan Kim96cb4df2009-06-16 15:32:49 -07005535{
5536 unsigned int gb, ratio;
5537
5538 /* Zone size in gigabytes */
5539 gb = zone->present_pages >> (30 - PAGE_SHIFT);
5540 if (gb)
5541 ratio = int_sqrt(10 * gb);
5542 else
5543 ratio = 1;
5544
5545 zone->inactive_ratio = ratio;
5546}
5547
KOSAKI Motohiro839a4fc2011-05-24 17:11:31 -07005548static void __meminit setup_per_zone_inactive_ratio(void)
Rik van Riel556adec2008-10-18 20:26:34 -07005549{
5550 struct zone *zone;
5551
Minchan Kim96cb4df2009-06-16 15:32:49 -07005552 for_each_zone(zone)
5553 calculate_zone_inactive_ratio(zone);
Rik van Riel556adec2008-10-18 20:26:34 -07005554}
5555
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556/*
5557 * Initialise min_free_kbytes.
5558 *
5559 * For small machines we want it small (128k min). For large machines
5560 * we want it large (64MB max). But it is not linear, because network
5561 * bandwidth does not increase linearly with machine size. We use
5562 *
5563 * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
5564 * min_free_kbytes = sqrt(lowmem_kbytes * 16)
5565 *
5566 * which yields
5567 *
5568 * 16MB: 512k
5569 * 32MB: 724k
5570 * 64MB: 1024k
5571 * 128MB: 1448k
5572 * 256MB: 2048k
5573 * 512MB: 2896k
5574 * 1024MB: 4096k
5575 * 2048MB: 5792k
5576 * 4096MB: 8192k
5577 * 8192MB: 11584k
5578 * 16384MB: 16384k
5579 */
KOSAKI Motohiro1b79acc2011-05-24 17:11:32 -07005580int __meminit init_per_zone_wmark_min(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581{
5582 unsigned long lowmem_kbytes;
5583
5584 lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
5585
5586 min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
5587 if (min_free_kbytes < 128)
5588 min_free_kbytes = 128;
5589 if (min_free_kbytes > 65536)
5590 min_free_kbytes = 65536;
Minchan Kimbc75d332009-06-16 15:32:48 -07005591 setup_per_zone_wmarks();
KOSAKI Motohiroa6cccdc2011-05-24 17:11:33 -07005592 refresh_zone_stat_thresholds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 setup_per_zone_lowmem_reserve();
Rik van Riel556adec2008-10-18 20:26:34 -07005594 setup_per_zone_inactive_ratio();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 return 0;
5596}
Minchan Kimbc75d332009-06-16 15:32:48 -07005597module_init(init_per_zone_wmark_min)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598
5599/*
5600 * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
5601 * that we can call two helper functions whenever min_free_kbytes
5602 * changes.
5603 */
5604int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005605 void __user *buffer, size_t *length, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005607 proc_dointvec(table, write, buffer, length, ppos);
Mel Gorman3b1d92c2007-05-06 14:49:30 -07005608 if (write)
Minchan Kimbc75d332009-06-16 15:32:48 -07005609 setup_per_zone_wmarks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 return 0;
5611}
5612
Christoph Lameter96146342006-07-03 00:24:13 -07005613#ifdef CONFIG_NUMA
5614int sysctl_min_unmapped_ratio_sysctl_handler(ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005615 void __user *buffer, size_t *length, loff_t *ppos)
Christoph Lameter96146342006-07-03 00:24:13 -07005616{
5617 struct zone *zone;
5618 int rc;
5619
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005620 rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
Christoph Lameter96146342006-07-03 00:24:13 -07005621 if (rc)
5622 return rc;
5623
5624 for_each_zone(zone)
Christoph Lameter8417bba2006-09-25 23:31:51 -07005625 zone->min_unmapped_pages = (zone->present_pages *
Christoph Lameter96146342006-07-03 00:24:13 -07005626 sysctl_min_unmapped_ratio) / 100;
5627 return 0;
5628}
Christoph Lameter0ff38492006-09-25 23:31:52 -07005629
5630int sysctl_min_slab_ratio_sysctl_handler(ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005631 void __user *buffer, size_t *length, loff_t *ppos)
Christoph Lameter0ff38492006-09-25 23:31:52 -07005632{
5633 struct zone *zone;
5634 int rc;
5635
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005636 rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
Christoph Lameter0ff38492006-09-25 23:31:52 -07005637 if (rc)
5638 return rc;
5639
5640 for_each_zone(zone)
5641 zone->min_slab_pages = (zone->present_pages *
5642 sysctl_min_slab_ratio) / 100;
5643 return 0;
5644}
Christoph Lameter96146342006-07-03 00:24:13 -07005645#endif
5646
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647/*
5648 * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
5649 * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
5650 * whenever sysctl_lowmem_reserve_ratio changes.
5651 *
5652 * The reserve ratio obviously has absolutely no relation with the
Mel Gorman41858962009-06-16 15:32:12 -07005653 * minimum watermarks. The lowmem reserve ratio can only make sense
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 * if in function of the boot time zone sizes.
5655 */
5656int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005657 void __user *buffer, size_t *length, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005659 proc_dointvec_minmax(table, write, buffer, length, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 setup_per_zone_lowmem_reserve();
5661 return 0;
5662}
5663
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08005664/*
5665 * percpu_pagelist_fraction - changes the pcp->high for each zone on each
5666 * cpu. It is the fraction of total pages in each zone that a hot per cpu pagelist
5667 * can have before it gets flushed back to buddy allocator.
5668 */
5669
5670int percpu_pagelist_fraction_sysctl_handler(ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005671 void __user *buffer, size_t *length, loff_t *ppos)
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08005672{
5673 struct zone *zone;
5674 unsigned int cpu;
5675 int ret;
5676
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005677 ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
Sasha Levin93278812012-05-10 13:01:44 -07005678 if (!write || (ret < 0))
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08005679 return ret;
Dimitri Sivanich364df0e2009-06-23 12:37:04 -07005680 for_each_populated_zone(zone) {
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09005681 for_each_possible_cpu(cpu) {
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08005682 unsigned long high;
5683 high = zone->present_pages / percpu_pagelist_fraction;
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09005684 setup_pagelist_highmark(
5685 per_cpu_ptr(zone->pageset, cpu), high);
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08005686 }
5687 }
5688 return 0;
5689}
5690
David S. Millerf034b5d2006-08-24 03:08:07 -07005691int hashdist = HASHDIST_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692
5693#ifdef CONFIG_NUMA
5694static int __init set_hashdist(char *str)
5695{
5696 if (!str)
5697 return 0;
5698 hashdist = simple_strtoul(str, &str, 0);
5699 return 1;
5700}
5701__setup("hashdist=", set_hashdist);
5702#endif
5703
5704/*
5705 * allocate a large system hash table from bootmem
5706 * - it is assumed that the hash table must contain an exact power-of-2
5707 * quantity of entries
5708 * - limit is the number of hash buckets, not the total allocation size
5709 */
5710void *__init alloc_large_system_hash(const char *tablename,
5711 unsigned long bucketsize,
5712 unsigned long numentries,
5713 int scale,
5714 int flags,
5715 unsigned int *_hash_shift,
5716 unsigned int *_hash_mask,
Tim Bird31fe62b2012-05-23 13:33:35 +00005717 unsigned long low_limit,
5718 unsigned long high_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719{
Tim Bird31fe62b2012-05-23 13:33:35 +00005720 unsigned long long max = high_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 unsigned long log2qty, size;
5722 void *table = NULL;
5723
5724 /* allow the kernel cmdline to have a say */
5725 if (!numentries) {
5726 /* round applicable memory size up to nearest megabyte */
Andrew Morton04903662006-12-06 20:37:33 -08005727 numentries = nr_kernel_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
5729 numentries >>= 20 - PAGE_SHIFT;
5730 numentries <<= 20 - PAGE_SHIFT;
5731
5732 /* limit to 1 bucket per 2^scale bytes of low memory */
5733 if (scale > PAGE_SHIFT)
5734 numentries >>= (scale - PAGE_SHIFT);
5735 else
5736 numentries <<= (PAGE_SHIFT - scale);
Paul Mundt9ab37b82007-01-05 16:36:30 -08005737
5738 /* Make sure we've got at least a 0-order allocation.. */
Jan Beulich2c85f512009-09-21 17:03:07 -07005739 if (unlikely(flags & HASH_SMALL)) {
5740 /* Makes no sense without HASH_EARLY */
5741 WARN_ON(!(flags & HASH_EARLY));
5742 if (!(numentries >> *_hash_shift)) {
5743 numentries = 1UL << *_hash_shift;
5744 BUG_ON(!numentries);
5745 }
5746 } else if (unlikely((numentries * bucketsize) < PAGE_SIZE))
Paul Mundt9ab37b82007-01-05 16:36:30 -08005747 numentries = PAGE_SIZE / bucketsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 }
John Hawkes6e692ed2006-03-25 03:08:02 -08005749 numentries = roundup_pow_of_two(numentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750
5751 /* limit allocation size to 1/16 total memory by default */
5752 if (max == 0) {
5753 max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
5754 do_div(max, bucketsize);
5755 }
Dimitri Sivanich074b8512012-02-08 12:39:07 -08005756 max = min(max, 0x80000000ULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757
Tim Bird31fe62b2012-05-23 13:33:35 +00005758 if (numentries < low_limit)
5759 numentries = low_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 if (numentries > max)
5761 numentries = max;
5762
David Howellsf0d1b0b2006-12-08 02:37:49 -08005763 log2qty = ilog2(numentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764
5765 do {
5766 size = bucketsize << log2qty;
5767 if (flags & HASH_EARLY)
Jan Beulich74768ed2008-08-12 15:08:39 -07005768 table = alloc_bootmem_nopanic(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 else if (hashdist)
5770 table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
5771 else {
Eric Dumazet1037b832007-07-15 23:38:05 -07005772 /*
5773 * If bucketsize is not a power-of-two, we may free
Mel Gormana1dd2682009-06-16 15:32:19 -07005774 * some pages at the end of hash table which
5775 * alloc_pages_exact() automatically does
Eric Dumazet1037b832007-07-15 23:38:05 -07005776 */
Catalin Marinas264ef8a2009-07-07 10:33:01 +01005777 if (get_order(size) < MAX_ORDER) {
Mel Gormana1dd2682009-06-16 15:32:19 -07005778 table = alloc_pages_exact(size, GFP_ATOMIC);
Catalin Marinas264ef8a2009-07-07 10:33:01 +01005779 kmemleak_alloc(table, size, 1, GFP_ATOMIC);
5780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781 }
5782 } while (!table && size > PAGE_SIZE && --log2qty);
5783
5784 if (!table)
5785 panic("Failed to allocate %s hash table\n", tablename);
5786
Robin Holtf241e6602010-10-07 12:59:26 -07005787 printk(KERN_INFO "%s hash table entries: %ld (order: %d, %lu bytes)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 tablename,
Robin Holtf241e6602010-10-07 12:59:26 -07005789 (1UL << log2qty),
David Howellsf0d1b0b2006-12-08 02:37:49 -08005790 ilog2(size) - PAGE_SHIFT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 size);
5792
5793 if (_hash_shift)
5794 *_hash_shift = log2qty;
5795 if (_hash_mask)
5796 *_hash_mask = (1 << log2qty) - 1;
5797
5798 return table;
5799}
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -08005800
Mel Gorman835c1342007-10-16 01:25:47 -07005801/* Return a pointer to the bitmap storing bits affecting a block of pages */
5802static inline unsigned long *get_pageblock_bitmap(struct zone *zone,
5803 unsigned long pfn)
5804{
5805#ifdef CONFIG_SPARSEMEM
5806 return __pfn_to_section(pfn)->pageblock_flags;
5807#else
5808 return zone->pageblock_flags;
5809#endif /* CONFIG_SPARSEMEM */
5810}
Andrew Morton6220ec72006-10-19 23:29:05 -07005811
Mel Gorman835c1342007-10-16 01:25:47 -07005812static inline int pfn_to_bitidx(struct zone *zone, unsigned long pfn)
5813{
5814#ifdef CONFIG_SPARSEMEM
5815 pfn &= (PAGES_PER_SECTION-1);
Mel Gormand9c23402007-10-16 01:26:01 -07005816 return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
Mel Gorman835c1342007-10-16 01:25:47 -07005817#else
Laura Abbottc060f942013-01-11 14:31:51 -08005818 pfn = pfn - round_down(zone->zone_start_pfn, pageblock_nr_pages);
Mel Gormand9c23402007-10-16 01:26:01 -07005819 return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
Mel Gorman835c1342007-10-16 01:25:47 -07005820#endif /* CONFIG_SPARSEMEM */
5821}
5822
5823/**
Mel Gormand9c23402007-10-16 01:26:01 -07005824 * 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 -07005825 * @page: The page within the block of interest
5826 * @start_bitidx: The first bit of interest to retrieve
5827 * @end_bitidx: The last bit of interest
5828 * returns pageblock_bits flags
5829 */
5830unsigned long get_pageblock_flags_group(struct page *page,
5831 int start_bitidx, int end_bitidx)
5832{
5833 struct zone *zone;
5834 unsigned long *bitmap;
5835 unsigned long pfn, bitidx;
5836 unsigned long flags = 0;
5837 unsigned long value = 1;
5838
5839 zone = page_zone(page);
5840 pfn = page_to_pfn(page);
5841 bitmap = get_pageblock_bitmap(zone, pfn);
5842 bitidx = pfn_to_bitidx(zone, pfn);
5843
5844 for (; start_bitidx <= end_bitidx; start_bitidx++, value <<= 1)
5845 if (test_bit(bitidx + start_bitidx, bitmap))
5846 flags |= value;
5847
5848 return flags;
5849}
5850
5851/**
Mel Gormand9c23402007-10-16 01:26:01 -07005852 * 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 -07005853 * @page: The page within the block of interest
5854 * @start_bitidx: The first bit of interest
5855 * @end_bitidx: The last bit of interest
5856 * @flags: The flags to set
5857 */
5858void set_pageblock_flags_group(struct page *page, unsigned long flags,
5859 int start_bitidx, int end_bitidx)
5860{
5861 struct zone *zone;
5862 unsigned long *bitmap;
5863 unsigned long pfn, bitidx;
5864 unsigned long value = 1;
5865
5866 zone = page_zone(page);
5867 pfn = page_to_pfn(page);
5868 bitmap = get_pageblock_bitmap(zone, pfn);
5869 bitidx = pfn_to_bitidx(zone, pfn);
KAMEZAWA Hiroyuki86051ca2008-04-29 00:58:21 -07005870 VM_BUG_ON(pfn < zone->zone_start_pfn);
5871 VM_BUG_ON(pfn >= zone->zone_start_pfn + zone->spanned_pages);
Mel Gorman835c1342007-10-16 01:25:47 -07005872
5873 for (; start_bitidx <= end_bitidx; start_bitidx++, value <<= 1)
5874 if (flags & value)
5875 __set_bit(bitidx + start_bitidx, bitmap);
5876 else
5877 __clear_bit(bitidx + start_bitidx, bitmap);
5878}
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -07005879
5880/*
Minchan Kim80934512012-07-31 16:43:01 -07005881 * This function checks whether pageblock includes unmovable pages or not.
5882 * If @count is not zero, it is okay to include less @count unmovable pages
5883 *
5884 * PageLRU check wihtout isolation or lru_lock could race so that
5885 * MIGRATE_MOVABLE block might include unmovable pages. It means you can't
5886 * expect this function should be exact.
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -07005887 */
Wen Congyangb023f462012-12-11 16:00:45 -08005888bool has_unmovable_pages(struct zone *zone, struct page *page, int count,
5889 bool skip_hwpoisoned_pages)
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005890{
5891 unsigned long pfn, iter, found;
Michal Nazarewicz47118af2011-12-29 13:09:50 +01005892 int mt;
5893
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005894 /*
5895 * For avoiding noise data, lru_add_drain_all() should be called
Minchan Kim80934512012-07-31 16:43:01 -07005896 * If ZONE_MOVABLE, the zone never contains unmovable pages
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005897 */
5898 if (zone_idx(zone) == ZONE_MOVABLE)
Minchan Kim80934512012-07-31 16:43:01 -07005899 return false;
Michal Nazarewicz47118af2011-12-29 13:09:50 +01005900 mt = get_pageblock_migratetype(page);
5901 if (mt == MIGRATE_MOVABLE || is_migrate_cma(mt))
Minchan Kim80934512012-07-31 16:43:01 -07005902 return false;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005903
5904 pfn = page_to_pfn(page);
5905 for (found = 0, iter = 0; iter < pageblock_nr_pages; iter++) {
5906 unsigned long check = pfn + iter;
5907
Namhyung Kim29723fc2011-02-25 14:44:25 -08005908 if (!pfn_valid_within(check))
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005909 continue;
Namhyung Kim29723fc2011-02-25 14:44:25 -08005910
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005911 page = pfn_to_page(check);
Minchan Kim97d255c2012-07-31 16:42:59 -07005912 /*
5913 * We can't use page_count without pin a page
5914 * because another CPU can free compound page.
5915 * This check already skips compound tails of THP
5916 * because their page->_count is zero at all time.
5917 */
5918 if (!atomic_read(&page->_count)) {
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005919 if (PageBuddy(page))
5920 iter += (1 << page_order(page)) - 1;
5921 continue;
5922 }
Minchan Kim97d255c2012-07-31 16:42:59 -07005923
Wen Congyangb023f462012-12-11 16:00:45 -08005924 /*
5925 * The HWPoisoned page may be not in buddy system, and
5926 * page_count() is not 0.
5927 */
5928 if (skip_hwpoisoned_pages && PageHWPoison(page))
5929 continue;
5930
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005931 if (!PageLRU(page))
5932 found++;
5933 /*
5934 * If there are RECLAIMABLE pages, we need to check it.
5935 * But now, memory offline itself doesn't call shrink_slab()
5936 * and it still to be fixed.
5937 */
5938 /*
5939 * If the page is not RAM, page_count()should be 0.
5940 * we don't need more check. This is an _used_ not-movable page.
5941 *
5942 * The problematic thing here is PG_reserved pages. PG_reserved
5943 * is set to both of a memory hole page and a _used_ kernel
5944 * page at boot.
5945 */
5946 if (found > count)
Minchan Kim80934512012-07-31 16:43:01 -07005947 return true;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005948 }
Minchan Kim80934512012-07-31 16:43:01 -07005949 return false;
KAMEZAWA Hiroyuki49ac8252010-10-26 14:21:30 -07005950}
5951
5952bool is_pageblock_removable_nolock(struct page *page)
5953{
Michal Hocko656a0702012-01-20 14:33:58 -08005954 struct zone *zone;
5955 unsigned long pfn;
Michal Hocko687875f2012-01-20 14:33:55 -08005956
5957 /*
5958 * We have to be careful here because we are iterating over memory
5959 * sections which are not zone aware so we might end up outside of
5960 * the zone but still within the section.
Michal Hocko656a0702012-01-20 14:33:58 -08005961 * We have to take care about the node as well. If the node is offline
5962 * its NODE_DATA will be NULL - see page_zone.
Michal Hocko687875f2012-01-20 14:33:55 -08005963 */
Michal Hocko656a0702012-01-20 14:33:58 -08005964 if (!node_online(page_to_nid(page)))
5965 return false;
5966
5967 zone = page_zone(page);
5968 pfn = page_to_pfn(page);
5969 if (zone->zone_start_pfn > pfn ||
Michal Hocko687875f2012-01-20 14:33:55 -08005970 zone->zone_start_pfn + zone->spanned_pages <= pfn)
5971 return false;
5972
Wen Congyangb023f462012-12-11 16:00:45 -08005973 return !has_unmovable_pages(zone, page, 0, true);
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -07005974}
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07005975
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01005976#ifdef CONFIG_CMA
5977
5978static unsigned long pfn_max_align_down(unsigned long pfn)
5979{
5980 return pfn & ~(max_t(unsigned long, MAX_ORDER_NR_PAGES,
5981 pageblock_nr_pages) - 1);
5982}
5983
5984static unsigned long pfn_max_align_up(unsigned long pfn)
5985{
5986 return ALIGN(pfn, max_t(unsigned long, MAX_ORDER_NR_PAGES,
5987 pageblock_nr_pages));
5988}
5989
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01005990/* [start, end) must belong to a single zone. */
Mel Gormanbb13ffe2012-10-08 16:32:41 -07005991static int __alloc_contig_migrate_range(struct compact_control *cc,
5992 unsigned long start, unsigned long end)
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01005993{
5994 /* This function is based on compact_zone() from compaction.c. */
Minchan Kimbeb51ea2012-10-08 16:33:51 -07005995 unsigned long nr_reclaimed;
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01005996 unsigned long pfn = start;
5997 unsigned int tries = 0;
5998 int ret = 0;
5999
Marek Szyprowskibe49a6e2012-12-12 13:51:19 -08006000 migrate_prep();
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006001
Mel Gormanbb13ffe2012-10-08 16:32:41 -07006002 while (pfn < end || !list_empty(&cc->migratepages)) {
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006003 if (fatal_signal_pending(current)) {
6004 ret = -EINTR;
6005 break;
6006 }
6007
Mel Gormanbb13ffe2012-10-08 16:32:41 -07006008 if (list_empty(&cc->migratepages)) {
6009 cc->nr_migratepages = 0;
6010 pfn = isolate_migratepages_range(cc->zone, cc,
Minchan Kime46a2872012-10-08 16:33:48 -07006011 pfn, end, true);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006012 if (!pfn) {
6013 ret = -EINTR;
6014 break;
6015 }
6016 tries = 0;
6017 } else if (++tries == 5) {
6018 ret = ret < 0 ? ret : -EBUSY;
6019 break;
6020 }
6021
Minchan Kimbeb51ea2012-10-08 16:33:51 -07006022 nr_reclaimed = reclaim_clean_pages_from_list(cc->zone,
6023 &cc->migratepages);
6024 cc->nr_migratepages -= nr_reclaimed;
Minchan Kim02c6de82012-10-08 16:31:55 -07006025
Mel Gormanbb13ffe2012-10-08 16:32:41 -07006026 ret = migrate_pages(&cc->migratepages,
Minchan Kim723a0642012-10-08 16:32:52 -07006027 alloc_migrate_target,
Mel Gorman7b2a2d42012-10-19 14:07:31 +01006028 0, false, MIGRATE_SYNC,
6029 MR_CMA);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006030 }
Srinivas Pandruvada2a6f5122013-02-22 16:32:09 -08006031 if (ret < 0) {
6032 putback_movable_pages(&cc->migratepages);
6033 return ret;
6034 }
6035 return 0;
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006036}
6037
6038/**
6039 * alloc_contig_range() -- tries to allocate given range of pages
6040 * @start: start PFN to allocate
6041 * @end: one-past-the-last PFN to allocate
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02006042 * @migratetype: migratetype of the underlaying pageblocks (either
6043 * #MIGRATE_MOVABLE or #MIGRATE_CMA). All pageblocks
6044 * in range must have the same migratetype and it must
6045 * be either of the two.
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006046 *
6047 * The PFN range does not have to be pageblock or MAX_ORDER_NR_PAGES
6048 * aligned, however it's the caller's responsibility to guarantee that
6049 * we are the only thread that changes migrate type of pageblocks the
6050 * pages fall in.
6051 *
6052 * The PFN range must belong to a single zone.
6053 *
6054 * Returns zero on success or negative error code. On success all
6055 * pages which PFN is in [start, end) are allocated for the caller and
6056 * need to be freed with free_contig_range().
6057 */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02006058int alloc_contig_range(unsigned long start, unsigned long end,
6059 unsigned migratetype)
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006060{
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006061 unsigned long outer_start, outer_end;
6062 int ret = 0, order;
6063
Mel Gormanbb13ffe2012-10-08 16:32:41 -07006064 struct compact_control cc = {
6065 .nr_migratepages = 0,
6066 .order = -1,
6067 .zone = page_zone(pfn_to_page(start)),
6068 .sync = true,
6069 .ignore_skip_hint = true,
6070 };
6071 INIT_LIST_HEAD(&cc.migratepages);
6072
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006073 /*
6074 * What we do here is we mark all pageblocks in range as
6075 * MIGRATE_ISOLATE. Because pageblock and max order pages may
6076 * have different sizes, and due to the way page allocator
6077 * work, we align the range to biggest of the two pages so
6078 * that page allocator won't try to merge buddies from
6079 * different pageblocks and change MIGRATE_ISOLATE to some
6080 * other migration type.
6081 *
6082 * Once the pageblocks are marked as MIGRATE_ISOLATE, we
6083 * migrate the pages from an unaligned range (ie. pages that
6084 * we are interested in). This will put all the pages in
6085 * range back to page allocator as MIGRATE_ISOLATE.
6086 *
6087 * When this is done, we take the pages in range from page
6088 * allocator removing them from the buddy system. This way
6089 * page allocator will never consider using them.
6090 *
6091 * This lets us mark the pageblocks back as
6092 * MIGRATE_CMA/MIGRATE_MOVABLE so that free pages in the
6093 * aligned range but not in the unaligned, original range are
6094 * put back to page allocator so that buddy can use them.
6095 */
6096
6097 ret = start_isolate_page_range(pfn_max_align_down(start),
Wen Congyangb023f462012-12-11 16:00:45 -08006098 pfn_max_align_up(end), migratetype,
6099 false);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006100 if (ret)
Bob Liu86a595f2012-10-25 13:37:56 -07006101 return ret;
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006102
Mel Gormanbb13ffe2012-10-08 16:32:41 -07006103 ret = __alloc_contig_migrate_range(&cc, start, end);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006104 if (ret)
6105 goto done;
6106
6107 /*
6108 * Pages from [start, end) are within a MAX_ORDER_NR_PAGES
6109 * aligned blocks that are marked as MIGRATE_ISOLATE. What's
6110 * more, all pages in [start, end) are free in page allocator.
6111 * What we are going to do is to allocate all pages from
6112 * [start, end) (that is remove them from page allocator).
6113 *
6114 * The only problem is that pages at the beginning and at the
6115 * end of interesting range may be not aligned with pages that
6116 * page allocator holds, ie. they can be part of higher order
6117 * pages. Because of this, we reserve the bigger range and
6118 * once this is done free the pages we are not interested in.
6119 *
6120 * We don't have to hold zone->lock here because the pages are
6121 * isolated thus they won't get removed from buddy.
6122 */
6123
6124 lru_add_drain_all();
6125 drain_all_pages();
6126
6127 order = 0;
6128 outer_start = start;
6129 while (!PageBuddy(pfn_to_page(outer_start))) {
6130 if (++order >= MAX_ORDER) {
6131 ret = -EBUSY;
6132 goto done;
6133 }
6134 outer_start &= ~0UL << order;
6135 }
6136
6137 /* Make sure the range is really isolated. */
Wen Congyangb023f462012-12-11 16:00:45 -08006138 if (test_pages_isolated(outer_start, end, false)) {
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006139 pr_warn("alloc_contig_range test_pages_isolated(%lx, %lx) failed\n",
6140 outer_start, end);
6141 ret = -EBUSY;
6142 goto done;
6143 }
6144
Marek Szyprowski49f223a2012-01-25 12:49:24 +01006145
6146 /* Grab isolated pages from freelists. */
Mel Gormanbb13ffe2012-10-08 16:32:41 -07006147 outer_end = isolate_freepages_range(&cc, outer_start, end);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006148 if (!outer_end) {
6149 ret = -EBUSY;
6150 goto done;
6151 }
6152
6153 /* Free head and tail (if any) */
6154 if (start != outer_start)
6155 free_contig_range(outer_start, start - outer_start);
6156 if (end != outer_end)
6157 free_contig_range(end, outer_end - end);
6158
6159done:
6160 undo_isolate_page_range(pfn_max_align_down(start),
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +02006161 pfn_max_align_up(end), migratetype);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006162 return ret;
6163}
6164
6165void free_contig_range(unsigned long pfn, unsigned nr_pages)
6166{
Marek Szyprowskibcc2b022012-12-20 15:05:18 -08006167 unsigned int count = 0;
6168
6169 for (; nr_pages--; pfn++) {
6170 struct page *page = pfn_to_page(pfn);
6171
6172 count += page_count(page) != 1;
6173 __free_page(page);
6174 }
6175 WARN(count != 0, "%d pages are still in use!\n", count);
Michal Nazarewicz041d3a82011-12-29 13:09:50 +01006176}
6177#endif
6178
Jiang Liu4ed7e022012-07-31 16:43:35 -07006179#ifdef CONFIG_MEMORY_HOTPLUG
6180static int __meminit __zone_pcp_update(void *data)
6181{
6182 struct zone *zone = data;
6183 int cpu;
6184 unsigned long batch = zone_batchsize(zone), flags;
6185
6186 for_each_possible_cpu(cpu) {
6187 struct per_cpu_pageset *pset;
6188 struct per_cpu_pages *pcp;
6189
6190 pset = per_cpu_ptr(zone->pageset, cpu);
6191 pcp = &pset->pcp;
6192
6193 local_irq_save(flags);
6194 if (pcp->count > 0)
6195 free_pcppages_bulk(zone, pcp->count, pcp);
Minchan Kim5a883812012-10-08 16:33:39 -07006196 drain_zonestat(zone, pset);
Jiang Liu4ed7e022012-07-31 16:43:35 -07006197 setup_pageset(pset, batch);
6198 local_irq_restore(flags);
6199 }
6200 return 0;
6201}
6202
6203void __meminit zone_pcp_update(struct zone *zone)
6204{
6205 stop_machine(__zone_pcp_update, zone, NULL);
6206}
6207#endif
6208
Jiang Liu340175b2012-07-31 16:43:32 -07006209void zone_pcp_reset(struct zone *zone)
6210{
6211 unsigned long flags;
Minchan Kim5a883812012-10-08 16:33:39 -07006212 int cpu;
6213 struct per_cpu_pageset *pset;
Jiang Liu340175b2012-07-31 16:43:32 -07006214
6215 /* avoid races with drain_pages() */
6216 local_irq_save(flags);
6217 if (zone->pageset != &boot_pageset) {
Minchan Kim5a883812012-10-08 16:33:39 -07006218 for_each_online_cpu(cpu) {
6219 pset = per_cpu_ptr(zone->pageset, cpu);
6220 drain_zonestat(zone, pset);
6221 }
Jiang Liu340175b2012-07-31 16:43:32 -07006222 free_percpu(zone->pageset);
6223 zone->pageset = &boot_pageset;
6224 }
6225 local_irq_restore(flags);
6226}
6227
Wen Congyang6dcd73d2012-12-11 16:01:01 -08006228#ifdef CONFIG_MEMORY_HOTREMOVE
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07006229/*
6230 * All pages in the range must be isolated before calling this.
6231 */
6232void
6233__offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
6234{
6235 struct page *page;
6236 struct zone *zone;
6237 int order, i;
6238 unsigned long pfn;
6239 unsigned long flags;
6240 /* find the first valid pfn */
6241 for (pfn = start_pfn; pfn < end_pfn; pfn++)
6242 if (pfn_valid(pfn))
6243 break;
6244 if (pfn == end_pfn)
6245 return;
6246 zone = page_zone(pfn_to_page(pfn));
6247 spin_lock_irqsave(&zone->lock, flags);
6248 pfn = start_pfn;
6249 while (pfn < end_pfn) {
6250 if (!pfn_valid(pfn)) {
6251 pfn++;
6252 continue;
6253 }
6254 page = pfn_to_page(pfn);
Wen Congyangb023f462012-12-11 16:00:45 -08006255 /*
6256 * The HWPoisoned page may be not in buddy system, and
6257 * page_count() is not 0.
6258 */
6259 if (unlikely(!PageBuddy(page) && PageHWPoison(page))) {
6260 pfn++;
6261 SetPageReserved(page);
6262 continue;
6263 }
6264
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07006265 BUG_ON(page_count(page));
6266 BUG_ON(!PageBuddy(page));
6267 order = page_order(page);
6268#ifdef CONFIG_DEBUG_VM
6269 printk(KERN_INFO "remove from free list %lx %d %lx\n",
6270 pfn, 1 << order, end_pfn);
6271#endif
6272 list_del(&page->lru);
6273 rmv_page_order(page);
6274 zone->free_area[order].nr_free--;
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07006275 for (i = 0; i < (1 << order); i++)
6276 SetPageReserved((page+i));
6277 pfn += (1 << order);
6278 }
6279 spin_unlock_irqrestore(&zone->lock, flags);
6280}
6281#endif
Wu Fengguang8d22ba12009-12-16 12:19:58 +01006282
6283#ifdef CONFIG_MEMORY_FAILURE
6284bool is_free_buddy_page(struct page *page)
6285{
6286 struct zone *zone = page_zone(page);
6287 unsigned long pfn = page_to_pfn(page);
6288 unsigned long flags;
6289 int order;
6290
6291 spin_lock_irqsave(&zone->lock, flags);
6292 for (order = 0; order < MAX_ORDER; order++) {
6293 struct page *page_head = page - (pfn & ((1 << order) - 1));
6294
6295 if (PageBuddy(page_head) && page_order(page_head) >= order)
6296 break;
6297 }
6298 spin_unlock_irqrestore(&zone->lock, flags);
6299
6300 return order < MAX_ORDER;
6301}
6302#endif
Wu Fengguang718a3822010-03-10 15:20:43 -08006303
Andrew Morton51300ce2012-05-29 15:06:44 -07006304static const struct trace_print_flags pageflag_names[] = {
Wu Fengguang718a3822010-03-10 15:20:43 -08006305 {1UL << PG_locked, "locked" },
6306 {1UL << PG_error, "error" },
6307 {1UL << PG_referenced, "referenced" },
6308 {1UL << PG_uptodate, "uptodate" },
6309 {1UL << PG_dirty, "dirty" },
6310 {1UL << PG_lru, "lru" },
6311 {1UL << PG_active, "active" },
6312 {1UL << PG_slab, "slab" },
6313 {1UL << PG_owner_priv_1, "owner_priv_1" },
6314 {1UL << PG_arch_1, "arch_1" },
6315 {1UL << PG_reserved, "reserved" },
6316 {1UL << PG_private, "private" },
6317 {1UL << PG_private_2, "private_2" },
6318 {1UL << PG_writeback, "writeback" },
6319#ifdef CONFIG_PAGEFLAGS_EXTENDED
6320 {1UL << PG_head, "head" },
6321 {1UL << PG_tail, "tail" },
6322#else
6323 {1UL << PG_compound, "compound" },
6324#endif
6325 {1UL << PG_swapcache, "swapcache" },
6326 {1UL << PG_mappedtodisk, "mappedtodisk" },
6327 {1UL << PG_reclaim, "reclaim" },
Wu Fengguang718a3822010-03-10 15:20:43 -08006328 {1UL << PG_swapbacked, "swapbacked" },
6329 {1UL << PG_unevictable, "unevictable" },
6330#ifdef CONFIG_MMU
6331 {1UL << PG_mlocked, "mlocked" },
6332#endif
6333#ifdef CONFIG_ARCH_USES_PG_UNCACHED
6334 {1UL << PG_uncached, "uncached" },
6335#endif
6336#ifdef CONFIG_MEMORY_FAILURE
6337 {1UL << PG_hwpoison, "hwpoison" },
6338#endif
Gavin Shanbe9cd872012-05-29 15:06:44 -07006339#ifdef CONFIG_TRANSPARENT_HUGEPAGE
6340 {1UL << PG_compound_lock, "compound_lock" },
6341#endif
Wu Fengguang718a3822010-03-10 15:20:43 -08006342};
6343
6344static void dump_page_flags(unsigned long flags)
6345{
6346 const char *delim = "";
6347 unsigned long mask;
6348 int i;
6349
Andrew Morton51300ce2012-05-29 15:06:44 -07006350 BUILD_BUG_ON(ARRAY_SIZE(pageflag_names) != __NR_PAGEFLAGS);
Johannes Weineracc50c12012-05-29 15:06:44 -07006351
Wu Fengguang718a3822010-03-10 15:20:43 -08006352 printk(KERN_ALERT "page flags: %#lx(", flags);
6353
6354 /* remove zone id */
6355 flags &= (1UL << NR_PAGEFLAGS) - 1;
6356
Andrew Morton51300ce2012-05-29 15:06:44 -07006357 for (i = 0; i < ARRAY_SIZE(pageflag_names) && flags; i++) {
Wu Fengguang718a3822010-03-10 15:20:43 -08006358
6359 mask = pageflag_names[i].mask;
6360 if ((flags & mask) != mask)
6361 continue;
6362
6363 flags &= ~mask;
6364 printk("%s%s", delim, pageflag_names[i].name);
6365 delim = "|";
6366 }
6367
6368 /* check for left over flags */
6369 if (flags)
6370 printk("%s%#lx", delim, flags);
6371
6372 printk(")\n");
6373}
6374
6375void dump_page(struct page *page)
6376{
6377 printk(KERN_ALERT
6378 "page:%p count:%d mapcount:%d mapping:%p index:%#lx\n",
Andrea Arcangeli4e9f64c2011-01-13 15:46:29 -08006379 page, atomic_read(&page->_count), page_mapcount(page),
Wu Fengguang718a3822010-03-10 15:20:43 -08006380 page->mapping, page->index);
6381 dump_page_flags(page->flags);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07006382 mem_cgroup_print_bad_page(page);
Wu Fengguang718a3822010-03-10 15:20:43 -08006383}