2 * linux/mm/page_alloc.c
4 * Manages the free list, the system allocates free pages here.
5 * Note that kmalloc() lives in slab.c
7 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
8 * Swap reorganised 29.12.95, Stephen Tweedie
9 * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
10 * Reshaped it to be a zoned allocator, Ingo Molnar, Red Hat, 1999
11 * Discontiguous memory support, Kanoj Sarcar, SGI, Nov 1999
12 * Zone balancing, Kanoj Sarcar, SGI, Jan 2000
13 * Per cpu hot/cold page lists, bulk allocation, Martin J. Bligh, Sept 2002
14 * (lots of bits borrowed from Ingo Molnar & Andrew Morton)
17 #include <linux/config.h>
18 #include <linux/stddef.h>
20 #include <linux/swap.h>
21 #include <linux/interrupt.h>
22 #include <linux/pagemap.h>
23 #include <linux/bootmem.h>
24 #include <linux/compiler.h>
25 #include <linux/module.h>
26 #include <linux/suspend.h>
27 #include <linux/pagevec.h>
28 #include <linux/blkdev.h>
29 #include <linux/slab.h>
30 #include <linux/notifier.h>
31 #include <linux/topology.h>
32 #include <linux/sysctl.h>
33 #include <linux/cpu.h>
34 #include <linux/cpuset.h>
35 #include <linux/nodemask.h>
36 #include <linux/vmalloc.h>
38 #include <asm/tlbflush.h>
42 * MCD - HACK: Find somewhere to initialize this EARLY, or make this
45 nodemask_t node_online_map = { { [0] = 1UL } };
46 EXPORT_SYMBOL(node_online_map);
47 nodemask_t node_possible_map = NODE_MASK_ALL;
48 EXPORT_SYMBOL(node_possible_map);
49 struct pglist_data *pgdat_list;
50 unsigned long totalram_pages;
51 unsigned long totalhigh_pages;
55 * results with 256, 32 in the lowmem_reserve sysctl:
56 * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
57 * 1G machine -> (16M dma, 784M normal, 224M high)
58 * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
59 * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
60 * HIGHMEM allocation will (224M+784M)/256 of ram reserved in ZONE_DMA
62 int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1] = { 256, 32 };
64 EXPORT_SYMBOL(totalram_pages);
65 EXPORT_SYMBOL(nr_swap_pages);
68 * Used by page_zone() to look up the address of the struct zone whose
69 * id is encoded in the upper bits of page->flags
71 struct zone *zone_table[1 << (ZONES_SHIFT + NODES_SHIFT)];
72 EXPORT_SYMBOL(zone_table);
74 static char *zone_names[MAX_NR_ZONES] = { "DMA", "Normal", "HighMem" };
75 int min_free_kbytes = 1024;
77 unsigned long __initdata nr_kernel_pages;
78 unsigned long __initdata nr_all_pages;
81 * Temporary debugging check for pages not lying within a given zone.
83 static int bad_range(struct zone *zone, struct page *page)
85 if (page_to_pfn(page) >= zone->zone_start_pfn + zone->spanned_pages)
87 if (page_to_pfn(page) < zone->zone_start_pfn)
89 #ifdef CONFIG_HOLES_IN_ZONE
90 if (!pfn_valid(page_to_pfn(page)))
93 if (zone != page_zone(page))
98 static void bad_page(const char *function, struct page *page)
100 printk(KERN_EMERG "Bad page state at %s (in process '%s', page %p)\n",
101 function, current->comm, page);
102 printk(KERN_EMERG "flags:0x%0*lx mapping:%p mapcount:%d count:%d\n",
103 (int)(2*sizeof(page_flags_t)), (unsigned long)page->flags,
104 page->mapping, page_mapcount(page), page_count(page));
105 printk(KERN_EMERG "Backtrace:\n");
107 printk(KERN_EMERG "Trying to fix it up, but a reboot is needed\n");
108 page->flags &= ~(1 << PG_private |
115 set_page_count(page, 0);
116 reset_page_mapcount(page);
117 page->mapping = NULL;
118 tainted |= TAINT_BAD_PAGE;
121 #ifndef CONFIG_HUGETLB_PAGE
122 #define prep_compound_page(page, order) do { } while (0)
123 #define destroy_compound_page(page, order) do { } while (0)
126 * Higher-order pages are called "compound pages". They are structured thusly:
128 * The first PAGE_SIZE page is called the "head page".
130 * The remaining PAGE_SIZE pages are called "tail pages".
132 * All pages have PG_compound set. All pages have their ->private pointing at
133 * the head page (even the head page has this).
135 * The first tail page's ->mapping, if non-zero, holds the address of the
136 * compound page's put_page() function.
138 * The order of the allocation is stored in the first tail page's ->index
139 * This is only for debug at present. This usage means that zero-order pages
140 * may not be compound.
142 static void prep_compound_page(struct page *page, unsigned long order)
145 int nr_pages = 1 << order;
147 page[1].mapping = NULL;
148 page[1].index = order;
149 for (i = 0; i < nr_pages; i++) {
150 struct page *p = page + i;
153 p->private = (unsigned long)page;
157 static void destroy_compound_page(struct page *page, unsigned long order)
160 int nr_pages = 1 << order;
162 if (!PageCompound(page))
165 if (page[1].index != order)
166 bad_page(__FUNCTION__, page);
168 for (i = 0; i < nr_pages; i++) {
169 struct page *p = page + i;
171 if (!PageCompound(p))
172 bad_page(__FUNCTION__, page);
173 if (p->private != (unsigned long)page)
174 bad_page(__FUNCTION__, page);
175 ClearPageCompound(p);
178 #endif /* CONFIG_HUGETLB_PAGE */
181 * function for dealing with page's order in buddy system.
182 * zone->lock is already acquired when we use these.
183 * So, we don't need atomic page->flags operations here.
185 static inline unsigned long page_order(struct page *page) {
186 return page->private;
189 static inline void set_page_order(struct page *page, int order) {
190 page->private = order;
191 __SetPagePrivate(page);
194 static inline void rmv_page_order(struct page *page)
196 __ClearPagePrivate(page);
201 * Locate the struct page for both the matching buddy in our
202 * pair (buddy1) and the combined O(n+1) page they form (page).
204 * 1) Any buddy B1 will have an order O twin B2 which satisfies
205 * the following equation:
207 * For example, if the starting buddy (buddy2) is #8 its order
209 * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
211 * 2) Any buddy B will have an order O+1 parent P which
212 * satisfies the following equation:
215 * Assumption: *_mem_map is contigious at least up to MAX_ORDER
217 static inline struct page *
218 __page_find_buddy(struct page *page, unsigned long page_idx, unsigned int order)
220 unsigned long buddy_idx = page_idx ^ (1 << order);
222 return page + (buddy_idx - page_idx);
225 static inline unsigned long
226 __find_combined_index(unsigned long page_idx, unsigned int order)
228 return (page_idx & ~(1 << order));
232 * This function checks whether a page is free && is the buddy
233 * we can do coalesce a page and its buddy if
234 * (a) the buddy is free &&
235 * (b) the buddy is on the buddy system &&
236 * (c) a page and its buddy have the same order.
237 * for recording page's order, we use page->private and PG_private.
240 static inline int page_is_buddy(struct page *page, int order)
242 if (PagePrivate(page) &&
243 (page_order(page) == order) &&
244 !PageReserved(page) &&
245 page_count(page) == 0)
251 * Freeing function for a buddy system allocator.
253 * The concept of a buddy system is to maintain direct-mapped table
254 * (containing bit values) for memory blocks of various "orders".
255 * The bottom level table contains the map for the smallest allocatable
256 * units of memory (here, pages), and each level above it describes
257 * pairs of units from the levels below, hence, "buddies".
258 * At a high level, all that happens here is marking the table entry
259 * at the bottom level available, and propagating the changes upward
260 * as necessary, plus some accounting needed to play nicely with other
261 * parts of the VM system.
262 * At each level, we keep a list of pages, which are heads of continuous
263 * free pages of length of (1 << order) and marked with PG_Private.Page's
264 * order is recorded in page->private field.
265 * So when we are allocating or freeing one, we can derive the state of the
266 * other. That is, if we allocate a small block, and both were
267 * free, the remainder of the region must be split into blocks.
268 * If a block is freed, and its buddy is also free, then this
269 * triggers coalescing into a block of larger size.
274 static inline void __free_pages_bulk (struct page *page,
275 struct zone *zone, unsigned int order)
277 unsigned long page_idx;
278 int order_size = 1 << order;
281 destroy_compound_page(page, order);
283 page_idx = page_to_pfn(page) & ((1 << MAX_ORDER) - 1);
285 BUG_ON(page_idx & (order_size - 1));
286 BUG_ON(bad_range(zone, page));
288 zone->free_pages += order_size;
289 while (order < MAX_ORDER-1) {
290 unsigned long combined_idx;
291 struct free_area *area;
294 combined_idx = __find_combined_index(page_idx, order);
295 buddy = __page_find_buddy(page, page_idx, order);
297 if (bad_range(zone, buddy))
299 if (!page_is_buddy(buddy, order))
300 break; /* Move the buddy up one level. */
301 list_del(&buddy->lru);
302 area = zone->free_area + order;
304 rmv_page_order(buddy);
305 page = page + (combined_idx - page_idx);
306 page_idx = combined_idx;
309 set_page_order(page, order);
310 list_add(&page->lru, &zone->free_area[order].free_list);
311 zone->free_area[order].nr_free++;
314 static inline void free_pages_check(const char *function, struct page *page)
316 if ( page_mapcount(page) ||
317 page->mapping != NULL ||
318 page_count(page) != 0 ||
327 1 << PG_writeback )))
328 bad_page(function, page);
330 ClearPageDirty(page);
334 * Frees a list of pages.
335 * Assumes all pages on list are in same zone, and of same order.
336 * count is the number of pages to free, or 0 for all on the list.
338 * If the zone was previously in an "all pages pinned" state then look to
339 * see if this freeing clears that state.
341 * And clear the zone's pages_scanned counter, to hold off the "all pages are
342 * pinned" detection logic.
345 free_pages_bulk(struct zone *zone, int count,
346 struct list_head *list, unsigned int order)
349 struct page *page = NULL;
352 spin_lock_irqsave(&zone->lock, flags);
353 zone->all_unreclaimable = 0;
354 zone->pages_scanned = 0;
355 while (!list_empty(list) && count--) {
356 page = list_entry(list->prev, struct page, lru);
357 /* have to delete it as __free_pages_bulk list manipulates */
358 list_del(&page->lru);
359 __free_pages_bulk(page, zone, order);
362 spin_unlock_irqrestore(&zone->lock, flags);
366 void __free_pages_ok(struct page *page, unsigned int order)
371 arch_free_page(page, order);
373 mod_page_state(pgfree, 1 << order);
377 for (i = 1 ; i < (1 << order) ; ++i)
378 __put_page(page + i);
381 for (i = 0 ; i < (1 << order) ; ++i)
382 free_pages_check(__FUNCTION__, page + i);
383 list_add(&page->lru, &list);
384 kernel_map_pages(page, 1<<order, 0);
385 free_pages_bulk(page_zone(page), 1, &list, order);
390 * The order of subdivision here is critical for the IO subsystem.
391 * Please do not alter this order without good reasons and regression
392 * testing. Specifically, as large blocks of memory are subdivided,
393 * the order in which smaller blocks are delivered depends on the order
394 * they're subdivided in this function. This is the primary factor
395 * influencing the order in which pages are delivered to the IO
396 * subsystem according to empirical testing, and this is also justified
397 * by considering the behavior of a buddy system containing a single
398 * large block of memory acted on by a series of small allocations.
399 * This behavior is a critical factor in sglist merging's success.
403 static inline struct page *
404 expand(struct zone *zone, struct page *page,
405 int low, int high, struct free_area *area)
407 unsigned long size = 1 << high;
413 BUG_ON(bad_range(zone, &page[size]));
414 list_add(&page[size].lru, &area->free_list);
416 set_page_order(&page[size], high);
421 void set_page_refs(struct page *page, int order)
424 set_page_count(page, 1);
429 * We need to reference all the pages for this order, otherwise if
430 * anyone accesses one of the pages with (get/put) it will be freed.
431 * - eg: access_process_vm()
433 for (i = 0; i < (1 << order); i++)
434 set_page_count(page + i, 1);
435 #endif /* CONFIG_MMU */
439 * This page is about to be returned from the page allocator
441 static void prep_new_page(struct page *page, int order)
443 if (page->mapping || page_mapcount(page) ||
452 1 << PG_writeback )))
453 bad_page(__FUNCTION__, page);
455 page->flags &= ~(1 << PG_uptodate | 1 << PG_error |
456 1 << PG_referenced | 1 << PG_arch_1 |
457 1 << PG_checked | 1 << PG_mappedtodisk);
459 set_page_refs(page, order);
460 kernel_map_pages(page, 1 << order, 1);
464 * Do the hard work of removing an element from the buddy allocator.
465 * Call me with the zone->lock already held.
467 static struct page *__rmqueue(struct zone *zone, unsigned int order)
469 struct free_area * area;
470 unsigned int current_order;
473 for (current_order = order; current_order < MAX_ORDER; ++current_order) {
474 area = zone->free_area + current_order;
475 if (list_empty(&area->free_list))
478 page = list_entry(area->free_list.next, struct page, lru);
479 list_del(&page->lru);
480 rmv_page_order(page);
482 zone->free_pages -= 1UL << order;
483 return expand(zone, page, order, current_order, area);
490 * Obtain a specified number of elements from the buddy allocator, all under
491 * a single hold of the lock, for efficiency. Add them to the supplied list.
492 * Returns the number of new pages which were placed at *list.
494 static int rmqueue_bulk(struct zone *zone, unsigned int order,
495 unsigned long count, struct list_head *list)
502 spin_lock_irqsave(&zone->lock, flags);
503 for (i = 0; i < count; ++i) {
504 page = __rmqueue(zone, order);
508 list_add_tail(&page->lru, list);
510 spin_unlock_irqrestore(&zone->lock, flags);
514 #if defined(CONFIG_PM) || defined(CONFIG_HOTPLUG_CPU)
515 static void __drain_pages(unsigned int cpu)
520 for_each_zone(zone) {
521 struct per_cpu_pageset *pset;
523 pset = &zone->pageset[cpu];
524 for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
525 struct per_cpu_pages *pcp;
528 pcp->count -= free_pages_bulk(zone, pcp->count,
533 #endif /* CONFIG_PM || CONFIG_HOTPLUG_CPU */
537 void mark_free_pages(struct zone *zone)
539 unsigned long zone_pfn, flags;
541 struct list_head *curr;
543 if (!zone->spanned_pages)
546 spin_lock_irqsave(&zone->lock, flags);
547 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
548 ClearPageNosaveFree(pfn_to_page(zone_pfn + zone->zone_start_pfn));
550 for (order = MAX_ORDER - 1; order >= 0; --order)
551 list_for_each(curr, &zone->free_area[order].free_list) {
552 unsigned long start_pfn, i;
554 start_pfn = page_to_pfn(list_entry(curr, struct page, lru));
556 for (i=0; i < (1<<order); i++)
557 SetPageNosaveFree(pfn_to_page(start_pfn+i));
559 spin_unlock_irqrestore(&zone->lock, flags);
563 * Spill all of this CPU's per-cpu pages back into the buddy allocator.
565 void drain_local_pages(void)
569 local_irq_save(flags);
570 __drain_pages(smp_processor_id());
571 local_irq_restore(flags);
573 #endif /* CONFIG_PM */
575 static void zone_statistics(struct zonelist *zonelist, struct zone *z)
580 pg_data_t *pg = z->zone_pgdat;
581 pg_data_t *orig = zonelist->zones[0]->zone_pgdat;
582 struct per_cpu_pageset *p;
584 local_irq_save(flags);
585 cpu = smp_processor_id();
586 p = &z->pageset[cpu];
588 z->pageset[cpu].numa_hit++;
591 zonelist->zones[0]->pageset[cpu].numa_foreign++;
593 if (pg == NODE_DATA(numa_node_id()))
597 local_irq_restore(flags);
602 * Free a 0-order page
604 static void FASTCALL(free_hot_cold_page(struct page *page, int cold));
605 static void fastcall free_hot_cold_page(struct page *page, int cold)
607 struct zone *zone = page_zone(page);
608 struct per_cpu_pages *pcp;
611 arch_free_page(page, 0);
613 kernel_map_pages(page, 1, 0);
614 inc_page_state(pgfree);
616 page->mapping = NULL;
617 free_pages_check(__FUNCTION__, page);
618 pcp = &zone->pageset[get_cpu()].pcp[cold];
619 local_irq_save(flags);
620 if (pcp->count >= pcp->high)
621 pcp->count -= free_pages_bulk(zone, pcp->batch, &pcp->list, 0);
622 list_add(&page->lru, &pcp->list);
624 local_irq_restore(flags);
628 void fastcall free_hot_page(struct page *page)
630 free_hot_cold_page(page, 0);
633 void fastcall free_cold_page(struct page *page)
635 free_hot_cold_page(page, 1);
638 static inline void prep_zero_page(struct page *page, int order, unsigned int __nocast gfp_flags)
642 BUG_ON((gfp_flags & (__GFP_WAIT | __GFP_HIGHMEM)) == __GFP_HIGHMEM);
643 for(i = 0; i < (1 << order); i++)
644 clear_highpage(page + i);
648 * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
649 * we cheat by calling it from here, in the order > 0 path. Saves a branch
653 buffered_rmqueue(struct zone *zone, int order, unsigned int __nocast gfp_flags)
656 struct page *page = NULL;
657 int cold = !!(gfp_flags & __GFP_COLD);
660 struct per_cpu_pages *pcp;
662 pcp = &zone->pageset[get_cpu()].pcp[cold];
663 local_irq_save(flags);
664 if (pcp->count <= pcp->low)
665 pcp->count += rmqueue_bulk(zone, 0,
666 pcp->batch, &pcp->list);
668 page = list_entry(pcp->list.next, struct page, lru);
669 list_del(&page->lru);
672 local_irq_restore(flags);
677 spin_lock_irqsave(&zone->lock, flags);
678 page = __rmqueue(zone, order);
679 spin_unlock_irqrestore(&zone->lock, flags);
683 BUG_ON(bad_range(zone, page));
684 mod_page_state_zone(zone, pgalloc, 1 << order);
685 prep_new_page(page, order);
687 if (gfp_flags & __GFP_ZERO)
688 prep_zero_page(page, order, gfp_flags);
690 if (order && (gfp_flags & __GFP_COMP))
691 prep_compound_page(page, order);
697 * Return 1 if free pages are above 'mark'. This takes into account the order
700 int zone_watermark_ok(struct zone *z, int order, unsigned long mark,
701 int classzone_idx, int can_try_harder, int gfp_high)
703 /* free_pages my go negative - that's OK */
704 long min = mark, free_pages = z->free_pages - (1 << order) + 1;
712 if (free_pages <= min + z->lowmem_reserve[classzone_idx])
714 for (o = 0; o < order; o++) {
715 /* At the next order, this order's pages become unavailable */
716 free_pages -= z->free_area[o].nr_free << o;
718 /* Require fewer higher order pages to be free */
721 if (free_pages <= min)
728 should_reclaim_zone(struct zone *z, unsigned int gfp_mask)
730 if (!z->reclaim_pages)
736 * This is the 'heart' of the zoned buddy allocator.
738 struct page * fastcall
739 __alloc_pages(unsigned int __nocast gfp_mask, unsigned int order,
740 struct zonelist *zonelist)
742 const int wait = gfp_mask & __GFP_WAIT;
743 struct zone **zones, *z;
745 struct reclaim_state reclaim_state;
746 struct task_struct *p = current;
751 int did_some_progress;
753 might_sleep_if(wait);
756 * The caller may dip into page reserves a bit more if the caller
757 * cannot run direct reclaim, or is the caller has realtime scheduling
760 can_try_harder = (unlikely(rt_task(p)) && !in_interrupt()) || !wait;
762 zones = zonelist->zones; /* the list of zones suitable for gfp_mask */
764 if (unlikely(zones[0] == NULL)) {
765 /* Should this ever happen?? */
769 classzone_idx = zone_idx(zones[0]);
772 /* Go through the zonelist once, looking for a zone with enough free */
773 for (i = 0; (z = zones[i]) != NULL; i++) {
774 int do_reclaim = should_reclaim_zone(z, gfp_mask);
776 if (!cpuset_zone_allowed(z))
780 * If the zone is to attempt early page reclaim then this loop
781 * will try to reclaim pages and check the watermark a second
782 * time before giving up and falling back to the next zone.
785 if (!zone_watermark_ok(z, order, z->pages_low,
786 classzone_idx, 0, 0)) {
790 zone_reclaim(z, gfp_mask, order);
791 /* Only try reclaim once */
793 goto zone_reclaim_retry;
797 page = buffered_rmqueue(z, order, gfp_mask);
802 for (i = 0; (z = zones[i]) != NULL; i++)
803 wakeup_kswapd(z, order);
806 * Go through the zonelist again. Let __GFP_HIGH and allocations
807 * coming from realtime tasks to go deeper into reserves
809 * This is the last chance, in general, before the goto nopage.
810 * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
812 for (i = 0; (z = zones[i]) != NULL; i++) {
813 if (!zone_watermark_ok(z, order, z->pages_min,
814 classzone_idx, can_try_harder,
815 gfp_mask & __GFP_HIGH))
818 if (wait && !cpuset_zone_allowed(z))
821 page = buffered_rmqueue(z, order, gfp_mask);
826 /* This allocation should allow future memory freeing. */
828 if (((p->flags & PF_MEMALLOC) || unlikely(test_thread_flag(TIF_MEMDIE)))
829 && !in_interrupt()) {
830 if (!(gfp_mask & __GFP_NOMEMALLOC)) {
831 /* go through the zonelist yet again, ignoring mins */
832 for (i = 0; (z = zones[i]) != NULL; i++) {
833 if (!cpuset_zone_allowed(z))
835 page = buffered_rmqueue(z, order, gfp_mask);
843 /* Atomic allocations - we can't balance anything */
850 /* We now go into synchronous reclaim */
851 p->flags |= PF_MEMALLOC;
852 reclaim_state.reclaimed_slab = 0;
853 p->reclaim_state = &reclaim_state;
855 did_some_progress = try_to_free_pages(zones, gfp_mask, order);
857 p->reclaim_state = NULL;
858 p->flags &= ~PF_MEMALLOC;
862 if (likely(did_some_progress)) {
864 * Go through the zonelist yet one more time, keep
865 * very high watermark here, this is only to catch
866 * a parallel oom killing, we must fail if we're still
867 * under heavy pressure.
869 for (i = 0; (z = zones[i]) != NULL; i++) {
870 if (!zone_watermark_ok(z, order, z->pages_min,
871 classzone_idx, can_try_harder,
872 gfp_mask & __GFP_HIGH))
875 if (!cpuset_zone_allowed(z))
878 page = buffered_rmqueue(z, order, gfp_mask);
882 } else if ((gfp_mask & __GFP_FS) && !(gfp_mask & __GFP_NORETRY)) {
884 * Go through the zonelist yet one more time, keep
885 * very high watermark here, this is only to catch
886 * a parallel oom killing, we must fail if we're still
887 * under heavy pressure.
889 for (i = 0; (z = zones[i]) != NULL; i++) {
890 if (!zone_watermark_ok(z, order, z->pages_high,
891 classzone_idx, 0, 0))
894 if (!cpuset_zone_allowed(z))
897 page = buffered_rmqueue(z, order, gfp_mask);
902 out_of_memory(gfp_mask);
907 * Don't let big-order allocations loop unless the caller explicitly
908 * requests that. Wait for some write requests to complete then retry.
910 * In this implementation, __GFP_REPEAT means __GFP_NOFAIL for order
911 * <= 3, but that may not be true in other implementations.
914 if (!(gfp_mask & __GFP_NORETRY)) {
915 if ((order <= 3) || (gfp_mask & __GFP_REPEAT))
917 if (gfp_mask & __GFP_NOFAIL)
921 blk_congestion_wait(WRITE, HZ/50);
926 if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) {
927 printk(KERN_WARNING "%s: page allocation failure."
928 " order:%d, mode:0x%x\n",
929 p->comm, order, gfp_mask);
934 zone_statistics(zonelist, z);
938 EXPORT_SYMBOL(__alloc_pages);
941 * Common helper functions.
943 fastcall unsigned long __get_free_pages(unsigned int __nocast gfp_mask, unsigned int order)
946 page = alloc_pages(gfp_mask, order);
949 return (unsigned long) page_address(page);
952 EXPORT_SYMBOL(__get_free_pages);
954 fastcall unsigned long get_zeroed_page(unsigned int __nocast gfp_mask)
959 * get_zeroed_page() returns a 32-bit address, which cannot represent
962 BUG_ON(gfp_mask & __GFP_HIGHMEM);
964 page = alloc_pages(gfp_mask | __GFP_ZERO, 0);
966 return (unsigned long) page_address(page);
970 EXPORT_SYMBOL(get_zeroed_page);
972 void __pagevec_free(struct pagevec *pvec)
974 int i = pagevec_count(pvec);
977 free_hot_cold_page(pvec->pages[i], pvec->cold);
980 fastcall void __free_pages(struct page *page, unsigned int order)
982 if (!PageReserved(page) && put_page_testzero(page)) {
986 __free_pages_ok(page, order);
990 EXPORT_SYMBOL(__free_pages);
992 fastcall void free_pages(unsigned long addr, unsigned int order)
995 BUG_ON(!virt_addr_valid((void *)addr));
996 __free_pages(virt_to_page((void *)addr), order);
1000 EXPORT_SYMBOL(free_pages);
1003 * Total amount of free (allocatable) RAM:
1005 unsigned int nr_free_pages(void)
1007 unsigned int sum = 0;
1011 sum += zone->free_pages;
1016 EXPORT_SYMBOL(nr_free_pages);
1019 unsigned int nr_free_pages_pgdat(pg_data_t *pgdat)
1021 unsigned int i, sum = 0;
1023 for (i = 0; i < MAX_NR_ZONES; i++)
1024 sum += pgdat->node_zones[i].free_pages;
1030 static unsigned int nr_free_zone_pages(int offset)
1033 unsigned int sum = 0;
1035 for_each_pgdat(pgdat) {
1036 struct zonelist *zonelist = pgdat->node_zonelists + offset;
1037 struct zone **zonep = zonelist->zones;
1040 for (zone = *zonep++; zone; zone = *zonep++) {
1041 unsigned long size = zone->present_pages;
1042 unsigned long high = zone->pages_high;
1052 * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
1054 unsigned int nr_free_buffer_pages(void)
1056 return nr_free_zone_pages(GFP_USER & GFP_ZONEMASK);
1060 * Amount of free RAM allocatable within all zones
1062 unsigned int nr_free_pagecache_pages(void)
1064 return nr_free_zone_pages(GFP_HIGHUSER & GFP_ZONEMASK);
1067 #ifdef CONFIG_HIGHMEM
1068 unsigned int nr_free_highpages (void)
1071 unsigned int pages = 0;
1073 for_each_pgdat(pgdat)
1074 pages += pgdat->node_zones[ZONE_HIGHMEM].free_pages;
1081 static void show_node(struct zone *zone)
1083 printk("Node %d ", zone->zone_pgdat->node_id);
1086 #define show_node(zone) do { } while (0)
1090 * Accumulate the page_state information across all CPUs.
1091 * The result is unavoidably approximate - it can change
1092 * during and after execution of this function.
1094 static DEFINE_PER_CPU(struct page_state, page_states) = {0};
1096 atomic_t nr_pagecache = ATOMIC_INIT(0);
1097 EXPORT_SYMBOL(nr_pagecache);
1099 DEFINE_PER_CPU(long, nr_pagecache_local) = 0;
1102 void __get_page_state(struct page_state *ret, int nr)
1106 memset(ret, 0, sizeof(*ret));
1108 cpu = first_cpu(cpu_online_map);
1109 while (cpu < NR_CPUS) {
1110 unsigned long *in, *out, off;
1112 in = (unsigned long *)&per_cpu(page_states, cpu);
1114 cpu = next_cpu(cpu, cpu_online_map);
1117 prefetch(&per_cpu(page_states, cpu));
1119 out = (unsigned long *)ret;
1120 for (off = 0; off < nr; off++)
1125 void get_page_state(struct page_state *ret)
1129 nr = offsetof(struct page_state, GET_PAGE_STATE_LAST);
1130 nr /= sizeof(unsigned long);
1132 __get_page_state(ret, nr + 1);
1135 void get_full_page_state(struct page_state *ret)
1137 __get_page_state(ret, sizeof(*ret) / sizeof(unsigned long));
1140 unsigned long __read_page_state(unsigned offset)
1142 unsigned long ret = 0;
1145 for_each_online_cpu(cpu) {
1148 in = (unsigned long)&per_cpu(page_states, cpu) + offset;
1149 ret += *((unsigned long *)in);
1154 void __mod_page_state(unsigned offset, unsigned long delta)
1156 unsigned long flags;
1159 local_irq_save(flags);
1160 ptr = &__get_cpu_var(page_states);
1161 *(unsigned long*)(ptr + offset) += delta;
1162 local_irq_restore(flags);
1165 EXPORT_SYMBOL(__mod_page_state);
1167 void __get_zone_counts(unsigned long *active, unsigned long *inactive,
1168 unsigned long *free, struct pglist_data *pgdat)
1170 struct zone *zones = pgdat->node_zones;
1176 for (i = 0; i < MAX_NR_ZONES; i++) {
1177 *active += zones[i].nr_active;
1178 *inactive += zones[i].nr_inactive;
1179 *free += zones[i].free_pages;
1183 void get_zone_counts(unsigned long *active,
1184 unsigned long *inactive, unsigned long *free)
1186 struct pglist_data *pgdat;
1191 for_each_pgdat(pgdat) {
1192 unsigned long l, m, n;
1193 __get_zone_counts(&l, &m, &n, pgdat);
1200 void si_meminfo(struct sysinfo *val)
1202 val->totalram = totalram_pages;
1204 val->freeram = nr_free_pages();
1205 val->bufferram = nr_blockdev_pages();
1206 #ifdef CONFIG_HIGHMEM
1207 val->totalhigh = totalhigh_pages;
1208 val->freehigh = nr_free_highpages();
1213 val->mem_unit = PAGE_SIZE;
1216 EXPORT_SYMBOL(si_meminfo);
1219 void si_meminfo_node(struct sysinfo *val, int nid)
1221 pg_data_t *pgdat = NODE_DATA(nid);
1223 val->totalram = pgdat->node_present_pages;
1224 val->freeram = nr_free_pages_pgdat(pgdat);
1225 val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].present_pages;
1226 val->freehigh = pgdat->node_zones[ZONE_HIGHMEM].free_pages;
1227 val->mem_unit = PAGE_SIZE;
1231 #define K(x) ((x) << (PAGE_SHIFT-10))
1234 * Show free area list (used inside shift_scroll-lock stuff)
1235 * We also calculate the percentage fragmentation. We do this by counting the
1236 * memory on each free list with the exception of the first item on the list.
1238 void show_free_areas(void)
1240 struct page_state ps;
1241 int cpu, temperature;
1242 unsigned long active;
1243 unsigned long inactive;
1247 for_each_zone(zone) {
1249 printk("%s per-cpu:", zone->name);
1251 if (!zone->present_pages) {
1257 for (cpu = 0; cpu < NR_CPUS; ++cpu) {
1258 struct per_cpu_pageset *pageset;
1260 if (!cpu_possible(cpu))
1263 pageset = zone->pageset + cpu;
1265 for (temperature = 0; temperature < 2; temperature++)
1266 printk("cpu %d %s: low %d, high %d, batch %d\n",
1268 temperature ? "cold" : "hot",
1269 pageset->pcp[temperature].low,
1270 pageset->pcp[temperature].high,
1271 pageset->pcp[temperature].batch);
1275 get_page_state(&ps);
1276 get_zone_counts(&active, &inactive, &free);
1278 printk("\nFree pages: %11ukB (%ukB HighMem)\n",
1280 K(nr_free_highpages()));
1282 printk("Active:%lu inactive:%lu dirty:%lu writeback:%lu "
1283 "unstable:%lu free:%u slab:%lu mapped:%lu pagetables:%lu\n",
1292 ps.nr_page_table_pages);
1294 for_each_zone(zone) {
1306 " pages_scanned:%lu"
1307 " all_unreclaimable? %s"
1310 K(zone->free_pages),
1313 K(zone->pages_high),
1315 K(zone->nr_inactive),
1316 K(zone->present_pages),
1317 zone->pages_scanned,
1318 (zone->all_unreclaimable ? "yes" : "no")
1320 printk("lowmem_reserve[]:");
1321 for (i = 0; i < MAX_NR_ZONES; i++)
1322 printk(" %lu", zone->lowmem_reserve[i]);
1326 for_each_zone(zone) {
1327 unsigned long nr, flags, order, total = 0;
1330 printk("%s: ", zone->name);
1331 if (!zone->present_pages) {
1336 spin_lock_irqsave(&zone->lock, flags);
1337 for (order = 0; order < MAX_ORDER; order++) {
1338 nr = zone->free_area[order].nr_free;
1339 total += nr << order;
1340 printk("%lu*%lukB ", nr, K(1UL) << order);
1342 spin_unlock_irqrestore(&zone->lock, flags);
1343 printk("= %lukB\n", K(total));
1346 show_swap_cache_info();
1350 * Builds allocation fallback zone lists.
1352 static int __init build_zonelists_node(pg_data_t *pgdat, struct zonelist *zonelist, int j, int k)
1359 zone = pgdat->node_zones + ZONE_HIGHMEM;
1360 if (zone->present_pages) {
1361 #ifndef CONFIG_HIGHMEM
1364 zonelist->zones[j++] = zone;
1367 zone = pgdat->node_zones + ZONE_NORMAL;
1368 if (zone->present_pages)
1369 zonelist->zones[j++] = zone;
1371 zone = pgdat->node_zones + ZONE_DMA;
1372 if (zone->present_pages)
1373 zonelist->zones[j++] = zone;
1380 #define MAX_NODE_LOAD (num_online_nodes())
1381 static int __initdata node_load[MAX_NUMNODES];
1383 * find_next_best_node - find the next node that should appear in a given node's fallback list
1384 * @node: node whose fallback list we're appending
1385 * @used_node_mask: nodemask_t of already used nodes
1387 * We use a number of factors to determine which is the next node that should
1388 * appear on a given node's fallback list. The node should not have appeared
1389 * already in @node's fallback list, and it should be the next closest node
1390 * according to the distance array (which contains arbitrary distance values
1391 * from each node to each node in the system), and should also prefer nodes
1392 * with no CPUs, since presumably they'll have very little allocation pressure
1393 * on them otherwise.
1394 * It returns -1 if no node is found.
1396 static int __init find_next_best_node(int node, nodemask_t *used_node_mask)
1399 int min_val = INT_MAX;
1402 for_each_online_node(i) {
1405 /* Start from local node */
1406 n = (node+i) % num_online_nodes();
1408 /* Don't want a node to appear more than once */
1409 if (node_isset(n, *used_node_mask))
1412 /* Use the local node if we haven't already */
1413 if (!node_isset(node, *used_node_mask)) {
1418 /* Use the distance array to find the distance */
1419 val = node_distance(node, n);
1421 /* Give preference to headless and unused nodes */
1422 tmp = node_to_cpumask(n);
1423 if (!cpus_empty(tmp))
1424 val += PENALTY_FOR_NODE_WITH_CPUS;
1426 /* Slight preference for less loaded node */
1427 val *= (MAX_NODE_LOAD*MAX_NUMNODES);
1428 val += node_load[n];
1430 if (val < min_val) {
1437 node_set(best_node, *used_node_mask);
1442 static void __init build_zonelists(pg_data_t *pgdat)
1444 int i, j, k, node, local_node;
1445 int prev_node, load;
1446 struct zonelist *zonelist;
1447 nodemask_t used_mask;
1449 /* initialize zonelists */
1450 for (i = 0; i < GFP_ZONETYPES; i++) {
1451 zonelist = pgdat->node_zonelists + i;
1452 zonelist->zones[0] = NULL;
1455 /* NUMA-aware ordering of nodes */
1456 local_node = pgdat->node_id;
1457 load = num_online_nodes();
1458 prev_node = local_node;
1459 nodes_clear(used_mask);
1460 while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
1462 * We don't want to pressure a particular node.
1463 * So adding penalty to the first node in same
1464 * distance group to make it round-robin.
1466 if (node_distance(local_node, node) !=
1467 node_distance(local_node, prev_node))
1468 node_load[node] += load;
1471 for (i = 0; i < GFP_ZONETYPES; i++) {
1472 zonelist = pgdat->node_zonelists + i;
1473 for (j = 0; zonelist->zones[j] != NULL; j++);
1476 if (i & __GFP_HIGHMEM)
1481 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1482 zonelist->zones[j] = NULL;
1487 #else /* CONFIG_NUMA */
1489 static void __init build_zonelists(pg_data_t *pgdat)
1491 int i, j, k, node, local_node;
1493 local_node = pgdat->node_id;
1494 for (i = 0; i < GFP_ZONETYPES; i++) {
1495 struct zonelist *zonelist;
1497 zonelist = pgdat->node_zonelists + i;
1501 if (i & __GFP_HIGHMEM)
1506 j = build_zonelists_node(pgdat, zonelist, j, k);
1508 * Now we build the zonelist so that it contains the zones
1509 * of all the other nodes.
1510 * We don't want to pressure a particular node, so when
1511 * building the zones for node N, we make sure that the
1512 * zones coming right after the local ones are those from
1513 * node N+1 (modulo N)
1515 for (node = local_node + 1; node < MAX_NUMNODES; node++) {
1516 if (!node_online(node))
1518 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1520 for (node = 0; node < local_node; node++) {
1521 if (!node_online(node))
1523 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1526 zonelist->zones[j] = NULL;
1530 #endif /* CONFIG_NUMA */
1532 void __init build_all_zonelists(void)
1536 for_each_online_node(i)
1537 build_zonelists(NODE_DATA(i));
1538 printk("Built %i zonelists\n", num_online_nodes());
1539 cpuset_init_current_mems_allowed();
1543 * Helper functions to size the waitqueue hash table.
1544 * Essentially these want to choose hash table sizes sufficiently
1545 * large so that collisions trying to wait on pages are rare.
1546 * But in fact, the number of active page waitqueues on typical
1547 * systems is ridiculously low, less than 200. So this is even
1548 * conservative, even though it seems large.
1550 * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
1551 * waitqueues, i.e. the size of the waitq table given the number of pages.
1553 #define PAGES_PER_WAITQUEUE 256
1555 static inline unsigned long wait_table_size(unsigned long pages)
1557 unsigned long size = 1;
1559 pages /= PAGES_PER_WAITQUEUE;
1561 while (size < pages)
1565 * Once we have dozens or even hundreds of threads sleeping
1566 * on IO we've got bigger problems than wait queue collision.
1567 * Limit the size of the wait table to a reasonable size.
1569 size = min(size, 4096UL);
1571 return max(size, 4UL);
1575 * This is an integer logarithm so that shifts can be used later
1576 * to extract the more random high bits from the multiplicative
1577 * hash function before the remainder is taken.
1579 static inline unsigned long wait_table_bits(unsigned long size)
1584 #define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
1586 static void __init calculate_zone_totalpages(struct pglist_data *pgdat,
1587 unsigned long *zones_size, unsigned long *zholes_size)
1589 unsigned long realtotalpages, totalpages = 0;
1592 for (i = 0; i < MAX_NR_ZONES; i++)
1593 totalpages += zones_size[i];
1594 pgdat->node_spanned_pages = totalpages;
1596 realtotalpages = totalpages;
1598 for (i = 0; i < MAX_NR_ZONES; i++)
1599 realtotalpages -= zholes_size[i];
1600 pgdat->node_present_pages = realtotalpages;
1601 printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id, realtotalpages);
1606 * Initially all pages are reserved - free ones are freed
1607 * up by free_all_bootmem() once the early boot process is
1608 * done. Non-atomic initialization, single-pass.
1610 void __init memmap_init_zone(unsigned long size, int nid, unsigned long zone,
1611 unsigned long start_pfn)
1613 struct page *start = pfn_to_page(start_pfn);
1616 for (page = start; page < (start + size); page++) {
1617 set_page_zone(page, NODEZONE(nid, zone));
1618 set_page_count(page, 0);
1619 reset_page_mapcount(page);
1620 SetPageReserved(page);
1621 INIT_LIST_HEAD(&page->lru);
1622 #ifdef WANT_PAGE_VIRTUAL
1623 /* The shift won't overflow because ZONE_NORMAL is below 4G. */
1624 if (!is_highmem_idx(zone))
1625 set_page_address(page, __va(start_pfn << PAGE_SHIFT));
1631 void zone_init_free_lists(struct pglist_data *pgdat, struct zone *zone,
1635 for (order = 0; order < MAX_ORDER ; order++) {
1636 INIT_LIST_HEAD(&zone->free_area[order].free_list);
1637 zone->free_area[order].nr_free = 0;
1641 #ifndef __HAVE_ARCH_MEMMAP_INIT
1642 #define memmap_init(size, nid, zone, start_pfn) \
1643 memmap_init_zone((size), (nid), (zone), (start_pfn))
1647 * Set up the zone data structures:
1648 * - mark all pages reserved
1649 * - mark all memory queues empty
1650 * - clear the memory bitmaps
1652 static void __init free_area_init_core(struct pglist_data *pgdat,
1653 unsigned long *zones_size, unsigned long *zholes_size)
1656 const unsigned long zone_required_alignment = 1UL << (MAX_ORDER-1);
1657 int cpu, nid = pgdat->node_id;
1658 unsigned long zone_start_pfn = pgdat->node_start_pfn;
1660 pgdat->nr_zones = 0;
1661 init_waitqueue_head(&pgdat->kswapd_wait);
1662 pgdat->kswapd_max_order = 0;
1664 for (j = 0; j < MAX_NR_ZONES; j++) {
1665 struct zone *zone = pgdat->node_zones + j;
1666 unsigned long size, realsize;
1667 unsigned long batch;
1669 zone_table[NODEZONE(nid, j)] = zone;
1670 realsize = size = zones_size[j];
1672 realsize -= zholes_size[j];
1674 if (j == ZONE_DMA || j == ZONE_NORMAL)
1675 nr_kernel_pages += realsize;
1676 nr_all_pages += realsize;
1678 zone->spanned_pages = size;
1679 zone->present_pages = realsize;
1680 zone->name = zone_names[j];
1681 spin_lock_init(&zone->lock);
1682 spin_lock_init(&zone->lru_lock);
1683 zone->zone_pgdat = pgdat;
1684 zone->free_pages = 0;
1686 zone->temp_priority = zone->prev_priority = DEF_PRIORITY;
1689 * The per-cpu-pages pools are set to around 1000th of the
1690 * size of the zone. But no more than 1/4 of a meg - there's
1691 * no point in going beyond the size of L2 cache.
1693 * OK, so we don't know how big the cache is. So guess.
1695 batch = zone->present_pages / 1024;
1696 if (batch * PAGE_SIZE > 256 * 1024)
1697 batch = (256 * 1024) / PAGE_SIZE;
1698 batch /= 4; /* We effectively *= 4 below */
1703 * Clamp the batch to a 2^n - 1 value. Having a power
1704 * of 2 value was found to be more likely to have
1705 * suboptimal cache aliasing properties in some cases.
1707 * For example if 2 tasks are alternately allocating
1708 * batches of pages, one task can end up with a lot
1709 * of pages of one half of the possible page colors
1710 * and the other with pages of the other colors.
1712 batch = (1 << fls(batch + batch/2)) - 1;
1714 for (cpu = 0; cpu < NR_CPUS; cpu++) {
1715 struct per_cpu_pages *pcp;
1717 pcp = &zone->pageset[cpu].pcp[0]; /* hot */
1719 pcp->low = 2 * batch;
1720 pcp->high = 6 * batch;
1721 pcp->batch = 1 * batch;
1722 INIT_LIST_HEAD(&pcp->list);
1724 pcp = &zone->pageset[cpu].pcp[1]; /* cold */
1727 pcp->high = 2 * batch;
1728 pcp->batch = 1 * batch;
1729 INIT_LIST_HEAD(&pcp->list);
1731 printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%lu\n",
1732 zone_names[j], realsize, batch);
1733 INIT_LIST_HEAD(&zone->active_list);
1734 INIT_LIST_HEAD(&zone->inactive_list);
1735 zone->nr_scan_active = 0;
1736 zone->nr_scan_inactive = 0;
1737 zone->nr_active = 0;
1738 zone->nr_inactive = 0;
1743 * The per-page waitqueue mechanism uses hashed waitqueues
1746 zone->wait_table_size = wait_table_size(size);
1747 zone->wait_table_bits =
1748 wait_table_bits(zone->wait_table_size);
1749 zone->wait_table = (wait_queue_head_t *)
1750 alloc_bootmem_node(pgdat, zone->wait_table_size
1751 * sizeof(wait_queue_head_t));
1753 for(i = 0; i < zone->wait_table_size; ++i)
1754 init_waitqueue_head(zone->wait_table + i);
1756 pgdat->nr_zones = j+1;
1758 zone->zone_mem_map = pfn_to_page(zone_start_pfn);
1759 zone->zone_start_pfn = zone_start_pfn;
1761 if ((zone_start_pfn) & (zone_required_alignment-1))
1762 printk(KERN_CRIT "BUG: wrong zone alignment, it will crash\n");
1764 memmap_init(size, nid, j, zone_start_pfn);
1766 zone_start_pfn += size;
1768 zone_init_free_lists(pgdat, zone, zone->spanned_pages);
1772 static void __init alloc_node_mem_map(struct pglist_data *pgdat)
1776 /* Skip empty nodes */
1777 if (!pgdat->node_spanned_pages)
1780 /* ia64 gets its own node_mem_map, before this, without bootmem */
1781 if (!pgdat->node_mem_map) {
1782 size = (pgdat->node_spanned_pages + 1) * sizeof(struct page);
1783 pgdat->node_mem_map = alloc_bootmem_node(pgdat, size);
1785 #ifndef CONFIG_DISCONTIGMEM
1787 * With no DISCONTIG, the global mem_map is just set as node 0's
1789 if (pgdat == NODE_DATA(0))
1790 mem_map = NODE_DATA(0)->node_mem_map;
1794 void __init free_area_init_node(int nid, struct pglist_data *pgdat,
1795 unsigned long *zones_size, unsigned long node_start_pfn,
1796 unsigned long *zholes_size)
1798 pgdat->node_id = nid;
1799 pgdat->node_start_pfn = node_start_pfn;
1800 calculate_zone_totalpages(pgdat, zones_size, zholes_size);
1802 alloc_node_mem_map(pgdat);
1804 free_area_init_core(pgdat, zones_size, zholes_size);
1807 #ifndef CONFIG_DISCONTIGMEM
1808 static bootmem_data_t contig_bootmem_data;
1809 struct pglist_data contig_page_data = { .bdata = &contig_bootmem_data };
1811 EXPORT_SYMBOL(contig_page_data);
1813 void __init free_area_init(unsigned long *zones_size)
1815 free_area_init_node(0, &contig_page_data, zones_size,
1816 __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
1820 #ifdef CONFIG_PROC_FS
1822 #include <linux/seq_file.h>
1824 static void *frag_start(struct seq_file *m, loff_t *pos)
1829 for (pgdat = pgdat_list; pgdat && node; pgdat = pgdat->pgdat_next)
1835 static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
1837 pg_data_t *pgdat = (pg_data_t *)arg;
1840 return pgdat->pgdat_next;
1843 static void frag_stop(struct seq_file *m, void *arg)
1848 * This walks the free areas for each zone.
1850 static int frag_show(struct seq_file *m, void *arg)
1852 pg_data_t *pgdat = (pg_data_t *)arg;
1854 struct zone *node_zones = pgdat->node_zones;
1855 unsigned long flags;
1858 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
1859 if (!zone->present_pages)
1862 spin_lock_irqsave(&zone->lock, flags);
1863 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
1864 for (order = 0; order < MAX_ORDER; ++order)
1865 seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
1866 spin_unlock_irqrestore(&zone->lock, flags);
1872 struct seq_operations fragmentation_op = {
1873 .start = frag_start,
1880 * Output information about zones in @pgdat.
1882 static int zoneinfo_show(struct seq_file *m, void *arg)
1884 pg_data_t *pgdat = arg;
1886 struct zone *node_zones = pgdat->node_zones;
1887 unsigned long flags;
1889 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; zone++) {
1892 if (!zone->present_pages)
1895 spin_lock_irqsave(&zone->lock, flags);
1896 seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
1904 "\n scanned %lu (a: %lu i: %lu)"
1913 zone->pages_scanned,
1914 zone->nr_scan_active, zone->nr_scan_inactive,
1915 zone->spanned_pages,
1916 zone->present_pages);
1918 "\n protection: (%lu",
1919 zone->lowmem_reserve[0]);
1920 for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
1921 seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
1925 for (i = 0; i < ARRAY_SIZE(zone->pageset); i++) {
1926 struct per_cpu_pageset *pageset;
1929 pageset = &zone->pageset[i];
1930 for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
1931 if (pageset->pcp[j].count)
1934 if (j == ARRAY_SIZE(pageset->pcp))
1936 for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
1938 "\n cpu: %i pcp: %i"
1944 pageset->pcp[j].count,
1945 pageset->pcp[j].low,
1946 pageset->pcp[j].high,
1947 pageset->pcp[j].batch);
1953 "\n numa_foreign: %lu"
1954 "\n interleave_hit: %lu"
1955 "\n local_node: %lu"
1956 "\n other_node: %lu",
1959 pageset->numa_foreign,
1960 pageset->interleave_hit,
1961 pageset->local_node,
1962 pageset->other_node);
1966 "\n all_unreclaimable: %u"
1967 "\n prev_priority: %i"
1968 "\n temp_priority: %i"
1969 "\n start_pfn: %lu",
1970 zone->all_unreclaimable,
1971 zone->prev_priority,
1972 zone->temp_priority,
1973 zone->zone_start_pfn);
1974 spin_unlock_irqrestore(&zone->lock, flags);
1980 struct seq_operations zoneinfo_op = {
1981 .start = frag_start, /* iterate over all zones. The same as in
1985 .show = zoneinfo_show,
1988 static char *vmstat_text[] = {
1992 "nr_page_table_pages",
2017 "pgscan_kswapd_high",
2018 "pgscan_kswapd_normal",
2020 "pgscan_kswapd_dma",
2021 "pgscan_direct_high",
2022 "pgscan_direct_normal",
2023 "pgscan_direct_dma",
2028 "kswapd_inodesteal",
2036 static void *vmstat_start(struct seq_file *m, loff_t *pos)
2038 struct page_state *ps;
2040 if (*pos >= ARRAY_SIZE(vmstat_text))
2043 ps = kmalloc(sizeof(*ps), GFP_KERNEL);
2046 return ERR_PTR(-ENOMEM);
2047 get_full_page_state(ps);
2048 ps->pgpgin /= 2; /* sectors -> kbytes */
2050 return (unsigned long *)ps + *pos;
2053 static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
2056 if (*pos >= ARRAY_SIZE(vmstat_text))
2058 return (unsigned long *)m->private + *pos;
2061 static int vmstat_show(struct seq_file *m, void *arg)
2063 unsigned long *l = arg;
2064 unsigned long off = l - (unsigned long *)m->private;
2066 seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
2070 static void vmstat_stop(struct seq_file *m, void *arg)
2076 struct seq_operations vmstat_op = {
2077 .start = vmstat_start,
2078 .next = vmstat_next,
2079 .stop = vmstat_stop,
2080 .show = vmstat_show,
2083 #endif /* CONFIG_PROC_FS */
2085 #ifdef CONFIG_HOTPLUG_CPU
2086 static int page_alloc_cpu_notify(struct notifier_block *self,
2087 unsigned long action, void *hcpu)
2089 int cpu = (unsigned long)hcpu;
2091 unsigned long *src, *dest;
2093 if (action == CPU_DEAD) {
2096 /* Drain local pagecache count. */
2097 count = &per_cpu(nr_pagecache_local, cpu);
2098 atomic_add(*count, &nr_pagecache);
2100 local_irq_disable();
2103 /* Add dead cpu's page_states to our own. */
2104 dest = (unsigned long *)&__get_cpu_var(page_states);
2105 src = (unsigned long *)&per_cpu(page_states, cpu);
2107 for (i = 0; i < sizeof(struct page_state)/sizeof(unsigned long);
2117 #endif /* CONFIG_HOTPLUG_CPU */
2119 void __init page_alloc_init(void)
2121 hotcpu_notifier(page_alloc_cpu_notify, 0);
2125 * setup_per_zone_lowmem_reserve - called whenever
2126 * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
2127 * has a correct pages reserved value, so an adequate number of
2128 * pages are left in the zone after a successful __alloc_pages().
2130 static void setup_per_zone_lowmem_reserve(void)
2132 struct pglist_data *pgdat;
2135 for_each_pgdat(pgdat) {
2136 for (j = 0; j < MAX_NR_ZONES; j++) {
2137 struct zone *zone = pgdat->node_zones + j;
2138 unsigned long present_pages = zone->present_pages;
2140 zone->lowmem_reserve[j] = 0;
2142 for (idx = j-1; idx >= 0; idx--) {
2143 struct zone *lower_zone;
2145 if (sysctl_lowmem_reserve_ratio[idx] < 1)
2146 sysctl_lowmem_reserve_ratio[idx] = 1;
2148 lower_zone = pgdat->node_zones + idx;
2149 lower_zone->lowmem_reserve[j] = present_pages /
2150 sysctl_lowmem_reserve_ratio[idx];
2151 present_pages += lower_zone->present_pages;
2158 * setup_per_zone_pages_min - called when min_free_kbytes changes. Ensures
2159 * that the pages_{min,low,high} values for each zone are set correctly
2160 * with respect to min_free_kbytes.
2162 static void setup_per_zone_pages_min(void)
2164 unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
2165 unsigned long lowmem_pages = 0;
2167 unsigned long flags;
2169 /* Calculate total number of !ZONE_HIGHMEM pages */
2170 for_each_zone(zone) {
2171 if (!is_highmem(zone))
2172 lowmem_pages += zone->present_pages;
2175 for_each_zone(zone) {
2176 spin_lock_irqsave(&zone->lru_lock, flags);
2177 if (is_highmem(zone)) {
2179 * Often, highmem doesn't need to reserve any pages.
2180 * But the pages_min/low/high values are also used for
2181 * batching up page reclaim activity so we need a
2182 * decent value here.
2186 min_pages = zone->present_pages / 1024;
2187 if (min_pages < SWAP_CLUSTER_MAX)
2188 min_pages = SWAP_CLUSTER_MAX;
2189 if (min_pages > 128)
2191 zone->pages_min = min_pages;
2193 /* if it's a lowmem zone, reserve a number of pages
2194 * proportionate to the zone's size.
2196 zone->pages_min = (pages_min * zone->present_pages) /
2201 * When interpreting these watermarks, just keep in mind that:
2202 * zone->pages_min == (zone->pages_min * 4) / 4;
2204 zone->pages_low = (zone->pages_min * 5) / 4;
2205 zone->pages_high = (zone->pages_min * 6) / 4;
2206 spin_unlock_irqrestore(&zone->lru_lock, flags);
2211 * Initialise min_free_kbytes.
2213 * For small machines we want it small (128k min). For large machines
2214 * we want it large (64MB max). But it is not linear, because network
2215 * bandwidth does not increase linearly with machine size. We use
2217 * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
2218 * min_free_kbytes = sqrt(lowmem_kbytes * 16)
2234 static int __init init_per_zone_pages_min(void)
2236 unsigned long lowmem_kbytes;
2238 lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
2240 min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
2241 if (min_free_kbytes < 128)
2242 min_free_kbytes = 128;
2243 if (min_free_kbytes > 65536)
2244 min_free_kbytes = 65536;
2245 setup_per_zone_pages_min();
2246 setup_per_zone_lowmem_reserve();
2249 module_init(init_per_zone_pages_min)
2252 * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
2253 * that we can call two helper functions whenever min_free_kbytes
2256 int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
2257 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
2259 proc_dointvec(table, write, file, buffer, length, ppos);
2260 setup_per_zone_pages_min();
2265 * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
2266 * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
2267 * whenever sysctl_lowmem_reserve_ratio changes.
2269 * The reserve ratio obviously has absolutely no relation with the
2270 * pages_min watermarks. The lowmem reserve ratio can only make sense
2271 * if in function of the boot time zone sizes.
2273 int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
2274 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
2276 proc_dointvec_minmax(table, write, file, buffer, length, ppos);
2277 setup_per_zone_lowmem_reserve();
2281 __initdata int hashdist = HASHDIST_DEFAULT;
2284 static int __init set_hashdist(char *str)
2288 hashdist = simple_strtoul(str, &str, 0);
2291 __setup("hashdist=", set_hashdist);
2295 * allocate a large system hash table from bootmem
2296 * - it is assumed that the hash table must contain an exact power-of-2
2297 * quantity of entries
2298 * - limit is the number of hash buckets, not the total allocation size
2300 void *__init alloc_large_system_hash(const char *tablename,
2301 unsigned long bucketsize,
2302 unsigned long numentries,
2305 unsigned int *_hash_shift,
2306 unsigned int *_hash_mask,
2307 unsigned long limit)
2309 unsigned long long max = limit;
2310 unsigned long log2qty, size;
2313 /* allow the kernel cmdline to have a say */
2315 /* round applicable memory size up to nearest megabyte */
2316 numentries = (flags & HASH_HIGHMEM) ? nr_all_pages : nr_kernel_pages;
2317 numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
2318 numentries >>= 20 - PAGE_SHIFT;
2319 numentries <<= 20 - PAGE_SHIFT;
2321 /* limit to 1 bucket per 2^scale bytes of low memory */
2322 if (scale > PAGE_SHIFT)
2323 numentries >>= (scale - PAGE_SHIFT);
2325 numentries <<= (PAGE_SHIFT - scale);
2327 /* rounded up to nearest power of 2 in size */
2328 numentries = 1UL << (long_log2(numentries) + 1);
2330 /* limit allocation size to 1/16 total memory by default */
2332 max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
2333 do_div(max, bucketsize);
2336 if (numentries > max)
2339 log2qty = long_log2(numentries);
2342 size = bucketsize << log2qty;
2343 if (flags & HASH_EARLY)
2344 table = alloc_bootmem(size);
2346 table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
2348 unsigned long order;
2349 for (order = 0; ((1UL << order) << PAGE_SHIFT) < size; order++)
2351 table = (void*) __get_free_pages(GFP_ATOMIC, order);
2353 } while (!table && size > PAGE_SIZE && --log2qty);
2356 panic("Failed to allocate %s hash table\n", tablename);
2358 printk("%s hash table entries: %d (order: %d, %lu bytes)\n",
2361 long_log2(size) - PAGE_SHIFT,
2365 *_hash_shift = log2qty;
2367 *_hash_mask = (1 << log2qty) - 1;