[PATCH] unpaged: unifdefed PageCompound
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / mm / page_alloc.c
blob23b84c4e1a57beaf3a3b2cd578318b3da592071c
1 /*
2 * linux/mm/page_alloc.c
4 * Manages the free list, the system allocates free pages here.
5 * Note that kmalloc() lives in slab.c
7 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
8 * Swap reorganised 29.12.95, Stephen Tweedie
9 * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
10 * Reshaped it to be a zoned allocator, Ingo Molnar, Red Hat, 1999
11 * Discontiguous memory support, Kanoj Sarcar, SGI, Nov 1999
12 * Zone balancing, Kanoj Sarcar, SGI, Jan 2000
13 * Per cpu hot/cold page lists, bulk allocation, Martin J. Bligh, Sept 2002
14 * (lots of bits borrowed from Ingo Molnar & Andrew Morton)
17 #include <linux/config.h>
18 #include <linux/stddef.h>
19 #include <linux/mm.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/kernel.h>
26 #include <linux/module.h>
27 #include <linux/suspend.h>
28 #include <linux/pagevec.h>
29 #include <linux/blkdev.h>
30 #include <linux/slab.h>
31 #include <linux/notifier.h>
32 #include <linux/topology.h>
33 #include <linux/sysctl.h>
34 #include <linux/cpu.h>
35 #include <linux/cpuset.h>
36 #include <linux/memory_hotplug.h>
37 #include <linux/nodemask.h>
38 #include <linux/vmalloc.h>
40 #include <asm/tlbflush.h>
41 #include "internal.h"
44 * MCD - HACK: Find somewhere to initialize this EARLY, or make this
45 * initializer cleaner
47 nodemask_t node_online_map __read_mostly = { { [0] = 1UL } };
48 EXPORT_SYMBOL(node_online_map);
49 nodemask_t node_possible_map __read_mostly = NODE_MASK_ALL;
50 EXPORT_SYMBOL(node_possible_map);
51 struct pglist_data *pgdat_list __read_mostly;
52 unsigned long totalram_pages __read_mostly;
53 unsigned long totalhigh_pages __read_mostly;
54 long nr_swap_pages;
57 * results with 256, 32 in the lowmem_reserve sysctl:
58 * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
59 * 1G machine -> (16M dma, 784M normal, 224M high)
60 * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
61 * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
62 * HIGHMEM allocation will (224M+784M)/256 of ram reserved in ZONE_DMA
64 * TBD: should special case ZONE_DMA32 machines here - in those we normally
65 * don't need any ZONE_NORMAL reservation
67 int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1] = { 256, 256, 32 };
69 EXPORT_SYMBOL(totalram_pages);
72 * Used by page_zone() to look up the address of the struct zone whose
73 * id is encoded in the upper bits of page->flags
75 struct zone *zone_table[1 << ZONETABLE_SHIFT] __read_mostly;
76 EXPORT_SYMBOL(zone_table);
78 static char *zone_names[MAX_NR_ZONES] = { "DMA", "DMA32", "Normal", "HighMem" };
79 int min_free_kbytes = 1024;
81 unsigned long __initdata nr_kernel_pages;
82 unsigned long __initdata nr_all_pages;
84 static int page_outside_zone_boundaries(struct zone *zone, struct page *page)
86 int ret = 0;
87 unsigned seq;
88 unsigned long pfn = page_to_pfn(page);
90 do {
91 seq = zone_span_seqbegin(zone);
92 if (pfn >= zone->zone_start_pfn + zone->spanned_pages)
93 ret = 1;
94 else if (pfn < zone->zone_start_pfn)
95 ret = 1;
96 } while (zone_span_seqretry(zone, seq));
98 return ret;
101 static int page_is_consistent(struct zone *zone, struct page *page)
103 #ifdef CONFIG_HOLES_IN_ZONE
104 if (!pfn_valid(page_to_pfn(page)))
105 return 0;
106 #endif
107 if (zone != page_zone(page))
108 return 0;
110 return 1;
113 * Temporary debugging check for pages not lying within a given zone.
115 static int bad_range(struct zone *zone, struct page *page)
117 if (page_outside_zone_boundaries(zone, page))
118 return 1;
119 if (!page_is_consistent(zone, page))
120 return 1;
122 return 0;
125 static void bad_page(const char *function, struct page *page)
127 printk(KERN_EMERG "Bad page state at %s (in process '%s', page %p)\n",
128 function, current->comm, page);
129 printk(KERN_EMERG "flags:0x%0*lx mapping:%p mapcount:%d count:%d\n",
130 (int)(2*sizeof(unsigned long)), (unsigned long)page->flags,
131 page->mapping, page_mapcount(page), page_count(page));
132 printk(KERN_EMERG "Backtrace:\n");
133 dump_stack();
134 printk(KERN_EMERG "Trying to fix it up, but a reboot is needed\n");
135 page->flags &= ~(1 << PG_lru |
136 1 << PG_private |
137 1 << PG_locked |
138 1 << PG_active |
139 1 << PG_dirty |
140 1 << PG_reclaim |
141 1 << PG_slab |
142 1 << PG_swapcache |
143 1 << PG_writeback |
144 1 << PG_reserved );
145 set_page_count(page, 0);
146 reset_page_mapcount(page);
147 page->mapping = NULL;
148 add_taint(TAINT_BAD_PAGE);
152 * Higher-order pages are called "compound pages". They are structured thusly:
154 * The first PAGE_SIZE page is called the "head page".
156 * The remaining PAGE_SIZE pages are called "tail pages".
158 * All pages have PG_compound set. All pages have their ->private pointing at
159 * the head page (even the head page has this).
161 * The first tail page's ->mapping, if non-zero, holds the address of the
162 * compound page's put_page() function.
164 * The order of the allocation is stored in the first tail page's ->index
165 * This is only for debug at present. This usage means that zero-order pages
166 * may not be compound.
168 static void prep_compound_page(struct page *page, unsigned long order)
170 int i;
171 int nr_pages = 1 << order;
173 page[1].mapping = NULL;
174 page[1].index = order;
175 for (i = 0; i < nr_pages; i++) {
176 struct page *p = page + i;
178 SetPageCompound(p);
179 set_page_private(p, (unsigned long)page);
183 static void destroy_compound_page(struct page *page, unsigned long order)
185 int i;
186 int nr_pages = 1 << order;
188 if (!PageCompound(page))
189 return;
191 if (page[1].index != order)
192 bad_page(__FUNCTION__, page);
194 for (i = 0; i < nr_pages; i++) {
195 struct page *p = page + i;
197 if (!PageCompound(p))
198 bad_page(__FUNCTION__, page);
199 if (page_private(p) != (unsigned long)page)
200 bad_page(__FUNCTION__, page);
201 ClearPageCompound(p);
206 * function for dealing with page's order in buddy system.
207 * zone->lock is already acquired when we use these.
208 * So, we don't need atomic page->flags operations here.
210 static inline unsigned long page_order(struct page *page) {
211 return page_private(page);
214 static inline void set_page_order(struct page *page, int order) {
215 set_page_private(page, order);
216 __SetPagePrivate(page);
219 static inline void rmv_page_order(struct page *page)
221 __ClearPagePrivate(page);
222 set_page_private(page, 0);
226 * Locate the struct page for both the matching buddy in our
227 * pair (buddy1) and the combined O(n+1) page they form (page).
229 * 1) Any buddy B1 will have an order O twin B2 which satisfies
230 * the following equation:
231 * B2 = B1 ^ (1 << O)
232 * For example, if the starting buddy (buddy2) is #8 its order
233 * 1 buddy is #10:
234 * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
236 * 2) Any buddy B will have an order O+1 parent P which
237 * satisfies the following equation:
238 * P = B & ~(1 << O)
240 * Assumption: *_mem_map is contigious at least up to MAX_ORDER
242 static inline struct page *
243 __page_find_buddy(struct page *page, unsigned long page_idx, unsigned int order)
245 unsigned long buddy_idx = page_idx ^ (1 << order);
247 return page + (buddy_idx - page_idx);
250 static inline unsigned long
251 __find_combined_index(unsigned long page_idx, unsigned int order)
253 return (page_idx & ~(1 << order));
257 * This function checks whether a page is free && is the buddy
258 * we can do coalesce a page and its buddy if
259 * (a) the buddy is free &&
260 * (b) the buddy is on the buddy system &&
261 * (c) a page and its buddy have the same order.
262 * for recording page's order, we use page_private(page) and PG_private.
265 static inline int page_is_buddy(struct page *page, int order)
267 if (PagePrivate(page) &&
268 (page_order(page) == order) &&
269 page_count(page) == 0)
270 return 1;
271 return 0;
275 * Freeing function for a buddy system allocator.
277 * The concept of a buddy system is to maintain direct-mapped table
278 * (containing bit values) for memory blocks of various "orders".
279 * The bottom level table contains the map for the smallest allocatable
280 * units of memory (here, pages), and each level above it describes
281 * pairs of units from the levels below, hence, "buddies".
282 * At a high level, all that happens here is marking the table entry
283 * at the bottom level available, and propagating the changes upward
284 * as necessary, plus some accounting needed to play nicely with other
285 * parts of the VM system.
286 * At each level, we keep a list of pages, which are heads of continuous
287 * free pages of length of (1 << order) and marked with PG_Private.Page's
288 * order is recorded in page_private(page) field.
289 * So when we are allocating or freeing one, we can derive the state of the
290 * other. That is, if we allocate a small block, and both were
291 * free, the remainder of the region must be split into blocks.
292 * If a block is freed, and its buddy is also free, then this
293 * triggers coalescing into a block of larger size.
295 * -- wli
298 static inline void __free_pages_bulk (struct page *page,
299 struct zone *zone, unsigned int order)
301 unsigned long page_idx;
302 int order_size = 1 << order;
304 if (unlikely(order))
305 destroy_compound_page(page, order);
307 page_idx = page_to_pfn(page) & ((1 << MAX_ORDER) - 1);
309 BUG_ON(page_idx & (order_size - 1));
310 BUG_ON(bad_range(zone, page));
312 zone->free_pages += order_size;
313 while (order < MAX_ORDER-1) {
314 unsigned long combined_idx;
315 struct free_area *area;
316 struct page *buddy;
318 combined_idx = __find_combined_index(page_idx, order);
319 buddy = __page_find_buddy(page, page_idx, order);
321 if (bad_range(zone, buddy))
322 break;
323 if (!page_is_buddy(buddy, order))
324 break; /* Move the buddy up one level. */
325 list_del(&buddy->lru);
326 area = zone->free_area + order;
327 area->nr_free--;
328 rmv_page_order(buddy);
329 page = page + (combined_idx - page_idx);
330 page_idx = combined_idx;
331 order++;
333 set_page_order(page, order);
334 list_add(&page->lru, &zone->free_area[order].free_list);
335 zone->free_area[order].nr_free++;
338 static inline void free_pages_check(const char *function, struct page *page)
340 if ( page_mapcount(page) ||
341 page->mapping != NULL ||
342 page_count(page) != 0 ||
343 (page->flags & (
344 1 << PG_lru |
345 1 << PG_private |
346 1 << PG_locked |
347 1 << PG_active |
348 1 << PG_reclaim |
349 1 << PG_slab |
350 1 << PG_swapcache |
351 1 << PG_writeback |
352 1 << PG_reserved )))
353 bad_page(function, page);
354 if (PageDirty(page))
355 __ClearPageDirty(page);
359 * Frees a list of pages.
360 * Assumes all pages on list are in same zone, and of same order.
361 * count is the number of pages to free.
363 * If the zone was previously in an "all pages pinned" state then look to
364 * see if this freeing clears that state.
366 * And clear the zone's pages_scanned counter, to hold off the "all pages are
367 * pinned" detection logic.
369 static int
370 free_pages_bulk(struct zone *zone, int count,
371 struct list_head *list, unsigned int order)
373 unsigned long flags;
374 struct page *page = NULL;
375 int ret = 0;
377 spin_lock_irqsave(&zone->lock, flags);
378 zone->all_unreclaimable = 0;
379 zone->pages_scanned = 0;
380 while (!list_empty(list) && count--) {
381 page = list_entry(list->prev, struct page, lru);
382 /* have to delete it as __free_pages_bulk list manipulates */
383 list_del(&page->lru);
384 __free_pages_bulk(page, zone, order);
385 ret++;
387 spin_unlock_irqrestore(&zone->lock, flags);
388 return ret;
391 void __free_pages_ok(struct page *page, unsigned int order)
393 LIST_HEAD(list);
394 int i;
396 arch_free_page(page, order);
398 mod_page_state(pgfree, 1 << order);
400 #ifndef CONFIG_MMU
401 if (order > 0)
402 for (i = 1 ; i < (1 << order) ; ++i)
403 __put_page(page + i);
404 #endif
406 for (i = 0 ; i < (1 << order) ; ++i)
407 free_pages_check(__FUNCTION__, page + i);
408 list_add(&page->lru, &list);
409 kernel_map_pages(page, 1<<order, 0);
410 free_pages_bulk(page_zone(page), 1, &list, order);
415 * The order of subdivision here is critical for the IO subsystem.
416 * Please do not alter this order without good reasons and regression
417 * testing. Specifically, as large blocks of memory are subdivided,
418 * the order in which smaller blocks are delivered depends on the order
419 * they're subdivided in this function. This is the primary factor
420 * influencing the order in which pages are delivered to the IO
421 * subsystem according to empirical testing, and this is also justified
422 * by considering the behavior of a buddy system containing a single
423 * large block of memory acted on by a series of small allocations.
424 * This behavior is a critical factor in sglist merging's success.
426 * -- wli
428 static inline struct page *
429 expand(struct zone *zone, struct page *page,
430 int low, int high, struct free_area *area)
432 unsigned long size = 1 << high;
434 while (high > low) {
435 area--;
436 high--;
437 size >>= 1;
438 BUG_ON(bad_range(zone, &page[size]));
439 list_add(&page[size].lru, &area->free_list);
440 area->nr_free++;
441 set_page_order(&page[size], high);
443 return page;
446 void set_page_refs(struct page *page, int order)
448 #ifdef CONFIG_MMU
449 set_page_count(page, 1);
450 #else
451 int i;
454 * We need to reference all the pages for this order, otherwise if
455 * anyone accesses one of the pages with (get/put) it will be freed.
456 * - eg: access_process_vm()
458 for (i = 0; i < (1 << order); i++)
459 set_page_count(page + i, 1);
460 #endif /* CONFIG_MMU */
464 * This page is about to be returned from the page allocator
466 static void prep_new_page(struct page *page, int order)
468 if ( page_mapcount(page) ||
469 page->mapping != NULL ||
470 page_count(page) != 0 ||
471 (page->flags & (
472 1 << PG_lru |
473 1 << PG_private |
474 1 << PG_locked |
475 1 << PG_active |
476 1 << PG_dirty |
477 1 << PG_reclaim |
478 1 << PG_slab |
479 1 << PG_swapcache |
480 1 << PG_writeback |
481 1 << PG_reserved )))
482 bad_page(__FUNCTION__, page);
484 page->flags &= ~(1 << PG_uptodate | 1 << PG_error |
485 1 << PG_referenced | 1 << PG_arch_1 |
486 1 << PG_checked | 1 << PG_mappedtodisk);
487 set_page_private(page, 0);
488 set_page_refs(page, order);
489 kernel_map_pages(page, 1 << order, 1);
493 * Do the hard work of removing an element from the buddy allocator.
494 * Call me with the zone->lock already held.
496 static struct page *__rmqueue(struct zone *zone, unsigned int order)
498 struct free_area * area;
499 unsigned int current_order;
500 struct page *page;
502 for (current_order = order; current_order < MAX_ORDER; ++current_order) {
503 area = zone->free_area + current_order;
504 if (list_empty(&area->free_list))
505 continue;
507 page = list_entry(area->free_list.next, struct page, lru);
508 list_del(&page->lru);
509 rmv_page_order(page);
510 area->nr_free--;
511 zone->free_pages -= 1UL << order;
512 return expand(zone, page, order, current_order, area);
515 return NULL;
519 * Obtain a specified number of elements from the buddy allocator, all under
520 * a single hold of the lock, for efficiency. Add them to the supplied list.
521 * Returns the number of new pages which were placed at *list.
523 static int rmqueue_bulk(struct zone *zone, unsigned int order,
524 unsigned long count, struct list_head *list)
526 unsigned long flags;
527 int i;
528 int allocated = 0;
529 struct page *page;
531 spin_lock_irqsave(&zone->lock, flags);
532 for (i = 0; i < count; ++i) {
533 page = __rmqueue(zone, order);
534 if (page == NULL)
535 break;
536 allocated++;
537 list_add_tail(&page->lru, list);
539 spin_unlock_irqrestore(&zone->lock, flags);
540 return allocated;
543 #ifdef CONFIG_NUMA
544 /* Called from the slab reaper to drain remote pagesets */
545 void drain_remote_pages(void)
547 struct zone *zone;
548 int i;
549 unsigned long flags;
551 local_irq_save(flags);
552 for_each_zone(zone) {
553 struct per_cpu_pageset *pset;
555 /* Do not drain local pagesets */
556 if (zone->zone_pgdat->node_id == numa_node_id())
557 continue;
559 pset = zone->pageset[smp_processor_id()];
560 for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
561 struct per_cpu_pages *pcp;
563 pcp = &pset->pcp[i];
564 if (pcp->count)
565 pcp->count -= free_pages_bulk(zone, pcp->count,
566 &pcp->list, 0);
569 local_irq_restore(flags);
571 #endif
573 #if defined(CONFIG_PM) || defined(CONFIG_HOTPLUG_CPU)
574 static void __drain_pages(unsigned int cpu)
576 struct zone *zone;
577 int i;
579 for_each_zone(zone) {
580 struct per_cpu_pageset *pset;
582 pset = zone_pcp(zone, cpu);
583 for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
584 struct per_cpu_pages *pcp;
586 pcp = &pset->pcp[i];
587 pcp->count -= free_pages_bulk(zone, pcp->count,
588 &pcp->list, 0);
592 #endif /* CONFIG_PM || CONFIG_HOTPLUG_CPU */
594 #ifdef CONFIG_PM
596 void mark_free_pages(struct zone *zone)
598 unsigned long zone_pfn, flags;
599 int order;
600 struct list_head *curr;
602 if (!zone->spanned_pages)
603 return;
605 spin_lock_irqsave(&zone->lock, flags);
606 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
607 ClearPageNosaveFree(pfn_to_page(zone_pfn + zone->zone_start_pfn));
609 for (order = MAX_ORDER - 1; order >= 0; --order)
610 list_for_each(curr, &zone->free_area[order].free_list) {
611 unsigned long start_pfn, i;
613 start_pfn = page_to_pfn(list_entry(curr, struct page, lru));
615 for (i=0; i < (1<<order); i++)
616 SetPageNosaveFree(pfn_to_page(start_pfn+i));
618 spin_unlock_irqrestore(&zone->lock, flags);
622 * Spill all of this CPU's per-cpu pages back into the buddy allocator.
624 void drain_local_pages(void)
626 unsigned long flags;
628 local_irq_save(flags);
629 __drain_pages(smp_processor_id());
630 local_irq_restore(flags);
632 #endif /* CONFIG_PM */
634 static void zone_statistics(struct zonelist *zonelist, struct zone *z)
636 #ifdef CONFIG_NUMA
637 unsigned long flags;
638 int cpu;
639 pg_data_t *pg = z->zone_pgdat;
640 pg_data_t *orig = zonelist->zones[0]->zone_pgdat;
641 struct per_cpu_pageset *p;
643 local_irq_save(flags);
644 cpu = smp_processor_id();
645 p = zone_pcp(z,cpu);
646 if (pg == orig) {
647 p->numa_hit++;
648 } else {
649 p->numa_miss++;
650 zone_pcp(zonelist->zones[0], cpu)->numa_foreign++;
652 if (pg == NODE_DATA(numa_node_id()))
653 p->local_node++;
654 else
655 p->other_node++;
656 local_irq_restore(flags);
657 #endif
661 * Free a 0-order page
663 static void FASTCALL(free_hot_cold_page(struct page *page, int cold));
664 static void fastcall free_hot_cold_page(struct page *page, int cold)
666 struct zone *zone = page_zone(page);
667 struct per_cpu_pages *pcp;
668 unsigned long flags;
670 arch_free_page(page, 0);
672 kernel_map_pages(page, 1, 0);
673 inc_page_state(pgfree);
674 if (PageAnon(page))
675 page->mapping = NULL;
676 free_pages_check(__FUNCTION__, page);
677 pcp = &zone_pcp(zone, get_cpu())->pcp[cold];
678 local_irq_save(flags);
679 list_add(&page->lru, &pcp->list);
680 pcp->count++;
681 if (pcp->count >= pcp->high)
682 pcp->count -= free_pages_bulk(zone, pcp->batch, &pcp->list, 0);
683 local_irq_restore(flags);
684 put_cpu();
687 void fastcall free_hot_page(struct page *page)
689 free_hot_cold_page(page, 0);
692 void fastcall free_cold_page(struct page *page)
694 free_hot_cold_page(page, 1);
697 static inline void prep_zero_page(struct page *page, int order, gfp_t gfp_flags)
699 int i;
701 BUG_ON((gfp_flags & (__GFP_WAIT | __GFP_HIGHMEM)) == __GFP_HIGHMEM);
702 for(i = 0; i < (1 << order); i++)
703 clear_highpage(page + i);
707 * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
708 * we cheat by calling it from here, in the order > 0 path. Saves a branch
709 * or two.
711 static struct page *
712 buffered_rmqueue(struct zone *zone, int order, gfp_t gfp_flags)
714 unsigned long flags;
715 struct page *page = NULL;
716 int cold = !!(gfp_flags & __GFP_COLD);
718 if (order == 0) {
719 struct per_cpu_pages *pcp;
721 pcp = &zone_pcp(zone, get_cpu())->pcp[cold];
722 local_irq_save(flags);
723 if (pcp->count <= pcp->low)
724 pcp->count += rmqueue_bulk(zone, 0,
725 pcp->batch, &pcp->list);
726 if (pcp->count) {
727 page = list_entry(pcp->list.next, struct page, lru);
728 list_del(&page->lru);
729 pcp->count--;
731 local_irq_restore(flags);
732 put_cpu();
733 } else {
734 spin_lock_irqsave(&zone->lock, flags);
735 page = __rmqueue(zone, order);
736 spin_unlock_irqrestore(&zone->lock, flags);
739 if (page != NULL) {
740 BUG_ON(bad_range(zone, page));
741 mod_page_state_zone(zone, pgalloc, 1 << order);
742 prep_new_page(page, order);
744 if (gfp_flags & __GFP_ZERO)
745 prep_zero_page(page, order, gfp_flags);
747 if (order && (gfp_flags & __GFP_COMP))
748 prep_compound_page(page, order);
750 return page;
753 #define ALLOC_NO_WATERMARKS 0x01 /* don't check watermarks at all */
754 #define ALLOC_HARDER 0x02 /* try to alloc harder */
755 #define ALLOC_HIGH 0x04 /* __GFP_HIGH set */
756 #define ALLOC_CPUSET 0x08 /* check for correct cpuset */
759 * Return 1 if free pages are above 'mark'. This takes into account the order
760 * of the allocation.
762 int zone_watermark_ok(struct zone *z, int order, unsigned long mark,
763 int classzone_idx, int alloc_flags)
765 /* free_pages my go negative - that's OK */
766 long min = mark, free_pages = z->free_pages - (1 << order) + 1;
767 int o;
769 if (alloc_flags & ALLOC_HIGH)
770 min -= min / 2;
771 if (alloc_flags & ALLOC_HARDER)
772 min -= min / 4;
774 if (free_pages <= min + z->lowmem_reserve[classzone_idx])
775 return 0;
776 for (o = 0; o < order; o++) {
777 /* At the next order, this order's pages become unavailable */
778 free_pages -= z->free_area[o].nr_free << o;
780 /* Require fewer higher order pages to be free */
781 min >>= 1;
783 if (free_pages <= min)
784 return 0;
786 return 1;
790 * get_page_from_freeliest goes through the zonelist trying to allocate
791 * a page.
793 static struct page *
794 get_page_from_freelist(gfp_t gfp_mask, unsigned int order,
795 struct zonelist *zonelist, int alloc_flags)
797 struct zone **z = zonelist->zones;
798 struct page *page = NULL;
799 int classzone_idx = zone_idx(*z);
802 * Go through the zonelist once, looking for a zone with enough free.
803 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
805 do {
806 if ((alloc_flags & ALLOC_CPUSET) &&
807 !cpuset_zone_allowed(*z, gfp_mask))
808 continue;
810 if (!(alloc_flags & ALLOC_NO_WATERMARKS)) {
811 if (!zone_watermark_ok(*z, order, (*z)->pages_low,
812 classzone_idx, alloc_flags))
813 continue;
816 page = buffered_rmqueue(*z, order, gfp_mask);
817 if (page) {
818 zone_statistics(zonelist, *z);
819 break;
821 } while (*(++z) != NULL);
822 return page;
826 * This is the 'heart' of the zoned buddy allocator.
828 struct page * fastcall
829 __alloc_pages(gfp_t gfp_mask, unsigned int order,
830 struct zonelist *zonelist)
832 const gfp_t wait = gfp_mask & __GFP_WAIT;
833 struct zone **z;
834 struct page *page;
835 struct reclaim_state reclaim_state;
836 struct task_struct *p = current;
837 int do_retry;
838 int alloc_flags;
839 int did_some_progress;
841 might_sleep_if(wait);
843 restart:
844 z = zonelist->zones; /* the list of zones suitable for gfp_mask */
846 if (unlikely(*z == NULL)) {
847 /* Should this ever happen?? */
848 return NULL;
851 page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, order,
852 zonelist, ALLOC_CPUSET);
853 if (page)
854 goto got_pg;
856 do {
857 wakeup_kswapd(*z, order);
858 } while (*(++z));
861 * OK, we're below the kswapd watermark and have kicked background
862 * reclaim. Now things get more complex, so set up alloc_flags according
863 * to how we want to proceed.
865 * The caller may dip into page reserves a bit more if the caller
866 * cannot run direct reclaim, or if the caller has realtime scheduling
867 * policy.
869 alloc_flags = 0;
870 if ((unlikely(rt_task(p)) && !in_interrupt()) || !wait)
871 alloc_flags |= ALLOC_HARDER;
872 if (gfp_mask & __GFP_HIGH)
873 alloc_flags |= ALLOC_HIGH;
874 if (wait)
875 alloc_flags |= ALLOC_CPUSET;
878 * Go through the zonelist again. Let __GFP_HIGH and allocations
879 * coming from realtime tasks go deeper into reserves.
881 * This is the last chance, in general, before the goto nopage.
882 * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
883 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
885 page = get_page_from_freelist(gfp_mask, order, zonelist, alloc_flags);
886 if (page)
887 goto got_pg;
889 /* This allocation should allow future memory freeing. */
891 if (((p->flags & PF_MEMALLOC) || unlikely(test_thread_flag(TIF_MEMDIE)))
892 && !in_interrupt()) {
893 if (!(gfp_mask & __GFP_NOMEMALLOC)) {
894 nofail_alloc:
895 /* go through the zonelist yet again, ignoring mins */
896 page = get_page_from_freelist(gfp_mask, order,
897 zonelist, ALLOC_NO_WATERMARKS|ALLOC_CPUSET);
898 if (page)
899 goto got_pg;
900 if (gfp_mask & __GFP_NOFAIL) {
901 blk_congestion_wait(WRITE, HZ/50);
902 goto nofail_alloc;
905 goto nopage;
908 /* Atomic allocations - we can't balance anything */
909 if (!wait)
910 goto nopage;
912 rebalance:
913 cond_resched();
915 /* We now go into synchronous reclaim */
916 p->flags |= PF_MEMALLOC;
917 reclaim_state.reclaimed_slab = 0;
918 p->reclaim_state = &reclaim_state;
920 did_some_progress = try_to_free_pages(zonelist->zones, gfp_mask);
922 p->reclaim_state = NULL;
923 p->flags &= ~PF_MEMALLOC;
925 cond_resched();
927 if (likely(did_some_progress)) {
928 page = get_page_from_freelist(gfp_mask, order,
929 zonelist, alloc_flags);
930 if (page)
931 goto got_pg;
932 } else if ((gfp_mask & __GFP_FS) && !(gfp_mask & __GFP_NORETRY)) {
934 * Go through the zonelist yet one more time, keep
935 * very high watermark here, this is only to catch
936 * a parallel oom killing, we must fail if we're still
937 * under heavy pressure.
939 page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, order,
940 zonelist, ALLOC_CPUSET);
941 if (page)
942 goto got_pg;
944 out_of_memory(gfp_mask, order);
945 goto restart;
949 * Don't let big-order allocations loop unless the caller explicitly
950 * requests that. Wait for some write requests to complete then retry.
952 * In this implementation, __GFP_REPEAT means __GFP_NOFAIL for order
953 * <= 3, but that may not be true in other implementations.
955 do_retry = 0;
956 if (!(gfp_mask & __GFP_NORETRY)) {
957 if ((order <= 3) || (gfp_mask & __GFP_REPEAT))
958 do_retry = 1;
959 if (gfp_mask & __GFP_NOFAIL)
960 do_retry = 1;
962 if (do_retry) {
963 blk_congestion_wait(WRITE, HZ/50);
964 goto rebalance;
967 nopage:
968 if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) {
969 printk(KERN_WARNING "%s: page allocation failure."
970 " order:%d, mode:0x%x\n",
971 p->comm, order, gfp_mask);
972 dump_stack();
973 show_mem();
975 got_pg:
976 return page;
979 EXPORT_SYMBOL(__alloc_pages);
982 * Common helper functions.
984 fastcall unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
986 struct page * page;
987 page = alloc_pages(gfp_mask, order);
988 if (!page)
989 return 0;
990 return (unsigned long) page_address(page);
993 EXPORT_SYMBOL(__get_free_pages);
995 fastcall unsigned long get_zeroed_page(gfp_t gfp_mask)
997 struct page * page;
1000 * get_zeroed_page() returns a 32-bit address, which cannot represent
1001 * a highmem page
1003 BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
1005 page = alloc_pages(gfp_mask | __GFP_ZERO, 0);
1006 if (page)
1007 return (unsigned long) page_address(page);
1008 return 0;
1011 EXPORT_SYMBOL(get_zeroed_page);
1013 void __pagevec_free(struct pagevec *pvec)
1015 int i = pagevec_count(pvec);
1017 while (--i >= 0)
1018 free_hot_cold_page(pvec->pages[i], pvec->cold);
1021 fastcall void __free_pages(struct page *page, unsigned int order)
1023 if (put_page_testzero(page)) {
1024 if (order == 0)
1025 free_hot_page(page);
1026 else
1027 __free_pages_ok(page, order);
1031 EXPORT_SYMBOL(__free_pages);
1033 fastcall void free_pages(unsigned long addr, unsigned int order)
1035 if (addr != 0) {
1036 BUG_ON(!virt_addr_valid((void *)addr));
1037 __free_pages(virt_to_page((void *)addr), order);
1041 EXPORT_SYMBOL(free_pages);
1044 * Total amount of free (allocatable) RAM:
1046 unsigned int nr_free_pages(void)
1048 unsigned int sum = 0;
1049 struct zone *zone;
1051 for_each_zone(zone)
1052 sum += zone->free_pages;
1054 return sum;
1057 EXPORT_SYMBOL(nr_free_pages);
1059 #ifdef CONFIG_NUMA
1060 unsigned int nr_free_pages_pgdat(pg_data_t *pgdat)
1062 unsigned int i, sum = 0;
1064 for (i = 0; i < MAX_NR_ZONES; i++)
1065 sum += pgdat->node_zones[i].free_pages;
1067 return sum;
1069 #endif
1071 static unsigned int nr_free_zone_pages(int offset)
1073 /* Just pick one node, since fallback list is circular */
1074 pg_data_t *pgdat = NODE_DATA(numa_node_id());
1075 unsigned int sum = 0;
1077 struct zonelist *zonelist = pgdat->node_zonelists + offset;
1078 struct zone **zonep = zonelist->zones;
1079 struct zone *zone;
1081 for (zone = *zonep++; zone; zone = *zonep++) {
1082 unsigned long size = zone->present_pages;
1083 unsigned long high = zone->pages_high;
1084 if (size > high)
1085 sum += size - high;
1088 return sum;
1092 * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
1094 unsigned int nr_free_buffer_pages(void)
1096 return nr_free_zone_pages(gfp_zone(GFP_USER));
1100 * Amount of free RAM allocatable within all zones
1102 unsigned int nr_free_pagecache_pages(void)
1104 return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER));
1107 #ifdef CONFIG_HIGHMEM
1108 unsigned int nr_free_highpages (void)
1110 pg_data_t *pgdat;
1111 unsigned int pages = 0;
1113 for_each_pgdat(pgdat)
1114 pages += pgdat->node_zones[ZONE_HIGHMEM].free_pages;
1116 return pages;
1118 #endif
1120 #ifdef CONFIG_NUMA
1121 static void show_node(struct zone *zone)
1123 printk("Node %d ", zone->zone_pgdat->node_id);
1125 #else
1126 #define show_node(zone) do { } while (0)
1127 #endif
1130 * Accumulate the page_state information across all CPUs.
1131 * The result is unavoidably approximate - it can change
1132 * during and after execution of this function.
1134 static DEFINE_PER_CPU(struct page_state, page_states) = {0};
1136 atomic_t nr_pagecache = ATOMIC_INIT(0);
1137 EXPORT_SYMBOL(nr_pagecache);
1138 #ifdef CONFIG_SMP
1139 DEFINE_PER_CPU(long, nr_pagecache_local) = 0;
1140 #endif
1142 void __get_page_state(struct page_state *ret, int nr, cpumask_t *cpumask)
1144 int cpu = 0;
1146 memset(ret, 0, sizeof(*ret));
1147 cpus_and(*cpumask, *cpumask, cpu_online_map);
1149 cpu = first_cpu(*cpumask);
1150 while (cpu < NR_CPUS) {
1151 unsigned long *in, *out, off;
1153 in = (unsigned long *)&per_cpu(page_states, cpu);
1155 cpu = next_cpu(cpu, *cpumask);
1157 if (cpu < NR_CPUS)
1158 prefetch(&per_cpu(page_states, cpu));
1160 out = (unsigned long *)ret;
1161 for (off = 0; off < nr; off++)
1162 *out++ += *in++;
1166 void get_page_state_node(struct page_state *ret, int node)
1168 int nr;
1169 cpumask_t mask = node_to_cpumask(node);
1171 nr = offsetof(struct page_state, GET_PAGE_STATE_LAST);
1172 nr /= sizeof(unsigned long);
1174 __get_page_state(ret, nr+1, &mask);
1177 void get_page_state(struct page_state *ret)
1179 int nr;
1180 cpumask_t mask = CPU_MASK_ALL;
1182 nr = offsetof(struct page_state, GET_PAGE_STATE_LAST);
1183 nr /= sizeof(unsigned long);
1185 __get_page_state(ret, nr + 1, &mask);
1188 void get_full_page_state(struct page_state *ret)
1190 cpumask_t mask = CPU_MASK_ALL;
1192 __get_page_state(ret, sizeof(*ret) / sizeof(unsigned long), &mask);
1195 unsigned long __read_page_state(unsigned long offset)
1197 unsigned long ret = 0;
1198 int cpu;
1200 for_each_online_cpu(cpu) {
1201 unsigned long in;
1203 in = (unsigned long)&per_cpu(page_states, cpu) + offset;
1204 ret += *((unsigned long *)in);
1206 return ret;
1209 void __mod_page_state(unsigned long offset, unsigned long delta)
1211 unsigned long flags;
1212 void* ptr;
1214 local_irq_save(flags);
1215 ptr = &__get_cpu_var(page_states);
1216 *(unsigned long*)(ptr + offset) += delta;
1217 local_irq_restore(flags);
1220 EXPORT_SYMBOL(__mod_page_state);
1222 void __get_zone_counts(unsigned long *active, unsigned long *inactive,
1223 unsigned long *free, struct pglist_data *pgdat)
1225 struct zone *zones = pgdat->node_zones;
1226 int i;
1228 *active = 0;
1229 *inactive = 0;
1230 *free = 0;
1231 for (i = 0; i < MAX_NR_ZONES; i++) {
1232 *active += zones[i].nr_active;
1233 *inactive += zones[i].nr_inactive;
1234 *free += zones[i].free_pages;
1238 void get_zone_counts(unsigned long *active,
1239 unsigned long *inactive, unsigned long *free)
1241 struct pglist_data *pgdat;
1243 *active = 0;
1244 *inactive = 0;
1245 *free = 0;
1246 for_each_pgdat(pgdat) {
1247 unsigned long l, m, n;
1248 __get_zone_counts(&l, &m, &n, pgdat);
1249 *active += l;
1250 *inactive += m;
1251 *free += n;
1255 void si_meminfo(struct sysinfo *val)
1257 val->totalram = totalram_pages;
1258 val->sharedram = 0;
1259 val->freeram = nr_free_pages();
1260 val->bufferram = nr_blockdev_pages();
1261 #ifdef CONFIG_HIGHMEM
1262 val->totalhigh = totalhigh_pages;
1263 val->freehigh = nr_free_highpages();
1264 #else
1265 val->totalhigh = 0;
1266 val->freehigh = 0;
1267 #endif
1268 val->mem_unit = PAGE_SIZE;
1271 EXPORT_SYMBOL(si_meminfo);
1273 #ifdef CONFIG_NUMA
1274 void si_meminfo_node(struct sysinfo *val, int nid)
1276 pg_data_t *pgdat = NODE_DATA(nid);
1278 val->totalram = pgdat->node_present_pages;
1279 val->freeram = nr_free_pages_pgdat(pgdat);
1280 val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].present_pages;
1281 val->freehigh = pgdat->node_zones[ZONE_HIGHMEM].free_pages;
1282 val->mem_unit = PAGE_SIZE;
1284 #endif
1286 #define K(x) ((x) << (PAGE_SHIFT-10))
1289 * Show free area list (used inside shift_scroll-lock stuff)
1290 * We also calculate the percentage fragmentation. We do this by counting the
1291 * memory on each free list with the exception of the first item on the list.
1293 void show_free_areas(void)
1295 struct page_state ps;
1296 int cpu, temperature;
1297 unsigned long active;
1298 unsigned long inactive;
1299 unsigned long free;
1300 struct zone *zone;
1302 for_each_zone(zone) {
1303 show_node(zone);
1304 printk("%s per-cpu:", zone->name);
1306 if (!zone->present_pages) {
1307 printk(" empty\n");
1308 continue;
1309 } else
1310 printk("\n");
1312 for_each_online_cpu(cpu) {
1313 struct per_cpu_pageset *pageset;
1315 pageset = zone_pcp(zone, cpu);
1317 for (temperature = 0; temperature < 2; temperature++)
1318 printk("cpu %d %s: low %d, high %d, batch %d used:%d\n",
1319 cpu,
1320 temperature ? "cold" : "hot",
1321 pageset->pcp[temperature].low,
1322 pageset->pcp[temperature].high,
1323 pageset->pcp[temperature].batch,
1324 pageset->pcp[temperature].count);
1328 get_page_state(&ps);
1329 get_zone_counts(&active, &inactive, &free);
1331 printk("Free pages: %11ukB (%ukB HighMem)\n",
1332 K(nr_free_pages()),
1333 K(nr_free_highpages()));
1335 printk("Active:%lu inactive:%lu dirty:%lu writeback:%lu "
1336 "unstable:%lu free:%u slab:%lu mapped:%lu pagetables:%lu\n",
1337 active,
1338 inactive,
1339 ps.nr_dirty,
1340 ps.nr_writeback,
1341 ps.nr_unstable,
1342 nr_free_pages(),
1343 ps.nr_slab,
1344 ps.nr_mapped,
1345 ps.nr_page_table_pages);
1347 for_each_zone(zone) {
1348 int i;
1350 show_node(zone);
1351 printk("%s"
1352 " free:%lukB"
1353 " min:%lukB"
1354 " low:%lukB"
1355 " high:%lukB"
1356 " active:%lukB"
1357 " inactive:%lukB"
1358 " present:%lukB"
1359 " pages_scanned:%lu"
1360 " all_unreclaimable? %s"
1361 "\n",
1362 zone->name,
1363 K(zone->free_pages),
1364 K(zone->pages_min),
1365 K(zone->pages_low),
1366 K(zone->pages_high),
1367 K(zone->nr_active),
1368 K(zone->nr_inactive),
1369 K(zone->present_pages),
1370 zone->pages_scanned,
1371 (zone->all_unreclaimable ? "yes" : "no")
1373 printk("lowmem_reserve[]:");
1374 for (i = 0; i < MAX_NR_ZONES; i++)
1375 printk(" %lu", zone->lowmem_reserve[i]);
1376 printk("\n");
1379 for_each_zone(zone) {
1380 unsigned long nr, flags, order, total = 0;
1382 show_node(zone);
1383 printk("%s: ", zone->name);
1384 if (!zone->present_pages) {
1385 printk("empty\n");
1386 continue;
1389 spin_lock_irqsave(&zone->lock, flags);
1390 for (order = 0; order < MAX_ORDER; order++) {
1391 nr = zone->free_area[order].nr_free;
1392 total += nr << order;
1393 printk("%lu*%lukB ", nr, K(1UL) << order);
1395 spin_unlock_irqrestore(&zone->lock, flags);
1396 printk("= %lukB\n", K(total));
1399 show_swap_cache_info();
1403 * Builds allocation fallback zone lists.
1405 static int __init build_zonelists_node(pg_data_t *pgdat, struct zonelist *zonelist, int j, int k)
1407 switch (k) {
1408 struct zone *zone;
1409 default:
1410 BUG();
1411 case ZONE_HIGHMEM:
1412 zone = pgdat->node_zones + ZONE_HIGHMEM;
1413 if (zone->present_pages) {
1414 #ifndef CONFIG_HIGHMEM
1415 BUG();
1416 #endif
1417 zonelist->zones[j++] = zone;
1419 case ZONE_NORMAL:
1420 zone = pgdat->node_zones + ZONE_NORMAL;
1421 if (zone->present_pages)
1422 zonelist->zones[j++] = zone;
1423 case ZONE_DMA32:
1424 zone = pgdat->node_zones + ZONE_DMA32;
1425 if (zone->present_pages)
1426 zonelist->zones[j++] = zone;
1427 case ZONE_DMA:
1428 zone = pgdat->node_zones + ZONE_DMA;
1429 if (zone->present_pages)
1430 zonelist->zones[j++] = zone;
1433 return j;
1436 static inline int highest_zone(int zone_bits)
1438 int res = ZONE_NORMAL;
1439 if (zone_bits & (__force int)__GFP_HIGHMEM)
1440 res = ZONE_HIGHMEM;
1441 if (zone_bits & (__force int)__GFP_DMA32)
1442 res = ZONE_DMA32;
1443 if (zone_bits & (__force int)__GFP_DMA)
1444 res = ZONE_DMA;
1445 return res;
1448 #ifdef CONFIG_NUMA
1449 #define MAX_NODE_LOAD (num_online_nodes())
1450 static int __initdata node_load[MAX_NUMNODES];
1452 * find_next_best_node - find the next node that should appear in a given node's fallback list
1453 * @node: node whose fallback list we're appending
1454 * @used_node_mask: nodemask_t of already used nodes
1456 * We use a number of factors to determine which is the next node that should
1457 * appear on a given node's fallback list. The node should not have appeared
1458 * already in @node's fallback list, and it should be the next closest node
1459 * according to the distance array (which contains arbitrary distance values
1460 * from each node to each node in the system), and should also prefer nodes
1461 * with no CPUs, since presumably they'll have very little allocation pressure
1462 * on them otherwise.
1463 * It returns -1 if no node is found.
1465 static int __init find_next_best_node(int node, nodemask_t *used_node_mask)
1467 int i, n, val;
1468 int min_val = INT_MAX;
1469 int best_node = -1;
1471 for_each_online_node(i) {
1472 cpumask_t tmp;
1474 /* Start from local node */
1475 n = (node+i) % num_online_nodes();
1477 /* Don't want a node to appear more than once */
1478 if (node_isset(n, *used_node_mask))
1479 continue;
1481 /* Use the local node if we haven't already */
1482 if (!node_isset(node, *used_node_mask)) {
1483 best_node = node;
1484 break;
1487 /* Use the distance array to find the distance */
1488 val = node_distance(node, n);
1490 /* Give preference to headless and unused nodes */
1491 tmp = node_to_cpumask(n);
1492 if (!cpus_empty(tmp))
1493 val += PENALTY_FOR_NODE_WITH_CPUS;
1495 /* Slight preference for less loaded node */
1496 val *= (MAX_NODE_LOAD*MAX_NUMNODES);
1497 val += node_load[n];
1499 if (val < min_val) {
1500 min_val = val;
1501 best_node = n;
1505 if (best_node >= 0)
1506 node_set(best_node, *used_node_mask);
1508 return best_node;
1511 static void __init build_zonelists(pg_data_t *pgdat)
1513 int i, j, k, node, local_node;
1514 int prev_node, load;
1515 struct zonelist *zonelist;
1516 nodemask_t used_mask;
1518 /* initialize zonelists */
1519 for (i = 0; i < GFP_ZONETYPES; i++) {
1520 zonelist = pgdat->node_zonelists + i;
1521 zonelist->zones[0] = NULL;
1524 /* NUMA-aware ordering of nodes */
1525 local_node = pgdat->node_id;
1526 load = num_online_nodes();
1527 prev_node = local_node;
1528 nodes_clear(used_mask);
1529 while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
1531 * We don't want to pressure a particular node.
1532 * So adding penalty to the first node in same
1533 * distance group to make it round-robin.
1535 if (node_distance(local_node, node) !=
1536 node_distance(local_node, prev_node))
1537 node_load[node] += load;
1538 prev_node = node;
1539 load--;
1540 for (i = 0; i < GFP_ZONETYPES; i++) {
1541 zonelist = pgdat->node_zonelists + i;
1542 for (j = 0; zonelist->zones[j] != NULL; j++);
1544 k = highest_zone(i);
1546 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1547 zonelist->zones[j] = NULL;
1552 #else /* CONFIG_NUMA */
1554 static void __init build_zonelists(pg_data_t *pgdat)
1556 int i, j, k, node, local_node;
1558 local_node = pgdat->node_id;
1559 for (i = 0; i < GFP_ZONETYPES; i++) {
1560 struct zonelist *zonelist;
1562 zonelist = pgdat->node_zonelists + i;
1564 j = 0;
1565 k = highest_zone(i);
1566 j = build_zonelists_node(pgdat, zonelist, j, k);
1568 * Now we build the zonelist so that it contains the zones
1569 * of all the other nodes.
1570 * We don't want to pressure a particular node, so when
1571 * building the zones for node N, we make sure that the
1572 * zones coming right after the local ones are those from
1573 * node N+1 (modulo N)
1575 for (node = local_node + 1; node < MAX_NUMNODES; node++) {
1576 if (!node_online(node))
1577 continue;
1578 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1580 for (node = 0; node < local_node; node++) {
1581 if (!node_online(node))
1582 continue;
1583 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1586 zonelist->zones[j] = NULL;
1590 #endif /* CONFIG_NUMA */
1592 void __init build_all_zonelists(void)
1594 int i;
1596 for_each_online_node(i)
1597 build_zonelists(NODE_DATA(i));
1598 printk("Built %i zonelists\n", num_online_nodes());
1599 cpuset_init_current_mems_allowed();
1603 * Helper functions to size the waitqueue hash table.
1604 * Essentially these want to choose hash table sizes sufficiently
1605 * large so that collisions trying to wait on pages are rare.
1606 * But in fact, the number of active page waitqueues on typical
1607 * systems is ridiculously low, less than 200. So this is even
1608 * conservative, even though it seems large.
1610 * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
1611 * waitqueues, i.e. the size of the waitq table given the number of pages.
1613 #define PAGES_PER_WAITQUEUE 256
1615 static inline unsigned long wait_table_size(unsigned long pages)
1617 unsigned long size = 1;
1619 pages /= PAGES_PER_WAITQUEUE;
1621 while (size < pages)
1622 size <<= 1;
1625 * Once we have dozens or even hundreds of threads sleeping
1626 * on IO we've got bigger problems than wait queue collision.
1627 * Limit the size of the wait table to a reasonable size.
1629 size = min(size, 4096UL);
1631 return max(size, 4UL);
1635 * This is an integer logarithm so that shifts can be used later
1636 * to extract the more random high bits from the multiplicative
1637 * hash function before the remainder is taken.
1639 static inline unsigned long wait_table_bits(unsigned long size)
1641 return ffz(~size);
1644 #define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
1646 static void __init calculate_zone_totalpages(struct pglist_data *pgdat,
1647 unsigned long *zones_size, unsigned long *zholes_size)
1649 unsigned long realtotalpages, totalpages = 0;
1650 int i;
1652 for (i = 0; i < MAX_NR_ZONES; i++)
1653 totalpages += zones_size[i];
1654 pgdat->node_spanned_pages = totalpages;
1656 realtotalpages = totalpages;
1657 if (zholes_size)
1658 for (i = 0; i < MAX_NR_ZONES; i++)
1659 realtotalpages -= zholes_size[i];
1660 pgdat->node_present_pages = realtotalpages;
1661 printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id, realtotalpages);
1666 * Initially all pages are reserved - free ones are freed
1667 * up by free_all_bootmem() once the early boot process is
1668 * done. Non-atomic initialization, single-pass.
1670 void __devinit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
1671 unsigned long start_pfn)
1673 struct page *page;
1674 unsigned long end_pfn = start_pfn + size;
1675 unsigned long pfn;
1677 for (pfn = start_pfn; pfn < end_pfn; pfn++, page++) {
1678 if (!early_pfn_valid(pfn))
1679 continue;
1680 if (!early_pfn_in_nid(pfn, nid))
1681 continue;
1682 page = pfn_to_page(pfn);
1683 set_page_links(page, zone, nid, pfn);
1684 set_page_count(page, 1);
1685 reset_page_mapcount(page);
1686 SetPageReserved(page);
1687 INIT_LIST_HEAD(&page->lru);
1688 #ifdef WANT_PAGE_VIRTUAL
1689 /* The shift won't overflow because ZONE_NORMAL is below 4G. */
1690 if (!is_highmem_idx(zone))
1691 set_page_address(page, __va(pfn << PAGE_SHIFT));
1692 #endif
1696 void zone_init_free_lists(struct pglist_data *pgdat, struct zone *zone,
1697 unsigned long size)
1699 int order;
1700 for (order = 0; order < MAX_ORDER ; order++) {
1701 INIT_LIST_HEAD(&zone->free_area[order].free_list);
1702 zone->free_area[order].nr_free = 0;
1706 #define ZONETABLE_INDEX(x, zone_nr) ((x << ZONES_SHIFT) | zone_nr)
1707 void zonetable_add(struct zone *zone, int nid, int zid, unsigned long pfn,
1708 unsigned long size)
1710 unsigned long snum = pfn_to_section_nr(pfn);
1711 unsigned long end = pfn_to_section_nr(pfn + size);
1713 if (FLAGS_HAS_NODE)
1714 zone_table[ZONETABLE_INDEX(nid, zid)] = zone;
1715 else
1716 for (; snum <= end; snum++)
1717 zone_table[ZONETABLE_INDEX(snum, zid)] = zone;
1720 #ifndef __HAVE_ARCH_MEMMAP_INIT
1721 #define memmap_init(size, nid, zone, start_pfn) \
1722 memmap_init_zone((size), (nid), (zone), (start_pfn))
1723 #endif
1725 static int __devinit zone_batchsize(struct zone *zone)
1727 int batch;
1730 * The per-cpu-pages pools are set to around 1000th of the
1731 * size of the zone. But no more than 1/2 of a meg.
1733 * OK, so we don't know how big the cache is. So guess.
1735 batch = zone->present_pages / 1024;
1736 if (batch * PAGE_SIZE > 512 * 1024)
1737 batch = (512 * 1024) / PAGE_SIZE;
1738 batch /= 4; /* We effectively *= 4 below */
1739 if (batch < 1)
1740 batch = 1;
1743 * We will be trying to allcoate bigger chunks of contiguous
1744 * memory of the order of fls(batch). This should result in
1745 * better cache coloring.
1747 * A sanity check also to ensure that batch is still in limits.
1749 batch = (1 << fls(batch + batch/2));
1751 if (fls(batch) >= (PAGE_SHIFT + MAX_ORDER - 2))
1752 batch = PAGE_SHIFT + ((MAX_ORDER - 1 - PAGE_SHIFT)/2);
1754 return batch;
1757 inline void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
1759 struct per_cpu_pages *pcp;
1761 memset(p, 0, sizeof(*p));
1763 pcp = &p->pcp[0]; /* hot */
1764 pcp->count = 0;
1765 pcp->low = 0;
1766 pcp->high = 6 * batch;
1767 pcp->batch = max(1UL, 1 * batch);
1768 INIT_LIST_HEAD(&pcp->list);
1770 pcp = &p->pcp[1]; /* cold*/
1771 pcp->count = 0;
1772 pcp->low = 0;
1773 pcp->high = 2 * batch;
1774 pcp->batch = max(1UL, batch/2);
1775 INIT_LIST_HEAD(&pcp->list);
1778 #ifdef CONFIG_NUMA
1780 * Boot pageset table. One per cpu which is going to be used for all
1781 * zones and all nodes. The parameters will be set in such a way
1782 * that an item put on a list will immediately be handed over to
1783 * the buddy list. This is safe since pageset manipulation is done
1784 * with interrupts disabled.
1786 * Some NUMA counter updates may also be caught by the boot pagesets.
1788 * The boot_pagesets must be kept even after bootup is complete for
1789 * unused processors and/or zones. They do play a role for bootstrapping
1790 * hotplugged processors.
1792 * zoneinfo_show() and maybe other functions do
1793 * not check if the processor is online before following the pageset pointer.
1794 * Other parts of the kernel may not check if the zone is available.
1796 static struct per_cpu_pageset
1797 boot_pageset[NR_CPUS];
1800 * Dynamically allocate memory for the
1801 * per cpu pageset array in struct zone.
1803 static int __devinit process_zones(int cpu)
1805 struct zone *zone, *dzone;
1807 for_each_zone(zone) {
1809 zone->pageset[cpu] = kmalloc_node(sizeof(struct per_cpu_pageset),
1810 GFP_KERNEL, cpu_to_node(cpu));
1811 if (!zone->pageset[cpu])
1812 goto bad;
1814 setup_pageset(zone->pageset[cpu], zone_batchsize(zone));
1817 return 0;
1818 bad:
1819 for_each_zone(dzone) {
1820 if (dzone == zone)
1821 break;
1822 kfree(dzone->pageset[cpu]);
1823 dzone->pageset[cpu] = NULL;
1825 return -ENOMEM;
1828 static inline void free_zone_pagesets(int cpu)
1830 #ifdef CONFIG_NUMA
1831 struct zone *zone;
1833 for_each_zone(zone) {
1834 struct per_cpu_pageset *pset = zone_pcp(zone, cpu);
1836 zone_pcp(zone, cpu) = NULL;
1837 kfree(pset);
1839 #endif
1842 static int __devinit pageset_cpuup_callback(struct notifier_block *nfb,
1843 unsigned long action,
1844 void *hcpu)
1846 int cpu = (long)hcpu;
1847 int ret = NOTIFY_OK;
1849 switch (action) {
1850 case CPU_UP_PREPARE:
1851 if (process_zones(cpu))
1852 ret = NOTIFY_BAD;
1853 break;
1854 case CPU_UP_CANCELED:
1855 case CPU_DEAD:
1856 free_zone_pagesets(cpu);
1857 break;
1858 default:
1859 break;
1861 return ret;
1864 static struct notifier_block pageset_notifier =
1865 { &pageset_cpuup_callback, NULL, 0 };
1867 void __init setup_per_cpu_pageset()
1869 int err;
1871 /* Initialize per_cpu_pageset for cpu 0.
1872 * A cpuup callback will do this for every cpu
1873 * as it comes online
1875 err = process_zones(smp_processor_id());
1876 BUG_ON(err);
1877 register_cpu_notifier(&pageset_notifier);
1880 #endif
1882 static __devinit
1883 void zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages)
1885 int i;
1886 struct pglist_data *pgdat = zone->zone_pgdat;
1889 * The per-page waitqueue mechanism uses hashed waitqueues
1890 * per zone.
1892 zone->wait_table_size = wait_table_size(zone_size_pages);
1893 zone->wait_table_bits = wait_table_bits(zone->wait_table_size);
1894 zone->wait_table = (wait_queue_head_t *)
1895 alloc_bootmem_node(pgdat, zone->wait_table_size
1896 * sizeof(wait_queue_head_t));
1898 for(i = 0; i < zone->wait_table_size; ++i)
1899 init_waitqueue_head(zone->wait_table + i);
1902 static __devinit void zone_pcp_init(struct zone *zone)
1904 int cpu;
1905 unsigned long batch = zone_batchsize(zone);
1907 for (cpu = 0; cpu < NR_CPUS; cpu++) {
1908 #ifdef CONFIG_NUMA
1909 /* Early boot. Slab allocator not functional yet */
1910 zone->pageset[cpu] = &boot_pageset[cpu];
1911 setup_pageset(&boot_pageset[cpu],0);
1912 #else
1913 setup_pageset(zone_pcp(zone,cpu), batch);
1914 #endif
1916 printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%lu\n",
1917 zone->name, zone->present_pages, batch);
1920 static __devinit void init_currently_empty_zone(struct zone *zone,
1921 unsigned long zone_start_pfn, unsigned long size)
1923 struct pglist_data *pgdat = zone->zone_pgdat;
1925 zone_wait_table_init(zone, size);
1926 pgdat->nr_zones = zone_idx(zone) + 1;
1928 zone->zone_mem_map = pfn_to_page(zone_start_pfn);
1929 zone->zone_start_pfn = zone_start_pfn;
1931 memmap_init(size, pgdat->node_id, zone_idx(zone), zone_start_pfn);
1933 zone_init_free_lists(pgdat, zone, zone->spanned_pages);
1937 * Set up the zone data structures:
1938 * - mark all pages reserved
1939 * - mark all memory queues empty
1940 * - clear the memory bitmaps
1942 static void __init free_area_init_core(struct pglist_data *pgdat,
1943 unsigned long *zones_size, unsigned long *zholes_size)
1945 unsigned long j;
1946 int nid = pgdat->node_id;
1947 unsigned long zone_start_pfn = pgdat->node_start_pfn;
1949 pgdat_resize_init(pgdat);
1950 pgdat->nr_zones = 0;
1951 init_waitqueue_head(&pgdat->kswapd_wait);
1952 pgdat->kswapd_max_order = 0;
1954 for (j = 0; j < MAX_NR_ZONES; j++) {
1955 struct zone *zone = pgdat->node_zones + j;
1956 unsigned long size, realsize;
1958 realsize = size = zones_size[j];
1959 if (zholes_size)
1960 realsize -= zholes_size[j];
1962 if (j < ZONE_HIGHMEM)
1963 nr_kernel_pages += realsize;
1964 nr_all_pages += realsize;
1966 zone->spanned_pages = size;
1967 zone->present_pages = realsize;
1968 zone->name = zone_names[j];
1969 spin_lock_init(&zone->lock);
1970 spin_lock_init(&zone->lru_lock);
1971 zone_seqlock_init(zone);
1972 zone->zone_pgdat = pgdat;
1973 zone->free_pages = 0;
1975 zone->temp_priority = zone->prev_priority = DEF_PRIORITY;
1977 zone_pcp_init(zone);
1978 INIT_LIST_HEAD(&zone->active_list);
1979 INIT_LIST_HEAD(&zone->inactive_list);
1980 zone->nr_scan_active = 0;
1981 zone->nr_scan_inactive = 0;
1982 zone->nr_active = 0;
1983 zone->nr_inactive = 0;
1984 atomic_set(&zone->reclaim_in_progress, 0);
1985 if (!size)
1986 continue;
1988 zonetable_add(zone, nid, j, zone_start_pfn, size);
1989 init_currently_empty_zone(zone, zone_start_pfn, size);
1990 zone_start_pfn += size;
1994 static void __init alloc_node_mem_map(struct pglist_data *pgdat)
1996 /* Skip empty nodes */
1997 if (!pgdat->node_spanned_pages)
1998 return;
2000 #ifdef CONFIG_FLAT_NODE_MEM_MAP
2001 /* ia64 gets its own node_mem_map, before this, without bootmem */
2002 if (!pgdat->node_mem_map) {
2003 unsigned long size;
2004 struct page *map;
2006 size = (pgdat->node_spanned_pages + 1) * sizeof(struct page);
2007 map = alloc_remap(pgdat->node_id, size);
2008 if (!map)
2009 map = alloc_bootmem_node(pgdat, size);
2010 pgdat->node_mem_map = map;
2012 #ifdef CONFIG_FLATMEM
2014 * With no DISCONTIG, the global mem_map is just set as node 0's
2016 if (pgdat == NODE_DATA(0))
2017 mem_map = NODE_DATA(0)->node_mem_map;
2018 #endif
2019 #endif /* CONFIG_FLAT_NODE_MEM_MAP */
2022 void __init free_area_init_node(int nid, struct pglist_data *pgdat,
2023 unsigned long *zones_size, unsigned long node_start_pfn,
2024 unsigned long *zholes_size)
2026 pgdat->node_id = nid;
2027 pgdat->node_start_pfn = node_start_pfn;
2028 calculate_zone_totalpages(pgdat, zones_size, zholes_size);
2030 alloc_node_mem_map(pgdat);
2032 free_area_init_core(pgdat, zones_size, zholes_size);
2035 #ifndef CONFIG_NEED_MULTIPLE_NODES
2036 static bootmem_data_t contig_bootmem_data;
2037 struct pglist_data contig_page_data = { .bdata = &contig_bootmem_data };
2039 EXPORT_SYMBOL(contig_page_data);
2040 #endif
2042 void __init free_area_init(unsigned long *zones_size)
2044 free_area_init_node(0, NODE_DATA(0), zones_size,
2045 __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
2048 #ifdef CONFIG_PROC_FS
2050 #include <linux/seq_file.h>
2052 static void *frag_start(struct seq_file *m, loff_t *pos)
2054 pg_data_t *pgdat;
2055 loff_t node = *pos;
2057 for (pgdat = pgdat_list; pgdat && node; pgdat = pgdat->pgdat_next)
2058 --node;
2060 return pgdat;
2063 static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
2065 pg_data_t *pgdat = (pg_data_t *)arg;
2067 (*pos)++;
2068 return pgdat->pgdat_next;
2071 static void frag_stop(struct seq_file *m, void *arg)
2076 * This walks the free areas for each zone.
2078 static int frag_show(struct seq_file *m, void *arg)
2080 pg_data_t *pgdat = (pg_data_t *)arg;
2081 struct zone *zone;
2082 struct zone *node_zones = pgdat->node_zones;
2083 unsigned long flags;
2084 int order;
2086 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
2087 if (!zone->present_pages)
2088 continue;
2090 spin_lock_irqsave(&zone->lock, flags);
2091 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
2092 for (order = 0; order < MAX_ORDER; ++order)
2093 seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
2094 spin_unlock_irqrestore(&zone->lock, flags);
2095 seq_putc(m, '\n');
2097 return 0;
2100 struct seq_operations fragmentation_op = {
2101 .start = frag_start,
2102 .next = frag_next,
2103 .stop = frag_stop,
2104 .show = frag_show,
2108 * Output information about zones in @pgdat.
2110 static int zoneinfo_show(struct seq_file *m, void *arg)
2112 pg_data_t *pgdat = arg;
2113 struct zone *zone;
2114 struct zone *node_zones = pgdat->node_zones;
2115 unsigned long flags;
2117 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; zone++) {
2118 int i;
2120 if (!zone->present_pages)
2121 continue;
2123 spin_lock_irqsave(&zone->lock, flags);
2124 seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
2125 seq_printf(m,
2126 "\n pages free %lu"
2127 "\n min %lu"
2128 "\n low %lu"
2129 "\n high %lu"
2130 "\n active %lu"
2131 "\n inactive %lu"
2132 "\n scanned %lu (a: %lu i: %lu)"
2133 "\n spanned %lu"
2134 "\n present %lu",
2135 zone->free_pages,
2136 zone->pages_min,
2137 zone->pages_low,
2138 zone->pages_high,
2139 zone->nr_active,
2140 zone->nr_inactive,
2141 zone->pages_scanned,
2142 zone->nr_scan_active, zone->nr_scan_inactive,
2143 zone->spanned_pages,
2144 zone->present_pages);
2145 seq_printf(m,
2146 "\n protection: (%lu",
2147 zone->lowmem_reserve[0]);
2148 for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
2149 seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
2150 seq_printf(m,
2152 "\n pagesets");
2153 for (i = 0; i < ARRAY_SIZE(zone->pageset); i++) {
2154 struct per_cpu_pageset *pageset;
2155 int j;
2157 pageset = zone_pcp(zone, i);
2158 for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
2159 if (pageset->pcp[j].count)
2160 break;
2162 if (j == ARRAY_SIZE(pageset->pcp))
2163 continue;
2164 for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
2165 seq_printf(m,
2166 "\n cpu: %i pcp: %i"
2167 "\n count: %i"
2168 "\n low: %i"
2169 "\n high: %i"
2170 "\n batch: %i",
2171 i, j,
2172 pageset->pcp[j].count,
2173 pageset->pcp[j].low,
2174 pageset->pcp[j].high,
2175 pageset->pcp[j].batch);
2177 #ifdef CONFIG_NUMA
2178 seq_printf(m,
2179 "\n numa_hit: %lu"
2180 "\n numa_miss: %lu"
2181 "\n numa_foreign: %lu"
2182 "\n interleave_hit: %lu"
2183 "\n local_node: %lu"
2184 "\n other_node: %lu",
2185 pageset->numa_hit,
2186 pageset->numa_miss,
2187 pageset->numa_foreign,
2188 pageset->interleave_hit,
2189 pageset->local_node,
2190 pageset->other_node);
2191 #endif
2193 seq_printf(m,
2194 "\n all_unreclaimable: %u"
2195 "\n prev_priority: %i"
2196 "\n temp_priority: %i"
2197 "\n start_pfn: %lu",
2198 zone->all_unreclaimable,
2199 zone->prev_priority,
2200 zone->temp_priority,
2201 zone->zone_start_pfn);
2202 spin_unlock_irqrestore(&zone->lock, flags);
2203 seq_putc(m, '\n');
2205 return 0;
2208 struct seq_operations zoneinfo_op = {
2209 .start = frag_start, /* iterate over all zones. The same as in
2210 * fragmentation. */
2211 .next = frag_next,
2212 .stop = frag_stop,
2213 .show = zoneinfo_show,
2216 static char *vmstat_text[] = {
2217 "nr_dirty",
2218 "nr_writeback",
2219 "nr_unstable",
2220 "nr_page_table_pages",
2221 "nr_mapped",
2222 "nr_slab",
2224 "pgpgin",
2225 "pgpgout",
2226 "pswpin",
2227 "pswpout",
2228 "pgalloc_high",
2230 "pgalloc_normal",
2231 "pgalloc_dma",
2232 "pgfree",
2233 "pgactivate",
2234 "pgdeactivate",
2236 "pgfault",
2237 "pgmajfault",
2238 "pgrefill_high",
2239 "pgrefill_normal",
2240 "pgrefill_dma",
2242 "pgsteal_high",
2243 "pgsteal_normal",
2244 "pgsteal_dma",
2245 "pgscan_kswapd_high",
2246 "pgscan_kswapd_normal",
2248 "pgscan_kswapd_dma",
2249 "pgscan_direct_high",
2250 "pgscan_direct_normal",
2251 "pgscan_direct_dma",
2252 "pginodesteal",
2254 "slabs_scanned",
2255 "kswapd_steal",
2256 "kswapd_inodesteal",
2257 "pageoutrun",
2258 "allocstall",
2260 "pgrotated",
2261 "nr_bounce",
2264 static void *vmstat_start(struct seq_file *m, loff_t *pos)
2266 struct page_state *ps;
2268 if (*pos >= ARRAY_SIZE(vmstat_text))
2269 return NULL;
2271 ps = kmalloc(sizeof(*ps), GFP_KERNEL);
2272 m->private = ps;
2273 if (!ps)
2274 return ERR_PTR(-ENOMEM);
2275 get_full_page_state(ps);
2276 ps->pgpgin /= 2; /* sectors -> kbytes */
2277 ps->pgpgout /= 2;
2278 return (unsigned long *)ps + *pos;
2281 static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
2283 (*pos)++;
2284 if (*pos >= ARRAY_SIZE(vmstat_text))
2285 return NULL;
2286 return (unsigned long *)m->private + *pos;
2289 static int vmstat_show(struct seq_file *m, void *arg)
2291 unsigned long *l = arg;
2292 unsigned long off = l - (unsigned long *)m->private;
2294 seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
2295 return 0;
2298 static void vmstat_stop(struct seq_file *m, void *arg)
2300 kfree(m->private);
2301 m->private = NULL;
2304 struct seq_operations vmstat_op = {
2305 .start = vmstat_start,
2306 .next = vmstat_next,
2307 .stop = vmstat_stop,
2308 .show = vmstat_show,
2311 #endif /* CONFIG_PROC_FS */
2313 #ifdef CONFIG_HOTPLUG_CPU
2314 static int page_alloc_cpu_notify(struct notifier_block *self,
2315 unsigned long action, void *hcpu)
2317 int cpu = (unsigned long)hcpu;
2318 long *count;
2319 unsigned long *src, *dest;
2321 if (action == CPU_DEAD) {
2322 int i;
2324 /* Drain local pagecache count. */
2325 count = &per_cpu(nr_pagecache_local, cpu);
2326 atomic_add(*count, &nr_pagecache);
2327 *count = 0;
2328 local_irq_disable();
2329 __drain_pages(cpu);
2331 /* Add dead cpu's page_states to our own. */
2332 dest = (unsigned long *)&__get_cpu_var(page_states);
2333 src = (unsigned long *)&per_cpu(page_states, cpu);
2335 for (i = 0; i < sizeof(struct page_state)/sizeof(unsigned long);
2336 i++) {
2337 dest[i] += src[i];
2338 src[i] = 0;
2341 local_irq_enable();
2343 return NOTIFY_OK;
2345 #endif /* CONFIG_HOTPLUG_CPU */
2347 void __init page_alloc_init(void)
2349 hotcpu_notifier(page_alloc_cpu_notify, 0);
2353 * setup_per_zone_lowmem_reserve - called whenever
2354 * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
2355 * has a correct pages reserved value, so an adequate number of
2356 * pages are left in the zone after a successful __alloc_pages().
2358 static void setup_per_zone_lowmem_reserve(void)
2360 struct pglist_data *pgdat;
2361 int j, idx;
2363 for_each_pgdat(pgdat) {
2364 for (j = 0; j < MAX_NR_ZONES; j++) {
2365 struct zone *zone = pgdat->node_zones + j;
2366 unsigned long present_pages = zone->present_pages;
2368 zone->lowmem_reserve[j] = 0;
2370 for (idx = j-1; idx >= 0; idx--) {
2371 struct zone *lower_zone;
2373 if (sysctl_lowmem_reserve_ratio[idx] < 1)
2374 sysctl_lowmem_reserve_ratio[idx] = 1;
2376 lower_zone = pgdat->node_zones + idx;
2377 lower_zone->lowmem_reserve[j] = present_pages /
2378 sysctl_lowmem_reserve_ratio[idx];
2379 present_pages += lower_zone->present_pages;
2386 * setup_per_zone_pages_min - called when min_free_kbytes changes. Ensures
2387 * that the pages_{min,low,high} values for each zone are set correctly
2388 * with respect to min_free_kbytes.
2390 void setup_per_zone_pages_min(void)
2392 unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
2393 unsigned long lowmem_pages = 0;
2394 struct zone *zone;
2395 unsigned long flags;
2397 /* Calculate total number of !ZONE_HIGHMEM pages */
2398 for_each_zone(zone) {
2399 if (!is_highmem(zone))
2400 lowmem_pages += zone->present_pages;
2403 for_each_zone(zone) {
2404 unsigned long tmp;
2405 spin_lock_irqsave(&zone->lru_lock, flags);
2406 tmp = (pages_min * zone->present_pages) / lowmem_pages;
2407 if (is_highmem(zone)) {
2409 * __GFP_HIGH and PF_MEMALLOC allocations usually don't
2410 * need highmem pages, so cap pages_min to a small
2411 * value here.
2413 * The (pages_high-pages_low) and (pages_low-pages_min)
2414 * deltas controls asynch page reclaim, and so should
2415 * not be capped for highmem.
2417 int min_pages;
2419 min_pages = zone->present_pages / 1024;
2420 if (min_pages < SWAP_CLUSTER_MAX)
2421 min_pages = SWAP_CLUSTER_MAX;
2422 if (min_pages > 128)
2423 min_pages = 128;
2424 zone->pages_min = min_pages;
2425 } else {
2427 * If it's a lowmem zone, reserve a number of pages
2428 * proportionate to the zone's size.
2430 zone->pages_min = tmp;
2433 zone->pages_low = zone->pages_min + tmp / 4;
2434 zone->pages_high = zone->pages_min + tmp / 2;
2435 spin_unlock_irqrestore(&zone->lru_lock, flags);
2440 * Initialise min_free_kbytes.
2442 * For small machines we want it small (128k min). For large machines
2443 * we want it large (64MB max). But it is not linear, because network
2444 * bandwidth does not increase linearly with machine size. We use
2446 * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
2447 * min_free_kbytes = sqrt(lowmem_kbytes * 16)
2449 * which yields
2451 * 16MB: 512k
2452 * 32MB: 724k
2453 * 64MB: 1024k
2454 * 128MB: 1448k
2455 * 256MB: 2048k
2456 * 512MB: 2896k
2457 * 1024MB: 4096k
2458 * 2048MB: 5792k
2459 * 4096MB: 8192k
2460 * 8192MB: 11584k
2461 * 16384MB: 16384k
2463 static int __init init_per_zone_pages_min(void)
2465 unsigned long lowmem_kbytes;
2467 lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
2469 min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
2470 if (min_free_kbytes < 128)
2471 min_free_kbytes = 128;
2472 if (min_free_kbytes > 65536)
2473 min_free_kbytes = 65536;
2474 setup_per_zone_pages_min();
2475 setup_per_zone_lowmem_reserve();
2476 return 0;
2478 module_init(init_per_zone_pages_min)
2481 * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
2482 * that we can call two helper functions whenever min_free_kbytes
2483 * changes.
2485 int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
2486 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
2488 proc_dointvec(table, write, file, buffer, length, ppos);
2489 setup_per_zone_pages_min();
2490 return 0;
2494 * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
2495 * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
2496 * whenever sysctl_lowmem_reserve_ratio changes.
2498 * The reserve ratio obviously has absolutely no relation with the
2499 * pages_min watermarks. The lowmem reserve ratio can only make sense
2500 * if in function of the boot time zone sizes.
2502 int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
2503 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
2505 proc_dointvec_minmax(table, write, file, buffer, length, ppos);
2506 setup_per_zone_lowmem_reserve();
2507 return 0;
2510 __initdata int hashdist = HASHDIST_DEFAULT;
2512 #ifdef CONFIG_NUMA
2513 static int __init set_hashdist(char *str)
2515 if (!str)
2516 return 0;
2517 hashdist = simple_strtoul(str, &str, 0);
2518 return 1;
2520 __setup("hashdist=", set_hashdist);
2521 #endif
2524 * allocate a large system hash table from bootmem
2525 * - it is assumed that the hash table must contain an exact power-of-2
2526 * quantity of entries
2527 * - limit is the number of hash buckets, not the total allocation size
2529 void *__init alloc_large_system_hash(const char *tablename,
2530 unsigned long bucketsize,
2531 unsigned long numentries,
2532 int scale,
2533 int flags,
2534 unsigned int *_hash_shift,
2535 unsigned int *_hash_mask,
2536 unsigned long limit)
2538 unsigned long long max = limit;
2539 unsigned long log2qty, size;
2540 void *table = NULL;
2542 /* allow the kernel cmdline to have a say */
2543 if (!numentries) {
2544 /* round applicable memory size up to nearest megabyte */
2545 numentries = (flags & HASH_HIGHMEM) ? nr_all_pages : nr_kernel_pages;
2546 numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
2547 numentries >>= 20 - PAGE_SHIFT;
2548 numentries <<= 20 - PAGE_SHIFT;
2550 /* limit to 1 bucket per 2^scale bytes of low memory */
2551 if (scale > PAGE_SHIFT)
2552 numentries >>= (scale - PAGE_SHIFT);
2553 else
2554 numentries <<= (PAGE_SHIFT - scale);
2556 /* rounded up to nearest power of 2 in size */
2557 numentries = 1UL << (long_log2(numentries) + 1);
2559 /* limit allocation size to 1/16 total memory by default */
2560 if (max == 0) {
2561 max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
2562 do_div(max, bucketsize);
2565 if (numentries > max)
2566 numentries = max;
2568 log2qty = long_log2(numentries);
2570 do {
2571 size = bucketsize << log2qty;
2572 if (flags & HASH_EARLY)
2573 table = alloc_bootmem(size);
2574 else if (hashdist)
2575 table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
2576 else {
2577 unsigned long order;
2578 for (order = 0; ((1UL << order) << PAGE_SHIFT) < size; order++)
2580 table = (void*) __get_free_pages(GFP_ATOMIC, order);
2582 } while (!table && size > PAGE_SIZE && --log2qty);
2584 if (!table)
2585 panic("Failed to allocate %s hash table\n", tablename);
2587 printk("%s hash table entries: %d (order: %d, %lu bytes)\n",
2588 tablename,
2589 (1U << log2qty),
2590 long_log2(size) - PAGE_SHIFT,
2591 size);
2593 if (_hash_shift)
2594 *_hash_shift = log2qty;
2595 if (_hash_mask)
2596 *_hash_mask = (1 << log2qty) - 1;
2598 return table;