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>
35 #include <asm/tlbflush.h>
37 DECLARE_BITMAP(node_online_map, MAX_NUMNODES);
38 struct pglist_data *pgdat_list;
39 unsigned long totalram_pages;
40 unsigned long totalhigh_pages;
43 int sysctl_lower_zone_protection = 0;
45 EXPORT_SYMBOL(totalram_pages);
46 EXPORT_SYMBOL(nr_swap_pages);
49 * Used by page_zone() to look up the address of the struct zone whose
50 * id is encoded in the upper bits of page->flags
52 struct zone *zone_table[1 << (ZONES_SHIFT + NODES_SHIFT)];
53 EXPORT_SYMBOL(zone_table);
55 static char *zone_names[MAX_NR_ZONES] = { "DMA", "Normal", "HighMem" };
56 int min_free_kbytes = 1024;
59 * Temporary debugging check for pages not lying within a given zone.
61 static int bad_range(struct zone *zone, struct page *page)
63 if (page_to_pfn(page) >= zone->zone_start_pfn + zone->spanned_pages)
65 if (page_to_pfn(page) < zone->zone_start_pfn)
67 if (zone != page_zone(page))
72 static void bad_page(const char *function, struct page *page)
74 printk("Bad page state at %s\n", function);
75 printk("flags:0x%08lx mapping:%p mapped:%d count:%d\n",
76 page->flags, page->mapping,
77 page_mapped(page), page_count(page));
78 printk("Backtrace:\n");
80 printk("Trying to fix it up, but a reboot is needed\n");
81 page->flags &= ~(1 << PG_private |
87 set_page_count(page, 0);
91 #ifndef CONFIG_HUGETLB_PAGE
92 #define prep_compound_page(page, order) do { } while (0)
93 #define destroy_compound_page(page, order) do { } while (0)
96 * Higher-order pages are called "compound pages". They are structured thusly:
98 * The first PAGE_SIZE page is called the "head page".
100 * The remaining PAGE_SIZE pages are called "tail pages".
102 * All pages have PG_compound set. All pages have their lru.next pointing at
103 * the head page (even the head page has this).
105 * The head page's lru.prev, if non-zero, holds the address of the compound
106 * page's put_page() function.
108 * The order of the allocation is stored in the first tail page's lru.prev.
109 * This is only for debug at present. This usage means that zero-order pages
110 * may not be compound.
112 static void prep_compound_page(struct page *page, unsigned long order)
115 int nr_pages = 1 << order;
117 page->lru.prev = NULL;
118 page[1].lru.prev = (void *)order;
119 for (i = 0; i < nr_pages; i++) {
120 struct page *p = page + i;
123 p->lru.next = (void *)page;
127 static void destroy_compound_page(struct page *page, unsigned long order)
130 int nr_pages = 1 << order;
132 if (page[1].lru.prev != (void *)order)
133 bad_page(__FUNCTION__, page);
135 for (i = 0; i < nr_pages; i++) {
136 struct page *p = page + i;
138 if (!PageCompound(p))
139 bad_page(__FUNCTION__, page);
140 if (p->lru.next != (void *)page)
141 bad_page(__FUNCTION__, page);
142 ClearPageCompound(p);
145 #endif /* CONFIG_HUGETLB_PAGE */
148 * Freeing function for a buddy system allocator.
150 * The concept of a buddy system is to maintain direct-mapped table
151 * (containing bit values) for memory blocks of various "orders".
152 * The bottom level table contains the map for the smallest allocatable
153 * units of memory (here, pages), and each level above it describes
154 * pairs of units from the levels below, hence, "buddies".
155 * At a high level, all that happens here is marking the table entry
156 * at the bottom level available, and propagating the changes upward
157 * as necessary, plus some accounting needed to play nicely with other
158 * parts of the VM system.
159 * At each level, we keep one bit for each pair of blocks, which
160 * is set to 1 iff only one of the pair is allocated. So when we
161 * are allocating or freeing one, we can derive the state of the
162 * other. That is, if we allocate a small block, and both were
163 * free, the remainder of the region must be split into blocks.
164 * If a block is freed, and its buddy is also free, then this
165 * triggers coalescing into a block of larger size.
170 static inline void __free_pages_bulk (struct page *page, struct page *base,
171 struct zone *zone, struct free_area *area, unsigned long mask,
174 unsigned long page_idx, index;
177 destroy_compound_page(page, order);
178 page_idx = page - base;
179 if (page_idx & ~mask)
181 index = page_idx >> (1 + order);
183 zone->free_pages -= mask;
184 while (mask + (1 << (MAX_ORDER-1))) {
185 struct page *buddy1, *buddy2;
187 BUG_ON(area >= zone->free_area + MAX_ORDER);
188 if (!__test_and_change_bit(index, area->map))
190 * the buddy page is still allocated.
194 * Move the buddy up one level.
195 * This code is taking advantage of the identity:
198 buddy1 = base + (page_idx ^ -mask);
199 buddy2 = base + page_idx;
200 BUG_ON(bad_range(zone, buddy1));
201 BUG_ON(bad_range(zone, buddy2));
202 list_del(&buddy1->list);
208 list_add(&(base + page_idx)->list, &area->free_list);
211 static inline void free_pages_check(const char *function, struct page *page)
213 if ( page_mapped(page) ||
214 page->mapping != NULL ||
215 page_count(page) != 0 ||
223 1 << PG_writeback )))
224 bad_page(function, page);
226 ClearPageDirty(page);
230 * Frees a list of pages.
231 * Assumes all pages on list are in same zone, and of same order.
232 * count is the number of pages to free, or 0 for all on the list.
234 * If the zone was previously in an "all pages pinned" state then look to
235 * see if this freeing clears that state.
237 * And clear the zone's pages_scanned counter, to hold off the "all pages are
238 * pinned" detection logic.
241 free_pages_bulk(struct zone *zone, int count,
242 struct list_head *list, unsigned int order)
244 unsigned long mask, flags;
245 struct free_area *area;
246 struct page *base, *page = NULL;
249 mask = (~0UL) << order;
250 base = zone->zone_mem_map;
251 area = zone->free_area + order;
252 spin_lock_irqsave(&zone->lock, flags);
253 zone->all_unreclaimable = 0;
254 zone->pages_scanned = 0;
255 while (!list_empty(list) && count--) {
256 page = list_entry(list->prev, struct page, list);
257 /* have to delete it as __free_pages_bulk list manipulates */
258 list_del(&page->list);
259 __free_pages_bulk(page, base, zone, area, mask, order);
262 spin_unlock_irqrestore(&zone->lock, flags);
266 void __free_pages_ok(struct page *page, unsigned int order)
271 arch_free_page(page, order);
273 mod_page_state(pgfree, 1 << order);
274 for (i = 0 ; i < (1 << order) ; ++i)
275 free_pages_check(__FUNCTION__, page + i);
276 list_add(&page->list, &list);
277 kernel_map_pages(page, 1<<order, 0);
278 free_pages_bulk(page_zone(page), 1, &list, order);
281 #define MARK_USED(index, order, area) \
282 __change_bit((index) >> (1+(order)), (area)->map)
284 static inline struct page *
285 expand(struct zone *zone, struct page *page,
286 unsigned long index, int low, int high, struct free_area *area)
288 unsigned long size = 1 << high;
291 BUG_ON(bad_range(zone, page));
295 list_add(&page->list, &area->free_list);
296 MARK_USED(index, high, area);
303 static inline void set_page_refs(struct page *page, int order)
306 set_page_count(page, 1);
311 * We need to reference all the pages for this order, otherwise if
312 * anyone accesses one of the pages with (get/put) it will be freed.
314 for (i = 0; i < (1 << order); i++)
315 set_page_count(page+i, 1);
316 #endif /* CONFIG_MMU */
320 * This page is about to be returned from the page allocator
322 static void prep_new_page(struct page *page, int order)
324 if (page->mapping || page_mapped(page) ||
332 1 << PG_writeback )))
333 bad_page(__FUNCTION__, page);
335 page->flags &= ~(1 << PG_uptodate | 1 << PG_error |
336 1 << PG_referenced | 1 << PG_arch_1 |
337 1 << PG_checked | 1 << PG_mappedtodisk);
339 set_page_refs(page, order);
343 * Do the hard work of removing an element from the buddy allocator.
344 * Call me with the zone->lock already held.
346 static struct page *__rmqueue(struct zone *zone, unsigned int order)
348 struct free_area * area;
349 unsigned int current_order;
353 for (current_order = order; current_order < MAX_ORDER; ++current_order) {
354 area = zone->free_area + current_order;
355 if (list_empty(&area->free_list))
358 page = list_entry(area->free_list.next, struct page, list);
359 list_del(&page->list);
360 index = page - zone->zone_mem_map;
361 if (current_order != MAX_ORDER-1)
362 MARK_USED(index, current_order, area);
363 zone->free_pages -= 1UL << order;
364 return expand(zone, page, index, order, current_order, area);
371 * Obtain a specified number of elements from the buddy allocator, all under
372 * a single hold of the lock, for efficiency. Add them to the supplied list.
373 * Returns the number of new pages which were placed at *list.
375 static int rmqueue_bulk(struct zone *zone, unsigned int order,
376 unsigned long count, struct list_head *list)
383 spin_lock_irqsave(&zone->lock, flags);
384 for (i = 0; i < count; ++i) {
385 page = __rmqueue(zone, order);
389 list_add_tail(&page->list, list);
391 spin_unlock_irqrestore(&zone->lock, flags);
396 int is_head_of_free_region(struct page *page)
398 struct zone *zone = page_zone(page);
401 struct list_head *curr;
404 * Should not matter as we need quiescent system for
405 * suspend anyway, but...
407 spin_lock_irqsave(&zone->lock, flags);
408 for (order = MAX_ORDER - 1; order >= 0; --order)
409 list_for_each(curr, &zone->free_area[order].free_list)
410 if (page == list_entry(curr, struct page, list)) {
411 spin_unlock_irqrestore(&zone->lock, flags);
414 spin_unlock_irqrestore(&zone->lock, flags);
419 * Spill all of this CPU's per-cpu pages back into the buddy allocator.
421 void drain_local_pages(void)
427 local_irq_save(flags);
428 for_each_zone(zone) {
429 struct per_cpu_pageset *pset;
431 pset = &zone->pageset[smp_processor_id()];
432 for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
433 struct per_cpu_pages *pcp;
436 pcp->count -= free_pages_bulk(zone, pcp->count,
440 local_irq_restore(flags);
442 #endif /* CONFIG_PM */
445 * Free a 0-order page
447 static void FASTCALL(free_hot_cold_page(struct page *page, int cold));
448 static void free_hot_cold_page(struct page *page, int cold)
450 struct zone *zone = page_zone(page);
451 struct per_cpu_pages *pcp;
454 arch_free_page(page, 0);
456 kernel_map_pages(page, 1, 0);
457 inc_page_state(pgfree);
458 free_pages_check(__FUNCTION__, page);
459 pcp = &zone->pageset[get_cpu()].pcp[cold];
460 local_irq_save(flags);
461 if (pcp->count >= pcp->high)
462 pcp->count -= free_pages_bulk(zone, pcp->batch, &pcp->list, 0);
463 list_add(&page->list, &pcp->list);
465 local_irq_restore(flags);
469 void free_hot_page(struct page *page)
471 free_hot_cold_page(page, 0);
474 void free_cold_page(struct page *page)
476 free_hot_cold_page(page, 1);
480 * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
481 * we cheat by calling it from here, in the order > 0 path. Saves a branch
485 static struct page *buffered_rmqueue(struct zone *zone, int order, int cold)
488 struct page *page = NULL;
491 struct per_cpu_pages *pcp;
493 pcp = &zone->pageset[get_cpu()].pcp[cold];
494 local_irq_save(flags);
495 if (pcp->count <= pcp->low)
496 pcp->count += rmqueue_bulk(zone, 0,
497 pcp->batch, &pcp->list);
499 page = list_entry(pcp->list.next, struct page, list);
500 list_del(&page->list);
503 local_irq_restore(flags);
508 spin_lock_irqsave(&zone->lock, flags);
509 page = __rmqueue(zone, order);
510 spin_unlock_irqrestore(&zone->lock, flags);
512 prep_compound_page(page, order);
516 BUG_ON(bad_range(zone, page));
517 mod_page_state(pgalloc, 1 << order);
518 prep_new_page(page, order);
524 * This is the 'heart' of the zoned buddy allocator.
526 * Herein lies the mysterious "incremental min". That's the
528 * local_low = z->pages_low;
531 * thing. The intent here is to provide additional protection to low zones for
532 * allocation requests which _could_ use higher zones. So a GFP_HIGHMEM
533 * request is not allowed to dip as deeply into the normal zone as a GFP_KERNEL
534 * request. This preserves additional space in those lower zones for requests
535 * which really do need memory from those zones. It means that on a decent
536 * sized machine, GFP_HIGHMEM and GFP_KERNEL requests basically leave the DMA
540 __alloc_pages(unsigned int gfp_mask, unsigned int order,
541 struct zonelist *zonelist)
543 const int wait = gfp_mask & __GFP_WAIT;
547 struct reclaim_state reclaim_state;
548 struct task_struct *p = current;
553 might_sleep_if(wait);
556 if (gfp_mask & __GFP_COLD)
559 zones = zonelist->zones; /* the list of zones suitable for gfp_mask */
560 if (zones[0] == NULL) /* no zones in the zonelist */
563 /* Go through the zonelist once, looking for a zone with enough free */
565 for (i = 0; zones[i] != NULL; i++) {
566 struct zone *z = zones[i];
567 unsigned long local_low;
570 * This is the fabled 'incremental min'. We let real-time tasks
571 * dip their real-time paws a little deeper into reserves.
573 local_low = z->pages_low;
578 if (z->free_pages >= min ||
579 (!wait && z->free_pages >= z->pages_high)) {
580 page = buffered_rmqueue(z, order, cold);
584 min += z->pages_low * sysctl_lower_zone_protection;
587 /* we're somewhat low on memory, failed to find what we needed */
588 for (i = 0; zones[i] != NULL; i++)
589 wakeup_kswapd(zones[i]);
591 /* Go through the zonelist again, taking __GFP_HIGH into account */
593 for (i = 0; zones[i] != NULL; i++) {
594 unsigned long local_min;
595 struct zone *z = zones[i];
597 local_min = z->pages_min;
598 if (gfp_mask & __GFP_HIGH)
603 if (z->free_pages >= min ||
604 (!wait && z->free_pages >= z->pages_high)) {
605 page = buffered_rmqueue(z, order, cold);
609 min += local_min * sysctl_lower_zone_protection;
612 /* here we're in the low on memory slow path */
615 if ((p->flags & (PF_MEMALLOC | PF_MEMDIE)) && !in_interrupt()) {
616 /* go through the zonelist yet again, ignoring mins */
617 for (i = 0; zones[i] != NULL; i++) {
618 struct zone *z = zones[i];
620 page = buffered_rmqueue(z, order, cold);
627 /* Atomic allocations - we can't balance anything */
631 p->flags |= PF_MEMALLOC;
632 reclaim_state.reclaimed_slab = 0;
633 p->reclaim_state = &reclaim_state;
635 try_to_free_pages(zones, gfp_mask, order);
637 p->reclaim_state = NULL;
638 p->flags &= ~PF_MEMALLOC;
640 /* go through the zonelist yet one more time */
642 for (i = 0; zones[i] != NULL; i++) {
643 struct zone *z = zones[i];
646 if (z->free_pages >= min ||
647 (!wait && z->free_pages >= z->pages_high)) {
648 page = buffered_rmqueue(z, order, cold);
652 min += z->pages_low * sysctl_lower_zone_protection;
656 * Don't let big-order allocations loop unless the caller explicitly
657 * requests that. Wait for some write requests to complete then retry.
659 * In this implementation, __GFP_REPEAT means __GFP_NOFAIL, but that
660 * may not be true in other implementations.
663 if (!(gfp_mask & __GFP_NORETRY)) {
664 if ((order <= 3) || (gfp_mask & __GFP_REPEAT))
666 if (gfp_mask & __GFP_NOFAIL)
670 blk_congestion_wait(WRITE, HZ/50);
675 if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) {
676 printk(KERN_WARNING "%s: page allocation failure."
677 " order:%d, mode:0x%x\n",
678 p->comm, order, gfp_mask);
683 kernel_map_pages(page, 1 << order, 1);
687 EXPORT_SYMBOL(__alloc_pages);
690 * Common helper functions.
692 unsigned long __get_free_pages(unsigned int gfp_mask, unsigned int order)
696 page = alloc_pages(gfp_mask, order);
699 return (unsigned long) page_address(page);
702 EXPORT_SYMBOL(__get_free_pages);
704 unsigned long get_zeroed_page(unsigned int gfp_mask)
709 * get_zeroed_page() returns a 32-bit address, which cannot represent
712 BUG_ON(gfp_mask & __GFP_HIGHMEM);
714 page = alloc_pages(gfp_mask, 0);
716 void *address = page_address(page);
718 return (unsigned long) address;
723 EXPORT_SYMBOL(get_zeroed_page);
725 void __pagevec_free(struct pagevec *pvec)
727 int i = pagevec_count(pvec);
730 free_hot_cold_page(pvec->pages[i], pvec->cold);
733 void __free_pages(struct page *page, unsigned int order)
735 if (!PageReserved(page) && put_page_testzero(page)) {
739 __free_pages_ok(page, order);
743 EXPORT_SYMBOL(__free_pages);
745 void free_pages(unsigned long addr, unsigned int order)
748 BUG_ON(!virt_addr_valid(addr));
749 __free_pages(virt_to_page(addr), order);
753 EXPORT_SYMBOL(free_pages);
756 * Total amount of free (allocatable) RAM:
758 unsigned int nr_free_pages(void)
760 unsigned int sum = 0;
764 sum += zone->free_pages;
769 EXPORT_SYMBOL(nr_free_pages);
771 unsigned int nr_used_zone_pages(void)
773 unsigned int pages = 0;
777 pages += zone->nr_active + zone->nr_inactive;
783 unsigned int nr_free_pages_pgdat(pg_data_t *pgdat)
785 unsigned int i, sum = 0;
787 for (i = 0; i < MAX_NR_ZONES; i++)
788 sum += pgdat->node_zones[i].free_pages;
794 static unsigned int nr_free_zone_pages(int offset)
797 unsigned int sum = 0;
799 for_each_pgdat(pgdat) {
800 struct zonelist *zonelist = pgdat->node_zonelists + offset;
801 struct zone **zonep = zonelist->zones;
804 for (zone = *zonep++; zone; zone = *zonep++) {
805 unsigned long size = zone->present_pages;
806 unsigned long high = zone->pages_high;
816 * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
818 unsigned int nr_free_buffer_pages(void)
820 return nr_free_zone_pages(GFP_USER & GFP_ZONEMASK);
824 * Amount of free RAM allocatable within all zones
826 unsigned int nr_free_pagecache_pages(void)
828 return nr_free_zone_pages(GFP_HIGHUSER & GFP_ZONEMASK);
831 #ifdef CONFIG_HIGHMEM
832 unsigned int nr_free_highpages (void)
835 unsigned int pages = 0;
837 for_each_pgdat(pgdat)
838 pages += pgdat->node_zones[ZONE_HIGHMEM].free_pages;
845 static void show_node(struct zone *zone)
847 printk("Node %d ", zone->zone_pgdat->node_id);
850 #define show_node(zone) do { } while (0)
854 * Accumulate the page_state information across all CPUs.
855 * The result is unavoidably approximate - it can change
856 * during and after execution of this function.
858 DEFINE_PER_CPU(struct page_state, page_states) = {0};
859 EXPORT_PER_CPU_SYMBOL(page_states);
861 atomic_t nr_pagecache = ATOMIC_INIT(0);
862 EXPORT_SYMBOL(nr_pagecache);
864 DEFINE_PER_CPU(long, nr_pagecache_local) = 0;
867 void __get_page_state(struct page_state *ret, int nr)
871 memset(ret, 0, sizeof(*ret));
872 while (cpu < NR_CPUS) {
873 unsigned long *in, *out, off;
875 if (!cpu_possible(cpu)) {
880 in = (unsigned long *)&per_cpu(page_states, cpu);
882 if (cpu < NR_CPUS && cpu_possible(cpu))
883 prefetch(&per_cpu(page_states, cpu));
884 out = (unsigned long *)ret;
885 for (off = 0; off < nr; off++)
890 void get_page_state(struct page_state *ret)
894 nr = offsetof(struct page_state, GET_PAGE_STATE_LAST);
895 nr /= sizeof(unsigned long);
897 __get_page_state(ret, nr + 1);
900 void get_full_page_state(struct page_state *ret)
902 __get_page_state(ret, sizeof(*ret) / sizeof(unsigned long));
905 void get_zone_counts(unsigned long *active,
906 unsigned long *inactive, unsigned long *free)
913 for_each_zone(zone) {
914 *active += zone->nr_active;
915 *inactive += zone->nr_inactive;
916 *free += zone->free_pages;
920 void si_meminfo(struct sysinfo *val)
922 val->totalram = totalram_pages;
924 val->freeram = nr_free_pages();
925 val->bufferram = nr_blockdev_pages();
926 #ifdef CONFIG_HIGHMEM
927 val->totalhigh = totalhigh_pages;
928 val->freehigh = nr_free_highpages();
933 val->mem_unit = PAGE_SIZE;
936 EXPORT_SYMBOL(si_meminfo);
939 void si_meminfo_node(struct sysinfo *val, int nid)
941 pg_data_t *pgdat = NODE_DATA(nid);
943 val->totalram = pgdat->node_present_pages;
944 val->freeram = nr_free_pages_pgdat(pgdat);
945 val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].present_pages;
946 val->freehigh = pgdat->node_zones[ZONE_HIGHMEM].free_pages;
947 val->mem_unit = PAGE_SIZE;
951 #define K(x) ((x) << (PAGE_SHIFT-10))
954 * Show free area list (used inside shift_scroll-lock stuff)
955 * We also calculate the percentage fragmentation. We do this by counting the
956 * memory on each free list with the exception of the first item on the list.
958 void show_free_areas(void)
960 struct page_state ps;
961 int cpu, temperature;
962 unsigned long active;
963 unsigned long inactive;
967 for_each_zone(zone) {
969 printk("%s per-cpu:", zone->name);
971 if (!zone->present_pages) {
977 for (cpu = 0; cpu < NR_CPUS; ++cpu) {
978 struct per_cpu_pageset *pageset;
980 if (!cpu_possible(cpu))
983 pageset = zone->pageset + cpu;
985 for (temperature = 0; temperature < 2; temperature++)
986 printk("cpu %d %s: low %d, high %d, batch %d\n",
988 temperature ? "cold" : "hot",
989 pageset->pcp[temperature].low,
990 pageset->pcp[temperature].high,
991 pageset->pcp[temperature].batch);
996 get_zone_counts(&active, &inactive, &free);
998 printk("\nFree pages: %11ukB (%ukB HighMem)\n",
1000 K(nr_free_highpages()));
1002 printk("Active:%lu inactive:%lu dirty:%lu writeback:%lu "
1003 "unstable:%lu free:%u\n",
1011 for_each_zone(zone) {
1022 K(zone->free_pages),
1025 K(zone->pages_high),
1027 K(zone->nr_inactive)
1031 for_each_zone(zone) {
1032 struct list_head *elem;
1033 unsigned long nr, flags, order, total = 0;
1036 printk("%s: ", zone->name);
1037 if (!zone->present_pages) {
1042 spin_lock_irqsave(&zone->lock, flags);
1043 for (order = 0; order < MAX_ORDER; order++) {
1045 list_for_each(elem, &zone->free_area[order].free_list)
1047 total += nr << order;
1048 printk("%lu*%lukB ", nr, K(1UL) << order);
1050 spin_unlock_irqrestore(&zone->lock, flags);
1051 printk("= %lukB\n", K(total));
1054 show_swap_cache_info();
1058 * Builds allocation fallback zone lists.
1060 static int __init build_zonelists_node(pg_data_t *pgdat, struct zonelist *zonelist, int j, int k)
1067 zone = pgdat->node_zones + ZONE_HIGHMEM;
1068 if (zone->present_pages) {
1069 #ifndef CONFIG_HIGHMEM
1072 zonelist->zones[j++] = zone;
1075 zone = pgdat->node_zones + ZONE_NORMAL;
1076 if (zone->present_pages)
1077 zonelist->zones[j++] = zone;
1079 zone = pgdat->node_zones + ZONE_DMA;
1080 if (zone->present_pages)
1081 zonelist->zones[j++] = zone;
1087 static void __init build_zonelists(pg_data_t *pgdat)
1089 int i, j, k, node, local_node;
1091 local_node = pgdat->node_id;
1092 for (i = 0; i < MAX_NR_ZONES; i++) {
1093 struct zonelist *zonelist;
1095 zonelist = pgdat->node_zonelists + i;
1096 memset(zonelist, 0, sizeof(*zonelist));
1100 if (i & __GFP_HIGHMEM)
1105 j = build_zonelists_node(pgdat, zonelist, j, k);
1107 * Now we build the zonelist so that it contains the zones
1108 * of all the other nodes.
1109 * We don't want to pressure a particular node, so when
1110 * building the zones for node N, we make sure that the
1111 * zones coming right after the local ones are those from
1112 * node N+1 (modulo N)
1114 for (node = local_node + 1; node < numnodes; node++)
1115 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1116 for (node = 0; node < local_node; node++)
1117 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1119 zonelist->zones[j++] = NULL;
1123 void __init build_all_zonelists(void)
1127 for(i = 0 ; i < numnodes ; i++)
1128 build_zonelists(NODE_DATA(i));
1129 printk("Built %i zonelists\n", numnodes);
1133 * Helper functions to size the waitqueue hash table.
1134 * Essentially these want to choose hash table sizes sufficiently
1135 * large so that collisions trying to wait on pages are rare.
1136 * But in fact, the number of active page waitqueues on typical
1137 * systems is ridiculously low, less than 200. So this is even
1138 * conservative, even though it seems large.
1140 * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
1141 * waitqueues, i.e. the size of the waitq table given the number of pages.
1143 #define PAGES_PER_WAITQUEUE 256
1145 static inline unsigned long wait_table_size(unsigned long pages)
1147 unsigned long size = 1;
1149 pages /= PAGES_PER_WAITQUEUE;
1151 while (size < pages)
1155 * Once we have dozens or even hundreds of threads sleeping
1156 * on IO we've got bigger problems than wait queue collision.
1157 * Limit the size of the wait table to a reasonable size.
1159 size = min(size, 4096UL);
1161 return max(size, 4UL);
1165 * This is an integer logarithm so that shifts can be used later
1166 * to extract the more random high bits from the multiplicative
1167 * hash function before the remainder is taken.
1169 static inline unsigned long wait_table_bits(unsigned long size)
1174 #define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
1176 static void __init calculate_zone_totalpages(struct pglist_data *pgdat,
1177 unsigned long *zones_size, unsigned long *zholes_size)
1179 unsigned long realtotalpages, totalpages = 0;
1182 for (i = 0; i < MAX_NR_ZONES; i++)
1183 totalpages += zones_size[i];
1184 pgdat->node_spanned_pages = totalpages;
1186 realtotalpages = totalpages;
1188 for (i = 0; i < MAX_NR_ZONES; i++)
1189 realtotalpages -= zholes_size[i];
1190 pgdat->node_present_pages = realtotalpages;
1191 printk("On node %d totalpages: %lu\n", pgdat->node_id, realtotalpages);
1196 * Initially all pages are reserved - free ones are freed
1197 * up by free_all_bootmem() once the early boot process is
1198 * done. Non-atomic initialization, single-pass.
1200 void __init memmap_init_zone(struct page *start, unsigned long size, int nid,
1201 unsigned long zone, unsigned long start_pfn)
1205 for (page = start; page < (start + size); page++) {
1206 set_page_zone(page, NODEZONE(nid, zone));
1207 set_page_count(page, 0);
1208 SetPageReserved(page);
1209 INIT_LIST_HEAD(&page->list);
1210 #ifdef WANT_PAGE_VIRTUAL
1211 /* The shift won't overflow because ZONE_NORMAL is below 4G. */
1212 if (zone != ZONE_HIGHMEM)
1213 set_page_address(page, __va(start_pfn << PAGE_SHIFT));
1219 #ifndef __HAVE_ARCH_MEMMAP_INIT
1220 #define memmap_init(start, size, nid, zone, start_pfn) \
1221 memmap_init_zone((start), (size), (nid), (zone), (start_pfn))
1225 * Set up the zone data structures:
1226 * - mark all pages reserved
1227 * - mark all memory queues empty
1228 * - clear the memory bitmaps
1230 static void __init free_area_init_core(struct pglist_data *pgdat,
1231 unsigned long *zones_size, unsigned long *zholes_size)
1234 const unsigned long zone_required_alignment = 1UL << (MAX_ORDER-1);
1235 int cpu, nid = pgdat->node_id;
1236 struct page *lmem_map = pgdat->node_mem_map;
1237 unsigned long zone_start_pfn = pgdat->node_start_pfn;
1239 pgdat->nr_zones = 0;
1240 init_waitqueue_head(&pgdat->kswapd_wait);
1242 for (j = 0; j < MAX_NR_ZONES; j++) {
1243 struct zone *zone = pgdat->node_zones + j;
1244 unsigned long size, realsize;
1245 unsigned long batch;
1247 zone_table[NODEZONE(nid, j)] = zone;
1248 realsize = size = zones_size[j];
1250 realsize -= zholes_size[j];
1252 zone->spanned_pages = size;
1253 zone->present_pages = realsize;
1254 zone->name = zone_names[j];
1255 spin_lock_init(&zone->lock);
1256 spin_lock_init(&zone->lru_lock);
1257 zone->zone_pgdat = pgdat;
1258 zone->free_pages = 0;
1260 zone->temp_priority = zone->prev_priority = DEF_PRIORITY;
1263 * The per-cpu-pages pools are set to around 1000th of the
1264 * size of the zone. But no more than 1/4 of a meg - there's
1265 * no point in going beyond the size of L2 cache.
1267 * OK, so we don't know how big the cache is. So guess.
1269 batch = zone->present_pages / 1024;
1270 if (batch * PAGE_SIZE > 256 * 1024)
1271 batch = (256 * 1024) / PAGE_SIZE;
1272 batch /= 4; /* We effectively *= 4 below */
1276 for (cpu = 0; cpu < NR_CPUS; cpu++) {
1277 struct per_cpu_pages *pcp;
1279 pcp = &zone->pageset[cpu].pcp[0]; /* hot */
1281 pcp->low = 2 * batch;
1282 pcp->high = 6 * batch;
1283 pcp->batch = 1 * batch;
1284 INIT_LIST_HEAD(&pcp->list);
1286 pcp = &zone->pageset[cpu].pcp[1]; /* cold */
1289 pcp->high = 2 * batch;
1290 pcp->batch = 1 * batch;
1291 INIT_LIST_HEAD(&pcp->list);
1293 printk(" %s zone: %lu pages, LIFO batch:%lu\n",
1294 zone_names[j], realsize, batch);
1295 INIT_LIST_HEAD(&zone->active_list);
1296 INIT_LIST_HEAD(&zone->inactive_list);
1297 atomic_set(&zone->refill_counter, 0);
1298 zone->nr_active = 0;
1299 zone->nr_inactive = 0;
1304 * The per-page waitqueue mechanism uses hashed waitqueues
1307 zone->wait_table_size = wait_table_size(size);
1308 zone->wait_table_bits =
1309 wait_table_bits(zone->wait_table_size);
1310 zone->wait_table = (wait_queue_head_t *)
1311 alloc_bootmem_node(pgdat, zone->wait_table_size
1312 * sizeof(wait_queue_head_t));
1314 for(i = 0; i < zone->wait_table_size; ++i)
1315 init_waitqueue_head(zone->wait_table + i);
1317 pgdat->nr_zones = j+1;
1319 zone->zone_mem_map = lmem_map;
1320 zone->zone_start_pfn = zone_start_pfn;
1322 if ((zone_start_pfn) & (zone_required_alignment-1))
1323 printk("BUG: wrong zone alignment, it will crash\n");
1325 memmap_init(lmem_map, size, nid, j, zone_start_pfn);
1327 zone_start_pfn += size;
1330 for (i = 0; ; i++) {
1331 unsigned long bitmap_size;
1333 INIT_LIST_HEAD(&zone->free_area[i].free_list);
1334 if (i == MAX_ORDER-1) {
1335 zone->free_area[i].map = NULL;
1340 * Page buddy system uses "index >> (i+1)",
1341 * where "index" is at most "size-1".
1343 * The extra "+3" is to round down to byte
1344 * size (8 bits per byte assumption). Thus
1345 * we get "(size-1) >> (i+4)" as the last byte
1348 * The "+1" is because we want to round the
1349 * byte allocation up rather than down. So
1350 * we should have had a "+7" before we shifted
1351 * down by three. Also, we have to add one as
1352 * we actually _use_ the last bit (it's [0,n]
1353 * inclusive, not [0,n[).
1355 * So we actually had +7+1 before we shift
1356 * down by 3. But (n+8) >> 3 == (n >> 3) + 1
1357 * (modulo overflows, which we do not have).
1359 * Finally, we LONG_ALIGN because all bitmap
1360 * operations are on longs.
1362 bitmap_size = (size-1) >> (i+4);
1363 bitmap_size = LONG_ALIGN(bitmap_size+1);
1364 zone->free_area[i].map =
1365 (unsigned long *) alloc_bootmem_node(pgdat, bitmap_size);
1370 void __init free_area_init_node(int nid, struct pglist_data *pgdat,
1371 struct page *node_mem_map, unsigned long *zones_size,
1372 unsigned long node_start_pfn, unsigned long *zholes_size)
1376 pgdat->node_id = nid;
1377 pgdat->node_start_pfn = node_start_pfn;
1378 calculate_zone_totalpages(pgdat, zones_size, zholes_size);
1379 if (!node_mem_map) {
1380 size = (pgdat->node_spanned_pages + 1) * sizeof(struct page);
1381 node_mem_map = alloc_bootmem_node(pgdat, size);
1383 pgdat->node_mem_map = node_mem_map;
1385 free_area_init_core(pgdat, zones_size, zholes_size);
1388 #ifndef CONFIG_DISCONTIGMEM
1389 static bootmem_data_t contig_bootmem_data;
1390 struct pglist_data contig_page_data = { .bdata = &contig_bootmem_data };
1392 EXPORT_SYMBOL(contig_page_data);
1394 void __init free_area_init(unsigned long *zones_size)
1396 free_area_init_node(0, &contig_page_data, NULL, zones_size,
1397 __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
1398 mem_map = contig_page_data.node_mem_map;
1402 #ifdef CONFIG_PROC_FS
1404 #include <linux/seq_file.h>
1406 static void *frag_start(struct seq_file *m, loff_t *pos)
1411 for (pgdat = pgdat_list; pgdat && node; pgdat = pgdat->pgdat_next)
1417 static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
1419 pg_data_t *pgdat = (pg_data_t *)arg;
1422 return pgdat->pgdat_next;
1425 static void frag_stop(struct seq_file *m, void *arg)
1430 * This walks the freelist for each zone. Whilst this is slow, I'd rather
1431 * be slow here than slow down the fast path by keeping stats - mjbligh
1433 static int frag_show(struct seq_file *m, void *arg)
1435 pg_data_t *pgdat = (pg_data_t *)arg;
1437 struct zone *node_zones = pgdat->node_zones;
1438 unsigned long flags;
1441 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
1442 if (!zone->present_pages)
1445 spin_lock_irqsave(&zone->lock, flags);
1446 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
1447 for (order = 0; order < MAX_ORDER; ++order) {
1448 unsigned long nr_bufs = 0;
1449 struct list_head *elem;
1451 list_for_each(elem, &(zone->free_area[order].free_list))
1453 seq_printf(m, "%6lu ", nr_bufs);
1455 spin_unlock_irqrestore(&zone->lock, flags);
1461 struct seq_operations fragmentation_op = {
1462 .start = frag_start,
1468 static char *vmstat_text[] = {
1472 "nr_page_table_pages",
1494 "kswapd_inodesteal",
1500 static void *vmstat_start(struct seq_file *m, loff_t *pos)
1502 struct page_state *ps;
1504 if (*pos >= ARRAY_SIZE(vmstat_text))
1507 ps = kmalloc(sizeof(*ps), GFP_KERNEL);
1510 return ERR_PTR(-ENOMEM);
1511 get_full_page_state(ps);
1512 ps->pgpgin /= 2; /* sectors -> kbytes */
1514 return (unsigned long *)ps + *pos;
1517 static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
1520 if (*pos >= ARRAY_SIZE(vmstat_text))
1522 return (unsigned long *)m->private + *pos;
1525 static int vmstat_show(struct seq_file *m, void *arg)
1527 unsigned long *l = arg;
1528 unsigned long off = l - (unsigned long *)m->private;
1530 seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
1534 static void vmstat_stop(struct seq_file *m, void *arg)
1540 struct seq_operations vmstat_op = {
1541 .start = vmstat_start,
1542 .next = vmstat_next,
1543 .stop = vmstat_stop,
1544 .show = vmstat_show,
1547 #endif /* CONFIG_PROC_FS */
1550 void __init page_alloc_init(void)
1555 * setup_per_zone_pages_min - called when min_free_kbytes changes. Ensures
1556 * that the pages_{min,low,high} values for each zone are set correctly
1557 * with respect to min_free_kbytes.
1559 static void setup_per_zone_pages_min(void)
1561 unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
1562 unsigned long lowmem_pages = 0;
1564 unsigned long flags;
1566 /* Calculate total number of !ZONE_HIGHMEM pages */
1568 if (!is_highmem(zone))
1569 lowmem_pages += zone->present_pages;
1571 for_each_zone(zone) {
1572 spin_lock_irqsave(&zone->lru_lock, flags);
1573 if (is_highmem(zone)) {
1575 * Often, highmem doesn't need to reserve any pages.
1576 * But the pages_min/low/high values are also used for
1577 * batching up page reclaim activity so we need a
1578 * decent value here.
1582 min_pages = zone->present_pages / 1024;
1583 if (min_pages < SWAP_CLUSTER_MAX)
1584 min_pages = SWAP_CLUSTER_MAX;
1585 if (min_pages > 128)
1587 zone->pages_min = min_pages;
1589 /* if it's a lowmem zone, reserve a number of pages
1590 * proportionate to the zone's size.
1592 zone->pages_min = (pages_min * zone->present_pages) /
1596 zone->pages_low = zone->pages_min * 2;
1597 zone->pages_high = zone->pages_min * 3;
1598 spin_unlock_irqrestore(&zone->lru_lock, flags);
1603 * Initialise min_free_kbytes.
1605 * For small machines we want it small (128k min). For large machines
1606 * we want it large (16MB max). But it is not linear, because network
1607 * bandwidth does not increase linearly with machine size. We use
1609 * min_free_kbytes = sqrt(lowmem_kbytes)
1625 static int __init init_per_zone_pages_min(void)
1627 unsigned long lowmem_kbytes;
1629 lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
1631 min_free_kbytes = int_sqrt(lowmem_kbytes);
1632 if (min_free_kbytes < 128)
1633 min_free_kbytes = 128;
1634 if (min_free_kbytes > 16384)
1635 min_free_kbytes = 16384;
1636 setup_per_zone_pages_min();
1639 module_init(init_per_zone_pages_min)
1642 * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
1643 * that we can call setup_per_zone_pages_min() whenever min_free_kbytes
1646 int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
1647 struct file *file, void __user *buffer, size_t *length)
1649 proc_dointvec(table, write, file, buffer, length);
1650 setup_per_zone_pages_min();