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/vs_base.h>
35 #include <linux/vs_limit.h>
37 #include <asm/tlbflush.h>
39 DECLARE_BITMAP(node_online_map, MAX_NUMNODES);
40 struct pglist_data *pgdat_list;
41 unsigned long totalram_pages;
42 unsigned long totalhigh_pages;
45 int sysctl_lower_zone_protection = 0;
47 EXPORT_SYMBOL(totalram_pages);
48 EXPORT_SYMBOL(nr_swap_pages);
50 #ifdef CONFIG_CRASH_DUMP_MODULE
51 /* This symbol has to be exported to use 'for_each_pgdat' macro by modules. */
52 EXPORT_SYMBOL(pgdat_list);
56 * Used by page_zone() to look up the address of the struct zone whose
57 * id is encoded in the upper bits of page->flags
59 struct zone *zone_table[1 << (ZONES_SHIFT + NODES_SHIFT)];
60 EXPORT_SYMBOL(zone_table);
62 static char *zone_names[MAX_NR_ZONES] = { "DMA", "Normal", "HighMem" };
63 int min_free_kbytes = 1024;
65 static unsigned long __initdata nr_kernel_pages;
66 static unsigned long __initdata nr_all_pages;
69 * Temporary debugging check for pages not lying within a given zone.
71 static int bad_range(struct zone *zone, struct page *page)
73 if (page_to_pfn(page) >= zone->zone_start_pfn + zone->spanned_pages)
75 if (page_to_pfn(page) < zone->zone_start_pfn)
77 if (zone != page_zone(page))
82 static void bad_page(const char *function, struct page *page)
84 printk(KERN_EMERG "Bad page state at %s (in process '%s', page %p)\n",
85 function, current->comm, page);
86 printk(KERN_EMERG "flags:0x%08lx mapping:%p mapcount:%d count:%d\n",
87 (unsigned long)page->flags, page->mapping,
88 (int)page->mapcount, page_count(page));
89 printk(KERN_EMERG "Backtrace:\n");
91 printk(KERN_EMERG "Trying to fix it up, but a reboot is needed\n");
92 page->flags &= ~(1 << PG_private |
101 set_page_count(page, 0);
102 page->mapping = NULL;
106 #if !defined(CONFIG_HUGETLB_PAGE) && !defined(CONFIG_CRASH_DUMP) \
107 && !defined(CONFIG_CRASH_DUMP_MODULE)
108 #define prep_compound_page(page, order) do { } while (0)
109 #define destroy_compound_page(page, order) do { } while (0)
112 * Higher-order pages are called "compound pages". They are structured thusly:
114 * The first PAGE_SIZE page is called the "head page".
116 * The remaining PAGE_SIZE pages are called "tail pages".
118 * All pages have PG_compound set. All pages have their ->private pointing at
119 * the head page (even the head page has this).
121 * The first tail page's ->mapping, if non-zero, holds the address of the
122 * compound page's put_page() function.
124 * The order of the allocation is stored in the first tail page's ->index
125 * This is only for debug at present. This usage means that zero-order pages
126 * may not be compound.
128 static void prep_compound_page(struct page *page, unsigned long order)
131 int nr_pages = 1 << order;
133 page[1].mapping = NULL;
134 page[1].index = order;
135 for (i = 0; i < nr_pages; i++) {
136 struct page *p = page + i;
139 p->private = (unsigned long)page;
143 static void destroy_compound_page(struct page *page, unsigned long order)
146 int nr_pages = 1 << order;
148 if (!PageCompound(page))
151 if (page[1].index != order)
152 bad_page(__FUNCTION__, page);
154 for (i = 0; i < nr_pages; i++) {
155 struct page *p = page + i;
157 if (!PageCompound(p))
158 bad_page(__FUNCTION__, page);
159 if (p->private != (unsigned long)page)
160 bad_page(__FUNCTION__, page);
161 ClearPageCompound(p);
164 #endif /* CONFIG_HUGETLB_PAGE */
167 * Freeing function for a buddy system allocator.
169 * The concept of a buddy system is to maintain direct-mapped table
170 * (containing bit values) for memory blocks of various "orders".
171 * The bottom level table contains the map for the smallest allocatable
172 * units of memory (here, pages), and each level above it describes
173 * pairs of units from the levels below, hence, "buddies".
174 * At a high level, all that happens here is marking the table entry
175 * at the bottom level available, and propagating the changes upward
176 * as necessary, plus some accounting needed to play nicely with other
177 * parts of the VM system.
178 * At each level, we keep one bit for each pair of blocks, which
179 * is set to 1 iff only one of the pair is allocated. So when we
180 * are allocating or freeing one, we can derive the state of the
181 * other. That is, if we allocate a small block, and both were
182 * free, the remainder of the region must be split into blocks.
183 * If a block is freed, and its buddy is also free, then this
184 * triggers coalescing into a block of larger size.
189 static inline void __free_pages_bulk (struct page *page, struct page *base,
190 struct zone *zone, struct free_area *area, unsigned int order)
192 unsigned long page_idx, index, mask;
195 destroy_compound_page(page, order);
196 mask = (~0UL) << order;
197 page_idx = page - base;
198 if (page_idx & ~mask)
200 index = page_idx >> (1 + order);
202 zone->free_pages += 1 << order;
203 while (order < MAX_ORDER-1) {
204 struct page *buddy1, *buddy2;
206 BUG_ON(area >= zone->free_area + MAX_ORDER);
207 if (!__test_and_change_bit(index, area->map))
209 * the buddy page is still allocated.
213 /* Move the buddy up one level. */
214 buddy1 = base + (page_idx ^ (1 << order));
215 buddy2 = base + page_idx;
216 BUG_ON(bad_range(zone, buddy1));
217 BUG_ON(bad_range(zone, buddy2));
218 list_del(&buddy1->lru);
225 list_add(&(base + page_idx)->lru, &area->free_list);
228 static inline void free_pages_check(const char *function, struct page *page)
230 if ( page_mapped(page) ||
231 page->mapping != NULL ||
232 page_count(page) != 0 ||
243 1 << PG_writeback )))
244 bad_page(function, page);
246 ClearPageDirty(page);
250 * Frees a list of pages.
251 * Assumes all pages on list are in same zone, and of same order.
252 * count is the number of pages to free, or 0 for all on the list.
254 * If the zone was previously in an "all pages pinned" state then look to
255 * see if this freeing clears that state.
257 * And clear the zone's pages_scanned counter, to hold off the "all pages are
258 * pinned" detection logic.
261 free_pages_bulk(struct zone *zone, int count,
262 struct list_head *list, unsigned int order)
265 struct free_area *area;
266 struct page *base, *page = NULL;
269 base = zone->zone_mem_map;
270 area = zone->free_area + order;
271 spin_lock_irqsave(&zone->lock, flags);
272 zone->all_unreclaimable = 0;
273 zone->pages_scanned = 0;
274 while (!list_empty(list) && count--) {
275 page = list_entry(list->prev, struct page, lru);
276 /* have to delete it as __free_pages_bulk list manipulates */
277 list_del(&page->lru);
278 __free_pages_bulk(page, base, zone, area, order);
281 spin_unlock_irqrestore(&zone->lock, flags);
285 void __free_pages_ok(struct page *page, unsigned int order)
290 arch_free_page(page, order);
292 mod_page_state(pgfree, 1 << order);
293 for (i = 0 ; i < (1 << order) ; ++i)
294 free_pages_check(__FUNCTION__, page + i);
295 list_add(&page->lru, &list);
296 kernel_map_pages(page, 1<<order, 0);
297 free_pages_bulk(page_zone(page), 1, &list, order);
300 #define MARK_USED(index, order, area) \
301 __change_bit((index) >> (1+(order)), (area)->map)
304 * The order of subdivision here is critical for the IO subsystem.
305 * Please do not alter this order without good reasons and regression
306 * testing. Specifically, as large blocks of memory are subdivided,
307 * the order in which smaller blocks are delivered depends on the order
308 * they're subdivided in this function. This is the primary factor
309 * influencing the order in which pages are delivered to the IO
310 * subsystem according to empirical testing, and this is also justified
311 * by considering the behavior of a buddy system containing a single
312 * large block of memory acted on by a series of small allocations.
313 * This behavior is a critical factor in sglist merging's success.
317 static inline struct page *
318 expand(struct zone *zone, struct page *page,
319 unsigned long index, int low, int high, struct free_area *area)
321 unsigned long size = 1 << high;
327 BUG_ON(bad_range(zone, &page[size]));
328 list_add(&page[size].lru, &area->free_list);
329 MARK_USED(index + size, high, area);
334 static inline void set_page_refs(struct page *page, int order)
337 set_page_count(page, 1);
342 * We need to reference all the pages for this order, otherwise if
343 * anyone accesses one of the pages with (get/put) it will be freed.
345 for (i = 0; i < (1 << order); i++)
346 set_page_count(page+i, 1);
347 #endif /* CONFIG_MMU */
351 * This page is about to be returned from the page allocator
353 static void prep_new_page(struct page *page, int order)
355 if (page->mapping || page_mapped(page) ||
366 1 << PG_writeback )))
367 bad_page(__FUNCTION__, page);
369 page->flags &= ~(1 << PG_uptodate | 1 << PG_error |
370 1 << PG_referenced | 1 << PG_arch_1 |
371 1 << PG_checked | 1 << PG_mappedtodisk);
373 set_page_refs(page, order);
377 * Do the hard work of removing an element from the buddy allocator.
378 * Call me with the zone->lock already held.
380 static struct page *__rmqueue(struct zone *zone, unsigned int order)
382 struct free_area * area;
383 unsigned int current_order;
387 for (current_order = order; current_order < MAX_ORDER; ++current_order) {
388 area = zone->free_area + current_order;
389 if (list_empty(&area->free_list))
392 page = list_entry(area->free_list.next, struct page, lru);
393 list_del(&page->lru);
394 index = page - zone->zone_mem_map;
395 if (current_order != MAX_ORDER-1)
396 MARK_USED(index, current_order, area);
397 zone->free_pages -= 1UL << order;
398 return expand(zone, page, index, order, current_order, area);
405 * Obtain a specified number of elements from the buddy allocator, all under
406 * a single hold of the lock, for efficiency. Add them to the supplied list.
407 * Returns the number of new pages which were placed at *list.
409 static int rmqueue_bulk(struct zone *zone, unsigned int order,
410 unsigned long count, struct list_head *list)
417 spin_lock_irqsave(&zone->lock, flags);
418 for (i = 0; i < count; ++i) {
419 page = __rmqueue(zone, order);
423 list_add_tail(&page->lru, list);
425 spin_unlock_irqrestore(&zone->lock, flags);
429 #if defined(CONFIG_PM) || defined(CONFIG_HOTPLUG_CPU)
430 static void __drain_pages(unsigned int cpu)
435 for_each_zone(zone) {
436 struct per_cpu_pageset *pset;
438 pset = &zone->pageset[cpu];
439 for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
440 struct per_cpu_pages *pcp;
443 pcp->count -= free_pages_bulk(zone, pcp->count,
448 #endif /* CONFIG_PM || CONFIG_HOTPLUG_CPU */
451 int is_head_of_free_region(struct page *page)
453 struct zone *zone = page_zone(page);
456 struct list_head *curr;
459 * Should not matter as we need quiescent system for
460 * suspend anyway, but...
462 spin_lock_irqsave(&zone->lock, flags);
463 for (order = MAX_ORDER - 1; order >= 0; --order)
464 list_for_each(curr, &zone->free_area[order].free_list)
465 if (page == list_entry(curr, struct page, lru)) {
466 spin_unlock_irqrestore(&zone->lock, flags);
469 spin_unlock_irqrestore(&zone->lock, flags);
474 * Spill all of this CPU's per-cpu pages back into the buddy allocator.
476 void drain_local_pages(void)
480 local_irq_save(flags);
481 __drain_pages(smp_processor_id());
482 local_irq_restore(flags);
484 #endif /* CONFIG_PM */
486 static void zone_statistics(struct zonelist *zonelist, struct zone *z)
491 pg_data_t *pg = z->zone_pgdat;
492 pg_data_t *orig = zonelist->zones[0]->zone_pgdat;
493 struct per_cpu_pageset *p;
495 local_irq_save(flags);
496 cpu = smp_processor_id();
497 p = &z->pageset[cpu];
499 z->pageset[cpu].numa_hit++;
502 zonelist->zones[0]->pageset[cpu].numa_foreign++;
504 if (pg == NODE_DATA(numa_node_id()))
508 local_irq_restore(flags);
513 * Free a 0-order page
515 static void FASTCALL(free_hot_cold_page(struct page *page, int cold));
516 static void fastcall free_hot_cold_page(struct page *page, int cold)
518 struct zone *zone = page_zone(page);
519 struct per_cpu_pages *pcp;
522 arch_free_page(page, 0);
524 kernel_map_pages(page, 1, 0);
525 inc_page_state(pgfree);
526 free_pages_check(__FUNCTION__, page);
527 pcp = &zone->pageset[get_cpu()].pcp[cold];
528 local_irq_save(flags);
529 if (pcp->count >= pcp->high)
530 pcp->count -= free_pages_bulk(zone, pcp->batch, &pcp->list, 0);
531 list_add(&page->lru, &pcp->list);
533 local_irq_restore(flags);
537 void fastcall free_hot_page(struct page *page)
539 free_hot_cold_page(page, 0);
542 void fastcall free_cold_page(struct page *page)
544 free_hot_cold_page(page, 1);
548 * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
549 * we cheat by calling it from here, in the order > 0 path. Saves a branch
554 buffered_rmqueue(struct zone *zone, int order, int gfp_flags)
557 struct page *page = NULL;
558 int cold = !!(gfp_flags & __GFP_COLD);
561 struct per_cpu_pages *pcp;
563 pcp = &zone->pageset[get_cpu()].pcp[cold];
564 local_irq_save(flags);
565 if (pcp->count <= pcp->low)
566 pcp->count += rmqueue_bulk(zone, 0,
567 pcp->batch, &pcp->list);
569 page = list_entry(pcp->list.next, struct page, lru);
570 list_del(&page->lru);
573 local_irq_restore(flags);
578 spin_lock_irqsave(&zone->lock, flags);
579 page = __rmqueue(zone, order);
580 spin_unlock_irqrestore(&zone->lock, flags);
584 BUG_ON(bad_range(zone, page));
585 mod_page_state_zone(zone, pgalloc, 1 << order);
586 prep_new_page(page, order);
587 if (order && (gfp_flags & __GFP_COMP))
588 prep_compound_page(page, order);
594 * This is the 'heart' of the zoned buddy allocator.
596 * Herein lies the mysterious "incremental min". That's the
598 * local_low = z->pages_low;
601 * thing. The intent here is to provide additional protection to low zones for
602 * allocation requests which _could_ use higher zones. So a GFP_HIGHMEM
603 * request is not allowed to dip as deeply into the normal zone as a GFP_KERNEL
604 * request. This preserves additional space in those lower zones for requests
605 * which really do need memory from those zones. It means that on a decent
606 * sized machine, GFP_HIGHMEM and GFP_KERNEL requests basically leave the DMA
609 struct page * fastcall
610 __alloc_pages(unsigned int gfp_mask, unsigned int order,
611 struct zonelist *zonelist)
613 const int wait = gfp_mask & __GFP_WAIT;
617 struct reclaim_state reclaim_state;
618 struct task_struct *p = current;
623 might_sleep_if(wait);
625 zones = zonelist->zones; /* the list of zones suitable for gfp_mask */
626 if (zones[0] == NULL) /* no zones in the zonelist */
629 alloc_type = zone_idx(zones[0]);
631 /* Go through the zonelist once, looking for a zone with enough free */
632 for (i = 0; zones[i] != NULL; i++) {
633 struct zone *z = zones[i];
635 min = (1<<order) + z->protection[alloc_type];
638 * We let real-time tasks dip their real-time paws a little
639 * deeper into reserves.
642 min -= z->pages_low >> 1;
644 if (z->free_pages >= min ||
645 (!wait && z->free_pages >= z->pages_high)) {
646 page = buffered_rmqueue(z, order, gfp_mask);
648 zone_statistics(zonelist, z);
654 /* we're somewhat low on memory, failed to find what we needed */
655 for (i = 0; zones[i] != NULL; i++)
656 wakeup_kswapd(zones[i]);
658 /* Go through the zonelist again, taking __GFP_HIGH into account */
659 for (i = 0; zones[i] != NULL; i++) {
660 struct zone *z = zones[i];
662 min = (1<<order) + z->protection[alloc_type];
664 if (gfp_mask & __GFP_HIGH)
665 min -= z->pages_low >> 2;
667 min -= z->pages_low >> 1;
669 if (z->free_pages >= min ||
670 (!wait && z->free_pages >= z->pages_high)) {
671 page = buffered_rmqueue(z, order, gfp_mask);
673 zone_statistics(zonelist, z);
679 /* here we're in the low on memory slow path */
682 if ((p->flags & (PF_MEMALLOC | PF_MEMDIE)) && !in_interrupt()) {
683 /* go through the zonelist yet again, ignoring mins */
684 for (i = 0; zones[i] != NULL; i++) {
685 struct zone *z = zones[i];
687 page = buffered_rmqueue(z, order, gfp_mask);
689 zone_statistics(zonelist, z);
696 /* Atomic allocations - we can't balance anything */
700 p->flags |= PF_MEMALLOC;
701 reclaim_state.reclaimed_slab = 0;
702 p->reclaim_state = &reclaim_state;
704 try_to_free_pages(zones, gfp_mask, order);
706 p->reclaim_state = NULL;
707 p->flags &= ~PF_MEMALLOC;
709 /* go through the zonelist yet one more time */
710 for (i = 0; zones[i] != NULL; i++) {
711 struct zone *z = zones[i];
713 min = (1UL << order) + z->protection[alloc_type];
715 if (z->free_pages >= min ||
716 (!wait && z->free_pages >= z->pages_high)) {
717 page = buffered_rmqueue(z, order, gfp_mask);
719 zone_statistics(zonelist, z);
726 * Don't let big-order allocations loop unless the caller explicitly
727 * requests that. Wait for some write requests to complete then retry.
729 * In this implementation, __GFP_REPEAT means __GFP_NOFAIL, but that
730 * may not be true in other implementations.
733 if (!(gfp_mask & __GFP_NORETRY)) {
734 if ((order <= 3) || (gfp_mask & __GFP_REPEAT))
736 if (gfp_mask & __GFP_NOFAIL)
740 blk_congestion_wait(WRITE, HZ/50);
745 if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) {
746 printk(KERN_WARNING "%s: page allocation failure."
747 " order:%d, mode:0x%x\n",
748 p->comm, order, gfp_mask);
753 kernel_map_pages(page, 1 << order, 1);
757 EXPORT_SYMBOL(__alloc_pages);
760 * Common helper functions.
762 fastcall unsigned long __get_free_pages(unsigned int gfp_mask, unsigned int order)
765 page = alloc_pages(gfp_mask, order);
768 return (unsigned long) page_address(page);
771 EXPORT_SYMBOL(__get_free_pages);
773 fastcall unsigned long get_zeroed_page(unsigned int gfp_mask)
778 * get_zeroed_page() returns a 32-bit address, which cannot represent
781 BUG_ON(gfp_mask & __GFP_HIGHMEM);
783 page = alloc_pages(gfp_mask, 0);
785 void *address = page_address(page);
787 return (unsigned long) address;
792 EXPORT_SYMBOL(get_zeroed_page);
794 void __pagevec_free(struct pagevec *pvec)
796 int i = pagevec_count(pvec);
799 free_hot_cold_page(pvec->pages[i], pvec->cold);
802 fastcall void __free_pages(struct page *page, unsigned int order)
804 if (!PageReserved(page) && put_page_testzero(page)) {
808 __free_pages_ok(page, order);
812 EXPORT_SYMBOL(__free_pages);
814 fastcall void free_pages(unsigned long addr, unsigned int order)
817 BUG_ON(!virt_addr_valid(addr));
818 __free_pages(virt_to_page(addr), order);
822 EXPORT_SYMBOL(free_pages);
825 * Total amount of free (allocatable) RAM:
827 unsigned int nr_free_pages(void)
829 unsigned int sum = 0;
833 sum += zone->free_pages;
838 EXPORT_SYMBOL(nr_free_pages);
840 unsigned int nr_used_zone_pages(void)
842 unsigned int pages = 0;
846 pages += zone->nr_active + zone->nr_inactive;
852 unsigned int nr_free_pages_pgdat(pg_data_t *pgdat)
854 unsigned int i, sum = 0;
856 for (i = 0; i < MAX_NR_ZONES; i++)
857 sum += pgdat->node_zones[i].free_pages;
863 static unsigned int nr_free_zone_pages(int offset)
866 unsigned int sum = 0;
868 for_each_pgdat(pgdat) {
869 struct zonelist *zonelist = pgdat->node_zonelists + offset;
870 struct zone **zonep = zonelist->zones;
873 for (zone = *zonep++; zone; zone = *zonep++) {
874 unsigned long size = zone->present_pages;
875 unsigned long high = zone->pages_high;
885 * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
887 unsigned int nr_free_buffer_pages(void)
889 return nr_free_zone_pages(GFP_USER & GFP_ZONEMASK);
893 * Amount of free RAM allocatable within all zones
895 unsigned int nr_free_pagecache_pages(void)
897 return nr_free_zone_pages(GFP_HIGHUSER & GFP_ZONEMASK);
900 #ifdef CONFIG_HIGHMEM
901 unsigned int nr_free_highpages (void)
904 unsigned int pages = 0;
906 for_each_pgdat(pgdat)
907 pages += pgdat->node_zones[ZONE_HIGHMEM].free_pages;
914 static void show_node(struct zone *zone)
916 printk("Node %d ", zone->zone_pgdat->node_id);
919 #define show_node(zone) do { } while (0)
923 * Accumulate the page_state information across all CPUs.
924 * The result is unavoidably approximate - it can change
925 * during and after execution of this function.
927 DEFINE_PER_CPU(struct page_state, page_states) = {0};
928 EXPORT_PER_CPU_SYMBOL(page_states);
930 atomic_t nr_pagecache = ATOMIC_INIT(0);
931 EXPORT_SYMBOL(nr_pagecache);
933 DEFINE_PER_CPU(long, nr_pagecache_local) = 0;
936 void __get_page_state(struct page_state *ret, int nr)
940 memset(ret, 0, sizeof(*ret));
941 while (cpu < NR_CPUS) {
942 unsigned long *in, *out, off;
944 if (!cpu_possible(cpu)) {
949 in = (unsigned long *)&per_cpu(page_states, cpu);
951 if (cpu < NR_CPUS && cpu_possible(cpu))
952 prefetch(&per_cpu(page_states, cpu));
953 out = (unsigned long *)ret;
954 for (off = 0; off < nr; off++)
959 void get_page_state(struct page_state *ret)
963 nr = offsetof(struct page_state, GET_PAGE_STATE_LAST);
964 nr /= sizeof(unsigned long);
966 __get_page_state(ret, nr + 1);
969 void get_full_page_state(struct page_state *ret)
971 __get_page_state(ret, sizeof(*ret) / sizeof(unsigned long));
974 unsigned long __read_page_state(unsigned offset)
976 unsigned long ret = 0;
979 for (cpu = 0; cpu < NR_CPUS; cpu++) {
982 if (!cpu_possible(cpu))
985 in = (unsigned long)&per_cpu(page_states, cpu) + offset;
986 ret += *((unsigned long *)in);
991 void get_zone_counts(unsigned long *active,
992 unsigned long *inactive, unsigned long *free)
999 for_each_zone(zone) {
1000 *active += zone->nr_active;
1001 *inactive += zone->nr_inactive;
1002 *free += zone->free_pages;
1006 void si_meminfo(struct sysinfo *val)
1008 val->totalram = totalram_pages;
1010 val->freeram = nr_free_pages();
1011 val->bufferram = nr_blockdev_pages();
1012 #ifdef CONFIG_HIGHMEM
1013 val->totalhigh = totalhigh_pages;
1014 val->freehigh = nr_free_highpages();
1019 val->mem_unit = PAGE_SIZE;
1020 if (vx_flags(VXF_VIRT_MEM, 0))
1021 vx_vsi_meminfo(val);
1024 EXPORT_SYMBOL(si_meminfo);
1027 void si_meminfo_node(struct sysinfo *val, int nid)
1029 pg_data_t *pgdat = NODE_DATA(nid);
1031 val->totalram = pgdat->node_present_pages;
1032 val->freeram = nr_free_pages_pgdat(pgdat);
1033 val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].present_pages;
1034 val->freehigh = pgdat->node_zones[ZONE_HIGHMEM].free_pages;
1035 val->mem_unit = PAGE_SIZE;
1039 #define K(x) ((x) << (PAGE_SHIFT-10))
1042 * Show free area list (used inside shift_scroll-lock stuff)
1043 * We also calculate the percentage fragmentation. We do this by counting the
1044 * memory on each free list with the exception of the first item on the list.
1046 void show_free_areas(void)
1048 struct page_state ps;
1049 int cpu, temperature;
1050 unsigned long active;
1051 unsigned long inactive;
1055 for_each_zone(zone) {
1057 printk("%s per-cpu:", zone->name);
1059 if (!zone->present_pages) {
1065 for (cpu = 0; cpu < NR_CPUS; ++cpu) {
1066 struct per_cpu_pageset *pageset;
1068 if (!cpu_possible(cpu))
1071 pageset = zone->pageset + cpu;
1073 for (temperature = 0; temperature < 2; temperature++)
1074 printk("cpu %d %s: low %d, high %d, batch %d\n",
1076 temperature ? "cold" : "hot",
1077 pageset->pcp[temperature].low,
1078 pageset->pcp[temperature].high,
1079 pageset->pcp[temperature].batch);
1083 get_page_state(&ps);
1084 get_zone_counts(&active, &inactive, &free);
1086 printk("\nFree pages: %11ukB (%ukB HighMem)\n",
1088 K(nr_free_highpages()));
1090 printk("Active:%lu inactive:%lu dirty:%lu writeback:%lu "
1091 "unstable:%lu free:%u slab:%lu mapped:%lu pagetables:%lu\n",
1100 ps.nr_page_table_pages);
1102 for_each_zone(zone) {
1116 K(zone->free_pages),
1119 K(zone->pages_high),
1121 K(zone->nr_inactive),
1122 K(zone->present_pages)
1124 printk("protections[]:");
1125 for (i = 0; i < MAX_NR_ZONES; i++)
1126 printk(" %lu", zone->protection[i]);
1130 for_each_zone(zone) {
1131 struct list_head *elem;
1132 unsigned long nr, flags, order, total = 0;
1135 printk("%s: ", zone->name);
1136 if (!zone->present_pages) {
1141 spin_lock_irqsave(&zone->lock, flags);
1142 for (order = 0; order < MAX_ORDER; order++) {
1144 list_for_each(elem, &zone->free_area[order].free_list)
1146 total += nr << order;
1147 printk("%lu*%lukB ", nr, K(1UL) << order);
1149 spin_unlock_irqrestore(&zone->lock, flags);
1150 printk("= %lukB\n", K(total));
1153 show_swap_cache_info();
1157 * Builds allocation fallback zone lists.
1159 static int __init build_zonelists_node(pg_data_t *pgdat, struct zonelist *zonelist, int j, int k)
1166 zone = pgdat->node_zones + ZONE_HIGHMEM;
1167 if (zone->present_pages) {
1168 #ifndef CONFIG_HIGHMEM
1171 zonelist->zones[j++] = zone;
1174 zone = pgdat->node_zones + ZONE_NORMAL;
1175 if (zone->present_pages)
1176 zonelist->zones[j++] = zone;
1178 zone = pgdat->node_zones + ZONE_DMA;
1179 if (zone->present_pages)
1180 zonelist->zones[j++] = zone;
1187 #define MAX_NODE_LOAD (numnodes)
1188 static int __initdata node_load[MAX_NUMNODES];
1190 * find_next_best_node - find the next node that should appear in a given
1191 * node's fallback list
1192 * @node: node whose fallback list we're appending
1193 * @used_node_mask: pointer to the bitmap of already used nodes
1195 * We use a number of factors to determine which is the next node that should
1196 * appear on a given node's fallback list. The node should not have appeared
1197 * already in @node's fallback list, and it should be the next closest node
1198 * according to the distance array (which contains arbitrary distance values
1199 * from each node to each node in the system), and should also prefer nodes
1200 * with no CPUs, since presumably they'll have very little allocation pressure
1201 * on them otherwise.
1202 * It returns -1 if no node is found.
1204 static int __init find_next_best_node(int node, void *used_node_mask)
1207 int min_val = INT_MAX;
1210 for (i = 0; i < numnodes; i++) {
1213 /* Start from local node */
1214 n = (node+i)%numnodes;
1216 /* Don't want a node to appear more than once */
1217 if (test_bit(n, used_node_mask))
1220 /* Use the distance array to find the distance */
1221 val = node_distance(node, n);
1223 /* Give preference to headless and unused nodes */
1224 tmp = node_to_cpumask(n);
1225 if (!cpus_empty(tmp))
1226 val += PENALTY_FOR_NODE_WITH_CPUS;
1228 /* Slight preference for less loaded node */
1229 val *= (MAX_NODE_LOAD*MAX_NUMNODES);
1230 val += node_load[n];
1232 if (val < min_val) {
1239 set_bit(best_node, used_node_mask);
1244 static void __init build_zonelists(pg_data_t *pgdat)
1246 int i, j, k, node, local_node;
1247 int prev_node, load;
1248 struct zonelist *zonelist;
1249 DECLARE_BITMAP(used_mask, MAX_NUMNODES);
1251 /* initialize zonelists */
1252 for (i = 0; i < GFP_ZONETYPES; i++) {
1253 zonelist = pgdat->node_zonelists + i;
1254 memset(zonelist, 0, sizeof(*zonelist));
1255 zonelist->zones[0] = NULL;
1258 /* NUMA-aware ordering of nodes */
1259 local_node = pgdat->node_id;
1261 prev_node = local_node;
1262 bitmap_zero(used_mask, MAX_NUMNODES);
1263 while ((node = find_next_best_node(local_node, used_mask)) >= 0) {
1265 * We don't want to pressure a particular node.
1266 * So adding penalty to the first node in same
1267 * distance group to make it round-robin.
1269 if (node_distance(local_node, node) !=
1270 node_distance(local_node, prev_node))
1271 node_load[node] += load;
1274 for (i = 0; i < GFP_ZONETYPES; i++) {
1275 zonelist = pgdat->node_zonelists + i;
1276 for (j = 0; zonelist->zones[j] != NULL; j++);
1279 if (i & __GFP_HIGHMEM)
1284 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1285 zonelist->zones[j] = NULL;
1290 #else /* CONFIG_NUMA */
1292 static void __init build_zonelists(pg_data_t *pgdat)
1294 int i, j, k, node, local_node;
1296 local_node = pgdat->node_id;
1297 for (i = 0; i < GFP_ZONETYPES; i++) {
1298 struct zonelist *zonelist;
1300 zonelist = pgdat->node_zonelists + i;
1301 memset(zonelist, 0, sizeof(*zonelist));
1305 if (i & __GFP_HIGHMEM)
1310 j = build_zonelists_node(pgdat, zonelist, j, k);
1312 * Now we build the zonelist so that it contains the zones
1313 * of all the other nodes.
1314 * We don't want to pressure a particular node, so when
1315 * building the zones for node N, we make sure that the
1316 * zones coming right after the local ones are those from
1317 * node N+1 (modulo N)
1319 for (node = local_node + 1; node < numnodes; node++)
1320 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1321 for (node = 0; node < local_node; node++)
1322 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1324 zonelist->zones[j] = NULL;
1328 #endif /* CONFIG_NUMA */
1330 void __init build_all_zonelists(void)
1334 for(i = 0 ; i < numnodes ; i++)
1335 build_zonelists(NODE_DATA(i));
1336 printk("Built %i zonelists\n", numnodes);
1340 * Helper functions to size the waitqueue hash table.
1341 * Essentially these want to choose hash table sizes sufficiently
1342 * large so that collisions trying to wait on pages are rare.
1343 * But in fact, the number of active page waitqueues on typical
1344 * systems is ridiculously low, less than 200. So this is even
1345 * conservative, even though it seems large.
1347 * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
1348 * waitqueues, i.e. the size of the waitq table given the number of pages.
1350 #define PAGES_PER_WAITQUEUE 256
1352 static inline unsigned long wait_table_size(unsigned long pages)
1354 unsigned long size = 1;
1356 pages /= PAGES_PER_WAITQUEUE;
1358 while (size < pages)
1362 * Once we have dozens or even hundreds of threads sleeping
1363 * on IO we've got bigger problems than wait queue collision.
1364 * Limit the size of the wait table to a reasonable size.
1366 size = min(size, 4096UL);
1368 return max(size, 4UL);
1372 * This is an integer logarithm so that shifts can be used later
1373 * to extract the more random high bits from the multiplicative
1374 * hash function before the remainder is taken.
1376 static inline unsigned long wait_table_bits(unsigned long size)
1381 #define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
1383 static void __init calculate_zone_totalpages(struct pglist_data *pgdat,
1384 unsigned long *zones_size, unsigned long *zholes_size)
1386 unsigned long realtotalpages, totalpages = 0;
1389 for (i = 0; i < MAX_NR_ZONES; i++)
1390 totalpages += zones_size[i];
1391 pgdat->node_spanned_pages = totalpages;
1393 realtotalpages = totalpages;
1395 for (i = 0; i < MAX_NR_ZONES; i++)
1396 realtotalpages -= zholes_size[i];
1397 pgdat->node_present_pages = realtotalpages;
1398 printk("On node %d totalpages: %lu\n", pgdat->node_id, realtotalpages);
1403 * Initially all pages are reserved - free ones are freed
1404 * up by free_all_bootmem() once the early boot process is
1405 * done. Non-atomic initialization, single-pass.
1407 void __init memmap_init_zone(struct page *start, unsigned long size, int nid,
1408 unsigned long zone, unsigned long start_pfn)
1412 for (page = start; page < (start + size); page++) {
1413 set_page_zone(page, NODEZONE(nid, zone));
1414 set_page_count(page, 0);
1415 SetPageReserved(page);
1416 INIT_LIST_HEAD(&page->lru);
1417 #ifdef WANT_PAGE_VIRTUAL
1418 /* The shift won't overflow because ZONE_NORMAL is below 4G. */
1419 if (!is_highmem(zone))
1420 set_page_address(page, __va(start_pfn << PAGE_SHIFT));
1426 #ifndef __HAVE_ARCH_MEMMAP_INIT
1427 #define memmap_init(start, size, nid, zone, start_pfn) \
1428 memmap_init_zone((start), (size), (nid), (zone), (start_pfn))
1432 * Set up the zone data structures:
1433 * - mark all pages reserved
1434 * - mark all memory queues empty
1435 * - clear the memory bitmaps
1437 static void __init free_area_init_core(struct pglist_data *pgdat,
1438 unsigned long *zones_size, unsigned long *zholes_size)
1441 const unsigned long zone_required_alignment = 1UL << (MAX_ORDER-1);
1442 int cpu, nid = pgdat->node_id;
1443 struct page *lmem_map = pgdat->node_mem_map;
1444 unsigned long zone_start_pfn = pgdat->node_start_pfn;
1446 pgdat->nr_zones = 0;
1447 init_waitqueue_head(&pgdat->kswapd_wait);
1449 for (j = 0; j < MAX_NR_ZONES; j++) {
1450 struct zone *zone = pgdat->node_zones + j;
1451 unsigned long size, realsize;
1452 unsigned long batch;
1454 zone_table[NODEZONE(nid, j)] = zone;
1455 realsize = size = zones_size[j];
1457 realsize -= zholes_size[j];
1459 if (j == ZONE_DMA || j == ZONE_NORMAL)
1460 nr_kernel_pages += realsize;
1461 nr_all_pages += realsize;
1463 zone->spanned_pages = size;
1464 zone->present_pages = realsize;
1465 zone->name = zone_names[j];
1466 spin_lock_init(&zone->lock);
1467 spin_lock_init(&zone->lru_lock);
1468 zone->zone_pgdat = pgdat;
1469 zone->free_pages = 0;
1471 zone->temp_priority = zone->prev_priority = DEF_PRIORITY;
1474 * The per-cpu-pages pools are set to around 1000th of the
1475 * size of the zone. But no more than 1/4 of a meg - there's
1476 * no point in going beyond the size of L2 cache.
1478 * OK, so we don't know how big the cache is. So guess.
1480 batch = zone->present_pages / 1024;
1481 if (batch * PAGE_SIZE > 256 * 1024)
1482 batch = (256 * 1024) / PAGE_SIZE;
1483 batch /= 4; /* We effectively *= 4 below */
1487 for (cpu = 0; cpu < NR_CPUS; cpu++) {
1488 struct per_cpu_pages *pcp;
1490 pcp = &zone->pageset[cpu].pcp[0]; /* hot */
1492 pcp->low = 2 * batch;
1493 pcp->high = 6 * batch;
1494 pcp->batch = 1 * batch;
1495 INIT_LIST_HEAD(&pcp->list);
1497 pcp = &zone->pageset[cpu].pcp[1]; /* cold */
1500 pcp->high = 2 * batch;
1501 pcp->batch = 1 * batch;
1502 INIT_LIST_HEAD(&pcp->list);
1504 printk(" %s zone: %lu pages, LIFO batch:%lu\n",
1505 zone_names[j], realsize, batch);
1506 INIT_LIST_HEAD(&zone->active_list);
1507 INIT_LIST_HEAD(&zone->inactive_list);
1508 zone->nr_scan_active = 0;
1509 zone->nr_scan_inactive = 0;
1510 zone->nr_active = 0;
1511 zone->nr_inactive = 0;
1516 * The per-page waitqueue mechanism uses hashed waitqueues
1519 zone->wait_table_size = wait_table_size(size);
1520 zone->wait_table_bits =
1521 wait_table_bits(zone->wait_table_size);
1522 zone->wait_table = (wait_queue_head_t *)
1523 alloc_bootmem_node(pgdat, zone->wait_table_size
1524 * sizeof(wait_queue_head_t));
1526 for(i = 0; i < zone->wait_table_size; ++i)
1527 init_waitqueue_head(zone->wait_table + i);
1529 pgdat->nr_zones = j+1;
1531 zone->zone_mem_map = lmem_map;
1532 zone->zone_start_pfn = zone_start_pfn;
1534 if ((zone_start_pfn) & (zone_required_alignment-1))
1535 printk("BUG: wrong zone alignment, it will crash\n");
1537 memmap_init(lmem_map, size, nid, j, zone_start_pfn);
1539 zone_start_pfn += size;
1542 for (i = 0; ; i++) {
1543 unsigned long bitmap_size;
1545 INIT_LIST_HEAD(&zone->free_area[i].free_list);
1546 if (i == MAX_ORDER-1) {
1547 zone->free_area[i].map = NULL;
1552 * Page buddy system uses "index >> (i+1)",
1553 * where "index" is at most "size-1".
1555 * The extra "+3" is to round down to byte
1556 * size (8 bits per byte assumption). Thus
1557 * we get "(size-1) >> (i+4)" as the last byte
1560 * The "+1" is because we want to round the
1561 * byte allocation up rather than down. So
1562 * we should have had a "+7" before we shifted
1563 * down by three. Also, we have to add one as
1564 * we actually _use_ the last bit (it's [0,n]
1565 * inclusive, not [0,n[).
1567 * So we actually had +7+1 before we shift
1568 * down by 3. But (n+8) >> 3 == (n >> 3) + 1
1569 * (modulo overflows, which we do not have).
1571 * Finally, we LONG_ALIGN because all bitmap
1572 * operations are on longs.
1574 bitmap_size = (size-1) >> (i+4);
1575 bitmap_size = LONG_ALIGN(bitmap_size+1);
1576 zone->free_area[i].map =
1577 (unsigned long *) alloc_bootmem_node(pgdat, bitmap_size);
1582 void __init free_area_init_node(int nid, struct pglist_data *pgdat,
1583 struct page *node_mem_map, unsigned long *zones_size,
1584 unsigned long node_start_pfn, unsigned long *zholes_size)
1588 pgdat->node_id = nid;
1589 pgdat->node_start_pfn = node_start_pfn;
1590 calculate_zone_totalpages(pgdat, zones_size, zholes_size);
1591 if (!node_mem_map) {
1592 size = (pgdat->node_spanned_pages + 1) * sizeof(struct page);
1593 node_mem_map = alloc_bootmem_node(pgdat, size);
1595 pgdat->node_mem_map = node_mem_map;
1597 free_area_init_core(pgdat, zones_size, zholes_size);
1600 #ifndef CONFIG_DISCONTIGMEM
1601 static bootmem_data_t contig_bootmem_data;
1602 struct pglist_data contig_page_data = { .bdata = &contig_bootmem_data };
1604 EXPORT_SYMBOL(contig_page_data);
1606 void __init free_area_init(unsigned long *zones_size)
1608 free_area_init_node(0, &contig_page_data, NULL, zones_size,
1609 __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
1610 mem_map = contig_page_data.node_mem_map;
1614 #ifdef CONFIG_PROC_FS
1616 #include <linux/seq_file.h>
1618 static void *frag_start(struct seq_file *m, loff_t *pos)
1623 for (pgdat = pgdat_list; pgdat && node; pgdat = pgdat->pgdat_next)
1629 static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
1631 pg_data_t *pgdat = (pg_data_t *)arg;
1634 return pgdat->pgdat_next;
1637 static void frag_stop(struct seq_file *m, void *arg)
1642 * This walks the freelist for each zone. Whilst this is slow, I'd rather
1643 * be slow here than slow down the fast path by keeping stats - mjbligh
1645 static int frag_show(struct seq_file *m, void *arg)
1647 pg_data_t *pgdat = (pg_data_t *)arg;
1649 struct zone *node_zones = pgdat->node_zones;
1650 unsigned long flags;
1653 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
1654 if (!zone->present_pages)
1657 spin_lock_irqsave(&zone->lock, flags);
1658 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
1659 for (order = 0; order < MAX_ORDER; ++order) {
1660 unsigned long nr_bufs = 0;
1661 struct list_head *elem;
1663 list_for_each(elem, &(zone->free_area[order].free_list))
1665 seq_printf(m, "%6lu ", nr_bufs);
1667 spin_unlock_irqrestore(&zone->lock, flags);
1673 struct seq_operations fragmentation_op = {
1674 .start = frag_start,
1680 static char *vmstat_text[] = {
1684 "nr_page_table_pages",
1709 "pgscan_kswapd_high",
1710 "pgscan_kswapd_normal",
1712 "pgscan_kswapd_dma",
1713 "pgscan_direct_high",
1714 "pgscan_direct_normal",
1715 "pgscan_direct_dma",
1720 "kswapd_inodesteal",
1727 static void *vmstat_start(struct seq_file *m, loff_t *pos)
1729 struct page_state *ps;
1731 if (*pos >= ARRAY_SIZE(vmstat_text))
1734 ps = kmalloc(sizeof(*ps), GFP_KERNEL);
1737 return ERR_PTR(-ENOMEM);
1738 get_full_page_state(ps);
1739 ps->pgpgin /= 2; /* sectors -> kbytes */
1741 return (unsigned long *)ps + *pos;
1744 static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
1747 if (*pos >= ARRAY_SIZE(vmstat_text))
1749 return (unsigned long *)m->private + *pos;
1752 static int vmstat_show(struct seq_file *m, void *arg)
1754 unsigned long *l = arg;
1755 unsigned long off = l - (unsigned long *)m->private;
1757 seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
1761 static void vmstat_stop(struct seq_file *m, void *arg)
1767 struct seq_operations vmstat_op = {
1768 .start = vmstat_start,
1769 .next = vmstat_next,
1770 .stop = vmstat_stop,
1771 .show = vmstat_show,
1774 #endif /* CONFIG_PROC_FS */
1776 #ifdef CONFIG_HOTPLUG_CPU
1777 static int page_alloc_cpu_notify(struct notifier_block *self,
1778 unsigned long action, void *hcpu)
1780 int cpu = (unsigned long)hcpu;
1783 if (action == CPU_DEAD) {
1784 /* Drain local pagecache count. */
1785 count = &per_cpu(nr_pagecache_local, cpu);
1786 atomic_add(*count, &nr_pagecache);
1788 local_irq_disable();
1794 #endif /* CONFIG_HOTPLUG_CPU */
1796 void __init page_alloc_init(void)
1798 hotcpu_notifier(page_alloc_cpu_notify, 0);
1801 static unsigned long higherzone_val(struct zone *z, int max_zone,
1804 int z_idx = zone_idx(z);
1805 struct zone *higherzone;
1806 unsigned long pages;
1808 /* there is no higher zone to get a contribution from */
1809 if (z_idx == MAX_NR_ZONES-1)
1812 higherzone = &z->zone_pgdat->node_zones[z_idx+1];
1814 /* We always start with the higher zone's protection value */
1815 pages = higherzone->protection[alloc_type];
1818 * We get a lower-zone-protection contribution only if there are
1819 * pages in the higher zone and if we're not the highest zone
1820 * in the current zonelist. e.g., never happens for GFP_DMA. Happens
1821 * only for ZONE_DMA in a GFP_KERNEL allocation and happens for ZONE_DMA
1822 * and ZONE_NORMAL for a GFP_HIGHMEM allocation.
1824 if (higherzone->present_pages && z_idx < alloc_type)
1825 pages += higherzone->pages_low * sysctl_lower_zone_protection;
1831 * setup_per_zone_protection - called whenver min_free_kbytes or
1832 * sysctl_lower_zone_protection changes. Ensures that each zone
1833 * has a correct pages_protected value, so an adequate number of
1834 * pages are left in the zone after a successful __alloc_pages().
1836 * This algorithm is way confusing. I tries to keep the same behavior
1837 * as we had with the incremental min iterative algorithm.
1839 static void setup_per_zone_protection(void)
1841 struct pglist_data *pgdat;
1842 struct zone *zones, *zone;
1846 for_each_pgdat(pgdat) {
1847 zones = pgdat->node_zones;
1849 for (i = 0, max_zone = 0; i < MAX_NR_ZONES; i++)
1850 if (zones[i].present_pages)
1854 * For each of the different allocation types:
1855 * GFP_DMA -> GFP_KERNEL -> GFP_HIGHMEM
1857 for (i = 0; i < GFP_ZONETYPES; i++) {
1859 * For each of the zones:
1860 * ZONE_HIGHMEM -> ZONE_NORMAL -> ZONE_DMA
1862 for (j = MAX_NR_ZONES-1; j >= 0; j--) {
1866 * We never protect zones that don't have memory
1867 * in them (j>max_zone) or zones that aren't in
1868 * the zonelists for a certain type of
1869 * allocation (j>i). We have to assign these to
1870 * zero because the lower zones take
1871 * contributions from the higher zones.
1873 if (j > max_zone || j > i) {
1874 zone->protection[i] = 0;
1878 * The contribution of the next higher zone
1880 zone->protection[i] = higherzone_val(zone,
1882 zone->protection[i] += zone->pages_low;
1889 * setup_per_zone_pages_min - called when min_free_kbytes changes. Ensures
1890 * that the pages_{min,low,high} values for each zone are set correctly
1891 * with respect to min_free_kbytes.
1893 static void setup_per_zone_pages_min(void)
1895 unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
1896 unsigned long lowmem_pages = 0;
1898 unsigned long flags;
1900 /* Calculate total number of !ZONE_HIGHMEM pages */
1901 for_each_zone(zone) {
1902 if (!is_highmem(zone))
1903 lowmem_pages += zone->present_pages;
1906 for_each_zone(zone) {
1907 spin_lock_irqsave(&zone->lru_lock, flags);
1908 if (is_highmem(zone)) {
1910 * Often, highmem doesn't need to reserve any pages.
1911 * But the pages_min/low/high values are also used for
1912 * batching up page reclaim activity so we need a
1913 * decent value here.
1917 min_pages = zone->present_pages / 1024;
1918 if (min_pages < SWAP_CLUSTER_MAX)
1919 min_pages = SWAP_CLUSTER_MAX;
1920 if (min_pages > 128)
1922 zone->pages_min = min_pages;
1924 /* if it's a lowmem zone, reserve a number of pages
1925 * proportionate to the zone's size.
1927 zone->pages_min = (pages_min * zone->present_pages) /
1931 zone->pages_low = zone->pages_min * 2;
1932 zone->pages_high = zone->pages_min * 3;
1933 spin_unlock_irqrestore(&zone->lru_lock, flags);
1938 * Initialise min_free_kbytes.
1940 * For small machines we want it small (128k min). For large machines
1941 * we want it large (16MB max). But it is not linear, because network
1942 * bandwidth does not increase linearly with machine size. We use
1944 * min_free_kbytes = sqrt(lowmem_kbytes)
1960 static int __init init_per_zone_pages_min(void)
1962 unsigned long lowmem_kbytes;
1964 lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
1966 min_free_kbytes = int_sqrt(lowmem_kbytes);
1967 if (min_free_kbytes < 128)
1968 min_free_kbytes = 128;
1969 if (min_free_kbytes > 16384)
1970 min_free_kbytes = 16384;
1971 setup_per_zone_pages_min();
1972 setup_per_zone_protection();
1975 module_init(init_per_zone_pages_min)
1978 * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
1979 * that we can call two helper functions whenever min_free_kbytes
1982 int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
1983 struct file *file, void __user *buffer, size_t *length)
1985 proc_dointvec(table, write, file, buffer, length);
1986 setup_per_zone_pages_min();
1987 setup_per_zone_protection();
1992 * lower_zone_protection_sysctl_handler - just a wrapper around
1993 * proc_dointvec() so that we can call setup_per_zone_protection()
1994 * whenever sysctl_lower_zone_protection changes.
1996 int lower_zone_protection_sysctl_handler(ctl_table *table, int write,
1997 struct file *file, void __user *buffer, size_t *length)
1999 proc_dointvec_minmax(table, write, file, buffer, length);
2000 setup_per_zone_protection();
2005 * allocate a large system hash table from bootmem
2006 * - it is assumed that the hash table must contain an exact power-of-2
2007 * quantity of entries
2009 void *__init alloc_large_system_hash(const char *tablename,
2010 unsigned long bucketsize,
2011 unsigned long numentries,
2013 int consider_highmem,
2014 unsigned int *_hash_shift,
2015 unsigned int *_hash_mask)
2017 unsigned long mem, max, log2qty, size;
2020 /* round applicable memory size up to nearest megabyte */
2021 mem = consider_highmem ? nr_all_pages : nr_kernel_pages;
2022 mem += (1UL << (20 - PAGE_SHIFT)) - 1;
2023 mem >>= 20 - PAGE_SHIFT;
2024 mem <<= 20 - PAGE_SHIFT;
2026 /* limit to 1 bucket per 2^scale bytes of low memory (rounded up to
2027 * nearest power of 2 in size) */
2028 if (scale > PAGE_SHIFT)
2029 mem >>= (scale - PAGE_SHIFT);
2031 mem <<= (PAGE_SHIFT - scale);
2033 mem = 1UL << (long_log2(mem) + 1);
2035 /* limit allocation size */
2036 max = (1UL << (PAGE_SHIFT + MAX_SYS_HASH_TABLE_ORDER)) / bucketsize;
2040 /* allow the kernel cmdline to have a say */
2041 if (!numentries || numentries > max)
2044 log2qty = long_log2(numentries);
2047 size = bucketsize << log2qty;
2049 table = (void *) alloc_bootmem(size);
2051 } while (!table && size > PAGE_SIZE);
2054 panic("Failed to allocate %s hash table\n", tablename);
2056 printk("%s hash table entries: %d (order: %d, %lu bytes)\n",
2059 long_log2(size) - PAGE_SHIFT,
2063 *_hash_shift = log2qty;
2065 *_hash_mask = (1 << log2qty) - 1;