blob: ac4f8c6b5c10771b71196c0af1238e894d225655 [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;
Mel Gorman2a1e2742007-07-17 04:03:12 -0700141 unsigned long __initdata zone_movable_pfn[MAX_NUMNODES];
142
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 Morton6aa30012006-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 Morton6aa30012006-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 Morton6aa30012006-04-18 22:20:52 -0700373 return 1;
Nick Piggin676165a2006-04-10 11:21:48 +1000374 }
Andrew Morton6aa30012006-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 Piggin92be2e32006-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);
Christoph Lameterd85f3382007-05-06 14:49:39 -0700456 /*
457 * PageReclaim == PageTail. It is only an error
458 * for PageReclaim to be set if PageCompound is clear.
459 */
460 if (unlikely(!PageCompound(page) && PageReclaim(page)))
461 bad_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (PageDirty(page))
Nick Piggin242e5462005-09-03 15:54:50 -0700463 __ClearPageDirty(page);
Hugh Dickins689bceb2005-11-21 21:32:20 -0800464 /*
465 * For now, we report if PG_reserved was found set, but do not
466 * clear it, and do not free the page. But we shall soon need
467 * to do more, for when the ZERO_PAGE count wraps negative.
468 */
469 return PageReserved(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
472/*
473 * Frees a list of pages.
474 * Assumes all pages on list are in same zone, and of same order.
Renaud Lienhart207f36e2005-09-10 00:26:59 -0700475 * count is the number of pages to free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 *
477 * If the zone was previously in an "all pages pinned" state then look to
478 * see if this freeing clears that state.
479 *
480 * And clear the zone's pages_scanned counter, to hold off the "all pages are
481 * pinned" detection logic.
482 */
Nick Piggin48db57f2006-01-08 01:00:42 -0800483static void free_pages_bulk(struct zone *zone, int count,
484 struct list_head *list, int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Nick Pigginc54ad302006-01-06 00:10:56 -0800486 spin_lock(&zone->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 zone->all_unreclaimable = 0;
488 zone->pages_scanned = 0;
Nick Piggin48db57f2006-01-08 01:00:42 -0800489 while (count--) {
490 struct page *page;
491
Nick Piggin725d7042006-09-25 23:30:55 -0700492 VM_BUG_ON(list_empty(list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 page = list_entry(list->prev, struct page, lru);
Nick Piggin48db57f2006-01-08 01:00:42 -0800494 /* have to delete it as __free_one_page list manipulates */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 list_del(&page->lru);
Nick Piggin48db57f2006-01-08 01:00:42 -0800496 __free_one_page(page, zone, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
Nick Pigginc54ad302006-01-06 00:10:56 -0800498 spin_unlock(&zone->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
Nick Piggin48db57f2006-01-08 01:00:42 -0800501static void free_one_page(struct zone *zone, struct page *page, int order)
502{
Christoph Lameter006d22d2006-09-25 23:31:48 -0700503 spin_lock(&zone->lock);
504 zone->all_unreclaimable = 0;
505 zone->pages_scanned = 0;
Paul Jackson0798e512006-12-06 20:31:38 -0800506 __free_one_page(page, zone, order);
Christoph Lameter006d22d2006-09-25 23:31:48 -0700507 spin_unlock(&zone->lock);
Nick Piggin48db57f2006-01-08 01:00:42 -0800508}
509
510static void __free_pages_ok(struct page *page, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Nick Pigginc54ad302006-01-06 00:10:56 -0800512 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 int i;
Hugh Dickins689bceb2005-11-21 21:32:20 -0800514 int reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 for (i = 0 ; i < (1 << order) ; ++i)
Nick Piggin224abf92006-01-06 00:11:11 -0800517 reserved += free_pages_check(page + i);
Hugh Dickins689bceb2005-11-21 21:32:20 -0800518 if (reserved)
519 return;
520
Nick Piggin9858db52006-10-11 01:21:30 -0700521 if (!PageHighMem(page))
522 debug_check_no_locks_freed(page_address(page),PAGE_SIZE<<order);
Nick Piggindafb1362006-10-11 01:21:30 -0700523 arch_free_page(page, order);
Nick Piggin48db57f2006-01-08 01:00:42 -0800524 kernel_map_pages(page, 1 << order, 0);
Nick Piggindafb1362006-10-11 01:21:30 -0700525
Nick Pigginc54ad302006-01-06 00:10:56 -0800526 local_irq_save(flags);
Christoph Lameterf8891e52006-06-30 01:55:45 -0700527 __count_vm_events(PGFREE, 1 << order);
Nick Piggin48db57f2006-01-08 01:00:42 -0800528 free_one_page(page_zone(page), page, order);
Nick Pigginc54ad302006-01-06 00:10:56 -0800529 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
David Howellsa226f6c2006-01-06 00:11:08 -0800532/*
533 * permit the bootmem allocator to evade page validation on high-order frees
534 */
535void fastcall __init __free_pages_bootmem(struct page *page, unsigned int order)
536{
537 if (order == 0) {
538 __ClearPageReserved(page);
539 set_page_count(page, 0);
Nick Piggin7835e982006-03-22 00:08:40 -0800540 set_page_refcounted(page);
Nick Piggin545b1ea2006-03-22 00:08:07 -0800541 __free_page(page);
David Howellsa226f6c2006-01-06 00:11:08 -0800542 } else {
David Howellsa226f6c2006-01-06 00:11:08 -0800543 int loop;
544
Nick Piggin545b1ea2006-03-22 00:08:07 -0800545 prefetchw(page);
David Howellsa226f6c2006-01-06 00:11:08 -0800546 for (loop = 0; loop < BITS_PER_LONG; loop++) {
547 struct page *p = &page[loop];
548
Nick Piggin545b1ea2006-03-22 00:08:07 -0800549 if (loop + 1 < BITS_PER_LONG)
550 prefetchw(p + 1);
David Howellsa226f6c2006-01-06 00:11:08 -0800551 __ClearPageReserved(p);
552 set_page_count(p, 0);
553 }
554
Nick Piggin7835e982006-03-22 00:08:40 -0800555 set_page_refcounted(page);
Nick Piggin545b1ea2006-03-22 00:08:07 -0800556 __free_pages(page, order);
David Howellsa226f6c2006-01-06 00:11:08 -0800557 }
558}
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561/*
562 * The order of subdivision here is critical for the IO subsystem.
563 * Please do not alter this order without good reasons and regression
564 * testing. Specifically, as large blocks of memory are subdivided,
565 * the order in which smaller blocks are delivered depends on the order
566 * they're subdivided in this function. This is the primary factor
567 * influencing the order in which pages are delivered to the IO
568 * subsystem according to empirical testing, and this is also justified
569 * by considering the behavior of a buddy system containing a single
570 * large block of memory acted on by a series of small allocations.
571 * This behavior is a critical factor in sglist merging's success.
572 *
573 * -- wli
574 */
Nick Piggin085cc7d2006-01-06 00:11:01 -0800575static inline void expand(struct zone *zone, struct page *page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 int low, int high, struct free_area *area)
577{
578 unsigned long size = 1 << high;
579
580 while (high > low) {
581 area--;
582 high--;
583 size >>= 1;
Nick Piggin725d7042006-09-25 23:30:55 -0700584 VM_BUG_ON(bad_range(zone, &page[size]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 list_add(&page[size].lru, &area->free_list);
586 area->nr_free++;
587 set_page_order(&page[size], high);
588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591/*
592 * This page is about to be returned from the page allocator
593 */
Nick Piggin17cf4402006-03-22 00:08:41 -0800594static int prep_new_page(struct page *page, int order, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Nick Piggin92be2e32006-01-06 00:10:57 -0800596 if (unlikely(page_mapcount(page) |
597 (page->mapping != NULL) |
598 (page_count(page) != 0) |
Hugh Dickins334795e2005-06-21 17:15:08 -0700599 (page->flags & (
600 1 << PG_lru |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 1 << PG_private |
602 1 << PG_locked |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 1 << PG_active |
604 1 << PG_dirty |
605 1 << PG_reclaim |
Hugh Dickins334795e2005-06-21 17:15:08 -0700606 1 << PG_slab |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 1 << PG_swapcache |
Nick Pigginb5810032005-10-29 18:16:12 -0700608 1 << PG_writeback |
Nick Piggin676165a2006-04-10 11:21:48 +1000609 1 << PG_reserved |
610 1 << PG_buddy ))))
Nick Piggin224abf92006-01-06 00:11:11 -0800611 bad_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Hugh Dickins689bceb2005-11-21 21:32:20 -0800613 /*
614 * For now, we report if PG_reserved was found set, but do not
615 * clear it, and do not allocate the page: as a safety net.
616 */
617 if (PageReserved(page))
618 return 1;
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 page->flags &= ~(1 << PG_uptodate | 1 << PG_error |
621 1 << PG_referenced | 1 << PG_arch_1 |
Nick Piggin5409bae2007-02-28 20:12:27 -0800622 1 << PG_owner_priv_1 | 1 << PG_mappedtodisk);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700623 set_page_private(page, 0);
Nick Piggin7835e982006-03-22 00:08:40 -0800624 set_page_refcounted(page);
Nick Piggincc102502006-12-06 20:32:00 -0800625
626 arch_alloc_page(page, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 kernel_map_pages(page, 1 << order, 1);
Nick Piggin17cf4402006-03-22 00:08:41 -0800628
629 if (gfp_flags & __GFP_ZERO)
630 prep_zero_page(page, order, gfp_flags);
631
632 if (order && (gfp_flags & __GFP_COMP))
633 prep_compound_page(page, order);
634
Hugh Dickins689bceb2005-11-21 21:32:20 -0800635 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
638/*
639 * Do the hard work of removing an element from the buddy allocator.
640 * Call me with the zone->lock already held.
641 */
642static struct page *__rmqueue(struct zone *zone, unsigned int order)
643{
644 struct free_area * area;
645 unsigned int current_order;
646 struct page *page;
647
648 for (current_order = order; current_order < MAX_ORDER; ++current_order) {
649 area = zone->free_area + current_order;
650 if (list_empty(&area->free_list))
651 continue;
652
653 page = list_entry(area->free_list.next, struct page, lru);
654 list_del(&page->lru);
655 rmv_page_order(page);
656 area->nr_free--;
Christoph Lameterd23ad422007-02-10 01:43:02 -0800657 __mod_zone_page_state(zone, NR_FREE_PAGES, - (1UL << order));
Nick Piggin085cc7d2006-01-06 00:11:01 -0800658 expand(zone, page, order, current_order, area);
659 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
661
662 return NULL;
663}
664
665/*
666 * Obtain a specified number of elements from the buddy allocator, all under
667 * a single hold of the lock, for efficiency. Add them to the supplied list.
668 * Returns the number of new pages which were placed at *list.
669 */
670static int rmqueue_bulk(struct zone *zone, unsigned int order,
671 unsigned long count, struct list_head *list)
672{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Nick Pigginc54ad302006-01-06 00:10:56 -0800675 spin_lock(&zone->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 for (i = 0; i < count; ++i) {
Nick Piggin085cc7d2006-01-06 00:11:01 -0800677 struct page *page = __rmqueue(zone, order);
678 if (unlikely(page == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 list_add_tail(&page->lru, list);
681 }
Nick Pigginc54ad302006-01-06 00:10:56 -0800682 spin_unlock(&zone->lock);
Nick Piggin085cc7d2006-01-06 00:11:01 -0800683 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
Christoph Lameter4ae7c032005-06-21 17:14:57 -0700686#ifdef CONFIG_NUMA
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800687/*
Christoph Lameter4037d452007-05-09 02:35:14 -0700688 * Called from the vmstat counter updater to drain pagesets of this
689 * currently executing processor on remote nodes after they have
690 * expired.
691 *
Christoph Lameter879336c2006-03-22 00:09:08 -0800692 * Note that this function must be called with the thread pinned to
693 * a single processor.
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800694 */
Christoph Lameter4037d452007-05-09 02:35:14 -0700695void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
Christoph Lameter4ae7c032005-06-21 17:14:57 -0700696{
Christoph Lameter4ae7c032005-06-21 17:14:57 -0700697 unsigned long flags;
Christoph Lameter4037d452007-05-09 02:35:14 -0700698 int to_drain;
Christoph Lameter4ae7c032005-06-21 17:14:57 -0700699
Christoph Lameter4037d452007-05-09 02:35:14 -0700700 local_irq_save(flags);
701 if (pcp->count >= pcp->batch)
702 to_drain = pcp->batch;
703 else
704 to_drain = pcp->count;
705 free_pages_bulk(zone, to_drain, &pcp->list, 0);
706 pcp->count -= to_drain;
707 local_irq_restore(flags);
Christoph Lameter4ae7c032005-06-21 17:14:57 -0700708}
709#endif
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711static void __drain_pages(unsigned int cpu)
712{
Nick Pigginc54ad302006-01-06 00:10:56 -0800713 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 struct zone *zone;
715 int i;
716
717 for_each_zone(zone) {
718 struct per_cpu_pageset *pset;
719
Christoph Lameterf2e12bb2007-01-05 16:37:02 -0800720 if (!populated_zone(zone))
721 continue;
722
Christoph Lametere7c8d5c2005-06-21 17:14:47 -0700723 pset = zone_pcp(zone, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
725 struct per_cpu_pages *pcp;
726
727 pcp = &pset->pcp[i];
Nick Pigginc54ad302006-01-06 00:10:56 -0800728 local_irq_save(flags);
Nick Piggin48db57f2006-01-08 01:00:42 -0800729 free_pages_bulk(zone, pcp->count, &pcp->list, 0);
730 pcp->count = 0;
Nick Pigginc54ad302006-01-06 00:10:56 -0800731 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
733 }
734}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736#ifdef CONFIG_PM
737
738void mark_free_pages(struct zone *zone)
739{
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700740 unsigned long pfn, max_zone_pfn;
741 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 int order;
743 struct list_head *curr;
744
745 if (!zone->spanned_pages)
746 return;
747
748 spin_lock_irqsave(&zone->lock, flags);
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700749
750 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
751 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
752 if (pfn_valid(pfn)) {
753 struct page *page = pfn_to_page(pfn);
754
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700755 if (!swsusp_page_is_forbidden(page))
756 swsusp_unset_page_free(page);
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 for (order = MAX_ORDER - 1; order >= 0; --order)
760 list_for_each(curr, &zone->free_area[order].free_list) {
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700761 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700763 pfn = page_to_pfn(list_entry(curr, struct page, lru));
764 for (i = 0; i < (1UL << order); i++)
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700765 swsusp_set_page_free(pfn_to_page(pfn + i));
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 spin_unlock_irqrestore(&zone->lock, flags);
769}
770
771/*
772 * Spill all of this CPU's per-cpu pages back into the buddy allocator.
773 */
774void drain_local_pages(void)
775{
776 unsigned long flags;
777
778 local_irq_save(flags);
779 __drain_pages(smp_processor_id());
780 local_irq_restore(flags);
781}
782#endif /* CONFIG_PM */
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784/*
785 * Free a 0-order page
786 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787static void fastcall free_hot_cold_page(struct page *page, int cold)
788{
789 struct zone *zone = page_zone(page);
790 struct per_cpu_pages *pcp;
791 unsigned long flags;
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (PageAnon(page))
794 page->mapping = NULL;
Nick Piggin224abf92006-01-06 00:11:11 -0800795 if (free_pages_check(page))
Hugh Dickins689bceb2005-11-21 21:32:20 -0800796 return;
797
Nick Piggin9858db52006-10-11 01:21:30 -0700798 if (!PageHighMem(page))
799 debug_check_no_locks_freed(page_address(page), PAGE_SIZE);
Nick Piggindafb1362006-10-11 01:21:30 -0700800 arch_free_page(page, 0);
Hugh Dickins689bceb2005-11-21 21:32:20 -0800801 kernel_map_pages(page, 1, 0);
802
Christoph Lametere7c8d5c2005-06-21 17:14:47 -0700803 pcp = &zone_pcp(zone, get_cpu())->pcp[cold];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 local_irq_save(flags);
Christoph Lameterf8891e52006-06-30 01:55:45 -0700805 __count_vm_event(PGFREE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 list_add(&page->lru, &pcp->list);
807 pcp->count++;
Nick Piggin48db57f2006-01-08 01:00:42 -0800808 if (pcp->count >= pcp->high) {
809 free_pages_bulk(zone, pcp->batch, &pcp->list, 0);
810 pcp->count -= pcp->batch;
811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 local_irq_restore(flags);
813 put_cpu();
814}
815
816void fastcall free_hot_page(struct page *page)
817{
818 free_hot_cold_page(page, 0);
819}
820
821void fastcall free_cold_page(struct page *page)
822{
823 free_hot_cold_page(page, 1);
824}
825
Nick Piggin8dfcc9b2006-03-22 00:08:05 -0800826/*
827 * split_page takes a non-compound higher-order page, and splits it into
828 * n (1<<order) sub-pages: page[0..n]
829 * Each sub-page must be freed individually.
830 *
831 * Note: this is probably too low level an operation for use in drivers.
832 * Please consult with lkml before using this in your driver.
833 */
834void split_page(struct page *page, unsigned int order)
835{
836 int i;
837
Nick Piggin725d7042006-09-25 23:30:55 -0700838 VM_BUG_ON(PageCompound(page));
839 VM_BUG_ON(!page_count(page));
Nick Piggin7835e982006-03-22 00:08:40 -0800840 for (i = 1; i < (1 << order); i++)
841 set_page_refcounted(page + i);
Nick Piggin8dfcc9b2006-03-22 00:08:05 -0800842}
Nick Piggin8dfcc9b2006-03-22 00:08:05 -0800843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844/*
845 * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
846 * we cheat by calling it from here, in the order > 0 path. Saves a branch
847 * or two.
848 */
Nick Piggina74609f2006-01-06 00:11:20 -0800849static struct page *buffered_rmqueue(struct zonelist *zonelist,
850 struct zone *zone, int order, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
852 unsigned long flags;
Hugh Dickins689bceb2005-11-21 21:32:20 -0800853 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 int cold = !!(gfp_flags & __GFP_COLD);
Nick Piggina74609f2006-01-06 00:11:20 -0800855 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Hugh Dickins689bceb2005-11-21 21:32:20 -0800857again:
Nick Piggina74609f2006-01-06 00:11:20 -0800858 cpu = get_cpu();
Nick Piggin48db57f2006-01-08 01:00:42 -0800859 if (likely(order == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 struct per_cpu_pages *pcp;
861
Nick Piggina74609f2006-01-06 00:11:20 -0800862 pcp = &zone_pcp(zone, cpu)->pcp[cold];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 local_irq_save(flags);
Nick Piggina74609f2006-01-06 00:11:20 -0800864 if (!pcp->count) {
nkalmala941c7102006-11-02 22:07:04 -0800865 pcp->count = rmqueue_bulk(zone, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 pcp->batch, &pcp->list);
Nick Piggina74609f2006-01-06 00:11:20 -0800867 if (unlikely(!pcp->count))
868 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
Nick Piggina74609f2006-01-06 00:11:20 -0800870 page = list_entry(pcp->list.next, struct page, lru);
871 list_del(&page->lru);
872 pcp->count--;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800873 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 spin_lock_irqsave(&zone->lock, flags);
875 page = __rmqueue(zone, order);
Nick Piggina74609f2006-01-06 00:11:20 -0800876 spin_unlock(&zone->lock);
877 if (!page)
878 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 }
880
Christoph Lameterf8891e52006-06-30 01:55:45 -0700881 __count_zone_vm_events(PGALLOC, zone, 1 << order);
Christoph Lameterca889e62006-06-30 01:55:44 -0700882 zone_statistics(zonelist, zone);
Nick Piggina74609f2006-01-06 00:11:20 -0800883 local_irq_restore(flags);
884 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Nick Piggin725d7042006-09-25 23:30:55 -0700886 VM_BUG_ON(bad_range(zone, page));
Nick Piggin17cf4402006-03-22 00:08:41 -0800887 if (prep_new_page(page, order, gfp_flags))
Nick Piggina74609f2006-01-06 00:11:20 -0800888 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return page;
Nick Piggina74609f2006-01-06 00:11:20 -0800890
891failed:
892 local_irq_restore(flags);
893 put_cpu();
894 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895}
896
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800897#define ALLOC_NO_WATERMARKS 0x01 /* don't check watermarks at all */
Nick Piggin31488902005-11-28 13:44:03 -0800898#define ALLOC_WMARK_MIN 0x02 /* use pages_min watermark */
899#define ALLOC_WMARK_LOW 0x04 /* use pages_low watermark */
900#define ALLOC_WMARK_HIGH 0x08 /* use pages_high watermark */
901#define ALLOC_HARDER 0x10 /* try to alloc harder */
902#define ALLOC_HIGH 0x20 /* __GFP_HIGH set */
903#define ALLOC_CPUSET 0x40 /* check for correct cpuset */
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800904
Akinobu Mita933e3122006-12-08 02:39:45 -0800905#ifdef CONFIG_FAIL_PAGE_ALLOC
906
907static struct fail_page_alloc_attr {
908 struct fault_attr attr;
909
910 u32 ignore_gfp_highmem;
911 u32 ignore_gfp_wait;
Akinobu Mita54114992007-07-15 23:40:23 -0700912 u32 min_order;
Akinobu Mita933e3122006-12-08 02:39:45 -0800913
914#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
915
916 struct dentry *ignore_gfp_highmem_file;
917 struct dentry *ignore_gfp_wait_file;
Akinobu Mita54114992007-07-15 23:40:23 -0700918 struct dentry *min_order_file;
Akinobu Mita933e3122006-12-08 02:39:45 -0800919
920#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
921
922} fail_page_alloc = {
923 .attr = FAULT_ATTR_INITIALIZER,
Don Mullis6b1b60f2006-12-08 02:39:53 -0800924 .ignore_gfp_wait = 1,
925 .ignore_gfp_highmem = 1,
Akinobu Mita54114992007-07-15 23:40:23 -0700926 .min_order = 1,
Akinobu Mita933e3122006-12-08 02:39:45 -0800927};
928
929static int __init setup_fail_page_alloc(char *str)
930{
931 return setup_fault_attr(&fail_page_alloc.attr, str);
932}
933__setup("fail_page_alloc=", setup_fail_page_alloc);
934
935static int should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
936{
Akinobu Mita54114992007-07-15 23:40:23 -0700937 if (order < fail_page_alloc.min_order)
938 return 0;
Akinobu Mita933e3122006-12-08 02:39:45 -0800939 if (gfp_mask & __GFP_NOFAIL)
940 return 0;
941 if (fail_page_alloc.ignore_gfp_highmem && (gfp_mask & __GFP_HIGHMEM))
942 return 0;
943 if (fail_page_alloc.ignore_gfp_wait && (gfp_mask & __GFP_WAIT))
944 return 0;
945
946 return should_fail(&fail_page_alloc.attr, 1 << order);
947}
948
949#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
950
951static int __init fail_page_alloc_debugfs(void)
952{
953 mode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
954 struct dentry *dir;
955 int err;
956
957 err = init_fault_attr_dentries(&fail_page_alloc.attr,
958 "fail_page_alloc");
959 if (err)
960 return err;
961 dir = fail_page_alloc.attr.dentries.dir;
962
963 fail_page_alloc.ignore_gfp_wait_file =
964 debugfs_create_bool("ignore-gfp-wait", mode, dir,
965 &fail_page_alloc.ignore_gfp_wait);
966
967 fail_page_alloc.ignore_gfp_highmem_file =
968 debugfs_create_bool("ignore-gfp-highmem", mode, dir,
969 &fail_page_alloc.ignore_gfp_highmem);
Akinobu Mita54114992007-07-15 23:40:23 -0700970 fail_page_alloc.min_order_file =
971 debugfs_create_u32("min-order", mode, dir,
972 &fail_page_alloc.min_order);
Akinobu Mita933e3122006-12-08 02:39:45 -0800973
974 if (!fail_page_alloc.ignore_gfp_wait_file ||
Akinobu Mita54114992007-07-15 23:40:23 -0700975 !fail_page_alloc.ignore_gfp_highmem_file ||
976 !fail_page_alloc.min_order_file) {
Akinobu Mita933e3122006-12-08 02:39:45 -0800977 err = -ENOMEM;
978 debugfs_remove(fail_page_alloc.ignore_gfp_wait_file);
979 debugfs_remove(fail_page_alloc.ignore_gfp_highmem_file);
Akinobu Mita54114992007-07-15 23:40:23 -0700980 debugfs_remove(fail_page_alloc.min_order_file);
Akinobu Mita933e3122006-12-08 02:39:45 -0800981 cleanup_fault_attr_dentries(&fail_page_alloc.attr);
982 }
983
984 return err;
985}
986
987late_initcall(fail_page_alloc_debugfs);
988
989#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
990
991#else /* CONFIG_FAIL_PAGE_ALLOC */
992
993static inline int should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
994{
995 return 0;
996}
997
998#endif /* CONFIG_FAIL_PAGE_ALLOC */
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000/*
1001 * Return 1 if free pages are above 'mark'. This takes into account the order
1002 * of the allocation.
1003 */
1004int zone_watermark_ok(struct zone *z, int order, unsigned long mark,
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001005 int classzone_idx, int alloc_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
1007 /* free_pages my go negative - that's OK */
Christoph Lameterd23ad422007-02-10 01:43:02 -08001008 long min = mark;
1009 long free_pages = zone_page_state(z, NR_FREE_PAGES) - (1 << order) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 int o;
1011
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001012 if (alloc_flags & ALLOC_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 min -= min / 2;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001014 if (alloc_flags & ALLOC_HARDER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 min -= min / 4;
1016
1017 if (free_pages <= min + z->lowmem_reserve[classzone_idx])
1018 return 0;
1019 for (o = 0; o < order; o++) {
1020 /* At the next order, this order's pages become unavailable */
1021 free_pages -= z->free_area[o].nr_free << o;
1022
1023 /* Require fewer higher order pages to be free */
1024 min >>= 1;
1025
1026 if (free_pages <= min)
1027 return 0;
1028 }
1029 return 1;
1030}
1031
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001032#ifdef CONFIG_NUMA
1033/*
1034 * zlc_setup - Setup for "zonelist cache". Uses cached zone data to
1035 * skip over zones that are not allowed by the cpuset, or that have
1036 * been recently (in last second) found to be nearly full. See further
1037 * comments in mmzone.h. Reduces cache footprint of zonelist scans
1038 * that have to skip over alot of full or unallowed zones.
1039 *
1040 * If the zonelist cache is present in the passed in zonelist, then
1041 * returns a pointer to the allowed node mask (either the current
1042 * tasks mems_allowed, or node_online_map.)
1043 *
1044 * If the zonelist cache is not available for this zonelist, does
1045 * nothing and returns NULL.
1046 *
1047 * If the fullzones BITMAP in the zonelist cache is stale (more than
1048 * a second since last zap'd) then we zap it out (clear its bits.)
1049 *
1050 * We hold off even calling zlc_setup, until after we've checked the
1051 * first zone in the zonelist, on the theory that most allocations will
1052 * be satisfied from that first zone, so best to examine that zone as
1053 * quickly as we can.
1054 */
1055static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
1056{
1057 struct zonelist_cache *zlc; /* cached zonelist speedup info */
1058 nodemask_t *allowednodes; /* zonelist_cache approximation */
1059
1060 zlc = zonelist->zlcache_ptr;
1061 if (!zlc)
1062 return NULL;
1063
1064 if (jiffies - zlc->last_full_zap > 1 * HZ) {
1065 bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
1066 zlc->last_full_zap = jiffies;
1067 }
1068
1069 allowednodes = !in_interrupt() && (alloc_flags & ALLOC_CPUSET) ?
1070 &cpuset_current_mems_allowed :
1071 &node_online_map;
1072 return allowednodes;
1073}
1074
1075/*
1076 * Given 'z' scanning a zonelist, run a couple of quick checks to see
1077 * if it is worth looking at further for free memory:
1078 * 1) Check that the zone isn't thought to be full (doesn't have its
1079 * bit set in the zonelist_cache fullzones BITMAP).
1080 * 2) Check that the zones node (obtained from the zonelist_cache
1081 * z_to_n[] mapping) is allowed in the passed in allowednodes mask.
1082 * Return true (non-zero) if zone is worth looking at further, or
1083 * else return false (zero) if it is not.
1084 *
1085 * This check -ignores- the distinction between various watermarks,
1086 * such as GFP_HIGH, GFP_ATOMIC, PF_MEMALLOC, ... If a zone is
1087 * found to be full for any variation of these watermarks, it will
1088 * be considered full for up to one second by all requests, unless
1089 * we are so low on memory on all allowed nodes that we are forced
1090 * into the second scan of the zonelist.
1091 *
1092 * In the second scan we ignore this zonelist cache and exactly
1093 * apply the watermarks to all zones, even it is slower to do so.
1094 * We are low on memory in the second scan, and should leave no stone
1095 * unturned looking for a free page.
1096 */
1097static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zone **z,
1098 nodemask_t *allowednodes)
1099{
1100 struct zonelist_cache *zlc; /* cached zonelist speedup info */
1101 int i; /* index of *z in zonelist zones */
1102 int n; /* node that zone *z is on */
1103
1104 zlc = zonelist->zlcache_ptr;
1105 if (!zlc)
1106 return 1;
1107
1108 i = z - zonelist->zones;
1109 n = zlc->z_to_n[i];
1110
1111 /* This zone is worth trying if it is allowed but not full */
1112 return node_isset(n, *allowednodes) && !test_bit(i, zlc->fullzones);
1113}
1114
1115/*
1116 * Given 'z' scanning a zonelist, set the corresponding bit in
1117 * zlc->fullzones, so that subsequent attempts to allocate a page
1118 * from that zone don't waste time re-examining it.
1119 */
1120static void zlc_mark_zone_full(struct zonelist *zonelist, struct zone **z)
1121{
1122 struct zonelist_cache *zlc; /* cached zonelist speedup info */
1123 int i; /* index of *z in zonelist zones */
1124
1125 zlc = zonelist->zlcache_ptr;
1126 if (!zlc)
1127 return;
1128
1129 i = z - zonelist->zones;
1130
1131 set_bit(i, zlc->fullzones);
1132}
1133
1134#else /* CONFIG_NUMA */
1135
1136static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
1137{
1138 return NULL;
1139}
1140
1141static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zone **z,
1142 nodemask_t *allowednodes)
1143{
1144 return 1;
1145}
1146
1147static void zlc_mark_zone_full(struct zonelist *zonelist, struct zone **z)
1148{
1149}
1150#endif /* CONFIG_NUMA */
1151
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001152/*
Paul Jackson0798e512006-12-06 20:31:38 -08001153 * get_page_from_freelist goes through the zonelist trying to allocate
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001154 * a page.
1155 */
1156static struct page *
1157get_page_from_freelist(gfp_t gfp_mask, unsigned int order,
1158 struct zonelist *zonelist, int alloc_flags)
Martin Hicks753ee722005-06-21 17:14:41 -07001159{
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001160 struct zone **z;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001161 struct page *page = NULL;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001162 int classzone_idx = zone_idx(zonelist->zones[0]);
Christoph Lameter1192d522006-09-25 23:31:45 -07001163 struct zone *zone;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001164 nodemask_t *allowednodes = NULL;/* zonelist_cache approximation */
1165 int zlc_active = 0; /* set if using zonelist_cache */
1166 int did_zlc_setup = 0; /* just call zlc_setup() one time */
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001167
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001168zonelist_scan:
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001169 /*
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001170 * Scan zonelist, looking for a zone with enough free.
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001171 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
1172 */
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001173 z = zonelist->zones;
1174
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001175 do {
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001176 if (NUMA_BUILD && zlc_active &&
1177 !zlc_zone_worth_trying(zonelist, z, allowednodes))
1178 continue;
Christoph Lameter1192d522006-09-25 23:31:45 -07001179 zone = *z;
Christoph Lameter08e0f6a2006-09-27 01:50:06 -07001180 if (unlikely(NUMA_BUILD && (gfp_mask & __GFP_THISNODE) &&
Christoph Lameter1192d522006-09-25 23:31:45 -07001181 zone->zone_pgdat != zonelist->zones[0]->zone_pgdat))
Christoph Lameter9b819d22006-09-25 23:31:40 -07001182 break;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001183 if ((alloc_flags & ALLOC_CPUSET) &&
Paul Jackson02a0e532006-12-13 00:34:25 -08001184 !cpuset_zone_allowed_softwall(zone, gfp_mask))
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001185 goto try_next_zone;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001186
1187 if (!(alloc_flags & ALLOC_NO_WATERMARKS)) {
Nick Piggin31488902005-11-28 13:44:03 -08001188 unsigned long mark;
1189 if (alloc_flags & ALLOC_WMARK_MIN)
Christoph Lameter1192d522006-09-25 23:31:45 -07001190 mark = zone->pages_min;
Nick Piggin31488902005-11-28 13:44:03 -08001191 else if (alloc_flags & ALLOC_WMARK_LOW)
Christoph Lameter1192d522006-09-25 23:31:45 -07001192 mark = zone->pages_low;
Nick Piggin31488902005-11-28 13:44:03 -08001193 else
Christoph Lameter1192d522006-09-25 23:31:45 -07001194 mark = zone->pages_high;
Paul Jackson0798e512006-12-06 20:31:38 -08001195 if (!zone_watermark_ok(zone, order, mark,
1196 classzone_idx, alloc_flags)) {
Christoph Lameter9eeff232006-01-18 17:42:31 -08001197 if (!zone_reclaim_mode ||
Christoph Lameter1192d522006-09-25 23:31:45 -07001198 !zone_reclaim(zone, gfp_mask, order))
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001199 goto this_zone_full;
Paul Jackson0798e512006-12-06 20:31:38 -08001200 }
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001201 }
1202
Christoph Lameter1192d522006-09-25 23:31:45 -07001203 page = buffered_rmqueue(zonelist, zone, order, gfp_mask);
Paul Jackson0798e512006-12-06 20:31:38 -08001204 if (page)
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001205 break;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001206this_zone_full:
1207 if (NUMA_BUILD)
1208 zlc_mark_zone_full(zonelist, z);
1209try_next_zone:
1210 if (NUMA_BUILD && !did_zlc_setup) {
1211 /* we do zlc_setup after the first zone is tried */
1212 allowednodes = zlc_setup(zonelist, alloc_flags);
1213 zlc_active = 1;
1214 did_zlc_setup = 1;
1215 }
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001216 } while (*(++z) != NULL);
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001217
1218 if (unlikely(NUMA_BUILD && page == NULL && zlc_active)) {
1219 /* Disable zlc cache for second zonelist scan */
1220 zlc_active = 0;
1221 goto zonelist_scan;
1222 }
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001223 return page;
Martin Hicks753ee722005-06-21 17:14:41 -07001224}
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226/*
1227 * This is the 'heart' of the zoned buddy allocator.
1228 */
1229struct page * fastcall
Al Virodd0fc662005-10-07 07:46:04 +01001230__alloc_pages(gfp_t gfp_mask, unsigned int order,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 struct zonelist *zonelist)
1232{
Al Viro260b2362005-10-21 03:22:44 -04001233 const gfp_t wait = gfp_mask & __GFP_WAIT;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001234 struct zone **z;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 struct page *page;
1236 struct reclaim_state reclaim_state;
1237 struct task_struct *p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 int do_retry;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001239 int alloc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 int did_some_progress;
1241
1242 might_sleep_if(wait);
1243
Akinobu Mita933e3122006-12-08 02:39:45 -08001244 if (should_fail_alloc_page(gfp_mask, order))
1245 return NULL;
1246
Jens Axboe6b1de912005-11-17 21:35:02 +01001247restart:
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001248 z = zonelist->zones; /* the list of zones suitable for gfp_mask */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001250 if (unlikely(*z == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 /* Should this ever happen?? */
1252 return NULL;
1253 }
Jens Axboe6b1de912005-11-17 21:35:02 +01001254
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001255 page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, order,
Nick Piggin31488902005-11-28 13:44:03 -08001256 zonelist, ALLOC_WMARK_LOW|ALLOC_CPUSET);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001257 if (page)
1258 goto got_pg;
1259
Christoph Lameter952f3b52006-12-06 20:33:26 -08001260 /*
1261 * GFP_THISNODE (meaning __GFP_THISNODE, __GFP_NORETRY and
1262 * __GFP_NOWARN set) should not cause reclaim since the subsystem
1263 * (f.e. slab) using GFP_THISNODE may choose to trigger reclaim
1264 * using a larger set of nodes after it has established that the
1265 * allowed per node queues are empty and that nodes are
1266 * over allocated.
1267 */
1268 if (NUMA_BUILD && (gfp_mask & GFP_THISNODE) == GFP_THISNODE)
1269 goto nopage;
1270
Paul Jackson0798e512006-12-06 20:31:38 -08001271 for (z = zonelist->zones; *z; z++)
Chris Wright43b0bc02006-06-25 05:47:55 -07001272 wakeup_kswapd(*z, order);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001273
Paul Jackson9bf22292005-09-06 15:18:12 -07001274 /*
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001275 * OK, we're below the kswapd watermark and have kicked background
1276 * reclaim. Now things get more complex, so set up alloc_flags according
1277 * to how we want to proceed.
1278 *
1279 * The caller may dip into page reserves a bit more if the caller
1280 * cannot run direct reclaim, or if the caller has realtime scheduling
Paul Jackson4eac9152006-01-11 12:17:19 -08001281 * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will
1282 * set both ALLOC_HARDER (!wait) and ALLOC_HIGH (__GFP_HIGH).
Paul Jackson9bf22292005-09-06 15:18:12 -07001283 */
Nick Piggin31488902005-11-28 13:44:03 -08001284 alloc_flags = ALLOC_WMARK_MIN;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001285 if ((unlikely(rt_task(p)) && !in_interrupt()) || !wait)
1286 alloc_flags |= ALLOC_HARDER;
1287 if (gfp_mask & __GFP_HIGH)
1288 alloc_flags |= ALLOC_HIGH;
Paul Jacksonbdd804f2006-05-20 15:00:09 -07001289 if (wait)
1290 alloc_flags |= ALLOC_CPUSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 /*
1293 * Go through the zonelist again. Let __GFP_HIGH and allocations
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001294 * coming from realtime tasks go deeper into reserves.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 *
1296 * This is the last chance, in general, before the goto nopage.
1297 * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
Paul Jackson9bf22292005-09-06 15:18:12 -07001298 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 */
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001300 page = get_page_from_freelist(gfp_mask, order, zonelist, alloc_flags);
1301 if (page)
1302 goto got_pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 /* This allocation should allow future memory freeing. */
Nick Pigginb84a35b2005-05-01 08:58:36 -07001305
Kirill Korotaevb43a57b2006-12-06 20:32:27 -08001306rebalance:
Nick Pigginb84a35b2005-05-01 08:58:36 -07001307 if (((p->flags & PF_MEMALLOC) || unlikely(test_thread_flag(TIF_MEMDIE)))
1308 && !in_interrupt()) {
1309 if (!(gfp_mask & __GFP_NOMEMALLOC)) {
Kirill Korotaev885036d2005-11-13 16:06:41 -08001310nofail_alloc:
Nick Pigginb84a35b2005-05-01 08:58:36 -07001311 /* go through the zonelist yet again, ignoring mins */
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001312 page = get_page_from_freelist(gfp_mask, order,
Paul Jackson47f3a862006-01-06 00:10:32 -08001313 zonelist, ALLOC_NO_WATERMARKS);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001314 if (page)
1315 goto got_pg;
Kirill Korotaev885036d2005-11-13 16:06:41 -08001316 if (gfp_mask & __GFP_NOFAIL) {
Andrew Morton3fcfab12006-10-19 23:28:16 -07001317 congestion_wait(WRITE, HZ/50);
Kirill Korotaev885036d2005-11-13 16:06:41 -08001318 goto nofail_alloc;
1319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 }
1321 goto nopage;
1322 }
1323
1324 /* Atomic allocations - we can't balance anything */
1325 if (!wait)
1326 goto nopage;
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 cond_resched();
1329
1330 /* We now go into synchronous reclaim */
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001331 cpuset_memory_pressure_bump();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 p->flags |= PF_MEMALLOC;
1333 reclaim_state.reclaimed_slab = 0;
1334 p->reclaim_state = &reclaim_state;
1335
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001336 did_some_progress = try_to_free_pages(zonelist->zones, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 p->reclaim_state = NULL;
1339 p->flags &= ~PF_MEMALLOC;
1340
1341 cond_resched();
1342
1343 if (likely(did_some_progress)) {
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001344 page = get_page_from_freelist(gfp_mask, order,
1345 zonelist, alloc_flags);
1346 if (page)
1347 goto got_pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 } else if ((gfp_mask & __GFP_FS) && !(gfp_mask & __GFP_NORETRY)) {
1349 /*
1350 * Go through the zonelist yet one more time, keep
1351 * very high watermark here, this is only to catch
1352 * a parallel oom killing, we must fail if we're still
1353 * under heavy pressure.
1354 */
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001355 page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, order,
Nick Piggin31488902005-11-28 13:44:03 -08001356 zonelist, ALLOC_WMARK_HIGH|ALLOC_CPUSET);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001357 if (page)
1358 goto got_pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Christoph Lameter9b0f8b02006-02-20 18:27:52 -08001360 out_of_memory(zonelist, gfp_mask, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 goto restart;
1362 }
1363
1364 /*
1365 * Don't let big-order allocations loop unless the caller explicitly
1366 * requests that. Wait for some write requests to complete then retry.
1367 *
1368 * In this implementation, __GFP_REPEAT means __GFP_NOFAIL for order
1369 * <= 3, but that may not be true in other implementations.
1370 */
1371 do_retry = 0;
1372 if (!(gfp_mask & __GFP_NORETRY)) {
1373 if ((order <= 3) || (gfp_mask & __GFP_REPEAT))
1374 do_retry = 1;
1375 if (gfp_mask & __GFP_NOFAIL)
1376 do_retry = 1;
1377 }
1378 if (do_retry) {
Andrew Morton3fcfab12006-10-19 23:28:16 -07001379 congestion_wait(WRITE, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 goto rebalance;
1381 }
1382
1383nopage:
1384 if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) {
1385 printk(KERN_WARNING "%s: page allocation failure."
1386 " order:%d, mode:0x%x\n",
1387 p->comm, order, gfp_mask);
1388 dump_stack();
Janet Morgan578c2fd2005-06-21 17:14:56 -07001389 show_mem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391got_pg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return page;
1393}
1394
1395EXPORT_SYMBOL(__alloc_pages);
1396
1397/*
1398 * Common helper functions.
1399 */
Al Virodd0fc662005-10-07 07:46:04 +01001400fastcall unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
1402 struct page * page;
1403 page = alloc_pages(gfp_mask, order);
1404 if (!page)
1405 return 0;
1406 return (unsigned long) page_address(page);
1407}
1408
1409EXPORT_SYMBOL(__get_free_pages);
1410
Al Virodd0fc662005-10-07 07:46:04 +01001411fastcall unsigned long get_zeroed_page(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
1413 struct page * page;
1414
1415 /*
1416 * get_zeroed_page() returns a 32-bit address, which cannot represent
1417 * a highmem page
1418 */
Nick Piggin725d7042006-09-25 23:30:55 -07001419 VM_BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 page = alloc_pages(gfp_mask | __GFP_ZERO, 0);
1422 if (page)
1423 return (unsigned long) page_address(page);
1424 return 0;
1425}
1426
1427EXPORT_SYMBOL(get_zeroed_page);
1428
1429void __pagevec_free(struct pagevec *pvec)
1430{
1431 int i = pagevec_count(pvec);
1432
1433 while (--i >= 0)
1434 free_hot_cold_page(pvec->pages[i], pvec->cold);
1435}
1436
1437fastcall void __free_pages(struct page *page, unsigned int order)
1438{
Nick Pigginb5810032005-10-29 18:16:12 -07001439 if (put_page_testzero(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 if (order == 0)
1441 free_hot_page(page);
1442 else
1443 __free_pages_ok(page, order);
1444 }
1445}
1446
1447EXPORT_SYMBOL(__free_pages);
1448
1449fastcall void free_pages(unsigned long addr, unsigned int order)
1450{
1451 if (addr != 0) {
Nick Piggin725d7042006-09-25 23:30:55 -07001452 VM_BUG_ON(!virt_addr_valid((void *)addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 __free_pages(virt_to_page((void *)addr), order);
1454 }
1455}
1456
1457EXPORT_SYMBOL(free_pages);
1458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459static unsigned int nr_free_zone_pages(int offset)
1460{
Martin J. Blighe310fd42005-07-29 22:59:18 -07001461 /* Just pick one node, since fallback list is circular */
1462 pg_data_t *pgdat = NODE_DATA(numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 unsigned int sum = 0;
1464
Martin J. Blighe310fd42005-07-29 22:59:18 -07001465 struct zonelist *zonelist = pgdat->node_zonelists + offset;
1466 struct zone **zonep = zonelist->zones;
1467 struct zone *zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Martin J. Blighe310fd42005-07-29 22:59:18 -07001469 for (zone = *zonep++; zone; zone = *zonep++) {
1470 unsigned long size = zone->present_pages;
1471 unsigned long high = zone->pages_high;
1472 if (size > high)
1473 sum += size - high;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
1475
1476 return sum;
1477}
1478
1479/*
1480 * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
1481 */
1482unsigned int nr_free_buffer_pages(void)
1483{
Al Viroaf4ca452005-10-21 02:55:38 -04001484 return nr_free_zone_pages(gfp_zone(GFP_USER));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485}
1486
1487/*
1488 * Amount of free RAM allocatable within all zones
1489 */
1490unsigned int nr_free_pagecache_pages(void)
1491{
Mel Gorman2a1e2742007-07-17 04:03:12 -07001492 return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER_MOVABLE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
Christoph Lameter08e0f6a2006-09-27 01:50:06 -07001494
1495static inline void show_node(struct zone *zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
Christoph Lameter08e0f6a2006-09-27 01:50:06 -07001497 if (NUMA_BUILD)
Andy Whitcroft25ba77c2006-12-06 20:33:03 -08001498 printk("Node %d ", zone_to_nid(zone));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501void si_meminfo(struct sysinfo *val)
1502{
1503 val->totalram = totalram_pages;
1504 val->sharedram = 0;
Christoph Lameterd23ad422007-02-10 01:43:02 -08001505 val->freeram = global_page_state(NR_FREE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 val->bufferram = nr_blockdev_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 val->totalhigh = totalhigh_pages;
1508 val->freehigh = nr_free_highpages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 val->mem_unit = PAGE_SIZE;
1510}
1511
1512EXPORT_SYMBOL(si_meminfo);
1513
1514#ifdef CONFIG_NUMA
1515void si_meminfo_node(struct sysinfo *val, int nid)
1516{
1517 pg_data_t *pgdat = NODE_DATA(nid);
1518
1519 val->totalram = pgdat->node_present_pages;
Christoph Lameterd23ad422007-02-10 01:43:02 -08001520 val->freeram = node_page_state(nid, NR_FREE_PAGES);
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07001521#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].present_pages;
Christoph Lameterd23ad422007-02-10 01:43:02 -08001523 val->freehigh = zone_page_state(&pgdat->node_zones[ZONE_HIGHMEM],
1524 NR_FREE_PAGES);
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07001525#else
1526 val->totalhigh = 0;
1527 val->freehigh = 0;
1528#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 val->mem_unit = PAGE_SIZE;
1530}
1531#endif
1532
1533#define K(x) ((x) << (PAGE_SHIFT-10))
1534
1535/*
1536 * Show free area list (used inside shift_scroll-lock stuff)
1537 * We also calculate the percentage fragmentation. We do this by counting the
1538 * memory on each free list with the exception of the first item on the list.
1539 */
1540void show_free_areas(void)
1541{
Jes Sorensenc7241912006-09-27 01:50:05 -07001542 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 struct zone *zone;
1544
1545 for_each_zone(zone) {
Jes Sorensenc7241912006-09-27 01:50:05 -07001546 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 continue;
Jes Sorensenc7241912006-09-27 01:50:05 -07001548
1549 show_node(zone);
1550 printk("%s per-cpu:\n", zone->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Dave Jones6b482c62005-11-10 15:45:56 -05001552 for_each_online_cpu(cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 struct per_cpu_pageset *pageset;
1554
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001555 pageset = zone_pcp(zone, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Jes Sorensenc7241912006-09-27 01:50:05 -07001557 printk("CPU %4d: Hot: hi:%5d, btch:%4d usd:%4d "
1558 "Cold: hi:%5d, btch:%4d usd:%4d\n",
1559 cpu, pageset->pcp[0].high,
1560 pageset->pcp[0].batch, pageset->pcp[0].count,
1561 pageset->pcp[1].high, pageset->pcp[1].batch,
1562 pageset->pcp[1].count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 }
1564 }
1565
Andrew Mortona25700a2007-02-08 14:20:40 -08001566 printk("Active:%lu inactive:%lu dirty:%lu writeback:%lu unstable:%lu\n"
Christoph Lameterd23ad422007-02-10 01:43:02 -08001567 " free:%lu slab:%lu mapped:%lu pagetables:%lu bounce:%lu\n",
Christoph Lameter65e458d42007-02-10 01:43:05 -08001568 global_page_state(NR_ACTIVE),
1569 global_page_state(NR_INACTIVE),
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -07001570 global_page_state(NR_FILE_DIRTY),
Christoph Lameterce866b32006-06-30 01:55:40 -07001571 global_page_state(NR_WRITEBACK),
Christoph Lameterfd39fc82006-06-30 01:55:40 -07001572 global_page_state(NR_UNSTABLE_NFS),
Christoph Lameterd23ad422007-02-10 01:43:02 -08001573 global_page_state(NR_FREE_PAGES),
Christoph Lameter972d1a72006-09-25 23:31:51 -07001574 global_page_state(NR_SLAB_RECLAIMABLE) +
1575 global_page_state(NR_SLAB_UNRECLAIMABLE),
Christoph Lameter65ba55f2006-06-30 01:55:34 -07001576 global_page_state(NR_FILE_MAPPED),
Andrew Mortona25700a2007-02-08 14:20:40 -08001577 global_page_state(NR_PAGETABLE),
1578 global_page_state(NR_BOUNCE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 for_each_zone(zone) {
1581 int i;
1582
Jes Sorensenc7241912006-09-27 01:50:05 -07001583 if (!populated_zone(zone))
1584 continue;
1585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 show_node(zone);
1587 printk("%s"
1588 " free:%lukB"
1589 " min:%lukB"
1590 " low:%lukB"
1591 " high:%lukB"
1592 " active:%lukB"
1593 " inactive:%lukB"
1594 " present:%lukB"
1595 " pages_scanned:%lu"
1596 " all_unreclaimable? %s"
1597 "\n",
1598 zone->name,
Christoph Lameterd23ad422007-02-10 01:43:02 -08001599 K(zone_page_state(zone, NR_FREE_PAGES)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 K(zone->pages_min),
1601 K(zone->pages_low),
1602 K(zone->pages_high),
Christoph Lameterc8785382007-02-10 01:43:01 -08001603 K(zone_page_state(zone, NR_ACTIVE)),
1604 K(zone_page_state(zone, NR_INACTIVE)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 K(zone->present_pages),
1606 zone->pages_scanned,
1607 (zone->all_unreclaimable ? "yes" : "no")
1608 );
1609 printk("lowmem_reserve[]:");
1610 for (i = 0; i < MAX_NR_ZONES; i++)
1611 printk(" %lu", zone->lowmem_reserve[i]);
1612 printk("\n");
1613 }
1614
1615 for_each_zone(zone) {
Kirill Korotaev8f9de512006-06-23 02:03:50 -07001616 unsigned long nr[MAX_ORDER], flags, order, total = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Jes Sorensenc7241912006-09-27 01:50:05 -07001618 if (!populated_zone(zone))
1619 continue;
1620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 show_node(zone);
1622 printk("%s: ", zone->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 spin_lock_irqsave(&zone->lock, flags);
1625 for (order = 0; order < MAX_ORDER; order++) {
Kirill Korotaev8f9de512006-06-23 02:03:50 -07001626 nr[order] = zone->free_area[order].nr_free;
1627 total += nr[order] << order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 }
1629 spin_unlock_irqrestore(&zone->lock, flags);
Kirill Korotaev8f9de512006-06-23 02:03:50 -07001630 for (order = 0; order < MAX_ORDER; order++)
1631 printk("%lu*%lukB ", nr[order], K(1UL) << order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 printk("= %lukB\n", K(total));
1633 }
1634
1635 show_swap_cache_info();
1636}
1637
1638/*
1639 * Builds allocation fallback zone lists.
Christoph Lameter1a932052006-01-06 00:11:16 -08001640 *
1641 * Add all populated zones of a node to the zonelist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07001643static int build_zonelists_node(pg_data_t *pgdat, struct zonelist *zonelist,
1644 int nr_zones, enum zone_type zone_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
Christoph Lameter1a932052006-01-06 00:11:16 -08001646 struct zone *zone;
1647
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07001648 BUG_ON(zone_type >= MAX_NR_ZONES);
Christoph Lameter2f6726e2006-09-25 23:31:18 -07001649 zone_type++;
Christoph Lameter02a68a52006-01-06 00:11:18 -08001650
1651 do {
Christoph Lameter2f6726e2006-09-25 23:31:18 -07001652 zone_type--;
Christoph Lameter070f8032006-01-06 00:11:19 -08001653 zone = pgdat->node_zones + zone_type;
Christoph Lameter1a932052006-01-06 00:11:16 -08001654 if (populated_zone(zone)) {
Christoph Lameter070f8032006-01-06 00:11:19 -08001655 zonelist->zones[nr_zones++] = zone;
1656 check_highest_zone(zone_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 }
Christoph Lameter02a68a52006-01-06 00:11:18 -08001658
Christoph Lameter2f6726e2006-09-25 23:31:18 -07001659 } while (zone_type);
Christoph Lameter070f8032006-01-06 00:11:19 -08001660 return nr_zones;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661}
1662
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07001663
1664/*
1665 * zonelist_order:
1666 * 0 = automatic detection of better ordering.
1667 * 1 = order by ([node] distance, -zonetype)
1668 * 2 = order by (-zonetype, [node] distance)
1669 *
1670 * If not NUMA, ZONELIST_ORDER_ZONE and ZONELIST_ORDER_NODE will create
1671 * the same zonelist. So only NUMA can configure this param.
1672 */
1673#define ZONELIST_ORDER_DEFAULT 0
1674#define ZONELIST_ORDER_NODE 1
1675#define ZONELIST_ORDER_ZONE 2
1676
1677/* zonelist order in the kernel.
1678 * set_zonelist_order() will set this to NODE or ZONE.
1679 */
1680static int current_zonelist_order = ZONELIST_ORDER_DEFAULT;
1681static char zonelist_order_name[3][8] = {"Default", "Node", "Zone"};
1682
1683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684#ifdef CONFIG_NUMA
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07001685/* The value user specified ....changed by config */
1686static int user_zonelist_order = ZONELIST_ORDER_DEFAULT;
1687/* string for sysctl */
1688#define NUMA_ZONELIST_ORDER_LEN 16
1689char numa_zonelist_order[16] = "default";
1690
1691/*
1692 * interface for configure zonelist ordering.
1693 * command line option "numa_zonelist_order"
1694 * = "[dD]efault - default, automatic configuration.
1695 * = "[nN]ode - order by node locality, then by zone within node
1696 * = "[zZ]one - order by zone, then by locality within zone
1697 */
1698
1699static int __parse_numa_zonelist_order(char *s)
1700{
1701 if (*s == 'd' || *s == 'D') {
1702 user_zonelist_order = ZONELIST_ORDER_DEFAULT;
1703 } else if (*s == 'n' || *s == 'N') {
1704 user_zonelist_order = ZONELIST_ORDER_NODE;
1705 } else if (*s == 'z' || *s == 'Z') {
1706 user_zonelist_order = ZONELIST_ORDER_ZONE;
1707 } else {
1708 printk(KERN_WARNING
1709 "Ignoring invalid numa_zonelist_order value: "
1710 "%s\n", s);
1711 return -EINVAL;
1712 }
1713 return 0;
1714}
1715
1716static __init int setup_numa_zonelist_order(char *s)
1717{
1718 if (s)
1719 return __parse_numa_zonelist_order(s);
1720 return 0;
1721}
1722early_param("numa_zonelist_order", setup_numa_zonelist_order);
1723
1724/*
1725 * sysctl handler for numa_zonelist_order
1726 */
1727int numa_zonelist_order_handler(ctl_table *table, int write,
1728 struct file *file, void __user *buffer, size_t *length,
1729 loff_t *ppos)
1730{
1731 char saved_string[NUMA_ZONELIST_ORDER_LEN];
1732 int ret;
1733
1734 if (write)
1735 strncpy(saved_string, (char*)table->data,
1736 NUMA_ZONELIST_ORDER_LEN);
1737 ret = proc_dostring(table, write, file, buffer, length, ppos);
1738 if (ret)
1739 return ret;
1740 if (write) {
1741 int oldval = user_zonelist_order;
1742 if (__parse_numa_zonelist_order((char*)table->data)) {
1743 /*
1744 * bogus value. restore saved string
1745 */
1746 strncpy((char*)table->data, saved_string,
1747 NUMA_ZONELIST_ORDER_LEN);
1748 user_zonelist_order = oldval;
1749 } else if (oldval != user_zonelist_order)
1750 build_all_zonelists();
1751 }
1752 return 0;
1753}
1754
1755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756#define MAX_NODE_LOAD (num_online_nodes())
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07001757static int node_load[MAX_NUMNODES];
1758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759/**
Pavel Pisa4dc3b162005-05-01 08:59:25 -07001760 * 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 -07001761 * @node: node whose fallback list we're appending
1762 * @used_node_mask: nodemask_t of already used nodes
1763 *
1764 * We use a number of factors to determine which is the next node that should
1765 * appear on a given node's fallback list. The node should not have appeared
1766 * already in @node's fallback list, and it should be the next closest node
1767 * according to the distance array (which contains arbitrary distance values
1768 * from each node to each node in the system), and should also prefer nodes
1769 * with no CPUs, since presumably they'll have very little allocation pressure
1770 * on them otherwise.
1771 * It returns -1 if no node is found.
1772 */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07001773static int find_next_best_node(int node, nodemask_t *used_node_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
Linus Torvalds4cf808e2006-02-17 20:38:21 +01001775 int n, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 int min_val = INT_MAX;
1777 int best_node = -1;
1778
Linus Torvalds4cf808e2006-02-17 20:38:21 +01001779 /* Use the local node if we haven't already */
1780 if (!node_isset(node, *used_node_mask)) {
1781 node_set(node, *used_node_mask);
1782 return node;
1783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Linus Torvalds4cf808e2006-02-17 20:38:21 +01001785 for_each_online_node(n) {
1786 cpumask_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
1788 /* Don't want a node to appear more than once */
1789 if (node_isset(n, *used_node_mask))
1790 continue;
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 /* Use the distance array to find the distance */
1793 val = node_distance(node, n);
1794
Linus Torvalds4cf808e2006-02-17 20:38:21 +01001795 /* Penalize nodes under us ("prefer the next node") */
1796 val += (n < node);
1797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 /* Give preference to headless and unused nodes */
1799 tmp = node_to_cpumask(n);
1800 if (!cpus_empty(tmp))
1801 val += PENALTY_FOR_NODE_WITH_CPUS;
1802
1803 /* Slight preference for less loaded node */
1804 val *= (MAX_NODE_LOAD*MAX_NUMNODES);
1805 val += node_load[n];
1806
1807 if (val < min_val) {
1808 min_val = val;
1809 best_node = n;
1810 }
1811 }
1812
1813 if (best_node >= 0)
1814 node_set(best_node, *used_node_mask);
1815
1816 return best_node;
1817}
1818
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07001819
1820/*
1821 * Build zonelists ordered by node and zones within node.
1822 * This results in maximum locality--normal zone overflows into local
1823 * DMA zone, if any--but risks exhausting DMA zone.
1824 */
1825static void build_zonelists_in_node_order(pg_data_t *pgdat, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
Christoph Lameter19655d32006-09-25 23:31:19 -07001827 enum zone_type i;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07001828 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 struct zonelist *zonelist;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07001830
1831 for (i = 0; i < MAX_NR_ZONES; i++) {
1832 zonelist = pgdat->node_zonelists + i;
1833 for (j = 0; zonelist->zones[j] != NULL; j++)
1834 ;
1835 j = build_zonelists_node(NODE_DATA(node), zonelist, j, i);
1836 zonelist->zones[j] = NULL;
1837 }
1838}
1839
1840/*
1841 * Build zonelists ordered by zone and nodes within zones.
1842 * This results in conserving DMA zone[s] until all Normal memory is
1843 * exhausted, but results in overflowing to remote node while memory
1844 * may still exist in local DMA zone.
1845 */
1846static int node_order[MAX_NUMNODES];
1847
1848static void build_zonelists_in_zone_order(pg_data_t *pgdat, int nr_nodes)
1849{
1850 enum zone_type i;
1851 int pos, j, node;
1852 int zone_type; /* needs to be signed */
1853 struct zone *z;
1854 struct zonelist *zonelist;
1855
1856 for (i = 0; i < MAX_NR_ZONES; i++) {
1857 zonelist = pgdat->node_zonelists + i;
1858 pos = 0;
1859 for (zone_type = i; zone_type >= 0; zone_type--) {
1860 for (j = 0; j < nr_nodes; j++) {
1861 node = node_order[j];
1862 z = &NODE_DATA(node)->node_zones[zone_type];
1863 if (populated_zone(z)) {
1864 zonelist->zones[pos++] = z;
1865 check_highest_zone(zone_type);
1866 }
1867 }
1868 }
1869 zonelist->zones[pos] = NULL;
1870 }
1871}
1872
1873static int default_zonelist_order(void)
1874{
1875 int nid, zone_type;
1876 unsigned long low_kmem_size,total_size;
1877 struct zone *z;
1878 int average_size;
1879 /*
1880 * ZONE_DMA and ZONE_DMA32 can be very small area in the sytem.
1881 * If they are really small and used heavily, the system can fall
1882 * into OOM very easily.
1883 * This function detect ZONE_DMA/DMA32 size and confgigures zone order.
1884 */
1885 /* Is there ZONE_NORMAL ? (ex. ppc has only DMA zone..) */
1886 low_kmem_size = 0;
1887 total_size = 0;
1888 for_each_online_node(nid) {
1889 for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
1890 z = &NODE_DATA(nid)->node_zones[zone_type];
1891 if (populated_zone(z)) {
1892 if (zone_type < ZONE_NORMAL)
1893 low_kmem_size += z->present_pages;
1894 total_size += z->present_pages;
1895 }
1896 }
1897 }
1898 if (!low_kmem_size || /* there are no DMA area. */
1899 low_kmem_size > total_size/2) /* DMA/DMA32 is big. */
1900 return ZONELIST_ORDER_NODE;
1901 /*
1902 * look into each node's config.
1903 * If there is a node whose DMA/DMA32 memory is very big area on
1904 * local memory, NODE_ORDER may be suitable.
1905 */
1906 average_size = total_size / (num_online_nodes() + 1);
1907 for_each_online_node(nid) {
1908 low_kmem_size = 0;
1909 total_size = 0;
1910 for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
1911 z = &NODE_DATA(nid)->node_zones[zone_type];
1912 if (populated_zone(z)) {
1913 if (zone_type < ZONE_NORMAL)
1914 low_kmem_size += z->present_pages;
1915 total_size += z->present_pages;
1916 }
1917 }
1918 if (low_kmem_size &&
1919 total_size > average_size && /* ignore small node */
1920 low_kmem_size > total_size * 70/100)
1921 return ZONELIST_ORDER_NODE;
1922 }
1923 return ZONELIST_ORDER_ZONE;
1924}
1925
1926static void set_zonelist_order(void)
1927{
1928 if (user_zonelist_order == ZONELIST_ORDER_DEFAULT)
1929 current_zonelist_order = default_zonelist_order();
1930 else
1931 current_zonelist_order = user_zonelist_order;
1932}
1933
1934static void build_zonelists(pg_data_t *pgdat)
1935{
1936 int j, node, load;
1937 enum zone_type i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 nodemask_t used_mask;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07001939 int local_node, prev_node;
1940 struct zonelist *zonelist;
1941 int order = current_zonelist_order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
1943 /* initialize zonelists */
Christoph Lameter19655d32006-09-25 23:31:19 -07001944 for (i = 0; i < MAX_NR_ZONES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 zonelist = pgdat->node_zonelists + i;
1946 zonelist->zones[0] = NULL;
1947 }
1948
1949 /* NUMA-aware ordering of nodes */
1950 local_node = pgdat->node_id;
1951 load = num_online_nodes();
1952 prev_node = local_node;
1953 nodes_clear(used_mask);
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07001954
1955 memset(node_load, 0, sizeof(node_load));
1956 memset(node_order, 0, sizeof(node_order));
1957 j = 0;
1958
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
Christoph Lameter9eeff232006-01-18 17:42:31 -08001960 int distance = node_distance(local_node, node);
1961
1962 /*
1963 * If another node is sufficiently far away then it is better
1964 * to reclaim pages in a zone before going off node.
1965 */
1966 if (distance > RECLAIM_DISTANCE)
1967 zone_reclaim_mode = 1;
1968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 /*
1970 * We don't want to pressure a particular node.
1971 * So adding penalty to the first node in same
1972 * distance group to make it round-robin.
1973 */
Christoph Lameter9eeff232006-01-18 17:42:31 -08001974 if (distance != node_distance(local_node, prev_node))
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07001975 node_load[node] = load;
1976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 prev_node = node;
1978 load--;
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07001979 if (order == ZONELIST_ORDER_NODE)
1980 build_zonelists_in_node_order(pgdat, node);
1981 else
1982 node_order[j++] = node; /* remember order */
1983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07001985 if (order == ZONELIST_ORDER_ZONE) {
1986 /* calculate node order -- i.e., DMA last! */
1987 build_zonelists_in_zone_order(pgdat, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 }
1989}
1990
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001991/* Construct the zonelist performance cache - see further mmzone.h */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07001992static void build_zonelist_cache(pg_data_t *pgdat)
Paul Jackson9276b1bc2006-12-06 20:31:48 -08001993{
1994 int i;
1995
1996 for (i = 0; i < MAX_NR_ZONES; i++) {
1997 struct zonelist *zonelist;
1998 struct zonelist_cache *zlc;
1999 struct zone **z;
2000
2001 zonelist = pgdat->node_zonelists + i;
2002 zonelist->zlcache_ptr = zlc = &zonelist->zlcache;
2003 bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
2004 for (z = zonelist->zones; *z; z++)
2005 zlc->z_to_n[z - zonelist->zones] = zone_to_nid(*z);
2006 }
2007}
2008
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002009
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010#else /* CONFIG_NUMA */
2011
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002012static void set_zonelist_order(void)
2013{
2014 current_zonelist_order = ZONELIST_ORDER_ZONE;
2015}
2016
2017static void build_zonelists(pg_data_t *pgdat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
Christoph Lameter19655d32006-09-25 23:31:19 -07002019 int node, local_node;
2020 enum zone_type i,j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
2022 local_node = pgdat->node_id;
Christoph Lameter19655d32006-09-25 23:31:19 -07002023 for (i = 0; i < MAX_NR_ZONES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 struct zonelist *zonelist;
2025
2026 zonelist = pgdat->node_zonelists + i;
2027
Christoph Lameter19655d32006-09-25 23:31:19 -07002028 j = build_zonelists_node(pgdat, zonelist, 0, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 /*
2030 * Now we build the zonelist so that it contains the zones
2031 * of all the other nodes.
2032 * We don't want to pressure a particular node, so when
2033 * building the zones for node N, we make sure that the
2034 * zones coming right after the local ones are those from
2035 * node N+1 (modulo N)
2036 */
2037 for (node = local_node + 1; node < MAX_NUMNODES; node++) {
2038 if (!node_online(node))
2039 continue;
Christoph Lameter19655d32006-09-25 23:31:19 -07002040 j = build_zonelists_node(NODE_DATA(node), zonelist, j, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 }
2042 for (node = 0; node < local_node; node++) {
2043 if (!node_online(node))
2044 continue;
Christoph Lameter19655d32006-09-25 23:31:19 -07002045 j = build_zonelists_node(NODE_DATA(node), zonelist, j, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 }
2047
2048 zonelist->zones[j] = NULL;
2049 }
2050}
2051
Paul Jackson9276b1bc2006-12-06 20:31:48 -08002052/* non-NUMA variant of zonelist performance cache - just NULL zlcache_ptr */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002053static void build_zonelist_cache(pg_data_t *pgdat)
Paul Jackson9276b1bc2006-12-06 20:31:48 -08002054{
2055 int i;
2056
2057 for (i = 0; i < MAX_NR_ZONES; i++)
2058 pgdat->node_zonelists[i].zlcache_ptr = NULL;
2059}
2060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061#endif /* CONFIG_NUMA */
2062
Yasunori Goto68113782006-06-23 02:03:11 -07002063/* return values int ....just for stop_machine_run() */
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002064static int __build_all_zonelists(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
Yasunori Goto68113782006-06-23 02:03:11 -07002066 int nid;
Paul Jackson9276b1bc2006-12-06 20:31:48 -08002067
2068 for_each_online_node(nid) {
Yasunori Goto68113782006-06-23 02:03:11 -07002069 build_zonelists(NODE_DATA(nid));
Paul Jackson9276b1bc2006-12-06 20:31:48 -08002070 build_zonelist_cache(NODE_DATA(nid));
2071 }
Yasunori Goto68113782006-06-23 02:03:11 -07002072 return 0;
2073}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002075void build_all_zonelists(void)
Yasunori Goto68113782006-06-23 02:03:11 -07002076{
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002077 set_zonelist_order();
2078
Yasunori Goto68113782006-06-23 02:03:11 -07002079 if (system_state == SYSTEM_BOOTING) {
Randy Dunlap423b41d2006-09-27 01:50:12 -07002080 __build_all_zonelists(NULL);
Yasunori Goto68113782006-06-23 02:03:11 -07002081 cpuset_init_current_mems_allowed();
2082 } else {
2083 /* we have to stop all cpus to guaranntee there is no user
2084 of zonelist */
2085 stop_machine_run(__build_all_zonelists, NULL, NR_CPUS);
2086 /* cpuset refresh routine should be here */
2087 }
Andrew Mortonbd1e22b2006-06-23 02:03:47 -07002088 vm_total_pages = nr_free_pagecache_pages();
KAMEZAWA Hiroyukif0c0b2b2007-07-15 23:38:01 -07002089 printk("Built %i zonelists in %s order. Total pages: %ld\n",
2090 num_online_nodes(),
2091 zonelist_order_name[current_zonelist_order],
2092 vm_total_pages);
2093#ifdef CONFIG_NUMA
2094 printk("Policy zone: %s\n", zone_names[policy_zone]);
2095#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096}
2097
2098/*
2099 * Helper functions to size the waitqueue hash table.
2100 * Essentially these want to choose hash table sizes sufficiently
2101 * large so that collisions trying to wait on pages are rare.
2102 * But in fact, the number of active page waitqueues on typical
2103 * systems is ridiculously low, less than 200. So this is even
2104 * conservative, even though it seems large.
2105 *
2106 * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
2107 * waitqueues, i.e. the size of the waitq table given the number of pages.
2108 */
2109#define PAGES_PER_WAITQUEUE 256
2110
Yasunori Gotocca448f2006-06-23 02:03:10 -07002111#ifndef CONFIG_MEMORY_HOTPLUG
Yasunori Goto02b694d2006-06-23 02:03:08 -07002112static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
2114 unsigned long size = 1;
2115
2116 pages /= PAGES_PER_WAITQUEUE;
2117
2118 while (size < pages)
2119 size <<= 1;
2120
2121 /*
2122 * Once we have dozens or even hundreds of threads sleeping
2123 * on IO we've got bigger problems than wait queue collision.
2124 * Limit the size of the wait table to a reasonable size.
2125 */
2126 size = min(size, 4096UL);
2127
2128 return max(size, 4UL);
2129}
Yasunori Gotocca448f2006-06-23 02:03:10 -07002130#else
2131/*
2132 * A zone's size might be changed by hot-add, so it is not possible to determine
2133 * a suitable size for its wait_table. So we use the maximum size now.
2134 *
2135 * The max wait table size = 4096 x sizeof(wait_queue_head_t). ie:
2136 *
2137 * i386 (preemption config) : 4096 x 16 = 64Kbyte.
2138 * ia64, x86-64 (no preemption): 4096 x 20 = 80Kbyte.
2139 * ia64, x86-64 (preemption) : 4096 x 24 = 96Kbyte.
2140 *
2141 * The maximum entries are prepared when a zone's memory is (512K + 256) pages
2142 * or more by the traditional way. (See above). It equals:
2143 *
2144 * i386, x86-64, powerpc(4K page size) : = ( 2G + 1M)byte.
2145 * ia64(16K page size) : = ( 8G + 4M)byte.
2146 * powerpc (64K page size) : = (32G +16M)byte.
2147 */
2148static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
2149{
2150 return 4096UL;
2151}
2152#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
2154/*
2155 * This is an integer logarithm so that shifts can be used later
2156 * to extract the more random high bits from the multiplicative
2157 * hash function before the remainder is taken.
2158 */
2159static inline unsigned long wait_table_bits(unsigned long size)
2160{
2161 return ffz(~size);
2162}
2163
2164#define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
2165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166/*
2167 * Initially all pages are reserved - free ones are freed
2168 * up by free_all_bootmem() once the early boot process is
2169 * done. Non-atomic initialization, single-pass.
2170 */
Matt Tolentinoc09b4242006-01-17 07:03:44 +01002171void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
Dave Hansena2f3aa02007-01-10 23:15:30 -08002172 unsigned long start_pfn, enum memmap_context context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 struct page *page;
Andy Whitcroft29751f62005-06-23 00:08:00 -07002175 unsigned long end_pfn = start_pfn + size;
2176 unsigned long pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Greg Ungerercbe8dd42006-01-12 01:05:24 -08002178 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
Dave Hansena2f3aa02007-01-10 23:15:30 -08002179 /*
2180 * There can be holes in boot-time mem_map[]s
2181 * handed to this function. They do not
2182 * exist on hotplugged memory.
2183 */
2184 if (context == MEMMAP_EARLY) {
2185 if (!early_pfn_valid(pfn))
2186 continue;
2187 if (!early_pfn_in_nid(pfn, nid))
2188 continue;
2189 }
Andy Whitcroftd41dee32005-06-23 00:07:54 -07002190 page = pfn_to_page(pfn);
2191 set_page_links(page, zone, nid, pfn);
Nick Piggin7835e982006-03-22 00:08:40 -08002192 init_page_count(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 reset_page_mapcount(page);
2194 SetPageReserved(page);
2195 INIT_LIST_HEAD(&page->lru);
2196#ifdef WANT_PAGE_VIRTUAL
2197 /* The shift won't overflow because ZONE_NORMAL is below 4G. */
2198 if (!is_highmem_idx(zone))
Bob Picco3212c6b2005-06-27 14:36:28 -07002199 set_page_address(page, __va(pfn << PAGE_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 }
2202}
2203
Paul Mundt6ea6e682007-07-15 23:38:20 -07002204static void __meminit zone_init_free_lists(struct pglist_data *pgdat,
2205 struct zone *zone, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206{
2207 int order;
2208 for (order = 0; order < MAX_ORDER ; order++) {
2209 INIT_LIST_HEAD(&zone->free_area[order].free_list);
2210 zone->free_area[order].nr_free = 0;
2211 }
2212}
2213
2214#ifndef __HAVE_ARCH_MEMMAP_INIT
2215#define memmap_init(size, nid, zone, start_pfn) \
Dave Hansena2f3aa02007-01-10 23:15:30 -08002216 memmap_init_zone((size), (nid), (zone), (start_pfn), MEMMAP_EARLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217#endif
2218
Paul Mundtd09c6b82007-06-14 15:13:16 +09002219static int __devinit zone_batchsize(struct zone *zone)
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002220{
2221 int batch;
2222
2223 /*
2224 * The per-cpu-pages pools are set to around 1000th of the
Seth, Rohitba56e912005-10-29 18:15:47 -07002225 * size of the zone. But no more than 1/2 of a meg.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002226 *
2227 * OK, so we don't know how big the cache is. So guess.
2228 */
2229 batch = zone->present_pages / 1024;
Seth, Rohitba56e912005-10-29 18:15:47 -07002230 if (batch * PAGE_SIZE > 512 * 1024)
2231 batch = (512 * 1024) / PAGE_SIZE;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002232 batch /= 4; /* We effectively *= 4 below */
2233 if (batch < 1)
2234 batch = 1;
2235
2236 /*
Nick Piggin0ceaacc2005-12-04 13:55:25 +11002237 * Clamp the batch to a 2^n - 1 value. Having a power
2238 * of 2 value was found to be more likely to have
2239 * suboptimal cache aliasing properties in some cases.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002240 *
Nick Piggin0ceaacc2005-12-04 13:55:25 +11002241 * For example if 2 tasks are alternately allocating
2242 * batches of pages, one task can end up with a lot
2243 * of pages of one half of the possible page colors
2244 * and the other with pages of the other colors.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002245 */
Nick Piggin0ceaacc2005-12-04 13:55:25 +11002246 batch = (1 << (fls(batch + batch/2)-1)) - 1;
Seth, Rohitba56e912005-10-29 18:15:47 -07002247
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002248 return batch;
2249}
2250
Christoph Lameter2caaad42005-06-21 17:15:00 -07002251inline void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
2252{
2253 struct per_cpu_pages *pcp;
2254
Magnus Damm1c6fe942005-10-26 01:58:59 -07002255 memset(p, 0, sizeof(*p));
2256
Christoph Lameter2caaad42005-06-21 17:15:00 -07002257 pcp = &p->pcp[0]; /* hot */
2258 pcp->count = 0;
Christoph Lameter2caaad42005-06-21 17:15:00 -07002259 pcp->high = 6 * batch;
2260 pcp->batch = max(1UL, 1 * batch);
2261 INIT_LIST_HEAD(&pcp->list);
2262
2263 pcp = &p->pcp[1]; /* cold*/
2264 pcp->count = 0;
Christoph Lameter2caaad42005-06-21 17:15:00 -07002265 pcp->high = 2 * batch;
Seth, Rohite46a5e22005-10-29 18:15:48 -07002266 pcp->batch = max(1UL, batch/2);
Christoph Lameter2caaad42005-06-21 17:15:00 -07002267 INIT_LIST_HEAD(&pcp->list);
2268}
2269
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08002270/*
2271 * setup_pagelist_highmark() sets the high water mark for hot per_cpu_pagelist
2272 * to the value high for the pageset p.
2273 */
2274
2275static void setup_pagelist_highmark(struct per_cpu_pageset *p,
2276 unsigned long high)
2277{
2278 struct per_cpu_pages *pcp;
2279
2280 pcp = &p->pcp[0]; /* hot list */
2281 pcp->high = high;
2282 pcp->batch = max(1UL, high/4);
2283 if ((high/4) > (PAGE_SHIFT * 8))
2284 pcp->batch = PAGE_SHIFT * 8;
2285}
2286
2287
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002288#ifdef CONFIG_NUMA
2289/*
Christoph Lameter2caaad42005-06-21 17:15:00 -07002290 * Boot pageset table. One per cpu which is going to be used for all
2291 * zones and all nodes. The parameters will be set in such a way
2292 * that an item put on a list will immediately be handed over to
2293 * the buddy list. This is safe since pageset manipulation is done
2294 * with interrupts disabled.
2295 *
2296 * Some NUMA counter updates may also be caught by the boot pagesets.
Christoph Lameterb7c84c62005-06-22 20:26:07 -07002297 *
2298 * The boot_pagesets must be kept even after bootup is complete for
2299 * unused processors and/or zones. They do play a role for bootstrapping
2300 * hotplugged processors.
2301 *
2302 * zoneinfo_show() and maybe other functions do
2303 * not check if the processor is online before following the pageset pointer.
2304 * Other parts of the kernel may not check if the zone is available.
Christoph Lameter2caaad42005-06-21 17:15:00 -07002305 */
Eric Dumazet88a2a4ac2006-02-04 23:27:36 -08002306static struct per_cpu_pageset boot_pageset[NR_CPUS];
Christoph Lameter2caaad42005-06-21 17:15:00 -07002307
2308/*
2309 * Dynamically allocate memory for the
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002310 * per cpu pageset array in struct zone.
2311 */
Ashok Raj6292d9a2006-02-01 03:04:44 -08002312static int __cpuinit process_zones(int cpu)
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002313{
2314 struct zone *zone, *dzone;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002315
2316 for_each_zone(zone) {
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002317
Christoph Lameter66a55032006-09-27 01:50:09 -07002318 if (!populated_zone(zone))
2319 continue;
2320
Nick Piggin23316bc2006-01-08 01:00:41 -08002321 zone_pcp(zone, cpu) = kmalloc_node(sizeof(struct per_cpu_pageset),
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002322 GFP_KERNEL, cpu_to_node(cpu));
Nick Piggin23316bc2006-01-08 01:00:41 -08002323 if (!zone_pcp(zone, cpu))
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002324 goto bad;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002325
Nick Piggin23316bc2006-01-08 01:00:41 -08002326 setup_pageset(zone_pcp(zone, cpu), zone_batchsize(zone));
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08002327
2328 if (percpu_pagelist_fraction)
2329 setup_pagelist_highmark(zone_pcp(zone, cpu),
2330 (zone->present_pages / percpu_pagelist_fraction));
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002331 }
2332
2333 return 0;
2334bad:
2335 for_each_zone(dzone) {
2336 if (dzone == zone)
2337 break;
Nick Piggin23316bc2006-01-08 01:00:41 -08002338 kfree(zone_pcp(dzone, cpu));
2339 zone_pcp(dzone, cpu) = NULL;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002340 }
2341 return -ENOMEM;
2342}
2343
2344static inline void free_zone_pagesets(int cpu)
2345{
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002346 struct zone *zone;
2347
2348 for_each_zone(zone) {
2349 struct per_cpu_pageset *pset = zone_pcp(zone, cpu);
2350
David Rientjesf3ef9ea2006-09-25 16:24:57 -07002351 /* Free per_cpu_pageset if it is slab allocated */
2352 if (pset != &boot_pageset[cpu])
2353 kfree(pset);
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002354 zone_pcp(zone, cpu) = NULL;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002355 }
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002356}
2357
Chandra Seetharaman9c7b2162006-06-27 02:54:07 -07002358static int __cpuinit pageset_cpuup_callback(struct notifier_block *nfb,
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002359 unsigned long action,
2360 void *hcpu)
2361{
2362 int cpu = (long)hcpu;
2363 int ret = NOTIFY_OK;
2364
2365 switch (action) {
Andy Whitcroftce421c72006-12-06 20:33:08 -08002366 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002367 case CPU_UP_PREPARE_FROZEN:
Andy Whitcroftce421c72006-12-06 20:33:08 -08002368 if (process_zones(cpu))
2369 ret = NOTIFY_BAD;
2370 break;
2371 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002372 case CPU_UP_CANCELED_FROZEN:
Andy Whitcroftce421c72006-12-06 20:33:08 -08002373 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002374 case CPU_DEAD_FROZEN:
Andy Whitcroftce421c72006-12-06 20:33:08 -08002375 free_zone_pagesets(cpu);
2376 break;
2377 default:
2378 break;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002379 }
2380 return ret;
2381}
2382
Chandra Seetharaman74b85f32006-06-27 02:54:09 -07002383static struct notifier_block __cpuinitdata pageset_notifier =
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002384 { &pageset_cpuup_callback, NULL, 0 };
2385
Al Viro78d99552005-12-15 09:18:25 +00002386void __init setup_per_cpu_pageset(void)
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002387{
2388 int err;
2389
2390 /* Initialize per_cpu_pageset for cpu 0.
2391 * A cpuup callback will do this for every cpu
2392 * as it comes online
2393 */
2394 err = process_zones(smp_processor_id());
2395 BUG_ON(err);
2396 register_cpu_notifier(&pageset_notifier);
2397}
2398
2399#endif
2400
Sam Ravnborg577a32f2007-05-17 23:29:25 +02002401static noinline __init_refok
Yasunori Gotocca448f2006-06-23 02:03:10 -07002402int zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages)
Dave Hansened8ece22005-10-29 18:16:50 -07002403{
2404 int i;
2405 struct pglist_data *pgdat = zone->zone_pgdat;
Yasunori Gotocca448f2006-06-23 02:03:10 -07002406 size_t alloc_size;
Dave Hansened8ece22005-10-29 18:16:50 -07002407
2408 /*
2409 * The per-page waitqueue mechanism uses hashed waitqueues
2410 * per zone.
2411 */
Yasunori Goto02b694d2006-06-23 02:03:08 -07002412 zone->wait_table_hash_nr_entries =
2413 wait_table_hash_nr_entries(zone_size_pages);
2414 zone->wait_table_bits =
2415 wait_table_bits(zone->wait_table_hash_nr_entries);
Yasunori Gotocca448f2006-06-23 02:03:10 -07002416 alloc_size = zone->wait_table_hash_nr_entries
2417 * sizeof(wait_queue_head_t);
2418
2419 if (system_state == SYSTEM_BOOTING) {
2420 zone->wait_table = (wait_queue_head_t *)
2421 alloc_bootmem_node(pgdat, alloc_size);
2422 } else {
2423 /*
2424 * This case means that a zone whose size was 0 gets new memory
2425 * via memory hot-add.
2426 * But it may be the case that a new node was hot-added. In
2427 * this case vmalloc() will not be able to use this new node's
2428 * memory - this wait_table must be initialized to use this new
2429 * node itself as well.
2430 * To use this new node's memory, further consideration will be
2431 * necessary.
2432 */
2433 zone->wait_table = (wait_queue_head_t *)vmalloc(alloc_size);
2434 }
2435 if (!zone->wait_table)
2436 return -ENOMEM;
Dave Hansened8ece22005-10-29 18:16:50 -07002437
Yasunori Goto02b694d2006-06-23 02:03:08 -07002438 for(i = 0; i < zone->wait_table_hash_nr_entries; ++i)
Dave Hansened8ece22005-10-29 18:16:50 -07002439 init_waitqueue_head(zone->wait_table + i);
Yasunori Gotocca448f2006-06-23 02:03:10 -07002440
2441 return 0;
Dave Hansened8ece22005-10-29 18:16:50 -07002442}
2443
Matt Tolentinoc09b4242006-01-17 07:03:44 +01002444static __meminit void zone_pcp_init(struct zone *zone)
Dave Hansened8ece22005-10-29 18:16:50 -07002445{
2446 int cpu;
2447 unsigned long batch = zone_batchsize(zone);
2448
2449 for (cpu = 0; cpu < NR_CPUS; cpu++) {
2450#ifdef CONFIG_NUMA
2451 /* Early boot. Slab allocator not functional yet */
Nick Piggin23316bc2006-01-08 01:00:41 -08002452 zone_pcp(zone, cpu) = &boot_pageset[cpu];
Dave Hansened8ece22005-10-29 18:16:50 -07002453 setup_pageset(&boot_pageset[cpu],0);
2454#else
2455 setup_pageset(zone_pcp(zone,cpu), batch);
2456#endif
2457 }
Anton Blanchardf5335c02006-03-25 03:06:49 -08002458 if (zone->present_pages)
2459 printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%lu\n",
2460 zone->name, zone->present_pages, batch);
Dave Hansened8ece22005-10-29 18:16:50 -07002461}
2462
Yasunori Goto718127c2006-06-23 02:03:10 -07002463__meminit int init_currently_empty_zone(struct zone *zone,
2464 unsigned long zone_start_pfn,
Dave Hansena2f3aa02007-01-10 23:15:30 -08002465 unsigned long size,
2466 enum memmap_context context)
Dave Hansened8ece22005-10-29 18:16:50 -07002467{
2468 struct pglist_data *pgdat = zone->zone_pgdat;
Yasunori Gotocca448f2006-06-23 02:03:10 -07002469 int ret;
2470 ret = zone_wait_table_init(zone, size);
2471 if (ret)
2472 return ret;
Dave Hansened8ece22005-10-29 18:16:50 -07002473 pgdat->nr_zones = zone_idx(zone) + 1;
2474
Dave Hansened8ece22005-10-29 18:16:50 -07002475 zone->zone_start_pfn = zone_start_pfn;
2476
2477 memmap_init(size, pgdat->node_id, zone_idx(zone), zone_start_pfn);
2478
2479 zone_init_free_lists(pgdat, zone, zone->spanned_pages);
Yasunori Goto718127c2006-06-23 02:03:10 -07002480
2481 return 0;
Dave Hansened8ece22005-10-29 18:16:50 -07002482}
2483
Mel Gormanc7132162006-09-27 01:49:43 -07002484#ifdef CONFIG_ARCH_POPULATES_NODE_MAP
2485/*
2486 * Basic iterator support. Return the first range of PFNs for a node
2487 * Note: nid == MAX_NUMNODES returns first region regardless of node
2488 */
Yasunori Gotoa3142c82007-05-08 00:23:07 -07002489static int __meminit first_active_region_index_in_nid(int nid)
Mel Gormanc7132162006-09-27 01:49:43 -07002490{
2491 int i;
2492
2493 for (i = 0; i < nr_nodemap_entries; i++)
2494 if (nid == MAX_NUMNODES || early_node_map[i].nid == nid)
2495 return i;
2496
2497 return -1;
2498}
2499
2500/*
2501 * Basic iterator support. Return the next active range of PFNs for a node
2502 * Note: nid == MAX_NUMNODES returns next region regardles of node
2503 */
Yasunori Gotoa3142c82007-05-08 00:23:07 -07002504static int __meminit next_active_region_index_in_nid(int index, int nid)
Mel Gormanc7132162006-09-27 01:49:43 -07002505{
2506 for (index = index + 1; index < nr_nodemap_entries; index++)
2507 if (nid == MAX_NUMNODES || early_node_map[index].nid == nid)
2508 return index;
2509
2510 return -1;
2511}
2512
2513#ifndef CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID
2514/*
2515 * Required by SPARSEMEM. Given a PFN, return what node the PFN is on.
2516 * Architectures may implement their own version but if add_active_range()
2517 * was used and there are no special requirements, this is a convenient
2518 * alternative
2519 */
Stephen Rothwell6f076f52007-05-10 03:15:27 -07002520int __meminit early_pfn_to_nid(unsigned long pfn)
Mel Gormanc7132162006-09-27 01:49:43 -07002521{
2522 int i;
2523
2524 for (i = 0; i < nr_nodemap_entries; i++) {
2525 unsigned long start_pfn = early_node_map[i].start_pfn;
2526 unsigned long end_pfn = early_node_map[i].end_pfn;
2527
2528 if (start_pfn <= pfn && pfn < end_pfn)
2529 return early_node_map[i].nid;
2530 }
2531
2532 return 0;
2533}
2534#endif /* CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID */
2535
2536/* Basic iterator support to walk early_node_map[] */
2537#define for_each_active_range_index_in_nid(i, nid) \
2538 for (i = first_active_region_index_in_nid(nid); i != -1; \
2539 i = next_active_region_index_in_nid(i, nid))
2540
2541/**
2542 * free_bootmem_with_active_regions - Call free_bootmem_node for each active range
Randy Dunlap88ca3b92006-10-04 02:15:25 -07002543 * @nid: The node to free memory on. If MAX_NUMNODES, all nodes are freed.
2544 * @max_low_pfn: The highest PFN that will be passed to free_bootmem_node
Mel Gormanc7132162006-09-27 01:49:43 -07002545 *
2546 * If an architecture guarantees that all ranges registered with
2547 * add_active_ranges() contain no holes and may be freed, this
2548 * this function may be used instead of calling free_bootmem() manually.
2549 */
2550void __init free_bootmem_with_active_regions(int nid,
2551 unsigned long max_low_pfn)
2552{
2553 int i;
2554
2555 for_each_active_range_index_in_nid(i, nid) {
2556 unsigned long size_pages = 0;
2557 unsigned long end_pfn = early_node_map[i].end_pfn;
2558
2559 if (early_node_map[i].start_pfn >= max_low_pfn)
2560 continue;
2561
2562 if (end_pfn > max_low_pfn)
2563 end_pfn = max_low_pfn;
2564
2565 size_pages = end_pfn - early_node_map[i].start_pfn;
2566 free_bootmem_node(NODE_DATA(early_node_map[i].nid),
2567 PFN_PHYS(early_node_map[i].start_pfn),
2568 size_pages << PAGE_SHIFT);
2569 }
2570}
2571
2572/**
2573 * sparse_memory_present_with_active_regions - Call memory_present for each active range
Randy Dunlap88ca3b92006-10-04 02:15:25 -07002574 * @nid: The node to call memory_present for. If MAX_NUMNODES, all nodes will be used.
Mel Gormanc7132162006-09-27 01:49:43 -07002575 *
2576 * If an architecture guarantees that all ranges registered with
2577 * add_active_ranges() contain no holes and may be freed, this
Randy Dunlap88ca3b92006-10-04 02:15:25 -07002578 * function may be used instead of calling memory_present() manually.
Mel Gormanc7132162006-09-27 01:49:43 -07002579 */
2580void __init sparse_memory_present_with_active_regions(int nid)
2581{
2582 int i;
2583
2584 for_each_active_range_index_in_nid(i, nid)
2585 memory_present(early_node_map[i].nid,
2586 early_node_map[i].start_pfn,
2587 early_node_map[i].end_pfn);
2588}
2589
2590/**
Mel Gormanfb014392006-09-27 01:49:59 -07002591 * push_node_boundaries - Push node boundaries to at least the requested boundary
2592 * @nid: The nid of the node to push the boundary for
2593 * @start_pfn: The start pfn of the node
2594 * @end_pfn: The end pfn of the node
2595 *
2596 * In reserve-based hot-add, mem_map is allocated that is unused until hotadd
2597 * time. Specifically, on x86_64, SRAT will report ranges that can potentially
2598 * be hotplugged even though no physical memory exists. This function allows
2599 * an arch to push out the node boundaries so mem_map is allocated that can
2600 * be used later.
2601 */
2602#ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
2603void __init push_node_boundaries(unsigned int nid,
2604 unsigned long start_pfn, unsigned long end_pfn)
2605{
2606 printk(KERN_DEBUG "Entering push_node_boundaries(%u, %lu, %lu)\n",
2607 nid, start_pfn, end_pfn);
2608
2609 /* Initialise the boundary for this node if necessary */
2610 if (node_boundary_end_pfn[nid] == 0)
2611 node_boundary_start_pfn[nid] = -1UL;
2612
2613 /* Update the boundaries */
2614 if (node_boundary_start_pfn[nid] > start_pfn)
2615 node_boundary_start_pfn[nid] = start_pfn;
2616 if (node_boundary_end_pfn[nid] < end_pfn)
2617 node_boundary_end_pfn[nid] = end_pfn;
2618}
2619
2620/* If necessary, push the node boundary out for reserve hotadd */
Jan Beulich98011f52007-07-15 23:38:17 -07002621static void __meminit account_node_boundary(unsigned int nid,
Mel Gormanfb014392006-09-27 01:49:59 -07002622 unsigned long *start_pfn, unsigned long *end_pfn)
2623{
2624 printk(KERN_DEBUG "Entering account_node_boundary(%u, %lu, %lu)\n",
2625 nid, *start_pfn, *end_pfn);
2626
2627 /* Return if boundary information has not been provided */
2628 if (node_boundary_end_pfn[nid] == 0)
2629 return;
2630
2631 /* Check the boundaries and update if necessary */
2632 if (node_boundary_start_pfn[nid] < *start_pfn)
2633 *start_pfn = node_boundary_start_pfn[nid];
2634 if (node_boundary_end_pfn[nid] > *end_pfn)
2635 *end_pfn = node_boundary_end_pfn[nid];
2636}
2637#else
2638void __init push_node_boundaries(unsigned int nid,
2639 unsigned long start_pfn, unsigned long end_pfn) {}
2640
Jan Beulich98011f52007-07-15 23:38:17 -07002641static void __meminit account_node_boundary(unsigned int nid,
Mel Gormanfb014392006-09-27 01:49:59 -07002642 unsigned long *start_pfn, unsigned long *end_pfn) {}
2643#endif
2644
2645
2646/**
Mel Gormanc7132162006-09-27 01:49:43 -07002647 * get_pfn_range_for_nid - Return the start and end page frames for a node
Randy Dunlap88ca3b92006-10-04 02:15:25 -07002648 * @nid: The nid to return the range for. If MAX_NUMNODES, the min and max PFN are returned.
2649 * @start_pfn: Passed by reference. On return, it will have the node start_pfn.
2650 * @end_pfn: Passed by reference. On return, it will have the node end_pfn.
Mel Gormanc7132162006-09-27 01:49:43 -07002651 *
2652 * It returns the start and end page frame of a node based on information
2653 * provided by an arch calling add_active_range(). If called for a node
2654 * with no available memory, a warning is printed and the start and end
Randy Dunlap88ca3b92006-10-04 02:15:25 -07002655 * PFNs will be 0.
Mel Gormanc7132162006-09-27 01:49:43 -07002656 */
Yasunori Gotoa3142c82007-05-08 00:23:07 -07002657void __meminit get_pfn_range_for_nid(unsigned int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07002658 unsigned long *start_pfn, unsigned long *end_pfn)
2659{
2660 int i;
2661 *start_pfn = -1UL;
2662 *end_pfn = 0;
2663
2664 for_each_active_range_index_in_nid(i, nid) {
2665 *start_pfn = min(*start_pfn, early_node_map[i].start_pfn);
2666 *end_pfn = max(*end_pfn, early_node_map[i].end_pfn);
2667 }
2668
2669 if (*start_pfn == -1UL) {
2670 printk(KERN_WARNING "Node %u active with no memory\n", nid);
2671 *start_pfn = 0;
2672 }
Mel Gormanfb014392006-09-27 01:49:59 -07002673
2674 /* Push the node boundaries out if requested */
2675 account_node_boundary(nid, start_pfn, end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07002676}
2677
2678/*
Mel Gorman2a1e2742007-07-17 04:03:12 -07002679 * This finds a zone that can be used for ZONE_MOVABLE pages. The
2680 * assumption is made that zones within a node are ordered in monotonic
2681 * increasing memory addresses so that the "highest" populated zone is used
2682 */
2683void __init find_usable_zone_for_movable(void)
2684{
2685 int zone_index;
2686 for (zone_index = MAX_NR_ZONES - 1; zone_index >= 0; zone_index--) {
2687 if (zone_index == ZONE_MOVABLE)
2688 continue;
2689
2690 if (arch_zone_highest_possible_pfn[zone_index] >
2691 arch_zone_lowest_possible_pfn[zone_index])
2692 break;
2693 }
2694
2695 VM_BUG_ON(zone_index == -1);
2696 movable_zone = zone_index;
2697}
2698
2699/*
2700 * The zone ranges provided by the architecture do not include ZONE_MOVABLE
2701 * because it is sized independant of architecture. Unlike the other zones,
2702 * the starting point for ZONE_MOVABLE is not fixed. It may be different
2703 * in each node depending on the size of each node and how evenly kernelcore
2704 * is distributed. This helper function adjusts the zone ranges
2705 * provided by the architecture for a given node by using the end of the
2706 * highest usable zone for ZONE_MOVABLE. This preserves the assumption that
2707 * zones within a node are in order of monotonic increases memory addresses
2708 */
2709void __meminit adjust_zone_range_for_zone_movable(int nid,
2710 unsigned long zone_type,
2711 unsigned long node_start_pfn,
2712 unsigned long node_end_pfn,
2713 unsigned long *zone_start_pfn,
2714 unsigned long *zone_end_pfn)
2715{
2716 /* Only adjust if ZONE_MOVABLE is on this node */
2717 if (zone_movable_pfn[nid]) {
2718 /* Size ZONE_MOVABLE */
2719 if (zone_type == ZONE_MOVABLE) {
2720 *zone_start_pfn = zone_movable_pfn[nid];
2721 *zone_end_pfn = min(node_end_pfn,
2722 arch_zone_highest_possible_pfn[movable_zone]);
2723
2724 /* Adjust for ZONE_MOVABLE starting within this range */
2725 } else if (*zone_start_pfn < zone_movable_pfn[nid] &&
2726 *zone_end_pfn > zone_movable_pfn[nid]) {
2727 *zone_end_pfn = zone_movable_pfn[nid];
2728
2729 /* Check if this whole range is within ZONE_MOVABLE */
2730 } else if (*zone_start_pfn >= zone_movable_pfn[nid])
2731 *zone_start_pfn = *zone_end_pfn;
2732 }
2733}
2734
2735/*
Mel Gormanc7132162006-09-27 01:49:43 -07002736 * Return the number of pages a zone spans in a node, including holes
2737 * present_pages = zone_spanned_pages_in_node() - zone_absent_pages_in_node()
2738 */
Paul Mundt6ea6e682007-07-15 23:38:20 -07002739static unsigned long __meminit zone_spanned_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07002740 unsigned long zone_type,
2741 unsigned long *ignored)
2742{
2743 unsigned long node_start_pfn, node_end_pfn;
2744 unsigned long zone_start_pfn, zone_end_pfn;
2745
2746 /* Get the start and end of the node and zone */
2747 get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
2748 zone_start_pfn = arch_zone_lowest_possible_pfn[zone_type];
2749 zone_end_pfn = arch_zone_highest_possible_pfn[zone_type];
Mel Gorman2a1e2742007-07-17 04:03:12 -07002750 adjust_zone_range_for_zone_movable(nid, zone_type,
2751 node_start_pfn, node_end_pfn,
2752 &zone_start_pfn, &zone_end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07002753
2754 /* Check that this node has pages within the zone's required range */
2755 if (zone_end_pfn < node_start_pfn || zone_start_pfn > node_end_pfn)
2756 return 0;
2757
2758 /* Move the zone boundaries inside the node if necessary */
2759 zone_end_pfn = min(zone_end_pfn, node_end_pfn);
2760 zone_start_pfn = max(zone_start_pfn, node_start_pfn);
2761
2762 /* Return the spanned pages */
2763 return zone_end_pfn - zone_start_pfn;
2764}
2765
2766/*
2767 * Return the number of holes in a range on a node. If nid is MAX_NUMNODES,
Randy Dunlap88ca3b92006-10-04 02:15:25 -07002768 * then all holes in the requested range will be accounted for.
Mel Gormanc7132162006-09-27 01:49:43 -07002769 */
Yasunori Gotoa3142c82007-05-08 00:23:07 -07002770unsigned long __meminit __absent_pages_in_range(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07002771 unsigned long range_start_pfn,
2772 unsigned long range_end_pfn)
2773{
2774 int i = 0;
2775 unsigned long prev_end_pfn = 0, hole_pages = 0;
2776 unsigned long start_pfn;
2777
2778 /* Find the end_pfn of the first active range of pfns in the node */
2779 i = first_active_region_index_in_nid(nid);
2780 if (i == -1)
2781 return 0;
2782
Mel Gorman9c7cd682006-09-27 01:49:58 -07002783 /* Account for ranges before physical memory on this node */
2784 if (early_node_map[i].start_pfn > range_start_pfn)
2785 hole_pages = early_node_map[i].start_pfn - range_start_pfn;
2786
Mel Gormanc7132162006-09-27 01:49:43 -07002787 prev_end_pfn = early_node_map[i].start_pfn;
2788
2789 /* Find all holes for the zone within the node */
2790 for (; i != -1; i = next_active_region_index_in_nid(i, nid)) {
2791
2792 /* No need to continue if prev_end_pfn is outside the zone */
2793 if (prev_end_pfn >= range_end_pfn)
2794 break;
2795
2796 /* Make sure the end of the zone is not within the hole */
2797 start_pfn = min(early_node_map[i].start_pfn, range_end_pfn);
2798 prev_end_pfn = max(prev_end_pfn, range_start_pfn);
2799
2800 /* Update the hole size cound and move on */
2801 if (start_pfn > range_start_pfn) {
2802 BUG_ON(prev_end_pfn > start_pfn);
2803 hole_pages += start_pfn - prev_end_pfn;
2804 }
2805 prev_end_pfn = early_node_map[i].end_pfn;
2806 }
2807
Mel Gorman9c7cd682006-09-27 01:49:58 -07002808 /* Account for ranges past physical memory on this node */
2809 if (range_end_pfn > prev_end_pfn)
Mel Gorman0c6cb972006-10-28 10:38:59 -07002810 hole_pages += range_end_pfn -
Mel Gorman9c7cd682006-09-27 01:49:58 -07002811 max(range_start_pfn, prev_end_pfn);
2812
Mel Gormanc7132162006-09-27 01:49:43 -07002813 return hole_pages;
2814}
2815
2816/**
2817 * absent_pages_in_range - Return number of page frames in holes within a range
2818 * @start_pfn: The start PFN to start searching for holes
2819 * @end_pfn: The end PFN to stop searching for holes
2820 *
Randy Dunlap88ca3b92006-10-04 02:15:25 -07002821 * It returns the number of pages frames in memory holes within a range.
Mel Gormanc7132162006-09-27 01:49:43 -07002822 */
2823unsigned long __init absent_pages_in_range(unsigned long start_pfn,
2824 unsigned long end_pfn)
2825{
2826 return __absent_pages_in_range(MAX_NUMNODES, start_pfn, end_pfn);
2827}
2828
2829/* Return the number of page frames in holes in a zone on a node */
Paul Mundt6ea6e682007-07-15 23:38:20 -07002830static unsigned long __meminit zone_absent_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07002831 unsigned long zone_type,
2832 unsigned long *ignored)
2833{
Mel Gorman9c7cd682006-09-27 01:49:58 -07002834 unsigned long node_start_pfn, node_end_pfn;
2835 unsigned long zone_start_pfn, zone_end_pfn;
2836
2837 get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
2838 zone_start_pfn = max(arch_zone_lowest_possible_pfn[zone_type],
2839 node_start_pfn);
2840 zone_end_pfn = min(arch_zone_highest_possible_pfn[zone_type],
2841 node_end_pfn);
2842
Mel Gorman2a1e2742007-07-17 04:03:12 -07002843 adjust_zone_range_for_zone_movable(nid, zone_type,
2844 node_start_pfn, node_end_pfn,
2845 &zone_start_pfn, &zone_end_pfn);
Mel Gorman9c7cd682006-09-27 01:49:58 -07002846 return __absent_pages_in_range(nid, zone_start_pfn, zone_end_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07002847}
Mel Gorman0e0b8642006-09-27 01:49:56 -07002848
Mel Gormanc7132162006-09-27 01:49:43 -07002849#else
Paul Mundt6ea6e682007-07-15 23:38:20 -07002850static inline unsigned long __meminit zone_spanned_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07002851 unsigned long zone_type,
2852 unsigned long *zones_size)
2853{
2854 return zones_size[zone_type];
2855}
2856
Paul Mundt6ea6e682007-07-15 23:38:20 -07002857static inline unsigned long __meminit zone_absent_pages_in_node(int nid,
Mel Gormanc7132162006-09-27 01:49:43 -07002858 unsigned long zone_type,
2859 unsigned long *zholes_size)
2860{
2861 if (!zholes_size)
2862 return 0;
2863
2864 return zholes_size[zone_type];
2865}
Mel Gorman0e0b8642006-09-27 01:49:56 -07002866
Mel Gormanc7132162006-09-27 01:49:43 -07002867#endif
2868
Yasunori Gotoa3142c82007-05-08 00:23:07 -07002869static void __meminit calculate_node_totalpages(struct pglist_data *pgdat,
Mel Gormanc7132162006-09-27 01:49:43 -07002870 unsigned long *zones_size, unsigned long *zholes_size)
2871{
2872 unsigned long realtotalpages, totalpages = 0;
2873 enum zone_type i;
2874
2875 for (i = 0; i < MAX_NR_ZONES; i++)
2876 totalpages += zone_spanned_pages_in_node(pgdat->node_id, i,
2877 zones_size);
2878 pgdat->node_spanned_pages = totalpages;
2879
2880 realtotalpages = totalpages;
2881 for (i = 0; i < MAX_NR_ZONES; i++)
2882 realtotalpages -=
2883 zone_absent_pages_in_node(pgdat->node_id, i,
2884 zholes_size);
2885 pgdat->node_present_pages = realtotalpages;
2886 printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id,
2887 realtotalpages);
2888}
2889
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890/*
2891 * Set up the zone data structures:
2892 * - mark all pages reserved
2893 * - mark all memory queues empty
2894 * - clear the memory bitmaps
2895 */
Yasunori Goto86356ab2006-06-23 02:03:09 -07002896static void __meminit free_area_init_core(struct pglist_data *pgdat,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 unsigned long *zones_size, unsigned long *zholes_size)
2898{
Christoph Lameter2f1b6242006-09-25 23:31:13 -07002899 enum zone_type j;
Dave Hansened8ece22005-10-29 18:16:50 -07002900 int nid = pgdat->node_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 unsigned long zone_start_pfn = pgdat->node_start_pfn;
Yasunori Goto718127c2006-06-23 02:03:10 -07002902 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
Dave Hansen208d54e2005-10-29 18:16:52 -07002904 pgdat_resize_init(pgdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 pgdat->nr_zones = 0;
2906 init_waitqueue_head(&pgdat->kswapd_wait);
2907 pgdat->kswapd_max_order = 0;
2908
2909 for (j = 0; j < MAX_NR_ZONES; j++) {
2910 struct zone *zone = pgdat->node_zones + j;
Mel Gorman0e0b8642006-09-27 01:49:56 -07002911 unsigned long size, realsize, memmap_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
Mel Gormanc7132162006-09-27 01:49:43 -07002913 size = zone_spanned_pages_in_node(nid, j, zones_size);
2914 realsize = size - zone_absent_pages_in_node(nid, j,
2915 zholes_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
Mel Gorman0e0b8642006-09-27 01:49:56 -07002917 /*
2918 * Adjust realsize so that it accounts for how much memory
2919 * is used by this zone for memmap. This affects the watermark
2920 * and per-cpu initialisations
2921 */
2922 memmap_pages = (size * sizeof(struct page)) >> PAGE_SHIFT;
2923 if (realsize >= memmap_pages) {
2924 realsize -= memmap_pages;
2925 printk(KERN_DEBUG
2926 " %s zone: %lu pages used for memmap\n",
2927 zone_names[j], memmap_pages);
2928 } else
2929 printk(KERN_WARNING
2930 " %s zone: %lu pages exceeds realsize %lu\n",
2931 zone_names[j], memmap_pages, realsize);
2932
Christoph Lameter62672762007-02-10 01:43:07 -08002933 /* Account for reserved pages */
2934 if (j == 0 && realsize > dma_reserve) {
Mel Gorman0e0b8642006-09-27 01:49:56 -07002935 realsize -= dma_reserve;
Christoph Lameter62672762007-02-10 01:43:07 -08002936 printk(KERN_DEBUG " %s zone: %lu pages reserved\n",
2937 zone_names[0], dma_reserve);
Mel Gorman0e0b8642006-09-27 01:49:56 -07002938 }
2939
Christoph Lameter98d2b0e2006-09-25 23:31:12 -07002940 if (!is_highmem_idx(j))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 nr_kernel_pages += realsize;
2942 nr_all_pages += realsize;
2943
2944 zone->spanned_pages = size;
2945 zone->present_pages = realsize;
Christoph Lameter96146342006-07-03 00:24:13 -07002946#ifdef CONFIG_NUMA
Christoph Lameterd5f541e2006-09-27 01:50:08 -07002947 zone->node = nid;
Christoph Lameter8417bba2006-09-25 23:31:51 -07002948 zone->min_unmapped_pages = (realsize*sysctl_min_unmapped_ratio)
Christoph Lameter96146342006-07-03 00:24:13 -07002949 / 100;
Christoph Lameter0ff38492006-09-25 23:31:52 -07002950 zone->min_slab_pages = (realsize * sysctl_min_slab_ratio) / 100;
Christoph Lameter96146342006-07-03 00:24:13 -07002951#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 zone->name = zone_names[j];
2953 spin_lock_init(&zone->lock);
2954 spin_lock_init(&zone->lru_lock);
Dave Hansenbdc8cb92005-10-29 18:16:53 -07002955 zone_seqlock_init(zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 zone->zone_pgdat = pgdat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
Martin Bligh3bb1a852006-10-28 10:38:24 -07002958 zone->prev_priority = DEF_PRIORITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
Dave Hansened8ece22005-10-29 18:16:50 -07002960 zone_pcp_init(zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 INIT_LIST_HEAD(&zone->active_list);
2962 INIT_LIST_HEAD(&zone->inactive_list);
2963 zone->nr_scan_active = 0;
2964 zone->nr_scan_inactive = 0;
Christoph Lameter2244b952006-06-30 01:55:33 -07002965 zap_zone_vm_stats(zone);
Martin Hicks53e9a612005-09-03 15:54:51 -07002966 atomic_set(&zone->reclaim_in_progress, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 if (!size)
2968 continue;
2969
Dave Hansena2f3aa02007-01-10 23:15:30 -08002970 ret = init_currently_empty_zone(zone, zone_start_pfn,
2971 size, MEMMAP_EARLY);
Yasunori Goto718127c2006-06-23 02:03:10 -07002972 BUG_ON(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 zone_start_pfn += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 }
2975}
2976
Sam Ravnborg577a32f2007-05-17 23:29:25 +02002977static void __init_refok alloc_node_mem_map(struct pglist_data *pgdat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 /* Skip empty nodes */
2980 if (!pgdat->node_spanned_pages)
2981 return;
2982
Andy Whitcroftd41dee32005-06-23 00:07:54 -07002983#ifdef CONFIG_FLAT_NODE_MEM_MAP
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 /* ia64 gets its own node_mem_map, before this, without bootmem */
2985 if (!pgdat->node_mem_map) {
Bob Piccoe984bb42006-05-20 15:00:31 -07002986 unsigned long size, start, end;
Andy Whitcroftd41dee32005-06-23 00:07:54 -07002987 struct page *map;
2988
Bob Piccoe984bb42006-05-20 15:00:31 -07002989 /*
2990 * The zone's endpoints aren't required to be MAX_ORDER
2991 * aligned but the node_mem_map endpoints must be in order
2992 * for the buddy allocator to function correctly.
2993 */
2994 start = pgdat->node_start_pfn & ~(MAX_ORDER_NR_PAGES - 1);
2995 end = pgdat->node_start_pfn + pgdat->node_spanned_pages;
2996 end = ALIGN(end, MAX_ORDER_NR_PAGES);
2997 size = (end - start) * sizeof(struct page);
Dave Hansen6f167ec2005-06-23 00:07:39 -07002998 map = alloc_remap(pgdat->node_id, size);
2999 if (!map)
3000 map = alloc_bootmem_node(pgdat, size);
Bob Piccoe984bb42006-05-20 15:00:31 -07003001 pgdat->node_mem_map = map + (pgdat->node_start_pfn - start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 }
Roman Zippel12d810c2007-05-31 00:40:54 -07003003#ifndef CONFIG_NEED_MULTIPLE_NODES
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 /*
3005 * With no DISCONTIG, the global mem_map is just set as node 0's
3006 */
Mel Gormanc7132162006-09-27 01:49:43 -07003007 if (pgdat == NODE_DATA(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 mem_map = NODE_DATA(0)->node_mem_map;
Mel Gormanc7132162006-09-27 01:49:43 -07003009#ifdef CONFIG_ARCH_POPULATES_NODE_MAP
3010 if (page_to_pfn(mem_map) != pgdat->node_start_pfn)
3011 mem_map -= pgdat->node_start_pfn;
3012#endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
3013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014#endif
Andy Whitcroftd41dee32005-06-23 00:07:54 -07003015#endif /* CONFIG_FLAT_NODE_MEM_MAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016}
3017
Yasunori Goto86356ab2006-06-23 02:03:09 -07003018void __meminit free_area_init_node(int nid, struct pglist_data *pgdat,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 unsigned long *zones_size, unsigned long node_start_pfn,
3020 unsigned long *zholes_size)
3021{
3022 pgdat->node_id = nid;
3023 pgdat->node_start_pfn = node_start_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07003024 calculate_node_totalpages(pgdat, zones_size, zholes_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
3026 alloc_node_mem_map(pgdat);
3027
3028 free_area_init_core(pgdat, zones_size, zholes_size);
3029}
3030
Mel Gormanc7132162006-09-27 01:49:43 -07003031#ifdef CONFIG_ARCH_POPULATES_NODE_MAP
Miklos Szeredi418508c2007-05-23 13:57:55 -07003032
3033#if MAX_NUMNODES > 1
3034/*
3035 * Figure out the number of possible node ids.
3036 */
3037static void __init setup_nr_node_ids(void)
3038{
3039 unsigned int node;
3040 unsigned int highest = 0;
3041
3042 for_each_node_mask(node, node_possible_map)
3043 highest = node;
3044 nr_node_ids = highest + 1;
3045}
3046#else
3047static inline void setup_nr_node_ids(void)
3048{
3049}
3050#endif
3051
Mel Gormanc7132162006-09-27 01:49:43 -07003052/**
3053 * add_active_range - Register a range of PFNs backed by physical memory
3054 * @nid: The node ID the range resides on
3055 * @start_pfn: The start PFN of the available physical memory
3056 * @end_pfn: The end PFN of the available physical memory
3057 *
3058 * These ranges are stored in an early_node_map[] and later used by
3059 * free_area_init_nodes() to calculate zone sizes and holes. If the
3060 * range spans a memory hole, it is up to the architecture to ensure
3061 * the memory is not freed by the bootmem allocator. If possible
3062 * the range being registered will be merged with existing ranges.
3063 */
3064void __init add_active_range(unsigned int nid, unsigned long start_pfn,
3065 unsigned long end_pfn)
3066{
3067 int i;
3068
3069 printk(KERN_DEBUG "Entering add_active_range(%d, %lu, %lu) "
3070 "%d entries of %d used\n",
3071 nid, start_pfn, end_pfn,
3072 nr_nodemap_entries, MAX_ACTIVE_REGIONS);
3073
3074 /* Merge with existing active regions if possible */
3075 for (i = 0; i < nr_nodemap_entries; i++) {
3076 if (early_node_map[i].nid != nid)
3077 continue;
3078
3079 /* Skip if an existing region covers this new one */
3080 if (start_pfn >= early_node_map[i].start_pfn &&
3081 end_pfn <= early_node_map[i].end_pfn)
3082 return;
3083
3084 /* Merge forward if suitable */
3085 if (start_pfn <= early_node_map[i].end_pfn &&
3086 end_pfn > early_node_map[i].end_pfn) {
3087 early_node_map[i].end_pfn = end_pfn;
3088 return;
3089 }
3090
3091 /* Merge backward if suitable */
3092 if (start_pfn < early_node_map[i].end_pfn &&
3093 end_pfn >= early_node_map[i].start_pfn) {
3094 early_node_map[i].start_pfn = start_pfn;
3095 return;
3096 }
3097 }
3098
3099 /* Check that early_node_map is large enough */
3100 if (i >= MAX_ACTIVE_REGIONS) {
3101 printk(KERN_CRIT "More than %d memory regions, truncating\n",
3102 MAX_ACTIVE_REGIONS);
3103 return;
3104 }
3105
3106 early_node_map[i].nid = nid;
3107 early_node_map[i].start_pfn = start_pfn;
3108 early_node_map[i].end_pfn = end_pfn;
3109 nr_nodemap_entries = i + 1;
3110}
3111
3112/**
3113 * shrink_active_range - Shrink an existing registered range of PFNs
3114 * @nid: The node id the range is on that should be shrunk
3115 * @old_end_pfn: The old end PFN of the range
3116 * @new_end_pfn: The new PFN of the range
3117 *
3118 * i386 with NUMA use alloc_remap() to store a node_mem_map on a local node.
3119 * The map is kept at the end physical page range that has already been
3120 * registered with add_active_range(). This function allows an arch to shrink
3121 * an existing registered range.
3122 */
3123void __init shrink_active_range(unsigned int nid, unsigned long old_end_pfn,
3124 unsigned long new_end_pfn)
3125{
3126 int i;
3127
3128 /* Find the old active region end and shrink */
3129 for_each_active_range_index_in_nid(i, nid)
3130 if (early_node_map[i].end_pfn == old_end_pfn) {
3131 early_node_map[i].end_pfn = new_end_pfn;
3132 break;
3133 }
3134}
3135
3136/**
3137 * remove_all_active_ranges - Remove all currently registered regions
Randy Dunlap88ca3b92006-10-04 02:15:25 -07003138 *
Mel Gormanc7132162006-09-27 01:49:43 -07003139 * During discovery, it may be found that a table like SRAT is invalid
3140 * and an alternative discovery method must be used. This function removes
3141 * all currently registered regions.
3142 */
Randy Dunlap88ca3b92006-10-04 02:15:25 -07003143void __init remove_all_active_ranges(void)
Mel Gormanc7132162006-09-27 01:49:43 -07003144{
3145 memset(early_node_map, 0, sizeof(early_node_map));
3146 nr_nodemap_entries = 0;
Mel Gormanfb014392006-09-27 01:49:59 -07003147#ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
3148 memset(node_boundary_start_pfn, 0, sizeof(node_boundary_start_pfn));
3149 memset(node_boundary_end_pfn, 0, sizeof(node_boundary_end_pfn));
3150#endif /* CONFIG_MEMORY_HOTPLUG_RESERVE */
Mel Gormanc7132162006-09-27 01:49:43 -07003151}
3152
3153/* Compare two active node_active_regions */
3154static int __init cmp_node_active_region(const void *a, const void *b)
3155{
3156 struct node_active_region *arange = (struct node_active_region *)a;
3157 struct node_active_region *brange = (struct node_active_region *)b;
3158
3159 /* Done this way to avoid overflows */
3160 if (arange->start_pfn > brange->start_pfn)
3161 return 1;
3162 if (arange->start_pfn < brange->start_pfn)
3163 return -1;
3164
3165 return 0;
3166}
3167
3168/* sort the node_map by start_pfn */
3169static void __init sort_node_map(void)
3170{
3171 sort(early_node_map, (size_t)nr_nodemap_entries,
3172 sizeof(struct node_active_region),
3173 cmp_node_active_region, NULL);
3174}
3175
Mel Gormana6af2bc2007-02-10 01:42:57 -08003176/* Find the lowest pfn for a node */
Mel Gormanc7132162006-09-27 01:49:43 -07003177unsigned long __init find_min_pfn_for_node(unsigned long nid)
3178{
3179 int i;
Mel Gormana6af2bc2007-02-10 01:42:57 -08003180 unsigned long min_pfn = ULONG_MAX;
Mel Gorman1abbfb42006-11-23 12:01:41 +00003181
Mel Gormanc7132162006-09-27 01:49:43 -07003182 /* Assuming a sorted map, the first range found has the starting pfn */
3183 for_each_active_range_index_in_nid(i, nid)
Mel Gormana6af2bc2007-02-10 01:42:57 -08003184 min_pfn = min(min_pfn, early_node_map[i].start_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07003185
Mel Gormana6af2bc2007-02-10 01:42:57 -08003186 if (min_pfn == ULONG_MAX) {
3187 printk(KERN_WARNING
3188 "Could not find start_pfn for node %lu\n", nid);
3189 return 0;
3190 }
3191
3192 return min_pfn;
Mel Gormanc7132162006-09-27 01:49:43 -07003193}
3194
3195/**
3196 * find_min_pfn_with_active_regions - Find the minimum PFN registered
3197 *
3198 * It returns the minimum PFN based on information provided via
Randy Dunlap88ca3b92006-10-04 02:15:25 -07003199 * add_active_range().
Mel Gormanc7132162006-09-27 01:49:43 -07003200 */
3201unsigned long __init find_min_pfn_with_active_regions(void)
3202{
3203 return find_min_pfn_for_node(MAX_NUMNODES);
3204}
3205
3206/**
3207 * find_max_pfn_with_active_regions - Find the maximum PFN registered
3208 *
3209 * It returns the maximum PFN based on information provided via
Randy Dunlap88ca3b92006-10-04 02:15:25 -07003210 * add_active_range().
Mel Gormanc7132162006-09-27 01:49:43 -07003211 */
3212unsigned long __init find_max_pfn_with_active_regions(void)
3213{
3214 int i;
3215 unsigned long max_pfn = 0;
3216
3217 for (i = 0; i < nr_nodemap_entries; i++)
3218 max_pfn = max(max_pfn, early_node_map[i].end_pfn);
3219
3220 return max_pfn;
3221}
3222
Mel Gorman7e63efe2007-07-17 04:03:15 -07003223unsigned long __init early_calculate_totalpages(void)
3224{
3225 int i;
3226 unsigned long totalpages = 0;
3227
3228 for (i = 0; i < nr_nodemap_entries; i++)
3229 totalpages += early_node_map[i].end_pfn -
3230 early_node_map[i].start_pfn;
3231
3232 return totalpages;
3233}
3234
Mel Gorman2a1e2742007-07-17 04:03:12 -07003235/*
3236 * Find the PFN the Movable zone begins in each node. Kernel memory
3237 * is spread evenly between nodes as long as the nodes have enough
3238 * memory. When they don't, some nodes will have more kernelcore than
3239 * others
3240 */
3241void __init find_zone_movable_pfns_for_nodes(unsigned long *movable_pfn)
3242{
3243 int i, nid;
3244 unsigned long usable_startpfn;
3245 unsigned long kernelcore_node, kernelcore_remaining;
3246 int usable_nodes = num_online_nodes();
3247
Mel Gorman7e63efe2007-07-17 04:03:15 -07003248 /*
3249 * If movablecore was specified, calculate what size of
3250 * kernelcore that corresponds so that memory usable for
3251 * any allocation type is evenly spread. If both kernelcore
3252 * and movablecore are specified, then the value of kernelcore
3253 * will be used for required_kernelcore if it's greater than
3254 * what movablecore would have allowed.
3255 */
3256 if (required_movablecore) {
3257 unsigned long totalpages = early_calculate_totalpages();
3258 unsigned long corepages;
3259
3260 /*
3261 * Round-up so that ZONE_MOVABLE is at least as large as what
3262 * was requested by the user
3263 */
3264 required_movablecore =
3265 roundup(required_movablecore, MAX_ORDER_NR_PAGES);
3266 corepages = totalpages - required_movablecore;
3267
3268 required_kernelcore = max(required_kernelcore, corepages);
3269 }
3270
Mel Gorman2a1e2742007-07-17 04:03:12 -07003271 /* If kernelcore was not specified, there is no ZONE_MOVABLE */
3272 if (!required_kernelcore)
3273 return;
3274
3275 /* usable_startpfn is the lowest possible pfn ZONE_MOVABLE can be at */
3276 find_usable_zone_for_movable();
3277 usable_startpfn = arch_zone_lowest_possible_pfn[movable_zone];
3278
3279restart:
3280 /* Spread kernelcore memory as evenly as possible throughout nodes */
3281 kernelcore_node = required_kernelcore / usable_nodes;
3282 for_each_online_node(nid) {
3283 /*
3284 * Recalculate kernelcore_node if the division per node
3285 * now exceeds what is necessary to satisfy the requested
3286 * amount of memory for the kernel
3287 */
3288 if (required_kernelcore < kernelcore_node)
3289 kernelcore_node = required_kernelcore / usable_nodes;
3290
3291 /*
3292 * As the map is walked, we track how much memory is usable
3293 * by the kernel using kernelcore_remaining. When it is
3294 * 0, the rest of the node is usable by ZONE_MOVABLE
3295 */
3296 kernelcore_remaining = kernelcore_node;
3297
3298 /* Go through each range of PFNs within this node */
3299 for_each_active_range_index_in_nid(i, nid) {
3300 unsigned long start_pfn, end_pfn;
3301 unsigned long size_pages;
3302
3303 start_pfn = max(early_node_map[i].start_pfn,
3304 zone_movable_pfn[nid]);
3305 end_pfn = early_node_map[i].end_pfn;
3306 if (start_pfn >= end_pfn)
3307 continue;
3308
3309 /* Account for what is only usable for kernelcore */
3310 if (start_pfn < usable_startpfn) {
3311 unsigned long kernel_pages;
3312 kernel_pages = min(end_pfn, usable_startpfn)
3313 - start_pfn;
3314
3315 kernelcore_remaining -= min(kernel_pages,
3316 kernelcore_remaining);
3317 required_kernelcore -= min(kernel_pages,
3318 required_kernelcore);
3319
3320 /* Continue if range is now fully accounted */
3321 if (end_pfn <= usable_startpfn) {
3322
3323 /*
3324 * Push zone_movable_pfn to the end so
3325 * that if we have to rebalance
3326 * kernelcore across nodes, we will
3327 * not double account here
3328 */
3329 zone_movable_pfn[nid] = end_pfn;
3330 continue;
3331 }
3332 start_pfn = usable_startpfn;
3333 }
3334
3335 /*
3336 * The usable PFN range for ZONE_MOVABLE is from
3337 * start_pfn->end_pfn. Calculate size_pages as the
3338 * number of pages used as kernelcore
3339 */
3340 size_pages = end_pfn - start_pfn;
3341 if (size_pages > kernelcore_remaining)
3342 size_pages = kernelcore_remaining;
3343 zone_movable_pfn[nid] = start_pfn + size_pages;
3344
3345 /*
3346 * Some kernelcore has been met, update counts and
3347 * break if the kernelcore for this node has been
3348 * satisified
3349 */
3350 required_kernelcore -= min(required_kernelcore,
3351 size_pages);
3352 kernelcore_remaining -= size_pages;
3353 if (!kernelcore_remaining)
3354 break;
3355 }
3356 }
3357
3358 /*
3359 * If there is still required_kernelcore, we do another pass with one
3360 * less node in the count. This will push zone_movable_pfn[nid] further
3361 * along on the nodes that still have memory until kernelcore is
3362 * satisified
3363 */
3364 usable_nodes--;
3365 if (usable_nodes && required_kernelcore > usable_nodes)
3366 goto restart;
3367
3368 /* Align start of ZONE_MOVABLE on all nids to MAX_ORDER_NR_PAGES */
3369 for (nid = 0; nid < MAX_NUMNODES; nid++)
3370 zone_movable_pfn[nid] =
3371 roundup(zone_movable_pfn[nid], MAX_ORDER_NR_PAGES);
3372}
3373
Mel Gormanc7132162006-09-27 01:49:43 -07003374/**
3375 * free_area_init_nodes - Initialise all pg_data_t and zone data
Randy Dunlap88ca3b92006-10-04 02:15:25 -07003376 * @max_zone_pfn: an array of max PFNs for each zone
Mel Gormanc7132162006-09-27 01:49:43 -07003377 *
3378 * This will call free_area_init_node() for each active node in the system.
3379 * Using the page ranges provided by add_active_range(), the size of each
3380 * zone in each node and their holes is calculated. If the maximum PFN
3381 * between two adjacent zones match, it is assumed that the zone is empty.
3382 * For example, if arch_max_dma_pfn == arch_max_dma32_pfn, it is assumed
3383 * that arch_max_dma32_pfn has no pages. It is also assumed that a zone
3384 * starts where the previous one ended. For example, ZONE_DMA32 starts
3385 * at arch_max_dma_pfn.
3386 */
3387void __init free_area_init_nodes(unsigned long *max_zone_pfn)
3388{
3389 unsigned long nid;
3390 enum zone_type i;
3391
Mel Gormana6af2bc2007-02-10 01:42:57 -08003392 /* Sort early_node_map as initialisation assumes it is sorted */
3393 sort_node_map();
3394
Mel Gormanc7132162006-09-27 01:49:43 -07003395 /* Record where the zone boundaries are */
3396 memset(arch_zone_lowest_possible_pfn, 0,
3397 sizeof(arch_zone_lowest_possible_pfn));
3398 memset(arch_zone_highest_possible_pfn, 0,
3399 sizeof(arch_zone_highest_possible_pfn));
3400 arch_zone_lowest_possible_pfn[0] = find_min_pfn_with_active_regions();
3401 arch_zone_highest_possible_pfn[0] = max_zone_pfn[0];
3402 for (i = 1; i < MAX_NR_ZONES; i++) {
Mel Gorman2a1e2742007-07-17 04:03:12 -07003403 if (i == ZONE_MOVABLE)
3404 continue;
Mel Gormanc7132162006-09-27 01:49:43 -07003405 arch_zone_lowest_possible_pfn[i] =
3406 arch_zone_highest_possible_pfn[i-1];
3407 arch_zone_highest_possible_pfn[i] =
3408 max(max_zone_pfn[i], arch_zone_lowest_possible_pfn[i]);
3409 }
Mel Gorman2a1e2742007-07-17 04:03:12 -07003410 arch_zone_lowest_possible_pfn[ZONE_MOVABLE] = 0;
3411 arch_zone_highest_possible_pfn[ZONE_MOVABLE] = 0;
3412
3413 /* Find the PFNs that ZONE_MOVABLE begins at in each node */
3414 memset(zone_movable_pfn, 0, sizeof(zone_movable_pfn));
3415 find_zone_movable_pfns_for_nodes(zone_movable_pfn);
Mel Gormanc7132162006-09-27 01:49:43 -07003416
Mel Gormanc7132162006-09-27 01:49:43 -07003417 /* Print out the zone ranges */
3418 printk("Zone PFN ranges:\n");
Mel Gorman2a1e2742007-07-17 04:03:12 -07003419 for (i = 0; i < MAX_NR_ZONES; i++) {
3420 if (i == ZONE_MOVABLE)
3421 continue;
Mel Gormanc7132162006-09-27 01:49:43 -07003422 printk(" %-8s %8lu -> %8lu\n",
3423 zone_names[i],
3424 arch_zone_lowest_possible_pfn[i],
3425 arch_zone_highest_possible_pfn[i]);
Mel Gorman2a1e2742007-07-17 04:03:12 -07003426 }
3427
3428 /* Print out the PFNs ZONE_MOVABLE begins at in each node */
3429 printk("Movable zone start PFN for each node\n");
3430 for (i = 0; i < MAX_NUMNODES; i++) {
3431 if (zone_movable_pfn[i])
3432 printk(" Node %d: %lu\n", i, zone_movable_pfn[i]);
3433 }
Mel Gormanc7132162006-09-27 01:49:43 -07003434
3435 /* Print out the early_node_map[] */
3436 printk("early_node_map[%d] active PFN ranges\n", nr_nodemap_entries);
3437 for (i = 0; i < nr_nodemap_entries; i++)
3438 printk(" %3d: %8lu -> %8lu\n", early_node_map[i].nid,
3439 early_node_map[i].start_pfn,
3440 early_node_map[i].end_pfn);
3441
3442 /* Initialise every node */
Christoph Lameter8ef82862007-02-20 13:57:52 -08003443 setup_nr_node_ids();
Mel Gormanc7132162006-09-27 01:49:43 -07003444 for_each_online_node(nid) {
3445 pg_data_t *pgdat = NODE_DATA(nid);
3446 free_area_init_node(nid, pgdat, NULL,
3447 find_min_pfn_for_node(nid), NULL);
3448 }
3449}
Mel Gorman2a1e2742007-07-17 04:03:12 -07003450
Mel Gorman7e63efe2007-07-17 04:03:15 -07003451static int __init cmdline_parse_core(char *p, unsigned long *core)
Mel Gorman2a1e2742007-07-17 04:03:12 -07003452{
3453 unsigned long long coremem;
3454 if (!p)
3455 return -EINVAL;
3456
3457 coremem = memparse(p, &p);
Mel Gorman7e63efe2007-07-17 04:03:15 -07003458 *core = coremem >> PAGE_SHIFT;
Mel Gorman2a1e2742007-07-17 04:03:12 -07003459
Mel Gorman7e63efe2007-07-17 04:03:15 -07003460 /* Paranoid check that UL is enough for the coremem value */
Mel Gorman2a1e2742007-07-17 04:03:12 -07003461 WARN_ON((coremem >> PAGE_SHIFT) > ULONG_MAX);
3462
3463 return 0;
3464}
Mel Gormaned7ed362007-07-17 04:03:14 -07003465
Mel Gorman7e63efe2007-07-17 04:03:15 -07003466/*
3467 * kernelcore=size sets the amount of memory for use for allocations that
3468 * cannot be reclaimed or migrated.
3469 */
3470static int __init cmdline_parse_kernelcore(char *p)
3471{
3472 return cmdline_parse_core(p, &required_kernelcore);
3473}
3474
3475/*
3476 * movablecore=size sets the amount of memory for use for allocations that
3477 * can be reclaimed or migrated.
3478 */
3479static int __init cmdline_parse_movablecore(char *p)
3480{
3481 return cmdline_parse_core(p, &required_movablecore);
3482}
3483
Mel Gormaned7ed362007-07-17 04:03:14 -07003484early_param("kernelcore", cmdline_parse_kernelcore);
Mel Gorman7e63efe2007-07-17 04:03:15 -07003485early_param("movablecore", cmdline_parse_movablecore);
Mel Gormaned7ed362007-07-17 04:03:14 -07003486
Mel Gormanc7132162006-09-27 01:49:43 -07003487#endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
3488
Mel Gorman0e0b8642006-09-27 01:49:56 -07003489/**
Randy Dunlap88ca3b92006-10-04 02:15:25 -07003490 * set_dma_reserve - set the specified number of pages reserved in the first zone
3491 * @new_dma_reserve: The number of pages to mark reserved
Mel Gorman0e0b8642006-09-27 01:49:56 -07003492 *
3493 * The per-cpu batchsize and zone watermarks are determined by present_pages.
3494 * In the DMA zone, a significant percentage may be consumed by kernel image
3495 * and other unfreeable allocations which can skew the watermarks badly. This
Randy Dunlap88ca3b92006-10-04 02:15:25 -07003496 * function may optionally be used to account for unfreeable pages in the
3497 * first zone (e.g., ZONE_DMA). The effect will be lower watermarks and
3498 * smaller per-cpu batchsize.
Mel Gorman0e0b8642006-09-27 01:49:56 -07003499 */
3500void __init set_dma_reserve(unsigned long new_dma_reserve)
3501{
3502 dma_reserve = new_dma_reserve;
3503}
3504
Dave Hansen93b75042005-06-23 00:07:47 -07003505#ifndef CONFIG_NEED_MULTIPLE_NODES
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506static bootmem_data_t contig_bootmem_data;
3507struct pglist_data contig_page_data = { .bdata = &contig_bootmem_data };
3508
3509EXPORT_SYMBOL(contig_page_data);
Dave Hansen93b75042005-06-23 00:07:47 -07003510#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511
3512void __init free_area_init(unsigned long *zones_size)
3513{
Dave Hansen93b75042005-06-23 00:07:47 -07003514 free_area_init_node(0, NODE_DATA(0), zones_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
3516}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518static int page_alloc_cpu_notify(struct notifier_block *self,
3519 unsigned long action, void *hcpu)
3520{
3521 int cpu = (unsigned long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003523 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 local_irq_disable();
3525 __drain_pages(cpu);
Christoph Lameterf8891e52006-06-30 01:55:45 -07003526 vm_events_fold_cpu(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 local_irq_enable();
Christoph Lameter2244b952006-06-30 01:55:33 -07003528 refresh_cpu_vm_stats(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 }
3530 return NOTIFY_OK;
3531}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532
3533void __init page_alloc_init(void)
3534{
3535 hotcpu_notifier(page_alloc_cpu_notify, 0);
3536}
3537
3538/*
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07003539 * calculate_totalreserve_pages - called when sysctl_lower_zone_reserve_ratio
3540 * or min_free_kbytes changes.
3541 */
3542static void calculate_totalreserve_pages(void)
3543{
3544 struct pglist_data *pgdat;
3545 unsigned long reserve_pages = 0;
Christoph Lameter2f6726e2006-09-25 23:31:18 -07003546 enum zone_type i, j;
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07003547
3548 for_each_online_pgdat(pgdat) {
3549 for (i = 0; i < MAX_NR_ZONES; i++) {
3550 struct zone *zone = pgdat->node_zones + i;
3551 unsigned long max = 0;
3552
3553 /* Find valid and maximum lowmem_reserve in the zone */
3554 for (j = i; j < MAX_NR_ZONES; j++) {
3555 if (zone->lowmem_reserve[j] > max)
3556 max = zone->lowmem_reserve[j];
3557 }
3558
3559 /* we treat pages_high as reserved pages. */
3560 max += zone->pages_high;
3561
3562 if (max > zone->present_pages)
3563 max = zone->present_pages;
3564 reserve_pages += max;
3565 }
3566 }
3567 totalreserve_pages = reserve_pages;
3568}
3569
3570/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 * setup_per_zone_lowmem_reserve - called whenever
3572 * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
3573 * has a correct pages reserved value, so an adequate number of
3574 * pages are left in the zone after a successful __alloc_pages().
3575 */
3576static void setup_per_zone_lowmem_reserve(void)
3577{
3578 struct pglist_data *pgdat;
Christoph Lameter2f6726e2006-09-25 23:31:18 -07003579 enum zone_type j, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580
KAMEZAWA Hiroyukiec936fc2006-03-27 01:15:59 -08003581 for_each_online_pgdat(pgdat) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 for (j = 0; j < MAX_NR_ZONES; j++) {
3583 struct zone *zone = pgdat->node_zones + j;
3584 unsigned long present_pages = zone->present_pages;
3585
3586 zone->lowmem_reserve[j] = 0;
3587
Christoph Lameter2f6726e2006-09-25 23:31:18 -07003588 idx = j;
3589 while (idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 struct zone *lower_zone;
3591
Christoph Lameter2f6726e2006-09-25 23:31:18 -07003592 idx--;
3593
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 if (sysctl_lowmem_reserve_ratio[idx] < 1)
3595 sysctl_lowmem_reserve_ratio[idx] = 1;
3596
3597 lower_zone = pgdat->node_zones + idx;
3598 lower_zone->lowmem_reserve[j] = present_pages /
3599 sysctl_lowmem_reserve_ratio[idx];
3600 present_pages += lower_zone->present_pages;
3601 }
3602 }
3603 }
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07003604
3605 /* update totalreserve_pages */
3606 calculate_totalreserve_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607}
3608
Randy Dunlap88ca3b92006-10-04 02:15:25 -07003609/**
3610 * setup_per_zone_pages_min - called when min_free_kbytes changes.
3611 *
3612 * Ensures that the pages_{min,low,high} values for each zone are set correctly
3613 * with respect to min_free_kbytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 */
Dave Hansen3947be12005-10-29 18:16:54 -07003615void setup_per_zone_pages_min(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616{
3617 unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
3618 unsigned long lowmem_pages = 0;
3619 struct zone *zone;
3620 unsigned long flags;
3621
3622 /* Calculate total number of !ZONE_HIGHMEM pages */
3623 for_each_zone(zone) {
3624 if (!is_highmem(zone))
3625 lowmem_pages += zone->present_pages;
3626 }
3627
3628 for_each_zone(zone) {
Andrew Mortonac924c62006-05-15 09:43:59 -07003629 u64 tmp;
3630
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 spin_lock_irqsave(&zone->lru_lock, flags);
Andrew Mortonac924c62006-05-15 09:43:59 -07003632 tmp = (u64)pages_min * zone->present_pages;
3633 do_div(tmp, lowmem_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 if (is_highmem(zone)) {
3635 /*
Nick Piggin669ed172005-11-13 16:06:45 -08003636 * __GFP_HIGH and PF_MEMALLOC allocations usually don't
3637 * need highmem pages, so cap pages_min to a small
3638 * value here.
3639 *
3640 * The (pages_high-pages_low) and (pages_low-pages_min)
3641 * deltas controls asynch page reclaim, and so should
3642 * not be capped for highmem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 */
3644 int min_pages;
3645
3646 min_pages = zone->present_pages / 1024;
3647 if (min_pages < SWAP_CLUSTER_MAX)
3648 min_pages = SWAP_CLUSTER_MAX;
3649 if (min_pages > 128)
3650 min_pages = 128;
3651 zone->pages_min = min_pages;
3652 } else {
Nick Piggin669ed172005-11-13 16:06:45 -08003653 /*
3654 * If it's a lowmem zone, reserve a number of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 * proportionate to the zone's size.
3656 */
Nick Piggin669ed172005-11-13 16:06:45 -08003657 zone->pages_min = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 }
3659
Andrew Mortonac924c62006-05-15 09:43:59 -07003660 zone->pages_low = zone->pages_min + (tmp >> 2);
3661 zone->pages_high = zone->pages_min + (tmp >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 spin_unlock_irqrestore(&zone->lru_lock, flags);
3663 }
Hideo AOKIcb45b0e2006-04-10 22:52:59 -07003664
3665 /* update totalreserve_pages */
3666 calculate_totalreserve_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667}
3668
3669/*
3670 * Initialise min_free_kbytes.
3671 *
3672 * For small machines we want it small (128k min). For large machines
3673 * we want it large (64MB max). But it is not linear, because network
3674 * bandwidth does not increase linearly with machine size. We use
3675 *
3676 * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
3677 * min_free_kbytes = sqrt(lowmem_kbytes * 16)
3678 *
3679 * which yields
3680 *
3681 * 16MB: 512k
3682 * 32MB: 724k
3683 * 64MB: 1024k
3684 * 128MB: 1448k
3685 * 256MB: 2048k
3686 * 512MB: 2896k
3687 * 1024MB: 4096k
3688 * 2048MB: 5792k
3689 * 4096MB: 8192k
3690 * 8192MB: 11584k
3691 * 16384MB: 16384k
3692 */
3693static int __init init_per_zone_pages_min(void)
3694{
3695 unsigned long lowmem_kbytes;
3696
3697 lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
3698
3699 min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
3700 if (min_free_kbytes < 128)
3701 min_free_kbytes = 128;
3702 if (min_free_kbytes > 65536)
3703 min_free_kbytes = 65536;
3704 setup_per_zone_pages_min();
3705 setup_per_zone_lowmem_reserve();
3706 return 0;
3707}
3708module_init(init_per_zone_pages_min)
3709
3710/*
3711 * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
3712 * that we can call two helper functions whenever min_free_kbytes
3713 * changes.
3714 */
3715int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
3716 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
3717{
3718 proc_dointvec(table, write, file, buffer, length, ppos);
Mel Gorman3b1d92c2007-05-06 14:49:30 -07003719 if (write)
3720 setup_per_zone_pages_min();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 return 0;
3722}
3723
Christoph Lameter96146342006-07-03 00:24:13 -07003724#ifdef CONFIG_NUMA
3725int sysctl_min_unmapped_ratio_sysctl_handler(ctl_table *table, int write,
3726 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
3727{
3728 struct zone *zone;
3729 int rc;
3730
3731 rc = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
3732 if (rc)
3733 return rc;
3734
3735 for_each_zone(zone)
Christoph Lameter8417bba2006-09-25 23:31:51 -07003736 zone->min_unmapped_pages = (zone->present_pages *
Christoph Lameter96146342006-07-03 00:24:13 -07003737 sysctl_min_unmapped_ratio) / 100;
3738 return 0;
3739}
Christoph Lameter0ff38492006-09-25 23:31:52 -07003740
3741int sysctl_min_slab_ratio_sysctl_handler(ctl_table *table, int write,
3742 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
3743{
3744 struct zone *zone;
3745 int rc;
3746
3747 rc = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
3748 if (rc)
3749 return rc;
3750
3751 for_each_zone(zone)
3752 zone->min_slab_pages = (zone->present_pages *
3753 sysctl_min_slab_ratio) / 100;
3754 return 0;
3755}
Christoph Lameter96146342006-07-03 00:24:13 -07003756#endif
3757
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758/*
3759 * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
3760 * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
3761 * whenever sysctl_lowmem_reserve_ratio changes.
3762 *
3763 * The reserve ratio obviously has absolutely no relation with the
3764 * pages_min watermarks. The lowmem reserve ratio can only make sense
3765 * if in function of the boot time zone sizes.
3766 */
3767int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
3768 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
3769{
3770 proc_dointvec_minmax(table, write, file, buffer, length, ppos);
3771 setup_per_zone_lowmem_reserve();
3772 return 0;
3773}
3774
Rohit Seth8ad4b1f2006-01-08 01:00:40 -08003775/*
3776 * percpu_pagelist_fraction - changes the pcp->high for each zone on each
3777 * cpu. It is the fraction of total pages in each zone that a hot per cpu pagelist
3778 * can have before it gets flushed back to buddy allocator.
3779 */
3780
3781int percpu_pagelist_fraction_sysctl_handler(ctl_table *table, int write,
3782 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
3783{
3784 struct zone *zone;
3785 unsigned int cpu;
3786 int ret;
3787
3788 ret = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
3789 if (!write || (ret == -EINVAL))
3790 return ret;
3791 for_each_zone(zone) {
3792 for_each_online_cpu(cpu) {
3793 unsigned long high;
3794 high = zone->present_pages / percpu_pagelist_fraction;
3795 setup_pagelist_highmark(zone_pcp(zone, cpu), high);
3796 }
3797 }
3798 return 0;
3799}
3800
David S. Millerf034b5d2006-08-24 03:08:07 -07003801int hashdist = HASHDIST_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802
3803#ifdef CONFIG_NUMA
3804static int __init set_hashdist(char *str)
3805{
3806 if (!str)
3807 return 0;
3808 hashdist = simple_strtoul(str, &str, 0);
3809 return 1;
3810}
3811__setup("hashdist=", set_hashdist);
3812#endif
3813
3814/*
3815 * allocate a large system hash table from bootmem
3816 * - it is assumed that the hash table must contain an exact power-of-2
3817 * quantity of entries
3818 * - limit is the number of hash buckets, not the total allocation size
3819 */
3820void *__init alloc_large_system_hash(const char *tablename,
3821 unsigned long bucketsize,
3822 unsigned long numentries,
3823 int scale,
3824 int flags,
3825 unsigned int *_hash_shift,
3826 unsigned int *_hash_mask,
3827 unsigned long limit)
3828{
3829 unsigned long long max = limit;
3830 unsigned long log2qty, size;
3831 void *table = NULL;
3832
3833 /* allow the kernel cmdline to have a say */
3834 if (!numentries) {
3835 /* round applicable memory size up to nearest megabyte */
Andrew Morton04903662006-12-06 20:37:33 -08003836 numentries = nr_kernel_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
3838 numentries >>= 20 - PAGE_SHIFT;
3839 numentries <<= 20 - PAGE_SHIFT;
3840
3841 /* limit to 1 bucket per 2^scale bytes of low memory */
3842 if (scale > PAGE_SHIFT)
3843 numentries >>= (scale - PAGE_SHIFT);
3844 else
3845 numentries <<= (PAGE_SHIFT - scale);
Paul Mundt9ab37b82007-01-05 16:36:30 -08003846
3847 /* Make sure we've got at least a 0-order allocation.. */
3848 if (unlikely((numentries * bucketsize) < PAGE_SIZE))
3849 numentries = PAGE_SIZE / bucketsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 }
John Hawkes6e692ed2006-03-25 03:08:02 -08003851 numentries = roundup_pow_of_two(numentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852
3853 /* limit allocation size to 1/16 total memory by default */
3854 if (max == 0) {
3855 max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
3856 do_div(max, bucketsize);
3857 }
3858
3859 if (numentries > max)
3860 numentries = max;
3861
David Howellsf0d1b0b2006-12-08 02:37:49 -08003862 log2qty = ilog2(numentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863
3864 do {
3865 size = bucketsize << log2qty;
3866 if (flags & HASH_EARLY)
3867 table = alloc_bootmem(size);
3868 else if (hashdist)
3869 table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
3870 else {
3871 unsigned long order;
3872 for (order = 0; ((1UL << order) << PAGE_SHIFT) < size; order++)
3873 ;
3874 table = (void*) __get_free_pages(GFP_ATOMIC, order);
Eric Dumazet1037b832007-07-15 23:38:05 -07003875 /*
3876 * If bucketsize is not a power-of-two, we may free
3877 * some pages at the end of hash table.
3878 */
3879 if (table) {
3880 unsigned long alloc_end = (unsigned long)table +
3881 (PAGE_SIZE << order);
3882 unsigned long used = (unsigned long)table +
3883 PAGE_ALIGN(size);
3884 split_page(virt_to_page(table), order);
3885 while (used < alloc_end) {
3886 free_page(used);
3887 used += PAGE_SIZE;
3888 }
3889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 }
3891 } while (!table && size > PAGE_SIZE && --log2qty);
3892
3893 if (!table)
3894 panic("Failed to allocate %s hash table\n", tablename);
3895
Dan Alonib49ad482007-07-15 23:38:23 -07003896 printk(KERN_INFO "%s hash table entries: %d (order: %d, %lu bytes)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 tablename,
3898 (1U << log2qty),
David Howellsf0d1b0b2006-12-08 02:37:49 -08003899 ilog2(size) - PAGE_SHIFT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 size);
3901
3902 if (_hash_shift)
3903 *_hash_shift = log2qty;
3904 if (_hash_mask)
3905 *_hash_mask = (1 << log2qty) - 1;
3906
3907 return table;
3908}
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -08003909
3910#ifdef CONFIG_OUT_OF_LINE_PFN_TO_PAGE
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -08003911struct page *pfn_to_page(unsigned long pfn)
3912{
Andy Whitcroft67de6482006-06-23 02:03:12 -07003913 return __pfn_to_page(pfn);
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -08003914}
3915unsigned long page_to_pfn(struct page *page)
3916{
Andy Whitcroft67de6482006-06-23 02:03:12 -07003917 return __page_to_pfn(page);
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -08003918}
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -08003919EXPORT_SYMBOL(pfn_to_page);
3920EXPORT_SYMBOL(page_to_pfn);
3921#endif /* CONFIG_OUT_OF_LINE_PFN_TO_PAGE */
Andrew Morton6220ec72006-10-19 23:29:05 -07003922
Andrew Morton6220ec72006-10-19 23:29:05 -07003923