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 nodemask_t node_possible_map
= NODE_MASK_ALL
;
47 struct pglist_data
*pgdat_list
;
48 unsigned long totalram_pages
;
49 unsigned long totalhigh_pages
;
53 * results with 256, 32 in the lowmem_reserve sysctl:
54 * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
55 * 1G machine -> (16M dma, 784M normal, 224M high)
56 * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
57 * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
58 * HIGHMEM allocation will (224M+784M)/256 of ram reserved in ZONE_DMA
60 int sysctl_lowmem_reserve_ratio
[MAX_NR_ZONES
-1] = { 256, 32 };
62 EXPORT_SYMBOL(totalram_pages
);
63 EXPORT_SYMBOL(nr_swap_pages
);
66 * Used by page_zone() to look up the address of the struct zone whose
67 * id is encoded in the upper bits of page->flags
69 struct zone
*zone_table
[1 << (ZONES_SHIFT
+ NODES_SHIFT
)];
70 EXPORT_SYMBOL(zone_table
);
72 static char *zone_names
[MAX_NR_ZONES
] = { "DMA", "Normal", "HighMem" };
73 int min_free_kbytes
= 1024;
75 unsigned long __initdata nr_kernel_pages
;
76 unsigned long __initdata nr_all_pages
;
79 * Temporary debugging check for pages not lying within a given zone.
81 static int bad_range(struct zone
*zone
, struct page
*page
)
83 if (page_to_pfn(page
) >= zone
->zone_start_pfn
+ zone
->spanned_pages
)
85 if (page_to_pfn(page
) < zone
->zone_start_pfn
)
87 #ifdef CONFIG_HOLES_IN_ZONE
88 if (!pfn_valid(page_to_pfn(page
)))
91 if (zone
!= page_zone(page
))
96 static void bad_page(const char *function
, struct page
*page
)
98 printk(KERN_EMERG
"Bad page state at %s (in process '%s', page %p)\n",
99 function
, current
->comm
, page
);
100 printk(KERN_EMERG
"flags:0x%0*lx mapping:%p mapcount:%d count:%d\n",
101 (int)(2*sizeof(page_flags_t
)), (unsigned long)page
->flags
,
102 page
->mapping
, page_mapcount(page
), page_count(page
));
103 printk(KERN_EMERG
"Backtrace:\n");
105 printk(KERN_EMERG
"Trying to fix it up, but a reboot is needed\n");
106 page
->flags
&= ~(1 << PG_private
|
113 set_page_count(page
, 0);
114 reset_page_mapcount(page
);
115 page
->mapping
= NULL
;
116 tainted
|= TAINT_BAD_PAGE
;
119 #ifndef CONFIG_HUGETLB_PAGE
120 #define prep_compound_page(page, order) do { } while (0)
121 #define destroy_compound_page(page, order) do { } while (0)
124 * Higher-order pages are called "compound pages". They are structured thusly:
126 * The first PAGE_SIZE page is called the "head page".
128 * The remaining PAGE_SIZE pages are called "tail pages".
130 * All pages have PG_compound set. All pages have their ->private pointing at
131 * the head page (even the head page has this).
133 * The first tail page's ->mapping, if non-zero, holds the address of the
134 * compound page's put_page() function.
136 * The order of the allocation is stored in the first tail page's ->index
137 * This is only for debug at present. This usage means that zero-order pages
138 * may not be compound.
140 static void prep_compound_page(struct page
*page
, unsigned long order
)
143 int nr_pages
= 1 << order
;
145 page
[1].mapping
= NULL
;
146 page
[1].index
= order
;
147 for (i
= 0; i
< nr_pages
; i
++) {
148 struct page
*p
= page
+ i
;
151 p
->private = (unsigned long)page
;
155 static void destroy_compound_page(struct page
*page
, unsigned long order
)
158 int nr_pages
= 1 << order
;
160 if (!PageCompound(page
))
163 if (page
[1].index
!= order
)
164 bad_page(__FUNCTION__
, page
);
166 for (i
= 0; i
< nr_pages
; i
++) {
167 struct page
*p
= page
+ i
;
169 if (!PageCompound(p
))
170 bad_page(__FUNCTION__
, page
);
171 if (p
->private != (unsigned long)page
)
172 bad_page(__FUNCTION__
, page
);
173 ClearPageCompound(p
);
176 #endif /* CONFIG_HUGETLB_PAGE */
179 * function for dealing with page's order in buddy system.
180 * zone->lock is already acquired when we use these.
181 * So, we don't need atomic page->flags operations here.
183 static inline unsigned long page_order(struct page
*page
) {
184 return page
->private;
187 static inline void set_page_order(struct page
*page
, int order
) {
188 page
->private = order
;
189 __SetPagePrivate(page
);
192 static inline void rmv_page_order(struct page
*page
)
194 __ClearPagePrivate(page
);
199 * Locate the struct page for both the matching buddy in our
200 * pair (buddy1) and the combined O(n+1) page they form (page).
202 * 1) Any buddy B1 will have an order O twin B2 which satisfies
203 * the following equation:
205 * For example, if the starting buddy (buddy2) is #8 its order
207 * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
209 * 2) Any buddy B will have an order O+1 parent P which
210 * satisfies the following equation:
213 * Assumption: *_mem_map is contigious at least up to MAX_ORDER
215 static inline struct page
*
216 __page_find_buddy(struct page
*page
, unsigned long page_idx
, unsigned int order
)
218 unsigned long buddy_idx
= page_idx
^ (1 << order
);
220 return page
+ (buddy_idx
- page_idx
);
223 static inline unsigned long
224 __find_combined_index(unsigned long page_idx
, unsigned int order
)
226 return (page_idx
& ~(1 << order
));
230 * This function checks whether a page is free && is the buddy
231 * we can do coalesce a page and its buddy if
232 * (a) the buddy is free &&
233 * (b) the buddy is on the buddy system &&
234 * (c) a page and its buddy have the same order.
235 * for recording page's order, we use page->private and PG_private.
238 static inline int page_is_buddy(struct page
*page
, int order
)
240 if (PagePrivate(page
) &&
241 (page_order(page
) == order
) &&
242 !PageReserved(page
) &&
243 page_count(page
) == 0)
249 * Freeing function for a buddy system allocator.
251 * The concept of a buddy system is to maintain direct-mapped table
252 * (containing bit values) for memory blocks of various "orders".
253 * The bottom level table contains the map for the smallest allocatable
254 * units of memory (here, pages), and each level above it describes
255 * pairs of units from the levels below, hence, "buddies".
256 * At a high level, all that happens here is marking the table entry
257 * at the bottom level available, and propagating the changes upward
258 * as necessary, plus some accounting needed to play nicely with other
259 * parts of the VM system.
260 * At each level, we keep a list of pages, which are heads of continuous
261 * free pages of length of (1 << order) and marked with PG_Private.Page's
262 * order is recorded in page->private field.
263 * So when we are allocating or freeing one, we can derive the state of the
264 * other. That is, if we allocate a small block, and both were
265 * free, the remainder of the region must be split into blocks.
266 * If a block is freed, and its buddy is also free, then this
267 * triggers coalescing into a block of larger size.
272 static inline void __free_pages_bulk (struct page
*page
,
273 struct zone
*zone
, unsigned int order
)
275 unsigned long page_idx
;
276 int order_size
= 1 << order
;
279 destroy_compound_page(page
, order
);
281 page_idx
= page_to_pfn(page
) & ((1 << MAX_ORDER
) - 1);
283 BUG_ON(page_idx
& (order_size
- 1));
284 BUG_ON(bad_range(zone
, page
));
286 zone
->free_pages
+= order_size
;
287 while (order
< MAX_ORDER
-1) {
288 unsigned long combined_idx
;
289 struct free_area
*area
;
292 combined_idx
= __find_combined_index(page_idx
, order
);
293 buddy
= __page_find_buddy(page
, page_idx
, order
);
295 if (bad_range(zone
, buddy
))
297 if (!page_is_buddy(buddy
, order
))
298 break; /* Move the buddy up one level. */
299 list_del(&buddy
->lru
);
300 area
= zone
->free_area
+ order
;
302 rmv_page_order(buddy
);
303 page
= page
+ (combined_idx
- page_idx
);
304 page_idx
= combined_idx
;
307 set_page_order(page
, order
);
308 list_add(&page
->lru
, &zone
->free_area
[order
].free_list
);
309 zone
->free_area
[order
].nr_free
++;
312 static inline void free_pages_check(const char *function
, struct page
*page
)
314 if ( page_mapcount(page
) ||
315 page
->mapping
!= NULL
||
316 page_count(page
) != 0 ||
325 1 << PG_writeback
)))
326 bad_page(function
, page
);
328 ClearPageDirty(page
);
332 * Frees a list of pages.
333 * Assumes all pages on list are in same zone, and of same order.
334 * count is the number of pages to free, or 0 for all on the list.
336 * If the zone was previously in an "all pages pinned" state then look to
337 * see if this freeing clears that state.
339 * And clear the zone's pages_scanned counter, to hold off the "all pages are
340 * pinned" detection logic.
343 free_pages_bulk(struct zone
*zone
, int count
,
344 struct list_head
*list
, unsigned int order
)
347 struct page
*page
= NULL
;
350 spin_lock_irqsave(&zone
->lock
, flags
);
351 zone
->all_unreclaimable
= 0;
352 zone
->pages_scanned
= 0;
353 while (!list_empty(list
) && count
--) {
354 page
= list_entry(list
->prev
, struct page
, lru
);
355 /* have to delete it as __free_pages_bulk list manipulates */
356 list_del(&page
->lru
);
357 __free_pages_bulk(page
, zone
, order
);
360 spin_unlock_irqrestore(&zone
->lock
, flags
);
364 void __free_pages_ok(struct page
*page
, unsigned int order
)
369 arch_free_page(page
, order
);
371 mod_page_state(pgfree
, 1 << order
);
375 for (i
= 1 ; i
< (1 << order
) ; ++i
)
376 __put_page(page
+ i
);
379 for (i
= 0 ; i
< (1 << order
) ; ++i
)
380 free_pages_check(__FUNCTION__
, page
+ i
);
381 list_add(&page
->lru
, &list
);
382 kernel_map_pages(page
, 1<<order
, 0);
383 free_pages_bulk(page_zone(page
), 1, &list
, order
);
388 * The order of subdivision here is critical for the IO subsystem.
389 * Please do not alter this order without good reasons and regression
390 * testing. Specifically, as large blocks of memory are subdivided,
391 * the order in which smaller blocks are delivered depends on the order
392 * they're subdivided in this function. This is the primary factor
393 * influencing the order in which pages are delivered to the IO
394 * subsystem according to empirical testing, and this is also justified
395 * by considering the behavior of a buddy system containing a single
396 * large block of memory acted on by a series of small allocations.
397 * This behavior is a critical factor in sglist merging's success.
401 static inline struct page
*
402 expand(struct zone
*zone
, struct page
*page
,
403 int low
, int high
, struct free_area
*area
)
405 unsigned long size
= 1 << high
;
411 BUG_ON(bad_range(zone
, &page
[size
]));
412 list_add(&page
[size
].lru
, &area
->free_list
);
414 set_page_order(&page
[size
], high
);
419 void set_page_refs(struct page
*page
, int order
)
422 set_page_count(page
, 1);
427 * We need to reference all the pages for this order, otherwise if
428 * anyone accesses one of the pages with (get/put) it will be freed.
429 * - eg: access_process_vm()
431 for (i
= 0; i
< (1 << order
); i
++)
432 set_page_count(page
+ i
, 1);
433 #endif /* CONFIG_MMU */
437 * This page is about to be returned from the page allocator
439 static void prep_new_page(struct page
*page
, int order
)
441 if (page
->mapping
|| page_mapcount(page
) ||
450 1 << PG_writeback
)))
451 bad_page(__FUNCTION__
, page
);
453 page
->flags
&= ~(1 << PG_uptodate
| 1 << PG_error
|
454 1 << PG_referenced
| 1 << PG_arch_1
|
455 1 << PG_checked
| 1 << PG_mappedtodisk
);
457 set_page_refs(page
, order
);
458 kernel_map_pages(page
, 1 << order
, 1);
462 * Do the hard work of removing an element from the buddy allocator.
463 * Call me with the zone->lock already held.
465 static struct page
*__rmqueue(struct zone
*zone
, unsigned int order
)
467 struct free_area
* area
;
468 unsigned int current_order
;
471 for (current_order
= order
; current_order
< MAX_ORDER
; ++current_order
) {
472 area
= zone
->free_area
+ current_order
;
473 if (list_empty(&area
->free_list
))
476 page
= list_entry(area
->free_list
.next
, struct page
, lru
);
477 list_del(&page
->lru
);
478 rmv_page_order(page
);
480 zone
->free_pages
-= 1UL << order
;
481 return expand(zone
, page
, order
, current_order
, area
);
488 * Obtain a specified number of elements from the buddy allocator, all under
489 * a single hold of the lock, for efficiency. Add them to the supplied list.
490 * Returns the number of new pages which were placed at *list.
492 static int rmqueue_bulk(struct zone
*zone
, unsigned int order
,
493 unsigned long count
, struct list_head
*list
)
500 spin_lock_irqsave(&zone
->lock
, flags
);
501 for (i
= 0; i
< count
; ++i
) {
502 page
= __rmqueue(zone
, order
);
506 list_add_tail(&page
->lru
, list
);
508 spin_unlock_irqrestore(&zone
->lock
, flags
);
512 #if defined(CONFIG_PM) || defined(CONFIG_HOTPLUG_CPU)
513 static void __drain_pages(unsigned int cpu
)
518 for_each_zone(zone
) {
519 struct per_cpu_pageset
*pset
;
521 pset
= &zone
->pageset
[cpu
];
522 for (i
= 0; i
< ARRAY_SIZE(pset
->pcp
); i
++) {
523 struct per_cpu_pages
*pcp
;
526 pcp
->count
-= free_pages_bulk(zone
, pcp
->count
,
531 #endif /* CONFIG_PM || CONFIG_HOTPLUG_CPU */
535 void mark_free_pages(struct zone
*zone
)
537 unsigned long zone_pfn
, flags
;
539 struct list_head
*curr
;
541 if (!zone
->spanned_pages
)
544 spin_lock_irqsave(&zone
->lock
, flags
);
545 for (zone_pfn
= 0; zone_pfn
< zone
->spanned_pages
; ++zone_pfn
)
546 ClearPageNosaveFree(pfn_to_page(zone_pfn
+ zone
->zone_start_pfn
));
548 for (order
= MAX_ORDER
- 1; order
>= 0; --order
)
549 list_for_each(curr
, &zone
->free_area
[order
].free_list
) {
550 unsigned long start_pfn
, i
;
552 start_pfn
= page_to_pfn(list_entry(curr
, struct page
, lru
));
554 for (i
=0; i
< (1<<order
); i
++)
555 SetPageNosaveFree(pfn_to_page(start_pfn
+i
));
557 spin_unlock_irqrestore(&zone
->lock
, flags
);
561 * Spill all of this CPU's per-cpu pages back into the buddy allocator.
563 void drain_local_pages(void)
567 local_irq_save(flags
);
568 __drain_pages(smp_processor_id());
569 local_irq_restore(flags
);
571 #endif /* CONFIG_PM */
573 static void zone_statistics(struct zonelist
*zonelist
, struct zone
*z
)
578 pg_data_t
*pg
= z
->zone_pgdat
;
579 pg_data_t
*orig
= zonelist
->zones
[0]->zone_pgdat
;
580 struct per_cpu_pageset
*p
;
582 local_irq_save(flags
);
583 cpu
= smp_processor_id();
584 p
= &z
->pageset
[cpu
];
586 z
->pageset
[cpu
].numa_hit
++;
589 zonelist
->zones
[0]->pageset
[cpu
].numa_foreign
++;
591 if (pg
== NODE_DATA(numa_node_id()))
595 local_irq_restore(flags
);
600 * Free a 0-order page
602 static void FASTCALL(free_hot_cold_page(struct page
*page
, int cold
));
603 static void fastcall
free_hot_cold_page(struct page
*page
, int cold
)
605 struct zone
*zone
= page_zone(page
);
606 struct per_cpu_pages
*pcp
;
609 arch_free_page(page
, 0);
611 kernel_map_pages(page
, 1, 0);
612 inc_page_state(pgfree
);
614 page
->mapping
= NULL
;
615 free_pages_check(__FUNCTION__
, page
);
616 pcp
= &zone
->pageset
[get_cpu()].pcp
[cold
];
617 local_irq_save(flags
);
618 if (pcp
->count
>= pcp
->high
)
619 pcp
->count
-= free_pages_bulk(zone
, pcp
->batch
, &pcp
->list
, 0);
620 list_add(&page
->lru
, &pcp
->list
);
622 local_irq_restore(flags
);
626 void fastcall
free_hot_page(struct page
*page
)
628 free_hot_cold_page(page
, 0);
631 void fastcall
free_cold_page(struct page
*page
)
633 free_hot_cold_page(page
, 1);
636 static inline void prep_zero_page(struct page
*page
, int order
, unsigned int __nocast gfp_flags
)
640 BUG_ON((gfp_flags
& (__GFP_WAIT
| __GFP_HIGHMEM
)) == __GFP_HIGHMEM
);
641 for(i
= 0; i
< (1 << order
); i
++)
642 clear_highpage(page
+ i
);
646 * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
647 * we cheat by calling it from here, in the order > 0 path. Saves a branch
651 buffered_rmqueue(struct zone
*zone
, int order
, unsigned int __nocast gfp_flags
)
654 struct page
*page
= NULL
;
655 int cold
= !!(gfp_flags
& __GFP_COLD
);
658 struct per_cpu_pages
*pcp
;
660 pcp
= &zone
->pageset
[get_cpu()].pcp
[cold
];
661 local_irq_save(flags
);
662 if (pcp
->count
<= pcp
->low
)
663 pcp
->count
+= rmqueue_bulk(zone
, 0,
664 pcp
->batch
, &pcp
->list
);
666 page
= list_entry(pcp
->list
.next
, struct page
, lru
);
667 list_del(&page
->lru
);
670 local_irq_restore(flags
);
675 spin_lock_irqsave(&zone
->lock
, flags
);
676 page
= __rmqueue(zone
, order
);
677 spin_unlock_irqrestore(&zone
->lock
, flags
);
681 BUG_ON(bad_range(zone
, page
));
682 mod_page_state_zone(zone
, pgalloc
, 1 << order
);
683 prep_new_page(page
, order
);
685 if (gfp_flags
& __GFP_ZERO
)
686 prep_zero_page(page
, order
, gfp_flags
);
688 if (order
&& (gfp_flags
& __GFP_COMP
))
689 prep_compound_page(page
, order
);
695 * Return 1 if free pages are above 'mark'. This takes into account the order
698 int zone_watermark_ok(struct zone
*z
, int order
, unsigned long mark
,
699 int classzone_idx
, int can_try_harder
, int gfp_high
)
701 /* free_pages my go negative - that's OK */
702 long min
= mark
, free_pages
= z
->free_pages
- (1 << order
) + 1;
710 if (free_pages
<= min
+ z
->lowmem_reserve
[classzone_idx
])
712 for (o
= 0; o
< order
; o
++) {
713 /* At the next order, this order's pages become unavailable */
714 free_pages
-= z
->free_area
[o
].nr_free
<< o
;
716 /* Require fewer higher order pages to be free */
719 if (free_pages
<= min
)
726 * This is the 'heart' of the zoned buddy allocator.
728 struct page
* fastcall
729 __alloc_pages(unsigned int __nocast gfp_mask
, unsigned int order
,
730 struct zonelist
*zonelist
)
732 const int wait
= gfp_mask
& __GFP_WAIT
;
733 struct zone
**zones
, *z
;
735 struct reclaim_state reclaim_state
;
736 struct task_struct
*p
= current
;
741 int did_some_progress
;
743 might_sleep_if(wait
);
746 * The caller may dip into page reserves a bit more if the caller
747 * cannot run direct reclaim, or is the caller has realtime scheduling
750 can_try_harder
= (unlikely(rt_task(p
)) && !in_interrupt()) || !wait
;
752 zones
= zonelist
->zones
; /* the list of zones suitable for gfp_mask */
754 if (unlikely(zones
[0] == NULL
)) {
755 /* Should this ever happen?? */
759 classzone_idx
= zone_idx(zones
[0]);
762 /* Go through the zonelist once, looking for a zone with enough free */
763 for (i
= 0; (z
= zones
[i
]) != NULL
; i
++) {
765 if (!zone_watermark_ok(z
, order
, z
->pages_low
,
766 classzone_idx
, 0, 0))
769 if (!cpuset_zone_allowed(z
))
772 page
= buffered_rmqueue(z
, order
, gfp_mask
);
777 for (i
= 0; (z
= zones
[i
]) != NULL
; i
++)
778 wakeup_kswapd(z
, order
);
781 * Go through the zonelist again. Let __GFP_HIGH and allocations
782 * coming from realtime tasks to go deeper into reserves
784 * This is the last chance, in general, before the goto nopage.
785 * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
787 for (i
= 0; (z
= zones
[i
]) != NULL
; i
++) {
788 if (!zone_watermark_ok(z
, order
, z
->pages_min
,
789 classzone_idx
, can_try_harder
,
790 gfp_mask
& __GFP_HIGH
))
793 if (wait
&& !cpuset_zone_allowed(z
))
796 page
= buffered_rmqueue(z
, order
, gfp_mask
);
801 /* This allocation should allow future memory freeing. */
802 if (((p
->flags
& PF_MEMALLOC
) || unlikely(test_thread_flag(TIF_MEMDIE
))) && !in_interrupt()) {
803 /* go through the zonelist yet again, ignoring mins */
804 for (i
= 0; (z
= zones
[i
]) != NULL
; i
++) {
805 if (!cpuset_zone_allowed(z
))
807 page
= buffered_rmqueue(z
, order
, gfp_mask
);
814 /* Atomic allocations - we can't balance anything */
821 /* We now go into synchronous reclaim */
822 p
->flags
|= PF_MEMALLOC
;
823 reclaim_state
.reclaimed_slab
= 0;
824 p
->reclaim_state
= &reclaim_state
;
826 did_some_progress
= try_to_free_pages(zones
, gfp_mask
, order
);
828 p
->reclaim_state
= NULL
;
829 p
->flags
&= ~PF_MEMALLOC
;
833 if (likely(did_some_progress
)) {
835 * Go through the zonelist yet one more time, keep
836 * very high watermark here, this is only to catch
837 * a parallel oom killing, we must fail if we're still
838 * under heavy pressure.
840 for (i
= 0; (z
= zones
[i
]) != NULL
; i
++) {
841 if (!zone_watermark_ok(z
, order
, z
->pages_min
,
842 classzone_idx
, can_try_harder
,
843 gfp_mask
& __GFP_HIGH
))
846 if (!cpuset_zone_allowed(z
))
849 page
= buffered_rmqueue(z
, order
, gfp_mask
);
853 } else if ((gfp_mask
& __GFP_FS
) && !(gfp_mask
& __GFP_NORETRY
)) {
855 * Go through the zonelist yet one more time, keep
856 * very high watermark here, this is only to catch
857 * a parallel oom killing, we must fail if we're still
858 * under heavy pressure.
860 for (i
= 0; (z
= zones
[i
]) != NULL
; i
++) {
861 if (!zone_watermark_ok(z
, order
, z
->pages_high
,
862 classzone_idx
, 0, 0))
865 if (!cpuset_zone_allowed(z
))
868 page
= buffered_rmqueue(z
, order
, gfp_mask
);
873 out_of_memory(gfp_mask
);
878 * Don't let big-order allocations loop unless the caller explicitly
879 * requests that. Wait for some write requests to complete then retry.
881 * In this implementation, __GFP_REPEAT means __GFP_NOFAIL for order
882 * <= 3, but that may not be true in other implementations.
885 if (!(gfp_mask
& __GFP_NORETRY
)) {
886 if ((order
<= 3) || (gfp_mask
& __GFP_REPEAT
))
888 if (gfp_mask
& __GFP_NOFAIL
)
892 blk_congestion_wait(WRITE
, HZ
/50);
897 if (!(gfp_mask
& __GFP_NOWARN
) && printk_ratelimit()) {
898 printk(KERN_WARNING
"%s: page allocation failure."
899 " order:%d, mode:0x%x\n",
900 p
->comm
, order
, gfp_mask
);
905 zone_statistics(zonelist
, z
);
909 EXPORT_SYMBOL(__alloc_pages
);
912 * Common helper functions.
914 fastcall
unsigned long __get_free_pages(unsigned int __nocast gfp_mask
, unsigned int order
)
917 page
= alloc_pages(gfp_mask
, order
);
920 return (unsigned long) page_address(page
);
923 EXPORT_SYMBOL(__get_free_pages
);
925 fastcall
unsigned long get_zeroed_page(unsigned int __nocast gfp_mask
)
930 * get_zeroed_page() returns a 32-bit address, which cannot represent
933 BUG_ON(gfp_mask
& __GFP_HIGHMEM
);
935 page
= alloc_pages(gfp_mask
| __GFP_ZERO
, 0);
937 return (unsigned long) page_address(page
);
941 EXPORT_SYMBOL(get_zeroed_page
);
943 void __pagevec_free(struct pagevec
*pvec
)
945 int i
= pagevec_count(pvec
);
948 free_hot_cold_page(pvec
->pages
[i
], pvec
->cold
);
951 fastcall
void __free_pages(struct page
*page
, unsigned int order
)
953 if (!PageReserved(page
) && put_page_testzero(page
)) {
957 __free_pages_ok(page
, order
);
961 EXPORT_SYMBOL(__free_pages
);
963 fastcall
void free_pages(unsigned long addr
, unsigned int order
)
966 BUG_ON(!virt_addr_valid((void *)addr
));
967 __free_pages(virt_to_page((void *)addr
), order
);
971 EXPORT_SYMBOL(free_pages
);
974 * Total amount of free (allocatable) RAM:
976 unsigned int nr_free_pages(void)
978 unsigned int sum
= 0;
982 sum
+= zone
->free_pages
;
987 EXPORT_SYMBOL(nr_free_pages
);
990 unsigned int nr_free_pages_pgdat(pg_data_t
*pgdat
)
992 unsigned int i
, sum
= 0;
994 for (i
= 0; i
< MAX_NR_ZONES
; i
++)
995 sum
+= pgdat
->node_zones
[i
].free_pages
;
1001 static unsigned int nr_free_zone_pages(int offset
)
1004 unsigned int sum
= 0;
1006 for_each_pgdat(pgdat
) {
1007 struct zonelist
*zonelist
= pgdat
->node_zonelists
+ offset
;
1008 struct zone
**zonep
= zonelist
->zones
;
1011 for (zone
= *zonep
++; zone
; zone
= *zonep
++) {
1012 unsigned long size
= zone
->present_pages
;
1013 unsigned long high
= zone
->pages_high
;
1023 * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
1025 unsigned int nr_free_buffer_pages(void)
1027 return nr_free_zone_pages(GFP_USER
& GFP_ZONEMASK
);
1031 * Amount of free RAM allocatable within all zones
1033 unsigned int nr_free_pagecache_pages(void)
1035 return nr_free_zone_pages(GFP_HIGHUSER
& GFP_ZONEMASK
);
1038 #ifdef CONFIG_HIGHMEM
1039 unsigned int nr_free_highpages (void)
1042 unsigned int pages
= 0;
1044 for_each_pgdat(pgdat
)
1045 pages
+= pgdat
->node_zones
[ZONE_HIGHMEM
].free_pages
;
1052 static void show_node(struct zone
*zone
)
1054 printk("Node %d ", zone
->zone_pgdat
->node_id
);
1057 #define show_node(zone) do { } while (0)
1061 * Accumulate the page_state information across all CPUs.
1062 * The result is unavoidably approximate - it can change
1063 * during and after execution of this function.
1065 static DEFINE_PER_CPU(struct page_state
, page_states
) = {0};
1067 atomic_t nr_pagecache
= ATOMIC_INIT(0);
1068 EXPORT_SYMBOL(nr_pagecache
);
1070 DEFINE_PER_CPU(long, nr_pagecache_local
) = 0;
1073 void __get_page_state(struct page_state
*ret
, int nr
)
1077 memset(ret
, 0, sizeof(*ret
));
1079 cpu
= first_cpu(cpu_online_map
);
1080 while (cpu
< NR_CPUS
) {
1081 unsigned long *in
, *out
, off
;
1083 in
= (unsigned long *)&per_cpu(page_states
, cpu
);
1085 cpu
= next_cpu(cpu
, cpu_online_map
);
1088 prefetch(&per_cpu(page_states
, cpu
));
1090 out
= (unsigned long *)ret
;
1091 for (off
= 0; off
< nr
; off
++)
1096 void get_page_state(struct page_state
*ret
)
1100 nr
= offsetof(struct page_state
, GET_PAGE_STATE_LAST
);
1101 nr
/= sizeof(unsigned long);
1103 __get_page_state(ret
, nr
+ 1);
1106 void get_full_page_state(struct page_state
*ret
)
1108 __get_page_state(ret
, sizeof(*ret
) / sizeof(unsigned long));
1111 unsigned long __read_page_state(unsigned offset
)
1113 unsigned long ret
= 0;
1116 for_each_online_cpu(cpu
) {
1119 in
= (unsigned long)&per_cpu(page_states
, cpu
) + offset
;
1120 ret
+= *((unsigned long *)in
);
1125 void __mod_page_state(unsigned offset
, unsigned long delta
)
1127 unsigned long flags
;
1130 local_irq_save(flags
);
1131 ptr
= &__get_cpu_var(page_states
);
1132 *(unsigned long*)(ptr
+ offset
) += delta
;
1133 local_irq_restore(flags
);
1136 EXPORT_SYMBOL(__mod_page_state
);
1138 void __get_zone_counts(unsigned long *active
, unsigned long *inactive
,
1139 unsigned long *free
, struct pglist_data
*pgdat
)
1141 struct zone
*zones
= pgdat
->node_zones
;
1147 for (i
= 0; i
< MAX_NR_ZONES
; i
++) {
1148 *active
+= zones
[i
].nr_active
;
1149 *inactive
+= zones
[i
].nr_inactive
;
1150 *free
+= zones
[i
].free_pages
;
1154 void get_zone_counts(unsigned long *active
,
1155 unsigned long *inactive
, unsigned long *free
)
1157 struct pglist_data
*pgdat
;
1162 for_each_pgdat(pgdat
) {
1163 unsigned long l
, m
, n
;
1164 __get_zone_counts(&l
, &m
, &n
, pgdat
);
1171 void si_meminfo(struct sysinfo
*val
)
1173 val
->totalram
= totalram_pages
;
1175 val
->freeram
= nr_free_pages();
1176 val
->bufferram
= nr_blockdev_pages();
1177 #ifdef CONFIG_HIGHMEM
1178 val
->totalhigh
= totalhigh_pages
;
1179 val
->freehigh
= nr_free_highpages();
1184 val
->mem_unit
= PAGE_SIZE
;
1187 EXPORT_SYMBOL(si_meminfo
);
1190 void si_meminfo_node(struct sysinfo
*val
, int nid
)
1192 pg_data_t
*pgdat
= NODE_DATA(nid
);
1194 val
->totalram
= pgdat
->node_present_pages
;
1195 val
->freeram
= nr_free_pages_pgdat(pgdat
);
1196 val
->totalhigh
= pgdat
->node_zones
[ZONE_HIGHMEM
].present_pages
;
1197 val
->freehigh
= pgdat
->node_zones
[ZONE_HIGHMEM
].free_pages
;
1198 val
->mem_unit
= PAGE_SIZE
;
1202 #define K(x) ((x) << (PAGE_SHIFT-10))
1205 * Show free area list (used inside shift_scroll-lock stuff)
1206 * We also calculate the percentage fragmentation. We do this by counting the
1207 * memory on each free list with the exception of the first item on the list.
1209 void show_free_areas(void)
1211 struct page_state ps
;
1212 int cpu
, temperature
;
1213 unsigned long active
;
1214 unsigned long inactive
;
1218 for_each_zone(zone
) {
1220 printk("%s per-cpu:", zone
->name
);
1222 if (!zone
->present_pages
) {
1228 for (cpu
= 0; cpu
< NR_CPUS
; ++cpu
) {
1229 struct per_cpu_pageset
*pageset
;
1231 if (!cpu_possible(cpu
))
1234 pageset
= zone
->pageset
+ cpu
;
1236 for (temperature
= 0; temperature
< 2; temperature
++)
1237 printk("cpu %d %s: low %d, high %d, batch %d\n",
1239 temperature
? "cold" : "hot",
1240 pageset
->pcp
[temperature
].low
,
1241 pageset
->pcp
[temperature
].high
,
1242 pageset
->pcp
[temperature
].batch
);
1246 get_page_state(&ps
);
1247 get_zone_counts(&active
, &inactive
, &free
);
1249 printk("\nFree pages: %11ukB (%ukB HighMem)\n",
1251 K(nr_free_highpages()));
1253 printk("Active:%lu inactive:%lu dirty:%lu writeback:%lu "
1254 "unstable:%lu free:%u slab:%lu mapped:%lu pagetables:%lu\n",
1263 ps
.nr_page_table_pages
);
1265 for_each_zone(zone
) {
1277 " pages_scanned:%lu"
1278 " all_unreclaimable? %s"
1281 K(zone
->free_pages
),
1284 K(zone
->pages_high
),
1286 K(zone
->nr_inactive
),
1287 K(zone
->present_pages
),
1288 zone
->pages_scanned
,
1289 (zone
->all_unreclaimable
? "yes" : "no")
1291 printk("lowmem_reserve[]:");
1292 for (i
= 0; i
< MAX_NR_ZONES
; i
++)
1293 printk(" %lu", zone
->lowmem_reserve
[i
]);
1297 for_each_zone(zone
) {
1298 unsigned long nr
, flags
, order
, total
= 0;
1301 printk("%s: ", zone
->name
);
1302 if (!zone
->present_pages
) {
1307 spin_lock_irqsave(&zone
->lock
, flags
);
1308 for (order
= 0; order
< MAX_ORDER
; order
++) {
1309 nr
= zone
->free_area
[order
].nr_free
;
1310 total
+= nr
<< order
;
1311 printk("%lu*%lukB ", nr
, K(1UL) << order
);
1313 spin_unlock_irqrestore(&zone
->lock
, flags
);
1314 printk("= %lukB\n", K(total
));
1317 show_swap_cache_info();
1321 * Builds allocation fallback zone lists.
1323 static int __init
build_zonelists_node(pg_data_t
*pgdat
, struct zonelist
*zonelist
, int j
, int k
)
1330 zone
= pgdat
->node_zones
+ ZONE_HIGHMEM
;
1331 if (zone
->present_pages
) {
1332 #ifndef CONFIG_HIGHMEM
1335 zonelist
->zones
[j
++] = zone
;
1338 zone
= pgdat
->node_zones
+ ZONE_NORMAL
;
1339 if (zone
->present_pages
)
1340 zonelist
->zones
[j
++] = zone
;
1342 zone
= pgdat
->node_zones
+ ZONE_DMA
;
1343 if (zone
->present_pages
)
1344 zonelist
->zones
[j
++] = zone
;
1351 #define MAX_NODE_LOAD (num_online_nodes())
1352 static int __initdata node_load
[MAX_NUMNODES
];
1354 * find_next_best_node - find the next node that should appear in a given
1355 * node's fallback list
1356 * @node: node whose fallback list we're appending
1357 * @used_node_mask: nodemask_t of already used nodes
1359 * We use a number of factors to determine which is the next node that should
1360 * appear on a given node's fallback list. The node should not have appeared
1361 * already in @node's fallback list, and it should be the next closest node
1362 * according to the distance array (which contains arbitrary distance values
1363 * from each node to each node in the system), and should also prefer nodes
1364 * with no CPUs, since presumably they'll have very little allocation pressure
1365 * on them otherwise.
1366 * It returns -1 if no node is found.
1368 static int __init
find_next_best_node(int node
, nodemask_t
*used_node_mask
)
1371 int min_val
= INT_MAX
;
1374 for_each_online_node(i
) {
1377 /* Start from local node */
1378 n
= (node
+i
) % num_online_nodes();
1380 /* Don't want a node to appear more than once */
1381 if (node_isset(n
, *used_node_mask
))
1384 /* Use the local node if we haven't already */
1385 if (!node_isset(node
, *used_node_mask
)) {
1390 /* Use the distance array to find the distance */
1391 val
= node_distance(node
, n
);
1393 /* Give preference to headless and unused nodes */
1394 tmp
= node_to_cpumask(n
);
1395 if (!cpus_empty(tmp
))
1396 val
+= PENALTY_FOR_NODE_WITH_CPUS
;
1398 /* Slight preference for less loaded node */
1399 val
*= (MAX_NODE_LOAD
*MAX_NUMNODES
);
1400 val
+= node_load
[n
];
1402 if (val
< min_val
) {
1409 node_set(best_node
, *used_node_mask
);
1414 static void __init
build_zonelists(pg_data_t
*pgdat
)
1416 int i
, j
, k
, node
, local_node
;
1417 int prev_node
, load
;
1418 struct zonelist
*zonelist
;
1419 nodemask_t used_mask
;
1421 /* initialize zonelists */
1422 for (i
= 0; i
< GFP_ZONETYPES
; i
++) {
1423 zonelist
= pgdat
->node_zonelists
+ i
;
1424 zonelist
->zones
[0] = NULL
;
1427 /* NUMA-aware ordering of nodes */
1428 local_node
= pgdat
->node_id
;
1429 load
= num_online_nodes();
1430 prev_node
= local_node
;
1431 nodes_clear(used_mask
);
1432 while ((node
= find_next_best_node(local_node
, &used_mask
)) >= 0) {
1434 * We don't want to pressure a particular node.
1435 * So adding penalty to the first node in same
1436 * distance group to make it round-robin.
1438 if (node_distance(local_node
, node
) !=
1439 node_distance(local_node
, prev_node
))
1440 node_load
[node
] += load
;
1443 for (i
= 0; i
< GFP_ZONETYPES
; i
++) {
1444 zonelist
= pgdat
->node_zonelists
+ i
;
1445 for (j
= 0; zonelist
->zones
[j
] != NULL
; j
++);
1448 if (i
& __GFP_HIGHMEM
)
1453 j
= build_zonelists_node(NODE_DATA(node
), zonelist
, j
, k
);
1454 zonelist
->zones
[j
] = NULL
;
1459 #else /* CONFIG_NUMA */
1461 static void __init
build_zonelists(pg_data_t
*pgdat
)
1463 int i
, j
, k
, node
, local_node
;
1465 local_node
= pgdat
->node_id
;
1466 for (i
= 0; i
< GFP_ZONETYPES
; i
++) {
1467 struct zonelist
*zonelist
;
1469 zonelist
= pgdat
->node_zonelists
+ i
;
1473 if (i
& __GFP_HIGHMEM
)
1478 j
= build_zonelists_node(pgdat
, zonelist
, j
, k
);
1480 * Now we build the zonelist so that it contains the zones
1481 * of all the other nodes.
1482 * We don't want to pressure a particular node, so when
1483 * building the zones for node N, we make sure that the
1484 * zones coming right after the local ones are those from
1485 * node N+1 (modulo N)
1487 for (node
= local_node
+ 1; node
< MAX_NUMNODES
; node
++) {
1488 if (!node_online(node
))
1490 j
= build_zonelists_node(NODE_DATA(node
), zonelist
, j
, k
);
1492 for (node
= 0; node
< local_node
; node
++) {
1493 if (!node_online(node
))
1495 j
= build_zonelists_node(NODE_DATA(node
), zonelist
, j
, k
);
1498 zonelist
->zones
[j
] = NULL
;
1502 #endif /* CONFIG_NUMA */
1504 void __init
build_all_zonelists(void)
1508 for_each_online_node(i
)
1509 build_zonelists(NODE_DATA(i
));
1510 printk("Built %i zonelists\n", num_online_nodes());
1511 cpuset_init_current_mems_allowed();
1515 * Helper functions to size the waitqueue hash table.
1516 * Essentially these want to choose hash table sizes sufficiently
1517 * large so that collisions trying to wait on pages are rare.
1518 * But in fact, the number of active page waitqueues on typical
1519 * systems is ridiculously low, less than 200. So this is even
1520 * conservative, even though it seems large.
1522 * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
1523 * waitqueues, i.e. the size of the waitq table given the number of pages.
1525 #define PAGES_PER_WAITQUEUE 256
1527 static inline unsigned long wait_table_size(unsigned long pages
)
1529 unsigned long size
= 1;
1531 pages
/= PAGES_PER_WAITQUEUE
;
1533 while (size
< pages
)
1537 * Once we have dozens or even hundreds of threads sleeping
1538 * on IO we've got bigger problems than wait queue collision.
1539 * Limit the size of the wait table to a reasonable size.
1541 size
= min(size
, 4096UL);
1543 return max(size
, 4UL);
1547 * This is an integer logarithm so that shifts can be used later
1548 * to extract the more random high bits from the multiplicative
1549 * hash function before the remainder is taken.
1551 static inline unsigned long wait_table_bits(unsigned long size
)
1556 #define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
1558 static void __init
calculate_zone_totalpages(struct pglist_data
*pgdat
,
1559 unsigned long *zones_size
, unsigned long *zholes_size
)
1561 unsigned long realtotalpages
, totalpages
= 0;
1564 for (i
= 0; i
< MAX_NR_ZONES
; i
++)
1565 totalpages
+= zones_size
[i
];
1566 pgdat
->node_spanned_pages
= totalpages
;
1568 realtotalpages
= totalpages
;
1570 for (i
= 0; i
< MAX_NR_ZONES
; i
++)
1571 realtotalpages
-= zholes_size
[i
];
1572 pgdat
->node_present_pages
= realtotalpages
;
1573 printk(KERN_DEBUG
"On node %d totalpages: %lu\n", pgdat
->node_id
, realtotalpages
);
1578 * Initially all pages are reserved - free ones are freed
1579 * up by free_all_bootmem() once the early boot process is
1580 * done. Non-atomic initialization, single-pass.
1582 void __init
memmap_init_zone(unsigned long size
, int nid
, unsigned long zone
,
1583 unsigned long start_pfn
)
1585 struct page
*start
= pfn_to_page(start_pfn
);
1588 for (page
= start
; page
< (start
+ size
); page
++) {
1589 set_page_zone(page
, NODEZONE(nid
, zone
));
1590 set_page_count(page
, 0);
1591 reset_page_mapcount(page
);
1592 SetPageReserved(page
);
1593 INIT_LIST_HEAD(&page
->lru
);
1594 #ifdef WANT_PAGE_VIRTUAL
1595 /* The shift won't overflow because ZONE_NORMAL is below 4G. */
1596 if (!is_highmem_idx(zone
))
1597 set_page_address(page
, __va(start_pfn
<< PAGE_SHIFT
));
1603 void zone_init_free_lists(struct pglist_data
*pgdat
, struct zone
*zone
,
1607 for (order
= 0; order
< MAX_ORDER
; order
++) {
1608 INIT_LIST_HEAD(&zone
->free_area
[order
].free_list
);
1609 zone
->free_area
[order
].nr_free
= 0;
1613 #ifndef __HAVE_ARCH_MEMMAP_INIT
1614 #define memmap_init(size, nid, zone, start_pfn) \
1615 memmap_init_zone((size), (nid), (zone), (start_pfn))
1619 * Set up the zone data structures:
1620 * - mark all pages reserved
1621 * - mark all memory queues empty
1622 * - clear the memory bitmaps
1624 static void __init
free_area_init_core(struct pglist_data
*pgdat
,
1625 unsigned long *zones_size
, unsigned long *zholes_size
)
1628 const unsigned long zone_required_alignment
= 1UL << (MAX_ORDER
-1);
1629 int cpu
, nid
= pgdat
->node_id
;
1630 unsigned long zone_start_pfn
= pgdat
->node_start_pfn
;
1632 pgdat
->nr_zones
= 0;
1633 init_waitqueue_head(&pgdat
->kswapd_wait
);
1634 pgdat
->kswapd_max_order
= 0;
1636 for (j
= 0; j
< MAX_NR_ZONES
; j
++) {
1637 struct zone
*zone
= pgdat
->node_zones
+ j
;
1638 unsigned long size
, realsize
;
1639 unsigned long batch
;
1641 zone_table
[NODEZONE(nid
, j
)] = zone
;
1642 realsize
= size
= zones_size
[j
];
1644 realsize
-= zholes_size
[j
];
1646 if (j
== ZONE_DMA
|| j
== ZONE_NORMAL
)
1647 nr_kernel_pages
+= realsize
;
1648 nr_all_pages
+= realsize
;
1650 zone
->spanned_pages
= size
;
1651 zone
->present_pages
= realsize
;
1652 zone
->name
= zone_names
[j
];
1653 spin_lock_init(&zone
->lock
);
1654 spin_lock_init(&zone
->lru_lock
);
1655 zone
->zone_pgdat
= pgdat
;
1656 zone
->free_pages
= 0;
1658 zone
->temp_priority
= zone
->prev_priority
= DEF_PRIORITY
;
1661 * The per-cpu-pages pools are set to around 1000th of the
1662 * size of the zone. But no more than 1/4 of a meg - there's
1663 * no point in going beyond the size of L2 cache.
1665 * OK, so we don't know how big the cache is. So guess.
1667 batch
= zone
->present_pages
/ 1024;
1668 if (batch
* PAGE_SIZE
> 256 * 1024)
1669 batch
= (256 * 1024) / PAGE_SIZE
;
1670 batch
/= 4; /* We effectively *= 4 below */
1674 for (cpu
= 0; cpu
< NR_CPUS
; cpu
++) {
1675 struct per_cpu_pages
*pcp
;
1677 pcp
= &zone
->pageset
[cpu
].pcp
[0]; /* hot */
1679 pcp
->low
= 2 * batch
;
1680 pcp
->high
= 6 * batch
;
1681 pcp
->batch
= 1 * batch
;
1682 INIT_LIST_HEAD(&pcp
->list
);
1684 pcp
= &zone
->pageset
[cpu
].pcp
[1]; /* cold */
1687 pcp
->high
= 2 * batch
;
1688 pcp
->batch
= 1 * batch
;
1689 INIT_LIST_HEAD(&pcp
->list
);
1691 printk(KERN_DEBUG
" %s zone: %lu pages, LIFO batch:%lu\n",
1692 zone_names
[j
], realsize
, batch
);
1693 INIT_LIST_HEAD(&zone
->active_list
);
1694 INIT_LIST_HEAD(&zone
->inactive_list
);
1695 zone
->nr_scan_active
= 0;
1696 zone
->nr_scan_inactive
= 0;
1697 zone
->nr_active
= 0;
1698 zone
->nr_inactive
= 0;
1703 * The per-page waitqueue mechanism uses hashed waitqueues
1706 zone
->wait_table_size
= wait_table_size(size
);
1707 zone
->wait_table_bits
=
1708 wait_table_bits(zone
->wait_table_size
);
1709 zone
->wait_table
= (wait_queue_head_t
*)
1710 alloc_bootmem_node(pgdat
, zone
->wait_table_size
1711 * sizeof(wait_queue_head_t
));
1713 for(i
= 0; i
< zone
->wait_table_size
; ++i
)
1714 init_waitqueue_head(zone
->wait_table
+ i
);
1716 pgdat
->nr_zones
= j
+1;
1718 zone
->zone_mem_map
= pfn_to_page(zone_start_pfn
);
1719 zone
->zone_start_pfn
= zone_start_pfn
;
1721 if ((zone_start_pfn
) & (zone_required_alignment
-1))
1722 printk(KERN_CRIT
"BUG: wrong zone alignment, it will crash\n");
1724 memmap_init(size
, nid
, j
, zone_start_pfn
);
1726 zone_start_pfn
+= size
;
1728 zone_init_free_lists(pgdat
, zone
, zone
->spanned_pages
);
1732 static void __init
alloc_node_mem_map(struct pglist_data
*pgdat
)
1736 /* Skip empty nodes */
1737 if (!pgdat
->node_spanned_pages
)
1740 /* ia64 gets its own node_mem_map, before this, without bootmem */
1741 if (!pgdat
->node_mem_map
) {
1742 size
= (pgdat
->node_spanned_pages
+ 1) * sizeof(struct page
);
1743 pgdat
->node_mem_map
= alloc_bootmem_node(pgdat
, size
);
1745 #ifndef CONFIG_DISCONTIGMEM
1747 * With no DISCONTIG, the global mem_map is just set as node 0's
1749 if (pgdat
== NODE_DATA(0))
1750 mem_map
= NODE_DATA(0)->node_mem_map
;
1754 void __init
free_area_init_node(int nid
, struct pglist_data
*pgdat
,
1755 unsigned long *zones_size
, unsigned long node_start_pfn
,
1756 unsigned long *zholes_size
)
1758 pgdat
->node_id
= nid
;
1759 pgdat
->node_start_pfn
= node_start_pfn
;
1760 calculate_zone_totalpages(pgdat
, zones_size
, zholes_size
);
1762 alloc_node_mem_map(pgdat
);
1764 free_area_init_core(pgdat
, zones_size
, zholes_size
);
1767 #ifndef CONFIG_DISCONTIGMEM
1768 static bootmem_data_t contig_bootmem_data
;
1769 struct pglist_data contig_page_data
= { .bdata
= &contig_bootmem_data
};
1771 EXPORT_SYMBOL(contig_page_data
);
1773 void __init
free_area_init(unsigned long *zones_size
)
1775 free_area_init_node(0, &contig_page_data
, zones_size
,
1776 __pa(PAGE_OFFSET
) >> PAGE_SHIFT
, NULL
);
1780 #ifdef CONFIG_PROC_FS
1782 #include <linux/seq_file.h>
1784 static void *frag_start(struct seq_file
*m
, loff_t
*pos
)
1789 for (pgdat
= pgdat_list
; pgdat
&& node
; pgdat
= pgdat
->pgdat_next
)
1795 static void *frag_next(struct seq_file
*m
, void *arg
, loff_t
*pos
)
1797 pg_data_t
*pgdat
= (pg_data_t
*)arg
;
1800 return pgdat
->pgdat_next
;
1803 static void frag_stop(struct seq_file
*m
, void *arg
)
1808 * This walks the free areas for each zone.
1810 static int frag_show(struct seq_file
*m
, void *arg
)
1812 pg_data_t
*pgdat
= (pg_data_t
*)arg
;
1814 struct zone
*node_zones
= pgdat
->node_zones
;
1815 unsigned long flags
;
1818 for (zone
= node_zones
; zone
- node_zones
< MAX_NR_ZONES
; ++zone
) {
1819 if (!zone
->present_pages
)
1822 spin_lock_irqsave(&zone
->lock
, flags
);
1823 seq_printf(m
, "Node %d, zone %8s ", pgdat
->node_id
, zone
->name
);
1824 for (order
= 0; order
< MAX_ORDER
; ++order
)
1825 seq_printf(m
, "%6lu ", zone
->free_area
[order
].nr_free
);
1826 spin_unlock_irqrestore(&zone
->lock
, flags
);
1832 struct seq_operations fragmentation_op
= {
1833 .start
= frag_start
,
1839 static char *vmstat_text
[] = {
1843 "nr_page_table_pages",
1868 "pgscan_kswapd_high",
1869 "pgscan_kswapd_normal",
1871 "pgscan_kswapd_dma",
1872 "pgscan_direct_high",
1873 "pgscan_direct_normal",
1874 "pgscan_direct_dma",
1879 "kswapd_inodesteal",
1886 static void *vmstat_start(struct seq_file
*m
, loff_t
*pos
)
1888 struct page_state
*ps
;
1890 if (*pos
>= ARRAY_SIZE(vmstat_text
))
1893 ps
= kmalloc(sizeof(*ps
), GFP_KERNEL
);
1896 return ERR_PTR(-ENOMEM
);
1897 get_full_page_state(ps
);
1898 ps
->pgpgin
/= 2; /* sectors -> kbytes */
1900 return (unsigned long *)ps
+ *pos
;
1903 static void *vmstat_next(struct seq_file
*m
, void *arg
, loff_t
*pos
)
1906 if (*pos
>= ARRAY_SIZE(vmstat_text
))
1908 return (unsigned long *)m
->private + *pos
;
1911 static int vmstat_show(struct seq_file
*m
, void *arg
)
1913 unsigned long *l
= arg
;
1914 unsigned long off
= l
- (unsigned long *)m
->private;
1916 seq_printf(m
, "%s %lu\n", vmstat_text
[off
], *l
);
1920 static void vmstat_stop(struct seq_file
*m
, void *arg
)
1926 struct seq_operations vmstat_op
= {
1927 .start
= vmstat_start
,
1928 .next
= vmstat_next
,
1929 .stop
= vmstat_stop
,
1930 .show
= vmstat_show
,
1933 #endif /* CONFIG_PROC_FS */
1935 #ifdef CONFIG_HOTPLUG_CPU
1936 static int page_alloc_cpu_notify(struct notifier_block
*self
,
1937 unsigned long action
, void *hcpu
)
1939 int cpu
= (unsigned long)hcpu
;
1941 unsigned long *src
, *dest
;
1943 if (action
== CPU_DEAD
) {
1946 /* Drain local pagecache count. */
1947 count
= &per_cpu(nr_pagecache_local
, cpu
);
1948 atomic_add(*count
, &nr_pagecache
);
1950 local_irq_disable();
1953 /* Add dead cpu's page_states to our own. */
1954 dest
= (unsigned long *)&__get_cpu_var(page_states
);
1955 src
= (unsigned long *)&per_cpu(page_states
, cpu
);
1957 for (i
= 0; i
< sizeof(struct page_state
)/sizeof(unsigned long);
1967 #endif /* CONFIG_HOTPLUG_CPU */
1969 void __init
page_alloc_init(void)
1971 hotcpu_notifier(page_alloc_cpu_notify
, 0);
1975 * setup_per_zone_lowmem_reserve - called whenever
1976 * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
1977 * has a correct pages reserved value, so an adequate number of
1978 * pages are left in the zone after a successful __alloc_pages().
1980 static void setup_per_zone_lowmem_reserve(void)
1982 struct pglist_data
*pgdat
;
1985 for_each_pgdat(pgdat
) {
1986 for (j
= 0; j
< MAX_NR_ZONES
; j
++) {
1987 struct zone
*zone
= pgdat
->node_zones
+ j
;
1988 unsigned long present_pages
= zone
->present_pages
;
1990 zone
->lowmem_reserve
[j
] = 0;
1992 for (idx
= j
-1; idx
>= 0; idx
--) {
1993 struct zone
*lower_zone
;
1995 if (sysctl_lowmem_reserve_ratio
[idx
] < 1)
1996 sysctl_lowmem_reserve_ratio
[idx
] = 1;
1998 lower_zone
= pgdat
->node_zones
+ idx
;
1999 lower_zone
->lowmem_reserve
[j
] = present_pages
/
2000 sysctl_lowmem_reserve_ratio
[idx
];
2001 present_pages
+= lower_zone
->present_pages
;
2008 * setup_per_zone_pages_min - called when min_free_kbytes changes. Ensures
2009 * that the pages_{min,low,high} values for each zone are set correctly
2010 * with respect to min_free_kbytes.
2012 static void setup_per_zone_pages_min(void)
2014 unsigned long pages_min
= min_free_kbytes
>> (PAGE_SHIFT
- 10);
2015 unsigned long lowmem_pages
= 0;
2017 unsigned long flags
;
2019 /* Calculate total number of !ZONE_HIGHMEM pages */
2020 for_each_zone(zone
) {
2021 if (!is_highmem(zone
))
2022 lowmem_pages
+= zone
->present_pages
;
2025 for_each_zone(zone
) {
2026 spin_lock_irqsave(&zone
->lru_lock
, flags
);
2027 if (is_highmem(zone
)) {
2029 * Often, highmem doesn't need to reserve any pages.
2030 * But the pages_min/low/high values are also used for
2031 * batching up page reclaim activity so we need a
2032 * decent value here.
2036 min_pages
= zone
->present_pages
/ 1024;
2037 if (min_pages
< SWAP_CLUSTER_MAX
)
2038 min_pages
= SWAP_CLUSTER_MAX
;
2039 if (min_pages
> 128)
2041 zone
->pages_min
= min_pages
;
2043 /* if it's a lowmem zone, reserve a number of pages
2044 * proportionate to the zone's size.
2046 zone
->pages_min
= (pages_min
* zone
->present_pages
) /
2051 * When interpreting these watermarks, just keep in mind that:
2052 * zone->pages_min == (zone->pages_min * 4) / 4;
2054 zone
->pages_low
= (zone
->pages_min
* 5) / 4;
2055 zone
->pages_high
= (zone
->pages_min
* 6) / 4;
2056 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
2061 * Initialise min_free_kbytes.
2063 * For small machines we want it small (128k min). For large machines
2064 * we want it large (64MB max). But it is not linear, because network
2065 * bandwidth does not increase linearly with machine size. We use
2067 * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
2068 * min_free_kbytes = sqrt(lowmem_kbytes * 16)
2084 static int __init
init_per_zone_pages_min(void)
2086 unsigned long lowmem_kbytes
;
2088 lowmem_kbytes
= nr_free_buffer_pages() * (PAGE_SIZE
>> 10);
2090 min_free_kbytes
= int_sqrt(lowmem_kbytes
* 16);
2091 if (min_free_kbytes
< 128)
2092 min_free_kbytes
= 128;
2093 if (min_free_kbytes
> 65536)
2094 min_free_kbytes
= 65536;
2095 setup_per_zone_pages_min();
2096 setup_per_zone_lowmem_reserve();
2099 module_init(init_per_zone_pages_min
)
2102 * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
2103 * that we can call two helper functions whenever min_free_kbytes
2106 int min_free_kbytes_sysctl_handler(ctl_table
*table
, int write
,
2107 struct file
*file
, void __user
*buffer
, size_t *length
, loff_t
*ppos
)
2109 proc_dointvec(table
, write
, file
, buffer
, length
, ppos
);
2110 setup_per_zone_pages_min();
2115 * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
2116 * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
2117 * whenever sysctl_lowmem_reserve_ratio changes.
2119 * The reserve ratio obviously has absolutely no relation with the
2120 * pages_min watermarks. The lowmem reserve ratio can only make sense
2121 * if in function of the boot time zone sizes.
2123 int lowmem_reserve_ratio_sysctl_handler(ctl_table
*table
, int write
,
2124 struct file
*file
, void __user
*buffer
, size_t *length
, loff_t
*ppos
)
2126 proc_dointvec_minmax(table
, write
, file
, buffer
, length
, ppos
);
2127 setup_per_zone_lowmem_reserve();
2131 __initdata
int hashdist
= HASHDIST_DEFAULT
;
2134 static int __init
set_hashdist(char *str
)
2138 hashdist
= simple_strtoul(str
, &str
, 0);
2141 __setup("hashdist=", set_hashdist
);
2145 * allocate a large system hash table from bootmem
2146 * - it is assumed that the hash table must contain an exact power-of-2
2147 * quantity of entries
2148 * - limit is the number of hash buckets, not the total allocation size
2150 void *__init
alloc_large_system_hash(const char *tablename
,
2151 unsigned long bucketsize
,
2152 unsigned long numentries
,
2155 unsigned int *_hash_shift
,
2156 unsigned int *_hash_mask
,
2157 unsigned long limit
)
2159 unsigned long long max
= limit
;
2160 unsigned long log2qty
, size
;
2163 /* allow the kernel cmdline to have a say */
2165 /* round applicable memory size up to nearest megabyte */
2166 numentries
= (flags
& HASH_HIGHMEM
) ? nr_all_pages
: nr_kernel_pages
;
2167 numentries
+= (1UL << (20 - PAGE_SHIFT
)) - 1;
2168 numentries
>>= 20 - PAGE_SHIFT
;
2169 numentries
<<= 20 - PAGE_SHIFT
;
2171 /* limit to 1 bucket per 2^scale bytes of low memory */
2172 if (scale
> PAGE_SHIFT
)
2173 numentries
>>= (scale
- PAGE_SHIFT
);
2175 numentries
<<= (PAGE_SHIFT
- scale
);
2177 /* rounded up to nearest power of 2 in size */
2178 numentries
= 1UL << (long_log2(numentries
) + 1);
2180 /* limit allocation size to 1/16 total memory by default */
2182 max
= ((unsigned long long)nr_all_pages
<< PAGE_SHIFT
) >> 4;
2183 do_div(max
, bucketsize
);
2186 if (numentries
> max
)
2189 log2qty
= long_log2(numentries
);
2192 size
= bucketsize
<< log2qty
;
2193 if (flags
& HASH_EARLY
)
2194 table
= alloc_bootmem(size
);
2196 table
= __vmalloc(size
, GFP_ATOMIC
, PAGE_KERNEL
);
2198 unsigned long order
;
2199 for (order
= 0; ((1UL << order
) << PAGE_SHIFT
) < size
; order
++)
2201 table
= (void*) __get_free_pages(GFP_ATOMIC
, order
);
2203 } while (!table
&& size
> PAGE_SIZE
&& --log2qty
);
2206 panic("Failed to allocate %s hash table\n", tablename
);
2208 printk("%s hash table entries: %d (order: %d, %lu bytes)\n",
2211 long_log2(size
) - PAGE_SHIFT
,
2215 *_hash_shift
= log2qty
;
2217 *_hash_mask
= (1 << log2qty
) - 1;