]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - mm/page_alloc.c
mm: make scan_zone_unevictable_pages() static
[thirdparty/kernel/linux.git] / mm / page_alloc.c
CommitLineData
1da177e4
LT
1/*
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
1da177e4
LT
17#include <linux/stddef.h>
18#include <linux/mm.h>
19#include <linux/swap.h>
20#include <linux/interrupt.h>
21#include <linux/pagemap.h>
10ed273f 22#include <linux/jiffies.h>
1da177e4
LT
23#include <linux/bootmem.h>
24#include <linux/compiler.h>
9f158333 25#include <linux/kernel.h>
1da177e4
LT
26#include <linux/module.h>
27#include <linux/suspend.h>
28#include <linux/pagevec.h>
29#include <linux/blkdev.h>
30#include <linux/slab.h>
5a3135c2 31#include <linux/oom.h>
1da177e4
LT
32#include <linux/notifier.h>
33#include <linux/topology.h>
34#include <linux/sysctl.h>
35#include <linux/cpu.h>
36#include <linux/cpuset.h>
bdc8cb98 37#include <linux/memory_hotplug.h>
1da177e4
LT
38#include <linux/nodemask.h>
39#include <linux/vmalloc.h>
4be38e35 40#include <linux/mempolicy.h>
6811378e 41#include <linux/stop_machine.h>
c713216d
MG
42#include <linux/sort.h>
43#include <linux/pfn.h>
3fcfab16 44#include <linux/backing-dev.h>
933e312e 45#include <linux/fault-inject.h>
a5d76b54 46#include <linux/page-isolation.h>
52d4b9ac 47#include <linux/page_cgroup.h>
3ac7fe5a 48#include <linux/debugobjects.h>
1da177e4
LT
49
50#include <asm/tlbflush.h>
ac924c60 51#include <asm/div64.h>
1da177e4
LT
52#include "internal.h"
53
54/*
13808910 55 * Array of node states.
1da177e4 56 */
13808910
CL
57nodemask_t node_states[NR_NODE_STATES] __read_mostly = {
58 [N_POSSIBLE] = NODE_MASK_ALL,
59 [N_ONLINE] = { { [0] = 1UL } },
60#ifndef CONFIG_NUMA
61 [N_NORMAL_MEMORY] = { { [0] = 1UL } },
62#ifdef CONFIG_HIGHMEM
63 [N_HIGH_MEMORY] = { { [0] = 1UL } },
64#endif
65 [N_CPU] = { { [0] = 1UL } },
66#endif /* NUMA */
67};
68EXPORT_SYMBOL(node_states);
69
6c231b7b 70unsigned long totalram_pages __read_mostly;
cb45b0e9 71unsigned long totalreserve_pages __read_mostly;
8ad4b1fb 72int percpu_pagelist_fraction;
1da177e4 73
d9c23400
MG
74#ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
75int pageblock_order __read_mostly;
76#endif
77
d98c7a09 78static void __free_pages_ok(struct page *page, unsigned int order);
a226f6c8 79
1da177e4
LT
80/*
81 * results with 256, 32 in the lowmem_reserve sysctl:
82 * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
83 * 1G machine -> (16M dma, 784M normal, 224M high)
84 * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
85 * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
86 * HIGHMEM allocation will (224M+784M)/256 of ram reserved in ZONE_DMA
a2f1b424
AK
87 *
88 * TBD: should special case ZONE_DMA32 machines here - in those we normally
89 * don't need any ZONE_NORMAL reservation
1da177e4 90 */
2f1b6248 91int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1] = {
4b51d669 92#ifdef CONFIG_ZONE_DMA
2f1b6248 93 256,
4b51d669 94#endif
fb0e7942 95#ifdef CONFIG_ZONE_DMA32
2f1b6248 96 256,
fb0e7942 97#endif
e53ef38d 98#ifdef CONFIG_HIGHMEM
2a1e274a 99 32,
e53ef38d 100#endif
2a1e274a 101 32,
2f1b6248 102};
1da177e4
LT
103
104EXPORT_SYMBOL(totalram_pages);
1da177e4 105
15ad7cdc 106static char * const zone_names[MAX_NR_ZONES] = {
4b51d669 107#ifdef CONFIG_ZONE_DMA
2f1b6248 108 "DMA",
4b51d669 109#endif
fb0e7942 110#ifdef CONFIG_ZONE_DMA32
2f1b6248 111 "DMA32",
fb0e7942 112#endif
2f1b6248 113 "Normal",
e53ef38d 114#ifdef CONFIG_HIGHMEM
2a1e274a 115 "HighMem",
e53ef38d 116#endif
2a1e274a 117 "Movable",
2f1b6248
CL
118};
119
1da177e4
LT
120int min_free_kbytes = 1024;
121
86356ab1
YG
122unsigned long __meminitdata nr_kernel_pages;
123unsigned long __meminitdata nr_all_pages;
a3142c8e 124static unsigned long __meminitdata dma_reserve;
1da177e4 125
c713216d
MG
126#ifdef CONFIG_ARCH_POPULATES_NODE_MAP
127 /*
183ff22b 128 * MAX_ACTIVE_REGIONS determines the maximum number of distinct
c713216d
MG
129 * ranges of memory (RAM) that may be registered with add_active_range().
130 * Ranges passed to add_active_range() will be merged if possible
131 * so the number of times add_active_range() can be called is
132 * related to the number of nodes and the number of holes
133 */
134 #ifdef CONFIG_MAX_ACTIVE_REGIONS
135 /* Allow an architecture to set MAX_ACTIVE_REGIONS to save memory */
136 #define MAX_ACTIVE_REGIONS CONFIG_MAX_ACTIVE_REGIONS
137 #else
138 #if MAX_NUMNODES >= 32
139 /* If there can be many nodes, allow up to 50 holes per node */
140 #define MAX_ACTIVE_REGIONS (MAX_NUMNODES*50)
141 #else
142 /* By default, allow up to 256 distinct regions */
143 #define MAX_ACTIVE_REGIONS 256
144 #endif
145 #endif
146
98011f56
JB
147 static struct node_active_region __meminitdata early_node_map[MAX_ACTIVE_REGIONS];
148 static int __meminitdata nr_nodemap_entries;
149 static unsigned long __meminitdata arch_zone_lowest_possible_pfn[MAX_NR_ZONES];
150 static unsigned long __meminitdata arch_zone_highest_possible_pfn[MAX_NR_ZONES];
fb01439c 151#ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
98011f56
JB
152 static unsigned long __meminitdata node_boundary_start_pfn[MAX_NUMNODES];
153 static unsigned long __meminitdata node_boundary_end_pfn[MAX_NUMNODES];
fb01439c 154#endif /* CONFIG_MEMORY_HOTPLUG_RESERVE */
b69a7288 155 static unsigned long __initdata required_kernelcore;
484f51f8 156 static unsigned long __initdata required_movablecore;
b69a7288 157 static unsigned long __meminitdata zone_movable_pfn[MAX_NUMNODES];
2a1e274a
MG
158
159 /* movable_zone is the "real" zone pages in ZONE_MOVABLE are taken from */
160 int movable_zone;
161 EXPORT_SYMBOL(movable_zone);
c713216d
MG
162#endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
163
418508c1
MS
164#if MAX_NUMNODES > 1
165int nr_node_ids __read_mostly = MAX_NUMNODES;
166EXPORT_SYMBOL(nr_node_ids);
167#endif
168
9ef9acb0
MG
169int page_group_by_mobility_disabled __read_mostly;
170
b2a0ac88
MG
171static void set_pageblock_migratetype(struct page *page, int migratetype)
172{
173 set_pageblock_flags_group(page, (unsigned long)migratetype,
174 PB_migrate, PB_migrate_end);
175}
176
13e7444b 177#ifdef CONFIG_DEBUG_VM
c6a57e19 178static int page_outside_zone_boundaries(struct zone *zone, struct page *page)
1da177e4 179{
bdc8cb98
DH
180 int ret = 0;
181 unsigned seq;
182 unsigned long pfn = page_to_pfn(page);
c6a57e19 183
bdc8cb98
DH
184 do {
185 seq = zone_span_seqbegin(zone);
186 if (pfn >= zone->zone_start_pfn + zone->spanned_pages)
187 ret = 1;
188 else if (pfn < zone->zone_start_pfn)
189 ret = 1;
190 } while (zone_span_seqretry(zone, seq));
191
192 return ret;
c6a57e19
DH
193}
194
195static int page_is_consistent(struct zone *zone, struct page *page)
196{
14e07298 197 if (!pfn_valid_within(page_to_pfn(page)))
c6a57e19 198 return 0;
1da177e4 199 if (zone != page_zone(page))
c6a57e19
DH
200 return 0;
201
202 return 1;
203}
204/*
205 * Temporary debugging check for pages not lying within a given zone.
206 */
207static int bad_range(struct zone *zone, struct page *page)
208{
209 if (page_outside_zone_boundaries(zone, page))
1da177e4 210 return 1;
c6a57e19
DH
211 if (!page_is_consistent(zone, page))
212 return 1;
213
1da177e4
LT
214 return 0;
215}
13e7444b
NP
216#else
217static inline int bad_range(struct zone *zone, struct page *page)
218{
219 return 0;
220}
221#endif
222
224abf92 223static void bad_page(struct page *page)
1da177e4 224{
9442ec9d
HD
225 printk(KERN_EMERG "Bad page state in process '%s'\n" KERN_EMERG
226 "page:%p flags:0x%0*lx mapping:%p mapcount:%d count:%d\n",
224abf92
NP
227 current->comm, page, (int)(2*sizeof(unsigned long)),
228 (unsigned long)page->flags, page->mapping,
229 page_mapcount(page), page_count(page));
52d4b9ac 230
9442ec9d
HD
231 printk(KERN_EMERG "Trying to fix it up, but a reboot is needed\n"
232 KERN_EMERG "Backtrace:\n");
1da177e4 233 dump_stack();
dfa7e20c 234 page->flags &= ~PAGE_FLAGS_CLEAR_WHEN_BAD;
1da177e4
LT
235 set_page_count(page, 0);
236 reset_page_mapcount(page);
237 page->mapping = NULL;
9f158333 238 add_taint(TAINT_BAD_PAGE);
1da177e4
LT
239}
240
1da177e4
LT
241/*
242 * Higher-order pages are called "compound pages". They are structured thusly:
243 *
244 * The first PAGE_SIZE page is called the "head page".
245 *
246 * The remaining PAGE_SIZE pages are called "tail pages".
247 *
248 * All pages have PG_compound set. All pages have their ->private pointing at
249 * the head page (even the head page has this).
250 *
41d78ba5
HD
251 * The first tail page's ->lru.next holds the address of the compound page's
252 * put_page() function. Its ->lru.prev holds the order of allocation.
253 * This usage means that zero-order pages may not be compound.
1da177e4 254 */
d98c7a09
HD
255
256static void free_compound_page(struct page *page)
257{
d85f3385 258 __free_pages_ok(page, compound_order(page));
d98c7a09
HD
259}
260
01ad1c08 261void prep_compound_page(struct page *page, unsigned long order)
18229df5
AW
262{
263 int i;
264 int nr_pages = 1 << order;
265
266 set_compound_page_dtor(page, free_compound_page);
267 set_compound_order(page, order);
268 __SetPageHead(page);
269 for (i = 1; i < nr_pages; i++) {
270 struct page *p = page + i;
271
272 __SetPageTail(p);
273 p->first_page = page;
274 }
275}
276
277#ifdef CONFIG_HUGETLBFS
278void prep_compound_gigantic_page(struct page *page, unsigned long order)
1da177e4
LT
279{
280 int i;
281 int nr_pages = 1 << order;
6babc32c 282 struct page *p = page + 1;
1da177e4 283
33f2ef89 284 set_compound_page_dtor(page, free_compound_page);
d85f3385 285 set_compound_order(page, order);
6d777953 286 __SetPageHead(page);
18229df5 287 for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) {
d85f3385 288 __SetPageTail(p);
d85f3385 289 p->first_page = page;
1da177e4
LT
290 }
291}
18229df5 292#endif
1da177e4
LT
293
294static void destroy_compound_page(struct page *page, unsigned long order)
295{
296 int i;
297 int nr_pages = 1 << order;
298
d85f3385 299 if (unlikely(compound_order(page) != order))
224abf92 300 bad_page(page);
1da177e4 301
6d777953 302 if (unlikely(!PageHead(page)))
d85f3385 303 bad_page(page);
6d777953 304 __ClearPageHead(page);
18229df5
AW
305 for (i = 1; i < nr_pages; i++) {
306 struct page *p = page + i;
1da177e4 307
6d777953 308 if (unlikely(!PageTail(p) |
d85f3385 309 (p->first_page != page)))
224abf92 310 bad_page(page);
d85f3385 311 __ClearPageTail(p);
1da177e4
LT
312 }
313}
1da177e4 314
17cf4406
NP
315static inline void prep_zero_page(struct page *page, int order, gfp_t gfp_flags)
316{
317 int i;
318
6626c5d5
AM
319 /*
320 * clear_highpage() will use KM_USER0, so it's a bug to use __GFP_ZERO
321 * and __GFP_HIGHMEM from hard or soft interrupt context.
322 */
725d704e 323 VM_BUG_ON((gfp_flags & __GFP_HIGHMEM) && in_interrupt());
17cf4406
NP
324 for (i = 0; i < (1 << order); i++)
325 clear_highpage(page + i);
326}
327
6aa3001b
AM
328static inline void set_page_order(struct page *page, int order)
329{
4c21e2f2 330 set_page_private(page, order);
676165a8 331 __SetPageBuddy(page);
1da177e4
LT
332}
333
334static inline void rmv_page_order(struct page *page)
335{
676165a8 336 __ClearPageBuddy(page);
4c21e2f2 337 set_page_private(page, 0);
1da177e4
LT
338}
339
340/*
341 * Locate the struct page for both the matching buddy in our
342 * pair (buddy1) and the combined O(n+1) page they form (page).
343 *
344 * 1) Any buddy B1 will have an order O twin B2 which satisfies
345 * the following equation:
346 * B2 = B1 ^ (1 << O)
347 * For example, if the starting buddy (buddy2) is #8 its order
348 * 1 buddy is #10:
349 * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
350 *
351 * 2) Any buddy B will have an order O+1 parent P which
352 * satisfies the following equation:
353 * P = B & ~(1 << O)
354 *
d6e05edc 355 * Assumption: *_mem_map is contiguous at least up to MAX_ORDER
1da177e4
LT
356 */
357static inline struct page *
358__page_find_buddy(struct page *page, unsigned long page_idx, unsigned int order)
359{
360 unsigned long buddy_idx = page_idx ^ (1 << order);
361
362 return page + (buddy_idx - page_idx);
363}
364
365static inline unsigned long
366__find_combined_index(unsigned long page_idx, unsigned int order)
367{
368 return (page_idx & ~(1 << order));
369}
370
371/*
372 * This function checks whether a page is free && is the buddy
373 * we can do coalesce a page and its buddy if
13e7444b 374 * (a) the buddy is not in a hole &&
676165a8 375 * (b) the buddy is in the buddy system &&
cb2b95e1
AW
376 * (c) a page and its buddy have the same order &&
377 * (d) a page and its buddy are in the same zone.
676165a8
NP
378 *
379 * For recording whether a page is in the buddy system, we use PG_buddy.
380 * Setting, clearing, and testing PG_buddy is serialized by zone->lock.
1da177e4 381 *
676165a8 382 * For recording page's order, we use page_private(page).
1da177e4 383 */
cb2b95e1
AW
384static inline int page_is_buddy(struct page *page, struct page *buddy,
385 int order)
1da177e4 386{
14e07298 387 if (!pfn_valid_within(page_to_pfn(buddy)))
13e7444b 388 return 0;
13e7444b 389
cb2b95e1
AW
390 if (page_zone_id(page) != page_zone_id(buddy))
391 return 0;
392
393 if (PageBuddy(buddy) && page_order(buddy) == order) {
394 BUG_ON(page_count(buddy) != 0);
6aa3001b 395 return 1;
676165a8 396 }
6aa3001b 397 return 0;
1da177e4
LT
398}
399
400/*
401 * Freeing function for a buddy system allocator.
402 *
403 * The concept of a buddy system is to maintain direct-mapped table
404 * (containing bit values) for memory blocks of various "orders".
405 * The bottom level table contains the map for the smallest allocatable
406 * units of memory (here, pages), and each level above it describes
407 * pairs of units from the levels below, hence, "buddies".
408 * At a high level, all that happens here is marking the table entry
409 * at the bottom level available, and propagating the changes upward
410 * as necessary, plus some accounting needed to play nicely with other
411 * parts of the VM system.
412 * At each level, we keep a list of pages, which are heads of continuous
676165a8 413 * free pages of length of (1 << order) and marked with PG_buddy. Page's
4c21e2f2 414 * order is recorded in page_private(page) field.
1da177e4
LT
415 * So when we are allocating or freeing one, we can derive the state of the
416 * other. That is, if we allocate a small block, and both were
417 * free, the remainder of the region must be split into blocks.
418 * If a block is freed, and its buddy is also free, then this
419 * triggers coalescing into a block of larger size.
420 *
421 * -- wli
422 */
423
48db57f8 424static inline void __free_one_page(struct page *page,
1da177e4
LT
425 struct zone *zone, unsigned int order)
426{
427 unsigned long page_idx;
428 int order_size = 1 << order;
b2a0ac88 429 int migratetype = get_pageblock_migratetype(page);
1da177e4 430
224abf92 431 if (unlikely(PageCompound(page)))
1da177e4
LT
432 destroy_compound_page(page, order);
433
434 page_idx = page_to_pfn(page) & ((1 << MAX_ORDER) - 1);
435
725d704e
NP
436 VM_BUG_ON(page_idx & (order_size - 1));
437 VM_BUG_ON(bad_range(zone, page));
1da177e4 438
d23ad423 439 __mod_zone_page_state(zone, NR_FREE_PAGES, order_size);
1da177e4
LT
440 while (order < MAX_ORDER-1) {
441 unsigned long combined_idx;
1da177e4
LT
442 struct page *buddy;
443
1da177e4 444 buddy = __page_find_buddy(page, page_idx, order);
cb2b95e1 445 if (!page_is_buddy(page, buddy, order))
3c82d0ce 446 break;
13e7444b 447
3c82d0ce 448 /* Our buddy is free, merge with it and move up one order. */
1da177e4 449 list_del(&buddy->lru);
b2a0ac88 450 zone->free_area[order].nr_free--;
1da177e4 451 rmv_page_order(buddy);
13e7444b 452 combined_idx = __find_combined_index(page_idx, order);
1da177e4
LT
453 page = page + (combined_idx - page_idx);
454 page_idx = combined_idx;
455 order++;
456 }
457 set_page_order(page, order);
b2a0ac88
MG
458 list_add(&page->lru,
459 &zone->free_area[order].free_list[migratetype]);
1da177e4
LT
460 zone->free_area[order].nr_free++;
461}
462
224abf92 463static inline int free_pages_check(struct page *page)
1da177e4 464{
985737cf 465 free_page_mlock(page);
92be2e33
NP
466 if (unlikely(page_mapcount(page) |
467 (page->mapping != NULL) |
468 (page_count(page) != 0) |
dfa7e20c 469 (page->flags & PAGE_FLAGS_CHECK_AT_FREE)))
224abf92 470 bad_page(page);
1da177e4 471 if (PageDirty(page))
242e5468 472 __ClearPageDirty(page);
b2e18538
RR
473 if (PageSwapBacked(page))
474 __ClearPageSwapBacked(page);
689bcebf
HD
475 /*
476 * For now, we report if PG_reserved was found set, but do not
477 * clear it, and do not free the page. But we shall soon need
478 * to do more, for when the ZERO_PAGE count wraps negative.
479 */
480 return PageReserved(page);
1da177e4
LT
481}
482
483/*
484 * Frees a list of pages.
485 * Assumes all pages on list are in same zone, and of same order.
207f36ee 486 * count is the number of pages to free.
1da177e4
LT
487 *
488 * If the zone was previously in an "all pages pinned" state then look to
489 * see if this freeing clears that state.
490 *
491 * And clear the zone's pages_scanned counter, to hold off the "all pages are
492 * pinned" detection logic.
493 */
48db57f8
NP
494static void free_pages_bulk(struct zone *zone, int count,
495 struct list_head *list, int order)
1da177e4 496{
c54ad30c 497 spin_lock(&zone->lock);
e815af95 498 zone_clear_flag(zone, ZONE_ALL_UNRECLAIMABLE);
1da177e4 499 zone->pages_scanned = 0;
48db57f8
NP
500 while (count--) {
501 struct page *page;
502
725d704e 503 VM_BUG_ON(list_empty(list));
1da177e4 504 page = list_entry(list->prev, struct page, lru);
48db57f8 505 /* have to delete it as __free_one_page list manipulates */
1da177e4 506 list_del(&page->lru);
48db57f8 507 __free_one_page(page, zone, order);
1da177e4 508 }
c54ad30c 509 spin_unlock(&zone->lock);
1da177e4
LT
510}
511
48db57f8 512static void free_one_page(struct zone *zone, struct page *page, int order)
1da177e4 513{
006d22d9 514 spin_lock(&zone->lock);
e815af95 515 zone_clear_flag(zone, ZONE_ALL_UNRECLAIMABLE);
006d22d9 516 zone->pages_scanned = 0;
0798e519 517 __free_one_page(page, zone, order);
006d22d9 518 spin_unlock(&zone->lock);
48db57f8
NP
519}
520
521static void __free_pages_ok(struct page *page, unsigned int order)
522{
523 unsigned long flags;
1da177e4 524 int i;
689bcebf 525 int reserved = 0;
1da177e4 526
1da177e4 527 for (i = 0 ; i < (1 << order) ; ++i)
224abf92 528 reserved += free_pages_check(page + i);
689bcebf
HD
529 if (reserved)
530 return;
531
3ac7fe5a 532 if (!PageHighMem(page)) {
9858db50 533 debug_check_no_locks_freed(page_address(page),PAGE_SIZE<<order);
3ac7fe5a
TG
534 debug_check_no_obj_freed(page_address(page),
535 PAGE_SIZE << order);
536 }
dafb1367 537 arch_free_page(page, order);
48db57f8 538 kernel_map_pages(page, 1 << order, 0);
dafb1367 539
c54ad30c 540 local_irq_save(flags);
f8891e5e 541 __count_vm_events(PGFREE, 1 << order);
48db57f8 542 free_one_page(page_zone(page), page, order);
c54ad30c 543 local_irq_restore(flags);
1da177e4
LT
544}
545
a226f6c8
DH
546/*
547 * permit the bootmem allocator to evade page validation on high-order frees
548 */
af370fb8 549void __meminit __free_pages_bootmem(struct page *page, unsigned int order)
a226f6c8
DH
550{
551 if (order == 0) {
552 __ClearPageReserved(page);
553 set_page_count(page, 0);
7835e98b 554 set_page_refcounted(page);
545b1ea9 555 __free_page(page);
a226f6c8 556 } else {
a226f6c8
DH
557 int loop;
558
545b1ea9 559 prefetchw(page);
a226f6c8
DH
560 for (loop = 0; loop < BITS_PER_LONG; loop++) {
561 struct page *p = &page[loop];
562
545b1ea9
NP
563 if (loop + 1 < BITS_PER_LONG)
564 prefetchw(p + 1);
a226f6c8
DH
565 __ClearPageReserved(p);
566 set_page_count(p, 0);
567 }
568
7835e98b 569 set_page_refcounted(page);
545b1ea9 570 __free_pages(page, order);
a226f6c8
DH
571 }
572}
573
1da177e4
LT
574
575/*
576 * The order of subdivision here is critical for the IO subsystem.
577 * Please do not alter this order without good reasons and regression
578 * testing. Specifically, as large blocks of memory are subdivided,
579 * the order in which smaller blocks are delivered depends on the order
580 * they're subdivided in this function. This is the primary factor
581 * influencing the order in which pages are delivered to the IO
582 * subsystem according to empirical testing, and this is also justified
583 * by considering the behavior of a buddy system containing a single
584 * large block of memory acted on by a series of small allocations.
585 * This behavior is a critical factor in sglist merging's success.
586 *
587 * -- wli
588 */
085cc7d5 589static inline void expand(struct zone *zone, struct page *page,
b2a0ac88
MG
590 int low, int high, struct free_area *area,
591 int migratetype)
1da177e4
LT
592{
593 unsigned long size = 1 << high;
594
595 while (high > low) {
596 area--;
597 high--;
598 size >>= 1;
725d704e 599 VM_BUG_ON(bad_range(zone, &page[size]));
b2a0ac88 600 list_add(&page[size].lru, &area->free_list[migratetype]);
1da177e4
LT
601 area->nr_free++;
602 set_page_order(&page[size], high);
603 }
1da177e4
LT
604}
605
1da177e4
LT
606/*
607 * This page is about to be returned from the page allocator
608 */
17cf4406 609static int prep_new_page(struct page *page, int order, gfp_t gfp_flags)
1da177e4 610{
92be2e33
NP
611 if (unlikely(page_mapcount(page) |
612 (page->mapping != NULL) |
613 (page_count(page) != 0) |
dfa7e20c 614 (page->flags & PAGE_FLAGS_CHECK_AT_PREP)))
224abf92 615 bad_page(page);
1da177e4 616
689bcebf
HD
617 /*
618 * For now, we report if PG_reserved was found set, but do not
619 * clear it, and do not allocate the page: as a safety net.
620 */
621 if (PageReserved(page))
622 return 1;
623
0a128b2b 624 page->flags &= ~(1 << PG_uptodate | 1 << PG_error | 1 << PG_reclaim |
1da177e4 625 1 << PG_referenced | 1 << PG_arch_1 |
b291f000
NP
626 1 << PG_owner_priv_1 | 1 << PG_mappedtodisk
627#ifdef CONFIG_UNEVICTABLE_LRU
628 | 1 << PG_mlocked
629#endif
630 );
4c21e2f2 631 set_page_private(page, 0);
7835e98b 632 set_page_refcounted(page);
cc102509
NP
633
634 arch_alloc_page(page, order);
1da177e4 635 kernel_map_pages(page, 1 << order, 1);
17cf4406
NP
636
637 if (gfp_flags & __GFP_ZERO)
638 prep_zero_page(page, order, gfp_flags);
639
640 if (order && (gfp_flags & __GFP_COMP))
641 prep_compound_page(page, order);
642
689bcebf 643 return 0;
1da177e4
LT
644}
645
56fd56b8
MG
646/*
647 * Go through the free lists for the given migratetype and remove
648 * the smallest available page from the freelists
649 */
650static struct page *__rmqueue_smallest(struct zone *zone, unsigned int order,
651 int migratetype)
652{
653 unsigned int current_order;
654 struct free_area * area;
655 struct page *page;
656
657 /* Find a page of the appropriate size in the preferred list */
658 for (current_order = order; current_order < MAX_ORDER; ++current_order) {
659 area = &(zone->free_area[current_order]);
660 if (list_empty(&area->free_list[migratetype]))
661 continue;
662
663 page = list_entry(area->free_list[migratetype].next,
664 struct page, lru);
665 list_del(&page->lru);
666 rmv_page_order(page);
667 area->nr_free--;
668 __mod_zone_page_state(zone, NR_FREE_PAGES, - (1UL << order));
669 expand(zone, page, order, current_order, area, migratetype);
670 return page;
671 }
672
673 return NULL;
674}
675
676
b2a0ac88
MG
677/*
678 * This array describes the order lists are fallen back to when
679 * the free lists for the desirable migrate type are depleted
680 */
681static int fallbacks[MIGRATE_TYPES][MIGRATE_TYPES-1] = {
64c5e135
MG
682 [MIGRATE_UNMOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_MOVABLE, MIGRATE_RESERVE },
683 [MIGRATE_RECLAIMABLE] = { MIGRATE_UNMOVABLE, MIGRATE_MOVABLE, MIGRATE_RESERVE },
684 [MIGRATE_MOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE, MIGRATE_RESERVE },
685 [MIGRATE_RESERVE] = { MIGRATE_RESERVE, MIGRATE_RESERVE, MIGRATE_RESERVE }, /* Never used */
b2a0ac88
MG
686};
687
c361be55
MG
688/*
689 * Move the free pages in a range to the free lists of the requested type.
d9c23400 690 * Note that start_page and end_pages are not aligned on a pageblock
c361be55
MG
691 * boundary. If alignment is required, use move_freepages_block()
692 */
b69a7288
AB
693static int move_freepages(struct zone *zone,
694 struct page *start_page, struct page *end_page,
695 int migratetype)
c361be55
MG
696{
697 struct page *page;
698 unsigned long order;
d100313f 699 int pages_moved = 0;
c361be55
MG
700
701#ifndef CONFIG_HOLES_IN_ZONE
702 /*
703 * page_zone is not safe to call in this context when
704 * CONFIG_HOLES_IN_ZONE is set. This bug check is probably redundant
705 * anyway as we check zone boundaries in move_freepages_block().
706 * Remove at a later date when no bug reports exist related to
ac0e5b7a 707 * grouping pages by mobility
c361be55
MG
708 */
709 BUG_ON(page_zone(start_page) != page_zone(end_page));
710#endif
711
712 for (page = start_page; page <= end_page;) {
344c790e
AL
713 /* Make sure we are not inadvertently changing nodes */
714 VM_BUG_ON(page_to_nid(page) != zone_to_nid(zone));
715
c361be55
MG
716 if (!pfn_valid_within(page_to_pfn(page))) {
717 page++;
718 continue;
719 }
720
721 if (!PageBuddy(page)) {
722 page++;
723 continue;
724 }
725
726 order = page_order(page);
727 list_del(&page->lru);
728 list_add(&page->lru,
729 &zone->free_area[order].free_list[migratetype]);
730 page += 1 << order;
d100313f 731 pages_moved += 1 << order;
c361be55
MG
732 }
733
d100313f 734 return pages_moved;
c361be55
MG
735}
736
b69a7288
AB
737static int move_freepages_block(struct zone *zone, struct page *page,
738 int migratetype)
c361be55
MG
739{
740 unsigned long start_pfn, end_pfn;
741 struct page *start_page, *end_page;
742
743 start_pfn = page_to_pfn(page);
d9c23400 744 start_pfn = start_pfn & ~(pageblock_nr_pages-1);
c361be55 745 start_page = pfn_to_page(start_pfn);
d9c23400
MG
746 end_page = start_page + pageblock_nr_pages - 1;
747 end_pfn = start_pfn + pageblock_nr_pages - 1;
c361be55
MG
748
749 /* Do not cross zone boundaries */
750 if (start_pfn < zone->zone_start_pfn)
751 start_page = page;
752 if (end_pfn >= zone->zone_start_pfn + zone->spanned_pages)
753 return 0;
754
755 return move_freepages(zone, start_page, end_page, migratetype);
756}
757
b2a0ac88
MG
758/* Remove an element from the buddy allocator from the fallback list */
759static struct page *__rmqueue_fallback(struct zone *zone, int order,
760 int start_migratetype)
761{
762 struct free_area * area;
763 int current_order;
764 struct page *page;
765 int migratetype, i;
766
767 /* Find the largest possible block of pages in the other list */
768 for (current_order = MAX_ORDER-1; current_order >= order;
769 --current_order) {
770 for (i = 0; i < MIGRATE_TYPES - 1; i++) {
771 migratetype = fallbacks[start_migratetype][i];
772
56fd56b8
MG
773 /* MIGRATE_RESERVE handled later if necessary */
774 if (migratetype == MIGRATE_RESERVE)
775 continue;
e010487d 776
b2a0ac88
MG
777 area = &(zone->free_area[current_order]);
778 if (list_empty(&area->free_list[migratetype]))
779 continue;
780
781 page = list_entry(area->free_list[migratetype].next,
782 struct page, lru);
783 area->nr_free--;
784
785 /*
c361be55 786 * If breaking a large block of pages, move all free
46dafbca
MG
787 * pages to the preferred allocation list. If falling
788 * back for a reclaimable kernel allocation, be more
789 * agressive about taking ownership of free pages
b2a0ac88 790 */
d9c23400 791 if (unlikely(current_order >= (pageblock_order >> 1)) ||
46dafbca
MG
792 start_migratetype == MIGRATE_RECLAIMABLE) {
793 unsigned long pages;
794 pages = move_freepages_block(zone, page,
795 start_migratetype);
796
797 /* Claim the whole block if over half of it is free */
d9c23400 798 if (pages >= (1 << (pageblock_order-1)))
46dafbca
MG
799 set_pageblock_migratetype(page,
800 start_migratetype);
801
b2a0ac88 802 migratetype = start_migratetype;
c361be55 803 }
b2a0ac88
MG
804
805 /* Remove the page from the freelists */
806 list_del(&page->lru);
807 rmv_page_order(page);
808 __mod_zone_page_state(zone, NR_FREE_PAGES,
809 -(1UL << order));
810
d9c23400 811 if (current_order == pageblock_order)
b2a0ac88
MG
812 set_pageblock_migratetype(page,
813 start_migratetype);
814
815 expand(zone, page, order, current_order, area, migratetype);
816 return page;
817 }
818 }
819
56fd56b8
MG
820 /* Use MIGRATE_RESERVE rather than fail an allocation */
821 return __rmqueue_smallest(zone, order, MIGRATE_RESERVE);
b2a0ac88
MG
822}
823
56fd56b8 824/*
1da177e4
LT
825 * Do the hard work of removing an element from the buddy allocator.
826 * Call me with the zone->lock already held.
827 */
b2a0ac88
MG
828static struct page *__rmqueue(struct zone *zone, unsigned int order,
829 int migratetype)
1da177e4 830{
1da177e4
LT
831 struct page *page;
832
56fd56b8 833 page = __rmqueue_smallest(zone, order, migratetype);
b2a0ac88 834
56fd56b8
MG
835 if (unlikely(!page))
836 page = __rmqueue_fallback(zone, order, migratetype);
b2a0ac88
MG
837
838 return page;
1da177e4
LT
839}
840
841/*
842 * Obtain a specified number of elements from the buddy allocator, all under
843 * a single hold of the lock, for efficiency. Add them to the supplied list.
844 * Returns the number of new pages which were placed at *list.
845 */
846static int rmqueue_bulk(struct zone *zone, unsigned int order,
b2a0ac88
MG
847 unsigned long count, struct list_head *list,
848 int migratetype)
1da177e4 849{
1da177e4 850 int i;
1da177e4 851
c54ad30c 852 spin_lock(&zone->lock);
1da177e4 853 for (i = 0; i < count; ++i) {
b2a0ac88 854 struct page *page = __rmqueue(zone, order, migratetype);
085cc7d5 855 if (unlikely(page == NULL))
1da177e4 856 break;
81eabcbe
MG
857
858 /*
859 * Split buddy pages returned by expand() are received here
860 * in physical page order. The page is added to the callers and
861 * list and the list head then moves forward. From the callers
862 * perspective, the linked list is ordered by page number in
863 * some conditions. This is useful for IO devices that can
864 * merge IO requests if the physical pages are ordered
865 * properly.
866 */
535131e6
MG
867 list_add(&page->lru, list);
868 set_page_private(page, migratetype);
81eabcbe 869 list = &page->lru;
1da177e4 870 }
c54ad30c 871 spin_unlock(&zone->lock);
085cc7d5 872 return i;
1da177e4
LT
873}
874
4ae7c039 875#ifdef CONFIG_NUMA
8fce4d8e 876/*
4037d452
CL
877 * Called from the vmstat counter updater to drain pagesets of this
878 * currently executing processor on remote nodes after they have
879 * expired.
880 *
879336c3
CL
881 * Note that this function must be called with the thread pinned to
882 * a single processor.
8fce4d8e 883 */
4037d452 884void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
4ae7c039 885{
4ae7c039 886 unsigned long flags;
4037d452 887 int to_drain;
4ae7c039 888
4037d452
CL
889 local_irq_save(flags);
890 if (pcp->count >= pcp->batch)
891 to_drain = pcp->batch;
892 else
893 to_drain = pcp->count;
894 free_pages_bulk(zone, to_drain, &pcp->list, 0);
895 pcp->count -= to_drain;
896 local_irq_restore(flags);
4ae7c039
CL
897}
898#endif
899
9f8f2172
CL
900/*
901 * Drain pages of the indicated processor.
902 *
903 * The processor must either be the current processor and the
904 * thread pinned to the current processor or a processor that
905 * is not online.
906 */
907static void drain_pages(unsigned int cpu)
1da177e4 908{
c54ad30c 909 unsigned long flags;
1da177e4 910 struct zone *zone;
1da177e4
LT
911
912 for_each_zone(zone) {
913 struct per_cpu_pageset *pset;
3dfa5721 914 struct per_cpu_pages *pcp;
1da177e4 915
f2e12bb2
CL
916 if (!populated_zone(zone))
917 continue;
918
e7c8d5c9 919 pset = zone_pcp(zone, cpu);
3dfa5721
CL
920
921 pcp = &pset->pcp;
922 local_irq_save(flags);
923 free_pages_bulk(zone, pcp->count, &pcp->list, 0);
924 pcp->count = 0;
925 local_irq_restore(flags);
1da177e4
LT
926 }
927}
1da177e4 928
9f8f2172
CL
929/*
930 * Spill all of this CPU's per-cpu pages back into the buddy allocator.
931 */
932void drain_local_pages(void *arg)
933{
934 drain_pages(smp_processor_id());
935}
936
937/*
938 * Spill all the per-cpu pages from all CPUs back into the buddy allocator
939 */
940void drain_all_pages(void)
941{
15c8b6c1 942 on_each_cpu(drain_local_pages, NULL, 1);
9f8f2172
CL
943}
944
296699de 945#ifdef CONFIG_HIBERNATION
1da177e4
LT
946
947void mark_free_pages(struct zone *zone)
948{
f623f0db
RW
949 unsigned long pfn, max_zone_pfn;
950 unsigned long flags;
b2a0ac88 951 int order, t;
1da177e4
LT
952 struct list_head *curr;
953
954 if (!zone->spanned_pages)
955 return;
956
957 spin_lock_irqsave(&zone->lock, flags);
f623f0db
RW
958
959 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
960 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
961 if (pfn_valid(pfn)) {
962 struct page *page = pfn_to_page(pfn);
963
7be98234
RW
964 if (!swsusp_page_is_forbidden(page))
965 swsusp_unset_page_free(page);
f623f0db 966 }
1da177e4 967
b2a0ac88
MG
968 for_each_migratetype_order(order, t) {
969 list_for_each(curr, &zone->free_area[order].free_list[t]) {
f623f0db 970 unsigned long i;
1da177e4 971
f623f0db
RW
972 pfn = page_to_pfn(list_entry(curr, struct page, lru));
973 for (i = 0; i < (1UL << order); i++)
7be98234 974 swsusp_set_page_free(pfn_to_page(pfn + i));
f623f0db 975 }
b2a0ac88 976 }
1da177e4
LT
977 spin_unlock_irqrestore(&zone->lock, flags);
978}
e2c55dc8 979#endif /* CONFIG_PM */
1da177e4 980
1da177e4
LT
981/*
982 * Free a 0-order page
983 */
920c7a5d 984static void free_hot_cold_page(struct page *page, int cold)
1da177e4
LT
985{
986 struct zone *zone = page_zone(page);
987 struct per_cpu_pages *pcp;
988 unsigned long flags;
989
1da177e4
LT
990 if (PageAnon(page))
991 page->mapping = NULL;
224abf92 992 if (free_pages_check(page))
689bcebf
HD
993 return;
994
3ac7fe5a 995 if (!PageHighMem(page)) {
9858db50 996 debug_check_no_locks_freed(page_address(page), PAGE_SIZE);
3ac7fe5a
TG
997 debug_check_no_obj_freed(page_address(page), PAGE_SIZE);
998 }
dafb1367 999 arch_free_page(page, 0);
689bcebf
HD
1000 kernel_map_pages(page, 1, 0);
1001
3dfa5721 1002 pcp = &zone_pcp(zone, get_cpu())->pcp;
1da177e4 1003 local_irq_save(flags);
f8891e5e 1004 __count_vm_event(PGFREE);
3dfa5721
CL
1005 if (cold)
1006 list_add_tail(&page->lru, &pcp->list);
1007 else
1008 list_add(&page->lru, &pcp->list);
535131e6 1009 set_page_private(page, get_pageblock_migratetype(page));
1da177e4 1010 pcp->count++;
48db57f8
NP
1011 if (pcp->count >= pcp->high) {
1012 free_pages_bulk(zone, pcp->batch, &pcp->list, 0);
1013 pcp->count -= pcp->batch;
1014 }
1da177e4
LT
1015 local_irq_restore(flags);
1016 put_cpu();
1017}
1018
920c7a5d 1019void free_hot_page(struct page *page)
1da177e4
LT
1020{
1021 free_hot_cold_page(page, 0);
1022}
1023
920c7a5d 1024void free_cold_page(struct page *page)
1da177e4
LT
1025{
1026 free_hot_cold_page(page, 1);
1027}
1028
8dfcc9ba
NP
1029/*
1030 * split_page takes a non-compound higher-order page, and splits it into
1031 * n (1<<order) sub-pages: page[0..n]
1032 * Each sub-page must be freed individually.
1033 *
1034 * Note: this is probably too low level an operation for use in drivers.
1035 * Please consult with lkml before using this in your driver.
1036 */
1037void split_page(struct page *page, unsigned int order)
1038{
1039 int i;
1040
725d704e
NP
1041 VM_BUG_ON(PageCompound(page));
1042 VM_BUG_ON(!page_count(page));
7835e98b
NP
1043 for (i = 1; i < (1 << order); i++)
1044 set_page_refcounted(page + i);
8dfcc9ba 1045}
8dfcc9ba 1046
1da177e4
LT
1047/*
1048 * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
1049 * we cheat by calling it from here, in the order > 0 path. Saves a branch
1050 * or two.
1051 */
18ea7e71 1052static struct page *buffered_rmqueue(struct zone *preferred_zone,
a74609fa 1053 struct zone *zone, int order, gfp_t gfp_flags)
1da177e4
LT
1054{
1055 unsigned long flags;
689bcebf 1056 struct page *page;
1da177e4 1057 int cold = !!(gfp_flags & __GFP_COLD);
a74609fa 1058 int cpu;
64c5e135 1059 int migratetype = allocflags_to_migratetype(gfp_flags);
1da177e4 1060
689bcebf 1061again:
a74609fa 1062 cpu = get_cpu();
48db57f8 1063 if (likely(order == 0)) {
1da177e4
LT
1064 struct per_cpu_pages *pcp;
1065
3dfa5721 1066 pcp = &zone_pcp(zone, cpu)->pcp;
1da177e4 1067 local_irq_save(flags);
a74609fa 1068 if (!pcp->count) {
941c7105 1069 pcp->count = rmqueue_bulk(zone, 0,
b2a0ac88 1070 pcp->batch, &pcp->list, migratetype);
a74609fa
NP
1071 if (unlikely(!pcp->count))
1072 goto failed;
1da177e4 1073 }
b92a6edd 1074
535131e6 1075 /* Find a page of the appropriate migrate type */
3dfa5721
CL
1076 if (cold) {
1077 list_for_each_entry_reverse(page, &pcp->list, lru)
1078 if (page_private(page) == migratetype)
1079 break;
1080 } else {
1081 list_for_each_entry(page, &pcp->list, lru)
1082 if (page_private(page) == migratetype)
1083 break;
1084 }
535131e6 1085
b92a6edd
MG
1086 /* Allocate more to the pcp list if necessary */
1087 if (unlikely(&page->lru == &pcp->list)) {
535131e6
MG
1088 pcp->count += rmqueue_bulk(zone, 0,
1089 pcp->batch, &pcp->list, migratetype);
1090 page = list_entry(pcp->list.next, struct page, lru);
535131e6 1091 }
b92a6edd
MG
1092
1093 list_del(&page->lru);
1094 pcp->count--;
7fb1d9fc 1095 } else {
1da177e4 1096 spin_lock_irqsave(&zone->lock, flags);
b2a0ac88 1097 page = __rmqueue(zone, order, migratetype);
a74609fa
NP
1098 spin_unlock(&zone->lock);
1099 if (!page)
1100 goto failed;
1da177e4
LT
1101 }
1102
f8891e5e 1103 __count_zone_vm_events(PGALLOC, zone, 1 << order);
18ea7e71 1104 zone_statistics(preferred_zone, zone);
a74609fa
NP
1105 local_irq_restore(flags);
1106 put_cpu();
1da177e4 1107
725d704e 1108 VM_BUG_ON(bad_range(zone, page));
17cf4406 1109 if (prep_new_page(page, order, gfp_flags))
a74609fa 1110 goto again;
1da177e4 1111 return page;
a74609fa
NP
1112
1113failed:
1114 local_irq_restore(flags);
1115 put_cpu();
1116 return NULL;
1da177e4
LT
1117}
1118
7fb1d9fc 1119#define ALLOC_NO_WATERMARKS 0x01 /* don't check watermarks at all */
3148890b
NP
1120#define ALLOC_WMARK_MIN 0x02 /* use pages_min watermark */
1121#define ALLOC_WMARK_LOW 0x04 /* use pages_low watermark */
1122#define ALLOC_WMARK_HIGH 0x08 /* use pages_high watermark */
1123#define ALLOC_HARDER 0x10 /* try to alloc harder */
1124#define ALLOC_HIGH 0x20 /* __GFP_HIGH set */
1125#define ALLOC_CPUSET 0x40 /* check for correct cpuset */
7fb1d9fc 1126
933e312e
AM
1127#ifdef CONFIG_FAIL_PAGE_ALLOC
1128
1129static struct fail_page_alloc_attr {
1130 struct fault_attr attr;
1131
1132 u32 ignore_gfp_highmem;
1133 u32 ignore_gfp_wait;
54114994 1134 u32 min_order;
933e312e
AM
1135
1136#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
1137
1138 struct dentry *ignore_gfp_highmem_file;
1139 struct dentry *ignore_gfp_wait_file;
54114994 1140 struct dentry *min_order_file;
933e312e
AM
1141
1142#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
1143
1144} fail_page_alloc = {
1145 .attr = FAULT_ATTR_INITIALIZER,
6b1b60f4
DM
1146 .ignore_gfp_wait = 1,
1147 .ignore_gfp_highmem = 1,
54114994 1148 .min_order = 1,
933e312e
AM
1149};
1150
1151static int __init setup_fail_page_alloc(char *str)
1152{
1153 return setup_fault_attr(&fail_page_alloc.attr, str);
1154}
1155__setup("fail_page_alloc=", setup_fail_page_alloc);
1156
1157static int should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
1158{
54114994
AM
1159 if (order < fail_page_alloc.min_order)
1160 return 0;
933e312e
AM
1161 if (gfp_mask & __GFP_NOFAIL)
1162 return 0;
1163 if (fail_page_alloc.ignore_gfp_highmem && (gfp_mask & __GFP_HIGHMEM))
1164 return 0;
1165 if (fail_page_alloc.ignore_gfp_wait && (gfp_mask & __GFP_WAIT))
1166 return 0;
1167
1168 return should_fail(&fail_page_alloc.attr, 1 << order);
1169}
1170
1171#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
1172
1173static int __init fail_page_alloc_debugfs(void)
1174{
1175 mode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
1176 struct dentry *dir;
1177 int err;
1178
1179 err = init_fault_attr_dentries(&fail_page_alloc.attr,
1180 "fail_page_alloc");
1181 if (err)
1182 return err;
1183 dir = fail_page_alloc.attr.dentries.dir;
1184
1185 fail_page_alloc.ignore_gfp_wait_file =
1186 debugfs_create_bool("ignore-gfp-wait", mode, dir,
1187 &fail_page_alloc.ignore_gfp_wait);
1188
1189 fail_page_alloc.ignore_gfp_highmem_file =
1190 debugfs_create_bool("ignore-gfp-highmem", mode, dir,
1191 &fail_page_alloc.ignore_gfp_highmem);
54114994
AM
1192 fail_page_alloc.min_order_file =
1193 debugfs_create_u32("min-order", mode, dir,
1194 &fail_page_alloc.min_order);
933e312e
AM
1195
1196 if (!fail_page_alloc.ignore_gfp_wait_file ||
54114994
AM
1197 !fail_page_alloc.ignore_gfp_highmem_file ||
1198 !fail_page_alloc.min_order_file) {
933e312e
AM
1199 err = -ENOMEM;
1200 debugfs_remove(fail_page_alloc.ignore_gfp_wait_file);
1201 debugfs_remove(fail_page_alloc.ignore_gfp_highmem_file);
54114994 1202 debugfs_remove(fail_page_alloc.min_order_file);
933e312e
AM
1203 cleanup_fault_attr_dentries(&fail_page_alloc.attr);
1204 }
1205
1206 return err;
1207}
1208
1209late_initcall(fail_page_alloc_debugfs);
1210
1211#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
1212
1213#else /* CONFIG_FAIL_PAGE_ALLOC */
1214
1215static inline int should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
1216{
1217 return 0;
1218}
1219
1220#endif /* CONFIG_FAIL_PAGE_ALLOC */
1221
1da177e4
LT
1222/*
1223 * Return 1 if free pages are above 'mark'. This takes into account the order
1224 * of the allocation.
1225 */
1226int zone_watermark_ok(struct zone *z, int order, unsigned long mark,
7fb1d9fc 1227 int classzone_idx, int alloc_flags)
1da177e4
LT
1228{
1229 /* free_pages my go negative - that's OK */
d23ad423
CL
1230 long min = mark;
1231 long free_pages = zone_page_state(z, NR_FREE_PAGES) - (1 << order) + 1;
1da177e4
LT
1232 int o;
1233
7fb1d9fc 1234 if (alloc_flags & ALLOC_HIGH)
1da177e4 1235 min -= min / 2;
7fb1d9fc 1236 if (alloc_flags & ALLOC_HARDER)
1da177e4
LT
1237 min -= min / 4;
1238
1239 if (free_pages <= min + z->lowmem_reserve[classzone_idx])
1240 return 0;
1241 for (o = 0; o < order; o++) {
1242 /* At the next order, this order's pages become unavailable */
1243 free_pages -= z->free_area[o].nr_free << o;
1244
1245 /* Require fewer higher order pages to be free */
1246 min >>= 1;
1247
1248 if (free_pages <= min)
1249 return 0;
1250 }
1251 return 1;
1252}
1253
9276b1bc
PJ
1254#ifdef CONFIG_NUMA
1255/*
1256 * zlc_setup - Setup for "zonelist cache". Uses cached zone data to
1257 * skip over zones that are not allowed by the cpuset, or that have
1258 * been recently (in last second) found to be nearly full. See further
1259 * comments in mmzone.h. Reduces cache footprint of zonelist scans
183ff22b 1260 * that have to skip over a lot of full or unallowed zones.
9276b1bc
PJ
1261 *
1262 * If the zonelist cache is present in the passed in zonelist, then
1263 * returns a pointer to the allowed node mask (either the current
37b07e41 1264 * tasks mems_allowed, or node_states[N_HIGH_MEMORY].)
9276b1bc
PJ
1265 *
1266 * If the zonelist cache is not available for this zonelist, does
1267 * nothing and returns NULL.
1268 *
1269 * If the fullzones BITMAP in the zonelist cache is stale (more than
1270 * a second since last zap'd) then we zap it out (clear its bits.)
1271 *
1272 * We hold off even calling zlc_setup, until after we've checked the
1273 * first zone in the zonelist, on the theory that most allocations will
1274 * be satisfied from that first zone, so best to examine that zone as
1275 * quickly as we can.
1276 */
1277static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
1278{
1279 struct zonelist_cache *zlc; /* cached zonelist speedup info */
1280 nodemask_t *allowednodes; /* zonelist_cache approximation */
1281
1282 zlc = zonelist->zlcache_ptr;
1283 if (!zlc)
1284 return NULL;
1285
f05111f5 1286 if (time_after(jiffies, zlc->last_full_zap + HZ)) {
9276b1bc
PJ
1287 bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
1288 zlc->last_full_zap = jiffies;
1289 }
1290
1291 allowednodes = !in_interrupt() && (alloc_flags & ALLOC_CPUSET) ?
1292 &cpuset_current_mems_allowed :
37b07e41 1293 &node_states[N_HIGH_MEMORY];
9276b1bc
PJ
1294 return allowednodes;
1295}
1296
1297/*
1298 * Given 'z' scanning a zonelist, run a couple of quick checks to see
1299 * if it is worth looking at further for free memory:
1300 * 1) Check that the zone isn't thought to be full (doesn't have its
1301 * bit set in the zonelist_cache fullzones BITMAP).
1302 * 2) Check that the zones node (obtained from the zonelist_cache
1303 * z_to_n[] mapping) is allowed in the passed in allowednodes mask.
1304 * Return true (non-zero) if zone is worth looking at further, or
1305 * else return false (zero) if it is not.
1306 *
1307 * This check -ignores- the distinction between various watermarks,
1308 * such as GFP_HIGH, GFP_ATOMIC, PF_MEMALLOC, ... If a zone is
1309 * found to be full for any variation of these watermarks, it will
1310 * be considered full for up to one second by all requests, unless
1311 * we are so low on memory on all allowed nodes that we are forced
1312 * into the second scan of the zonelist.
1313 *
1314 * In the second scan we ignore this zonelist cache and exactly
1315 * apply the watermarks to all zones, even it is slower to do so.
1316 * We are low on memory in the second scan, and should leave no stone
1317 * unturned looking for a free page.
1318 */
dd1a239f 1319static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zoneref *z,
9276b1bc
PJ
1320 nodemask_t *allowednodes)
1321{
1322 struct zonelist_cache *zlc; /* cached zonelist speedup info */
1323 int i; /* index of *z in zonelist zones */
1324 int n; /* node that zone *z is on */
1325
1326 zlc = zonelist->zlcache_ptr;
1327 if (!zlc)
1328 return 1;
1329
dd1a239f 1330 i = z - zonelist->_zonerefs;
9276b1bc
PJ
1331 n = zlc->z_to_n[i];
1332
1333 /* This zone is worth trying if it is allowed but not full */
1334 return node_isset(n, *allowednodes) && !test_bit(i, zlc->fullzones);
1335}
1336
1337/*
1338 * Given 'z' scanning a zonelist, set the corresponding bit in
1339 * zlc->fullzones, so that subsequent attempts to allocate a page
1340 * from that zone don't waste time re-examining it.
1341 */
dd1a239f 1342static void zlc_mark_zone_full(struct zonelist *zonelist, struct zoneref *z)
9276b1bc
PJ
1343{
1344 struct zonelist_cache *zlc; /* cached zonelist speedup info */
1345 int i; /* index of *z in zonelist zones */
1346
1347 zlc = zonelist->zlcache_ptr;
1348 if (!zlc)
1349 return;
1350
dd1a239f 1351 i = z - zonelist->_zonerefs;
9276b1bc
PJ
1352
1353 set_bit(i, zlc->fullzones);
1354}
1355
1356#else /* CONFIG_NUMA */
1357
1358static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
1359{
1360 return NULL;
1361}
1362
dd1a239f 1363static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zoneref *z,
9276b1bc
PJ
1364 nodemask_t *allowednodes)
1365{
1366 return 1;
1367}
1368
dd1a239f 1369static void zlc_mark_zone_full(struct zonelist *zonelist, struct zoneref *z)
9276b1bc
PJ
1370{
1371}
1372#endif /* CONFIG_NUMA */
1373
7fb1d9fc 1374/*
0798e519 1375 * get_page_from_freelist goes through the zonelist trying to allocate
7fb1d9fc
RS
1376 * a page.
1377 */
1378static struct page *
19770b32 1379get_page_from_freelist(gfp_t gfp_mask, nodemask_t *nodemask, unsigned int order,
54a6eb5c 1380 struct zonelist *zonelist, int high_zoneidx, int alloc_flags)
753ee728 1381{
dd1a239f 1382 struct zoneref *z;
7fb1d9fc 1383 struct page *page = NULL;
54a6eb5c 1384 int classzone_idx;
18ea7e71 1385 struct zone *zone, *preferred_zone;
9276b1bc
PJ
1386 nodemask_t *allowednodes = NULL;/* zonelist_cache approximation */
1387 int zlc_active = 0; /* set if using zonelist_cache */
1388 int did_zlc_setup = 0; /* just call zlc_setup() one time */
54a6eb5c 1389
19770b32
MG
1390 (void)first_zones_zonelist(zonelist, high_zoneidx, nodemask,
1391 &preferred_zone);
7eb54824
AW
1392 if (!preferred_zone)
1393 return NULL;
1394
19770b32 1395 classzone_idx = zone_idx(preferred_zone);
7fb1d9fc 1396
9276b1bc 1397zonelist_scan:
7fb1d9fc 1398 /*
9276b1bc 1399 * Scan zonelist, looking for a zone with enough free.
7fb1d9fc
RS
1400 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
1401 */
19770b32
MG
1402 for_each_zone_zonelist_nodemask(zone, z, zonelist,
1403 high_zoneidx, nodemask) {
9276b1bc
PJ
1404 if (NUMA_BUILD && zlc_active &&
1405 !zlc_zone_worth_trying(zonelist, z, allowednodes))
1406 continue;
7fb1d9fc 1407 if ((alloc_flags & ALLOC_CPUSET) &&
02a0e53d 1408 !cpuset_zone_allowed_softwall(zone, gfp_mask))
9276b1bc 1409 goto try_next_zone;
7fb1d9fc
RS
1410
1411 if (!(alloc_flags & ALLOC_NO_WATERMARKS)) {
3148890b
NP
1412 unsigned long mark;
1413 if (alloc_flags & ALLOC_WMARK_MIN)
1192d526 1414 mark = zone->pages_min;
3148890b 1415 else if (alloc_flags & ALLOC_WMARK_LOW)
1192d526 1416 mark = zone->pages_low;
3148890b 1417 else
1192d526 1418 mark = zone->pages_high;
0798e519
PJ
1419 if (!zone_watermark_ok(zone, order, mark,
1420 classzone_idx, alloc_flags)) {
9eeff239 1421 if (!zone_reclaim_mode ||
1192d526 1422 !zone_reclaim(zone, gfp_mask, order))
9276b1bc 1423 goto this_zone_full;
0798e519 1424 }
7fb1d9fc
RS
1425 }
1426
18ea7e71 1427 page = buffered_rmqueue(preferred_zone, zone, order, gfp_mask);
0798e519 1428 if (page)
7fb1d9fc 1429 break;
9276b1bc
PJ
1430this_zone_full:
1431 if (NUMA_BUILD)
1432 zlc_mark_zone_full(zonelist, z);
1433try_next_zone:
1434 if (NUMA_BUILD && !did_zlc_setup) {
1435 /* we do zlc_setup after the first zone is tried */
1436 allowednodes = zlc_setup(zonelist, alloc_flags);
1437 zlc_active = 1;
1438 did_zlc_setup = 1;
1439 }
54a6eb5c 1440 }
9276b1bc
PJ
1441
1442 if (unlikely(NUMA_BUILD && page == NULL && zlc_active)) {
1443 /* Disable zlc cache for second zonelist scan */
1444 zlc_active = 0;
1445 goto zonelist_scan;
1446 }
7fb1d9fc 1447 return page;
753ee728
MH
1448}
1449
1da177e4
LT
1450/*
1451 * This is the 'heart' of the zoned buddy allocator.
1452 */
e4048e5d 1453struct page *
19770b32
MG
1454__alloc_pages_internal(gfp_t gfp_mask, unsigned int order,
1455 struct zonelist *zonelist, nodemask_t *nodemask)
1da177e4 1456{
260b2367 1457 const gfp_t wait = gfp_mask & __GFP_WAIT;
54a6eb5c 1458 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
dd1a239f
MG
1459 struct zoneref *z;
1460 struct zone *zone;
1da177e4
LT
1461 struct page *page;
1462 struct reclaim_state reclaim_state;
1463 struct task_struct *p = current;
1da177e4 1464 int do_retry;
7fb1d9fc 1465 int alloc_flags;
a41f24ea
NA
1466 unsigned long did_some_progress;
1467 unsigned long pages_reclaimed = 0;
1da177e4
LT
1468
1469 might_sleep_if(wait);
1470
933e312e
AM
1471 if (should_fail_alloc_page(gfp_mask, order))
1472 return NULL;
1473
6b1de916 1474restart:
dd1a239f 1475 z = zonelist->_zonerefs; /* the list of zones suitable for gfp_mask */
1da177e4 1476
dd1a239f 1477 if (unlikely(!z->zone)) {
523b9458
CL
1478 /*
1479 * Happens if we have an empty zonelist as a result of
1480 * GFP_THISNODE being used on a memoryless node
1481 */
1da177e4
LT
1482 return NULL;
1483 }
6b1de916 1484
19770b32 1485 page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, nodemask, order,
54a6eb5c 1486 zonelist, high_zoneidx, ALLOC_WMARK_LOW|ALLOC_CPUSET);
7fb1d9fc
RS
1487 if (page)
1488 goto got_pg;
1da177e4 1489
952f3b51
CL
1490 /*
1491 * GFP_THISNODE (meaning __GFP_THISNODE, __GFP_NORETRY and
1492 * __GFP_NOWARN set) should not cause reclaim since the subsystem
1493 * (f.e. slab) using GFP_THISNODE may choose to trigger reclaim
1494 * using a larger set of nodes after it has established that the
1495 * allowed per node queues are empty and that nodes are
1496 * over allocated.
1497 */
1498 if (NUMA_BUILD && (gfp_mask & GFP_THISNODE) == GFP_THISNODE)
1499 goto nopage;
1500
dd1a239f
MG
1501 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx)
1502 wakeup_kswapd(zone, order);
1da177e4 1503
9bf2229f 1504 /*
7fb1d9fc
RS
1505 * OK, we're below the kswapd watermark and have kicked background
1506 * reclaim. Now things get more complex, so set up alloc_flags according
1507 * to how we want to proceed.
1508 *
1509 * The caller may dip into page reserves a bit more if the caller
1510 * cannot run direct reclaim, or if the caller has realtime scheduling
4eac915d
PJ
1511 * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will
1512 * set both ALLOC_HARDER (!wait) and ALLOC_HIGH (__GFP_HIGH).
9bf2229f 1513 */
3148890b 1514 alloc_flags = ALLOC_WMARK_MIN;
7fb1d9fc
RS
1515 if ((unlikely(rt_task(p)) && !in_interrupt()) || !wait)
1516 alloc_flags |= ALLOC_HARDER;
1517 if (gfp_mask & __GFP_HIGH)
1518 alloc_flags |= ALLOC_HIGH;
bdd804f4
PJ
1519 if (wait)
1520 alloc_flags |= ALLOC_CPUSET;
1da177e4
LT
1521
1522 /*
1523 * Go through the zonelist again. Let __GFP_HIGH and allocations
7fb1d9fc 1524 * coming from realtime tasks go deeper into reserves.
1da177e4
LT
1525 *
1526 * This is the last chance, in general, before the goto nopage.
1527 * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
9bf2229f 1528 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
1da177e4 1529 */
19770b32 1530 page = get_page_from_freelist(gfp_mask, nodemask, order, zonelist,
54a6eb5c 1531 high_zoneidx, alloc_flags);
7fb1d9fc
RS
1532 if (page)
1533 goto got_pg;
1da177e4
LT
1534
1535 /* This allocation should allow future memory freeing. */
b84a35be 1536
b43a57bb 1537rebalance:
b84a35be
NP
1538 if (((p->flags & PF_MEMALLOC) || unlikely(test_thread_flag(TIF_MEMDIE)))
1539 && !in_interrupt()) {
1540 if (!(gfp_mask & __GFP_NOMEMALLOC)) {
885036d3 1541nofail_alloc:
b84a35be 1542 /* go through the zonelist yet again, ignoring mins */
19770b32 1543 page = get_page_from_freelist(gfp_mask, nodemask, order,
54a6eb5c 1544 zonelist, high_zoneidx, ALLOC_NO_WATERMARKS);
7fb1d9fc
RS
1545 if (page)
1546 goto got_pg;
885036d3 1547 if (gfp_mask & __GFP_NOFAIL) {
3fcfab16 1548 congestion_wait(WRITE, HZ/50);
885036d3
KK
1549 goto nofail_alloc;
1550 }
1da177e4
LT
1551 }
1552 goto nopage;
1553 }
1554
1555 /* Atomic allocations - we can't balance anything */
1556 if (!wait)
1557 goto nopage;
1558
1da177e4
LT
1559 cond_resched();
1560
1561 /* We now go into synchronous reclaim */
3e0d98b9 1562 cpuset_memory_pressure_bump();
e33c3b5e
DR
1563 /*
1564 * The task's cpuset might have expanded its set of allowable nodes
1565 */
1566 cpuset_update_task_memory_state();
1da177e4
LT
1567 p->flags |= PF_MEMALLOC;
1568 reclaim_state.reclaimed_slab = 0;
1569 p->reclaim_state = &reclaim_state;
1570
dac1d27b 1571 did_some_progress = try_to_free_pages(zonelist, order, gfp_mask);
1da177e4
LT
1572
1573 p->reclaim_state = NULL;
1574 p->flags &= ~PF_MEMALLOC;
1575
1576 cond_resched();
1577
e2c55dc8 1578 if (order != 0)
9f8f2172 1579 drain_all_pages();
e2c55dc8 1580
1da177e4 1581 if (likely(did_some_progress)) {
19770b32 1582 page = get_page_from_freelist(gfp_mask, nodemask, order,
54a6eb5c 1583 zonelist, high_zoneidx, alloc_flags);
7fb1d9fc
RS
1584 if (page)
1585 goto got_pg;
1da177e4 1586 } else if ((gfp_mask & __GFP_FS) && !(gfp_mask & __GFP_NORETRY)) {
dd1a239f 1587 if (!try_set_zone_oom(zonelist, gfp_mask)) {
ff0ceb9d
DR
1588 schedule_timeout_uninterruptible(1);
1589 goto restart;
1590 }
1591
1da177e4
LT
1592 /*
1593 * Go through the zonelist yet one more time, keep
1594 * very high watermark here, this is only to catch
1595 * a parallel oom killing, we must fail if we're still
1596 * under heavy pressure.
1597 */
19770b32
MG
1598 page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, nodemask,
1599 order, zonelist, high_zoneidx,
1600 ALLOC_WMARK_HIGH|ALLOC_CPUSET);
ff0ceb9d 1601 if (page) {
dd1a239f 1602 clear_zonelist_oom(zonelist, gfp_mask);
7fb1d9fc 1603 goto got_pg;
ff0ceb9d 1604 }
1da177e4 1605
a8bbf72a 1606 /* The OOM killer will not help higher order allocs so fail */
ff0ceb9d 1607 if (order > PAGE_ALLOC_COSTLY_ORDER) {
dd1a239f 1608 clear_zonelist_oom(zonelist, gfp_mask);
a8bbf72a 1609 goto nopage;
ff0ceb9d 1610 }
a8bbf72a 1611
9b0f8b04 1612 out_of_memory(zonelist, gfp_mask, order);
dd1a239f 1613 clear_zonelist_oom(zonelist, gfp_mask);
1da177e4
LT
1614 goto restart;
1615 }
1616
1617 /*
1618 * Don't let big-order allocations loop unless the caller explicitly
1619 * requests that. Wait for some write requests to complete then retry.
1620 *
a41f24ea
NA
1621 * In this implementation, order <= PAGE_ALLOC_COSTLY_ORDER
1622 * means __GFP_NOFAIL, but that may not be true in other
ab857d09 1623 * implementations.
a41f24ea
NA
1624 *
1625 * For order > PAGE_ALLOC_COSTLY_ORDER, if __GFP_REPEAT is
1626 * specified, then we retry until we no longer reclaim any pages
1627 * (above), or we've reclaimed an order of pages at least as
1628 * large as the allocation's order. In both cases, if the
1629 * allocation still fails, we stop retrying.
1da177e4 1630 */
a41f24ea 1631 pages_reclaimed += did_some_progress;
1da177e4
LT
1632 do_retry = 0;
1633 if (!(gfp_mask & __GFP_NORETRY)) {
a41f24ea 1634 if (order <= PAGE_ALLOC_COSTLY_ORDER) {
1da177e4 1635 do_retry = 1;
a41f24ea
NA
1636 } else {
1637 if (gfp_mask & __GFP_REPEAT &&
1638 pages_reclaimed < (1 << order))
1639 do_retry = 1;
1640 }
1da177e4
LT
1641 if (gfp_mask & __GFP_NOFAIL)
1642 do_retry = 1;
1643 }
1644 if (do_retry) {
3fcfab16 1645 congestion_wait(WRITE, HZ/50);
1da177e4
LT
1646 goto rebalance;
1647 }
1648
1649nopage:
1650 if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) {
1651 printk(KERN_WARNING "%s: page allocation failure."
1652 " order:%d, mode:0x%x\n",
1653 p->comm, order, gfp_mask);
1654 dump_stack();
578c2fd6 1655 show_mem();
1da177e4 1656 }
1da177e4 1657got_pg:
1da177e4
LT
1658 return page;
1659}
e4048e5d 1660EXPORT_SYMBOL(__alloc_pages_internal);
1da177e4
LT
1661
1662/*
1663 * Common helper functions.
1664 */
920c7a5d 1665unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
1da177e4
LT
1666{
1667 struct page * page;
1668 page = alloc_pages(gfp_mask, order);
1669 if (!page)
1670 return 0;
1671 return (unsigned long) page_address(page);
1672}
1673
1674EXPORT_SYMBOL(__get_free_pages);
1675
920c7a5d 1676unsigned long get_zeroed_page(gfp_t gfp_mask)
1da177e4
LT
1677{
1678 struct page * page;
1679
1680 /*
1681 * get_zeroed_page() returns a 32-bit address, which cannot represent
1682 * a highmem page
1683 */
725d704e 1684 VM_BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
1da177e4
LT
1685
1686 page = alloc_pages(gfp_mask | __GFP_ZERO, 0);
1687 if (page)
1688 return (unsigned long) page_address(page);
1689 return 0;
1690}
1691
1692EXPORT_SYMBOL(get_zeroed_page);
1693
1694void __pagevec_free(struct pagevec *pvec)
1695{
1696 int i = pagevec_count(pvec);
1697
1698 while (--i >= 0)
1699 free_hot_cold_page(pvec->pages[i], pvec->cold);
1700}
1701
920c7a5d 1702void __free_pages(struct page *page, unsigned int order)
1da177e4 1703{
b5810039 1704 if (put_page_testzero(page)) {
1da177e4
LT
1705 if (order == 0)
1706 free_hot_page(page);
1707 else
1708 __free_pages_ok(page, order);
1709 }
1710}
1711
1712EXPORT_SYMBOL(__free_pages);
1713
920c7a5d 1714void free_pages(unsigned long addr, unsigned int order)
1da177e4
LT
1715{
1716 if (addr != 0) {
725d704e 1717 VM_BUG_ON(!virt_addr_valid((void *)addr));
1da177e4
LT
1718 __free_pages(virt_to_page((void *)addr), order);
1719 }
1720}
1721
1722EXPORT_SYMBOL(free_pages);
1723
2be0ffe2
TT
1724/**
1725 * alloc_pages_exact - allocate an exact number physically-contiguous pages.
1726 * @size: the number of bytes to allocate
1727 * @gfp_mask: GFP flags for the allocation
1728 *
1729 * This function is similar to alloc_pages(), except that it allocates the
1730 * minimum number of pages to satisfy the request. alloc_pages() can only
1731 * allocate memory in power-of-two pages.
1732 *
1733 * This function is also limited by MAX_ORDER.
1734 *
1735 * Memory allocated by this function must be released by free_pages_exact().
1736 */
1737void *alloc_pages_exact(size_t size, gfp_t gfp_mask)
1738{
1739 unsigned int order = get_order(size);
1740 unsigned long addr;
1741
1742 addr = __get_free_pages(gfp_mask, order);
1743 if (addr) {
1744 unsigned long alloc_end = addr + (PAGE_SIZE << order);
1745 unsigned long used = addr + PAGE_ALIGN(size);
1746
1747 split_page(virt_to_page(addr), order);
1748 while (used < alloc_end) {
1749 free_page(used);
1750 used += PAGE_SIZE;
1751 }
1752 }
1753
1754 return (void *)addr;
1755}
1756EXPORT_SYMBOL(alloc_pages_exact);
1757
1758/**
1759 * free_pages_exact - release memory allocated via alloc_pages_exact()
1760 * @virt: the value returned by alloc_pages_exact.
1761 * @size: size of allocation, same value as passed to alloc_pages_exact().
1762 *
1763 * Release the memory allocated by a previous call to alloc_pages_exact.
1764 */
1765void free_pages_exact(void *virt, size_t size)
1766{
1767 unsigned long addr = (unsigned long)virt;
1768 unsigned long end = addr + PAGE_ALIGN(size);
1769
1770 while (addr < end) {
1771 free_page(addr);
1772 addr += PAGE_SIZE;
1773 }
1774}
1775EXPORT_SYMBOL(free_pages_exact);
1776
1da177e4
LT
1777static unsigned int nr_free_zone_pages(int offset)
1778{
dd1a239f 1779 struct zoneref *z;
54a6eb5c
MG
1780 struct zone *zone;
1781
e310fd43 1782 /* Just pick one node, since fallback list is circular */
1da177e4
LT
1783 unsigned int sum = 0;
1784
0e88460d 1785 struct zonelist *zonelist = node_zonelist(numa_node_id(), GFP_KERNEL);
1da177e4 1786
54a6eb5c 1787 for_each_zone_zonelist(zone, z, zonelist, offset) {
e310fd43
MB
1788 unsigned long size = zone->present_pages;
1789 unsigned long high = zone->pages_high;
1790 if (size > high)
1791 sum += size - high;
1da177e4
LT
1792 }
1793
1794 return sum;
1795}
1796
1797/*
1798 * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
1799 */
1800unsigned int nr_free_buffer_pages(void)
1801{
af4ca457 1802 return nr_free_zone_pages(gfp_zone(GFP_USER));
1da177e4 1803}
c2f1a551 1804EXPORT_SYMBOL_GPL(nr_free_buffer_pages);
1da177e4
LT
1805
1806/*
1807 * Amount of free RAM allocatable within all zones
1808 */
1809unsigned int nr_free_pagecache_pages(void)
1810{
2a1e274a 1811 return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER_MOVABLE));
1da177e4 1812}
08e0f6a9
CL
1813
1814static inline void show_node(struct zone *zone)
1da177e4 1815{
08e0f6a9 1816 if (NUMA_BUILD)
25ba77c1 1817 printk("Node %d ", zone_to_nid(zone));
1da177e4 1818}
1da177e4 1819
1da177e4
LT
1820void si_meminfo(struct sysinfo *val)
1821{
1822 val->totalram = totalram_pages;
1823 val->sharedram = 0;
d23ad423 1824 val->freeram = global_page_state(NR_FREE_PAGES);
1da177e4 1825 val->bufferram = nr_blockdev_pages();
1da177e4
LT
1826 val->totalhigh = totalhigh_pages;
1827 val->freehigh = nr_free_highpages();
1da177e4
LT
1828 val->mem_unit = PAGE_SIZE;
1829}
1830
1831EXPORT_SYMBOL(si_meminfo);
1832
1833#ifdef CONFIG_NUMA
1834void si_meminfo_node(struct sysinfo *val, int nid)
1835{
1836 pg_data_t *pgdat = NODE_DATA(nid);
1837
1838 val->totalram = pgdat->node_present_pages;
d23ad423 1839 val->freeram = node_page_state(nid, NR_FREE_PAGES);
98d2b0eb 1840#ifdef CONFIG_HIGHMEM
1da177e4 1841 val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].present_pages;
d23ad423
CL
1842 val->freehigh = zone_page_state(&pgdat->node_zones[ZONE_HIGHMEM],
1843 NR_FREE_PAGES);
98d2b0eb
CL
1844#else
1845 val->totalhigh = 0;
1846 val->freehigh = 0;
1847#endif
1da177e4
LT
1848 val->mem_unit = PAGE_SIZE;
1849}
1850#endif
1851
1852#define K(x) ((x) << (PAGE_SHIFT-10))
1853
1854/*
1855 * Show free area list (used inside shift_scroll-lock stuff)
1856 * We also calculate the percentage fragmentation. We do this by counting the
1857 * memory on each free list with the exception of the first item on the list.
1858 */
1859void show_free_areas(void)
1860{
c7241913 1861 int cpu;
1da177e4
LT
1862 struct zone *zone;
1863
1864 for_each_zone(zone) {
c7241913 1865 if (!populated_zone(zone))
1da177e4 1866 continue;
c7241913
JS
1867
1868 show_node(zone);
1869 printk("%s per-cpu:\n", zone->name);
1da177e4 1870
6b482c67 1871 for_each_online_cpu(cpu) {
1da177e4
LT
1872 struct per_cpu_pageset *pageset;
1873
e7c8d5c9 1874 pageset = zone_pcp(zone, cpu);
1da177e4 1875
3dfa5721
CL
1876 printk("CPU %4d: hi:%5d, btch:%4d usd:%4d\n",
1877 cpu, pageset->pcp.high,
1878 pageset->pcp.batch, pageset->pcp.count);
1da177e4
LT
1879 }
1880 }
1881
7b854121
LS
1882 printk("Active_anon:%lu active_file:%lu inactive_anon:%lu\n"
1883 " inactive_file:%lu"
1884//TODO: check/adjust line lengths
1885#ifdef CONFIG_UNEVICTABLE_LRU
1886 " unevictable:%lu"
1887#endif
1888 " dirty:%lu writeback:%lu unstable:%lu\n"
d23ad423 1889 " free:%lu slab:%lu mapped:%lu pagetables:%lu bounce:%lu\n",
4f98a2fe
RR
1890 global_page_state(NR_ACTIVE_ANON),
1891 global_page_state(NR_ACTIVE_FILE),
1892 global_page_state(NR_INACTIVE_ANON),
1893 global_page_state(NR_INACTIVE_FILE),
7b854121
LS
1894#ifdef CONFIG_UNEVICTABLE_LRU
1895 global_page_state(NR_UNEVICTABLE),
1896#endif
b1e7a8fd 1897 global_page_state(NR_FILE_DIRTY),
ce866b34 1898 global_page_state(NR_WRITEBACK),
fd39fc85 1899 global_page_state(NR_UNSTABLE_NFS),
d23ad423 1900 global_page_state(NR_FREE_PAGES),
972d1a7b
CL
1901 global_page_state(NR_SLAB_RECLAIMABLE) +
1902 global_page_state(NR_SLAB_UNRECLAIMABLE),
65ba55f5 1903 global_page_state(NR_FILE_MAPPED),
a25700a5
AM
1904 global_page_state(NR_PAGETABLE),
1905 global_page_state(NR_BOUNCE));
1da177e4
LT
1906
1907 for_each_zone(zone) {
1908 int i;
1909
c7241913
JS
1910 if (!populated_zone(zone))
1911 continue;
1912
1da177e4
LT
1913 show_node(zone);
1914 printk("%s"
1915 " free:%lukB"
1916 " min:%lukB"
1917 " low:%lukB"
1918 " high:%lukB"
4f98a2fe
RR
1919 " active_anon:%lukB"
1920 " inactive_anon:%lukB"
1921 " active_file:%lukB"
1922 " inactive_file:%lukB"
7b854121
LS
1923#ifdef CONFIG_UNEVICTABLE_LRU
1924 " unevictable:%lukB"
1925#endif
1da177e4
LT
1926 " present:%lukB"
1927 " pages_scanned:%lu"
1928 " all_unreclaimable? %s"
1929 "\n",
1930 zone->name,
d23ad423 1931 K(zone_page_state(zone, NR_FREE_PAGES)),
1da177e4
LT
1932 K(zone->pages_min),
1933 K(zone->pages_low),
1934 K(zone->pages_high),
4f98a2fe
RR
1935 K(zone_page_state(zone, NR_ACTIVE_ANON)),
1936 K(zone_page_state(zone, NR_INACTIVE_ANON)),
1937 K(zone_page_state(zone, NR_ACTIVE_FILE)),
1938 K(zone_page_state(zone, NR_INACTIVE_FILE)),
7b854121
LS
1939#ifdef CONFIG_UNEVICTABLE_LRU
1940 K(zone_page_state(zone, NR_UNEVICTABLE)),
1941#endif
1da177e4
LT
1942 K(zone->present_pages),
1943 zone->pages_scanned,
e815af95 1944 (zone_is_all_unreclaimable(zone) ? "yes" : "no")
1da177e4
LT
1945 );
1946 printk("lowmem_reserve[]:");
1947 for (i = 0; i < MAX_NR_ZONES; i++)
1948 printk(" %lu", zone->lowmem_reserve[i]);
1949 printk("\n");
1950 }
1951
1952 for_each_zone(zone) {
8f9de51a 1953 unsigned long nr[MAX_ORDER], flags, order, total = 0;
1da177e4 1954
c7241913
JS
1955 if (!populated_zone(zone))
1956 continue;
1957
1da177e4
LT
1958 show_node(zone);
1959 printk("%s: ", zone->name);
1da177e4
LT
1960
1961 spin_lock_irqsave(&zone->lock, flags);
1962 for (order = 0; order < MAX_ORDER; order++) {
8f9de51a
KK
1963 nr[order] = zone->free_area[order].nr_free;
1964 total += nr[order] << order;
1da177e4
LT
1965 }
1966 spin_unlock_irqrestore(&zone->lock, flags);
8f9de51a
KK
1967 for (order = 0; order < MAX_ORDER; order++)
1968 printk("%lu*%lukB ", nr[order], K(1UL) << order);
1da177e4
LT
1969 printk("= %lukB\n", K(total));
1970 }
1971
e6f3602d
LW
1972 printk("%ld total pagecache pages\n", global_page_state(NR_FILE_PAGES));
1973
1da177e4
LT
1974 show_swap_cache_info();
1975}
1976
19770b32
MG
1977static void zoneref_set_zone(struct zone *zone, struct zoneref *zoneref)
1978{
1979 zoneref->zone = zone;
1980 zoneref->zone_idx = zone_idx(zone);
1981}
1982
1da177e4
LT
1983/*
1984 * Builds allocation fallback zone lists.
1a93205b
CL
1985 *
1986 * Add all populated zones of a node to the zonelist.
1da177e4 1987 */
f0c0b2b8
KH
1988static int build_zonelists_node(pg_data_t *pgdat, struct zonelist *zonelist,
1989 int nr_zones, enum zone_type zone_type)
1da177e4 1990{
1a93205b
CL
1991 struct zone *zone;
1992
98d2b0eb 1993 BUG_ON(zone_type >= MAX_NR_ZONES);
2f6726e5 1994 zone_type++;
02a68a5e
CL
1995
1996 do {
2f6726e5 1997 zone_type--;
070f8032 1998 zone = pgdat->node_zones + zone_type;
1a93205b 1999 if (populated_zone(zone)) {
dd1a239f
MG
2000 zoneref_set_zone(zone,
2001 &zonelist->_zonerefs[nr_zones++]);
070f8032 2002 check_highest_zone(zone_type);
1da177e4 2003 }
02a68a5e 2004
2f6726e5 2005 } while (zone_type);
070f8032 2006 return nr_zones;
1da177e4
LT
2007}
2008
f0c0b2b8
KH
2009
2010/*
2011 * zonelist_order:
2012 * 0 = automatic detection of better ordering.
2013 * 1 = order by ([node] distance, -zonetype)
2014 * 2 = order by (-zonetype, [node] distance)
2015 *
2016 * If not NUMA, ZONELIST_ORDER_ZONE and ZONELIST_ORDER_NODE will create
2017 * the same zonelist. So only NUMA can configure this param.
2018 */
2019#define ZONELIST_ORDER_DEFAULT 0
2020#define ZONELIST_ORDER_NODE 1
2021#define ZONELIST_ORDER_ZONE 2
2022
2023/* zonelist order in the kernel.
2024 * set_zonelist_order() will set this to NODE or ZONE.
2025 */
2026static int current_zonelist_order = ZONELIST_ORDER_DEFAULT;
2027static char zonelist_order_name[3][8] = {"Default", "Node", "Zone"};
2028
2029
1da177e4 2030#ifdef CONFIG_NUMA
f0c0b2b8
KH
2031/* The value user specified ....changed by config */
2032static int user_zonelist_order = ZONELIST_ORDER_DEFAULT;
2033/* string for sysctl */
2034#define NUMA_ZONELIST_ORDER_LEN 16
2035char numa_zonelist_order[16] = "default";
2036
2037/*
2038 * interface for configure zonelist ordering.
2039 * command line option "numa_zonelist_order"
2040 * = "[dD]efault - default, automatic configuration.
2041 * = "[nN]ode - order by node locality, then by zone within node
2042 * = "[zZ]one - order by zone, then by locality within zone
2043 */
2044
2045static int __parse_numa_zonelist_order(char *s)
2046{
2047 if (*s == 'd' || *s == 'D') {
2048 user_zonelist_order = ZONELIST_ORDER_DEFAULT;
2049 } else if (*s == 'n' || *s == 'N') {
2050 user_zonelist_order = ZONELIST_ORDER_NODE;
2051 } else if (*s == 'z' || *s == 'Z') {
2052 user_zonelist_order = ZONELIST_ORDER_ZONE;
2053 } else {
2054 printk(KERN_WARNING
2055 "Ignoring invalid numa_zonelist_order value: "
2056 "%s\n", s);
2057 return -EINVAL;
2058 }
2059 return 0;
2060}
2061
2062static __init int setup_numa_zonelist_order(char *s)
2063{
2064 if (s)
2065 return __parse_numa_zonelist_order(s);
2066 return 0;
2067}
2068early_param("numa_zonelist_order", setup_numa_zonelist_order);
2069
2070/*
2071 * sysctl handler for numa_zonelist_order
2072 */
2073int numa_zonelist_order_handler(ctl_table *table, int write,
2074 struct file *file, void __user *buffer, size_t *length,
2075 loff_t *ppos)
2076{
2077 char saved_string[NUMA_ZONELIST_ORDER_LEN];
2078 int ret;
2079
2080 if (write)
2081 strncpy(saved_string, (char*)table->data,
2082 NUMA_ZONELIST_ORDER_LEN);
2083 ret = proc_dostring(table, write, file, buffer, length, ppos);
2084 if (ret)
2085 return ret;
2086 if (write) {
2087 int oldval = user_zonelist_order;
2088 if (__parse_numa_zonelist_order((char*)table->data)) {
2089 /*
2090 * bogus value. restore saved string
2091 */
2092 strncpy((char*)table->data, saved_string,
2093 NUMA_ZONELIST_ORDER_LEN);
2094 user_zonelist_order = oldval;
2095 } else if (oldval != user_zonelist_order)
2096 build_all_zonelists();
2097 }
2098 return 0;
2099}
2100
2101
1da177e4 2102#define MAX_NODE_LOAD (num_online_nodes())
f0c0b2b8
KH
2103static int node_load[MAX_NUMNODES];
2104
1da177e4 2105/**
4dc3b16b 2106 * find_next_best_node - find the next node that should appear in a given node's fallback list
1da177e4
LT
2107 * @node: node whose fallback list we're appending
2108 * @used_node_mask: nodemask_t of already used nodes
2109 *
2110 * We use a number of factors to determine which is the next node that should
2111 * appear on a given node's fallback list. The node should not have appeared
2112 * already in @node's fallback list, and it should be the next closest node
2113 * according to the distance array (which contains arbitrary distance values
2114 * from each node to each node in the system), and should also prefer nodes
2115 * with no CPUs, since presumably they'll have very little allocation pressure
2116 * on them otherwise.
2117 * It returns -1 if no node is found.
2118 */
f0c0b2b8 2119static int find_next_best_node(int node, nodemask_t *used_node_mask)
1da177e4 2120{
4cf808eb 2121 int n, val;
1da177e4
LT
2122 int min_val = INT_MAX;
2123 int best_node = -1;
c5f59f08 2124 node_to_cpumask_ptr(tmp, 0);
1da177e4 2125
4cf808eb
LT
2126 /* Use the local node if we haven't already */
2127 if (!node_isset(node, *used_node_mask)) {
2128 node_set(node, *used_node_mask);
2129 return node;
2130 }
1da177e4 2131
37b07e41 2132 for_each_node_state(n, N_HIGH_MEMORY) {
1da177e4
LT
2133
2134 /* Don't want a node to appear more than once */
2135 if (node_isset(n, *used_node_mask))
2136 continue;
2137
1da177e4
LT
2138 /* Use the distance array to find the distance */
2139 val = node_distance(node, n);
2140
4cf808eb
LT
2141 /* Penalize nodes under us ("prefer the next node") */
2142 val += (n < node);
2143
1da177e4 2144 /* Give preference to headless and unused nodes */
c5f59f08
MT
2145 node_to_cpumask_ptr_next(tmp, n);
2146 if (!cpus_empty(*tmp))
1da177e4
LT
2147 val += PENALTY_FOR_NODE_WITH_CPUS;
2148
2149 /* Slight preference for less loaded node */
2150 val *= (MAX_NODE_LOAD*MAX_NUMNODES);
2151 val += node_load[n];
2152
2153 if (val < min_val) {
2154 min_val = val;
2155 best_node = n;
2156 }
2157 }
2158
2159 if (best_node >= 0)
2160 node_set(best_node, *used_node_mask);
2161
2162 return best_node;
2163}
2164
f0c0b2b8
KH
2165
2166/*
2167 * Build zonelists ordered by node and zones within node.
2168 * This results in maximum locality--normal zone overflows into local
2169 * DMA zone, if any--but risks exhausting DMA zone.
2170 */
2171static void build_zonelists_in_node_order(pg_data_t *pgdat, int node)
1da177e4 2172{
f0c0b2b8 2173 int j;
1da177e4 2174 struct zonelist *zonelist;
f0c0b2b8 2175
54a6eb5c 2176 zonelist = &pgdat->node_zonelists[0];
dd1a239f 2177 for (j = 0; zonelist->_zonerefs[j].zone != NULL; j++)
54a6eb5c
MG
2178 ;
2179 j = build_zonelists_node(NODE_DATA(node), zonelist, j,
2180 MAX_NR_ZONES - 1);
dd1a239f
MG
2181 zonelist->_zonerefs[j].zone = NULL;
2182 zonelist->_zonerefs[j].zone_idx = 0;
f0c0b2b8
KH
2183}
2184
523b9458
CL
2185/*
2186 * Build gfp_thisnode zonelists
2187 */
2188static void build_thisnode_zonelists(pg_data_t *pgdat)
2189{
523b9458
CL
2190 int j;
2191 struct zonelist *zonelist;
2192
54a6eb5c
MG
2193 zonelist = &pgdat->node_zonelists[1];
2194 j = build_zonelists_node(pgdat, zonelist, 0, MAX_NR_ZONES - 1);
dd1a239f
MG
2195 zonelist->_zonerefs[j].zone = NULL;
2196 zonelist->_zonerefs[j].zone_idx = 0;
523b9458
CL
2197}
2198
f0c0b2b8
KH
2199/*
2200 * Build zonelists ordered by zone and nodes within zones.
2201 * This results in conserving DMA zone[s] until all Normal memory is
2202 * exhausted, but results in overflowing to remote node while memory
2203 * may still exist in local DMA zone.
2204 */
2205static int node_order[MAX_NUMNODES];
2206
2207static void build_zonelists_in_zone_order(pg_data_t *pgdat, int nr_nodes)
2208{
f0c0b2b8
KH
2209 int pos, j, node;
2210 int zone_type; /* needs to be signed */
2211 struct zone *z;
2212 struct zonelist *zonelist;
2213
54a6eb5c
MG
2214 zonelist = &pgdat->node_zonelists[0];
2215 pos = 0;
2216 for (zone_type = MAX_NR_ZONES - 1; zone_type >= 0; zone_type--) {
2217 for (j = 0; j < nr_nodes; j++) {
2218 node = node_order[j];
2219 z = &NODE_DATA(node)->node_zones[zone_type];
2220 if (populated_zone(z)) {
dd1a239f
MG
2221 zoneref_set_zone(z,
2222 &zonelist->_zonerefs[pos++]);
54a6eb5c 2223 check_highest_zone(zone_type);
f0c0b2b8
KH
2224 }
2225 }
f0c0b2b8 2226 }
dd1a239f
MG
2227 zonelist->_zonerefs[pos].zone = NULL;
2228 zonelist->_zonerefs[pos].zone_idx = 0;
f0c0b2b8
KH
2229}
2230
2231static int default_zonelist_order(void)
2232{
2233 int nid, zone_type;
2234 unsigned long low_kmem_size,total_size;
2235 struct zone *z;
2236 int average_size;
2237 /*
2238 * ZONE_DMA and ZONE_DMA32 can be very small area in the sytem.
2239 * If they are really small and used heavily, the system can fall
2240 * into OOM very easily.
2241 * This function detect ZONE_DMA/DMA32 size and confgigures zone order.
2242 */
2243 /* Is there ZONE_NORMAL ? (ex. ppc has only DMA zone..) */
2244 low_kmem_size = 0;
2245 total_size = 0;
2246 for_each_online_node(nid) {
2247 for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
2248 z = &NODE_DATA(nid)->node_zones[zone_type];
2249 if (populated_zone(z)) {
2250 if (zone_type < ZONE_NORMAL)
2251 low_kmem_size += z->present_pages;
2252 total_size += z->present_pages;
2253 }
2254 }
2255 }
2256 if (!low_kmem_size || /* there are no DMA area. */
2257 low_kmem_size > total_size/2) /* DMA/DMA32 is big. */
2258 return ZONELIST_ORDER_NODE;
2259 /*
2260 * look into each node's config.
2261 * If there is a node whose DMA/DMA32 memory is very big area on
2262 * local memory, NODE_ORDER may be suitable.
2263 */
37b07e41
LS
2264 average_size = total_size /
2265 (nodes_weight(node_states[N_HIGH_MEMORY]) + 1);
f0c0b2b8
KH
2266 for_each_online_node(nid) {
2267 low_kmem_size = 0;
2268 total_size = 0;
2269 for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
2270 z = &NODE_DATA(nid)->node_zones[zone_type];
2271 if (populated_zone(z)) {
2272 if (zone_type < ZONE_NORMAL)
2273 low_kmem_size += z->present_pages;
2274 total_size += z->present_pages;
2275 }
2276 }
2277 if (low_kmem_size &&
2278 total_size > average_size && /* ignore small node */
2279 low_kmem_size > total_size * 70/100)
2280 return ZONELIST_ORDER_NODE;
2281 }
2282 return ZONELIST_ORDER_ZONE;
2283}
2284
2285static void set_zonelist_order(void)
2286{
2287 if (user_zonelist_order == ZONELIST_ORDER_DEFAULT)
2288 current_zonelist_order = default_zonelist_order();
2289 else
2290 current_zonelist_order = user_zonelist_order;
2291}
2292
2293static void build_zonelists(pg_data_t *pgdat)
2294{
2295 int j, node, load;
2296 enum zone_type i;
1da177e4 2297 nodemask_t used_mask;
f0c0b2b8
KH
2298 int local_node, prev_node;
2299 struct zonelist *zonelist;
2300 int order = current_zonelist_order;
1da177e4
LT
2301
2302 /* initialize zonelists */
523b9458 2303 for (i = 0; i < MAX_ZONELISTS; i++) {
1da177e4 2304 zonelist = pgdat->node_zonelists + i;
dd1a239f
MG
2305 zonelist->_zonerefs[0].zone = NULL;
2306 zonelist->_zonerefs[0].zone_idx = 0;
1da177e4
LT
2307 }
2308
2309 /* NUMA-aware ordering of nodes */
2310 local_node = pgdat->node_id;
2311 load = num_online_nodes();
2312 prev_node = local_node;
2313 nodes_clear(used_mask);
f0c0b2b8
KH
2314
2315 memset(node_load, 0, sizeof(node_load));
2316 memset(node_order, 0, sizeof(node_order));
2317 j = 0;
2318
1da177e4 2319 while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
9eeff239
CL
2320 int distance = node_distance(local_node, node);
2321
2322 /*
2323 * If another node is sufficiently far away then it is better
2324 * to reclaim pages in a zone before going off node.
2325 */
2326 if (distance > RECLAIM_DISTANCE)
2327 zone_reclaim_mode = 1;
2328
1da177e4
LT
2329 /*
2330 * We don't want to pressure a particular node.
2331 * So adding penalty to the first node in same
2332 * distance group to make it round-robin.
2333 */
9eeff239 2334 if (distance != node_distance(local_node, prev_node))
f0c0b2b8
KH
2335 node_load[node] = load;
2336
1da177e4
LT
2337 prev_node = node;
2338 load--;
f0c0b2b8
KH
2339 if (order == ZONELIST_ORDER_NODE)
2340 build_zonelists_in_node_order(pgdat, node);
2341 else
2342 node_order[j++] = node; /* remember order */
2343 }
1da177e4 2344
f0c0b2b8
KH
2345 if (order == ZONELIST_ORDER_ZONE) {
2346 /* calculate node order -- i.e., DMA last! */
2347 build_zonelists_in_zone_order(pgdat, j);
1da177e4 2348 }
523b9458
CL
2349
2350 build_thisnode_zonelists(pgdat);
1da177e4
LT
2351}
2352
9276b1bc 2353/* Construct the zonelist performance cache - see further mmzone.h */
f0c0b2b8 2354static void build_zonelist_cache(pg_data_t *pgdat)
9276b1bc 2355{
54a6eb5c
MG
2356 struct zonelist *zonelist;
2357 struct zonelist_cache *zlc;
dd1a239f 2358 struct zoneref *z;
9276b1bc 2359
54a6eb5c
MG
2360 zonelist = &pgdat->node_zonelists[0];
2361 zonelist->zlcache_ptr = zlc = &zonelist->zlcache;
2362 bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
dd1a239f
MG
2363 for (z = zonelist->_zonerefs; z->zone; z++)
2364 zlc->z_to_n[z - zonelist->_zonerefs] = zonelist_node_idx(z);
9276b1bc
PJ
2365}
2366
f0c0b2b8 2367
1da177e4
LT
2368#else /* CONFIG_NUMA */
2369
f0c0b2b8
KH
2370static void set_zonelist_order(void)
2371{
2372 current_zonelist_order = ZONELIST_ORDER_ZONE;
2373}
2374
2375static void build_zonelists(pg_data_t *pgdat)
1da177e4 2376{
19655d34 2377 int node, local_node;
54a6eb5c
MG
2378 enum zone_type j;
2379 struct zonelist *zonelist;
1da177e4
LT
2380
2381 local_node = pgdat->node_id;
1da177e4 2382
54a6eb5c
MG
2383 zonelist = &pgdat->node_zonelists[0];
2384 j = build_zonelists_node(pgdat, zonelist, 0, MAX_NR_ZONES - 1);
1da177e4 2385
54a6eb5c
MG
2386 /*
2387 * Now we build the zonelist so that it contains the zones
2388 * of all the other nodes.
2389 * We don't want to pressure a particular node, so when
2390 * building the zones for node N, we make sure that the
2391 * zones coming right after the local ones are those from
2392 * node N+1 (modulo N)
2393 */
2394 for (node = local_node + 1; node < MAX_NUMNODES; node++) {
2395 if (!node_online(node))
2396 continue;
2397 j = build_zonelists_node(NODE_DATA(node), zonelist, j,
2398 MAX_NR_ZONES - 1);
1da177e4 2399 }
54a6eb5c
MG
2400 for (node = 0; node < local_node; node++) {
2401 if (!node_online(node))
2402 continue;
2403 j = build_zonelists_node(NODE_DATA(node), zonelist, j,
2404 MAX_NR_ZONES - 1);
2405 }
2406
dd1a239f
MG
2407 zonelist->_zonerefs[j].zone = NULL;
2408 zonelist->_zonerefs[j].zone_idx = 0;
1da177e4
LT
2409}
2410
9276b1bc 2411/* non-NUMA variant of zonelist performance cache - just NULL zlcache_ptr */
f0c0b2b8 2412static void build_zonelist_cache(pg_data_t *pgdat)
9276b1bc 2413{
54a6eb5c 2414 pgdat->node_zonelists[0].zlcache_ptr = NULL;
9276b1bc
PJ
2415}
2416
1da177e4
LT
2417#endif /* CONFIG_NUMA */
2418
9b1a4d38 2419/* return values int ....just for stop_machine() */
f0c0b2b8 2420static int __build_all_zonelists(void *dummy)
1da177e4 2421{
6811378e 2422 int nid;
9276b1bc
PJ
2423
2424 for_each_online_node(nid) {
7ea1530a
CL
2425 pg_data_t *pgdat = NODE_DATA(nid);
2426
2427 build_zonelists(pgdat);
2428 build_zonelist_cache(pgdat);
9276b1bc 2429 }
6811378e
YG
2430 return 0;
2431}
2432
f0c0b2b8 2433void build_all_zonelists(void)
6811378e 2434{
f0c0b2b8
KH
2435 set_zonelist_order();
2436
6811378e 2437 if (system_state == SYSTEM_BOOTING) {
423b41d7 2438 __build_all_zonelists(NULL);
68ad8df4 2439 mminit_verify_zonelist();
6811378e
YG
2440 cpuset_init_current_mems_allowed();
2441 } else {
183ff22b 2442 /* we have to stop all cpus to guarantee there is no user
6811378e 2443 of zonelist */
9b1a4d38 2444 stop_machine(__build_all_zonelists, NULL, NULL);
6811378e
YG
2445 /* cpuset refresh routine should be here */
2446 }
bd1e22b8 2447 vm_total_pages = nr_free_pagecache_pages();
9ef9acb0
MG
2448 /*
2449 * Disable grouping by mobility if the number of pages in the
2450 * system is too low to allow the mechanism to work. It would be
2451 * more accurate, but expensive to check per-zone. This check is
2452 * made on memory-hotadd so a system can start with mobility
2453 * disabled and enable it later
2454 */
d9c23400 2455 if (vm_total_pages < (pageblock_nr_pages * MIGRATE_TYPES))
9ef9acb0
MG
2456 page_group_by_mobility_disabled = 1;
2457 else
2458 page_group_by_mobility_disabled = 0;
2459
2460 printk("Built %i zonelists in %s order, mobility grouping %s. "
2461 "Total pages: %ld\n",
f0c0b2b8
KH
2462 num_online_nodes(),
2463 zonelist_order_name[current_zonelist_order],
9ef9acb0 2464 page_group_by_mobility_disabled ? "off" : "on",
f0c0b2b8
KH
2465 vm_total_pages);
2466#ifdef CONFIG_NUMA
2467 printk("Policy zone: %s\n", zone_names[policy_zone]);
2468#endif
1da177e4
LT
2469}
2470
2471/*
2472 * Helper functions to size the waitqueue hash table.
2473 * Essentially these want to choose hash table sizes sufficiently
2474 * large so that collisions trying to wait on pages are rare.
2475 * But in fact, the number of active page waitqueues on typical
2476 * systems is ridiculously low, less than 200. So this is even
2477 * conservative, even though it seems large.
2478 *
2479 * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
2480 * waitqueues, i.e. the size of the waitq table given the number of pages.
2481 */
2482#define PAGES_PER_WAITQUEUE 256
2483
cca448fe 2484#ifndef CONFIG_MEMORY_HOTPLUG
02b694de 2485static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
1da177e4
LT
2486{
2487 unsigned long size = 1;
2488
2489 pages /= PAGES_PER_WAITQUEUE;
2490
2491 while (size < pages)
2492 size <<= 1;
2493
2494 /*
2495 * Once we have dozens or even hundreds of threads sleeping
2496 * on IO we've got bigger problems than wait queue collision.
2497 * Limit the size of the wait table to a reasonable size.
2498 */
2499 size = min(size, 4096UL);
2500
2501 return max(size, 4UL);
2502}
cca448fe
YG
2503#else
2504/*
2505 * A zone's size might be changed by hot-add, so it is not possible to determine
2506 * a suitable size for its wait_table. So we use the maximum size now.
2507 *
2508 * The max wait table size = 4096 x sizeof(wait_queue_head_t). ie:
2509 *
2510 * i386 (preemption config) : 4096 x 16 = 64Kbyte.
2511 * ia64, x86-64 (no preemption): 4096 x 20 = 80Kbyte.
2512 * ia64, x86-64 (preemption) : 4096 x 24 = 96Kbyte.
2513 *
2514 * The maximum entries are prepared when a zone's memory is (512K + 256) pages
2515 * or more by the traditional way. (See above). It equals:
2516 *
2517 * i386, x86-64, powerpc(4K page size) : = ( 2G + 1M)byte.
2518 * ia64(16K page size) : = ( 8G + 4M)byte.
2519 * powerpc (64K page size) : = (32G +16M)byte.
2520 */
2521static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
2522{
2523 return 4096UL;
2524}
2525#endif
1da177e4
LT
2526
2527/*
2528 * This is an integer logarithm so that shifts can be used later
2529 * to extract the more random high bits from the multiplicative
2530 * hash function before the remainder is taken.
2531 */
2532static inline unsigned long wait_table_bits(unsigned long size)
2533{
2534 return ffz(~size);
2535}
2536
2537#define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
2538
56fd56b8 2539/*
d9c23400 2540 * Mark a number of pageblocks as MIGRATE_RESERVE. The number
56fd56b8
MG
2541 * of blocks reserved is based on zone->pages_min. The memory within the
2542 * reserve will tend to store contiguous free pages. Setting min_free_kbytes
2543 * higher will lead to a bigger reserve which will get freed as contiguous
2544 * blocks as reclaim kicks in
2545 */
2546static void setup_zone_migrate_reserve(struct zone *zone)
2547{
2548 unsigned long start_pfn, pfn, end_pfn;
2549 struct page *page;
2550 unsigned long reserve, block_migratetype;
2551
2552 /* Get the start pfn, end pfn and the number of blocks to reserve */
2553 start_pfn = zone->zone_start_pfn;
2554 end_pfn = start_pfn + zone->spanned_pages;
d9c23400
MG
2555 reserve = roundup(zone->pages_min, pageblock_nr_pages) >>
2556 pageblock_order;
56fd56b8 2557
d9c23400 2558 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
56fd56b8
MG
2559 if (!pfn_valid(pfn))
2560 continue;
2561 page = pfn_to_page(pfn);
2562
344c790e
AL
2563 /* Watch out for overlapping nodes */
2564 if (page_to_nid(page) != zone_to_nid(zone))
2565 continue;
2566
56fd56b8
MG
2567 /* Blocks with reserved pages will never free, skip them. */
2568 if (PageReserved(page))
2569 continue;
2570
2571 block_migratetype = get_pageblock_migratetype(page);
2572
2573 /* If this block is reserved, account for it */
2574 if (reserve > 0 && block_migratetype == MIGRATE_RESERVE) {
2575 reserve--;
2576 continue;
2577 }
2578
2579 /* Suitable for reserving if this block is movable */
2580 if (reserve > 0 && block_migratetype == MIGRATE_MOVABLE) {
2581 set_pageblock_migratetype(page, MIGRATE_RESERVE);
2582 move_freepages_block(zone, page, MIGRATE_RESERVE);
2583 reserve--;
2584 continue;
2585 }
2586
2587 /*
2588 * If the reserve is met and this is a previous reserved block,
2589 * take it back
2590 */
2591 if (block_migratetype == MIGRATE_RESERVE) {
2592 set_pageblock_migratetype(page, MIGRATE_MOVABLE);
2593 move_freepages_block(zone, page, MIGRATE_MOVABLE);
2594 }
2595 }
2596}
ac0e5b7a 2597
1da177e4
LT
2598/*
2599 * Initially all pages are reserved - free ones are freed
2600 * up by free_all_bootmem() once the early boot process is
2601 * done. Non-atomic initialization, single-pass.
2602 */
c09b4240 2603void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
a2f3aa02 2604 unsigned long start_pfn, enum memmap_context context)
1da177e4 2605{
1da177e4 2606 struct page *page;
29751f69
AW
2607 unsigned long end_pfn = start_pfn + size;
2608 unsigned long pfn;
86051ca5 2609 struct zone *z;
1da177e4 2610
86051ca5 2611 z = &NODE_DATA(nid)->node_zones[zone];
cbe8dd4a 2612 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
a2f3aa02
DH
2613 /*
2614 * There can be holes in boot-time mem_map[]s
2615 * handed to this function. They do not
2616 * exist on hotplugged memory.
2617 */
2618 if (context == MEMMAP_EARLY) {
2619 if (!early_pfn_valid(pfn))
2620 continue;
2621 if (!early_pfn_in_nid(pfn, nid))
2622 continue;
2623 }
d41dee36
AW
2624 page = pfn_to_page(pfn);
2625 set_page_links(page, zone, nid, pfn);
708614e6 2626 mminit_verify_page_links(page, zone, nid, pfn);
7835e98b 2627 init_page_count(page);
1da177e4
LT
2628 reset_page_mapcount(page);
2629 SetPageReserved(page);
b2a0ac88
MG
2630 /*
2631 * Mark the block movable so that blocks are reserved for
2632 * movable at startup. This will force kernel allocations
2633 * to reserve their blocks rather than leaking throughout
2634 * the address space during boot when many long-lived
56fd56b8
MG
2635 * kernel allocations are made. Later some blocks near
2636 * the start are marked MIGRATE_RESERVE by
2637 * setup_zone_migrate_reserve()
86051ca5
KH
2638 *
2639 * bitmap is created for zone's valid pfn range. but memmap
2640 * can be created for invalid pages (for alignment)
2641 * check here not to call set_pageblock_migratetype() against
2642 * pfn out of zone.
b2a0ac88 2643 */
86051ca5
KH
2644 if ((z->zone_start_pfn <= pfn)
2645 && (pfn < z->zone_start_pfn + z->spanned_pages)
2646 && !(pfn & (pageblock_nr_pages - 1)))
56fd56b8 2647 set_pageblock_migratetype(page, MIGRATE_MOVABLE);
b2a0ac88 2648
1da177e4
LT
2649 INIT_LIST_HEAD(&page->lru);
2650#ifdef WANT_PAGE_VIRTUAL
2651 /* The shift won't overflow because ZONE_NORMAL is below 4G. */
2652 if (!is_highmem_idx(zone))
3212c6be 2653 set_page_address(page, __va(pfn << PAGE_SHIFT));
1da177e4 2654#endif
1da177e4
LT
2655 }
2656}
2657
1e548deb 2658static void __meminit zone_init_free_lists(struct zone *zone)
1da177e4 2659{
b2a0ac88
MG
2660 int order, t;
2661 for_each_migratetype_order(order, t) {
2662 INIT_LIST_HEAD(&zone->free_area[order].free_list[t]);
1da177e4
LT
2663 zone->free_area[order].nr_free = 0;
2664 }
2665}
2666
2667#ifndef __HAVE_ARCH_MEMMAP_INIT
2668#define memmap_init(size, nid, zone, start_pfn) \
a2f3aa02 2669 memmap_init_zone((size), (nid), (zone), (start_pfn), MEMMAP_EARLY)
1da177e4
LT
2670#endif
2671
1d6f4e60 2672static int zone_batchsize(struct zone *zone)
e7c8d5c9
CL
2673{
2674 int batch;
2675
2676 /*
2677 * The per-cpu-pages pools are set to around 1000th of the
ba56e91c 2678 * size of the zone. But no more than 1/2 of a meg.
e7c8d5c9
CL
2679 *
2680 * OK, so we don't know how big the cache is. So guess.
2681 */
2682 batch = zone->present_pages / 1024;
ba56e91c
SR
2683 if (batch * PAGE_SIZE > 512 * 1024)
2684 batch = (512 * 1024) / PAGE_SIZE;
e7c8d5c9
CL
2685 batch /= 4; /* We effectively *= 4 below */
2686 if (batch < 1)
2687 batch = 1;
2688
2689 /*
0ceaacc9
NP
2690 * Clamp the batch to a 2^n - 1 value. Having a power
2691 * of 2 value was found to be more likely to have
2692 * suboptimal cache aliasing properties in some cases.
e7c8d5c9 2693 *
0ceaacc9
NP
2694 * For example if 2 tasks are alternately allocating
2695 * batches of pages, one task can end up with a lot
2696 * of pages of one half of the possible page colors
2697 * and the other with pages of the other colors.
e7c8d5c9 2698 */
0ceaacc9 2699 batch = (1 << (fls(batch + batch/2)-1)) - 1;
ba56e91c 2700
e7c8d5c9
CL
2701 return batch;
2702}
2703
b69a7288 2704static void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
2caaad41
CL
2705{
2706 struct per_cpu_pages *pcp;
2707
1c6fe946
MD
2708 memset(p, 0, sizeof(*p));
2709
3dfa5721 2710 pcp = &p->pcp;
2caaad41 2711 pcp->count = 0;
2caaad41
CL
2712 pcp->high = 6 * batch;
2713 pcp->batch = max(1UL, 1 * batch);
2714 INIT_LIST_HEAD(&pcp->list);
2caaad41
CL
2715}
2716
8ad4b1fb
RS
2717/*
2718 * setup_pagelist_highmark() sets the high water mark for hot per_cpu_pagelist
2719 * to the value high for the pageset p.
2720 */
2721
2722static void setup_pagelist_highmark(struct per_cpu_pageset *p,
2723 unsigned long high)
2724{
2725 struct per_cpu_pages *pcp;
2726
3dfa5721 2727 pcp = &p->pcp;
8ad4b1fb
RS
2728 pcp->high = high;
2729 pcp->batch = max(1UL, high/4);
2730 if ((high/4) > (PAGE_SHIFT * 8))
2731 pcp->batch = PAGE_SHIFT * 8;
2732}
2733
2734
e7c8d5c9
CL
2735#ifdef CONFIG_NUMA
2736/*
2caaad41
CL
2737 * Boot pageset table. One per cpu which is going to be used for all
2738 * zones and all nodes. The parameters will be set in such a way
2739 * that an item put on a list will immediately be handed over to
2740 * the buddy list. This is safe since pageset manipulation is done
2741 * with interrupts disabled.
2742 *
2743 * Some NUMA counter updates may also be caught by the boot pagesets.
b7c84c6a
CL
2744 *
2745 * The boot_pagesets must be kept even after bootup is complete for
2746 * unused processors and/or zones. They do play a role for bootstrapping
2747 * hotplugged processors.
2748 *
2749 * zoneinfo_show() and maybe other functions do
2750 * not check if the processor is online before following the pageset pointer.
2751 * Other parts of the kernel may not check if the zone is available.
2caaad41 2752 */
88a2a4ac 2753static struct per_cpu_pageset boot_pageset[NR_CPUS];
2caaad41
CL
2754
2755/*
2756 * Dynamically allocate memory for the
e7c8d5c9
CL
2757 * per cpu pageset array in struct zone.
2758 */
6292d9aa 2759static int __cpuinit process_zones(int cpu)
e7c8d5c9
CL
2760{
2761 struct zone *zone, *dzone;
37c0708d
CL
2762 int node = cpu_to_node(cpu);
2763
2764 node_set_state(node, N_CPU); /* this node has a cpu */
e7c8d5c9
CL
2765
2766 for_each_zone(zone) {
e7c8d5c9 2767
66a55030
CL
2768 if (!populated_zone(zone))
2769 continue;
2770
23316bc8 2771 zone_pcp(zone, cpu) = kmalloc_node(sizeof(struct per_cpu_pageset),
37c0708d 2772 GFP_KERNEL, node);
23316bc8 2773 if (!zone_pcp(zone, cpu))
e7c8d5c9 2774 goto bad;
e7c8d5c9 2775
23316bc8 2776 setup_pageset(zone_pcp(zone, cpu), zone_batchsize(zone));
8ad4b1fb
RS
2777
2778 if (percpu_pagelist_fraction)
2779 setup_pagelist_highmark(zone_pcp(zone, cpu),
2780 (zone->present_pages / percpu_pagelist_fraction));
e7c8d5c9
CL
2781 }
2782
2783 return 0;
2784bad:
2785 for_each_zone(dzone) {
64191688
AM
2786 if (!populated_zone(dzone))
2787 continue;
e7c8d5c9
CL
2788 if (dzone == zone)
2789 break;
23316bc8
NP
2790 kfree(zone_pcp(dzone, cpu));
2791 zone_pcp(dzone, cpu) = NULL;
e7c8d5c9
CL
2792 }
2793 return -ENOMEM;
2794}
2795
2796static inline void free_zone_pagesets(int cpu)
2797{
e7c8d5c9
CL
2798 struct zone *zone;
2799
2800 for_each_zone(zone) {
2801 struct per_cpu_pageset *pset = zone_pcp(zone, cpu);
2802
f3ef9ead
DR
2803 /* Free per_cpu_pageset if it is slab allocated */
2804 if (pset != &boot_pageset[cpu])
2805 kfree(pset);
e7c8d5c9 2806 zone_pcp(zone, cpu) = NULL;
e7c8d5c9 2807 }
e7c8d5c9
CL
2808}
2809
9c7b216d 2810static int __cpuinit pageset_cpuup_callback(struct notifier_block *nfb,
e7c8d5c9
CL
2811 unsigned long action,
2812 void *hcpu)
2813{
2814 int cpu = (long)hcpu;
2815 int ret = NOTIFY_OK;
2816
2817 switch (action) {
ce421c79 2818 case CPU_UP_PREPARE:
8bb78442 2819 case CPU_UP_PREPARE_FROZEN:
ce421c79
AW
2820 if (process_zones(cpu))
2821 ret = NOTIFY_BAD;
2822 break;
2823 case CPU_UP_CANCELED:
8bb78442 2824 case CPU_UP_CANCELED_FROZEN:
ce421c79 2825 case CPU_DEAD:
8bb78442 2826 case CPU_DEAD_FROZEN:
ce421c79
AW
2827 free_zone_pagesets(cpu);
2828 break;
2829 default:
2830 break;
e7c8d5c9
CL
2831 }
2832 return ret;
2833}
2834
74b85f37 2835static struct notifier_block __cpuinitdata pageset_notifier =
e7c8d5c9
CL
2836 { &pageset_cpuup_callback, NULL, 0 };
2837
78d9955b 2838void __init setup_per_cpu_pageset(void)
e7c8d5c9
CL
2839{
2840 int err;
2841
2842 /* Initialize per_cpu_pageset for cpu 0.
2843 * A cpuup callback will do this for every cpu
2844 * as it comes online
2845 */
2846 err = process_zones(smp_processor_id());
2847 BUG_ON(err);
2848 register_cpu_notifier(&pageset_notifier);
2849}
2850
2851#endif
2852
577a32f6 2853static noinline __init_refok
cca448fe 2854int zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages)
ed8ece2e
DH
2855{
2856 int i;
2857 struct pglist_data *pgdat = zone->zone_pgdat;
cca448fe 2858 size_t alloc_size;
ed8ece2e
DH
2859
2860 /*
2861 * The per-page waitqueue mechanism uses hashed waitqueues
2862 * per zone.
2863 */
02b694de
YG
2864 zone->wait_table_hash_nr_entries =
2865 wait_table_hash_nr_entries(zone_size_pages);
2866 zone->wait_table_bits =
2867 wait_table_bits(zone->wait_table_hash_nr_entries);
cca448fe
YG
2868 alloc_size = zone->wait_table_hash_nr_entries
2869 * sizeof(wait_queue_head_t);
2870
cd94b9db 2871 if (!slab_is_available()) {
cca448fe
YG
2872 zone->wait_table = (wait_queue_head_t *)
2873 alloc_bootmem_node(pgdat, alloc_size);
2874 } else {
2875 /*
2876 * This case means that a zone whose size was 0 gets new memory
2877 * via memory hot-add.
2878 * But it may be the case that a new node was hot-added. In
2879 * this case vmalloc() will not be able to use this new node's
2880 * memory - this wait_table must be initialized to use this new
2881 * node itself as well.
2882 * To use this new node's memory, further consideration will be
2883 * necessary.
2884 */
8691f3a7 2885 zone->wait_table = vmalloc(alloc_size);
cca448fe
YG
2886 }
2887 if (!zone->wait_table)
2888 return -ENOMEM;
ed8ece2e 2889
02b694de 2890 for(i = 0; i < zone->wait_table_hash_nr_entries; ++i)
ed8ece2e 2891 init_waitqueue_head(zone->wait_table + i);
cca448fe
YG
2892
2893 return 0;
ed8ece2e
DH
2894}
2895
c09b4240 2896static __meminit void zone_pcp_init(struct zone *zone)
ed8ece2e
DH
2897{
2898 int cpu;
2899 unsigned long batch = zone_batchsize(zone);
2900
2901 for (cpu = 0; cpu < NR_CPUS; cpu++) {
2902#ifdef CONFIG_NUMA
2903 /* Early boot. Slab allocator not functional yet */
23316bc8 2904 zone_pcp(zone, cpu) = &boot_pageset[cpu];
ed8ece2e
DH
2905 setup_pageset(&boot_pageset[cpu],0);
2906#else
2907 setup_pageset(zone_pcp(zone,cpu), batch);
2908#endif
2909 }
f5335c0f
AB
2910 if (zone->present_pages)
2911 printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%lu\n",
2912 zone->name, zone->present_pages, batch);
ed8ece2e
DH
2913}
2914
718127cc
YG
2915__meminit int init_currently_empty_zone(struct zone *zone,
2916 unsigned long zone_start_pfn,
a2f3aa02
DH
2917 unsigned long size,
2918 enum memmap_context context)
ed8ece2e
DH
2919{
2920 struct pglist_data *pgdat = zone->zone_pgdat;
cca448fe
YG
2921 int ret;
2922 ret = zone_wait_table_init(zone, size);
2923 if (ret)
2924 return ret;
ed8ece2e
DH
2925 pgdat->nr_zones = zone_idx(zone) + 1;
2926
ed8ece2e
DH
2927 zone->zone_start_pfn = zone_start_pfn;
2928
708614e6
MG
2929 mminit_dprintk(MMINIT_TRACE, "memmap_init",
2930 "Initialising map node %d zone %lu pfns %lu -> %lu\n",
2931 pgdat->node_id,
2932 (unsigned long)zone_idx(zone),
2933 zone_start_pfn, (zone_start_pfn + size));
2934
1e548deb 2935 zone_init_free_lists(zone);
718127cc
YG
2936
2937 return 0;
ed8ece2e
DH
2938}
2939
c713216d
MG
2940#ifdef CONFIG_ARCH_POPULATES_NODE_MAP
2941/*
2942 * Basic iterator support. Return the first range of PFNs for a node
2943 * Note: nid == MAX_NUMNODES returns first region regardless of node
2944 */
a3142c8e 2945static int __meminit first_active_region_index_in_nid(int nid)
c713216d
MG
2946{
2947 int i;
2948
2949 for (i = 0; i < nr_nodemap_entries; i++)
2950 if (nid == MAX_NUMNODES || early_node_map[i].nid == nid)
2951 return i;
2952
2953 return -1;
2954}
2955
2956/*
2957 * Basic iterator support. Return the next active range of PFNs for a node
183ff22b 2958 * Note: nid == MAX_NUMNODES returns next region regardless of node
c713216d 2959 */
a3142c8e 2960static int __meminit next_active_region_index_in_nid(int index, int nid)
c713216d
MG
2961{
2962 for (index = index + 1; index < nr_nodemap_entries; index++)
2963 if (nid == MAX_NUMNODES || early_node_map[index].nid == nid)
2964 return index;
2965
2966 return -1;
2967}
2968
2969#ifndef CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID
2970/*
2971 * Required by SPARSEMEM. Given a PFN, return what node the PFN is on.
2972 * Architectures may implement their own version but if add_active_range()
2973 * was used and there are no special requirements, this is a convenient
2974 * alternative
2975 */
6f076f5d 2976int __meminit early_pfn_to_nid(unsigned long pfn)
c713216d
MG
2977{
2978 int i;
2979
2980 for (i = 0; i < nr_nodemap_entries; i++) {
2981 unsigned long start_pfn = early_node_map[i].start_pfn;
2982 unsigned long end_pfn = early_node_map[i].end_pfn;
2983
2984 if (start_pfn <= pfn && pfn < end_pfn)
2985 return early_node_map[i].nid;
2986 }
2987
2988 return 0;
2989}
2990#endif /* CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID */
2991
2992/* Basic iterator support to walk early_node_map[] */
2993#define for_each_active_range_index_in_nid(i, nid) \
2994 for (i = first_active_region_index_in_nid(nid); i != -1; \
2995 i = next_active_region_index_in_nid(i, nid))
2996
2997/**
2998 * free_bootmem_with_active_regions - Call free_bootmem_node for each active range
88ca3b94
RD
2999 * @nid: The node to free memory on. If MAX_NUMNODES, all nodes are freed.
3000 * @max_low_pfn: The highest PFN that will be passed to free_bootmem_node
c713216d
MG
3001 *
3002 * If an architecture guarantees that all ranges registered with
3003 * add_active_ranges() contain no holes and may be freed, this
3004 * this function may be used instead of calling free_bootmem() manually.
3005 */
3006void __init free_bootmem_with_active_regions(int nid,
3007 unsigned long max_low_pfn)
3008{
3009 int i;
3010
3011 for_each_active_range_index_in_nid(i, nid) {
3012 unsigned long size_pages = 0;
3013 unsigned long end_pfn = early_node_map[i].end_pfn;
3014
3015 if (early_node_map[i].start_pfn >= max_low_pfn)
3016 continue;
3017
3018 if (end_pfn > max_low_pfn)
3019 end_pfn = max_low_pfn;
3020
3021 size_pages = end_pfn - early_node_map[i].start_pfn;
3022 free_bootmem_node(NODE_DATA(early_node_map[i].nid),
3023 PFN_PHYS(early_node_map[i].start_pfn),
3024 size_pages << PAGE_SHIFT);
3025 }
3026}
3027
b5bc6c0e
YL
3028void __init work_with_active_regions(int nid, work_fn_t work_fn, void *data)
3029{
3030 int i;
d52d53b8 3031 int ret;
b5bc6c0e 3032
d52d53b8
YL
3033 for_each_active_range_index_in_nid(i, nid) {
3034 ret = work_fn(early_node_map[i].start_pfn,
3035 early_node_map[i].end_pfn, data);
3036 if (ret)
3037 break;
3038 }
b5bc6c0e 3039}
c713216d
MG
3040/**
3041 * sparse_memory_present_with_active_regions - Call memory_present for each active range
88ca3b94 3042 * @nid: The node to call memory_present for. If MAX_NUMNODES, all nodes will be used.
c713216d
MG
3043 *
3044 * If an architecture guarantees that all ranges registered with
3045 * add_active_ranges() contain no holes and may be freed, this
88ca3b94 3046 * function may be used instead of calling memory_present() manually.
c713216d
MG
3047 */
3048void __init sparse_memory_present_with_active_regions(int nid)
3049{
3050 int i;
3051
3052 for_each_active_range_index_in_nid(i, nid)
3053 memory_present(early_node_map[i].nid,
3054 early_node_map[i].start_pfn,
3055 early_node_map[i].end_pfn);
3056}
3057
fb01439c
MG
3058/**
3059 * push_node_boundaries - Push node boundaries to at least the requested boundary
3060 * @nid: The nid of the node to push the boundary for
3061 * @start_pfn: The start pfn of the node
3062 * @end_pfn: The end pfn of the node
3063 *
3064 * In reserve-based hot-add, mem_map is allocated that is unused until hotadd
3065 * time. Specifically, on x86_64, SRAT will report ranges that can potentially
3066 * be hotplugged even though no physical memory exists. This function allows
3067 * an arch to push out the node boundaries so mem_map is allocated that can
3068 * be used later.
3069 */
3070#ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
3071void __init push_node_boundaries(unsigned int nid,
3072 unsigned long start_pfn, unsigned long end_pfn)
3073{
6b74ab97
MG
3074 mminit_dprintk(MMINIT_TRACE, "zoneboundary",
3075 "Entering push_node_boundaries(%u, %lu, %lu)\n",
fb01439c
MG
3076 nid, start_pfn, end_pfn);
3077
3078 /* Initialise the boundary for this node if necessary */
3079 if (node_boundary_end_pfn[nid] == 0)
3080 node_boundary_start_pfn[nid] = -1UL;
3081
3082 /* Update the boundaries */
3083 if (node_boundary_start_pfn[nid] > start_pfn)
3084 node_boundary_start_pfn[nid] = start_pfn;
3085 if (node_boundary_end_pfn[nid] < end_pfn)
3086 node_boundary_end_pfn[nid] = end_pfn;
3087}
3088
3089/* If necessary, push the node boundary out for reserve hotadd */
98011f56 3090static void __meminit account_node_boundary(unsigned int nid,
fb01439c
MG
3091 unsigned long *start_pfn, unsigned long *end_pfn)
3092{
6b74ab97
MG
3093 mminit_dprintk(MMINIT_TRACE, "zoneboundary",
3094 "Entering account_node_boundary(%u, %lu, %lu)\n",
fb01439c
MG
3095 nid, *start_pfn, *end_pfn);
3096
3097 /* Return if boundary information has not been provided */
3098 if (node_boundary_end_pfn[nid] == 0)
3099 return;
3100
3101 /* Check the boundaries and update if necessary */
3102 if (node_boundary_start_pfn[nid] < *start_pfn)
3103 *start_pfn = node_boundary_start_pfn[nid];
3104 if (node_boundary_end_pfn[nid] > *end_pfn)
3105 *end_pfn = node_boundary_end_pfn[nid];
3106}
3107#else
3108void __init push_node_boundaries(unsigned int nid,
3109 unsigned long start_pfn, unsigned long end_pfn) {}
3110
98011f56 3111static void __meminit account_node_boundary(unsigned int nid,
fb01439c
MG
3112 unsigned long *start_pfn, unsigned long *end_pfn) {}
3113#endif
3114
3115
c713216d
MG
3116/**
3117 * get_pfn_range_for_nid - Return the start and end page frames for a node
88ca3b94
RD
3118 * @nid: The nid to return the range for. If MAX_NUMNODES, the min and max PFN are returned.
3119 * @start_pfn: Passed by reference. On return, it will have the node start_pfn.
3120 * @end_pfn: Passed by reference. On return, it will have the node end_pfn.
c713216d
MG
3121 *
3122 * It returns the start and end page frame of a node based on information
3123 * provided by an arch calling add_active_range(). If called for a node
3124 * with no available memory, a warning is printed and the start and end
88ca3b94 3125 * PFNs will be 0.
c713216d 3126 */
a3142c8e 3127void __meminit get_pfn_range_for_nid(unsigned int nid,
c713216d
MG
3128 unsigned long *start_pfn, unsigned long *end_pfn)
3129{
3130 int i;
3131 *start_pfn = -1UL;
3132 *end_pfn = 0;
3133
3134 for_each_active_range_index_in_nid(i, nid) {
3135 *start_pfn = min(*start_pfn, early_node_map[i].start_pfn);
3136 *end_pfn = max(*end_pfn, early_node_map[i].end_pfn);
3137 }
3138
633c0666 3139 if (*start_pfn == -1UL)
c713216d 3140 *start_pfn = 0;
fb01439c
MG
3141
3142 /* Push the node boundaries out if requested */
3143 account_node_boundary(nid, start_pfn, end_pfn);
c713216d
MG
3144}
3145
2a1e274a
MG
3146/*
3147 * This finds a zone that can be used for ZONE_MOVABLE pages. The
3148 * assumption is made that zones within a node are ordered in monotonic
3149 * increasing memory addresses so that the "highest" populated zone is used
3150 */
b69a7288 3151static void __init find_usable_zone_for_movable(void)
2a1e274a
MG
3152{
3153 int zone_index;
3154 for (zone_index = MAX_NR_ZONES - 1; zone_index >= 0; zone_index--) {
3155 if (zone_index == ZONE_MOVABLE)
3156 continue;
3157
3158 if (arch_zone_highest_possible_pfn[zone_index] >
3159 arch_zone_lowest_possible_pfn[zone_index])
3160 break;
3161 }
3162
3163 VM_BUG_ON(zone_index == -1);
3164 movable_zone = zone_index;
3165}
3166
3167/*
3168 * The zone ranges provided by the architecture do not include ZONE_MOVABLE
3169 * because it is sized independant of architecture. Unlike the other zones,
3170 * the starting point for ZONE_MOVABLE is not fixed. It may be different
3171 * in each node depending on the size of each node and how evenly kernelcore
3172 * is distributed. This helper function adjusts the zone ranges
3173 * provided by the architecture for a given node by using the end of the
3174 * highest usable zone for ZONE_MOVABLE. This preserves the assumption that
3175 * zones within a node are in order of monotonic increases memory addresses
3176 */
b69a7288 3177static void __meminit adjust_zone_range_for_zone_movable(int nid,
2a1e274a
MG
3178 unsigned long zone_type,
3179 unsigned long node_start_pfn,
3180 unsigned long node_end_pfn,
3181 unsigned long *zone_start_pfn,
3182 unsigned long *zone_end_pfn)
3183{
3184 /* Only adjust if ZONE_MOVABLE is on this node */
3185 if (zone_movable_pfn[nid]) {
3186 /* Size ZONE_MOVABLE */
3187 if (zone_type == ZONE_MOVABLE) {
3188 *zone_start_pfn = zone_movable_pfn[nid];
3189 *zone_end_pfn = min(node_end_pfn,
3190 arch_zone_highest_possible_pfn[movable_zone]);
3191
3192 /* Adjust for ZONE_MOVABLE starting within this range */
3193 } else if (*zone_start_pfn < zone_movable_pfn[nid] &&
3194 *zone_end_pfn > zone_movable_pfn[nid]) {
3195 *zone_end_pfn = zone_movable_pfn[nid];
3196
3197 /* Check if this whole range is within ZONE_MOVABLE */
3198 } else if (*zone_start_pfn >= zone_movable_pfn[nid])
3199 *zone_start_pfn = *zone_end_pfn;
3200 }
3201}
3202
c713216d
MG
3203/*
3204 * Return the number of pages a zone spans in a node, including holes
3205 * present_pages = zone_spanned_pages_in_node() - zone_absent_pages_in_node()
3206 */
6ea6e688 3207static unsigned long __meminit zone_spanned_pages_in_node(int nid,
c713216d
MG
3208 unsigned long zone_type,
3209 unsigned long *ignored)
3210{
3211 unsigned long node_start_pfn, node_end_pfn;
3212 unsigned long zone_start_pfn, zone_end_pfn;
3213
3214 /* Get the start and end of the node and zone */
3215 get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
3216 zone_start_pfn = arch_zone_lowest_possible_pfn[zone_type];
3217 zone_end_pfn = arch_zone_highest_possible_pfn[zone_type];
2a1e274a
MG
3218 adjust_zone_range_for_zone_movable(nid, zone_type,
3219 node_start_pfn, node_end_pfn,
3220 &zone_start_pfn, &zone_end_pfn);
c713216d
MG
3221
3222 /* Check that this node has pages within the zone's required range */
3223 if (zone_end_pfn < node_start_pfn || zone_start_pfn > node_end_pfn)
3224 return 0;
3225
3226 /* Move the zone boundaries inside the node if necessary */
3227 zone_end_pfn = min(zone_end_pfn, node_end_pfn);
3228 zone_start_pfn = max(zone_start_pfn, node_start_pfn);
3229
3230 /* Return the spanned pages */
3231 return zone_end_pfn - zone_start_pfn;
3232}
3233
3234/*
3235 * Return the number of holes in a range on a node. If nid is MAX_NUMNODES,
88ca3b94 3236 * then all holes in the requested range will be accounted for.
c713216d 3237 */
b69a7288 3238static unsigned long __meminit __absent_pages_in_range(int nid,
c713216d
MG
3239 unsigned long range_start_pfn,
3240 unsigned long range_end_pfn)
3241{
3242 int i = 0;
3243 unsigned long prev_end_pfn = 0, hole_pages = 0;
3244 unsigned long start_pfn;
3245
3246 /* Find the end_pfn of the first active range of pfns in the node */
3247 i = first_active_region_index_in_nid(nid);
3248 if (i == -1)
3249 return 0;
3250
b5445f95
MG
3251 prev_end_pfn = min(early_node_map[i].start_pfn, range_end_pfn);
3252
9c7cd687
MG
3253 /* Account for ranges before physical memory on this node */
3254 if (early_node_map[i].start_pfn > range_start_pfn)
b5445f95 3255 hole_pages = prev_end_pfn - range_start_pfn;
c713216d
MG
3256
3257 /* Find all holes for the zone within the node */
3258 for (; i != -1; i = next_active_region_index_in_nid(i, nid)) {
3259
3260 /* No need to continue if prev_end_pfn is outside the zone */
3261 if (prev_end_pfn >= range_end_pfn)
3262 break;
3263
3264 /* Make sure the end of the zone is not within the hole */
3265 start_pfn = min(early_node_map[i].start_pfn, range_end_pfn);
3266 prev_end_pfn = max(prev_end_pfn, range_start_pfn);
3267
3268 /* Update the hole size cound and move on */
3269 if (start_pfn > range_start_pfn) {
3270 BUG_ON(prev_end_pfn > start_pfn);
3271 hole_pages += start_pfn - prev_end_pfn;
3272 }
3273 prev_end_pfn = early_node_map[i].end_pfn;
3274 }
3275
9c7cd687
MG
3276 /* Account for ranges past physical memory on this node */
3277 if (range_end_pfn > prev_end_pfn)
0c6cb974 3278 hole_pages += range_end_pfn -
9c7cd687
MG
3279 max(range_start_pfn, prev_end_pfn);
3280
c713216d
MG
3281 return hole_pages;
3282}
3283
3284/**
3285 * absent_pages_in_range - Return number of page frames in holes within a range
3286 * @start_pfn: The start PFN to start searching for holes
3287 * @end_pfn: The end PFN to stop searching for holes
3288 *
88ca3b94 3289 * It returns the number of pages frames in memory holes within a range.
c713216d
MG
3290 */
3291unsigned long __init absent_pages_in_range(unsigned long start_pfn,
3292 unsigned long end_pfn)
3293{
3294 return __absent_pages_in_range(MAX_NUMNODES, start_pfn, end_pfn);
3295}
3296
3297/* Return the number of page frames in holes in a zone on a node */
6ea6e688 3298static unsigned long __meminit zone_absent_pages_in_node(int nid,
c713216d
MG
3299 unsigned long zone_type,
3300 unsigned long *ignored)
3301{
9c7cd687
MG
3302 unsigned long node_start_pfn, node_end_pfn;
3303 unsigned long zone_start_pfn, zone_end_pfn;
3304
3305 get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
3306 zone_start_pfn = max(arch_zone_lowest_possible_pfn[zone_type],
3307 node_start_pfn);
3308 zone_end_pfn = min(arch_zone_highest_possible_pfn[zone_type],
3309 node_end_pfn);
3310
2a1e274a
MG
3311 adjust_zone_range_for_zone_movable(nid, zone_type,
3312 node_start_pfn, node_end_pfn,
3313 &zone_start_pfn, &zone_end_pfn);
9c7cd687 3314 return __absent_pages_in_range(nid, zone_start_pfn, zone_end_pfn);
c713216d 3315}
0e0b864e 3316
c713216d 3317#else
6ea6e688 3318static inline unsigned long __meminit zone_spanned_pages_in_node(int nid,
c713216d
MG
3319 unsigned long zone_type,
3320 unsigned long *zones_size)
3321{
3322 return zones_size[zone_type];
3323}
3324
6ea6e688 3325static inline unsigned long __meminit zone_absent_pages_in_node(int nid,
c713216d
MG
3326 unsigned long zone_type,
3327 unsigned long *zholes_size)
3328{
3329 if (!zholes_size)
3330 return 0;
3331
3332 return zholes_size[zone_type];
3333}
0e0b864e 3334
c713216d
MG
3335#endif
3336
a3142c8e 3337static void __meminit calculate_node_totalpages(struct pglist_data *pgdat,
c713216d
MG
3338 unsigned long *zones_size, unsigned long *zholes_size)
3339{
3340 unsigned long realtotalpages, totalpages = 0;
3341 enum zone_type i;
3342
3343 for (i = 0; i < MAX_NR_ZONES; i++)
3344 totalpages += zone_spanned_pages_in_node(pgdat->node_id, i,
3345 zones_size);
3346 pgdat->node_spanned_pages = totalpages;
3347
3348 realtotalpages = totalpages;
3349 for (i = 0; i < MAX_NR_ZONES; i++)
3350 realtotalpages -=
3351 zone_absent_pages_in_node(pgdat->node_id, i,
3352 zholes_size);
3353 pgdat->node_present_pages = realtotalpages;
3354 printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id,
3355 realtotalpages);
3356}
3357
835c134e
MG
3358#ifndef CONFIG_SPARSEMEM
3359/*
3360 * Calculate the size of the zone->blockflags rounded to an unsigned long
d9c23400
MG
3361 * Start by making sure zonesize is a multiple of pageblock_order by rounding
3362 * up. Then use 1 NR_PAGEBLOCK_BITS worth of bits per pageblock, finally
835c134e
MG
3363 * round what is now in bits to nearest long in bits, then return it in
3364 * bytes.
3365 */
3366static unsigned long __init usemap_size(unsigned long zonesize)
3367{
3368 unsigned long usemapsize;
3369
d9c23400
MG
3370 usemapsize = roundup(zonesize, pageblock_nr_pages);
3371 usemapsize = usemapsize >> pageblock_order;
835c134e
MG
3372 usemapsize *= NR_PAGEBLOCK_BITS;
3373 usemapsize = roundup(usemapsize, 8 * sizeof(unsigned long));
3374
3375 return usemapsize / 8;
3376}
3377
3378static void __init setup_usemap(struct pglist_data *pgdat,
3379 struct zone *zone, unsigned long zonesize)
3380{
3381 unsigned long usemapsize = usemap_size(zonesize);
3382 zone->pageblock_flags = NULL;
58a01a45 3383 if (usemapsize)
835c134e 3384 zone->pageblock_flags = alloc_bootmem_node(pgdat, usemapsize);
835c134e
MG
3385}
3386#else
3387static void inline setup_usemap(struct pglist_data *pgdat,
3388 struct zone *zone, unsigned long zonesize) {}
3389#endif /* CONFIG_SPARSEMEM */
3390
d9c23400 3391#ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
ba72cb8c
MG
3392
3393/* Return a sensible default order for the pageblock size. */
3394static inline int pageblock_default_order(void)
3395{
3396 if (HPAGE_SHIFT > PAGE_SHIFT)
3397 return HUGETLB_PAGE_ORDER;
3398
3399 return MAX_ORDER-1;
3400}
3401
d9c23400
MG
3402/* Initialise the number of pages represented by NR_PAGEBLOCK_BITS */
3403static inline void __init set_pageblock_order(unsigned int order)
3404{
3405 /* Check that pageblock_nr_pages has not already been setup */
3406 if (pageblock_order)
3407 return;
3408
3409 /*
3410 * Assume the largest contiguous order of interest is a huge page.
3411 * This value may be variable depending on boot parameters on IA64
3412 */
3413 pageblock_order = order;
3414}
3415#else /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
3416
ba72cb8c
MG
3417/*
3418 * When CONFIG_HUGETLB_PAGE_SIZE_VARIABLE is not set, set_pageblock_order()
3419 * and pageblock_default_order() are unused as pageblock_order is set
3420 * at compile-time. See include/linux/pageblock-flags.h for the values of
3421 * pageblock_order based on the kernel config
3422 */
3423static inline int pageblock_default_order(unsigned int order)
3424{
3425 return MAX_ORDER-1;
3426}
d9c23400
MG
3427#define set_pageblock_order(x) do {} while (0)
3428
3429#endif /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
3430
1da177e4
LT
3431/*
3432 * Set up the zone data structures:
3433 * - mark all pages reserved
3434 * - mark all memory queues empty
3435 * - clear the memory bitmaps
3436 */
b5a0e011 3437static void __paginginit free_area_init_core(struct pglist_data *pgdat,
1da177e4
LT
3438 unsigned long *zones_size, unsigned long *zholes_size)
3439{
2f1b6248 3440 enum zone_type j;
ed8ece2e 3441 int nid = pgdat->node_id;
1da177e4 3442 unsigned long zone_start_pfn = pgdat->node_start_pfn;
718127cc 3443 int ret;
1da177e4 3444
208d54e5 3445 pgdat_resize_init(pgdat);
1da177e4
LT
3446 pgdat->nr_zones = 0;
3447 init_waitqueue_head(&pgdat->kswapd_wait);
3448 pgdat->kswapd_max_order = 0;
52d4b9ac 3449 pgdat_page_cgroup_init(pgdat);
1da177e4
LT
3450
3451 for (j = 0; j < MAX_NR_ZONES; j++) {
3452 struct zone *zone = pgdat->node_zones + j;
0e0b864e 3453 unsigned long size, realsize, memmap_pages;
b69408e8 3454 enum lru_list l;
1da177e4 3455
c713216d
MG
3456 size = zone_spanned_pages_in_node(nid, j, zones_size);
3457 realsize = size - zone_absent_pages_in_node(nid, j,
3458 zholes_size);
1da177e4 3459
0e0b864e
MG
3460 /*
3461 * Adjust realsize so that it accounts for how much memory
3462 * is used by this zone for memmap. This affects the watermark
3463 * and per-cpu initialisations
3464 */
f7232154
JW
3465 memmap_pages =
3466 PAGE_ALIGN(size * sizeof(struct page)) >> PAGE_SHIFT;
0e0b864e
MG
3467 if (realsize >= memmap_pages) {
3468 realsize -= memmap_pages;
5594c8c8
YL
3469 if (memmap_pages)
3470 printk(KERN_DEBUG
3471 " %s zone: %lu pages used for memmap\n",
3472 zone_names[j], memmap_pages);
0e0b864e
MG
3473 } else
3474 printk(KERN_WARNING
3475 " %s zone: %lu pages exceeds realsize %lu\n",
3476 zone_names[j], memmap_pages, realsize);
3477
6267276f
CL
3478 /* Account for reserved pages */
3479 if (j == 0 && realsize > dma_reserve) {
0e0b864e 3480 realsize -= dma_reserve;
d903ef9f 3481 printk(KERN_DEBUG " %s zone: %lu pages reserved\n",
6267276f 3482 zone_names[0], dma_reserve);
0e0b864e
MG
3483 }
3484
98d2b0eb 3485 if (!is_highmem_idx(j))
1da177e4
LT
3486 nr_kernel_pages += realsize;
3487 nr_all_pages += realsize;
3488
3489 zone->spanned_pages = size;
3490 zone->present_pages = realsize;
9614634f 3491#ifdef CONFIG_NUMA
d5f541ed 3492 zone->node = nid;
8417bba4 3493 zone->min_unmapped_pages = (realsize*sysctl_min_unmapped_ratio)
9614634f 3494 / 100;
0ff38490 3495 zone->min_slab_pages = (realsize * sysctl_min_slab_ratio) / 100;
9614634f 3496#endif
1da177e4
LT
3497 zone->name = zone_names[j];
3498 spin_lock_init(&zone->lock);
3499 spin_lock_init(&zone->lru_lock);
bdc8cb98 3500 zone_seqlock_init(zone);
1da177e4 3501 zone->zone_pgdat = pgdat;
1da177e4 3502
3bb1a852 3503 zone->prev_priority = DEF_PRIORITY;
1da177e4 3504
ed8ece2e 3505 zone_pcp_init(zone);
b69408e8
CL
3506 for_each_lru(l) {
3507 INIT_LIST_HEAD(&zone->lru[l].list);
3508 zone->lru[l].nr_scan = 0;
3509 }
4f98a2fe
RR
3510 zone->recent_rotated[0] = 0;
3511 zone->recent_rotated[1] = 0;
3512 zone->recent_scanned[0] = 0;
3513 zone->recent_scanned[1] = 0;
2244b95a 3514 zap_zone_vm_stats(zone);
e815af95 3515 zone->flags = 0;
1da177e4
LT
3516 if (!size)
3517 continue;
3518
ba72cb8c 3519 set_pageblock_order(pageblock_default_order());
835c134e 3520 setup_usemap(pgdat, zone, size);
a2f3aa02
DH
3521 ret = init_currently_empty_zone(zone, zone_start_pfn,
3522 size, MEMMAP_EARLY);
718127cc 3523 BUG_ON(ret);
76cdd58e 3524 memmap_init(size, nid, j, zone_start_pfn);
1da177e4 3525 zone_start_pfn += size;
1da177e4
LT
3526 }
3527}
3528
577a32f6 3529static void __init_refok alloc_node_mem_map(struct pglist_data *pgdat)
1da177e4 3530{
1da177e4
LT
3531 /* Skip empty nodes */
3532 if (!pgdat->node_spanned_pages)
3533 return;
3534
d41dee36 3535#ifdef CONFIG_FLAT_NODE_MEM_MAP
1da177e4
LT
3536 /* ia64 gets its own node_mem_map, before this, without bootmem */
3537 if (!pgdat->node_mem_map) {
e984bb43 3538 unsigned long size, start, end;
d41dee36
AW
3539 struct page *map;
3540
e984bb43
BP
3541 /*
3542 * The zone's endpoints aren't required to be MAX_ORDER
3543 * aligned but the node_mem_map endpoints must be in order
3544 * for the buddy allocator to function correctly.
3545 */
3546 start = pgdat->node_start_pfn & ~(MAX_ORDER_NR_PAGES - 1);
3547 end = pgdat->node_start_pfn + pgdat->node_spanned_pages;
3548 end = ALIGN(end, MAX_ORDER_NR_PAGES);
3549 size = (end - start) * sizeof(struct page);
6f167ec7
DH
3550 map = alloc_remap(pgdat->node_id, size);
3551 if (!map)
3552 map = alloc_bootmem_node(pgdat, size);
e984bb43 3553 pgdat->node_mem_map = map + (pgdat->node_start_pfn - start);
1da177e4 3554 }
12d810c1 3555#ifndef CONFIG_NEED_MULTIPLE_NODES
1da177e4
LT
3556 /*
3557 * With no DISCONTIG, the global mem_map is just set as node 0's
3558 */
c713216d 3559 if (pgdat == NODE_DATA(0)) {
1da177e4 3560 mem_map = NODE_DATA(0)->node_mem_map;
c713216d
MG
3561#ifdef CONFIG_ARCH_POPULATES_NODE_MAP
3562 if (page_to_pfn(mem_map) != pgdat->node_start_pfn)
467bc461 3563 mem_map -= (pgdat->node_start_pfn - ARCH_PFN_OFFSET);
c713216d
MG
3564#endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
3565 }
1da177e4 3566#endif
d41dee36 3567#endif /* CONFIG_FLAT_NODE_MEM_MAP */
1da177e4
LT
3568}
3569
9109fb7b
JW
3570void __paginginit free_area_init_node(int nid, unsigned long *zones_size,
3571 unsigned long node_start_pfn, unsigned long *zholes_size)
1da177e4 3572{
9109fb7b
JW
3573 pg_data_t *pgdat = NODE_DATA(nid);
3574
1da177e4
LT
3575 pgdat->node_id = nid;
3576 pgdat->node_start_pfn = node_start_pfn;
c713216d 3577 calculate_node_totalpages(pgdat, zones_size, zholes_size);
1da177e4
LT
3578
3579 alloc_node_mem_map(pgdat);
e8c27ac9
YL
3580#ifdef CONFIG_FLAT_NODE_MEM_MAP
3581 printk(KERN_DEBUG "free_area_init_node: node %d, pgdat %08lx, node_mem_map %08lx\n",
3582 nid, (unsigned long)pgdat,
3583 (unsigned long)pgdat->node_mem_map);
3584#endif
1da177e4
LT
3585
3586 free_area_init_core(pgdat, zones_size, zholes_size);
3587}
3588
c713216d 3589#ifdef CONFIG_ARCH_POPULATES_NODE_MAP
418508c1
MS
3590
3591#if MAX_NUMNODES > 1
3592/*
3593 * Figure out the number of possible node ids.
3594 */
3595static void __init setup_nr_node_ids(void)
3596{
3597 unsigned int node;
3598 unsigned int highest = 0;
3599
3600 for_each_node_mask(node, node_possible_map)
3601 highest = node;
3602 nr_node_ids = highest + 1;
3603}
3604#else
3605static inline void setup_nr_node_ids(void)
3606{
3607}
3608#endif
3609
c713216d
MG
3610/**
3611 * add_active_range - Register a range of PFNs backed by physical memory
3612 * @nid: The node ID the range resides on
3613 * @start_pfn: The start PFN of the available physical memory
3614 * @end_pfn: The end PFN of the available physical memory
3615 *
3616 * These ranges are stored in an early_node_map[] and later used by
3617 * free_area_init_nodes() to calculate zone sizes and holes. If the
3618 * range spans a memory hole, it is up to the architecture to ensure
3619 * the memory is not freed by the bootmem allocator. If possible
3620 * the range being registered will be merged with existing ranges.
3621 */
3622void __init add_active_range(unsigned int nid, unsigned long start_pfn,
3623 unsigned long end_pfn)
3624{
3625 int i;
3626
6b74ab97
MG
3627 mminit_dprintk(MMINIT_TRACE, "memory_register",
3628 "Entering add_active_range(%d, %#lx, %#lx) "
3629 "%d entries of %d used\n",
3630 nid, start_pfn, end_pfn,
3631 nr_nodemap_entries, MAX_ACTIVE_REGIONS);
c713216d 3632
2dbb51c4
MG
3633 mminit_validate_memmodel_limits(&start_pfn, &end_pfn);
3634
c713216d
MG
3635 /* Merge with existing active regions if possible */
3636 for (i = 0; i < nr_nodemap_entries; i++) {
3637 if (early_node_map[i].nid != nid)
3638 continue;
3639
3640 /* Skip if an existing region covers this new one */
3641 if (start_pfn >= early_node_map[i].start_pfn &&
3642 end_pfn <= early_node_map[i].end_pfn)
3643 return;
3644
3645 /* Merge forward if suitable */
3646 if (start_pfn <= early_node_map[i].end_pfn &&
3647 end_pfn > early_node_map[i].end_pfn) {
3648 early_node_map[i].end_pfn = end_pfn;
3649 return;
3650 }
3651
3652 /* Merge backward if suitable */
3653 if (start_pfn < early_node_map[i].end_pfn &&
3654 end_pfn >= early_node_map[i].start_pfn) {
3655 early_node_map[i].start_pfn = start_pfn;
3656 return;
3657 }
3658 }
3659
3660 /* Check that early_node_map is large enough */
3661 if (i >= MAX_ACTIVE_REGIONS) {
3662 printk(KERN_CRIT "More than %d memory regions, truncating\n",
3663 MAX_ACTIVE_REGIONS);
3664 return;
3665 }
3666
3667 early_node_map[i].nid = nid;
3668 early_node_map[i].start_pfn = start_pfn;
3669 early_node_map[i].end_pfn = end_pfn;
3670 nr_nodemap_entries = i + 1;
3671}
3672
3673/**
cc1050ba 3674 * remove_active_range - Shrink an existing registered range of PFNs
c713216d 3675 * @nid: The node id the range is on that should be shrunk
cc1050ba
YL
3676 * @start_pfn: The new PFN of the range
3677 * @end_pfn: The new PFN of the range
c713216d
MG
3678 *
3679 * i386 with NUMA use alloc_remap() to store a node_mem_map on a local node.
cc1a9d86
YL
3680 * The map is kept near the end physical page range that has already been
3681 * registered. This function allows an arch to shrink an existing registered
3682 * range.
c713216d 3683 */
cc1050ba
YL
3684void __init remove_active_range(unsigned int nid, unsigned long start_pfn,
3685 unsigned long end_pfn)
c713216d 3686{
cc1a9d86
YL
3687 int i, j;
3688 int removed = 0;
c713216d 3689
cc1050ba
YL
3690 printk(KERN_DEBUG "remove_active_range (%d, %lu, %lu)\n",
3691 nid, start_pfn, end_pfn);
3692
c713216d 3693 /* Find the old active region end and shrink */
cc1a9d86 3694 for_each_active_range_index_in_nid(i, nid) {
cc1050ba
YL
3695 if (early_node_map[i].start_pfn >= start_pfn &&
3696 early_node_map[i].end_pfn <= end_pfn) {
cc1a9d86 3697 /* clear it */
cc1050ba 3698 early_node_map[i].start_pfn = 0;
cc1a9d86
YL
3699 early_node_map[i].end_pfn = 0;
3700 removed = 1;
3701 continue;
3702 }
cc1050ba
YL
3703 if (early_node_map[i].start_pfn < start_pfn &&
3704 early_node_map[i].end_pfn > start_pfn) {
3705 unsigned long temp_end_pfn = early_node_map[i].end_pfn;
3706 early_node_map[i].end_pfn = start_pfn;
3707 if (temp_end_pfn > end_pfn)
3708 add_active_range(nid, end_pfn, temp_end_pfn);
3709 continue;
3710 }
3711 if (early_node_map[i].start_pfn >= start_pfn &&
3712 early_node_map[i].end_pfn > end_pfn &&
3713 early_node_map[i].start_pfn < end_pfn) {
3714 early_node_map[i].start_pfn = end_pfn;
cc1a9d86 3715 continue;
c713216d 3716 }
cc1a9d86
YL
3717 }
3718
3719 if (!removed)
3720 return;
3721
3722 /* remove the blank ones */
3723 for (i = nr_nodemap_entries - 1; i > 0; i--) {
3724 if (early_node_map[i].nid != nid)
3725 continue;
3726 if (early_node_map[i].end_pfn)
3727 continue;
3728 /* we found it, get rid of it */
3729 for (j = i; j < nr_nodemap_entries - 1; j++)
3730 memcpy(&early_node_map[j], &early_node_map[j+1],
3731 sizeof(early_node_map[j]));
3732 j = nr_nodemap_entries - 1;
3733 memset(&early_node_map[j], 0, sizeof(early_node_map[j]));
3734 nr_nodemap_entries--;
3735 }
c713216d
MG
3736}
3737
3738/**
3739 * remove_all_active_ranges - Remove all currently registered regions
88ca3b94 3740 *
c713216d
MG
3741 * During discovery, it may be found that a table like SRAT is invalid
3742 * and an alternative discovery method must be used. This function removes
3743 * all currently registered regions.
3744 */
88ca3b94 3745void __init remove_all_active_ranges(void)
c713216d
MG
3746{
3747 memset(early_node_map, 0, sizeof(early_node_map));
3748 nr_nodemap_entries = 0;
fb01439c
MG
3749#ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
3750 memset(node_boundary_start_pfn, 0, sizeof(node_boundary_start_pfn));
3751 memset(node_boundary_end_pfn, 0, sizeof(node_boundary_end_pfn));
3752#endif /* CONFIG_MEMORY_HOTPLUG_RESERVE */
c713216d
MG
3753}
3754
3755/* Compare two active node_active_regions */
3756static int __init cmp_node_active_region(const void *a, const void *b)
3757{
3758 struct node_active_region *arange = (struct node_active_region *)a;
3759 struct node_active_region *brange = (struct node_active_region *)b;
3760
3761 /* Done this way to avoid overflows */
3762 if (arange->start_pfn > brange->start_pfn)
3763 return 1;
3764 if (arange->start_pfn < brange->start_pfn)
3765 return -1;
3766
3767 return 0;
3768}
3769
3770/* sort the node_map by start_pfn */
3771static void __init sort_node_map(void)
3772{
3773 sort(early_node_map, (size_t)nr_nodemap_entries,
3774 sizeof(struct node_active_region),
3775 cmp_node_active_region, NULL);
3776}
3777
a6af2bc3 3778/* Find the lowest pfn for a node */
b69a7288 3779static unsigned long __init find_min_pfn_for_node(int nid)
c713216d
MG
3780{
3781 int i;
a6af2bc3 3782 unsigned long min_pfn = ULONG_MAX;
1abbfb41 3783
c713216d
MG
3784 /* Assuming a sorted map, the first range found has the starting pfn */
3785 for_each_active_range_index_in_nid(i, nid)
a6af2bc3 3786 min_pfn = min(min_pfn, early_node_map[i].start_pfn);
c713216d 3787
a6af2bc3
MG
3788 if (min_pfn == ULONG_MAX) {
3789 printk(KERN_WARNING
2bc0d261 3790 "Could not find start_pfn for node %d\n", nid);
a6af2bc3
MG
3791 return 0;
3792 }
3793
3794 return min_pfn;
c713216d
MG
3795}
3796
3797/**
3798 * find_min_pfn_with_active_regions - Find the minimum PFN registered
3799 *
3800 * It returns the minimum PFN based on information provided via
88ca3b94 3801 * add_active_range().
c713216d
MG
3802 */
3803unsigned long __init find_min_pfn_with_active_regions(void)
3804{
3805 return find_min_pfn_for_node(MAX_NUMNODES);
3806}
3807
37b07e41
LS
3808/*
3809 * early_calculate_totalpages()
3810 * Sum pages in active regions for movable zone.
3811 * Populate N_HIGH_MEMORY for calculating usable_nodes.
3812 */
484f51f8 3813static unsigned long __init early_calculate_totalpages(void)
7e63efef
MG
3814{
3815 int i;
3816 unsigned long totalpages = 0;
3817
37b07e41
LS
3818 for (i = 0; i < nr_nodemap_entries; i++) {
3819 unsigned long pages = early_node_map[i].end_pfn -
7e63efef 3820 early_node_map[i].start_pfn;
37b07e41
LS
3821 totalpages += pages;
3822 if (pages)
3823 node_set_state(early_node_map[i].nid, N_HIGH_MEMORY);
3824 }
3825 return totalpages;
7e63efef
MG
3826}
3827
2a1e274a
MG
3828/*
3829 * Find the PFN the Movable zone begins in each node. Kernel memory
3830 * is spread evenly between nodes as long as the nodes have enough
3831 * memory. When they don't, some nodes will have more kernelcore than
3832 * others
3833 */
b69a7288 3834static void __init find_zone_movable_pfns_for_nodes(unsigned long *movable_pfn)
2a1e274a
MG
3835{
3836 int i, nid;
3837 unsigned long usable_startpfn;
3838 unsigned long kernelcore_node, kernelcore_remaining;
37b07e41
LS
3839 unsigned long totalpages = early_calculate_totalpages();
3840 int usable_nodes = nodes_weight(node_states[N_HIGH_MEMORY]);
2a1e274a 3841
7e63efef
MG
3842 /*
3843 * If movablecore was specified, calculate what size of
3844 * kernelcore that corresponds so that memory usable for
3845 * any allocation type is evenly spread. If both kernelcore
3846 * and movablecore are specified, then the value of kernelcore
3847 * will be used for required_kernelcore if it's greater than
3848 * what movablecore would have allowed.
3849 */
3850 if (required_movablecore) {
7e63efef
MG
3851 unsigned long corepages;
3852
3853 /*
3854 * Round-up so that ZONE_MOVABLE is at least as large as what
3855 * was requested by the user
3856 */
3857 required_movablecore =
3858 roundup(required_movablecore, MAX_ORDER_NR_PAGES);
3859 corepages = totalpages - required_movablecore;
3860
3861 required_kernelcore = max(required_kernelcore, corepages);
3862 }
3863
2a1e274a
MG
3864 /* If kernelcore was not specified, there is no ZONE_MOVABLE */
3865 if (!required_kernelcore)
3866 return;
3867
3868 /* usable_startpfn is the lowest possible pfn ZONE_MOVABLE can be at */
3869 find_usable_zone_for_movable();
3870 usable_startpfn = arch_zone_lowest_possible_pfn[movable_zone];
3871
3872restart:
3873 /* Spread kernelcore memory as evenly as possible throughout nodes */
3874 kernelcore_node = required_kernelcore / usable_nodes;
37b07e41 3875 for_each_node_state(nid, N_HIGH_MEMORY) {
2a1e274a
MG
3876 /*
3877 * Recalculate kernelcore_node if the division per node
3878 * now exceeds what is necessary to satisfy the requested
3879 * amount of memory for the kernel
3880 */
3881 if (required_kernelcore < kernelcore_node)
3882 kernelcore_node = required_kernelcore / usable_nodes;
3883
3884 /*
3885 * As the map is walked, we track how much memory is usable
3886 * by the kernel using kernelcore_remaining. When it is
3887 * 0, the rest of the node is usable by ZONE_MOVABLE
3888 */
3889 kernelcore_remaining = kernelcore_node;
3890
3891 /* Go through each range of PFNs within this node */
3892 for_each_active_range_index_in_nid(i, nid) {
3893 unsigned long start_pfn, end_pfn;
3894 unsigned long size_pages;
3895
3896 start_pfn = max(early_node_map[i].start_pfn,
3897 zone_movable_pfn[nid]);
3898 end_pfn = early_node_map[i].end_pfn;
3899 if (start_pfn >= end_pfn)
3900 continue;
3901
3902 /* Account for what is only usable for kernelcore */
3903 if (start_pfn < usable_startpfn) {
3904 unsigned long kernel_pages;
3905 kernel_pages = min(end_pfn, usable_startpfn)
3906 - start_pfn;
3907
3908 kernelcore_remaining -= min(kernel_pages,
3909 kernelcore_remaining);
3910 required_kernelcore -= min(kernel_pages,
3911 required_kernelcore);
3912
3913 /* Continue if range is now fully accounted */
3914 if (end_pfn <= usable_startpfn) {
3915
3916 /*
3917 * Push zone_movable_pfn to the end so
3918 * that if we have to rebalance
3919 * kernelcore across nodes, we will
3920 * not double account here
3921 */
3922 zone_movable_pfn[nid] = end_pfn;
3923 continue;
3924 }
3925 start_pfn = usable_startpfn;
3926 }
3927
3928 /*
3929 * The usable PFN range for ZONE_MOVABLE is from
3930 * start_pfn->end_pfn. Calculate size_pages as the
3931 * number of pages used as kernelcore
3932 */
3933 size_pages = end_pfn - start_pfn;
3934 if (size_pages > kernelcore_remaining)
3935 size_pages = kernelcore_remaining;
3936 zone_movable_pfn[nid] = start_pfn + size_pages;
3937
3938 /*
3939 * Some kernelcore has been met, update counts and
3940 * break if the kernelcore for this node has been
3941 * satisified
3942 */
3943 required_kernelcore -= min(required_kernelcore,
3944 size_pages);
3945 kernelcore_remaining -= size_pages;
3946 if (!kernelcore_remaining)
3947 break;
3948 }
3949 }
3950
3951 /*
3952 * If there is still required_kernelcore, we do another pass with one
3953 * less node in the count. This will push zone_movable_pfn[nid] further
3954 * along on the nodes that still have memory until kernelcore is
3955 * satisified
3956 */
3957 usable_nodes--;
3958 if (usable_nodes && required_kernelcore > usable_nodes)
3959 goto restart;
3960
3961 /* Align start of ZONE_MOVABLE on all nids to MAX_ORDER_NR_PAGES */
3962 for (nid = 0; nid < MAX_NUMNODES; nid++)
3963 zone_movable_pfn[nid] =
3964 roundup(zone_movable_pfn[nid], MAX_ORDER_NR_PAGES);
3965}
3966
37b07e41
LS
3967/* Any regular memory on that node ? */
3968static void check_for_regular_memory(pg_data_t *pgdat)
3969{
3970#ifdef CONFIG_HIGHMEM
3971 enum zone_type zone_type;
3972
3973 for (zone_type = 0; zone_type <= ZONE_NORMAL; zone_type++) {
3974 struct zone *zone = &pgdat->node_zones[zone_type];
3975 if (zone->present_pages)
3976 node_set_state(zone_to_nid(zone), N_NORMAL_MEMORY);
3977 }
3978#endif
3979}
3980
c713216d
MG
3981/**
3982 * free_area_init_nodes - Initialise all pg_data_t and zone data
88ca3b94 3983 * @max_zone_pfn: an array of max PFNs for each zone
c713216d
MG
3984 *
3985 * This will call free_area_init_node() for each active node in the system.
3986 * Using the page ranges provided by add_active_range(), the size of each
3987 * zone in each node and their holes is calculated. If the maximum PFN
3988 * between two adjacent zones match, it is assumed that the zone is empty.
3989 * For example, if arch_max_dma_pfn == arch_max_dma32_pfn, it is assumed
3990 * that arch_max_dma32_pfn has no pages. It is also assumed that a zone
3991 * starts where the previous one ended. For example, ZONE_DMA32 starts
3992 * at arch_max_dma_pfn.
3993 */
3994void __init free_area_init_nodes(unsigned long *max_zone_pfn)
3995{
3996 unsigned long nid;
db99100d 3997 int i;
c713216d 3998
a6af2bc3
MG
3999 /* Sort early_node_map as initialisation assumes it is sorted */
4000 sort_node_map();
4001
c713216d
MG
4002 /* Record where the zone boundaries are */
4003 memset(arch_zone_lowest_possible_pfn, 0,
4004 sizeof(arch_zone_lowest_possible_pfn));
4005 memset(arch_zone_highest_possible_pfn, 0,
4006 sizeof(arch_zone_highest_possible_pfn));
4007 arch_zone_lowest_possible_pfn[0] = find_min_pfn_with_active_regions();
4008 arch_zone_highest_possible_pfn[0] = max_zone_pfn[0];
4009 for (i = 1; i < MAX_NR_ZONES; i++) {
2a1e274a
MG
4010 if (i == ZONE_MOVABLE)
4011 continue;
c713216d
MG
4012 arch_zone_lowest_possible_pfn[i] =
4013 arch_zone_highest_possible_pfn[i-1];
4014 arch_zone_highest_possible_pfn[i] =
4015 max(max_zone_pfn[i], arch_zone_lowest_possible_pfn[i]);
4016 }
2a1e274a
MG
4017 arch_zone_lowest_possible_pfn[ZONE_MOVABLE] = 0;
4018 arch_zone_highest_possible_pfn[ZONE_MOVABLE] = 0;
4019
4020 /* Find the PFNs that ZONE_MOVABLE begins at in each node */
4021 memset(zone_movable_pfn, 0, sizeof(zone_movable_pfn));
4022 find_zone_movable_pfns_for_nodes(zone_movable_pfn);
c713216d 4023
c713216d
MG
4024 /* Print out the zone ranges */
4025 printk("Zone PFN ranges:\n");
2a1e274a
MG
4026 for (i = 0; i < MAX_NR_ZONES; i++) {
4027 if (i == ZONE_MOVABLE)
4028 continue;
5dab8ec1 4029 printk(" %-8s %0#10lx -> %0#10lx\n",
c713216d
MG
4030 zone_names[i],
4031 arch_zone_lowest_possible_pfn[i],
4032 arch_zone_highest_possible_pfn[i]);
2a1e274a
MG
4033 }
4034
4035 /* Print out the PFNs ZONE_MOVABLE begins at in each node */
4036 printk("Movable zone start PFN for each node\n");
4037 for (i = 0; i < MAX_NUMNODES; i++) {
4038 if (zone_movable_pfn[i])
4039 printk(" Node %d: %lu\n", i, zone_movable_pfn[i]);
4040 }
c713216d
MG
4041
4042 /* Print out the early_node_map[] */
4043 printk("early_node_map[%d] active PFN ranges\n", nr_nodemap_entries);
4044 for (i = 0; i < nr_nodemap_entries; i++)
5dab8ec1 4045 printk(" %3d: %0#10lx -> %0#10lx\n", early_node_map[i].nid,
c713216d
MG
4046 early_node_map[i].start_pfn,
4047 early_node_map[i].end_pfn);
4048
4049 /* Initialise every node */
708614e6 4050 mminit_verify_pageflags_layout();
8ef82866 4051 setup_nr_node_ids();
c713216d
MG
4052 for_each_online_node(nid) {
4053 pg_data_t *pgdat = NODE_DATA(nid);
9109fb7b 4054 free_area_init_node(nid, NULL,
c713216d 4055 find_min_pfn_for_node(nid), NULL);
37b07e41
LS
4056
4057 /* Any memory on that node */
4058 if (pgdat->node_present_pages)
4059 node_set_state(nid, N_HIGH_MEMORY);
4060 check_for_regular_memory(pgdat);
c713216d
MG
4061 }
4062}
2a1e274a 4063
7e63efef 4064static int __init cmdline_parse_core(char *p, unsigned long *core)
2a1e274a
MG
4065{
4066 unsigned long long coremem;
4067 if (!p)
4068 return -EINVAL;
4069
4070 coremem = memparse(p, &p);
7e63efef 4071 *core = coremem >> PAGE_SHIFT;
2a1e274a 4072
7e63efef 4073 /* Paranoid check that UL is enough for the coremem value */
2a1e274a
MG
4074 WARN_ON((coremem >> PAGE_SHIFT) > ULONG_MAX);
4075
4076 return 0;
4077}
ed7ed365 4078
7e63efef
MG
4079/*
4080 * kernelcore=size sets the amount of memory for use for allocations that
4081 * cannot be reclaimed or migrated.
4082 */
4083static int __init cmdline_parse_kernelcore(char *p)
4084{
4085 return cmdline_parse_core(p, &required_kernelcore);
4086}
4087
4088/*
4089 * movablecore=size sets the amount of memory for use for allocations that
4090 * can be reclaimed or migrated.
4091 */
4092static int __init cmdline_parse_movablecore(char *p)
4093{
4094 return cmdline_parse_core(p, &required_movablecore);
4095}
4096
ed7ed365 4097early_param("kernelcore", cmdline_parse_kernelcore);
7e63efef 4098early_param("movablecore", cmdline_parse_movablecore);
ed7ed365 4099
c713216d
MG
4100#endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
4101
0e0b864e 4102/**
88ca3b94
RD
4103 * set_dma_reserve - set the specified number of pages reserved in the first zone
4104 * @new_dma_reserve: The number of pages to mark reserved
0e0b864e
MG
4105 *
4106 * The per-cpu batchsize and zone watermarks are determined by present_pages.
4107 * In the DMA zone, a significant percentage may be consumed by kernel image
4108 * and other unfreeable allocations which can skew the watermarks badly. This
88ca3b94
RD
4109 * function may optionally be used to account for unfreeable pages in the
4110 * first zone (e.g., ZONE_DMA). The effect will be lower watermarks and
4111 * smaller per-cpu batchsize.
0e0b864e
MG
4112 */
4113void __init set_dma_reserve(unsigned long new_dma_reserve)
4114{
4115 dma_reserve = new_dma_reserve;
4116}
4117
93b7504e 4118#ifndef CONFIG_NEED_MULTIPLE_NODES
52765583 4119struct pglist_data __refdata contig_page_data = { .bdata = &bootmem_node_data[0] };
1da177e4 4120EXPORT_SYMBOL(contig_page_data);
93b7504e 4121#endif
1da177e4
LT
4122
4123void __init free_area_init(unsigned long *zones_size)
4124{
9109fb7b 4125 free_area_init_node(0, zones_size,
1da177e4
LT
4126 __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
4127}
1da177e4 4128
1da177e4
LT
4129static int page_alloc_cpu_notify(struct notifier_block *self,
4130 unsigned long action, void *hcpu)
4131{
4132 int cpu = (unsigned long)hcpu;
1da177e4 4133
8bb78442 4134 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
9f8f2172
CL
4135 drain_pages(cpu);
4136
4137 /*
4138 * Spill the event counters of the dead processor
4139 * into the current processors event counters.
4140 * This artificially elevates the count of the current
4141 * processor.
4142 */
f8891e5e 4143 vm_events_fold_cpu(cpu);
9f8f2172
CL
4144
4145 /*
4146 * Zero the differential counters of the dead processor
4147 * so that the vm statistics are consistent.
4148 *
4149 * This is only okay since the processor is dead and cannot
4150 * race with what we are doing.
4151 */
2244b95a 4152 refresh_cpu_vm_stats(cpu);
1da177e4
LT
4153 }
4154 return NOTIFY_OK;
4155}
1da177e4
LT
4156
4157void __init page_alloc_init(void)
4158{
4159 hotcpu_notifier(page_alloc_cpu_notify, 0);
4160}
4161
cb45b0e9
HA
4162/*
4163 * calculate_totalreserve_pages - called when sysctl_lower_zone_reserve_ratio
4164 * or min_free_kbytes changes.
4165 */
4166static void calculate_totalreserve_pages(void)
4167{
4168 struct pglist_data *pgdat;
4169 unsigned long reserve_pages = 0;
2f6726e5 4170 enum zone_type i, j;
cb45b0e9
HA
4171
4172 for_each_online_pgdat(pgdat) {
4173 for (i = 0; i < MAX_NR_ZONES; i++) {
4174 struct zone *zone = pgdat->node_zones + i;
4175 unsigned long max = 0;
4176
4177 /* Find valid and maximum lowmem_reserve in the zone */
4178 for (j = i; j < MAX_NR_ZONES; j++) {
4179 if (zone->lowmem_reserve[j] > max)
4180 max = zone->lowmem_reserve[j];
4181 }
4182
4183 /* we treat pages_high as reserved pages. */
4184 max += zone->pages_high;
4185
4186 if (max > zone->present_pages)
4187 max = zone->present_pages;
4188 reserve_pages += max;
4189 }
4190 }
4191 totalreserve_pages = reserve_pages;
4192}
4193
1da177e4
LT
4194/*
4195 * setup_per_zone_lowmem_reserve - called whenever
4196 * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
4197 * has a correct pages reserved value, so an adequate number of
4198 * pages are left in the zone after a successful __alloc_pages().
4199 */
4200static void setup_per_zone_lowmem_reserve(void)
4201{
4202 struct pglist_data *pgdat;
2f6726e5 4203 enum zone_type j, idx;
1da177e4 4204
ec936fc5 4205 for_each_online_pgdat(pgdat) {
1da177e4
LT
4206 for (j = 0; j < MAX_NR_ZONES; j++) {
4207 struct zone *zone = pgdat->node_zones + j;
4208 unsigned long present_pages = zone->present_pages;
4209
4210 zone->lowmem_reserve[j] = 0;
4211
2f6726e5
CL
4212 idx = j;
4213 while (idx) {
1da177e4
LT
4214 struct zone *lower_zone;
4215
2f6726e5
CL
4216 idx--;
4217
1da177e4
LT
4218 if (sysctl_lowmem_reserve_ratio[idx] < 1)
4219 sysctl_lowmem_reserve_ratio[idx] = 1;
4220
4221 lower_zone = pgdat->node_zones + idx;
4222 lower_zone->lowmem_reserve[j] = present_pages /
4223 sysctl_lowmem_reserve_ratio[idx];
4224 present_pages += lower_zone->present_pages;
4225 }
4226 }
4227 }
cb45b0e9
HA
4228
4229 /* update totalreserve_pages */
4230 calculate_totalreserve_pages();
1da177e4
LT
4231}
4232
88ca3b94
RD
4233/**
4234 * setup_per_zone_pages_min - called when min_free_kbytes changes.
4235 *
4236 * Ensures that the pages_{min,low,high} values for each zone are set correctly
4237 * with respect to min_free_kbytes.
1da177e4 4238 */
3947be19 4239void setup_per_zone_pages_min(void)
1da177e4
LT
4240{
4241 unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
4242 unsigned long lowmem_pages = 0;
4243 struct zone *zone;
4244 unsigned long flags;
4245
4246 /* Calculate total number of !ZONE_HIGHMEM pages */
4247 for_each_zone(zone) {
4248 if (!is_highmem(zone))
4249 lowmem_pages += zone->present_pages;
4250 }
4251
4252 for_each_zone(zone) {
ac924c60
AM
4253 u64 tmp;
4254
1125b4e3 4255 spin_lock_irqsave(&zone->lock, flags);
ac924c60
AM
4256 tmp = (u64)pages_min * zone->present_pages;
4257 do_div(tmp, lowmem_pages);
1da177e4
LT
4258 if (is_highmem(zone)) {
4259 /*
669ed175
NP
4260 * __GFP_HIGH and PF_MEMALLOC allocations usually don't
4261 * need highmem pages, so cap pages_min to a small
4262 * value here.
4263 *
4264 * The (pages_high-pages_low) and (pages_low-pages_min)
4265 * deltas controls asynch page reclaim, and so should
4266 * not be capped for highmem.
1da177e4
LT
4267 */
4268 int min_pages;
4269
4270 min_pages = zone->present_pages / 1024;
4271 if (min_pages < SWAP_CLUSTER_MAX)
4272 min_pages = SWAP_CLUSTER_MAX;
4273 if (min_pages > 128)
4274 min_pages = 128;
4275 zone->pages_min = min_pages;
4276 } else {
669ed175
NP
4277 /*
4278 * If it's a lowmem zone, reserve a number of pages
1da177e4
LT
4279 * proportionate to the zone's size.
4280 */
669ed175 4281 zone->pages_min = tmp;
1da177e4
LT
4282 }
4283
ac924c60
AM
4284 zone->pages_low = zone->pages_min + (tmp >> 2);
4285 zone->pages_high = zone->pages_min + (tmp >> 1);
56fd56b8 4286 setup_zone_migrate_reserve(zone);
1125b4e3 4287 spin_unlock_irqrestore(&zone->lock, flags);
1da177e4 4288 }
cb45b0e9
HA
4289
4290 /* update totalreserve_pages */
4291 calculate_totalreserve_pages();
1da177e4
LT
4292}
4293
556adecb
RR
4294/**
4295 * setup_per_zone_inactive_ratio - called when min_free_kbytes changes.
4296 *
4297 * The inactive anon list should be small enough that the VM never has to
4298 * do too much work, but large enough that each inactive page has a chance
4299 * to be referenced again before it is swapped out.
4300 *
4301 * The inactive_anon ratio is the target ratio of ACTIVE_ANON to
4302 * INACTIVE_ANON pages on this zone's LRU, maintained by the
4303 * pageout code. A zone->inactive_ratio of 3 means 3:1 or 25% of
4304 * the anonymous pages are kept on the inactive list.
4305 *
4306 * total target max
4307 * memory ratio inactive anon
4308 * -------------------------------------
4309 * 10MB 1 5MB
4310 * 100MB 1 50MB
4311 * 1GB 3 250MB
4312 * 10GB 10 0.9GB
4313 * 100GB 31 3GB
4314 * 1TB 101 10GB
4315 * 10TB 320 32GB
4316 */
4317void setup_per_zone_inactive_ratio(void)
4318{
4319 struct zone *zone;
4320
4321 for_each_zone(zone) {
4322 unsigned int gb, ratio;
4323
4324 /* Zone size in gigabytes */
4325 gb = zone->present_pages >> (30 - PAGE_SHIFT);
4326 ratio = int_sqrt(10 * gb);
4327 if (!ratio)
4328 ratio = 1;
4329
4330 zone->inactive_ratio = ratio;
4331 }
4332}
4333
1da177e4
LT
4334/*
4335 * Initialise min_free_kbytes.
4336 *
4337 * For small machines we want it small (128k min). For large machines
4338 * we want it large (64MB max). But it is not linear, because network
4339 * bandwidth does not increase linearly with machine size. We use
4340 *
4341 * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
4342 * min_free_kbytes = sqrt(lowmem_kbytes * 16)
4343 *
4344 * which yields
4345 *
4346 * 16MB: 512k
4347 * 32MB: 724k
4348 * 64MB: 1024k
4349 * 128MB: 1448k
4350 * 256MB: 2048k
4351 * 512MB: 2896k
4352 * 1024MB: 4096k
4353 * 2048MB: 5792k
4354 * 4096MB: 8192k
4355 * 8192MB: 11584k
4356 * 16384MB: 16384k
4357 */
4358static int __init init_per_zone_pages_min(void)
4359{
4360 unsigned long lowmem_kbytes;
4361
4362 lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
4363
4364 min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
4365 if (min_free_kbytes < 128)
4366 min_free_kbytes = 128;
4367 if (min_free_kbytes > 65536)
4368 min_free_kbytes = 65536;
4369 setup_per_zone_pages_min();
4370 setup_per_zone_lowmem_reserve();
556adecb 4371 setup_per_zone_inactive_ratio();
1da177e4
LT
4372 return 0;
4373}
4374module_init(init_per_zone_pages_min)
4375
4376/*
4377 * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
4378 * that we can call two helper functions whenever min_free_kbytes
4379 * changes.
4380 */
4381int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
4382 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
4383{
4384 proc_dointvec(table, write, file, buffer, length, ppos);
3b1d92c5
MG
4385 if (write)
4386 setup_per_zone_pages_min();
1da177e4
LT
4387 return 0;
4388}
4389
9614634f
CL
4390#ifdef CONFIG_NUMA
4391int sysctl_min_unmapped_ratio_sysctl_handler(ctl_table *table, int write,
4392 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
4393{
4394 struct zone *zone;
4395 int rc;
4396
4397 rc = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
4398 if (rc)
4399 return rc;
4400
4401 for_each_zone(zone)
8417bba4 4402 zone->min_unmapped_pages = (zone->present_pages *
9614634f
CL
4403 sysctl_min_unmapped_ratio) / 100;
4404 return 0;
4405}
0ff38490
CL
4406
4407int sysctl_min_slab_ratio_sysctl_handler(ctl_table *table, int write,
4408 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
4409{
4410 struct zone *zone;
4411 int rc;
4412
4413 rc = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
4414 if (rc)
4415 return rc;
4416
4417 for_each_zone(zone)
4418 zone->min_slab_pages = (zone->present_pages *
4419 sysctl_min_slab_ratio) / 100;
4420 return 0;
4421}
9614634f
CL
4422#endif
4423
1da177e4
LT
4424/*
4425 * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
4426 * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
4427 * whenever sysctl_lowmem_reserve_ratio changes.
4428 *
4429 * The reserve ratio obviously has absolutely no relation with the
4430 * pages_min watermarks. The lowmem reserve ratio can only make sense
4431 * if in function of the boot time zone sizes.
4432 */
4433int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
4434 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
4435{
4436 proc_dointvec_minmax(table, write, file, buffer, length, ppos);
4437 setup_per_zone_lowmem_reserve();
4438 return 0;
4439}
4440
8ad4b1fb
RS
4441/*
4442 * percpu_pagelist_fraction - changes the pcp->high for each zone on each
4443 * cpu. It is the fraction of total pages in each zone that a hot per cpu pagelist
4444 * can have before it gets flushed back to buddy allocator.
4445 */
4446
4447int percpu_pagelist_fraction_sysctl_handler(ctl_table *table, int write,
4448 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
4449{
4450 struct zone *zone;
4451 unsigned int cpu;
4452 int ret;
4453
4454 ret = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
4455 if (!write || (ret == -EINVAL))
4456 return ret;
4457 for_each_zone(zone) {
4458 for_each_online_cpu(cpu) {
4459 unsigned long high;
4460 high = zone->present_pages / percpu_pagelist_fraction;
4461 setup_pagelist_highmark(zone_pcp(zone, cpu), high);
4462 }
4463 }
4464 return 0;
4465}
4466
f034b5d4 4467int hashdist = HASHDIST_DEFAULT;
1da177e4
LT
4468
4469#ifdef CONFIG_NUMA
4470static int __init set_hashdist(char *str)
4471{
4472 if (!str)
4473 return 0;
4474 hashdist = simple_strtoul(str, &str, 0);
4475 return 1;
4476}
4477__setup("hashdist=", set_hashdist);
4478#endif
4479
4480/*
4481 * allocate a large system hash table from bootmem
4482 * - it is assumed that the hash table must contain an exact power-of-2
4483 * quantity of entries
4484 * - limit is the number of hash buckets, not the total allocation size
4485 */
4486void *__init alloc_large_system_hash(const char *tablename,
4487 unsigned long bucketsize,
4488 unsigned long numentries,
4489 int scale,
4490 int flags,
4491 unsigned int *_hash_shift,
4492 unsigned int *_hash_mask,
4493 unsigned long limit)
4494{
4495 unsigned long long max = limit;
4496 unsigned long log2qty, size;
4497 void *table = NULL;
4498
4499 /* allow the kernel cmdline to have a say */
4500 if (!numentries) {
4501 /* round applicable memory size up to nearest megabyte */
04903664 4502 numentries = nr_kernel_pages;
1da177e4
LT
4503 numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
4504 numentries >>= 20 - PAGE_SHIFT;
4505 numentries <<= 20 - PAGE_SHIFT;
4506
4507 /* limit to 1 bucket per 2^scale bytes of low memory */
4508 if (scale > PAGE_SHIFT)
4509 numentries >>= (scale - PAGE_SHIFT);
4510 else
4511 numentries <<= (PAGE_SHIFT - scale);
9ab37b8f
PM
4512
4513 /* Make sure we've got at least a 0-order allocation.. */
4514 if (unlikely((numentries * bucketsize) < PAGE_SIZE))
4515 numentries = PAGE_SIZE / bucketsize;
1da177e4 4516 }
6e692ed3 4517 numentries = roundup_pow_of_two(numentries);
1da177e4
LT
4518
4519 /* limit allocation size to 1/16 total memory by default */
4520 if (max == 0) {
4521 max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
4522 do_div(max, bucketsize);
4523 }
4524
4525 if (numentries > max)
4526 numentries = max;
4527
f0d1b0b3 4528 log2qty = ilog2(numentries);
1da177e4
LT
4529
4530 do {
4531 size = bucketsize << log2qty;
4532 if (flags & HASH_EARLY)
74768ed8 4533 table = alloc_bootmem_nopanic(size);
1da177e4
LT
4534 else if (hashdist)
4535 table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
4536 else {
2309f9e6 4537 unsigned long order = get_order(size);
1da177e4 4538 table = (void*) __get_free_pages(GFP_ATOMIC, order);
1037b83b
ED
4539 /*
4540 * If bucketsize is not a power-of-two, we may free
4541 * some pages at the end of hash table.
4542 */
4543 if (table) {
4544 unsigned long alloc_end = (unsigned long)table +
4545 (PAGE_SIZE << order);
4546 unsigned long used = (unsigned long)table +
4547 PAGE_ALIGN(size);
4548 split_page(virt_to_page(table), order);
4549 while (used < alloc_end) {
4550 free_page(used);
4551 used += PAGE_SIZE;
4552 }
4553 }
1da177e4
LT
4554 }
4555 } while (!table && size > PAGE_SIZE && --log2qty);
4556
4557 if (!table)
4558 panic("Failed to allocate %s hash table\n", tablename);
4559
b49ad484 4560 printk(KERN_INFO "%s hash table entries: %d (order: %d, %lu bytes)\n",
1da177e4
LT
4561 tablename,
4562 (1U << log2qty),
f0d1b0b3 4563 ilog2(size) - PAGE_SHIFT,
1da177e4
LT
4564 size);
4565
4566 if (_hash_shift)
4567 *_hash_shift = log2qty;
4568 if (_hash_mask)
4569 *_hash_mask = (1 << log2qty) - 1;
4570
4571 return table;
4572}
a117e66e
KH
4573
4574#ifdef CONFIG_OUT_OF_LINE_PFN_TO_PAGE
a117e66e
KH
4575struct page *pfn_to_page(unsigned long pfn)
4576{
67de6482 4577 return __pfn_to_page(pfn);
a117e66e
KH
4578}
4579unsigned long page_to_pfn(struct page *page)
4580{
67de6482 4581 return __page_to_pfn(page);
a117e66e 4582}
a117e66e
KH
4583EXPORT_SYMBOL(pfn_to_page);
4584EXPORT_SYMBOL(page_to_pfn);
4585#endif /* CONFIG_OUT_OF_LINE_PFN_TO_PAGE */
6220ec78 4586
835c134e
MG
4587/* Return a pointer to the bitmap storing bits affecting a block of pages */
4588static inline unsigned long *get_pageblock_bitmap(struct zone *zone,
4589 unsigned long pfn)
4590{
4591#ifdef CONFIG_SPARSEMEM
4592 return __pfn_to_section(pfn)->pageblock_flags;
4593#else
4594 return zone->pageblock_flags;
4595#endif /* CONFIG_SPARSEMEM */
4596}
4597
4598static inline int pfn_to_bitidx(struct zone *zone, unsigned long pfn)
4599{
4600#ifdef CONFIG_SPARSEMEM
4601 pfn &= (PAGES_PER_SECTION-1);
d9c23400 4602 return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
835c134e
MG
4603#else
4604 pfn = pfn - zone->zone_start_pfn;
d9c23400 4605 return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
835c134e
MG
4606#endif /* CONFIG_SPARSEMEM */
4607}
4608
4609/**
d9c23400 4610 * get_pageblock_flags_group - Return the requested group of flags for the pageblock_nr_pages block of pages
835c134e
MG
4611 * @page: The page within the block of interest
4612 * @start_bitidx: The first bit of interest to retrieve
4613 * @end_bitidx: The last bit of interest
4614 * returns pageblock_bits flags
4615 */
4616unsigned long get_pageblock_flags_group(struct page *page,
4617 int start_bitidx, int end_bitidx)
4618{
4619 struct zone *zone;
4620 unsigned long *bitmap;
4621 unsigned long pfn, bitidx;
4622 unsigned long flags = 0;
4623 unsigned long value = 1;
4624
4625 zone = page_zone(page);
4626 pfn = page_to_pfn(page);
4627 bitmap = get_pageblock_bitmap(zone, pfn);
4628 bitidx = pfn_to_bitidx(zone, pfn);
4629
4630 for (; start_bitidx <= end_bitidx; start_bitidx++, value <<= 1)
4631 if (test_bit(bitidx + start_bitidx, bitmap))
4632 flags |= value;
6220ec78 4633
835c134e
MG
4634 return flags;
4635}
4636
4637/**
d9c23400 4638 * set_pageblock_flags_group - Set the requested group of flags for a pageblock_nr_pages block of pages
835c134e
MG
4639 * @page: The page within the block of interest
4640 * @start_bitidx: The first bit of interest
4641 * @end_bitidx: The last bit of interest
4642 * @flags: The flags to set
4643 */
4644void set_pageblock_flags_group(struct page *page, unsigned long flags,
4645 int start_bitidx, int end_bitidx)
4646{
4647 struct zone *zone;
4648 unsigned long *bitmap;
4649 unsigned long pfn, bitidx;
4650 unsigned long value = 1;
4651
4652 zone = page_zone(page);
4653 pfn = page_to_pfn(page);
4654 bitmap = get_pageblock_bitmap(zone, pfn);
4655 bitidx = pfn_to_bitidx(zone, pfn);
86051ca5
KH
4656 VM_BUG_ON(pfn < zone->zone_start_pfn);
4657 VM_BUG_ON(pfn >= zone->zone_start_pfn + zone->spanned_pages);
835c134e
MG
4658
4659 for (; start_bitidx <= end_bitidx; start_bitidx++, value <<= 1)
4660 if (flags & value)
4661 __set_bit(bitidx + start_bitidx, bitmap);
4662 else
4663 __clear_bit(bitidx + start_bitidx, bitmap);
4664}
a5d76b54
KH
4665
4666/*
4667 * This is designed as sub function...plz see page_isolation.c also.
4668 * set/clear page block's type to be ISOLATE.
4669 * page allocater never alloc memory from ISOLATE block.
4670 */
4671
4672int set_migratetype_isolate(struct page *page)
4673{
4674 struct zone *zone;
4675 unsigned long flags;
4676 int ret = -EBUSY;
4677
4678 zone = page_zone(page);
4679 spin_lock_irqsave(&zone->lock, flags);
4680 /*
4681 * In future, more migrate types will be able to be isolation target.
4682 */
4683 if (get_pageblock_migratetype(page) != MIGRATE_MOVABLE)
4684 goto out;
4685 set_pageblock_migratetype(page, MIGRATE_ISOLATE);
4686 move_freepages_block(zone, page, MIGRATE_ISOLATE);
4687 ret = 0;
4688out:
4689 spin_unlock_irqrestore(&zone->lock, flags);
4690 if (!ret)
9f8f2172 4691 drain_all_pages();
a5d76b54
KH
4692 return ret;
4693}
4694
4695void unset_migratetype_isolate(struct page *page)
4696{
4697 struct zone *zone;
4698 unsigned long flags;
4699 zone = page_zone(page);
4700 spin_lock_irqsave(&zone->lock, flags);
4701 if (get_pageblock_migratetype(page) != MIGRATE_ISOLATE)
4702 goto out;
4703 set_pageblock_migratetype(page, MIGRATE_MOVABLE);
4704 move_freepages_block(zone, page, MIGRATE_MOVABLE);
4705out:
4706 spin_unlock_irqrestore(&zone->lock, flags);
4707}
0c0e6195
KH
4708
4709#ifdef CONFIG_MEMORY_HOTREMOVE
4710/*
4711 * All pages in the range must be isolated before calling this.
4712 */
4713void
4714__offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
4715{
4716 struct page *page;
4717 struct zone *zone;
4718 int order, i;
4719 unsigned long pfn;
4720 unsigned long flags;
4721 /* find the first valid pfn */
4722 for (pfn = start_pfn; pfn < end_pfn; pfn++)
4723 if (pfn_valid(pfn))
4724 break;
4725 if (pfn == end_pfn)
4726 return;
4727 zone = page_zone(pfn_to_page(pfn));
4728 spin_lock_irqsave(&zone->lock, flags);
4729 pfn = start_pfn;
4730 while (pfn < end_pfn) {
4731 if (!pfn_valid(pfn)) {
4732 pfn++;
4733 continue;
4734 }
4735 page = pfn_to_page(pfn);
4736 BUG_ON(page_count(page));
4737 BUG_ON(!PageBuddy(page));
4738 order = page_order(page);
4739#ifdef CONFIG_DEBUG_VM
4740 printk(KERN_INFO "remove from free list %lx %d %lx\n",
4741 pfn, 1 << order, end_pfn);
4742#endif
4743 list_del(&page->lru);
4744 rmv_page_order(page);
4745 zone->free_area[order].nr_free--;
4746 __mod_zone_page_state(zone, NR_FREE_PAGES,
4747 - (1UL << order));
4748 for (i = 0; i < (1 << order); i++)
4749 SetPageReserved((page+i));
4750 pfn += (1 << order);
4751 }
4752 spin_unlock_irqrestore(&zone->lock, flags);
4753}
4754#endif