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>
19 #include <linux/swap.h>
20 #include <linux/interrupt.h>
21 #include <linux/pagemap.h>
22 #include <linux/jiffies.h>
23 #include <linux/bootmem.h>
24 #include <linux/compiler.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/suspend.h>
28 #include <linux/pagevec.h>
29 #include <linux/blkdev.h>
30 #include <linux/slab.h>
31 #include <linux/oom.h>
32 #include <linux/notifier.h>
33 #include <linux/topology.h>
34 #include <linux/sysctl.h>
35 #include <linux/cpu.h>
36 #include <linux/cpuset.h>
37 #include <linux/memory_hotplug.h>
38 #include <linux/nodemask.h>
39 #include <linux/vmalloc.h>
40 #include <linux/mempolicy.h>
41 #include <linux/stop_machine.h>
42 #include <linux/sort.h>
43 #include <linux/pfn.h>
44 #include <linux/backing-dev.h>
45 #include <linux/fault-inject.h>
46 #include <linux/page-isolation.h>
47 #include <linux/page_cgroup.h>
48 #include <linux/debugobjects.h>
50 #include <asm/tlbflush.h>
51 #include <asm/div64.h>
55 * Array of node states.
57 nodemask_t node_states
[NR_NODE_STATES
] __read_mostly
= {
58 [N_POSSIBLE
] = NODE_MASK_ALL
,
59 [N_ONLINE
] = { { [0] = 1UL } },
61 [N_NORMAL_MEMORY
] = { { [0] = 1UL } },
63 [N_HIGH_MEMORY
] = { { [0] = 1UL } },
65 [N_CPU
] = { { [0] = 1UL } },
68 EXPORT_SYMBOL(node_states
);
70 unsigned long totalram_pages __read_mostly
;
71 unsigned long totalreserve_pages __read_mostly
;
72 int percpu_pagelist_fraction
;
74 #ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
75 int pageblock_order __read_mostly
;
78 static void __free_pages_ok(struct page
*page
, unsigned int order
);
81 * results with 256, 32 in the lowmem_reserve sysctl:
82 * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
83 * 1G machine -> (16M dma, 784M normal, 224M high)
84 * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
85 * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
86 * HIGHMEM allocation will (224M+784M)/256 of ram reserved in ZONE_DMA
88 * TBD: should special case ZONE_DMA32 machines here - in those we normally
89 * don't need any ZONE_NORMAL reservation
91 int sysctl_lowmem_reserve_ratio
[MAX_NR_ZONES
-1] = {
92 #ifdef CONFIG_ZONE_DMA
95 #ifdef CONFIG_ZONE_DMA32
104 EXPORT_SYMBOL(totalram_pages
);
106 static char * const zone_names
[MAX_NR_ZONES
] = {
107 #ifdef CONFIG_ZONE_DMA
110 #ifdef CONFIG_ZONE_DMA32
114 #ifdef CONFIG_HIGHMEM
120 int min_free_kbytes
= 1024;
122 unsigned long __meminitdata nr_kernel_pages
;
123 unsigned long __meminitdata nr_all_pages
;
124 static unsigned long __meminitdata dma_reserve
;
126 #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
128 * MAX_ACTIVE_REGIONS determines the maximum number of distinct
129 * ranges of memory (RAM) that may be registered with add_active_range().
130 * Ranges passed to add_active_range() will be merged if possible
131 * so the number of times add_active_range() can be called is
132 * related to the number of nodes and the number of holes
134 #ifdef CONFIG_MAX_ACTIVE_REGIONS
135 /* Allow an architecture to set MAX_ACTIVE_REGIONS to save memory */
136 #define MAX_ACTIVE_REGIONS CONFIG_MAX_ACTIVE_REGIONS
138 #if MAX_NUMNODES >= 32
139 /* If there can be many nodes, allow up to 50 holes per node */
140 #define MAX_ACTIVE_REGIONS (MAX_NUMNODES*50)
142 /* By default, allow up to 256 distinct regions */
143 #define MAX_ACTIVE_REGIONS 256
147 static struct node_active_region __meminitdata early_node_map
[MAX_ACTIVE_REGIONS
];
148 static int __meminitdata nr_nodemap_entries
;
149 static unsigned long __meminitdata arch_zone_lowest_possible_pfn
[MAX_NR_ZONES
];
150 static unsigned long __meminitdata arch_zone_highest_possible_pfn
[MAX_NR_ZONES
];
151 #ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
152 static unsigned long __meminitdata node_boundary_start_pfn
[MAX_NUMNODES
];
153 static unsigned long __meminitdata node_boundary_end_pfn
[MAX_NUMNODES
];
154 #endif /* CONFIG_MEMORY_HOTPLUG_RESERVE */
155 static unsigned long __initdata required_kernelcore
;
156 static unsigned long __initdata required_movablecore
;
157 static unsigned long __meminitdata zone_movable_pfn
[MAX_NUMNODES
];
159 /* movable_zone is the "real" zone pages in ZONE_MOVABLE are taken from */
161 EXPORT_SYMBOL(movable_zone
);
162 #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
165 int nr_node_ids __read_mostly
= MAX_NUMNODES
;
166 EXPORT_SYMBOL(nr_node_ids
);
169 int page_group_by_mobility_disabled __read_mostly
;
171 static void set_pageblock_migratetype(struct page
*page
, int migratetype
)
173 set_pageblock_flags_group(page
, (unsigned long)migratetype
,
174 PB_migrate
, PB_migrate_end
);
177 #ifdef CONFIG_DEBUG_VM
178 static int page_outside_zone_boundaries(struct zone
*zone
, struct page
*page
)
182 unsigned long pfn
= page_to_pfn(page
);
185 seq
= zone_span_seqbegin(zone
);
186 if (pfn
>= zone
->zone_start_pfn
+ zone
->spanned_pages
)
188 else if (pfn
< zone
->zone_start_pfn
)
190 } while (zone_span_seqretry(zone
, seq
));
195 static int page_is_consistent(struct zone
*zone
, struct page
*page
)
197 if (!pfn_valid_within(page_to_pfn(page
)))
199 if (zone
!= page_zone(page
))
205 * Temporary debugging check for pages not lying within a given zone.
207 static int bad_range(struct zone
*zone
, struct page
*page
)
209 if (page_outside_zone_boundaries(zone
, page
))
211 if (!page_is_consistent(zone
, page
))
217 static inline int bad_range(struct zone
*zone
, struct page
*page
)
223 static void bad_page(struct page
*page
)
225 printk(KERN_EMERG
"Bad page state in process '%s'\n" KERN_EMERG
226 "page:%p flags:0x%0*lx mapping:%p mapcount:%d count:%d\n",
227 current
->comm
, page
, (int)(2*sizeof(unsigned long)),
228 (unsigned long)page
->flags
, page
->mapping
,
229 page_mapcount(page
), page_count(page
));
231 printk(KERN_EMERG
"Trying to fix it up, but a reboot is needed\n"
232 KERN_EMERG
"Backtrace:\n");
234 page
->flags
&= ~PAGE_FLAGS_CLEAR_WHEN_BAD
;
235 set_page_count(page
, 0);
236 reset_page_mapcount(page
);
237 page
->mapping
= NULL
;
238 add_taint(TAINT_BAD_PAGE
);
242 * Higher-order pages are called "compound pages". They are structured thusly:
244 * The first PAGE_SIZE page is called the "head page".
246 * The remaining PAGE_SIZE pages are called "tail pages".
248 * All pages have PG_compound set. All pages have their ->private pointing at
249 * the head page (even the head page has this).
251 * The first tail page's ->lru.next holds the address of the compound page's
252 * put_page() function. Its ->lru.prev holds the order of allocation.
253 * This usage means that zero-order pages may not be compound.
256 static void free_compound_page(struct page
*page
)
258 __free_pages_ok(page
, compound_order(page
));
261 void prep_compound_page(struct page
*page
, unsigned long order
)
264 int nr_pages
= 1 << order
;
266 set_compound_page_dtor(page
, free_compound_page
);
267 set_compound_order(page
, order
);
269 for (i
= 1; i
< nr_pages
; i
++) {
270 struct page
*p
= page
+ i
;
273 p
->first_page
= page
;
277 #ifdef CONFIG_HUGETLBFS
278 void prep_compound_gigantic_page(struct page
*page
, unsigned long order
)
281 int nr_pages
= 1 << order
;
282 struct page
*p
= page
+ 1;
284 set_compound_page_dtor(page
, free_compound_page
);
285 set_compound_order(page
, order
);
287 for (i
= 1; i
< nr_pages
; i
++, p
= mem_map_next(p
, page
, i
)) {
289 p
->first_page
= page
;
294 static void destroy_compound_page(struct page
*page
, unsigned long order
)
297 int nr_pages
= 1 << order
;
299 if (unlikely(compound_order(page
) != order
))
302 if (unlikely(!PageHead(page
)))
304 __ClearPageHead(page
);
305 for (i
= 1; i
< nr_pages
; i
++) {
306 struct page
*p
= page
+ i
;
308 if (unlikely(!PageTail(p
) |
309 (p
->first_page
!= page
)))
315 static inline void prep_zero_page(struct page
*page
, int order
, gfp_t gfp_flags
)
320 * clear_highpage() will use KM_USER0, so it's a bug to use __GFP_ZERO
321 * and __GFP_HIGHMEM from hard or soft interrupt context.
323 VM_BUG_ON((gfp_flags
& __GFP_HIGHMEM
) && in_interrupt());
324 for (i
= 0; i
< (1 << order
); i
++)
325 clear_highpage(page
+ i
);
328 static inline void set_page_order(struct page
*page
, int order
)
330 set_page_private(page
, order
);
331 __SetPageBuddy(page
);
334 static inline void rmv_page_order(struct page
*page
)
336 __ClearPageBuddy(page
);
337 set_page_private(page
, 0);
341 * Locate the struct page for both the matching buddy in our
342 * pair (buddy1) and the combined O(n+1) page they form (page).
344 * 1) Any buddy B1 will have an order O twin B2 which satisfies
345 * the following equation:
347 * For example, if the starting buddy (buddy2) is #8 its order
349 * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
351 * 2) Any buddy B will have an order O+1 parent P which
352 * satisfies the following equation:
355 * Assumption: *_mem_map is contiguous at least up to MAX_ORDER
357 static inline struct page
*
358 __page_find_buddy(struct page
*page
, unsigned long page_idx
, unsigned int order
)
360 unsigned long buddy_idx
= page_idx
^ (1 << order
);
362 return page
+ (buddy_idx
- page_idx
);
365 static inline unsigned long
366 __find_combined_index(unsigned long page_idx
, unsigned int order
)
368 return (page_idx
& ~(1 << order
));
372 * This function checks whether a page is free && is the buddy
373 * we can do coalesce a page and its buddy if
374 * (a) the buddy is not in a hole &&
375 * (b) the buddy is in the buddy system &&
376 * (c) a page and its buddy have the same order &&
377 * (d) a page and its buddy are in the same zone.
379 * For recording whether a page is in the buddy system, we use PG_buddy.
380 * Setting, clearing, and testing PG_buddy is serialized by zone->lock.
382 * For recording page's order, we use page_private(page).
384 static inline int page_is_buddy(struct page
*page
, struct page
*buddy
,
387 if (!pfn_valid_within(page_to_pfn(buddy
)))
390 if (page_zone_id(page
) != page_zone_id(buddy
))
393 if (PageBuddy(buddy
) && page_order(buddy
) == order
) {
394 BUG_ON(page_count(buddy
) != 0);
401 * Freeing function for a buddy system allocator.
403 * The concept of a buddy system is to maintain direct-mapped table
404 * (containing bit values) for memory blocks of various "orders".
405 * The bottom level table contains the map for the smallest allocatable
406 * units of memory (here, pages), and each level above it describes
407 * pairs of units from the levels below, hence, "buddies".
408 * At a high level, all that happens here is marking the table entry
409 * at the bottom level available, and propagating the changes upward
410 * as necessary, plus some accounting needed to play nicely with other
411 * parts of the VM system.
412 * At each level, we keep a list of pages, which are heads of continuous
413 * free pages of length of (1 << order) and marked with PG_buddy. Page's
414 * order is recorded in page_private(page) field.
415 * So when we are allocating or freeing one, we can derive the state of the
416 * other. That is, if we allocate a small block, and both were
417 * free, the remainder of the region must be split into blocks.
418 * If a block is freed, and its buddy is also free, then this
419 * triggers coalescing into a block of larger size.
424 static inline void __free_one_page(struct page
*page
,
425 struct zone
*zone
, unsigned int order
)
427 unsigned long page_idx
;
428 int order_size
= 1 << order
;
429 int migratetype
= get_pageblock_migratetype(page
);
431 if (unlikely(PageCompound(page
)))
432 destroy_compound_page(page
, order
);
434 page_idx
= page_to_pfn(page
) & ((1 << MAX_ORDER
) - 1);
436 VM_BUG_ON(page_idx
& (order_size
- 1));
437 VM_BUG_ON(bad_range(zone
, page
));
439 __mod_zone_page_state(zone
, NR_FREE_PAGES
, order_size
);
440 while (order
< MAX_ORDER
-1) {
441 unsigned long combined_idx
;
444 buddy
= __page_find_buddy(page
, page_idx
, order
);
445 if (!page_is_buddy(page
, buddy
, order
))
448 /* Our buddy is free, merge with it and move up one order. */
449 list_del(&buddy
->lru
);
450 zone
->free_area
[order
].nr_free
--;
451 rmv_page_order(buddy
);
452 combined_idx
= __find_combined_index(page_idx
, order
);
453 page
= page
+ (combined_idx
- page_idx
);
454 page_idx
= combined_idx
;
457 set_page_order(page
, order
);
459 &zone
->free_area
[order
].free_list
[migratetype
]);
460 zone
->free_area
[order
].nr_free
++;
463 static inline int free_pages_check(struct page
*page
)
465 free_page_mlock(page
);
466 if (unlikely(page_mapcount(page
) |
467 (page
->mapping
!= NULL
) |
468 (page_count(page
) != 0) |
469 (page
->flags
& PAGE_FLAGS_CHECK_AT_FREE
)))
472 __ClearPageDirty(page
);
473 if (PageSwapBacked(page
))
474 __ClearPageSwapBacked(page
);
476 * For now, we report if PG_reserved was found set, but do not
477 * clear it, and do not free the page. But we shall soon need
478 * to do more, for when the ZERO_PAGE count wraps negative.
480 return PageReserved(page
);
484 * Frees a list of pages.
485 * Assumes all pages on list are in same zone, and of same order.
486 * count is the number of pages to free.
488 * If the zone was previously in an "all pages pinned" state then look to
489 * see if this freeing clears that state.
491 * And clear the zone's pages_scanned counter, to hold off the "all pages are
492 * pinned" detection logic.
494 static void free_pages_bulk(struct zone
*zone
, int count
,
495 struct list_head
*list
, int order
)
497 spin_lock(&zone
->lock
);
498 zone_clear_flag(zone
, ZONE_ALL_UNRECLAIMABLE
);
499 zone
->pages_scanned
= 0;
503 VM_BUG_ON(list_empty(list
));
504 page
= list_entry(list
->prev
, struct page
, lru
);
505 /* have to delete it as __free_one_page list manipulates */
506 list_del(&page
->lru
);
507 __free_one_page(page
, zone
, order
);
509 spin_unlock(&zone
->lock
);
512 static void free_one_page(struct zone
*zone
, struct page
*page
, int order
)
514 spin_lock(&zone
->lock
);
515 zone_clear_flag(zone
, ZONE_ALL_UNRECLAIMABLE
);
516 zone
->pages_scanned
= 0;
517 __free_one_page(page
, zone
, order
);
518 spin_unlock(&zone
->lock
);
521 static void __free_pages_ok(struct page
*page
, unsigned int order
)
527 for (i
= 0 ; i
< (1 << order
) ; ++i
)
528 reserved
+= free_pages_check(page
+ i
);
532 if (!PageHighMem(page
)) {
533 debug_check_no_locks_freed(page_address(page
),PAGE_SIZE
<<order
);
534 debug_check_no_obj_freed(page_address(page
),
537 arch_free_page(page
, order
);
538 kernel_map_pages(page
, 1 << order
, 0);
540 local_irq_save(flags
);
541 __count_vm_events(PGFREE
, 1 << order
);
542 free_one_page(page_zone(page
), page
, order
);
543 local_irq_restore(flags
);
547 * permit the bootmem allocator to evade page validation on high-order frees
549 void __meminit
__free_pages_bootmem(struct page
*page
, unsigned int order
)
552 __ClearPageReserved(page
);
553 set_page_count(page
, 0);
554 set_page_refcounted(page
);
560 for (loop
= 0; loop
< BITS_PER_LONG
; loop
++) {
561 struct page
*p
= &page
[loop
];
563 if (loop
+ 1 < BITS_PER_LONG
)
565 __ClearPageReserved(p
);
566 set_page_count(p
, 0);
569 set_page_refcounted(page
);
570 __free_pages(page
, order
);
576 * The order of subdivision here is critical for the IO subsystem.
577 * Please do not alter this order without good reasons and regression
578 * testing. Specifically, as large blocks of memory are subdivided,
579 * the order in which smaller blocks are delivered depends on the order
580 * they're subdivided in this function. This is the primary factor
581 * influencing the order in which pages are delivered to the IO
582 * subsystem according to empirical testing, and this is also justified
583 * by considering the behavior of a buddy system containing a single
584 * large block of memory acted on by a series of small allocations.
585 * This behavior is a critical factor in sglist merging's success.
589 static inline void expand(struct zone
*zone
, struct page
*page
,
590 int low
, int high
, struct free_area
*area
,
593 unsigned long size
= 1 << high
;
599 VM_BUG_ON(bad_range(zone
, &page
[size
]));
600 list_add(&page
[size
].lru
, &area
->free_list
[migratetype
]);
602 set_page_order(&page
[size
], high
);
607 * This page is about to be returned from the page allocator
609 static int prep_new_page(struct page
*page
, int order
, gfp_t gfp_flags
)
611 if (unlikely(page_mapcount(page
) |
612 (page
->mapping
!= NULL
) |
613 (page_count(page
) != 0) |
614 (page
->flags
& PAGE_FLAGS_CHECK_AT_PREP
)))
618 * For now, we report if PG_reserved was found set, but do not
619 * clear it, and do not allocate the page: as a safety net.
621 if (PageReserved(page
))
624 page
->flags
&= ~(1 << PG_uptodate
| 1 << PG_error
| 1 << PG_reclaim
|
625 1 << PG_referenced
| 1 << PG_arch_1
|
626 1 << PG_owner_priv_1
| 1 << PG_mappedtodisk
627 #ifdef CONFIG_UNEVICTABLE_LRU
631 set_page_private(page
, 0);
632 set_page_refcounted(page
);
634 arch_alloc_page(page
, order
);
635 kernel_map_pages(page
, 1 << order
, 1);
637 if (gfp_flags
& __GFP_ZERO
)
638 prep_zero_page(page
, order
, gfp_flags
);
640 if (order
&& (gfp_flags
& __GFP_COMP
))
641 prep_compound_page(page
, order
);
647 * Go through the free lists for the given migratetype and remove
648 * the smallest available page from the freelists
650 static struct page
*__rmqueue_smallest(struct zone
*zone
, unsigned int order
,
653 unsigned int current_order
;
654 struct free_area
* area
;
657 /* Find a page of the appropriate size in the preferred list */
658 for (current_order
= order
; current_order
< MAX_ORDER
; ++current_order
) {
659 area
= &(zone
->free_area
[current_order
]);
660 if (list_empty(&area
->free_list
[migratetype
]))
663 page
= list_entry(area
->free_list
[migratetype
].next
,
665 list_del(&page
->lru
);
666 rmv_page_order(page
);
668 __mod_zone_page_state(zone
, NR_FREE_PAGES
, - (1UL << order
));
669 expand(zone
, page
, order
, current_order
, area
, migratetype
);
678 * This array describes the order lists are fallen back to when
679 * the free lists for the desirable migrate type are depleted
681 static int fallbacks
[MIGRATE_TYPES
][MIGRATE_TYPES
-1] = {
682 [MIGRATE_UNMOVABLE
] = { MIGRATE_RECLAIMABLE
, MIGRATE_MOVABLE
, MIGRATE_RESERVE
},
683 [MIGRATE_RECLAIMABLE
] = { MIGRATE_UNMOVABLE
, MIGRATE_MOVABLE
, MIGRATE_RESERVE
},
684 [MIGRATE_MOVABLE
] = { MIGRATE_RECLAIMABLE
, MIGRATE_UNMOVABLE
, MIGRATE_RESERVE
},
685 [MIGRATE_RESERVE
] = { MIGRATE_RESERVE
, MIGRATE_RESERVE
, MIGRATE_RESERVE
}, /* Never used */
689 * Move the free pages in a range to the free lists of the requested type.
690 * Note that start_page and end_pages are not aligned on a pageblock
691 * boundary. If alignment is required, use move_freepages_block()
693 static int move_freepages(struct zone
*zone
,
694 struct page
*start_page
, struct page
*end_page
,
701 #ifndef CONFIG_HOLES_IN_ZONE
703 * page_zone is not safe to call in this context when
704 * CONFIG_HOLES_IN_ZONE is set. This bug check is probably redundant
705 * anyway as we check zone boundaries in move_freepages_block().
706 * Remove at a later date when no bug reports exist related to
707 * grouping pages by mobility
709 BUG_ON(page_zone(start_page
) != page_zone(end_page
));
712 for (page
= start_page
; page
<= end_page
;) {
713 /* Make sure we are not inadvertently changing nodes */
714 VM_BUG_ON(page_to_nid(page
) != zone_to_nid(zone
));
716 if (!pfn_valid_within(page_to_pfn(page
))) {
721 if (!PageBuddy(page
)) {
726 order
= page_order(page
);
727 list_del(&page
->lru
);
729 &zone
->free_area
[order
].free_list
[migratetype
]);
731 pages_moved
+= 1 << order
;
737 static int move_freepages_block(struct zone
*zone
, struct page
*page
,
740 unsigned long start_pfn
, end_pfn
;
741 struct page
*start_page
, *end_page
;
743 start_pfn
= page_to_pfn(page
);
744 start_pfn
= start_pfn
& ~(pageblock_nr_pages
-1);
745 start_page
= pfn_to_page(start_pfn
);
746 end_page
= start_page
+ pageblock_nr_pages
- 1;
747 end_pfn
= start_pfn
+ pageblock_nr_pages
- 1;
749 /* Do not cross zone boundaries */
750 if (start_pfn
< zone
->zone_start_pfn
)
752 if (end_pfn
>= zone
->zone_start_pfn
+ zone
->spanned_pages
)
755 return move_freepages(zone
, start_page
, end_page
, migratetype
);
758 /* Remove an element from the buddy allocator from the fallback list */
759 static struct page
*__rmqueue_fallback(struct zone
*zone
, int order
,
760 int start_migratetype
)
762 struct free_area
* area
;
767 /* Find the largest possible block of pages in the other list */
768 for (current_order
= MAX_ORDER
-1; current_order
>= order
;
770 for (i
= 0; i
< MIGRATE_TYPES
- 1; i
++) {
771 migratetype
= fallbacks
[start_migratetype
][i
];
773 /* MIGRATE_RESERVE handled later if necessary */
774 if (migratetype
== MIGRATE_RESERVE
)
777 area
= &(zone
->free_area
[current_order
]);
778 if (list_empty(&area
->free_list
[migratetype
]))
781 page
= list_entry(area
->free_list
[migratetype
].next
,
786 * If breaking a large block of pages, move all free
787 * pages to the preferred allocation list. If falling
788 * back for a reclaimable kernel allocation, be more
789 * agressive about taking ownership of free pages
791 if (unlikely(current_order
>= (pageblock_order
>> 1)) ||
792 start_migratetype
== MIGRATE_RECLAIMABLE
) {
794 pages
= move_freepages_block(zone
, page
,
797 /* Claim the whole block if over half of it is free */
798 if (pages
>= (1 << (pageblock_order
-1)))
799 set_pageblock_migratetype(page
,
802 migratetype
= start_migratetype
;
805 /* Remove the page from the freelists */
806 list_del(&page
->lru
);
807 rmv_page_order(page
);
808 __mod_zone_page_state(zone
, NR_FREE_PAGES
,
811 if (current_order
== pageblock_order
)
812 set_pageblock_migratetype(page
,
815 expand(zone
, page
, order
, current_order
, area
, migratetype
);
820 /* Use MIGRATE_RESERVE rather than fail an allocation */
821 return __rmqueue_smallest(zone
, order
, MIGRATE_RESERVE
);
825 * Do the hard work of removing an element from the buddy allocator.
826 * Call me with the zone->lock already held.
828 static struct page
*__rmqueue(struct zone
*zone
, unsigned int order
,
833 page
= __rmqueue_smallest(zone
, order
, migratetype
);
836 page
= __rmqueue_fallback(zone
, order
, migratetype
);
842 * Obtain a specified number of elements from the buddy allocator, all under
843 * a single hold of the lock, for efficiency. Add them to the supplied list.
844 * Returns the number of new pages which were placed at *list.
846 static int rmqueue_bulk(struct zone
*zone
, unsigned int order
,
847 unsigned long count
, struct list_head
*list
,
852 spin_lock(&zone
->lock
);
853 for (i
= 0; i
< count
; ++i
) {
854 struct page
*page
= __rmqueue(zone
, order
, migratetype
);
855 if (unlikely(page
== NULL
))
859 * Split buddy pages returned by expand() are received here
860 * in physical page order. The page is added to the callers and
861 * list and the list head then moves forward. From the callers
862 * perspective, the linked list is ordered by page number in
863 * some conditions. This is useful for IO devices that can
864 * merge IO requests if the physical pages are ordered
867 list_add(&page
->lru
, list
);
868 set_page_private(page
, migratetype
);
871 spin_unlock(&zone
->lock
);
877 * Called from the vmstat counter updater to drain pagesets of this
878 * currently executing processor on remote nodes after they have
881 * Note that this function must be called with the thread pinned to
882 * a single processor.
884 void drain_zone_pages(struct zone
*zone
, struct per_cpu_pages
*pcp
)
889 local_irq_save(flags
);
890 if (pcp
->count
>= pcp
->batch
)
891 to_drain
= pcp
->batch
;
893 to_drain
= pcp
->count
;
894 free_pages_bulk(zone
, to_drain
, &pcp
->list
, 0);
895 pcp
->count
-= to_drain
;
896 local_irq_restore(flags
);
901 * Drain pages of the indicated processor.
903 * The processor must either be the current processor and the
904 * thread pinned to the current processor or a processor that
907 static void drain_pages(unsigned int cpu
)
912 for_each_zone(zone
) {
913 struct per_cpu_pageset
*pset
;
914 struct per_cpu_pages
*pcp
;
916 if (!populated_zone(zone
))
919 pset
= zone_pcp(zone
, cpu
);
922 local_irq_save(flags
);
923 free_pages_bulk(zone
, pcp
->count
, &pcp
->list
, 0);
925 local_irq_restore(flags
);
930 * Spill all of this CPU's per-cpu pages back into the buddy allocator.
932 void drain_local_pages(void *arg
)
934 drain_pages(smp_processor_id());
938 * Spill all the per-cpu pages from all CPUs back into the buddy allocator
940 void drain_all_pages(void)
942 on_each_cpu(drain_local_pages
, NULL
, 1);
945 #ifdef CONFIG_HIBERNATION
947 void mark_free_pages(struct zone
*zone
)
949 unsigned long pfn
, max_zone_pfn
;
952 struct list_head
*curr
;
954 if (!zone
->spanned_pages
)
957 spin_lock_irqsave(&zone
->lock
, flags
);
959 max_zone_pfn
= zone
->zone_start_pfn
+ zone
->spanned_pages
;
960 for (pfn
= zone
->zone_start_pfn
; pfn
< max_zone_pfn
; pfn
++)
961 if (pfn_valid(pfn
)) {
962 struct page
*page
= pfn_to_page(pfn
);
964 if (!swsusp_page_is_forbidden(page
))
965 swsusp_unset_page_free(page
);
968 for_each_migratetype_order(order
, t
) {
969 list_for_each(curr
, &zone
->free_area
[order
].free_list
[t
]) {
972 pfn
= page_to_pfn(list_entry(curr
, struct page
, lru
));
973 for (i
= 0; i
< (1UL << order
); i
++)
974 swsusp_set_page_free(pfn_to_page(pfn
+ i
));
977 spin_unlock_irqrestore(&zone
->lock
, flags
);
979 #endif /* CONFIG_PM */
982 * Free a 0-order page
984 static void free_hot_cold_page(struct page
*page
, int cold
)
986 struct zone
*zone
= page_zone(page
);
987 struct per_cpu_pages
*pcp
;
991 page
->mapping
= NULL
;
992 if (free_pages_check(page
))
995 if (!PageHighMem(page
)) {
996 debug_check_no_locks_freed(page_address(page
), PAGE_SIZE
);
997 debug_check_no_obj_freed(page_address(page
), PAGE_SIZE
);
999 arch_free_page(page
, 0);
1000 kernel_map_pages(page
, 1, 0);
1002 pcp
= &zone_pcp(zone
, get_cpu())->pcp
;
1003 local_irq_save(flags
);
1004 __count_vm_event(PGFREE
);
1006 list_add_tail(&page
->lru
, &pcp
->list
);
1008 list_add(&page
->lru
, &pcp
->list
);
1009 set_page_private(page
, get_pageblock_migratetype(page
));
1011 if (pcp
->count
>= pcp
->high
) {
1012 free_pages_bulk(zone
, pcp
->batch
, &pcp
->list
, 0);
1013 pcp
->count
-= pcp
->batch
;
1015 local_irq_restore(flags
);
1019 void free_hot_page(struct page
*page
)
1021 free_hot_cold_page(page
, 0);
1024 void free_cold_page(struct page
*page
)
1026 free_hot_cold_page(page
, 1);
1030 * split_page takes a non-compound higher-order page, and splits it into
1031 * n (1<<order) sub-pages: page[0..n]
1032 * Each sub-page must be freed individually.
1034 * Note: this is probably too low level an operation for use in drivers.
1035 * Please consult with lkml before using this in your driver.
1037 void split_page(struct page
*page
, unsigned int order
)
1041 VM_BUG_ON(PageCompound(page
));
1042 VM_BUG_ON(!page_count(page
));
1043 for (i
= 1; i
< (1 << order
); i
++)
1044 set_page_refcounted(page
+ i
);
1048 * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
1049 * we cheat by calling it from here, in the order > 0 path. Saves a branch
1052 static struct page
*buffered_rmqueue(struct zone
*preferred_zone
,
1053 struct zone
*zone
, int order
, gfp_t gfp_flags
)
1055 unsigned long flags
;
1057 int cold
= !!(gfp_flags
& __GFP_COLD
);
1059 int migratetype
= allocflags_to_migratetype(gfp_flags
);
1063 if (likely(order
== 0)) {
1064 struct per_cpu_pages
*pcp
;
1066 pcp
= &zone_pcp(zone
, cpu
)->pcp
;
1067 local_irq_save(flags
);
1069 pcp
->count
= rmqueue_bulk(zone
, 0,
1070 pcp
->batch
, &pcp
->list
, migratetype
);
1071 if (unlikely(!pcp
->count
))
1075 /* Find a page of the appropriate migrate type */
1077 list_for_each_entry_reverse(page
, &pcp
->list
, lru
)
1078 if (page_private(page
) == migratetype
)
1081 list_for_each_entry(page
, &pcp
->list
, lru
)
1082 if (page_private(page
) == migratetype
)
1086 /* Allocate more to the pcp list if necessary */
1087 if (unlikely(&page
->lru
== &pcp
->list
)) {
1088 pcp
->count
+= rmqueue_bulk(zone
, 0,
1089 pcp
->batch
, &pcp
->list
, migratetype
);
1090 page
= list_entry(pcp
->list
.next
, struct page
, lru
);
1093 list_del(&page
->lru
);
1096 spin_lock_irqsave(&zone
->lock
, flags
);
1097 page
= __rmqueue(zone
, order
, migratetype
);
1098 spin_unlock(&zone
->lock
);
1103 __count_zone_vm_events(PGALLOC
, zone
, 1 << order
);
1104 zone_statistics(preferred_zone
, zone
);
1105 local_irq_restore(flags
);
1108 VM_BUG_ON(bad_range(zone
, page
));
1109 if (prep_new_page(page
, order
, gfp_flags
))
1114 local_irq_restore(flags
);
1119 #define ALLOC_NO_WATERMARKS 0x01 /* don't check watermarks at all */
1120 #define ALLOC_WMARK_MIN 0x02 /* use pages_min watermark */
1121 #define ALLOC_WMARK_LOW 0x04 /* use pages_low watermark */
1122 #define ALLOC_WMARK_HIGH 0x08 /* use pages_high watermark */
1123 #define ALLOC_HARDER 0x10 /* try to alloc harder */
1124 #define ALLOC_HIGH 0x20 /* __GFP_HIGH set */
1125 #define ALLOC_CPUSET 0x40 /* check for correct cpuset */
1127 #ifdef CONFIG_FAIL_PAGE_ALLOC
1129 static struct fail_page_alloc_attr
{
1130 struct fault_attr attr
;
1132 u32 ignore_gfp_highmem
;
1133 u32 ignore_gfp_wait
;
1136 #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
1138 struct dentry
*ignore_gfp_highmem_file
;
1139 struct dentry
*ignore_gfp_wait_file
;
1140 struct dentry
*min_order_file
;
1142 #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
1144 } fail_page_alloc
= {
1145 .attr
= FAULT_ATTR_INITIALIZER
,
1146 .ignore_gfp_wait
= 1,
1147 .ignore_gfp_highmem
= 1,
1151 static int __init
setup_fail_page_alloc(char *str
)
1153 return setup_fault_attr(&fail_page_alloc
.attr
, str
);
1155 __setup("fail_page_alloc=", setup_fail_page_alloc
);
1157 static int should_fail_alloc_page(gfp_t gfp_mask
, unsigned int order
)
1159 if (order
< fail_page_alloc
.min_order
)
1161 if (gfp_mask
& __GFP_NOFAIL
)
1163 if (fail_page_alloc
.ignore_gfp_highmem
&& (gfp_mask
& __GFP_HIGHMEM
))
1165 if (fail_page_alloc
.ignore_gfp_wait
&& (gfp_mask
& __GFP_WAIT
))
1168 return should_fail(&fail_page_alloc
.attr
, 1 << order
);
1171 #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
1173 static int __init
fail_page_alloc_debugfs(void)
1175 mode_t mode
= S_IFREG
| S_IRUSR
| S_IWUSR
;
1179 err
= init_fault_attr_dentries(&fail_page_alloc
.attr
,
1183 dir
= fail_page_alloc
.attr
.dentries
.dir
;
1185 fail_page_alloc
.ignore_gfp_wait_file
=
1186 debugfs_create_bool("ignore-gfp-wait", mode
, dir
,
1187 &fail_page_alloc
.ignore_gfp_wait
);
1189 fail_page_alloc
.ignore_gfp_highmem_file
=
1190 debugfs_create_bool("ignore-gfp-highmem", mode
, dir
,
1191 &fail_page_alloc
.ignore_gfp_highmem
);
1192 fail_page_alloc
.min_order_file
=
1193 debugfs_create_u32("min-order", mode
, dir
,
1194 &fail_page_alloc
.min_order
);
1196 if (!fail_page_alloc
.ignore_gfp_wait_file
||
1197 !fail_page_alloc
.ignore_gfp_highmem_file
||
1198 !fail_page_alloc
.min_order_file
) {
1200 debugfs_remove(fail_page_alloc
.ignore_gfp_wait_file
);
1201 debugfs_remove(fail_page_alloc
.ignore_gfp_highmem_file
);
1202 debugfs_remove(fail_page_alloc
.min_order_file
);
1203 cleanup_fault_attr_dentries(&fail_page_alloc
.attr
);
1209 late_initcall(fail_page_alloc_debugfs
);
1211 #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
1213 #else /* CONFIG_FAIL_PAGE_ALLOC */
1215 static inline int should_fail_alloc_page(gfp_t gfp_mask
, unsigned int order
)
1220 #endif /* CONFIG_FAIL_PAGE_ALLOC */
1223 * Return 1 if free pages are above 'mark'. This takes into account the order
1224 * of the allocation.
1226 int zone_watermark_ok(struct zone
*z
, int order
, unsigned long mark
,
1227 int classzone_idx
, int alloc_flags
)
1229 /* free_pages my go negative - that's OK */
1231 long free_pages
= zone_page_state(z
, NR_FREE_PAGES
) - (1 << order
) + 1;
1234 if (alloc_flags
& ALLOC_HIGH
)
1236 if (alloc_flags
& ALLOC_HARDER
)
1239 if (free_pages
<= min
+ z
->lowmem_reserve
[classzone_idx
])
1241 for (o
= 0; o
< order
; o
++) {
1242 /* At the next order, this order's pages become unavailable */
1243 free_pages
-= z
->free_area
[o
].nr_free
<< o
;
1245 /* Require fewer higher order pages to be free */
1248 if (free_pages
<= min
)
1256 * zlc_setup - Setup for "zonelist cache". Uses cached zone data to
1257 * skip over zones that are not allowed by the cpuset, or that have
1258 * been recently (in last second) found to be nearly full. See further
1259 * comments in mmzone.h. Reduces cache footprint of zonelist scans
1260 * that have to skip over a lot of full or unallowed zones.
1262 * If the zonelist cache is present in the passed in zonelist, then
1263 * returns a pointer to the allowed node mask (either the current
1264 * tasks mems_allowed, or node_states[N_HIGH_MEMORY].)
1266 * If the zonelist cache is not available for this zonelist, does
1267 * nothing and returns NULL.
1269 * If the fullzones BITMAP in the zonelist cache is stale (more than
1270 * a second since last zap'd) then we zap it out (clear its bits.)
1272 * We hold off even calling zlc_setup, until after we've checked the
1273 * first zone in the zonelist, on the theory that most allocations will
1274 * be satisfied from that first zone, so best to examine that zone as
1275 * quickly as we can.
1277 static nodemask_t
*zlc_setup(struct zonelist
*zonelist
, int alloc_flags
)
1279 struct zonelist_cache
*zlc
; /* cached zonelist speedup info */
1280 nodemask_t
*allowednodes
; /* zonelist_cache approximation */
1282 zlc
= zonelist
->zlcache_ptr
;
1286 if (time_after(jiffies
, zlc
->last_full_zap
+ HZ
)) {
1287 bitmap_zero(zlc
->fullzones
, MAX_ZONES_PER_ZONELIST
);
1288 zlc
->last_full_zap
= jiffies
;
1291 allowednodes
= !in_interrupt() && (alloc_flags
& ALLOC_CPUSET
) ?
1292 &cpuset_current_mems_allowed
:
1293 &node_states
[N_HIGH_MEMORY
];
1294 return allowednodes
;
1298 * Given 'z' scanning a zonelist, run a couple of quick checks to see
1299 * if it is worth looking at further for free memory:
1300 * 1) Check that the zone isn't thought to be full (doesn't have its
1301 * bit set in the zonelist_cache fullzones BITMAP).
1302 * 2) Check that the zones node (obtained from the zonelist_cache
1303 * z_to_n[] mapping) is allowed in the passed in allowednodes mask.
1304 * Return true (non-zero) if zone is worth looking at further, or
1305 * else return false (zero) if it is not.
1307 * This check -ignores- the distinction between various watermarks,
1308 * such as GFP_HIGH, GFP_ATOMIC, PF_MEMALLOC, ... If a zone is
1309 * found to be full for any variation of these watermarks, it will
1310 * be considered full for up to one second by all requests, unless
1311 * we are so low on memory on all allowed nodes that we are forced
1312 * into the second scan of the zonelist.
1314 * In the second scan we ignore this zonelist cache and exactly
1315 * apply the watermarks to all zones, even it is slower to do so.
1316 * We are low on memory in the second scan, and should leave no stone
1317 * unturned looking for a free page.
1319 static int zlc_zone_worth_trying(struct zonelist
*zonelist
, struct zoneref
*z
,
1320 nodemask_t
*allowednodes
)
1322 struct zonelist_cache
*zlc
; /* cached zonelist speedup info */
1323 int i
; /* index of *z in zonelist zones */
1324 int n
; /* node that zone *z is on */
1326 zlc
= zonelist
->zlcache_ptr
;
1330 i
= z
- zonelist
->_zonerefs
;
1333 /* This zone is worth trying if it is allowed but not full */
1334 return node_isset(n
, *allowednodes
) && !test_bit(i
, zlc
->fullzones
);
1338 * Given 'z' scanning a zonelist, set the corresponding bit in
1339 * zlc->fullzones, so that subsequent attempts to allocate a page
1340 * from that zone don't waste time re-examining it.
1342 static void zlc_mark_zone_full(struct zonelist
*zonelist
, struct zoneref
*z
)
1344 struct zonelist_cache
*zlc
; /* cached zonelist speedup info */
1345 int i
; /* index of *z in zonelist zones */
1347 zlc
= zonelist
->zlcache_ptr
;
1351 i
= z
- zonelist
->_zonerefs
;
1353 set_bit(i
, zlc
->fullzones
);
1356 #else /* CONFIG_NUMA */
1358 static nodemask_t
*zlc_setup(struct zonelist
*zonelist
, int alloc_flags
)
1363 static int zlc_zone_worth_trying(struct zonelist
*zonelist
, struct zoneref
*z
,
1364 nodemask_t
*allowednodes
)
1369 static void zlc_mark_zone_full(struct zonelist
*zonelist
, struct zoneref
*z
)
1372 #endif /* CONFIG_NUMA */
1375 * get_page_from_freelist goes through the zonelist trying to allocate
1378 static struct page
*
1379 get_page_from_freelist(gfp_t gfp_mask
, nodemask_t
*nodemask
, unsigned int order
,
1380 struct zonelist
*zonelist
, int high_zoneidx
, int alloc_flags
)
1383 struct page
*page
= NULL
;
1385 struct zone
*zone
, *preferred_zone
;
1386 nodemask_t
*allowednodes
= NULL
;/* zonelist_cache approximation */
1387 int zlc_active
= 0; /* set if using zonelist_cache */
1388 int did_zlc_setup
= 0; /* just call zlc_setup() one time */
1390 (void)first_zones_zonelist(zonelist
, high_zoneidx
, nodemask
,
1392 if (!preferred_zone
)
1395 classzone_idx
= zone_idx(preferred_zone
);
1399 * Scan zonelist, looking for a zone with enough free.
1400 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
1402 for_each_zone_zonelist_nodemask(zone
, z
, zonelist
,
1403 high_zoneidx
, nodemask
) {
1404 if (NUMA_BUILD
&& zlc_active
&&
1405 !zlc_zone_worth_trying(zonelist
, z
, allowednodes
))
1407 if ((alloc_flags
& ALLOC_CPUSET
) &&
1408 !cpuset_zone_allowed_softwall(zone
, gfp_mask
))
1411 if (!(alloc_flags
& ALLOC_NO_WATERMARKS
)) {
1413 if (alloc_flags
& ALLOC_WMARK_MIN
)
1414 mark
= zone
->pages_min
;
1415 else if (alloc_flags
& ALLOC_WMARK_LOW
)
1416 mark
= zone
->pages_low
;
1418 mark
= zone
->pages_high
;
1419 if (!zone_watermark_ok(zone
, order
, mark
,
1420 classzone_idx
, alloc_flags
)) {
1421 if (!zone_reclaim_mode
||
1422 !zone_reclaim(zone
, gfp_mask
, order
))
1423 goto this_zone_full
;
1427 page
= buffered_rmqueue(preferred_zone
, zone
, order
, gfp_mask
);
1432 zlc_mark_zone_full(zonelist
, z
);
1434 if (NUMA_BUILD
&& !did_zlc_setup
) {
1435 /* we do zlc_setup after the first zone is tried */
1436 allowednodes
= zlc_setup(zonelist
, alloc_flags
);
1442 if (unlikely(NUMA_BUILD
&& page
== NULL
&& zlc_active
)) {
1443 /* Disable zlc cache for second zonelist scan */
1451 * This is the 'heart' of the zoned buddy allocator.
1454 __alloc_pages_internal(gfp_t gfp_mask
, unsigned int order
,
1455 struct zonelist
*zonelist
, nodemask_t
*nodemask
)
1457 const gfp_t wait
= gfp_mask
& __GFP_WAIT
;
1458 enum zone_type high_zoneidx
= gfp_zone(gfp_mask
);
1462 struct reclaim_state reclaim_state
;
1463 struct task_struct
*p
= current
;
1466 unsigned long did_some_progress
;
1467 unsigned long pages_reclaimed
= 0;
1469 might_sleep_if(wait
);
1471 if (should_fail_alloc_page(gfp_mask
, order
))
1475 z
= zonelist
->_zonerefs
; /* the list of zones suitable for gfp_mask */
1477 if (unlikely(!z
->zone
)) {
1479 * Happens if we have an empty zonelist as a result of
1480 * GFP_THISNODE being used on a memoryless node
1485 page
= get_page_from_freelist(gfp_mask
|__GFP_HARDWALL
, nodemask
, order
,
1486 zonelist
, high_zoneidx
, ALLOC_WMARK_LOW
|ALLOC_CPUSET
);
1491 * GFP_THISNODE (meaning __GFP_THISNODE, __GFP_NORETRY and
1492 * __GFP_NOWARN set) should not cause reclaim since the subsystem
1493 * (f.e. slab) using GFP_THISNODE may choose to trigger reclaim
1494 * using a larger set of nodes after it has established that the
1495 * allowed per node queues are empty and that nodes are
1498 if (NUMA_BUILD
&& (gfp_mask
& GFP_THISNODE
) == GFP_THISNODE
)
1501 for_each_zone_zonelist(zone
, z
, zonelist
, high_zoneidx
)
1502 wakeup_kswapd(zone
, order
);
1505 * OK, we're below the kswapd watermark and have kicked background
1506 * reclaim. Now things get more complex, so set up alloc_flags according
1507 * to how we want to proceed.
1509 * The caller may dip into page reserves a bit more if the caller
1510 * cannot run direct reclaim, or if the caller has realtime scheduling
1511 * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will
1512 * set both ALLOC_HARDER (!wait) and ALLOC_HIGH (__GFP_HIGH).
1514 alloc_flags
= ALLOC_WMARK_MIN
;
1515 if ((unlikely(rt_task(p
)) && !in_interrupt()) || !wait
)
1516 alloc_flags
|= ALLOC_HARDER
;
1517 if (gfp_mask
& __GFP_HIGH
)
1518 alloc_flags
|= ALLOC_HIGH
;
1520 alloc_flags
|= ALLOC_CPUSET
;
1523 * Go through the zonelist again. Let __GFP_HIGH and allocations
1524 * coming from realtime tasks go deeper into reserves.
1526 * This is the last chance, in general, before the goto nopage.
1527 * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
1528 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
1530 page
= get_page_from_freelist(gfp_mask
, nodemask
, order
, zonelist
,
1531 high_zoneidx
, alloc_flags
);
1535 /* This allocation should allow future memory freeing. */
1538 if (((p
->flags
& PF_MEMALLOC
) || unlikely(test_thread_flag(TIF_MEMDIE
)))
1539 && !in_interrupt()) {
1540 if (!(gfp_mask
& __GFP_NOMEMALLOC
)) {
1542 /* go through the zonelist yet again, ignoring mins */
1543 page
= get_page_from_freelist(gfp_mask
, nodemask
, order
,
1544 zonelist
, high_zoneidx
, ALLOC_NO_WATERMARKS
);
1547 if (gfp_mask
& __GFP_NOFAIL
) {
1548 congestion_wait(WRITE
, HZ
/50);
1555 /* Atomic allocations - we can't balance anything */
1561 /* We now go into synchronous reclaim */
1562 cpuset_memory_pressure_bump();
1564 * The task's cpuset might have expanded its set of allowable nodes
1566 cpuset_update_task_memory_state();
1567 p
->flags
|= PF_MEMALLOC
;
1568 reclaim_state
.reclaimed_slab
= 0;
1569 p
->reclaim_state
= &reclaim_state
;
1571 did_some_progress
= try_to_free_pages(zonelist
, order
, gfp_mask
);
1573 p
->reclaim_state
= NULL
;
1574 p
->flags
&= ~PF_MEMALLOC
;
1581 if (likely(did_some_progress
)) {
1582 page
= get_page_from_freelist(gfp_mask
, nodemask
, order
,
1583 zonelist
, high_zoneidx
, alloc_flags
);
1586 } else if ((gfp_mask
& __GFP_FS
) && !(gfp_mask
& __GFP_NORETRY
)) {
1587 if (!try_set_zone_oom(zonelist
, gfp_mask
)) {
1588 schedule_timeout_uninterruptible(1);
1593 * Go through the zonelist yet one more time, keep
1594 * very high watermark here, this is only to catch
1595 * a parallel oom killing, we must fail if we're still
1596 * under heavy pressure.
1598 page
= get_page_from_freelist(gfp_mask
|__GFP_HARDWALL
, nodemask
,
1599 order
, zonelist
, high_zoneidx
,
1600 ALLOC_WMARK_HIGH
|ALLOC_CPUSET
);
1602 clear_zonelist_oom(zonelist
, gfp_mask
);
1606 /* The OOM killer will not help higher order allocs so fail */
1607 if (order
> PAGE_ALLOC_COSTLY_ORDER
) {
1608 clear_zonelist_oom(zonelist
, gfp_mask
);
1612 out_of_memory(zonelist
, gfp_mask
, order
);
1613 clear_zonelist_oom(zonelist
, gfp_mask
);
1618 * Don't let big-order allocations loop unless the caller explicitly
1619 * requests that. Wait for some write requests to complete then retry.
1621 * In this implementation, order <= PAGE_ALLOC_COSTLY_ORDER
1622 * means __GFP_NOFAIL, but that may not be true in other
1625 * For order > PAGE_ALLOC_COSTLY_ORDER, if __GFP_REPEAT is
1626 * specified, then we retry until we no longer reclaim any pages
1627 * (above), or we've reclaimed an order of pages at least as
1628 * large as the allocation's order. In both cases, if the
1629 * allocation still fails, we stop retrying.
1631 pages_reclaimed
+= did_some_progress
;
1633 if (!(gfp_mask
& __GFP_NORETRY
)) {
1634 if (order
<= PAGE_ALLOC_COSTLY_ORDER
) {
1637 if (gfp_mask
& __GFP_REPEAT
&&
1638 pages_reclaimed
< (1 << order
))
1641 if (gfp_mask
& __GFP_NOFAIL
)
1645 congestion_wait(WRITE
, HZ
/50);
1650 if (!(gfp_mask
& __GFP_NOWARN
) && printk_ratelimit()) {
1651 printk(KERN_WARNING
"%s: page allocation failure."
1652 " order:%d, mode:0x%x\n",
1653 p
->comm
, order
, gfp_mask
);
1660 EXPORT_SYMBOL(__alloc_pages_internal
);
1663 * Common helper functions.
1665 unsigned long __get_free_pages(gfp_t gfp_mask
, unsigned int order
)
1668 page
= alloc_pages(gfp_mask
, order
);
1671 return (unsigned long) page_address(page
);
1674 EXPORT_SYMBOL(__get_free_pages
);
1676 unsigned long get_zeroed_page(gfp_t gfp_mask
)
1681 * get_zeroed_page() returns a 32-bit address, which cannot represent
1684 VM_BUG_ON((gfp_mask
& __GFP_HIGHMEM
) != 0);
1686 page
= alloc_pages(gfp_mask
| __GFP_ZERO
, 0);
1688 return (unsigned long) page_address(page
);
1692 EXPORT_SYMBOL(get_zeroed_page
);
1694 void __pagevec_free(struct pagevec
*pvec
)
1696 int i
= pagevec_count(pvec
);
1699 free_hot_cold_page(pvec
->pages
[i
], pvec
->cold
);
1702 void __free_pages(struct page
*page
, unsigned int order
)
1704 if (put_page_testzero(page
)) {
1706 free_hot_page(page
);
1708 __free_pages_ok(page
, order
);
1712 EXPORT_SYMBOL(__free_pages
);
1714 void free_pages(unsigned long addr
, unsigned int order
)
1717 VM_BUG_ON(!virt_addr_valid((void *)addr
));
1718 __free_pages(virt_to_page((void *)addr
), order
);
1722 EXPORT_SYMBOL(free_pages
);
1725 * alloc_pages_exact - allocate an exact number physically-contiguous pages.
1726 * @size: the number of bytes to allocate
1727 * @gfp_mask: GFP flags for the allocation
1729 * This function is similar to alloc_pages(), except that it allocates the
1730 * minimum number of pages to satisfy the request. alloc_pages() can only
1731 * allocate memory in power-of-two pages.
1733 * This function is also limited by MAX_ORDER.
1735 * Memory allocated by this function must be released by free_pages_exact().
1737 void *alloc_pages_exact(size_t size
, gfp_t gfp_mask
)
1739 unsigned int order
= get_order(size
);
1742 addr
= __get_free_pages(gfp_mask
, order
);
1744 unsigned long alloc_end
= addr
+ (PAGE_SIZE
<< order
);
1745 unsigned long used
= addr
+ PAGE_ALIGN(size
);
1747 split_page(virt_to_page(addr
), order
);
1748 while (used
< alloc_end
) {
1754 return (void *)addr
;
1756 EXPORT_SYMBOL(alloc_pages_exact
);
1759 * free_pages_exact - release memory allocated via alloc_pages_exact()
1760 * @virt: the value returned by alloc_pages_exact.
1761 * @size: size of allocation, same value as passed to alloc_pages_exact().
1763 * Release the memory allocated by a previous call to alloc_pages_exact.
1765 void free_pages_exact(void *virt
, size_t size
)
1767 unsigned long addr
= (unsigned long)virt
;
1768 unsigned long end
= addr
+ PAGE_ALIGN(size
);
1770 while (addr
< end
) {
1775 EXPORT_SYMBOL(free_pages_exact
);
1777 static unsigned int nr_free_zone_pages(int offset
)
1782 /* Just pick one node, since fallback list is circular */
1783 unsigned int sum
= 0;
1785 struct zonelist
*zonelist
= node_zonelist(numa_node_id(), GFP_KERNEL
);
1787 for_each_zone_zonelist(zone
, z
, zonelist
, offset
) {
1788 unsigned long size
= zone
->present_pages
;
1789 unsigned long high
= zone
->pages_high
;
1798 * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
1800 unsigned int nr_free_buffer_pages(void)
1802 return nr_free_zone_pages(gfp_zone(GFP_USER
));
1804 EXPORT_SYMBOL_GPL(nr_free_buffer_pages
);
1807 * Amount of free RAM allocatable within all zones
1809 unsigned int nr_free_pagecache_pages(void)
1811 return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER_MOVABLE
));
1814 static inline void show_node(struct zone
*zone
)
1817 printk("Node %d ", zone_to_nid(zone
));
1820 void si_meminfo(struct sysinfo
*val
)
1822 val
->totalram
= totalram_pages
;
1824 val
->freeram
= global_page_state(NR_FREE_PAGES
);
1825 val
->bufferram
= nr_blockdev_pages();
1826 val
->totalhigh
= totalhigh_pages
;
1827 val
->freehigh
= nr_free_highpages();
1828 val
->mem_unit
= PAGE_SIZE
;
1831 EXPORT_SYMBOL(si_meminfo
);
1834 void si_meminfo_node(struct sysinfo
*val
, int nid
)
1836 pg_data_t
*pgdat
= NODE_DATA(nid
);
1838 val
->totalram
= pgdat
->node_present_pages
;
1839 val
->freeram
= node_page_state(nid
, NR_FREE_PAGES
);
1840 #ifdef CONFIG_HIGHMEM
1841 val
->totalhigh
= pgdat
->node_zones
[ZONE_HIGHMEM
].present_pages
;
1842 val
->freehigh
= zone_page_state(&pgdat
->node_zones
[ZONE_HIGHMEM
],
1848 val
->mem_unit
= PAGE_SIZE
;
1852 #define K(x) ((x) << (PAGE_SHIFT-10))
1855 * Show free area list (used inside shift_scroll-lock stuff)
1856 * We also calculate the percentage fragmentation. We do this by counting the
1857 * memory on each free list with the exception of the first item on the list.
1859 void show_free_areas(void)
1864 for_each_zone(zone
) {
1865 if (!populated_zone(zone
))
1869 printk("%s per-cpu:\n", zone
->name
);
1871 for_each_online_cpu(cpu
) {
1872 struct per_cpu_pageset
*pageset
;
1874 pageset
= zone_pcp(zone
, cpu
);
1876 printk("CPU %4d: hi:%5d, btch:%4d usd:%4d\n",
1877 cpu
, pageset
->pcp
.high
,
1878 pageset
->pcp
.batch
, pageset
->pcp
.count
);
1882 printk("Active_anon:%lu active_file:%lu inactive_anon:%lu\n"
1883 " inactive_file:%lu"
1884 //TODO: check/adjust line lengths
1885 #ifdef CONFIG_UNEVICTABLE_LRU
1888 " dirty:%lu writeback:%lu unstable:%lu\n"
1889 " free:%lu slab:%lu mapped:%lu pagetables:%lu bounce:%lu\n",
1890 global_page_state(NR_ACTIVE_ANON
),
1891 global_page_state(NR_ACTIVE_FILE
),
1892 global_page_state(NR_INACTIVE_ANON
),
1893 global_page_state(NR_INACTIVE_FILE
),
1894 #ifdef CONFIG_UNEVICTABLE_LRU
1895 global_page_state(NR_UNEVICTABLE
),
1897 global_page_state(NR_FILE_DIRTY
),
1898 global_page_state(NR_WRITEBACK
),
1899 global_page_state(NR_UNSTABLE_NFS
),
1900 global_page_state(NR_FREE_PAGES
),
1901 global_page_state(NR_SLAB_RECLAIMABLE
) +
1902 global_page_state(NR_SLAB_UNRECLAIMABLE
),
1903 global_page_state(NR_FILE_MAPPED
),
1904 global_page_state(NR_PAGETABLE
),
1905 global_page_state(NR_BOUNCE
));
1907 for_each_zone(zone
) {
1910 if (!populated_zone(zone
))
1919 " active_anon:%lukB"
1920 " inactive_anon:%lukB"
1921 " active_file:%lukB"
1922 " inactive_file:%lukB"
1923 #ifdef CONFIG_UNEVICTABLE_LRU
1924 " unevictable:%lukB"
1927 " pages_scanned:%lu"
1928 " all_unreclaimable? %s"
1931 K(zone_page_state(zone
, NR_FREE_PAGES
)),
1934 K(zone
->pages_high
),
1935 K(zone_page_state(zone
, NR_ACTIVE_ANON
)),
1936 K(zone_page_state(zone
, NR_INACTIVE_ANON
)),
1937 K(zone_page_state(zone
, NR_ACTIVE_FILE
)),
1938 K(zone_page_state(zone
, NR_INACTIVE_FILE
)),
1939 #ifdef CONFIG_UNEVICTABLE_LRU
1940 K(zone_page_state(zone
, NR_UNEVICTABLE
)),
1942 K(zone
->present_pages
),
1943 zone
->pages_scanned
,
1944 (zone_is_all_unreclaimable(zone
) ? "yes" : "no")
1946 printk("lowmem_reserve[]:");
1947 for (i
= 0; i
< MAX_NR_ZONES
; i
++)
1948 printk(" %lu", zone
->lowmem_reserve
[i
]);
1952 for_each_zone(zone
) {
1953 unsigned long nr
[MAX_ORDER
], flags
, order
, total
= 0;
1955 if (!populated_zone(zone
))
1959 printk("%s: ", zone
->name
);
1961 spin_lock_irqsave(&zone
->lock
, flags
);
1962 for (order
= 0; order
< MAX_ORDER
; order
++) {
1963 nr
[order
] = zone
->free_area
[order
].nr_free
;
1964 total
+= nr
[order
] << order
;
1966 spin_unlock_irqrestore(&zone
->lock
, flags
);
1967 for (order
= 0; order
< MAX_ORDER
; order
++)
1968 printk("%lu*%lukB ", nr
[order
], K(1UL) << order
);
1969 printk("= %lukB\n", K(total
));
1972 printk("%ld total pagecache pages\n", global_page_state(NR_FILE_PAGES
));
1974 show_swap_cache_info();
1977 static void zoneref_set_zone(struct zone
*zone
, struct zoneref
*zoneref
)
1979 zoneref
->zone
= zone
;
1980 zoneref
->zone_idx
= zone_idx(zone
);
1984 * Builds allocation fallback zone lists.
1986 * Add all populated zones of a node to the zonelist.
1988 static int build_zonelists_node(pg_data_t
*pgdat
, struct zonelist
*zonelist
,
1989 int nr_zones
, enum zone_type zone_type
)
1993 BUG_ON(zone_type
>= MAX_NR_ZONES
);
1998 zone
= pgdat
->node_zones
+ zone_type
;
1999 if (populated_zone(zone
)) {
2000 zoneref_set_zone(zone
,
2001 &zonelist
->_zonerefs
[nr_zones
++]);
2002 check_highest_zone(zone_type
);
2005 } while (zone_type
);
2012 * 0 = automatic detection of better ordering.
2013 * 1 = order by ([node] distance, -zonetype)
2014 * 2 = order by (-zonetype, [node] distance)
2016 * If not NUMA, ZONELIST_ORDER_ZONE and ZONELIST_ORDER_NODE will create
2017 * the same zonelist. So only NUMA can configure this param.
2019 #define ZONELIST_ORDER_DEFAULT 0
2020 #define ZONELIST_ORDER_NODE 1
2021 #define ZONELIST_ORDER_ZONE 2
2023 /* zonelist order in the kernel.
2024 * set_zonelist_order() will set this to NODE or ZONE.
2026 static int current_zonelist_order
= ZONELIST_ORDER_DEFAULT
;
2027 static char zonelist_order_name
[3][8] = {"Default", "Node", "Zone"};
2031 /* The value user specified ....changed by config */
2032 static int user_zonelist_order
= ZONELIST_ORDER_DEFAULT
;
2033 /* string for sysctl */
2034 #define NUMA_ZONELIST_ORDER_LEN 16
2035 char numa_zonelist_order
[16] = "default";
2038 * interface for configure zonelist ordering.
2039 * command line option "numa_zonelist_order"
2040 * = "[dD]efault - default, automatic configuration.
2041 * = "[nN]ode - order by node locality, then by zone within node
2042 * = "[zZ]one - order by zone, then by locality within zone
2045 static int __parse_numa_zonelist_order(char *s
)
2047 if (*s
== 'd' || *s
== 'D') {
2048 user_zonelist_order
= ZONELIST_ORDER_DEFAULT
;
2049 } else if (*s
== 'n' || *s
== 'N') {
2050 user_zonelist_order
= ZONELIST_ORDER_NODE
;
2051 } else if (*s
== 'z' || *s
== 'Z') {
2052 user_zonelist_order
= ZONELIST_ORDER_ZONE
;
2055 "Ignoring invalid numa_zonelist_order value: "
2062 static __init
int setup_numa_zonelist_order(char *s
)
2065 return __parse_numa_zonelist_order(s
);
2068 early_param("numa_zonelist_order", setup_numa_zonelist_order
);
2071 * sysctl handler for numa_zonelist_order
2073 int numa_zonelist_order_handler(ctl_table
*table
, int write
,
2074 struct file
*file
, void __user
*buffer
, size_t *length
,
2077 char saved_string
[NUMA_ZONELIST_ORDER_LEN
];
2081 strncpy(saved_string
, (char*)table
->data
,
2082 NUMA_ZONELIST_ORDER_LEN
);
2083 ret
= proc_dostring(table
, write
, file
, buffer
, length
, ppos
);
2087 int oldval
= user_zonelist_order
;
2088 if (__parse_numa_zonelist_order((char*)table
->data
)) {
2090 * bogus value. restore saved string
2092 strncpy((char*)table
->data
, saved_string
,
2093 NUMA_ZONELIST_ORDER_LEN
);
2094 user_zonelist_order
= oldval
;
2095 } else if (oldval
!= user_zonelist_order
)
2096 build_all_zonelists();
2102 #define MAX_NODE_LOAD (num_online_nodes())
2103 static int node_load
[MAX_NUMNODES
];
2106 * find_next_best_node - find the next node that should appear in a given node's fallback list
2107 * @node: node whose fallback list we're appending
2108 * @used_node_mask: nodemask_t of already used nodes
2110 * We use a number of factors to determine which is the next node that should
2111 * appear on a given node's fallback list. The node should not have appeared
2112 * already in @node's fallback list, and it should be the next closest node
2113 * according to the distance array (which contains arbitrary distance values
2114 * from each node to each node in the system), and should also prefer nodes
2115 * with no CPUs, since presumably they'll have very little allocation pressure
2116 * on them otherwise.
2117 * It returns -1 if no node is found.
2119 static int find_next_best_node(int node
, nodemask_t
*used_node_mask
)
2122 int min_val
= INT_MAX
;
2124 node_to_cpumask_ptr(tmp
, 0);
2126 /* Use the local node if we haven't already */
2127 if (!node_isset(node
, *used_node_mask
)) {
2128 node_set(node
, *used_node_mask
);
2132 for_each_node_state(n
, N_HIGH_MEMORY
) {
2134 /* Don't want a node to appear more than once */
2135 if (node_isset(n
, *used_node_mask
))
2138 /* Use the distance array to find the distance */
2139 val
= node_distance(node
, n
);
2141 /* Penalize nodes under us ("prefer the next node") */
2144 /* Give preference to headless and unused nodes */
2145 node_to_cpumask_ptr_next(tmp
, n
);
2146 if (!cpus_empty(*tmp
))
2147 val
+= PENALTY_FOR_NODE_WITH_CPUS
;
2149 /* Slight preference for less loaded node */
2150 val
*= (MAX_NODE_LOAD
*MAX_NUMNODES
);
2151 val
+= node_load
[n
];
2153 if (val
< min_val
) {
2160 node_set(best_node
, *used_node_mask
);
2167 * Build zonelists ordered by node and zones within node.
2168 * This results in maximum locality--normal zone overflows into local
2169 * DMA zone, if any--but risks exhausting DMA zone.
2171 static void build_zonelists_in_node_order(pg_data_t
*pgdat
, int node
)
2174 struct zonelist
*zonelist
;
2176 zonelist
= &pgdat
->node_zonelists
[0];
2177 for (j
= 0; zonelist
->_zonerefs
[j
].zone
!= NULL
; j
++)
2179 j
= build_zonelists_node(NODE_DATA(node
), zonelist
, j
,
2181 zonelist
->_zonerefs
[j
].zone
= NULL
;
2182 zonelist
->_zonerefs
[j
].zone_idx
= 0;
2186 * Build gfp_thisnode zonelists
2188 static void build_thisnode_zonelists(pg_data_t
*pgdat
)
2191 struct zonelist
*zonelist
;
2193 zonelist
= &pgdat
->node_zonelists
[1];
2194 j
= build_zonelists_node(pgdat
, zonelist
, 0, MAX_NR_ZONES
- 1);
2195 zonelist
->_zonerefs
[j
].zone
= NULL
;
2196 zonelist
->_zonerefs
[j
].zone_idx
= 0;
2200 * Build zonelists ordered by zone and nodes within zones.
2201 * This results in conserving DMA zone[s] until all Normal memory is
2202 * exhausted, but results in overflowing to remote node while memory
2203 * may still exist in local DMA zone.
2205 static int node_order
[MAX_NUMNODES
];
2207 static void build_zonelists_in_zone_order(pg_data_t
*pgdat
, int nr_nodes
)
2210 int zone_type
; /* needs to be signed */
2212 struct zonelist
*zonelist
;
2214 zonelist
= &pgdat
->node_zonelists
[0];
2216 for (zone_type
= MAX_NR_ZONES
- 1; zone_type
>= 0; zone_type
--) {
2217 for (j
= 0; j
< nr_nodes
; j
++) {
2218 node
= node_order
[j
];
2219 z
= &NODE_DATA(node
)->node_zones
[zone_type
];
2220 if (populated_zone(z
)) {
2222 &zonelist
->_zonerefs
[pos
++]);
2223 check_highest_zone(zone_type
);
2227 zonelist
->_zonerefs
[pos
].zone
= NULL
;
2228 zonelist
->_zonerefs
[pos
].zone_idx
= 0;
2231 static int default_zonelist_order(void)
2234 unsigned long low_kmem_size
,total_size
;
2238 * ZONE_DMA and ZONE_DMA32 can be very small area in the sytem.
2239 * If they are really small and used heavily, the system can fall
2240 * into OOM very easily.
2241 * This function detect ZONE_DMA/DMA32 size and confgigures zone order.
2243 /* Is there ZONE_NORMAL ? (ex. ppc has only DMA zone..) */
2246 for_each_online_node(nid
) {
2247 for (zone_type
= 0; zone_type
< MAX_NR_ZONES
; zone_type
++) {
2248 z
= &NODE_DATA(nid
)->node_zones
[zone_type
];
2249 if (populated_zone(z
)) {
2250 if (zone_type
< ZONE_NORMAL
)
2251 low_kmem_size
+= z
->present_pages
;
2252 total_size
+= z
->present_pages
;
2256 if (!low_kmem_size
|| /* there are no DMA area. */
2257 low_kmem_size
> total_size
/2) /* DMA/DMA32 is big. */
2258 return ZONELIST_ORDER_NODE
;
2260 * look into each node's config.
2261 * If there is a node whose DMA/DMA32 memory is very big area on
2262 * local memory, NODE_ORDER may be suitable.
2264 average_size
= total_size
/
2265 (nodes_weight(node_states
[N_HIGH_MEMORY
]) + 1);
2266 for_each_online_node(nid
) {
2269 for (zone_type
= 0; zone_type
< MAX_NR_ZONES
; zone_type
++) {
2270 z
= &NODE_DATA(nid
)->node_zones
[zone_type
];
2271 if (populated_zone(z
)) {
2272 if (zone_type
< ZONE_NORMAL
)
2273 low_kmem_size
+= z
->present_pages
;
2274 total_size
+= z
->present_pages
;
2277 if (low_kmem_size
&&
2278 total_size
> average_size
&& /* ignore small node */
2279 low_kmem_size
> total_size
* 70/100)
2280 return ZONELIST_ORDER_NODE
;
2282 return ZONELIST_ORDER_ZONE
;
2285 static void set_zonelist_order(void)
2287 if (user_zonelist_order
== ZONELIST_ORDER_DEFAULT
)
2288 current_zonelist_order
= default_zonelist_order();
2290 current_zonelist_order
= user_zonelist_order
;
2293 static void build_zonelists(pg_data_t
*pgdat
)
2297 nodemask_t used_mask
;
2298 int local_node
, prev_node
;
2299 struct zonelist
*zonelist
;
2300 int order
= current_zonelist_order
;
2302 /* initialize zonelists */
2303 for (i
= 0; i
< MAX_ZONELISTS
; i
++) {
2304 zonelist
= pgdat
->node_zonelists
+ i
;
2305 zonelist
->_zonerefs
[0].zone
= NULL
;
2306 zonelist
->_zonerefs
[0].zone_idx
= 0;
2309 /* NUMA-aware ordering of nodes */
2310 local_node
= pgdat
->node_id
;
2311 load
= num_online_nodes();
2312 prev_node
= local_node
;
2313 nodes_clear(used_mask
);
2315 memset(node_load
, 0, sizeof(node_load
));
2316 memset(node_order
, 0, sizeof(node_order
));
2319 while ((node
= find_next_best_node(local_node
, &used_mask
)) >= 0) {
2320 int distance
= node_distance(local_node
, node
);
2323 * If another node is sufficiently far away then it is better
2324 * to reclaim pages in a zone before going off node.
2326 if (distance
> RECLAIM_DISTANCE
)
2327 zone_reclaim_mode
= 1;
2330 * We don't want to pressure a particular node.
2331 * So adding penalty to the first node in same
2332 * distance group to make it round-robin.
2334 if (distance
!= node_distance(local_node
, prev_node
))
2335 node_load
[node
] = load
;
2339 if (order
== ZONELIST_ORDER_NODE
)
2340 build_zonelists_in_node_order(pgdat
, node
);
2342 node_order
[j
++] = node
; /* remember order */
2345 if (order
== ZONELIST_ORDER_ZONE
) {
2346 /* calculate node order -- i.e., DMA last! */
2347 build_zonelists_in_zone_order(pgdat
, j
);
2350 build_thisnode_zonelists(pgdat
);
2353 /* Construct the zonelist performance cache - see further mmzone.h */
2354 static void build_zonelist_cache(pg_data_t
*pgdat
)
2356 struct zonelist
*zonelist
;
2357 struct zonelist_cache
*zlc
;
2360 zonelist
= &pgdat
->node_zonelists
[0];
2361 zonelist
->zlcache_ptr
= zlc
= &zonelist
->zlcache
;
2362 bitmap_zero(zlc
->fullzones
, MAX_ZONES_PER_ZONELIST
);
2363 for (z
= zonelist
->_zonerefs
; z
->zone
; z
++)
2364 zlc
->z_to_n
[z
- zonelist
->_zonerefs
] = zonelist_node_idx(z
);
2368 #else /* CONFIG_NUMA */
2370 static void set_zonelist_order(void)
2372 current_zonelist_order
= ZONELIST_ORDER_ZONE
;
2375 static void build_zonelists(pg_data_t
*pgdat
)
2377 int node
, local_node
;
2379 struct zonelist
*zonelist
;
2381 local_node
= pgdat
->node_id
;
2383 zonelist
= &pgdat
->node_zonelists
[0];
2384 j
= build_zonelists_node(pgdat
, zonelist
, 0, MAX_NR_ZONES
- 1);
2387 * Now we build the zonelist so that it contains the zones
2388 * of all the other nodes.
2389 * We don't want to pressure a particular node, so when
2390 * building the zones for node N, we make sure that the
2391 * zones coming right after the local ones are those from
2392 * node N+1 (modulo N)
2394 for (node
= local_node
+ 1; node
< MAX_NUMNODES
; node
++) {
2395 if (!node_online(node
))
2397 j
= build_zonelists_node(NODE_DATA(node
), zonelist
, j
,
2400 for (node
= 0; node
< local_node
; node
++) {
2401 if (!node_online(node
))
2403 j
= build_zonelists_node(NODE_DATA(node
), zonelist
, j
,
2407 zonelist
->_zonerefs
[j
].zone
= NULL
;
2408 zonelist
->_zonerefs
[j
].zone_idx
= 0;
2411 /* non-NUMA variant of zonelist performance cache - just NULL zlcache_ptr */
2412 static void build_zonelist_cache(pg_data_t
*pgdat
)
2414 pgdat
->node_zonelists
[0].zlcache_ptr
= NULL
;
2417 #endif /* CONFIG_NUMA */
2419 /* return values int ....just for stop_machine() */
2420 static int __build_all_zonelists(void *dummy
)
2424 for_each_online_node(nid
) {
2425 pg_data_t
*pgdat
= NODE_DATA(nid
);
2427 build_zonelists(pgdat
);
2428 build_zonelist_cache(pgdat
);
2433 void build_all_zonelists(void)
2435 set_zonelist_order();
2437 if (system_state
== SYSTEM_BOOTING
) {
2438 __build_all_zonelists(NULL
);
2439 mminit_verify_zonelist();
2440 cpuset_init_current_mems_allowed();
2442 /* we have to stop all cpus to guarantee there is no user
2444 stop_machine(__build_all_zonelists
, NULL
, NULL
);
2445 /* cpuset refresh routine should be here */
2447 vm_total_pages
= nr_free_pagecache_pages();
2449 * Disable grouping by mobility if the number of pages in the
2450 * system is too low to allow the mechanism to work. It would be
2451 * more accurate, but expensive to check per-zone. This check is
2452 * made on memory-hotadd so a system can start with mobility
2453 * disabled and enable it later
2455 if (vm_total_pages
< (pageblock_nr_pages
* MIGRATE_TYPES
))
2456 page_group_by_mobility_disabled
= 1;
2458 page_group_by_mobility_disabled
= 0;
2460 printk("Built %i zonelists in %s order, mobility grouping %s. "
2461 "Total pages: %ld\n",
2463 zonelist_order_name
[current_zonelist_order
],
2464 page_group_by_mobility_disabled
? "off" : "on",
2467 printk("Policy zone: %s\n", zone_names
[policy_zone
]);
2472 * Helper functions to size the waitqueue hash table.
2473 * Essentially these want to choose hash table sizes sufficiently
2474 * large so that collisions trying to wait on pages are rare.
2475 * But in fact, the number of active page waitqueues on typical
2476 * systems is ridiculously low, less than 200. So this is even
2477 * conservative, even though it seems large.
2479 * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
2480 * waitqueues, i.e. the size of the waitq table given the number of pages.
2482 #define PAGES_PER_WAITQUEUE 256
2484 #ifndef CONFIG_MEMORY_HOTPLUG
2485 static inline unsigned long wait_table_hash_nr_entries(unsigned long pages
)
2487 unsigned long size
= 1;
2489 pages
/= PAGES_PER_WAITQUEUE
;
2491 while (size
< pages
)
2495 * Once we have dozens or even hundreds of threads sleeping
2496 * on IO we've got bigger problems than wait queue collision.
2497 * Limit the size of the wait table to a reasonable size.
2499 size
= min(size
, 4096UL);
2501 return max(size
, 4UL);
2505 * A zone's size might be changed by hot-add, so it is not possible to determine
2506 * a suitable size for its wait_table. So we use the maximum size now.
2508 * The max wait table size = 4096 x sizeof(wait_queue_head_t). ie:
2510 * i386 (preemption config) : 4096 x 16 = 64Kbyte.
2511 * ia64, x86-64 (no preemption): 4096 x 20 = 80Kbyte.
2512 * ia64, x86-64 (preemption) : 4096 x 24 = 96Kbyte.
2514 * The maximum entries are prepared when a zone's memory is (512K + 256) pages
2515 * or more by the traditional way. (See above). It equals:
2517 * i386, x86-64, powerpc(4K page size) : = ( 2G + 1M)byte.
2518 * ia64(16K page size) : = ( 8G + 4M)byte.
2519 * powerpc (64K page size) : = (32G +16M)byte.
2521 static inline unsigned long wait_table_hash_nr_entries(unsigned long pages
)
2528 * This is an integer logarithm so that shifts can be used later
2529 * to extract the more random high bits from the multiplicative
2530 * hash function before the remainder is taken.
2532 static inline unsigned long wait_table_bits(unsigned long size
)
2537 #define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
2540 * Mark a number of pageblocks as MIGRATE_RESERVE. The number
2541 * of blocks reserved is based on zone->pages_min. The memory within the
2542 * reserve will tend to store contiguous free pages. Setting min_free_kbytes
2543 * higher will lead to a bigger reserve which will get freed as contiguous
2544 * blocks as reclaim kicks in
2546 static void setup_zone_migrate_reserve(struct zone
*zone
)
2548 unsigned long start_pfn
, pfn
, end_pfn
;
2550 unsigned long reserve
, block_migratetype
;
2552 /* Get the start pfn, end pfn and the number of blocks to reserve */
2553 start_pfn
= zone
->zone_start_pfn
;
2554 end_pfn
= start_pfn
+ zone
->spanned_pages
;
2555 reserve
= roundup(zone
->pages_min
, pageblock_nr_pages
) >>
2558 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
+= pageblock_nr_pages
) {
2559 if (!pfn_valid(pfn
))
2561 page
= pfn_to_page(pfn
);
2563 /* Watch out for overlapping nodes */
2564 if (page_to_nid(page
) != zone_to_nid(zone
))
2567 /* Blocks with reserved pages will never free, skip them. */
2568 if (PageReserved(page
))
2571 block_migratetype
= get_pageblock_migratetype(page
);
2573 /* If this block is reserved, account for it */
2574 if (reserve
> 0 && block_migratetype
== MIGRATE_RESERVE
) {
2579 /* Suitable for reserving if this block is movable */
2580 if (reserve
> 0 && block_migratetype
== MIGRATE_MOVABLE
) {
2581 set_pageblock_migratetype(page
, MIGRATE_RESERVE
);
2582 move_freepages_block(zone
, page
, MIGRATE_RESERVE
);
2588 * If the reserve is met and this is a previous reserved block,
2591 if (block_migratetype
== MIGRATE_RESERVE
) {
2592 set_pageblock_migratetype(page
, MIGRATE_MOVABLE
);
2593 move_freepages_block(zone
, page
, MIGRATE_MOVABLE
);
2599 * Initially all pages are reserved - free ones are freed
2600 * up by free_all_bootmem() once the early boot process is
2601 * done. Non-atomic initialization, single-pass.
2603 void __meminit
memmap_init_zone(unsigned long size
, int nid
, unsigned long zone
,
2604 unsigned long start_pfn
, enum memmap_context context
)
2607 unsigned long end_pfn
= start_pfn
+ size
;
2611 z
= &NODE_DATA(nid
)->node_zones
[zone
];
2612 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
++) {
2614 * There can be holes in boot-time mem_map[]s
2615 * handed to this function. They do not
2616 * exist on hotplugged memory.
2618 if (context
== MEMMAP_EARLY
) {
2619 if (!early_pfn_valid(pfn
))
2621 if (!early_pfn_in_nid(pfn
, nid
))
2624 page
= pfn_to_page(pfn
);
2625 set_page_links(page
, zone
, nid
, pfn
);
2626 mminit_verify_page_links(page
, zone
, nid
, pfn
);
2627 init_page_count(page
);
2628 reset_page_mapcount(page
);
2629 SetPageReserved(page
);
2631 * Mark the block movable so that blocks are reserved for
2632 * movable at startup. This will force kernel allocations
2633 * to reserve their blocks rather than leaking throughout
2634 * the address space during boot when many long-lived
2635 * kernel allocations are made. Later some blocks near
2636 * the start are marked MIGRATE_RESERVE by
2637 * setup_zone_migrate_reserve()
2639 * bitmap is created for zone's valid pfn range. but memmap
2640 * can be created for invalid pages (for alignment)
2641 * check here not to call set_pageblock_migratetype() against
2644 if ((z
->zone_start_pfn
<= pfn
)
2645 && (pfn
< z
->zone_start_pfn
+ z
->spanned_pages
)
2646 && !(pfn
& (pageblock_nr_pages
- 1)))
2647 set_pageblock_migratetype(page
, MIGRATE_MOVABLE
);
2649 INIT_LIST_HEAD(&page
->lru
);
2650 #ifdef WANT_PAGE_VIRTUAL
2651 /* The shift won't overflow because ZONE_NORMAL is below 4G. */
2652 if (!is_highmem_idx(zone
))
2653 set_page_address(page
, __va(pfn
<< PAGE_SHIFT
));
2658 static void __meminit
zone_init_free_lists(struct zone
*zone
)
2661 for_each_migratetype_order(order
, t
) {
2662 INIT_LIST_HEAD(&zone
->free_area
[order
].free_list
[t
]);
2663 zone
->free_area
[order
].nr_free
= 0;
2667 #ifndef __HAVE_ARCH_MEMMAP_INIT
2668 #define memmap_init(size, nid, zone, start_pfn) \
2669 memmap_init_zone((size), (nid), (zone), (start_pfn), MEMMAP_EARLY)
2672 static int zone_batchsize(struct zone
*zone
)
2677 * The per-cpu-pages pools are set to around 1000th of the
2678 * size of the zone. But no more than 1/2 of a meg.
2680 * OK, so we don't know how big the cache is. So guess.
2682 batch
= zone
->present_pages
/ 1024;
2683 if (batch
* PAGE_SIZE
> 512 * 1024)
2684 batch
= (512 * 1024) / PAGE_SIZE
;
2685 batch
/= 4; /* We effectively *= 4 below */
2690 * Clamp the batch to a 2^n - 1 value. Having a power
2691 * of 2 value was found to be more likely to have
2692 * suboptimal cache aliasing properties in some cases.
2694 * For example if 2 tasks are alternately allocating
2695 * batches of pages, one task can end up with a lot
2696 * of pages of one half of the possible page colors
2697 * and the other with pages of the other colors.
2699 batch
= (1 << (fls(batch
+ batch
/2)-1)) - 1;
2704 static void setup_pageset(struct per_cpu_pageset
*p
, unsigned long batch
)
2706 struct per_cpu_pages
*pcp
;
2708 memset(p
, 0, sizeof(*p
));
2712 pcp
->high
= 6 * batch
;
2713 pcp
->batch
= max(1UL, 1 * batch
);
2714 INIT_LIST_HEAD(&pcp
->list
);
2718 * setup_pagelist_highmark() sets the high water mark for hot per_cpu_pagelist
2719 * to the value high for the pageset p.
2722 static void setup_pagelist_highmark(struct per_cpu_pageset
*p
,
2725 struct per_cpu_pages
*pcp
;
2729 pcp
->batch
= max(1UL, high
/4);
2730 if ((high
/4) > (PAGE_SHIFT
* 8))
2731 pcp
->batch
= PAGE_SHIFT
* 8;
2737 * Boot pageset table. One per cpu which is going to be used for all
2738 * zones and all nodes. The parameters will be set in such a way
2739 * that an item put on a list will immediately be handed over to
2740 * the buddy list. This is safe since pageset manipulation is done
2741 * with interrupts disabled.
2743 * Some NUMA counter updates may also be caught by the boot pagesets.
2745 * The boot_pagesets must be kept even after bootup is complete for
2746 * unused processors and/or zones. They do play a role for bootstrapping
2747 * hotplugged processors.
2749 * zoneinfo_show() and maybe other functions do
2750 * not check if the processor is online before following the pageset pointer.
2751 * Other parts of the kernel may not check if the zone is available.
2753 static struct per_cpu_pageset boot_pageset
[NR_CPUS
];
2756 * Dynamically allocate memory for the
2757 * per cpu pageset array in struct zone.
2759 static int __cpuinit
process_zones(int cpu
)
2761 struct zone
*zone
, *dzone
;
2762 int node
= cpu_to_node(cpu
);
2764 node_set_state(node
, N_CPU
); /* this node has a cpu */
2766 for_each_zone(zone
) {
2768 if (!populated_zone(zone
))
2771 zone_pcp(zone
, cpu
) = kmalloc_node(sizeof(struct per_cpu_pageset
),
2773 if (!zone_pcp(zone
, cpu
))
2776 setup_pageset(zone_pcp(zone
, cpu
), zone_batchsize(zone
));
2778 if (percpu_pagelist_fraction
)
2779 setup_pagelist_highmark(zone_pcp(zone
, cpu
),
2780 (zone
->present_pages
/ percpu_pagelist_fraction
));
2785 for_each_zone(dzone
) {
2786 if (!populated_zone(dzone
))
2790 kfree(zone_pcp(dzone
, cpu
));
2791 zone_pcp(dzone
, cpu
) = NULL
;
2796 static inline void free_zone_pagesets(int cpu
)
2800 for_each_zone(zone
) {
2801 struct per_cpu_pageset
*pset
= zone_pcp(zone
, cpu
);
2803 /* Free per_cpu_pageset if it is slab allocated */
2804 if (pset
!= &boot_pageset
[cpu
])
2806 zone_pcp(zone
, cpu
) = NULL
;
2810 static int __cpuinit
pageset_cpuup_callback(struct notifier_block
*nfb
,
2811 unsigned long action
,
2814 int cpu
= (long)hcpu
;
2815 int ret
= NOTIFY_OK
;
2818 case CPU_UP_PREPARE
:
2819 case CPU_UP_PREPARE_FROZEN
:
2820 if (process_zones(cpu
))
2823 case CPU_UP_CANCELED
:
2824 case CPU_UP_CANCELED_FROZEN
:
2826 case CPU_DEAD_FROZEN
:
2827 free_zone_pagesets(cpu
);
2835 static struct notifier_block __cpuinitdata pageset_notifier
=
2836 { &pageset_cpuup_callback
, NULL
, 0 };
2838 void __init
setup_per_cpu_pageset(void)
2842 /* Initialize per_cpu_pageset for cpu 0.
2843 * A cpuup callback will do this for every cpu
2844 * as it comes online
2846 err
= process_zones(smp_processor_id());
2848 register_cpu_notifier(&pageset_notifier
);
2853 static noinline __init_refok
2854 int zone_wait_table_init(struct zone
*zone
, unsigned long zone_size_pages
)
2857 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
2861 * The per-page waitqueue mechanism uses hashed waitqueues
2864 zone
->wait_table_hash_nr_entries
=
2865 wait_table_hash_nr_entries(zone_size_pages
);
2866 zone
->wait_table_bits
=
2867 wait_table_bits(zone
->wait_table_hash_nr_entries
);
2868 alloc_size
= zone
->wait_table_hash_nr_entries
2869 * sizeof(wait_queue_head_t
);
2871 if (!slab_is_available()) {
2872 zone
->wait_table
= (wait_queue_head_t
*)
2873 alloc_bootmem_node(pgdat
, alloc_size
);
2876 * This case means that a zone whose size was 0 gets new memory
2877 * via memory hot-add.
2878 * But it may be the case that a new node was hot-added. In
2879 * this case vmalloc() will not be able to use this new node's
2880 * memory - this wait_table must be initialized to use this new
2881 * node itself as well.
2882 * To use this new node's memory, further consideration will be
2885 zone
->wait_table
= vmalloc(alloc_size
);
2887 if (!zone
->wait_table
)
2890 for(i
= 0; i
< zone
->wait_table_hash_nr_entries
; ++i
)
2891 init_waitqueue_head(zone
->wait_table
+ i
);
2896 static __meminit
void zone_pcp_init(struct zone
*zone
)
2899 unsigned long batch
= zone_batchsize(zone
);
2901 for (cpu
= 0; cpu
< NR_CPUS
; cpu
++) {
2903 /* Early boot. Slab allocator not functional yet */
2904 zone_pcp(zone
, cpu
) = &boot_pageset
[cpu
];
2905 setup_pageset(&boot_pageset
[cpu
],0);
2907 setup_pageset(zone_pcp(zone
,cpu
), batch
);
2910 if (zone
->present_pages
)
2911 printk(KERN_DEBUG
" %s zone: %lu pages, LIFO batch:%lu\n",
2912 zone
->name
, zone
->present_pages
, batch
);
2915 __meminit
int init_currently_empty_zone(struct zone
*zone
,
2916 unsigned long zone_start_pfn
,
2918 enum memmap_context context
)
2920 struct pglist_data
*pgdat
= zone
->zone_pgdat
;
2922 ret
= zone_wait_table_init(zone
, size
);
2925 pgdat
->nr_zones
= zone_idx(zone
) + 1;
2927 zone
->zone_start_pfn
= zone_start_pfn
;
2929 mminit_dprintk(MMINIT_TRACE
, "memmap_init",
2930 "Initialising map node %d zone %lu pfns %lu -> %lu\n",
2932 (unsigned long)zone_idx(zone
),
2933 zone_start_pfn
, (zone_start_pfn
+ size
));
2935 zone_init_free_lists(zone
);
2940 #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
2942 * Basic iterator support. Return the first range of PFNs for a node
2943 * Note: nid == MAX_NUMNODES returns first region regardless of node
2945 static int __meminit
first_active_region_index_in_nid(int nid
)
2949 for (i
= 0; i
< nr_nodemap_entries
; i
++)
2950 if (nid
== MAX_NUMNODES
|| early_node_map
[i
].nid
== nid
)
2957 * Basic iterator support. Return the next active range of PFNs for a node
2958 * Note: nid == MAX_NUMNODES returns next region regardless of node
2960 static int __meminit
next_active_region_index_in_nid(int index
, int nid
)
2962 for (index
= index
+ 1; index
< nr_nodemap_entries
; index
++)
2963 if (nid
== MAX_NUMNODES
|| early_node_map
[index
].nid
== nid
)
2969 #ifndef CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID
2971 * Required by SPARSEMEM. Given a PFN, return what node the PFN is on.
2972 * Architectures may implement their own version but if add_active_range()
2973 * was used and there are no special requirements, this is a convenient
2976 int __meminit
early_pfn_to_nid(unsigned long pfn
)
2980 for (i
= 0; i
< nr_nodemap_entries
; i
++) {
2981 unsigned long start_pfn
= early_node_map
[i
].start_pfn
;
2982 unsigned long end_pfn
= early_node_map
[i
].end_pfn
;
2984 if (start_pfn
<= pfn
&& pfn
< end_pfn
)
2985 return early_node_map
[i
].nid
;
2990 #endif /* CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID */
2992 /* Basic iterator support to walk early_node_map[] */
2993 #define for_each_active_range_index_in_nid(i, nid) \
2994 for (i = first_active_region_index_in_nid(nid); i != -1; \
2995 i = next_active_region_index_in_nid(i, nid))
2998 * free_bootmem_with_active_regions - Call free_bootmem_node for each active range
2999 * @nid: The node to free memory on. If MAX_NUMNODES, all nodes are freed.
3000 * @max_low_pfn: The highest PFN that will be passed to free_bootmem_node
3002 * If an architecture guarantees that all ranges registered with
3003 * add_active_ranges() contain no holes and may be freed, this
3004 * this function may be used instead of calling free_bootmem() manually.
3006 void __init
free_bootmem_with_active_regions(int nid
,
3007 unsigned long max_low_pfn
)
3011 for_each_active_range_index_in_nid(i
, nid
) {
3012 unsigned long size_pages
= 0;
3013 unsigned long end_pfn
= early_node_map
[i
].end_pfn
;
3015 if (early_node_map
[i
].start_pfn
>= max_low_pfn
)
3018 if (end_pfn
> max_low_pfn
)
3019 end_pfn
= max_low_pfn
;
3021 size_pages
= end_pfn
- early_node_map
[i
].start_pfn
;
3022 free_bootmem_node(NODE_DATA(early_node_map
[i
].nid
),
3023 PFN_PHYS(early_node_map
[i
].start_pfn
),
3024 size_pages
<< PAGE_SHIFT
);
3028 void __init
work_with_active_regions(int nid
, work_fn_t work_fn
, void *data
)
3033 for_each_active_range_index_in_nid(i
, nid
) {
3034 ret
= work_fn(early_node_map
[i
].start_pfn
,
3035 early_node_map
[i
].end_pfn
, data
);
3041 * sparse_memory_present_with_active_regions - Call memory_present for each active range
3042 * @nid: The node to call memory_present for. If MAX_NUMNODES, all nodes will be used.
3044 * If an architecture guarantees that all ranges registered with
3045 * add_active_ranges() contain no holes and may be freed, this
3046 * function may be used instead of calling memory_present() manually.
3048 void __init
sparse_memory_present_with_active_regions(int nid
)
3052 for_each_active_range_index_in_nid(i
, nid
)
3053 memory_present(early_node_map
[i
].nid
,
3054 early_node_map
[i
].start_pfn
,
3055 early_node_map
[i
].end_pfn
);
3059 * push_node_boundaries - Push node boundaries to at least the requested boundary
3060 * @nid: The nid of the node to push the boundary for
3061 * @start_pfn: The start pfn of the node
3062 * @end_pfn: The end pfn of the node
3064 * In reserve-based hot-add, mem_map is allocated that is unused until hotadd
3065 * time. Specifically, on x86_64, SRAT will report ranges that can potentially
3066 * be hotplugged even though no physical memory exists. This function allows
3067 * an arch to push out the node boundaries so mem_map is allocated that can
3070 #ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
3071 void __init
push_node_boundaries(unsigned int nid
,
3072 unsigned long start_pfn
, unsigned long end_pfn
)
3074 mminit_dprintk(MMINIT_TRACE
, "zoneboundary",
3075 "Entering push_node_boundaries(%u, %lu, %lu)\n",
3076 nid
, start_pfn
, end_pfn
);
3078 /* Initialise the boundary for this node if necessary */
3079 if (node_boundary_end_pfn
[nid
] == 0)
3080 node_boundary_start_pfn
[nid
] = -1UL;
3082 /* Update the boundaries */
3083 if (node_boundary_start_pfn
[nid
] > start_pfn
)
3084 node_boundary_start_pfn
[nid
] = start_pfn
;
3085 if (node_boundary_end_pfn
[nid
] < end_pfn
)
3086 node_boundary_end_pfn
[nid
] = end_pfn
;
3089 /* If necessary, push the node boundary out for reserve hotadd */
3090 static void __meminit
account_node_boundary(unsigned int nid
,
3091 unsigned long *start_pfn
, unsigned long *end_pfn
)
3093 mminit_dprintk(MMINIT_TRACE
, "zoneboundary",
3094 "Entering account_node_boundary(%u, %lu, %lu)\n",
3095 nid
, *start_pfn
, *end_pfn
);
3097 /* Return if boundary information has not been provided */
3098 if (node_boundary_end_pfn
[nid
] == 0)
3101 /* Check the boundaries and update if necessary */
3102 if (node_boundary_start_pfn
[nid
] < *start_pfn
)
3103 *start_pfn
= node_boundary_start_pfn
[nid
];
3104 if (node_boundary_end_pfn
[nid
] > *end_pfn
)
3105 *end_pfn
= node_boundary_end_pfn
[nid
];
3108 void __init
push_node_boundaries(unsigned int nid
,
3109 unsigned long start_pfn
, unsigned long end_pfn
) {}
3111 static void __meminit
account_node_boundary(unsigned int nid
,
3112 unsigned long *start_pfn
, unsigned long *end_pfn
) {}
3117 * get_pfn_range_for_nid - Return the start and end page frames for a node
3118 * @nid: The nid to return the range for. If MAX_NUMNODES, the min and max PFN are returned.
3119 * @start_pfn: Passed by reference. On return, it will have the node start_pfn.
3120 * @end_pfn: Passed by reference. On return, it will have the node end_pfn.
3122 * It returns the start and end page frame of a node based on information
3123 * provided by an arch calling add_active_range(). If called for a node
3124 * with no available memory, a warning is printed and the start and end
3127 void __meminit
get_pfn_range_for_nid(unsigned int nid
,
3128 unsigned long *start_pfn
, unsigned long *end_pfn
)
3134 for_each_active_range_index_in_nid(i
, nid
) {
3135 *start_pfn
= min(*start_pfn
, early_node_map
[i
].start_pfn
);
3136 *end_pfn
= max(*end_pfn
, early_node_map
[i
].end_pfn
);
3139 if (*start_pfn
== -1UL)
3142 /* Push the node boundaries out if requested */
3143 account_node_boundary(nid
, start_pfn
, end_pfn
);
3147 * This finds a zone that can be used for ZONE_MOVABLE pages. The
3148 * assumption is made that zones within a node are ordered in monotonic
3149 * increasing memory addresses so that the "highest" populated zone is used
3151 static void __init
find_usable_zone_for_movable(void)
3154 for (zone_index
= MAX_NR_ZONES
- 1; zone_index
>= 0; zone_index
--) {
3155 if (zone_index
== ZONE_MOVABLE
)
3158 if (arch_zone_highest_possible_pfn
[zone_index
] >
3159 arch_zone_lowest_possible_pfn
[zone_index
])
3163 VM_BUG_ON(zone_index
== -1);
3164 movable_zone
= zone_index
;
3168 * The zone ranges provided by the architecture do not include ZONE_MOVABLE
3169 * because it is sized independant of architecture. Unlike the other zones,
3170 * the starting point for ZONE_MOVABLE is not fixed. It may be different
3171 * in each node depending on the size of each node and how evenly kernelcore
3172 * is distributed. This helper function adjusts the zone ranges
3173 * provided by the architecture for a given node by using the end of the
3174 * highest usable zone for ZONE_MOVABLE. This preserves the assumption that
3175 * zones within a node are in order of monotonic increases memory addresses
3177 static void __meminit
adjust_zone_range_for_zone_movable(int nid
,
3178 unsigned long zone_type
,
3179 unsigned long node_start_pfn
,
3180 unsigned long node_end_pfn
,
3181 unsigned long *zone_start_pfn
,
3182 unsigned long *zone_end_pfn
)
3184 /* Only adjust if ZONE_MOVABLE is on this node */
3185 if (zone_movable_pfn
[nid
]) {
3186 /* Size ZONE_MOVABLE */
3187 if (zone_type
== ZONE_MOVABLE
) {
3188 *zone_start_pfn
= zone_movable_pfn
[nid
];
3189 *zone_end_pfn
= min(node_end_pfn
,
3190 arch_zone_highest_possible_pfn
[movable_zone
]);
3192 /* Adjust for ZONE_MOVABLE starting within this range */
3193 } else if (*zone_start_pfn
< zone_movable_pfn
[nid
] &&
3194 *zone_end_pfn
> zone_movable_pfn
[nid
]) {
3195 *zone_end_pfn
= zone_movable_pfn
[nid
];
3197 /* Check if this whole range is within ZONE_MOVABLE */
3198 } else if (*zone_start_pfn
>= zone_movable_pfn
[nid
])
3199 *zone_start_pfn
= *zone_end_pfn
;
3204 * Return the number of pages a zone spans in a node, including holes
3205 * present_pages = zone_spanned_pages_in_node() - zone_absent_pages_in_node()
3207 static unsigned long __meminit
zone_spanned_pages_in_node(int nid
,
3208 unsigned long zone_type
,
3209 unsigned long *ignored
)
3211 unsigned long node_start_pfn
, node_end_pfn
;
3212 unsigned long zone_start_pfn
, zone_end_pfn
;
3214 /* Get the start and end of the node and zone */
3215 get_pfn_range_for_nid(nid
, &node_start_pfn
, &node_end_pfn
);
3216 zone_start_pfn
= arch_zone_lowest_possible_pfn
[zone_type
];
3217 zone_end_pfn
= arch_zone_highest_possible_pfn
[zone_type
];
3218 adjust_zone_range_for_zone_movable(nid
, zone_type
,
3219 node_start_pfn
, node_end_pfn
,
3220 &zone_start_pfn
, &zone_end_pfn
);
3222 /* Check that this node has pages within the zone's required range */
3223 if (zone_end_pfn
< node_start_pfn
|| zone_start_pfn
> node_end_pfn
)
3226 /* Move the zone boundaries inside the node if necessary */
3227 zone_end_pfn
= min(zone_end_pfn
, node_end_pfn
);
3228 zone_start_pfn
= max(zone_start_pfn
, node_start_pfn
);
3230 /* Return the spanned pages */
3231 return zone_end_pfn
- zone_start_pfn
;
3235 * Return the number of holes in a range on a node. If nid is MAX_NUMNODES,
3236 * then all holes in the requested range will be accounted for.
3238 static unsigned long __meminit
__absent_pages_in_range(int nid
,
3239 unsigned long range_start_pfn
,
3240 unsigned long range_end_pfn
)
3243 unsigned long prev_end_pfn
= 0, hole_pages
= 0;
3244 unsigned long start_pfn
;
3246 /* Find the end_pfn of the first active range of pfns in the node */
3247 i
= first_active_region_index_in_nid(nid
);
3251 prev_end_pfn
= min(early_node_map
[i
].start_pfn
, range_end_pfn
);
3253 /* Account for ranges before physical memory on this node */
3254 if (early_node_map
[i
].start_pfn
> range_start_pfn
)
3255 hole_pages
= prev_end_pfn
- range_start_pfn
;
3257 /* Find all holes for the zone within the node */
3258 for (; i
!= -1; i
= next_active_region_index_in_nid(i
, nid
)) {
3260 /* No need to continue if prev_end_pfn is outside the zone */
3261 if (prev_end_pfn
>= range_end_pfn
)
3264 /* Make sure the end of the zone is not within the hole */
3265 start_pfn
= min(early_node_map
[i
].start_pfn
, range_end_pfn
);
3266 prev_end_pfn
= max(prev_end_pfn
, range_start_pfn
);
3268 /* Update the hole size cound and move on */
3269 if (start_pfn
> range_start_pfn
) {
3270 BUG_ON(prev_end_pfn
> start_pfn
);
3271 hole_pages
+= start_pfn
- prev_end_pfn
;
3273 prev_end_pfn
= early_node_map
[i
].end_pfn
;
3276 /* Account for ranges past physical memory on this node */
3277 if (range_end_pfn
> prev_end_pfn
)
3278 hole_pages
+= range_end_pfn
-
3279 max(range_start_pfn
, prev_end_pfn
);
3285 * absent_pages_in_range - Return number of page frames in holes within a range
3286 * @start_pfn: The start PFN to start searching for holes
3287 * @end_pfn: The end PFN to stop searching for holes
3289 * It returns the number of pages frames in memory holes within a range.
3291 unsigned long __init
absent_pages_in_range(unsigned long start_pfn
,
3292 unsigned long end_pfn
)
3294 return __absent_pages_in_range(MAX_NUMNODES
, start_pfn
, end_pfn
);
3297 /* Return the number of page frames in holes in a zone on a node */
3298 static unsigned long __meminit
zone_absent_pages_in_node(int nid
,
3299 unsigned long zone_type
,
3300 unsigned long *ignored
)
3302 unsigned long node_start_pfn
, node_end_pfn
;
3303 unsigned long zone_start_pfn
, zone_end_pfn
;
3305 get_pfn_range_for_nid(nid
, &node_start_pfn
, &node_end_pfn
);
3306 zone_start_pfn
= max(arch_zone_lowest_possible_pfn
[zone_type
],
3308 zone_end_pfn
= min(arch_zone_highest_possible_pfn
[zone_type
],
3311 adjust_zone_range_for_zone_movable(nid
, zone_type
,
3312 node_start_pfn
, node_end_pfn
,
3313 &zone_start_pfn
, &zone_end_pfn
);
3314 return __absent_pages_in_range(nid
, zone_start_pfn
, zone_end_pfn
);
3318 static inline unsigned long __meminit
zone_spanned_pages_in_node(int nid
,
3319 unsigned long zone_type
,
3320 unsigned long *zones_size
)
3322 return zones_size
[zone_type
];
3325 static inline unsigned long __meminit
zone_absent_pages_in_node(int nid
,
3326 unsigned long zone_type
,
3327 unsigned long *zholes_size
)
3332 return zholes_size
[zone_type
];
3337 static void __meminit
calculate_node_totalpages(struct pglist_data
*pgdat
,
3338 unsigned long *zones_size
, unsigned long *zholes_size
)
3340 unsigned long realtotalpages
, totalpages
= 0;
3343 for (i
= 0; i
< MAX_NR_ZONES
; i
++)
3344 totalpages
+= zone_spanned_pages_in_node(pgdat
->node_id
, i
,
3346 pgdat
->node_spanned_pages
= totalpages
;
3348 realtotalpages
= totalpages
;
3349 for (i
= 0; i
< MAX_NR_ZONES
; i
++)
3351 zone_absent_pages_in_node(pgdat
->node_id
, i
,
3353 pgdat
->node_present_pages
= realtotalpages
;
3354 printk(KERN_DEBUG
"On node %d totalpages: %lu\n", pgdat
->node_id
,
3358 #ifndef CONFIG_SPARSEMEM
3360 * Calculate the size of the zone->blockflags rounded to an unsigned long
3361 * Start by making sure zonesize is a multiple of pageblock_order by rounding
3362 * up. Then use 1 NR_PAGEBLOCK_BITS worth of bits per pageblock, finally
3363 * round what is now in bits to nearest long in bits, then return it in
3366 static unsigned long __init
usemap_size(unsigned long zonesize
)
3368 unsigned long usemapsize
;
3370 usemapsize
= roundup(zonesize
, pageblock_nr_pages
);
3371 usemapsize
= usemapsize
>> pageblock_order
;
3372 usemapsize
*= NR_PAGEBLOCK_BITS
;
3373 usemapsize
= roundup(usemapsize
, 8 * sizeof(unsigned long));
3375 return usemapsize
/ 8;
3378 static void __init
setup_usemap(struct pglist_data
*pgdat
,
3379 struct zone
*zone
, unsigned long zonesize
)
3381 unsigned long usemapsize
= usemap_size(zonesize
);
3382 zone
->pageblock_flags
= NULL
;
3384 zone
->pageblock_flags
= alloc_bootmem_node(pgdat
, usemapsize
);
3387 static void inline setup_usemap(struct pglist_data
*pgdat
,
3388 struct zone
*zone
, unsigned long zonesize
) {}
3389 #endif /* CONFIG_SPARSEMEM */
3391 #ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
3393 /* Return a sensible default order for the pageblock size. */
3394 static inline int pageblock_default_order(void)
3396 if (HPAGE_SHIFT
> PAGE_SHIFT
)
3397 return HUGETLB_PAGE_ORDER
;
3402 /* Initialise the number of pages represented by NR_PAGEBLOCK_BITS */
3403 static inline void __init
set_pageblock_order(unsigned int order
)
3405 /* Check that pageblock_nr_pages has not already been setup */
3406 if (pageblock_order
)
3410 * Assume the largest contiguous order of interest is a huge page.
3411 * This value may be variable depending on boot parameters on IA64
3413 pageblock_order
= order
;
3415 #else /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
3418 * When CONFIG_HUGETLB_PAGE_SIZE_VARIABLE is not set, set_pageblock_order()
3419 * and pageblock_default_order() are unused as pageblock_order is set
3420 * at compile-time. See include/linux/pageblock-flags.h for the values of
3421 * pageblock_order based on the kernel config
3423 static inline int pageblock_default_order(unsigned int order
)
3427 #define set_pageblock_order(x) do {} while (0)
3429 #endif /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
3432 * Set up the zone data structures:
3433 * - mark all pages reserved
3434 * - mark all memory queues empty
3435 * - clear the memory bitmaps
3437 static void __paginginit
free_area_init_core(struct pglist_data
*pgdat
,
3438 unsigned long *zones_size
, unsigned long *zholes_size
)
3441 int nid
= pgdat
->node_id
;
3442 unsigned long zone_start_pfn
= pgdat
->node_start_pfn
;
3445 pgdat_resize_init(pgdat
);
3446 pgdat
->nr_zones
= 0;
3447 init_waitqueue_head(&pgdat
->kswapd_wait
);
3448 pgdat
->kswapd_max_order
= 0;
3449 pgdat_page_cgroup_init(pgdat
);
3451 for (j
= 0; j
< MAX_NR_ZONES
; j
++) {
3452 struct zone
*zone
= pgdat
->node_zones
+ j
;
3453 unsigned long size
, realsize
, memmap_pages
;
3456 size
= zone_spanned_pages_in_node(nid
, j
, zones_size
);
3457 realsize
= size
- zone_absent_pages_in_node(nid
, j
,
3461 * Adjust realsize so that it accounts for how much memory
3462 * is used by this zone for memmap. This affects the watermark
3463 * and per-cpu initialisations
3466 PAGE_ALIGN(size
* sizeof(struct page
)) >> PAGE_SHIFT
;
3467 if (realsize
>= memmap_pages
) {
3468 realsize
-= memmap_pages
;
3471 " %s zone: %lu pages used for memmap\n",
3472 zone_names
[j
], memmap_pages
);
3475 " %s zone: %lu pages exceeds realsize %lu\n",
3476 zone_names
[j
], memmap_pages
, realsize
);
3478 /* Account for reserved pages */
3479 if (j
== 0 && realsize
> dma_reserve
) {
3480 realsize
-= dma_reserve
;
3481 printk(KERN_DEBUG
" %s zone: %lu pages reserved\n",
3482 zone_names
[0], dma_reserve
);
3485 if (!is_highmem_idx(j
))
3486 nr_kernel_pages
+= realsize
;
3487 nr_all_pages
+= realsize
;
3489 zone
->spanned_pages
= size
;
3490 zone
->present_pages
= realsize
;
3493 zone
->min_unmapped_pages
= (realsize
*sysctl_min_unmapped_ratio
)
3495 zone
->min_slab_pages
= (realsize
* sysctl_min_slab_ratio
) / 100;
3497 zone
->name
= zone_names
[j
];
3498 spin_lock_init(&zone
->lock
);
3499 spin_lock_init(&zone
->lru_lock
);
3500 zone_seqlock_init(zone
);
3501 zone
->zone_pgdat
= pgdat
;
3503 zone
->prev_priority
= DEF_PRIORITY
;
3505 zone_pcp_init(zone
);
3507 INIT_LIST_HEAD(&zone
->lru
[l
].list
);
3508 zone
->lru
[l
].nr_scan
= 0;
3510 zone
->recent_rotated
[0] = 0;
3511 zone
->recent_rotated
[1] = 0;
3512 zone
->recent_scanned
[0] = 0;
3513 zone
->recent_scanned
[1] = 0;
3514 zap_zone_vm_stats(zone
);
3519 set_pageblock_order(pageblock_default_order());
3520 setup_usemap(pgdat
, zone
, size
);
3521 ret
= init_currently_empty_zone(zone
, zone_start_pfn
,
3522 size
, MEMMAP_EARLY
);
3524 memmap_init(size
, nid
, j
, zone_start_pfn
);
3525 zone_start_pfn
+= size
;
3529 static void __init_refok
alloc_node_mem_map(struct pglist_data
*pgdat
)
3531 /* Skip empty nodes */
3532 if (!pgdat
->node_spanned_pages
)
3535 #ifdef CONFIG_FLAT_NODE_MEM_MAP
3536 /* ia64 gets its own node_mem_map, before this, without bootmem */
3537 if (!pgdat
->node_mem_map
) {
3538 unsigned long size
, start
, end
;
3542 * The zone's endpoints aren't required to be MAX_ORDER
3543 * aligned but the node_mem_map endpoints must be in order
3544 * for the buddy allocator to function correctly.
3546 start
= pgdat
->node_start_pfn
& ~(MAX_ORDER_NR_PAGES
- 1);
3547 end
= pgdat
->node_start_pfn
+ pgdat
->node_spanned_pages
;
3548 end
= ALIGN(end
, MAX_ORDER_NR_PAGES
);
3549 size
= (end
- start
) * sizeof(struct page
);
3550 map
= alloc_remap(pgdat
->node_id
, size
);
3552 map
= alloc_bootmem_node(pgdat
, size
);
3553 pgdat
->node_mem_map
= map
+ (pgdat
->node_start_pfn
- start
);
3555 #ifndef CONFIG_NEED_MULTIPLE_NODES
3557 * With no DISCONTIG, the global mem_map is just set as node 0's
3559 if (pgdat
== NODE_DATA(0)) {
3560 mem_map
= NODE_DATA(0)->node_mem_map
;
3561 #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
3562 if (page_to_pfn(mem_map
) != pgdat
->node_start_pfn
)
3563 mem_map
-= (pgdat
->node_start_pfn
- ARCH_PFN_OFFSET
);
3564 #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
3567 #endif /* CONFIG_FLAT_NODE_MEM_MAP */
3570 void __paginginit
free_area_init_node(int nid
, unsigned long *zones_size
,
3571 unsigned long node_start_pfn
, unsigned long *zholes_size
)
3573 pg_data_t
*pgdat
= NODE_DATA(nid
);
3575 pgdat
->node_id
= nid
;
3576 pgdat
->node_start_pfn
= node_start_pfn
;
3577 calculate_node_totalpages(pgdat
, zones_size
, zholes_size
);
3579 alloc_node_mem_map(pgdat
);
3580 #ifdef CONFIG_FLAT_NODE_MEM_MAP
3581 printk(KERN_DEBUG
"free_area_init_node: node %d, pgdat %08lx, node_mem_map %08lx\n",
3582 nid
, (unsigned long)pgdat
,
3583 (unsigned long)pgdat
->node_mem_map
);
3586 free_area_init_core(pgdat
, zones_size
, zholes_size
);
3589 #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
3591 #if MAX_NUMNODES > 1
3593 * Figure out the number of possible node ids.
3595 static void __init
setup_nr_node_ids(void)
3598 unsigned int highest
= 0;
3600 for_each_node_mask(node
, node_possible_map
)
3602 nr_node_ids
= highest
+ 1;
3605 static inline void setup_nr_node_ids(void)
3611 * add_active_range - Register a range of PFNs backed by physical memory
3612 * @nid: The node ID the range resides on
3613 * @start_pfn: The start PFN of the available physical memory
3614 * @end_pfn: The end PFN of the available physical memory
3616 * These ranges are stored in an early_node_map[] and later used by
3617 * free_area_init_nodes() to calculate zone sizes and holes. If the
3618 * range spans a memory hole, it is up to the architecture to ensure
3619 * the memory is not freed by the bootmem allocator. If possible
3620 * the range being registered will be merged with existing ranges.
3622 void __init
add_active_range(unsigned int nid
, unsigned long start_pfn
,
3623 unsigned long end_pfn
)
3627 mminit_dprintk(MMINIT_TRACE
, "memory_register",
3628 "Entering add_active_range(%d, %#lx, %#lx) "
3629 "%d entries of %d used\n",
3630 nid
, start_pfn
, end_pfn
,
3631 nr_nodemap_entries
, MAX_ACTIVE_REGIONS
);
3633 mminit_validate_memmodel_limits(&start_pfn
, &end_pfn
);
3635 /* Merge with existing active regions if possible */
3636 for (i
= 0; i
< nr_nodemap_entries
; i
++) {
3637 if (early_node_map
[i
].nid
!= nid
)
3640 /* Skip if an existing region covers this new one */
3641 if (start_pfn
>= early_node_map
[i
].start_pfn
&&
3642 end_pfn
<= early_node_map
[i
].end_pfn
)
3645 /* Merge forward if suitable */
3646 if (start_pfn
<= early_node_map
[i
].end_pfn
&&
3647 end_pfn
> early_node_map
[i
].end_pfn
) {
3648 early_node_map
[i
].end_pfn
= end_pfn
;
3652 /* Merge backward if suitable */
3653 if (start_pfn
< early_node_map
[i
].end_pfn
&&
3654 end_pfn
>= early_node_map
[i
].start_pfn
) {
3655 early_node_map
[i
].start_pfn
= start_pfn
;
3660 /* Check that early_node_map is large enough */
3661 if (i
>= MAX_ACTIVE_REGIONS
) {
3662 printk(KERN_CRIT
"More than %d memory regions, truncating\n",
3663 MAX_ACTIVE_REGIONS
);
3667 early_node_map
[i
].nid
= nid
;
3668 early_node_map
[i
].start_pfn
= start_pfn
;
3669 early_node_map
[i
].end_pfn
= end_pfn
;
3670 nr_nodemap_entries
= i
+ 1;
3674 * remove_active_range - Shrink an existing registered range of PFNs
3675 * @nid: The node id the range is on that should be shrunk
3676 * @start_pfn: The new PFN of the range
3677 * @end_pfn: The new PFN of the range
3679 * i386 with NUMA use alloc_remap() to store a node_mem_map on a local node.
3680 * The map is kept near the end physical page range that has already been
3681 * registered. This function allows an arch to shrink an existing registered
3684 void __init
remove_active_range(unsigned int nid
, unsigned long start_pfn
,
3685 unsigned long end_pfn
)
3690 printk(KERN_DEBUG
"remove_active_range (%d, %lu, %lu)\n",
3691 nid
, start_pfn
, end_pfn
);
3693 /* Find the old active region end and shrink */
3694 for_each_active_range_index_in_nid(i
, nid
) {
3695 if (early_node_map
[i
].start_pfn
>= start_pfn
&&
3696 early_node_map
[i
].end_pfn
<= end_pfn
) {
3698 early_node_map
[i
].start_pfn
= 0;
3699 early_node_map
[i
].end_pfn
= 0;
3703 if (early_node_map
[i
].start_pfn
< start_pfn
&&
3704 early_node_map
[i
].end_pfn
> start_pfn
) {
3705 unsigned long temp_end_pfn
= early_node_map
[i
].end_pfn
;
3706 early_node_map
[i
].end_pfn
= start_pfn
;
3707 if (temp_end_pfn
> end_pfn
)
3708 add_active_range(nid
, end_pfn
, temp_end_pfn
);
3711 if (early_node_map
[i
].start_pfn
>= start_pfn
&&
3712 early_node_map
[i
].end_pfn
> end_pfn
&&
3713 early_node_map
[i
].start_pfn
< end_pfn
) {
3714 early_node_map
[i
].start_pfn
= end_pfn
;
3722 /* remove the blank ones */
3723 for (i
= nr_nodemap_entries
- 1; i
> 0; i
--) {
3724 if (early_node_map
[i
].nid
!= nid
)
3726 if (early_node_map
[i
].end_pfn
)
3728 /* we found it, get rid of it */
3729 for (j
= i
; j
< nr_nodemap_entries
- 1; j
++)
3730 memcpy(&early_node_map
[j
], &early_node_map
[j
+1],
3731 sizeof(early_node_map
[j
]));
3732 j
= nr_nodemap_entries
- 1;
3733 memset(&early_node_map
[j
], 0, sizeof(early_node_map
[j
]));
3734 nr_nodemap_entries
--;
3739 * remove_all_active_ranges - Remove all currently registered regions
3741 * During discovery, it may be found that a table like SRAT is invalid
3742 * and an alternative discovery method must be used. This function removes
3743 * all currently registered regions.
3745 void __init
remove_all_active_ranges(void)
3747 memset(early_node_map
, 0, sizeof(early_node_map
));
3748 nr_nodemap_entries
= 0;
3749 #ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
3750 memset(node_boundary_start_pfn
, 0, sizeof(node_boundary_start_pfn
));
3751 memset(node_boundary_end_pfn
, 0, sizeof(node_boundary_end_pfn
));
3752 #endif /* CONFIG_MEMORY_HOTPLUG_RESERVE */
3755 /* Compare two active node_active_regions */
3756 static int __init
cmp_node_active_region(const void *a
, const void *b
)
3758 struct node_active_region
*arange
= (struct node_active_region
*)a
;
3759 struct node_active_region
*brange
= (struct node_active_region
*)b
;
3761 /* Done this way to avoid overflows */
3762 if (arange
->start_pfn
> brange
->start_pfn
)
3764 if (arange
->start_pfn
< brange
->start_pfn
)
3770 /* sort the node_map by start_pfn */
3771 static void __init
sort_node_map(void)
3773 sort(early_node_map
, (size_t)nr_nodemap_entries
,
3774 sizeof(struct node_active_region
),
3775 cmp_node_active_region
, NULL
);
3778 /* Find the lowest pfn for a node */
3779 static unsigned long __init
find_min_pfn_for_node(int nid
)
3782 unsigned long min_pfn
= ULONG_MAX
;
3784 /* Assuming a sorted map, the first range found has the starting pfn */
3785 for_each_active_range_index_in_nid(i
, nid
)
3786 min_pfn
= min(min_pfn
, early_node_map
[i
].start_pfn
);
3788 if (min_pfn
== ULONG_MAX
) {
3790 "Could not find start_pfn for node %d\n", nid
);
3798 * find_min_pfn_with_active_regions - Find the minimum PFN registered
3800 * It returns the minimum PFN based on information provided via
3801 * add_active_range().
3803 unsigned long __init
find_min_pfn_with_active_regions(void)
3805 return find_min_pfn_for_node(MAX_NUMNODES
);
3809 * early_calculate_totalpages()
3810 * Sum pages in active regions for movable zone.
3811 * Populate N_HIGH_MEMORY for calculating usable_nodes.
3813 static unsigned long __init
early_calculate_totalpages(void)
3816 unsigned long totalpages
= 0;
3818 for (i
= 0; i
< nr_nodemap_entries
; i
++) {
3819 unsigned long pages
= early_node_map
[i
].end_pfn
-
3820 early_node_map
[i
].start_pfn
;
3821 totalpages
+= pages
;
3823 node_set_state(early_node_map
[i
].nid
, N_HIGH_MEMORY
);
3829 * Find the PFN the Movable zone begins in each node. Kernel memory
3830 * is spread evenly between nodes as long as the nodes have enough
3831 * memory. When they don't, some nodes will have more kernelcore than
3834 static void __init
find_zone_movable_pfns_for_nodes(unsigned long *movable_pfn
)
3837 unsigned long usable_startpfn
;
3838 unsigned long kernelcore_node
, kernelcore_remaining
;
3839 unsigned long totalpages
= early_calculate_totalpages();
3840 int usable_nodes
= nodes_weight(node_states
[N_HIGH_MEMORY
]);
3843 * If movablecore was specified, calculate what size of
3844 * kernelcore that corresponds so that memory usable for
3845 * any allocation type is evenly spread. If both kernelcore
3846 * and movablecore are specified, then the value of kernelcore
3847 * will be used for required_kernelcore if it's greater than
3848 * what movablecore would have allowed.
3850 if (required_movablecore
) {
3851 unsigned long corepages
;
3854 * Round-up so that ZONE_MOVABLE is at least as large as what
3855 * was requested by the user
3857 required_movablecore
=
3858 roundup(required_movablecore
, MAX_ORDER_NR_PAGES
);
3859 corepages
= totalpages
- required_movablecore
;
3861 required_kernelcore
= max(required_kernelcore
, corepages
);
3864 /* If kernelcore was not specified, there is no ZONE_MOVABLE */
3865 if (!required_kernelcore
)
3868 /* usable_startpfn is the lowest possible pfn ZONE_MOVABLE can be at */
3869 find_usable_zone_for_movable();
3870 usable_startpfn
= arch_zone_lowest_possible_pfn
[movable_zone
];
3873 /* Spread kernelcore memory as evenly as possible throughout nodes */
3874 kernelcore_node
= required_kernelcore
/ usable_nodes
;
3875 for_each_node_state(nid
, N_HIGH_MEMORY
) {
3877 * Recalculate kernelcore_node if the division per node
3878 * now exceeds what is necessary to satisfy the requested
3879 * amount of memory for the kernel
3881 if (required_kernelcore
< kernelcore_node
)
3882 kernelcore_node
= required_kernelcore
/ usable_nodes
;
3885 * As the map is walked, we track how much memory is usable
3886 * by the kernel using kernelcore_remaining. When it is
3887 * 0, the rest of the node is usable by ZONE_MOVABLE
3889 kernelcore_remaining
= kernelcore_node
;
3891 /* Go through each range of PFNs within this node */
3892 for_each_active_range_index_in_nid(i
, nid
) {
3893 unsigned long start_pfn
, end_pfn
;
3894 unsigned long size_pages
;
3896 start_pfn
= max(early_node_map
[i
].start_pfn
,
3897 zone_movable_pfn
[nid
]);
3898 end_pfn
= early_node_map
[i
].end_pfn
;
3899 if (start_pfn
>= end_pfn
)
3902 /* Account for what is only usable for kernelcore */
3903 if (start_pfn
< usable_startpfn
) {
3904 unsigned long kernel_pages
;
3905 kernel_pages
= min(end_pfn
, usable_startpfn
)
3908 kernelcore_remaining
-= min(kernel_pages
,
3909 kernelcore_remaining
);
3910 required_kernelcore
-= min(kernel_pages
,
3911 required_kernelcore
);
3913 /* Continue if range is now fully accounted */
3914 if (end_pfn
<= usable_startpfn
) {
3917 * Push zone_movable_pfn to the end so
3918 * that if we have to rebalance
3919 * kernelcore across nodes, we will
3920 * not double account here
3922 zone_movable_pfn
[nid
] = end_pfn
;
3925 start_pfn
= usable_startpfn
;
3929 * The usable PFN range for ZONE_MOVABLE is from
3930 * start_pfn->end_pfn. Calculate size_pages as the
3931 * number of pages used as kernelcore
3933 size_pages
= end_pfn
- start_pfn
;
3934 if (size_pages
> kernelcore_remaining
)
3935 size_pages
= kernelcore_remaining
;
3936 zone_movable_pfn
[nid
] = start_pfn
+ size_pages
;
3939 * Some kernelcore has been met, update counts and
3940 * break if the kernelcore for this node has been
3943 required_kernelcore
-= min(required_kernelcore
,
3945 kernelcore_remaining
-= size_pages
;
3946 if (!kernelcore_remaining
)
3952 * If there is still required_kernelcore, we do another pass with one
3953 * less node in the count. This will push zone_movable_pfn[nid] further
3954 * along on the nodes that still have memory until kernelcore is
3958 if (usable_nodes
&& required_kernelcore
> usable_nodes
)
3961 /* Align start of ZONE_MOVABLE on all nids to MAX_ORDER_NR_PAGES */
3962 for (nid
= 0; nid
< MAX_NUMNODES
; nid
++)
3963 zone_movable_pfn
[nid
] =
3964 roundup(zone_movable_pfn
[nid
], MAX_ORDER_NR_PAGES
);
3967 /* Any regular memory on that node ? */
3968 static void check_for_regular_memory(pg_data_t
*pgdat
)
3970 #ifdef CONFIG_HIGHMEM
3971 enum zone_type zone_type
;
3973 for (zone_type
= 0; zone_type
<= ZONE_NORMAL
; zone_type
++) {
3974 struct zone
*zone
= &pgdat
->node_zones
[zone_type
];
3975 if (zone
->present_pages
)
3976 node_set_state(zone_to_nid(zone
), N_NORMAL_MEMORY
);
3982 * free_area_init_nodes - Initialise all pg_data_t and zone data
3983 * @max_zone_pfn: an array of max PFNs for each zone
3985 * This will call free_area_init_node() for each active node in the system.
3986 * Using the page ranges provided by add_active_range(), the size of each
3987 * zone in each node and their holes is calculated. If the maximum PFN
3988 * between two adjacent zones match, it is assumed that the zone is empty.
3989 * For example, if arch_max_dma_pfn == arch_max_dma32_pfn, it is assumed
3990 * that arch_max_dma32_pfn has no pages. It is also assumed that a zone
3991 * starts where the previous one ended. For example, ZONE_DMA32 starts
3992 * at arch_max_dma_pfn.
3994 void __init
free_area_init_nodes(unsigned long *max_zone_pfn
)
3999 /* Sort early_node_map as initialisation assumes it is sorted */
4002 /* Record where the zone boundaries are */
4003 memset(arch_zone_lowest_possible_pfn
, 0,
4004 sizeof(arch_zone_lowest_possible_pfn
));
4005 memset(arch_zone_highest_possible_pfn
, 0,
4006 sizeof(arch_zone_highest_possible_pfn
));
4007 arch_zone_lowest_possible_pfn
[0] = find_min_pfn_with_active_regions();
4008 arch_zone_highest_possible_pfn
[0] = max_zone_pfn
[0];
4009 for (i
= 1; i
< MAX_NR_ZONES
; i
++) {
4010 if (i
== ZONE_MOVABLE
)
4012 arch_zone_lowest_possible_pfn
[i
] =
4013 arch_zone_highest_possible_pfn
[i
-1];
4014 arch_zone_highest_possible_pfn
[i
] =
4015 max(max_zone_pfn
[i
], arch_zone_lowest_possible_pfn
[i
]);
4017 arch_zone_lowest_possible_pfn
[ZONE_MOVABLE
] = 0;
4018 arch_zone_highest_possible_pfn
[ZONE_MOVABLE
] = 0;
4020 /* Find the PFNs that ZONE_MOVABLE begins at in each node */
4021 memset(zone_movable_pfn
, 0, sizeof(zone_movable_pfn
));
4022 find_zone_movable_pfns_for_nodes(zone_movable_pfn
);
4024 /* Print out the zone ranges */
4025 printk("Zone PFN ranges:\n");
4026 for (i
= 0; i
< MAX_NR_ZONES
; i
++) {
4027 if (i
== ZONE_MOVABLE
)
4029 printk(" %-8s %0#10lx -> %0#10lx\n",
4031 arch_zone_lowest_possible_pfn
[i
],
4032 arch_zone_highest_possible_pfn
[i
]);
4035 /* Print out the PFNs ZONE_MOVABLE begins at in each node */
4036 printk("Movable zone start PFN for each node\n");
4037 for (i
= 0; i
< MAX_NUMNODES
; i
++) {
4038 if (zone_movable_pfn
[i
])
4039 printk(" Node %d: %lu\n", i
, zone_movable_pfn
[i
]);
4042 /* Print out the early_node_map[] */
4043 printk("early_node_map[%d] active PFN ranges\n", nr_nodemap_entries
);
4044 for (i
= 0; i
< nr_nodemap_entries
; i
++)
4045 printk(" %3d: %0#10lx -> %0#10lx\n", early_node_map
[i
].nid
,
4046 early_node_map
[i
].start_pfn
,
4047 early_node_map
[i
].end_pfn
);
4049 /* Initialise every node */
4050 mminit_verify_pageflags_layout();
4051 setup_nr_node_ids();
4052 for_each_online_node(nid
) {
4053 pg_data_t
*pgdat
= NODE_DATA(nid
);
4054 free_area_init_node(nid
, NULL
,
4055 find_min_pfn_for_node(nid
), NULL
);
4057 /* Any memory on that node */
4058 if (pgdat
->node_present_pages
)
4059 node_set_state(nid
, N_HIGH_MEMORY
);
4060 check_for_regular_memory(pgdat
);
4064 static int __init
cmdline_parse_core(char *p
, unsigned long *core
)
4066 unsigned long long coremem
;
4070 coremem
= memparse(p
, &p
);
4071 *core
= coremem
>> PAGE_SHIFT
;
4073 /* Paranoid check that UL is enough for the coremem value */
4074 WARN_ON((coremem
>> PAGE_SHIFT
) > ULONG_MAX
);
4080 * kernelcore=size sets the amount of memory for use for allocations that
4081 * cannot be reclaimed or migrated.
4083 static int __init
cmdline_parse_kernelcore(char *p
)
4085 return cmdline_parse_core(p
, &required_kernelcore
);
4089 * movablecore=size sets the amount of memory for use for allocations that
4090 * can be reclaimed or migrated.
4092 static int __init
cmdline_parse_movablecore(char *p
)
4094 return cmdline_parse_core(p
, &required_movablecore
);
4097 early_param("kernelcore", cmdline_parse_kernelcore
);
4098 early_param("movablecore", cmdline_parse_movablecore
);
4100 #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
4103 * set_dma_reserve - set the specified number of pages reserved in the first zone
4104 * @new_dma_reserve: The number of pages to mark reserved
4106 * The per-cpu batchsize and zone watermarks are determined by present_pages.
4107 * In the DMA zone, a significant percentage may be consumed by kernel image
4108 * and other unfreeable allocations which can skew the watermarks badly. This
4109 * function may optionally be used to account for unfreeable pages in the
4110 * first zone (e.g., ZONE_DMA). The effect will be lower watermarks and
4111 * smaller per-cpu batchsize.
4113 void __init
set_dma_reserve(unsigned long new_dma_reserve
)
4115 dma_reserve
= new_dma_reserve
;
4118 #ifndef CONFIG_NEED_MULTIPLE_NODES
4119 struct pglist_data __refdata contig_page_data
= { .bdata
= &bootmem_node_data
[0] };
4120 EXPORT_SYMBOL(contig_page_data
);
4123 void __init
free_area_init(unsigned long *zones_size
)
4125 free_area_init_node(0, zones_size
,
4126 __pa(PAGE_OFFSET
) >> PAGE_SHIFT
, NULL
);
4129 static int page_alloc_cpu_notify(struct notifier_block
*self
,
4130 unsigned long action
, void *hcpu
)
4132 int cpu
= (unsigned long)hcpu
;
4134 if (action
== CPU_DEAD
|| action
== CPU_DEAD_FROZEN
) {
4138 * Spill the event counters of the dead processor
4139 * into the current processors event counters.
4140 * This artificially elevates the count of the current
4143 vm_events_fold_cpu(cpu
);
4146 * Zero the differential counters of the dead processor
4147 * so that the vm statistics are consistent.
4149 * This is only okay since the processor is dead and cannot
4150 * race with what we are doing.
4152 refresh_cpu_vm_stats(cpu
);
4157 void __init
page_alloc_init(void)
4159 hotcpu_notifier(page_alloc_cpu_notify
, 0);
4163 * calculate_totalreserve_pages - called when sysctl_lower_zone_reserve_ratio
4164 * or min_free_kbytes changes.
4166 static void calculate_totalreserve_pages(void)
4168 struct pglist_data
*pgdat
;
4169 unsigned long reserve_pages
= 0;
4170 enum zone_type i
, j
;
4172 for_each_online_pgdat(pgdat
) {
4173 for (i
= 0; i
< MAX_NR_ZONES
; i
++) {
4174 struct zone
*zone
= pgdat
->node_zones
+ i
;
4175 unsigned long max
= 0;
4177 /* Find valid and maximum lowmem_reserve in the zone */
4178 for (j
= i
; j
< MAX_NR_ZONES
; j
++) {
4179 if (zone
->lowmem_reserve
[j
] > max
)
4180 max
= zone
->lowmem_reserve
[j
];
4183 /* we treat pages_high as reserved pages. */
4184 max
+= zone
->pages_high
;
4186 if (max
> zone
->present_pages
)
4187 max
= zone
->present_pages
;
4188 reserve_pages
+= max
;
4191 totalreserve_pages
= reserve_pages
;
4195 * setup_per_zone_lowmem_reserve - called whenever
4196 * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
4197 * has a correct pages reserved value, so an adequate number of
4198 * pages are left in the zone after a successful __alloc_pages().
4200 static void setup_per_zone_lowmem_reserve(void)
4202 struct pglist_data
*pgdat
;
4203 enum zone_type j
, idx
;
4205 for_each_online_pgdat(pgdat
) {
4206 for (j
= 0; j
< MAX_NR_ZONES
; j
++) {
4207 struct zone
*zone
= pgdat
->node_zones
+ j
;
4208 unsigned long present_pages
= zone
->present_pages
;
4210 zone
->lowmem_reserve
[j
] = 0;
4214 struct zone
*lower_zone
;
4218 if (sysctl_lowmem_reserve_ratio
[idx
] < 1)
4219 sysctl_lowmem_reserve_ratio
[idx
] = 1;
4221 lower_zone
= pgdat
->node_zones
+ idx
;
4222 lower_zone
->lowmem_reserve
[j
] = present_pages
/
4223 sysctl_lowmem_reserve_ratio
[idx
];
4224 present_pages
+= lower_zone
->present_pages
;
4229 /* update totalreserve_pages */
4230 calculate_totalreserve_pages();
4234 * setup_per_zone_pages_min - called when min_free_kbytes changes.
4236 * Ensures that the pages_{min,low,high} values for each zone are set correctly
4237 * with respect to min_free_kbytes.
4239 void setup_per_zone_pages_min(void)
4241 unsigned long pages_min
= min_free_kbytes
>> (PAGE_SHIFT
- 10);
4242 unsigned long lowmem_pages
= 0;
4244 unsigned long flags
;
4246 /* Calculate total number of !ZONE_HIGHMEM pages */
4247 for_each_zone(zone
) {
4248 if (!is_highmem(zone
))
4249 lowmem_pages
+= zone
->present_pages
;
4252 for_each_zone(zone
) {
4255 spin_lock_irqsave(&zone
->lock
, flags
);
4256 tmp
= (u64
)pages_min
* zone
->present_pages
;
4257 do_div(tmp
, lowmem_pages
);
4258 if (is_highmem(zone
)) {
4260 * __GFP_HIGH and PF_MEMALLOC allocations usually don't
4261 * need highmem pages, so cap pages_min to a small
4264 * The (pages_high-pages_low) and (pages_low-pages_min)
4265 * deltas controls asynch page reclaim, and so should
4266 * not be capped for highmem.
4270 min_pages
= zone
->present_pages
/ 1024;
4271 if (min_pages
< SWAP_CLUSTER_MAX
)
4272 min_pages
= SWAP_CLUSTER_MAX
;
4273 if (min_pages
> 128)
4275 zone
->pages_min
= min_pages
;
4278 * If it's a lowmem zone, reserve a number of pages
4279 * proportionate to the zone's size.
4281 zone
->pages_min
= tmp
;
4284 zone
->pages_low
= zone
->pages_min
+ (tmp
>> 2);
4285 zone
->pages_high
= zone
->pages_min
+ (tmp
>> 1);
4286 setup_zone_migrate_reserve(zone
);
4287 spin_unlock_irqrestore(&zone
->lock
, flags
);
4290 /* update totalreserve_pages */
4291 calculate_totalreserve_pages();
4295 * setup_per_zone_inactive_ratio - called when min_free_kbytes changes.
4297 * The inactive anon list should be small enough that the VM never has to
4298 * do too much work, but large enough that each inactive page has a chance
4299 * to be referenced again before it is swapped out.
4301 * The inactive_anon ratio is the target ratio of ACTIVE_ANON to
4302 * INACTIVE_ANON pages on this zone's LRU, maintained by the
4303 * pageout code. A zone->inactive_ratio of 3 means 3:1 or 25% of
4304 * the anonymous pages are kept on the inactive list.
4307 * memory ratio inactive anon
4308 * -------------------------------------
4317 static void setup_per_zone_inactive_ratio(void)
4321 for_each_zone(zone
) {
4322 unsigned int gb
, ratio
;
4324 /* Zone size in gigabytes */
4325 gb
= zone
->present_pages
>> (30 - PAGE_SHIFT
);
4326 ratio
= int_sqrt(10 * gb
);
4330 zone
->inactive_ratio
= ratio
;
4335 * Initialise min_free_kbytes.
4337 * For small machines we want it small (128k min). For large machines
4338 * we want it large (64MB max). But it is not linear, because network
4339 * bandwidth does not increase linearly with machine size. We use
4341 * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
4342 * min_free_kbytes = sqrt(lowmem_kbytes * 16)
4358 static int __init
init_per_zone_pages_min(void)
4360 unsigned long lowmem_kbytes
;
4362 lowmem_kbytes
= nr_free_buffer_pages() * (PAGE_SIZE
>> 10);
4364 min_free_kbytes
= int_sqrt(lowmem_kbytes
* 16);
4365 if (min_free_kbytes
< 128)
4366 min_free_kbytes
= 128;
4367 if (min_free_kbytes
> 65536)
4368 min_free_kbytes
= 65536;
4369 setup_per_zone_pages_min();
4370 setup_per_zone_lowmem_reserve();
4371 setup_per_zone_inactive_ratio();
4374 module_init(init_per_zone_pages_min
)
4377 * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
4378 * that we can call two helper functions whenever min_free_kbytes
4381 int min_free_kbytes_sysctl_handler(ctl_table
*table
, int write
,
4382 struct file
*file
, void __user
*buffer
, size_t *length
, loff_t
*ppos
)
4384 proc_dointvec(table
, write
, file
, buffer
, length
, ppos
);
4386 setup_per_zone_pages_min();
4391 int sysctl_min_unmapped_ratio_sysctl_handler(ctl_table
*table
, int write
,
4392 struct file
*file
, void __user
*buffer
, size_t *length
, loff_t
*ppos
)
4397 rc
= proc_dointvec_minmax(table
, write
, file
, buffer
, length
, ppos
);
4402 zone
->min_unmapped_pages
= (zone
->present_pages
*
4403 sysctl_min_unmapped_ratio
) / 100;
4407 int sysctl_min_slab_ratio_sysctl_handler(ctl_table
*table
, int write
,
4408 struct file
*file
, void __user
*buffer
, size_t *length
, loff_t
*ppos
)
4413 rc
= proc_dointvec_minmax(table
, write
, file
, buffer
, length
, ppos
);
4418 zone
->min_slab_pages
= (zone
->present_pages
*
4419 sysctl_min_slab_ratio
) / 100;
4425 * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
4426 * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
4427 * whenever sysctl_lowmem_reserve_ratio changes.
4429 * The reserve ratio obviously has absolutely no relation with the
4430 * pages_min watermarks. The lowmem reserve ratio can only make sense
4431 * if in function of the boot time zone sizes.
4433 int lowmem_reserve_ratio_sysctl_handler(ctl_table
*table
, int write
,
4434 struct file
*file
, void __user
*buffer
, size_t *length
, loff_t
*ppos
)
4436 proc_dointvec_minmax(table
, write
, file
, buffer
, length
, ppos
);
4437 setup_per_zone_lowmem_reserve();
4442 * percpu_pagelist_fraction - changes the pcp->high for each zone on each
4443 * cpu. It is the fraction of total pages in each zone that a hot per cpu pagelist
4444 * can have before it gets flushed back to buddy allocator.
4447 int percpu_pagelist_fraction_sysctl_handler(ctl_table
*table
, int write
,
4448 struct file
*file
, void __user
*buffer
, size_t *length
, loff_t
*ppos
)
4454 ret
= proc_dointvec_minmax(table
, write
, file
, buffer
, length
, ppos
);
4455 if (!write
|| (ret
== -EINVAL
))
4457 for_each_zone(zone
) {
4458 for_each_online_cpu(cpu
) {
4460 high
= zone
->present_pages
/ percpu_pagelist_fraction
;
4461 setup_pagelist_highmark(zone_pcp(zone
, cpu
), high
);
4467 int hashdist
= HASHDIST_DEFAULT
;
4470 static int __init
set_hashdist(char *str
)
4474 hashdist
= simple_strtoul(str
, &str
, 0);
4477 __setup("hashdist=", set_hashdist
);
4481 * allocate a large system hash table from bootmem
4482 * - it is assumed that the hash table must contain an exact power-of-2
4483 * quantity of entries
4484 * - limit is the number of hash buckets, not the total allocation size
4486 void *__init
alloc_large_system_hash(const char *tablename
,
4487 unsigned long bucketsize
,
4488 unsigned long numentries
,
4491 unsigned int *_hash_shift
,
4492 unsigned int *_hash_mask
,
4493 unsigned long limit
)
4495 unsigned long long max
= limit
;
4496 unsigned long log2qty
, size
;
4499 /* allow the kernel cmdline to have a say */
4501 /* round applicable memory size up to nearest megabyte */
4502 numentries
= nr_kernel_pages
;
4503 numentries
+= (1UL << (20 - PAGE_SHIFT
)) - 1;
4504 numentries
>>= 20 - PAGE_SHIFT
;
4505 numentries
<<= 20 - PAGE_SHIFT
;
4507 /* limit to 1 bucket per 2^scale bytes of low memory */
4508 if (scale
> PAGE_SHIFT
)
4509 numentries
>>= (scale
- PAGE_SHIFT
);
4511 numentries
<<= (PAGE_SHIFT
- scale
);
4513 /* Make sure we've got at least a 0-order allocation.. */
4514 if (unlikely((numentries
* bucketsize
) < PAGE_SIZE
))
4515 numentries
= PAGE_SIZE
/ bucketsize
;
4517 numentries
= roundup_pow_of_two(numentries
);
4519 /* limit allocation size to 1/16 total memory by default */
4521 max
= ((unsigned long long)nr_all_pages
<< PAGE_SHIFT
) >> 4;
4522 do_div(max
, bucketsize
);
4525 if (numentries
> max
)
4528 log2qty
= ilog2(numentries
);
4531 size
= bucketsize
<< log2qty
;
4532 if (flags
& HASH_EARLY
)
4533 table
= alloc_bootmem_nopanic(size
);
4535 table
= __vmalloc(size
, GFP_ATOMIC
, PAGE_KERNEL
);
4537 unsigned long order
= get_order(size
);
4538 table
= (void*) __get_free_pages(GFP_ATOMIC
, order
);
4540 * If bucketsize is not a power-of-two, we may free
4541 * some pages at the end of hash table.
4544 unsigned long alloc_end
= (unsigned long)table
+
4545 (PAGE_SIZE
<< order
);
4546 unsigned long used
= (unsigned long)table
+
4548 split_page(virt_to_page(table
), order
);
4549 while (used
< alloc_end
) {
4555 } while (!table
&& size
> PAGE_SIZE
&& --log2qty
);
4558 panic("Failed to allocate %s hash table\n", tablename
);
4560 printk(KERN_INFO
"%s hash table entries: %d (order: %d, %lu bytes)\n",
4563 ilog2(size
) - PAGE_SHIFT
,
4567 *_hash_shift
= log2qty
;
4569 *_hash_mask
= (1 << log2qty
) - 1;
4574 #ifdef CONFIG_OUT_OF_LINE_PFN_TO_PAGE
4575 struct page
*pfn_to_page(unsigned long pfn
)
4577 return __pfn_to_page(pfn
);
4579 unsigned long page_to_pfn(struct page
*page
)
4581 return __page_to_pfn(page
);
4583 EXPORT_SYMBOL(pfn_to_page
);
4584 EXPORT_SYMBOL(page_to_pfn
);
4585 #endif /* CONFIG_OUT_OF_LINE_PFN_TO_PAGE */
4587 /* Return a pointer to the bitmap storing bits affecting a block of pages */
4588 static inline unsigned long *get_pageblock_bitmap(struct zone
*zone
,
4591 #ifdef CONFIG_SPARSEMEM
4592 return __pfn_to_section(pfn
)->pageblock_flags
;
4594 return zone
->pageblock_flags
;
4595 #endif /* CONFIG_SPARSEMEM */
4598 static inline int pfn_to_bitidx(struct zone
*zone
, unsigned long pfn
)
4600 #ifdef CONFIG_SPARSEMEM
4601 pfn
&= (PAGES_PER_SECTION
-1);
4602 return (pfn
>> pageblock_order
) * NR_PAGEBLOCK_BITS
;
4604 pfn
= pfn
- zone
->zone_start_pfn
;
4605 return (pfn
>> pageblock_order
) * NR_PAGEBLOCK_BITS
;
4606 #endif /* CONFIG_SPARSEMEM */
4610 * get_pageblock_flags_group - Return the requested group of flags for the pageblock_nr_pages block of pages
4611 * @page: The page within the block of interest
4612 * @start_bitidx: The first bit of interest to retrieve
4613 * @end_bitidx: The last bit of interest
4614 * returns pageblock_bits flags
4616 unsigned long get_pageblock_flags_group(struct page
*page
,
4617 int start_bitidx
, int end_bitidx
)
4620 unsigned long *bitmap
;
4621 unsigned long pfn
, bitidx
;
4622 unsigned long flags
= 0;
4623 unsigned long value
= 1;
4625 zone
= page_zone(page
);
4626 pfn
= page_to_pfn(page
);
4627 bitmap
= get_pageblock_bitmap(zone
, pfn
);
4628 bitidx
= pfn_to_bitidx(zone
, pfn
);
4630 for (; start_bitidx
<= end_bitidx
; start_bitidx
++, value
<<= 1)
4631 if (test_bit(bitidx
+ start_bitidx
, bitmap
))
4638 * set_pageblock_flags_group - Set the requested group of flags for a pageblock_nr_pages block of pages
4639 * @page: The page within the block of interest
4640 * @start_bitidx: The first bit of interest
4641 * @end_bitidx: The last bit of interest
4642 * @flags: The flags to set
4644 void set_pageblock_flags_group(struct page
*page
, unsigned long flags
,
4645 int start_bitidx
, int end_bitidx
)
4648 unsigned long *bitmap
;
4649 unsigned long pfn
, bitidx
;
4650 unsigned long value
= 1;
4652 zone
= page_zone(page
);
4653 pfn
= page_to_pfn(page
);
4654 bitmap
= get_pageblock_bitmap(zone
, pfn
);
4655 bitidx
= pfn_to_bitidx(zone
, pfn
);
4656 VM_BUG_ON(pfn
< zone
->zone_start_pfn
);
4657 VM_BUG_ON(pfn
>= zone
->zone_start_pfn
+ zone
->spanned_pages
);
4659 for (; start_bitidx
<= end_bitidx
; start_bitidx
++, value
<<= 1)
4661 __set_bit(bitidx
+ start_bitidx
, bitmap
);
4663 __clear_bit(bitidx
+ start_bitidx
, bitmap
);
4667 * This is designed as sub function...plz see page_isolation.c also.
4668 * set/clear page block's type to be ISOLATE.
4669 * page allocater never alloc memory from ISOLATE block.
4672 int set_migratetype_isolate(struct page
*page
)
4675 unsigned long flags
;
4678 zone
= page_zone(page
);
4679 spin_lock_irqsave(&zone
->lock
, flags
);
4681 * In future, more migrate types will be able to be isolation target.
4683 if (get_pageblock_migratetype(page
) != MIGRATE_MOVABLE
)
4685 set_pageblock_migratetype(page
, MIGRATE_ISOLATE
);
4686 move_freepages_block(zone
, page
, MIGRATE_ISOLATE
);
4689 spin_unlock_irqrestore(&zone
->lock
, flags
);
4695 void unset_migratetype_isolate(struct page
*page
)
4698 unsigned long flags
;
4699 zone
= page_zone(page
);
4700 spin_lock_irqsave(&zone
->lock
, flags
);
4701 if (get_pageblock_migratetype(page
) != MIGRATE_ISOLATE
)
4703 set_pageblock_migratetype(page
, MIGRATE_MOVABLE
);
4704 move_freepages_block(zone
, page
, MIGRATE_MOVABLE
);
4706 spin_unlock_irqrestore(&zone
->lock
, flags
);
4709 #ifdef CONFIG_MEMORY_HOTREMOVE
4711 * All pages in the range must be isolated before calling this.
4714 __offline_isolated_pages(unsigned long start_pfn
, unsigned long end_pfn
)
4720 unsigned long flags
;
4721 /* find the first valid pfn */
4722 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
++)
4727 zone
= page_zone(pfn_to_page(pfn
));
4728 spin_lock_irqsave(&zone
->lock
, flags
);
4730 while (pfn
< end_pfn
) {
4731 if (!pfn_valid(pfn
)) {
4735 page
= pfn_to_page(pfn
);
4736 BUG_ON(page_count(page
));
4737 BUG_ON(!PageBuddy(page
));
4738 order
= page_order(page
);
4739 #ifdef CONFIG_DEBUG_VM
4740 printk(KERN_INFO
"remove from free list %lx %d %lx\n",
4741 pfn
, 1 << order
, end_pfn
);
4743 list_del(&page
->lru
);
4744 rmv_page_order(page
);
4745 zone
->free_area
[order
].nr_free
--;
4746 __mod_zone_page_state(zone
, NR_FREE_PAGES
,
4748 for (i
= 0; i
< (1 << order
); i
++)
4749 SetPageReserved((page
+i
));
4750 pfn
+= (1 << order
);
4752 spin_unlock_irqrestore(&zone
->lock
, flags
);