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/config.h>
18 #include <linux/stddef.h>
20 #include <linux/swap.h>
21 #include <linux/interrupt.h>
22 #include <linux/pagemap.h>
23 #include <linux/bootmem.h>
24 #include <linux/compiler.h>
25 #include <linux/module.h>
26 #include <linux/suspend.h>
27 #include <linux/pagevec.h>
28 #include <linux/blkdev.h>
29 #include <linux/slab.h>
30 #include <linux/notifier.h>
31 #include <linux/topology.h>
32 #include <linux/sysctl.h>
33 #include <linux/cpu.h>
34 #include <linux/cpuset.h>
35 #include <linux/nodemask.h>
36 #include <linux/vmalloc.h>
38 #include <asm/tlbflush.h>
42 * MCD - HACK: Find somewhere to initialize this EARLY, or make this
45 nodemask_t node_online_map
= { { [0] = 1UL } };
46 EXPORT_SYMBOL(node_online_map
);
47 nodemask_t node_possible_map
= NODE_MASK_ALL
;
48 EXPORT_SYMBOL(node_possible_map
);
49 struct pglist_data
*pgdat_list
;
50 unsigned long totalram_pages
;
51 unsigned long totalhigh_pages
;
55 * results with 256, 32 in the lowmem_reserve sysctl:
56 * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
57 * 1G machine -> (16M dma, 784M normal, 224M high)
58 * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
59 * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
60 * HIGHMEM allocation will (224M+784M)/256 of ram reserved in ZONE_DMA
62 int sysctl_lowmem_reserve_ratio
[MAX_NR_ZONES
-1] = { 256, 32 };
64 EXPORT_SYMBOL(totalram_pages
);
65 EXPORT_SYMBOL(nr_swap_pages
);
68 * Used by page_zone() to look up the address of the struct zone whose
69 * id is encoded in the upper bits of page->flags
71 struct zone
*zone_table
[1 << ZONETABLE_SHIFT
];
72 EXPORT_SYMBOL(zone_table
);
74 static char *zone_names
[MAX_NR_ZONES
] = { "DMA", "Normal", "HighMem" };
75 int min_free_kbytes
= 1024;
77 unsigned long __initdata nr_kernel_pages
;
78 unsigned long __initdata nr_all_pages
;
81 * Temporary debugging check for pages not lying within a given zone.
83 static int bad_range(struct zone
*zone
, struct page
*page
)
85 if (page_to_pfn(page
) >= zone
->zone_start_pfn
+ zone
->spanned_pages
)
87 if (page_to_pfn(page
) < zone
->zone_start_pfn
)
89 #ifdef CONFIG_HOLES_IN_ZONE
90 if (!pfn_valid(page_to_pfn(page
)))
93 if (zone
!= page_zone(page
))
98 static void bad_page(const char *function
, struct page
*page
)
100 printk(KERN_EMERG
"Bad page state at %s (in process '%s', page %p)\n",
101 function
, current
->comm
, page
);
102 printk(KERN_EMERG
"flags:0x%0*lx mapping:%p mapcount:%d count:%d\n",
103 (int)(2*sizeof(page_flags_t
)), (unsigned long)page
->flags
,
104 page
->mapping
, page_mapcount(page
), page_count(page
));
105 printk(KERN_EMERG
"Backtrace:\n");
107 printk(KERN_EMERG
"Trying to fix it up, but a reboot is needed\n");
108 page
->flags
&= ~(1 << PG_lru
|
117 set_page_count(page
, 0);
118 reset_page_mapcount(page
);
119 page
->mapping
= NULL
;
120 tainted
|= TAINT_BAD_PAGE
;
123 #ifndef CONFIG_HUGETLB_PAGE
124 #define prep_compound_page(page, order) do { } while (0)
125 #define destroy_compound_page(page, order) do { } while (0)
128 * Higher-order pages are called "compound pages". They are structured thusly:
130 * The first PAGE_SIZE page is called the "head page".
132 * The remaining PAGE_SIZE pages are called "tail pages".
134 * All pages have PG_compound set. All pages have their ->private pointing at
135 * the head page (even the head page has this).
137 * The first tail page's ->mapping, if non-zero, holds the address of the
138 * compound page's put_page() function.
140 * The order of the allocation is stored in the first tail page's ->index
141 * This is only for debug at present. This usage means that zero-order pages
142 * may not be compound.
144 static void prep_compound_page(struct page
*page
, unsigned long order
)
147 int nr_pages
= 1 << order
;
149 page
[1].mapping
= NULL
;
150 page
[1].index
= order
;
151 for (i
= 0; i
< nr_pages
; i
++) {
152 struct page
*p
= page
+ i
;
155 p
->private = (unsigned long)page
;
159 static void destroy_compound_page(struct page
*page
, unsigned long order
)
162 int nr_pages
= 1 << order
;
164 if (!PageCompound(page
))
167 if (page
[1].index
!= order
)
168 bad_page(__FUNCTION__
, page
);
170 for (i
= 0; i
< nr_pages
; i
++) {
171 struct page
*p
= page
+ i
;
173 if (!PageCompound(p
))
174 bad_page(__FUNCTION__
, page
);
175 if (p
->private != (unsigned long)page
)
176 bad_page(__FUNCTION__
, page
);
177 ClearPageCompound(p
);
180 #endif /* CONFIG_HUGETLB_PAGE */
183 * function for dealing with page's order in buddy system.
184 * zone->lock is already acquired when we use these.
185 * So, we don't need atomic page->flags operations here.
187 static inline unsigned long page_order(struct page
*page
) {
188 return page
->private;
191 static inline void set_page_order(struct page
*page
, int order
) {
192 page
->private = order
;
193 __SetPagePrivate(page
);
196 static inline void rmv_page_order(struct page
*page
)
198 __ClearPagePrivate(page
);
203 * Locate the struct page for both the matching buddy in our
204 * pair (buddy1) and the combined O(n+1) page they form (page).
206 * 1) Any buddy B1 will have an order O twin B2 which satisfies
207 * the following equation:
209 * For example, if the starting buddy (buddy2) is #8 its order
211 * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
213 * 2) Any buddy B will have an order O+1 parent P which
214 * satisfies the following equation:
217 * Assumption: *_mem_map is contigious at least up to MAX_ORDER
219 static inline struct page
*
220 __page_find_buddy(struct page
*page
, unsigned long page_idx
, unsigned int order
)
222 unsigned long buddy_idx
= page_idx
^ (1 << order
);
224 return page
+ (buddy_idx
- page_idx
);
227 static inline unsigned long
228 __find_combined_index(unsigned long page_idx
, unsigned int order
)
230 return (page_idx
& ~(1 << order
));
234 * This function checks whether a page is free && is the buddy
235 * we can do coalesce a page and its buddy if
236 * (a) the buddy is free &&
237 * (b) the buddy is on the buddy system &&
238 * (c) a page and its buddy have the same order.
239 * for recording page's order, we use page->private and PG_private.
242 static inline int page_is_buddy(struct page
*page
, int order
)
244 if (PagePrivate(page
) &&
245 (page_order(page
) == order
) &&
246 !PageReserved(page
) &&
247 page_count(page
) == 0)
253 * Freeing function for a buddy system allocator.
255 * The concept of a buddy system is to maintain direct-mapped table
256 * (containing bit values) for memory blocks of various "orders".
257 * The bottom level table contains the map for the smallest allocatable
258 * units of memory (here, pages), and each level above it describes
259 * pairs of units from the levels below, hence, "buddies".
260 * At a high level, all that happens here is marking the table entry
261 * at the bottom level available, and propagating the changes upward
262 * as necessary, plus some accounting needed to play nicely with other
263 * parts of the VM system.
264 * At each level, we keep a list of pages, which are heads of continuous
265 * free pages of length of (1 << order) and marked with PG_Private.Page's
266 * order is recorded in page->private field.
267 * So when we are allocating or freeing one, we can derive the state of the
268 * other. That is, if we allocate a small block, and both were
269 * free, the remainder of the region must be split into blocks.
270 * If a block is freed, and its buddy is also free, then this
271 * triggers coalescing into a block of larger size.
276 static inline void __free_pages_bulk (struct page
*page
,
277 struct zone
*zone
, unsigned int order
)
279 unsigned long page_idx
;
280 int order_size
= 1 << order
;
283 destroy_compound_page(page
, order
);
285 page_idx
= page_to_pfn(page
) & ((1 << MAX_ORDER
) - 1);
287 BUG_ON(page_idx
& (order_size
- 1));
288 BUG_ON(bad_range(zone
, page
));
290 zone
->free_pages
+= order_size
;
291 while (order
< MAX_ORDER
-1) {
292 unsigned long combined_idx
;
293 struct free_area
*area
;
296 combined_idx
= __find_combined_index(page_idx
, order
);
297 buddy
= __page_find_buddy(page
, page_idx
, order
);
299 if (bad_range(zone
, buddy
))
301 if (!page_is_buddy(buddy
, order
))
302 break; /* Move the buddy up one level. */
303 list_del(&buddy
->lru
);
304 area
= zone
->free_area
+ order
;
306 rmv_page_order(buddy
);
307 page
= page
+ (combined_idx
- page_idx
);
308 page_idx
= combined_idx
;
311 set_page_order(page
, order
);
312 list_add(&page
->lru
, &zone
->free_area
[order
].free_list
);
313 zone
->free_area
[order
].nr_free
++;
316 static inline void free_pages_check(const char *function
, struct page
*page
)
318 if ( page_mapcount(page
) ||
319 page
->mapping
!= NULL
||
320 page_count(page
) != 0 ||
329 1 << PG_writeback
)))
330 bad_page(function
, page
);
332 ClearPageDirty(page
);
336 * Frees a list of pages.
337 * Assumes all pages on list are in same zone, and of same order.
338 * count is the number of pages to free, or 0 for all on the list.
340 * If the zone was previously in an "all pages pinned" state then look to
341 * see if this freeing clears that state.
343 * And clear the zone's pages_scanned counter, to hold off the "all pages are
344 * pinned" detection logic.
347 free_pages_bulk(struct zone
*zone
, int count
,
348 struct list_head
*list
, unsigned int order
)
351 struct page
*page
= NULL
;
354 spin_lock_irqsave(&zone
->lock
, flags
);
355 zone
->all_unreclaimable
= 0;
356 zone
->pages_scanned
= 0;
357 while (!list_empty(list
) && count
--) {
358 page
= list_entry(list
->prev
, struct page
, lru
);
359 /* have to delete it as __free_pages_bulk list manipulates */
360 list_del(&page
->lru
);
361 __free_pages_bulk(page
, zone
, order
);
364 spin_unlock_irqrestore(&zone
->lock
, flags
);
368 void __free_pages_ok(struct page
*page
, unsigned int order
)
373 arch_free_page(page
, order
);
375 mod_page_state(pgfree
, 1 << order
);
379 for (i
= 1 ; i
< (1 << order
) ; ++i
)
380 __put_page(page
+ i
);
383 for (i
= 0 ; i
< (1 << order
) ; ++i
)
384 free_pages_check(__FUNCTION__
, page
+ i
);
385 list_add(&page
->lru
, &list
);
386 kernel_map_pages(page
, 1<<order
, 0);
387 free_pages_bulk(page_zone(page
), 1, &list
, order
);
392 * The order of subdivision here is critical for the IO subsystem.
393 * Please do not alter this order without good reasons and regression
394 * testing. Specifically, as large blocks of memory are subdivided,
395 * the order in which smaller blocks are delivered depends on the order
396 * they're subdivided in this function. This is the primary factor
397 * influencing the order in which pages are delivered to the IO
398 * subsystem according to empirical testing, and this is also justified
399 * by considering the behavior of a buddy system containing a single
400 * large block of memory acted on by a series of small allocations.
401 * This behavior is a critical factor in sglist merging's success.
405 static inline struct page
*
406 expand(struct zone
*zone
, struct page
*page
,
407 int low
, int high
, struct free_area
*area
)
409 unsigned long size
= 1 << high
;
415 BUG_ON(bad_range(zone
, &page
[size
]));
416 list_add(&page
[size
].lru
, &area
->free_list
);
418 set_page_order(&page
[size
], high
);
423 void set_page_refs(struct page
*page
, int order
)
426 set_page_count(page
, 1);
431 * We need to reference all the pages for this order, otherwise if
432 * anyone accesses one of the pages with (get/put) it will be freed.
433 * - eg: access_process_vm()
435 for (i
= 0; i
< (1 << order
); i
++)
436 set_page_count(page
+ i
, 1);
437 #endif /* CONFIG_MMU */
441 * This page is about to be returned from the page allocator
443 static void prep_new_page(struct page
*page
, int order
)
445 if ( page_mapcount(page
) ||
446 page
->mapping
!= NULL
||
447 page_count(page
) != 0 ||
457 1 << PG_writeback
)))
458 bad_page(__FUNCTION__
, page
);
460 page
->flags
&= ~(1 << PG_uptodate
| 1 << PG_error
|
461 1 << PG_referenced
| 1 << PG_arch_1
|
462 1 << PG_checked
| 1 << PG_mappedtodisk
);
464 set_page_refs(page
, order
);
465 kernel_map_pages(page
, 1 << order
, 1);
469 * Do the hard work of removing an element from the buddy allocator.
470 * Call me with the zone->lock already held.
472 static struct page
*__rmqueue(struct zone
*zone
, unsigned int order
)
474 struct free_area
* area
;
475 unsigned int current_order
;
478 for (current_order
= order
; current_order
< MAX_ORDER
; ++current_order
) {
479 area
= zone
->free_area
+ current_order
;
480 if (list_empty(&area
->free_list
))
483 page
= list_entry(area
->free_list
.next
, struct page
, lru
);
484 list_del(&page
->lru
);
485 rmv_page_order(page
);
487 zone
->free_pages
-= 1UL << order
;
488 return expand(zone
, page
, order
, current_order
, area
);
495 * Obtain a specified number of elements from the buddy allocator, all under
496 * a single hold of the lock, for efficiency. Add them to the supplied list.
497 * Returns the number of new pages which were placed at *list.
499 static int rmqueue_bulk(struct zone
*zone
, unsigned int order
,
500 unsigned long count
, struct list_head
*list
)
507 spin_lock_irqsave(&zone
->lock
, flags
);
508 for (i
= 0; i
< count
; ++i
) {
509 page
= __rmqueue(zone
, order
);
513 list_add_tail(&page
->lru
, list
);
515 spin_unlock_irqrestore(&zone
->lock
, flags
);
520 /* Called from the slab reaper to drain remote pagesets */
521 void drain_remote_pages(void)
527 local_irq_save(flags
);
528 for_each_zone(zone
) {
529 struct per_cpu_pageset
*pset
;
531 /* Do not drain local pagesets */
532 if (zone
->zone_pgdat
->node_id
== numa_node_id())
535 pset
= zone
->pageset
[smp_processor_id()];
536 for (i
= 0; i
< ARRAY_SIZE(pset
->pcp
); i
++) {
537 struct per_cpu_pages
*pcp
;
541 pcp
->count
-= free_pages_bulk(zone
, pcp
->count
,
545 local_irq_restore(flags
);
549 #if defined(CONFIG_PM) || defined(CONFIG_HOTPLUG_CPU)
550 static void __drain_pages(unsigned int cpu
)
555 for_each_zone(zone
) {
556 struct per_cpu_pageset
*pset
;
558 pset
= zone_pcp(zone
, cpu
);
559 for (i
= 0; i
< ARRAY_SIZE(pset
->pcp
); i
++) {
560 struct per_cpu_pages
*pcp
;
563 pcp
->count
-= free_pages_bulk(zone
, pcp
->count
,
568 #endif /* CONFIG_PM || CONFIG_HOTPLUG_CPU */
572 void mark_free_pages(struct zone
*zone
)
574 unsigned long zone_pfn
, flags
;
576 struct list_head
*curr
;
578 if (!zone
->spanned_pages
)
581 spin_lock_irqsave(&zone
->lock
, flags
);
582 for (zone_pfn
= 0; zone_pfn
< zone
->spanned_pages
; ++zone_pfn
)
583 ClearPageNosaveFree(pfn_to_page(zone_pfn
+ zone
->zone_start_pfn
));
585 for (order
= MAX_ORDER
- 1; order
>= 0; --order
)
586 list_for_each(curr
, &zone
->free_area
[order
].free_list
) {
587 unsigned long start_pfn
, i
;
589 start_pfn
= page_to_pfn(list_entry(curr
, struct page
, lru
));
591 for (i
=0; i
< (1<<order
); i
++)
592 SetPageNosaveFree(pfn_to_page(start_pfn
+i
));
594 spin_unlock_irqrestore(&zone
->lock
, flags
);
598 * Spill all of this CPU's per-cpu pages back into the buddy allocator.
600 void drain_local_pages(void)
604 local_irq_save(flags
);
605 __drain_pages(smp_processor_id());
606 local_irq_restore(flags
);
608 #endif /* CONFIG_PM */
610 static void zone_statistics(struct zonelist
*zonelist
, struct zone
*z
)
615 pg_data_t
*pg
= z
->zone_pgdat
;
616 pg_data_t
*orig
= zonelist
->zones
[0]->zone_pgdat
;
617 struct per_cpu_pageset
*p
;
619 local_irq_save(flags
);
620 cpu
= smp_processor_id();
626 zone_pcp(zonelist
->zones
[0], cpu
)->numa_foreign
++;
628 if (pg
== NODE_DATA(numa_node_id()))
632 local_irq_restore(flags
);
637 * Free a 0-order page
639 static void FASTCALL(free_hot_cold_page(struct page
*page
, int cold
));
640 static void fastcall
free_hot_cold_page(struct page
*page
, int cold
)
642 struct zone
*zone
= page_zone(page
);
643 struct per_cpu_pages
*pcp
;
646 arch_free_page(page
, 0);
648 kernel_map_pages(page
, 1, 0);
649 inc_page_state(pgfree
);
651 page
->mapping
= NULL
;
652 free_pages_check(__FUNCTION__
, page
);
653 pcp
= &zone_pcp(zone
, get_cpu())->pcp
[cold
];
654 local_irq_save(flags
);
655 list_add(&page
->lru
, &pcp
->list
);
657 if (pcp
->count
>= pcp
->high
)
658 pcp
->count
-= free_pages_bulk(zone
, pcp
->batch
, &pcp
->list
, 0);
659 local_irq_restore(flags
);
663 void fastcall
free_hot_page(struct page
*page
)
665 free_hot_cold_page(page
, 0);
668 void fastcall
free_cold_page(struct page
*page
)
670 free_hot_cold_page(page
, 1);
673 static inline void prep_zero_page(struct page
*page
, int order
, unsigned int __nocast gfp_flags
)
677 BUG_ON((gfp_flags
& (__GFP_WAIT
| __GFP_HIGHMEM
)) == __GFP_HIGHMEM
);
678 for(i
= 0; i
< (1 << order
); i
++)
679 clear_highpage(page
+ i
);
683 * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
684 * we cheat by calling it from here, in the order > 0 path. Saves a branch
688 buffered_rmqueue(struct zone
*zone
, int order
, unsigned int __nocast gfp_flags
)
691 struct page
*page
= NULL
;
692 int cold
= !!(gfp_flags
& __GFP_COLD
);
695 struct per_cpu_pages
*pcp
;
697 pcp
= &zone_pcp(zone
, get_cpu())->pcp
[cold
];
698 local_irq_save(flags
);
699 if (pcp
->count
<= pcp
->low
)
700 pcp
->count
+= rmqueue_bulk(zone
, 0,
701 pcp
->batch
, &pcp
->list
);
703 page
= list_entry(pcp
->list
.next
, struct page
, lru
);
704 list_del(&page
->lru
);
707 local_irq_restore(flags
);
712 spin_lock_irqsave(&zone
->lock
, flags
);
713 page
= __rmqueue(zone
, order
);
714 spin_unlock_irqrestore(&zone
->lock
, flags
);
718 BUG_ON(bad_range(zone
, page
));
719 mod_page_state_zone(zone
, pgalloc
, 1 << order
);
720 prep_new_page(page
, order
);
722 if (gfp_flags
& __GFP_ZERO
)
723 prep_zero_page(page
, order
, gfp_flags
);
725 if (order
&& (gfp_flags
& __GFP_COMP
))
726 prep_compound_page(page
, order
);
732 * Return 1 if free pages are above 'mark'. This takes into account the order
735 int zone_watermark_ok(struct zone
*z
, int order
, unsigned long mark
,
736 int classzone_idx
, int can_try_harder
, int gfp_high
)
738 /* free_pages my go negative - that's OK */
739 long min
= mark
, free_pages
= z
->free_pages
- (1 << order
) + 1;
747 if (free_pages
<= min
+ z
->lowmem_reserve
[classzone_idx
])
749 for (o
= 0; o
< order
; o
++) {
750 /* At the next order, this order's pages become unavailable */
751 free_pages
-= z
->free_area
[o
].nr_free
<< o
;
753 /* Require fewer higher order pages to be free */
756 if (free_pages
<= min
)
763 should_reclaim_zone(struct zone
*z
, unsigned int gfp_mask
)
765 if (!z
->reclaim_pages
)
767 if (gfp_mask
& __GFP_NORECLAIM
)
773 * This is the 'heart' of the zoned buddy allocator.
775 struct page
* fastcall
776 __alloc_pages(unsigned int __nocast gfp_mask
, unsigned int order
,
777 struct zonelist
*zonelist
)
779 const int wait
= gfp_mask
& __GFP_WAIT
;
780 struct zone
**zones
, *z
;
782 struct reclaim_state reclaim_state
;
783 struct task_struct
*p
= current
;
788 int did_some_progress
;
790 might_sleep_if(wait
);
793 * The caller may dip into page reserves a bit more if the caller
794 * cannot run direct reclaim, or is the caller has realtime scheduling
797 can_try_harder
= (unlikely(rt_task(p
)) && !in_interrupt()) || !wait
;
799 zones
= zonelist
->zones
; /* the list of zones suitable for gfp_mask */
801 if (unlikely(zones
[0] == NULL
)) {
802 /* Should this ever happen?? */
806 classzone_idx
= zone_idx(zones
[0]);
809 /* Go through the zonelist once, looking for a zone with enough free */
810 for (i
= 0; (z
= zones
[i
]) != NULL
; i
++) {
811 int do_reclaim
= should_reclaim_zone(z
, gfp_mask
);
813 if (!cpuset_zone_allowed(z
))
817 * If the zone is to attempt early page reclaim then this loop
818 * will try to reclaim pages and check the watermark a second
819 * time before giving up and falling back to the next zone.
822 if (!zone_watermark_ok(z
, order
, z
->pages_low
,
823 classzone_idx
, 0, 0)) {
827 zone_reclaim(z
, gfp_mask
, order
);
828 /* Only try reclaim once */
830 goto zone_reclaim_retry
;
834 page
= buffered_rmqueue(z
, order
, gfp_mask
);
839 for (i
= 0; (z
= zones
[i
]) != NULL
; i
++)
840 wakeup_kswapd(z
, order
);
843 * Go through the zonelist again. Let __GFP_HIGH and allocations
844 * coming from realtime tasks to go deeper into reserves
846 * This is the last chance, in general, before the goto nopage.
847 * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
849 for (i
= 0; (z
= zones
[i
]) != NULL
; i
++) {
850 if (!zone_watermark_ok(z
, order
, z
->pages_min
,
851 classzone_idx
, can_try_harder
,
852 gfp_mask
& __GFP_HIGH
))
855 if (wait
&& !cpuset_zone_allowed(z
))
858 page
= buffered_rmqueue(z
, order
, gfp_mask
);
863 /* This allocation should allow future memory freeing. */
865 if (((p
->flags
& PF_MEMALLOC
) || unlikely(test_thread_flag(TIF_MEMDIE
)))
866 && !in_interrupt()) {
867 if (!(gfp_mask
& __GFP_NOMEMALLOC
)) {
868 /* go through the zonelist yet again, ignoring mins */
869 for (i
= 0; (z
= zones
[i
]) != NULL
; i
++) {
870 if (!cpuset_zone_allowed(z
))
872 page
= buffered_rmqueue(z
, order
, gfp_mask
);
880 /* Atomic allocations - we can't balance anything */
887 /* We now go into synchronous reclaim */
888 p
->flags
|= PF_MEMALLOC
;
889 reclaim_state
.reclaimed_slab
= 0;
890 p
->reclaim_state
= &reclaim_state
;
892 did_some_progress
= try_to_free_pages(zones
, gfp_mask
);
894 p
->reclaim_state
= NULL
;
895 p
->flags
&= ~PF_MEMALLOC
;
899 if (likely(did_some_progress
)) {
900 for (i
= 0; (z
= zones
[i
]) != NULL
; i
++) {
901 if (!zone_watermark_ok(z
, order
, z
->pages_min
,
902 classzone_idx
, can_try_harder
,
903 gfp_mask
& __GFP_HIGH
))
906 if (!cpuset_zone_allowed(z
))
909 page
= buffered_rmqueue(z
, order
, gfp_mask
);
913 } else if ((gfp_mask
& __GFP_FS
) && !(gfp_mask
& __GFP_NORETRY
)) {
915 * Go through the zonelist yet one more time, keep
916 * very high watermark here, this is only to catch
917 * a parallel oom killing, we must fail if we're still
918 * under heavy pressure.
920 for (i
= 0; (z
= zones
[i
]) != NULL
; i
++) {
921 if (!zone_watermark_ok(z
, order
, z
->pages_high
,
922 classzone_idx
, 0, 0))
925 if (!cpuset_zone_allowed(z
))
928 page
= buffered_rmqueue(z
, order
, gfp_mask
);
933 out_of_memory(gfp_mask
, order
);
938 * Don't let big-order allocations loop unless the caller explicitly
939 * requests that. Wait for some write requests to complete then retry.
941 * In this implementation, __GFP_REPEAT means __GFP_NOFAIL for order
942 * <= 3, but that may not be true in other implementations.
945 if (!(gfp_mask
& __GFP_NORETRY
)) {
946 if ((order
<= 3) || (gfp_mask
& __GFP_REPEAT
))
948 if (gfp_mask
& __GFP_NOFAIL
)
952 blk_congestion_wait(WRITE
, HZ
/50);
957 if (!(gfp_mask
& __GFP_NOWARN
) && printk_ratelimit()) {
958 printk(KERN_WARNING
"%s: page allocation failure."
959 " order:%d, mode:0x%x\n",
960 p
->comm
, order
, gfp_mask
);
966 zone_statistics(zonelist
, z
);
970 EXPORT_SYMBOL(__alloc_pages
);
973 * Common helper functions.
975 fastcall
unsigned long __get_free_pages(unsigned int __nocast gfp_mask
, unsigned int order
)
978 page
= alloc_pages(gfp_mask
, order
);
981 return (unsigned long) page_address(page
);
984 EXPORT_SYMBOL(__get_free_pages
);
986 fastcall
unsigned long get_zeroed_page(unsigned int __nocast gfp_mask
)
991 * get_zeroed_page() returns a 32-bit address, which cannot represent
994 BUG_ON(gfp_mask
& __GFP_HIGHMEM
);
996 page
= alloc_pages(gfp_mask
| __GFP_ZERO
, 0);
998 return (unsigned long) page_address(page
);
1002 EXPORT_SYMBOL(get_zeroed_page
);
1004 void __pagevec_free(struct pagevec
*pvec
)
1006 int i
= pagevec_count(pvec
);
1009 free_hot_cold_page(pvec
->pages
[i
], pvec
->cold
);
1012 fastcall
void __free_pages(struct page
*page
, unsigned int order
)
1014 if (!PageReserved(page
) && put_page_testzero(page
)) {
1016 free_hot_page(page
);
1018 __free_pages_ok(page
, order
);
1022 EXPORT_SYMBOL(__free_pages
);
1024 fastcall
void free_pages(unsigned long addr
, unsigned int order
)
1027 BUG_ON(!virt_addr_valid((void *)addr
));
1028 __free_pages(virt_to_page((void *)addr
), order
);
1032 EXPORT_SYMBOL(free_pages
);
1035 * Total amount of free (allocatable) RAM:
1037 unsigned int nr_free_pages(void)
1039 unsigned int sum
= 0;
1043 sum
+= zone
->free_pages
;
1048 EXPORT_SYMBOL(nr_free_pages
);
1051 unsigned int nr_free_pages_pgdat(pg_data_t
*pgdat
)
1053 unsigned int i
, sum
= 0;
1055 for (i
= 0; i
< MAX_NR_ZONES
; i
++)
1056 sum
+= pgdat
->node_zones
[i
].free_pages
;
1062 static unsigned int nr_free_zone_pages(int offset
)
1065 unsigned int sum
= 0;
1067 for_each_pgdat(pgdat
) {
1068 struct zonelist
*zonelist
= pgdat
->node_zonelists
+ offset
;
1069 struct zone
**zonep
= zonelist
->zones
;
1072 for (zone
= *zonep
++; zone
; zone
= *zonep
++) {
1073 unsigned long size
= zone
->present_pages
;
1074 unsigned long high
= zone
->pages_high
;
1084 * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
1086 unsigned int nr_free_buffer_pages(void)
1088 return nr_free_zone_pages(GFP_USER
& GFP_ZONEMASK
);
1092 * Amount of free RAM allocatable within all zones
1094 unsigned int nr_free_pagecache_pages(void)
1096 return nr_free_zone_pages(GFP_HIGHUSER
& GFP_ZONEMASK
);
1099 #ifdef CONFIG_HIGHMEM
1100 unsigned int nr_free_highpages (void)
1103 unsigned int pages
= 0;
1105 for_each_pgdat(pgdat
)
1106 pages
+= pgdat
->node_zones
[ZONE_HIGHMEM
].free_pages
;
1113 static void show_node(struct zone
*zone
)
1115 printk("Node %d ", zone
->zone_pgdat
->node_id
);
1118 #define show_node(zone) do { } while (0)
1122 * Accumulate the page_state information across all CPUs.
1123 * The result is unavoidably approximate - it can change
1124 * during and after execution of this function.
1126 static DEFINE_PER_CPU(struct page_state
, page_states
) = {0};
1128 atomic_t nr_pagecache
= ATOMIC_INIT(0);
1129 EXPORT_SYMBOL(nr_pagecache
);
1131 DEFINE_PER_CPU(long, nr_pagecache_local
) = 0;
1134 void __get_page_state(struct page_state
*ret
, int nr
)
1138 memset(ret
, 0, sizeof(*ret
));
1140 cpu
= first_cpu(cpu_online_map
);
1141 while (cpu
< NR_CPUS
) {
1142 unsigned long *in
, *out
, off
;
1144 in
= (unsigned long *)&per_cpu(page_states
, cpu
);
1146 cpu
= next_cpu(cpu
, cpu_online_map
);
1149 prefetch(&per_cpu(page_states
, cpu
));
1151 out
= (unsigned long *)ret
;
1152 for (off
= 0; off
< nr
; off
++)
1157 void get_page_state(struct page_state
*ret
)
1161 nr
= offsetof(struct page_state
, GET_PAGE_STATE_LAST
);
1162 nr
/= sizeof(unsigned long);
1164 __get_page_state(ret
, nr
+ 1);
1167 void get_full_page_state(struct page_state
*ret
)
1169 __get_page_state(ret
, sizeof(*ret
) / sizeof(unsigned long));
1172 unsigned long __read_page_state(unsigned long offset
)
1174 unsigned long ret
= 0;
1177 for_each_online_cpu(cpu
) {
1180 in
= (unsigned long)&per_cpu(page_states
, cpu
) + offset
;
1181 ret
+= *((unsigned long *)in
);
1186 void __mod_page_state(unsigned long offset
, unsigned long delta
)
1188 unsigned long flags
;
1191 local_irq_save(flags
);
1192 ptr
= &__get_cpu_var(page_states
);
1193 *(unsigned long*)(ptr
+ offset
) += delta
;
1194 local_irq_restore(flags
);
1197 EXPORT_SYMBOL(__mod_page_state
);
1199 void __get_zone_counts(unsigned long *active
, unsigned long *inactive
,
1200 unsigned long *free
, struct pglist_data
*pgdat
)
1202 struct zone
*zones
= pgdat
->node_zones
;
1208 for (i
= 0; i
< MAX_NR_ZONES
; i
++) {
1209 *active
+= zones
[i
].nr_active
;
1210 *inactive
+= zones
[i
].nr_inactive
;
1211 *free
+= zones
[i
].free_pages
;
1215 void get_zone_counts(unsigned long *active
,
1216 unsigned long *inactive
, unsigned long *free
)
1218 struct pglist_data
*pgdat
;
1223 for_each_pgdat(pgdat
) {
1224 unsigned long l
, m
, n
;
1225 __get_zone_counts(&l
, &m
, &n
, pgdat
);
1232 void si_meminfo(struct sysinfo
*val
)
1234 val
->totalram
= totalram_pages
;
1236 val
->freeram
= nr_free_pages();
1237 val
->bufferram
= nr_blockdev_pages();
1238 #ifdef CONFIG_HIGHMEM
1239 val
->totalhigh
= totalhigh_pages
;
1240 val
->freehigh
= nr_free_highpages();
1245 val
->mem_unit
= PAGE_SIZE
;
1248 EXPORT_SYMBOL(si_meminfo
);
1251 void si_meminfo_node(struct sysinfo
*val
, int nid
)
1253 pg_data_t
*pgdat
= NODE_DATA(nid
);
1255 val
->totalram
= pgdat
->node_present_pages
;
1256 val
->freeram
= nr_free_pages_pgdat(pgdat
);
1257 val
->totalhigh
= pgdat
->node_zones
[ZONE_HIGHMEM
].present_pages
;
1258 val
->freehigh
= pgdat
->node_zones
[ZONE_HIGHMEM
].free_pages
;
1259 val
->mem_unit
= PAGE_SIZE
;
1263 #define K(x) ((x) << (PAGE_SHIFT-10))
1266 * Show free area list (used inside shift_scroll-lock stuff)
1267 * We also calculate the percentage fragmentation. We do this by counting the
1268 * memory on each free list with the exception of the first item on the list.
1270 void show_free_areas(void)
1272 struct page_state ps
;
1273 int cpu
, temperature
;
1274 unsigned long active
;
1275 unsigned long inactive
;
1279 for_each_zone(zone
) {
1281 printk("%s per-cpu:", zone
->name
);
1283 if (!zone
->present_pages
) {
1289 for (cpu
= 0; cpu
< NR_CPUS
; ++cpu
) {
1290 struct per_cpu_pageset
*pageset
;
1292 if (!cpu_possible(cpu
))
1295 pageset
= zone_pcp(zone
, cpu
);
1297 for (temperature
= 0; temperature
< 2; temperature
++)
1298 printk("cpu %d %s: low %d, high %d, batch %d used:%d\n",
1300 temperature
? "cold" : "hot",
1301 pageset
->pcp
[temperature
].low
,
1302 pageset
->pcp
[temperature
].high
,
1303 pageset
->pcp
[temperature
].batch
,
1304 pageset
->pcp
[temperature
].count
);
1308 get_page_state(&ps
);
1309 get_zone_counts(&active
, &inactive
, &free
);
1311 printk("Free pages: %11ukB (%ukB HighMem)\n",
1313 K(nr_free_highpages()));
1315 printk("Active:%lu inactive:%lu dirty:%lu writeback:%lu "
1316 "unstable:%lu free:%u slab:%lu mapped:%lu pagetables:%lu\n",
1325 ps
.nr_page_table_pages
);
1327 for_each_zone(zone
) {
1339 " pages_scanned:%lu"
1340 " all_unreclaimable? %s"
1343 K(zone
->free_pages
),
1346 K(zone
->pages_high
),
1348 K(zone
->nr_inactive
),
1349 K(zone
->present_pages
),
1350 zone
->pages_scanned
,
1351 (zone
->all_unreclaimable
? "yes" : "no")
1353 printk("lowmem_reserve[]:");
1354 for (i
= 0; i
< MAX_NR_ZONES
; i
++)
1355 printk(" %lu", zone
->lowmem_reserve
[i
]);
1359 for_each_zone(zone
) {
1360 unsigned long nr
, flags
, order
, total
= 0;
1363 printk("%s: ", zone
->name
);
1364 if (!zone
->present_pages
) {
1369 spin_lock_irqsave(&zone
->lock
, flags
);
1370 for (order
= 0; order
< MAX_ORDER
; order
++) {
1371 nr
= zone
->free_area
[order
].nr_free
;
1372 total
+= nr
<< order
;
1373 printk("%lu*%lukB ", nr
, K(1UL) << order
);
1375 spin_unlock_irqrestore(&zone
->lock
, flags
);
1376 printk("= %lukB\n", K(total
));
1379 show_swap_cache_info();
1383 * Builds allocation fallback zone lists.
1385 static int __init
build_zonelists_node(pg_data_t
*pgdat
, struct zonelist
*zonelist
, int j
, int k
)
1392 zone
= pgdat
->node_zones
+ ZONE_HIGHMEM
;
1393 if (zone
->present_pages
) {
1394 #ifndef CONFIG_HIGHMEM
1397 zonelist
->zones
[j
++] = zone
;
1400 zone
= pgdat
->node_zones
+ ZONE_NORMAL
;
1401 if (zone
->present_pages
)
1402 zonelist
->zones
[j
++] = zone
;
1404 zone
= pgdat
->node_zones
+ ZONE_DMA
;
1405 if (zone
->present_pages
)
1406 zonelist
->zones
[j
++] = zone
;
1413 #define MAX_NODE_LOAD (num_online_nodes())
1414 static int __initdata node_load
[MAX_NUMNODES
];
1416 * find_next_best_node - find the next node that should appear in a given node's fallback list
1417 * @node: node whose fallback list we're appending
1418 * @used_node_mask: nodemask_t of already used nodes
1420 * We use a number of factors to determine which is the next node that should
1421 * appear on a given node's fallback list. The node should not have appeared
1422 * already in @node's fallback list, and it should be the next closest node
1423 * according to the distance array (which contains arbitrary distance values
1424 * from each node to each node in the system), and should also prefer nodes
1425 * with no CPUs, since presumably they'll have very little allocation pressure
1426 * on them otherwise.
1427 * It returns -1 if no node is found.
1429 static int __init
find_next_best_node(int node
, nodemask_t
*used_node_mask
)
1432 int min_val
= INT_MAX
;
1435 for_each_online_node(i
) {
1438 /* Start from local node */
1439 n
= (node
+i
) % num_online_nodes();
1441 /* Don't want a node to appear more than once */
1442 if (node_isset(n
, *used_node_mask
))
1445 /* Use the local node if we haven't already */
1446 if (!node_isset(node
, *used_node_mask
)) {
1451 /* Use the distance array to find the distance */
1452 val
= node_distance(node
, n
);
1454 /* Give preference to headless and unused nodes */
1455 tmp
= node_to_cpumask(n
);
1456 if (!cpus_empty(tmp
))
1457 val
+= PENALTY_FOR_NODE_WITH_CPUS
;
1459 /* Slight preference for less loaded node */
1460 val
*= (MAX_NODE_LOAD
*MAX_NUMNODES
);
1461 val
+= node_load
[n
];
1463 if (val
< min_val
) {
1470 node_set(best_node
, *used_node_mask
);
1475 static void __init
build_zonelists(pg_data_t
*pgdat
)
1477 int i
, j
, k
, node
, local_node
;
1478 int prev_node
, load
;
1479 struct zonelist
*zonelist
;
1480 nodemask_t used_mask
;
1482 /* initialize zonelists */
1483 for (i
= 0; i
< GFP_ZONETYPES
; i
++) {
1484 zonelist
= pgdat
->node_zonelists
+ i
;
1485 zonelist
->zones
[0] = NULL
;
1488 /* NUMA-aware ordering of nodes */
1489 local_node
= pgdat
->node_id
;
1490 load
= num_online_nodes();
1491 prev_node
= local_node
;
1492 nodes_clear(used_mask
);
1493 while ((node
= find_next_best_node(local_node
, &used_mask
)) >= 0) {
1495 * We don't want to pressure a particular node.
1496 * So adding penalty to the first node in same
1497 * distance group to make it round-robin.
1499 if (node_distance(local_node
, node
) !=
1500 node_distance(local_node
, prev_node
))
1501 node_load
[node
] += load
;
1504 for (i
= 0; i
< GFP_ZONETYPES
; i
++) {
1505 zonelist
= pgdat
->node_zonelists
+ i
;
1506 for (j
= 0; zonelist
->zones
[j
] != NULL
; j
++);
1509 if (i
& __GFP_HIGHMEM
)
1514 j
= build_zonelists_node(NODE_DATA(node
), zonelist
, j
, k
);
1515 zonelist
->zones
[j
] = NULL
;
1520 #else /* CONFIG_NUMA */
1522 static void __init
build_zonelists(pg_data_t
*pgdat
)
1524 int i
, j
, k
, node
, local_node
;
1526 local_node
= pgdat
->node_id
;
1527 for (i
= 0; i
< GFP_ZONETYPES
; i
++) {
1528 struct zonelist
*zonelist
;
1530 zonelist
= pgdat
->node_zonelists
+ i
;
1534 if (i
& __GFP_HIGHMEM
)
1539 j
= build_zonelists_node(pgdat
, zonelist
, j
, k
);
1541 * Now we build the zonelist so that it contains the zones
1542 * of all the other nodes.
1543 * We don't want to pressure a particular node, so when
1544 * building the zones for node N, we make sure that the
1545 * zones coming right after the local ones are those from
1546 * node N+1 (modulo N)
1548 for (node
= local_node
+ 1; node
< MAX_NUMNODES
; node
++) {
1549 if (!node_online(node
))
1551 j
= build_zonelists_node(NODE_DATA(node
), zonelist
, j
, k
);
1553 for (node
= 0; node
< local_node
; node
++) {
1554 if (!node_online(node
))
1556 j
= build_zonelists_node(NODE_DATA(node
), zonelist
, j
, k
);
1559 zonelist
->zones
[j
] = NULL
;
1563 #endif /* CONFIG_NUMA */
1565 void __init
build_all_zonelists(void)
1569 for_each_online_node(i
)
1570 build_zonelists(NODE_DATA(i
));
1571 printk("Built %i zonelists\n", num_online_nodes());
1572 cpuset_init_current_mems_allowed();
1576 * Helper functions to size the waitqueue hash table.
1577 * Essentially these want to choose hash table sizes sufficiently
1578 * large so that collisions trying to wait on pages are rare.
1579 * But in fact, the number of active page waitqueues on typical
1580 * systems is ridiculously low, less than 200. So this is even
1581 * conservative, even though it seems large.
1583 * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
1584 * waitqueues, i.e. the size of the waitq table given the number of pages.
1586 #define PAGES_PER_WAITQUEUE 256
1588 static inline unsigned long wait_table_size(unsigned long pages
)
1590 unsigned long size
= 1;
1592 pages
/= PAGES_PER_WAITQUEUE
;
1594 while (size
< pages
)
1598 * Once we have dozens or even hundreds of threads sleeping
1599 * on IO we've got bigger problems than wait queue collision.
1600 * Limit the size of the wait table to a reasonable size.
1602 size
= min(size
, 4096UL);
1604 return max(size
, 4UL);
1608 * This is an integer logarithm so that shifts can be used later
1609 * to extract the more random high bits from the multiplicative
1610 * hash function before the remainder is taken.
1612 static inline unsigned long wait_table_bits(unsigned long size
)
1617 #define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
1619 static void __init
calculate_zone_totalpages(struct pglist_data
*pgdat
,
1620 unsigned long *zones_size
, unsigned long *zholes_size
)
1622 unsigned long realtotalpages
, totalpages
= 0;
1625 for (i
= 0; i
< MAX_NR_ZONES
; i
++)
1626 totalpages
+= zones_size
[i
];
1627 pgdat
->node_spanned_pages
= totalpages
;
1629 realtotalpages
= totalpages
;
1631 for (i
= 0; i
< MAX_NR_ZONES
; i
++)
1632 realtotalpages
-= zholes_size
[i
];
1633 pgdat
->node_present_pages
= realtotalpages
;
1634 printk(KERN_DEBUG
"On node %d totalpages: %lu\n", pgdat
->node_id
, realtotalpages
);
1639 * Initially all pages are reserved - free ones are freed
1640 * up by free_all_bootmem() once the early boot process is
1641 * done. Non-atomic initialization, single-pass.
1643 void __init
memmap_init_zone(unsigned long size
, int nid
, unsigned long zone
,
1644 unsigned long start_pfn
)
1647 unsigned long end_pfn
= start_pfn
+ size
;
1650 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
++, page
++) {
1651 if (!early_pfn_valid(pfn
))
1653 if (!early_pfn_in_nid(pfn
, nid
))
1655 page
= pfn_to_page(pfn
);
1656 set_page_links(page
, zone
, nid
, pfn
);
1657 set_page_count(page
, 0);
1658 reset_page_mapcount(page
);
1659 SetPageReserved(page
);
1660 INIT_LIST_HEAD(&page
->lru
);
1661 #ifdef WANT_PAGE_VIRTUAL
1662 /* The shift won't overflow because ZONE_NORMAL is below 4G. */
1663 if (!is_highmem_idx(zone
))
1664 set_page_address(page
, __va(pfn
<< PAGE_SHIFT
));
1669 void zone_init_free_lists(struct pglist_data
*pgdat
, struct zone
*zone
,
1673 for (order
= 0; order
< MAX_ORDER
; order
++) {
1674 INIT_LIST_HEAD(&zone
->free_area
[order
].free_list
);
1675 zone
->free_area
[order
].nr_free
= 0;
1679 #define ZONETABLE_INDEX(x, zone_nr) ((x << ZONES_SHIFT) | zone_nr)
1680 void zonetable_add(struct zone
*zone
, int nid
, int zid
, unsigned long pfn
,
1683 unsigned long snum
= pfn_to_section_nr(pfn
);
1684 unsigned long end
= pfn_to_section_nr(pfn
+ size
);
1687 zone_table
[ZONETABLE_INDEX(nid
, zid
)] = zone
;
1689 for (; snum
<= end
; snum
++)
1690 zone_table
[ZONETABLE_INDEX(snum
, zid
)] = zone
;
1693 #ifndef __HAVE_ARCH_MEMMAP_INIT
1694 #define memmap_init(size, nid, zone, start_pfn) \
1695 memmap_init_zone((size), (nid), (zone), (start_pfn))
1698 static int __devinit
zone_batchsize(struct zone
*zone
)
1703 * The per-cpu-pages pools are set to around 1000th of the
1704 * size of the zone. But no more than 1/4 of a meg - there's
1705 * no point in going beyond the size of L2 cache.
1707 * OK, so we don't know how big the cache is. So guess.
1709 batch
= zone
->present_pages
/ 1024;
1710 if (batch
* PAGE_SIZE
> 256 * 1024)
1711 batch
= (256 * 1024) / PAGE_SIZE
;
1712 batch
/= 4; /* We effectively *= 4 below */
1717 * Clamp the batch to a 2^n - 1 value. Having a power
1718 * of 2 value was found to be more likely to have
1719 * suboptimal cache aliasing properties in some cases.
1721 * For example if 2 tasks are alternately allocating
1722 * batches of pages, one task can end up with a lot
1723 * of pages of one half of the possible page colors
1724 * and the other with pages of the other colors.
1726 batch
= (1 << fls(batch
+ batch
/2)) - 1;
1730 inline void setup_pageset(struct per_cpu_pageset
*p
, unsigned long batch
)
1732 struct per_cpu_pages
*pcp
;
1734 pcp
= &p
->pcp
[0]; /* hot */
1736 pcp
->low
= 2 * batch
;
1737 pcp
->high
= 6 * batch
;
1738 pcp
->batch
= max(1UL, 1 * batch
);
1739 INIT_LIST_HEAD(&pcp
->list
);
1741 pcp
= &p
->pcp
[1]; /* cold*/
1744 pcp
->high
= 2 * batch
;
1745 pcp
->batch
= max(1UL, 1 * batch
);
1746 INIT_LIST_HEAD(&pcp
->list
);
1751 * Boot pageset table. One per cpu which is going to be used for all
1752 * zones and all nodes. The parameters will be set in such a way
1753 * that an item put on a list will immediately be handed over to
1754 * the buddy list. This is safe since pageset manipulation is done
1755 * with interrupts disabled.
1757 * Some NUMA counter updates may also be caught by the boot pagesets.
1759 * The boot_pagesets must be kept even after bootup is complete for
1760 * unused processors and/or zones. They do play a role for bootstrapping
1761 * hotplugged processors.
1763 * zoneinfo_show() and maybe other functions do
1764 * not check if the processor is online before following the pageset pointer.
1765 * Other parts of the kernel may not check if the zone is available.
1767 static struct per_cpu_pageset
1768 boot_pageset
[NR_CPUS
];
1771 * Dynamically allocate memory for the
1772 * per cpu pageset array in struct zone.
1774 static int __devinit
process_zones(int cpu
)
1776 struct zone
*zone
, *dzone
;
1778 for_each_zone(zone
) {
1780 zone
->pageset
[cpu
] = kmalloc_node(sizeof(struct per_cpu_pageset
),
1781 GFP_KERNEL
, cpu_to_node(cpu
));
1782 if (!zone
->pageset
[cpu
])
1785 setup_pageset(zone
->pageset
[cpu
], zone_batchsize(zone
));
1790 for_each_zone(dzone
) {
1793 kfree(dzone
->pageset
[cpu
]);
1794 dzone
->pageset
[cpu
] = NULL
;
1799 static inline void free_zone_pagesets(int cpu
)
1804 for_each_zone(zone
) {
1805 struct per_cpu_pageset
*pset
= zone_pcp(zone
, cpu
);
1807 zone_pcp(zone
, cpu
) = NULL
;
1813 static int __devinit
pageset_cpuup_callback(struct notifier_block
*nfb
,
1814 unsigned long action
,
1817 int cpu
= (long)hcpu
;
1818 int ret
= NOTIFY_OK
;
1821 case CPU_UP_PREPARE
:
1822 if (process_zones(cpu
))
1825 #ifdef CONFIG_HOTPLUG_CPU
1827 free_zone_pagesets(cpu
);
1836 static struct notifier_block pageset_notifier
=
1837 { &pageset_cpuup_callback
, NULL
, 0 };
1839 void __init
setup_per_cpu_pageset()
1843 /* Initialize per_cpu_pageset for cpu 0.
1844 * A cpuup callback will do this for every cpu
1845 * as it comes online
1847 err
= process_zones(smp_processor_id());
1849 register_cpu_notifier(&pageset_notifier
);
1855 * Set up the zone data structures:
1856 * - mark all pages reserved
1857 * - mark all memory queues empty
1858 * - clear the memory bitmaps
1860 static void __init
free_area_init_core(struct pglist_data
*pgdat
,
1861 unsigned long *zones_size
, unsigned long *zholes_size
)
1864 const unsigned long zone_required_alignment
= 1UL << (MAX_ORDER
-1);
1865 int cpu
, nid
= pgdat
->node_id
;
1866 unsigned long zone_start_pfn
= pgdat
->node_start_pfn
;
1868 pgdat
->nr_zones
= 0;
1869 init_waitqueue_head(&pgdat
->kswapd_wait
);
1870 pgdat
->kswapd_max_order
= 0;
1872 for (j
= 0; j
< MAX_NR_ZONES
; j
++) {
1873 struct zone
*zone
= pgdat
->node_zones
+ j
;
1874 unsigned long size
, realsize
;
1875 unsigned long batch
;
1877 realsize
= size
= zones_size
[j
];
1879 realsize
-= zholes_size
[j
];
1881 if (j
== ZONE_DMA
|| j
== ZONE_NORMAL
)
1882 nr_kernel_pages
+= realsize
;
1883 nr_all_pages
+= realsize
;
1885 zone
->spanned_pages
= size
;
1886 zone
->present_pages
= realsize
;
1887 zone
->name
= zone_names
[j
];
1888 spin_lock_init(&zone
->lock
);
1889 spin_lock_init(&zone
->lru_lock
);
1890 zone
->zone_pgdat
= pgdat
;
1891 zone
->free_pages
= 0;
1893 zone
->temp_priority
= zone
->prev_priority
= DEF_PRIORITY
;
1895 batch
= zone_batchsize(zone
);
1897 for (cpu
= 0; cpu
< NR_CPUS
; cpu
++) {
1899 /* Early boot. Slab allocator not functional yet */
1900 zone
->pageset
[cpu
] = &boot_pageset
[cpu
];
1901 setup_pageset(&boot_pageset
[cpu
],0);
1903 setup_pageset(zone_pcp(zone
,cpu
), batch
);
1906 printk(KERN_DEBUG
" %s zone: %lu pages, LIFO batch:%lu\n",
1907 zone_names
[j
], realsize
, batch
);
1908 INIT_LIST_HEAD(&zone
->active_list
);
1909 INIT_LIST_HEAD(&zone
->inactive_list
);
1910 zone
->nr_scan_active
= 0;
1911 zone
->nr_scan_inactive
= 0;
1912 zone
->nr_active
= 0;
1913 zone
->nr_inactive
= 0;
1914 atomic_set(&zone
->reclaim_in_progress
, -1);
1919 * The per-page waitqueue mechanism uses hashed waitqueues
1922 zone
->wait_table_size
= wait_table_size(size
);
1923 zone
->wait_table_bits
=
1924 wait_table_bits(zone
->wait_table_size
);
1925 zone
->wait_table
= (wait_queue_head_t
*)
1926 alloc_bootmem_node(pgdat
, zone
->wait_table_size
1927 * sizeof(wait_queue_head_t
));
1929 for(i
= 0; i
< zone
->wait_table_size
; ++i
)
1930 init_waitqueue_head(zone
->wait_table
+ i
);
1932 pgdat
->nr_zones
= j
+1;
1934 zone
->zone_mem_map
= pfn_to_page(zone_start_pfn
);
1935 zone
->zone_start_pfn
= zone_start_pfn
;
1937 if ((zone_start_pfn
) & (zone_required_alignment
-1))
1938 printk(KERN_CRIT
"BUG: wrong zone alignment, it will crash\n");
1940 memmap_init(size
, nid
, j
, zone_start_pfn
);
1942 zonetable_add(zone
, nid
, j
, zone_start_pfn
, size
);
1944 zone_start_pfn
+= size
;
1946 zone_init_free_lists(pgdat
, zone
, zone
->spanned_pages
);
1950 static void __init
alloc_node_mem_map(struct pglist_data
*pgdat
)
1952 /* Skip empty nodes */
1953 if (!pgdat
->node_spanned_pages
)
1956 #ifdef CONFIG_FLAT_NODE_MEM_MAP
1957 /* ia64 gets its own node_mem_map, before this, without bootmem */
1958 if (!pgdat
->node_mem_map
) {
1962 size
= (pgdat
->node_spanned_pages
+ 1) * sizeof(struct page
);
1963 map
= alloc_remap(pgdat
->node_id
, size
);
1965 map
= alloc_bootmem_node(pgdat
, size
);
1966 pgdat
->node_mem_map
= map
;
1968 #ifdef CONFIG_FLATMEM
1970 * With no DISCONTIG, the global mem_map is just set as node 0's
1972 if (pgdat
== NODE_DATA(0))
1973 mem_map
= NODE_DATA(0)->node_mem_map
;
1975 #endif /* CONFIG_FLAT_NODE_MEM_MAP */
1978 void __init
free_area_init_node(int nid
, struct pglist_data
*pgdat
,
1979 unsigned long *zones_size
, unsigned long node_start_pfn
,
1980 unsigned long *zholes_size
)
1982 pgdat
->node_id
= nid
;
1983 pgdat
->node_start_pfn
= node_start_pfn
;
1984 calculate_zone_totalpages(pgdat
, zones_size
, zholes_size
);
1986 alloc_node_mem_map(pgdat
);
1988 free_area_init_core(pgdat
, zones_size
, zholes_size
);
1991 #ifndef CONFIG_NEED_MULTIPLE_NODES
1992 static bootmem_data_t contig_bootmem_data
;
1993 struct pglist_data contig_page_data
= { .bdata
= &contig_bootmem_data
};
1995 EXPORT_SYMBOL(contig_page_data
);
1998 void __init
free_area_init(unsigned long *zones_size
)
2000 free_area_init_node(0, NODE_DATA(0), zones_size
,
2001 __pa(PAGE_OFFSET
) >> PAGE_SHIFT
, NULL
);
2004 #ifdef CONFIG_PROC_FS
2006 #include <linux/seq_file.h>
2008 static void *frag_start(struct seq_file
*m
, loff_t
*pos
)
2013 for (pgdat
= pgdat_list
; pgdat
&& node
; pgdat
= pgdat
->pgdat_next
)
2019 static void *frag_next(struct seq_file
*m
, void *arg
, loff_t
*pos
)
2021 pg_data_t
*pgdat
= (pg_data_t
*)arg
;
2024 return pgdat
->pgdat_next
;
2027 static void frag_stop(struct seq_file
*m
, void *arg
)
2032 * This walks the free areas for each zone.
2034 static int frag_show(struct seq_file
*m
, void *arg
)
2036 pg_data_t
*pgdat
= (pg_data_t
*)arg
;
2038 struct zone
*node_zones
= pgdat
->node_zones
;
2039 unsigned long flags
;
2042 for (zone
= node_zones
; zone
- node_zones
< MAX_NR_ZONES
; ++zone
) {
2043 if (!zone
->present_pages
)
2046 spin_lock_irqsave(&zone
->lock
, flags
);
2047 seq_printf(m
, "Node %d, zone %8s ", pgdat
->node_id
, zone
->name
);
2048 for (order
= 0; order
< MAX_ORDER
; ++order
)
2049 seq_printf(m
, "%6lu ", zone
->free_area
[order
].nr_free
);
2050 spin_unlock_irqrestore(&zone
->lock
, flags
);
2056 struct seq_operations fragmentation_op
= {
2057 .start
= frag_start
,
2064 * Output information about zones in @pgdat.
2066 static int zoneinfo_show(struct seq_file
*m
, void *arg
)
2068 pg_data_t
*pgdat
= arg
;
2070 struct zone
*node_zones
= pgdat
->node_zones
;
2071 unsigned long flags
;
2073 for (zone
= node_zones
; zone
- node_zones
< MAX_NR_ZONES
; zone
++) {
2076 if (!zone
->present_pages
)
2079 spin_lock_irqsave(&zone
->lock
, flags
);
2080 seq_printf(m
, "Node %d, zone %8s", pgdat
->node_id
, zone
->name
);
2088 "\n scanned %lu (a: %lu i: %lu)"
2097 zone
->pages_scanned
,
2098 zone
->nr_scan_active
, zone
->nr_scan_inactive
,
2099 zone
->spanned_pages
,
2100 zone
->present_pages
);
2102 "\n protection: (%lu",
2103 zone
->lowmem_reserve
[0]);
2104 for (i
= 1; i
< ARRAY_SIZE(zone
->lowmem_reserve
); i
++)
2105 seq_printf(m
, ", %lu", zone
->lowmem_reserve
[i
]);
2109 for (i
= 0; i
< ARRAY_SIZE(zone
->pageset
); i
++) {
2110 struct per_cpu_pageset
*pageset
;
2113 pageset
= zone_pcp(zone
, i
);
2114 for (j
= 0; j
< ARRAY_SIZE(pageset
->pcp
); j
++) {
2115 if (pageset
->pcp
[j
].count
)
2118 if (j
== ARRAY_SIZE(pageset
->pcp
))
2120 for (j
= 0; j
< ARRAY_SIZE(pageset
->pcp
); j
++) {
2122 "\n cpu: %i pcp: %i"
2128 pageset
->pcp
[j
].count
,
2129 pageset
->pcp
[j
].low
,
2130 pageset
->pcp
[j
].high
,
2131 pageset
->pcp
[j
].batch
);
2137 "\n numa_foreign: %lu"
2138 "\n interleave_hit: %lu"
2139 "\n local_node: %lu"
2140 "\n other_node: %lu",
2143 pageset
->numa_foreign
,
2144 pageset
->interleave_hit
,
2145 pageset
->local_node
,
2146 pageset
->other_node
);
2150 "\n all_unreclaimable: %u"
2151 "\n prev_priority: %i"
2152 "\n temp_priority: %i"
2153 "\n start_pfn: %lu",
2154 zone
->all_unreclaimable
,
2155 zone
->prev_priority
,
2156 zone
->temp_priority
,
2157 zone
->zone_start_pfn
);
2158 spin_unlock_irqrestore(&zone
->lock
, flags
);
2164 struct seq_operations zoneinfo_op
= {
2165 .start
= frag_start
, /* iterate over all zones. The same as in
2169 .show
= zoneinfo_show
,
2172 static char *vmstat_text
[] = {
2176 "nr_page_table_pages",
2201 "pgscan_kswapd_high",
2202 "pgscan_kswapd_normal",
2204 "pgscan_kswapd_dma",
2205 "pgscan_direct_high",
2206 "pgscan_direct_normal",
2207 "pgscan_direct_dma",
2212 "kswapd_inodesteal",
2220 static void *vmstat_start(struct seq_file
*m
, loff_t
*pos
)
2222 struct page_state
*ps
;
2224 if (*pos
>= ARRAY_SIZE(vmstat_text
))
2227 ps
= kmalloc(sizeof(*ps
), GFP_KERNEL
);
2230 return ERR_PTR(-ENOMEM
);
2231 get_full_page_state(ps
);
2232 ps
->pgpgin
/= 2; /* sectors -> kbytes */
2234 return (unsigned long *)ps
+ *pos
;
2237 static void *vmstat_next(struct seq_file
*m
, void *arg
, loff_t
*pos
)
2240 if (*pos
>= ARRAY_SIZE(vmstat_text
))
2242 return (unsigned long *)m
->private + *pos
;
2245 static int vmstat_show(struct seq_file
*m
, void *arg
)
2247 unsigned long *l
= arg
;
2248 unsigned long off
= l
- (unsigned long *)m
->private;
2250 seq_printf(m
, "%s %lu\n", vmstat_text
[off
], *l
);
2254 static void vmstat_stop(struct seq_file
*m
, void *arg
)
2260 struct seq_operations vmstat_op
= {
2261 .start
= vmstat_start
,
2262 .next
= vmstat_next
,
2263 .stop
= vmstat_stop
,
2264 .show
= vmstat_show
,
2267 #endif /* CONFIG_PROC_FS */
2269 #ifdef CONFIG_HOTPLUG_CPU
2270 static int page_alloc_cpu_notify(struct notifier_block
*self
,
2271 unsigned long action
, void *hcpu
)
2273 int cpu
= (unsigned long)hcpu
;
2275 unsigned long *src
, *dest
;
2277 if (action
== CPU_DEAD
) {
2280 /* Drain local pagecache count. */
2281 count
= &per_cpu(nr_pagecache_local
, cpu
);
2282 atomic_add(*count
, &nr_pagecache
);
2284 local_irq_disable();
2287 /* Add dead cpu's page_states to our own. */
2288 dest
= (unsigned long *)&__get_cpu_var(page_states
);
2289 src
= (unsigned long *)&per_cpu(page_states
, cpu
);
2291 for (i
= 0; i
< sizeof(struct page_state
)/sizeof(unsigned long);
2301 #endif /* CONFIG_HOTPLUG_CPU */
2303 void __init
page_alloc_init(void)
2305 hotcpu_notifier(page_alloc_cpu_notify
, 0);
2309 * setup_per_zone_lowmem_reserve - called whenever
2310 * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
2311 * has a correct pages reserved value, so an adequate number of
2312 * pages are left in the zone after a successful __alloc_pages().
2314 static void setup_per_zone_lowmem_reserve(void)
2316 struct pglist_data
*pgdat
;
2319 for_each_pgdat(pgdat
) {
2320 for (j
= 0; j
< MAX_NR_ZONES
; j
++) {
2321 struct zone
*zone
= pgdat
->node_zones
+ j
;
2322 unsigned long present_pages
= zone
->present_pages
;
2324 zone
->lowmem_reserve
[j
] = 0;
2326 for (idx
= j
-1; idx
>= 0; idx
--) {
2327 struct zone
*lower_zone
;
2329 if (sysctl_lowmem_reserve_ratio
[idx
] < 1)
2330 sysctl_lowmem_reserve_ratio
[idx
] = 1;
2332 lower_zone
= pgdat
->node_zones
+ idx
;
2333 lower_zone
->lowmem_reserve
[j
] = present_pages
/
2334 sysctl_lowmem_reserve_ratio
[idx
];
2335 present_pages
+= lower_zone
->present_pages
;
2342 * setup_per_zone_pages_min - called when min_free_kbytes changes. Ensures
2343 * that the pages_{min,low,high} values for each zone are set correctly
2344 * with respect to min_free_kbytes.
2346 static void setup_per_zone_pages_min(void)
2348 unsigned long pages_min
= min_free_kbytes
>> (PAGE_SHIFT
- 10);
2349 unsigned long lowmem_pages
= 0;
2351 unsigned long flags
;
2353 /* Calculate total number of !ZONE_HIGHMEM pages */
2354 for_each_zone(zone
) {
2355 if (!is_highmem(zone
))
2356 lowmem_pages
+= zone
->present_pages
;
2359 for_each_zone(zone
) {
2360 spin_lock_irqsave(&zone
->lru_lock
, flags
);
2361 if (is_highmem(zone
)) {
2363 * Often, highmem doesn't need to reserve any pages.
2364 * But the pages_min/low/high values are also used for
2365 * batching up page reclaim activity so we need a
2366 * decent value here.
2370 min_pages
= zone
->present_pages
/ 1024;
2371 if (min_pages
< SWAP_CLUSTER_MAX
)
2372 min_pages
= SWAP_CLUSTER_MAX
;
2373 if (min_pages
> 128)
2375 zone
->pages_min
= min_pages
;
2377 /* if it's a lowmem zone, reserve a number of pages
2378 * proportionate to the zone's size.
2380 zone
->pages_min
= (pages_min
* zone
->present_pages
) /
2385 * When interpreting these watermarks, just keep in mind that:
2386 * zone->pages_min == (zone->pages_min * 4) / 4;
2388 zone
->pages_low
= (zone
->pages_min
* 5) / 4;
2389 zone
->pages_high
= (zone
->pages_min
* 6) / 4;
2390 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
2395 * Initialise min_free_kbytes.
2397 * For small machines we want it small (128k min). For large machines
2398 * we want it large (64MB max). But it is not linear, because network
2399 * bandwidth does not increase linearly with machine size. We use
2401 * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
2402 * min_free_kbytes = sqrt(lowmem_kbytes * 16)
2418 static int __init
init_per_zone_pages_min(void)
2420 unsigned long lowmem_kbytes
;
2422 lowmem_kbytes
= nr_free_buffer_pages() * (PAGE_SIZE
>> 10);
2424 min_free_kbytes
= int_sqrt(lowmem_kbytes
* 16);
2425 if (min_free_kbytes
< 128)
2426 min_free_kbytes
= 128;
2427 if (min_free_kbytes
> 65536)
2428 min_free_kbytes
= 65536;
2429 setup_per_zone_pages_min();
2430 setup_per_zone_lowmem_reserve();
2433 module_init(init_per_zone_pages_min
)
2436 * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
2437 * that we can call two helper functions whenever min_free_kbytes
2440 int min_free_kbytes_sysctl_handler(ctl_table
*table
, int write
,
2441 struct file
*file
, void __user
*buffer
, size_t *length
, loff_t
*ppos
)
2443 proc_dointvec(table
, write
, file
, buffer
, length
, ppos
);
2444 setup_per_zone_pages_min();
2449 * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
2450 * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
2451 * whenever sysctl_lowmem_reserve_ratio changes.
2453 * The reserve ratio obviously has absolutely no relation with the
2454 * pages_min watermarks. The lowmem reserve ratio can only make sense
2455 * if in function of the boot time zone sizes.
2457 int lowmem_reserve_ratio_sysctl_handler(ctl_table
*table
, int write
,
2458 struct file
*file
, void __user
*buffer
, size_t *length
, loff_t
*ppos
)
2460 proc_dointvec_minmax(table
, write
, file
, buffer
, length
, ppos
);
2461 setup_per_zone_lowmem_reserve();
2465 __initdata
int hashdist
= HASHDIST_DEFAULT
;
2468 static int __init
set_hashdist(char *str
)
2472 hashdist
= simple_strtoul(str
, &str
, 0);
2475 __setup("hashdist=", set_hashdist
);
2479 * allocate a large system hash table from bootmem
2480 * - it is assumed that the hash table must contain an exact power-of-2
2481 * quantity of entries
2482 * - limit is the number of hash buckets, not the total allocation size
2484 void *__init
alloc_large_system_hash(const char *tablename
,
2485 unsigned long bucketsize
,
2486 unsigned long numentries
,
2489 unsigned int *_hash_shift
,
2490 unsigned int *_hash_mask
,
2491 unsigned long limit
)
2493 unsigned long long max
= limit
;
2494 unsigned long log2qty
, size
;
2497 /* allow the kernel cmdline to have a say */
2499 /* round applicable memory size up to nearest megabyte */
2500 numentries
= (flags
& HASH_HIGHMEM
) ? nr_all_pages
: nr_kernel_pages
;
2501 numentries
+= (1UL << (20 - PAGE_SHIFT
)) - 1;
2502 numentries
>>= 20 - PAGE_SHIFT
;
2503 numentries
<<= 20 - PAGE_SHIFT
;
2505 /* limit to 1 bucket per 2^scale bytes of low memory */
2506 if (scale
> PAGE_SHIFT
)
2507 numentries
>>= (scale
- PAGE_SHIFT
);
2509 numentries
<<= (PAGE_SHIFT
- scale
);
2511 /* rounded up to nearest power of 2 in size */
2512 numentries
= 1UL << (long_log2(numentries
) + 1);
2514 /* limit allocation size to 1/16 total memory by default */
2516 max
= ((unsigned long long)nr_all_pages
<< PAGE_SHIFT
) >> 4;
2517 do_div(max
, bucketsize
);
2520 if (numentries
> max
)
2523 log2qty
= long_log2(numentries
);
2526 size
= bucketsize
<< log2qty
;
2527 if (flags
& HASH_EARLY
)
2528 table
= alloc_bootmem(size
);
2530 table
= __vmalloc(size
, GFP_ATOMIC
, PAGE_KERNEL
);
2532 unsigned long order
;
2533 for (order
= 0; ((1UL << order
) << PAGE_SHIFT
) < size
; order
++)
2535 table
= (void*) __get_free_pages(GFP_ATOMIC
, order
);
2537 } while (!table
&& size
> PAGE_SIZE
&& --log2qty
);
2540 panic("Failed to allocate %s hash table\n", tablename
);
2542 printk("%s hash table entries: %d (order: %d, %lu bytes)\n",
2545 long_log2(size
) - PAGE_SHIFT
,
2549 *_hash_shift
= log2qty
;
2551 *_hash_mask
= (1 << log2qty
) - 1;