[PATCH] DVB: frontend conversion #3
[linux-2.6/history.git] / mm / page_alloc.c
blob274a7af40a89ec269138d678dd1d0dec1f8bf2ed
1 /*
2 * linux/mm/page_alloc.c
4 * Manages the free list, the system allocates free pages here.
5 * Note that kmalloc() lives in slab.c
7 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
8 * Swap reorganised 29.12.95, Stephen Tweedie
9 * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
10 * Reshaped it to be a zoned allocator, Ingo Molnar, Red Hat, 1999
11 * Discontiguous memory support, Kanoj Sarcar, SGI, Nov 1999
12 * Zone balancing, Kanoj Sarcar, SGI, Jan 2000
13 * Per cpu hot/cold page lists, bulk allocation, Martin J. Bligh, Sept 2002
14 * (lots of bits borrowed from Ingo Molnar & Andrew Morton)
17 #include <linux/config.h>
18 #include <linux/stddef.h>
19 #include <linux/mm.h>
20 #include <linux/swap.h>
21 #include <linux/interrupt.h>
22 #include <linux/pagemap.h>
23 #include <linux/bootmem.h>
24 #include <linux/compiler.h>
25 #include <linux/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/nodemask.h>
36 #include <asm/tlbflush.h>
38 nodemask_t node_online_map = NODE_MASK_NONE;
39 nodemask_t node_possible_map = NODE_MASK_ALL;
40 struct pglist_data *pgdat_list;
41 unsigned long totalram_pages;
42 unsigned long totalhigh_pages;
43 long nr_swap_pages;
44 int numnodes = 1;
45 int sysctl_lower_zone_protection = 0;
47 EXPORT_SYMBOL(totalram_pages);
48 EXPORT_SYMBOL(nr_swap_pages);
51 * Used by page_zone() to look up the address of the struct zone whose
52 * id is encoded in the upper bits of page->flags
54 struct zone *zone_table[1 << (ZONES_SHIFT + NODES_SHIFT)];
55 EXPORT_SYMBOL(zone_table);
57 static char *zone_names[MAX_NR_ZONES] = { "DMA", "Normal", "HighMem" };
58 int min_free_kbytes = 1024;
60 unsigned long __initdata nr_kernel_pages;
61 unsigned long __initdata nr_all_pages;
64 * Temporary debugging check for pages not lying within a given zone.
66 static int bad_range(struct zone *zone, struct page *page)
68 if (page_to_pfn(page) >= zone->zone_start_pfn + zone->spanned_pages)
69 return 1;
70 if (page_to_pfn(page) < zone->zone_start_pfn)
71 return 1;
72 if (zone != page_zone(page))
73 return 1;
74 return 0;
77 static void bad_page(const char *function, struct page *page)
79 printk(KERN_EMERG "Bad page state at %s (in process '%s', page %p)\n",
80 function, current->comm, page);
81 printk(KERN_EMERG "flags:0x%0*lx mapping:%p mapcount:%d count:%d\n",
82 (int)(2*sizeof(page_flags_t)), (unsigned long)page->flags,
83 page->mapping, page_mapcount(page), page_count(page));
84 printk(KERN_EMERG "Backtrace:\n");
85 dump_stack();
86 printk(KERN_EMERG "Trying to fix it up, but a reboot is needed\n");
87 page->flags &= ~(1 << PG_private |
88 1 << PG_locked |
89 1 << PG_lru |
90 1 << PG_active |
91 1 << PG_dirty |
92 1 << PG_swapcache |
93 1 << PG_writeback);
94 set_page_count(page, 0);
95 reset_page_mapcount(page);
96 page->mapping = NULL;
97 tainted |= TAINT_BAD_PAGE;
100 #ifndef CONFIG_HUGETLB_PAGE
101 #define prep_compound_page(page, order) do { } while (0)
102 #define destroy_compound_page(page, order) do { } while (0)
103 #else
105 * Higher-order pages are called "compound pages". They are structured thusly:
107 * The first PAGE_SIZE page is called the "head page".
109 * The remaining PAGE_SIZE pages are called "tail pages".
111 * All pages have PG_compound set. All pages have their ->private pointing at
112 * the head page (even the head page has this).
114 * The first tail page's ->mapping, if non-zero, holds the address of the
115 * compound page's put_page() function.
117 * The order of the allocation is stored in the first tail page's ->index
118 * This is only for debug at present. This usage means that zero-order pages
119 * may not be compound.
121 static void prep_compound_page(struct page *page, unsigned long order)
123 int i;
124 int nr_pages = 1 << order;
126 page[1].mapping = NULL;
127 page[1].index = order;
128 for (i = 0; i < nr_pages; i++) {
129 struct page *p = page + i;
131 SetPageCompound(p);
132 p->private = (unsigned long)page;
136 static void destroy_compound_page(struct page *page, unsigned long order)
138 int i;
139 int nr_pages = 1 << order;
141 if (!PageCompound(page))
142 return;
144 if (page[1].index != order)
145 bad_page(__FUNCTION__, page);
147 for (i = 0; i < nr_pages; i++) {
148 struct page *p = page + i;
150 if (!PageCompound(p))
151 bad_page(__FUNCTION__, page);
152 if (p->private != (unsigned long)page)
153 bad_page(__FUNCTION__, page);
154 ClearPageCompound(p);
157 #endif /* CONFIG_HUGETLB_PAGE */
160 * Freeing function for a buddy system allocator.
162 * The concept of a buddy system is to maintain direct-mapped table
163 * (containing bit values) for memory blocks of various "orders".
164 * The bottom level table contains the map for the smallest allocatable
165 * units of memory (here, pages), and each level above it describes
166 * pairs of units from the levels below, hence, "buddies".
167 * At a high level, all that happens here is marking the table entry
168 * at the bottom level available, and propagating the changes upward
169 * as necessary, plus some accounting needed to play nicely with other
170 * parts of the VM system.
171 * At each level, we keep one bit for each pair of blocks, which
172 * is set to 1 iff only one of the pair is allocated. So when we
173 * are allocating or freeing one, we can derive the state of the
174 * other. That is, if we allocate a small block, and both were
175 * free, the remainder of the region must be split into blocks.
176 * If a block is freed, and its buddy is also free, then this
177 * triggers coalescing into a block of larger size.
179 * -- wli
182 static inline void __free_pages_bulk (struct page *page, struct page *base,
183 struct zone *zone, struct free_area *area, unsigned int order)
185 unsigned long page_idx, index, mask;
187 if (order)
188 destroy_compound_page(page, order);
189 mask = (~0UL) << order;
190 page_idx = page - base;
191 if (page_idx & ~mask)
192 BUG();
193 index = page_idx >> (1 + order);
195 zone->free_pages += 1 << order;
196 while (order < MAX_ORDER-1) {
197 struct page *buddy1, *buddy2;
199 BUG_ON(area >= zone->free_area + MAX_ORDER);
200 if (!__test_and_change_bit(index, area->map))
202 * the buddy page is still allocated.
204 break;
206 /* Move the buddy up one level. */
207 buddy1 = base + (page_idx ^ (1 << order));
208 buddy2 = base + page_idx;
209 BUG_ON(bad_range(zone, buddy1));
210 BUG_ON(bad_range(zone, buddy2));
211 list_del(&buddy1->lru);
212 mask <<= 1;
213 order++;
214 area++;
215 index >>= 1;
216 page_idx &= mask;
218 list_add(&(base + page_idx)->lru, &area->free_list);
221 static inline void free_pages_check(const char *function, struct page *page)
223 if ( page_mapped(page) ||
224 page->mapping != NULL ||
225 page_count(page) != 0 ||
226 (page->flags & (
227 1 << PG_lru |
228 1 << PG_private |
229 1 << PG_locked |
230 1 << PG_active |
231 1 << PG_reclaim |
232 1 << PG_slab |
233 1 << PG_swapcache |
234 1 << PG_writeback )))
235 bad_page(function, page);
236 if (PageDirty(page))
237 ClearPageDirty(page);
241 * Frees a list of pages.
242 * Assumes all pages on list are in same zone, and of same order.
243 * count is the number of pages to free, or 0 for all on the list.
245 * If the zone was previously in an "all pages pinned" state then look to
246 * see if this freeing clears that state.
248 * And clear the zone's pages_scanned counter, to hold off the "all pages are
249 * pinned" detection logic.
251 static int
252 free_pages_bulk(struct zone *zone, int count,
253 struct list_head *list, unsigned int order)
255 unsigned long flags;
256 struct free_area *area;
257 struct page *base, *page = NULL;
258 int ret = 0;
260 base = zone->zone_mem_map;
261 area = zone->free_area + order;
262 spin_lock_irqsave(&zone->lock, flags);
263 zone->all_unreclaimable = 0;
264 zone->pages_scanned = 0;
265 while (!list_empty(list) && count--) {
266 page = list_entry(list->prev, struct page, lru);
267 /* have to delete it as __free_pages_bulk list manipulates */
268 list_del(&page->lru);
269 __free_pages_bulk(page, base, zone, area, order);
270 ret++;
272 spin_unlock_irqrestore(&zone->lock, flags);
273 return ret;
276 void __free_pages_ok(struct page *page, unsigned int order)
278 LIST_HEAD(list);
279 int i;
281 arch_free_page(page, order);
283 mod_page_state(pgfree, 1 << order);
284 for (i = 0 ; i < (1 << order) ; ++i)
285 free_pages_check(__FUNCTION__, page + i);
286 list_add(&page->lru, &list);
287 kernel_map_pages(page, 1<<order, 0);
288 free_pages_bulk(page_zone(page), 1, &list, order);
291 #define MARK_USED(index, order, area) \
292 __change_bit((index) >> (1+(order)), (area)->map)
295 * The order of subdivision here is critical for the IO subsystem.
296 * Please do not alter this order without good reasons and regression
297 * testing. Specifically, as large blocks of memory are subdivided,
298 * the order in which smaller blocks are delivered depends on the order
299 * they're subdivided in this function. This is the primary factor
300 * influencing the order in which pages are delivered to the IO
301 * subsystem according to empirical testing, and this is also justified
302 * by considering the behavior of a buddy system containing a single
303 * large block of memory acted on by a series of small allocations.
304 * This behavior is a critical factor in sglist merging's success.
306 * -- wli
308 static inline struct page *
309 expand(struct zone *zone, struct page *page,
310 unsigned long index, int low, int high, struct free_area *area)
312 unsigned long size = 1 << high;
314 while (high > low) {
315 area--;
316 high--;
317 size >>= 1;
318 BUG_ON(bad_range(zone, &page[size]));
319 list_add(&page[size].lru, &area->free_list);
320 MARK_USED(index + size, high, area);
322 return page;
325 static inline void set_page_refs(struct page *page, int order)
327 #ifdef CONFIG_MMU
328 set_page_count(page, 1);
329 #else
330 int i;
333 * We need to reference all the pages for this order, otherwise if
334 * anyone accesses one of the pages with (get/put) it will be freed.
336 for (i = 0; i < (1 << order); i++)
337 set_page_count(page+i, 1);
338 #endif /* CONFIG_MMU */
342 * This page is about to be returned from the page allocator
344 static void prep_new_page(struct page *page, int order)
346 if (page->mapping || page_mapped(page) ||
347 (page->flags & (
348 1 << PG_private |
349 1 << PG_locked |
350 1 << PG_lru |
351 1 << PG_active |
352 1 << PG_dirty |
353 1 << PG_reclaim |
354 1 << PG_swapcache |
355 1 << PG_writeback )))
356 bad_page(__FUNCTION__, page);
358 page->flags &= ~(1 << PG_uptodate | 1 << PG_error |
359 1 << PG_referenced | 1 << PG_arch_1 |
360 1 << PG_checked | 1 << PG_mappedtodisk);
361 page->private = 0;
362 set_page_refs(page, order);
366 * Do the hard work of removing an element from the buddy allocator.
367 * Call me with the zone->lock already held.
369 static struct page *__rmqueue(struct zone *zone, unsigned int order)
371 struct free_area * area;
372 unsigned int current_order;
373 struct page *page;
374 unsigned int index;
376 for (current_order = order; current_order < MAX_ORDER; ++current_order) {
377 area = zone->free_area + current_order;
378 if (list_empty(&area->free_list))
379 continue;
381 page = list_entry(area->free_list.next, struct page, lru);
382 list_del(&page->lru);
383 index = page - zone->zone_mem_map;
384 if (current_order != MAX_ORDER-1)
385 MARK_USED(index, current_order, area);
386 zone->free_pages -= 1UL << order;
387 return expand(zone, page, index, order, current_order, area);
390 return NULL;
394 * Obtain a specified number of elements from the buddy allocator, all under
395 * a single hold of the lock, for efficiency. Add them to the supplied list.
396 * Returns the number of new pages which were placed at *list.
398 static int rmqueue_bulk(struct zone *zone, unsigned int order,
399 unsigned long count, struct list_head *list)
401 unsigned long flags;
402 int i;
403 int allocated = 0;
404 struct page *page;
406 spin_lock_irqsave(&zone->lock, flags);
407 for (i = 0; i < count; ++i) {
408 page = __rmqueue(zone, order);
409 if (page == NULL)
410 break;
411 allocated++;
412 list_add_tail(&page->lru, list);
414 spin_unlock_irqrestore(&zone->lock, flags);
415 return allocated;
418 #if defined(CONFIG_PM) || defined(CONFIG_HOTPLUG_CPU)
419 static void __drain_pages(unsigned int cpu)
421 struct zone *zone;
422 int i;
424 for_each_zone(zone) {
425 struct per_cpu_pageset *pset;
427 pset = &zone->pageset[cpu];
428 for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
429 struct per_cpu_pages *pcp;
431 pcp = &pset->pcp[i];
432 pcp->count -= free_pages_bulk(zone, pcp->count,
433 &pcp->list, 0);
437 #endif /* CONFIG_PM || CONFIG_HOTPLUG_CPU */
439 #ifdef CONFIG_PM
440 int is_head_of_free_region(struct page *page)
442 struct zone *zone = page_zone(page);
443 unsigned long flags;
444 int order;
445 struct list_head *curr;
448 * Should not matter as we need quiescent system for
449 * suspend anyway, but...
451 spin_lock_irqsave(&zone->lock, flags);
452 for (order = MAX_ORDER - 1; order >= 0; --order)
453 list_for_each(curr, &zone->free_area[order].free_list)
454 if (page == list_entry(curr, struct page, lru)) {
455 spin_unlock_irqrestore(&zone->lock, flags);
456 return 1 << order;
458 spin_unlock_irqrestore(&zone->lock, flags);
459 return 0;
463 * Spill all of this CPU's per-cpu pages back into the buddy allocator.
465 void drain_local_pages(void)
467 unsigned long flags;
469 local_irq_save(flags);
470 __drain_pages(smp_processor_id());
471 local_irq_restore(flags);
473 #endif /* CONFIG_PM */
475 static void zone_statistics(struct zonelist *zonelist, struct zone *z)
477 #ifdef CONFIG_NUMA
478 unsigned long flags;
479 int cpu;
480 pg_data_t *pg = z->zone_pgdat;
481 pg_data_t *orig = zonelist->zones[0]->zone_pgdat;
482 struct per_cpu_pageset *p;
484 local_irq_save(flags);
485 cpu = smp_processor_id();
486 p = &z->pageset[cpu];
487 if (pg == orig) {
488 z->pageset[cpu].numa_hit++;
489 } else {
490 p->numa_miss++;
491 zonelist->zones[0]->pageset[cpu].numa_foreign++;
493 if (pg == NODE_DATA(numa_node_id()))
494 p->local_node++;
495 else
496 p->other_node++;
497 local_irq_restore(flags);
498 #endif
502 * Free a 0-order page
504 static void FASTCALL(free_hot_cold_page(struct page *page, int cold));
505 static void fastcall free_hot_cold_page(struct page *page, int cold)
507 struct zone *zone = page_zone(page);
508 struct per_cpu_pages *pcp;
509 unsigned long flags;
511 arch_free_page(page, 0);
513 kernel_map_pages(page, 1, 0);
514 inc_page_state(pgfree);
515 if (PageAnon(page))
516 page->mapping = NULL;
517 free_pages_check(__FUNCTION__, page);
518 pcp = &zone->pageset[get_cpu()].pcp[cold];
519 local_irq_save(flags);
520 if (pcp->count >= pcp->high)
521 pcp->count -= free_pages_bulk(zone, pcp->batch, &pcp->list, 0);
522 list_add(&page->lru, &pcp->list);
523 pcp->count++;
524 local_irq_restore(flags);
525 put_cpu();
528 void fastcall free_hot_page(struct page *page)
530 free_hot_cold_page(page, 0);
533 void fastcall free_cold_page(struct page *page)
535 free_hot_cold_page(page, 1);
539 * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
540 * we cheat by calling it from here, in the order > 0 path. Saves a branch
541 * or two.
544 static struct page *
545 buffered_rmqueue(struct zone *zone, int order, int gfp_flags)
547 unsigned long flags;
548 struct page *page = NULL;
549 int cold = !!(gfp_flags & __GFP_COLD);
551 if (order == 0) {
552 struct per_cpu_pages *pcp;
554 pcp = &zone->pageset[get_cpu()].pcp[cold];
555 local_irq_save(flags);
556 if (pcp->count <= pcp->low)
557 pcp->count += rmqueue_bulk(zone, 0,
558 pcp->batch, &pcp->list);
559 if (pcp->count) {
560 page = list_entry(pcp->list.next, struct page, lru);
561 list_del(&page->lru);
562 pcp->count--;
564 local_irq_restore(flags);
565 put_cpu();
568 if (page == NULL) {
569 spin_lock_irqsave(&zone->lock, flags);
570 page = __rmqueue(zone, order);
571 spin_unlock_irqrestore(&zone->lock, flags);
574 if (page != NULL) {
575 BUG_ON(bad_range(zone, page));
576 mod_page_state_zone(zone, pgalloc, 1 << order);
577 prep_new_page(page, order);
578 if (order && (gfp_flags & __GFP_COMP))
579 prep_compound_page(page, order);
581 return page;
585 * This is the 'heart' of the zoned buddy allocator.
587 * Herein lies the mysterious "incremental min". That's the
589 * local_low = z->pages_low;
590 * min += local_low;
592 * thing. The intent here is to provide additional protection to low zones for
593 * allocation requests which _could_ use higher zones. So a GFP_HIGHMEM
594 * request is not allowed to dip as deeply into the normal zone as a GFP_KERNEL
595 * request. This preserves additional space in those lower zones for requests
596 * which really do need memory from those zones. It means that on a decent
597 * sized machine, GFP_HIGHMEM and GFP_KERNEL requests basically leave the DMA
598 * zone untouched.
600 struct page * fastcall
601 __alloc_pages(unsigned int gfp_mask, unsigned int order,
602 struct zonelist *zonelist)
604 const int wait = gfp_mask & __GFP_WAIT;
605 unsigned long min;
606 struct zone **zones, *z;
607 struct page *page;
608 struct reclaim_state reclaim_state;
609 struct task_struct *p = current;
610 int i;
611 int alloc_type;
612 int do_retry;
613 int can_try_harder;
615 might_sleep_if(wait);
618 * The caller may dip into page reserves a bit more if the caller
619 * cannot run direct reclaim, or is the caller has realtime scheduling
620 * policy
622 can_try_harder = (unlikely(rt_task(p)) && !in_interrupt()) || !wait;
624 zones = zonelist->zones; /* the list of zones suitable for gfp_mask */
626 if (unlikely(zones[0] == NULL)) {
627 /* Should this ever happen?? */
628 return NULL;
631 alloc_type = zone_idx(zones[0]);
633 /* Go through the zonelist once, looking for a zone with enough free */
634 for (i = 0; (z = zones[i]) != NULL; i++) {
635 min = z->pages_low + (1<<order) + z->protection[alloc_type];
637 if (z->free_pages < min)
638 continue;
640 page = buffered_rmqueue(z, order, gfp_mask);
641 if (page)
642 goto got_pg;
645 for (i = 0; (z = zones[i]) != NULL; i++)
646 wakeup_kswapd(z);
649 * Go through the zonelist again. Let __GFP_HIGH and allocations
650 * coming from realtime tasks to go deeper into reserves
652 for (i = 0; (z = zones[i]) != NULL; i++) {
653 min = z->pages_min;
654 if (gfp_mask & __GFP_HIGH)
655 min /= 2;
656 if (can_try_harder)
657 min -= min / 4;
658 min += (1<<order) + z->protection[alloc_type];
660 if (z->free_pages < min)
661 continue;
663 page = buffered_rmqueue(z, order, gfp_mask);
664 if (page)
665 goto got_pg;
668 /* This allocation should allow future memory freeing. */
669 if ((p->flags & (PF_MEMALLOC | PF_MEMDIE)) && !in_interrupt()) {
670 /* go through the zonelist yet again, ignoring mins */
671 for (i = 0; (z = zones[i]) != NULL; i++) {
672 page = buffered_rmqueue(z, order, gfp_mask);
673 if (page)
674 goto got_pg;
676 goto nopage;
679 /* Atomic allocations - we can't balance anything */
680 if (!wait)
681 goto nopage;
683 rebalance:
684 /* We now go into synchronous reclaim */
685 p->flags |= PF_MEMALLOC;
686 reclaim_state.reclaimed_slab = 0;
687 p->reclaim_state = &reclaim_state;
689 try_to_free_pages(zones, gfp_mask, order);
691 p->reclaim_state = NULL;
692 p->flags &= ~PF_MEMALLOC;
694 /* go through the zonelist yet one more time */
695 for (i = 0; (z = zones[i]) != NULL; i++) {
696 min = z->pages_min;
697 if (gfp_mask & __GFP_HIGH)
698 min /= 2;
699 if (can_try_harder)
700 min -= min / 4;
701 min += (1<<order) + z->protection[alloc_type];
703 if (z->free_pages < min)
704 continue;
706 page = buffered_rmqueue(z, order, gfp_mask);
707 if (page)
708 goto got_pg;
712 * Don't let big-order allocations loop unless the caller explicitly
713 * requests that. Wait for some write requests to complete then retry.
715 * In this implementation, __GFP_REPEAT means __GFP_NOFAIL for order
716 * <= 3, but that may not be true in other implementations.
718 do_retry = 0;
719 if (!(gfp_mask & __GFP_NORETRY)) {
720 if ((order <= 3) || (gfp_mask & __GFP_REPEAT))
721 do_retry = 1;
722 if (gfp_mask & __GFP_NOFAIL)
723 do_retry = 1;
725 if (do_retry) {
726 blk_congestion_wait(WRITE, HZ/50);
727 goto rebalance;
730 nopage:
731 if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) {
732 printk(KERN_WARNING "%s: page allocation failure."
733 " order:%d, mode:0x%x\n",
734 p->comm, order, gfp_mask);
735 dump_stack();
737 return NULL;
738 got_pg:
739 zone_statistics(zonelist, z);
740 kernel_map_pages(page, 1 << order, 1);
741 return page;
744 EXPORT_SYMBOL(__alloc_pages);
747 * Common helper functions.
749 fastcall unsigned long __get_free_pages(unsigned int gfp_mask, unsigned int order)
751 struct page * page;
752 page = alloc_pages(gfp_mask, order);
753 if (!page)
754 return 0;
755 return (unsigned long) page_address(page);
758 EXPORT_SYMBOL(__get_free_pages);
760 fastcall unsigned long get_zeroed_page(unsigned int gfp_mask)
762 struct page * page;
765 * get_zeroed_page() returns a 32-bit address, which cannot represent
766 * a highmem page
768 BUG_ON(gfp_mask & __GFP_HIGHMEM);
770 page = alloc_pages(gfp_mask, 0);
771 if (page) {
772 void *address = page_address(page);
773 clear_page(address);
774 return (unsigned long) address;
776 return 0;
779 EXPORT_SYMBOL(get_zeroed_page);
781 void __pagevec_free(struct pagevec *pvec)
783 int i = pagevec_count(pvec);
785 while (--i >= 0)
786 free_hot_cold_page(pvec->pages[i], pvec->cold);
789 fastcall void __free_pages(struct page *page, unsigned int order)
791 if (!PageReserved(page) && put_page_testzero(page)) {
792 if (order == 0)
793 free_hot_page(page);
794 else
795 __free_pages_ok(page, order);
799 EXPORT_SYMBOL(__free_pages);
801 fastcall void free_pages(unsigned long addr, unsigned int order)
803 if (addr != 0) {
804 BUG_ON(!virt_addr_valid((void *)addr));
805 __free_pages(virt_to_page((void *)addr), order);
809 EXPORT_SYMBOL(free_pages);
812 * Total amount of free (allocatable) RAM:
814 unsigned int nr_free_pages(void)
816 unsigned int sum = 0;
817 struct zone *zone;
819 for_each_zone(zone)
820 sum += zone->free_pages;
822 return sum;
825 EXPORT_SYMBOL(nr_free_pages);
827 #ifdef CONFIG_NUMA
828 unsigned int nr_free_pages_pgdat(pg_data_t *pgdat)
830 unsigned int i, sum = 0;
832 for (i = 0; i < MAX_NR_ZONES; i++)
833 sum += pgdat->node_zones[i].free_pages;
835 return sum;
837 #endif
839 static unsigned int nr_free_zone_pages(int offset)
841 pg_data_t *pgdat;
842 unsigned int sum = 0;
844 for_each_pgdat(pgdat) {
845 struct zonelist *zonelist = pgdat->node_zonelists + offset;
846 struct zone **zonep = zonelist->zones;
847 struct zone *zone;
849 for (zone = *zonep++; zone; zone = *zonep++) {
850 unsigned long size = zone->present_pages;
851 unsigned long high = zone->pages_high;
852 if (size > high)
853 sum += size - high;
857 return sum;
861 * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
863 unsigned int nr_free_buffer_pages(void)
865 return nr_free_zone_pages(GFP_USER & GFP_ZONEMASK);
869 * Amount of free RAM allocatable within all zones
871 unsigned int nr_free_pagecache_pages(void)
873 return nr_free_zone_pages(GFP_HIGHUSER & GFP_ZONEMASK);
876 #ifdef CONFIG_HIGHMEM
877 unsigned int nr_free_highpages (void)
879 pg_data_t *pgdat;
880 unsigned int pages = 0;
882 for_each_pgdat(pgdat)
883 pages += pgdat->node_zones[ZONE_HIGHMEM].free_pages;
885 return pages;
887 #endif
889 #ifdef CONFIG_NUMA
890 static void show_node(struct zone *zone)
892 printk("Node %d ", zone->zone_pgdat->node_id);
894 #else
895 #define show_node(zone) do { } while (0)
896 #endif
899 * Accumulate the page_state information across all CPUs.
900 * The result is unavoidably approximate - it can change
901 * during and after execution of this function.
903 DEFINE_PER_CPU(struct page_state, page_states) = {0};
904 EXPORT_PER_CPU_SYMBOL(page_states);
906 atomic_t nr_pagecache = ATOMIC_INIT(0);
907 EXPORT_SYMBOL(nr_pagecache);
908 #ifdef CONFIG_SMP
909 DEFINE_PER_CPU(long, nr_pagecache_local) = 0;
910 #endif
912 void __get_page_state(struct page_state *ret, int nr)
914 int cpu = 0;
916 memset(ret, 0, sizeof(*ret));
917 while (cpu < NR_CPUS) {
918 unsigned long *in, *out, off;
920 if (!cpu_possible(cpu)) {
921 cpu++;
922 continue;
925 in = (unsigned long *)&per_cpu(page_states, cpu);
926 cpu++;
927 if (cpu < NR_CPUS && cpu_possible(cpu))
928 prefetch(&per_cpu(page_states, cpu));
929 out = (unsigned long *)ret;
930 for (off = 0; off < nr; off++)
931 *out++ += *in++;
935 void get_page_state(struct page_state *ret)
937 int nr;
939 nr = offsetof(struct page_state, GET_PAGE_STATE_LAST);
940 nr /= sizeof(unsigned long);
942 __get_page_state(ret, nr + 1);
945 void get_full_page_state(struct page_state *ret)
947 __get_page_state(ret, sizeof(*ret) / sizeof(unsigned long));
950 unsigned long __read_page_state(unsigned offset)
952 unsigned long ret = 0;
953 int cpu;
955 for (cpu = 0; cpu < NR_CPUS; cpu++) {
956 unsigned long in;
958 if (!cpu_possible(cpu))
959 continue;
961 in = (unsigned long)&per_cpu(page_states, cpu) + offset;
962 ret += *((unsigned long *)in);
964 return ret;
967 void __get_zone_counts(unsigned long *active, unsigned long *inactive,
968 unsigned long *free, struct pglist_data *pgdat)
970 struct zone *zones = pgdat->node_zones;
971 int i;
973 *active = 0;
974 *inactive = 0;
975 *free = 0;
976 for (i = 0; i < MAX_NR_ZONES; i++) {
977 *active += zones[i].nr_active;
978 *inactive += zones[i].nr_inactive;
979 *free += zones[i].free_pages;
983 void get_zone_counts(unsigned long *active,
984 unsigned long *inactive, unsigned long *free)
986 struct pglist_data *pgdat;
988 *active = 0;
989 *inactive = 0;
990 *free = 0;
991 for_each_pgdat(pgdat) {
992 unsigned long l, m, n;
993 __get_zone_counts(&l, &m, &n, pgdat);
994 *active += l;
995 *inactive += m;
996 *free += n;
1000 void si_meminfo(struct sysinfo *val)
1002 val->totalram = totalram_pages;
1003 val->sharedram = 0;
1004 val->freeram = nr_free_pages();
1005 val->bufferram = nr_blockdev_pages();
1006 #ifdef CONFIG_HIGHMEM
1007 val->totalhigh = totalhigh_pages;
1008 val->freehigh = nr_free_highpages();
1009 #else
1010 val->totalhigh = 0;
1011 val->freehigh = 0;
1012 #endif
1013 val->mem_unit = PAGE_SIZE;
1016 EXPORT_SYMBOL(si_meminfo);
1018 #ifdef CONFIG_NUMA
1019 void si_meminfo_node(struct sysinfo *val, int nid)
1021 pg_data_t *pgdat = NODE_DATA(nid);
1023 val->totalram = pgdat->node_present_pages;
1024 val->freeram = nr_free_pages_pgdat(pgdat);
1025 val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].present_pages;
1026 val->freehigh = pgdat->node_zones[ZONE_HIGHMEM].free_pages;
1027 val->mem_unit = PAGE_SIZE;
1029 #endif
1031 #define K(x) ((x) << (PAGE_SHIFT-10))
1034 * Show free area list (used inside shift_scroll-lock stuff)
1035 * We also calculate the percentage fragmentation. We do this by counting the
1036 * memory on each free list with the exception of the first item on the list.
1038 void show_free_areas(void)
1040 struct page_state ps;
1041 int cpu, temperature;
1042 unsigned long active;
1043 unsigned long inactive;
1044 unsigned long free;
1045 struct zone *zone;
1047 for_each_zone(zone) {
1048 show_node(zone);
1049 printk("%s per-cpu:", zone->name);
1051 if (!zone->present_pages) {
1052 printk(" empty\n");
1053 continue;
1054 } else
1055 printk("\n");
1057 for (cpu = 0; cpu < NR_CPUS; ++cpu) {
1058 struct per_cpu_pageset *pageset;
1060 if (!cpu_possible(cpu))
1061 continue;
1063 pageset = zone->pageset + cpu;
1065 for (temperature = 0; temperature < 2; temperature++)
1066 printk("cpu %d %s: low %d, high %d, batch %d\n",
1067 cpu,
1068 temperature ? "cold" : "hot",
1069 pageset->pcp[temperature].low,
1070 pageset->pcp[temperature].high,
1071 pageset->pcp[temperature].batch);
1075 get_page_state(&ps);
1076 get_zone_counts(&active, &inactive, &free);
1078 printk("\nFree pages: %11ukB (%ukB HighMem)\n",
1079 K(nr_free_pages()),
1080 K(nr_free_highpages()));
1082 printk("Active:%lu inactive:%lu dirty:%lu writeback:%lu "
1083 "unstable:%lu free:%u slab:%lu mapped:%lu pagetables:%lu\n",
1084 active,
1085 inactive,
1086 ps.nr_dirty,
1087 ps.nr_writeback,
1088 ps.nr_unstable,
1089 nr_free_pages(),
1090 ps.nr_slab,
1091 ps.nr_mapped,
1092 ps.nr_page_table_pages);
1094 for_each_zone(zone) {
1095 int i;
1097 show_node(zone);
1098 printk("%s"
1099 " free:%lukB"
1100 " min:%lukB"
1101 " low:%lukB"
1102 " high:%lukB"
1103 " active:%lukB"
1104 " inactive:%lukB"
1105 " present:%lukB"
1106 "\n",
1107 zone->name,
1108 K(zone->free_pages),
1109 K(zone->pages_min),
1110 K(zone->pages_low),
1111 K(zone->pages_high),
1112 K(zone->nr_active),
1113 K(zone->nr_inactive),
1114 K(zone->present_pages)
1116 printk("protections[]:");
1117 for (i = 0; i < MAX_NR_ZONES; i++)
1118 printk(" %lu", zone->protection[i]);
1119 printk("\n");
1122 for_each_zone(zone) {
1123 struct list_head *elem;
1124 unsigned long nr, flags, order, total = 0;
1126 show_node(zone);
1127 printk("%s: ", zone->name);
1128 if (!zone->present_pages) {
1129 printk("empty\n");
1130 continue;
1133 spin_lock_irqsave(&zone->lock, flags);
1134 for (order = 0; order < MAX_ORDER; order++) {
1135 nr = 0;
1136 list_for_each(elem, &zone->free_area[order].free_list)
1137 ++nr;
1138 total += nr << order;
1139 printk("%lu*%lukB ", nr, K(1UL) << order);
1141 spin_unlock_irqrestore(&zone->lock, flags);
1142 printk("= %lukB\n", K(total));
1145 show_swap_cache_info();
1149 * Builds allocation fallback zone lists.
1151 static int __init build_zonelists_node(pg_data_t *pgdat, struct zonelist *zonelist, int j, int k)
1153 switch (k) {
1154 struct zone *zone;
1155 default:
1156 BUG();
1157 case ZONE_HIGHMEM:
1158 zone = pgdat->node_zones + ZONE_HIGHMEM;
1159 if (zone->present_pages) {
1160 #ifndef CONFIG_HIGHMEM
1161 BUG();
1162 #endif
1163 zonelist->zones[j++] = zone;
1165 case ZONE_NORMAL:
1166 zone = pgdat->node_zones + ZONE_NORMAL;
1167 if (zone->present_pages)
1168 zonelist->zones[j++] = zone;
1169 case ZONE_DMA:
1170 zone = pgdat->node_zones + ZONE_DMA;
1171 if (zone->present_pages)
1172 zonelist->zones[j++] = zone;
1175 return j;
1178 #ifdef CONFIG_NUMA
1179 #define MAX_NODE_LOAD (numnodes)
1180 static int __initdata node_load[MAX_NUMNODES];
1182 * find_next_best_node - find the next node that should appear in a given
1183 * node's fallback list
1184 * @node: node whose fallback list we're appending
1185 * @used_node_mask: pointer to the bitmap of already used nodes
1187 * We use a number of factors to determine which is the next node that should
1188 * appear on a given node's fallback list. The node should not have appeared
1189 * already in @node's fallback list, and it should be the next closest node
1190 * according to the distance array (which contains arbitrary distance values
1191 * from each node to each node in the system), and should also prefer nodes
1192 * with no CPUs, since presumably they'll have very little allocation pressure
1193 * on them otherwise.
1194 * It returns -1 if no node is found.
1196 static int __init find_next_best_node(int node, void *used_node_mask)
1198 int i, n, val;
1199 int min_val = INT_MAX;
1200 int best_node = -1;
1202 for (i = 0; i < numnodes; i++) {
1203 cpumask_t tmp;
1205 /* Start from local node */
1206 n = (node+i)%numnodes;
1208 /* Don't want a node to appear more than once */
1209 if (test_bit(n, used_node_mask))
1210 continue;
1212 /* Use the distance array to find the distance */
1213 val = node_distance(node, n);
1215 /* Give preference to headless and unused nodes */
1216 tmp = node_to_cpumask(n);
1217 if (!cpus_empty(tmp))
1218 val += PENALTY_FOR_NODE_WITH_CPUS;
1220 /* Slight preference for less loaded node */
1221 val *= (MAX_NODE_LOAD*MAX_NUMNODES);
1222 val += node_load[n];
1224 if (val < min_val) {
1225 min_val = val;
1226 best_node = n;
1230 if (best_node >= 0)
1231 set_bit(best_node, used_node_mask);
1233 return best_node;
1236 static void __init build_zonelists(pg_data_t *pgdat)
1238 int i, j, k, node, local_node;
1239 int prev_node, load;
1240 struct zonelist *zonelist;
1241 DECLARE_BITMAP(used_mask, MAX_NUMNODES);
1243 /* initialize zonelists */
1244 for (i = 0; i < GFP_ZONETYPES; i++) {
1245 zonelist = pgdat->node_zonelists + i;
1246 memset(zonelist, 0, sizeof(*zonelist));
1247 zonelist->zones[0] = NULL;
1250 /* NUMA-aware ordering of nodes */
1251 local_node = pgdat->node_id;
1252 load = numnodes;
1253 prev_node = local_node;
1254 bitmap_zero(used_mask, MAX_NUMNODES);
1255 while ((node = find_next_best_node(local_node, used_mask)) >= 0) {
1257 * We don't want to pressure a particular node.
1258 * So adding penalty to the first node in same
1259 * distance group to make it round-robin.
1261 if (node_distance(local_node, node) !=
1262 node_distance(local_node, prev_node))
1263 node_load[node] += load;
1264 prev_node = node;
1265 load--;
1266 for (i = 0; i < GFP_ZONETYPES; i++) {
1267 zonelist = pgdat->node_zonelists + i;
1268 for (j = 0; zonelist->zones[j] != NULL; j++);
1270 k = ZONE_NORMAL;
1271 if (i & __GFP_HIGHMEM)
1272 k = ZONE_HIGHMEM;
1273 if (i & __GFP_DMA)
1274 k = ZONE_DMA;
1276 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1277 zonelist->zones[j] = NULL;
1282 #else /* CONFIG_NUMA */
1284 static void __init build_zonelists(pg_data_t *pgdat)
1286 int i, j, k, node, local_node;
1288 local_node = pgdat->node_id;
1289 for (i = 0; i < GFP_ZONETYPES; i++) {
1290 struct zonelist *zonelist;
1292 zonelist = pgdat->node_zonelists + i;
1293 memset(zonelist, 0, sizeof(*zonelist));
1295 j = 0;
1296 k = ZONE_NORMAL;
1297 if (i & __GFP_HIGHMEM)
1298 k = ZONE_HIGHMEM;
1299 if (i & __GFP_DMA)
1300 k = ZONE_DMA;
1302 j = build_zonelists_node(pgdat, zonelist, j, k);
1304 * Now we build the zonelist so that it contains the zones
1305 * of all the other nodes.
1306 * We don't want to pressure a particular node, so when
1307 * building the zones for node N, we make sure that the
1308 * zones coming right after the local ones are those from
1309 * node N+1 (modulo N)
1311 for (node = local_node + 1; node < numnodes; node++)
1312 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1313 for (node = 0; node < local_node; node++)
1314 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1316 zonelist->zones[j] = NULL;
1320 #endif /* CONFIG_NUMA */
1322 void __init build_all_zonelists(void)
1324 int i;
1326 for(i = 0 ; i < numnodes ; i++)
1327 build_zonelists(NODE_DATA(i));
1328 printk("Built %i zonelists\n", numnodes);
1332 * Helper functions to size the waitqueue hash table.
1333 * Essentially these want to choose hash table sizes sufficiently
1334 * large so that collisions trying to wait on pages are rare.
1335 * But in fact, the number of active page waitqueues on typical
1336 * systems is ridiculously low, less than 200. So this is even
1337 * conservative, even though it seems large.
1339 * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
1340 * waitqueues, i.e. the size of the waitq table given the number of pages.
1342 #define PAGES_PER_WAITQUEUE 256
1344 static inline unsigned long wait_table_size(unsigned long pages)
1346 unsigned long size = 1;
1348 pages /= PAGES_PER_WAITQUEUE;
1350 while (size < pages)
1351 size <<= 1;
1354 * Once we have dozens or even hundreds of threads sleeping
1355 * on IO we've got bigger problems than wait queue collision.
1356 * Limit the size of the wait table to a reasonable size.
1358 size = min(size, 4096UL);
1360 return max(size, 4UL);
1364 * This is an integer logarithm so that shifts can be used later
1365 * to extract the more random high bits from the multiplicative
1366 * hash function before the remainder is taken.
1368 static inline unsigned long wait_table_bits(unsigned long size)
1370 return ffz(~size);
1373 #define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
1375 static void __init calculate_zone_totalpages(struct pglist_data *pgdat,
1376 unsigned long *zones_size, unsigned long *zholes_size)
1378 unsigned long realtotalpages, totalpages = 0;
1379 int i;
1381 for (i = 0; i < MAX_NR_ZONES; i++)
1382 totalpages += zones_size[i];
1383 pgdat->node_spanned_pages = totalpages;
1385 realtotalpages = totalpages;
1386 if (zholes_size)
1387 for (i = 0; i < MAX_NR_ZONES; i++)
1388 realtotalpages -= zholes_size[i];
1389 pgdat->node_present_pages = realtotalpages;
1390 printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id, realtotalpages);
1395 * Initially all pages are reserved - free ones are freed
1396 * up by free_all_bootmem() once the early boot process is
1397 * done. Non-atomic initialization, single-pass.
1399 void __init memmap_init_zone(unsigned long size, int nid, unsigned long zone,
1400 unsigned long start_pfn)
1402 struct page *start = pfn_to_page(start_pfn);
1403 struct page *page;
1405 for (page = start; page < (start + size); page++) {
1406 set_page_zone(page, NODEZONE(nid, zone));
1407 set_page_count(page, 0);
1408 reset_page_mapcount(page);
1409 SetPageReserved(page);
1410 INIT_LIST_HEAD(&page->lru);
1411 #ifdef WANT_PAGE_VIRTUAL
1412 /* The shift won't overflow because ZONE_NORMAL is below 4G. */
1413 if (!is_highmem_idx(zone))
1414 set_page_address(page, __va(start_pfn << PAGE_SHIFT));
1415 #endif
1416 start_pfn++;
1421 * Page buddy system uses "index >> (i+1)", where "index" is
1422 * at most "size-1".
1424 * The extra "+3" is to round down to byte size (8 bits per byte
1425 * assumption). Thus we get "(size-1) >> (i+4)" as the last byte
1426 * we can access.
1428 * The "+1" is because we want to round the byte allocation up
1429 * rather than down. So we should have had a "+7" before we shifted
1430 * down by three. Also, we have to add one as we actually _use_ the
1431 * last bit (it's [0,n] inclusive, not [0,n[).
1433 * So we actually had +7+1 before we shift down by 3. But
1434 * (n+8) >> 3 == (n >> 3) + 1 (modulo overflows, which we do not have).
1436 * Finally, we LONG_ALIGN because all bitmap operations are on longs.
1438 unsigned long pages_to_bitmap_size(unsigned long order, unsigned long nr_pages)
1440 unsigned long bitmap_size;
1442 bitmap_size = (nr_pages-1) >> (order+4);
1443 bitmap_size = LONG_ALIGN(bitmap_size+1);
1445 return bitmap_size;
1448 void zone_init_free_lists(struct pglist_data *pgdat, struct zone *zone, unsigned long size)
1450 int order;
1451 for (order = 0; ; order++) {
1452 unsigned long bitmap_size;
1454 INIT_LIST_HEAD(&zone->free_area[order].free_list);
1455 if (order == MAX_ORDER-1) {
1456 zone->free_area[order].map = NULL;
1457 break;
1460 bitmap_size = pages_to_bitmap_size(order, size);
1461 zone->free_area[order].map =
1462 (unsigned long *) alloc_bootmem_node(pgdat, bitmap_size);
1466 #ifndef __HAVE_ARCH_MEMMAP_INIT
1467 #define memmap_init(size, nid, zone, start_pfn) \
1468 memmap_init_zone((size), (nid), (zone), (start_pfn))
1469 #endif
1472 * Set up the zone data structures:
1473 * - mark all pages reserved
1474 * - mark all memory queues empty
1475 * - clear the memory bitmaps
1477 static void __init free_area_init_core(struct pglist_data *pgdat,
1478 unsigned long *zones_size, unsigned long *zholes_size)
1480 unsigned long i, j;
1481 const unsigned long zone_required_alignment = 1UL << (MAX_ORDER-1);
1482 int cpu, nid = pgdat->node_id;
1483 unsigned long zone_start_pfn = pgdat->node_start_pfn;
1485 pgdat->nr_zones = 0;
1486 init_waitqueue_head(&pgdat->kswapd_wait);
1488 for (j = 0; j < MAX_NR_ZONES; j++) {
1489 struct zone *zone = pgdat->node_zones + j;
1490 unsigned long size, realsize;
1491 unsigned long batch;
1493 zone_table[NODEZONE(nid, j)] = zone;
1494 realsize = size = zones_size[j];
1495 if (zholes_size)
1496 realsize -= zholes_size[j];
1498 if (j == ZONE_DMA || j == ZONE_NORMAL)
1499 nr_kernel_pages += realsize;
1500 nr_all_pages += realsize;
1502 zone->spanned_pages = size;
1503 zone->present_pages = realsize;
1504 zone->name = zone_names[j];
1505 spin_lock_init(&zone->lock);
1506 spin_lock_init(&zone->lru_lock);
1507 zone->zone_pgdat = pgdat;
1508 zone->free_pages = 0;
1510 zone->temp_priority = zone->prev_priority = DEF_PRIORITY;
1513 * The per-cpu-pages pools are set to around 1000th of the
1514 * size of the zone. But no more than 1/4 of a meg - there's
1515 * no point in going beyond the size of L2 cache.
1517 * OK, so we don't know how big the cache is. So guess.
1519 batch = zone->present_pages / 1024;
1520 if (batch * PAGE_SIZE > 256 * 1024)
1521 batch = (256 * 1024) / PAGE_SIZE;
1522 batch /= 4; /* We effectively *= 4 below */
1523 if (batch < 1)
1524 batch = 1;
1526 for (cpu = 0; cpu < NR_CPUS; cpu++) {
1527 struct per_cpu_pages *pcp;
1529 pcp = &zone->pageset[cpu].pcp[0]; /* hot */
1530 pcp->count = 0;
1531 pcp->low = 2 * batch;
1532 pcp->high = 6 * batch;
1533 pcp->batch = 1 * batch;
1534 INIT_LIST_HEAD(&pcp->list);
1536 pcp = &zone->pageset[cpu].pcp[1]; /* cold */
1537 pcp->count = 0;
1538 pcp->low = 0;
1539 pcp->high = 2 * batch;
1540 pcp->batch = 1 * batch;
1541 INIT_LIST_HEAD(&pcp->list);
1543 printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%lu\n",
1544 zone_names[j], realsize, batch);
1545 INIT_LIST_HEAD(&zone->active_list);
1546 INIT_LIST_HEAD(&zone->inactive_list);
1547 zone->nr_scan_active = 0;
1548 zone->nr_scan_inactive = 0;
1549 zone->nr_active = 0;
1550 zone->nr_inactive = 0;
1551 if (!size)
1552 continue;
1555 * The per-page waitqueue mechanism uses hashed waitqueues
1556 * per zone.
1558 zone->wait_table_size = wait_table_size(size);
1559 zone->wait_table_bits =
1560 wait_table_bits(zone->wait_table_size);
1561 zone->wait_table = (wait_queue_head_t *)
1562 alloc_bootmem_node(pgdat, zone->wait_table_size
1563 * sizeof(wait_queue_head_t));
1565 for(i = 0; i < zone->wait_table_size; ++i)
1566 init_waitqueue_head(zone->wait_table + i);
1568 pgdat->nr_zones = j+1;
1570 zone->zone_mem_map = pfn_to_page(zone_start_pfn);
1571 zone->zone_start_pfn = zone_start_pfn;
1573 if ((zone_start_pfn) & (zone_required_alignment-1))
1574 printk("BUG: wrong zone alignment, it will crash\n");
1576 memmap_init(size, nid, j, zone_start_pfn);
1578 zone_start_pfn += size;
1580 zone_init_free_lists(pgdat, zone, zone->spanned_pages);
1584 void __init node_alloc_mem_map(struct pglist_data *pgdat)
1586 unsigned long size;
1588 size = (pgdat->node_spanned_pages + 1) * sizeof(struct page);
1589 pgdat->node_mem_map = alloc_bootmem_node(pgdat, size);
1590 #ifndef CONFIG_DISCONTIGMEM
1591 mem_map = contig_page_data.node_mem_map;
1592 #endif
1595 void __init free_area_init_node(int nid, struct pglist_data *pgdat,
1596 unsigned long *zones_size, unsigned long node_start_pfn,
1597 unsigned long *zholes_size)
1599 pgdat->node_id = nid;
1600 pgdat->node_start_pfn = node_start_pfn;
1601 calculate_zone_totalpages(pgdat, zones_size, zholes_size);
1603 if (!pfn_to_page(node_start_pfn))
1604 node_alloc_mem_map(pgdat);
1606 free_area_init_core(pgdat, zones_size, zholes_size);
1609 #ifndef CONFIG_DISCONTIGMEM
1610 static bootmem_data_t contig_bootmem_data;
1611 struct pglist_data contig_page_data = { .bdata = &contig_bootmem_data };
1613 EXPORT_SYMBOL(contig_page_data);
1615 void __init free_area_init(unsigned long *zones_size)
1617 free_area_init_node(0, &contig_page_data, zones_size,
1618 __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
1620 #endif
1622 #ifdef CONFIG_PROC_FS
1624 #include <linux/seq_file.h>
1626 static void *frag_start(struct seq_file *m, loff_t *pos)
1628 pg_data_t *pgdat;
1629 loff_t node = *pos;
1631 for (pgdat = pgdat_list; pgdat && node; pgdat = pgdat->pgdat_next)
1632 --node;
1634 return pgdat;
1637 static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
1639 pg_data_t *pgdat = (pg_data_t *)arg;
1641 (*pos)++;
1642 return pgdat->pgdat_next;
1645 static void frag_stop(struct seq_file *m, void *arg)
1650 * This walks the freelist for each zone. Whilst this is slow, I'd rather
1651 * be slow here than slow down the fast path by keeping stats - mjbligh
1653 static int frag_show(struct seq_file *m, void *arg)
1655 pg_data_t *pgdat = (pg_data_t *)arg;
1656 struct zone *zone;
1657 struct zone *node_zones = pgdat->node_zones;
1658 unsigned long flags;
1659 int order;
1661 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
1662 if (!zone->present_pages)
1663 continue;
1665 spin_lock_irqsave(&zone->lock, flags);
1666 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
1667 for (order = 0; order < MAX_ORDER; ++order) {
1668 unsigned long nr_bufs = 0;
1669 struct list_head *elem;
1671 list_for_each(elem, &(zone->free_area[order].free_list))
1672 ++nr_bufs;
1673 seq_printf(m, "%6lu ", nr_bufs);
1675 spin_unlock_irqrestore(&zone->lock, flags);
1676 seq_putc(m, '\n');
1678 return 0;
1681 struct seq_operations fragmentation_op = {
1682 .start = frag_start,
1683 .next = frag_next,
1684 .stop = frag_stop,
1685 .show = frag_show,
1688 static char *vmstat_text[] = {
1689 "nr_dirty",
1690 "nr_writeback",
1691 "nr_unstable",
1692 "nr_page_table_pages",
1693 "nr_mapped",
1694 "nr_slab",
1696 "pgpgin",
1697 "pgpgout",
1698 "pswpin",
1699 "pswpout",
1700 "pgalloc_high",
1702 "pgalloc_normal",
1703 "pgalloc_dma",
1704 "pgfree",
1705 "pgactivate",
1706 "pgdeactivate",
1708 "pgfault",
1709 "pgmajfault",
1710 "pgrefill_high",
1711 "pgrefill_normal",
1712 "pgrefill_dma",
1714 "pgsteal_high",
1715 "pgsteal_normal",
1716 "pgsteal_dma",
1717 "pgscan_kswapd_high",
1718 "pgscan_kswapd_normal",
1720 "pgscan_kswapd_dma",
1721 "pgscan_direct_high",
1722 "pgscan_direct_normal",
1723 "pgscan_direct_dma",
1724 "pginodesteal",
1726 "slabs_scanned",
1727 "kswapd_steal",
1728 "kswapd_inodesteal",
1729 "pageoutrun",
1730 "allocstall",
1732 "pgrotated",
1735 static void *vmstat_start(struct seq_file *m, loff_t *pos)
1737 struct page_state *ps;
1739 if (*pos >= ARRAY_SIZE(vmstat_text))
1740 return NULL;
1742 ps = kmalloc(sizeof(*ps), GFP_KERNEL);
1743 m->private = ps;
1744 if (!ps)
1745 return ERR_PTR(-ENOMEM);
1746 get_full_page_state(ps);
1747 ps->pgpgin /= 2; /* sectors -> kbytes */
1748 ps->pgpgout /= 2;
1749 return (unsigned long *)ps + *pos;
1752 static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
1754 (*pos)++;
1755 if (*pos >= ARRAY_SIZE(vmstat_text))
1756 return NULL;
1757 return (unsigned long *)m->private + *pos;
1760 static int vmstat_show(struct seq_file *m, void *arg)
1762 unsigned long *l = arg;
1763 unsigned long off = l - (unsigned long *)m->private;
1765 seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
1766 return 0;
1769 static void vmstat_stop(struct seq_file *m, void *arg)
1771 kfree(m->private);
1772 m->private = NULL;
1775 struct seq_operations vmstat_op = {
1776 .start = vmstat_start,
1777 .next = vmstat_next,
1778 .stop = vmstat_stop,
1779 .show = vmstat_show,
1782 #endif /* CONFIG_PROC_FS */
1784 #ifdef CONFIG_HOTPLUG_CPU
1785 static int page_alloc_cpu_notify(struct notifier_block *self,
1786 unsigned long action, void *hcpu)
1788 int cpu = (unsigned long)hcpu;
1789 long *count;
1791 if (action == CPU_DEAD) {
1792 /* Drain local pagecache count. */
1793 count = &per_cpu(nr_pagecache_local, cpu);
1794 atomic_add(*count, &nr_pagecache);
1795 *count = 0;
1796 local_irq_disable();
1797 __drain_pages(cpu);
1798 local_irq_enable();
1800 return NOTIFY_OK;
1802 #endif /* CONFIG_HOTPLUG_CPU */
1804 void __init page_alloc_init(void)
1806 hotcpu_notifier(page_alloc_cpu_notify, 0);
1809 static unsigned long higherzone_val(struct zone *z, int max_zone,
1810 int alloc_type)
1812 int z_idx = zone_idx(z);
1813 struct zone *higherzone;
1814 unsigned long pages;
1816 /* there is no higher zone to get a contribution from */
1817 if (z_idx == MAX_NR_ZONES-1)
1818 return 0;
1820 higherzone = &z->zone_pgdat->node_zones[z_idx+1];
1822 /* We always start with the higher zone's protection value */
1823 pages = higherzone->protection[alloc_type];
1826 * We get a lower-zone-protection contribution only if there are
1827 * pages in the higher zone and if we're not the highest zone
1828 * in the current zonelist. e.g., never happens for GFP_DMA. Happens
1829 * only for ZONE_DMA in a GFP_KERNEL allocation and happens for ZONE_DMA
1830 * and ZONE_NORMAL for a GFP_HIGHMEM allocation.
1832 if (higherzone->present_pages && z_idx < alloc_type)
1833 pages += higherzone->pages_low * sysctl_lower_zone_protection;
1835 return pages;
1839 * setup_per_zone_protection - called whenver min_free_kbytes or
1840 * sysctl_lower_zone_protection changes. Ensures that each zone
1841 * has a correct pages_protected value, so an adequate number of
1842 * pages are left in the zone after a successful __alloc_pages().
1844 * This algorithm is way confusing. I tries to keep the same behavior
1845 * as we had with the incremental min iterative algorithm.
1847 static void setup_per_zone_protection(void)
1849 struct pglist_data *pgdat;
1850 struct zone *zones, *zone;
1851 int max_zone;
1852 int i, j;
1854 for_each_pgdat(pgdat) {
1855 zones = pgdat->node_zones;
1857 for (i = 0, max_zone = 0; i < MAX_NR_ZONES; i++)
1858 if (zones[i].present_pages)
1859 max_zone = i;
1862 * For each of the different allocation types:
1863 * GFP_DMA -> GFP_KERNEL -> GFP_HIGHMEM
1865 for (i = 0; i < GFP_ZONETYPES; i++) {
1867 * For each of the zones:
1868 * ZONE_HIGHMEM -> ZONE_NORMAL -> ZONE_DMA
1870 for (j = MAX_NR_ZONES-1; j >= 0; j--) {
1871 zone = &zones[j];
1874 * We never protect zones that don't have memory
1875 * in them (j>max_zone) or zones that aren't in
1876 * the zonelists for a certain type of
1877 * allocation (j>=i). We have to assign these
1878 * to zero because the lower zones take
1879 * contributions from the higher zones.
1881 if (j > max_zone || j >= i) {
1882 zone->protection[i] = 0;
1883 continue;
1886 * The contribution of the next higher zone
1888 zone->protection[i] = higherzone_val(zone,
1889 max_zone, i);
1896 * setup_per_zone_pages_min - called when min_free_kbytes changes. Ensures
1897 * that the pages_{min,low,high} values for each zone are set correctly
1898 * with respect to min_free_kbytes.
1900 static void setup_per_zone_pages_min(void)
1902 unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
1903 unsigned long lowmem_pages = 0;
1904 struct zone *zone;
1905 unsigned long flags;
1907 /* Calculate total number of !ZONE_HIGHMEM pages */
1908 for_each_zone(zone) {
1909 if (!is_highmem(zone))
1910 lowmem_pages += zone->present_pages;
1913 for_each_zone(zone) {
1914 spin_lock_irqsave(&zone->lru_lock, flags);
1915 if (is_highmem(zone)) {
1917 * Often, highmem doesn't need to reserve any pages.
1918 * But the pages_min/low/high values are also used for
1919 * batching up page reclaim activity so we need a
1920 * decent value here.
1922 int min_pages;
1924 min_pages = zone->present_pages / 1024;
1925 if (min_pages < SWAP_CLUSTER_MAX)
1926 min_pages = SWAP_CLUSTER_MAX;
1927 if (min_pages > 128)
1928 min_pages = 128;
1929 zone->pages_min = min_pages;
1930 } else {
1931 /* if it's a lowmem zone, reserve a number of pages
1932 * proportionate to the zone's size.
1934 zone->pages_min = (pages_min * zone->present_pages) /
1935 lowmem_pages;
1938 zone->pages_low = zone->pages_min * 2;
1939 zone->pages_high = zone->pages_min * 3;
1940 spin_unlock_irqrestore(&zone->lru_lock, flags);
1945 * Initialise min_free_kbytes.
1947 * For small machines we want it small (128k min). For large machines
1948 * we want it large (16MB max). But it is not linear, because network
1949 * bandwidth does not increase linearly with machine size. We use
1951 * min_free_kbytes = sqrt(lowmem_kbytes)
1953 * which yields
1955 * 16MB: 128k
1956 * 32MB: 181k
1957 * 64MB: 256k
1958 * 128MB: 362k
1959 * 256MB: 512k
1960 * 512MB: 724k
1961 * 1024MB: 1024k
1962 * 2048MB: 1448k
1963 * 4096MB: 2048k
1964 * 8192MB: 2896k
1965 * 16384MB: 4096k
1967 static int __init init_per_zone_pages_min(void)
1969 unsigned long lowmem_kbytes;
1971 lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
1973 min_free_kbytes = int_sqrt(lowmem_kbytes);
1974 if (min_free_kbytes < 128)
1975 min_free_kbytes = 128;
1976 if (min_free_kbytes > 16384)
1977 min_free_kbytes = 16384;
1978 setup_per_zone_pages_min();
1979 setup_per_zone_protection();
1980 return 0;
1982 module_init(init_per_zone_pages_min)
1985 * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
1986 * that we can call two helper functions whenever min_free_kbytes
1987 * changes.
1989 int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
1990 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
1992 proc_dointvec(table, write, file, buffer, length, ppos);
1993 setup_per_zone_pages_min();
1994 setup_per_zone_protection();
1995 return 0;
1999 * lower_zone_protection_sysctl_handler - just a wrapper around
2000 * proc_dointvec() so that we can call setup_per_zone_protection()
2001 * whenever sysctl_lower_zone_protection changes.
2003 int lower_zone_protection_sysctl_handler(ctl_table *table, int write,
2004 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
2006 proc_dointvec_minmax(table, write, file, buffer, length, ppos);
2007 setup_per_zone_protection();
2008 return 0;
2012 * allocate a large system hash table from bootmem
2013 * - it is assumed that the hash table must contain an exact power-of-2
2014 * quantity of entries
2016 void *__init alloc_large_system_hash(const char *tablename,
2017 unsigned long bucketsize,
2018 unsigned long numentries,
2019 int scale,
2020 int consider_highmem,
2021 unsigned int *_hash_shift,
2022 unsigned int *_hash_mask)
2024 unsigned long long max;
2025 unsigned long log2qty, size;
2026 void *table;
2028 /* allow the kernel cmdline to have a say */
2029 if (!numentries) {
2030 /* round applicable memory size up to nearest megabyte */
2031 numentries = consider_highmem ? nr_all_pages : nr_kernel_pages;
2032 numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
2033 numentries >>= 20 - PAGE_SHIFT;
2034 numentries <<= 20 - PAGE_SHIFT;
2036 /* limit to 1 bucket per 2^scale bytes of low memory */
2037 if (scale > PAGE_SHIFT)
2038 numentries >>= (scale - PAGE_SHIFT);
2039 else
2040 numentries <<= (PAGE_SHIFT - scale);
2042 /* rounded up to nearest power of 2 in size */
2043 numentries = 1UL << (long_log2(numentries) + 1);
2045 /* limit allocation size to 1/16 total memory */
2046 max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
2047 do_div(max, bucketsize);
2049 if (numentries > max)
2050 numentries = max;
2052 log2qty = long_log2(numentries);
2054 do {
2055 size = bucketsize << log2qty;
2056 table = alloc_bootmem(size);
2057 } while (!table && size > PAGE_SIZE && --log2qty);
2059 if (!table)
2060 panic("Failed to allocate %s hash table\n", tablename);
2062 printk("%s hash table entries: %d (order: %d, %lu bytes)\n",
2063 tablename,
2064 (1U << log2qty),
2065 long_log2(size) - PAGE_SHIFT,
2066 size);
2068 if (_hash_shift)
2069 *_hash_shift = log2qty;
2070 if (_hash_mask)
2071 *_hash_mask = (1 << log2qty) - 1;
2073 return table;