blob: 1a8c59571cb7303515c7b616f9572c4706ccebb6 [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>
22#include <linux/bootmem.h>
23#include <linux/compiler.h>
Randy Dunlap9f158332005-09-13 01:25:16 -070024#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26#include <linux/suspend.h>
27#include <linux/pagevec.h>
28#include <linux/blkdev.h>
29#include <linux/slab.h>
30#include <linux/notifier.h>
31#include <linux/topology.h>
32#include <linux/sysctl.h>
33#include <linux/cpu.h>
34#include <linux/cpuset.h>
Dave Hansenbdc8cb92005-10-29 18:16:53 -070035#include <linux/memory_hotplug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/nodemask.h>
37#include <linux/vmalloc.h>
Christoph Lameter4be38e32006-01-06 00:11:17 -080038#include <linux/mempolicy.h>
Yasunori Goto68113782006-06-23 02:03:11 -070039#include <linux/stop_machine.h>
Mel Gormanc7132162006-09-27 01:49:43 -070040#include <linux/sort.h>
41#include <linux/pfn.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070042#include <linux/backing-dev.h>
Akinobu Mita933e3122006-12-08 02:39:45 -080043#include <linux/fault-inject.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include <asm/tlbflush.h>
Andrew Mortonac924c62006-05-15 09:43:59 -070046#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include "internal.h"
48
49/*
50 * MCD - HACK: Find somewhere to initialize this EARLY, or make this
51 * initializer cleaner
52 */
Christoph Lameterc3d8c142005-09-06 15:16:33 -070053nodemask_t node_online_map __read_mostly = { { [0] = 1UL } };
Dean Nelson7223a932005-03-23 19:00:00 -070054EXPORT_SYMBOL(node_online_map);
Christoph Lameterc3d8c142005-09-06 15:16:33 -070055nodemask_t node_possible_map __read_mostly = NODE_MASK_ALL;
Dean Nelson7223a932005-03-23 19:00:00 -070056EXPORT_SYMBOL(node_possible_map);
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -070057unsigned long totalram_pages __read_mostly;
Hideo AOKIcb45b0e2006-04-10 22:52:59 -070058unsigned long totalreserve_pages __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059long nr_swap_pages;
Rohit Seth8ad4b1f2006-01-08 01:00:40 -080060int percpu_pagelist_fraction;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Hugh Dickinsd98c7a02006-02-14 13:52:59 -080062static void __free_pages_ok(struct page *page, unsigned int order);
David Howellsa226f6c2006-01-06 00:11:08 -080063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/*
65 * results with 256, 32 in the lowmem_reserve sysctl:
66 * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
67 * 1G machine -> (16M dma, 784M normal, 224M high)
68 * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
69 * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
70 * HIGHMEM allocation will (224M+784M)/256 of ram reserved in ZONE_DMA
Andi Kleena2f1b422005-11-05 17:25:53 +010071 *
72 * TBD: should special case ZONE_DMA32 machines here - in those we normally
73 * don't need any ZONE_NORMAL reservation
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 */
Christoph Lameter2f1b6242006-09-25 23:31:13 -070075int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1] = {
Christoph Lameter4b51d662007-02-10 01:43:10 -080076#ifdef CONFIG_ZONE_DMA
Christoph Lameter2f1b6242006-09-25 23:31:13 -070077 256,
Christoph Lameter4b51d662007-02-10 01:43:10 -080078#endif
Christoph Lameterfb0e7942006-09-25 23:31:13 -070079#ifdef CONFIG_ZONE_DMA32
Christoph Lameter2f1b6242006-09-25 23:31:13 -070080 256,
Christoph Lameterfb0e7942006-09-25 23:31:13 -070081#endif
Christoph Lametere53ef382006-09-25 23:31:14 -070082#ifdef CONFIG_HIGHMEM
Mel Gorman2a1e2742007-07-17 04:03:12 -070083 32,
Christoph Lametere53ef382006-09-25 23:31:14 -070084#endif
Mel Gorman2a1e2742007-07-17 04:03:12 -070085 32,
Christoph Lameter2f1b6242006-09-25 23:31:13 -070086};
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88EXPORT_SYMBOL(totalram_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Helge Deller15ad7cd2006-12-06 20:40:36 -080090static char * const zone_names[MAX_NR_ZONES] = {
Christoph Lameter4b51d662007-02-10 01:43:10 -080091#ifdef CONFIG_ZONE_DMA
Christoph Lameter2f1b6242006-09-25 23:31:13 -070092 "DMA",
Christoph Lameter4b51d662007-02-10 01:43:10 -080093#endif
Christoph Lameterfb0e7942006-09-25 23:31:13 -070094#ifdef CONFIG_ZONE_DMA32
Christoph Lameter2f1b6242006-09-25 23:31:13 -070095 "DMA32",
Christoph Lameterfb0e7942006-09-25 23:31:13 -070096#endif
Christoph Lameter2f1b6242006-09-25 23:31:13 -070097 "Normal",
Christoph Lametere53ef382006-09-25 23:31:14 -070098#ifdef CONFIG_HIGHMEM
Mel Gorman2a1e2742007-07-17 04:03:12 -070099 "HighMem",
Christoph Lametere53ef382006-09-25 23:31:14 -0700100#endif
Mel Gorman2a1e2742007-07-17 04:03:12 -0700101 "Movable",
Christoph Lameter2f1b6242006-09-25 23:31:13 -0700102};
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104int min_free_kbytes = 1024;
105
Yasunori Goto86356ab2006-06-23 02:03:09 -0700106unsigned long __meminitdata nr_kernel_pages;
107unsigned long __meminitdata nr_all_pages;
Yasunori Gotoa3142c82007-05-08 00:23:07 -0700108static unsigned long __meminitdata dma_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Mel Gormanc7132162006-09-27 01:49:43 -0700110#ifdef CONFIG_ARCH_POPULATES_NODE_MAP
111 /*
112 * MAX_ACTIVE_REGIONS determines the maxmimum number of distinct
113 * ranges of memory (RAM) that may be registered with add_active_range().
114 * Ranges passed to add_active_range() will be merged if possible
115 * so the number of times add_active_range() can be called is
116 * related to the number of nodes and the number of holes
117 */
118 #ifdef CONFIG_MAX_ACTIVE_REGIONS
119 /* Allow an architecture to set MAX_ACTIVE_REGIONS to save memory */
120 #define MAX_ACTIVE_REGIONS CONFIG_MAX_ACTIVE_REGIONS
121 #else
122 #if MAX_NUMNODES >= 32
123 /* If there can be many nodes, allow up to 50 holes per node */
124 #define MAX_ACTIVE_REGIONS (MAX_NUMNODES*50)
125 #else
126 /* By default, allow up to 256 distinct regions */
127 #define MAX_ACTIVE_REGIONS 256
128 #endif
129 #endif
130
Jan Beulich98011f52007-07-15 23:38:17 -0700131 static struct node_active_region __meminitdata early_node_map[MAX_ACTIVE_REGIONS];
132 static int __meminitdata nr_nodemap_entries;
133 static unsigned long __meminitdata arch_zone_lowest_possible_pfn[MAX_NR_ZONES];
134 static unsigned long __meminitdata arch_zone_highest_possible_pfn[MAX_NR_ZONES];
Mel Gormanfb014392006-09-27 01:49:59 -0700135#ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
Jan Beulich98011f52007-07-15 23:38:17 -0700136 static unsigned long __meminitdata node_boundary_start_pfn[MAX_NUMNODES];
137 static unsigned long __meminitdata node_boundary_end_pfn[MAX_NUMNODES];
Mel Gormanfb014392006-09-27 01:49:59 -0700138#endif /* CONFIG_MEMORY_HOTPLUG_RESERVE */
Mel Gorman2a1e2742007-07-17 04:03:12 -0700139 unsigned long __initdata required_kernelcore;
Mel Gorman7e63efe2007-07-17 04:03:15 -0700140 unsigned long __initdata required_movablecore;
Paul Mundte2289292007-07-20 00:31:44 -0700141 unsigned long __meminitdata zone_movable_pfn[MAX_NUMNODES];
Mel Gorman2a1e2742007-07-17 04:03:12 -0700142
143 /* movable_zone is the "real" zone pages in ZONE_MOVABLE are taken from */
144 int movable_zone;
145 EXPORT_SYMBOL(movable_zone);
Mel Gormanc7132162006-09-27 01:49:43 -0700146#endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
147
Miklos Szeredi418508c2007-05-23 13:57:55 -0700148#if MAX_NUMNODES > 1
149int nr_node_ids __read_mostly = MAX_NUMNODES;
150EXPORT_SYMBOL(nr_node_ids);
151#endif
152
Nick Piggin13e74442006-01-06 00:10:58 -0800153#ifdef CONFIG_DEBUG_VM
Dave Hansenc6a57e12005-10-29 18:16:52 -0700154static int page_outside_zone_boundaries(struct zone *zone, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Dave Hansenbdc8cb92005-10-29 18:16:53 -0700156 int ret = 0;
157 unsigned seq;
158 unsigned long pfn = page_to_pfn(page);
Dave Hansenc6a57e12005-10-29 18:16:52 -0700159
Dave Hansenbdc8cb92005-10-29 18:16:53 -0700160 do {
161 seq = zone_span_seqbegin(zone);
162 if (pfn >= zone->zone_start_pfn + zone->spanned_pages)
163 ret = 1;
164 else if (pfn < zone->zone_start_pfn)
165 ret = 1;
166 } while (zone_span_seqretry(zone, seq));
167
168 return ret;
Dave Hansenc6a57e12005-10-29 18:16:52 -0700169}
170
171static int page_is_consistent(struct zone *zone, struct page *page)
172{
Andy Whitcroft14e07292007-05-06 14:49:14 -0700173 if (!pfn_valid_within(page_to_pfn(page)))
Dave Hansenc6a57e12005-10-29 18:16:52 -0700174 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (zone != page_zone(page))
Dave Hansenc6a57e12005-10-29 18:16:52 -0700176 return 0;
177
178 return 1;
179}
180/*
181 * Temporary debugging check for pages not lying within a given zone.
182 */
183static int bad_range(struct zone *zone, struct page *page)
184{
185 if (page_outside_zone_boundaries(zone, page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return 1;
Dave Hansenc6a57e12005-10-29 18:16:52 -0700187 if (!page_is_consistent(zone, page))
188 return 1;
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return 0;
191}
Nick Piggin13e74442006-01-06 00:10:58 -0800192#else
193static inline int bad_range(struct zone *zone, struct page *page)
194{
195 return 0;
196}
197#endif
198
Nick Piggin224abf92006-01-06 00:11:11 -0800199static void bad_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Nick Piggin224abf92006-01-06 00:11:11 -0800201 printk(KERN_EMERG "Bad page state in process '%s'\n"
Hugh Dickins7365f3d2006-01-11 12:17:18 -0800202 KERN_EMERG "page:%p flags:0x%0*lx mapping:%p mapcount:%d count:%d\n"
203 KERN_EMERG "Trying to fix it up, but a reboot is needed\n"
204 KERN_EMERG "Backtrace:\n",
Nick Piggin224abf92006-01-06 00:11:11 -0800205 current->comm, page, (int)(2*sizeof(unsigned long)),
206 (unsigned long)page->flags, page->mapping,
207 page_mapcount(page), page_count(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 dump_stack();
Hugh Dickins334795e2005-06-21 17:15:08 -0700209 page->flags &= ~(1 << PG_lru |
210 1 << PG_private |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 1 << PG_locked |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 1 << PG_active |
213 1 << PG_dirty |
Hugh Dickins334795e2005-06-21 17:15:08 -0700214 1 << PG_reclaim |
215 1 << PG_slab |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 1 << PG_swapcache |
Nick Piggin676165a2006-04-10 11:21:48 +1000217 1 << PG_writeback |
218 1 << PG_buddy );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 set_page_count(page, 0);
220 reset_page_mapcount(page);
221 page->mapping = NULL;
Randy Dunlap9f158332005-09-13 01:25:16 -0700222 add_taint(TAINT_BAD_PAGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225/*
226 * Higher-order pages are called "compound pages". They are structured thusly:
227 *
228 * The first PAGE_SIZE page is called the "head page".
229 *
230 * The remaining PAGE_SIZE pages are called "tail pages".
231 *
232 * All pages have PG_compound set. All pages have their ->private pointing at
233 * the head page (even the head page has this).
234 *
Hugh Dickins41d78ba2006-02-14 13:52:58 -0800235 * The first tail page's ->lru.next holds the address of the compound page's
236 * put_page() function. Its ->lru.prev holds the order of allocation.
237 * This usage means that zero-order pages may not be compound.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 */
Hugh Dickinsd98c7a02006-02-14 13:52:59 -0800239
240static void free_compound_page(struct page *page)
241{
Christoph Lameterd85f3382007-05-06 14:49:39 -0700242 __free_pages_ok(page, compound_order(page));
Hugh Dickinsd98c7a02006-02-14 13:52:59 -0800243}
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245static void prep_compound_page(struct page *page, unsigned long order)
246{
247 int i;
248 int nr_pages = 1 << order;
249
Andy Whitcroft33f2ef82006-12-06 20:33:32 -0800250 set_compound_page_dtor(page, free_compound_page);
Christoph Lameterd85f3382007-05-06 14:49:39 -0700251 set_compound_order(page, order);
Christoph Lameter6d777952007-05-06 14:49:40 -0700252 __SetPageHead(page);
Christoph Lameterd85f3382007-05-06 14:49:39 -0700253 for (i = 1; i < nr_pages; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 struct page *p = page + i;
255
Christoph Lameterd85f3382007-05-06 14:49:39 -0700256 __SetPageTail(p);
Christoph Lameterd85f3382007-05-06 14:49:39 -0700257 p->first_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259}
260
261static void destroy_compound_page(struct page *page, unsigned long order)
262{
263 int i;
264 int nr_pages = 1 << order;
265
Christoph Lameterd85f3382007-05-06 14:49:39 -0700266 if (unlikely(compound_order(page) != order))
Nick Piggin224abf92006-01-06 00:11:11 -0800267 bad_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Christoph Lameter6d777952007-05-06 14:49:40 -0700269 if (unlikely(!PageHead(page)))
Christoph Lameterd85f3382007-05-06 14:49:39 -0700270 bad_page(page);
Christoph Lameter6d777952007-05-06 14:49:40 -0700271 __ClearPageHead(page);
Christoph Lameterd85f3382007-05-06 14:49:39 -0700272 for (i = 1; i < nr_pages; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 struct page *p = page + i;
274
Christoph Lameter6d777952007-05-06 14:49:40 -0700275 if (unlikely(!PageTail(p) |
Christoph Lameterd85f3382007-05-06 14:49:39 -0700276 (p->first_page != page)))
Nick Piggin224abf92006-01-06 00:11:11 -0800277 bad_page(page);
Christoph Lameterd85f3382007-05-06 14:49:39 -0700278 __ClearPageTail(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
280}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Nick Piggin17cf4402006-03-22 00:08:41 -0800282static inline void prep_zero_page(struct page *page, int order, gfp_t gfp_flags)
283{
284 int i;
285
Nick Piggin725d7042006-09-25 23:30:55 -0700286 VM_BUG_ON((gfp_flags & (__GFP_WAIT | __GFP_HIGHMEM)) == __GFP_HIGHMEM);
Andrew Morton6626c5d2006-03-22 00:08:42 -0800287 /*
288 * clear_highpage() will use KM_USER0, so it's a bug to use __GFP_ZERO
289 * and __GFP_HIGHMEM from hard or soft interrupt context.
290 */
Nick Piggin725d7042006-09-25 23:30:55 -0700291 VM_BUG_ON((gfp_flags & __GFP_HIGHMEM) && in_interrupt());
Nick Piggin17cf4402006-03-22 00:08:41 -0800292 for (i = 0; i < (1 << order); i++)
293 clear_highpage(page + i);
294}
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296/*
297 * function for dealing with page's order in buddy system.
298 * zone->lock is already acquired when we use these.
299 * So, we don't need atomic page->flags operations here.
300 */
Andrew Morton6aa3001b22006-04-18 22:20:52 -0700301static inline unsigned long page_order(struct page *page)
302{
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700303 return page_private(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
Andrew Morton6aa3001b22006-04-18 22:20:52 -0700306static inline void set_page_order(struct page *page, int order)
307{
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700308 set_page_private(page, order);
Nick Piggin676165a2006-04-10 11:21:48 +1000309 __SetPageBuddy(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
312static inline void rmv_page_order(struct page *page)
313{
Nick Piggin676165a2006-04-10 11:21:48 +1000314 __ClearPageBuddy(page);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700315 set_page_private(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
318/*
319 * Locate the struct page for both the matching buddy in our
320 * pair (buddy1) and the combined O(n+1) page they form (page).
321 *
322 * 1) Any buddy B1 will have an order O twin B2 which satisfies
323 * the following equation:
324 * B2 = B1 ^ (1 << O)
325 * For example, if the starting buddy (buddy2) is #8 its order
326 * 1 buddy is #10:
327 * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
328 *
329 * 2) Any buddy B will have an order O+1 parent P which
330 * satisfies the following equation:
331 * P = B & ~(1 << O)
332 *
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200333 * Assumption: *_mem_map is contiguous at least up to MAX_ORDER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 */
335static inline struct page *
336__page_find_buddy(struct page *page, unsigned long page_idx, unsigned int order)
337{
338 unsigned long buddy_idx = page_idx ^ (1 << order);
339
340 return page + (buddy_idx - page_idx);
341}
342
343static inline unsigned long
344__find_combined_index(unsigned long page_idx, unsigned int order)
345{
346 return (page_idx & ~(1 << order));
347}
348
349/*
350 * This function checks whether a page is free && is the buddy
351 * we can do coalesce a page and its buddy if
Nick Piggin13e74442006-01-06 00:10:58 -0800352 * (a) the buddy is not in a hole &&
Nick Piggin676165a2006-04-10 11:21:48 +1000353 * (b) the buddy is in the buddy system &&
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700354 * (c) a page and its buddy have the same order &&
355 * (d) a page and its buddy are in the same zone.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 *
Nick Piggin676165a2006-04-10 11:21:48 +1000357 * For recording whether a page is in the buddy system, we use PG_buddy.
358 * Setting, clearing, and testing PG_buddy is serialized by zone->lock.
359 *
360 * For recording page's order, we use page_private(page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 */
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700362static inline int page_is_buddy(struct page *page, struct page *buddy,
363 int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Andy Whitcroft14e07292007-05-06 14:49:14 -0700365 if (!pfn_valid_within(page_to_pfn(buddy)))
Nick Piggin13e74442006-01-06 00:10:58 -0800366 return 0;
Nick Piggin13e74442006-01-06 00:10:58 -0800367
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700368 if (page_zone_id(page) != page_zone_id(buddy))
369 return 0;
370
371 if (PageBuddy(buddy) && page_order(buddy) == order) {
372 BUG_ON(page_count(buddy) != 0);
Andrew Morton6aa3001b22006-04-18 22:20:52 -0700373 return 1;
Nick Piggin676165a2006-04-10 11:21:48 +1000374 }
Andrew Morton6aa3001b22006-04-18 22:20:52 -0700375 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
378/*
379 * Freeing function for a buddy system allocator.
380 *
381 * The concept of a buddy system is to maintain direct-mapped table
382 * (containing bit values) for memory blocks of various "orders".
383 * The bottom level table contains the map for the smallest allocatable
384 * units of memory (here, pages), and each level above it describes
385 * pairs of units from the levels below, hence, "buddies".
386 * At a high level, all that happens here is marking the table entry
387 * at the bottom level available, and propagating the changes upward
388 * as necessary, plus some accounting needed to play nicely with other
389 * parts of the VM system.
390 * At each level, we keep a list of pages, which are heads of continuous
Nick Piggin676165a2006-04-10 11:21:48 +1000391 * free pages of length of (1 << order) and marked with PG_buddy. Page's
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700392 * order is recorded in page_private(page) field.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 * So when we are allocating or freeing one, we can derive the state of the
394 * other. That is, if we allocate a small block, and both were
395 * free, the remainder of the region must be split into blocks.
396 * If a block is freed, and its buddy is also free, then this
397 * triggers coalescing into a block of larger size.
398 *
399 * -- wli
400 */
401
Nick Piggin48db57f2006-01-08 01:00:42 -0800402static inline void __free_one_page(struct page *page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 struct zone *zone, unsigned int order)
404{
405 unsigned long page_idx;
406 int order_size = 1 << order;
407
Nick Piggin224abf92006-01-06 00:11:11 -0800408 if (unlikely(PageCompound(page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 destroy_compound_page(page, order);
410
411 page_idx = page_to_pfn(page) & ((1 << MAX_ORDER) - 1);
412
Nick Piggin725d7042006-09-25 23:30:55 -0700413 VM_BUG_ON(page_idx & (order_size - 1));
414 VM_BUG_ON(bad_range(zone, page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Christoph Lameterd23ad422007-02-10 01:43:02 -0800416 __mod_zone_page_state(zone, NR_FREE_PAGES, order_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 while (order < MAX_ORDER-1) {
418 unsigned long combined_idx;
419 struct free_area *area;
420 struct page *buddy;
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 buddy = __page_find_buddy(page, page_idx, order);
Andy Whitcroftcb2b95e2006-06-23 02:03:01 -0700423 if (!page_is_buddy(page, buddy, order))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 break; /* Move the buddy up one level. */
Nick Piggin13e74442006-01-06 00:10:58 -0800425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 list_del(&buddy->lru);
427 area = zone->free_area + order;
428 area->nr_free--;
429 rmv_page_order(buddy);
Nick Piggin13e74442006-01-06 00:10:58 -0800430 combined_idx = __find_combined_index(page_idx, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 page = page + (combined_idx - page_idx);
432 page_idx = combined_idx;
433 order++;
434 }
435 set_page_order(page, order);
436 list_add(&page->lru, &zone->free_area[order].free_list);
437 zone->free_area[order].nr_free++;
438}
439
Nick Piggin224abf92006-01-06 00:11:11 -0800440static inline int free_pages_check(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Nick Piggin92be2e332006-01-06 00:10:57 -0800442 if (unlikely(page_mapcount(page) |
443 (page->mapping != NULL) |
444 (page_count(page) != 0) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 (page->flags & (
446 1 << PG_lru |
447 1 << PG_private |
448 1 << PG_locked |
449 1 << PG_active |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 1 << PG_slab |
451 1 << PG_swapcache |
Nick Pigginb5810032005-10-29 18:16:12 -0700452 1 << PG_writeback |
Nick Piggin676165a2006-04-10 11:21:48 +1000453 1 << PG_reserved |
454 1 << PG_buddy ))))
Nick Piggin224abf92006-01-06 00:11:11 -0800455 bad_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (PageDirty(page))
Nick Piggin242e5462005-09-03 15:54:50 -0700457 __ClearPageDirty(page);
Hugh Dickins689bceb2005-11-21 21:32:20 -0800458 /*
459 * For now, we report if PG_reserved was found set, but do not
460 * clear it, and do not free the page. But we shall soon need
461 * to do more, for when the ZERO_PAGE count wraps negative.
462 */
463 return PageReserved(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
466/*
467 * Frees a list of pages.
468 * Assumes all pages on list are in same zone, and of same order.
Renaud Lienhart207f36e2005-09-10 00:26:59 -0700469 * count is the number of pages to free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 *
471 * If the zone was previously in an "all pages pinned" state then look to
472 * see if this freeing clears that state.
473 *
474 * And clear the zone's pages_scanned counter, to hold off the "all pages are
475 * pinned" detection logic.
476 */
Nick Piggin48db57f2006-01-08 01:00:42 -0800477static void free_pages_bulk(struct zone *zone, int count,
478 struct list_head *list, int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Nick Pigginc54ad302006-01-06 00:10:56 -0800480 spin_lock(&zone->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 zone->all_unreclaimable = 0;
482 zone->pages_scanned = 0;
Nick Piggin48db57f2006-01-08 01:00:42 -0800483 while (count--) {
484 struct page *page;
485
Nick Piggin725d7042006-09-25 23:30:55 -0700486 VM_BUG_ON(list_empty(list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 page = list_entry(list->prev, struct page, lru);
Nick Piggin48db57f2006-01-08 01:00:42 -0800488 /* have to delete it as __free_one_page list manipulates */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 list_del(&page->lru);
Nick Piggin48db57f2006-01-08 01:00:42 -0800490 __free_one_page(page, zone, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
Nick Pigginc54ad302006-01-06 00:10:56 -0800492 spin_unlock(&zone->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
Nick Piggin48db57f2006-01-08 01:00:42 -0800495static void free_one_page(struct zone *zone, struct page *page, int order)
496{
Christoph Lameter006d22d2006-09-25 23:31:48 -0700497 spin_lock(&zone->lock);
498 zone->all_unreclaimable = 0;
499 zone->pages_scanned = 0;
Paul Jackson0798e512006-12-06 20:31:38 -0800500 __free_one_page(page, zone, order);
Christoph Lameter006d22d2006-09-25 23:31:48 -0700501 spin_unlock(&zone->lock);
Nick Piggin48db57f2006-01-08 01:00:42 -0800502}
503
504static void __free_pages_ok(struct page *page, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Nick Pigginc54ad302006-01-06 00:10:56 -0800506 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 int i;
Hugh Dickins689bceb2005-11-21 21:32:20 -0800508 int reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 for (i = 0 ; i < (1 << order) ; ++i)
Nick Piggin224abf92006-01-06 00:11:11 -0800511 reserved += free_pages_check(page + i);
Hugh Dickins689bceb2005-11-21 21:32:20 -0800512 if (reserved)
513 return;
514
Nick Piggin9858db52006-10-11 01:21:30 -0700515 if (!PageHighMem(page))
516 debug_check_no_locks_freed(page_address(page),PAGE_SIZE<<order);
Nick Piggindafb1362006-10-11 01:21:30 -0700517 arch_free_page(page, order);
Nick Piggin48db57f2006-01-08 01:00:42 -0800518 kernel_map_pages(page, 1 << order, 0);
Nick Piggindafb1362006-10-11 01:21:30 -0700519
Nick Pigginc54ad302006-01-06 00:10:56 -0800520 local_irq_save(flags);
Christoph Lameterf8891e52006-06-30 01:55:45 -0700521 __count_vm_events(PGFREE, 1 << order);
Nick Piggin48db57f2006-01-08 01:00:42 -0800522 free_one_page(page_zone(page), page, order);
Nick Pigginc54ad302006-01-06 00:10:56 -0800523 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
David Howellsa226f6c2006-01-06 00:11:08 -0800526/*
527 * permit the bootmem allocator to evade page validation on high-order frees
528 */
529void fastcall __init __free_pages_bootmem(struct page *page, unsigned int order)
530{
531 if (order == 0) {
532 __ClearPageReserved(page);
533 set_page_count(page, 0);
Nick Piggin7835e982006-03-22 00:08:40 -0800534 set_page_refcounted(page);
Nick Piggin545b1ea2006-03-22 00:08:07 -0800535 __free_page(page);
David Howellsa226f6c2006-01-06 00:11:08 -0800536 } else {
David Howellsa226f6c2006-01-06 00:11:08 -0800537 int loop;
538
Nick Piggin545b1ea2006-03-22 00:08:07 -0800539 prefetchw(page);
David Howellsa226f6c2006-01-06 00:11:08 -0800540 for (loop = 0; loop < BITS_PER_LONG; loop++) {
541 struct page *p = &page[loop];
542
Nick Piggin545b1ea2006-03-22 00:08:07 -0800543 if (loop + 1 < BITS_PER_LONG)
544 prefetchw(p + 1);
David Howellsa226f6c2006-01-06 00:11:08 -0800545 __ClearPageReserved(p);
546 set_page_count(p, 0);
547 }
548
Nick Piggin7835e982006-03-22 00:08:40 -0800549 set_page_refcounted(page);
Nick Piggin545b1ea2006-03-22 00:08:07 -0800550 __free_pages(page, order);
David Howellsa226f6c2006-01-06 00:11:08 -0800551 }
552}
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555/*
556 * The order of subdivision here is critical for the IO subsystem.
557 * Please do not alter this order without good reasons and regression
558 * testing. Specifically, as large blocks of memory are subdivided,
559 * the order in which smaller blocks are delivered depends on the order
560 * they're subdivided in this function. This is the primary factor
561 * influencing the order in which pages are delivered to the IO
562 * subsystem according to empirical testing, and this is also justified
563 * by considering the behavior of a buddy system containing a single
564 * large block of memory acted on by a series of small allocations.
565 * This behavior is a critical factor in sglist merging's success.
566 *
567 * -- wli
568 */
Nick Piggin085cc7d2006-01-06 00:11:01 -0800569static inline void expand(struct zone *zone, struct page *page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 int low, int high, struct free_area *area)
571{
572 unsigned long size = 1 << high;
573
574 while (high > low) {
575 area--;
576 high--;
577 size >>= 1;
Nick Piggin725d7042006-09-25 23:30:55 -0700578 VM_BUG_ON(bad_range(zone, &page[size]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 list_add(&page[size].lru, &area->free_list);
580 area->nr_free++;
581 set_page_order(&page[size], high);
582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585/*
586 * This page is about to be returned from the page allocator
587 */
Nick Piggin17cf4402006-03-22 00:08:41 -0800588static int prep_new_page(struct page *page, int order, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Nick Piggin92be2e332006-01-06 00:10:57 -0800590 if (unlikely(page_mapcount(page) |
591 (page->mapping != NULL) |
592 (page_count(page) != 0) |
Hugh Dickins334795e2005-06-21 17:15:08 -0700593 (page->flags & (
594 1 << PG_lru |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 1 << PG_private |
596 1 << PG_locked |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 1 << PG_active |
598 1 << PG_dirty |
Hugh Dickins334795e2005-06-21 17:15:08 -0700599 1 << PG_slab |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 1 << PG_swapcache |
Nick Pigginb5810032005-10-29 18:16:12 -0700601 1 << PG_writeback |
Nick Piggin676165a2006-04-10 11:21:48 +1000602 1 << PG_reserved |
603 1 << PG_buddy ))))
Nick Piggin224abf92006-01-06 00:11:11 -0800604 bad_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Hugh Dickins689bceb2005-11-21 21:32:20 -0800606 /*
607 * For now, we report if PG_reserved was found set, but do not
608 * clear it, and do not allocate the page: as a safety net.
609 */
610 if (PageReserved(page))
611 return 1;
612
Fengguang Wud77c2d72007-07-19 01:47:55 -0700613 page->flags &= ~(1 << PG_uptodate | 1 << PG_error | 1 << PG_readahead |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 1 << PG_referenced | 1 << PG_arch_1 |
Nick Piggin5409bae2007-02-28 20:12:27 -0800615 1 << PG_owner_priv_1 | 1 << PG_mappedtodisk);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700616 set_page_private(page, 0);
Nick Piggin7835e982006-03-22 00:08:40 -0800617 set_page_refcounted(page);
Nick Piggincc1025092006-12-06 20:32:00 -0800618
619 arch_alloc_page(page, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 kernel_map_pages(page, 1 << order, 1);
Nick Piggin17cf4402006-03-22 00:08:41 -0800621
622 if (gfp_flags & __GFP_ZERO)
623 prep_zero_page(page, order, gfp_flags);
624
625 if (order && (gfp_flags & __GFP_COMP))
626 prep_compound_page(page, order);
627
Hugh Dickins689bceb2005-11-21 21:32:20 -0800628 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
631/*
632 * Do the hard work of removing an element from the buddy allocator.
633 * Call me with the zone->lock already held.
634 */
635static struct page *__rmqueue(struct zone *zone, unsigned int order)
636{
637 struct free_area * area;
638 unsigned int current_order;
639 struct page *page;
640
641 for (current_order = order; current_order < MAX_ORDER; ++current_order) {
642 area = zone->free_area + current_order;
643 if (list_empty(&area->free_list))
644 continue;
645
646 page = list_entry(area->free_list.next, struct page, lru);
647 list_del(&page->lru);
648 rmv_page_order(page);
649 area->nr_free--;
Christoph Lameterd23ad422007-02-10 01:43:02 -0800650 __mod_zone_page_state(zone, NR_FREE_PAGES, - (1UL << order));
Nick Piggin085cc7d2006-01-06 00:11:01 -0800651 expand(zone, page, order, current_order, area);
652 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654
655 return NULL;
656}
657
658/*
659 * Obtain a specified number of elements from the buddy allocator, all under
660 * a single hold of the lock, for efficiency. Add them to the supplied list.
661 * Returns the number of new pages which were placed at *list.
662 */
663static int rmqueue_bulk(struct zone *zone, unsigned int order,
664 unsigned long count, struct list_head *list)
665{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Nick Pigginc54ad302006-01-06 00:10:56 -0800668 spin_lock(&zone->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 for (i = 0; i < count; ++i) {
Nick Piggin085cc7d2006-01-06 00:11:01 -0800670 struct page *page = __rmqueue(zone, order);
671 if (unlikely(page == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 list_add_tail(&page->lru, list);
674 }
Nick Pigginc54ad302006-01-06 00:10:56 -0800675 spin_unlock(&zone->lock);
Nick Piggin085cc7d2006-01-06 00:11:01 -0800676 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
Christoph Lameter4ae7c032005-06-21 17:14:57 -0700679#ifdef CONFIG_NUMA
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800680/*
Christoph Lameter4037d452007-05-09 02:35:14 -0700681 * Called from the vmstat counter updater to drain pagesets of this
682 * currently executing processor on remote nodes after they have
683 * expired.
684 *
Christoph Lameter879336c2006-03-22 00:09:08 -0800685 * Note that this function must be called with the thread pinned to
686 * a single processor.
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800687 */
Christoph Lameter4037d452007-05-09 02:35:14 -0700688void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
Christoph Lameter4ae7c032005-06-21 17:14:57 -0700689{
Christoph Lameter4ae7c032005-06-21 17:14:57 -0700690 unsigned long flags;
Christoph Lameter4037d452007-05-09 02:35:14 -0700691 int to_drain;
Christoph Lameter4ae7c032005-06-21 17:14:57 -0700692
Christoph Lameter4037d452007-05-09 02:35:14 -0700693 local_irq_save(flags);
694 if (pcp->count >= pcp->batch)
695 to_drain = pcp->batch;
696 else
697 to_drain = pcp->count;
698 free_pages_bulk(zone, to_drain, &pcp->list, 0);
699 pcp->count -= to_drain;
700 local_irq_restore(flags);
Christoph Lameter4ae7c032005-06-21 17:14:57 -0700701}
702#endif
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704static void __drain_pages(unsigned int cpu)
705{
Nick Pigginc54ad302006-01-06 00:10:56 -0800706 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 struct zone *zone;
708 int i;
709
710 for_each_zone(zone) {
711 struct per_cpu_pageset *pset;
712
Christoph Lameterf2e12bb2007-01-05 16:37:02 -0800713 if (!populated_zone(zone))
714 continue;
715
Christoph Lametere7c8d5c2005-06-21 17:14:47 -0700716 pset = zone_pcp(zone, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
718 struct per_cpu_pages *pcp;
719
720 pcp = &pset->pcp[i];
Nick Pigginc54ad302006-01-06 00:10:56 -0800721 local_irq_save(flags);
Nick Piggin48db57f2006-01-08 01:00:42 -0800722 free_pages_bulk(zone, pcp->count, &pcp->list, 0);
723 pcp->count = 0;
Nick Pigginc54ad302006-01-06 00:10:56 -0800724 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726 }
727}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200729#ifdef CONFIG_HIBERNATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731void mark_free_pages(struct zone *zone)
732{
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700733 unsigned long pfn, max_zone_pfn;
734 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 int order;
736 struct list_head *curr;
737
738 if (!zone->spanned_pages)
739 return;
740
741 spin_lock_irqsave(&zone->lock, flags);
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700742
743 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
744 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
745 if (pfn_valid(pfn)) {
746 struct page *page = pfn_to_page(pfn);
747
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700748 if (!swsusp_page_is_forbidden(page))
749 swsusp_unset_page_free(page);
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 for (order = MAX_ORDER - 1; order >= 0; --order)
753 list_for_each(curr, &zone->free_area[order].free_list) {
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700754 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700756 pfn = page_to_pfn(list_entry(curr, struct page, lru));
757 for (i = 0; i < (1UL << order); i++)
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700758 swsusp_set_page_free(pfn_to_page(pfn + i));
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 spin_unlock_irqrestore(&zone->lock, flags);
762}
763
764/*
765 * Spill all of this CPU's per-cpu pages back into the buddy allocator.
766 */
767void drain_local_pages(void)
768{
769 unsigned long flags;
770
771 local_irq_save(flags);
772 __drain_pages(smp_processor_id());
773 local_irq_restore(flags);
774}
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200775#endif /* CONFIG_HIBERNATION */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777/*
778 * Free a 0-order page
779 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780static void fastcall free_hot_cold_page(struct page *page, int cold)
781{
782 struct zone *zone = page_zone(page);
783 struct per_cpu_pages *pcp;
784 unsigned long flags;
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (PageAnon(page))
787 page->mapping = NULL;
Nick Piggin224abf92006-01-06 00:11:11 -0800788 if (free_pages_check(page))
Hugh Dickins689bceb2005-11-21 21:32:20 -0800789 return;
790
Nick Piggin9858db52006-10-11 01:21:30 -0700791 if (!PageHighMem(page))
792 debug_check_no_locks_freed(page_address(page), PAGE_SIZE);
Nick Piggindafb1362006-10-11 01:21:30 -0700793 arch_free_page(page, 0);
Hugh Dickins689bceb2005-11-21 21:32:20 -0800794 kernel_map_pages(page, 1, 0);
795
Christoph Lametere7c8d5c2005-06-21 17:14:47 -0700796 pcp = &zone_pcp(zone, get_cpu())->pcp[cold];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 local_irq_save(flags);
Christoph Lameterf8891e52006-06-30 01:55:45 -0700798 __count_vm_event(PGFREE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 list_add(&page->lru, &pcp->list);
800 pcp->count++;
Nick Piggin48db57f2006-01-08 01:00:42 -0800801 if (pcp->count >= pcp->high) {
802 free_pages_bulk(zone, pcp->batch, &pcp->list, 0);
803 pcp->count -= pcp->batch;
804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 local_irq_restore(flags);
806 put_cpu();
807}
808
809void fastcall free_hot_page(struct page *page)
810{
811 free_hot_cold_page(page, 0);
812}
813
814void fastcall free_cold_page(struct page *page)
815{
816 free_hot_cold_page(page, 1);
817}
818
Nick Piggin8dfcc9b2006-03-22 00:08:05 -0800819/*
820 * split_page takes a non-compound higher-order page, and splits it into
821 * n (1<<order) sub-pages: page[0..n]
822 * Each sub-page must be freed individually.
823 *
824 * Note: this is probably too low level an operation for use in drivers.
825 * Please consult with lkml before using this in your driver.
826 */
827void split_page(struct page *page, unsigned int order)
828{
829 int i;
830
Nick Piggin725d7042006-09-25 23:30:55 -0700831 VM_BUG_ON(PageCompound(page));
832 VM_BUG_ON(!page_count(page));
Nick Piggin7835e982006-03-22 00:08:40 -0800833 for (i = 1; i < (1 << order); i++)
834 set_page_refcounted(page + i);
Nick Piggin8dfcc9b2006-03-22 00:08:05 -0800835}
Nick Piggin8dfcc9b2006-03-22 00:08:05 -0800836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837/*
838 * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
839 * we cheat by calling it from here, in the order > 0 path. Saves a branch
840 * or two.
841 */
Nick Piggina74609f2006-01-06 00:11:20 -0800842static struct page *buffered_rmqueue(struct zonelist *zonelist,
843 struct zone *zone, int order, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
845 unsigned long flags;
Hugh Dickins689bceb2005-11-21 21:32:20 -0800846 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 int cold = !!(gfp_flags & __GFP_COLD);
Nick Piggina74609f2006-01-06 00:11:20 -0800848 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Hugh Dickins689bceb2005-11-21 21:32:20 -0800850again:
Nick Piggina74609f2006-01-06 00:11:20 -0800851 cpu = get_cpu();
Nick Piggin48db57f2006-01-08 01:00:42 -0800852 if (likely(order == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 struct per_cpu_pages *pcp;
854
Nick Piggina74609f2006-01-06 00:11:20 -0800855 pcp = &zone_pcp(zone, cpu)->pcp[cold];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 local_irq_save(flags);
Nick Piggina74609f2006-01-06 00:11:20 -0800857 if (!pcp->count) {
nkalmala941c7102006-11-02 22:07:04 -0800858 pcp->count = rmqueue_bulk(zone, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 pcp->batch, &pcp->list);
Nick Piggina74609f2006-01-06 00:11:20 -0800860 if (unlikely(!pcp->count))
861 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
Nick Piggina74609f2006-01-06 00:11:20 -0800863 page = list_entry(pcp->list.next, struct page, lru);
864 list_del(&page->lru);
865 pcp->count--;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800866 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 spin_lock_irqsave(&zone->lock, flags);
868 page = __rmqueue(zone, order);
Nick Piggina74609f2006-01-06 00:11:20 -0800869 spin_unlock(&zone->lock);
870 if (!page)
871 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
873
Christoph Lameterf8891e52006-06-30 01:55:45 -0700874 __count_zone_vm_events(PGALLOC, zone, 1 << order);
Christoph Lameterca889e62006-06-30 01:55:44 -0700875 zone_statistics(zonelist, zone);
Nick Piggina74609f2006-01-06 00:11:20 -0800876 local_irq_restore(flags);
877 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Nick Piggin725d7042006-09-25 23:30:55 -0700879 VM_BUG_ON(bad_range(zone, page));
Nick Piggin17cf4402006-03-22 00:08:41 -0800880 if (prep_new_page(page, order, gfp_flags))
Nick Piggina74609f2006-01-06 00:11:20 -0800881 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return page;
Nick Piggina74609f2006-01-06 00:11:20 -0800883
884failed:
885 local_irq_restore(flags);
886 put_cpu();
887 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800890#define ALLOC_NO_WATERMARKS 0x01 /* don't check watermarks at all */
Nick Piggin31488902005-11-28 13:44:03 -0800891#define ALLOC_WMARK_MIN 0x02 /* use pages_min watermark */
892#define ALLOC_WMARK_LOW 0x04 /* use pages_low watermark */
893#define ALLOC_WMARK_HIGH 0x08 /* use pages_high watermark */
894#define ALLOC_HARDER 0x10 /* try to alloc harder */
895#define ALLOC_HIGH 0x20 /* __GFP_HIGH set */
896#define ALLOC_CPUSET 0x40 /* check for correct cpuset */
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800897
Akinobu Mita933e3122006-12-08 02:39:45 -0800898#ifdef CONFIG_FAIL_PAGE_ALLOC
899
900static struct fail_page_alloc_attr {
901 struct fault_attr attr;
902
903 u32 ignore_gfp_highmem;
904 u32 ignore_gfp_wait;
Akinobu Mita54114992007-07-15 23:40:23 -0700905 u32 min_order;
Akinobu Mita933e3122006-12-08 02:39:45 -0800906
907#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
908
909 struct dentry *ignore_gfp_highmem_file;
910 struct dentry *ignore_gfp_wait_file;
Akinobu Mita54114992007-07-15 23:40:23 -0700911 struct dentry *min_order_file;
Akinobu Mita933e3122006-12-08 02:39:45 -0800912
913#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
914
915} fail_page_alloc = {
916 .attr = FAULT_ATTR_INITIALIZER,
Don Mullis6b1b60f2006-12-08 02:39:53 -0800917 .ignore_gfp_wait = 1,
918 .ignore_gfp_highmem = 1,
Akinobu Mita54114992007-07-15 23:40:23 -0700919 .min_order = 1,
Akinobu Mita933e3122006-12-08 02:39:45 -0800920};
921
922static int __init setup_fail_page_alloc(char *str)
923{
924 return setup_fault_attr(&fail_page_alloc.attr, str);
925}
926__setup("fail_page_alloc=", setup_fail_page_alloc);
927
928static int should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
929{
Akinobu Mita54114992007-07-15 23:40:23 -0700930 if (order < fail_page_alloc.min_order)
931 return 0;
Akinobu Mita933e3122006-12-08 02:39:45 -0800932 if (gfp_mask & __GFP_NOFAIL)
933 return 0;
934 if (fail_page_alloc.ignore_gfp_highmem && (gfp_mask & __GFP_HIGHMEM))
935 return 0;
936 if (fail_page_alloc.ignore_gfp_wait && (gfp_mask & __GFP_WAIT))
937 return 0;
938
939 return should_fail(&fail_page_alloc.attr, 1 << order);
940}
941
942#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
943
944static int __init fail_page_alloc_debugfs(void)
945{
946 mode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
947 struct dentry *dir;
948 int err;
949
950 err = init_fault_attr_dentries(&fail_page_alloc.attr,
951 "fail_page_alloc");
952 if (err)
953 return err;
954 dir = fail_page_alloc.attr.dentries.dir;
955
956 fail_page_alloc.ignore_gfp_wait_file =
957 debugfs_create_bool("ignore-gfp-wait", mode, dir,
958 &fail_page_alloc.ignore_gfp_wait);
959
960 fail_page_alloc.ignore_gfp_highmem_file =
961 debugfs_create_bool("ignore-gfp-highmem", mode, dir,
962 &fail_page_alloc.ignore_gfp_highmem);
Akinobu Mita54114992007-07-15 23:40:23 -0700963 fail_page_alloc.min_order_file =
964 debugfs_create_u32("min-order", mode, dir,
965 &fail_page_alloc.min_order);
Akinobu Mita933e3122006-12-08 02:39:45 -0800966
967 if (!fail_page_alloc.ignore_gfp_wait_file ||
Akinobu Mita54114992007-07-15 23:40:23 -0700968 !fail_page_alloc.ignore_gfp_highmem_file ||
969 !fail_page_alloc.min_order_file) {
Akinobu Mita933e3122006-12-08 02:39:45 -0800970 err = -ENOMEM;
971 debugfs_remove(fail_page_alloc.ignore_gfp_wait_file);
972 debugfs_remove(fail_page_alloc.ignore_gfp_highmem_file);
Akinobu Mita54114992007-07-15 23:40:23 -0700973 debugfs_remove(fail_page_alloc.min_order_file);
Akinobu Mita933e3122006-12-08 02:39:45 -0800974 cleanup_fault_attr_dentries(&fail_page_alloc.attr);
975 }
976
977 return err;
978}
979
980late_initcall(fail_page_alloc_debugfs);
981
982#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
983
984#else /* CONFIG_FAIL_PAGE_ALLOC */
985
986static inline int should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
987{
988 return 0;
989}
990
991#endif /* CONFIG_FAIL_PAGE_ALLOC */
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993/*
994 * Return 1 if free pages are above 'mark'. This takes into account the order
995 * of the allocation.
996 */
997int zone_watermark_ok(struct zone *z, int order, unsigned long mark,
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800998 int classzone_idx, int alloc_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
1000 /* free_pages my go negative - that's OK */
Christoph Lameterd23ad422007-02-10 01:43:02 -08001001 long min = mark;
1002 long free_pages = zone_page_state(z, NR_FREE_PAGES) - (1 << order) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 int o;
1004
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001005 if (alloc_flags & ALLOC_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 min -= min / 2;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001007 if (alloc_flags & ALLOC_HARDER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 min -= min / 4;
1009
1010 if (free_pages <= min + z->lowmem_reserve[classzone_idx])
1011 return 0;
1012 for (o = 0; o < order; o++) {
1013 /* At the next order, this order's pages become unavailable */
1014 free_pages -= z->free_area[o].nr_free << o;
1015
1016 /* Require fewer higher order pages to be free */
1017 min >>= 1;
1018
1019 if (free_pages <= min)
1020 return 0;
1021 }
1022 return 1;
1023}
1024
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001025#ifdef CONFIG_NUMA
1026/*
1027 * zlc_setup - Setup for "zonelist cache". Uses cached zone data to
1028 * skip over zones that are not allowed by the cpuset, or that have
1029 * been recently (in last second) found to be nearly full. See further
1030 * comments in mmzone.h. Reduces cache footprint of zonelist scans
1031 * that have to skip over alot of full or unallowed zones.
1032 *
1033 * If the zonelist cache is present in the passed in zonelist, then
1034 * returns a pointer to the allowed node mask (either the current
1035 * tasks mems_allowed, or node_online_map.)
1036 *
1037 * If the zonelist cache is not available for this zonelist, does
1038 * nothing and returns NULL.
1039 *
1040 * If the fullzones BITMAP in the zonelist cache is stale (more than
1041 * a second since last zap'd) then we zap it out (clear its bits.)
1042 *
1043 * We hold off even calling zlc_setup, until after we've checked the
1044 * first zone in the zonelist, on the theory that most allocations will
1045 * be satisfied from that first zone, so best to examine that zone as
1046 * quickly as we can.
1047 */
1048static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
1049{
1050 struct zonelist_cache *zlc; /* cached zonelist speedup info */
1051 nodemask_t *allowednodes; /* zonelist_cache approximation */
1052
1053 zlc = zonelist->zlcache_ptr;
1054 if (!zlc)
1055 return NULL;
1056
1057 if (jiffies - zlc->last_full_zap > 1 * HZ) {
1058 bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
1059 zlc->last_full_zap = jiffies;
1060 }
1061
1062 allowednodes = !in_interrupt() && (alloc_flags & ALLOC_CPUSET) ?
1063 &cpuset_current_mems_allowed :
1064 &node_online_map;
1065 return allowednodes;
1066}
1067
1068/*
1069 * Given 'z' scanning a zonelist, run a couple of quick checks to see
1070 * if it is worth looking at further for free memory:
1071 * 1) Check that the zone isn't thought to be full (doesn't have its
1072 * bit set in the zonelist_cache fullzones BITMAP).
1073 * 2) Check that the zones node (obtained from the zonelist_cache
1074 * z_to_n[] mapping) is allowed in the passed in allowednodes mask.
1075 * Return true (non-zero) if zone is worth looking at further, or
1076 * else return false (zero) if it is not.
1077 *
1078 * This check -ignores- the distinction between various watermarks,
1079 * such as GFP_HIGH, GFP_ATOMIC, PF_MEMALLOC, ... If a zone is
1080 * found to be full for any variation of these watermarks, it will
1081 * be considered full for up to one second by all requests, unless
1082 * we are so low on memory on all allowed nodes that we are forced
1083 * into the second scan of the zonelist.
1084 *
1085 * In the second scan we ignore this zonelist cache and exactly
1086 * apply the watermarks to all zones, even it is slower to do so.
1087 * We are low on memory in the second scan, and should leave no stone
1088 * unturned looking for a free page.
1089 */
1090static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zone **z,
1091 nodemask_t *allowednodes)
1092{
1093 struct zonelist_cache *zlc; /* cached zonelist speedup info */
1094 int i; /* index of *z in zonelist zones */
1095 int n; /* node that zone *z is on */
1096
1097 zlc = zonelist->zlcache_ptr;
1098 if (!zlc)
1099 return 1;
1100
1101 i = z - zonelist->zones;
1102 n = zlc->z_to_n[i];
1103
1104 /* This zone is worth trying if it is allowed but not full */
1105 return node_isset(n, *allowednodes) && !test_bit(i, zlc->fullzones);
1106}
1107
1108/*
1109 * Given 'z' scanning a zonelist, set the corresponding bit in
1110 * zlc->fullzones, so that subsequent attempts to allocate a page
1111 * from that zone don't waste time re-examining it.
1112 */
1113static void zlc_mark_zone_full(struct zonelist *zonelist, struct zone **z)
1114{
1115 struct zonelist_cache *zlc; /* cached zonelist speedup info */
1116 int i; /* index of *z in zonelist zones */
1117
1118 zlc = zonelist->zlcache_ptr;
1119 if (!zlc)
1120 return;
1121
1122 i = z - zonelist->zones;
1123
1124 set_bit(i, zlc->fullzones);
1125}
1126
1127#else /* CONFIG_NUMA */
1128
1129static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
1130{
1131 return NULL;
1132}
1133
1134static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zone **z,
1135 nodemask_t *allowednodes)
1136{
1137 return 1;
1138}
1139
1140static void zlc_mark_zone_full(struct zonelist *zonelist, struct zone **z)
1141{
1142}
1143#endif /* CONFIG_NUMA */
1144
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001145/*
Paul Jackson0798e512006-12-06 20:31:38 -08001146 * get_page_from_freelist goes through the zonelist trying to allocate
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001147 * a page.
1148 */
1149static struct page *
1150get_page_from_freelist(gfp_t gfp_mask, unsigned int order,
1151 struct zonelist *zonelist, int alloc_flags)
Martin Hicks753ee722005-06-21 17:14:41 -07001152{
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001153 struct zone **z;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001154 struct page *page = NULL;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001155 int classzone_idx = zone_idx(zonelist->zones[0]);
Christoph Lameter1192d522006-09-25 23:31:45 -07001156 struct zone *zone;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001157 nodemask_t *allowednodes = NULL;/* zonelist_cache approximation */
1158 int zlc_active = 0; /* set if using zonelist_cache */
1159 int did_zlc_setup = 0; /* just call zlc_setup() one time */
Mel Gormanb377fd32007-08-22 14:02:05 -07001160 enum zone_type highest_zoneidx = -1; /* Gets set for policy zonelists */
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001161
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001162zonelist_scan:
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001163 /*
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001164 * Scan zonelist, looking for a zone with enough free.
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001165 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
1166 */
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001167 z = zonelist->zones;
1168
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001169 do {
Mel Gormanb377fd32007-08-22 14:02:05 -07001170 /*
1171 * In NUMA, this could be a policy zonelist which contains
1172 * zones that may not be allowed by the current gfp_mask.
1173 * Check the zone is allowed by the current flags
1174 */
1175 if (unlikely(alloc_should_filter_zonelist(zonelist))) {
1176 if (highest_zoneidx == -1)
1177 highest_zoneidx = gfp_zone(gfp_mask);
1178 if (zone_idx(*z) > highest_zoneidx)
1179 continue;
1180 }
1181
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001182 if (NUMA_BUILD && zlc_active &&
1183 !zlc_zone_worth_trying(zonelist, z, allowednodes))
1184 continue;
Christoph Lameter1192d522006-09-25 23:31:45 -07001185 zone = *z;
Christoph Lameter08e0f6a2006-09-27 01:50:06 -07001186 if (unlikely(NUMA_BUILD && (gfp_mask & __GFP_THISNODE) &&
Christoph Lameter1192d522006-09-25 23:31:45 -07001187 zone->zone_pgdat != zonelist->zones[0]->zone_pgdat))
Christoph Lameter9b819d22006-09-25 23:31:40 -07001188 break;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001189 if ((alloc_flags & ALLOC_CPUSET) &&
Paul Jackson02a0e532006-12-13 00:34:25 -08001190 !cpuset_zone_allowed_softwall(zone, gfp_mask))
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001191 goto try_next_zone;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001192
1193 if (!(alloc_flags & ALLOC_NO_WATERMARKS)) {
Nick Piggin31488902005-11-28 13:44:03 -08001194 unsigned long mark;
1195 if (alloc_flags & ALLOC_WMARK_MIN)
Christoph Lameter1192d522006-09-25 23:31:45 -07001196 mark = zone->pages_min;
Nick Piggin31488902005-11-28 13:44:03 -08001197 else if (alloc_flags & ALLOC_WMARK_LOW)
Christoph Lameter1192d522006-09-25 23:31:45 -07001198 mark = zone->pages_low;
Nick Piggin31488902005-11-28 13:44:03 -08001199 else
Christoph Lameter1192d522006-09-25 23:31:45 -07001200 mark = zone->pages_high;
Paul Jackson0798e512006-12-06 20:31:38 -08001201 if (!zone_watermark_ok(zone, order, mark,
1202 classzone_idx, alloc_flags)) {
Christoph Lameter9eeff232006-01-18 17:42:31 -08001203 if (!zone_reclaim_mode ||
Christoph Lameter1192d522006-09-25 23:31:45 -07001204 !zone_reclaim(zone, gfp_mask, order))
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001205 goto this_zone_full;
Paul Jackson0798e512006-12-06 20:31:38 -08001206 }
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001207 }
1208
Christoph Lameter1192d522006-09-25 23:31:45 -07001209 page = buffered_rmqueue(zonelist, zone, order, gfp_mask);
Paul Jackson0798e512006-12-06 20:31:38 -08001210 if (page)
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001211 break;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001212this_zone_full:
1213 if (NUMA_BUILD)
1214 zlc_mark_zone_full(zonelist, z);
1215try_next_zone:
1216 if (NUMA_BUILD && !did_zlc_setup) {
1217 /* we do zlc_setup after the first zone is tried */
1218 allowednodes = zlc_setup(zonelist, alloc_flags);
1219 zlc_active = 1;
1220 did_zlc_setup = 1;
1221 }
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001222 } while (*(++z) != NULL);
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001223
1224 if (unlikely(NUMA_BUILD && page == NULL && zlc_active)) {
1225 /* Disable zlc cache for second zonelist scan */
1226 zlc_active = 0;
1227 goto zonelist_scan;
1228 }
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001229 return page;
Martin Hicks753ee722005-06-21 17:14:41 -07001230}
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232/*
1233 * This is the 'heart' of the zoned buddy allocator.
1234 */
1235struct page * fastcall
Al Virodd0fc662005-10-07 07:46:04 +01001236__alloc_pages(gfp_t gfp_mask, unsigned int order,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 struct zonelist *zonelist)
1238{
Al Viro260b2362005-10-21 03:22:44 -04001239 const gfp_t wait = gfp_mask & __GFP_WAIT;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001240 struct zone **z;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 struct page *page;
1242 struct reclaim_state reclaim_state;
1243 struct task_struct *p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 int do_retry;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001245 int alloc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 int did_some_progress;
1247
1248 might_sleep_if(wait);
1249
Akinobu Mita933e3122006-12-08 02:39:45 -08001250 if (should_fail_alloc_page(gfp_mask, order))
1251 return NULL;
1252
Jens Axboe6b1de912005-11-17 21:35:02 +01001253restart:
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001254 z = zonelist->zones; /* the list of zones suitable for gfp_mask */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001256 if (unlikely(*z == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 /* Should this ever happen?? */
1258 return NULL;
1259 }
Jens Axboe6b1de912005-11-17 21:35:02 +01001260
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001261 page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, order,
Nick Piggin31488902005-11-28 13:44:03 -08001262 zonelist, ALLOC_WMARK_LOW|ALLOC_CPUSET);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001263 if (page)
1264 goto got_pg;
1265
Christoph Lameter952f3b52006-12-06 20:33:26 -08001266 /*
1267 * GFP_THISNODE (meaning __GFP_THISNODE, __GFP_NORETRY and
1268 * __GFP_NOWARN set) should not cause reclaim since the subsystem
1269 * (f.e. slab) using GFP_THISNODE may choose to trigger reclaim
1270 * using a larger set of nodes after it has established that the
1271 * allowed per node queues are empty and that nodes are
1272 * over allocated.
1273 */
1274 if (NUMA_BUILD && (gfp_mask & GFP_THISNODE) == GFP_THISNODE)
1275 goto nopage;
1276
Paul Jackson0798e512006-12-06 20:31:38 -08001277 for (z = zonelist->zones; *z; z++)
Chris Wright43b0bc02006-06-25 05:47:55 -07001278 wakeup_kswapd(*z, order);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001279
Paul Jackson9bf22292005-09-06 15:18:12 -07001280 /*
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001281 * OK, we're below the kswapd watermark and have kicked background
1282 * reclaim. Now things get more complex, so set up alloc_flags according
1283 * to how we want to proceed.
1284 *
1285 * The caller may dip into page reserves a bit more if the caller
1286 * cannot run direct reclaim, or if the caller has realtime scheduling
Paul Jackson4eac9152006-01-11 12:17:19 -08001287 * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will
1288 * set both ALLOC_HARDER (!wait) and ALLOC_HIGH (__GFP_HIGH).
Paul Jackson9bf22292005-09-06 15:18:12 -07001289 */
Nick Piggin31488902005-11-28 13:44:03 -08001290 alloc_flags = ALLOC_WMARK_MIN;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001291 if ((unlikely(rt_task(p)) && !in_interrupt()) || !wait)
1292 alloc_flags |= ALLOC_HARDER;
1293 if (gfp_mask & __GFP_HIGH)
1294 alloc_flags |= ALLOC_HIGH;
Paul Jacksonbdd804f2006-05-20 15:00:09 -07001295 if (wait)
1296 alloc_flags |= ALLOC_CPUSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 /*
1299 * Go through the zonelist again. Let __GFP_HIGH and allocations
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001300 * coming from realtime tasks go deeper into reserves.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 *
1302 * This is the last chance, in general, before the goto nopage.
1303 * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
Paul Jackson9bf22292005-09-06 15:18:12 -07001304 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 */
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001306 page = get_page_from_freelist(gfp_mask, order, zonelist, alloc_flags);
1307 if (page)
1308 goto got_pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 /* This allocation should allow future memory freeing. */
Nick Pigginb84a35b2005-05-01 08:58:36 -07001311
Kirill Korotaevb43a57b2006-12-06 20:32:27 -08001312rebalance:
Nick Pigginb84a35b2005-05-01 08:58:36 -07001313 if (((p->flags & PF_MEMALLOC) || unlikely(test_thread_flag(TIF_MEMDIE)))
1314 && !in_interrupt()) {
1315 if (!(gfp_mask & __GFP_NOMEMALLOC)) {
Kirill Korotaev885036d2005-11-13 16:06:41 -08001316nofail_alloc:
Nick Pigginb84a35b2005-05-01 08:58:36 -07001317 /* go through the zonelist yet again, ignoring mins */
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001318 page = get_page_from_freelist(gfp_mask, order,
Paul Jackson47f3a862006-01-06 00:10:32 -08001319 zonelist, ALLOC_NO_WATERMARKS);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001320 if (page)
1321 goto got_pg;
Kirill Korotaev885036d2005-11-13 16:06:41 -08001322 if (gfp_mask & __GFP_NOFAIL) {
Andrew Morton3fcfab12006-10-19 23:28:16 -07001323 congestion_wait(WRITE, HZ/50);
Kirill Korotaev885036d2005-11-13 16:06:41 -08001324 goto nofail_alloc;
1325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 }
1327 goto nopage;
1328 }
1329
1330 /* Atomic allocations - we can't balance anything */
1331 if (!wait)
1332 goto nopage;
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 cond_resched();
1335
1336 /* We now go into synchronous reclaim */
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001337 cpuset_memory_pressure_bump();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 p->flags |= PF_MEMALLOC;
1339 reclaim_state.reclaimed_slab = 0;
1340 p->reclaim_state = &reclaim_state;
1341
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001342 did_some_progress = try_to_free_pages(zonelist->zones, order, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 p->reclaim_state = NULL;
1345 p->flags &= ~PF_MEMALLOC;
1346
1347 cond_resched();
1348
1349 if (likely(did_some_progress)) {
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001350 page = get_page_from_freelist(gfp_mask, order,
1351 zonelist, alloc_flags);
1352 if (page)
1353 goto got_pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 } else if ((gfp_mask & __GFP_FS) && !(gfp_mask & __GFP_NORETRY)) {
1355 /*
1356 * Go through the zonelist yet one more time, keep
1357 * very high watermark here, this is only to catch
1358 * a parallel oom killing, we must fail if we're still
1359 * under heavy pressure.
1360 */
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001361 page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, order,
Nick Piggin31488902005-11-28 13:44:03 -08001362 zonelist, ALLOC_WMARK_HIGH|ALLOC_CPUSET);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001363 if (page)
1364 goto got_pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Mel Gormana8bbf722007-07-31 00:37:30 -07001366 /* The OOM killer will not help higher order allocs so fail */
1367 if (order > PAGE_ALLOC_COSTLY_ORDER)
1368 goto nopage;
1369
Christoph Lameter9b0f8b02006-02-20 18:27:52 -08001370 out_of_memory(zonelist, gfp_mask, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 goto restart;
1372 }
1373
1374 /*
1375 * Don't let big-order allocations loop unless the caller explicitly
1376 * requests that. Wait for some write requests to complete then retry.
1377 *
1378 * In this implementation, __GFP_REPEAT means __GFP_NOFAIL for order
1379 * <= 3, but that may not be true in other implementations.
1380 */
1381 do_retry = 0;
1382 if (!(gfp_mask & __GFP_NORETRY)) {
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001383 if ((order <= PAGE_ALLOC_COSTLY_ORDER) ||
1384 (gfp_mask & __GFP_REPEAT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 do_retry = 1;
1386 if (gfp_mask & __GFP_NOFAIL)
1387 do_retry = 1;
1388 }
1389 if (do_retry) {
Andrew Morton3fcfab12006-10-19 23:28:16 -07001390 congestion_wait(WRITE, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 goto rebalance;
1392 }
1393
1394nopage:
1395 if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) {
1396 printk(KERN_WARNING "%s: page allocation failure."
1397 " order:%d, mode:0x%x\n",
1398 p->comm, order, gfp_mask);
1399 dump_stack();
Janet Morgan578c2fd2005-06-21 17:14:56 -07001400 show_mem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402got_pg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 return page;
1404}
1405
1406EXPORT_SYMBOL(__alloc_pages);
1407
1408/*
1409 * Common helper functions.
1410 */
Al Virodd0fc662005-10-07 07:46:04 +01001411fastcall unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
1413 struct page * page;
1414 page = alloc_pages(gfp_mask, order);
1415 if (!page)
1416 return 0;
1417 return (unsigned long) page_address(page);
1418}
1419
1420EXPORT_SYMBOL(__get_free_pages);
1421
Al Virodd0fc662005-10-07 07:46:04 +01001422fastcall unsigned long get_zeroed_page(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
1424 struct page * page;
1425
1426 /*
1427 * get_zeroed_page() returns a 32-bit address, which cannot represent
1428 * a highmem page
1429 */
Nick Piggin725d7042006-09-25 23:30:55 -07001430 VM_BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 page = alloc_pages(gfp_mask | __GFP_ZERO, 0);
1433 if (page)
1434 return (unsigned long) page_address(page);
1435 return 0;
1436}
1437
1438EXPORT_SYMBOL(get_zeroed_page);
1439
1440void __pagevec_free(struct pagevec *pvec)
1441{
1442 int i = pagevec_count(pvec);
1443
1444 while (--i >= 0)
1445 free_hot_cold_page(pvec->pages[i], pvec->cold);
1446}
1447
1448fastcall void __free_pages(struct page *page, unsigned int order)
1449{
Nick Pigginb5810032005-10-29 18:16:12 -07001450 if (put_page_testzero(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 if (order == 0)
1452 free_hot_page(page);
1453 else
1454 __free_pages_ok(page, order);
1455 }
1456}
1457
1458EXPORT_SYMBOL(__free_pages);
1459
1460fastcall void free_pages(unsigned long addr, unsigned int order)
1461{
1462 if (addr != 0) {
Nick Piggin725d7042006-09-25 23:30:55 -07001463 VM_BUG_ON(!virt_addr_valid((void *)addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 __free_pages(virt_to_page((void *)addr), order);
1465 }
1466}
1467
1468EXPORT_SYMBOL(free_pages);
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470static unsigned int nr_free_zone_pages(int offset)
1471{
Martin J. Blighe310fd42005-07-29 22:59:18 -07001472 /* Just pick one node, since fallback list is circular */
1473 pg_data_t *pgdat = NODE_DATA(numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 unsigned int sum = 0;
1475
Martin J. Blighe310fd42005-07-29 22:59:18 -07001476 struct zonelist *zonelist = pgdat->node_zonelists + offset;
1477 struct zone **zonep = zonelist->zones;
1478 struct zone *zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Martin J. Blighe310fd42005-07-29 22:59:18 -07001480 for (zone = *zonep++; zone; zone = *zonep++) {
1481 unsigned long size = zone->present_pages;
1482 unsigned long high = zone->pages_high;
1483 if (size > high)
1484 sum += size - high;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
1486
1487 return sum;
1488}
1489
1490/*
1491 * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
1492 */
1493unsigned int nr_free_buffer_pages(void)
1494{
Al Viroaf4ca452005-10-21 02:55:38 -04001495 return nr_free_zone_pages(gfp_zone(GFP_USER));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496}
Meelap Shahc2f1a552007-07-17 04:04:39 -07001497EXPORT_SYMBOL_GPL(nr_free_buffer_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499/*
1500 * Amount of free RAM allocatable within all zones
1501 */
1502unsigned int nr_free_pagecache_pages(void)
1503{
Mel Gorman2a1e2742007-07-17 04:03:12 -07001504 return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER_MOVABLE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505}
Christoph Lameter08e0f6a2006-09-27 01:50:06 -07001506
1507static inline void show_node(struct zone *zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
Christoph Lameter08e0f6a2006-09-27 01:50:06 -07001509 if (NUMA_BUILD)
Andy Whitcroft25ba77c2006-12-06 20:33:03 -08001510 printk("Node %d ", zone_to_nid(zone));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513void si_meminfo(struct sysinfo *val)
1514{
1515 val->totalram = totalram_pages;
1516 val->sharedram = 0;
Christoph Lameterd23ad422007-02-10 01:43:02 -08001517 val->freeram = global_page_state(NR_FREE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 val->bufferram = nr_blockdev_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 val->totalhigh = totalhigh_pages;
1520 val->freehigh = nr_free_highpages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 val->mem_unit = PAGE_SIZE;
1522}
1523
1524EXPORT_SYMBOL(si_meminfo);
1525
1526#ifdef CONFIG_NUMA
1527void si_meminfo_node(struct sysinfo *val, int nid)
1528{
1529 pg_data_t *pgdat = NODE_DATA(nid);
1530
1531 val->totalram = pgdat->node_present_pages;
Christoph Lameterd23ad422007-02-10 01:43:02 -08001532 val->freeram = node_page_state(nid, NR_FREE_PAGES);
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07001533#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].present_pages;
Christoph Lameterd23ad422007-02-10 01:43:02 -08001535 val->freehigh = zone_page_state(&pgdat->node_zones[ZONE_HIGHMEM],
1536 NR_FREE_PAGES);
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07001537#else
1538 val->totalhigh = 0;
1539 val->freehigh = 0;
1540#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 val->mem_unit = PAGE_SIZE;
1542}
1543#endif
1544
1545#define K(x) ((x) << (PAGE_SHIFT-10))
1546
1547/*
1548 * Show free area list (used inside shift_scroll-lock stuff)
1549 * We also calculate the percentage fragmentation. We do this by counting the
1550 * memory on each free list with the exception of the first item on the list.
1551 */
1552void show_free_areas(void)
1553{
Jes Sorensenc7241912006-09-27 01:50:05 -07001554 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 struct zone *zone;
1556
1557 for_each_zone(zone) {
Jes Sorensenc7241912006-09-27 01:50:05 -07001558 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 continue;
Jes Sorensenc7241912006-09-27 01:50:05 -07001560
1561 show_node(zone);
1562 printk("%s per-cpu:\n", zone->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Dave Jones6b482c62005-11-10 15:45:56 -05001564 for_each_online_cpu(cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 struct per_cpu_pageset *pageset;
1566
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001567 pageset = zone_pcp(zone, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Jes Sorensenc7241912006-09-27 01:50:05 -07001569 printk("CPU %4d: Hot: hi:%5d, btch:%4d usd:%4d "
1570 "Cold: hi:%5d, btch:%4d usd:%4d\n",
1571 cpu, pageset->pcp[0].high,
1572 pageset->pcp[0].batch, pageset->pcp[0].count,
1573 pageset->pcp[1].high, pageset->pcp[1].batch,
1574 pageset->pcp[1].count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 }
1576 }
1577
Andrew Mortona25700a2007-02-08 14:20:40 -08001578 printk("Active:%lu inactive:%lu dirty:%lu writeback:%lu unstable:%lu\n"
Christoph Lameterd23ad422007-02-10 01:43:02 -08001579 " free:%lu slab:%lu mapped:%lu pagetables:%lu bounce:%lu\n",
Christoph Lameter65e458d42007-02-10 01:43:05 -08001580 global_page_state(NR_ACTIVE),
1581 global_page_state(NR_INACTIVE),
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -07001582 global_page_state(NR_FILE_DIRTY),
Christoph Lameterce866b32006-06-30 01:55:40 -07001583 global_page_state(NR_WRITEBACK),
Christoph Lameterfd39fc82006-06-30 01:55:40 -07001584 global_page_state(NR_UNSTABLE_NFS),
Christoph Lameterd23ad422007-02-10 01:43:02 -08001585 global_page_state(NR_FREE_PAGES),
Christoph Lameter972d1a72006-09-25 23:31:51 -07001586 global_page_state(NR_SLAB_RECLAIMABLE) +
1587 global_page_state(NR_SLAB_UNRECLAIMABLE),
Christoph Lameter65ba55f2006-06-30 01:55:34 -07001588 global_page_state(NR_FILE_MAPPED),
Andrew Mortona25700a2007-02-08 14:20:40 -08001589 global_page_state(NR_PAGETABLE),
1590 global_page_state(NR_BOUNCE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 for_each_zone(zone) {
1593 int i;
1594
Jes Sorensenc7241912006-09-27 01:50:05 -07001595 if (!populated_zone(zone))
1596 continue;
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 show_node(zone);
1599 printk("%s"
1600 " free:%lukB"
1601 " min:%lukB"
1602 " low:%lukB"
1603 " high:%lukB"
1604 " active:%lukB"
1605 " inactive:%lukB"
1606 " present:%lukB"
1607 " pages_scanned:%lu"
1608 " all_unreclaimable? %s"
1609 "\n",
1610 zone->name,
Christoph Lameterd23ad422007-02-10 01:43:02 -08001611 K(zone_page_state(zone, NR_FREE_PAGES)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 K(zone->pages_min),
1613 K(zone->pages_low),
1614 K(zone->pages_high),
Christoph Lameterc8785382007-02-10 01:43:01 -08001615 K(zone_page_state(zone, NR_ACTIVE)),
1616 K(zone_page_state(zone, NR_INACTIVE)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 K(zone->present_pages),
1618 zone->pages_scanned,
1619 (zone->all_unreclaimable ? "yes" : "no")
1620 );
1621 printk("lowmem_reserve[]:");
1622 for (i = 0; i < MAX_NR_ZONES; i++)
1623 printk(" %lu", zone->lowmem_reserve[i]);
1624 printk("\n");
1625 }
1626
1627 for_each_zone(zone) {
Kirill Korotaev8f9de512006-06-23 02:03:50 -07001628 unsigned long nr[MAX_ORDER], flags, order, total = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Jes Sorensenc7241912006-09-27 01:50:05 -07001630 if (!populated_zone(zone))
1631 continue;
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 show_node(zone);
1634 printk("%s: ", zone->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
1636 spin_lock_irqsave(&zone->lock, flags);
1637 for (order = 0; order < MAX_ORDER; order++) {
Kirill Korotaev8f9de512006-06-23 02:03:50 -07001638 nr[order] = zone->free_area[order].nr_free;
1639 total += nr[order] << order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 }
1641 spin_unlock_irqrestore(&zone->lock, flags);
Kirill Korotaev8f9de512006-06-23 02:03:50 -07001642 for (order = 0; order < MAX_ORDER; order++)
1643 printk("%lu*%lukB ", nr[order], K(1UL) << order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 printk("= %lukB\n", K(total));
1645 }
1646
1647 show_swap_cache_info();
1648}
1649
1650/*
1651 * Builds allocation fallback zone lists.
Christoph Lameter1a932052006-01-06 00:11:16 -08001652 *
1653 * Add all populated zones of a node to the zonelist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07001655static int build_zonelists_node(pg_data_t *pgdat, struct zonelist *zonelist,
1656 int nr_zones, enum zone_type zone_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
Christoph Lameter1a932052006-01-06 00:11:16 -08001658 struct zone *zone;
1659
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07001660 BUG_ON(zone_type >= MAX_NR_ZONES);
Christoph Lameter2f6726e2006-09-25 23:31:18 -07001661 zone_type++;
Christoph Lameter02a68a52006-01-06 00:11:18 -08001662
1663 do {
Christoph Lameter2f6726e2006-09-25 23:31:18 -07001664 zone_type--;
Christoph Lameter070f8032006-01-06 00:11:19 -08001665 zone = pgdat->node_zones + zone_type;
Christoph Lameter1a932052006-01-06 00:11:16 -08001666 if (populated_zone(zone)) {
Christoph Lameter070f8032006-01-06 00:11:19 -08001667 zonelist->zones[nr_zones++] = zone;
1668 check_highest_zone(zone_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 }
Christoph Lameter02a68a52006-01-06 00:11:18 -08001670
Christoph Lameter2f6726e2006-09-25 23:31:18 -07001671 } while (zone_type);
Christoph Lameter070f8032006-01-06 00:11:19 -08001672 return nr_zones;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673}
1674
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07001675
1676/*
1677 * zonelist_order:
1678 * 0 = automatic detection of better ordering.
1679 * 1 = order by ([node] distance, -zonetype)
1680 * 2 = order by (-zonetype, [node] distance)
1681 *
1682 * If not NUMA, ZONELIST_ORDER_ZONE and ZONELIST_ORDER_NODE will create
1683 * the same zonelist. So only NUMA can configure this param.
1684 */
1685#define ZONELIST_ORDER_DEFAULT 0
1686#define ZONELIST_ORDER_NODE 1
1687#define ZONELIST_ORDER_ZONE 2
1688
1689/* zonelist order in the kernel.
1690 * set_zonelist_order() will set this to NODE or ZONE.
1691 */
1692static int current_zonelist_order = ZONELIST_ORDER_DEFAULT;
1693static char zonelist_order_name[3][8] = {"Default", "Node", "Zone"};
1694
1695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696#ifdef CONFIG_NUMA
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07001697/* The value user specified ....changed by config */
1698static int user_zonelist_order = ZONELIST_ORDER_DEFAULT;
1699/* string for sysctl */
1700#define NUMA_ZONELIST_ORDER_LEN 16
1701char numa_zonelist_order[16] = "default";
1702
1703/*
1704 * interface for configure zonelist ordering.
1705 * command line option "numa_zonelist_order"
1706 * = "[dD]efault - default, automatic configuration.
1707 * = "[nN]ode - order by node locality, then by zone within node
1708 * = "[zZ]one - order by zone, then by locality within zone
1709 */
1710
1711static int __parse_numa_zonelist_order(char *s)
1712{
1713 if (*s == 'd' || *s == 'D') {
1714 user_zonelist_order = ZONELIST_ORDER_DEFAULT;
1715 } else if (*s == 'n' || *s == 'N') {
1716 user_zonelist_order = ZONELIST_ORDER_NODE;
1717 } else if (*s == 'z' || *s == 'Z') {
1718 user_zonelist_order = ZONELIST_ORDER_ZONE;
1719 } else {
1720 printk(KERN_WARNING
1721 "Ignoring invalid numa_zonelist_order value: "
1722 "%s\n", s);
1723 return -EINVAL;
1724 }
1725 return 0;
1726}
1727
1728static __init int setup_numa_zonelist_order(char *s)
1729{
1730 if (s)
1731 return __parse_numa_zonelist_order(s);
1732 return 0;
1733}
1734early_param("numa_zonelist_order", setup_numa_zonelist_order);
1735
1736/*
1737 * sysctl handler for numa_zonelist_order
1738 */
1739int numa_zonelist_order_handler(ctl_table *table, int write,
1740 struct file *file, void __user *buffer, size_t *length,
1741 loff_t *ppos)
1742{
1743 char saved_string[NUMA_ZONELIST_ORDER_LEN];
1744 int ret;
1745
1746 if (write)
1747 strncpy(saved_string, (char*)table->data,
1748 NUMA_ZONELIST_ORDER_LEN);
1749 ret = proc_dostring(table, write, file, buffer, length, ppos);
1750 if (ret)
1751 return ret;
1752 if (write) {
1753 int oldval = user_zonelist_order;
1754 if (__parse_numa_zonelist_order((char*)table->data)) {
1755 /*
1756 * bogus value. restore saved string
1757 */
1758 strncpy((char*)table->data, saved_string,
1759 NUMA_ZONELIST_ORDER_LEN);
1760 user_zonelist_order = oldval;
1761 } else if (oldval != user_zonelist_order)
1762 build_all_zonelists();
1763 }
1764 return 0;
1765}
1766
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768#define MAX_NODE_LOAD (num_online_nodes())
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07001769static int node_load[MAX_NUMNODES];
1770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771/**
Pavel Pisa4dc3b162005-05-01 08:59:25 -07001772 * 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 -07001773 * @node: node whose fallback list we're appending
1774 * @used_node_mask: nodemask_t of already used nodes
1775 *
1776 * We use a number of factors to determine which is the next node that should
1777 * appear on a given node's fallback list. The node should not have appeared
1778 * already in @node's fallback list, and it should be the next closest node
1779 * according to the distance array (which contains arbitrary distance values
1780 * from each node to each node in the system), and should also prefer nodes
1781 * with no CPUs, since presumably they'll have very little allocation pressure
1782 * on them otherwise.
1783 * It returns -1 if no node is found.
1784 */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07001785static int find_next_best_node(int node, nodemask_t *used_node_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
Linus Torvalds4cf808eb2006-02-17 20:38:21 +01001787 int n, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 int min_val = INT_MAX;
1789 int best_node = -1;
1790
Linus Torvalds4cf808eb2006-02-17 20:38:21 +01001791 /* Use the local node if we haven't already */
1792 if (!node_isset(node, *used_node_mask)) {
1793 node_set(node, *used_node_mask);
1794 return node;
1795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Linus Torvalds4cf808eb2006-02-17 20:38:21 +01001797 for_each_online_node(n) {
1798 cpumask_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800 /* Don't want a node to appear more than once */
1801 if (node_isset(n, *used_node_mask))
1802 continue;
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 /* Use the distance array to find the distance */
1805 val = node_distance(node, n);
1806
Linus Torvalds4cf808eb2006-02-17 20:38:21 +01001807 /* Penalize nodes under us ("prefer the next node") */
1808 val += (n < node);
1809
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 /* Give preference to headless and unused nodes */
1811 tmp = node_to_cpumask(n);
1812 if (!cpus_empty(tmp))
1813 val += PENALTY_FOR_NODE_WITH_CPUS;
1814
1815 /* Slight preference for less loaded node */
1816 val *= (MAX_NODE_LOAD*MAX_NUMNODES);
1817 val += node_load[n];
1818
1819 if (val < min_val) {
1820 min_val = val;
1821 best_node = n;
1822 }
1823 }
1824
1825 if (best_node >= 0)
1826 node_set(best_node, *used_node_mask);
1827
1828 return best_node;
1829}
1830
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07001831
1832/*
1833 * Build zonelists ordered by node and zones within node.
1834 * This results in maximum locality--normal zone overflows into local
1835 * DMA zone, if any--but risks exhausting DMA zone.
1836 */
1837static void build_zonelists_in_node_order(pg_data_t *pgdat, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
Christoph Lameter19655d32006-09-25 23:31:19 -07001839 enum zone_type i;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07001840 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 struct zonelist *zonelist;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07001842
1843 for (i = 0; i < MAX_NR_ZONES; i++) {
1844 zonelist = pgdat->node_zonelists + i;
1845 for (j = 0; zonelist->zones[j] != NULL; j++)
1846 ;
1847 j = build_zonelists_node(NODE_DATA(node), zonelist, j, i);
1848 zonelist->zones[j] = NULL;
1849 }
1850}
1851
1852/*
1853 * Build zonelists ordered by zone and nodes within zones.
1854 * This results in conserving DMA zone[s] until all Normal memory is
1855 * exhausted, but results in overflowing to remote node while memory
1856 * may still exist in local DMA zone.
1857 */
1858static int node_order[MAX_NUMNODES];
1859
1860static void build_zonelists_in_zone_order(pg_data_t *pgdat, int nr_nodes)
1861{
1862 enum zone_type i;
1863 int pos, j, node;
1864 int zone_type; /* needs to be signed */
1865 struct zone *z;
1866 struct zonelist *zonelist;
1867
1868 for (i = 0; i < MAX_NR_ZONES; i++) {
1869 zonelist = pgdat->node_zonelists + i;
1870 pos = 0;
1871 for (zone_type = i; zone_type >= 0; zone_type--) {
1872 for (j = 0; j < nr_nodes; j++) {
1873 node = node_order[j];
1874 z = &NODE_DATA(node)->node_zones[zone_type];
1875 if (populated_zone(z)) {
1876 zonelist->zones[pos++] = z;
1877 check_highest_zone(zone_type);
1878 }
1879 }
1880 }
1881 zonelist->zones[pos] = NULL;
1882 }
1883}
1884
1885static int default_zonelist_order(void)
1886{
1887 int nid, zone_type;
1888 unsigned long low_kmem_size,total_size;
1889 struct zone *z;
1890 int average_size;
1891 /*
1892 * ZONE_DMA and ZONE_DMA32 can be very small area in the sytem.
1893 * If they are really small and used heavily, the system can fall
1894 * into OOM very easily.
1895 * This function detect ZONE_DMA/DMA32 size and confgigures zone order.
1896 */
1897 /* Is there ZONE_NORMAL ? (ex. ppc has only DMA zone..) */
1898 low_kmem_size = 0;
1899 total_size = 0;
1900 for_each_online_node(nid) {
1901 for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
1902 z = &NODE_DATA(nid)->node_zones[zone_type];
1903 if (populated_zone(z)) {
1904 if (zone_type < ZONE_NORMAL)
1905 low_kmem_size += z->present_pages;
1906 total_size += z->present_pages;
1907 }
1908 }
1909 }
1910 if (!low_kmem_size || /* there are no DMA area. */
1911 low_kmem_size > total_size/2) /* DMA/DMA32 is big. */
1912 return ZONELIST_ORDER_NODE;
1913 /*
1914 * look into each node's config.
1915 * If there is a node whose DMA/DMA32 memory is very big area on
1916 * local memory, NODE_ORDER may be suitable.
1917 */
1918 average_size = total_size / (num_online_nodes() + 1);
1919 for_each_online_node(nid) {
1920 low_kmem_size = 0;
1921 total_size = 0;
1922 for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
1923 z = &NODE_DATA(nid)->node_zones[zone_type];
1924 if (populated_zone(z)) {
1925 if (zone_type < ZONE_NORMAL)
1926 low_kmem_size += z->present_pages;
1927 total_size += z->present_pages;
1928 }
1929 }
1930 if (low_kmem_size &&
1931 total_size > average_size && /* ignore small node */
1932 low_kmem_size > total_size * 70/100)
1933 return ZONELIST_ORDER_NODE;
1934 }
1935 return ZONELIST_ORDER_ZONE;
1936}
1937
1938static void set_zonelist_order(void)
1939{
1940 if (user_zonelist_order == ZONELIST_ORDER_DEFAULT)
1941 current_zonelist_order = default_zonelist_order();
1942 else
1943 current_zonelist_order = user_zonelist_order;
1944}
1945
1946static void build_zonelists(pg_data_t *pgdat)
1947{
1948 int j, node, load;
1949 enum zone_type i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 nodemask_t used_mask;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07001951 int local_node, prev_node;
1952 struct zonelist *zonelist;
1953 int order = current_zonelist_order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 /* initialize zonelists */
Christoph Lameter19655d32006-09-25 23:31:19 -07001956 for (i = 0; i < MAX_NR_ZONES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 zonelist = pgdat->node_zonelists + i;
1958 zonelist->zones[0] = NULL;
1959 }
1960
1961 /* NUMA-aware ordering of nodes */
1962 local_node = pgdat->node_id;
1963 load = num_online_nodes();
1964 prev_node = local_node;
1965 nodes_clear(used_mask);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07001966
1967 memset(node_load, 0, sizeof(node_load));
1968 memset(node_order, 0, sizeof(node_order));
1969 j = 0;
1970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
Christoph Lameter9eeff232006-01-18 17:42:31 -08001972 int distance = node_distance(local_node, node);
1973
1974 /*
1975 * If another node is sufficiently far away then it is better
1976 * to reclaim pages in a zone before going off node.
1977 */
1978 if (distance > RECLAIM_DISTANCE)
1979 zone_reclaim_mode = 1;
1980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 /*
1982 * We don't want to pressure a particular node.
1983 * So adding penalty to the first node in same
1984 * distance group to make it round-robin.
1985 */
Christoph Lameter9eeff232006-01-18 17:42:31 -08001986 if (distance != node_distance(local_node, prev_node))
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07001987 node_load[node] = load;
1988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 prev_node = node;
1990 load--;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07001991 if (order == ZONELIST_ORDER_NODE)
1992 build_zonelists_in_node_order(pgdat, node);
1993 else
1994 node_order[j++] = node; /* remember order */
1995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07001997 if (order == ZONELIST_ORDER_ZONE) {
1998 /* calculate node order -- i.e., DMA last! */
1999 build_zonelists_in_zone_order(pgdat, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 }
2001}
2002
Paul Jackson9276b1bc2006-12-06 20:31:48 -08002003/* Construct the zonelist performance cache - see further mmzone.h */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002004static void build_zonelist_cache(pg_data_t *pgdat)
Paul Jackson9276b1bc2006-12-06 20:31:48 -08002005{
2006 int i;
2007
2008 for (i = 0; i < MAX_NR_ZONES; i++) {
2009 struct zonelist *zonelist;
2010 struct zonelist_cache *zlc;
2011 struct zone **z;
2012
2013 zonelist = pgdat->node_zonelists + i;
2014 zonelist->zlcache_ptr = zlc = &zonelist->zlcache;
2015 bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
2016 for (z = zonelist->zones; *z; z++)
2017 zlc->z_to_n[z - zonelist->zones] = zone_to_nid(*z);
2018 }
2019}
2020
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022#else /* CONFIG_NUMA */
2023
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002024static void set_zonelist_order(void)
2025{
2026 current_zonelist_order = ZONELIST_ORDER_ZONE;
2027}
2028
2029static void build_zonelists(pg_data_t *pgdat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
Christoph Lameter19655d32006-09-25 23:31:19 -07002031 int node, local_node;
2032 enum zone_type i,j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
2034 local_node = pgdat->node_id;
Christoph Lameter19655d32006-09-25 23:31:19 -07002035 for (i = 0; i < MAX_NR_ZONES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 struct zonelist *zonelist;
2037
2038 zonelist = pgdat->node_zonelists + i;
2039
Christoph Lameter19655d32006-09-25 23:31:19 -07002040 j = build_zonelists_node(pgdat, zonelist, 0, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 /*
2042 * Now we build the zonelist so that it contains the zones
2043 * of all the other nodes.
2044 * We don't want to pressure a particular node, so when
2045 * building the zones for node N, we make sure that the
2046 * zones coming right after the local ones are those from
2047 * node N+1 (modulo N)
2048 */
2049 for (node = local_node + 1; node < MAX_NUMNODES; node++) {
2050 if (!node_online(node))
2051 continue;
Christoph Lameter19655d32006-09-25 23:31:19 -07002052 j = build_zonelists_node(NODE_DATA(node), zonelist, j, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 }
2054 for (node = 0; node < local_node; node++) {
2055 if (!node_online(node))
2056 continue;
Christoph Lameter19655d32006-09-25 23:31:19 -07002057 j = build_zonelists_node(NODE_DATA(node), zonelist, j, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 }
2059
2060 zonelist->zones[j] = NULL;
2061 }
2062}
2063
Paul Jackson9276b1bc2006-12-06 20:31:48 -08002064/* non-NUMA variant of zonelist performance cache - just NULL zlcache_ptr */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002065static void build_zonelist_cache(pg_data_t *pgdat)
Paul Jackson9276b1bc2006-12-06 20:31:48 -08002066{
2067 int i;
2068
2069 for (i = 0; i < MAX_NR_ZONES; i++)
2070 pgdat->node_zonelists[i].zlcache_ptr = NULL;
2071}
2072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073#endif /* CONFIG_NUMA */
2074
Yasunori Goto68113782006-06-23 02:03:11 -07002075/* return values int ....just for stop_machine_run() */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002076static int __build_all_zonelists(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
Yasunori Goto68113782006-06-23 02:03:11 -07002078 int nid;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08002079
2080 for_each_online_node(nid) {
Yasunori Goto68113782006-06-23 02:03:11 -07002081 build_zonelists(NODE_DATA(nid));
Paul Jackson9276b1bc2006-12-06 20:31:48 -08002082 build_zonelist_cache(NODE_DATA(nid));
2083 }
Yasunori Goto68113782006-06-23 02:03:11 -07002084 return 0;
2085}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002087void build_all_zonelists(void)
Yasunori Goto68113782006-06-23 02:03:11 -07002088{
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002089 set_zonelist_order();
2090
Yasunori Goto68113782006-06-23 02:03:11 -07002091 if (system_state == SYSTEM_BOOTING) {
Randy Dunlap423b41d2006-09-27 01:50:12 -07002092 __build_all_zonelists(NULL);
Yasunori Goto68113782006-06-23 02:03:11 -07002093 cpuset_init_current_mems_allowed();
2094 } else {
2095 /* we have to stop all cpus to guaranntee there is no user
2096 of zonelist */
2097 stop_machine_run(__build_all_zonelists, NULL, NR_CPUS);
2098 /* cpuset refresh routine should be here */
2099 }
Andrew Mortonbd1e22b2006-06-23 02:03:47 -07002100 vm_total_pages = nr_free_pagecache_pages();
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002101 printk("Built %i zonelists in %s order. Total pages: %ld\n",
2102 num_online_nodes(),
2103 zonelist_order_name[current_zonelist_order],
2104 vm_total_pages);
2105#ifdef CONFIG_NUMA
2106 printk("Policy zone: %s\n", zone_names[policy_zone]);
2107#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108}
2109
2110/*
2111 * Helper functions to size the waitqueue hash table.
2112 * Essentially these want to choose hash table sizes sufficiently
2113 * large so that collisions trying to wait on pages are rare.
2114 * But in fact, the number of active page waitqueues on typical
2115 * systems is ridiculously low, less than 200. So this is even
2116 * conservative, even though it seems large.
2117 *
2118 * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
2119 * waitqueues, i.e. the size of the waitq table given the number of pages.
2120 */
2121#define PAGES_PER_WAITQUEUE 256
2122
Yasunori Gotocca448f2006-06-23 02:03:10 -07002123#ifndef CONFIG_MEMORY_HOTPLUG
Yasunori Goto02b694d2006-06-23 02:03:08 -07002124static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
2126 unsigned long size = 1;
2127
2128 pages /= PAGES_PER_WAITQUEUE;
2129
2130 while (size < pages)
2131 size <<= 1;
2132
2133 /*
2134 * Once we have dozens or even hundreds of threads sleeping
2135 * on IO we've got bigger problems than wait queue collision.
2136 * Limit the size of the wait table to a reasonable size.
2137 */
2138 size = min(size, 4096UL);
2139
2140 return max(size, 4UL);
2141}
Yasunori Gotocca448f2006-06-23 02:03:10 -07002142#else
2143/*
2144 * A zone's size might be changed by hot-add, so it is not possible to determine
2145 * a suitable size for its wait_table. So we use the maximum size now.
2146 *
2147 * The max wait table size = 4096 x sizeof(wait_queue_head_t). ie:
2148 *
2149 * i386 (preemption config) : 4096 x 16 = 64Kbyte.
2150 * ia64, x86-64 (no preemption): 4096 x 20 = 80Kbyte.
2151 * ia64, x86-64 (preemption) : 4096 x 24 = 96Kbyte.
2152 *
2153 * The maximum entries are prepared when a zone's memory is (512K + 256) pages
2154 * or more by the traditional way. (See above). It equals:
2155 *
2156 * i386, x86-64, powerpc(4K page size) : = ( 2G + 1M)byte.
2157 * ia64(16K page size) : = ( 8G + 4M)byte.
2158 * powerpc (64K page size) : = (32G +16M)byte.
2159 */
2160static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
2161{
2162 return 4096UL;
2163}
2164#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
2166/*
2167 * This is an integer logarithm so that shifts can be used later
2168 * to extract the more random high bits from the multiplicative
2169 * hash function before the remainder is taken.
2170 */
2171static inline unsigned long wait_table_bits(unsigned long size)
2172{
2173 return ffz(~size);
2174}
2175
2176#define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
2177
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178/*
2179 * Initially all pages are reserved - free ones are freed
2180 * up by free_all_bootmem() once the early boot process is
2181 * done. Non-atomic initialization, single-pass.
2182 */
Matt Tolentinoc09b4242006-01-17 07:03:44 +01002183void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
Dave Hansena2f3aa022007-01-10 23:15:30 -08002184 unsigned long start_pfn, enum memmap_context context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 struct page *page;
Andy Whitcroft29751f62005-06-23 00:08:00 -07002187 unsigned long end_pfn = start_pfn + size;
2188 unsigned long pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Greg Ungerercbe8dd42006-01-12 01:05:24 -08002190 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
Dave Hansena2f3aa022007-01-10 23:15:30 -08002191 /*
2192 * There can be holes in boot-time mem_map[]s
2193 * handed to this function. They do not
2194 * exist on hotplugged memory.
2195 */
2196 if (context == MEMMAP_EARLY) {
2197 if (!early_pfn_valid(pfn))
2198 continue;
2199 if (!early_pfn_in_nid(pfn, nid))
2200 continue;
2201 }
Andy Whitcroftd41dee32005-06-23 00:07:54 -07002202 page = pfn_to_page(pfn);
2203 set_page_links(page, zone, nid, pfn);
Nick Piggin7835e982006-03-22 00:08:40 -08002204 init_page_count(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 reset_page_mapcount(page);
2206 SetPageReserved(page);
2207 INIT_LIST_HEAD(&page->lru);
2208#ifdef WANT_PAGE_VIRTUAL
2209 /* The shift won't overflow because ZONE_NORMAL is below 4G. */
2210 if (!is_highmem_idx(zone))
Bob Picco3212c6b2005-06-27 14:36:28 -07002211 set_page_address(page, __va(pfn << PAGE_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 }
2214}
2215
Paul Mundt6ea6e682007-07-15 23:38:20 -07002216static void __meminit zone_init_free_lists(struct pglist_data *pgdat,
2217 struct zone *zone, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
2219 int order;
2220 for (order = 0; order < MAX_ORDER ; order++) {
2221 INIT_LIST_HEAD(&zone->free_area[order].free_list);
2222 zone->free_area[order].nr_free = 0;
2223 }
2224}
2225
2226#ifndef __HAVE_ARCH_MEMMAP_INIT
2227#define memmap_init(size, nid, zone, start_pfn) \
Dave Hansena2f3aa022007-01-10 23:15:30 -08002228 memmap_init_zone((size), (nid), (zone), (start_pfn), MEMMAP_EARLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229#endif
2230
Paul Mundtd09c6b82007-06-14 15:13:16 +09002231static int __devinit zone_batchsize(struct zone *zone)
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002232{
2233 int batch;
2234
2235 /*
2236 * The per-cpu-pages pools are set to around 1000th of the
Seth, Rohitba56e912005-10-29 18:15:47 -07002237 * size of the zone. But no more than 1/2 of a meg.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002238 *
2239 * OK, so we don't know how big the cache is. So guess.
2240 */
2241 batch = zone->present_pages / 1024;
Seth, Rohitba56e912005-10-29 18:15:47 -07002242 if (batch * PAGE_SIZE > 512 * 1024)
2243 batch = (512 * 1024) / PAGE_SIZE;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002244 batch /= 4; /* We effectively *= 4 below */
2245 if (batch < 1)
2246 batch = 1;
2247
2248 /*
Nick Piggin0ceaacc2005-12-04 13:55:25 +11002249 * Clamp the batch to a 2^n - 1 value. Having a power
2250 * of 2 value was found to be more likely to have
2251 * suboptimal cache aliasing properties in some cases.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002252 *
Nick Piggin0ceaacc2005-12-04 13:55:25 +11002253 * For example if 2 tasks are alternately allocating
2254 * batches of pages, one task can end up with a lot
2255 * of pages of one half of the possible page colors
2256 * and the other with pages of the other colors.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002257 */
Nick Piggin0ceaacc2005-12-04 13:55:25 +11002258 batch = (1 << (fls(batch + batch/2)-1)) - 1;
Seth, Rohitba56e912005-10-29 18:15:47 -07002259
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002260 return batch;
2261}
2262
Christoph Lameter2caaad42005-06-21 17:15:00 -07002263inline void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
2264{
2265 struct per_cpu_pages *pcp;
2266
Magnus Damm1c6fe942005-10-26 01:58:59 -07002267 memset(p, 0, sizeof(*p));
2268
Christoph Lameter2caaad42005-06-21 17:15:00 -07002269 pcp = &p->pcp[0]; /* hot */
2270 pcp->count = 0;
Christoph Lameter2caaad42005-06-21 17:15:00 -07002271 pcp->high = 6 * batch;
2272 pcp->batch = max(1UL, 1 * batch);
2273 INIT_LIST_HEAD(&pcp->list);
2274
2275 pcp = &p->pcp[1]; /* cold*/
2276 pcp->count = 0;
Christoph Lameter2caaad42005-06-21 17:15:00 -07002277 pcp->high = 2 * batch;
Seth, Rohite46a5e22005-10-29 18:15:48 -07002278 pcp->batch = max(1UL, batch/2);
Christoph Lameter2caaad42005-06-21 17:15:00 -07002279 INIT_LIST_HEAD(&pcp->list);
2280}
2281
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08002282/*
2283 * setup_pagelist_highmark() sets the high water mark for hot per_cpu_pagelist
2284 * to the value high for the pageset p.
2285 */
2286
2287static void setup_pagelist_highmark(struct per_cpu_pageset *p,
2288 unsigned long high)
2289{
2290 struct per_cpu_pages *pcp;
2291
2292 pcp = &p->pcp[0]; /* hot list */
2293 pcp->high = high;
2294 pcp->batch = max(1UL, high/4);
2295 if ((high/4) > (PAGE_SHIFT * 8))
2296 pcp->batch = PAGE_SHIFT * 8;
2297}
2298
2299
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002300#ifdef CONFIG_NUMA
2301/*
Christoph Lameter2caaad42005-06-21 17:15:00 -07002302 * Boot pageset table. One per cpu which is going to be used for all
2303 * zones and all nodes. The parameters will be set in such a way
2304 * that an item put on a list will immediately be handed over to
2305 * the buddy list. This is safe since pageset manipulation is done
2306 * with interrupts disabled.
2307 *
2308 * Some NUMA counter updates may also be caught by the boot pagesets.
Christoph Lameterb7c84c62005-06-22 20:26:07 -07002309 *
2310 * The boot_pagesets must be kept even after bootup is complete for
2311 * unused processors and/or zones. They do play a role for bootstrapping
2312 * hotplugged processors.
2313 *
2314 * zoneinfo_show() and maybe other functions do
2315 * not check if the processor is online before following the pageset pointer.
2316 * Other parts of the kernel may not check if the zone is available.
Christoph Lameter2caaad42005-06-21 17:15:00 -07002317 */
Eric Dumazet88a2a4ac2006-02-04 23:27:36 -08002318static struct per_cpu_pageset boot_pageset[NR_CPUS];
Christoph Lameter2caaad42005-06-21 17:15:00 -07002319
2320/*
2321 * Dynamically allocate memory for the
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002322 * per cpu pageset array in struct zone.
2323 */
Ashok Raj6292d9a2006-02-01 03:04:44 -08002324static int __cpuinit process_zones(int cpu)
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002325{
2326 struct zone *zone, *dzone;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002327
2328 for_each_zone(zone) {
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002329
Christoph Lameter66a55032006-09-27 01:50:09 -07002330 if (!populated_zone(zone))
2331 continue;
2332
Nick Piggin23316bc2006-01-08 01:00:41 -08002333 zone_pcp(zone, cpu) = kmalloc_node(sizeof(struct per_cpu_pageset),
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002334 GFP_KERNEL, cpu_to_node(cpu));
Nick Piggin23316bc2006-01-08 01:00:41 -08002335 if (!zone_pcp(zone, cpu))
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002336 goto bad;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002337
Nick Piggin23316bc2006-01-08 01:00:41 -08002338 setup_pageset(zone_pcp(zone, cpu), zone_batchsize(zone));
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08002339
2340 if (percpu_pagelist_fraction)
2341 setup_pagelist_highmark(zone_pcp(zone, cpu),
2342 (zone->present_pages / percpu_pagelist_fraction));
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002343 }
2344
2345 return 0;
2346bad:
2347 for_each_zone(dzone) {
Andrew Morton64191682007-08-30 23:56:17 -07002348 if (!populated_zone(dzone))
2349 continue;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002350 if (dzone == zone)
2351 break;
Nick Piggin23316bc2006-01-08 01:00:41 -08002352 kfree(zone_pcp(dzone, cpu));
2353 zone_pcp(dzone, cpu) = NULL;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002354 }
2355 return -ENOMEM;
2356}
2357
2358static inline void free_zone_pagesets(int cpu)
2359{
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002360 struct zone *zone;
2361
2362 for_each_zone(zone) {
2363 struct per_cpu_pageset *pset = zone_pcp(zone, cpu);
2364
David Rientjesf3ef9ea2006-09-25 16:24:57 -07002365 /* Free per_cpu_pageset if it is slab allocated */
2366 if (pset != &boot_pageset[cpu])
2367 kfree(pset);
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002368 zone_pcp(zone, cpu) = NULL;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002369 }
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002370}
2371
Chandra Seetharaman9c7b2162006-06-27 02:54:07 -07002372static int __cpuinit pageset_cpuup_callback(struct notifier_block *nfb,
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002373 unsigned long action,
2374 void *hcpu)
2375{
2376 int cpu = (long)hcpu;
2377 int ret = NOTIFY_OK;
2378
2379 switch (action) {
Andy Whitcroftce421c72006-12-06 20:33:08 -08002380 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002381 case CPU_UP_PREPARE_FROZEN:
Andy Whitcroftce421c72006-12-06 20:33:08 -08002382 if (process_zones(cpu))
2383 ret = NOTIFY_BAD;
2384 break;
2385 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002386 case CPU_UP_CANCELED_FROZEN:
Andy Whitcroftce421c72006-12-06 20:33:08 -08002387 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002388 case CPU_DEAD_FROZEN:
Andy Whitcroftce421c72006-12-06 20:33:08 -08002389 free_zone_pagesets(cpu);
2390 break;
2391 default:
2392 break;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002393 }
2394 return ret;
2395}
2396
Chandra Seetharaman74b85f32006-06-27 02:54:09 -07002397static struct notifier_block __cpuinitdata pageset_notifier =
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002398 { &pageset_cpuup_callback, NULL, 0 };
2399
Al Viro78d99552005-12-15 09:18:25 +00002400void __init setup_per_cpu_pageset(void)
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002401{
2402 int err;
2403
2404 /* Initialize per_cpu_pageset for cpu 0.
2405 * A cpuup callback will do this for every cpu
2406 * as it comes online
2407 */
2408 err = process_zones(smp_processor_id());
2409 BUG_ON(err);
2410 register_cpu_notifier(&pageset_notifier);
2411}
2412
2413#endif
2414
Sam Ravnborg577a32f2007-05-17 23:29:25 +02002415static noinline __init_refok
Yasunori Gotocca448f2006-06-23 02:03:10 -07002416int zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages)
Dave Hansened8ece22005-10-29 18:16:50 -07002417{
2418 int i;
2419 struct pglist_data *pgdat = zone->zone_pgdat;
Yasunori Gotocca448f2006-06-23 02:03:10 -07002420 size_t alloc_size;
Dave Hansened8ece22005-10-29 18:16:50 -07002421
2422 /*
2423 * The per-page waitqueue mechanism uses hashed waitqueues
2424 * per zone.
2425 */
Yasunori Goto02b694d2006-06-23 02:03:08 -07002426 zone->wait_table_hash_nr_entries =
2427 wait_table_hash_nr_entries(zone_size_pages);
2428 zone->wait_table_bits =
2429 wait_table_bits(zone->wait_table_hash_nr_entries);
Yasunori Gotocca448f2006-06-23 02:03:10 -07002430 alloc_size = zone->wait_table_hash_nr_entries
2431 * sizeof(wait_queue_head_t);
2432
2433 if (system_state == SYSTEM_BOOTING) {
2434 zone->wait_table = (wait_queue_head_t *)
2435 alloc_bootmem_node(pgdat, alloc_size);
2436 } else {
2437 /*
2438 * This case means that a zone whose size was 0 gets new memory
2439 * via memory hot-add.
2440 * But it may be the case that a new node was hot-added. In
2441 * this case vmalloc() will not be able to use this new node's
2442 * memory - this wait_table must be initialized to use this new
2443 * node itself as well.
2444 * To use this new node's memory, further consideration will be
2445 * necessary.
2446 */
2447 zone->wait_table = (wait_queue_head_t *)vmalloc(alloc_size);
2448 }
2449 if (!zone->wait_table)
2450 return -ENOMEM;
Dave Hansened8ece22005-10-29 18:16:50 -07002451
Yasunori Goto02b694d2006-06-23 02:03:08 -07002452 for(i = 0; i < zone->wait_table_hash_nr_entries; ++i)
Dave Hansened8ece22005-10-29 18:16:50 -07002453 init_waitqueue_head(zone->wait_table + i);
Yasunori Gotocca448f2006-06-23 02:03:10 -07002454
2455 return 0;
Dave Hansened8ece22005-10-29 18:16:50 -07002456}
2457
Matt Tolentinoc09b4242006-01-17 07:03:44 +01002458static __meminit void zone_pcp_init(struct zone *zone)
Dave Hansened8ece22005-10-29 18:16:50 -07002459{
2460 int cpu;
2461 unsigned long batch = zone_batchsize(zone);
2462
2463 for (cpu = 0; cpu < NR_CPUS; cpu++) {
2464#ifdef CONFIG_NUMA
2465 /* Early boot. Slab allocator not functional yet */
Nick Piggin23316bc2006-01-08 01:00:41 -08002466 zone_pcp(zone, cpu) = &boot_pageset[cpu];
Dave Hansened8ece22005-10-29 18:16:50 -07002467 setup_pageset(&boot_pageset[cpu],0);
2468#else
2469 setup_pageset(zone_pcp(zone,cpu), batch);
2470#endif
2471 }
Anton Blanchardf5335c02006-03-25 03:06:49 -08002472 if (zone->present_pages)
2473 printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%lu\n",
2474 zone->name, zone->present_pages, batch);
Dave Hansened8ece22005-10-29 18:16:50 -07002475}
2476
Yasunori Goto718127c2006-06-23 02:03:10 -07002477__meminit int init_currently_empty_zone(struct zone *zone,
2478 unsigned long zone_start_pfn,
Dave Hansena2f3aa022007-01-10 23:15:30 -08002479 unsigned long size,
2480 enum memmap_context context)
Dave Hansened8ece22005-10-29 18:16:50 -07002481{
2482 struct pglist_data *pgdat = zone->zone_pgdat;
Yasunori Gotocca448f2006-06-23 02:03:10 -07002483 int ret;
2484 ret = zone_wait_table_init(zone, size);
2485 if (ret)
2486 return ret;
Dave Hansened8ece22005-10-29 18:16:50 -07002487 pgdat->nr_zones = zone_idx(zone) + 1;
2488
Dave Hansened8ece22005-10-29 18:16:50 -07002489 zone->zone_start_pfn = zone_start_pfn;
2490
2491 memmap_init(size, pgdat->node_id, zone_idx(zone), zone_start_pfn);
2492
2493 zone_init_free_lists(pgdat, zone, zone->spanned_pages);
Yasunori Goto718127c2006-06-23 02:03:10 -07002494
2495 return 0;
Dave Hansened8ece22005-10-29 18:16:50 -07002496}
2497
Mel Gormanc7132162006-09-27 01:49:43 -07002498#ifdef CONFIG_ARCH_POPULATES_NODE_MAP
2499/*
2500 * Basic iterator support. Return the first range of PFNs for a node
2501 * Note: nid == MAX_NUMNODES returns first region regardless of node
2502 */
Yasunori Gotoa3142c82007-05-08 00:23:07 -07002503static int __meminit first_active_region_index_in_nid(int nid)
Mel Gormanc7132162006-09-27 01:49:43 -07002504{
2505 int i;
2506
2507 for (i = 0; i < nr_nodemap_entries; i++)
2508 if (nid == MAX_NUMNODES || early_node_map[i].nid == nid)
2509 return i;
2510
2511 return -1;
2512}
2513
2514/*
2515 * Basic iterator support. Return the next active range of PFNs for a node
2516 * Note: nid == MAX_NUMNODES returns next region regardles of node
2517 */
Yasunori Gotoa3142c82007-05-08 00:23:07 -07002518static int __meminit next_active_region_index_in_nid(int index, int nid)
Mel Gormanc7132162006-09-27 01:49:43 -07002519{
2520 for (index = index + 1; index < nr_nodemap_entries; index++)
2521 if (nid == MAX_NUMNODES || early_node_map[index].nid == nid)
2522 return index;
2523
2524 return -1;
2525}
2526
2527#ifndef CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID
2528/*
2529 * Required by SPARSEMEM. Given a PFN, return what node the PFN is on.
2530 * Architectures may implement their own version but if add_active_range()
2531 * was used and there are no special requirements, this is a convenient
2532 * alternative
2533 */
Stephen Rothwell6f076f52007-05-10 03:15:27 -07002534int __meminit early_pfn_to_nid(unsigned long pfn)
Mel Gormanc7132162006-09-27 01:49:43 -07002535{
2536 int i;
2537
2538 for (i = 0; i < nr_nodemap_entries; i++) {
2539 unsigned long start_pfn = early_node_map[i].start_pfn;
2540 unsigned long end_pfn = early_node_map[i].end_pfn;
2541
2542 if (start_pfn <= pfn && pfn < end_pfn)
2543 return early_node_map[i].nid;
2544 }
2545
2546 return 0;
2547}
2548#endif /* CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID */
2549
2550/* Basic iterator support to walk early_node_map[] */
2551#define for_each_active_range_index_in_nid(i, nid) \
2552 for (i = first_active_region_index_in_nid(nid); i != -1; \
2553 i = next_active_region_index_in_nid(i, nid))
2554
2555/**
2556 * free_bootmem_with_active_regions - Call free_bootmem_node for each active range
Randy Dunlap88ca3b92006-10-04 02:15:25 -07002557 * @nid: The node to free memory on. If MAX_NUMNODES, all nodes are freed.
2558 * @max_low_pfn: The highest PFN that will be passed to free_bootmem_node
Mel Gormanc7132162006-09-27 01:49:43 -07002559 *
2560 * If an architecture guarantees that all ranges registered with
2561 * add_active_ranges() contain no holes and may be freed, this
2562 * this function may be used instead of calling free_bootmem() manually.
2563 */
2564void __init free_bootmem_with_active_regions(int nid,
2565 unsigned long max_low_pfn)
2566{
2567 int i;
2568
2569 for_each_active_range_index_in_nid(i, nid) {
2570 unsigned long size_pages = 0;
2571 unsigned long end_pfn = early_node_map[i].end_pfn;
2572
2573 if (early_node_map[i].start_pfn >= max_low_pfn)
2574 continue;
2575
2576 if (end_pfn > max_low_pfn)
2577 end_pfn = max_low_pfn;
2578
2579 size_pages = end_pfn - early_node_map[i].start_pfn;
2580 free_bootmem_node(NODE_DATA(early_node_map[i].nid),
2581 PFN_PHYS(early_node_map[i].start_pfn),
2582 size_pages << PAGE_SHIFT);
2583 }
2584}
2585
2586/**
2587 * sparse_memory_present_with_active_regions - Call memory_present for each active range
Randy Dunlap88ca3b92006-10-04 02:15:25 -07002588 * @nid: The node to call memory_present for. If MAX_NUMNODES, all nodes will be used.
Mel Gormanc7132162006-09-27 01:49:43 -07002589 *
2590 * If an architecture guarantees that all ranges registered with
2591 * add_active_ranges() contain no holes and may be freed, this
Randy Dunlap88ca3b92006-10-04 02:15:25 -07002592 * function may be used instead of calling memory_present() manually.
Mel Gormanc7132162006-09-27 01:49:43 -07002593 */
2594void __init sparse_memory_present_with_active_regions(int nid)
2595{
2596 int i;
2597
2598 for_each_active_range_index_in_nid(i, nid)
2599 memory_present(early_node_map[i].nid,
2600 early_node_map[i].start_pfn,
2601 early_node_map[i].end_pfn);
2602}
2603
2604/**
Mel Gormanfb014392006-09-27 01:49:59 -07002605 * push_node_boundaries - Push node boundaries to at least the requested boundary
2606 * @nid: The nid of the node to push the boundary for
2607 * @start_pfn: The start pfn of the node
2608 * @end_pfn: The end pfn of the node
2609 *
2610 * In reserve-based hot-add, mem_map is allocated that is unused until hotadd
2611 * time. Specifically, on x86_64, SRAT will report ranges that can potentially
2612 * be hotplugged even though no physical memory exists. This function allows
2613 * an arch to push out the node boundaries so mem_map is allocated that can
2614 * be used later.
2615 */
2616#ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
2617void __init push_node_boundaries(unsigned int nid,
2618 unsigned long start_pfn, unsigned long end_pfn)
2619{
2620 printk(KERN_DEBUG "Entering push_node_boundaries(%u, %lu, %lu)\n",
2621 nid, start_pfn, end_pfn);
2622
2623 /* Initialise the boundary for this node if necessary */
2624 if (node_boundary_end_pfn[nid] == 0)
2625 node_boundary_start_pfn[nid] = -1UL;
2626
2627 /* Update the boundaries */
2628 if (node_boundary_start_pfn[nid] > start_pfn)
2629 node_boundary_start_pfn[nid] = start_pfn;
2630 if (node_boundary_end_pfn[nid] < end_pfn)
2631 node_boundary_end_pfn[nid] = end_pfn;
2632}
2633
2634/* If necessary, push the node boundary out for reserve hotadd */
Jan Beulich98011f52007-07-15 23:38:17 -07002635static void __meminit account_node_boundary(unsigned int nid,
Mel Gormanfb014392006-09-27 01:49:59 -07002636 unsigned long *start_pfn, unsigned long *end_pfn)
2637{
2638 printk(KERN_DEBUG "Entering account_node_boundary(%u, %lu, %lu)\n",
2639 nid, *start_pfn, *end_pfn);
2640
2641 /* Return if boundary information has not been provided */
2642 if (node_boundary_end_pfn[nid] == 0)
2643 return;
2644
2645 /* Check the boundaries and update if necessary */
2646 if (node_boundary_start_pfn[nid] < *start_pfn)
2647 *start_pfn = node_boundary_start_pfn[nid];
2648 if (node_boundary_end_pfn[nid] > *end_pfn)
2649 *end_pfn = node_boundary_end_pfn[nid];
2650}
2651#else
2652void __init push_node_boundaries(unsigned int nid,
2653 unsigned long start_pfn, unsigned long end_pfn) {}
2654
Jan Beulich98011f52007-07-15 23:38:17 -07002655static void __meminit account_node_boundary(unsigned int nid,
Mel Gormanfb014392006-09-27 01:49:59 -07002656 unsigned long *start_pfn, unsigned long *end_pfn) {}
2657#endif
2658
2659
2660/**
Mel Gormanc7132162006-09-27 01:49:43 -07002661 * get_pfn_range_for_nid - Return the start and end page frames for a node
Randy Dunlap88ca3b92006-10-04 02:15:25 -07002662 * @nid: The nid to return the range for. If MAX_NUMNODES, the min and max PFN are returned.
2663 * @start_pfn: Passed by reference. On return, it will have the node start_pfn.
2664 * @end_pfn: Passed by reference. On return, it will have the node end_pfn.
Mel Gormanc7132162006-09-27 01:49:43 -07002665 *
2666 * It returns the start and end page frame of a node based on information
2667 * provided by an arch calling add_active_range(). If called for a node
2668 * with no available memory, a warning is printed and the start and end
Randy Dunlap88ca3b92006-10-04 02:15:25 -07002669 * PFNs will be 0.
Mel Gormanc7132162006-09-27 01:49:43 -07002670 */
Yasunori Gotoa3142c82007-05-08 00:23:07 -07002671void __meminit get_pfn_range_for_nid(unsigned int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07002672 unsigned long *start_pfn, unsigned long *end_pfn)
2673{
2674 int i;
2675 *start_pfn = -1UL;
2676 *end_pfn = 0;
2677
2678 for_each_active_range_index_in_nid(i, nid) {
2679 *start_pfn = min(*start_pfn, early_node_map[i].start_pfn);
2680 *end_pfn = max(*end_pfn, early_node_map[i].end_pfn);
2681 }
2682
2683 if (*start_pfn == -1UL) {
2684 printk(KERN_WARNING "Node %u active with no memory\n", nid);
2685 *start_pfn = 0;
2686 }
Mel Gormanfb014392006-09-27 01:49:59 -07002687
2688 /* Push the node boundaries out if requested */
2689 account_node_boundary(nid, start_pfn, end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07002690}
2691
2692/*
Mel Gorman2a1e2742007-07-17 04:03:12 -07002693 * This finds a zone that can be used for ZONE_MOVABLE pages. The
2694 * assumption is made that zones within a node are ordered in monotonic
2695 * increasing memory addresses so that the "highest" populated zone is used
2696 */
2697void __init find_usable_zone_for_movable(void)
2698{
2699 int zone_index;
2700 for (zone_index = MAX_NR_ZONES - 1; zone_index >= 0; zone_index--) {
2701 if (zone_index == ZONE_MOVABLE)
2702 continue;
2703
2704 if (arch_zone_highest_possible_pfn[zone_index] >
2705 arch_zone_lowest_possible_pfn[zone_index])
2706 break;
2707 }
2708
2709 VM_BUG_ON(zone_index == -1);
2710 movable_zone = zone_index;
2711}
2712
2713/*
2714 * The zone ranges provided by the architecture do not include ZONE_MOVABLE
2715 * because it is sized independant of architecture. Unlike the other zones,
2716 * the starting point for ZONE_MOVABLE is not fixed. It may be different
2717 * in each node depending on the size of each node and how evenly kernelcore
2718 * is distributed. This helper function adjusts the zone ranges
2719 * provided by the architecture for a given node by using the end of the
2720 * highest usable zone for ZONE_MOVABLE. This preserves the assumption that
2721 * zones within a node are in order of monotonic increases memory addresses
2722 */
2723void __meminit adjust_zone_range_for_zone_movable(int nid,
2724 unsigned long zone_type,
2725 unsigned long node_start_pfn,
2726 unsigned long node_end_pfn,
2727 unsigned long *zone_start_pfn,
2728 unsigned long *zone_end_pfn)
2729{
2730 /* Only adjust if ZONE_MOVABLE is on this node */
2731 if (zone_movable_pfn[nid]) {
2732 /* Size ZONE_MOVABLE */
2733 if (zone_type == ZONE_MOVABLE) {
2734 *zone_start_pfn = zone_movable_pfn[nid];
2735 *zone_end_pfn = min(node_end_pfn,
2736 arch_zone_highest_possible_pfn[movable_zone]);
2737
2738 /* Adjust for ZONE_MOVABLE starting within this range */
2739 } else if (*zone_start_pfn < zone_movable_pfn[nid] &&
2740 *zone_end_pfn > zone_movable_pfn[nid]) {
2741 *zone_end_pfn = zone_movable_pfn[nid];
2742
2743 /* Check if this whole range is within ZONE_MOVABLE */
2744 } else if (*zone_start_pfn >= zone_movable_pfn[nid])
2745 *zone_start_pfn = *zone_end_pfn;
2746 }
2747}
2748
2749/*
Mel Gormanc7132162006-09-27 01:49:43 -07002750 * Return the number of pages a zone spans in a node, including holes
2751 * present_pages = zone_spanned_pages_in_node() - zone_absent_pages_in_node()
2752 */
Paul Mundt6ea6e682007-07-15 23:38:20 -07002753static unsigned long __meminit zone_spanned_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07002754 unsigned long zone_type,
2755 unsigned long *ignored)
2756{
2757 unsigned long node_start_pfn, node_end_pfn;
2758 unsigned long zone_start_pfn, zone_end_pfn;
2759
2760 /* Get the start and end of the node and zone */
2761 get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
2762 zone_start_pfn = arch_zone_lowest_possible_pfn[zone_type];
2763 zone_end_pfn = arch_zone_highest_possible_pfn[zone_type];
Mel Gorman2a1e2742007-07-17 04:03:12 -07002764 adjust_zone_range_for_zone_movable(nid, zone_type,
2765 node_start_pfn, node_end_pfn,
2766 &zone_start_pfn, &zone_end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07002767
2768 /* Check that this node has pages within the zone's required range */
2769 if (zone_end_pfn < node_start_pfn || zone_start_pfn > node_end_pfn)
2770 return 0;
2771
2772 /* Move the zone boundaries inside the node if necessary */
2773 zone_end_pfn = min(zone_end_pfn, node_end_pfn);
2774 zone_start_pfn = max(zone_start_pfn, node_start_pfn);
2775
2776 /* Return the spanned pages */
2777 return zone_end_pfn - zone_start_pfn;
2778}
2779
2780/*
2781 * Return the number of holes in a range on a node. If nid is MAX_NUMNODES,
Randy Dunlap88ca3b92006-10-04 02:15:25 -07002782 * then all holes in the requested range will be accounted for.
Mel Gormanc7132162006-09-27 01:49:43 -07002783 */
Yasunori Gotoa3142c82007-05-08 00:23:07 -07002784unsigned long __meminit __absent_pages_in_range(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07002785 unsigned long range_start_pfn,
2786 unsigned long range_end_pfn)
2787{
2788 int i = 0;
2789 unsigned long prev_end_pfn = 0, hole_pages = 0;
2790 unsigned long start_pfn;
2791
2792 /* Find the end_pfn of the first active range of pfns in the node */
2793 i = first_active_region_index_in_nid(nid);
2794 if (i == -1)
2795 return 0;
2796
Mel Gormanb5445f92007-07-26 10:41:18 -07002797 prev_end_pfn = min(early_node_map[i].start_pfn, range_end_pfn);
2798
Mel Gorman9c7cd682006-09-27 01:49:58 -07002799 /* Account for ranges before physical memory on this node */
2800 if (early_node_map[i].start_pfn > range_start_pfn)
Mel Gormanb5445f92007-07-26 10:41:18 -07002801 hole_pages = prev_end_pfn - range_start_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07002802
2803 /* Find all holes for the zone within the node */
2804 for (; i != -1; i = next_active_region_index_in_nid(i, nid)) {
2805
2806 /* No need to continue if prev_end_pfn is outside the zone */
2807 if (prev_end_pfn >= range_end_pfn)
2808 break;
2809
2810 /* Make sure the end of the zone is not within the hole */
2811 start_pfn = min(early_node_map[i].start_pfn, range_end_pfn);
2812 prev_end_pfn = max(prev_end_pfn, range_start_pfn);
2813
2814 /* Update the hole size cound and move on */
2815 if (start_pfn > range_start_pfn) {
2816 BUG_ON(prev_end_pfn > start_pfn);
2817 hole_pages += start_pfn - prev_end_pfn;
2818 }
2819 prev_end_pfn = early_node_map[i].end_pfn;
2820 }
2821
Mel Gorman9c7cd682006-09-27 01:49:58 -07002822 /* Account for ranges past physical memory on this node */
2823 if (range_end_pfn > prev_end_pfn)
Mel Gorman0c6cb972006-10-28 10:38:59 -07002824 hole_pages += range_end_pfn -
Mel Gorman9c7cd682006-09-27 01:49:58 -07002825 max(range_start_pfn, prev_end_pfn);
2826
Mel Gormanc7132162006-09-27 01:49:43 -07002827 return hole_pages;
2828}
2829
2830/**
2831 * absent_pages_in_range - Return number of page frames in holes within a range
2832 * @start_pfn: The start PFN to start searching for holes
2833 * @end_pfn: The end PFN to stop searching for holes
2834 *
Randy Dunlap88ca3b92006-10-04 02:15:25 -07002835 * It returns the number of pages frames in memory holes within a range.
Mel Gormanc7132162006-09-27 01:49:43 -07002836 */
2837unsigned long __init absent_pages_in_range(unsigned long start_pfn,
2838 unsigned long end_pfn)
2839{
2840 return __absent_pages_in_range(MAX_NUMNODES, start_pfn, end_pfn);
2841}
2842
2843/* Return the number of page frames in holes in a zone on a node */
Paul Mundt6ea6e682007-07-15 23:38:20 -07002844static unsigned long __meminit zone_absent_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07002845 unsigned long zone_type,
2846 unsigned long *ignored)
2847{
Mel Gorman9c7cd682006-09-27 01:49:58 -07002848 unsigned long node_start_pfn, node_end_pfn;
2849 unsigned long zone_start_pfn, zone_end_pfn;
2850
2851 get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
2852 zone_start_pfn = max(arch_zone_lowest_possible_pfn[zone_type],
2853 node_start_pfn);
2854 zone_end_pfn = min(arch_zone_highest_possible_pfn[zone_type],
2855 node_end_pfn);
2856
Mel Gorman2a1e2742007-07-17 04:03:12 -07002857 adjust_zone_range_for_zone_movable(nid, zone_type,
2858 node_start_pfn, node_end_pfn,
2859 &zone_start_pfn, &zone_end_pfn);
Mel Gorman9c7cd682006-09-27 01:49:58 -07002860 return __absent_pages_in_range(nid, zone_start_pfn, zone_end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07002861}
Mel Gorman0e0b8642006-09-27 01:49:56 -07002862
Mel Gormanc7132162006-09-27 01:49:43 -07002863#else
Paul Mundt6ea6e682007-07-15 23:38:20 -07002864static inline unsigned long __meminit zone_spanned_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07002865 unsigned long zone_type,
2866 unsigned long *zones_size)
2867{
2868 return zones_size[zone_type];
2869}
2870
Paul Mundt6ea6e682007-07-15 23:38:20 -07002871static inline unsigned long __meminit zone_absent_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07002872 unsigned long zone_type,
2873 unsigned long *zholes_size)
2874{
2875 if (!zholes_size)
2876 return 0;
2877
2878 return zholes_size[zone_type];
2879}
Mel Gorman0e0b8642006-09-27 01:49:56 -07002880
Mel Gormanc7132162006-09-27 01:49:43 -07002881#endif
2882
Yasunori Gotoa3142c82007-05-08 00:23:07 -07002883static void __meminit calculate_node_totalpages(struct pglist_data *pgdat,
Mel Gormanc7132162006-09-27 01:49:43 -07002884 unsigned long *zones_size, unsigned long *zholes_size)
2885{
2886 unsigned long realtotalpages, totalpages = 0;
2887 enum zone_type i;
2888
2889 for (i = 0; i < MAX_NR_ZONES; i++)
2890 totalpages += zone_spanned_pages_in_node(pgdat->node_id, i,
2891 zones_size);
2892 pgdat->node_spanned_pages = totalpages;
2893
2894 realtotalpages = totalpages;
2895 for (i = 0; i < MAX_NR_ZONES; i++)
2896 realtotalpages -=
2897 zone_absent_pages_in_node(pgdat->node_id, i,
2898 zholes_size);
2899 pgdat->node_present_pages = realtotalpages;
2900 printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id,
2901 realtotalpages);
2902}
2903
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904/*
2905 * Set up the zone data structures:
2906 * - mark all pages reserved
2907 * - mark all memory queues empty
2908 * - clear the memory bitmaps
2909 */
Yasunori Goto86356ab2006-06-23 02:03:09 -07002910static void __meminit free_area_init_core(struct pglist_data *pgdat,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 unsigned long *zones_size, unsigned long *zholes_size)
2912{
Christoph Lameter2f1b6242006-09-25 23:31:13 -07002913 enum zone_type j;
Dave Hansened8ece22005-10-29 18:16:50 -07002914 int nid = pgdat->node_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 unsigned long zone_start_pfn = pgdat->node_start_pfn;
Yasunori Goto718127c2006-06-23 02:03:10 -07002916 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
Dave Hansen208d54e2005-10-29 18:16:52 -07002918 pgdat_resize_init(pgdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 pgdat->nr_zones = 0;
2920 init_waitqueue_head(&pgdat->kswapd_wait);
2921 pgdat->kswapd_max_order = 0;
2922
2923 for (j = 0; j < MAX_NR_ZONES; j++) {
2924 struct zone *zone = pgdat->node_zones + j;
Mel Gorman0e0b8642006-09-27 01:49:56 -07002925 unsigned long size, realsize, memmap_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
Mel Gormanc7132162006-09-27 01:49:43 -07002927 size = zone_spanned_pages_in_node(nid, j, zones_size);
2928 realsize = size - zone_absent_pages_in_node(nid, j,
2929 zholes_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
Mel Gorman0e0b8642006-09-27 01:49:56 -07002931 /*
2932 * Adjust realsize so that it accounts for how much memory
2933 * is used by this zone for memmap. This affects the watermark
2934 * and per-cpu initialisations
2935 */
2936 memmap_pages = (size * sizeof(struct page)) >> PAGE_SHIFT;
2937 if (realsize >= memmap_pages) {
2938 realsize -= memmap_pages;
2939 printk(KERN_DEBUG
2940 " %s zone: %lu pages used for memmap\n",
2941 zone_names[j], memmap_pages);
2942 } else
2943 printk(KERN_WARNING
2944 " %s zone: %lu pages exceeds realsize %lu\n",
2945 zone_names[j], memmap_pages, realsize);
2946
Christoph Lameter62672762007-02-10 01:43:07 -08002947 /* Account for reserved pages */
2948 if (j == 0 && realsize > dma_reserve) {
Mel Gorman0e0b8642006-09-27 01:49:56 -07002949 realsize -= dma_reserve;
Christoph Lameter62672762007-02-10 01:43:07 -08002950 printk(KERN_DEBUG " %s zone: %lu pages reserved\n",
2951 zone_names[0], dma_reserve);
Mel Gorman0e0b8642006-09-27 01:49:56 -07002952 }
2953
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07002954 if (!is_highmem_idx(j))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 nr_kernel_pages += realsize;
2956 nr_all_pages += realsize;
2957
2958 zone->spanned_pages = size;
2959 zone->present_pages = realsize;
Christoph Lameter96146342006-07-03 00:24:13 -07002960#ifdef CONFIG_NUMA
Christoph Lameterd5f541e2006-09-27 01:50:08 -07002961 zone->node = nid;
Christoph Lameter8417bba2006-09-25 23:31:51 -07002962 zone->min_unmapped_pages = (realsize*sysctl_min_unmapped_ratio)
Christoph Lameter96146342006-07-03 00:24:13 -07002963 / 100;
Christoph Lameter0ff38492006-09-25 23:31:52 -07002964 zone->min_slab_pages = (realsize * sysctl_min_slab_ratio) / 100;
Christoph Lameter96146342006-07-03 00:24:13 -07002965#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 zone->name = zone_names[j];
2967 spin_lock_init(&zone->lock);
2968 spin_lock_init(&zone->lru_lock);
Dave Hansenbdc8cb92005-10-29 18:16:53 -07002969 zone_seqlock_init(zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 zone->zone_pgdat = pgdat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
Martin Bligh3bb1a8522006-10-28 10:38:24 -07002972 zone->prev_priority = DEF_PRIORITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
Dave Hansened8ece22005-10-29 18:16:50 -07002974 zone_pcp_init(zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 INIT_LIST_HEAD(&zone->active_list);
2976 INIT_LIST_HEAD(&zone->inactive_list);
2977 zone->nr_scan_active = 0;
2978 zone->nr_scan_inactive = 0;
Christoph Lameter2244b952006-06-30 01:55:33 -07002979 zap_zone_vm_stats(zone);
Martin Hicks53e9a612005-09-03 15:54:51 -07002980 atomic_set(&zone->reclaim_in_progress, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 if (!size)
2982 continue;
2983
Dave Hansena2f3aa022007-01-10 23:15:30 -08002984 ret = init_currently_empty_zone(zone, zone_start_pfn,
2985 size, MEMMAP_EARLY);
Yasunori Goto718127c2006-06-23 02:03:10 -07002986 BUG_ON(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 zone_start_pfn += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 }
2989}
2990
Sam Ravnborg577a32f2007-05-17 23:29:25 +02002991static void __init_refok alloc_node_mem_map(struct pglist_data *pgdat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 /* Skip empty nodes */
2994 if (!pgdat->node_spanned_pages)
2995 return;
2996
Andy Whitcroftd41dee32005-06-23 00:07:54 -07002997#ifdef CONFIG_FLAT_NODE_MEM_MAP
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 /* ia64 gets its own node_mem_map, before this, without bootmem */
2999 if (!pgdat->node_mem_map) {
Bob Piccoe984bb42006-05-20 15:00:31 -07003000 unsigned long size, start, end;
Andy Whitcroftd41dee32005-06-23 00:07:54 -07003001 struct page *map;
3002
Bob Piccoe984bb42006-05-20 15:00:31 -07003003 /*
3004 * The zone's endpoints aren't required to be MAX_ORDER
3005 * aligned but the node_mem_map endpoints must be in order
3006 * for the buddy allocator to function correctly.
3007 */
3008 start = pgdat->node_start_pfn & ~(MAX_ORDER_NR_PAGES - 1);
3009 end = pgdat->node_start_pfn + pgdat->node_spanned_pages;
3010 end = ALIGN(end, MAX_ORDER_NR_PAGES);
3011 size = (end - start) * sizeof(struct page);
Dave Hansen6f167ec2005-06-23 00:07:39 -07003012 map = alloc_remap(pgdat->node_id, size);
3013 if (!map)
3014 map = alloc_bootmem_node(pgdat, size);
Bob Piccoe984bb42006-05-20 15:00:31 -07003015 pgdat->node_mem_map = map + (pgdat->node_start_pfn - start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 }
Roman Zippel12d810c2007-05-31 00:40:54 -07003017#ifndef CONFIG_NEED_MULTIPLE_NODES
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 /*
3019 * With no DISCONTIG, the global mem_map is just set as node 0's
3020 */
Mel Gormanc7132162006-09-27 01:49:43 -07003021 if (pgdat == NODE_DATA(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 mem_map = NODE_DATA(0)->node_mem_map;
Mel Gormanc7132162006-09-27 01:49:43 -07003023#ifdef CONFIG_ARCH_POPULATES_NODE_MAP
3024 if (page_to_pfn(mem_map) != pgdat->node_start_pfn)
3025 mem_map -= pgdat->node_start_pfn;
3026#endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
3027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028#endif
Andy Whitcroftd41dee32005-06-23 00:07:54 -07003029#endif /* CONFIG_FLAT_NODE_MEM_MAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030}
3031
Yasunori Goto86356ab2006-06-23 02:03:09 -07003032void __meminit free_area_init_node(int nid, struct pglist_data *pgdat,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 unsigned long *zones_size, unsigned long node_start_pfn,
3034 unsigned long *zholes_size)
3035{
3036 pgdat->node_id = nid;
3037 pgdat->node_start_pfn = node_start_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07003038 calculate_node_totalpages(pgdat, zones_size, zholes_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
3040 alloc_node_mem_map(pgdat);
3041
3042 free_area_init_core(pgdat, zones_size, zholes_size);
3043}
3044
Mel Gormanc7132162006-09-27 01:49:43 -07003045#ifdef CONFIG_ARCH_POPULATES_NODE_MAP
Miklos Szeredi418508c2007-05-23 13:57:55 -07003046
3047#if MAX_NUMNODES > 1
3048/*
3049 * Figure out the number of possible node ids.
3050 */
3051static void __init setup_nr_node_ids(void)
3052{
3053 unsigned int node;
3054 unsigned int highest = 0;
3055
3056 for_each_node_mask(node, node_possible_map)
3057 highest = node;
3058 nr_node_ids = highest + 1;
3059}
3060#else
3061static inline void setup_nr_node_ids(void)
3062{
3063}
3064#endif
3065
Mel Gormanc7132162006-09-27 01:49:43 -07003066/**
3067 * add_active_range - Register a range of PFNs backed by physical memory
3068 * @nid: The node ID the range resides on
3069 * @start_pfn: The start PFN of the available physical memory
3070 * @end_pfn: The end PFN of the available physical memory
3071 *
3072 * These ranges are stored in an early_node_map[] and later used by
3073 * free_area_init_nodes() to calculate zone sizes and holes. If the
3074 * range spans a memory hole, it is up to the architecture to ensure
3075 * the memory is not freed by the bootmem allocator. If possible
3076 * the range being registered will be merged with existing ranges.
3077 */
3078void __init add_active_range(unsigned int nid, unsigned long start_pfn,
3079 unsigned long end_pfn)
3080{
3081 int i;
3082
3083 printk(KERN_DEBUG "Entering add_active_range(%d, %lu, %lu) "
3084 "%d entries of %d used\n",
3085 nid, start_pfn, end_pfn,
3086 nr_nodemap_entries, MAX_ACTIVE_REGIONS);
3087
3088 /* Merge with existing active regions if possible */
3089 for (i = 0; i < nr_nodemap_entries; i++) {
3090 if (early_node_map[i].nid != nid)
3091 continue;
3092
3093 /* Skip if an existing region covers this new one */
3094 if (start_pfn >= early_node_map[i].start_pfn &&
3095 end_pfn <= early_node_map[i].end_pfn)
3096 return;
3097
3098 /* Merge forward if suitable */
3099 if (start_pfn <= early_node_map[i].end_pfn &&
3100 end_pfn > early_node_map[i].end_pfn) {
3101 early_node_map[i].end_pfn = end_pfn;
3102 return;
3103 }
3104
3105 /* Merge backward if suitable */
3106 if (start_pfn < early_node_map[i].end_pfn &&
3107 end_pfn >= early_node_map[i].start_pfn) {
3108 early_node_map[i].start_pfn = start_pfn;
3109 return;
3110 }
3111 }
3112
3113 /* Check that early_node_map is large enough */
3114 if (i >= MAX_ACTIVE_REGIONS) {
3115 printk(KERN_CRIT "More than %d memory regions, truncating\n",
3116 MAX_ACTIVE_REGIONS);
3117 return;
3118 }
3119
3120 early_node_map[i].nid = nid;
3121 early_node_map[i].start_pfn = start_pfn;
3122 early_node_map[i].end_pfn = end_pfn;
3123 nr_nodemap_entries = i + 1;
3124}
3125
3126/**
3127 * shrink_active_range - Shrink an existing registered range of PFNs
3128 * @nid: The node id the range is on that should be shrunk
3129 * @old_end_pfn: The old end PFN of the range
3130 * @new_end_pfn: The new PFN of the range
3131 *
3132 * i386 with NUMA use alloc_remap() to store a node_mem_map on a local node.
3133 * The map is kept at the end physical page range that has already been
3134 * registered with add_active_range(). This function allows an arch to shrink
3135 * an existing registered range.
3136 */
3137void __init shrink_active_range(unsigned int nid, unsigned long old_end_pfn,
3138 unsigned long new_end_pfn)
3139{
3140 int i;
3141
3142 /* Find the old active region end and shrink */
3143 for_each_active_range_index_in_nid(i, nid)
3144 if (early_node_map[i].end_pfn == old_end_pfn) {
3145 early_node_map[i].end_pfn = new_end_pfn;
3146 break;
3147 }
3148}
3149
3150/**
3151 * remove_all_active_ranges - Remove all currently registered regions
Randy Dunlap88ca3b92006-10-04 02:15:25 -07003152 *
Mel Gormanc7132162006-09-27 01:49:43 -07003153 * During discovery, it may be found that a table like SRAT is invalid
3154 * and an alternative discovery method must be used. This function removes
3155 * all currently registered regions.
3156 */
Randy Dunlap88ca3b92006-10-04 02:15:25 -07003157void __init remove_all_active_ranges(void)
Mel Gormanc7132162006-09-27 01:49:43 -07003158{
3159 memset(early_node_map, 0, sizeof(early_node_map));
3160 nr_nodemap_entries = 0;
Mel Gormanfb014392006-09-27 01:49:59 -07003161#ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
3162 memset(node_boundary_start_pfn, 0, sizeof(node_boundary_start_pfn));
3163 memset(node_boundary_end_pfn, 0, sizeof(node_boundary_end_pfn));
3164#endif /* CONFIG_MEMORY_HOTPLUG_RESERVE */
Mel Gormanc7132162006-09-27 01:49:43 -07003165}
3166
3167/* Compare two active node_active_regions */
3168static int __init cmp_node_active_region(const void *a, const void *b)
3169{
3170 struct node_active_region *arange = (struct node_active_region *)a;
3171 struct node_active_region *brange = (struct node_active_region *)b;
3172
3173 /* Done this way to avoid overflows */
3174 if (arange->start_pfn > brange->start_pfn)
3175 return 1;
3176 if (arange->start_pfn < brange->start_pfn)
3177 return -1;
3178
3179 return 0;
3180}
3181
3182/* sort the node_map by start_pfn */
3183static void __init sort_node_map(void)
3184{
3185 sort(early_node_map, (size_t)nr_nodemap_entries,
3186 sizeof(struct node_active_region),
3187 cmp_node_active_region, NULL);
3188}
3189
Mel Gormana6af2bc2007-02-10 01:42:57 -08003190/* Find the lowest pfn for a node */
Mel Gormanc7132162006-09-27 01:49:43 -07003191unsigned long __init find_min_pfn_for_node(unsigned long nid)
3192{
3193 int i;
Mel Gormana6af2bc2007-02-10 01:42:57 -08003194 unsigned long min_pfn = ULONG_MAX;
Mel Gorman1abbfb42006-11-23 12:01:41 +00003195
Mel Gormanc7132162006-09-27 01:49:43 -07003196 /* Assuming a sorted map, the first range found has the starting pfn */
3197 for_each_active_range_index_in_nid(i, nid)
Mel Gormana6af2bc2007-02-10 01:42:57 -08003198 min_pfn = min(min_pfn, early_node_map[i].start_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07003199
Mel Gormana6af2bc2007-02-10 01:42:57 -08003200 if (min_pfn == ULONG_MAX) {
3201 printk(KERN_WARNING
3202 "Could not find start_pfn for node %lu\n", nid);
3203 return 0;
3204 }
3205
3206 return min_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07003207}
3208
3209/**
3210 * find_min_pfn_with_active_regions - Find the minimum PFN registered
3211 *
3212 * It returns the minimum PFN based on information provided via
Randy Dunlap88ca3b92006-10-04 02:15:25 -07003213 * add_active_range().
Mel Gormanc7132162006-09-27 01:49:43 -07003214 */
3215unsigned long __init find_min_pfn_with_active_regions(void)
3216{
3217 return find_min_pfn_for_node(MAX_NUMNODES);
3218}
3219
3220/**
3221 * find_max_pfn_with_active_regions - Find the maximum PFN registered
3222 *
3223 * It returns the maximum PFN based on information provided via
Randy Dunlap88ca3b92006-10-04 02:15:25 -07003224 * add_active_range().
Mel Gormanc7132162006-09-27 01:49:43 -07003225 */
3226unsigned long __init find_max_pfn_with_active_regions(void)
3227{
3228 int i;
3229 unsigned long max_pfn = 0;
3230
3231 for (i = 0; i < nr_nodemap_entries; i++)
3232 max_pfn = max(max_pfn, early_node_map[i].end_pfn);
3233
3234 return max_pfn;
3235}
3236
Mel Gorman7e63efe2007-07-17 04:03:15 -07003237unsigned long __init early_calculate_totalpages(void)
3238{
3239 int i;
3240 unsigned long totalpages = 0;
3241
3242 for (i = 0; i < nr_nodemap_entries; i++)
3243 totalpages += early_node_map[i].end_pfn -
3244 early_node_map[i].start_pfn;
3245
3246 return totalpages;
3247}
3248
Mel Gorman2a1e2742007-07-17 04:03:12 -07003249/*
3250 * Find the PFN the Movable zone begins in each node. Kernel memory
3251 * is spread evenly between nodes as long as the nodes have enough
3252 * memory. When they don't, some nodes will have more kernelcore than
3253 * others
3254 */
3255void __init find_zone_movable_pfns_for_nodes(unsigned long *movable_pfn)
3256{
3257 int i, nid;
3258 unsigned long usable_startpfn;
3259 unsigned long kernelcore_node, kernelcore_remaining;
3260 int usable_nodes = num_online_nodes();
3261
Mel Gorman7e63efe2007-07-17 04:03:15 -07003262 /*
3263 * If movablecore was specified, calculate what size of
3264 * kernelcore that corresponds so that memory usable for
3265 * any allocation type is evenly spread. If both kernelcore
3266 * and movablecore are specified, then the value of kernelcore
3267 * will be used for required_kernelcore if it's greater than
3268 * what movablecore would have allowed.
3269 */
3270 if (required_movablecore) {
3271 unsigned long totalpages = early_calculate_totalpages();
3272 unsigned long corepages;
3273
3274 /*
3275 * Round-up so that ZONE_MOVABLE is at least as large as what
3276 * was requested by the user
3277 */
3278 required_movablecore =
3279 roundup(required_movablecore, MAX_ORDER_NR_PAGES);
3280 corepages = totalpages - required_movablecore;
3281
3282 required_kernelcore = max(required_kernelcore, corepages);
3283 }
3284
Mel Gorman2a1e2742007-07-17 04:03:12 -07003285 /* If kernelcore was not specified, there is no ZONE_MOVABLE */
3286 if (!required_kernelcore)
3287 return;
3288
3289 /* usable_startpfn is the lowest possible pfn ZONE_MOVABLE can be at */
3290 find_usable_zone_for_movable();
3291 usable_startpfn = arch_zone_lowest_possible_pfn[movable_zone];
3292
3293restart:
3294 /* Spread kernelcore memory as evenly as possible throughout nodes */
3295 kernelcore_node = required_kernelcore / usable_nodes;
3296 for_each_online_node(nid) {
3297 /*
3298 * Recalculate kernelcore_node if the division per node
3299 * now exceeds what is necessary to satisfy the requested
3300 * amount of memory for the kernel
3301 */
3302 if (required_kernelcore < kernelcore_node)
3303 kernelcore_node = required_kernelcore / usable_nodes;
3304
3305 /*
3306 * As the map is walked, we track how much memory is usable
3307 * by the kernel using kernelcore_remaining. When it is
3308 * 0, the rest of the node is usable by ZONE_MOVABLE
3309 */
3310 kernelcore_remaining = kernelcore_node;
3311
3312 /* Go through each range of PFNs within this node */
3313 for_each_active_range_index_in_nid(i, nid) {
3314 unsigned long start_pfn, end_pfn;
3315 unsigned long size_pages;
3316
3317 start_pfn = max(early_node_map[i].start_pfn,
3318 zone_movable_pfn[nid]);
3319 end_pfn = early_node_map[i].end_pfn;
3320 if (start_pfn >= end_pfn)
3321 continue;
3322
3323 /* Account for what is only usable for kernelcore */
3324 if (start_pfn < usable_startpfn) {
3325 unsigned long kernel_pages;
3326 kernel_pages = min(end_pfn, usable_startpfn)
3327 - start_pfn;
3328
3329 kernelcore_remaining -= min(kernel_pages,
3330 kernelcore_remaining);
3331 required_kernelcore -= min(kernel_pages,
3332 required_kernelcore);
3333
3334 /* Continue if range is now fully accounted */
3335 if (end_pfn <= usable_startpfn) {
3336
3337 /*
3338 * Push zone_movable_pfn to the end so
3339 * that if we have to rebalance
3340 * kernelcore across nodes, we will
3341 * not double account here
3342 */
3343 zone_movable_pfn[nid] = end_pfn;
3344 continue;
3345 }
3346 start_pfn = usable_startpfn;
3347 }
3348
3349 /*
3350 * The usable PFN range for ZONE_MOVABLE is from
3351 * start_pfn->end_pfn. Calculate size_pages as the
3352 * number of pages used as kernelcore
3353 */
3354 size_pages = end_pfn - start_pfn;
3355 if (size_pages > kernelcore_remaining)
3356 size_pages = kernelcore_remaining;
3357 zone_movable_pfn[nid] = start_pfn + size_pages;
3358
3359 /*
3360 * Some kernelcore has been met, update counts and
3361 * break if the kernelcore for this node has been
3362 * satisified
3363 */
3364 required_kernelcore -= min(required_kernelcore,
3365 size_pages);
3366 kernelcore_remaining -= size_pages;
3367 if (!kernelcore_remaining)
3368 break;
3369 }
3370 }
3371
3372 /*
3373 * If there is still required_kernelcore, we do another pass with one
3374 * less node in the count. This will push zone_movable_pfn[nid] further
3375 * along on the nodes that still have memory until kernelcore is
3376 * satisified
3377 */
3378 usable_nodes--;
3379 if (usable_nodes && required_kernelcore > usable_nodes)
3380 goto restart;
3381
3382 /* Align start of ZONE_MOVABLE on all nids to MAX_ORDER_NR_PAGES */
3383 for (nid = 0; nid < MAX_NUMNODES; nid++)
3384 zone_movable_pfn[nid] =
3385 roundup(zone_movable_pfn[nid], MAX_ORDER_NR_PAGES);
3386}
3387
Mel Gormanc7132162006-09-27 01:49:43 -07003388/**
3389 * free_area_init_nodes - Initialise all pg_data_t and zone data
Randy Dunlap88ca3b92006-10-04 02:15:25 -07003390 * @max_zone_pfn: an array of max PFNs for each zone
Mel Gormanc7132162006-09-27 01:49:43 -07003391 *
3392 * This will call free_area_init_node() for each active node in the system.
3393 * Using the page ranges provided by add_active_range(), the size of each
3394 * zone in each node and their holes is calculated. If the maximum PFN
3395 * between two adjacent zones match, it is assumed that the zone is empty.
3396 * For example, if arch_max_dma_pfn == arch_max_dma32_pfn, it is assumed
3397 * that arch_max_dma32_pfn has no pages. It is also assumed that a zone
3398 * starts where the previous one ended. For example, ZONE_DMA32 starts
3399 * at arch_max_dma_pfn.
3400 */
3401void __init free_area_init_nodes(unsigned long *max_zone_pfn)
3402{
3403 unsigned long nid;
3404 enum zone_type i;
3405
Mel Gormana6af2bc2007-02-10 01:42:57 -08003406 /* Sort early_node_map as initialisation assumes it is sorted */
3407 sort_node_map();
3408
Mel Gormanc7132162006-09-27 01:49:43 -07003409 /* Record where the zone boundaries are */
3410 memset(arch_zone_lowest_possible_pfn, 0,
3411 sizeof(arch_zone_lowest_possible_pfn));
3412 memset(arch_zone_highest_possible_pfn, 0,
3413 sizeof(arch_zone_highest_possible_pfn));
3414 arch_zone_lowest_possible_pfn[0] = find_min_pfn_with_active_regions();
3415 arch_zone_highest_possible_pfn[0] = max_zone_pfn[0];
3416 for (i = 1; i < MAX_NR_ZONES; i++) {
Mel Gorman2a1e2742007-07-17 04:03:12 -07003417 if (i == ZONE_MOVABLE)
3418 continue;
Mel Gormanc7132162006-09-27 01:49:43 -07003419 arch_zone_lowest_possible_pfn[i] =
3420 arch_zone_highest_possible_pfn[i-1];
3421 arch_zone_highest_possible_pfn[i] =
3422 max(max_zone_pfn[i], arch_zone_lowest_possible_pfn[i]);
3423 }
Mel Gorman2a1e2742007-07-17 04:03:12 -07003424 arch_zone_lowest_possible_pfn[ZONE_MOVABLE] = 0;
3425 arch_zone_highest_possible_pfn[ZONE_MOVABLE] = 0;
3426
3427 /* Find the PFNs that ZONE_MOVABLE begins at in each node */
3428 memset(zone_movable_pfn, 0, sizeof(zone_movable_pfn));
3429 find_zone_movable_pfns_for_nodes(zone_movable_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07003430
Mel Gormanc7132162006-09-27 01:49:43 -07003431 /* Print out the zone ranges */
3432 printk("Zone PFN ranges:\n");
Mel Gorman2a1e2742007-07-17 04:03:12 -07003433 for (i = 0; i < MAX_NR_ZONES; i++) {
3434 if (i == ZONE_MOVABLE)
3435 continue;
Mel Gormanc7132162006-09-27 01:49:43 -07003436 printk(" %-8s %8lu -> %8lu\n",
3437 zone_names[i],
3438 arch_zone_lowest_possible_pfn[i],
3439 arch_zone_highest_possible_pfn[i]);
Mel Gorman2a1e2742007-07-17 04:03:12 -07003440 }
3441
3442 /* Print out the PFNs ZONE_MOVABLE begins at in each node */
3443 printk("Movable zone start PFN for each node\n");
3444 for (i = 0; i < MAX_NUMNODES; i++) {
3445 if (zone_movable_pfn[i])
3446 printk(" Node %d: %lu\n", i, zone_movable_pfn[i]);
3447 }
Mel Gormanc7132162006-09-27 01:49:43 -07003448
3449 /* Print out the early_node_map[] */
3450 printk("early_node_map[%d] active PFN ranges\n", nr_nodemap_entries);
3451 for (i = 0; i < nr_nodemap_entries; i++)
3452 printk(" %3d: %8lu -> %8lu\n", early_node_map[i].nid,
3453 early_node_map[i].start_pfn,
3454 early_node_map[i].end_pfn);
3455
3456 /* Initialise every node */
Christoph Lameter8ef82862007-02-20 13:57:52 -08003457 setup_nr_node_ids();
Mel Gormanc7132162006-09-27 01:49:43 -07003458 for_each_online_node(nid) {
3459 pg_data_t *pgdat = NODE_DATA(nid);
3460 free_area_init_node(nid, pgdat, NULL,
3461 find_min_pfn_for_node(nid), NULL);
3462 }
3463}
Mel Gorman2a1e2742007-07-17 04:03:12 -07003464
Mel Gorman7e63efe2007-07-17 04:03:15 -07003465static int __init cmdline_parse_core(char *p, unsigned long *core)
Mel Gorman2a1e2742007-07-17 04:03:12 -07003466{
3467 unsigned long long coremem;
3468 if (!p)
3469 return -EINVAL;
3470
3471 coremem = memparse(p, &p);
Mel Gorman7e63efe2007-07-17 04:03:15 -07003472 *core = coremem >> PAGE_SHIFT;
Mel Gorman2a1e2742007-07-17 04:03:12 -07003473
Mel Gorman7e63efe2007-07-17 04:03:15 -07003474 /* Paranoid check that UL is enough for the coremem value */
Mel Gorman2a1e2742007-07-17 04:03:12 -07003475 WARN_ON((coremem >> PAGE_SHIFT) > ULONG_MAX);
3476
3477 return 0;
3478}
Mel Gormaned7ed362007-07-17 04:03:14 -07003479
Mel Gorman7e63efe2007-07-17 04:03:15 -07003480/*
3481 * kernelcore=size sets the amount of memory for use for allocations that
3482 * cannot be reclaimed or migrated.
3483 */
3484static int __init cmdline_parse_kernelcore(char *p)
3485{
3486 return cmdline_parse_core(p, &required_kernelcore);
3487}
3488
3489/*
3490 * movablecore=size sets the amount of memory for use for allocations that
3491 * can be reclaimed or migrated.
3492 */
3493static int __init cmdline_parse_movablecore(char *p)
3494{
3495 return cmdline_parse_core(p, &required_movablecore);
3496}
3497
Mel Gormaned7ed362007-07-17 04:03:14 -07003498early_param("kernelcore", cmdline_parse_kernelcore);
Mel Gorman7e63efe2007-07-17 04:03:15 -07003499early_param("movablecore", cmdline_parse_movablecore);
Mel Gormaned7ed362007-07-17 04:03:14 -07003500
Mel Gormanc7132162006-09-27 01:49:43 -07003501#endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
3502
Mel Gorman0e0b8642006-09-27 01:49:56 -07003503/**
Randy Dunlap88ca3b92006-10-04 02:15:25 -07003504 * set_dma_reserve - set the specified number of pages reserved in the first zone
3505 * @new_dma_reserve: The number of pages to mark reserved
Mel Gorman0e0b8642006-09-27 01:49:56 -07003506 *
3507 * The per-cpu batchsize and zone watermarks are determined by present_pages.
3508 * In the DMA zone, a significant percentage may be consumed by kernel image
3509 * and other unfreeable allocations which can skew the watermarks badly. This
Randy Dunlap88ca3b92006-10-04 02:15:25 -07003510 * function may optionally be used to account for unfreeable pages in the
3511 * first zone (e.g., ZONE_DMA). The effect will be lower watermarks and
3512 * smaller per-cpu batchsize.
Mel Gorman0e0b8642006-09-27 01:49:56 -07003513 */
3514void __init set_dma_reserve(unsigned long new_dma_reserve)
3515{
3516 dma_reserve = new_dma_reserve;
3517}
3518
Dave Hansen93b75042005-06-23 00:07:47 -07003519#ifndef CONFIG_NEED_MULTIPLE_NODES
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520static bootmem_data_t contig_bootmem_data;
3521struct pglist_data contig_page_data = { .bdata = &contig_bootmem_data };
3522
3523EXPORT_SYMBOL(contig_page_data);
Dave Hansen93b75042005-06-23 00:07:47 -07003524#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525
3526void __init free_area_init(unsigned long *zones_size)
3527{
Dave Hansen93b75042005-06-23 00:07:47 -07003528 free_area_init_node(0, NODE_DATA(0), zones_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
3530}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532static int page_alloc_cpu_notify(struct notifier_block *self,
3533 unsigned long action, void *hcpu)
3534{
3535 int cpu = (unsigned long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003537 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 local_irq_disable();
3539 __drain_pages(cpu);
Christoph Lameterf8891e52006-06-30 01:55:45 -07003540 vm_events_fold_cpu(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 local_irq_enable();
Christoph Lameter2244b952006-06-30 01:55:33 -07003542 refresh_cpu_vm_stats(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 }
3544 return NOTIFY_OK;
3545}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546
3547void __init page_alloc_init(void)
3548{
3549 hotcpu_notifier(page_alloc_cpu_notify, 0);
3550}
3551
3552/*
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07003553 * calculate_totalreserve_pages - called when sysctl_lower_zone_reserve_ratio
3554 * or min_free_kbytes changes.
3555 */
3556static void calculate_totalreserve_pages(void)
3557{
3558 struct pglist_data *pgdat;
3559 unsigned long reserve_pages = 0;
Christoph Lameter2f6726e2006-09-25 23:31:18 -07003560 enum zone_type i, j;
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07003561
3562 for_each_online_pgdat(pgdat) {
3563 for (i = 0; i < MAX_NR_ZONES; i++) {
3564 struct zone *zone = pgdat->node_zones + i;
3565 unsigned long max = 0;
3566
3567 /* Find valid and maximum lowmem_reserve in the zone */
3568 for (j = i; j < MAX_NR_ZONES; j++) {
3569 if (zone->lowmem_reserve[j] > max)
3570 max = zone->lowmem_reserve[j];
3571 }
3572
3573 /* we treat pages_high as reserved pages. */
3574 max += zone->pages_high;
3575
3576 if (max > zone->present_pages)
3577 max = zone->present_pages;
3578 reserve_pages += max;
3579 }
3580 }
3581 totalreserve_pages = reserve_pages;
3582}
3583
3584/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 * setup_per_zone_lowmem_reserve - called whenever
3586 * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
3587 * has a correct pages reserved value, so an adequate number of
3588 * pages are left in the zone after a successful __alloc_pages().
3589 */
3590static void setup_per_zone_lowmem_reserve(void)
3591{
3592 struct pglist_data *pgdat;
Christoph Lameter2f6726e2006-09-25 23:31:18 -07003593 enum zone_type j, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594
KAMEZAWA Hiroyukiec936fc2006-03-27 01:15:59 -08003595 for_each_online_pgdat(pgdat) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 for (j = 0; j < MAX_NR_ZONES; j++) {
3597 struct zone *zone = pgdat->node_zones + j;
3598 unsigned long present_pages = zone->present_pages;
3599
3600 zone->lowmem_reserve[j] = 0;
3601
Christoph Lameter2f6726e2006-09-25 23:31:18 -07003602 idx = j;
3603 while (idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 struct zone *lower_zone;
3605
Christoph Lameter2f6726e2006-09-25 23:31:18 -07003606 idx--;
3607
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 if (sysctl_lowmem_reserve_ratio[idx] < 1)
3609 sysctl_lowmem_reserve_ratio[idx] = 1;
3610
3611 lower_zone = pgdat->node_zones + idx;
3612 lower_zone->lowmem_reserve[j] = present_pages /
3613 sysctl_lowmem_reserve_ratio[idx];
3614 present_pages += lower_zone->present_pages;
3615 }
3616 }
3617 }
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07003618
3619 /* update totalreserve_pages */
3620 calculate_totalreserve_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621}
3622
Randy Dunlap88ca3b92006-10-04 02:15:25 -07003623/**
3624 * setup_per_zone_pages_min - called when min_free_kbytes changes.
3625 *
3626 * Ensures that the pages_{min,low,high} values for each zone are set correctly
3627 * with respect to min_free_kbytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 */
Dave Hansen3947be12005-10-29 18:16:54 -07003629void setup_per_zone_pages_min(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630{
3631 unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
3632 unsigned long lowmem_pages = 0;
3633 struct zone *zone;
3634 unsigned long flags;
3635
3636 /* Calculate total number of !ZONE_HIGHMEM pages */
3637 for_each_zone(zone) {
3638 if (!is_highmem(zone))
3639 lowmem_pages += zone->present_pages;
3640 }
3641
3642 for_each_zone(zone) {
Andrew Mortonac924c62006-05-15 09:43:59 -07003643 u64 tmp;
3644
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 spin_lock_irqsave(&zone->lru_lock, flags);
Andrew Mortonac924c62006-05-15 09:43:59 -07003646 tmp = (u64)pages_min * zone->present_pages;
3647 do_div(tmp, lowmem_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 if (is_highmem(zone)) {
3649 /*
Nick Piggin669ed172005-11-13 16:06:45 -08003650 * __GFP_HIGH and PF_MEMALLOC allocations usually don't
3651 * need highmem pages, so cap pages_min to a small
3652 * value here.
3653 *
3654 * The (pages_high-pages_low) and (pages_low-pages_min)
3655 * deltas controls asynch page reclaim, and so should
3656 * not be capped for highmem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 */
3658 int min_pages;
3659
3660 min_pages = zone->present_pages / 1024;
3661 if (min_pages < SWAP_CLUSTER_MAX)
3662 min_pages = SWAP_CLUSTER_MAX;
3663 if (min_pages > 128)
3664 min_pages = 128;
3665 zone->pages_min = min_pages;
3666 } else {
Nick Piggin669ed172005-11-13 16:06:45 -08003667 /*
3668 * If it's a lowmem zone, reserve a number of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 * proportionate to the zone's size.
3670 */
Nick Piggin669ed172005-11-13 16:06:45 -08003671 zone->pages_min = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 }
3673
Andrew Mortonac924c62006-05-15 09:43:59 -07003674 zone->pages_low = zone->pages_min + (tmp >> 2);
3675 zone->pages_high = zone->pages_min + (tmp >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 spin_unlock_irqrestore(&zone->lru_lock, flags);
3677 }
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07003678
3679 /* update totalreserve_pages */
3680 calculate_totalreserve_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681}
3682
3683/*
3684 * Initialise min_free_kbytes.
3685 *
3686 * For small machines we want it small (128k min). For large machines
3687 * we want it large (64MB max). But it is not linear, because network
3688 * bandwidth does not increase linearly with machine size. We use
3689 *
3690 * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
3691 * min_free_kbytes = sqrt(lowmem_kbytes * 16)
3692 *
3693 * which yields
3694 *
3695 * 16MB: 512k
3696 * 32MB: 724k
3697 * 64MB: 1024k
3698 * 128MB: 1448k
3699 * 256MB: 2048k
3700 * 512MB: 2896k
3701 * 1024MB: 4096k
3702 * 2048MB: 5792k
3703 * 4096MB: 8192k
3704 * 8192MB: 11584k
3705 * 16384MB: 16384k
3706 */
3707static int __init init_per_zone_pages_min(void)
3708{
3709 unsigned long lowmem_kbytes;
3710
3711 lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
3712
3713 min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
3714 if (min_free_kbytes < 128)
3715 min_free_kbytes = 128;
3716 if (min_free_kbytes > 65536)
3717 min_free_kbytes = 65536;
3718 setup_per_zone_pages_min();
3719 setup_per_zone_lowmem_reserve();
3720 return 0;
3721}
3722module_init(init_per_zone_pages_min)
3723
3724/*
3725 * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
3726 * that we can call two helper functions whenever min_free_kbytes
3727 * changes.
3728 */
3729int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
3730 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
3731{
3732 proc_dointvec(table, write, file, buffer, length, ppos);
Mel Gorman3b1d92c2007-05-06 14:49:30 -07003733 if (write)
3734 setup_per_zone_pages_min();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 return 0;
3736}
3737
Christoph Lameter96146342006-07-03 00:24:13 -07003738#ifdef CONFIG_NUMA
3739int sysctl_min_unmapped_ratio_sysctl_handler(ctl_table *table, int write,
3740 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
3741{
3742 struct zone *zone;
3743 int rc;
3744
3745 rc = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
3746 if (rc)
3747 return rc;
3748
3749 for_each_zone(zone)
Christoph Lameter8417bba2006-09-25 23:31:51 -07003750 zone->min_unmapped_pages = (zone->present_pages *
Christoph Lameter96146342006-07-03 00:24:13 -07003751 sysctl_min_unmapped_ratio) / 100;
3752 return 0;
3753}
Christoph Lameter0ff38492006-09-25 23:31:52 -07003754
3755int sysctl_min_slab_ratio_sysctl_handler(ctl_table *table, int write,
3756 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
3757{
3758 struct zone *zone;
3759 int rc;
3760
3761 rc = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
3762 if (rc)
3763 return rc;
3764
3765 for_each_zone(zone)
3766 zone->min_slab_pages = (zone->present_pages *
3767 sysctl_min_slab_ratio) / 100;
3768 return 0;
3769}
Christoph Lameter96146342006-07-03 00:24:13 -07003770#endif
3771
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772/*
3773 * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
3774 * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
3775 * whenever sysctl_lowmem_reserve_ratio changes.
3776 *
3777 * The reserve ratio obviously has absolutely no relation with the
3778 * pages_min watermarks. The lowmem reserve ratio can only make sense
3779 * if in function of the boot time zone sizes.
3780 */
3781int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
3782 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
3783{
3784 proc_dointvec_minmax(table, write, file, buffer, length, ppos);
3785 setup_per_zone_lowmem_reserve();
3786 return 0;
3787}
3788
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08003789/*
3790 * percpu_pagelist_fraction - changes the pcp->high for each zone on each
3791 * cpu. It is the fraction of total pages in each zone that a hot per cpu pagelist
3792 * can have before it gets flushed back to buddy allocator.
3793 */
3794
3795int percpu_pagelist_fraction_sysctl_handler(ctl_table *table, int write,
3796 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
3797{
3798 struct zone *zone;
3799 unsigned int cpu;
3800 int ret;
3801
3802 ret = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
3803 if (!write || (ret == -EINVAL))
3804 return ret;
3805 for_each_zone(zone) {
3806 for_each_online_cpu(cpu) {
3807 unsigned long high;
3808 high = zone->present_pages / percpu_pagelist_fraction;
3809 setup_pagelist_highmark(zone_pcp(zone, cpu), high);
3810 }
3811 }
3812 return 0;
3813}
3814
David S. Millerf034b5d2006-08-24 03:08:07 -07003815int hashdist = HASHDIST_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816
3817#ifdef CONFIG_NUMA
3818static int __init set_hashdist(char *str)
3819{
3820 if (!str)
3821 return 0;
3822 hashdist = simple_strtoul(str, &str, 0);
3823 return 1;
3824}
3825__setup("hashdist=", set_hashdist);
3826#endif
3827
3828/*
3829 * allocate a large system hash table from bootmem
3830 * - it is assumed that the hash table must contain an exact power-of-2
3831 * quantity of entries
3832 * - limit is the number of hash buckets, not the total allocation size
3833 */
3834void *__init alloc_large_system_hash(const char *tablename,
3835 unsigned long bucketsize,
3836 unsigned long numentries,
3837 int scale,
3838 int flags,
3839 unsigned int *_hash_shift,
3840 unsigned int *_hash_mask,
3841 unsigned long limit)
3842{
3843 unsigned long long max = limit;
3844 unsigned long log2qty, size;
3845 void *table = NULL;
3846
3847 /* allow the kernel cmdline to have a say */
3848 if (!numentries) {
3849 /* round applicable memory size up to nearest megabyte */
Andrew Morton04903662006-12-06 20:37:33 -08003850 numentries = nr_kernel_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
3852 numentries >>= 20 - PAGE_SHIFT;
3853 numentries <<= 20 - PAGE_SHIFT;
3854
3855 /* limit to 1 bucket per 2^scale bytes of low memory */
3856 if (scale > PAGE_SHIFT)
3857 numentries >>= (scale - PAGE_SHIFT);
3858 else
3859 numentries <<= (PAGE_SHIFT - scale);
Paul Mundt9ab37b82007-01-05 16:36:30 -08003860
3861 /* Make sure we've got at least a 0-order allocation.. */
3862 if (unlikely((numentries * bucketsize) < PAGE_SIZE))
3863 numentries = PAGE_SIZE / bucketsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 }
John Hawkes6e692ed2006-03-25 03:08:02 -08003865 numentries = roundup_pow_of_two(numentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866
3867 /* limit allocation size to 1/16 total memory by default */
3868 if (max == 0) {
3869 max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
3870 do_div(max, bucketsize);
3871 }
3872
3873 if (numentries > max)
3874 numentries = max;
3875
David Howellsf0d1b0b2006-12-08 02:37:49 -08003876 log2qty = ilog2(numentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877
3878 do {
3879 size = bucketsize << log2qty;
3880 if (flags & HASH_EARLY)
3881 table = alloc_bootmem(size);
3882 else if (hashdist)
3883 table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
3884 else {
3885 unsigned long order;
3886 for (order = 0; ((1UL << order) << PAGE_SHIFT) < size; order++)
3887 ;
3888 table = (void*) __get_free_pages(GFP_ATOMIC, order);
Eric Dumazet1037b832007-07-15 23:38:05 -07003889 /*
3890 * If bucketsize is not a power-of-two, we may free
3891 * some pages at the end of hash table.
3892 */
3893 if (table) {
3894 unsigned long alloc_end = (unsigned long)table +
3895 (PAGE_SIZE << order);
3896 unsigned long used = (unsigned long)table +
3897 PAGE_ALIGN(size);
3898 split_page(virt_to_page(table), order);
3899 while (used < alloc_end) {
3900 free_page(used);
3901 used += PAGE_SIZE;
3902 }
3903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 }
3905 } while (!table && size > PAGE_SIZE && --log2qty);
3906
3907 if (!table)
3908 panic("Failed to allocate %s hash table\n", tablename);
3909
Dan Alonib49ad482007-07-15 23:38:23 -07003910 printk(KERN_INFO "%s hash table entries: %d (order: %d, %lu bytes)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 tablename,
3912 (1U << log2qty),
David Howellsf0d1b0b2006-12-08 02:37:49 -08003913 ilog2(size) - PAGE_SHIFT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 size);
3915
3916 if (_hash_shift)
3917 *_hash_shift = log2qty;
3918 if (_hash_mask)
3919 *_hash_mask = (1 << log2qty) - 1;
3920
3921 return table;
3922}
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -08003923
3924#ifdef CONFIG_OUT_OF_LINE_PFN_TO_PAGE
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -08003925struct page *pfn_to_page(unsigned long pfn)
3926{
Andy Whitcroft67de6482006-06-23 02:03:12 -07003927 return __pfn_to_page(pfn);
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -08003928}
3929unsigned long page_to_pfn(struct page *page)
3930{
Andy Whitcroft67de6482006-06-23 02:03:12 -07003931 return __page_to_pfn(page);
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -08003932}
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -08003933EXPORT_SYMBOL(pfn_to_page);
3934EXPORT_SYMBOL(page_to_pfn);
3935#endif /* CONFIG_OUT_OF_LINE_PFN_TO_PAGE */
Andrew Morton6220ec72006-10-19 23:29:05 -07003936
Andrew Morton6220ec72006-10-19 23:29:05 -07003937