Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / mm / page_alloc.c
blob84b10a2ad6575e3c0aa6e31c428472949f9cf5fd
1 /*
2 * linux/mm/page_alloc.c
4 * Manages the free list, the system allocates free pages here.
5 * Note that kmalloc() lives in slab.c
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)
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>
22 <<<<<<< HEAD:mm/page_alloc.c
23 =======
24 #include <linux/jiffies.h>
25 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:mm/page_alloc.c
26 #include <linux/bootmem.h>
27 #include <linux/compiler.h>
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/suspend.h>
31 #include <linux/pagevec.h>
32 #include <linux/blkdev.h>
33 #include <linux/slab.h>
34 #include <linux/oom.h>
35 #include <linux/notifier.h>
36 #include <linux/topology.h>
37 #include <linux/sysctl.h>
38 #include <linux/cpu.h>
39 #include <linux/cpuset.h>
40 #include <linux/memory_hotplug.h>
41 #include <linux/nodemask.h>
42 #include <linux/vmalloc.h>
43 #include <linux/mempolicy.h>
44 #include <linux/stop_machine.h>
45 #include <linux/sort.h>
46 #include <linux/pfn.h>
47 #include <linux/backing-dev.h>
48 #include <linux/fault-inject.h>
49 #include <linux/page-isolation.h>
50 #include <linux/memcontrol.h>
52 #include <asm/tlbflush.h>
53 #include <asm/div64.h>
54 #include "internal.h"
57 * Array of node states.
59 nodemask_t node_states[NR_NODE_STATES] __read_mostly = {
60 [N_POSSIBLE] = NODE_MASK_ALL,
61 [N_ONLINE] = { { [0] = 1UL } },
62 #ifndef CONFIG_NUMA
63 [N_NORMAL_MEMORY] = { { [0] = 1UL } },
64 #ifdef CONFIG_HIGHMEM
65 [N_HIGH_MEMORY] = { { [0] = 1UL } },
66 #endif
67 [N_CPU] = { { [0] = 1UL } },
68 #endif /* NUMA */
70 EXPORT_SYMBOL(node_states);
72 unsigned long totalram_pages __read_mostly;
73 unsigned long totalreserve_pages __read_mostly;
74 long nr_swap_pages;
75 int percpu_pagelist_fraction;
77 #ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
78 int pageblock_order __read_mostly;
79 #endif
81 static void __free_pages_ok(struct page *page, unsigned int order);
84 * results with 256, 32 in the lowmem_reserve sysctl:
85 * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
86 * 1G machine -> (16M dma, 784M normal, 224M high)
87 * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
88 * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
89 * HIGHMEM allocation will (224M+784M)/256 of ram reserved in ZONE_DMA
91 * TBD: should special case ZONE_DMA32 machines here - in those we normally
92 * don't need any ZONE_NORMAL reservation
94 int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1] = {
95 #ifdef CONFIG_ZONE_DMA
96 256,
97 #endif
98 #ifdef CONFIG_ZONE_DMA32
99 256,
100 #endif
101 #ifdef CONFIG_HIGHMEM
103 #endif
107 EXPORT_SYMBOL(totalram_pages);
109 static char * const zone_names[MAX_NR_ZONES] = {
110 #ifdef CONFIG_ZONE_DMA
111 "DMA",
112 #endif
113 #ifdef CONFIG_ZONE_DMA32
114 "DMA32",
115 #endif
116 "Normal",
117 #ifdef CONFIG_HIGHMEM
118 "HighMem",
119 #endif
120 "Movable",
123 int min_free_kbytes = 1024;
125 unsigned long __meminitdata nr_kernel_pages;
126 unsigned long __meminitdata nr_all_pages;
127 static unsigned long __meminitdata dma_reserve;
129 #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
131 * MAX_ACTIVE_REGIONS determines the maximum number of distinct
132 * ranges of memory (RAM) that may be registered with add_active_range().
133 * Ranges passed to add_active_range() will be merged if possible
134 * so the number of times add_active_range() can be called is
135 * related to the number of nodes and the number of holes
137 #ifdef CONFIG_MAX_ACTIVE_REGIONS
138 /* Allow an architecture to set MAX_ACTIVE_REGIONS to save memory */
139 #define MAX_ACTIVE_REGIONS CONFIG_MAX_ACTIVE_REGIONS
140 #else
141 #if MAX_NUMNODES >= 32
142 /* If there can be many nodes, allow up to 50 holes per node */
143 #define MAX_ACTIVE_REGIONS (MAX_NUMNODES*50)
144 #else
145 /* By default, allow up to 256 distinct regions */
146 #define MAX_ACTIVE_REGIONS 256
147 #endif
148 #endif
150 static struct node_active_region __meminitdata early_node_map[MAX_ACTIVE_REGIONS];
151 static int __meminitdata nr_nodemap_entries;
152 static unsigned long __meminitdata arch_zone_lowest_possible_pfn[MAX_NR_ZONES];
153 static unsigned long __meminitdata arch_zone_highest_possible_pfn[MAX_NR_ZONES];
154 #ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
155 static unsigned long __meminitdata node_boundary_start_pfn[MAX_NUMNODES];
156 static unsigned long __meminitdata node_boundary_end_pfn[MAX_NUMNODES];
157 #endif /* CONFIG_MEMORY_HOTPLUG_RESERVE */
158 unsigned long __initdata required_kernelcore;
159 static unsigned long __initdata required_movablecore;
160 unsigned long __meminitdata zone_movable_pfn[MAX_NUMNODES];
162 /* movable_zone is the "real" zone pages in ZONE_MOVABLE are taken from */
163 int movable_zone;
164 EXPORT_SYMBOL(movable_zone);
165 #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
167 #if MAX_NUMNODES > 1
168 int nr_node_ids __read_mostly = MAX_NUMNODES;
169 EXPORT_SYMBOL(nr_node_ids);
170 #endif
172 int page_group_by_mobility_disabled __read_mostly;
174 static void set_pageblock_migratetype(struct page *page, int migratetype)
176 set_pageblock_flags_group(page, (unsigned long)migratetype,
177 PB_migrate, PB_migrate_end);
180 #ifdef CONFIG_DEBUG_VM
181 static int page_outside_zone_boundaries(struct zone *zone, struct page *page)
183 int ret = 0;
184 unsigned seq;
185 unsigned long pfn = page_to_pfn(page);
187 do {
188 seq = zone_span_seqbegin(zone);
189 if (pfn >= zone->zone_start_pfn + zone->spanned_pages)
190 ret = 1;
191 else if (pfn < zone->zone_start_pfn)
192 ret = 1;
193 } while (zone_span_seqretry(zone, seq));
195 return ret;
198 static int page_is_consistent(struct zone *zone, struct page *page)
200 if (!pfn_valid_within(page_to_pfn(page)))
201 return 0;
202 if (zone != page_zone(page))
203 return 0;
205 return 1;
208 * Temporary debugging check for pages not lying within a given zone.
210 static int bad_range(struct zone *zone, struct page *page)
212 if (page_outside_zone_boundaries(zone, page))
213 return 1;
214 if (!page_is_consistent(zone, page))
215 return 1;
217 return 0;
219 #else
220 static inline int bad_range(struct zone *zone, struct page *page)
222 return 0;
224 #endif
226 static void bad_page(struct page *page)
228 <<<<<<< HEAD:mm/page_alloc.c
229 printk(KERN_EMERG "Bad page state in process '%s'\n"
230 KERN_EMERG "page:%p flags:0x%0*lx mapping:%p mapcount:%d count:%d\n"
231 KERN_EMERG "Trying to fix it up, but a reboot is needed\n"
232 KERN_EMERG "Backtrace:\n",
233 =======
234 void *pc = page_get_page_cgroup(page);
236 printk(KERN_EMERG "Bad page state in process '%s'\n" KERN_EMERG
237 "page:%p flags:0x%0*lx mapping:%p mapcount:%d count:%d\n",
238 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:mm/page_alloc.c
239 current->comm, page, (int)(2*sizeof(unsigned long)),
240 (unsigned long)page->flags, page->mapping,
241 page_mapcount(page), page_count(page));
242 <<<<<<< HEAD:mm/page_alloc.c
243 =======
244 if (pc) {
245 printk(KERN_EMERG "cgroup:%p\n", pc);
246 page_reset_bad_cgroup(page);
248 printk(KERN_EMERG "Trying to fix it up, but a reboot is needed\n"
249 KERN_EMERG "Backtrace:\n");
250 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:mm/page_alloc.c
251 dump_stack();
252 page->flags &= ~(1 << PG_lru |
253 1 << PG_private |
254 1 << PG_locked |
255 1 << PG_active |
256 1 << PG_dirty |
257 1 << PG_reclaim |
258 1 << PG_slab |
259 1 << PG_swapcache |
260 1 << PG_writeback |
261 1 << PG_buddy );
262 set_page_count(page, 0);
263 reset_page_mapcount(page);
264 page->mapping = NULL;
265 add_taint(TAINT_BAD_PAGE);
269 * Higher-order pages are called "compound pages". They are structured thusly:
271 * The first PAGE_SIZE page is called the "head page".
273 * The remaining PAGE_SIZE pages are called "tail pages".
275 * All pages have PG_compound set. All pages have their ->private pointing at
276 * the head page (even the head page has this).
278 * The first tail page's ->lru.next holds the address of the compound page's
279 * put_page() function. Its ->lru.prev holds the order of allocation.
280 * This usage means that zero-order pages may not be compound.
283 static void free_compound_page(struct page *page)
285 __free_pages_ok(page, compound_order(page));
288 static void prep_compound_page(struct page *page, unsigned long order)
290 int i;
291 int nr_pages = 1 << order;
293 set_compound_page_dtor(page, free_compound_page);
294 set_compound_order(page, order);
295 __SetPageHead(page);
296 for (i = 1; i < nr_pages; i++) {
297 struct page *p = page + i;
299 __SetPageTail(p);
300 p->first_page = page;
304 static void destroy_compound_page(struct page *page, unsigned long order)
306 int i;
307 int nr_pages = 1 << order;
309 if (unlikely(compound_order(page) != order))
310 bad_page(page);
312 if (unlikely(!PageHead(page)))
313 bad_page(page);
314 __ClearPageHead(page);
315 for (i = 1; i < nr_pages; i++) {
316 struct page *p = page + i;
318 if (unlikely(!PageTail(p) |
319 (p->first_page != page)))
320 bad_page(page);
321 __ClearPageTail(p);
325 static inline void prep_zero_page(struct page *page, int order, gfp_t gfp_flags)
327 int i;
330 * clear_highpage() will use KM_USER0, so it's a bug to use __GFP_ZERO
331 * and __GFP_HIGHMEM from hard or soft interrupt context.
333 VM_BUG_ON((gfp_flags & __GFP_HIGHMEM) && in_interrupt());
334 for (i = 0; i < (1 << order); i++)
335 clear_highpage(page + i);
338 static inline void set_page_order(struct page *page, int order)
340 set_page_private(page, order);
341 __SetPageBuddy(page);
344 static inline void rmv_page_order(struct page *page)
346 __ClearPageBuddy(page);
347 set_page_private(page, 0);
351 * Locate the struct page for both the matching buddy in our
352 * pair (buddy1) and the combined O(n+1) page they form (page).
354 * 1) Any buddy B1 will have an order O twin B2 which satisfies
355 * the following equation:
356 * B2 = B1 ^ (1 << O)
357 * For example, if the starting buddy (buddy2) is #8 its order
358 * 1 buddy is #10:
359 * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
361 * 2) Any buddy B will have an order O+1 parent P which
362 * satisfies the following equation:
363 * P = B & ~(1 << O)
365 * Assumption: *_mem_map is contiguous at least up to MAX_ORDER
367 static inline struct page *
368 __page_find_buddy(struct page *page, unsigned long page_idx, unsigned int order)
370 unsigned long buddy_idx = page_idx ^ (1 << order);
372 return page + (buddy_idx - page_idx);
375 static inline unsigned long
376 __find_combined_index(unsigned long page_idx, unsigned int order)
378 return (page_idx & ~(1 << order));
382 * This function checks whether a page is free && is the buddy
383 * we can do coalesce a page and its buddy if
384 * (a) the buddy is not in a hole &&
385 * (b) the buddy is in the buddy system &&
386 * (c) a page and its buddy have the same order &&
387 * (d) a page and its buddy are in the same zone.
389 * For recording whether a page is in the buddy system, we use PG_buddy.
390 * Setting, clearing, and testing PG_buddy is serialized by zone->lock.
392 * For recording page's order, we use page_private(page).
394 static inline int page_is_buddy(struct page *page, struct page *buddy,
395 int order)
397 if (!pfn_valid_within(page_to_pfn(buddy)))
398 return 0;
400 if (page_zone_id(page) != page_zone_id(buddy))
401 return 0;
403 if (PageBuddy(buddy) && page_order(buddy) == order) {
404 BUG_ON(page_count(buddy) != 0);
405 return 1;
407 return 0;
411 * Freeing function for a buddy system allocator.
413 * The concept of a buddy system is to maintain direct-mapped table
414 * (containing bit values) for memory blocks of various "orders".
415 * The bottom level table contains the map for the smallest allocatable
416 * units of memory (here, pages), and each level above it describes
417 * pairs of units from the levels below, hence, "buddies".
418 * At a high level, all that happens here is marking the table entry
419 * at the bottom level available, and propagating the changes upward
420 * as necessary, plus some accounting needed to play nicely with other
421 * parts of the VM system.
422 * At each level, we keep a list of pages, which are heads of continuous
423 * free pages of length of (1 << order) and marked with PG_buddy. Page's
424 * order is recorded in page_private(page) field.
425 * So when we are allocating or freeing one, we can derive the state of the
426 * other. That is, if we allocate a small block, and both were
427 * free, the remainder of the region must be split into blocks.
428 * If a block is freed, and its buddy is also free, then this
429 * triggers coalescing into a block of larger size.
431 * -- wli
434 static inline void __free_one_page(struct page *page,
435 struct zone *zone, unsigned int order)
437 unsigned long page_idx;
438 int order_size = 1 << order;
439 int migratetype = get_pageblock_migratetype(page);
441 if (unlikely(PageCompound(page)))
442 destroy_compound_page(page, order);
444 page_idx = page_to_pfn(page) & ((1 << MAX_ORDER) - 1);
446 VM_BUG_ON(page_idx & (order_size - 1));
447 VM_BUG_ON(bad_range(zone, page));
449 __mod_zone_page_state(zone, NR_FREE_PAGES, order_size);
450 while (order < MAX_ORDER-1) {
451 unsigned long combined_idx;
452 struct page *buddy;
454 buddy = __page_find_buddy(page, page_idx, order);
455 if (!page_is_buddy(page, buddy, order))
456 break; /* Move the buddy up one level. */
458 list_del(&buddy->lru);
459 zone->free_area[order].nr_free--;
460 rmv_page_order(buddy);
461 combined_idx = __find_combined_index(page_idx, order);
462 page = page + (combined_idx - page_idx);
463 page_idx = combined_idx;
464 order++;
466 set_page_order(page, order);
467 list_add(&page->lru,
468 &zone->free_area[order].free_list[migratetype]);
469 zone->free_area[order].nr_free++;
472 static inline int free_pages_check(struct page *page)
474 if (unlikely(page_mapcount(page) |
475 (page->mapping != NULL) |
476 <<<<<<< HEAD:mm/page_alloc.c
477 =======
478 (page_get_page_cgroup(page) != NULL) |
479 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:mm/page_alloc.c
480 (page_count(page) != 0) |
481 (page->flags & (
482 1 << PG_lru |
483 1 << PG_private |
484 1 << PG_locked |
485 1 << PG_active |
486 1 << PG_slab |
487 1 << PG_swapcache |
488 1 << PG_writeback |
489 1 << PG_reserved |
490 1 << PG_buddy ))))
491 bad_page(page);
492 if (PageDirty(page))
493 __ClearPageDirty(page);
495 * For now, we report if PG_reserved was found set, but do not
496 * clear it, and do not free the page. But we shall soon need
497 * to do more, for when the ZERO_PAGE count wraps negative.
499 return PageReserved(page);
503 * Frees a list of pages.
504 * Assumes all pages on list are in same zone, and of same order.
505 * count is the number of pages to free.
507 * If the zone was previously in an "all pages pinned" state then look to
508 * see if this freeing clears that state.
510 * And clear the zone's pages_scanned counter, to hold off the "all pages are
511 * pinned" detection logic.
513 static void free_pages_bulk(struct zone *zone, int count,
514 struct list_head *list, int order)
516 spin_lock(&zone->lock);
517 zone_clear_flag(zone, ZONE_ALL_UNRECLAIMABLE);
518 zone->pages_scanned = 0;
519 while (count--) {
520 struct page *page;
522 VM_BUG_ON(list_empty(list));
523 page = list_entry(list->prev, struct page, lru);
524 /* have to delete it as __free_one_page list manipulates */
525 list_del(&page->lru);
526 __free_one_page(page, zone, order);
528 spin_unlock(&zone->lock);
531 static void free_one_page(struct zone *zone, struct page *page, int order)
533 spin_lock(&zone->lock);
534 zone_clear_flag(zone, ZONE_ALL_UNRECLAIMABLE);
535 zone->pages_scanned = 0;
536 __free_one_page(page, zone, order);
537 spin_unlock(&zone->lock);
540 static void __free_pages_ok(struct page *page, unsigned int order)
542 unsigned long flags;
543 int i;
544 int reserved = 0;
546 for (i = 0 ; i < (1 << order) ; ++i)
547 reserved += free_pages_check(page + i);
548 if (reserved)
549 return;
551 if (!PageHighMem(page))
552 debug_check_no_locks_freed(page_address(page),PAGE_SIZE<<order);
553 arch_free_page(page, order);
554 kernel_map_pages(page, 1 << order, 0);
556 local_irq_save(flags);
557 __count_vm_events(PGFREE, 1 << order);
558 free_one_page(page_zone(page), page, order);
559 local_irq_restore(flags);
563 * permit the bootmem allocator to evade page validation on high-order frees
565 void __init __free_pages_bootmem(struct page *page, unsigned int order)
567 if (order == 0) {
568 __ClearPageReserved(page);
569 set_page_count(page, 0);
570 set_page_refcounted(page);
571 __free_page(page);
572 } else {
573 int loop;
575 prefetchw(page);
576 for (loop = 0; loop < BITS_PER_LONG; loop++) {
577 struct page *p = &page[loop];
579 if (loop + 1 < BITS_PER_LONG)
580 prefetchw(p + 1);
581 __ClearPageReserved(p);
582 set_page_count(p, 0);
585 set_page_refcounted(page);
586 __free_pages(page, order);
592 * The order of subdivision here is critical for the IO subsystem.
593 * Please do not alter this order without good reasons and regression
594 * testing. Specifically, as large blocks of memory are subdivided,
595 * the order in which smaller blocks are delivered depends on the order
596 * they're subdivided in this function. This is the primary factor
597 * influencing the order in which pages are delivered to the IO
598 * subsystem according to empirical testing, and this is also justified
599 * by considering the behavior of a buddy system containing a single
600 * large block of memory acted on by a series of small allocations.
601 * This behavior is a critical factor in sglist merging's success.
603 * -- wli
605 static inline void expand(struct zone *zone, struct page *page,
606 int low, int high, struct free_area *area,
607 int migratetype)
609 unsigned long size = 1 << high;
611 while (high > low) {
612 area--;
613 high--;
614 size >>= 1;
615 VM_BUG_ON(bad_range(zone, &page[size]));
616 list_add(&page[size].lru, &area->free_list[migratetype]);
617 area->nr_free++;
618 set_page_order(&page[size], high);
623 * This page is about to be returned from the page allocator
625 static int prep_new_page(struct page *page, int order, gfp_t gfp_flags)
627 if (unlikely(page_mapcount(page) |
628 (page->mapping != NULL) |
629 <<<<<<< HEAD:mm/page_alloc.c
630 =======
631 (page_get_page_cgroup(page) != NULL) |
632 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:mm/page_alloc.c
633 (page_count(page) != 0) |
634 (page->flags & (
635 1 << PG_lru |
636 1 << PG_private |
637 1 << PG_locked |
638 1 << PG_active |
639 1 << PG_dirty |
640 1 << PG_slab |
641 1 << PG_swapcache |
642 1 << PG_writeback |
643 1 << PG_reserved |
644 1 << PG_buddy ))))
645 bad_page(page);
648 * For now, we report if PG_reserved was found set, but do not
649 * clear it, and do not allocate the page: as a safety net.
651 if (PageReserved(page))
652 return 1;
654 page->flags &= ~(1 << PG_uptodate | 1 << PG_error | 1 << PG_readahead |
655 1 << PG_referenced | 1 << PG_arch_1 |
656 1 << PG_owner_priv_1 | 1 << PG_mappedtodisk);
657 set_page_private(page, 0);
658 set_page_refcounted(page);
660 arch_alloc_page(page, order);
661 kernel_map_pages(page, 1 << order, 1);
663 if (gfp_flags & __GFP_ZERO)
664 prep_zero_page(page, order, gfp_flags);
666 if (order && (gfp_flags & __GFP_COMP))
667 prep_compound_page(page, order);
669 return 0;
673 * Go through the free lists for the given migratetype and remove
674 * the smallest available page from the freelists
676 static struct page *__rmqueue_smallest(struct zone *zone, unsigned int order,
677 int migratetype)
679 unsigned int current_order;
680 struct free_area * area;
681 struct page *page;
683 /* Find a page of the appropriate size in the preferred list */
684 for (current_order = order; current_order < MAX_ORDER; ++current_order) {
685 area = &(zone->free_area[current_order]);
686 if (list_empty(&area->free_list[migratetype]))
687 continue;
689 page = list_entry(area->free_list[migratetype].next,
690 struct page, lru);
691 list_del(&page->lru);
692 rmv_page_order(page);
693 area->nr_free--;
694 __mod_zone_page_state(zone, NR_FREE_PAGES, - (1UL << order));
695 expand(zone, page, order, current_order, area, migratetype);
696 return page;
699 return NULL;
704 * This array describes the order lists are fallen back to when
705 * the free lists for the desirable migrate type are depleted
707 static int fallbacks[MIGRATE_TYPES][MIGRATE_TYPES-1] = {
708 [MIGRATE_UNMOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_MOVABLE, MIGRATE_RESERVE },
709 [MIGRATE_RECLAIMABLE] = { MIGRATE_UNMOVABLE, MIGRATE_MOVABLE, MIGRATE_RESERVE },
710 [MIGRATE_MOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE, MIGRATE_RESERVE },
711 [MIGRATE_RESERVE] = { MIGRATE_RESERVE, MIGRATE_RESERVE, MIGRATE_RESERVE }, /* Never used */
715 * Move the free pages in a range to the free lists of the requested type.
716 * Note that start_page and end_pages are not aligned on a pageblock
717 * boundary. If alignment is required, use move_freepages_block()
719 int move_freepages(struct zone *zone,
720 struct page *start_page, struct page *end_page,
721 int migratetype)
723 struct page *page;
724 unsigned long order;
725 int pages_moved = 0;
727 #ifndef CONFIG_HOLES_IN_ZONE
729 * page_zone is not safe to call in this context when
730 * CONFIG_HOLES_IN_ZONE is set. This bug check is probably redundant
731 * anyway as we check zone boundaries in move_freepages_block().
732 * Remove at a later date when no bug reports exist related to
733 * grouping pages by mobility
735 BUG_ON(page_zone(start_page) != page_zone(end_page));
736 #endif
738 for (page = start_page; page <= end_page;) {
739 if (!pfn_valid_within(page_to_pfn(page))) {
740 page++;
741 continue;
744 if (!PageBuddy(page)) {
745 page++;
746 continue;
749 order = page_order(page);
750 list_del(&page->lru);
751 list_add(&page->lru,
752 &zone->free_area[order].free_list[migratetype]);
753 page += 1 << order;
754 pages_moved += 1 << order;
757 return pages_moved;
760 int move_freepages_block(struct zone *zone, struct page *page, int migratetype)
762 unsigned long start_pfn, end_pfn;
763 struct page *start_page, *end_page;
765 start_pfn = page_to_pfn(page);
766 start_pfn = start_pfn & ~(pageblock_nr_pages-1);
767 start_page = pfn_to_page(start_pfn);
768 end_page = start_page + pageblock_nr_pages - 1;
769 end_pfn = start_pfn + pageblock_nr_pages - 1;
771 /* Do not cross zone boundaries */
772 if (start_pfn < zone->zone_start_pfn)
773 start_page = page;
774 if (end_pfn >= zone->zone_start_pfn + zone->spanned_pages)
775 return 0;
777 return move_freepages(zone, start_page, end_page, migratetype);
780 /* Remove an element from the buddy allocator from the fallback list */
781 static struct page *__rmqueue_fallback(struct zone *zone, int order,
782 int start_migratetype)
784 struct free_area * area;
785 int current_order;
786 struct page *page;
787 int migratetype, i;
789 /* Find the largest possible block of pages in the other list */
790 for (current_order = MAX_ORDER-1; current_order >= order;
791 --current_order) {
792 for (i = 0; i < MIGRATE_TYPES - 1; i++) {
793 migratetype = fallbacks[start_migratetype][i];
795 /* MIGRATE_RESERVE handled later if necessary */
796 if (migratetype == MIGRATE_RESERVE)
797 continue;
799 area = &(zone->free_area[current_order]);
800 if (list_empty(&area->free_list[migratetype]))
801 continue;
803 page = list_entry(area->free_list[migratetype].next,
804 struct page, lru);
805 area->nr_free--;
808 * If breaking a large block of pages, move all free
809 * pages to the preferred allocation list. If falling
810 * back for a reclaimable kernel allocation, be more
811 * agressive about taking ownership of free pages
813 if (unlikely(current_order >= (pageblock_order >> 1)) ||
814 start_migratetype == MIGRATE_RECLAIMABLE) {
815 unsigned long pages;
816 pages = move_freepages_block(zone, page,
817 start_migratetype);
819 /* Claim the whole block if over half of it is free */
820 if (pages >= (1 << (pageblock_order-1)))
821 set_pageblock_migratetype(page,
822 start_migratetype);
824 migratetype = start_migratetype;
827 /* Remove the page from the freelists */
828 list_del(&page->lru);
829 rmv_page_order(page);
830 __mod_zone_page_state(zone, NR_FREE_PAGES,
831 -(1UL << order));
833 if (current_order == pageblock_order)
834 set_pageblock_migratetype(page,
835 start_migratetype);
837 expand(zone, page, order, current_order, area, migratetype);
838 return page;
842 /* Use MIGRATE_RESERVE rather than fail an allocation */
843 return __rmqueue_smallest(zone, order, MIGRATE_RESERVE);
847 * Do the hard work of removing an element from the buddy allocator.
848 * Call me with the zone->lock already held.
850 static struct page *__rmqueue(struct zone *zone, unsigned int order,
851 int migratetype)
853 struct page *page;
855 page = __rmqueue_smallest(zone, order, migratetype);
857 if (unlikely(!page))
858 page = __rmqueue_fallback(zone, order, migratetype);
860 return page;
864 * Obtain a specified number of elements from the buddy allocator, all under
865 * a single hold of the lock, for efficiency. Add them to the supplied list.
866 * Returns the number of new pages which were placed at *list.
868 static int rmqueue_bulk(struct zone *zone, unsigned int order,
869 unsigned long count, struct list_head *list,
870 int migratetype)
872 int i;
874 spin_lock(&zone->lock);
875 for (i = 0; i < count; ++i) {
876 struct page *page = __rmqueue(zone, order, migratetype);
877 if (unlikely(page == NULL))
878 break;
881 * Split buddy pages returned by expand() are received here
882 * in physical page order. The page is added to the callers and
883 * list and the list head then moves forward. From the callers
884 * perspective, the linked list is ordered by page number in
885 * some conditions. This is useful for IO devices that can
886 * merge IO requests if the physical pages are ordered
887 * properly.
889 list_add(&page->lru, list);
890 set_page_private(page, migratetype);
891 list = &page->lru;
893 spin_unlock(&zone->lock);
894 return i;
897 #ifdef CONFIG_NUMA
899 * Called from the vmstat counter updater to drain pagesets of this
900 * currently executing processor on remote nodes after they have
901 * expired.
903 * Note that this function must be called with the thread pinned to
904 * a single processor.
906 void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
908 unsigned long flags;
909 int to_drain;
911 local_irq_save(flags);
912 if (pcp->count >= pcp->batch)
913 to_drain = pcp->batch;
914 else
915 to_drain = pcp->count;
916 free_pages_bulk(zone, to_drain, &pcp->list, 0);
917 pcp->count -= to_drain;
918 local_irq_restore(flags);
920 #endif
923 * Drain pages of the indicated processor.
925 * The processor must either be the current processor and the
926 * thread pinned to the current processor or a processor that
927 * is not online.
929 static void drain_pages(unsigned int cpu)
931 unsigned long flags;
932 struct zone *zone;
934 for_each_zone(zone) {
935 struct per_cpu_pageset *pset;
936 struct per_cpu_pages *pcp;
938 if (!populated_zone(zone))
939 continue;
941 pset = zone_pcp(zone, cpu);
943 pcp = &pset->pcp;
944 local_irq_save(flags);
945 free_pages_bulk(zone, pcp->count, &pcp->list, 0);
946 pcp->count = 0;
947 local_irq_restore(flags);
952 * Spill all of this CPU's per-cpu pages back into the buddy allocator.
954 void drain_local_pages(void *arg)
956 drain_pages(smp_processor_id());
960 * Spill all the per-cpu pages from all CPUs back into the buddy allocator
962 void drain_all_pages(void)
964 on_each_cpu(drain_local_pages, NULL, 0, 1);
967 #ifdef CONFIG_HIBERNATION
969 void mark_free_pages(struct zone *zone)
971 unsigned long pfn, max_zone_pfn;
972 unsigned long flags;
973 int order, t;
974 struct list_head *curr;
976 if (!zone->spanned_pages)
977 return;
979 spin_lock_irqsave(&zone->lock, flags);
981 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
982 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
983 if (pfn_valid(pfn)) {
984 struct page *page = pfn_to_page(pfn);
986 if (!swsusp_page_is_forbidden(page))
987 swsusp_unset_page_free(page);
990 for_each_migratetype_order(order, t) {
991 list_for_each(curr, &zone->free_area[order].free_list[t]) {
992 unsigned long i;
994 pfn = page_to_pfn(list_entry(curr, struct page, lru));
995 for (i = 0; i < (1UL << order); i++)
996 swsusp_set_page_free(pfn_to_page(pfn + i));
999 spin_unlock_irqrestore(&zone->lock, flags);
1001 #endif /* CONFIG_PM */
1004 * Free a 0-order page
1006 static void free_hot_cold_page(struct page *page, int cold)
1008 struct zone *zone = page_zone(page);
1009 struct per_cpu_pages *pcp;
1010 unsigned long flags;
1012 if (PageAnon(page))
1013 page->mapping = NULL;
1014 if (free_pages_check(page))
1015 return;
1017 if (!PageHighMem(page))
1018 debug_check_no_locks_freed(page_address(page), PAGE_SIZE);
1019 <<<<<<< HEAD:mm/page_alloc.c
1020 VM_BUG_ON(page_get_page_cgroup(page));
1021 =======
1022 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:mm/page_alloc.c
1023 arch_free_page(page, 0);
1024 kernel_map_pages(page, 1, 0);
1026 pcp = &zone_pcp(zone, get_cpu())->pcp;
1027 local_irq_save(flags);
1028 __count_vm_event(PGFREE);
1029 if (cold)
1030 list_add_tail(&page->lru, &pcp->list);
1031 else
1032 list_add(&page->lru, &pcp->list);
1033 set_page_private(page, get_pageblock_migratetype(page));
1034 pcp->count++;
1035 if (pcp->count >= pcp->high) {
1036 free_pages_bulk(zone, pcp->batch, &pcp->list, 0);
1037 pcp->count -= pcp->batch;
1039 local_irq_restore(flags);
1040 put_cpu();
1043 void free_hot_page(struct page *page)
1045 free_hot_cold_page(page, 0);
1048 void free_cold_page(struct page *page)
1050 free_hot_cold_page(page, 1);
1054 * split_page takes a non-compound higher-order page, and splits it into
1055 * n (1<<order) sub-pages: page[0..n]
1056 * Each sub-page must be freed individually.
1058 * Note: this is probably too low level an operation for use in drivers.
1059 * Please consult with lkml before using this in your driver.
1061 void split_page(struct page *page, unsigned int order)
1063 int i;
1065 VM_BUG_ON(PageCompound(page));
1066 VM_BUG_ON(!page_count(page));
1067 for (i = 1; i < (1 << order); i++)
1068 set_page_refcounted(page + i);
1072 * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
1073 * we cheat by calling it from here, in the order > 0 path. Saves a branch
1074 * or two.
1076 static struct page *buffered_rmqueue(struct zonelist *zonelist,
1077 struct zone *zone, int order, gfp_t gfp_flags)
1079 unsigned long flags;
1080 struct page *page;
1081 int cold = !!(gfp_flags & __GFP_COLD);
1082 int cpu;
1083 int migratetype = allocflags_to_migratetype(gfp_flags);
1085 again:
1086 cpu = get_cpu();
1087 if (likely(order == 0)) {
1088 struct per_cpu_pages *pcp;
1090 pcp = &zone_pcp(zone, cpu)->pcp;
1091 local_irq_save(flags);
1092 if (!pcp->count) {
1093 pcp->count = rmqueue_bulk(zone, 0,
1094 pcp->batch, &pcp->list, migratetype);
1095 if (unlikely(!pcp->count))
1096 goto failed;
1099 /* Find a page of the appropriate migrate type */
1100 if (cold) {
1101 list_for_each_entry_reverse(page, &pcp->list, lru)
1102 if (page_private(page) == migratetype)
1103 break;
1104 } else {
1105 list_for_each_entry(page, &pcp->list, lru)
1106 if (page_private(page) == migratetype)
1107 break;
1110 /* Allocate more to the pcp list if necessary */
1111 if (unlikely(&page->lru == &pcp->list)) {
1112 pcp->count += rmqueue_bulk(zone, 0,
1113 pcp->batch, &pcp->list, migratetype);
1114 page = list_entry(pcp->list.next, struct page, lru);
1117 list_del(&page->lru);
1118 pcp->count--;
1119 } else {
1120 spin_lock_irqsave(&zone->lock, flags);
1121 page = __rmqueue(zone, order, migratetype);
1122 spin_unlock(&zone->lock);
1123 if (!page)
1124 goto failed;
1127 __count_zone_vm_events(PGALLOC, zone, 1 << order);
1128 zone_statistics(zonelist, zone);
1129 local_irq_restore(flags);
1130 put_cpu();
1132 VM_BUG_ON(bad_range(zone, page));
1133 if (prep_new_page(page, order, gfp_flags))
1134 goto again;
1135 return page;
1137 failed:
1138 local_irq_restore(flags);
1139 put_cpu();
1140 return NULL;
1143 #define ALLOC_NO_WATERMARKS 0x01 /* don't check watermarks at all */
1144 #define ALLOC_WMARK_MIN 0x02 /* use pages_min watermark */
1145 #define ALLOC_WMARK_LOW 0x04 /* use pages_low watermark */
1146 #define ALLOC_WMARK_HIGH 0x08 /* use pages_high watermark */
1147 #define ALLOC_HARDER 0x10 /* try to alloc harder */
1148 #define ALLOC_HIGH 0x20 /* __GFP_HIGH set */
1149 #define ALLOC_CPUSET 0x40 /* check for correct cpuset */
1151 #ifdef CONFIG_FAIL_PAGE_ALLOC
1153 static struct fail_page_alloc_attr {
1154 struct fault_attr attr;
1156 u32 ignore_gfp_highmem;
1157 u32 ignore_gfp_wait;
1158 u32 min_order;
1160 #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
1162 struct dentry *ignore_gfp_highmem_file;
1163 struct dentry *ignore_gfp_wait_file;
1164 struct dentry *min_order_file;
1166 #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
1168 } fail_page_alloc = {
1169 .attr = FAULT_ATTR_INITIALIZER,
1170 .ignore_gfp_wait = 1,
1171 .ignore_gfp_highmem = 1,
1172 .min_order = 1,
1175 static int __init setup_fail_page_alloc(char *str)
1177 return setup_fault_attr(&fail_page_alloc.attr, str);
1179 __setup("fail_page_alloc=", setup_fail_page_alloc);
1181 static int should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
1183 if (order < fail_page_alloc.min_order)
1184 return 0;
1185 if (gfp_mask & __GFP_NOFAIL)
1186 return 0;
1187 if (fail_page_alloc.ignore_gfp_highmem && (gfp_mask & __GFP_HIGHMEM))
1188 return 0;
1189 if (fail_page_alloc.ignore_gfp_wait && (gfp_mask & __GFP_WAIT))
1190 return 0;
1192 return should_fail(&fail_page_alloc.attr, 1 << order);
1195 #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
1197 static int __init fail_page_alloc_debugfs(void)
1199 mode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
1200 struct dentry *dir;
1201 int err;
1203 err = init_fault_attr_dentries(&fail_page_alloc.attr,
1204 "fail_page_alloc");
1205 if (err)
1206 return err;
1207 dir = fail_page_alloc.attr.dentries.dir;
1209 fail_page_alloc.ignore_gfp_wait_file =
1210 debugfs_create_bool("ignore-gfp-wait", mode, dir,
1211 &fail_page_alloc.ignore_gfp_wait);
1213 fail_page_alloc.ignore_gfp_highmem_file =
1214 debugfs_create_bool("ignore-gfp-highmem", mode, dir,
1215 &fail_page_alloc.ignore_gfp_highmem);
1216 fail_page_alloc.min_order_file =
1217 debugfs_create_u32("min-order", mode, dir,
1218 &fail_page_alloc.min_order);
1220 if (!fail_page_alloc.ignore_gfp_wait_file ||
1221 !fail_page_alloc.ignore_gfp_highmem_file ||
1222 !fail_page_alloc.min_order_file) {
1223 err = -ENOMEM;
1224 debugfs_remove(fail_page_alloc.ignore_gfp_wait_file);
1225 debugfs_remove(fail_page_alloc.ignore_gfp_highmem_file);
1226 debugfs_remove(fail_page_alloc.min_order_file);
1227 cleanup_fault_attr_dentries(&fail_page_alloc.attr);
1230 return err;
1233 late_initcall(fail_page_alloc_debugfs);
1235 #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
1237 #else /* CONFIG_FAIL_PAGE_ALLOC */
1239 static inline int should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
1241 return 0;
1244 #endif /* CONFIG_FAIL_PAGE_ALLOC */
1247 * Return 1 if free pages are above 'mark'. This takes into account the order
1248 * of the allocation.
1250 int zone_watermark_ok(struct zone *z, int order, unsigned long mark,
1251 int classzone_idx, int alloc_flags)
1253 /* free_pages my go negative - that's OK */
1254 long min = mark;
1255 long free_pages = zone_page_state(z, NR_FREE_PAGES) - (1 << order) + 1;
1256 int o;
1258 if (alloc_flags & ALLOC_HIGH)
1259 min -= min / 2;
1260 if (alloc_flags & ALLOC_HARDER)
1261 min -= min / 4;
1263 if (free_pages <= min + z->lowmem_reserve[classzone_idx])
1264 return 0;
1265 for (o = 0; o < order; o++) {
1266 /* At the next order, this order's pages become unavailable */
1267 free_pages -= z->free_area[o].nr_free << o;
1269 /* Require fewer higher order pages to be free */
1270 min >>= 1;
1272 if (free_pages <= min)
1273 return 0;
1275 return 1;
1278 #ifdef CONFIG_NUMA
1280 * zlc_setup - Setup for "zonelist cache". Uses cached zone data to
1281 * skip over zones that are not allowed by the cpuset, or that have
1282 * been recently (in last second) found to be nearly full. See further
1283 * comments in mmzone.h. Reduces cache footprint of zonelist scans
1284 * that have to skip over a lot of full or unallowed zones.
1286 * If the zonelist cache is present in the passed in zonelist, then
1287 * returns a pointer to the allowed node mask (either the current
1288 * tasks mems_allowed, or node_states[N_HIGH_MEMORY].)
1290 * If the zonelist cache is not available for this zonelist, does
1291 * nothing and returns NULL.
1293 * If the fullzones BITMAP in the zonelist cache is stale (more than
1294 * a second since last zap'd) then we zap it out (clear its bits.)
1296 * We hold off even calling zlc_setup, until after we've checked the
1297 * first zone in the zonelist, on the theory that most allocations will
1298 * be satisfied from that first zone, so best to examine that zone as
1299 * quickly as we can.
1301 static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
1303 struct zonelist_cache *zlc; /* cached zonelist speedup info */
1304 nodemask_t *allowednodes; /* zonelist_cache approximation */
1306 zlc = zonelist->zlcache_ptr;
1307 if (!zlc)
1308 return NULL;
1310 <<<<<<< HEAD:mm/page_alloc.c
1311 if (jiffies - zlc->last_full_zap > 1 * HZ) {
1312 =======
1313 if (time_after(jiffies, zlc->last_full_zap + HZ)) {
1314 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:mm/page_alloc.c
1315 bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
1316 zlc->last_full_zap = jiffies;
1319 allowednodes = !in_interrupt() && (alloc_flags & ALLOC_CPUSET) ?
1320 &cpuset_current_mems_allowed :
1321 &node_states[N_HIGH_MEMORY];
1322 return allowednodes;
1326 * Given 'z' scanning a zonelist, run a couple of quick checks to see
1327 * if it is worth looking at further for free memory:
1328 * 1) Check that the zone isn't thought to be full (doesn't have its
1329 * bit set in the zonelist_cache fullzones BITMAP).
1330 * 2) Check that the zones node (obtained from the zonelist_cache
1331 * z_to_n[] mapping) is allowed in the passed in allowednodes mask.
1332 * Return true (non-zero) if zone is worth looking at further, or
1333 * else return false (zero) if it is not.
1335 * This check -ignores- the distinction between various watermarks,
1336 * such as GFP_HIGH, GFP_ATOMIC, PF_MEMALLOC, ... If a zone is
1337 * found to be full for any variation of these watermarks, it will
1338 * be considered full for up to one second by all requests, unless
1339 * we are so low on memory on all allowed nodes that we are forced
1340 * into the second scan of the zonelist.
1342 * In the second scan we ignore this zonelist cache and exactly
1343 * apply the watermarks to all zones, even it is slower to do so.
1344 * We are low on memory in the second scan, and should leave no stone
1345 * unturned looking for a free page.
1347 static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zone **z,
1348 nodemask_t *allowednodes)
1350 struct zonelist_cache *zlc; /* cached zonelist speedup info */
1351 int i; /* index of *z in zonelist zones */
1352 int n; /* node that zone *z is on */
1354 zlc = zonelist->zlcache_ptr;
1355 if (!zlc)
1356 return 1;
1358 i = z - zonelist->zones;
1359 n = zlc->z_to_n[i];
1361 /* This zone is worth trying if it is allowed but not full */
1362 return node_isset(n, *allowednodes) && !test_bit(i, zlc->fullzones);
1366 * Given 'z' scanning a zonelist, set the corresponding bit in
1367 * zlc->fullzones, so that subsequent attempts to allocate a page
1368 * from that zone don't waste time re-examining it.
1370 static void zlc_mark_zone_full(struct zonelist *zonelist, struct zone **z)
1372 struct zonelist_cache *zlc; /* cached zonelist speedup info */
1373 int i; /* index of *z in zonelist zones */
1375 zlc = zonelist->zlcache_ptr;
1376 if (!zlc)
1377 return;
1379 i = z - zonelist->zones;
1381 set_bit(i, zlc->fullzones);
1384 #else /* CONFIG_NUMA */
1386 static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
1388 return NULL;
1391 static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zone **z,
1392 nodemask_t *allowednodes)
1394 return 1;
1397 static void zlc_mark_zone_full(struct zonelist *zonelist, struct zone **z)
1400 #endif /* CONFIG_NUMA */
1403 * get_page_from_freelist goes through the zonelist trying to allocate
1404 * a page.
1406 static struct page *
1407 get_page_from_freelist(gfp_t gfp_mask, unsigned int order,
1408 struct zonelist *zonelist, int alloc_flags)
1410 struct zone **z;
1411 struct page *page = NULL;
1412 int classzone_idx = zone_idx(zonelist->zones[0]);
1413 struct zone *zone;
1414 nodemask_t *allowednodes = NULL;/* zonelist_cache approximation */
1415 int zlc_active = 0; /* set if using zonelist_cache */
1416 int did_zlc_setup = 0; /* just call zlc_setup() one time */
1417 enum zone_type highest_zoneidx = -1; /* Gets set for policy zonelists */
1419 zonelist_scan:
1421 * Scan zonelist, looking for a zone with enough free.
1422 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
1424 z = zonelist->zones;
1426 do {
1428 * In NUMA, this could be a policy zonelist which contains
1429 * zones that may not be allowed by the current gfp_mask.
1430 * Check the zone is allowed by the current flags
1432 if (unlikely(alloc_should_filter_zonelist(zonelist))) {
1433 if (highest_zoneidx == -1)
1434 highest_zoneidx = gfp_zone(gfp_mask);
1435 if (zone_idx(*z) > highest_zoneidx)
1436 continue;
1439 if (NUMA_BUILD && zlc_active &&
1440 !zlc_zone_worth_trying(zonelist, z, allowednodes))
1441 continue;
1442 zone = *z;
1443 if ((alloc_flags & ALLOC_CPUSET) &&
1444 !cpuset_zone_allowed_softwall(zone, gfp_mask))
1445 goto try_next_zone;
1447 if (!(alloc_flags & ALLOC_NO_WATERMARKS)) {
1448 unsigned long mark;
1449 if (alloc_flags & ALLOC_WMARK_MIN)
1450 mark = zone->pages_min;
1451 else if (alloc_flags & ALLOC_WMARK_LOW)
1452 mark = zone->pages_low;
1453 else
1454 mark = zone->pages_high;
1455 if (!zone_watermark_ok(zone, order, mark,
1456 classzone_idx, alloc_flags)) {
1457 if (!zone_reclaim_mode ||
1458 !zone_reclaim(zone, gfp_mask, order))
1459 goto this_zone_full;
1463 page = buffered_rmqueue(zonelist, zone, order, gfp_mask);
1464 if (page)
1465 break;
1466 this_zone_full:
1467 if (NUMA_BUILD)
1468 zlc_mark_zone_full(zonelist, z);
1469 try_next_zone:
1470 if (NUMA_BUILD && !did_zlc_setup) {
1471 /* we do zlc_setup after the first zone is tried */
1472 allowednodes = zlc_setup(zonelist, alloc_flags);
1473 zlc_active = 1;
1474 did_zlc_setup = 1;
1476 } while (*(++z) != NULL);
1478 if (unlikely(NUMA_BUILD && page == NULL && zlc_active)) {
1479 /* Disable zlc cache for second zonelist scan */
1480 zlc_active = 0;
1481 goto zonelist_scan;
1483 return page;
1487 * This is the 'heart' of the zoned buddy allocator.
1489 struct page *
1490 __alloc_pages(gfp_t gfp_mask, unsigned int order,
1491 struct zonelist *zonelist)
1493 const gfp_t wait = gfp_mask & __GFP_WAIT;
1494 struct zone **z;
1495 struct page *page;
1496 struct reclaim_state reclaim_state;
1497 struct task_struct *p = current;
1498 int do_retry;
1499 int alloc_flags;
1500 int did_some_progress;
1502 might_sleep_if(wait);
1504 if (should_fail_alloc_page(gfp_mask, order))
1505 return NULL;
1507 restart:
1508 z = zonelist->zones; /* the list of zones suitable for gfp_mask */
1510 if (unlikely(*z == NULL)) {
1512 * Happens if we have an empty zonelist as a result of
1513 * GFP_THISNODE being used on a memoryless node
1515 return NULL;
1518 page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, order,
1519 zonelist, ALLOC_WMARK_LOW|ALLOC_CPUSET);
1520 if (page)
1521 goto got_pg;
1524 * GFP_THISNODE (meaning __GFP_THISNODE, __GFP_NORETRY and
1525 * __GFP_NOWARN set) should not cause reclaim since the subsystem
1526 * (f.e. slab) using GFP_THISNODE may choose to trigger reclaim
1527 * using a larger set of nodes after it has established that the
1528 * allowed per node queues are empty and that nodes are
1529 * over allocated.
1531 if (NUMA_BUILD && (gfp_mask & GFP_THISNODE) == GFP_THISNODE)
1532 goto nopage;
1534 for (z = zonelist->zones; *z; z++)
1535 wakeup_kswapd(*z, order);
1538 * OK, we're below the kswapd watermark and have kicked background
1539 * reclaim. Now things get more complex, so set up alloc_flags according
1540 * to how we want to proceed.
1542 * The caller may dip into page reserves a bit more if the caller
1543 * cannot run direct reclaim, or if the caller has realtime scheduling
1544 * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will
1545 * set both ALLOC_HARDER (!wait) and ALLOC_HIGH (__GFP_HIGH).
1547 alloc_flags = ALLOC_WMARK_MIN;
1548 if ((unlikely(rt_task(p)) && !in_interrupt()) || !wait)
1549 alloc_flags |= ALLOC_HARDER;
1550 if (gfp_mask & __GFP_HIGH)
1551 alloc_flags |= ALLOC_HIGH;
1552 if (wait)
1553 alloc_flags |= ALLOC_CPUSET;
1556 * Go through the zonelist again. Let __GFP_HIGH and allocations
1557 * coming from realtime tasks go deeper into reserves.
1559 * This is the last chance, in general, before the goto nopage.
1560 * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
1561 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
1563 page = get_page_from_freelist(gfp_mask, order, zonelist, alloc_flags);
1564 if (page)
1565 goto got_pg;
1567 /* This allocation should allow future memory freeing. */
1569 rebalance:
1570 if (((p->flags & PF_MEMALLOC) || unlikely(test_thread_flag(TIF_MEMDIE)))
1571 && !in_interrupt()) {
1572 if (!(gfp_mask & __GFP_NOMEMALLOC)) {
1573 nofail_alloc:
1574 /* go through the zonelist yet again, ignoring mins */
1575 page = get_page_from_freelist(gfp_mask, order,
1576 zonelist, ALLOC_NO_WATERMARKS);
1577 if (page)
1578 goto got_pg;
1579 if (gfp_mask & __GFP_NOFAIL) {
1580 congestion_wait(WRITE, HZ/50);
1581 goto nofail_alloc;
1584 goto nopage;
1587 /* Atomic allocations - we can't balance anything */
1588 if (!wait)
1589 goto nopage;
1591 cond_resched();
1593 /* We now go into synchronous reclaim */
1594 cpuset_memory_pressure_bump();
1595 p->flags |= PF_MEMALLOC;
1596 reclaim_state.reclaimed_slab = 0;
1597 p->reclaim_state = &reclaim_state;
1599 did_some_progress = try_to_free_pages(zonelist->zones, order, gfp_mask);
1601 p->reclaim_state = NULL;
1602 p->flags &= ~PF_MEMALLOC;
1604 cond_resched();
1606 if (order != 0)
1607 drain_all_pages();
1609 if (likely(did_some_progress)) {
1610 page = get_page_from_freelist(gfp_mask, order,
1611 zonelist, alloc_flags);
1612 if (page)
1613 goto got_pg;
1614 } else if ((gfp_mask & __GFP_FS) && !(gfp_mask & __GFP_NORETRY)) {
1615 if (!try_set_zone_oom(zonelist)) {
1616 schedule_timeout_uninterruptible(1);
1617 goto restart;
1621 * Go through the zonelist yet one more time, keep
1622 * very high watermark here, this is only to catch
1623 * a parallel oom killing, we must fail if we're still
1624 * under heavy pressure.
1626 page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, order,
1627 zonelist, ALLOC_WMARK_HIGH|ALLOC_CPUSET);
1628 if (page) {
1629 clear_zonelist_oom(zonelist);
1630 goto got_pg;
1633 /* The OOM killer will not help higher order allocs so fail */
1634 if (order > PAGE_ALLOC_COSTLY_ORDER) {
1635 clear_zonelist_oom(zonelist);
1636 goto nopage;
1639 out_of_memory(zonelist, gfp_mask, order);
1640 clear_zonelist_oom(zonelist);
1641 goto restart;
1645 * Don't let big-order allocations loop unless the caller explicitly
1646 * requests that. Wait for some write requests to complete then retry.
1648 * In this implementation, __GFP_REPEAT means __GFP_NOFAIL for order
1649 * <= 3, but that may not be true in other implementations.
1651 do_retry = 0;
1652 if (!(gfp_mask & __GFP_NORETRY)) {
1653 if ((order <= PAGE_ALLOC_COSTLY_ORDER) ||
1654 (gfp_mask & __GFP_REPEAT))
1655 do_retry = 1;
1656 if (gfp_mask & __GFP_NOFAIL)
1657 do_retry = 1;
1659 if (do_retry) {
1660 congestion_wait(WRITE, HZ/50);
1661 goto rebalance;
1664 nopage:
1665 if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) {
1666 printk(KERN_WARNING "%s: page allocation failure."
1667 " order:%d, mode:0x%x\n",
1668 p->comm, order, gfp_mask);
1669 dump_stack();
1670 show_mem();
1672 got_pg:
1673 return page;
1676 EXPORT_SYMBOL(__alloc_pages);
1679 * Common helper functions.
1681 unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
1683 struct page * page;
1684 page = alloc_pages(gfp_mask, order);
1685 if (!page)
1686 return 0;
1687 return (unsigned long) page_address(page);
1690 EXPORT_SYMBOL(__get_free_pages);
1692 unsigned long get_zeroed_page(gfp_t gfp_mask)
1694 struct page * page;
1697 * get_zeroed_page() returns a 32-bit address, which cannot represent
1698 * a highmem page
1700 VM_BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
1702 page = alloc_pages(gfp_mask | __GFP_ZERO, 0);
1703 if (page)
1704 return (unsigned long) page_address(page);
1705 return 0;
1708 EXPORT_SYMBOL(get_zeroed_page);
1710 void __pagevec_free(struct pagevec *pvec)
1712 int i = pagevec_count(pvec);
1714 while (--i >= 0)
1715 free_hot_cold_page(pvec->pages[i], pvec->cold);
1718 void __free_pages(struct page *page, unsigned int order)
1720 if (put_page_testzero(page)) {
1721 if (order == 0)
1722 free_hot_page(page);
1723 else
1724 __free_pages_ok(page, order);
1728 EXPORT_SYMBOL(__free_pages);
1730 void free_pages(unsigned long addr, unsigned int order)
1732 if (addr != 0) {
1733 VM_BUG_ON(!virt_addr_valid((void *)addr));
1734 __free_pages(virt_to_page((void *)addr), order);
1738 EXPORT_SYMBOL(free_pages);
1740 static unsigned int nr_free_zone_pages(int offset)
1742 /* Just pick one node, since fallback list is circular */
1743 pg_data_t *pgdat = NODE_DATA(numa_node_id());
1744 unsigned int sum = 0;
1746 struct zonelist *zonelist = pgdat->node_zonelists + offset;
1747 struct zone **zonep = zonelist->zones;
1748 struct zone *zone;
1750 for (zone = *zonep++; zone; zone = *zonep++) {
1751 unsigned long size = zone->present_pages;
1752 unsigned long high = zone->pages_high;
1753 if (size > high)
1754 sum += size - high;
1757 return sum;
1761 * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
1763 unsigned int nr_free_buffer_pages(void)
1765 return nr_free_zone_pages(gfp_zone(GFP_USER));
1767 EXPORT_SYMBOL_GPL(nr_free_buffer_pages);
1770 * Amount of free RAM allocatable within all zones
1772 unsigned int nr_free_pagecache_pages(void)
1774 return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER_MOVABLE));
1777 static inline void show_node(struct zone *zone)
1779 if (NUMA_BUILD)
1780 printk("Node %d ", zone_to_nid(zone));
1783 void si_meminfo(struct sysinfo *val)
1785 val->totalram = totalram_pages;
1786 val->sharedram = 0;
1787 val->freeram = global_page_state(NR_FREE_PAGES);
1788 val->bufferram = nr_blockdev_pages();
1789 val->totalhigh = totalhigh_pages;
1790 val->freehigh = nr_free_highpages();
1791 val->mem_unit = PAGE_SIZE;
1794 EXPORT_SYMBOL(si_meminfo);
1796 #ifdef CONFIG_NUMA
1797 void si_meminfo_node(struct sysinfo *val, int nid)
1799 pg_data_t *pgdat = NODE_DATA(nid);
1801 val->totalram = pgdat->node_present_pages;
1802 val->freeram = node_page_state(nid, NR_FREE_PAGES);
1803 #ifdef CONFIG_HIGHMEM
1804 val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].present_pages;
1805 val->freehigh = zone_page_state(&pgdat->node_zones[ZONE_HIGHMEM],
1806 NR_FREE_PAGES);
1807 #else
1808 val->totalhigh = 0;
1809 val->freehigh = 0;
1810 #endif
1811 val->mem_unit = PAGE_SIZE;
1813 #endif
1815 #define K(x) ((x) << (PAGE_SHIFT-10))
1818 * Show free area list (used inside shift_scroll-lock stuff)
1819 * We also calculate the percentage fragmentation. We do this by counting the
1820 * memory on each free list with the exception of the first item on the list.
1822 void show_free_areas(void)
1824 int cpu;
1825 struct zone *zone;
1827 for_each_zone(zone) {
1828 if (!populated_zone(zone))
1829 continue;
1831 show_node(zone);
1832 printk("%s per-cpu:\n", zone->name);
1834 for_each_online_cpu(cpu) {
1835 struct per_cpu_pageset *pageset;
1837 pageset = zone_pcp(zone, cpu);
1839 printk("CPU %4d: hi:%5d, btch:%4d usd:%4d\n",
1840 cpu, pageset->pcp.high,
1841 pageset->pcp.batch, pageset->pcp.count);
1845 printk("Active:%lu inactive:%lu dirty:%lu writeback:%lu unstable:%lu\n"
1846 " free:%lu slab:%lu mapped:%lu pagetables:%lu bounce:%lu\n",
1847 global_page_state(NR_ACTIVE),
1848 global_page_state(NR_INACTIVE),
1849 global_page_state(NR_FILE_DIRTY),
1850 global_page_state(NR_WRITEBACK),
1851 global_page_state(NR_UNSTABLE_NFS),
1852 global_page_state(NR_FREE_PAGES),
1853 global_page_state(NR_SLAB_RECLAIMABLE) +
1854 global_page_state(NR_SLAB_UNRECLAIMABLE),
1855 global_page_state(NR_FILE_MAPPED),
1856 global_page_state(NR_PAGETABLE),
1857 global_page_state(NR_BOUNCE));
1859 for_each_zone(zone) {
1860 int i;
1862 if (!populated_zone(zone))
1863 continue;
1865 show_node(zone);
1866 printk("%s"
1867 " free:%lukB"
1868 " min:%lukB"
1869 " low:%lukB"
1870 " high:%lukB"
1871 " active:%lukB"
1872 " inactive:%lukB"
1873 " present:%lukB"
1874 " pages_scanned:%lu"
1875 " all_unreclaimable? %s"
1876 "\n",
1877 zone->name,
1878 K(zone_page_state(zone, NR_FREE_PAGES)),
1879 K(zone->pages_min),
1880 K(zone->pages_low),
1881 K(zone->pages_high),
1882 K(zone_page_state(zone, NR_ACTIVE)),
1883 K(zone_page_state(zone, NR_INACTIVE)),
1884 K(zone->present_pages),
1885 zone->pages_scanned,
1886 (zone_is_all_unreclaimable(zone) ? "yes" : "no")
1888 printk("lowmem_reserve[]:");
1889 for (i = 0; i < MAX_NR_ZONES; i++)
1890 printk(" %lu", zone->lowmem_reserve[i]);
1891 printk("\n");
1894 for_each_zone(zone) {
1895 unsigned long nr[MAX_ORDER], flags, order, total = 0;
1897 if (!populated_zone(zone))
1898 continue;
1900 show_node(zone);
1901 printk("%s: ", zone->name);
1903 spin_lock_irqsave(&zone->lock, flags);
1904 for (order = 0; order < MAX_ORDER; order++) {
1905 nr[order] = zone->free_area[order].nr_free;
1906 total += nr[order] << order;
1908 spin_unlock_irqrestore(&zone->lock, flags);
1909 for (order = 0; order < MAX_ORDER; order++)
1910 printk("%lu*%lukB ", nr[order], K(1UL) << order);
1911 printk("= %lukB\n", K(total));
1914 printk("%ld total pagecache pages\n", global_page_state(NR_FILE_PAGES));
1916 show_swap_cache_info();
1920 * Builds allocation fallback zone lists.
1922 * Add all populated zones of a node to the zonelist.
1924 static int build_zonelists_node(pg_data_t *pgdat, struct zonelist *zonelist,
1925 int nr_zones, enum zone_type zone_type)
1927 struct zone *zone;
1929 BUG_ON(zone_type >= MAX_NR_ZONES);
1930 zone_type++;
1932 do {
1933 zone_type--;
1934 zone = pgdat->node_zones + zone_type;
1935 if (populated_zone(zone)) {
1936 zonelist->zones[nr_zones++] = zone;
1937 check_highest_zone(zone_type);
1940 } while (zone_type);
1941 return nr_zones;
1946 * zonelist_order:
1947 * 0 = automatic detection of better ordering.
1948 * 1 = order by ([node] distance, -zonetype)
1949 * 2 = order by (-zonetype, [node] distance)
1951 * If not NUMA, ZONELIST_ORDER_ZONE and ZONELIST_ORDER_NODE will create
1952 * the same zonelist. So only NUMA can configure this param.
1954 #define ZONELIST_ORDER_DEFAULT 0
1955 #define ZONELIST_ORDER_NODE 1
1956 #define ZONELIST_ORDER_ZONE 2
1958 /* zonelist order in the kernel.
1959 * set_zonelist_order() will set this to NODE or ZONE.
1961 static int current_zonelist_order = ZONELIST_ORDER_DEFAULT;
1962 static char zonelist_order_name[3][8] = {"Default", "Node", "Zone"};
1965 #ifdef CONFIG_NUMA
1966 /* The value user specified ....changed by config */
1967 static int user_zonelist_order = ZONELIST_ORDER_DEFAULT;
1968 /* string for sysctl */
1969 #define NUMA_ZONELIST_ORDER_LEN 16
1970 char numa_zonelist_order[16] = "default";
1973 * interface for configure zonelist ordering.
1974 * command line option "numa_zonelist_order"
1975 * = "[dD]efault - default, automatic configuration.
1976 * = "[nN]ode - order by node locality, then by zone within node
1977 * = "[zZ]one - order by zone, then by locality within zone
1980 static int __parse_numa_zonelist_order(char *s)
1982 if (*s == 'd' || *s == 'D') {
1983 user_zonelist_order = ZONELIST_ORDER_DEFAULT;
1984 } else if (*s == 'n' || *s == 'N') {
1985 user_zonelist_order = ZONELIST_ORDER_NODE;
1986 } else if (*s == 'z' || *s == 'Z') {
1987 user_zonelist_order = ZONELIST_ORDER_ZONE;
1988 } else {
1989 printk(KERN_WARNING
1990 "Ignoring invalid numa_zonelist_order value: "
1991 "%s\n", s);
1992 return -EINVAL;
1994 return 0;
1997 static __init int setup_numa_zonelist_order(char *s)
1999 if (s)
2000 return __parse_numa_zonelist_order(s);
2001 return 0;
2003 early_param("numa_zonelist_order", setup_numa_zonelist_order);
2006 * sysctl handler for numa_zonelist_order
2008 int numa_zonelist_order_handler(ctl_table *table, int write,
2009 struct file *file, void __user *buffer, size_t *length,
2010 loff_t *ppos)
2012 char saved_string[NUMA_ZONELIST_ORDER_LEN];
2013 int ret;
2015 if (write)
2016 strncpy(saved_string, (char*)table->data,
2017 NUMA_ZONELIST_ORDER_LEN);
2018 ret = proc_dostring(table, write, file, buffer, length, ppos);
2019 if (ret)
2020 return ret;
2021 if (write) {
2022 int oldval = user_zonelist_order;
2023 if (__parse_numa_zonelist_order((char*)table->data)) {
2025 * bogus value. restore saved string
2027 strncpy((char*)table->data, saved_string,
2028 NUMA_ZONELIST_ORDER_LEN);
2029 user_zonelist_order = oldval;
2030 } else if (oldval != user_zonelist_order)
2031 build_all_zonelists();
2033 return 0;
2037 #define MAX_NODE_LOAD (num_online_nodes())
2038 static int node_load[MAX_NUMNODES];
2041 * find_next_best_node - find the next node that should appear in a given node's fallback list
2042 * @node: node whose fallback list we're appending
2043 * @used_node_mask: nodemask_t of already used nodes
2045 * We use a number of factors to determine which is the next node that should
2046 * appear on a given node's fallback list. The node should not have appeared
2047 * already in @node's fallback list, and it should be the next closest node
2048 * according to the distance array (which contains arbitrary distance values
2049 * from each node to each node in the system), and should also prefer nodes
2050 * with no CPUs, since presumably they'll have very little allocation pressure
2051 * on them otherwise.
2052 * It returns -1 if no node is found.
2054 static int find_next_best_node(int node, nodemask_t *used_node_mask)
2056 int n, val;
2057 int min_val = INT_MAX;
2058 int best_node = -1;
2060 /* Use the local node if we haven't already */
2061 if (!node_isset(node, *used_node_mask)) {
2062 node_set(node, *used_node_mask);
2063 return node;
2066 for_each_node_state(n, N_HIGH_MEMORY) {
2067 cpumask_t tmp;
2069 /* Don't want a node to appear more than once */
2070 if (node_isset(n, *used_node_mask))
2071 continue;
2073 /* Use the distance array to find the distance */
2074 val = node_distance(node, n);
2076 /* Penalize nodes under us ("prefer the next node") */
2077 val += (n < node);
2079 /* Give preference to headless and unused nodes */
2080 tmp = node_to_cpumask(n);
2081 if (!cpus_empty(tmp))
2082 val += PENALTY_FOR_NODE_WITH_CPUS;
2084 /* Slight preference for less loaded node */
2085 val *= (MAX_NODE_LOAD*MAX_NUMNODES);
2086 val += node_load[n];
2088 if (val < min_val) {
2089 min_val = val;
2090 best_node = n;
2094 if (best_node >= 0)
2095 node_set(best_node, *used_node_mask);
2097 return best_node;
2102 * Build zonelists ordered by node and zones within node.
2103 * This results in maximum locality--normal zone overflows into local
2104 * DMA zone, if any--but risks exhausting DMA zone.
2106 static void build_zonelists_in_node_order(pg_data_t *pgdat, int node)
2108 enum zone_type i;
2109 int j;
2110 struct zonelist *zonelist;
2112 for (i = 0; i < MAX_NR_ZONES; i++) {
2113 zonelist = pgdat->node_zonelists + i;
2114 for (j = 0; zonelist->zones[j] != NULL; j++)
2116 j = build_zonelists_node(NODE_DATA(node), zonelist, j, i);
2117 zonelist->zones[j] = NULL;
2122 * Build gfp_thisnode zonelists
2124 static void build_thisnode_zonelists(pg_data_t *pgdat)
2126 enum zone_type i;
2127 int j;
2128 struct zonelist *zonelist;
2130 for (i = 0; i < MAX_NR_ZONES; i++) {
2131 zonelist = pgdat->node_zonelists + MAX_NR_ZONES + i;
2132 j = build_zonelists_node(pgdat, zonelist, 0, i);
2133 zonelist->zones[j] = NULL;
2138 * Build zonelists ordered by zone and nodes within zones.
2139 * This results in conserving DMA zone[s] until all Normal memory is
2140 * exhausted, but results in overflowing to remote node while memory
2141 * may still exist in local DMA zone.
2143 static int node_order[MAX_NUMNODES];
2145 static void build_zonelists_in_zone_order(pg_data_t *pgdat, int nr_nodes)
2147 enum zone_type i;
2148 int pos, j, node;
2149 int zone_type; /* needs to be signed */
2150 struct zone *z;
2151 struct zonelist *zonelist;
2153 for (i = 0; i < MAX_NR_ZONES; i++) {
2154 zonelist = pgdat->node_zonelists + i;
2155 pos = 0;
2156 for (zone_type = i; zone_type >= 0; zone_type--) {
2157 for (j = 0; j < nr_nodes; j++) {
2158 node = node_order[j];
2159 z = &NODE_DATA(node)->node_zones[zone_type];
2160 if (populated_zone(z)) {
2161 zonelist->zones[pos++] = z;
2162 check_highest_zone(zone_type);
2166 zonelist->zones[pos] = NULL;
2170 static int default_zonelist_order(void)
2172 int nid, zone_type;
2173 unsigned long low_kmem_size,total_size;
2174 struct zone *z;
2175 int average_size;
2177 * ZONE_DMA and ZONE_DMA32 can be very small area in the sytem.
2178 * If they are really small and used heavily, the system can fall
2179 * into OOM very easily.
2180 * This function detect ZONE_DMA/DMA32 size and confgigures zone order.
2182 /* Is there ZONE_NORMAL ? (ex. ppc has only DMA zone..) */
2183 low_kmem_size = 0;
2184 total_size = 0;
2185 for_each_online_node(nid) {
2186 for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
2187 z = &NODE_DATA(nid)->node_zones[zone_type];
2188 if (populated_zone(z)) {
2189 if (zone_type < ZONE_NORMAL)
2190 low_kmem_size += z->present_pages;
2191 total_size += z->present_pages;
2195 if (!low_kmem_size || /* there are no DMA area. */
2196 low_kmem_size > total_size/2) /* DMA/DMA32 is big. */
2197 return ZONELIST_ORDER_NODE;
2199 * look into each node's config.
2200 * If there is a node whose DMA/DMA32 memory is very big area on
2201 * local memory, NODE_ORDER may be suitable.
2203 average_size = total_size /
2204 (nodes_weight(node_states[N_HIGH_MEMORY]) + 1);
2205 for_each_online_node(nid) {
2206 low_kmem_size = 0;
2207 total_size = 0;
2208 for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
2209 z = &NODE_DATA(nid)->node_zones[zone_type];
2210 if (populated_zone(z)) {
2211 if (zone_type < ZONE_NORMAL)
2212 low_kmem_size += z->present_pages;
2213 total_size += z->present_pages;
2216 if (low_kmem_size &&
2217 total_size > average_size && /* ignore small node */
2218 low_kmem_size > total_size * 70/100)
2219 return ZONELIST_ORDER_NODE;
2221 return ZONELIST_ORDER_ZONE;
2224 static void set_zonelist_order(void)
2226 if (user_zonelist_order == ZONELIST_ORDER_DEFAULT)
2227 current_zonelist_order = default_zonelist_order();
2228 else
2229 current_zonelist_order = user_zonelist_order;
2232 static void build_zonelists(pg_data_t *pgdat)
2234 int j, node, load;
2235 enum zone_type i;
2236 nodemask_t used_mask;
2237 int local_node, prev_node;
2238 struct zonelist *zonelist;
2239 int order = current_zonelist_order;
2241 /* initialize zonelists */
2242 for (i = 0; i < MAX_ZONELISTS; i++) {
2243 zonelist = pgdat->node_zonelists + i;
2244 zonelist->zones[0] = NULL;
2247 /* NUMA-aware ordering of nodes */
2248 local_node = pgdat->node_id;
2249 load = num_online_nodes();
2250 prev_node = local_node;
2251 nodes_clear(used_mask);
2253 memset(node_load, 0, sizeof(node_load));
2254 memset(node_order, 0, sizeof(node_order));
2255 j = 0;
2257 while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
2258 int distance = node_distance(local_node, node);
2261 * If another node is sufficiently far away then it is better
2262 * to reclaim pages in a zone before going off node.
2264 if (distance > RECLAIM_DISTANCE)
2265 zone_reclaim_mode = 1;
2268 * We don't want to pressure a particular node.
2269 * So adding penalty to the first node in same
2270 * distance group to make it round-robin.
2272 if (distance != node_distance(local_node, prev_node))
2273 node_load[node] = load;
2275 prev_node = node;
2276 load--;
2277 if (order == ZONELIST_ORDER_NODE)
2278 build_zonelists_in_node_order(pgdat, node);
2279 else
2280 node_order[j++] = node; /* remember order */
2283 if (order == ZONELIST_ORDER_ZONE) {
2284 /* calculate node order -- i.e., DMA last! */
2285 build_zonelists_in_zone_order(pgdat, j);
2288 build_thisnode_zonelists(pgdat);
2291 /* Construct the zonelist performance cache - see further mmzone.h */
2292 static void build_zonelist_cache(pg_data_t *pgdat)
2294 int i;
2296 for (i = 0; i < MAX_NR_ZONES; i++) {
2297 struct zonelist *zonelist;
2298 struct zonelist_cache *zlc;
2299 struct zone **z;
2301 zonelist = pgdat->node_zonelists + i;
2302 zonelist->zlcache_ptr = zlc = &zonelist->zlcache;
2303 bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
2304 for (z = zonelist->zones; *z; z++)
2305 zlc->z_to_n[z - zonelist->zones] = zone_to_nid(*z);
2310 #else /* CONFIG_NUMA */
2312 static void set_zonelist_order(void)
2314 current_zonelist_order = ZONELIST_ORDER_ZONE;
2317 static void build_zonelists(pg_data_t *pgdat)
2319 int node, local_node;
2320 enum zone_type i,j;
2322 local_node = pgdat->node_id;
2323 for (i = 0; i < MAX_NR_ZONES; i++) {
2324 struct zonelist *zonelist;
2326 zonelist = pgdat->node_zonelists + i;
2328 j = build_zonelists_node(pgdat, zonelist, 0, i);
2330 * Now we build the zonelist so that it contains the zones
2331 * of all the other nodes.
2332 * We don't want to pressure a particular node, so when
2333 * building the zones for node N, we make sure that the
2334 * zones coming right after the local ones are those from
2335 * node N+1 (modulo N)
2337 for (node = local_node + 1; node < MAX_NUMNODES; node++) {
2338 if (!node_online(node))
2339 continue;
2340 j = build_zonelists_node(NODE_DATA(node), zonelist, j, i);
2342 for (node = 0; node < local_node; node++) {
2343 if (!node_online(node))
2344 continue;
2345 j = build_zonelists_node(NODE_DATA(node), zonelist, j, i);
2348 zonelist->zones[j] = NULL;
2352 /* non-NUMA variant of zonelist performance cache - just NULL zlcache_ptr */
2353 static void build_zonelist_cache(pg_data_t *pgdat)
2355 int i;
2357 for (i = 0; i < MAX_NR_ZONES; i++)
2358 pgdat->node_zonelists[i].zlcache_ptr = NULL;
2361 #endif /* CONFIG_NUMA */
2363 /* return values int ....just for stop_machine_run() */
2364 static int __build_all_zonelists(void *dummy)
2366 int nid;
2368 for_each_online_node(nid) {
2369 pg_data_t *pgdat = NODE_DATA(nid);
2371 build_zonelists(pgdat);
2372 build_zonelist_cache(pgdat);
2374 return 0;
2377 void build_all_zonelists(void)
2379 set_zonelist_order();
2381 if (system_state == SYSTEM_BOOTING) {
2382 __build_all_zonelists(NULL);
2383 cpuset_init_current_mems_allowed();
2384 } else {
2385 /* we have to stop all cpus to guarantee there is no user
2386 of zonelist */
2387 stop_machine_run(__build_all_zonelists, NULL, NR_CPUS);
2388 /* cpuset refresh routine should be here */
2390 vm_total_pages = nr_free_pagecache_pages();
2392 * Disable grouping by mobility if the number of pages in the
2393 * system is too low to allow the mechanism to work. It would be
2394 * more accurate, but expensive to check per-zone. This check is
2395 * made on memory-hotadd so a system can start with mobility
2396 * disabled and enable it later
2398 if (vm_total_pages < (pageblock_nr_pages * MIGRATE_TYPES))
2399 page_group_by_mobility_disabled = 1;
2400 else
2401 page_group_by_mobility_disabled = 0;
2403 printk("Built %i zonelists in %s order, mobility grouping %s. "
2404 "Total pages: %ld\n",
2405 num_online_nodes(),
2406 zonelist_order_name[current_zonelist_order],
2407 page_group_by_mobility_disabled ? "off" : "on",
2408 vm_total_pages);
2409 #ifdef CONFIG_NUMA
2410 printk("Policy zone: %s\n", zone_names[policy_zone]);
2411 #endif
2415 * Helper functions to size the waitqueue hash table.
2416 * Essentially these want to choose hash table sizes sufficiently
2417 * large so that collisions trying to wait on pages are rare.
2418 * But in fact, the number of active page waitqueues on typical
2419 * systems is ridiculously low, less than 200. So this is even
2420 * conservative, even though it seems large.
2422 * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
2423 * waitqueues, i.e. the size of the waitq table given the number of pages.
2425 #define PAGES_PER_WAITQUEUE 256
2427 #ifndef CONFIG_MEMORY_HOTPLUG
2428 static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
2430 unsigned long size = 1;
2432 pages /= PAGES_PER_WAITQUEUE;
2434 while (size < pages)
2435 size <<= 1;
2438 * Once we have dozens or even hundreds of threads sleeping
2439 * on IO we've got bigger problems than wait queue collision.
2440 * Limit the size of the wait table to a reasonable size.
2442 size = min(size, 4096UL);
2444 return max(size, 4UL);
2446 #else
2448 * A zone's size might be changed by hot-add, so it is not possible to determine
2449 * a suitable size for its wait_table. So we use the maximum size now.
2451 * The max wait table size = 4096 x sizeof(wait_queue_head_t). ie:
2453 * i386 (preemption config) : 4096 x 16 = 64Kbyte.
2454 * ia64, x86-64 (no preemption): 4096 x 20 = 80Kbyte.
2455 * ia64, x86-64 (preemption) : 4096 x 24 = 96Kbyte.
2457 * The maximum entries are prepared when a zone's memory is (512K + 256) pages
2458 * or more by the traditional way. (See above). It equals:
2460 * i386, x86-64, powerpc(4K page size) : = ( 2G + 1M)byte.
2461 * ia64(16K page size) : = ( 8G + 4M)byte.
2462 * powerpc (64K page size) : = (32G +16M)byte.
2464 static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
2466 return 4096UL;
2468 #endif
2471 * This is an integer logarithm so that shifts can be used later
2472 * to extract the more random high bits from the multiplicative
2473 * hash function before the remainder is taken.
2475 static inline unsigned long wait_table_bits(unsigned long size)
2477 return ffz(~size);
2480 #define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
2483 * Mark a number of pageblocks as MIGRATE_RESERVE. The number
2484 * of blocks reserved is based on zone->pages_min. The memory within the
2485 * reserve will tend to store contiguous free pages. Setting min_free_kbytes
2486 * higher will lead to a bigger reserve which will get freed as contiguous
2487 * blocks as reclaim kicks in
2489 static void setup_zone_migrate_reserve(struct zone *zone)
2491 unsigned long start_pfn, pfn, end_pfn;
2492 struct page *page;
2493 unsigned long reserve, block_migratetype;
2495 /* Get the start pfn, end pfn and the number of blocks to reserve */
2496 start_pfn = zone->zone_start_pfn;
2497 end_pfn = start_pfn + zone->spanned_pages;
2498 reserve = roundup(zone->pages_min, pageblock_nr_pages) >>
2499 pageblock_order;
2501 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
2502 if (!pfn_valid(pfn))
2503 continue;
2504 page = pfn_to_page(pfn);
2506 /* Blocks with reserved pages will never free, skip them. */
2507 if (PageReserved(page))
2508 continue;
2510 block_migratetype = get_pageblock_migratetype(page);
2512 /* If this block is reserved, account for it */
2513 if (reserve > 0 && block_migratetype == MIGRATE_RESERVE) {
2514 reserve--;
2515 continue;
2518 /* Suitable for reserving if this block is movable */
2519 if (reserve > 0 && block_migratetype == MIGRATE_MOVABLE) {
2520 set_pageblock_migratetype(page, MIGRATE_RESERVE);
2521 move_freepages_block(zone, page, MIGRATE_RESERVE);
2522 reserve--;
2523 continue;
2527 * If the reserve is met and this is a previous reserved block,
2528 * take it back
2530 if (block_migratetype == MIGRATE_RESERVE) {
2531 set_pageblock_migratetype(page, MIGRATE_MOVABLE);
2532 move_freepages_block(zone, page, MIGRATE_MOVABLE);
2538 * Initially all pages are reserved - free ones are freed
2539 * up by free_all_bootmem() once the early boot process is
2540 * done. Non-atomic initialization, single-pass.
2542 void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
2543 unsigned long start_pfn, enum memmap_context context)
2545 struct page *page;
2546 unsigned long end_pfn = start_pfn + size;
2547 unsigned long pfn;
2549 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
2551 * There can be holes in boot-time mem_map[]s
2552 * handed to this function. They do not
2553 * exist on hotplugged memory.
2555 if (context == MEMMAP_EARLY) {
2556 if (!early_pfn_valid(pfn))
2557 continue;
2558 if (!early_pfn_in_nid(pfn, nid))
2559 continue;
2561 page = pfn_to_page(pfn);
2562 set_page_links(page, zone, nid, pfn);
2563 init_page_count(page);
2564 reset_page_mapcount(page);
2565 <<<<<<< HEAD:mm/page_alloc.c
2566 page_assign_page_cgroup(page, NULL);
2567 =======
2568 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:mm/page_alloc.c
2569 SetPageReserved(page);
2572 * Mark the block movable so that blocks are reserved for
2573 * movable at startup. This will force kernel allocations
2574 * to reserve their blocks rather than leaking throughout
2575 * the address space during boot when many long-lived
2576 * kernel allocations are made. Later some blocks near
2577 * the start are marked MIGRATE_RESERVE by
2578 * setup_zone_migrate_reserve()
2580 if ((pfn & (pageblock_nr_pages-1)))
2581 set_pageblock_migratetype(page, MIGRATE_MOVABLE);
2583 INIT_LIST_HEAD(&page->lru);
2584 #ifdef WANT_PAGE_VIRTUAL
2585 /* The shift won't overflow because ZONE_NORMAL is below 4G. */
2586 if (!is_highmem_idx(zone))
2587 set_page_address(page, __va(pfn << PAGE_SHIFT));
2588 #endif
2592 static void __meminit zone_init_free_lists(struct zone *zone)
2594 int order, t;
2595 for_each_migratetype_order(order, t) {
2596 INIT_LIST_HEAD(&zone->free_area[order].free_list[t]);
2597 zone->free_area[order].nr_free = 0;
2601 #ifndef __HAVE_ARCH_MEMMAP_INIT
2602 #define memmap_init(size, nid, zone, start_pfn) \
2603 memmap_init_zone((size), (nid), (zone), (start_pfn), MEMMAP_EARLY)
2604 #endif
2606 static int zone_batchsize(struct zone *zone)
2608 int batch;
2611 * The per-cpu-pages pools are set to around 1000th of the
2612 * size of the zone. But no more than 1/2 of a meg.
2614 * OK, so we don't know how big the cache is. So guess.
2616 batch = zone->present_pages / 1024;
2617 if (batch * PAGE_SIZE > 512 * 1024)
2618 batch = (512 * 1024) / PAGE_SIZE;
2619 batch /= 4; /* We effectively *= 4 below */
2620 if (batch < 1)
2621 batch = 1;
2624 * Clamp the batch to a 2^n - 1 value. Having a power
2625 * of 2 value was found to be more likely to have
2626 * suboptimal cache aliasing properties in some cases.
2628 * For example if 2 tasks are alternately allocating
2629 * batches of pages, one task can end up with a lot
2630 * of pages of one half of the possible page colors
2631 * and the other with pages of the other colors.
2633 batch = (1 << (fls(batch + batch/2)-1)) - 1;
2635 return batch;
2638 inline void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
2640 struct per_cpu_pages *pcp;
2642 memset(p, 0, sizeof(*p));
2644 pcp = &p->pcp;
2645 pcp->count = 0;
2646 pcp->high = 6 * batch;
2647 pcp->batch = max(1UL, 1 * batch);
2648 INIT_LIST_HEAD(&pcp->list);
2652 * setup_pagelist_highmark() sets the high water mark for hot per_cpu_pagelist
2653 * to the value high for the pageset p.
2656 static void setup_pagelist_highmark(struct per_cpu_pageset *p,
2657 unsigned long high)
2659 struct per_cpu_pages *pcp;
2661 pcp = &p->pcp;
2662 pcp->high = high;
2663 pcp->batch = max(1UL, high/4);
2664 if ((high/4) > (PAGE_SHIFT * 8))
2665 pcp->batch = PAGE_SHIFT * 8;
2669 #ifdef CONFIG_NUMA
2671 * Boot pageset table. One per cpu which is going to be used for all
2672 * zones and all nodes. The parameters will be set in such a way
2673 * that an item put on a list will immediately be handed over to
2674 * the buddy list. This is safe since pageset manipulation is done
2675 * with interrupts disabled.
2677 * Some NUMA counter updates may also be caught by the boot pagesets.
2679 * The boot_pagesets must be kept even after bootup is complete for
2680 * unused processors and/or zones. They do play a role for bootstrapping
2681 * hotplugged processors.
2683 * zoneinfo_show() and maybe other functions do
2684 * not check if the processor is online before following the pageset pointer.
2685 * Other parts of the kernel may not check if the zone is available.
2687 static struct per_cpu_pageset boot_pageset[NR_CPUS];
2690 * Dynamically allocate memory for the
2691 * per cpu pageset array in struct zone.
2693 static int __cpuinit process_zones(int cpu)
2695 struct zone *zone, *dzone;
2696 int node = cpu_to_node(cpu);
2698 node_set_state(node, N_CPU); /* this node has a cpu */
2700 for_each_zone(zone) {
2702 if (!populated_zone(zone))
2703 continue;
2705 zone_pcp(zone, cpu) = kmalloc_node(sizeof(struct per_cpu_pageset),
2706 GFP_KERNEL, node);
2707 if (!zone_pcp(zone, cpu))
2708 goto bad;
2710 setup_pageset(zone_pcp(zone, cpu), zone_batchsize(zone));
2712 if (percpu_pagelist_fraction)
2713 setup_pagelist_highmark(zone_pcp(zone, cpu),
2714 (zone->present_pages / percpu_pagelist_fraction));
2717 return 0;
2718 bad:
2719 for_each_zone(dzone) {
2720 if (!populated_zone(dzone))
2721 continue;
2722 if (dzone == zone)
2723 break;
2724 kfree(zone_pcp(dzone, cpu));
2725 zone_pcp(dzone, cpu) = NULL;
2727 return -ENOMEM;
2730 static inline void free_zone_pagesets(int cpu)
2732 struct zone *zone;
2734 for_each_zone(zone) {
2735 struct per_cpu_pageset *pset = zone_pcp(zone, cpu);
2737 /* Free per_cpu_pageset if it is slab allocated */
2738 if (pset != &boot_pageset[cpu])
2739 kfree(pset);
2740 zone_pcp(zone, cpu) = NULL;
2744 static int __cpuinit pageset_cpuup_callback(struct notifier_block *nfb,
2745 unsigned long action,
2746 void *hcpu)
2748 int cpu = (long)hcpu;
2749 int ret = NOTIFY_OK;
2751 switch (action) {
2752 case CPU_UP_PREPARE:
2753 case CPU_UP_PREPARE_FROZEN:
2754 if (process_zones(cpu))
2755 ret = NOTIFY_BAD;
2756 break;
2757 case CPU_UP_CANCELED:
2758 case CPU_UP_CANCELED_FROZEN:
2759 case CPU_DEAD:
2760 case CPU_DEAD_FROZEN:
2761 free_zone_pagesets(cpu);
2762 break;
2763 default:
2764 break;
2766 return ret;
2769 static struct notifier_block __cpuinitdata pageset_notifier =
2770 { &pageset_cpuup_callback, NULL, 0 };
2772 void __init setup_per_cpu_pageset(void)
2774 int err;
2776 /* Initialize per_cpu_pageset for cpu 0.
2777 * A cpuup callback will do this for every cpu
2778 * as it comes online
2780 err = process_zones(smp_processor_id());
2781 BUG_ON(err);
2782 register_cpu_notifier(&pageset_notifier);
2785 #endif
2787 static noinline __init_refok
2788 int zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages)
2790 int i;
2791 struct pglist_data *pgdat = zone->zone_pgdat;
2792 size_t alloc_size;
2795 * The per-page waitqueue mechanism uses hashed waitqueues
2796 * per zone.
2798 zone->wait_table_hash_nr_entries =
2799 wait_table_hash_nr_entries(zone_size_pages);
2800 zone->wait_table_bits =
2801 wait_table_bits(zone->wait_table_hash_nr_entries);
2802 alloc_size = zone->wait_table_hash_nr_entries
2803 * sizeof(wait_queue_head_t);
2805 if (system_state == SYSTEM_BOOTING) {
2806 zone->wait_table = (wait_queue_head_t *)
2807 alloc_bootmem_node(pgdat, alloc_size);
2808 } else {
2810 * This case means that a zone whose size was 0 gets new memory
2811 * via memory hot-add.
2812 * But it may be the case that a new node was hot-added. In
2813 * this case vmalloc() will not be able to use this new node's
2814 * memory - this wait_table must be initialized to use this new
2815 * node itself as well.
2816 * To use this new node's memory, further consideration will be
2817 * necessary.
2819 zone->wait_table = vmalloc(alloc_size);
2821 if (!zone->wait_table)
2822 return -ENOMEM;
2824 for(i = 0; i < zone->wait_table_hash_nr_entries; ++i)
2825 init_waitqueue_head(zone->wait_table + i);
2827 return 0;
2830 static __meminit void zone_pcp_init(struct zone *zone)
2832 int cpu;
2833 unsigned long batch = zone_batchsize(zone);
2835 for (cpu = 0; cpu < NR_CPUS; cpu++) {
2836 #ifdef CONFIG_NUMA
2837 /* Early boot. Slab allocator not functional yet */
2838 zone_pcp(zone, cpu) = &boot_pageset[cpu];
2839 setup_pageset(&boot_pageset[cpu],0);
2840 #else
2841 setup_pageset(zone_pcp(zone,cpu), batch);
2842 #endif
2844 if (zone->present_pages)
2845 printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%lu\n",
2846 zone->name, zone->present_pages, batch);
2849 __meminit int init_currently_empty_zone(struct zone *zone,
2850 unsigned long zone_start_pfn,
2851 unsigned long size,
2852 enum memmap_context context)
2854 struct pglist_data *pgdat = zone->zone_pgdat;
2855 int ret;
2856 ret = zone_wait_table_init(zone, size);
2857 if (ret)
2858 return ret;
2859 pgdat->nr_zones = zone_idx(zone) + 1;
2861 zone->zone_start_pfn = zone_start_pfn;
2863 memmap_init(size, pgdat->node_id, zone_idx(zone), zone_start_pfn);
2865 zone_init_free_lists(zone);
2867 return 0;
2870 #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
2872 * Basic iterator support. Return the first range of PFNs for a node
2873 * Note: nid == MAX_NUMNODES returns first region regardless of node
2875 static int __meminit first_active_region_index_in_nid(int nid)
2877 int i;
2879 for (i = 0; i < nr_nodemap_entries; i++)
2880 if (nid == MAX_NUMNODES || early_node_map[i].nid == nid)
2881 return i;
2883 return -1;
2887 * Basic iterator support. Return the next active range of PFNs for a node
2888 * Note: nid == MAX_NUMNODES returns next region regardless of node
2890 static int __meminit next_active_region_index_in_nid(int index, int nid)
2892 for (index = index + 1; index < nr_nodemap_entries; index++)
2893 if (nid == MAX_NUMNODES || early_node_map[index].nid == nid)
2894 return index;
2896 return -1;
2899 #ifndef CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID
2901 * Required by SPARSEMEM. Given a PFN, return what node the PFN is on.
2902 * Architectures may implement their own version but if add_active_range()
2903 * was used and there are no special requirements, this is a convenient
2904 * alternative
2906 int __meminit early_pfn_to_nid(unsigned long pfn)
2908 int i;
2910 for (i = 0; i < nr_nodemap_entries; i++) {
2911 unsigned long start_pfn = early_node_map[i].start_pfn;
2912 unsigned long end_pfn = early_node_map[i].end_pfn;
2914 if (start_pfn <= pfn && pfn < end_pfn)
2915 return early_node_map[i].nid;
2918 return 0;
2920 #endif /* CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID */
2922 /* Basic iterator support to walk early_node_map[] */
2923 #define for_each_active_range_index_in_nid(i, nid) \
2924 for (i = first_active_region_index_in_nid(nid); i != -1; \
2925 i = next_active_region_index_in_nid(i, nid))
2928 * free_bootmem_with_active_regions - Call free_bootmem_node for each active range
2929 * @nid: The node to free memory on. If MAX_NUMNODES, all nodes are freed.
2930 * @max_low_pfn: The highest PFN that will be passed to free_bootmem_node
2932 * If an architecture guarantees that all ranges registered with
2933 * add_active_ranges() contain no holes and may be freed, this
2934 * this function may be used instead of calling free_bootmem() manually.
2936 void __init free_bootmem_with_active_regions(int nid,
2937 unsigned long max_low_pfn)
2939 int i;
2941 for_each_active_range_index_in_nid(i, nid) {
2942 unsigned long size_pages = 0;
2943 unsigned long end_pfn = early_node_map[i].end_pfn;
2945 if (early_node_map[i].start_pfn >= max_low_pfn)
2946 continue;
2948 if (end_pfn > max_low_pfn)
2949 end_pfn = max_low_pfn;
2951 size_pages = end_pfn - early_node_map[i].start_pfn;
2952 free_bootmem_node(NODE_DATA(early_node_map[i].nid),
2953 PFN_PHYS(early_node_map[i].start_pfn),
2954 size_pages << PAGE_SHIFT);
2959 * sparse_memory_present_with_active_regions - Call memory_present for each active range
2960 * @nid: The node to call memory_present for. If MAX_NUMNODES, all nodes will be used.
2962 * If an architecture guarantees that all ranges registered with
2963 * add_active_ranges() contain no holes and may be freed, this
2964 * function may be used instead of calling memory_present() manually.
2966 void __init sparse_memory_present_with_active_regions(int nid)
2968 int i;
2970 for_each_active_range_index_in_nid(i, nid)
2971 memory_present(early_node_map[i].nid,
2972 early_node_map[i].start_pfn,
2973 early_node_map[i].end_pfn);
2977 * push_node_boundaries - Push node boundaries to at least the requested boundary
2978 * @nid: The nid of the node to push the boundary for
2979 * @start_pfn: The start pfn of the node
2980 * @end_pfn: The end pfn of the node
2982 * In reserve-based hot-add, mem_map is allocated that is unused until hotadd
2983 * time. Specifically, on x86_64, SRAT will report ranges that can potentially
2984 * be hotplugged even though no physical memory exists. This function allows
2985 * an arch to push out the node boundaries so mem_map is allocated that can
2986 * be used later.
2988 #ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
2989 void __init push_node_boundaries(unsigned int nid,
2990 unsigned long start_pfn, unsigned long end_pfn)
2992 printk(KERN_DEBUG "Entering push_node_boundaries(%u, %lu, %lu)\n",
2993 nid, start_pfn, end_pfn);
2995 /* Initialise the boundary for this node if necessary */
2996 if (node_boundary_end_pfn[nid] == 0)
2997 node_boundary_start_pfn[nid] = -1UL;
2999 /* Update the boundaries */
3000 if (node_boundary_start_pfn[nid] > start_pfn)
3001 node_boundary_start_pfn[nid] = start_pfn;
3002 if (node_boundary_end_pfn[nid] < end_pfn)
3003 node_boundary_end_pfn[nid] = end_pfn;
3006 /* If necessary, push the node boundary out for reserve hotadd */
3007 static void __meminit account_node_boundary(unsigned int nid,
3008 unsigned long *start_pfn, unsigned long *end_pfn)
3010 printk(KERN_DEBUG "Entering account_node_boundary(%u, %lu, %lu)\n",
3011 nid, *start_pfn, *end_pfn);
3013 /* Return if boundary information has not been provided */
3014 if (node_boundary_end_pfn[nid] == 0)
3015 return;
3017 /* Check the boundaries and update if necessary */
3018 if (node_boundary_start_pfn[nid] < *start_pfn)
3019 *start_pfn = node_boundary_start_pfn[nid];
3020 if (node_boundary_end_pfn[nid] > *end_pfn)
3021 *end_pfn = node_boundary_end_pfn[nid];
3023 #else
3024 void __init push_node_boundaries(unsigned int nid,
3025 unsigned long start_pfn, unsigned long end_pfn) {}
3027 static void __meminit account_node_boundary(unsigned int nid,
3028 unsigned long *start_pfn, unsigned long *end_pfn) {}
3029 #endif
3033 * get_pfn_range_for_nid - Return the start and end page frames for a node
3034 * @nid: The nid to return the range for. If MAX_NUMNODES, the min and max PFN are returned.
3035 * @start_pfn: Passed by reference. On return, it will have the node start_pfn.
3036 * @end_pfn: Passed by reference. On return, it will have the node end_pfn.
3038 * It returns the start and end page frame of a node based on information
3039 * provided by an arch calling add_active_range(). If called for a node
3040 * with no available memory, a warning is printed and the start and end
3041 * PFNs will be 0.
3043 void __meminit get_pfn_range_for_nid(unsigned int nid,
3044 unsigned long *start_pfn, unsigned long *end_pfn)
3046 int i;
3047 *start_pfn = -1UL;
3048 *end_pfn = 0;
3050 for_each_active_range_index_in_nid(i, nid) {
3051 *start_pfn = min(*start_pfn, early_node_map[i].start_pfn);
3052 *end_pfn = max(*end_pfn, early_node_map[i].end_pfn);
3055 if (*start_pfn == -1UL)
3056 *start_pfn = 0;
3058 /* Push the node boundaries out if requested */
3059 account_node_boundary(nid, start_pfn, end_pfn);
3063 * This finds a zone that can be used for ZONE_MOVABLE pages. The
3064 * assumption is made that zones within a node are ordered in monotonic
3065 * increasing memory addresses so that the "highest" populated zone is used
3067 void __init find_usable_zone_for_movable(void)
3069 int zone_index;
3070 for (zone_index = MAX_NR_ZONES - 1; zone_index >= 0; zone_index--) {
3071 if (zone_index == ZONE_MOVABLE)
3072 continue;
3074 if (arch_zone_highest_possible_pfn[zone_index] >
3075 arch_zone_lowest_possible_pfn[zone_index])
3076 break;
3079 VM_BUG_ON(zone_index == -1);
3080 movable_zone = zone_index;
3084 * The zone ranges provided by the architecture do not include ZONE_MOVABLE
3085 * because it is sized independant of architecture. Unlike the other zones,
3086 * the starting point for ZONE_MOVABLE is not fixed. It may be different
3087 * in each node depending on the size of each node and how evenly kernelcore
3088 * is distributed. This helper function adjusts the zone ranges
3089 * provided by the architecture for a given node by using the end of the
3090 * highest usable zone for ZONE_MOVABLE. This preserves the assumption that
3091 * zones within a node are in order of monotonic increases memory addresses
3093 void __meminit adjust_zone_range_for_zone_movable(int nid,
3094 unsigned long zone_type,
3095 unsigned long node_start_pfn,
3096 unsigned long node_end_pfn,
3097 unsigned long *zone_start_pfn,
3098 unsigned long *zone_end_pfn)
3100 /* Only adjust if ZONE_MOVABLE is on this node */
3101 if (zone_movable_pfn[nid]) {
3102 /* Size ZONE_MOVABLE */
3103 if (zone_type == ZONE_MOVABLE) {
3104 *zone_start_pfn = zone_movable_pfn[nid];
3105 *zone_end_pfn = min(node_end_pfn,
3106 arch_zone_highest_possible_pfn[movable_zone]);
3108 /* Adjust for ZONE_MOVABLE starting within this range */
3109 } else if (*zone_start_pfn < zone_movable_pfn[nid] &&
3110 *zone_end_pfn > zone_movable_pfn[nid]) {
3111 *zone_end_pfn = zone_movable_pfn[nid];
3113 /* Check if this whole range is within ZONE_MOVABLE */
3114 } else if (*zone_start_pfn >= zone_movable_pfn[nid])
3115 *zone_start_pfn = *zone_end_pfn;
3120 * Return the number of pages a zone spans in a node, including holes
3121 * present_pages = zone_spanned_pages_in_node() - zone_absent_pages_in_node()
3123 static unsigned long __meminit zone_spanned_pages_in_node(int nid,
3124 unsigned long zone_type,
3125 unsigned long *ignored)
3127 unsigned long node_start_pfn, node_end_pfn;
3128 unsigned long zone_start_pfn, zone_end_pfn;
3130 /* Get the start and end of the node and zone */
3131 get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
3132 zone_start_pfn = arch_zone_lowest_possible_pfn[zone_type];
3133 zone_end_pfn = arch_zone_highest_possible_pfn[zone_type];
3134 adjust_zone_range_for_zone_movable(nid, zone_type,
3135 node_start_pfn, node_end_pfn,
3136 &zone_start_pfn, &zone_end_pfn);
3138 /* Check that this node has pages within the zone's required range */
3139 if (zone_end_pfn < node_start_pfn || zone_start_pfn > node_end_pfn)
3140 return 0;
3142 /* Move the zone boundaries inside the node if necessary */
3143 zone_end_pfn = min(zone_end_pfn, node_end_pfn);
3144 zone_start_pfn = max(zone_start_pfn, node_start_pfn);
3146 /* Return the spanned pages */
3147 return zone_end_pfn - zone_start_pfn;
3151 * Return the number of holes in a range on a node. If nid is MAX_NUMNODES,
3152 * then all holes in the requested range will be accounted for.
3154 unsigned long __meminit __absent_pages_in_range(int nid,
3155 unsigned long range_start_pfn,
3156 unsigned long range_end_pfn)
3158 int i = 0;
3159 unsigned long prev_end_pfn = 0, hole_pages = 0;
3160 unsigned long start_pfn;
3162 /* Find the end_pfn of the first active range of pfns in the node */
3163 i = first_active_region_index_in_nid(nid);
3164 if (i == -1)
3165 return 0;
3167 prev_end_pfn = min(early_node_map[i].start_pfn, range_end_pfn);
3169 /* Account for ranges before physical memory on this node */
3170 if (early_node_map[i].start_pfn > range_start_pfn)
3171 hole_pages = prev_end_pfn - range_start_pfn;
3173 /* Find all holes for the zone within the node */
3174 for (; i != -1; i = next_active_region_index_in_nid(i, nid)) {
3176 /* No need to continue if prev_end_pfn is outside the zone */
3177 if (prev_end_pfn >= range_end_pfn)
3178 break;
3180 /* Make sure the end of the zone is not within the hole */
3181 start_pfn = min(early_node_map[i].start_pfn, range_end_pfn);
3182 prev_end_pfn = max(prev_end_pfn, range_start_pfn);
3184 /* Update the hole size cound and move on */
3185 if (start_pfn > range_start_pfn) {
3186 BUG_ON(prev_end_pfn > start_pfn);
3187 hole_pages += start_pfn - prev_end_pfn;
3189 prev_end_pfn = early_node_map[i].end_pfn;
3192 /* Account for ranges past physical memory on this node */
3193 if (range_end_pfn > prev_end_pfn)
3194 hole_pages += range_end_pfn -
3195 max(range_start_pfn, prev_end_pfn);
3197 return hole_pages;
3201 * absent_pages_in_range - Return number of page frames in holes within a range
3202 * @start_pfn: The start PFN to start searching for holes
3203 * @end_pfn: The end PFN to stop searching for holes
3205 * It returns the number of pages frames in memory holes within a range.
3207 unsigned long __init absent_pages_in_range(unsigned long start_pfn,
3208 unsigned long end_pfn)
3210 return __absent_pages_in_range(MAX_NUMNODES, start_pfn, end_pfn);
3213 /* Return the number of page frames in holes in a zone on a node */
3214 static unsigned long __meminit zone_absent_pages_in_node(int nid,
3215 unsigned long zone_type,
3216 unsigned long *ignored)
3218 unsigned long node_start_pfn, node_end_pfn;
3219 unsigned long zone_start_pfn, zone_end_pfn;
3221 get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
3222 zone_start_pfn = max(arch_zone_lowest_possible_pfn[zone_type],
3223 node_start_pfn);
3224 zone_end_pfn = min(arch_zone_highest_possible_pfn[zone_type],
3225 node_end_pfn);
3227 adjust_zone_range_for_zone_movable(nid, zone_type,
3228 node_start_pfn, node_end_pfn,
3229 &zone_start_pfn, &zone_end_pfn);
3230 return __absent_pages_in_range(nid, zone_start_pfn, zone_end_pfn);
3233 #else
3234 static inline unsigned long __meminit zone_spanned_pages_in_node(int nid,
3235 unsigned long zone_type,
3236 unsigned long *zones_size)
3238 return zones_size[zone_type];
3241 static inline unsigned long __meminit zone_absent_pages_in_node(int nid,
3242 unsigned long zone_type,
3243 unsigned long *zholes_size)
3245 if (!zholes_size)
3246 return 0;
3248 return zholes_size[zone_type];
3251 #endif
3253 static void __meminit calculate_node_totalpages(struct pglist_data *pgdat,
3254 unsigned long *zones_size, unsigned long *zholes_size)
3256 unsigned long realtotalpages, totalpages = 0;
3257 enum zone_type i;
3259 for (i = 0; i < MAX_NR_ZONES; i++)
3260 totalpages += zone_spanned_pages_in_node(pgdat->node_id, i,
3261 zones_size);
3262 pgdat->node_spanned_pages = totalpages;
3264 realtotalpages = totalpages;
3265 for (i = 0; i < MAX_NR_ZONES; i++)
3266 realtotalpages -=
3267 zone_absent_pages_in_node(pgdat->node_id, i,
3268 zholes_size);
3269 pgdat->node_present_pages = realtotalpages;
3270 printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id,
3271 realtotalpages);
3274 #ifndef CONFIG_SPARSEMEM
3276 * Calculate the size of the zone->blockflags rounded to an unsigned long
3277 * Start by making sure zonesize is a multiple of pageblock_order by rounding
3278 * up. Then use 1 NR_PAGEBLOCK_BITS worth of bits per pageblock, finally
3279 * round what is now in bits to nearest long in bits, then return it in
3280 * bytes.
3282 static unsigned long __init usemap_size(unsigned long zonesize)
3284 unsigned long usemapsize;
3286 usemapsize = roundup(zonesize, pageblock_nr_pages);
3287 usemapsize = usemapsize >> pageblock_order;
3288 usemapsize *= NR_PAGEBLOCK_BITS;
3289 usemapsize = roundup(usemapsize, 8 * sizeof(unsigned long));
3291 return usemapsize / 8;
3294 static void __init setup_usemap(struct pglist_data *pgdat,
3295 struct zone *zone, unsigned long zonesize)
3297 unsigned long usemapsize = usemap_size(zonesize);
3298 zone->pageblock_flags = NULL;
3299 if (usemapsize) {
3300 zone->pageblock_flags = alloc_bootmem_node(pgdat, usemapsize);
3301 memset(zone->pageblock_flags, 0, usemapsize);
3304 #else
3305 static void inline setup_usemap(struct pglist_data *pgdat,
3306 struct zone *zone, unsigned long zonesize) {}
3307 #endif /* CONFIG_SPARSEMEM */
3309 #ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
3311 /* Return a sensible default order for the pageblock size. */
3312 static inline int pageblock_default_order(void)
3314 if (HPAGE_SHIFT > PAGE_SHIFT)
3315 return HUGETLB_PAGE_ORDER;
3317 return MAX_ORDER-1;
3320 /* Initialise the number of pages represented by NR_PAGEBLOCK_BITS */
3321 static inline void __init set_pageblock_order(unsigned int order)
3323 /* Check that pageblock_nr_pages has not already been setup */
3324 if (pageblock_order)
3325 return;
3328 * Assume the largest contiguous order of interest is a huge page.
3329 * This value may be variable depending on boot parameters on IA64
3331 pageblock_order = order;
3333 #else /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
3336 * When CONFIG_HUGETLB_PAGE_SIZE_VARIABLE is not set, set_pageblock_order()
3337 * and pageblock_default_order() are unused as pageblock_order is set
3338 * at compile-time. See include/linux/pageblock-flags.h for the values of
3339 * pageblock_order based on the kernel config
3341 static inline int pageblock_default_order(unsigned int order)
3343 return MAX_ORDER-1;
3345 #define set_pageblock_order(x) do {} while (0)
3347 #endif /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
3350 * Set up the zone data structures:
3351 * - mark all pages reserved
3352 * - mark all memory queues empty
3353 * - clear the memory bitmaps
3355 <<<<<<< HEAD:mm/page_alloc.c
3356 static void __meminit free_area_init_core(struct pglist_data *pgdat,
3357 =======
3358 static void __paginginit free_area_init_core(struct pglist_data *pgdat,
3359 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:mm/page_alloc.c
3360 unsigned long *zones_size, unsigned long *zholes_size)
3362 enum zone_type j;
3363 int nid = pgdat->node_id;
3364 unsigned long zone_start_pfn = pgdat->node_start_pfn;
3365 int ret;
3367 pgdat_resize_init(pgdat);
3368 pgdat->nr_zones = 0;
3369 init_waitqueue_head(&pgdat->kswapd_wait);
3370 pgdat->kswapd_max_order = 0;
3372 for (j = 0; j < MAX_NR_ZONES; j++) {
3373 struct zone *zone = pgdat->node_zones + j;
3374 unsigned long size, realsize, memmap_pages;
3376 size = zone_spanned_pages_in_node(nid, j, zones_size);
3377 realsize = size - zone_absent_pages_in_node(nid, j,
3378 zholes_size);
3381 * Adjust realsize so that it accounts for how much memory
3382 * is used by this zone for memmap. This affects the watermark
3383 * and per-cpu initialisations
3385 memmap_pages = (size * sizeof(struct page)) >> PAGE_SHIFT;
3386 if (realsize >= memmap_pages) {
3387 realsize -= memmap_pages;
3388 printk(KERN_DEBUG
3389 " %s zone: %lu pages used for memmap\n",
3390 zone_names[j], memmap_pages);
3391 } else
3392 printk(KERN_WARNING
3393 " %s zone: %lu pages exceeds realsize %lu\n",
3394 zone_names[j], memmap_pages, realsize);
3396 /* Account for reserved pages */
3397 if (j == 0 && realsize > dma_reserve) {
3398 realsize -= dma_reserve;
3399 printk(KERN_DEBUG " %s zone: %lu pages reserved\n",
3400 zone_names[0], dma_reserve);
3403 if (!is_highmem_idx(j))
3404 nr_kernel_pages += realsize;
3405 nr_all_pages += realsize;
3407 zone->spanned_pages = size;
3408 zone->present_pages = realsize;
3409 #ifdef CONFIG_NUMA
3410 zone->node = nid;
3411 zone->min_unmapped_pages = (realsize*sysctl_min_unmapped_ratio)
3412 / 100;
3413 zone->min_slab_pages = (realsize * sysctl_min_slab_ratio) / 100;
3414 #endif
3415 zone->name = zone_names[j];
3416 spin_lock_init(&zone->lock);
3417 spin_lock_init(&zone->lru_lock);
3418 zone_seqlock_init(zone);
3419 zone->zone_pgdat = pgdat;
3421 zone->prev_priority = DEF_PRIORITY;
3423 zone_pcp_init(zone);
3424 INIT_LIST_HEAD(&zone->active_list);
3425 INIT_LIST_HEAD(&zone->inactive_list);
3426 zone->nr_scan_active = 0;
3427 zone->nr_scan_inactive = 0;
3428 zap_zone_vm_stats(zone);
3429 zone->flags = 0;
3430 if (!size)
3431 continue;
3433 set_pageblock_order(pageblock_default_order());
3434 setup_usemap(pgdat, zone, size);
3435 ret = init_currently_empty_zone(zone, zone_start_pfn,
3436 size, MEMMAP_EARLY);
3437 BUG_ON(ret);
3438 zone_start_pfn += size;
3442 static void __init_refok alloc_node_mem_map(struct pglist_data *pgdat)
3444 /* Skip empty nodes */
3445 if (!pgdat->node_spanned_pages)
3446 return;
3448 #ifdef CONFIG_FLAT_NODE_MEM_MAP
3449 /* ia64 gets its own node_mem_map, before this, without bootmem */
3450 if (!pgdat->node_mem_map) {
3451 unsigned long size, start, end;
3452 struct page *map;
3455 * The zone's endpoints aren't required to be MAX_ORDER
3456 * aligned but the node_mem_map endpoints must be in order
3457 * for the buddy allocator to function correctly.
3459 start = pgdat->node_start_pfn & ~(MAX_ORDER_NR_PAGES - 1);
3460 end = pgdat->node_start_pfn + pgdat->node_spanned_pages;
3461 end = ALIGN(end, MAX_ORDER_NR_PAGES);
3462 size = (end - start) * sizeof(struct page);
3463 map = alloc_remap(pgdat->node_id, size);
3464 if (!map)
3465 map = alloc_bootmem_node(pgdat, size);
3466 pgdat->node_mem_map = map + (pgdat->node_start_pfn - start);
3468 #ifndef CONFIG_NEED_MULTIPLE_NODES
3470 * With no DISCONTIG, the global mem_map is just set as node 0's
3472 if (pgdat == NODE_DATA(0)) {
3473 mem_map = NODE_DATA(0)->node_mem_map;
3474 #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
3475 if (page_to_pfn(mem_map) != pgdat->node_start_pfn)
3476 mem_map -= (pgdat->node_start_pfn - ARCH_PFN_OFFSET);
3477 #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
3479 #endif
3480 #endif /* CONFIG_FLAT_NODE_MEM_MAP */
3483 <<<<<<< HEAD:mm/page_alloc.c
3484 void __meminit free_area_init_node(int nid, struct pglist_data *pgdat,
3485 =======
3486 void __paginginit free_area_init_node(int nid, struct pglist_data *pgdat,
3487 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:mm/page_alloc.c
3488 unsigned long *zones_size, unsigned long node_start_pfn,
3489 unsigned long *zholes_size)
3491 pgdat->node_id = nid;
3492 pgdat->node_start_pfn = node_start_pfn;
3493 calculate_node_totalpages(pgdat, zones_size, zholes_size);
3495 alloc_node_mem_map(pgdat);
3497 free_area_init_core(pgdat, zones_size, zholes_size);
3500 #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
3502 #if MAX_NUMNODES > 1
3504 * Figure out the number of possible node ids.
3506 static void __init setup_nr_node_ids(void)
3508 unsigned int node;
3509 unsigned int highest = 0;
3511 for_each_node_mask(node, node_possible_map)
3512 highest = node;
3513 nr_node_ids = highest + 1;
3515 #else
3516 static inline void setup_nr_node_ids(void)
3519 #endif
3522 * add_active_range - Register a range of PFNs backed by physical memory
3523 * @nid: The node ID the range resides on
3524 * @start_pfn: The start PFN of the available physical memory
3525 * @end_pfn: The end PFN of the available physical memory
3527 * These ranges are stored in an early_node_map[] and later used by
3528 * free_area_init_nodes() to calculate zone sizes and holes. If the
3529 * range spans a memory hole, it is up to the architecture to ensure
3530 * the memory is not freed by the bootmem allocator. If possible
3531 * the range being registered will be merged with existing ranges.
3533 void __init add_active_range(unsigned int nid, unsigned long start_pfn,
3534 unsigned long end_pfn)
3536 int i;
3538 printk(KERN_DEBUG "Entering add_active_range(%d, %lu, %lu) "
3539 "%d entries of %d used\n",
3540 nid, start_pfn, end_pfn,
3541 nr_nodemap_entries, MAX_ACTIVE_REGIONS);
3543 /* Merge with existing active regions if possible */
3544 for (i = 0; i < nr_nodemap_entries; i++) {
3545 if (early_node_map[i].nid != nid)
3546 continue;
3548 /* Skip if an existing region covers this new one */
3549 if (start_pfn >= early_node_map[i].start_pfn &&
3550 end_pfn <= early_node_map[i].end_pfn)
3551 return;
3553 /* Merge forward if suitable */
3554 if (start_pfn <= early_node_map[i].end_pfn &&
3555 end_pfn > early_node_map[i].end_pfn) {
3556 early_node_map[i].end_pfn = end_pfn;
3557 return;
3560 /* Merge backward if suitable */
3561 if (start_pfn < early_node_map[i].end_pfn &&
3562 end_pfn >= early_node_map[i].start_pfn) {
3563 early_node_map[i].start_pfn = start_pfn;
3564 return;
3568 /* Check that early_node_map is large enough */
3569 if (i >= MAX_ACTIVE_REGIONS) {
3570 printk(KERN_CRIT "More than %d memory regions, truncating\n",
3571 MAX_ACTIVE_REGIONS);
3572 return;
3575 early_node_map[i].nid = nid;
3576 early_node_map[i].start_pfn = start_pfn;
3577 early_node_map[i].end_pfn = end_pfn;
3578 nr_nodemap_entries = i + 1;
3582 * shrink_active_range - Shrink an existing registered range of PFNs
3583 * @nid: The node id the range is on that should be shrunk
3584 * @old_end_pfn: The old end PFN of the range
3585 * @new_end_pfn: The new PFN of the range
3587 * i386 with NUMA use alloc_remap() to store a node_mem_map on a local node.
3588 * The map is kept at the end physical page range that has already been
3589 * registered with add_active_range(). This function allows an arch to shrink
3590 * an existing registered range.
3592 void __init shrink_active_range(unsigned int nid, unsigned long old_end_pfn,
3593 unsigned long new_end_pfn)
3595 int i;
3597 /* Find the old active region end and shrink */
3598 for_each_active_range_index_in_nid(i, nid)
3599 if (early_node_map[i].end_pfn == old_end_pfn) {
3600 early_node_map[i].end_pfn = new_end_pfn;
3601 break;
3606 * remove_all_active_ranges - Remove all currently registered regions
3608 * During discovery, it may be found that a table like SRAT is invalid
3609 * and an alternative discovery method must be used. This function removes
3610 * all currently registered regions.
3612 void __init remove_all_active_ranges(void)
3614 memset(early_node_map, 0, sizeof(early_node_map));
3615 nr_nodemap_entries = 0;
3616 #ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
3617 memset(node_boundary_start_pfn, 0, sizeof(node_boundary_start_pfn));
3618 memset(node_boundary_end_pfn, 0, sizeof(node_boundary_end_pfn));
3619 #endif /* CONFIG_MEMORY_HOTPLUG_RESERVE */
3622 /* Compare two active node_active_regions */
3623 static int __init cmp_node_active_region(const void *a, const void *b)
3625 struct node_active_region *arange = (struct node_active_region *)a;
3626 struct node_active_region *brange = (struct node_active_region *)b;
3628 /* Done this way to avoid overflows */
3629 if (arange->start_pfn > brange->start_pfn)
3630 return 1;
3631 if (arange->start_pfn < brange->start_pfn)
3632 return -1;
3634 return 0;
3637 /* sort the node_map by start_pfn */
3638 static void __init sort_node_map(void)
3640 sort(early_node_map, (size_t)nr_nodemap_entries,
3641 sizeof(struct node_active_region),
3642 cmp_node_active_region, NULL);
3645 /* Find the lowest pfn for a node */
3646 unsigned long __init find_min_pfn_for_node(unsigned long nid)
3648 int i;
3649 unsigned long min_pfn = ULONG_MAX;
3651 /* Assuming a sorted map, the first range found has the starting pfn */
3652 for_each_active_range_index_in_nid(i, nid)
3653 min_pfn = min(min_pfn, early_node_map[i].start_pfn);
3655 if (min_pfn == ULONG_MAX) {
3656 printk(KERN_WARNING
3657 "Could not find start_pfn for node %lu\n", nid);
3658 return 0;
3661 return min_pfn;
3665 * find_min_pfn_with_active_regions - Find the minimum PFN registered
3667 * It returns the minimum PFN based on information provided via
3668 * add_active_range().
3670 unsigned long __init find_min_pfn_with_active_regions(void)
3672 return find_min_pfn_for_node(MAX_NUMNODES);
3676 * find_max_pfn_with_active_regions - Find the maximum PFN registered
3678 * It returns the maximum PFN based on information provided via
3679 * add_active_range().
3681 unsigned long __init find_max_pfn_with_active_regions(void)
3683 int i;
3684 unsigned long max_pfn = 0;
3686 for (i = 0; i < nr_nodemap_entries; i++)
3687 max_pfn = max(max_pfn, early_node_map[i].end_pfn);
3689 return max_pfn;
3693 * early_calculate_totalpages()
3694 * Sum pages in active regions for movable zone.
3695 * Populate N_HIGH_MEMORY for calculating usable_nodes.
3697 static unsigned long __init early_calculate_totalpages(void)
3699 int i;
3700 unsigned long totalpages = 0;
3702 for (i = 0; i < nr_nodemap_entries; i++) {
3703 unsigned long pages = early_node_map[i].end_pfn -
3704 early_node_map[i].start_pfn;
3705 totalpages += pages;
3706 if (pages)
3707 node_set_state(early_node_map[i].nid, N_HIGH_MEMORY);
3709 return totalpages;
3713 * Find the PFN the Movable zone begins in each node. Kernel memory
3714 * is spread evenly between nodes as long as the nodes have enough
3715 * memory. When they don't, some nodes will have more kernelcore than
3716 * others
3718 void __init find_zone_movable_pfns_for_nodes(unsigned long *movable_pfn)
3720 int i, nid;
3721 unsigned long usable_startpfn;
3722 unsigned long kernelcore_node, kernelcore_remaining;
3723 unsigned long totalpages = early_calculate_totalpages();
3724 int usable_nodes = nodes_weight(node_states[N_HIGH_MEMORY]);
3727 * If movablecore was specified, calculate what size of
3728 * kernelcore that corresponds so that memory usable for
3729 * any allocation type is evenly spread. If both kernelcore
3730 * and movablecore are specified, then the value of kernelcore
3731 * will be used for required_kernelcore if it's greater than
3732 * what movablecore would have allowed.
3734 if (required_movablecore) {
3735 unsigned long corepages;
3738 * Round-up so that ZONE_MOVABLE is at least as large as what
3739 * was requested by the user
3741 required_movablecore =
3742 roundup(required_movablecore, MAX_ORDER_NR_PAGES);
3743 corepages = totalpages - required_movablecore;
3745 required_kernelcore = max(required_kernelcore, corepages);
3748 /* If kernelcore was not specified, there is no ZONE_MOVABLE */
3749 if (!required_kernelcore)
3750 return;
3752 /* usable_startpfn is the lowest possible pfn ZONE_MOVABLE can be at */
3753 find_usable_zone_for_movable();
3754 usable_startpfn = arch_zone_lowest_possible_pfn[movable_zone];
3756 restart:
3757 /* Spread kernelcore memory as evenly as possible throughout nodes */
3758 kernelcore_node = required_kernelcore / usable_nodes;
3759 for_each_node_state(nid, N_HIGH_MEMORY) {
3761 * Recalculate kernelcore_node if the division per node
3762 * now exceeds what is necessary to satisfy the requested
3763 * amount of memory for the kernel
3765 if (required_kernelcore < kernelcore_node)
3766 kernelcore_node = required_kernelcore / usable_nodes;
3769 * As the map is walked, we track how much memory is usable
3770 * by the kernel using kernelcore_remaining. When it is
3771 * 0, the rest of the node is usable by ZONE_MOVABLE
3773 kernelcore_remaining = kernelcore_node;
3775 /* Go through each range of PFNs within this node */
3776 for_each_active_range_index_in_nid(i, nid) {
3777 unsigned long start_pfn, end_pfn;
3778 unsigned long size_pages;
3780 start_pfn = max(early_node_map[i].start_pfn,
3781 zone_movable_pfn[nid]);
3782 end_pfn = early_node_map[i].end_pfn;
3783 if (start_pfn >= end_pfn)
3784 continue;
3786 /* Account for what is only usable for kernelcore */
3787 if (start_pfn < usable_startpfn) {
3788 unsigned long kernel_pages;
3789 kernel_pages = min(end_pfn, usable_startpfn)
3790 - start_pfn;
3792 kernelcore_remaining -= min(kernel_pages,
3793 kernelcore_remaining);
3794 required_kernelcore -= min(kernel_pages,
3795 required_kernelcore);
3797 /* Continue if range is now fully accounted */
3798 if (end_pfn <= usable_startpfn) {
3801 * Push zone_movable_pfn to the end so
3802 * that if we have to rebalance
3803 * kernelcore across nodes, we will
3804 * not double account here
3806 zone_movable_pfn[nid] = end_pfn;
3807 continue;
3809 start_pfn = usable_startpfn;
3813 * The usable PFN range for ZONE_MOVABLE is from
3814 * start_pfn->end_pfn. Calculate size_pages as the
3815 * number of pages used as kernelcore
3817 size_pages = end_pfn - start_pfn;
3818 if (size_pages > kernelcore_remaining)
3819 size_pages = kernelcore_remaining;
3820 zone_movable_pfn[nid] = start_pfn + size_pages;
3823 * Some kernelcore has been met, update counts and
3824 * break if the kernelcore for this node has been
3825 * satisified
3827 required_kernelcore -= min(required_kernelcore,
3828 size_pages);
3829 kernelcore_remaining -= size_pages;
3830 if (!kernelcore_remaining)
3831 break;
3836 * If there is still required_kernelcore, we do another pass with one
3837 * less node in the count. This will push zone_movable_pfn[nid] further
3838 * along on the nodes that still have memory until kernelcore is
3839 * satisified
3841 usable_nodes--;
3842 if (usable_nodes && required_kernelcore > usable_nodes)
3843 goto restart;
3845 /* Align start of ZONE_MOVABLE on all nids to MAX_ORDER_NR_PAGES */
3846 for (nid = 0; nid < MAX_NUMNODES; nid++)
3847 zone_movable_pfn[nid] =
3848 roundup(zone_movable_pfn[nid], MAX_ORDER_NR_PAGES);
3851 /* Any regular memory on that node ? */
3852 static void check_for_regular_memory(pg_data_t *pgdat)
3854 #ifdef CONFIG_HIGHMEM
3855 enum zone_type zone_type;
3857 for (zone_type = 0; zone_type <= ZONE_NORMAL; zone_type++) {
3858 struct zone *zone = &pgdat->node_zones[zone_type];
3859 if (zone->present_pages)
3860 node_set_state(zone_to_nid(zone), N_NORMAL_MEMORY);
3862 #endif
3866 * free_area_init_nodes - Initialise all pg_data_t and zone data
3867 * @max_zone_pfn: an array of max PFNs for each zone
3869 * This will call free_area_init_node() for each active node in the system.
3870 * Using the page ranges provided by add_active_range(), the size of each
3871 * zone in each node and their holes is calculated. If the maximum PFN
3872 * between two adjacent zones match, it is assumed that the zone is empty.
3873 * For example, if arch_max_dma_pfn == arch_max_dma32_pfn, it is assumed
3874 * that arch_max_dma32_pfn has no pages. It is also assumed that a zone
3875 * starts where the previous one ended. For example, ZONE_DMA32 starts
3876 * at arch_max_dma_pfn.
3878 void __init free_area_init_nodes(unsigned long *max_zone_pfn)
3880 unsigned long nid;
3881 enum zone_type i;
3883 /* Sort early_node_map as initialisation assumes it is sorted */
3884 sort_node_map();
3886 /* Record where the zone boundaries are */
3887 memset(arch_zone_lowest_possible_pfn, 0,
3888 sizeof(arch_zone_lowest_possible_pfn));
3889 memset(arch_zone_highest_possible_pfn, 0,
3890 sizeof(arch_zone_highest_possible_pfn));
3891 arch_zone_lowest_possible_pfn[0] = find_min_pfn_with_active_regions();
3892 arch_zone_highest_possible_pfn[0] = max_zone_pfn[0];
3893 for (i = 1; i < MAX_NR_ZONES; i++) {
3894 if (i == ZONE_MOVABLE)
3895 continue;
3896 arch_zone_lowest_possible_pfn[i] =
3897 arch_zone_highest_possible_pfn[i-1];
3898 arch_zone_highest_possible_pfn[i] =
3899 max(max_zone_pfn[i], arch_zone_lowest_possible_pfn[i]);
3901 arch_zone_lowest_possible_pfn[ZONE_MOVABLE] = 0;
3902 arch_zone_highest_possible_pfn[ZONE_MOVABLE] = 0;
3904 /* Find the PFNs that ZONE_MOVABLE begins at in each node */
3905 memset(zone_movable_pfn, 0, sizeof(zone_movable_pfn));
3906 find_zone_movable_pfns_for_nodes(zone_movable_pfn);
3908 /* Print out the zone ranges */
3909 printk("Zone PFN ranges:\n");
3910 for (i = 0; i < MAX_NR_ZONES; i++) {
3911 if (i == ZONE_MOVABLE)
3912 continue;
3913 printk(" %-8s %8lu -> %8lu\n",
3914 zone_names[i],
3915 arch_zone_lowest_possible_pfn[i],
3916 arch_zone_highest_possible_pfn[i]);
3919 /* Print out the PFNs ZONE_MOVABLE begins at in each node */
3920 printk("Movable zone start PFN for each node\n");
3921 for (i = 0; i < MAX_NUMNODES; i++) {
3922 if (zone_movable_pfn[i])
3923 printk(" Node %d: %lu\n", i, zone_movable_pfn[i]);
3926 /* Print out the early_node_map[] */
3927 printk("early_node_map[%d] active PFN ranges\n", nr_nodemap_entries);
3928 for (i = 0; i < nr_nodemap_entries; i++)
3929 printk(" %3d: %8lu -> %8lu\n", early_node_map[i].nid,
3930 early_node_map[i].start_pfn,
3931 early_node_map[i].end_pfn);
3933 /* Initialise every node */
3934 setup_nr_node_ids();
3935 for_each_online_node(nid) {
3936 pg_data_t *pgdat = NODE_DATA(nid);
3937 free_area_init_node(nid, pgdat, NULL,
3938 find_min_pfn_for_node(nid), NULL);
3940 /* Any memory on that node */
3941 if (pgdat->node_present_pages)
3942 node_set_state(nid, N_HIGH_MEMORY);
3943 check_for_regular_memory(pgdat);
3947 static int __init cmdline_parse_core(char *p, unsigned long *core)
3949 unsigned long long coremem;
3950 if (!p)
3951 return -EINVAL;
3953 coremem = memparse(p, &p);
3954 *core = coremem >> PAGE_SHIFT;
3956 /* Paranoid check that UL is enough for the coremem value */
3957 WARN_ON((coremem >> PAGE_SHIFT) > ULONG_MAX);
3959 return 0;
3963 * kernelcore=size sets the amount of memory for use for allocations that
3964 * cannot be reclaimed or migrated.
3966 static int __init cmdline_parse_kernelcore(char *p)
3968 return cmdline_parse_core(p, &required_kernelcore);
3972 * movablecore=size sets the amount of memory for use for allocations that
3973 * can be reclaimed or migrated.
3975 static int __init cmdline_parse_movablecore(char *p)
3977 return cmdline_parse_core(p, &required_movablecore);
3980 early_param("kernelcore", cmdline_parse_kernelcore);
3981 early_param("movablecore", cmdline_parse_movablecore);
3983 #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
3986 * set_dma_reserve - set the specified number of pages reserved in the first zone
3987 * @new_dma_reserve: The number of pages to mark reserved
3989 * The per-cpu batchsize and zone watermarks are determined by present_pages.
3990 * In the DMA zone, a significant percentage may be consumed by kernel image
3991 * and other unfreeable allocations which can skew the watermarks badly. This
3992 * function may optionally be used to account for unfreeable pages in the
3993 * first zone (e.g., ZONE_DMA). The effect will be lower watermarks and
3994 * smaller per-cpu batchsize.
3996 void __init set_dma_reserve(unsigned long new_dma_reserve)
3998 dma_reserve = new_dma_reserve;
4001 #ifndef CONFIG_NEED_MULTIPLE_NODES
4002 static bootmem_data_t contig_bootmem_data;
4003 struct pglist_data contig_page_data = { .bdata = &contig_bootmem_data };
4005 EXPORT_SYMBOL(contig_page_data);
4006 #endif
4008 void __init free_area_init(unsigned long *zones_size)
4010 free_area_init_node(0, NODE_DATA(0), zones_size,
4011 __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
4014 static int page_alloc_cpu_notify(struct notifier_block *self,
4015 unsigned long action, void *hcpu)
4017 int cpu = (unsigned long)hcpu;
4019 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
4020 drain_pages(cpu);
4023 * Spill the event counters of the dead processor
4024 * into the current processors event counters.
4025 * This artificially elevates the count of the current
4026 * processor.
4028 vm_events_fold_cpu(cpu);
4031 * Zero the differential counters of the dead processor
4032 * so that the vm statistics are consistent.
4034 * This is only okay since the processor is dead and cannot
4035 * race with what we are doing.
4037 refresh_cpu_vm_stats(cpu);
4039 return NOTIFY_OK;
4042 void __init page_alloc_init(void)
4044 hotcpu_notifier(page_alloc_cpu_notify, 0);
4048 * calculate_totalreserve_pages - called when sysctl_lower_zone_reserve_ratio
4049 * or min_free_kbytes changes.
4051 static void calculate_totalreserve_pages(void)
4053 struct pglist_data *pgdat;
4054 unsigned long reserve_pages = 0;
4055 enum zone_type i, j;
4057 for_each_online_pgdat(pgdat) {
4058 for (i = 0; i < MAX_NR_ZONES; i++) {
4059 struct zone *zone = pgdat->node_zones + i;
4060 unsigned long max = 0;
4062 /* Find valid and maximum lowmem_reserve in the zone */
4063 for (j = i; j < MAX_NR_ZONES; j++) {
4064 if (zone->lowmem_reserve[j] > max)
4065 max = zone->lowmem_reserve[j];
4068 /* we treat pages_high as reserved pages. */
4069 max += zone->pages_high;
4071 if (max > zone->present_pages)
4072 max = zone->present_pages;
4073 reserve_pages += max;
4076 totalreserve_pages = reserve_pages;
4080 * setup_per_zone_lowmem_reserve - called whenever
4081 * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
4082 * has a correct pages reserved value, so an adequate number of
4083 * pages are left in the zone after a successful __alloc_pages().
4085 static void setup_per_zone_lowmem_reserve(void)
4087 struct pglist_data *pgdat;
4088 enum zone_type j, idx;
4090 for_each_online_pgdat(pgdat) {
4091 for (j = 0; j < MAX_NR_ZONES; j++) {
4092 struct zone *zone = pgdat->node_zones + j;
4093 unsigned long present_pages = zone->present_pages;
4095 zone->lowmem_reserve[j] = 0;
4097 idx = j;
4098 while (idx) {
4099 struct zone *lower_zone;
4101 idx--;
4103 if (sysctl_lowmem_reserve_ratio[idx] < 1)
4104 sysctl_lowmem_reserve_ratio[idx] = 1;
4106 lower_zone = pgdat->node_zones + idx;
4107 lower_zone->lowmem_reserve[j] = present_pages /
4108 sysctl_lowmem_reserve_ratio[idx];
4109 present_pages += lower_zone->present_pages;
4114 /* update totalreserve_pages */
4115 calculate_totalreserve_pages();
4119 * setup_per_zone_pages_min - called when min_free_kbytes changes.
4121 * Ensures that the pages_{min,low,high} values for each zone are set correctly
4122 * with respect to min_free_kbytes.
4124 void setup_per_zone_pages_min(void)
4126 unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
4127 unsigned long lowmem_pages = 0;
4128 struct zone *zone;
4129 unsigned long flags;
4131 /* Calculate total number of !ZONE_HIGHMEM pages */
4132 for_each_zone(zone) {
4133 if (!is_highmem(zone))
4134 lowmem_pages += zone->present_pages;
4137 for_each_zone(zone) {
4138 u64 tmp;
4140 spin_lock_irqsave(&zone->lru_lock, flags);
4141 tmp = (u64)pages_min * zone->present_pages;
4142 do_div(tmp, lowmem_pages);
4143 if (is_highmem(zone)) {
4145 * __GFP_HIGH and PF_MEMALLOC allocations usually don't
4146 * need highmem pages, so cap pages_min to a small
4147 * value here.
4149 * The (pages_high-pages_low) and (pages_low-pages_min)
4150 * deltas controls asynch page reclaim, and so should
4151 * not be capped for highmem.
4153 int min_pages;
4155 min_pages = zone->present_pages / 1024;
4156 if (min_pages < SWAP_CLUSTER_MAX)
4157 min_pages = SWAP_CLUSTER_MAX;
4158 if (min_pages > 128)
4159 min_pages = 128;
4160 zone->pages_min = min_pages;
4161 } else {
4163 * If it's a lowmem zone, reserve a number of pages
4164 * proportionate to the zone's size.
4166 zone->pages_min = tmp;
4169 zone->pages_low = zone->pages_min + (tmp >> 2);
4170 zone->pages_high = zone->pages_min + (tmp >> 1);
4171 setup_zone_migrate_reserve(zone);
4172 spin_unlock_irqrestore(&zone->lru_lock, flags);
4175 /* update totalreserve_pages */
4176 calculate_totalreserve_pages();
4180 * Initialise min_free_kbytes.
4182 * For small machines we want it small (128k min). For large machines
4183 * we want it large (64MB max). But it is not linear, because network
4184 * bandwidth does not increase linearly with machine size. We use
4186 * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
4187 * min_free_kbytes = sqrt(lowmem_kbytes * 16)
4189 * which yields
4191 * 16MB: 512k
4192 * 32MB: 724k
4193 * 64MB: 1024k
4194 * 128MB: 1448k
4195 * 256MB: 2048k
4196 * 512MB: 2896k
4197 * 1024MB: 4096k
4198 * 2048MB: 5792k
4199 * 4096MB: 8192k
4200 * 8192MB: 11584k
4201 * 16384MB: 16384k
4203 static int __init init_per_zone_pages_min(void)
4205 unsigned long lowmem_kbytes;
4207 lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
4209 min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
4210 if (min_free_kbytes < 128)
4211 min_free_kbytes = 128;
4212 if (min_free_kbytes > 65536)
4213 min_free_kbytes = 65536;
4214 setup_per_zone_pages_min();
4215 setup_per_zone_lowmem_reserve();
4216 return 0;
4218 module_init(init_per_zone_pages_min)
4221 * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
4222 * that we can call two helper functions whenever min_free_kbytes
4223 * changes.
4225 int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
4226 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
4228 proc_dointvec(table, write, file, buffer, length, ppos);
4229 if (write)
4230 setup_per_zone_pages_min();
4231 return 0;
4234 #ifdef CONFIG_NUMA
4235 int sysctl_min_unmapped_ratio_sysctl_handler(ctl_table *table, int write,
4236 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
4238 struct zone *zone;
4239 int rc;
4241 rc = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
4242 if (rc)
4243 return rc;
4245 for_each_zone(zone)
4246 zone->min_unmapped_pages = (zone->present_pages *
4247 sysctl_min_unmapped_ratio) / 100;
4248 return 0;
4251 int sysctl_min_slab_ratio_sysctl_handler(ctl_table *table, int write,
4252 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
4254 struct zone *zone;
4255 int rc;
4257 rc = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
4258 if (rc)
4259 return rc;
4261 for_each_zone(zone)
4262 zone->min_slab_pages = (zone->present_pages *
4263 sysctl_min_slab_ratio) / 100;
4264 return 0;
4266 #endif
4269 * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
4270 * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
4271 * whenever sysctl_lowmem_reserve_ratio changes.
4273 * The reserve ratio obviously has absolutely no relation with the
4274 * pages_min watermarks. The lowmem reserve ratio can only make sense
4275 * if in function of the boot time zone sizes.
4277 int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
4278 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
4280 proc_dointvec_minmax(table, write, file, buffer, length, ppos);
4281 setup_per_zone_lowmem_reserve();
4282 return 0;
4286 * percpu_pagelist_fraction - changes the pcp->high for each zone on each
4287 * cpu. It is the fraction of total pages in each zone that a hot per cpu pagelist
4288 * can have before it gets flushed back to buddy allocator.
4291 int percpu_pagelist_fraction_sysctl_handler(ctl_table *table, int write,
4292 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
4294 struct zone *zone;
4295 unsigned int cpu;
4296 int ret;
4298 ret = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
4299 if (!write || (ret == -EINVAL))
4300 return ret;
4301 for_each_zone(zone) {
4302 for_each_online_cpu(cpu) {
4303 unsigned long high;
4304 high = zone->present_pages / percpu_pagelist_fraction;
4305 setup_pagelist_highmark(zone_pcp(zone, cpu), high);
4308 return 0;
4311 int hashdist = HASHDIST_DEFAULT;
4313 #ifdef CONFIG_NUMA
4314 static int __init set_hashdist(char *str)
4316 if (!str)
4317 return 0;
4318 hashdist = simple_strtoul(str, &str, 0);
4319 return 1;
4321 __setup("hashdist=", set_hashdist);
4322 #endif
4325 * allocate a large system hash table from bootmem
4326 * - it is assumed that the hash table must contain an exact power-of-2
4327 * quantity of entries
4328 * - limit is the number of hash buckets, not the total allocation size
4330 void *__init alloc_large_system_hash(const char *tablename,
4331 unsigned long bucketsize,
4332 unsigned long numentries,
4333 int scale,
4334 int flags,
4335 unsigned int *_hash_shift,
4336 unsigned int *_hash_mask,
4337 unsigned long limit)
4339 unsigned long long max = limit;
4340 unsigned long log2qty, size;
4341 void *table = NULL;
4343 /* allow the kernel cmdline to have a say */
4344 if (!numentries) {
4345 /* round applicable memory size up to nearest megabyte */
4346 numentries = nr_kernel_pages;
4347 numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
4348 numentries >>= 20 - PAGE_SHIFT;
4349 numentries <<= 20 - PAGE_SHIFT;
4351 /* limit to 1 bucket per 2^scale bytes of low memory */
4352 if (scale > PAGE_SHIFT)
4353 numentries >>= (scale - PAGE_SHIFT);
4354 else
4355 numentries <<= (PAGE_SHIFT - scale);
4357 /* Make sure we've got at least a 0-order allocation.. */
4358 if (unlikely((numentries * bucketsize) < PAGE_SIZE))
4359 numentries = PAGE_SIZE / bucketsize;
4361 numentries = roundup_pow_of_two(numentries);
4363 /* limit allocation size to 1/16 total memory by default */
4364 if (max == 0) {
4365 max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
4366 do_div(max, bucketsize);
4369 if (numentries > max)
4370 numentries = max;
4372 log2qty = ilog2(numentries);
4374 do {
4375 size = bucketsize << log2qty;
4376 if (flags & HASH_EARLY)
4377 table = alloc_bootmem(size);
4378 else if (hashdist)
4379 table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
4380 else {
4381 unsigned long order;
4382 for (order = 0; ((1UL << order) << PAGE_SHIFT) < size; order++)
4384 table = (void*) __get_free_pages(GFP_ATOMIC, order);
4386 * If bucketsize is not a power-of-two, we may free
4387 * some pages at the end of hash table.
4389 if (table) {
4390 unsigned long alloc_end = (unsigned long)table +
4391 (PAGE_SIZE << order);
4392 unsigned long used = (unsigned long)table +
4393 PAGE_ALIGN(size);
4394 split_page(virt_to_page(table), order);
4395 while (used < alloc_end) {
4396 free_page(used);
4397 used += PAGE_SIZE;
4401 } while (!table && size > PAGE_SIZE && --log2qty);
4403 if (!table)
4404 panic("Failed to allocate %s hash table\n", tablename);
4406 printk(KERN_INFO "%s hash table entries: %d (order: %d, %lu bytes)\n",
4407 tablename,
4408 (1U << log2qty),
4409 ilog2(size) - PAGE_SHIFT,
4410 size);
4412 if (_hash_shift)
4413 *_hash_shift = log2qty;
4414 if (_hash_mask)
4415 *_hash_mask = (1 << log2qty) - 1;
4417 return table;
4420 #ifdef CONFIG_OUT_OF_LINE_PFN_TO_PAGE
4421 struct page *pfn_to_page(unsigned long pfn)
4423 return __pfn_to_page(pfn);
4425 unsigned long page_to_pfn(struct page *page)
4427 return __page_to_pfn(page);
4429 EXPORT_SYMBOL(pfn_to_page);
4430 EXPORT_SYMBOL(page_to_pfn);
4431 #endif /* CONFIG_OUT_OF_LINE_PFN_TO_PAGE */
4433 /* Return a pointer to the bitmap storing bits affecting a block of pages */
4434 static inline unsigned long *get_pageblock_bitmap(struct zone *zone,
4435 unsigned long pfn)
4437 #ifdef CONFIG_SPARSEMEM
4438 return __pfn_to_section(pfn)->pageblock_flags;
4439 #else
4440 return zone->pageblock_flags;
4441 #endif /* CONFIG_SPARSEMEM */
4444 static inline int pfn_to_bitidx(struct zone *zone, unsigned long pfn)
4446 #ifdef CONFIG_SPARSEMEM
4447 pfn &= (PAGES_PER_SECTION-1);
4448 return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
4449 #else
4450 pfn = pfn - zone->zone_start_pfn;
4451 return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
4452 #endif /* CONFIG_SPARSEMEM */
4456 * get_pageblock_flags_group - Return the requested group of flags for the pageblock_nr_pages block of pages
4457 * @page: The page within the block of interest
4458 * @start_bitidx: The first bit of interest to retrieve
4459 * @end_bitidx: The last bit of interest
4460 * returns pageblock_bits flags
4462 unsigned long get_pageblock_flags_group(struct page *page,
4463 int start_bitidx, int end_bitidx)
4465 struct zone *zone;
4466 unsigned long *bitmap;
4467 unsigned long pfn, bitidx;
4468 unsigned long flags = 0;
4469 unsigned long value = 1;
4471 zone = page_zone(page);
4472 pfn = page_to_pfn(page);
4473 bitmap = get_pageblock_bitmap(zone, pfn);
4474 bitidx = pfn_to_bitidx(zone, pfn);
4476 for (; start_bitidx <= end_bitidx; start_bitidx++, value <<= 1)
4477 if (test_bit(bitidx + start_bitidx, bitmap))
4478 flags |= value;
4480 return flags;
4484 * set_pageblock_flags_group - Set the requested group of flags for a pageblock_nr_pages block of pages
4485 * @page: The page within the block of interest
4486 * @start_bitidx: The first bit of interest
4487 * @end_bitidx: The last bit of interest
4488 * @flags: The flags to set
4490 void set_pageblock_flags_group(struct page *page, unsigned long flags,
4491 int start_bitidx, int end_bitidx)
4493 struct zone *zone;
4494 unsigned long *bitmap;
4495 unsigned long pfn, bitidx;
4496 unsigned long value = 1;
4498 zone = page_zone(page);
4499 pfn = page_to_pfn(page);
4500 bitmap = get_pageblock_bitmap(zone, pfn);
4501 bitidx = pfn_to_bitidx(zone, pfn);
4503 for (; start_bitidx <= end_bitidx; start_bitidx++, value <<= 1)
4504 if (flags & value)
4505 __set_bit(bitidx + start_bitidx, bitmap);
4506 else
4507 __clear_bit(bitidx + start_bitidx, bitmap);
4511 * This is designed as sub function...plz see page_isolation.c also.
4512 * set/clear page block's type to be ISOLATE.
4513 * page allocater never alloc memory from ISOLATE block.
4516 int set_migratetype_isolate(struct page *page)
4518 struct zone *zone;
4519 unsigned long flags;
4520 int ret = -EBUSY;
4522 zone = page_zone(page);
4523 spin_lock_irqsave(&zone->lock, flags);
4525 * In future, more migrate types will be able to be isolation target.
4527 if (get_pageblock_migratetype(page) != MIGRATE_MOVABLE)
4528 goto out;
4529 set_pageblock_migratetype(page, MIGRATE_ISOLATE);
4530 move_freepages_block(zone, page, MIGRATE_ISOLATE);
4531 ret = 0;
4532 out:
4533 spin_unlock_irqrestore(&zone->lock, flags);
4534 if (!ret)
4535 drain_all_pages();
4536 return ret;
4539 void unset_migratetype_isolate(struct page *page)
4541 struct zone *zone;
4542 unsigned long flags;
4543 zone = page_zone(page);
4544 spin_lock_irqsave(&zone->lock, flags);
4545 if (get_pageblock_migratetype(page) != MIGRATE_ISOLATE)
4546 goto out;
4547 set_pageblock_migratetype(page, MIGRATE_MOVABLE);
4548 move_freepages_block(zone, page, MIGRATE_MOVABLE);
4549 out:
4550 spin_unlock_irqrestore(&zone->lock, flags);
4553 #ifdef CONFIG_MEMORY_HOTREMOVE
4555 * All pages in the range must be isolated before calling this.
4557 void
4558 __offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
4560 struct page *page;
4561 struct zone *zone;
4562 int order, i;
4563 unsigned long pfn;
4564 unsigned long flags;
4565 /* find the first valid pfn */
4566 for (pfn = start_pfn; pfn < end_pfn; pfn++)
4567 if (pfn_valid(pfn))
4568 break;
4569 if (pfn == end_pfn)
4570 return;
4571 zone = page_zone(pfn_to_page(pfn));
4572 spin_lock_irqsave(&zone->lock, flags);
4573 pfn = start_pfn;
4574 while (pfn < end_pfn) {
4575 if (!pfn_valid(pfn)) {
4576 pfn++;
4577 continue;
4579 page = pfn_to_page(pfn);
4580 BUG_ON(page_count(page));
4581 BUG_ON(!PageBuddy(page));
4582 order = page_order(page);
4583 #ifdef CONFIG_DEBUG_VM
4584 printk(KERN_INFO "remove from free list %lx %d %lx\n",
4585 pfn, 1 << order, end_pfn);
4586 #endif
4587 list_del(&page->lru);
4588 rmv_page_order(page);
4589 zone->free_area[order].nr_free--;
4590 __mod_zone_page_state(zone, NR_FREE_PAGES,
4591 - (1UL << order));
4592 for (i = 0; i < (1 << order); i++)
4593 SetPageReserved((page+i));
4594 pfn += (1 << order);
4596 spin_unlock_irqrestore(&zone->lock, flags);
4598 #endif