blob: 31c512410a994b1dd8b70e24781c56ecb2e28962 [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>
24#include <linux/compiler.h>
Randy Dunlap9f158332005-09-13 01:25:16 -070025#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/module.h>
27#include <linux/suspend.h>
28#include <linux/pagevec.h>
29#include <linux/blkdev.h>
30#include <linux/slab.h>
David Rientjes5a3135c22007-10-16 23:25:53 -070031#include <linux/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/notifier.h>
33#include <linux/topology.h>
34#include <linux/sysctl.h>
35#include <linux/cpu.h>
36#include <linux/cpuset.h>
Dave Hansenbdc8cb92005-10-29 18:16:53 -070037#include <linux/memory_hotplug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/nodemask.h>
39#include <linux/vmalloc.h>
Christoph Lameter4be38e32006-01-06 00:11:17 -080040#include <linux/mempolicy.h>
Yasunori Goto68113782006-06-23 02:03:11 -070041#include <linux/stop_machine.h>
Mel Gormanc7132162006-09-27 01:49:43 -070042#include <linux/sort.h>
43#include <linux/pfn.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070044#include <linux/backing-dev.h>
Akinobu Mita933e3122006-12-08 02:39:45 -080045#include <linux/fault-inject.h>
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -070046#include <linux/page-isolation.h>
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070047#include <linux/page_cgroup.h>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070048#include <linux/debugobjects.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include <asm/tlbflush.h>
Andrew Mortonac924c62006-05-15 09:43:59 -070051#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include "internal.h"
53
54/*
Christoph Lameter13808912007-10-16 01:25:27 -070055 * Array of node states.
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 */
Christoph Lameter13808912007-10-16 01:25:27 -070057nodemask_t node_states[NR_NODE_STATES] __read_mostly = {
58 [N_POSSIBLE] = NODE_MASK_ALL,
59 [N_ONLINE] = { { [0] = 1UL } },
60#ifndef CONFIG_NUMA
61 [N_NORMAL_MEMORY] = { { [0] = 1UL } },
62#ifdef CONFIG_HIGHMEM
63 [N_HIGH_MEMORY] = { { [0] = 1UL } },
64#endif
65 [N_CPU] = { { [0] = 1UL } },
66#endif /* NUMA */
67};
68EXPORT_SYMBOL(node_states);
69
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -070070unsigned long totalram_pages __read_mostly;
Hideo AOKIcb45b0e2006-04-10 22:52:59 -070071unsigned long totalreserve_pages __read_mostly;
Rohit Seth8ad4b1f2006-01-08 01:00:40 -080072int percpu_pagelist_fraction;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Mel Gormand9c23402007-10-16 01:26:01 -070074#ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
75int pageblock_order __read_mostly;
76#endif
77
Hugh Dickinsd98c7a02006-02-14 13:52:59 -080078static void __free_pages_ok(struct page *page, unsigned int order);
David Howellsa226f6c2006-01-06 00:11:08 -080079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/*
81 * results with 256, 32 in the lowmem_reserve sysctl:
82 * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
83 * 1G machine -> (16M dma, 784M normal, 224M high)
84 * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
85 * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
86 * HIGHMEM allocation will (224M+784M)/256 of ram reserved in ZONE_DMA
Andi Kleena2f1b422005-11-05 17:25:53 +010087 *
88 * TBD: should special case ZONE_DMA32 machines here - in those we normally
89 * don't need any ZONE_NORMAL reservation
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 */
Christoph Lameter2f1b6242006-09-25 23:31:13 -070091int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1] = {
Christoph Lameter4b51d662007-02-10 01:43:10 -080092#ifdef CONFIG_ZONE_DMA
Christoph Lameter2f1b6242006-09-25 23:31:13 -070093 256,
Christoph Lameter4b51d662007-02-10 01:43:10 -080094#endif
Christoph Lameterfb0e7942006-09-25 23:31:13 -070095#ifdef CONFIG_ZONE_DMA32
Christoph Lameter2f1b6242006-09-25 23:31:13 -070096 256,
Christoph Lameterfb0e7942006-09-25 23:31:13 -070097#endif
Christoph Lametere53ef382006-09-25 23:31:14 -070098#ifdef CONFIG_HIGHMEM
Mel Gorman2a1e2742007-07-17 04:03:12 -070099 32,
Christoph Lametere53ef382006-09-25 23:31:14 -0700100#endif
Mel Gorman2a1e2742007-07-17 04:03:12 -0700101 32,
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700102};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104EXPORT_SYMBOL(totalram_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Helge Deller15ad7cd2006-12-06 20:40:36 -0800106static char * const zone_names[MAX_NR_ZONES] = {
Christoph Lameter4b51d662007-02-10 01:43:10 -0800107#ifdef CONFIG_ZONE_DMA
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700108 "DMA",
Christoph Lameter4b51d662007-02-10 01:43:10 -0800109#endif
Christoph Lameterfb0e7942006-09-25 23:31:13 -0700110#ifdef CONFIG_ZONE_DMA32
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700111 "DMA32",
Christoph Lameterfb0e7942006-09-25 23:31:13 -0700112#endif
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700113 "Normal",
Christoph Lametere53ef382006-09-25 23:31:14 -0700114#ifdef CONFIG_HIGHMEM
Mel Gorman2a1e2742007-07-17 04:03:12 -0700115 "HighMem",
Christoph Lametere53ef382006-09-25 23:31:14 -0700116#endif
Mel Gorman2a1e2742007-07-17 04:03:12 -0700117 "Movable",
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700118};
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120int min_free_kbytes = 1024;
121
Yasunori Goto86356ab2006-06-23 02:03:09 -0700122unsigned long __meminitdata nr_kernel_pages;
123unsigned long __meminitdata nr_all_pages;
Yasunori Gotoa3142c82007-05-08 00:23:07 -0700124static unsigned long __meminitdata dma_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Mel Gormanc7132162006-09-27 01:49:43 -0700126#ifdef CONFIG_ARCH_POPULATES_NODE_MAP
127 /*
Simon Arlott183ff222007-10-20 01:27:18 +0200128 * MAX_ACTIVE_REGIONS determines the maximum number of distinct
Mel Gormanc7132162006-09-27 01:49:43 -0700129 * ranges of memory (RAM) that may be registered with add_active_range().
130 * Ranges passed to add_active_range() will be merged if possible
131 * so the number of times add_active_range() can be called is
132 * related to the number of nodes and the number of holes
133 */
134 #ifdef CONFIG_MAX_ACTIVE_REGIONS
135 /* Allow an architecture to set MAX_ACTIVE_REGIONS to save memory */
136 #define MAX_ACTIVE_REGIONS CONFIG_MAX_ACTIVE_REGIONS
137 #else
138 #if MAX_NUMNODES >= 32
139 /* If there can be many nodes, allow up to 50 holes per node */
140 #define MAX_ACTIVE_REGIONS (MAX_NUMNODES*50)
141 #else
142 /* By default, allow up to 256 distinct regions */
143 #define MAX_ACTIVE_REGIONS 256
144 #endif
145 #endif
146
Jan Beulich98011f52007-07-15 23:38:17 -0700147 static struct node_active_region __meminitdata early_node_map[MAX_ACTIVE_REGIONS];
148 static int __meminitdata nr_nodemap_entries;
149 static unsigned long __meminitdata arch_zone_lowest_possible_pfn[MAX_NR_ZONES];
150 static unsigned long __meminitdata arch_zone_highest_possible_pfn[MAX_NR_ZONES];
Mel Gormanfb014392006-09-27 01:49:59 -0700151#ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
Jan Beulich98011f52007-07-15 23:38:17 -0700152 static unsigned long __meminitdata node_boundary_start_pfn[MAX_NUMNODES];
153 static unsigned long __meminitdata node_boundary_end_pfn[MAX_NUMNODES];
Mel Gormanfb014392006-09-27 01:49:59 -0700154#endif /* CONFIG_MEMORY_HOTPLUG_RESERVE */
Adrian Bunkb69a7282008-07-23 21:28:12 -0700155 static unsigned long __initdata required_kernelcore;
Adrian Bunk484f51f2007-10-16 01:26:03 -0700156 static unsigned long __initdata required_movablecore;
Adrian Bunkb69a7282008-07-23 21:28:12 -0700157 static unsigned long __meminitdata zone_movable_pfn[MAX_NUMNODES];
Mel Gorman2a1e2742007-07-17 04:03:12 -0700158
159 /* movable_zone is the "real" zone pages in ZONE_MOVABLE are taken from */
160 int movable_zone;
161 EXPORT_SYMBOL(movable_zone);
Mel Gormanc7132162006-09-27 01:49:43 -0700162#endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
163
Miklos Szeredi418508c2007-05-23 13:57:55 -0700164#if MAX_NUMNODES > 1
165int nr_node_ids __read_mostly = MAX_NUMNODES;
166EXPORT_SYMBOL(nr_node_ids);
167#endif
168
Mel Gorman9ef9acb2007-10-16 01:25:54 -0700169int page_group_by_mobility_disabled __read_mostly;
170
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700171static void set_pageblock_migratetype(struct page *page, int migratetype)
172{
173 set_pageblock_flags_group(page, (unsigned long)migratetype,
174 PB_migrate, PB_migrate_end);
175}
176
Nick Piggin13e74442006-01-06 00:10:58 -0800177#ifdef CONFIG_DEBUG_VM
Dave Hansenc6a57e12005-10-29 18:16:52 -0700178static int page_outside_zone_boundaries(struct zone *zone, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Dave Hansenbdc8cb92005-10-29 18:16:53 -0700180 int ret = 0;
181 unsigned seq;
182 unsigned long pfn = page_to_pfn(page);
Dave Hansenc6a57e12005-10-29 18:16:52 -0700183
Dave Hansenbdc8cb92005-10-29 18:16:53 -0700184 do {
185 seq = zone_span_seqbegin(zone);
186 if (pfn >= zone->zone_start_pfn + zone->spanned_pages)
187 ret = 1;
188 else if (pfn < zone->zone_start_pfn)
189 ret = 1;
190 } while (zone_span_seqretry(zone, seq));
191
192 return ret;
Dave Hansenc6a57e12005-10-29 18:16:52 -0700193}
194
195static int page_is_consistent(struct zone *zone, struct page *page)
196{
Andy Whitcroft14e07292007-05-06 14:49:14 -0700197 if (!pfn_valid_within(page_to_pfn(page)))
Dave Hansenc6a57e12005-10-29 18:16:52 -0700198 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (zone != page_zone(page))
Dave Hansenc6a57e12005-10-29 18:16:52 -0700200 return 0;
201
202 return 1;
203}
204/*
205 * Temporary debugging check for pages not lying within a given zone.
206 */
207static int bad_range(struct zone *zone, struct page *page)
208{
209 if (page_outside_zone_boundaries(zone, page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return 1;
Dave Hansenc6a57e12005-10-29 18:16:52 -0700211 if (!page_is_consistent(zone, page))
212 return 1;
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return 0;
215}
Nick Piggin13e74442006-01-06 00:10:58 -0800216#else
217static inline int bad_range(struct zone *zone, struct page *page)
218{
219 return 0;
220}
221#endif
222
Nick Piggin224abf92006-01-06 00:11:11 -0800223static void bad_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Hugh Dickins9442ec92008-03-04 14:29:07 -0800225 printk(KERN_EMERG "Bad page state in process '%s'\n" KERN_EMERG
226 "page:%p flags:0x%0*lx mapping:%p mapcount:%d count:%d\n",
Nick Piggin224abf92006-01-06 00:11:11 -0800227 current->comm, page, (int)(2*sizeof(unsigned long)),
228 (unsigned long)page->flags, page->mapping,
229 page_mapcount(page), page_count(page));
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700230
Hugh Dickins9442ec92008-03-04 14:29:07 -0800231 printk(KERN_EMERG "Trying to fix it up, but a reboot is needed\n"
232 KERN_EMERG "Backtrace:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 dump_stack();
Russ Andersondfa7e202008-06-09 11:18:45 -0500234 page->flags &= ~PAGE_FLAGS_CLEAR_WHEN_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 set_page_count(page, 0);
236 reset_page_mapcount(page);
237 page->mapping = NULL;
Randy Dunlap9f158332005-09-13 01:25:16 -0700238 add_taint(TAINT_BAD_PAGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241/*
242 * Higher-order pages are called "compound pages". They are structured thusly:
243 *
244 * The first PAGE_SIZE page is called the "head page".
245 *
246 * The remaining PAGE_SIZE pages are called "tail pages".
247 *
248 * All pages have PG_compound set. All pages have their ->private pointing at
249 * the head page (even the head page has this).
250 *
Hugh Dickins41d78ba2006-02-14 13:52:58 -0800251 * The first tail page's ->lru.next holds the address of the compound page's
252 * put_page() function. Its ->lru.prev holds the order of allocation.
253 * This usage means that zero-order pages may not be compound.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 */
Hugh Dickinsd98c7a02006-02-14 13:52:59 -0800255
256static void free_compound_page(struct page *page)
257{
Christoph Lameterd85f3382007-05-06 14:49:39 -0700258 __free_pages_ok(page, compound_order(page));
Hugh Dickinsd98c7a02006-02-14 13:52:59 -0800259}
260
Andi Kleen01ad1c02008-07-23 21:27:46 -0700261void prep_compound_page(struct page *page, unsigned long order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 int i;
264 int nr_pages = 1 << order;
265
Andy Whitcroft33f2ef82006-12-06 20:33:32 -0800266 set_compound_page_dtor(page, free_compound_page);
Christoph Lameterd85f3382007-05-06 14:49:39 -0700267 set_compound_order(page, order);
Christoph Lameter6d777952007-05-06 14:49:40 -0700268 __SetPageHead(page);
Andy Whitcroft18229df2008-11-06 12:53:27 -0800269 for (i = 1; i < nr_pages; i++) {
270 struct page *p = page + i;
271
Christoph Lameterd85f3382007-05-06 14:49:39 -0700272 __SetPageTail(p);
Christoph Lameterd85f3382007-05-06 14:49:39 -0700273 p->first_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
275}
276
Andy Whitcroft18229df2008-11-06 12:53:27 -0800277#ifdef CONFIG_HUGETLBFS
278void prep_compound_gigantic_page(struct page *page, unsigned long order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 int i;
281 int nr_pages = 1 << order;
Andy Whitcroft6babc322008-10-02 14:50:18 -0700282 struct page *p = page + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Andy Whitcroft18229df2008-11-06 12:53:27 -0800284 set_compound_page_dtor(page, free_compound_page);
285 set_compound_order(page, order);
286 __SetPageHead(page);
287 for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) {
288 __SetPageTail(p);
289 p->first_page = page;
290 }
291}
292#endif
293
294static void destroy_compound_page(struct page *page, unsigned long order)
295{
296 int i;
297 int nr_pages = 1 << order;
298
Christoph Lameterd85f3382007-05-06 14:49:39 -0700299 if (unlikely(compound_order(page) != order))
Nick Piggin224abf92006-01-06 00:11:11 -0800300 bad_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Christoph Lameter6d777952007-05-06 14:49:40 -0700302 if (unlikely(!PageHead(page)))
Christoph Lameterd85f3382007-05-06 14:49:39 -0700303 bad_page(page);
Christoph Lameter6d777952007-05-06 14:49:40 -0700304 __ClearPageHead(page);
Andy Whitcroft18229df2008-11-06 12:53:27 -0800305 for (i = 1; i < nr_pages; i++) {
306 struct page *p = page + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Christoph Lameter6d777952007-05-06 14:49:40 -0700308 if (unlikely(!PageTail(p) |
Christoph Lameterd85f3382007-05-06 14:49:39 -0700309 (p->first_page != page)))
Nick Piggin224abf92006-01-06 00:11:11 -0800310 bad_page(page);
Christoph Lameterd85f3382007-05-06 14:49:39 -0700311 __ClearPageTail(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Nick Piggin17cf4402006-03-22 00:08:41 -0800315static inline void prep_zero_page(struct page *page, int order, gfp_t gfp_flags)
316{
317 int i;
318
Andrew Morton6626c5d2006-03-22 00:08:42 -0800319 /*
320 * clear_highpage() will use KM_USER0, so it's a bug to use __GFP_ZERO
321 * and __GFP_HIGHMEM from hard or soft interrupt context.
322 */
Nick Piggin725d7042006-09-25 23:30:55 -0700323 VM_BUG_ON((gfp_flags & __GFP_HIGHMEM) && in_interrupt());
Nick Piggin17cf4402006-03-22 00:08:41 -0800324 for (i = 0; i < (1 << order); i++)
325 clear_highpage(page + i);
326}
327
Andrew Morton6aa3001b22006-04-18 22:20:52 -0700328static inline void set_page_order(struct page *page, int order)
329{
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700330 set_page_private(page, order);
Nick Piggin676165a2006-04-10 11:21:48 +1000331 __SetPageBuddy(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
334static inline void rmv_page_order(struct page *page)
335{
Nick Piggin676165a2006-04-10 11:21:48 +1000336 __ClearPageBuddy(page);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700337 set_page_private(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
340/*
341 * Locate the struct page for both the matching buddy in our
342 * pair (buddy1) and the combined O(n+1) page they form (page).
343 *
344 * 1) Any buddy B1 will have an order O twin B2 which satisfies
345 * the following equation:
346 * B2 = B1 ^ (1 << O)
347 * For example, if the starting buddy (buddy2) is #8 its order
348 * 1 buddy is #10:
349 * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
350 *
351 * 2) Any buddy B will have an order O+1 parent P which
352 * satisfies the following equation:
353 * P = B & ~(1 << O)
354 *
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200355 * Assumption: *_mem_map is contiguous at least up to MAX_ORDER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 */
357static inline struct page *
358__page_find_buddy(struct page *page, unsigned long page_idx, unsigned int order)
359{
360 unsigned long buddy_idx = page_idx ^ (1 << order);
361
362 return page + (buddy_idx - page_idx);
363}
364
365static inline unsigned long
366__find_combined_index(unsigned long page_idx, unsigned int order)
367{
368 return (page_idx & ~(1 << order));
369}
370
371/*
372 * This function checks whether a page is free && is the buddy
373 * we can do coalesce a page and its buddy if
Nick Piggin13e74442006-01-06 00:10:58 -0800374 * (a) the buddy is not in a hole &&
Nick Piggin676165a2006-04-10 11:21:48 +1000375 * (b) the buddy is in the buddy system &&
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700376 * (c) a page and its buddy have the same order &&
377 * (d) a page and its buddy are in the same zone.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 *
Nick Piggin676165a2006-04-10 11:21:48 +1000379 * For recording whether a page is in the buddy system, we use PG_buddy.
380 * Setting, clearing, and testing PG_buddy is serialized by zone->lock.
381 *
382 * For recording page's order, we use page_private(page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 */
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700384static inline int page_is_buddy(struct page *page, struct page *buddy,
385 int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Andy Whitcroft14e07292007-05-06 14:49:14 -0700387 if (!pfn_valid_within(page_to_pfn(buddy)))
Nick Piggin13e74442006-01-06 00:10:58 -0800388 return 0;
Nick Piggin13e74442006-01-06 00:10:58 -0800389
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700390 if (page_zone_id(page) != page_zone_id(buddy))
391 return 0;
392
393 if (PageBuddy(buddy) && page_order(buddy) == order) {
394 BUG_ON(page_count(buddy) != 0);
Andrew Morton6aa3001b22006-04-18 22:20:52 -0700395 return 1;
Nick Piggin676165a2006-04-10 11:21:48 +1000396 }
Andrew Morton6aa3001b22006-04-18 22:20:52 -0700397 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
400/*
401 * Freeing function for a buddy system allocator.
402 *
403 * The concept of a buddy system is to maintain direct-mapped table
404 * (containing bit values) for memory blocks of various "orders".
405 * The bottom level table contains the map for the smallest allocatable
406 * units of memory (here, pages), and each level above it describes
407 * pairs of units from the levels below, hence, "buddies".
408 * At a high level, all that happens here is marking the table entry
409 * at the bottom level available, and propagating the changes upward
410 * as necessary, plus some accounting needed to play nicely with other
411 * parts of the VM system.
412 * At each level, we keep a list of pages, which are heads of continuous
Nick Piggin676165a2006-04-10 11:21:48 +1000413 * free pages of length of (1 << order) and marked with PG_buddy. Page's
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700414 * order is recorded in page_private(page) field.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 * So when we are allocating or freeing one, we can derive the state of the
416 * other. That is, if we allocate a small block, and both were
417 * free, the remainder of the region must be split into blocks.
418 * If a block is freed, and its buddy is also free, then this
419 * triggers coalescing into a block of larger size.
420 *
421 * -- wli
422 */
423
Nick Piggin48db57f2006-01-08 01:00:42 -0800424static inline void __free_one_page(struct page *page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 struct zone *zone, unsigned int order)
426{
427 unsigned long page_idx;
428 int order_size = 1 << order;
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700429 int migratetype = get_pageblock_migratetype(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Nick Piggin224abf92006-01-06 00:11:11 -0800431 if (unlikely(PageCompound(page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 destroy_compound_page(page, order);
433
434 page_idx = page_to_pfn(page) & ((1 << MAX_ORDER) - 1);
435
Nick Piggin725d7042006-09-25 23:30:55 -0700436 VM_BUG_ON(page_idx & (order_size - 1));
437 VM_BUG_ON(bad_range(zone, page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Christoph Lameterd23ad422007-02-10 01:43:02 -0800439 __mod_zone_page_state(zone, NR_FREE_PAGES, order_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 while (order < MAX_ORDER-1) {
441 unsigned long combined_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 struct page *buddy;
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 buddy = __page_find_buddy(page, page_idx, order);
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700445 if (!page_is_buddy(page, buddy, order))
Andy Whitcroft3c82d0c2008-07-23 21:27:11 -0700446 break;
Nick Piggin13e74442006-01-06 00:10:58 -0800447
Andy Whitcroft3c82d0c2008-07-23 21:27:11 -0700448 /* Our buddy is free, merge with it and move up one order. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 list_del(&buddy->lru);
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700450 zone->free_area[order].nr_free--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 rmv_page_order(buddy);
Nick Piggin13e74442006-01-06 00:10:58 -0800452 combined_idx = __find_combined_index(page_idx, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 page = page + (combined_idx - page_idx);
454 page_idx = combined_idx;
455 order++;
456 }
457 set_page_order(page, order);
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700458 list_add(&page->lru,
459 &zone->free_area[order].free_list[migratetype]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 zone->free_area[order].nr_free++;
461}
462
Nick Piggin224abf92006-01-06 00:11:11 -0800463static inline int free_pages_check(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Lee Schermerhorn985737c2008-10-18 20:26:53 -0700465 free_page_mlock(page);
Nick Piggin92be2e332006-01-06 00:10:57 -0800466 if (unlikely(page_mapcount(page) |
467 (page->mapping != NULL) |
468 (page_count(page) != 0) |
Russ Andersondfa7e202008-06-09 11:18:45 -0500469 (page->flags & PAGE_FLAGS_CHECK_AT_FREE)))
Nick Piggin224abf92006-01-06 00:11:11 -0800470 bad_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (PageDirty(page))
Nick Piggin242e5462005-09-03 15:54:50 -0700472 __ClearPageDirty(page);
Rik van Rielb2e18532008-10-18 20:26:30 -0700473 if (PageSwapBacked(page))
474 __ClearPageSwapBacked(page);
Hugh Dickins689bceb2005-11-21 21:32:20 -0800475 /*
476 * For now, we report if PG_reserved was found set, but do not
477 * clear it, and do not free the page. But we shall soon need
478 * to do more, for when the ZERO_PAGE count wraps negative.
479 */
480 return PageReserved(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
483/*
484 * Frees a list of pages.
485 * Assumes all pages on list are in same zone, and of same order.
Renaud Lienhart207f36e2005-09-10 00:26:59 -0700486 * count is the number of pages to free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 *
488 * If the zone was previously in an "all pages pinned" state then look to
489 * see if this freeing clears that state.
490 *
491 * And clear the zone's pages_scanned counter, to hold off the "all pages are
492 * pinned" detection logic.
493 */
Nick Piggin48db57f2006-01-08 01:00:42 -0800494static void free_pages_bulk(struct zone *zone, int count,
495 struct list_head *list, int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Nick Pigginc54ad302006-01-06 00:10:56 -0800497 spin_lock(&zone->lock);
David Rientjese815af92007-10-16 23:25:54 -0700498 zone_clear_flag(zone, ZONE_ALL_UNRECLAIMABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 zone->pages_scanned = 0;
Nick Piggin48db57f2006-01-08 01:00:42 -0800500 while (count--) {
501 struct page *page;
502
Nick Piggin725d7042006-09-25 23:30:55 -0700503 VM_BUG_ON(list_empty(list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 page = list_entry(list->prev, struct page, lru);
Nick Piggin48db57f2006-01-08 01:00:42 -0800505 /* have to delete it as __free_one_page list manipulates */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 list_del(&page->lru);
Nick Piggin48db57f2006-01-08 01:00:42 -0800507 __free_one_page(page, zone, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
Nick Pigginc54ad302006-01-06 00:10:56 -0800509 spin_unlock(&zone->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
Nick Piggin48db57f2006-01-08 01:00:42 -0800512static void free_one_page(struct zone *zone, struct page *page, int order)
513{
Christoph Lameter006d22d2006-09-25 23:31:48 -0700514 spin_lock(&zone->lock);
David Rientjese815af92007-10-16 23:25:54 -0700515 zone_clear_flag(zone, ZONE_ALL_UNRECLAIMABLE);
Christoph Lameter006d22d2006-09-25 23:31:48 -0700516 zone->pages_scanned = 0;
Paul Jackson0798e512006-12-06 20:31:38 -0800517 __free_one_page(page, zone, order);
Christoph Lameter006d22d2006-09-25 23:31:48 -0700518 spin_unlock(&zone->lock);
Nick Piggin48db57f2006-01-08 01:00:42 -0800519}
520
521static void __free_pages_ok(struct page *page, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Nick Pigginc54ad302006-01-06 00:10:56 -0800523 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 int i;
Hugh Dickins689bceb2005-11-21 21:32:20 -0800525 int reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 for (i = 0 ; i < (1 << order) ; ++i)
Nick Piggin224abf92006-01-06 00:11:11 -0800528 reserved += free_pages_check(page + i);
Hugh Dickins689bceb2005-11-21 21:32:20 -0800529 if (reserved)
530 return;
531
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700532 if (!PageHighMem(page)) {
Nick Piggin9858db52006-10-11 01:21:30 -0700533 debug_check_no_locks_freed(page_address(page),PAGE_SIZE<<order);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700534 debug_check_no_obj_freed(page_address(page),
535 PAGE_SIZE << order);
536 }
Nick Piggindafb1362006-10-11 01:21:30 -0700537 arch_free_page(page, order);
Nick Piggin48db57f2006-01-08 01:00:42 -0800538 kernel_map_pages(page, 1 << order, 0);
Nick Piggindafb1362006-10-11 01:21:30 -0700539
Nick Pigginc54ad302006-01-06 00:10:56 -0800540 local_irq_save(flags);
Christoph Lameterf8891e52006-06-30 01:55:45 -0700541 __count_vm_events(PGFREE, 1 << order);
Nick Piggin48db57f2006-01-08 01:00:42 -0800542 free_one_page(page_zone(page), page, order);
Nick Pigginc54ad302006-01-06 00:10:56 -0800543 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
David Howellsa226f6c2006-01-06 00:11:08 -0800546/*
547 * permit the bootmem allocator to evade page validation on high-order frees
548 */
Yasunori Gotoaf370fb2008-07-23 21:28:17 -0700549void __meminit __free_pages_bootmem(struct page *page, unsigned int order)
David Howellsa226f6c2006-01-06 00:11:08 -0800550{
551 if (order == 0) {
552 __ClearPageReserved(page);
553 set_page_count(page, 0);
Nick Piggin7835e982006-03-22 00:08:40 -0800554 set_page_refcounted(page);
Nick Piggin545b1ea2006-03-22 00:08:07 -0800555 __free_page(page);
David Howellsa226f6c2006-01-06 00:11:08 -0800556 } else {
David Howellsa226f6c2006-01-06 00:11:08 -0800557 int loop;
558
Nick Piggin545b1ea2006-03-22 00:08:07 -0800559 prefetchw(page);
David Howellsa226f6c2006-01-06 00:11:08 -0800560 for (loop = 0; loop < BITS_PER_LONG; loop++) {
561 struct page *p = &page[loop];
562
Nick Piggin545b1ea2006-03-22 00:08:07 -0800563 if (loop + 1 < BITS_PER_LONG)
564 prefetchw(p + 1);
David Howellsa226f6c2006-01-06 00:11:08 -0800565 __ClearPageReserved(p);
566 set_page_count(p, 0);
567 }
568
Nick Piggin7835e982006-03-22 00:08:40 -0800569 set_page_refcounted(page);
Nick Piggin545b1ea2006-03-22 00:08:07 -0800570 __free_pages(page, order);
David Howellsa226f6c2006-01-06 00:11:08 -0800571 }
572}
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575/*
576 * The order of subdivision here is critical for the IO subsystem.
577 * Please do not alter this order without good reasons and regression
578 * testing. Specifically, as large blocks of memory are subdivided,
579 * the order in which smaller blocks are delivered depends on the order
580 * they're subdivided in this function. This is the primary factor
581 * influencing the order in which pages are delivered to the IO
582 * subsystem according to empirical testing, and this is also justified
583 * by considering the behavior of a buddy system containing a single
584 * large block of memory acted on by a series of small allocations.
585 * This behavior is a critical factor in sglist merging's success.
586 *
587 * -- wli
588 */
Nick Piggin085cc7d2006-01-06 00:11:01 -0800589static inline void expand(struct zone *zone, struct page *page,
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700590 int low, int high, struct free_area *area,
591 int migratetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 unsigned long size = 1 << high;
594
595 while (high > low) {
596 area--;
597 high--;
598 size >>= 1;
Nick Piggin725d7042006-09-25 23:30:55 -0700599 VM_BUG_ON(bad_range(zone, &page[size]));
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700600 list_add(&page[size].lru, &area->free_list[migratetype]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 area->nr_free++;
602 set_page_order(&page[size], high);
603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606/*
607 * This page is about to be returned from the page allocator
608 */
Nick Piggin17cf4402006-03-22 00:08:41 -0800609static int prep_new_page(struct page *page, int order, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Nick Piggin92be2e332006-01-06 00:10:57 -0800611 if (unlikely(page_mapcount(page) |
612 (page->mapping != NULL) |
613 (page_count(page) != 0) |
Russ Andersondfa7e202008-06-09 11:18:45 -0500614 (page->flags & PAGE_FLAGS_CHECK_AT_PREP)))
Nick Piggin224abf92006-01-06 00:11:11 -0800615 bad_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Hugh Dickins689bceb2005-11-21 21:32:20 -0800617 /*
618 * For now, we report if PG_reserved was found set, but do not
619 * clear it, and do not allocate the page: as a safety net.
620 */
621 if (PageReserved(page))
622 return 1;
623
Christoph Lameter0a128b22008-04-28 02:12:52 -0700624 page->flags &= ~(1 << PG_uptodate | 1 << PG_error | 1 << PG_reclaim |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 1 << PG_referenced | 1 << PG_arch_1 |
Nick Pigginb291f002008-10-18 20:26:44 -0700626 1 << PG_owner_priv_1 | 1 << PG_mappedtodisk
627#ifdef CONFIG_UNEVICTABLE_LRU
628 | 1 << PG_mlocked
629#endif
630 );
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700631 set_page_private(page, 0);
Nick Piggin7835e982006-03-22 00:08:40 -0800632 set_page_refcounted(page);
Nick Piggincc1025092006-12-06 20:32:00 -0800633
634 arch_alloc_page(page, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 kernel_map_pages(page, 1 << order, 1);
Nick Piggin17cf4402006-03-22 00:08:41 -0800636
637 if (gfp_flags & __GFP_ZERO)
638 prep_zero_page(page, order, gfp_flags);
639
640 if (order && (gfp_flags & __GFP_COMP))
641 prep_compound_page(page, order);
642
Hugh Dickins689bceb2005-11-21 21:32:20 -0800643 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
Mel Gorman56fd56b2007-10-16 01:25:58 -0700646/*
647 * Go through the free lists for the given migratetype and remove
648 * the smallest available page from the freelists
649 */
650static struct page *__rmqueue_smallest(struct zone *zone, unsigned int order,
651 int migratetype)
652{
653 unsigned int current_order;
654 struct free_area * area;
655 struct page *page;
656
657 /* Find a page of the appropriate size in the preferred list */
658 for (current_order = order; current_order < MAX_ORDER; ++current_order) {
659 area = &(zone->free_area[current_order]);
660 if (list_empty(&area->free_list[migratetype]))
661 continue;
662
663 page = list_entry(area->free_list[migratetype].next,
664 struct page, lru);
665 list_del(&page->lru);
666 rmv_page_order(page);
667 area->nr_free--;
668 __mod_zone_page_state(zone, NR_FREE_PAGES, - (1UL << order));
669 expand(zone, page, order, current_order, area, migratetype);
670 return page;
671 }
672
673 return NULL;
674}
675
676
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700677/*
678 * This array describes the order lists are fallen back to when
679 * the free lists for the desirable migrate type are depleted
680 */
681static int fallbacks[MIGRATE_TYPES][MIGRATE_TYPES-1] = {
Mel Gorman64c5e132007-10-16 01:25:59 -0700682 [MIGRATE_UNMOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_MOVABLE, MIGRATE_RESERVE },
683 [MIGRATE_RECLAIMABLE] = { MIGRATE_UNMOVABLE, MIGRATE_MOVABLE, MIGRATE_RESERVE },
684 [MIGRATE_MOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE, MIGRATE_RESERVE },
685 [MIGRATE_RESERVE] = { MIGRATE_RESERVE, MIGRATE_RESERVE, MIGRATE_RESERVE }, /* Never used */
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700686};
687
Mel Gormanc361be52007-10-16 01:25:51 -0700688/*
689 * Move the free pages in a range to the free lists of the requested type.
Mel Gormand9c23402007-10-16 01:26:01 -0700690 * Note that start_page and end_pages are not aligned on a pageblock
Mel Gormanc361be52007-10-16 01:25:51 -0700691 * boundary. If alignment is required, use move_freepages_block()
692 */
Adrian Bunkb69a7282008-07-23 21:28:12 -0700693static int move_freepages(struct zone *zone,
694 struct page *start_page, struct page *end_page,
695 int migratetype)
Mel Gormanc361be52007-10-16 01:25:51 -0700696{
697 struct page *page;
698 unsigned long order;
Mel Gormand1003132007-10-16 01:26:00 -0700699 int pages_moved = 0;
Mel Gormanc361be52007-10-16 01:25:51 -0700700
701#ifndef CONFIG_HOLES_IN_ZONE
702 /*
703 * page_zone is not safe to call in this context when
704 * CONFIG_HOLES_IN_ZONE is set. This bug check is probably redundant
705 * anyway as we check zone boundaries in move_freepages_block().
706 * Remove at a later date when no bug reports exist related to
Mel Gormanac0e5b72007-10-16 01:25:58 -0700707 * grouping pages by mobility
Mel Gormanc361be52007-10-16 01:25:51 -0700708 */
709 BUG_ON(page_zone(start_page) != page_zone(end_page));
710#endif
711
712 for (page = start_page; page <= end_page;) {
Adam Litke344c7902008-09-02 14:35:38 -0700713 /* Make sure we are not inadvertently changing nodes */
714 VM_BUG_ON(page_to_nid(page) != zone_to_nid(zone));
715
Mel Gormanc361be52007-10-16 01:25:51 -0700716 if (!pfn_valid_within(page_to_pfn(page))) {
717 page++;
718 continue;
719 }
720
721 if (!PageBuddy(page)) {
722 page++;
723 continue;
724 }
725
726 order = page_order(page);
727 list_del(&page->lru);
728 list_add(&page->lru,
729 &zone->free_area[order].free_list[migratetype]);
730 page += 1 << order;
Mel Gormand1003132007-10-16 01:26:00 -0700731 pages_moved += 1 << order;
Mel Gormanc361be52007-10-16 01:25:51 -0700732 }
733
Mel Gormand1003132007-10-16 01:26:00 -0700734 return pages_moved;
Mel Gormanc361be52007-10-16 01:25:51 -0700735}
736
Adrian Bunkb69a7282008-07-23 21:28:12 -0700737static int move_freepages_block(struct zone *zone, struct page *page,
738 int migratetype)
Mel Gormanc361be52007-10-16 01:25:51 -0700739{
740 unsigned long start_pfn, end_pfn;
741 struct page *start_page, *end_page;
742
743 start_pfn = page_to_pfn(page);
Mel Gormand9c23402007-10-16 01:26:01 -0700744 start_pfn = start_pfn & ~(pageblock_nr_pages-1);
Mel Gormanc361be52007-10-16 01:25:51 -0700745 start_page = pfn_to_page(start_pfn);
Mel Gormand9c23402007-10-16 01:26:01 -0700746 end_page = start_page + pageblock_nr_pages - 1;
747 end_pfn = start_pfn + pageblock_nr_pages - 1;
Mel Gormanc361be52007-10-16 01:25:51 -0700748
749 /* Do not cross zone boundaries */
750 if (start_pfn < zone->zone_start_pfn)
751 start_page = page;
752 if (end_pfn >= zone->zone_start_pfn + zone->spanned_pages)
753 return 0;
754
755 return move_freepages(zone, start_page, end_page, migratetype);
756}
757
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700758/* Remove an element from the buddy allocator from the fallback list */
759static struct page *__rmqueue_fallback(struct zone *zone, int order,
760 int start_migratetype)
761{
762 struct free_area * area;
763 int current_order;
764 struct page *page;
765 int migratetype, i;
766
767 /* Find the largest possible block of pages in the other list */
768 for (current_order = MAX_ORDER-1; current_order >= order;
769 --current_order) {
770 for (i = 0; i < MIGRATE_TYPES - 1; i++) {
771 migratetype = fallbacks[start_migratetype][i];
772
Mel Gorman56fd56b2007-10-16 01:25:58 -0700773 /* MIGRATE_RESERVE handled later if necessary */
774 if (migratetype == MIGRATE_RESERVE)
775 continue;
Mel Gormane0104872007-10-16 01:25:53 -0700776
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700777 area = &(zone->free_area[current_order]);
778 if (list_empty(&area->free_list[migratetype]))
779 continue;
780
781 page = list_entry(area->free_list[migratetype].next,
782 struct page, lru);
783 area->nr_free--;
784
785 /*
Mel Gormanc361be52007-10-16 01:25:51 -0700786 * If breaking a large block of pages, move all free
Mel Gorman46dafbc2007-10-16 01:25:55 -0700787 * pages to the preferred allocation list. If falling
788 * back for a reclaimable kernel allocation, be more
789 * agressive about taking ownership of free pages
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700790 */
Mel Gormand9c23402007-10-16 01:26:01 -0700791 if (unlikely(current_order >= (pageblock_order >> 1)) ||
Mel Gorman46dafbc2007-10-16 01:25:55 -0700792 start_migratetype == MIGRATE_RECLAIMABLE) {
793 unsigned long pages;
794 pages = move_freepages_block(zone, page,
795 start_migratetype);
796
797 /* Claim the whole block if over half of it is free */
Mel Gormand9c23402007-10-16 01:26:01 -0700798 if (pages >= (1 << (pageblock_order-1)))
Mel Gorman46dafbc2007-10-16 01:25:55 -0700799 set_pageblock_migratetype(page,
800 start_migratetype);
801
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700802 migratetype = start_migratetype;
Mel Gormanc361be52007-10-16 01:25:51 -0700803 }
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700804
805 /* Remove the page from the freelists */
806 list_del(&page->lru);
807 rmv_page_order(page);
808 __mod_zone_page_state(zone, NR_FREE_PAGES,
809 -(1UL << order));
810
Mel Gormand9c23402007-10-16 01:26:01 -0700811 if (current_order == pageblock_order)
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700812 set_pageblock_migratetype(page,
813 start_migratetype);
814
815 expand(zone, page, order, current_order, area, migratetype);
816 return page;
817 }
818 }
819
Mel Gorman56fd56b2007-10-16 01:25:58 -0700820 /* Use MIGRATE_RESERVE rather than fail an allocation */
821 return __rmqueue_smallest(zone, order, MIGRATE_RESERVE);
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700822}
823
Mel Gorman56fd56b2007-10-16 01:25:58 -0700824/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 * Do the hard work of removing an element from the buddy allocator.
826 * Call me with the zone->lock already held.
827 */
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700828static struct page *__rmqueue(struct zone *zone, unsigned int order,
829 int migratetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 struct page *page;
832
Mel Gorman56fd56b2007-10-16 01:25:58 -0700833 page = __rmqueue_smallest(zone, order, migratetype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Mel Gorman56fd56b2007-10-16 01:25:58 -0700835 if (unlikely(!page))
836 page = __rmqueue_fallback(zone, order, migratetype);
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700837
838 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
841/*
842 * Obtain a specified number of elements from the buddy allocator, all under
843 * a single hold of the lock, for efficiency. Add them to the supplied list.
844 * Returns the number of new pages which were placed at *list.
845 */
846static int rmqueue_bulk(struct zone *zone, unsigned int order,
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700847 unsigned long count, struct list_head *list,
848 int migratetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Nick Pigginc54ad302006-01-06 00:10:56 -0800852 spin_lock(&zone->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 for (i = 0; i < count; ++i) {
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700854 struct page *page = __rmqueue(zone, order, migratetype);
Nick Piggin085cc7d2006-01-06 00:11:01 -0800855 if (unlikely(page == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 break;
Mel Gorman81eabcb2007-12-17 16:20:05 -0800857
858 /*
859 * Split buddy pages returned by expand() are received here
860 * in physical page order. The page is added to the callers and
861 * list and the list head then moves forward. From the callers
862 * perspective, the linked list is ordered by page number in
863 * some conditions. This is useful for IO devices that can
864 * merge IO requests if the physical pages are ordered
865 * properly.
866 */
Mel Gorman535131e62007-10-16 01:25:49 -0700867 list_add(&page->lru, list);
868 set_page_private(page, migratetype);
Mel Gorman81eabcb2007-12-17 16:20:05 -0800869 list = &page->lru;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
Nick Pigginc54ad302006-01-06 00:10:56 -0800871 spin_unlock(&zone->lock);
Nick Piggin085cc7d2006-01-06 00:11:01 -0800872 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
874
Christoph Lameter4ae7c032005-06-21 17:14:57 -0700875#ifdef CONFIG_NUMA
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800876/*
Christoph Lameter4037d452007-05-09 02:35:14 -0700877 * Called from the vmstat counter updater to drain pagesets of this
878 * currently executing processor on remote nodes after they have
879 * expired.
880 *
Christoph Lameter879336c2006-03-22 00:09:08 -0800881 * Note that this function must be called with the thread pinned to
882 * a single processor.
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800883 */
Christoph Lameter4037d452007-05-09 02:35:14 -0700884void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
Christoph Lameter4ae7c032005-06-21 17:14:57 -0700885{
Christoph Lameter4ae7c032005-06-21 17:14:57 -0700886 unsigned long flags;
Christoph Lameter4037d452007-05-09 02:35:14 -0700887 int to_drain;
Christoph Lameter4ae7c032005-06-21 17:14:57 -0700888
Christoph Lameter4037d452007-05-09 02:35:14 -0700889 local_irq_save(flags);
890 if (pcp->count >= pcp->batch)
891 to_drain = pcp->batch;
892 else
893 to_drain = pcp->count;
894 free_pages_bulk(zone, to_drain, &pcp->list, 0);
895 pcp->count -= to_drain;
896 local_irq_restore(flags);
Christoph Lameter4ae7c032005-06-21 17:14:57 -0700897}
898#endif
899
Christoph Lameter9f8f2172008-02-04 22:29:11 -0800900/*
901 * Drain pages of the indicated processor.
902 *
903 * The processor must either be the current processor and the
904 * thread pinned to the current processor or a processor that
905 * is not online.
906 */
907static void drain_pages(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Nick Pigginc54ad302006-01-06 00:10:56 -0800909 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 struct zone *zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 for_each_zone(zone) {
913 struct per_cpu_pageset *pset;
Christoph Lameter3dfa5722008-02-04 22:29:19 -0800914 struct per_cpu_pages *pcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Christoph Lameterf2e12bb2007-01-05 16:37:02 -0800916 if (!populated_zone(zone))
917 continue;
918
Christoph Lametere7c8d5c2005-06-21 17:14:47 -0700919 pset = zone_pcp(zone, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Christoph Lameter3dfa5722008-02-04 22:29:19 -0800921 pcp = &pset->pcp;
922 local_irq_save(flags);
923 free_pages_bulk(zone, pcp->count, &pcp->list, 0);
924 pcp->count = 0;
925 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
927}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Christoph Lameter9f8f2172008-02-04 22:29:11 -0800929/*
930 * Spill all of this CPU's per-cpu pages back into the buddy allocator.
931 */
932void drain_local_pages(void *arg)
933{
934 drain_pages(smp_processor_id());
935}
936
937/*
938 * Spill all the per-cpu pages from all CPUs back into the buddy allocator
939 */
940void drain_all_pages(void)
941{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200942 on_each_cpu(drain_local_pages, NULL, 1);
Christoph Lameter9f8f2172008-02-04 22:29:11 -0800943}
944
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200945#ifdef CONFIG_HIBERNATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947void mark_free_pages(struct zone *zone)
948{
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700949 unsigned long pfn, max_zone_pfn;
950 unsigned long flags;
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700951 int order, t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 struct list_head *curr;
953
954 if (!zone->spanned_pages)
955 return;
956
957 spin_lock_irqsave(&zone->lock, flags);
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700958
959 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
960 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
961 if (pfn_valid(pfn)) {
962 struct page *page = pfn_to_page(pfn);
963
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700964 if (!swsusp_page_is_forbidden(page))
965 swsusp_unset_page_free(page);
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700968 for_each_migratetype_order(order, t) {
969 list_for_each(curr, &zone->free_area[order].free_list[t]) {
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700970 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700972 pfn = page_to_pfn(list_entry(curr, struct page, lru));
973 for (i = 0; i < (1UL << order); i++)
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700974 swsusp_set_page_free(pfn_to_page(pfn + i));
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700975 }
Mel Gormanb2a0ac82007-10-16 01:25:48 -0700976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 spin_unlock_irqrestore(&zone->lock, flags);
978}
Mel Gormane2c55dc2007-10-16 01:25:50 -0700979#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 * Free a 0-order page
983 */
Harvey Harrison920c7a52008-02-04 22:29:26 -0800984static void free_hot_cold_page(struct page *page, int cold)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
986 struct zone *zone = page_zone(page);
987 struct per_cpu_pages *pcp;
988 unsigned long flags;
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 if (PageAnon(page))
991 page->mapping = NULL;
Nick Piggin224abf92006-01-06 00:11:11 -0800992 if (free_pages_check(page))
Hugh Dickins689bceb2005-11-21 21:32:20 -0800993 return;
994
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700995 if (!PageHighMem(page)) {
Nick Piggin9858db52006-10-11 01:21:30 -0700996 debug_check_no_locks_freed(page_address(page), PAGE_SIZE);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700997 debug_check_no_obj_freed(page_address(page), PAGE_SIZE);
998 }
Nick Piggindafb1362006-10-11 01:21:30 -0700999 arch_free_page(page, 0);
Hugh Dickins689bceb2005-11-21 21:32:20 -08001000 kernel_map_pages(page, 1, 0);
1001
Christoph Lameter3dfa5722008-02-04 22:29:19 -08001002 pcp = &zone_pcp(zone, get_cpu())->pcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 local_irq_save(flags);
Christoph Lameterf8891e52006-06-30 01:55:45 -07001004 __count_vm_event(PGFREE);
Christoph Lameter3dfa5722008-02-04 22:29:19 -08001005 if (cold)
1006 list_add_tail(&page->lru, &pcp->list);
1007 else
1008 list_add(&page->lru, &pcp->list);
Mel Gorman535131e62007-10-16 01:25:49 -07001009 set_page_private(page, get_pageblock_migratetype(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 pcp->count++;
Nick Piggin48db57f2006-01-08 01:00:42 -08001011 if (pcp->count >= pcp->high) {
1012 free_pages_bulk(zone, pcp->batch, &pcp->list, 0);
1013 pcp->count -= pcp->batch;
1014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 local_irq_restore(flags);
1016 put_cpu();
1017}
1018
Harvey Harrison920c7a52008-02-04 22:29:26 -08001019void free_hot_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
1021 free_hot_cold_page(page, 0);
1022}
1023
Harvey Harrison920c7a52008-02-04 22:29:26 -08001024void free_cold_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
1026 free_hot_cold_page(page, 1);
1027}
1028
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08001029/*
1030 * split_page takes a non-compound higher-order page, and splits it into
1031 * n (1<<order) sub-pages: page[0..n]
1032 * Each sub-page must be freed individually.
1033 *
1034 * Note: this is probably too low level an operation for use in drivers.
1035 * Please consult with lkml before using this in your driver.
1036 */
1037void split_page(struct page *page, unsigned int order)
1038{
1039 int i;
1040
Nick Piggin725d7042006-09-25 23:30:55 -07001041 VM_BUG_ON(PageCompound(page));
1042 VM_BUG_ON(!page_count(page));
Nick Piggin7835e982006-03-22 00:08:40 -08001043 for (i = 1; i < (1 << order); i++)
1044 set_page_refcounted(page + i);
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08001045}
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08001046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047/*
1048 * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
1049 * we cheat by calling it from here, in the order > 0 path. Saves a branch
1050 * or two.
1051 */
Mel Gorman18ea7e72008-04-28 02:12:14 -07001052static struct page *buffered_rmqueue(struct zone *preferred_zone,
Nick Piggina74609f2006-01-06 00:11:20 -08001053 struct zone *zone, int order, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
1055 unsigned long flags;
Hugh Dickins689bceb2005-11-21 21:32:20 -08001056 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 int cold = !!(gfp_flags & __GFP_COLD);
Nick Piggina74609f2006-01-06 00:11:20 -08001058 int cpu;
Mel Gorman64c5e132007-10-16 01:25:59 -07001059 int migratetype = allocflags_to_migratetype(gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Hugh Dickins689bceb2005-11-21 21:32:20 -08001061again:
Nick Piggina74609f2006-01-06 00:11:20 -08001062 cpu = get_cpu();
Nick Piggin48db57f2006-01-08 01:00:42 -08001063 if (likely(order == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 struct per_cpu_pages *pcp;
1065
Christoph Lameter3dfa5722008-02-04 22:29:19 -08001066 pcp = &zone_pcp(zone, cpu)->pcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 local_irq_save(flags);
Nick Piggina74609f2006-01-06 00:11:20 -08001068 if (!pcp->count) {
nkalmala941c7102006-11-02 22:07:04 -08001069 pcp->count = rmqueue_bulk(zone, 0,
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001070 pcp->batch, &pcp->list, migratetype);
Nick Piggina74609f2006-01-06 00:11:20 -08001071 if (unlikely(!pcp->count))
1072 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 }
Mel Gorman535131e62007-10-16 01:25:49 -07001074
Mel Gormanb92a6ed2007-10-16 01:25:50 -07001075 /* Find a page of the appropriate migrate type */
Christoph Lameter3dfa5722008-02-04 22:29:19 -08001076 if (cold) {
1077 list_for_each_entry_reverse(page, &pcp->list, lru)
1078 if (page_private(page) == migratetype)
1079 break;
1080 } else {
1081 list_for_each_entry(page, &pcp->list, lru)
1082 if (page_private(page) == migratetype)
1083 break;
1084 }
Mel Gormanb92a6ed2007-10-16 01:25:50 -07001085
1086 /* Allocate more to the pcp list if necessary */
1087 if (unlikely(&page->lru == &pcp->list)) {
Mel Gorman535131e62007-10-16 01:25:49 -07001088 pcp->count += rmqueue_bulk(zone, 0,
1089 pcp->batch, &pcp->list, migratetype);
1090 page = list_entry(pcp->list.next, struct page, lru);
Mel Gorman535131e62007-10-16 01:25:49 -07001091 }
Mel Gormanb92a6ed2007-10-16 01:25:50 -07001092
1093 list_del(&page->lru);
1094 pcp->count--;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001095 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 spin_lock_irqsave(&zone->lock, flags);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07001097 page = __rmqueue(zone, order, migratetype);
Nick Piggina74609f2006-01-06 00:11:20 -08001098 spin_unlock(&zone->lock);
1099 if (!page)
1100 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
1102
Christoph Lameterf8891e52006-06-30 01:55:45 -07001103 __count_zone_vm_events(PGALLOC, zone, 1 << order);
Mel Gorman18ea7e72008-04-28 02:12:14 -07001104 zone_statistics(preferred_zone, zone);
Nick Piggina74609f2006-01-06 00:11:20 -08001105 local_irq_restore(flags);
1106 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Nick Piggin725d7042006-09-25 23:30:55 -07001108 VM_BUG_ON(bad_range(zone, page));
Nick Piggin17cf4402006-03-22 00:08:41 -08001109 if (prep_new_page(page, order, gfp_flags))
Nick Piggina74609f2006-01-06 00:11:20 -08001110 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return page;
Nick Piggina74609f2006-01-06 00:11:20 -08001112
1113failed:
1114 local_irq_restore(flags);
1115 put_cpu();
1116 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117}
1118
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001119#define ALLOC_NO_WATERMARKS 0x01 /* don't check watermarks at all */
Nick Piggin31488902005-11-28 13:44:03 -08001120#define ALLOC_WMARK_MIN 0x02 /* use pages_min watermark */
1121#define ALLOC_WMARK_LOW 0x04 /* use pages_low watermark */
1122#define ALLOC_WMARK_HIGH 0x08 /* use pages_high watermark */
1123#define ALLOC_HARDER 0x10 /* try to alloc harder */
1124#define ALLOC_HIGH 0x20 /* __GFP_HIGH set */
1125#define ALLOC_CPUSET 0x40 /* check for correct cpuset */
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001126
Akinobu Mita933e3122006-12-08 02:39:45 -08001127#ifdef CONFIG_FAIL_PAGE_ALLOC
1128
1129static struct fail_page_alloc_attr {
1130 struct fault_attr attr;
1131
1132 u32 ignore_gfp_highmem;
1133 u32 ignore_gfp_wait;
Akinobu Mita54114992007-07-15 23:40:23 -07001134 u32 min_order;
Akinobu Mita933e3122006-12-08 02:39:45 -08001135
1136#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
1137
1138 struct dentry *ignore_gfp_highmem_file;
1139 struct dentry *ignore_gfp_wait_file;
Akinobu Mita54114992007-07-15 23:40:23 -07001140 struct dentry *min_order_file;
Akinobu Mita933e3122006-12-08 02:39:45 -08001141
1142#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
1143
1144} fail_page_alloc = {
1145 .attr = FAULT_ATTR_INITIALIZER,
Don Mullis6b1b60f2006-12-08 02:39:53 -08001146 .ignore_gfp_wait = 1,
1147 .ignore_gfp_highmem = 1,
Akinobu Mita54114992007-07-15 23:40:23 -07001148 .min_order = 1,
Akinobu Mita933e3122006-12-08 02:39:45 -08001149};
1150
1151static int __init setup_fail_page_alloc(char *str)
1152{
1153 return setup_fault_attr(&fail_page_alloc.attr, str);
1154}
1155__setup("fail_page_alloc=", setup_fail_page_alloc);
1156
1157static int should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
1158{
Akinobu Mita54114992007-07-15 23:40:23 -07001159 if (order < fail_page_alloc.min_order)
1160 return 0;
Akinobu Mita933e3122006-12-08 02:39:45 -08001161 if (gfp_mask & __GFP_NOFAIL)
1162 return 0;
1163 if (fail_page_alloc.ignore_gfp_highmem && (gfp_mask & __GFP_HIGHMEM))
1164 return 0;
1165 if (fail_page_alloc.ignore_gfp_wait && (gfp_mask & __GFP_WAIT))
1166 return 0;
1167
1168 return should_fail(&fail_page_alloc.attr, 1 << order);
1169}
1170
1171#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
1172
1173static int __init fail_page_alloc_debugfs(void)
1174{
1175 mode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
1176 struct dentry *dir;
1177 int err;
1178
1179 err = init_fault_attr_dentries(&fail_page_alloc.attr,
1180 "fail_page_alloc");
1181 if (err)
1182 return err;
1183 dir = fail_page_alloc.attr.dentries.dir;
1184
1185 fail_page_alloc.ignore_gfp_wait_file =
1186 debugfs_create_bool("ignore-gfp-wait", mode, dir,
1187 &fail_page_alloc.ignore_gfp_wait);
1188
1189 fail_page_alloc.ignore_gfp_highmem_file =
1190 debugfs_create_bool("ignore-gfp-highmem", mode, dir,
1191 &fail_page_alloc.ignore_gfp_highmem);
Akinobu Mita54114992007-07-15 23:40:23 -07001192 fail_page_alloc.min_order_file =
1193 debugfs_create_u32("min-order", mode, dir,
1194 &fail_page_alloc.min_order);
Akinobu Mita933e3122006-12-08 02:39:45 -08001195
1196 if (!fail_page_alloc.ignore_gfp_wait_file ||
Akinobu Mita54114992007-07-15 23:40:23 -07001197 !fail_page_alloc.ignore_gfp_highmem_file ||
1198 !fail_page_alloc.min_order_file) {
Akinobu Mita933e3122006-12-08 02:39:45 -08001199 err = -ENOMEM;
1200 debugfs_remove(fail_page_alloc.ignore_gfp_wait_file);
1201 debugfs_remove(fail_page_alloc.ignore_gfp_highmem_file);
Akinobu Mita54114992007-07-15 23:40:23 -07001202 debugfs_remove(fail_page_alloc.min_order_file);
Akinobu Mita933e3122006-12-08 02:39:45 -08001203 cleanup_fault_attr_dentries(&fail_page_alloc.attr);
1204 }
1205
1206 return err;
1207}
1208
1209late_initcall(fail_page_alloc_debugfs);
1210
1211#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
1212
1213#else /* CONFIG_FAIL_PAGE_ALLOC */
1214
1215static inline int should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
1216{
1217 return 0;
1218}
1219
1220#endif /* CONFIG_FAIL_PAGE_ALLOC */
1221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222/*
1223 * Return 1 if free pages are above 'mark'. This takes into account the order
1224 * of the allocation.
1225 */
1226int zone_watermark_ok(struct zone *z, int order, unsigned long mark,
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001227 int classzone_idx, int alloc_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
1229 /* free_pages my go negative - that's OK */
Christoph Lameterd23ad422007-02-10 01:43:02 -08001230 long min = mark;
1231 long free_pages = zone_page_state(z, NR_FREE_PAGES) - (1 << order) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 int o;
1233
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001234 if (alloc_flags & ALLOC_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 min -= min / 2;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001236 if (alloc_flags & ALLOC_HARDER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 min -= min / 4;
1238
1239 if (free_pages <= min + z->lowmem_reserve[classzone_idx])
1240 return 0;
1241 for (o = 0; o < order; o++) {
1242 /* At the next order, this order's pages become unavailable */
1243 free_pages -= z->free_area[o].nr_free << o;
1244
1245 /* Require fewer higher order pages to be free */
1246 min >>= 1;
1247
1248 if (free_pages <= min)
1249 return 0;
1250 }
1251 return 1;
1252}
1253
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001254#ifdef CONFIG_NUMA
1255/*
1256 * zlc_setup - Setup for "zonelist cache". Uses cached zone data to
1257 * skip over zones that are not allowed by the cpuset, or that have
1258 * been recently (in last second) found to be nearly full. See further
1259 * comments in mmzone.h. Reduces cache footprint of zonelist scans
Simon Arlott183ff222007-10-20 01:27:18 +02001260 * that have to skip over a lot of full or unallowed zones.
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001261 *
1262 * If the zonelist cache is present in the passed in zonelist, then
1263 * returns a pointer to the allowed node mask (either the current
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07001264 * tasks mems_allowed, or node_states[N_HIGH_MEMORY].)
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001265 *
1266 * If the zonelist cache is not available for this zonelist, does
1267 * nothing and returns NULL.
1268 *
1269 * If the fullzones BITMAP in the zonelist cache is stale (more than
1270 * a second since last zap'd) then we zap it out (clear its bits.)
1271 *
1272 * We hold off even calling zlc_setup, until after we've checked the
1273 * first zone in the zonelist, on the theory that most allocations will
1274 * be satisfied from that first zone, so best to examine that zone as
1275 * quickly as we can.
1276 */
1277static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
1278{
1279 struct zonelist_cache *zlc; /* cached zonelist speedup info */
1280 nodemask_t *allowednodes; /* zonelist_cache approximation */
1281
1282 zlc = zonelist->zlcache_ptr;
1283 if (!zlc)
1284 return NULL;
1285
S.Caglar Onurf05111f2008-04-28 02:12:38 -07001286 if (time_after(jiffies, zlc->last_full_zap + HZ)) {
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001287 bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
1288 zlc->last_full_zap = jiffies;
1289 }
1290
1291 allowednodes = !in_interrupt() && (alloc_flags & ALLOC_CPUSET) ?
1292 &cpuset_current_mems_allowed :
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07001293 &node_states[N_HIGH_MEMORY];
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001294 return allowednodes;
1295}
1296
1297/*
1298 * Given 'z' scanning a zonelist, run a couple of quick checks to see
1299 * if it is worth looking at further for free memory:
1300 * 1) Check that the zone isn't thought to be full (doesn't have its
1301 * bit set in the zonelist_cache fullzones BITMAP).
1302 * 2) Check that the zones node (obtained from the zonelist_cache
1303 * z_to_n[] mapping) is allowed in the passed in allowednodes mask.
1304 * Return true (non-zero) if zone is worth looking at further, or
1305 * else return false (zero) if it is not.
1306 *
1307 * This check -ignores- the distinction between various watermarks,
1308 * such as GFP_HIGH, GFP_ATOMIC, PF_MEMALLOC, ... If a zone is
1309 * found to be full for any variation of these watermarks, it will
1310 * be considered full for up to one second by all requests, unless
1311 * we are so low on memory on all allowed nodes that we are forced
1312 * into the second scan of the zonelist.
1313 *
1314 * In the second scan we ignore this zonelist cache and exactly
1315 * apply the watermarks to all zones, even it is slower to do so.
1316 * We are low on memory in the second scan, and should leave no stone
1317 * unturned looking for a free page.
1318 */
Mel Gormandd1a2392008-04-28 02:12:17 -07001319static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zoneref *z,
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001320 nodemask_t *allowednodes)
1321{
1322 struct zonelist_cache *zlc; /* cached zonelist speedup info */
1323 int i; /* index of *z in zonelist zones */
1324 int n; /* node that zone *z is on */
1325
1326 zlc = zonelist->zlcache_ptr;
1327 if (!zlc)
1328 return 1;
1329
Mel Gormandd1a2392008-04-28 02:12:17 -07001330 i = z - zonelist->_zonerefs;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001331 n = zlc->z_to_n[i];
1332
1333 /* This zone is worth trying if it is allowed but not full */
1334 return node_isset(n, *allowednodes) && !test_bit(i, zlc->fullzones);
1335}
1336
1337/*
1338 * Given 'z' scanning a zonelist, set the corresponding bit in
1339 * zlc->fullzones, so that subsequent attempts to allocate a page
1340 * from that zone don't waste time re-examining it.
1341 */
Mel Gormandd1a2392008-04-28 02:12:17 -07001342static void zlc_mark_zone_full(struct zonelist *zonelist, struct zoneref *z)
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001343{
1344 struct zonelist_cache *zlc; /* cached zonelist speedup info */
1345 int i; /* index of *z in zonelist zones */
1346
1347 zlc = zonelist->zlcache_ptr;
1348 if (!zlc)
1349 return;
1350
Mel Gormandd1a2392008-04-28 02:12:17 -07001351 i = z - zonelist->_zonerefs;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001352
1353 set_bit(i, zlc->fullzones);
1354}
1355
1356#else /* CONFIG_NUMA */
1357
1358static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
1359{
1360 return NULL;
1361}
1362
Mel Gormandd1a2392008-04-28 02:12:17 -07001363static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zoneref *z,
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001364 nodemask_t *allowednodes)
1365{
1366 return 1;
1367}
1368
Mel Gormandd1a2392008-04-28 02:12:17 -07001369static void zlc_mark_zone_full(struct zonelist *zonelist, struct zoneref *z)
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001370{
1371}
1372#endif /* CONFIG_NUMA */
1373
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001374/*
Paul Jackson0798e512006-12-06 20:31:38 -08001375 * get_page_from_freelist goes through the zonelist trying to allocate
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001376 * a page.
1377 */
1378static struct page *
Mel Gorman19770b32008-04-28 02:12:18 -07001379get_page_from_freelist(gfp_t gfp_mask, nodemask_t *nodemask, unsigned int order,
Mel Gorman54a6eb52008-04-28 02:12:16 -07001380 struct zonelist *zonelist, int high_zoneidx, int alloc_flags)
Martin Hicks753ee722005-06-21 17:14:41 -07001381{
Mel Gormandd1a2392008-04-28 02:12:17 -07001382 struct zoneref *z;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001383 struct page *page = NULL;
Mel Gorman54a6eb52008-04-28 02:12:16 -07001384 int classzone_idx;
Mel Gorman18ea7e72008-04-28 02:12:14 -07001385 struct zone *zone, *preferred_zone;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001386 nodemask_t *allowednodes = NULL;/* zonelist_cache approximation */
1387 int zlc_active = 0; /* set if using zonelist_cache */
1388 int did_zlc_setup = 0; /* just call zlc_setup() one time */
Mel Gorman54a6eb52008-04-28 02:12:16 -07001389
Mel Gorman19770b32008-04-28 02:12:18 -07001390 (void)first_zones_zonelist(zonelist, high_zoneidx, nodemask,
1391 &preferred_zone);
Andy Whitcroft7eb54822008-05-23 13:04:50 -07001392 if (!preferred_zone)
1393 return NULL;
1394
Mel Gorman19770b32008-04-28 02:12:18 -07001395 classzone_idx = zone_idx(preferred_zone);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001396
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001397zonelist_scan:
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001398 /*
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001399 * Scan zonelist, looking for a zone with enough free.
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001400 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
1401 */
Mel Gorman19770b32008-04-28 02:12:18 -07001402 for_each_zone_zonelist_nodemask(zone, z, zonelist,
1403 high_zoneidx, nodemask) {
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001404 if (NUMA_BUILD && zlc_active &&
1405 !zlc_zone_worth_trying(zonelist, z, allowednodes))
1406 continue;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001407 if ((alloc_flags & ALLOC_CPUSET) &&
Paul Jackson02a0e532006-12-13 00:34:25 -08001408 !cpuset_zone_allowed_softwall(zone, gfp_mask))
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001409 goto try_next_zone;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001410
1411 if (!(alloc_flags & ALLOC_NO_WATERMARKS)) {
Nick Piggin31488902005-11-28 13:44:03 -08001412 unsigned long mark;
1413 if (alloc_flags & ALLOC_WMARK_MIN)
Christoph Lameter1192d522006-09-25 23:31:45 -07001414 mark = zone->pages_min;
Nick Piggin31488902005-11-28 13:44:03 -08001415 else if (alloc_flags & ALLOC_WMARK_LOW)
Christoph Lameter1192d522006-09-25 23:31:45 -07001416 mark = zone->pages_low;
Nick Piggin31488902005-11-28 13:44:03 -08001417 else
Christoph Lameter1192d522006-09-25 23:31:45 -07001418 mark = zone->pages_high;
Paul Jackson0798e512006-12-06 20:31:38 -08001419 if (!zone_watermark_ok(zone, order, mark,
1420 classzone_idx, alloc_flags)) {
Christoph Lameter9eeff232006-01-18 17:42:31 -08001421 if (!zone_reclaim_mode ||
Christoph Lameter1192d522006-09-25 23:31:45 -07001422 !zone_reclaim(zone, gfp_mask, order))
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001423 goto this_zone_full;
Paul Jackson0798e512006-12-06 20:31:38 -08001424 }
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001425 }
1426
Mel Gorman18ea7e72008-04-28 02:12:14 -07001427 page = buffered_rmqueue(preferred_zone, zone, order, gfp_mask);
Paul Jackson0798e512006-12-06 20:31:38 -08001428 if (page)
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001429 break;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001430this_zone_full:
1431 if (NUMA_BUILD)
1432 zlc_mark_zone_full(zonelist, z);
1433try_next_zone:
1434 if (NUMA_BUILD && !did_zlc_setup) {
1435 /* we do zlc_setup after the first zone is tried */
1436 allowednodes = zlc_setup(zonelist, alloc_flags);
1437 zlc_active = 1;
1438 did_zlc_setup = 1;
1439 }
Mel Gorman54a6eb52008-04-28 02:12:16 -07001440 }
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001441
1442 if (unlikely(NUMA_BUILD && page == NULL && zlc_active)) {
1443 /* Disable zlc cache for second zonelist scan */
1444 zlc_active = 0;
1445 goto zonelist_scan;
1446 }
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001447 return page;
Martin Hicks753ee722005-06-21 17:14:41 -07001448}
1449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450/*
1451 * This is the 'heart' of the zoned buddy allocator.
1452 */
KOSAKI Motohiroe4048e52008-07-23 21:27:01 -07001453struct page *
Mel Gorman19770b32008-04-28 02:12:18 -07001454__alloc_pages_internal(gfp_t gfp_mask, unsigned int order,
1455 struct zonelist *zonelist, nodemask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
Al Viro260b2362005-10-21 03:22:44 -04001457 const gfp_t wait = gfp_mask & __GFP_WAIT;
Mel Gorman54a6eb52008-04-28 02:12:16 -07001458 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
Mel Gormandd1a2392008-04-28 02:12:17 -07001459 struct zoneref *z;
1460 struct zone *zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 struct page *page;
1462 struct reclaim_state reclaim_state;
1463 struct task_struct *p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 int do_retry;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001465 int alloc_flags;
Nishanth Aravamudana41f24e2008-04-29 00:58:25 -07001466 unsigned long did_some_progress;
1467 unsigned long pages_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469 might_sleep_if(wait);
1470
Akinobu Mita933e3122006-12-08 02:39:45 -08001471 if (should_fail_alloc_page(gfp_mask, order))
1472 return NULL;
1473
Jens Axboe6b1de912005-11-17 21:35:02 +01001474restart:
Mel Gormandd1a2392008-04-28 02:12:17 -07001475 z = zonelist->_zonerefs; /* the list of zones suitable for gfp_mask */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Mel Gormandd1a2392008-04-28 02:12:17 -07001477 if (unlikely(!z->zone)) {
Christoph Lameter523b9452007-10-16 01:25:37 -07001478 /*
1479 * Happens if we have an empty zonelist as a result of
1480 * GFP_THISNODE being used on a memoryless node
1481 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 return NULL;
1483 }
Jens Axboe6b1de912005-11-17 21:35:02 +01001484
Mel Gorman19770b32008-04-28 02:12:18 -07001485 page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, nodemask, order,
Mel Gorman54a6eb52008-04-28 02:12:16 -07001486 zonelist, high_zoneidx, ALLOC_WMARK_LOW|ALLOC_CPUSET);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001487 if (page)
1488 goto got_pg;
1489
Christoph Lameter952f3b52006-12-06 20:33:26 -08001490 /*
1491 * GFP_THISNODE (meaning __GFP_THISNODE, __GFP_NORETRY and
1492 * __GFP_NOWARN set) should not cause reclaim since the subsystem
1493 * (f.e. slab) using GFP_THISNODE may choose to trigger reclaim
1494 * using a larger set of nodes after it has established that the
1495 * allowed per node queues are empty and that nodes are
1496 * over allocated.
1497 */
1498 if (NUMA_BUILD && (gfp_mask & GFP_THISNODE) == GFP_THISNODE)
1499 goto nopage;
1500
Mel Gormandd1a2392008-04-28 02:12:17 -07001501 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx)
1502 wakeup_kswapd(zone, order);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001503
Paul Jackson9bf22292005-09-06 15:18:12 -07001504 /*
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001505 * OK, we're below the kswapd watermark and have kicked background
1506 * reclaim. Now things get more complex, so set up alloc_flags according
1507 * to how we want to proceed.
1508 *
1509 * The caller may dip into page reserves a bit more if the caller
1510 * cannot run direct reclaim, or if the caller has realtime scheduling
Paul Jackson4eac9152006-01-11 12:17:19 -08001511 * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will
1512 * set both ALLOC_HARDER (!wait) and ALLOC_HIGH (__GFP_HIGH).
Paul Jackson9bf22292005-09-06 15:18:12 -07001513 */
Nick Piggin31488902005-11-28 13:44:03 -08001514 alloc_flags = ALLOC_WMARK_MIN;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001515 if ((unlikely(rt_task(p)) && !in_interrupt()) || !wait)
1516 alloc_flags |= ALLOC_HARDER;
1517 if (gfp_mask & __GFP_HIGH)
1518 alloc_flags |= ALLOC_HIGH;
Paul Jacksonbdd804f2006-05-20 15:00:09 -07001519 if (wait)
1520 alloc_flags |= ALLOC_CPUSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 /*
1523 * Go through the zonelist again. Let __GFP_HIGH and allocations
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001524 * coming from realtime tasks go deeper into reserves.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 *
1526 * This is the last chance, in general, before the goto nopage.
1527 * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
Paul Jackson9bf22292005-09-06 15:18:12 -07001528 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 */
Mel Gorman19770b32008-04-28 02:12:18 -07001530 page = get_page_from_freelist(gfp_mask, nodemask, order, zonelist,
Mel Gorman54a6eb52008-04-28 02:12:16 -07001531 high_zoneidx, alloc_flags);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001532 if (page)
1533 goto got_pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 /* This allocation should allow future memory freeing. */
Nick Pigginb84a35b2005-05-01 08:58:36 -07001536
Kirill Korotaevb43a57b2006-12-06 20:32:27 -08001537rebalance:
Nick Pigginb84a35b2005-05-01 08:58:36 -07001538 if (((p->flags & PF_MEMALLOC) || unlikely(test_thread_flag(TIF_MEMDIE)))
1539 && !in_interrupt()) {
1540 if (!(gfp_mask & __GFP_NOMEMALLOC)) {
Kirill Korotaev885036d2005-11-13 16:06:41 -08001541nofail_alloc:
Nick Pigginb84a35b2005-05-01 08:58:36 -07001542 /* go through the zonelist yet again, ignoring mins */
Mel Gorman19770b32008-04-28 02:12:18 -07001543 page = get_page_from_freelist(gfp_mask, nodemask, order,
Mel Gorman54a6eb52008-04-28 02:12:16 -07001544 zonelist, high_zoneidx, ALLOC_NO_WATERMARKS);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001545 if (page)
1546 goto got_pg;
Kirill Korotaev885036d2005-11-13 16:06:41 -08001547 if (gfp_mask & __GFP_NOFAIL) {
Andrew Morton3fcfab12006-10-19 23:28:16 -07001548 congestion_wait(WRITE, HZ/50);
Kirill Korotaev885036d2005-11-13 16:06:41 -08001549 goto nofail_alloc;
1550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 }
1552 goto nopage;
1553 }
1554
1555 /* Atomic allocations - we can't balance anything */
1556 if (!wait)
1557 goto nopage;
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 cond_resched();
1560
1561 /* We now go into synchronous reclaim */
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001562 cpuset_memory_pressure_bump();
David Rientjese33c3b52008-11-12 13:25:37 -08001563 /*
1564 * The task's cpuset might have expanded its set of allowable nodes
1565 */
1566 cpuset_update_task_memory_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 p->flags |= PF_MEMALLOC;
1568 reclaim_state.reclaimed_slab = 0;
1569 p->reclaim_state = &reclaim_state;
1570
Mel Gormandac1d272008-04-28 02:12:12 -07001571 did_some_progress = try_to_free_pages(zonelist, order, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 p->reclaim_state = NULL;
1574 p->flags &= ~PF_MEMALLOC;
1575
1576 cond_resched();
1577
Mel Gormane2c55dc2007-10-16 01:25:50 -07001578 if (order != 0)
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001579 drain_all_pages();
Mel Gormane2c55dc2007-10-16 01:25:50 -07001580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 if (likely(did_some_progress)) {
Mel Gorman19770b32008-04-28 02:12:18 -07001582 page = get_page_from_freelist(gfp_mask, nodemask, order,
Mel Gorman54a6eb52008-04-28 02:12:16 -07001583 zonelist, high_zoneidx, alloc_flags);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001584 if (page)
1585 goto got_pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 } else if ((gfp_mask & __GFP_FS) && !(gfp_mask & __GFP_NORETRY)) {
Mel Gormandd1a2392008-04-28 02:12:17 -07001587 if (!try_set_zone_oom(zonelist, gfp_mask)) {
David Rientjesff0ceb92007-10-16 23:25:56 -07001588 schedule_timeout_uninterruptible(1);
1589 goto restart;
1590 }
1591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 /*
1593 * Go through the zonelist yet one more time, keep
1594 * very high watermark here, this is only to catch
1595 * a parallel oom killing, we must fail if we're still
1596 * under heavy pressure.
1597 */
Mel Gorman19770b32008-04-28 02:12:18 -07001598 page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, nodemask,
1599 order, zonelist, high_zoneidx,
1600 ALLOC_WMARK_HIGH|ALLOC_CPUSET);
David Rientjesff0ceb92007-10-16 23:25:56 -07001601 if (page) {
Mel Gormandd1a2392008-04-28 02:12:17 -07001602 clear_zonelist_oom(zonelist, gfp_mask);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001603 goto got_pg;
David Rientjesff0ceb92007-10-16 23:25:56 -07001604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Mel Gormana8bbf722007-07-31 00:37:30 -07001606 /* The OOM killer will not help higher order allocs so fail */
David Rientjesff0ceb92007-10-16 23:25:56 -07001607 if (order > PAGE_ALLOC_COSTLY_ORDER) {
Mel Gormandd1a2392008-04-28 02:12:17 -07001608 clear_zonelist_oom(zonelist, gfp_mask);
Mel Gormana8bbf722007-07-31 00:37:30 -07001609 goto nopage;
David Rientjesff0ceb92007-10-16 23:25:56 -07001610 }
Mel Gormana8bbf722007-07-31 00:37:30 -07001611
Christoph Lameter9b0f8b02006-02-20 18:27:52 -08001612 out_of_memory(zonelist, gfp_mask, order);
Mel Gormandd1a2392008-04-28 02:12:17 -07001613 clear_zonelist_oom(zonelist, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 goto restart;
1615 }
1616
1617 /*
1618 * Don't let big-order allocations loop unless the caller explicitly
1619 * requests that. Wait for some write requests to complete then retry.
1620 *
Nishanth Aravamudana41f24e2008-04-29 00:58:25 -07001621 * In this implementation, order <= PAGE_ALLOC_COSTLY_ORDER
1622 * means __GFP_NOFAIL, but that may not be true in other
Nishanth Aravamudanab857d02008-04-29 00:58:23 -07001623 * implementations.
Nishanth Aravamudana41f24e2008-04-29 00:58:25 -07001624 *
1625 * For order > PAGE_ALLOC_COSTLY_ORDER, if __GFP_REPEAT is
1626 * specified, then we retry until we no longer reclaim any pages
1627 * (above), or we've reclaimed an order of pages at least as
1628 * large as the allocation's order. In both cases, if the
1629 * allocation still fails, we stop retrying.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 */
Nishanth Aravamudana41f24e2008-04-29 00:58:25 -07001631 pages_reclaimed += did_some_progress;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 do_retry = 0;
1633 if (!(gfp_mask & __GFP_NORETRY)) {
Nishanth Aravamudana41f24e2008-04-29 00:58:25 -07001634 if (order <= PAGE_ALLOC_COSTLY_ORDER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 do_retry = 1;
Nishanth Aravamudana41f24e2008-04-29 00:58:25 -07001636 } else {
1637 if (gfp_mask & __GFP_REPEAT &&
1638 pages_reclaimed < (1 << order))
1639 do_retry = 1;
1640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 if (gfp_mask & __GFP_NOFAIL)
1642 do_retry = 1;
1643 }
1644 if (do_retry) {
Andrew Morton3fcfab12006-10-19 23:28:16 -07001645 congestion_wait(WRITE, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 goto rebalance;
1647 }
1648
1649nopage:
1650 if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) {
1651 printk(KERN_WARNING "%s: page allocation failure."
1652 " order:%d, mode:0x%x\n",
1653 p->comm, order, gfp_mask);
1654 dump_stack();
Janet Morgan578c2fd2005-06-21 17:14:56 -07001655 show_mem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657got_pg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 return page;
1659}
KOSAKI Motohiroe4048e52008-07-23 21:27:01 -07001660EXPORT_SYMBOL(__alloc_pages_internal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662/*
1663 * Common helper functions.
1664 */
Harvey Harrison920c7a52008-02-04 22:29:26 -08001665unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
1667 struct page * page;
1668 page = alloc_pages(gfp_mask, order);
1669 if (!page)
1670 return 0;
1671 return (unsigned long) page_address(page);
1672}
1673
1674EXPORT_SYMBOL(__get_free_pages);
1675
Harvey Harrison920c7a52008-02-04 22:29:26 -08001676unsigned long get_zeroed_page(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
1678 struct page * page;
1679
1680 /*
1681 * get_zeroed_page() returns a 32-bit address, which cannot represent
1682 * a highmem page
1683 */
Nick Piggin725d7042006-09-25 23:30:55 -07001684 VM_BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
1686 page = alloc_pages(gfp_mask | __GFP_ZERO, 0);
1687 if (page)
1688 return (unsigned long) page_address(page);
1689 return 0;
1690}
1691
1692EXPORT_SYMBOL(get_zeroed_page);
1693
1694void __pagevec_free(struct pagevec *pvec)
1695{
1696 int i = pagevec_count(pvec);
1697
1698 while (--i >= 0)
1699 free_hot_cold_page(pvec->pages[i], pvec->cold);
1700}
1701
Harvey Harrison920c7a52008-02-04 22:29:26 -08001702void __free_pages(struct page *page, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
Nick Pigginb5810032005-10-29 18:16:12 -07001704 if (put_page_testzero(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 if (order == 0)
1706 free_hot_page(page);
1707 else
1708 __free_pages_ok(page, order);
1709 }
1710}
1711
1712EXPORT_SYMBOL(__free_pages);
1713
Harvey Harrison920c7a52008-02-04 22:29:26 -08001714void free_pages(unsigned long addr, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715{
1716 if (addr != 0) {
Nick Piggin725d7042006-09-25 23:30:55 -07001717 VM_BUG_ON(!virt_addr_valid((void *)addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 __free_pages(virt_to_page((void *)addr), order);
1719 }
1720}
1721
1722EXPORT_SYMBOL(free_pages);
1723
Timur Tabi2be0ffe2008-07-23 21:28:11 -07001724/**
1725 * alloc_pages_exact - allocate an exact number physically-contiguous pages.
1726 * @size: the number of bytes to allocate
1727 * @gfp_mask: GFP flags for the allocation
1728 *
1729 * This function is similar to alloc_pages(), except that it allocates the
1730 * minimum number of pages to satisfy the request. alloc_pages() can only
1731 * allocate memory in power-of-two pages.
1732 *
1733 * This function is also limited by MAX_ORDER.
1734 *
1735 * Memory allocated by this function must be released by free_pages_exact().
1736 */
1737void *alloc_pages_exact(size_t size, gfp_t gfp_mask)
1738{
1739 unsigned int order = get_order(size);
1740 unsigned long addr;
1741
1742 addr = __get_free_pages(gfp_mask, order);
1743 if (addr) {
1744 unsigned long alloc_end = addr + (PAGE_SIZE << order);
1745 unsigned long used = addr + PAGE_ALIGN(size);
1746
1747 split_page(virt_to_page(addr), order);
1748 while (used < alloc_end) {
1749 free_page(used);
1750 used += PAGE_SIZE;
1751 }
1752 }
1753
1754 return (void *)addr;
1755}
1756EXPORT_SYMBOL(alloc_pages_exact);
1757
1758/**
1759 * free_pages_exact - release memory allocated via alloc_pages_exact()
1760 * @virt: the value returned by alloc_pages_exact.
1761 * @size: size of allocation, same value as passed to alloc_pages_exact().
1762 *
1763 * Release the memory allocated by a previous call to alloc_pages_exact.
1764 */
1765void free_pages_exact(void *virt, size_t size)
1766{
1767 unsigned long addr = (unsigned long)virt;
1768 unsigned long end = addr + PAGE_ALIGN(size);
1769
1770 while (addr < end) {
1771 free_page(addr);
1772 addr += PAGE_SIZE;
1773 }
1774}
1775EXPORT_SYMBOL(free_pages_exact);
1776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777static unsigned int nr_free_zone_pages(int offset)
1778{
Mel Gormandd1a2392008-04-28 02:12:17 -07001779 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07001780 struct zone *zone;
1781
Martin J. Blighe310fd42005-07-29 22:59:18 -07001782 /* Just pick one node, since fallback list is circular */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 unsigned int sum = 0;
1784
Mel Gorman0e884602008-04-28 02:12:14 -07001785 struct zonelist *zonelist = node_zonelist(numa_node_id(), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Mel Gorman54a6eb52008-04-28 02:12:16 -07001787 for_each_zone_zonelist(zone, z, zonelist, offset) {
Martin J. Blighe310fd42005-07-29 22:59:18 -07001788 unsigned long size = zone->present_pages;
1789 unsigned long high = zone->pages_high;
1790 if (size > high)
1791 sum += size - high;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 }
1793
1794 return sum;
1795}
1796
1797/*
1798 * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
1799 */
1800unsigned int nr_free_buffer_pages(void)
1801{
Al Viroaf4ca452005-10-21 02:55:38 -04001802 return nr_free_zone_pages(gfp_zone(GFP_USER));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803}
Meelap Shahc2f1a552007-07-17 04:04:39 -07001804EXPORT_SYMBOL_GPL(nr_free_buffer_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806/*
1807 * Amount of free RAM allocatable within all zones
1808 */
1809unsigned int nr_free_pagecache_pages(void)
1810{
Mel Gorman2a1e2742007-07-17 04:03:12 -07001811 return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER_MOVABLE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812}
Christoph Lameter08e0f6a2006-09-27 01:50:06 -07001813
1814static inline void show_node(struct zone *zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815{
Christoph Lameter08e0f6a2006-09-27 01:50:06 -07001816 if (NUMA_BUILD)
Andy Whitcroft25ba77c2006-12-06 20:33:03 -08001817 printk("Node %d ", zone_to_nid(zone));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820void si_meminfo(struct sysinfo *val)
1821{
1822 val->totalram = totalram_pages;
1823 val->sharedram = 0;
Christoph Lameterd23ad422007-02-10 01:43:02 -08001824 val->freeram = global_page_state(NR_FREE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 val->bufferram = nr_blockdev_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 val->totalhigh = totalhigh_pages;
1827 val->freehigh = nr_free_highpages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 val->mem_unit = PAGE_SIZE;
1829}
1830
1831EXPORT_SYMBOL(si_meminfo);
1832
1833#ifdef CONFIG_NUMA
1834void si_meminfo_node(struct sysinfo *val, int nid)
1835{
1836 pg_data_t *pgdat = NODE_DATA(nid);
1837
1838 val->totalram = pgdat->node_present_pages;
Christoph Lameterd23ad422007-02-10 01:43:02 -08001839 val->freeram = node_page_state(nid, NR_FREE_PAGES);
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07001840#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].present_pages;
Christoph Lameterd23ad422007-02-10 01:43:02 -08001842 val->freehigh = zone_page_state(&pgdat->node_zones[ZONE_HIGHMEM],
1843 NR_FREE_PAGES);
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07001844#else
1845 val->totalhigh = 0;
1846 val->freehigh = 0;
1847#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 val->mem_unit = PAGE_SIZE;
1849}
1850#endif
1851
1852#define K(x) ((x) << (PAGE_SHIFT-10))
1853
1854/*
1855 * Show free area list (used inside shift_scroll-lock stuff)
1856 * We also calculate the percentage fragmentation. We do this by counting the
1857 * memory on each free list with the exception of the first item on the list.
1858 */
1859void show_free_areas(void)
1860{
Jes Sorensenc7241912006-09-27 01:50:05 -07001861 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 struct zone *zone;
1863
1864 for_each_zone(zone) {
Jes Sorensenc7241912006-09-27 01:50:05 -07001865 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 continue;
Jes Sorensenc7241912006-09-27 01:50:05 -07001867
1868 show_node(zone);
1869 printk("%s per-cpu:\n", zone->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Dave Jones6b482c62005-11-10 15:45:56 -05001871 for_each_online_cpu(cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 struct per_cpu_pageset *pageset;
1873
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001874 pageset = zone_pcp(zone, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Christoph Lameter3dfa5722008-02-04 22:29:19 -08001876 printk("CPU %4d: hi:%5d, btch:%4d usd:%4d\n",
1877 cpu, pageset->pcp.high,
1878 pageset->pcp.batch, pageset->pcp.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 }
1880 }
1881
Lee Schermerhorn7b854122008-10-18 20:26:40 -07001882 printk("Active_anon:%lu active_file:%lu inactive_anon:%lu\n"
1883 " inactive_file:%lu"
1884//TODO: check/adjust line lengths
1885#ifdef CONFIG_UNEVICTABLE_LRU
1886 " unevictable:%lu"
1887#endif
1888 " dirty:%lu writeback:%lu unstable:%lu\n"
Christoph Lameterd23ad422007-02-10 01:43:02 -08001889 " free:%lu slab:%lu mapped:%lu pagetables:%lu bounce:%lu\n",
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001890 global_page_state(NR_ACTIVE_ANON),
1891 global_page_state(NR_ACTIVE_FILE),
1892 global_page_state(NR_INACTIVE_ANON),
1893 global_page_state(NR_INACTIVE_FILE),
Lee Schermerhorn7b854122008-10-18 20:26:40 -07001894#ifdef CONFIG_UNEVICTABLE_LRU
1895 global_page_state(NR_UNEVICTABLE),
1896#endif
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -07001897 global_page_state(NR_FILE_DIRTY),
Christoph Lameterce866b32006-06-30 01:55:40 -07001898 global_page_state(NR_WRITEBACK),
Christoph Lameterfd39fc82006-06-30 01:55:40 -07001899 global_page_state(NR_UNSTABLE_NFS),
Christoph Lameterd23ad422007-02-10 01:43:02 -08001900 global_page_state(NR_FREE_PAGES),
Christoph Lameter972d1a72006-09-25 23:31:51 -07001901 global_page_state(NR_SLAB_RECLAIMABLE) +
1902 global_page_state(NR_SLAB_UNRECLAIMABLE),
Christoph Lameter65ba55f2006-06-30 01:55:34 -07001903 global_page_state(NR_FILE_MAPPED),
Andrew Mortona25700a2007-02-08 14:20:40 -08001904 global_page_state(NR_PAGETABLE),
1905 global_page_state(NR_BOUNCE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
1907 for_each_zone(zone) {
1908 int i;
1909
Jes Sorensenc7241912006-09-27 01:50:05 -07001910 if (!populated_zone(zone))
1911 continue;
1912
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 show_node(zone);
1914 printk("%s"
1915 " free:%lukB"
1916 " min:%lukB"
1917 " low:%lukB"
1918 " high:%lukB"
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001919 " active_anon:%lukB"
1920 " inactive_anon:%lukB"
1921 " active_file:%lukB"
1922 " inactive_file:%lukB"
Lee Schermerhorn7b854122008-10-18 20:26:40 -07001923#ifdef CONFIG_UNEVICTABLE_LRU
1924 " unevictable:%lukB"
1925#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 " present:%lukB"
1927 " pages_scanned:%lu"
1928 " all_unreclaimable? %s"
1929 "\n",
1930 zone->name,
Christoph Lameterd23ad422007-02-10 01:43:02 -08001931 K(zone_page_state(zone, NR_FREE_PAGES)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 K(zone->pages_min),
1933 K(zone->pages_low),
1934 K(zone->pages_high),
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001935 K(zone_page_state(zone, NR_ACTIVE_ANON)),
1936 K(zone_page_state(zone, NR_INACTIVE_ANON)),
1937 K(zone_page_state(zone, NR_ACTIVE_FILE)),
1938 K(zone_page_state(zone, NR_INACTIVE_FILE)),
Lee Schermerhorn7b854122008-10-18 20:26:40 -07001939#ifdef CONFIG_UNEVICTABLE_LRU
1940 K(zone_page_state(zone, NR_UNEVICTABLE)),
1941#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 K(zone->present_pages),
1943 zone->pages_scanned,
David Rientjese815af92007-10-16 23:25:54 -07001944 (zone_is_all_unreclaimable(zone) ? "yes" : "no")
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 );
1946 printk("lowmem_reserve[]:");
1947 for (i = 0; i < MAX_NR_ZONES; i++)
1948 printk(" %lu", zone->lowmem_reserve[i]);
1949 printk("\n");
1950 }
1951
1952 for_each_zone(zone) {
Kirill Korotaev8f9de512006-06-23 02:03:50 -07001953 unsigned long nr[MAX_ORDER], flags, order, total = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Jes Sorensenc7241912006-09-27 01:50:05 -07001955 if (!populated_zone(zone))
1956 continue;
1957
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 show_node(zone);
1959 printk("%s: ", zone->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961 spin_lock_irqsave(&zone->lock, flags);
1962 for (order = 0; order < MAX_ORDER; order++) {
Kirill Korotaev8f9de512006-06-23 02:03:50 -07001963 nr[order] = zone->free_area[order].nr_free;
1964 total += nr[order] << order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 }
1966 spin_unlock_irqrestore(&zone->lock, flags);
Kirill Korotaev8f9de512006-06-23 02:03:50 -07001967 for (order = 0; order < MAX_ORDER; order++)
1968 printk("%lu*%lukB ", nr[order], K(1UL) << order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 printk("= %lukB\n", K(total));
1970 }
1971
Larry Woodmane6f36022008-02-04 22:29:30 -08001972 printk("%ld total pagecache pages\n", global_page_state(NR_FILE_PAGES));
1973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 show_swap_cache_info();
1975}
1976
Mel Gorman19770b32008-04-28 02:12:18 -07001977static void zoneref_set_zone(struct zone *zone, struct zoneref *zoneref)
1978{
1979 zoneref->zone = zone;
1980 zoneref->zone_idx = zone_idx(zone);
1981}
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983/*
1984 * Builds allocation fallback zone lists.
Christoph Lameter1a932052006-01-06 00:11:16 -08001985 *
1986 * Add all populated zones of a node to the zonelist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07001988static int build_zonelists_node(pg_data_t *pgdat, struct zonelist *zonelist,
1989 int nr_zones, enum zone_type zone_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
Christoph Lameter1a932052006-01-06 00:11:16 -08001991 struct zone *zone;
1992
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07001993 BUG_ON(zone_type >= MAX_NR_ZONES);
Christoph Lameter2f6726e2006-09-25 23:31:18 -07001994 zone_type++;
Christoph Lameter02a68a52006-01-06 00:11:18 -08001995
1996 do {
Christoph Lameter2f6726e2006-09-25 23:31:18 -07001997 zone_type--;
Christoph Lameter070f8032006-01-06 00:11:19 -08001998 zone = pgdat->node_zones + zone_type;
Christoph Lameter1a932052006-01-06 00:11:16 -08001999 if (populated_zone(zone)) {
Mel Gormandd1a2392008-04-28 02:12:17 -07002000 zoneref_set_zone(zone,
2001 &zonelist->_zonerefs[nr_zones++]);
Christoph Lameter070f8032006-01-06 00:11:19 -08002002 check_highest_zone(zone_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 }
Christoph Lameter02a68a52006-01-06 00:11:18 -08002004
Christoph Lameter2f6726e2006-09-25 23:31:18 -07002005 } while (zone_type);
Christoph Lameter070f8032006-01-06 00:11:19 -08002006 return nr_zones;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007}
2008
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002009
2010/*
2011 * zonelist_order:
2012 * 0 = automatic detection of better ordering.
2013 * 1 = order by ([node] distance, -zonetype)
2014 * 2 = order by (-zonetype, [node] distance)
2015 *
2016 * If not NUMA, ZONELIST_ORDER_ZONE and ZONELIST_ORDER_NODE will create
2017 * the same zonelist. So only NUMA can configure this param.
2018 */
2019#define ZONELIST_ORDER_DEFAULT 0
2020#define ZONELIST_ORDER_NODE 1
2021#define ZONELIST_ORDER_ZONE 2
2022
2023/* zonelist order in the kernel.
2024 * set_zonelist_order() will set this to NODE or ZONE.
2025 */
2026static int current_zonelist_order = ZONELIST_ORDER_DEFAULT;
2027static char zonelist_order_name[3][8] = {"Default", "Node", "Zone"};
2028
2029
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030#ifdef CONFIG_NUMA
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002031/* The value user specified ....changed by config */
2032static int user_zonelist_order = ZONELIST_ORDER_DEFAULT;
2033/* string for sysctl */
2034#define NUMA_ZONELIST_ORDER_LEN 16
2035char numa_zonelist_order[16] = "default";
2036
2037/*
2038 * interface for configure zonelist ordering.
2039 * command line option "numa_zonelist_order"
2040 * = "[dD]efault - default, automatic configuration.
2041 * = "[nN]ode - order by node locality, then by zone within node
2042 * = "[zZ]one - order by zone, then by locality within zone
2043 */
2044
2045static int __parse_numa_zonelist_order(char *s)
2046{
2047 if (*s == 'd' || *s == 'D') {
2048 user_zonelist_order = ZONELIST_ORDER_DEFAULT;
2049 } else if (*s == 'n' || *s == 'N') {
2050 user_zonelist_order = ZONELIST_ORDER_NODE;
2051 } else if (*s == 'z' || *s == 'Z') {
2052 user_zonelist_order = ZONELIST_ORDER_ZONE;
2053 } else {
2054 printk(KERN_WARNING
2055 "Ignoring invalid numa_zonelist_order value: "
2056 "%s\n", s);
2057 return -EINVAL;
2058 }
2059 return 0;
2060}
2061
2062static __init int setup_numa_zonelist_order(char *s)
2063{
2064 if (s)
2065 return __parse_numa_zonelist_order(s);
2066 return 0;
2067}
2068early_param("numa_zonelist_order", setup_numa_zonelist_order);
2069
2070/*
2071 * sysctl handler for numa_zonelist_order
2072 */
2073int numa_zonelist_order_handler(ctl_table *table, int write,
2074 struct file *file, void __user *buffer, size_t *length,
2075 loff_t *ppos)
2076{
2077 char saved_string[NUMA_ZONELIST_ORDER_LEN];
2078 int ret;
2079
2080 if (write)
2081 strncpy(saved_string, (char*)table->data,
2082 NUMA_ZONELIST_ORDER_LEN);
2083 ret = proc_dostring(table, write, file, buffer, length, ppos);
2084 if (ret)
2085 return ret;
2086 if (write) {
2087 int oldval = user_zonelist_order;
2088 if (__parse_numa_zonelist_order((char*)table->data)) {
2089 /*
2090 * bogus value. restore saved string
2091 */
2092 strncpy((char*)table->data, saved_string,
2093 NUMA_ZONELIST_ORDER_LEN);
2094 user_zonelist_order = oldval;
2095 } else if (oldval != user_zonelist_order)
2096 build_all_zonelists();
2097 }
2098 return 0;
2099}
2100
2101
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102#define MAX_NODE_LOAD (num_online_nodes())
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002103static int node_load[MAX_NUMNODES];
2104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105/**
Pavel Pisa4dc3b162005-05-01 08:59:25 -07002106 * 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 -07002107 * @node: node whose fallback list we're appending
2108 * @used_node_mask: nodemask_t of already used nodes
2109 *
2110 * We use a number of factors to determine which is the next node that should
2111 * appear on a given node's fallback list. The node should not have appeared
2112 * already in @node's fallback list, and it should be the next closest node
2113 * according to the distance array (which contains arbitrary distance values
2114 * from each node to each node in the system), and should also prefer nodes
2115 * with no CPUs, since presumably they'll have very little allocation pressure
2116 * on them otherwise.
2117 * It returns -1 if no node is found.
2118 */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002119static int find_next_best_node(int node, nodemask_t *used_node_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
Linus Torvalds4cf808e2006-02-17 20:38:21 +01002121 int n, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 int min_val = INT_MAX;
2123 int best_node = -1;
Mike Travisc5f59f02008-04-04 18:11:10 -07002124 node_to_cpumask_ptr(tmp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Linus Torvalds4cf808e2006-02-17 20:38:21 +01002126 /* Use the local node if we haven't already */
2127 if (!node_isset(node, *used_node_mask)) {
2128 node_set(node, *used_node_mask);
2129 return node;
2130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07002132 for_each_node_state(n, N_HIGH_MEMORY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134 /* Don't want a node to appear more than once */
2135 if (node_isset(n, *used_node_mask))
2136 continue;
2137
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 /* Use the distance array to find the distance */
2139 val = node_distance(node, n);
2140
Linus Torvalds4cf808e2006-02-17 20:38:21 +01002141 /* Penalize nodes under us ("prefer the next node") */
2142 val += (n < node);
2143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 /* Give preference to headless and unused nodes */
Mike Travisc5f59f02008-04-04 18:11:10 -07002145 node_to_cpumask_ptr_next(tmp, n);
2146 if (!cpus_empty(*tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 val += PENALTY_FOR_NODE_WITH_CPUS;
2148
2149 /* Slight preference for less loaded node */
2150 val *= (MAX_NODE_LOAD*MAX_NUMNODES);
2151 val += node_load[n];
2152
2153 if (val < min_val) {
2154 min_val = val;
2155 best_node = n;
2156 }
2157 }
2158
2159 if (best_node >= 0)
2160 node_set(best_node, *used_node_mask);
2161
2162 return best_node;
2163}
2164
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002165
2166/*
2167 * Build zonelists ordered by node and zones within node.
2168 * This results in maximum locality--normal zone overflows into local
2169 * DMA zone, if any--but risks exhausting DMA zone.
2170 */
2171static void build_zonelists_in_node_order(pg_data_t *pgdat, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172{
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002173 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 struct zonelist *zonelist;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002175
Mel Gorman54a6eb52008-04-28 02:12:16 -07002176 zonelist = &pgdat->node_zonelists[0];
Mel Gormandd1a2392008-04-28 02:12:17 -07002177 for (j = 0; zonelist->_zonerefs[j].zone != NULL; j++)
Mel Gorman54a6eb52008-04-28 02:12:16 -07002178 ;
2179 j = build_zonelists_node(NODE_DATA(node), zonelist, j,
2180 MAX_NR_ZONES - 1);
Mel Gormandd1a2392008-04-28 02:12:17 -07002181 zonelist->_zonerefs[j].zone = NULL;
2182 zonelist->_zonerefs[j].zone_idx = 0;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002183}
2184
2185/*
Christoph Lameter523b9452007-10-16 01:25:37 -07002186 * Build gfp_thisnode zonelists
2187 */
2188static void build_thisnode_zonelists(pg_data_t *pgdat)
2189{
Christoph Lameter523b9452007-10-16 01:25:37 -07002190 int j;
2191 struct zonelist *zonelist;
2192
Mel Gorman54a6eb52008-04-28 02:12:16 -07002193 zonelist = &pgdat->node_zonelists[1];
2194 j = build_zonelists_node(pgdat, zonelist, 0, MAX_NR_ZONES - 1);
Mel Gormandd1a2392008-04-28 02:12:17 -07002195 zonelist->_zonerefs[j].zone = NULL;
2196 zonelist->_zonerefs[j].zone_idx = 0;
Christoph Lameter523b9452007-10-16 01:25:37 -07002197}
2198
2199/*
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002200 * Build zonelists ordered by zone and nodes within zones.
2201 * This results in conserving DMA zone[s] until all Normal memory is
2202 * exhausted, but results in overflowing to remote node while memory
2203 * may still exist in local DMA zone.
2204 */
2205static int node_order[MAX_NUMNODES];
2206
2207static void build_zonelists_in_zone_order(pg_data_t *pgdat, int nr_nodes)
2208{
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002209 int pos, j, node;
2210 int zone_type; /* needs to be signed */
2211 struct zone *z;
2212 struct zonelist *zonelist;
2213
Mel Gorman54a6eb52008-04-28 02:12:16 -07002214 zonelist = &pgdat->node_zonelists[0];
2215 pos = 0;
2216 for (zone_type = MAX_NR_ZONES - 1; zone_type >= 0; zone_type--) {
2217 for (j = 0; j < nr_nodes; j++) {
2218 node = node_order[j];
2219 z = &NODE_DATA(node)->node_zones[zone_type];
2220 if (populated_zone(z)) {
Mel Gormandd1a2392008-04-28 02:12:17 -07002221 zoneref_set_zone(z,
2222 &zonelist->_zonerefs[pos++]);
Mel Gorman54a6eb52008-04-28 02:12:16 -07002223 check_highest_zone(zone_type);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002224 }
2225 }
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002226 }
Mel Gormandd1a2392008-04-28 02:12:17 -07002227 zonelist->_zonerefs[pos].zone = NULL;
2228 zonelist->_zonerefs[pos].zone_idx = 0;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002229}
2230
2231static int default_zonelist_order(void)
2232{
2233 int nid, zone_type;
2234 unsigned long low_kmem_size,total_size;
2235 struct zone *z;
2236 int average_size;
2237 /*
2238 * ZONE_DMA and ZONE_DMA32 can be very small area in the sytem.
2239 * If they are really small and used heavily, the system can fall
2240 * into OOM very easily.
2241 * This function detect ZONE_DMA/DMA32 size and confgigures zone order.
2242 */
2243 /* Is there ZONE_NORMAL ? (ex. ppc has only DMA zone..) */
2244 low_kmem_size = 0;
2245 total_size = 0;
2246 for_each_online_node(nid) {
2247 for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
2248 z = &NODE_DATA(nid)->node_zones[zone_type];
2249 if (populated_zone(z)) {
2250 if (zone_type < ZONE_NORMAL)
2251 low_kmem_size += z->present_pages;
2252 total_size += z->present_pages;
2253 }
2254 }
2255 }
2256 if (!low_kmem_size || /* there are no DMA area. */
2257 low_kmem_size > total_size/2) /* DMA/DMA32 is big. */
2258 return ZONELIST_ORDER_NODE;
2259 /*
2260 * look into each node's config.
2261 * If there is a node whose DMA/DMA32 memory is very big area on
2262 * local memory, NODE_ORDER may be suitable.
2263 */
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07002264 average_size = total_size /
2265 (nodes_weight(node_states[N_HIGH_MEMORY]) + 1);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002266 for_each_online_node(nid) {
2267 low_kmem_size = 0;
2268 total_size = 0;
2269 for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
2270 z = &NODE_DATA(nid)->node_zones[zone_type];
2271 if (populated_zone(z)) {
2272 if (zone_type < ZONE_NORMAL)
2273 low_kmem_size += z->present_pages;
2274 total_size += z->present_pages;
2275 }
2276 }
2277 if (low_kmem_size &&
2278 total_size > average_size && /* ignore small node */
2279 low_kmem_size > total_size * 70/100)
2280 return ZONELIST_ORDER_NODE;
2281 }
2282 return ZONELIST_ORDER_ZONE;
2283}
2284
2285static void set_zonelist_order(void)
2286{
2287 if (user_zonelist_order == ZONELIST_ORDER_DEFAULT)
2288 current_zonelist_order = default_zonelist_order();
2289 else
2290 current_zonelist_order = user_zonelist_order;
2291}
2292
2293static void build_zonelists(pg_data_t *pgdat)
2294{
2295 int j, node, load;
2296 enum zone_type i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 nodemask_t used_mask;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002298 int local_node, prev_node;
2299 struct zonelist *zonelist;
2300 int order = current_zonelist_order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
2302 /* initialize zonelists */
Christoph Lameter523b9452007-10-16 01:25:37 -07002303 for (i = 0; i < MAX_ZONELISTS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 zonelist = pgdat->node_zonelists + i;
Mel Gormandd1a2392008-04-28 02:12:17 -07002305 zonelist->_zonerefs[0].zone = NULL;
2306 zonelist->_zonerefs[0].zone_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 }
2308
2309 /* NUMA-aware ordering of nodes */
2310 local_node = pgdat->node_id;
2311 load = num_online_nodes();
2312 prev_node = local_node;
2313 nodes_clear(used_mask);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002314
2315 memset(node_load, 0, sizeof(node_load));
2316 memset(node_order, 0, sizeof(node_order));
2317 j = 0;
2318
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
Christoph Lameter9eeff232006-01-18 17:42:31 -08002320 int distance = node_distance(local_node, node);
2321
2322 /*
2323 * If another node is sufficiently far away then it is better
2324 * to reclaim pages in a zone before going off node.
2325 */
2326 if (distance > RECLAIM_DISTANCE)
2327 zone_reclaim_mode = 1;
2328
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 /*
2330 * We don't want to pressure a particular node.
2331 * So adding penalty to the first node in same
2332 * distance group to make it round-robin.
2333 */
Christoph Lameter9eeff232006-01-18 17:42:31 -08002334 if (distance != node_distance(local_node, prev_node))
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002335 node_load[node] = load;
2336
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 prev_node = node;
2338 load--;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002339 if (order == ZONELIST_ORDER_NODE)
2340 build_zonelists_in_node_order(pgdat, node);
2341 else
2342 node_order[j++] = node; /* remember order */
2343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002345 if (order == ZONELIST_ORDER_ZONE) {
2346 /* calculate node order -- i.e., DMA last! */
2347 build_zonelists_in_zone_order(pgdat, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 }
Christoph Lameter523b9452007-10-16 01:25:37 -07002349
2350 build_thisnode_zonelists(pgdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351}
2352
Paul Jackson9276b1bc2006-12-06 20:31:48 -08002353/* Construct the zonelist performance cache - see further mmzone.h */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002354static void build_zonelist_cache(pg_data_t *pgdat)
Paul Jackson9276b1bc2006-12-06 20:31:48 -08002355{
Mel Gorman54a6eb52008-04-28 02:12:16 -07002356 struct zonelist *zonelist;
2357 struct zonelist_cache *zlc;
Mel Gormandd1a2392008-04-28 02:12:17 -07002358 struct zoneref *z;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08002359
Mel Gorman54a6eb52008-04-28 02:12:16 -07002360 zonelist = &pgdat->node_zonelists[0];
2361 zonelist->zlcache_ptr = zlc = &zonelist->zlcache;
2362 bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
Mel Gormandd1a2392008-04-28 02:12:17 -07002363 for (z = zonelist->_zonerefs; z->zone; z++)
2364 zlc->z_to_n[z - zonelist->_zonerefs] = zonelist_node_idx(z);
Paul Jackson9276b1bc2006-12-06 20:31:48 -08002365}
2366
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002367
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368#else /* CONFIG_NUMA */
2369
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002370static void set_zonelist_order(void)
2371{
2372 current_zonelist_order = ZONELIST_ORDER_ZONE;
2373}
2374
2375static void build_zonelists(pg_data_t *pgdat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376{
Christoph Lameter19655d32006-09-25 23:31:19 -07002377 int node, local_node;
Mel Gorman54a6eb52008-04-28 02:12:16 -07002378 enum zone_type j;
2379 struct zonelist *zonelist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
2381 local_node = pgdat->node_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Mel Gorman54a6eb52008-04-28 02:12:16 -07002383 zonelist = &pgdat->node_zonelists[0];
2384 j = build_zonelists_node(pgdat, zonelist, 0, MAX_NR_ZONES - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Mel Gorman54a6eb52008-04-28 02:12:16 -07002386 /*
2387 * Now we build the zonelist so that it contains the zones
2388 * of all the other nodes.
2389 * We don't want to pressure a particular node, so when
2390 * building the zones for node N, we make sure that the
2391 * zones coming right after the local ones are those from
2392 * node N+1 (modulo N)
2393 */
2394 for (node = local_node + 1; node < MAX_NUMNODES; node++) {
2395 if (!node_online(node))
2396 continue;
2397 j = build_zonelists_node(NODE_DATA(node), zonelist, j,
2398 MAX_NR_ZONES - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 }
Mel Gorman54a6eb52008-04-28 02:12:16 -07002400 for (node = 0; node < local_node; node++) {
2401 if (!node_online(node))
2402 continue;
2403 j = build_zonelists_node(NODE_DATA(node), zonelist, j,
2404 MAX_NR_ZONES - 1);
2405 }
2406
Mel Gormandd1a2392008-04-28 02:12:17 -07002407 zonelist->_zonerefs[j].zone = NULL;
2408 zonelist->_zonerefs[j].zone_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409}
2410
Paul Jackson9276b1bc2006-12-06 20:31:48 -08002411/* non-NUMA variant of zonelist performance cache - just NULL zlcache_ptr */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002412static void build_zonelist_cache(pg_data_t *pgdat)
Paul Jackson9276b1bc2006-12-06 20:31:48 -08002413{
Mel Gorman54a6eb52008-04-28 02:12:16 -07002414 pgdat->node_zonelists[0].zlcache_ptr = NULL;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08002415}
2416
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417#endif /* CONFIG_NUMA */
2418
Rusty Russell9b1a4d32008-07-28 12:16:30 -05002419/* return values int ....just for stop_machine() */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002420static int __build_all_zonelists(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421{
Yasunori Goto68113782006-06-23 02:03:11 -07002422 int nid;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08002423
2424 for_each_online_node(nid) {
Christoph Lameter7ea15302007-10-16 01:25:29 -07002425 pg_data_t *pgdat = NODE_DATA(nid);
2426
2427 build_zonelists(pgdat);
2428 build_zonelist_cache(pgdat);
Paul Jackson9276b1bc2006-12-06 20:31:48 -08002429 }
Yasunori Goto68113782006-06-23 02:03:11 -07002430 return 0;
2431}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002433void build_all_zonelists(void)
Yasunori Goto68113782006-06-23 02:03:11 -07002434{
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002435 set_zonelist_order();
2436
Yasunori Goto68113782006-06-23 02:03:11 -07002437 if (system_state == SYSTEM_BOOTING) {
Randy Dunlap423b41d2006-09-27 01:50:12 -07002438 __build_all_zonelists(NULL);
Mel Gorman68ad8df2008-07-23 21:26:52 -07002439 mminit_verify_zonelist();
Yasunori Goto68113782006-06-23 02:03:11 -07002440 cpuset_init_current_mems_allowed();
2441 } else {
Simon Arlott183ff222007-10-20 01:27:18 +02002442 /* we have to stop all cpus to guarantee there is no user
Yasunori Goto68113782006-06-23 02:03:11 -07002443 of zonelist */
Rusty Russell9b1a4d32008-07-28 12:16:30 -05002444 stop_machine(__build_all_zonelists, NULL, NULL);
Yasunori Goto68113782006-06-23 02:03:11 -07002445 /* cpuset refresh routine should be here */
2446 }
Andrew Mortonbd1e22b2006-06-23 02:03:47 -07002447 vm_total_pages = nr_free_pagecache_pages();
Mel Gorman9ef9acb2007-10-16 01:25:54 -07002448 /*
2449 * Disable grouping by mobility if the number of pages in the
2450 * system is too low to allow the mechanism to work. It would be
2451 * more accurate, but expensive to check per-zone. This check is
2452 * made on memory-hotadd so a system can start with mobility
2453 * disabled and enable it later
2454 */
Mel Gormand9c23402007-10-16 01:26:01 -07002455 if (vm_total_pages < (pageblock_nr_pages * MIGRATE_TYPES))
Mel Gorman9ef9acb2007-10-16 01:25:54 -07002456 page_group_by_mobility_disabled = 1;
2457 else
2458 page_group_by_mobility_disabled = 0;
2459
2460 printk("Built %i zonelists in %s order, mobility grouping %s. "
2461 "Total pages: %ld\n",
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002462 num_online_nodes(),
2463 zonelist_order_name[current_zonelist_order],
Mel Gorman9ef9acb2007-10-16 01:25:54 -07002464 page_group_by_mobility_disabled ? "off" : "on",
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002465 vm_total_pages);
2466#ifdef CONFIG_NUMA
2467 printk("Policy zone: %s\n", zone_names[policy_zone]);
2468#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469}
2470
2471/*
2472 * Helper functions to size the waitqueue hash table.
2473 * Essentially these want to choose hash table sizes sufficiently
2474 * large so that collisions trying to wait on pages are rare.
2475 * But in fact, the number of active page waitqueues on typical
2476 * systems is ridiculously low, less than 200. So this is even
2477 * conservative, even though it seems large.
2478 *
2479 * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
2480 * waitqueues, i.e. the size of the waitq table given the number of pages.
2481 */
2482#define PAGES_PER_WAITQUEUE 256
2483
Yasunori Gotocca448f2006-06-23 02:03:10 -07002484#ifndef CONFIG_MEMORY_HOTPLUG
Yasunori Goto02b694d2006-06-23 02:03:08 -07002485static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486{
2487 unsigned long size = 1;
2488
2489 pages /= PAGES_PER_WAITQUEUE;
2490
2491 while (size < pages)
2492 size <<= 1;
2493
2494 /*
2495 * Once we have dozens or even hundreds of threads sleeping
2496 * on IO we've got bigger problems than wait queue collision.
2497 * Limit the size of the wait table to a reasonable size.
2498 */
2499 size = min(size, 4096UL);
2500
2501 return max(size, 4UL);
2502}
Yasunori Gotocca448f2006-06-23 02:03:10 -07002503#else
2504/*
2505 * A zone's size might be changed by hot-add, so it is not possible to determine
2506 * a suitable size for its wait_table. So we use the maximum size now.
2507 *
2508 * The max wait table size = 4096 x sizeof(wait_queue_head_t). ie:
2509 *
2510 * i386 (preemption config) : 4096 x 16 = 64Kbyte.
2511 * ia64, x86-64 (no preemption): 4096 x 20 = 80Kbyte.
2512 * ia64, x86-64 (preemption) : 4096 x 24 = 96Kbyte.
2513 *
2514 * The maximum entries are prepared when a zone's memory is (512K + 256) pages
2515 * or more by the traditional way. (See above). It equals:
2516 *
2517 * i386, x86-64, powerpc(4K page size) : = ( 2G + 1M)byte.
2518 * ia64(16K page size) : = ( 8G + 4M)byte.
2519 * powerpc (64K page size) : = (32G +16M)byte.
2520 */
2521static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
2522{
2523 return 4096UL;
2524}
2525#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
2527/*
2528 * This is an integer logarithm so that shifts can be used later
2529 * to extract the more random high bits from the multiplicative
2530 * hash function before the remainder is taken.
2531 */
2532static inline unsigned long wait_table_bits(unsigned long size)
2533{
2534 return ffz(~size);
2535}
2536
2537#define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
2538
Mel Gorman56fd56b2007-10-16 01:25:58 -07002539/*
Mel Gormand9c23402007-10-16 01:26:01 -07002540 * Mark a number of pageblocks as MIGRATE_RESERVE. The number
Mel Gorman56fd56b2007-10-16 01:25:58 -07002541 * of blocks reserved is based on zone->pages_min. The memory within the
2542 * reserve will tend to store contiguous free pages. Setting min_free_kbytes
2543 * higher will lead to a bigger reserve which will get freed as contiguous
2544 * blocks as reclaim kicks in
2545 */
2546static void setup_zone_migrate_reserve(struct zone *zone)
2547{
2548 unsigned long start_pfn, pfn, end_pfn;
2549 struct page *page;
2550 unsigned long reserve, block_migratetype;
2551
2552 /* Get the start pfn, end pfn and the number of blocks to reserve */
2553 start_pfn = zone->zone_start_pfn;
2554 end_pfn = start_pfn + zone->spanned_pages;
Mel Gormand9c23402007-10-16 01:26:01 -07002555 reserve = roundup(zone->pages_min, pageblock_nr_pages) >>
2556 pageblock_order;
Mel Gorman56fd56b2007-10-16 01:25:58 -07002557
Mel Gormand9c23402007-10-16 01:26:01 -07002558 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
Mel Gorman56fd56b2007-10-16 01:25:58 -07002559 if (!pfn_valid(pfn))
2560 continue;
2561 page = pfn_to_page(pfn);
2562
Adam Litke344c7902008-09-02 14:35:38 -07002563 /* Watch out for overlapping nodes */
2564 if (page_to_nid(page) != zone_to_nid(zone))
2565 continue;
2566
Mel Gorman56fd56b2007-10-16 01:25:58 -07002567 /* Blocks with reserved pages will never free, skip them. */
2568 if (PageReserved(page))
2569 continue;
2570
2571 block_migratetype = get_pageblock_migratetype(page);
2572
2573 /* If this block is reserved, account for it */
2574 if (reserve > 0 && block_migratetype == MIGRATE_RESERVE) {
2575 reserve--;
2576 continue;
2577 }
2578
2579 /* Suitable for reserving if this block is movable */
2580 if (reserve > 0 && block_migratetype == MIGRATE_MOVABLE) {
2581 set_pageblock_migratetype(page, MIGRATE_RESERVE);
2582 move_freepages_block(zone, page, MIGRATE_RESERVE);
2583 reserve--;
2584 continue;
2585 }
2586
2587 /*
2588 * If the reserve is met and this is a previous reserved block,
2589 * take it back
2590 */
2591 if (block_migratetype == MIGRATE_RESERVE) {
2592 set_pageblock_migratetype(page, MIGRATE_MOVABLE);
2593 move_freepages_block(zone, page, MIGRATE_MOVABLE);
2594 }
2595 }
2596}
Mel Gormanac0e5b72007-10-16 01:25:58 -07002597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598/*
2599 * Initially all pages are reserved - free ones are freed
2600 * up by free_all_bootmem() once the early boot process is
2601 * done. Non-atomic initialization, single-pass.
2602 */
Matt Tolentinoc09b4242006-01-17 07:03:44 +01002603void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
Dave Hansena2f3aa022007-01-10 23:15:30 -08002604 unsigned long start_pfn, enum memmap_context context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 struct page *page;
Andy Whitcroft29751f62005-06-23 00:08:00 -07002607 unsigned long end_pfn = start_pfn + size;
2608 unsigned long pfn;
KAMEZAWA Hiroyuki86051ca2008-04-29 00:58:21 -07002609 struct zone *z;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
KAMEZAWA Hiroyuki86051ca2008-04-29 00:58:21 -07002611 z = &NODE_DATA(nid)->node_zones[zone];
Greg Ungerercbe8dd42006-01-12 01:05:24 -08002612 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
Dave Hansena2f3aa022007-01-10 23:15:30 -08002613 /*
2614 * There can be holes in boot-time mem_map[]s
2615 * handed to this function. They do not
2616 * exist on hotplugged memory.
2617 */
2618 if (context == MEMMAP_EARLY) {
2619 if (!early_pfn_valid(pfn))
2620 continue;
2621 if (!early_pfn_in_nid(pfn, nid))
2622 continue;
2623 }
Andy Whitcroftd41dee32005-06-23 00:07:54 -07002624 page = pfn_to_page(pfn);
2625 set_page_links(page, zone, nid, pfn);
Mel Gorman708614e2008-07-23 21:26:51 -07002626 mminit_verify_page_links(page, zone, nid, pfn);
Nick Piggin7835e982006-03-22 00:08:40 -08002627 init_page_count(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 reset_page_mapcount(page);
2629 SetPageReserved(page);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002630 /*
2631 * Mark the block movable so that blocks are reserved for
2632 * movable at startup. This will force kernel allocations
2633 * to reserve their blocks rather than leaking throughout
2634 * the address space during boot when many long-lived
Mel Gorman56fd56b2007-10-16 01:25:58 -07002635 * kernel allocations are made. Later some blocks near
2636 * the start are marked MIGRATE_RESERVE by
2637 * setup_zone_migrate_reserve()
KAMEZAWA Hiroyuki86051ca2008-04-29 00:58:21 -07002638 *
2639 * bitmap is created for zone's valid pfn range. but memmap
2640 * can be created for invalid pages (for alignment)
2641 * check here not to call set_pageblock_migratetype() against
2642 * pfn out of zone.
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002643 */
KAMEZAWA Hiroyuki86051ca2008-04-29 00:58:21 -07002644 if ((z->zone_start_pfn <= pfn)
2645 && (pfn < z->zone_start_pfn + z->spanned_pages)
2646 && !(pfn & (pageblock_nr_pages - 1)))
Mel Gorman56fd56b2007-10-16 01:25:58 -07002647 set_pageblock_migratetype(page, MIGRATE_MOVABLE);
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002648
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 INIT_LIST_HEAD(&page->lru);
2650#ifdef WANT_PAGE_VIRTUAL
2651 /* The shift won't overflow because ZONE_NORMAL is below 4G. */
2652 if (!is_highmem_idx(zone))
Bob Picco3212c6b2005-06-27 14:36:28 -07002653 set_page_address(page, __va(pfn << PAGE_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 }
2656}
2657
Andi Kleen1e548de2008-02-04 22:29:26 -08002658static void __meminit zone_init_free_lists(struct zone *zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659{
Mel Gormanb2a0ac82007-10-16 01:25:48 -07002660 int order, t;
2661 for_each_migratetype_order(order, t) {
2662 INIT_LIST_HEAD(&zone->free_area[order].free_list[t]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 zone->free_area[order].nr_free = 0;
2664 }
2665}
2666
2667#ifndef __HAVE_ARCH_MEMMAP_INIT
2668#define memmap_init(size, nid, zone, start_pfn) \
Dave Hansena2f3aa022007-01-10 23:15:30 -08002669 memmap_init_zone((size), (nid), (zone), (start_pfn), MEMMAP_EARLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670#endif
2671
Sam Ravnborg1d6f4e62008-01-17 15:21:12 -08002672static int zone_batchsize(struct zone *zone)
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002673{
2674 int batch;
2675
2676 /*
2677 * The per-cpu-pages pools are set to around 1000th of the
Seth, Rohitba56e912005-10-29 18:15:47 -07002678 * size of the zone. But no more than 1/2 of a meg.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002679 *
2680 * OK, so we don't know how big the cache is. So guess.
2681 */
2682 batch = zone->present_pages / 1024;
Seth, Rohitba56e912005-10-29 18:15:47 -07002683 if (batch * PAGE_SIZE > 512 * 1024)
2684 batch = (512 * 1024) / PAGE_SIZE;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002685 batch /= 4; /* We effectively *= 4 below */
2686 if (batch < 1)
2687 batch = 1;
2688
2689 /*
Nick Piggin0ceaacc2005-12-04 13:55:25 +11002690 * Clamp the batch to a 2^n - 1 value. Having a power
2691 * of 2 value was found to be more likely to have
2692 * suboptimal cache aliasing properties in some cases.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002693 *
Nick Piggin0ceaacc2005-12-04 13:55:25 +11002694 * For example if 2 tasks are alternately allocating
2695 * batches of pages, one task can end up with a lot
2696 * of pages of one half of the possible page colors
2697 * and the other with pages of the other colors.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002698 */
Nick Piggin0ceaacc2005-12-04 13:55:25 +11002699 batch = (1 << (fls(batch + batch/2)-1)) - 1;
Seth, Rohitba56e912005-10-29 18:15:47 -07002700
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002701 return batch;
2702}
2703
Adrian Bunkb69a7282008-07-23 21:28:12 -07002704static void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
Christoph Lameter2caaad42005-06-21 17:15:00 -07002705{
2706 struct per_cpu_pages *pcp;
2707
Magnus Damm1c6fe942005-10-26 01:58:59 -07002708 memset(p, 0, sizeof(*p));
2709
Christoph Lameter3dfa5722008-02-04 22:29:19 -08002710 pcp = &p->pcp;
Christoph Lameter2caaad42005-06-21 17:15:00 -07002711 pcp->count = 0;
Christoph Lameter2caaad42005-06-21 17:15:00 -07002712 pcp->high = 6 * batch;
2713 pcp->batch = max(1UL, 1 * batch);
2714 INIT_LIST_HEAD(&pcp->list);
Christoph Lameter2caaad42005-06-21 17:15:00 -07002715}
2716
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08002717/*
2718 * setup_pagelist_highmark() sets the high water mark for hot per_cpu_pagelist
2719 * to the value high for the pageset p.
2720 */
2721
2722static void setup_pagelist_highmark(struct per_cpu_pageset *p,
2723 unsigned long high)
2724{
2725 struct per_cpu_pages *pcp;
2726
Christoph Lameter3dfa5722008-02-04 22:29:19 -08002727 pcp = &p->pcp;
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08002728 pcp->high = high;
2729 pcp->batch = max(1UL, high/4);
2730 if ((high/4) > (PAGE_SHIFT * 8))
2731 pcp->batch = PAGE_SHIFT * 8;
2732}
2733
2734
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002735#ifdef CONFIG_NUMA
2736/*
Christoph Lameter2caaad42005-06-21 17:15:00 -07002737 * Boot pageset table. One per cpu which is going to be used for all
2738 * zones and all nodes. The parameters will be set in such a way
2739 * that an item put on a list will immediately be handed over to
2740 * the buddy list. This is safe since pageset manipulation is done
2741 * with interrupts disabled.
2742 *
2743 * Some NUMA counter updates may also be caught by the boot pagesets.
Christoph Lameterb7c84c62005-06-22 20:26:07 -07002744 *
2745 * The boot_pagesets must be kept even after bootup is complete for
2746 * unused processors and/or zones. They do play a role for bootstrapping
2747 * hotplugged processors.
2748 *
2749 * zoneinfo_show() and maybe other functions do
2750 * not check if the processor is online before following the pageset pointer.
2751 * Other parts of the kernel may not check if the zone is available.
Christoph Lameter2caaad42005-06-21 17:15:00 -07002752 */
Eric Dumazet88a2a4ac2006-02-04 23:27:36 -08002753static struct per_cpu_pageset boot_pageset[NR_CPUS];
Christoph Lameter2caaad42005-06-21 17:15:00 -07002754
2755/*
2756 * Dynamically allocate memory for the
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002757 * per cpu pageset array in struct zone.
2758 */
Ashok Raj6292d9a2006-02-01 03:04:44 -08002759static int __cpuinit process_zones(int cpu)
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002760{
2761 struct zone *zone, *dzone;
Christoph Lameter37c07082007-10-16 01:25:36 -07002762 int node = cpu_to_node(cpu);
2763
2764 node_set_state(node, N_CPU); /* this node has a cpu */
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002765
2766 for_each_zone(zone) {
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002767
Christoph Lameter66a55032006-09-27 01:50:09 -07002768 if (!populated_zone(zone))
2769 continue;
2770
Nick Piggin23316bc2006-01-08 01:00:41 -08002771 zone_pcp(zone, cpu) = kmalloc_node(sizeof(struct per_cpu_pageset),
Christoph Lameter37c07082007-10-16 01:25:36 -07002772 GFP_KERNEL, node);
Nick Piggin23316bc2006-01-08 01:00:41 -08002773 if (!zone_pcp(zone, cpu))
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002774 goto bad;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002775
Nick Piggin23316bc2006-01-08 01:00:41 -08002776 setup_pageset(zone_pcp(zone, cpu), zone_batchsize(zone));
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08002777
2778 if (percpu_pagelist_fraction)
2779 setup_pagelist_highmark(zone_pcp(zone, cpu),
2780 (zone->present_pages / percpu_pagelist_fraction));
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002781 }
2782
2783 return 0;
2784bad:
2785 for_each_zone(dzone) {
Andrew Morton64191682007-08-30 23:56:17 -07002786 if (!populated_zone(dzone))
2787 continue;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002788 if (dzone == zone)
2789 break;
Nick Piggin23316bc2006-01-08 01:00:41 -08002790 kfree(zone_pcp(dzone, cpu));
2791 zone_pcp(dzone, cpu) = NULL;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002792 }
2793 return -ENOMEM;
2794}
2795
2796static inline void free_zone_pagesets(int cpu)
2797{
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002798 struct zone *zone;
2799
2800 for_each_zone(zone) {
2801 struct per_cpu_pageset *pset = zone_pcp(zone, cpu);
2802
David Rientjesf3ef9ea2006-09-25 16:24:57 -07002803 /* Free per_cpu_pageset if it is slab allocated */
2804 if (pset != &boot_pageset[cpu])
2805 kfree(pset);
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002806 zone_pcp(zone, cpu) = NULL;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002807 }
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002808}
2809
Chandra Seetharaman9c7b2162006-06-27 02:54:07 -07002810static int __cpuinit pageset_cpuup_callback(struct notifier_block *nfb,
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002811 unsigned long action,
2812 void *hcpu)
2813{
2814 int cpu = (long)hcpu;
2815 int ret = NOTIFY_OK;
2816
2817 switch (action) {
Andy Whitcroftce421c72006-12-06 20:33:08 -08002818 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002819 case CPU_UP_PREPARE_FROZEN:
Andy Whitcroftce421c72006-12-06 20:33:08 -08002820 if (process_zones(cpu))
2821 ret = NOTIFY_BAD;
2822 break;
2823 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002824 case CPU_UP_CANCELED_FROZEN:
Andy Whitcroftce421c72006-12-06 20:33:08 -08002825 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002826 case CPU_DEAD_FROZEN:
Andy Whitcroftce421c72006-12-06 20:33:08 -08002827 free_zone_pagesets(cpu);
2828 break;
2829 default:
2830 break;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002831 }
2832 return ret;
2833}
2834
Chandra Seetharaman74b85f32006-06-27 02:54:09 -07002835static struct notifier_block __cpuinitdata pageset_notifier =
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002836 { &pageset_cpuup_callback, NULL, 0 };
2837
Al Viro78d99552005-12-15 09:18:25 +00002838void __init setup_per_cpu_pageset(void)
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002839{
2840 int err;
2841
2842 /* Initialize per_cpu_pageset for cpu 0.
2843 * A cpuup callback will do this for every cpu
2844 * as it comes online
2845 */
2846 err = process_zones(smp_processor_id());
2847 BUG_ON(err);
2848 register_cpu_notifier(&pageset_notifier);
2849}
2850
2851#endif
2852
Sam Ravnborg577a32f2007-05-17 23:29:25 +02002853static noinline __init_refok
Yasunori Gotocca448f2006-06-23 02:03:10 -07002854int zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages)
Dave Hansened8ece22005-10-29 18:16:50 -07002855{
2856 int i;
2857 struct pglist_data *pgdat = zone->zone_pgdat;
Yasunori Gotocca448f2006-06-23 02:03:10 -07002858 size_t alloc_size;
Dave Hansened8ece22005-10-29 18:16:50 -07002859
2860 /*
2861 * The per-page waitqueue mechanism uses hashed waitqueues
2862 * per zone.
2863 */
Yasunori Goto02b694d2006-06-23 02:03:08 -07002864 zone->wait_table_hash_nr_entries =
2865 wait_table_hash_nr_entries(zone_size_pages);
2866 zone->wait_table_bits =
2867 wait_table_bits(zone->wait_table_hash_nr_entries);
Yasunori Gotocca448f2006-06-23 02:03:10 -07002868 alloc_size = zone->wait_table_hash_nr_entries
2869 * sizeof(wait_queue_head_t);
2870
Heiko Carstenscd94b9d2008-05-23 13:04:52 -07002871 if (!slab_is_available()) {
Yasunori Gotocca448f2006-06-23 02:03:10 -07002872 zone->wait_table = (wait_queue_head_t *)
2873 alloc_bootmem_node(pgdat, alloc_size);
2874 } else {
2875 /*
2876 * This case means that a zone whose size was 0 gets new memory
2877 * via memory hot-add.
2878 * But it may be the case that a new node was hot-added. In
2879 * this case vmalloc() will not be able to use this new node's
2880 * memory - this wait_table must be initialized to use this new
2881 * node itself as well.
2882 * To use this new node's memory, further consideration will be
2883 * necessary.
2884 */
Jesper Juhl8691f3a2007-10-16 01:24:49 -07002885 zone->wait_table = vmalloc(alloc_size);
Yasunori Gotocca448f2006-06-23 02:03:10 -07002886 }
2887 if (!zone->wait_table)
2888 return -ENOMEM;
Dave Hansened8ece22005-10-29 18:16:50 -07002889
Yasunori Goto02b694d2006-06-23 02:03:08 -07002890 for(i = 0; i < zone->wait_table_hash_nr_entries; ++i)
Dave Hansened8ece22005-10-29 18:16:50 -07002891 init_waitqueue_head(zone->wait_table + i);
Yasunori Gotocca448f2006-06-23 02:03:10 -07002892
2893 return 0;
Dave Hansened8ece22005-10-29 18:16:50 -07002894}
2895
Matt Tolentinoc09b4242006-01-17 07:03:44 +01002896static __meminit void zone_pcp_init(struct zone *zone)
Dave Hansened8ece22005-10-29 18:16:50 -07002897{
2898 int cpu;
2899 unsigned long batch = zone_batchsize(zone);
2900
2901 for (cpu = 0; cpu < NR_CPUS; cpu++) {
2902#ifdef CONFIG_NUMA
2903 /* Early boot. Slab allocator not functional yet */
Nick Piggin23316bc2006-01-08 01:00:41 -08002904 zone_pcp(zone, cpu) = &boot_pageset[cpu];
Dave Hansened8ece22005-10-29 18:16:50 -07002905 setup_pageset(&boot_pageset[cpu],0);
2906#else
2907 setup_pageset(zone_pcp(zone,cpu), batch);
2908#endif
2909 }
Anton Blanchardf5335c02006-03-25 03:06:49 -08002910 if (zone->present_pages)
2911 printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%lu\n",
2912 zone->name, zone->present_pages, batch);
Dave Hansened8ece22005-10-29 18:16:50 -07002913}
2914
Yasunori Goto718127c2006-06-23 02:03:10 -07002915__meminit int init_currently_empty_zone(struct zone *zone,
2916 unsigned long zone_start_pfn,
Dave Hansena2f3aa022007-01-10 23:15:30 -08002917 unsigned long size,
2918 enum memmap_context context)
Dave Hansened8ece22005-10-29 18:16:50 -07002919{
2920 struct pglist_data *pgdat = zone->zone_pgdat;
Yasunori Gotocca448f2006-06-23 02:03:10 -07002921 int ret;
2922 ret = zone_wait_table_init(zone, size);
2923 if (ret)
2924 return ret;
Dave Hansened8ece22005-10-29 18:16:50 -07002925 pgdat->nr_zones = zone_idx(zone) + 1;
2926
Dave Hansened8ece22005-10-29 18:16:50 -07002927 zone->zone_start_pfn = zone_start_pfn;
2928
Mel Gorman708614e2008-07-23 21:26:51 -07002929 mminit_dprintk(MMINIT_TRACE, "memmap_init",
2930 "Initialising map node %d zone %lu pfns %lu -> %lu\n",
2931 pgdat->node_id,
2932 (unsigned long)zone_idx(zone),
2933 zone_start_pfn, (zone_start_pfn + size));
2934
Andi Kleen1e548de2008-02-04 22:29:26 -08002935 zone_init_free_lists(zone);
Yasunori Goto718127c2006-06-23 02:03:10 -07002936
2937 return 0;
Dave Hansened8ece22005-10-29 18:16:50 -07002938}
2939
Mel Gormanc7132162006-09-27 01:49:43 -07002940#ifdef CONFIG_ARCH_POPULATES_NODE_MAP
2941/*
2942 * Basic iterator support. Return the first range of PFNs for a node
2943 * Note: nid == MAX_NUMNODES returns first region regardless of node
2944 */
Yasunori Gotoa3142c82007-05-08 00:23:07 -07002945static int __meminit first_active_region_index_in_nid(int nid)
Mel Gormanc7132162006-09-27 01:49:43 -07002946{
2947 int i;
2948
2949 for (i = 0; i < nr_nodemap_entries; i++)
2950 if (nid == MAX_NUMNODES || early_node_map[i].nid == nid)
2951 return i;
2952
2953 return -1;
2954}
2955
2956/*
2957 * Basic iterator support. Return the next active range of PFNs for a node
Simon Arlott183ff222007-10-20 01:27:18 +02002958 * Note: nid == MAX_NUMNODES returns next region regardless of node
Mel Gormanc7132162006-09-27 01:49:43 -07002959 */
Yasunori Gotoa3142c82007-05-08 00:23:07 -07002960static int __meminit next_active_region_index_in_nid(int index, int nid)
Mel Gormanc7132162006-09-27 01:49:43 -07002961{
2962 for (index = index + 1; index < nr_nodemap_entries; index++)
2963 if (nid == MAX_NUMNODES || early_node_map[index].nid == nid)
2964 return index;
2965
2966 return -1;
2967}
2968
2969#ifndef CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID
2970/*
2971 * Required by SPARSEMEM. Given a PFN, return what node the PFN is on.
2972 * Architectures may implement their own version but if add_active_range()
2973 * was used and there are no special requirements, this is a convenient
2974 * alternative
2975 */
Stephen Rothwell6f076f52007-05-10 03:15:27 -07002976int __meminit early_pfn_to_nid(unsigned long pfn)
Mel Gormanc7132162006-09-27 01:49:43 -07002977{
2978 int i;
2979
2980 for (i = 0; i < nr_nodemap_entries; i++) {
2981 unsigned long start_pfn = early_node_map[i].start_pfn;
2982 unsigned long end_pfn = early_node_map[i].end_pfn;
2983
2984 if (start_pfn <= pfn && pfn < end_pfn)
2985 return early_node_map[i].nid;
2986 }
2987
2988 return 0;
2989}
2990#endif /* CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID */
2991
2992/* Basic iterator support to walk early_node_map[] */
2993#define for_each_active_range_index_in_nid(i, nid) \
2994 for (i = first_active_region_index_in_nid(nid); i != -1; \
2995 i = next_active_region_index_in_nid(i, nid))
2996
2997/**
2998 * free_bootmem_with_active_regions - Call free_bootmem_node for each active range
Randy Dunlap88ca3b92006-10-04 02:15:25 -07002999 * @nid: The node to free memory on. If MAX_NUMNODES, all nodes are freed.
3000 * @max_low_pfn: The highest PFN that will be passed to free_bootmem_node
Mel Gormanc7132162006-09-27 01:49:43 -07003001 *
3002 * If an architecture guarantees that all ranges registered with
3003 * add_active_ranges() contain no holes and may be freed, this
3004 * this function may be used instead of calling free_bootmem() manually.
3005 */
3006void __init free_bootmem_with_active_regions(int nid,
3007 unsigned long max_low_pfn)
3008{
3009 int i;
3010
3011 for_each_active_range_index_in_nid(i, nid) {
3012 unsigned long size_pages = 0;
3013 unsigned long end_pfn = early_node_map[i].end_pfn;
3014
3015 if (early_node_map[i].start_pfn >= max_low_pfn)
3016 continue;
3017
3018 if (end_pfn > max_low_pfn)
3019 end_pfn = max_low_pfn;
3020
3021 size_pages = end_pfn - early_node_map[i].start_pfn;
3022 free_bootmem_node(NODE_DATA(early_node_map[i].nid),
3023 PFN_PHYS(early_node_map[i].start_pfn),
3024 size_pages << PAGE_SHIFT);
3025 }
3026}
3027
Yinghai Lub5bc6c02008-06-14 18:32:52 -07003028void __init work_with_active_regions(int nid, work_fn_t work_fn, void *data)
3029{
3030 int i;
Yinghai Lud52d53b2008-06-16 20:10:55 -07003031 int ret;
Yinghai Lub5bc6c02008-06-14 18:32:52 -07003032
Yinghai Lud52d53b2008-06-16 20:10:55 -07003033 for_each_active_range_index_in_nid(i, nid) {
3034 ret = work_fn(early_node_map[i].start_pfn,
3035 early_node_map[i].end_pfn, data);
3036 if (ret)
3037 break;
3038 }
Yinghai Lub5bc6c02008-06-14 18:32:52 -07003039}
Mel Gormanc7132162006-09-27 01:49:43 -07003040/**
3041 * sparse_memory_present_with_active_regions - Call memory_present for each active range
Randy Dunlap88ca3b92006-10-04 02:15:25 -07003042 * @nid: The node to call memory_present for. If MAX_NUMNODES, all nodes will be used.
Mel Gormanc7132162006-09-27 01:49:43 -07003043 *
3044 * If an architecture guarantees that all ranges registered with
3045 * add_active_ranges() contain no holes and may be freed, this
Randy Dunlap88ca3b92006-10-04 02:15:25 -07003046 * function may be used instead of calling memory_present() manually.
Mel Gormanc7132162006-09-27 01:49:43 -07003047 */
3048void __init sparse_memory_present_with_active_regions(int nid)
3049{
3050 int i;
3051
3052 for_each_active_range_index_in_nid(i, nid)
3053 memory_present(early_node_map[i].nid,
3054 early_node_map[i].start_pfn,
3055 early_node_map[i].end_pfn);
3056}
3057
3058/**
Mel Gormanfb014392006-09-27 01:49:59 -07003059 * push_node_boundaries - Push node boundaries to at least the requested boundary
3060 * @nid: The nid of the node to push the boundary for
3061 * @start_pfn: The start pfn of the node
3062 * @end_pfn: The end pfn of the node
3063 *
3064 * In reserve-based hot-add, mem_map is allocated that is unused until hotadd
3065 * time. Specifically, on x86_64, SRAT will report ranges that can potentially
3066 * be hotplugged even though no physical memory exists. This function allows
3067 * an arch to push out the node boundaries so mem_map is allocated that can
3068 * be used later.
3069 */
3070#ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
3071void __init push_node_boundaries(unsigned int nid,
3072 unsigned long start_pfn, unsigned long end_pfn)
3073{
Mel Gorman6b74ab92008-07-23 21:26:49 -07003074 mminit_dprintk(MMINIT_TRACE, "zoneboundary",
3075 "Entering push_node_boundaries(%u, %lu, %lu)\n",
Mel Gormanfb014392006-09-27 01:49:59 -07003076 nid, start_pfn, end_pfn);
3077
3078 /* Initialise the boundary for this node if necessary */
3079 if (node_boundary_end_pfn[nid] == 0)
3080 node_boundary_start_pfn[nid] = -1UL;
3081
3082 /* Update the boundaries */
3083 if (node_boundary_start_pfn[nid] > start_pfn)
3084 node_boundary_start_pfn[nid] = start_pfn;
3085 if (node_boundary_end_pfn[nid] < end_pfn)
3086 node_boundary_end_pfn[nid] = end_pfn;
3087}
3088
3089/* If necessary, push the node boundary out for reserve hotadd */
Jan Beulich98011f52007-07-15 23:38:17 -07003090static void __meminit account_node_boundary(unsigned int nid,
Mel Gormanfb014392006-09-27 01:49:59 -07003091 unsigned long *start_pfn, unsigned long *end_pfn)
3092{
Mel Gorman6b74ab92008-07-23 21:26:49 -07003093 mminit_dprintk(MMINIT_TRACE, "zoneboundary",
3094 "Entering account_node_boundary(%u, %lu, %lu)\n",
Mel Gormanfb014392006-09-27 01:49:59 -07003095 nid, *start_pfn, *end_pfn);
3096
3097 /* Return if boundary information has not been provided */
3098 if (node_boundary_end_pfn[nid] == 0)
3099 return;
3100
3101 /* Check the boundaries and update if necessary */
3102 if (node_boundary_start_pfn[nid] < *start_pfn)
3103 *start_pfn = node_boundary_start_pfn[nid];
3104 if (node_boundary_end_pfn[nid] > *end_pfn)
3105 *end_pfn = node_boundary_end_pfn[nid];
3106}
3107#else
3108void __init push_node_boundaries(unsigned int nid,
3109 unsigned long start_pfn, unsigned long end_pfn) {}
3110
Jan Beulich98011f52007-07-15 23:38:17 -07003111static void __meminit account_node_boundary(unsigned int nid,
Mel Gormanfb014392006-09-27 01:49:59 -07003112 unsigned long *start_pfn, unsigned long *end_pfn) {}
3113#endif
3114
3115
3116/**
Mel Gormanc7132162006-09-27 01:49:43 -07003117 * get_pfn_range_for_nid - Return the start and end page frames for a node
Randy Dunlap88ca3b92006-10-04 02:15:25 -07003118 * @nid: The nid to return the range for. If MAX_NUMNODES, the min and max PFN are returned.
3119 * @start_pfn: Passed by reference. On return, it will have the node start_pfn.
3120 * @end_pfn: Passed by reference. On return, it will have the node end_pfn.
Mel Gormanc7132162006-09-27 01:49:43 -07003121 *
3122 * It returns the start and end page frame of a node based on information
3123 * provided by an arch calling add_active_range(). If called for a node
3124 * with no available memory, a warning is printed and the start and end
Randy Dunlap88ca3b92006-10-04 02:15:25 -07003125 * PFNs will be 0.
Mel Gormanc7132162006-09-27 01:49:43 -07003126 */
Yasunori Gotoa3142c82007-05-08 00:23:07 -07003127void __meminit get_pfn_range_for_nid(unsigned int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07003128 unsigned long *start_pfn, unsigned long *end_pfn)
3129{
3130 int i;
3131 *start_pfn = -1UL;
3132 *end_pfn = 0;
3133
3134 for_each_active_range_index_in_nid(i, nid) {
3135 *start_pfn = min(*start_pfn, early_node_map[i].start_pfn);
3136 *end_pfn = max(*end_pfn, early_node_map[i].end_pfn);
3137 }
3138
Christoph Lameter633c0662007-10-16 01:25:37 -07003139 if (*start_pfn == -1UL)
Mel Gormanc7132162006-09-27 01:49:43 -07003140 *start_pfn = 0;
Mel Gormanfb014392006-09-27 01:49:59 -07003141
3142 /* Push the node boundaries out if requested */
3143 account_node_boundary(nid, start_pfn, end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07003144}
3145
3146/*
Mel Gorman2a1e2742007-07-17 04:03:12 -07003147 * This finds a zone that can be used for ZONE_MOVABLE pages. The
3148 * assumption is made that zones within a node are ordered in monotonic
3149 * increasing memory addresses so that the "highest" populated zone is used
3150 */
Adrian Bunkb69a7282008-07-23 21:28:12 -07003151static void __init find_usable_zone_for_movable(void)
Mel Gorman2a1e2742007-07-17 04:03:12 -07003152{
3153 int zone_index;
3154 for (zone_index = MAX_NR_ZONES - 1; zone_index >= 0; zone_index--) {
3155 if (zone_index == ZONE_MOVABLE)
3156 continue;
3157
3158 if (arch_zone_highest_possible_pfn[zone_index] >
3159 arch_zone_lowest_possible_pfn[zone_index])
3160 break;
3161 }
3162
3163 VM_BUG_ON(zone_index == -1);
3164 movable_zone = zone_index;
3165}
3166
3167/*
3168 * The zone ranges provided by the architecture do not include ZONE_MOVABLE
3169 * because it is sized independant of architecture. Unlike the other zones,
3170 * the starting point for ZONE_MOVABLE is not fixed. It may be different
3171 * in each node depending on the size of each node and how evenly kernelcore
3172 * is distributed. This helper function adjusts the zone ranges
3173 * provided by the architecture for a given node by using the end of the
3174 * highest usable zone for ZONE_MOVABLE. This preserves the assumption that
3175 * zones within a node are in order of monotonic increases memory addresses
3176 */
Adrian Bunkb69a7282008-07-23 21:28:12 -07003177static void __meminit adjust_zone_range_for_zone_movable(int nid,
Mel Gorman2a1e2742007-07-17 04:03:12 -07003178 unsigned long zone_type,
3179 unsigned long node_start_pfn,
3180 unsigned long node_end_pfn,
3181 unsigned long *zone_start_pfn,
3182 unsigned long *zone_end_pfn)
3183{
3184 /* Only adjust if ZONE_MOVABLE is on this node */
3185 if (zone_movable_pfn[nid]) {
3186 /* Size ZONE_MOVABLE */
3187 if (zone_type == ZONE_MOVABLE) {
3188 *zone_start_pfn = zone_movable_pfn[nid];
3189 *zone_end_pfn = min(node_end_pfn,
3190 arch_zone_highest_possible_pfn[movable_zone]);
3191
3192 /* Adjust for ZONE_MOVABLE starting within this range */
3193 } else if (*zone_start_pfn < zone_movable_pfn[nid] &&
3194 *zone_end_pfn > zone_movable_pfn[nid]) {
3195 *zone_end_pfn = zone_movable_pfn[nid];
3196
3197 /* Check if this whole range is within ZONE_MOVABLE */
3198 } else if (*zone_start_pfn >= zone_movable_pfn[nid])
3199 *zone_start_pfn = *zone_end_pfn;
3200 }
3201}
3202
3203/*
Mel Gormanc7132162006-09-27 01:49:43 -07003204 * Return the number of pages a zone spans in a node, including holes
3205 * present_pages = zone_spanned_pages_in_node() - zone_absent_pages_in_node()
3206 */
Paul Mundt6ea6e682007-07-15 23:38:20 -07003207static unsigned long __meminit zone_spanned_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07003208 unsigned long zone_type,
3209 unsigned long *ignored)
3210{
3211 unsigned long node_start_pfn, node_end_pfn;
3212 unsigned long zone_start_pfn, zone_end_pfn;
3213
3214 /* Get the start and end of the node and zone */
3215 get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
3216 zone_start_pfn = arch_zone_lowest_possible_pfn[zone_type];
3217 zone_end_pfn = arch_zone_highest_possible_pfn[zone_type];
Mel Gorman2a1e2742007-07-17 04:03:12 -07003218 adjust_zone_range_for_zone_movable(nid, zone_type,
3219 node_start_pfn, node_end_pfn,
3220 &zone_start_pfn, &zone_end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07003221
3222 /* Check that this node has pages within the zone's required range */
3223 if (zone_end_pfn < node_start_pfn || zone_start_pfn > node_end_pfn)
3224 return 0;
3225
3226 /* Move the zone boundaries inside the node if necessary */
3227 zone_end_pfn = min(zone_end_pfn, node_end_pfn);
3228 zone_start_pfn = max(zone_start_pfn, node_start_pfn);
3229
3230 /* Return the spanned pages */
3231 return zone_end_pfn - zone_start_pfn;
3232}
3233
3234/*
3235 * Return the number of holes in a range on a node. If nid is MAX_NUMNODES,
Randy Dunlap88ca3b92006-10-04 02:15:25 -07003236 * then all holes in the requested range will be accounted for.
Mel Gormanc7132162006-09-27 01:49:43 -07003237 */
Adrian Bunkb69a7282008-07-23 21:28:12 -07003238static unsigned long __meminit __absent_pages_in_range(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07003239 unsigned long range_start_pfn,
3240 unsigned long range_end_pfn)
3241{
3242 int i = 0;
3243 unsigned long prev_end_pfn = 0, hole_pages = 0;
3244 unsigned long start_pfn;
3245
3246 /* Find the end_pfn of the first active range of pfns in the node */
3247 i = first_active_region_index_in_nid(nid);
3248 if (i == -1)
3249 return 0;
3250
Mel Gormanb5445f92007-07-26 10:41:18 -07003251 prev_end_pfn = min(early_node_map[i].start_pfn, range_end_pfn);
3252
Mel Gorman9c7cd682006-09-27 01:49:58 -07003253 /* Account for ranges before physical memory on this node */
3254 if (early_node_map[i].start_pfn > range_start_pfn)
Mel Gormanb5445f92007-07-26 10:41:18 -07003255 hole_pages = prev_end_pfn - range_start_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07003256
3257 /* Find all holes for the zone within the node */
3258 for (; i != -1; i = next_active_region_index_in_nid(i, nid)) {
3259
3260 /* No need to continue if prev_end_pfn is outside the zone */
3261 if (prev_end_pfn >= range_end_pfn)
3262 break;
3263
3264 /* Make sure the end of the zone is not within the hole */
3265 start_pfn = min(early_node_map[i].start_pfn, range_end_pfn);
3266 prev_end_pfn = max(prev_end_pfn, range_start_pfn);
3267
3268 /* Update the hole size cound and move on */
3269 if (start_pfn > range_start_pfn) {
3270 BUG_ON(prev_end_pfn > start_pfn);
3271 hole_pages += start_pfn - prev_end_pfn;
3272 }
3273 prev_end_pfn = early_node_map[i].end_pfn;
3274 }
3275
Mel Gorman9c7cd682006-09-27 01:49:58 -07003276 /* Account for ranges past physical memory on this node */
3277 if (range_end_pfn > prev_end_pfn)
Mel Gorman0c6cb972006-10-28 10:38:59 -07003278 hole_pages += range_end_pfn -
Mel Gorman9c7cd682006-09-27 01:49:58 -07003279 max(range_start_pfn, prev_end_pfn);
3280
Mel Gormanc7132162006-09-27 01:49:43 -07003281 return hole_pages;
3282}
3283
3284/**
3285 * absent_pages_in_range - Return number of page frames in holes within a range
3286 * @start_pfn: The start PFN to start searching for holes
3287 * @end_pfn: The end PFN to stop searching for holes
3288 *
Randy Dunlap88ca3b92006-10-04 02:15:25 -07003289 * It returns the number of pages frames in memory holes within a range.
Mel Gormanc7132162006-09-27 01:49:43 -07003290 */
3291unsigned long __init absent_pages_in_range(unsigned long start_pfn,
3292 unsigned long end_pfn)
3293{
3294 return __absent_pages_in_range(MAX_NUMNODES, start_pfn, end_pfn);
3295}
3296
3297/* Return the number of page frames in holes in a zone on a node */
Paul Mundt6ea6e682007-07-15 23:38:20 -07003298static unsigned long __meminit zone_absent_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07003299 unsigned long zone_type,
3300 unsigned long *ignored)
3301{
Mel Gorman9c7cd682006-09-27 01:49:58 -07003302 unsigned long node_start_pfn, node_end_pfn;
3303 unsigned long zone_start_pfn, zone_end_pfn;
3304
3305 get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
3306 zone_start_pfn = max(arch_zone_lowest_possible_pfn[zone_type],
3307 node_start_pfn);
3308 zone_end_pfn = min(arch_zone_highest_possible_pfn[zone_type],
3309 node_end_pfn);
3310
Mel Gorman2a1e2742007-07-17 04:03:12 -07003311 adjust_zone_range_for_zone_movable(nid, zone_type,
3312 node_start_pfn, node_end_pfn,
3313 &zone_start_pfn, &zone_end_pfn);
Mel Gorman9c7cd682006-09-27 01:49:58 -07003314 return __absent_pages_in_range(nid, zone_start_pfn, zone_end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07003315}
Mel Gorman0e0b8642006-09-27 01:49:56 -07003316
Mel Gormanc7132162006-09-27 01:49:43 -07003317#else
Paul Mundt6ea6e682007-07-15 23:38:20 -07003318static inline unsigned long __meminit zone_spanned_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07003319 unsigned long zone_type,
3320 unsigned long *zones_size)
3321{
3322 return zones_size[zone_type];
3323}
3324
Paul Mundt6ea6e682007-07-15 23:38:20 -07003325static inline unsigned long __meminit zone_absent_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07003326 unsigned long zone_type,
3327 unsigned long *zholes_size)
3328{
3329 if (!zholes_size)
3330 return 0;
3331
3332 return zholes_size[zone_type];
3333}
Mel Gorman0e0b8642006-09-27 01:49:56 -07003334
Mel Gormanc7132162006-09-27 01:49:43 -07003335#endif
3336
Yasunori Gotoa3142c82007-05-08 00:23:07 -07003337static void __meminit calculate_node_totalpages(struct pglist_data *pgdat,
Mel Gormanc7132162006-09-27 01:49:43 -07003338 unsigned long *zones_size, unsigned long *zholes_size)
3339{
3340 unsigned long realtotalpages, totalpages = 0;
3341 enum zone_type i;
3342
3343 for (i = 0; i < MAX_NR_ZONES; i++)
3344 totalpages += zone_spanned_pages_in_node(pgdat->node_id, i,
3345 zones_size);
3346 pgdat->node_spanned_pages = totalpages;
3347
3348 realtotalpages = totalpages;
3349 for (i = 0; i < MAX_NR_ZONES; i++)
3350 realtotalpages -=
3351 zone_absent_pages_in_node(pgdat->node_id, i,
3352 zholes_size);
3353 pgdat->node_present_pages = realtotalpages;
3354 printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id,
3355 realtotalpages);
3356}
3357
Mel Gorman835c1342007-10-16 01:25:47 -07003358#ifndef CONFIG_SPARSEMEM
3359/*
3360 * Calculate the size of the zone->blockflags rounded to an unsigned long
Mel Gormand9c23402007-10-16 01:26:01 -07003361 * Start by making sure zonesize is a multiple of pageblock_order by rounding
3362 * up. Then use 1 NR_PAGEBLOCK_BITS worth of bits per pageblock, finally
Mel Gorman835c1342007-10-16 01:25:47 -07003363 * round what is now in bits to nearest long in bits, then return it in
3364 * bytes.
3365 */
3366static unsigned long __init usemap_size(unsigned long zonesize)
3367{
3368 unsigned long usemapsize;
3369
Mel Gormand9c23402007-10-16 01:26:01 -07003370 usemapsize = roundup(zonesize, pageblock_nr_pages);
3371 usemapsize = usemapsize >> pageblock_order;
Mel Gorman835c1342007-10-16 01:25:47 -07003372 usemapsize *= NR_PAGEBLOCK_BITS;
3373 usemapsize = roundup(usemapsize, 8 * sizeof(unsigned long));
3374
3375 return usemapsize / 8;
3376}
3377
3378static void __init setup_usemap(struct pglist_data *pgdat,
3379 struct zone *zone, unsigned long zonesize)
3380{
3381 unsigned long usemapsize = usemap_size(zonesize);
3382 zone->pageblock_flags = NULL;
Julia Lawall58a01a42009-01-06 14:39:28 -08003383 if (usemapsize)
Mel Gorman835c1342007-10-16 01:25:47 -07003384 zone->pageblock_flags = alloc_bootmem_node(pgdat, usemapsize);
Mel Gorman835c1342007-10-16 01:25:47 -07003385}
3386#else
3387static void inline setup_usemap(struct pglist_data *pgdat,
3388 struct zone *zone, unsigned long zonesize) {}
3389#endif /* CONFIG_SPARSEMEM */
3390
Mel Gormand9c23402007-10-16 01:26:01 -07003391#ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
Mel Gormanba72cb82007-11-28 16:21:13 -08003392
3393/* Return a sensible default order for the pageblock size. */
3394static inline int pageblock_default_order(void)
3395{
3396 if (HPAGE_SHIFT > PAGE_SHIFT)
3397 return HUGETLB_PAGE_ORDER;
3398
3399 return MAX_ORDER-1;
3400}
3401
Mel Gormand9c23402007-10-16 01:26:01 -07003402/* Initialise the number of pages represented by NR_PAGEBLOCK_BITS */
3403static inline void __init set_pageblock_order(unsigned int order)
3404{
3405 /* Check that pageblock_nr_pages has not already been setup */
3406 if (pageblock_order)
3407 return;
3408
3409 /*
3410 * Assume the largest contiguous order of interest is a huge page.
3411 * This value may be variable depending on boot parameters on IA64
3412 */
3413 pageblock_order = order;
3414}
3415#else /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
3416
Mel Gormanba72cb82007-11-28 16:21:13 -08003417/*
3418 * When CONFIG_HUGETLB_PAGE_SIZE_VARIABLE is not set, set_pageblock_order()
3419 * and pageblock_default_order() are unused as pageblock_order is set
3420 * at compile-time. See include/linux/pageblock-flags.h for the values of
3421 * pageblock_order based on the kernel config
3422 */
3423static inline int pageblock_default_order(unsigned int order)
3424{
3425 return MAX_ORDER-1;
3426}
Mel Gormand9c23402007-10-16 01:26:01 -07003427#define set_pageblock_order(x) do {} while (0)
3428
3429#endif /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
3430
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431/*
3432 * Set up the zone data structures:
3433 * - mark all pages reserved
3434 * - mark all memory queues empty
3435 * - clear the memory bitmaps
3436 */
Alexander van Heukelumb5a0e012008-02-23 15:24:06 -08003437static void __paginginit free_area_init_core(struct pglist_data *pgdat,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 unsigned long *zones_size, unsigned long *zholes_size)
3439{
Christoph Lameter2f1b6242006-09-25 23:31:13 -07003440 enum zone_type j;
Dave Hansened8ece22005-10-29 18:16:50 -07003441 int nid = pgdat->node_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 unsigned long zone_start_pfn = pgdat->node_start_pfn;
Yasunori Goto718127c2006-06-23 02:03:10 -07003443 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
Dave Hansen208d54e2005-10-29 18:16:52 -07003445 pgdat_resize_init(pgdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 pgdat->nr_zones = 0;
3447 init_waitqueue_head(&pgdat->kswapd_wait);
3448 pgdat->kswapd_max_order = 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003449 pgdat_page_cgroup_init(pgdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450
3451 for (j = 0; j < MAX_NR_ZONES; j++) {
3452 struct zone *zone = pgdat->node_zones + j;
Mel Gorman0e0b8642006-09-27 01:49:56 -07003453 unsigned long size, realsize, memmap_pages;
Christoph Lameterb69408e2008-10-18 20:26:14 -07003454 enum lru_list l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455
Mel Gormanc7132162006-09-27 01:49:43 -07003456 size = zone_spanned_pages_in_node(nid, j, zones_size);
3457 realsize = size - zone_absent_pages_in_node(nid, j,
3458 zholes_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
Mel Gorman0e0b8642006-09-27 01:49:56 -07003460 /*
3461 * Adjust realsize so that it accounts for how much memory
3462 * is used by this zone for memmap. This affects the watermark
3463 * and per-cpu initialisations
3464 */
Johannes Weinerf7232152008-05-23 13:04:21 -07003465 memmap_pages =
3466 PAGE_ALIGN(size * sizeof(struct page)) >> PAGE_SHIFT;
Mel Gorman0e0b8642006-09-27 01:49:56 -07003467 if (realsize >= memmap_pages) {
3468 realsize -= memmap_pages;
Yinghai Lu5594c8c2009-01-06 14:39:14 -08003469 if (memmap_pages)
3470 printk(KERN_DEBUG
3471 " %s zone: %lu pages used for memmap\n",
3472 zone_names[j], memmap_pages);
Mel Gorman0e0b8642006-09-27 01:49:56 -07003473 } else
3474 printk(KERN_WARNING
3475 " %s zone: %lu pages exceeds realsize %lu\n",
3476 zone_names[j], memmap_pages, realsize);
3477
Christoph Lameter62672762007-02-10 01:43:07 -08003478 /* Account for reserved pages */
3479 if (j == 0 && realsize > dma_reserve) {
Mel Gorman0e0b8642006-09-27 01:49:56 -07003480 realsize -= dma_reserve;
Yinghai Lud903ef92008-10-18 20:27:06 -07003481 printk(KERN_DEBUG " %s zone: %lu pages reserved\n",
Christoph Lameter62672762007-02-10 01:43:07 -08003482 zone_names[0], dma_reserve);
Mel Gorman0e0b8642006-09-27 01:49:56 -07003483 }
3484
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07003485 if (!is_highmem_idx(j))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 nr_kernel_pages += realsize;
3487 nr_all_pages += realsize;
3488
3489 zone->spanned_pages = size;
3490 zone->present_pages = realsize;
Christoph Lameter96146342006-07-03 00:24:13 -07003491#ifdef CONFIG_NUMA
Christoph Lameterd5f541e2006-09-27 01:50:08 -07003492 zone->node = nid;
Christoph Lameter8417bba2006-09-25 23:31:51 -07003493 zone->min_unmapped_pages = (realsize*sysctl_min_unmapped_ratio)
Christoph Lameter96146342006-07-03 00:24:13 -07003494 / 100;
Christoph Lameter0ff38492006-09-25 23:31:52 -07003495 zone->min_slab_pages = (realsize * sysctl_min_slab_ratio) / 100;
Christoph Lameter96146342006-07-03 00:24:13 -07003496#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 zone->name = zone_names[j];
3498 spin_lock_init(&zone->lock);
3499 spin_lock_init(&zone->lru_lock);
Dave Hansenbdc8cb92005-10-29 18:16:53 -07003500 zone_seqlock_init(zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 zone->zone_pgdat = pgdat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502
Martin Bligh3bb1a8522006-10-28 10:38:24 -07003503 zone->prev_priority = DEF_PRIORITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504
Dave Hansened8ece22005-10-29 18:16:50 -07003505 zone_pcp_init(zone);
Christoph Lameterb69408e2008-10-18 20:26:14 -07003506 for_each_lru(l) {
3507 INIT_LIST_HEAD(&zone->lru[l].list);
3508 zone->lru[l].nr_scan = 0;
3509 }
Rik van Riel4f98a2f2008-10-18 20:26:32 -07003510 zone->recent_rotated[0] = 0;
3511 zone->recent_rotated[1] = 0;
3512 zone->recent_scanned[0] = 0;
3513 zone->recent_scanned[1] = 0;
Christoph Lameter2244b952006-06-30 01:55:33 -07003514 zap_zone_vm_stats(zone);
David Rientjese815af92007-10-16 23:25:54 -07003515 zone->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 if (!size)
3517 continue;
3518
Mel Gormanba72cb82007-11-28 16:21:13 -08003519 set_pageblock_order(pageblock_default_order());
Mel Gorman835c1342007-10-16 01:25:47 -07003520 setup_usemap(pgdat, zone, size);
Dave Hansena2f3aa022007-01-10 23:15:30 -08003521 ret = init_currently_empty_zone(zone, zone_start_pfn,
3522 size, MEMMAP_EARLY);
Yasunori Goto718127c2006-06-23 02:03:10 -07003523 BUG_ON(ret);
Heiko Carstens76cdd582008-05-14 16:05:52 -07003524 memmap_init(size, nid, j, zone_start_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 zone_start_pfn += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 }
3527}
3528
Sam Ravnborg577a32f2007-05-17 23:29:25 +02003529static void __init_refok alloc_node_mem_map(struct pglist_data *pgdat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 /* Skip empty nodes */
3532 if (!pgdat->node_spanned_pages)
3533 return;
3534
Andy Whitcroftd41dee32005-06-23 00:07:54 -07003535#ifdef CONFIG_FLAT_NODE_MEM_MAP
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 /* ia64 gets its own node_mem_map, before this, without bootmem */
3537 if (!pgdat->node_mem_map) {
Bob Piccoe984bb42006-05-20 15:00:31 -07003538 unsigned long size, start, end;
Andy Whitcroftd41dee32005-06-23 00:07:54 -07003539 struct page *map;
3540
Bob Piccoe984bb42006-05-20 15:00:31 -07003541 /*
3542 * The zone's endpoints aren't required to be MAX_ORDER
3543 * aligned but the node_mem_map endpoints must be in order
3544 * for the buddy allocator to function correctly.
3545 */
3546 start = pgdat->node_start_pfn & ~(MAX_ORDER_NR_PAGES - 1);
3547 end = pgdat->node_start_pfn + pgdat->node_spanned_pages;
3548 end = ALIGN(end, MAX_ORDER_NR_PAGES);
3549 size = (end - start) * sizeof(struct page);
Dave Hansen6f167ec2005-06-23 00:07:39 -07003550 map = alloc_remap(pgdat->node_id, size);
3551 if (!map)
3552 map = alloc_bootmem_node(pgdat, size);
Bob Piccoe984bb42006-05-20 15:00:31 -07003553 pgdat->node_mem_map = map + (pgdat->node_start_pfn - start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 }
Roman Zippel12d810c2007-05-31 00:40:54 -07003555#ifndef CONFIG_NEED_MULTIPLE_NODES
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 /*
3557 * With no DISCONTIG, the global mem_map is just set as node 0's
3558 */
Mel Gormanc7132162006-09-27 01:49:43 -07003559 if (pgdat == NODE_DATA(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 mem_map = NODE_DATA(0)->node_mem_map;
Mel Gormanc7132162006-09-27 01:49:43 -07003561#ifdef CONFIG_ARCH_POPULATES_NODE_MAP
3562 if (page_to_pfn(mem_map) != pgdat->node_start_pfn)
Thomas Bogendoerfer467bc462008-01-08 15:33:11 -08003563 mem_map -= (pgdat->node_start_pfn - ARCH_PFN_OFFSET);
Mel Gormanc7132162006-09-27 01:49:43 -07003564#endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
3565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566#endif
Andy Whitcroftd41dee32005-06-23 00:07:54 -07003567#endif /* CONFIG_FLAT_NODE_MEM_MAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568}
3569
Johannes Weiner9109fb72008-07-23 21:27:20 -07003570void __paginginit free_area_init_node(int nid, unsigned long *zones_size,
3571 unsigned long node_start_pfn, unsigned long *zholes_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572{
Johannes Weiner9109fb72008-07-23 21:27:20 -07003573 pg_data_t *pgdat = NODE_DATA(nid);
3574
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 pgdat->node_id = nid;
3576 pgdat->node_start_pfn = node_start_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07003577 calculate_node_totalpages(pgdat, zones_size, zholes_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
3579 alloc_node_mem_map(pgdat);
Yinghai Lue8c27ac2008-06-01 13:15:22 -07003580#ifdef CONFIG_FLAT_NODE_MEM_MAP
3581 printk(KERN_DEBUG "free_area_init_node: node %d, pgdat %08lx, node_mem_map %08lx\n",
3582 nid, (unsigned long)pgdat,
3583 (unsigned long)pgdat->node_mem_map);
3584#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585
3586 free_area_init_core(pgdat, zones_size, zholes_size);
3587}
3588
Mel Gormanc7132162006-09-27 01:49:43 -07003589#ifdef CONFIG_ARCH_POPULATES_NODE_MAP
Miklos Szeredi418508c2007-05-23 13:57:55 -07003590
3591#if MAX_NUMNODES > 1
3592/*
3593 * Figure out the number of possible node ids.
3594 */
3595static void __init setup_nr_node_ids(void)
3596{
3597 unsigned int node;
3598 unsigned int highest = 0;
3599
3600 for_each_node_mask(node, node_possible_map)
3601 highest = node;
3602 nr_node_ids = highest + 1;
3603}
3604#else
3605static inline void setup_nr_node_ids(void)
3606{
3607}
3608#endif
3609
Mel Gormanc7132162006-09-27 01:49:43 -07003610/**
3611 * add_active_range - Register a range of PFNs backed by physical memory
3612 * @nid: The node ID the range resides on
3613 * @start_pfn: The start PFN of the available physical memory
3614 * @end_pfn: The end PFN of the available physical memory
3615 *
3616 * These ranges are stored in an early_node_map[] and later used by
3617 * free_area_init_nodes() to calculate zone sizes and holes. If the
3618 * range spans a memory hole, it is up to the architecture to ensure
3619 * the memory is not freed by the bootmem allocator. If possible
3620 * the range being registered will be merged with existing ranges.
3621 */
3622void __init add_active_range(unsigned int nid, unsigned long start_pfn,
3623 unsigned long end_pfn)
3624{
3625 int i;
3626
Mel Gorman6b74ab92008-07-23 21:26:49 -07003627 mminit_dprintk(MMINIT_TRACE, "memory_register",
3628 "Entering add_active_range(%d, %#lx, %#lx) "
3629 "%d entries of %d used\n",
3630 nid, start_pfn, end_pfn,
3631 nr_nodemap_entries, MAX_ACTIVE_REGIONS);
Mel Gormanc7132162006-09-27 01:49:43 -07003632
Mel Gorman2dbb51c2008-07-23 21:26:52 -07003633 mminit_validate_memmodel_limits(&start_pfn, &end_pfn);
3634
Mel Gormanc7132162006-09-27 01:49:43 -07003635 /* Merge with existing active regions if possible */
3636 for (i = 0; i < nr_nodemap_entries; i++) {
3637 if (early_node_map[i].nid != nid)
3638 continue;
3639
3640 /* Skip if an existing region covers this new one */
3641 if (start_pfn >= early_node_map[i].start_pfn &&
3642 end_pfn <= early_node_map[i].end_pfn)
3643 return;
3644
3645 /* Merge forward if suitable */
3646 if (start_pfn <= early_node_map[i].end_pfn &&
3647 end_pfn > early_node_map[i].end_pfn) {
3648 early_node_map[i].end_pfn = end_pfn;
3649 return;
3650 }
3651
3652 /* Merge backward if suitable */
3653 if (start_pfn < early_node_map[i].end_pfn &&
3654 end_pfn >= early_node_map[i].start_pfn) {
3655 early_node_map[i].start_pfn = start_pfn;
3656 return;
3657 }
3658 }
3659
3660 /* Check that early_node_map is large enough */
3661 if (i >= MAX_ACTIVE_REGIONS) {
3662 printk(KERN_CRIT "More than %d memory regions, truncating\n",
3663 MAX_ACTIVE_REGIONS);
3664 return;
3665 }
3666
3667 early_node_map[i].nid = nid;
3668 early_node_map[i].start_pfn = start_pfn;
3669 early_node_map[i].end_pfn = end_pfn;
3670 nr_nodemap_entries = i + 1;
3671}
3672
3673/**
Yinghai Lucc1050b2008-06-13 19:08:52 -07003674 * remove_active_range - Shrink an existing registered range of PFNs
Mel Gormanc7132162006-09-27 01:49:43 -07003675 * @nid: The node id the range is on that should be shrunk
Yinghai Lucc1050b2008-06-13 19:08:52 -07003676 * @start_pfn: The new PFN of the range
3677 * @end_pfn: The new PFN of the range
Mel Gormanc7132162006-09-27 01:49:43 -07003678 *
3679 * i386 with NUMA use alloc_remap() to store a node_mem_map on a local node.
Yinghai Lucc1a9d82008-06-08 19:39:16 -07003680 * The map is kept near the end physical page range that has already been
3681 * registered. This function allows an arch to shrink an existing registered
3682 * range.
Mel Gormanc7132162006-09-27 01:49:43 -07003683 */
Yinghai Lucc1050b2008-06-13 19:08:52 -07003684void __init remove_active_range(unsigned int nid, unsigned long start_pfn,
3685 unsigned long end_pfn)
Mel Gormanc7132162006-09-27 01:49:43 -07003686{
Yinghai Lucc1a9d82008-06-08 19:39:16 -07003687 int i, j;
3688 int removed = 0;
Mel Gormanc7132162006-09-27 01:49:43 -07003689
Yinghai Lucc1050b2008-06-13 19:08:52 -07003690 printk(KERN_DEBUG "remove_active_range (%d, %lu, %lu)\n",
3691 nid, start_pfn, end_pfn);
3692
Mel Gormanc7132162006-09-27 01:49:43 -07003693 /* Find the old active region end and shrink */
Yinghai Lucc1a9d82008-06-08 19:39:16 -07003694 for_each_active_range_index_in_nid(i, nid) {
Yinghai Lucc1050b2008-06-13 19:08:52 -07003695 if (early_node_map[i].start_pfn >= start_pfn &&
3696 early_node_map[i].end_pfn <= end_pfn) {
Yinghai Lucc1a9d82008-06-08 19:39:16 -07003697 /* clear it */
Yinghai Lucc1050b2008-06-13 19:08:52 -07003698 early_node_map[i].start_pfn = 0;
Yinghai Lucc1a9d82008-06-08 19:39:16 -07003699 early_node_map[i].end_pfn = 0;
3700 removed = 1;
3701 continue;
Mel Gormanc7132162006-09-27 01:49:43 -07003702 }
Yinghai Lucc1050b2008-06-13 19:08:52 -07003703 if (early_node_map[i].start_pfn < start_pfn &&
3704 early_node_map[i].end_pfn > start_pfn) {
3705 unsigned long temp_end_pfn = early_node_map[i].end_pfn;
3706 early_node_map[i].end_pfn = start_pfn;
3707 if (temp_end_pfn > end_pfn)
3708 add_active_range(nid, end_pfn, temp_end_pfn);
3709 continue;
3710 }
3711 if (early_node_map[i].start_pfn >= start_pfn &&
3712 early_node_map[i].end_pfn > end_pfn &&
3713 early_node_map[i].start_pfn < end_pfn) {
3714 early_node_map[i].start_pfn = end_pfn;
Yinghai Lucc1a9d82008-06-08 19:39:16 -07003715 continue;
3716 }
3717 }
3718
3719 if (!removed)
3720 return;
3721
3722 /* remove the blank ones */
3723 for (i = nr_nodemap_entries - 1; i > 0; i--) {
3724 if (early_node_map[i].nid != nid)
3725 continue;
3726 if (early_node_map[i].end_pfn)
3727 continue;
3728 /* we found it, get rid of it */
3729 for (j = i; j < nr_nodemap_entries - 1; j++)
3730 memcpy(&early_node_map[j], &early_node_map[j+1],
3731 sizeof(early_node_map[j]));
3732 j = nr_nodemap_entries - 1;
3733 memset(&early_node_map[j], 0, sizeof(early_node_map[j]));
3734 nr_nodemap_entries--;
3735 }
Mel Gormanc7132162006-09-27 01:49:43 -07003736}
3737
3738/**
3739 * remove_all_active_ranges - Remove all currently registered regions
Randy Dunlap88ca3b92006-10-04 02:15:25 -07003740 *
Mel Gormanc7132162006-09-27 01:49:43 -07003741 * During discovery, it may be found that a table like SRAT is invalid
3742 * and an alternative discovery method must be used. This function removes
3743 * all currently registered regions.
3744 */
Randy Dunlap88ca3b92006-10-04 02:15:25 -07003745void __init remove_all_active_ranges(void)
Mel Gormanc7132162006-09-27 01:49:43 -07003746{
3747 memset(early_node_map, 0, sizeof(early_node_map));
3748 nr_nodemap_entries = 0;
Mel Gormanfb014392006-09-27 01:49:59 -07003749#ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
3750 memset(node_boundary_start_pfn, 0, sizeof(node_boundary_start_pfn));
3751 memset(node_boundary_end_pfn, 0, sizeof(node_boundary_end_pfn));
3752#endif /* CONFIG_MEMORY_HOTPLUG_RESERVE */
Mel Gormanc7132162006-09-27 01:49:43 -07003753}
3754
3755/* Compare two active node_active_regions */
3756static int __init cmp_node_active_region(const void *a, const void *b)
3757{
3758 struct node_active_region *arange = (struct node_active_region *)a;
3759 struct node_active_region *brange = (struct node_active_region *)b;
3760
3761 /* Done this way to avoid overflows */
3762 if (arange->start_pfn > brange->start_pfn)
3763 return 1;
3764 if (arange->start_pfn < brange->start_pfn)
3765 return -1;
3766
3767 return 0;
3768}
3769
3770/* sort the node_map by start_pfn */
3771static void __init sort_node_map(void)
3772{
3773 sort(early_node_map, (size_t)nr_nodemap_entries,
3774 sizeof(struct node_active_region),
3775 cmp_node_active_region, NULL);
3776}
3777
Mel Gormana6af2bc2007-02-10 01:42:57 -08003778/* Find the lowest pfn for a node */
Adrian Bunkb69a7282008-07-23 21:28:12 -07003779static unsigned long __init find_min_pfn_for_node(int nid)
Mel Gormanc7132162006-09-27 01:49:43 -07003780{
3781 int i;
Mel Gormana6af2bc2007-02-10 01:42:57 -08003782 unsigned long min_pfn = ULONG_MAX;
Mel Gorman1abbfb42006-11-23 12:01:41 +00003783
Mel Gormanc7132162006-09-27 01:49:43 -07003784 /* Assuming a sorted map, the first range found has the starting pfn */
3785 for_each_active_range_index_in_nid(i, nid)
Mel Gormana6af2bc2007-02-10 01:42:57 -08003786 min_pfn = min(min_pfn, early_node_map[i].start_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07003787
Mel Gormana6af2bc2007-02-10 01:42:57 -08003788 if (min_pfn == ULONG_MAX) {
3789 printk(KERN_WARNING
Paul Jackson2bc0d262008-06-22 07:22:17 -07003790 "Could not find start_pfn for node %d\n", nid);
Mel Gormana6af2bc2007-02-10 01:42:57 -08003791 return 0;
3792 }
3793
3794 return min_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07003795}
3796
3797/**
3798 * find_min_pfn_with_active_regions - Find the minimum PFN registered
3799 *
3800 * It returns the minimum PFN based on information provided via
Randy Dunlap88ca3b92006-10-04 02:15:25 -07003801 * add_active_range().
Mel Gormanc7132162006-09-27 01:49:43 -07003802 */
3803unsigned long __init find_min_pfn_with_active_regions(void)
3804{
3805 return find_min_pfn_for_node(MAX_NUMNODES);
3806}
3807
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07003808/*
3809 * early_calculate_totalpages()
3810 * Sum pages in active regions for movable zone.
3811 * Populate N_HIGH_MEMORY for calculating usable_nodes.
3812 */
Adrian Bunk484f51f2007-10-16 01:26:03 -07003813static unsigned long __init early_calculate_totalpages(void)
Mel Gorman7e63efe2007-07-17 04:03:15 -07003814{
3815 int i;
3816 unsigned long totalpages = 0;
3817
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07003818 for (i = 0; i < nr_nodemap_entries; i++) {
3819 unsigned long pages = early_node_map[i].end_pfn -
Mel Gorman7e63efe2007-07-17 04:03:15 -07003820 early_node_map[i].start_pfn;
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07003821 totalpages += pages;
3822 if (pages)
3823 node_set_state(early_node_map[i].nid, N_HIGH_MEMORY);
3824 }
3825 return totalpages;
Mel Gorman7e63efe2007-07-17 04:03:15 -07003826}
3827
Mel Gorman2a1e2742007-07-17 04:03:12 -07003828/*
3829 * Find the PFN the Movable zone begins in each node. Kernel memory
3830 * is spread evenly between nodes as long as the nodes have enough
3831 * memory. When they don't, some nodes will have more kernelcore than
3832 * others
3833 */
Adrian Bunkb69a7282008-07-23 21:28:12 -07003834static void __init find_zone_movable_pfns_for_nodes(unsigned long *movable_pfn)
Mel Gorman2a1e2742007-07-17 04:03:12 -07003835{
3836 int i, nid;
3837 unsigned long usable_startpfn;
3838 unsigned long kernelcore_node, kernelcore_remaining;
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07003839 unsigned long totalpages = early_calculate_totalpages();
3840 int usable_nodes = nodes_weight(node_states[N_HIGH_MEMORY]);
Mel Gorman2a1e2742007-07-17 04:03:12 -07003841
Mel Gorman7e63efe2007-07-17 04:03:15 -07003842 /*
3843 * If movablecore was specified, calculate what size of
3844 * kernelcore that corresponds so that memory usable for
3845 * any allocation type is evenly spread. If both kernelcore
3846 * and movablecore are specified, then the value of kernelcore
3847 * will be used for required_kernelcore if it's greater than
3848 * what movablecore would have allowed.
3849 */
3850 if (required_movablecore) {
Mel Gorman7e63efe2007-07-17 04:03:15 -07003851 unsigned long corepages;
3852
3853 /*
3854 * Round-up so that ZONE_MOVABLE is at least as large as what
3855 * was requested by the user
3856 */
3857 required_movablecore =
3858 roundup(required_movablecore, MAX_ORDER_NR_PAGES);
3859 corepages = totalpages - required_movablecore;
3860
3861 required_kernelcore = max(required_kernelcore, corepages);
3862 }
3863
Mel Gorman2a1e2742007-07-17 04:03:12 -07003864 /* If kernelcore was not specified, there is no ZONE_MOVABLE */
3865 if (!required_kernelcore)
3866 return;
3867
3868 /* usable_startpfn is the lowest possible pfn ZONE_MOVABLE can be at */
3869 find_usable_zone_for_movable();
3870 usable_startpfn = arch_zone_lowest_possible_pfn[movable_zone];
3871
3872restart:
3873 /* Spread kernelcore memory as evenly as possible throughout nodes */
3874 kernelcore_node = required_kernelcore / usable_nodes;
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07003875 for_each_node_state(nid, N_HIGH_MEMORY) {
Mel Gorman2a1e2742007-07-17 04:03:12 -07003876 /*
3877 * Recalculate kernelcore_node if the division per node
3878 * now exceeds what is necessary to satisfy the requested
3879 * amount of memory for the kernel
3880 */
3881 if (required_kernelcore < kernelcore_node)
3882 kernelcore_node = required_kernelcore / usable_nodes;
3883
3884 /*
3885 * As the map is walked, we track how much memory is usable
3886 * by the kernel using kernelcore_remaining. When it is
3887 * 0, the rest of the node is usable by ZONE_MOVABLE
3888 */
3889 kernelcore_remaining = kernelcore_node;
3890
3891 /* Go through each range of PFNs within this node */
3892 for_each_active_range_index_in_nid(i, nid) {
3893 unsigned long start_pfn, end_pfn;
3894 unsigned long size_pages;
3895
3896 start_pfn = max(early_node_map[i].start_pfn,
3897 zone_movable_pfn[nid]);
3898 end_pfn = early_node_map[i].end_pfn;
3899 if (start_pfn >= end_pfn)
3900 continue;
3901
3902 /* Account for what is only usable for kernelcore */
3903 if (start_pfn < usable_startpfn) {
3904 unsigned long kernel_pages;
3905 kernel_pages = min(end_pfn, usable_startpfn)
3906 - start_pfn;
3907
3908 kernelcore_remaining -= min(kernel_pages,
3909 kernelcore_remaining);
3910 required_kernelcore -= min(kernel_pages,
3911 required_kernelcore);
3912
3913 /* Continue if range is now fully accounted */
3914 if (end_pfn <= usable_startpfn) {
3915
3916 /*
3917 * Push zone_movable_pfn to the end so
3918 * that if we have to rebalance
3919 * kernelcore across nodes, we will
3920 * not double account here
3921 */
3922 zone_movable_pfn[nid] = end_pfn;
3923 continue;
3924 }
3925 start_pfn = usable_startpfn;
3926 }
3927
3928 /*
3929 * The usable PFN range for ZONE_MOVABLE is from
3930 * start_pfn->end_pfn. Calculate size_pages as the
3931 * number of pages used as kernelcore
3932 */
3933 size_pages = end_pfn - start_pfn;
3934 if (size_pages > kernelcore_remaining)
3935 size_pages = kernelcore_remaining;
3936 zone_movable_pfn[nid] = start_pfn + size_pages;
3937
3938 /*
3939 * Some kernelcore has been met, update counts and
3940 * break if the kernelcore for this node has been
3941 * satisified
3942 */
3943 required_kernelcore -= min(required_kernelcore,
3944 size_pages);
3945 kernelcore_remaining -= size_pages;
3946 if (!kernelcore_remaining)
3947 break;
3948 }
3949 }
3950
3951 /*
3952 * If there is still required_kernelcore, we do another pass with one
3953 * less node in the count. This will push zone_movable_pfn[nid] further
3954 * along on the nodes that still have memory until kernelcore is
3955 * satisified
3956 */
3957 usable_nodes--;
3958 if (usable_nodes && required_kernelcore > usable_nodes)
3959 goto restart;
3960
3961 /* Align start of ZONE_MOVABLE on all nids to MAX_ORDER_NR_PAGES */
3962 for (nid = 0; nid < MAX_NUMNODES; nid++)
3963 zone_movable_pfn[nid] =
3964 roundup(zone_movable_pfn[nid], MAX_ORDER_NR_PAGES);
3965}
3966
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07003967/* Any regular memory on that node ? */
3968static void check_for_regular_memory(pg_data_t *pgdat)
3969{
3970#ifdef CONFIG_HIGHMEM
3971 enum zone_type zone_type;
3972
3973 for (zone_type = 0; zone_type <= ZONE_NORMAL; zone_type++) {
3974 struct zone *zone = &pgdat->node_zones[zone_type];
3975 if (zone->present_pages)
3976 node_set_state(zone_to_nid(zone), N_NORMAL_MEMORY);
3977 }
3978#endif
3979}
3980
Mel Gormanc7132162006-09-27 01:49:43 -07003981/**
3982 * free_area_init_nodes - Initialise all pg_data_t and zone data
Randy Dunlap88ca3b92006-10-04 02:15:25 -07003983 * @max_zone_pfn: an array of max PFNs for each zone
Mel Gormanc7132162006-09-27 01:49:43 -07003984 *
3985 * This will call free_area_init_node() for each active node in the system.
3986 * Using the page ranges provided by add_active_range(), the size of each
3987 * zone in each node and their holes is calculated. If the maximum PFN
3988 * between two adjacent zones match, it is assumed that the zone is empty.
3989 * For example, if arch_max_dma_pfn == arch_max_dma32_pfn, it is assumed
3990 * that arch_max_dma32_pfn has no pages. It is also assumed that a zone
3991 * starts where the previous one ended. For example, ZONE_DMA32 starts
3992 * at arch_max_dma_pfn.
3993 */
3994void __init free_area_init_nodes(unsigned long *max_zone_pfn)
3995{
3996 unsigned long nid;
Andrew Mortondb991002008-10-15 22:01:07 -07003997 int i;
Mel Gormanc7132162006-09-27 01:49:43 -07003998
Mel Gormana6af2bc2007-02-10 01:42:57 -08003999 /* Sort early_node_map as initialisation assumes it is sorted */
4000 sort_node_map();
4001
Mel Gormanc7132162006-09-27 01:49:43 -07004002 /* Record where the zone boundaries are */
4003 memset(arch_zone_lowest_possible_pfn, 0,
4004 sizeof(arch_zone_lowest_possible_pfn));
4005 memset(arch_zone_highest_possible_pfn, 0,
4006 sizeof(arch_zone_highest_possible_pfn));
4007 arch_zone_lowest_possible_pfn[0] = find_min_pfn_with_active_regions();
4008 arch_zone_highest_possible_pfn[0] = max_zone_pfn[0];
4009 for (i = 1; i < MAX_NR_ZONES; i++) {
Mel Gorman2a1e2742007-07-17 04:03:12 -07004010 if (i == ZONE_MOVABLE)
4011 continue;
Mel Gormanc7132162006-09-27 01:49:43 -07004012 arch_zone_lowest_possible_pfn[i] =
4013 arch_zone_highest_possible_pfn[i-1];
4014 arch_zone_highest_possible_pfn[i] =
4015 max(max_zone_pfn[i], arch_zone_lowest_possible_pfn[i]);
4016 }
Mel Gorman2a1e2742007-07-17 04:03:12 -07004017 arch_zone_lowest_possible_pfn[ZONE_MOVABLE] = 0;
4018 arch_zone_highest_possible_pfn[ZONE_MOVABLE] = 0;
4019
4020 /* Find the PFNs that ZONE_MOVABLE begins at in each node */
4021 memset(zone_movable_pfn, 0, sizeof(zone_movable_pfn));
4022 find_zone_movable_pfns_for_nodes(zone_movable_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07004023
Mel Gormanc7132162006-09-27 01:49:43 -07004024 /* Print out the zone ranges */
4025 printk("Zone PFN ranges:\n");
Mel Gorman2a1e2742007-07-17 04:03:12 -07004026 for (i = 0; i < MAX_NR_ZONES; i++) {
4027 if (i == ZONE_MOVABLE)
4028 continue;
Paul Jackson5dab8ec2008-06-25 05:44:40 -07004029 printk(" %-8s %0#10lx -> %0#10lx\n",
Mel Gormanc7132162006-09-27 01:49:43 -07004030 zone_names[i],
4031 arch_zone_lowest_possible_pfn[i],
4032 arch_zone_highest_possible_pfn[i]);
Mel Gorman2a1e2742007-07-17 04:03:12 -07004033 }
4034
4035 /* Print out the PFNs ZONE_MOVABLE begins at in each node */
4036 printk("Movable zone start PFN for each node\n");
4037 for (i = 0; i < MAX_NUMNODES; i++) {
4038 if (zone_movable_pfn[i])
4039 printk(" Node %d: %lu\n", i, zone_movable_pfn[i]);
4040 }
Mel Gormanc7132162006-09-27 01:49:43 -07004041
4042 /* Print out the early_node_map[] */
4043 printk("early_node_map[%d] active PFN ranges\n", nr_nodemap_entries);
4044 for (i = 0; i < nr_nodemap_entries; i++)
Paul Jackson5dab8ec2008-06-25 05:44:40 -07004045 printk(" %3d: %0#10lx -> %0#10lx\n", early_node_map[i].nid,
Mel Gormanc7132162006-09-27 01:49:43 -07004046 early_node_map[i].start_pfn,
4047 early_node_map[i].end_pfn);
4048
4049 /* Initialise every node */
Mel Gorman708614e2008-07-23 21:26:51 -07004050 mminit_verify_pageflags_layout();
Christoph Lameter8ef82862007-02-20 13:57:52 -08004051 setup_nr_node_ids();
Mel Gormanc7132162006-09-27 01:49:43 -07004052 for_each_online_node(nid) {
4053 pg_data_t *pgdat = NODE_DATA(nid);
Johannes Weiner9109fb72008-07-23 21:27:20 -07004054 free_area_init_node(nid, NULL,
Mel Gormanc7132162006-09-27 01:49:43 -07004055 find_min_pfn_for_node(nid), NULL);
Lee Schermerhorn37b07e42007-10-16 01:25:39 -07004056
4057 /* Any memory on that node */
4058 if (pgdat->node_present_pages)
4059 node_set_state(nid, N_HIGH_MEMORY);
4060 check_for_regular_memory(pgdat);
Mel Gormanc7132162006-09-27 01:49:43 -07004061 }
4062}
Mel Gorman2a1e2742007-07-17 04:03:12 -07004063
Mel Gorman7e63efe2007-07-17 04:03:15 -07004064static int __init cmdline_parse_core(char *p, unsigned long *core)
Mel Gorman2a1e2742007-07-17 04:03:12 -07004065{
4066 unsigned long long coremem;
4067 if (!p)
4068 return -EINVAL;
4069
4070 coremem = memparse(p, &p);
Mel Gorman7e63efe2007-07-17 04:03:15 -07004071 *core = coremem >> PAGE_SHIFT;
Mel Gorman2a1e2742007-07-17 04:03:12 -07004072
Mel Gorman7e63efe2007-07-17 04:03:15 -07004073 /* Paranoid check that UL is enough for the coremem value */
Mel Gorman2a1e2742007-07-17 04:03:12 -07004074 WARN_ON((coremem >> PAGE_SHIFT) > ULONG_MAX);
4075
4076 return 0;
4077}
Mel Gormaned7ed362007-07-17 04:03:14 -07004078
Mel Gorman7e63efe2007-07-17 04:03:15 -07004079/*
4080 * kernelcore=size sets the amount of memory for use for allocations that
4081 * cannot be reclaimed or migrated.
4082 */
4083static int __init cmdline_parse_kernelcore(char *p)
4084{
4085 return cmdline_parse_core(p, &required_kernelcore);
4086}
4087
4088/*
4089 * movablecore=size sets the amount of memory for use for allocations that
4090 * can be reclaimed or migrated.
4091 */
4092static int __init cmdline_parse_movablecore(char *p)
4093{
4094 return cmdline_parse_core(p, &required_movablecore);
4095}
4096
Mel Gormaned7ed362007-07-17 04:03:14 -07004097early_param("kernelcore", cmdline_parse_kernelcore);
Mel Gorman7e63efe2007-07-17 04:03:15 -07004098early_param("movablecore", cmdline_parse_movablecore);
Mel Gormaned7ed362007-07-17 04:03:14 -07004099
Mel Gormanc7132162006-09-27 01:49:43 -07004100#endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
4101
Mel Gorman0e0b8642006-09-27 01:49:56 -07004102/**
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004103 * set_dma_reserve - set the specified number of pages reserved in the first zone
4104 * @new_dma_reserve: The number of pages to mark reserved
Mel Gorman0e0b8642006-09-27 01:49:56 -07004105 *
4106 * The per-cpu batchsize and zone watermarks are determined by present_pages.
4107 * In the DMA zone, a significant percentage may be consumed by kernel image
4108 * and other unfreeable allocations which can skew the watermarks badly. This
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004109 * function may optionally be used to account for unfreeable pages in the
4110 * first zone (e.g., ZONE_DMA). The effect will be lower watermarks and
4111 * smaller per-cpu batchsize.
Mel Gorman0e0b8642006-09-27 01:49:56 -07004112 */
4113void __init set_dma_reserve(unsigned long new_dma_reserve)
4114{
4115 dma_reserve = new_dma_reserve;
4116}
4117
Dave Hansen93b75042005-06-23 00:07:47 -07004118#ifndef CONFIG_NEED_MULTIPLE_NODES
Marcin Slusarz52765582008-09-02 14:35:41 -07004119struct pglist_data __refdata contig_page_data = { .bdata = &bootmem_node_data[0] };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120EXPORT_SYMBOL(contig_page_data);
Dave Hansen93b75042005-06-23 00:07:47 -07004121#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122
4123void __init free_area_init(unsigned long *zones_size)
4124{
Johannes Weiner9109fb72008-07-23 21:27:20 -07004125 free_area_init_node(0, zones_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
4127}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129static int page_alloc_cpu_notify(struct notifier_block *self,
4130 unsigned long action, void *hcpu)
4131{
4132 int cpu = (unsigned long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07004134 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
Christoph Lameter9f8f2172008-02-04 22:29:11 -08004135 drain_pages(cpu);
4136
4137 /*
4138 * Spill the event counters of the dead processor
4139 * into the current processors event counters.
4140 * This artificially elevates the count of the current
4141 * processor.
4142 */
Christoph Lameterf8891e52006-06-30 01:55:45 -07004143 vm_events_fold_cpu(cpu);
Christoph Lameter9f8f2172008-02-04 22:29:11 -08004144
4145 /*
4146 * Zero the differential counters of the dead processor
4147 * so that the vm statistics are consistent.
4148 *
4149 * This is only okay since the processor is dead and cannot
4150 * race with what we are doing.
4151 */
Christoph Lameter2244b952006-06-30 01:55:33 -07004152 refresh_cpu_vm_stats(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 }
4154 return NOTIFY_OK;
4155}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156
4157void __init page_alloc_init(void)
4158{
4159 hotcpu_notifier(page_alloc_cpu_notify, 0);
4160}
4161
4162/*
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07004163 * calculate_totalreserve_pages - called when sysctl_lower_zone_reserve_ratio
4164 * or min_free_kbytes changes.
4165 */
4166static void calculate_totalreserve_pages(void)
4167{
4168 struct pglist_data *pgdat;
4169 unsigned long reserve_pages = 0;
Christoph Lameter2f6726e2006-09-25 23:31:18 -07004170 enum zone_type i, j;
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07004171
4172 for_each_online_pgdat(pgdat) {
4173 for (i = 0; i < MAX_NR_ZONES; i++) {
4174 struct zone *zone = pgdat->node_zones + i;
4175 unsigned long max = 0;
4176
4177 /* Find valid and maximum lowmem_reserve in the zone */
4178 for (j = i; j < MAX_NR_ZONES; j++) {
4179 if (zone->lowmem_reserve[j] > max)
4180 max = zone->lowmem_reserve[j];
4181 }
4182
4183 /* we treat pages_high as reserved pages. */
4184 max += zone->pages_high;
4185
4186 if (max > zone->present_pages)
4187 max = zone->present_pages;
4188 reserve_pages += max;
4189 }
4190 }
4191 totalreserve_pages = reserve_pages;
4192}
4193
4194/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 * setup_per_zone_lowmem_reserve - called whenever
4196 * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
4197 * has a correct pages reserved value, so an adequate number of
4198 * pages are left in the zone after a successful __alloc_pages().
4199 */
4200static void setup_per_zone_lowmem_reserve(void)
4201{
4202 struct pglist_data *pgdat;
Christoph Lameter2f6726e2006-09-25 23:31:18 -07004203 enum zone_type j, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204
KAMEZAWA Hiroyukiec936fc2006-03-27 01:15:59 -08004205 for_each_online_pgdat(pgdat) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 for (j = 0; j < MAX_NR_ZONES; j++) {
4207 struct zone *zone = pgdat->node_zones + j;
4208 unsigned long present_pages = zone->present_pages;
4209
4210 zone->lowmem_reserve[j] = 0;
4211
Christoph Lameter2f6726e2006-09-25 23:31:18 -07004212 idx = j;
4213 while (idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 struct zone *lower_zone;
4215
Christoph Lameter2f6726e2006-09-25 23:31:18 -07004216 idx--;
4217
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 if (sysctl_lowmem_reserve_ratio[idx] < 1)
4219 sysctl_lowmem_reserve_ratio[idx] = 1;
4220
4221 lower_zone = pgdat->node_zones + idx;
4222 lower_zone->lowmem_reserve[j] = present_pages /
4223 sysctl_lowmem_reserve_ratio[idx];
4224 present_pages += lower_zone->present_pages;
4225 }
4226 }
4227 }
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07004228
4229 /* update totalreserve_pages */
4230 calculate_totalreserve_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231}
4232
Randy Dunlap88ca3b92006-10-04 02:15:25 -07004233/**
4234 * setup_per_zone_pages_min - called when min_free_kbytes changes.
4235 *
4236 * Ensures that the pages_{min,low,high} values for each zone are set correctly
4237 * with respect to min_free_kbytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 */
Dave Hansen3947be12005-10-29 18:16:54 -07004239void setup_per_zone_pages_min(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240{
4241 unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
4242 unsigned long lowmem_pages = 0;
4243 struct zone *zone;
4244 unsigned long flags;
4245
4246 /* Calculate total number of !ZONE_HIGHMEM pages */
4247 for_each_zone(zone) {
4248 if (!is_highmem(zone))
4249 lowmem_pages += zone->present_pages;
4250 }
4251
4252 for_each_zone(zone) {
Andrew Mortonac924c62006-05-15 09:43:59 -07004253 u64 tmp;
4254
Gerald Schaefer1125b4e2008-10-18 20:27:11 -07004255 spin_lock_irqsave(&zone->lock, flags);
Andrew Mortonac924c62006-05-15 09:43:59 -07004256 tmp = (u64)pages_min * zone->present_pages;
4257 do_div(tmp, lowmem_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 if (is_highmem(zone)) {
4259 /*
Nick Piggin669ed172005-11-13 16:06:45 -08004260 * __GFP_HIGH and PF_MEMALLOC allocations usually don't
4261 * need highmem pages, so cap pages_min to a small
4262 * value here.
4263 *
4264 * The (pages_high-pages_low) and (pages_low-pages_min)
4265 * deltas controls asynch page reclaim, and so should
4266 * not be capped for highmem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 */
4268 int min_pages;
4269
4270 min_pages = zone->present_pages / 1024;
4271 if (min_pages < SWAP_CLUSTER_MAX)
4272 min_pages = SWAP_CLUSTER_MAX;
4273 if (min_pages > 128)
4274 min_pages = 128;
4275 zone->pages_min = min_pages;
4276 } else {
Nick Piggin669ed172005-11-13 16:06:45 -08004277 /*
4278 * If it's a lowmem zone, reserve a number of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 * proportionate to the zone's size.
4280 */
Nick Piggin669ed172005-11-13 16:06:45 -08004281 zone->pages_min = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 }
4283
Andrew Mortonac924c62006-05-15 09:43:59 -07004284 zone->pages_low = zone->pages_min + (tmp >> 2);
4285 zone->pages_high = zone->pages_min + (tmp >> 1);
Mel Gorman56fd56b2007-10-16 01:25:58 -07004286 setup_zone_migrate_reserve(zone);
Gerald Schaefer1125b4e2008-10-18 20:27:11 -07004287 spin_unlock_irqrestore(&zone->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 }
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07004289
4290 /* update totalreserve_pages */
4291 calculate_totalreserve_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292}
4293
Rik van Riel556adec2008-10-18 20:26:34 -07004294/**
4295 * setup_per_zone_inactive_ratio - called when min_free_kbytes changes.
4296 *
4297 * The inactive anon list should be small enough that the VM never has to
4298 * do too much work, but large enough that each inactive page has a chance
4299 * to be referenced again before it is swapped out.
4300 *
4301 * The inactive_anon ratio is the target ratio of ACTIVE_ANON to
4302 * INACTIVE_ANON pages on this zone's LRU, maintained by the
4303 * pageout code. A zone->inactive_ratio of 3 means 3:1 or 25% of
4304 * the anonymous pages are kept on the inactive list.
4305 *
4306 * total target max
4307 * memory ratio inactive anon
4308 * -------------------------------------
4309 * 10MB 1 5MB
4310 * 100MB 1 50MB
4311 * 1GB 3 250MB
4312 * 10GB 10 0.9GB
4313 * 100GB 31 3GB
4314 * 1TB 101 10GB
4315 * 10TB 320 32GB
4316 */
KOSAKI Motohiroefab8182009-01-06 14:39:46 -08004317static void setup_per_zone_inactive_ratio(void)
Rik van Riel556adec2008-10-18 20:26:34 -07004318{
4319 struct zone *zone;
4320
4321 for_each_zone(zone) {
4322 unsigned int gb, ratio;
4323
4324 /* Zone size in gigabytes */
4325 gb = zone->present_pages >> (30 - PAGE_SHIFT);
4326 ratio = int_sqrt(10 * gb);
4327 if (!ratio)
4328 ratio = 1;
4329
4330 zone->inactive_ratio = ratio;
4331 }
4332}
4333
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334/*
4335 * Initialise min_free_kbytes.
4336 *
4337 * For small machines we want it small (128k min). For large machines
4338 * we want it large (64MB max). But it is not linear, because network
4339 * bandwidth does not increase linearly with machine size. We use
4340 *
4341 * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
4342 * min_free_kbytes = sqrt(lowmem_kbytes * 16)
4343 *
4344 * which yields
4345 *
4346 * 16MB: 512k
4347 * 32MB: 724k
4348 * 64MB: 1024k
4349 * 128MB: 1448k
4350 * 256MB: 2048k
4351 * 512MB: 2896k
4352 * 1024MB: 4096k
4353 * 2048MB: 5792k
4354 * 4096MB: 8192k
4355 * 8192MB: 11584k
4356 * 16384MB: 16384k
4357 */
4358static int __init init_per_zone_pages_min(void)
4359{
4360 unsigned long lowmem_kbytes;
4361
4362 lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
4363
4364 min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
4365 if (min_free_kbytes < 128)
4366 min_free_kbytes = 128;
4367 if (min_free_kbytes > 65536)
4368 min_free_kbytes = 65536;
4369 setup_per_zone_pages_min();
4370 setup_per_zone_lowmem_reserve();
Rik van Riel556adec2008-10-18 20:26:34 -07004371 setup_per_zone_inactive_ratio();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 return 0;
4373}
4374module_init(init_per_zone_pages_min)
4375
4376/*
4377 * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
4378 * that we can call two helper functions whenever min_free_kbytes
4379 * changes.
4380 */
4381int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
4382 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
4383{
4384 proc_dointvec(table, write, file, buffer, length, ppos);
Mel Gorman3b1d92c2007-05-06 14:49:30 -07004385 if (write)
4386 setup_per_zone_pages_min();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 return 0;
4388}
4389
Christoph Lameter96146342006-07-03 00:24:13 -07004390#ifdef CONFIG_NUMA
4391int sysctl_min_unmapped_ratio_sysctl_handler(ctl_table *table, int write,
4392 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
4393{
4394 struct zone *zone;
4395 int rc;
4396
4397 rc = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
4398 if (rc)
4399 return rc;
4400
4401 for_each_zone(zone)
Christoph Lameter8417bba2006-09-25 23:31:51 -07004402 zone->min_unmapped_pages = (zone->present_pages *
Christoph Lameter96146342006-07-03 00:24:13 -07004403 sysctl_min_unmapped_ratio) / 100;
4404 return 0;
4405}
Christoph Lameter0ff38492006-09-25 23:31:52 -07004406
4407int sysctl_min_slab_ratio_sysctl_handler(ctl_table *table, int write,
4408 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
4409{
4410 struct zone *zone;
4411 int rc;
4412
4413 rc = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
4414 if (rc)
4415 return rc;
4416
4417 for_each_zone(zone)
4418 zone->min_slab_pages = (zone->present_pages *
4419 sysctl_min_slab_ratio) / 100;
4420 return 0;
4421}
Christoph Lameter96146342006-07-03 00:24:13 -07004422#endif
4423
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424/*
4425 * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
4426 * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
4427 * whenever sysctl_lowmem_reserve_ratio changes.
4428 *
4429 * The reserve ratio obviously has absolutely no relation with the
4430 * pages_min watermarks. The lowmem reserve ratio can only make sense
4431 * if in function of the boot time zone sizes.
4432 */
4433int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
4434 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
4435{
4436 proc_dointvec_minmax(table, write, file, buffer, length, ppos);
4437 setup_per_zone_lowmem_reserve();
4438 return 0;
4439}
4440
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08004441/*
4442 * percpu_pagelist_fraction - changes the pcp->high for each zone on each
4443 * cpu. It is the fraction of total pages in each zone that a hot per cpu pagelist
4444 * can have before it gets flushed back to buddy allocator.
4445 */
4446
4447int percpu_pagelist_fraction_sysctl_handler(ctl_table *table, int write,
4448 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
4449{
4450 struct zone *zone;
4451 unsigned int cpu;
4452 int ret;
4453
4454 ret = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
4455 if (!write || (ret == -EINVAL))
4456 return ret;
4457 for_each_zone(zone) {
4458 for_each_online_cpu(cpu) {
4459 unsigned long high;
4460 high = zone->present_pages / percpu_pagelist_fraction;
4461 setup_pagelist_highmark(zone_pcp(zone, cpu), high);
4462 }
4463 }
4464 return 0;
4465}
4466
David S. Millerf034b5d2006-08-24 03:08:07 -07004467int hashdist = HASHDIST_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468
4469#ifdef CONFIG_NUMA
4470static int __init set_hashdist(char *str)
4471{
4472 if (!str)
4473 return 0;
4474 hashdist = simple_strtoul(str, &str, 0);
4475 return 1;
4476}
4477__setup("hashdist=", set_hashdist);
4478#endif
4479
4480/*
4481 * allocate a large system hash table from bootmem
4482 * - it is assumed that the hash table must contain an exact power-of-2
4483 * quantity of entries
4484 * - limit is the number of hash buckets, not the total allocation size
4485 */
4486void *__init alloc_large_system_hash(const char *tablename,
4487 unsigned long bucketsize,
4488 unsigned long numentries,
4489 int scale,
4490 int flags,
4491 unsigned int *_hash_shift,
4492 unsigned int *_hash_mask,
4493 unsigned long limit)
4494{
4495 unsigned long long max = limit;
4496 unsigned long log2qty, size;
4497 void *table = NULL;
4498
4499 /* allow the kernel cmdline to have a say */
4500 if (!numentries) {
4501 /* round applicable memory size up to nearest megabyte */
Andrew Morton04903662006-12-06 20:37:33 -08004502 numentries = nr_kernel_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
4504 numentries >>= 20 - PAGE_SHIFT;
4505 numentries <<= 20 - PAGE_SHIFT;
4506
4507 /* limit to 1 bucket per 2^scale bytes of low memory */
4508 if (scale > PAGE_SHIFT)
4509 numentries >>= (scale - PAGE_SHIFT);
4510 else
4511 numentries <<= (PAGE_SHIFT - scale);
Paul Mundt9ab37b82007-01-05 16:36:30 -08004512
4513 /* Make sure we've got at least a 0-order allocation.. */
4514 if (unlikely((numentries * bucketsize) < PAGE_SIZE))
4515 numentries = PAGE_SIZE / bucketsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 }
John Hawkes6e692ed2006-03-25 03:08:02 -08004517 numentries = roundup_pow_of_two(numentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518
4519 /* limit allocation size to 1/16 total memory by default */
4520 if (max == 0) {
4521 max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
4522 do_div(max, bucketsize);
4523 }
4524
4525 if (numentries > max)
4526 numentries = max;
4527
David Howellsf0d1b0b2006-12-08 02:37:49 -08004528 log2qty = ilog2(numentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529
4530 do {
4531 size = bucketsize << log2qty;
4532 if (flags & HASH_EARLY)
Jan Beulich74768ed2008-08-12 15:08:39 -07004533 table = alloc_bootmem_nopanic(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 else if (hashdist)
4535 table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
4536 else {
Pavel Machek2309f9e2008-04-28 02:13:35 -07004537 unsigned long order = get_order(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 table = (void*) __get_free_pages(GFP_ATOMIC, order);
Eric Dumazet1037b832007-07-15 23:38:05 -07004539 /*
4540 * If bucketsize is not a power-of-two, we may free
4541 * some pages at the end of hash table.
4542 */
4543 if (table) {
4544 unsigned long alloc_end = (unsigned long)table +
4545 (PAGE_SIZE << order);
4546 unsigned long used = (unsigned long)table +
4547 PAGE_ALIGN(size);
4548 split_page(virt_to_page(table), order);
4549 while (used < alloc_end) {
4550 free_page(used);
4551 used += PAGE_SIZE;
4552 }
4553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 }
4555 } while (!table && size > PAGE_SIZE && --log2qty);
4556
4557 if (!table)
4558 panic("Failed to allocate %s hash table\n", tablename);
4559
Dan Alonib49ad482007-07-15 23:38:23 -07004560 printk(KERN_INFO "%s hash table entries: %d (order: %d, %lu bytes)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 tablename,
4562 (1U << log2qty),
David Howellsf0d1b0b2006-12-08 02:37:49 -08004563 ilog2(size) - PAGE_SHIFT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 size);
4565
4566 if (_hash_shift)
4567 *_hash_shift = log2qty;
4568 if (_hash_mask)
4569 *_hash_mask = (1 << log2qty) - 1;
4570
4571 return table;
4572}
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -08004573
4574#ifdef CONFIG_OUT_OF_LINE_PFN_TO_PAGE
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -08004575struct page *pfn_to_page(unsigned long pfn)
4576{
Andy Whitcroft67de6482006-06-23 02:03:12 -07004577 return __pfn_to_page(pfn);
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -08004578}
4579unsigned long page_to_pfn(struct page *page)
4580{
Andy Whitcroft67de6482006-06-23 02:03:12 -07004581 return __page_to_pfn(page);
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -08004582}
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -08004583EXPORT_SYMBOL(pfn_to_page);
4584EXPORT_SYMBOL(page_to_pfn);
4585#endif /* CONFIG_OUT_OF_LINE_PFN_TO_PAGE */
Andrew Morton6220ec72006-10-19 23:29:05 -07004586
Mel Gorman835c1342007-10-16 01:25:47 -07004587/* Return a pointer to the bitmap storing bits affecting a block of pages */
4588static inline unsigned long *get_pageblock_bitmap(struct zone *zone,
4589 unsigned long pfn)
4590{
4591#ifdef CONFIG_SPARSEMEM
4592 return __pfn_to_section(pfn)->pageblock_flags;
4593#else
4594 return zone->pageblock_flags;
4595#endif /* CONFIG_SPARSEMEM */
4596}
Andrew Morton6220ec72006-10-19 23:29:05 -07004597
Mel Gorman835c1342007-10-16 01:25:47 -07004598static inline int pfn_to_bitidx(struct zone *zone, unsigned long pfn)
4599{
4600#ifdef CONFIG_SPARSEMEM
4601 pfn &= (PAGES_PER_SECTION-1);
Mel Gormand9c23402007-10-16 01:26:01 -07004602 return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
Mel Gorman835c1342007-10-16 01:25:47 -07004603#else
4604 pfn = pfn - zone->zone_start_pfn;
Mel Gormand9c23402007-10-16 01:26:01 -07004605 return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
Mel Gorman835c1342007-10-16 01:25:47 -07004606#endif /* CONFIG_SPARSEMEM */
4607}
4608
4609/**
Mel Gormand9c23402007-10-16 01:26:01 -07004610 * 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 -07004611 * @page: The page within the block of interest
4612 * @start_bitidx: The first bit of interest to retrieve
4613 * @end_bitidx: The last bit of interest
4614 * returns pageblock_bits flags
4615 */
4616unsigned long get_pageblock_flags_group(struct page *page,
4617 int start_bitidx, int end_bitidx)
4618{
4619 struct zone *zone;
4620 unsigned long *bitmap;
4621 unsigned long pfn, bitidx;
4622 unsigned long flags = 0;
4623 unsigned long value = 1;
4624
4625 zone = page_zone(page);
4626 pfn = page_to_pfn(page);
4627 bitmap = get_pageblock_bitmap(zone, pfn);
4628 bitidx = pfn_to_bitidx(zone, pfn);
4629
4630 for (; start_bitidx <= end_bitidx; start_bitidx++, value <<= 1)
4631 if (test_bit(bitidx + start_bitidx, bitmap))
4632 flags |= value;
4633
4634 return flags;
4635}
4636
4637/**
Mel Gormand9c23402007-10-16 01:26:01 -07004638 * 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 -07004639 * @page: The page within the block of interest
4640 * @start_bitidx: The first bit of interest
4641 * @end_bitidx: The last bit of interest
4642 * @flags: The flags to set
4643 */
4644void set_pageblock_flags_group(struct page *page, unsigned long flags,
4645 int start_bitidx, int end_bitidx)
4646{
4647 struct zone *zone;
4648 unsigned long *bitmap;
4649 unsigned long pfn, bitidx;
4650 unsigned long value = 1;
4651
4652 zone = page_zone(page);
4653 pfn = page_to_pfn(page);
4654 bitmap = get_pageblock_bitmap(zone, pfn);
4655 bitidx = pfn_to_bitidx(zone, pfn);
KAMEZAWA Hiroyuki86051ca2008-04-29 00:58:21 -07004656 VM_BUG_ON(pfn < zone->zone_start_pfn);
4657 VM_BUG_ON(pfn >= zone->zone_start_pfn + zone->spanned_pages);
Mel Gorman835c1342007-10-16 01:25:47 -07004658
4659 for (; start_bitidx <= end_bitidx; start_bitidx++, value <<= 1)
4660 if (flags & value)
4661 __set_bit(bitidx + start_bitidx, bitmap);
4662 else
4663 __clear_bit(bitidx + start_bitidx, bitmap);
4664}
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -07004665
4666/*
4667 * This is designed as sub function...plz see page_isolation.c also.
4668 * set/clear page block's type to be ISOLATE.
4669 * page allocater never alloc memory from ISOLATE block.
4670 */
4671
4672int set_migratetype_isolate(struct page *page)
4673{
4674 struct zone *zone;
4675 unsigned long flags;
4676 int ret = -EBUSY;
4677
4678 zone = page_zone(page);
4679 spin_lock_irqsave(&zone->lock, flags);
4680 /*
4681 * In future, more migrate types will be able to be isolation target.
4682 */
4683 if (get_pageblock_migratetype(page) != MIGRATE_MOVABLE)
4684 goto out;
4685 set_pageblock_migratetype(page, MIGRATE_ISOLATE);
4686 move_freepages_block(zone, page, MIGRATE_ISOLATE);
4687 ret = 0;
4688out:
4689 spin_unlock_irqrestore(&zone->lock, flags);
4690 if (!ret)
Christoph Lameter9f8f2172008-02-04 22:29:11 -08004691 drain_all_pages();
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -07004692 return ret;
4693}
4694
4695void unset_migratetype_isolate(struct page *page)
4696{
4697 struct zone *zone;
4698 unsigned long flags;
4699 zone = page_zone(page);
4700 spin_lock_irqsave(&zone->lock, flags);
4701 if (get_pageblock_migratetype(page) != MIGRATE_ISOLATE)
4702 goto out;
4703 set_pageblock_migratetype(page, MIGRATE_MOVABLE);
4704 move_freepages_block(zone, page, MIGRATE_MOVABLE);
4705out:
4706 spin_unlock_irqrestore(&zone->lock, flags);
4707}
KAMEZAWA Hiroyuki0c0e6192007-10-16 01:26:12 -07004708
4709#ifdef CONFIG_MEMORY_HOTREMOVE
4710/*
4711 * All pages in the range must be isolated before calling this.
4712 */
4713void
4714__offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
4715{
4716 struct page *page;
4717 struct zone *zone;
4718 int order, i;
4719 unsigned long pfn;
4720 unsigned long flags;
4721 /* find the first valid pfn */
4722 for (pfn = start_pfn; pfn < end_pfn; pfn++)
4723 if (pfn_valid(pfn))
4724 break;
4725 if (pfn == end_pfn)
4726 return;
4727 zone = page_zone(pfn_to_page(pfn));
4728 spin_lock_irqsave(&zone->lock, flags);
4729 pfn = start_pfn;
4730 while (pfn < end_pfn) {
4731 if (!pfn_valid(pfn)) {
4732 pfn++;
4733 continue;
4734 }
4735 page = pfn_to_page(pfn);
4736 BUG_ON(page_count(page));
4737 BUG_ON(!PageBuddy(page));
4738 order = page_order(page);
4739#ifdef CONFIG_DEBUG_VM
4740 printk(KERN_INFO "remove from free list %lx %d %lx\n",
4741 pfn, 1 << order, end_pfn);
4742#endif
4743 list_del(&page->lru);
4744 rmv_page_order(page);
4745 zone->free_area[order].nr_free--;
4746 __mod_zone_page_state(zone, NR_FREE_PAGES,
4747 - (1UL << order));
4748 for (i = 0; i < (1 << order); i++)
4749 SetPageReserved((page+i));
4750 pfn += (1 << order);
4751 }
4752 spin_unlock_irqrestore(&zone->lock, flags);
4753}
4754#endif