sky2: kfree_skb with IRQ with netconsole
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / mm / page_alloc.c
blobbf777e4f6be2271f564ffd331223d009e2801dbe
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/stddef.h>
18 #include <linux/mm.h>
19 #include <linux/swap.h>
20 #include <linux/interrupt.h>
21 #include <linux/pagemap.h>
22 #include <linux/bootmem.h>
23 #include <linux/compiler.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/suspend.h>
27 #include <linux/pagevec.h>
28 #include <linux/blkdev.h>
29 #include <linux/slab.h>
30 #include <linux/notifier.h>
31 #include <linux/topology.h>
32 #include <linux/sysctl.h>
33 #include <linux/cpu.h>
34 #include <linux/cpuset.h>
35 #include <linux/memory_hotplug.h>
36 #include <linux/nodemask.h>
37 #include <linux/vmalloc.h>
38 #include <linux/mempolicy.h>
39 #include <linux/stop_machine.h>
40 #include <linux/sort.h>
41 #include <linux/pfn.h>
42 #include <linux/backing-dev.h>
44 #include <asm/tlbflush.h>
45 #include <asm/div64.h>
46 #include "internal.h"
49 * MCD - HACK: Find somewhere to initialize this EARLY, or make this
50 * initializer cleaner
52 nodemask_t node_online_map __read_mostly = { { [0] = 1UL } };
53 EXPORT_SYMBOL(node_online_map);
54 nodemask_t node_possible_map __read_mostly = NODE_MASK_ALL;
55 EXPORT_SYMBOL(node_possible_map);
56 unsigned long totalram_pages __read_mostly;
57 unsigned long totalreserve_pages __read_mostly;
58 long nr_swap_pages;
59 int percpu_pagelist_fraction;
61 static void __free_pages_ok(struct page *page, unsigned int order);
64 * results with 256, 32 in the lowmem_reserve sysctl:
65 * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
66 * 1G machine -> (16M dma, 784M normal, 224M high)
67 * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
68 * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
69 * HIGHMEM allocation will (224M+784M)/256 of ram reserved in ZONE_DMA
71 * TBD: should special case ZONE_DMA32 machines here - in those we normally
72 * don't need any ZONE_NORMAL reservation
74 int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1] = {
75 256,
76 #ifdef CONFIG_ZONE_DMA32
77 256,
78 #endif
79 #ifdef CONFIG_HIGHMEM
81 #endif
84 EXPORT_SYMBOL(totalram_pages);
87 * Used by page_zone() to look up the address of the struct zone whose
88 * id is encoded in the upper bits of page->flags
90 struct zone *zone_table[1 << ZONETABLE_SHIFT] __read_mostly;
91 EXPORT_SYMBOL(zone_table);
93 static char *zone_names[MAX_NR_ZONES] = {
94 "DMA",
95 #ifdef CONFIG_ZONE_DMA32
96 "DMA32",
97 #endif
98 "Normal",
99 #ifdef CONFIG_HIGHMEM
100 "HighMem"
101 #endif
104 int min_free_kbytes = 1024;
106 unsigned long __meminitdata nr_kernel_pages;
107 unsigned long __meminitdata nr_all_pages;
108 static unsigned long __initdata dma_reserve;
110 #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
112 * MAX_ACTIVE_REGIONS determines the maxmimum number of distinct
113 * ranges of memory (RAM) that may be registered with add_active_range().
114 * Ranges passed to add_active_range() will be merged if possible
115 * so the number of times add_active_range() can be called is
116 * related to the number of nodes and the number of holes
118 #ifdef CONFIG_MAX_ACTIVE_REGIONS
119 /* Allow an architecture to set MAX_ACTIVE_REGIONS to save memory */
120 #define MAX_ACTIVE_REGIONS CONFIG_MAX_ACTIVE_REGIONS
121 #else
122 #if MAX_NUMNODES >= 32
123 /* If there can be many nodes, allow up to 50 holes per node */
124 #define MAX_ACTIVE_REGIONS (MAX_NUMNODES*50)
125 #else
126 /* By default, allow up to 256 distinct regions */
127 #define MAX_ACTIVE_REGIONS 256
128 #endif
129 #endif
131 struct node_active_region __initdata early_node_map[MAX_ACTIVE_REGIONS];
132 int __initdata nr_nodemap_entries;
133 unsigned long __initdata arch_zone_lowest_possible_pfn[MAX_NR_ZONES];
134 unsigned long __initdata arch_zone_highest_possible_pfn[MAX_NR_ZONES];
135 #ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
136 unsigned long __initdata node_boundary_start_pfn[MAX_NUMNODES];
137 unsigned long __initdata node_boundary_end_pfn[MAX_NUMNODES];
138 #endif /* CONFIG_MEMORY_HOTPLUG_RESERVE */
139 #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
141 #ifdef CONFIG_DEBUG_VM
142 static int page_outside_zone_boundaries(struct zone *zone, struct page *page)
144 int ret = 0;
145 unsigned seq;
146 unsigned long pfn = page_to_pfn(page);
148 do {
149 seq = zone_span_seqbegin(zone);
150 if (pfn >= zone->zone_start_pfn + zone->spanned_pages)
151 ret = 1;
152 else if (pfn < zone->zone_start_pfn)
153 ret = 1;
154 } while (zone_span_seqretry(zone, seq));
156 return ret;
159 static int page_is_consistent(struct zone *zone, struct page *page)
161 #ifdef CONFIG_HOLES_IN_ZONE
162 if (!pfn_valid(page_to_pfn(page)))
163 return 0;
164 #endif
165 if (zone != page_zone(page))
166 return 0;
168 return 1;
171 * Temporary debugging check for pages not lying within a given zone.
173 static int bad_range(struct zone *zone, struct page *page)
175 if (page_outside_zone_boundaries(zone, page))
176 return 1;
177 if (!page_is_consistent(zone, page))
178 return 1;
180 return 0;
182 #else
183 static inline int bad_range(struct zone *zone, struct page *page)
185 return 0;
187 #endif
189 static void bad_page(struct page *page)
191 printk(KERN_EMERG "Bad page state in process '%s'\n"
192 KERN_EMERG "page:%p flags:0x%0*lx mapping:%p mapcount:%d count:%d\n"
193 KERN_EMERG "Trying to fix it up, but a reboot is needed\n"
194 KERN_EMERG "Backtrace:\n",
195 current->comm, page, (int)(2*sizeof(unsigned long)),
196 (unsigned long)page->flags, page->mapping,
197 page_mapcount(page), page_count(page));
198 dump_stack();
199 page->flags &= ~(1 << PG_lru |
200 1 << PG_private |
201 1 << PG_locked |
202 1 << PG_active |
203 1 << PG_dirty |
204 1 << PG_reclaim |
205 1 << PG_slab |
206 1 << PG_swapcache |
207 1 << PG_writeback |
208 1 << PG_buddy );
209 set_page_count(page, 0);
210 reset_page_mapcount(page);
211 page->mapping = NULL;
212 add_taint(TAINT_BAD_PAGE);
216 * Higher-order pages are called "compound pages". They are structured thusly:
218 * The first PAGE_SIZE page is called the "head page".
220 * The remaining PAGE_SIZE pages are called "tail pages".
222 * All pages have PG_compound set. All pages have their ->private pointing at
223 * the head page (even the head page has this).
225 * The first tail page's ->lru.next holds the address of the compound page's
226 * put_page() function. Its ->lru.prev holds the order of allocation.
227 * This usage means that zero-order pages may not be compound.
230 static void free_compound_page(struct page *page)
232 __free_pages_ok(page, (unsigned long)page[1].lru.prev);
235 static void prep_compound_page(struct page *page, unsigned long order)
237 int i;
238 int nr_pages = 1 << order;
240 page[1].lru.next = (void *)free_compound_page; /* set dtor */
241 page[1].lru.prev = (void *)order;
242 for (i = 0; i < nr_pages; i++) {
243 struct page *p = page + i;
245 __SetPageCompound(p);
246 set_page_private(p, (unsigned long)page);
250 static void destroy_compound_page(struct page *page, unsigned long order)
252 int i;
253 int nr_pages = 1 << order;
255 if (unlikely((unsigned long)page[1].lru.prev != order))
256 bad_page(page);
258 for (i = 0; i < nr_pages; i++) {
259 struct page *p = page + i;
261 if (unlikely(!PageCompound(p) |
262 (page_private(p) != (unsigned long)page)))
263 bad_page(page);
264 __ClearPageCompound(p);
268 static inline void prep_zero_page(struct page *page, int order, gfp_t gfp_flags)
270 int i;
272 VM_BUG_ON((gfp_flags & (__GFP_WAIT | __GFP_HIGHMEM)) == __GFP_HIGHMEM);
274 * clear_highpage() will use KM_USER0, so it's a bug to use __GFP_ZERO
275 * and __GFP_HIGHMEM from hard or soft interrupt context.
277 VM_BUG_ON((gfp_flags & __GFP_HIGHMEM) && in_interrupt());
278 for (i = 0; i < (1 << order); i++)
279 clear_highpage(page + i);
283 * function for dealing with page's order in buddy system.
284 * zone->lock is already acquired when we use these.
285 * So, we don't need atomic page->flags operations here.
287 static inline unsigned long page_order(struct page *page)
289 return page_private(page);
292 static inline void set_page_order(struct page *page, int order)
294 set_page_private(page, order);
295 __SetPageBuddy(page);
298 static inline void rmv_page_order(struct page *page)
300 __ClearPageBuddy(page);
301 set_page_private(page, 0);
305 * Locate the struct page for both the matching buddy in our
306 * pair (buddy1) and the combined O(n+1) page they form (page).
308 * 1) Any buddy B1 will have an order O twin B2 which satisfies
309 * the following equation:
310 * B2 = B1 ^ (1 << O)
311 * For example, if the starting buddy (buddy2) is #8 its order
312 * 1 buddy is #10:
313 * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
315 * 2) Any buddy B will have an order O+1 parent P which
316 * satisfies the following equation:
317 * P = B & ~(1 << O)
319 * Assumption: *_mem_map is contiguous at least up to MAX_ORDER
321 static inline struct page *
322 __page_find_buddy(struct page *page, unsigned long page_idx, unsigned int order)
324 unsigned long buddy_idx = page_idx ^ (1 << order);
326 return page + (buddy_idx - page_idx);
329 static inline unsigned long
330 __find_combined_index(unsigned long page_idx, unsigned int order)
332 return (page_idx & ~(1 << order));
336 * This function checks whether a page is free && is the buddy
337 * we can do coalesce a page and its buddy if
338 * (a) the buddy is not in a hole &&
339 * (b) the buddy is in the buddy system &&
340 * (c) a page and its buddy have the same order &&
341 * (d) a page and its buddy are in the same zone.
343 * For recording whether a page is in the buddy system, we use PG_buddy.
344 * Setting, clearing, and testing PG_buddy is serialized by zone->lock.
346 * For recording page's order, we use page_private(page).
348 static inline int page_is_buddy(struct page *page, struct page *buddy,
349 int order)
351 #ifdef CONFIG_HOLES_IN_ZONE
352 if (!pfn_valid(page_to_pfn(buddy)))
353 return 0;
354 #endif
356 if (page_zone_id(page) != page_zone_id(buddy))
357 return 0;
359 if (PageBuddy(buddy) && page_order(buddy) == order) {
360 BUG_ON(page_count(buddy) != 0);
361 return 1;
363 return 0;
367 * Freeing function for a buddy system allocator.
369 * The concept of a buddy system is to maintain direct-mapped table
370 * (containing bit values) for memory blocks of various "orders".
371 * The bottom level table contains the map for the smallest allocatable
372 * units of memory (here, pages), and each level above it describes
373 * pairs of units from the levels below, hence, "buddies".
374 * At a high level, all that happens here is marking the table entry
375 * at the bottom level available, and propagating the changes upward
376 * as necessary, plus some accounting needed to play nicely with other
377 * parts of the VM system.
378 * At each level, we keep a list of pages, which are heads of continuous
379 * free pages of length of (1 << order) and marked with PG_buddy. Page's
380 * order is recorded in page_private(page) field.
381 * So when we are allocating or freeing one, we can derive the state of the
382 * other. That is, if we allocate a small block, and both were
383 * free, the remainder of the region must be split into blocks.
384 * If a block is freed, and its buddy is also free, then this
385 * triggers coalescing into a block of larger size.
387 * -- wli
390 static inline void __free_one_page(struct page *page,
391 struct zone *zone, unsigned int order)
393 unsigned long page_idx;
394 int order_size = 1 << order;
396 if (unlikely(PageCompound(page)))
397 destroy_compound_page(page, order);
399 page_idx = page_to_pfn(page) & ((1 << MAX_ORDER) - 1);
401 VM_BUG_ON(page_idx & (order_size - 1));
402 VM_BUG_ON(bad_range(zone, page));
404 zone->free_pages += order_size;
405 while (order < MAX_ORDER-1) {
406 unsigned long combined_idx;
407 struct free_area *area;
408 struct page *buddy;
410 buddy = __page_find_buddy(page, page_idx, order);
411 if (!page_is_buddy(page, buddy, order))
412 break; /* Move the buddy up one level. */
414 list_del(&buddy->lru);
415 area = zone->free_area + order;
416 area->nr_free--;
417 rmv_page_order(buddy);
418 combined_idx = __find_combined_index(page_idx, order);
419 page = page + (combined_idx - page_idx);
420 page_idx = combined_idx;
421 order++;
423 set_page_order(page, order);
424 list_add(&page->lru, &zone->free_area[order].free_list);
425 zone->free_area[order].nr_free++;
428 static inline int free_pages_check(struct page *page)
430 if (unlikely(page_mapcount(page) |
431 (page->mapping != NULL) |
432 (page_count(page) != 0) |
433 (page->flags & (
434 1 << PG_lru |
435 1 << PG_private |
436 1 << PG_locked |
437 1 << PG_active |
438 1 << PG_reclaim |
439 1 << PG_slab |
440 1 << PG_swapcache |
441 1 << PG_writeback |
442 1 << PG_reserved |
443 1 << PG_buddy ))))
444 bad_page(page);
445 if (PageDirty(page))
446 __ClearPageDirty(page);
448 * For now, we report if PG_reserved was found set, but do not
449 * clear it, and do not free the page. But we shall soon need
450 * to do more, for when the ZERO_PAGE count wraps negative.
452 return PageReserved(page);
456 * Frees a list of pages.
457 * Assumes all pages on list are in same zone, and of same order.
458 * count is the number of pages to free.
460 * If the zone was previously in an "all pages pinned" state then look to
461 * see if this freeing clears that state.
463 * And clear the zone's pages_scanned counter, to hold off the "all pages are
464 * pinned" detection logic.
466 static void free_pages_bulk(struct zone *zone, int count,
467 struct list_head *list, int order)
469 spin_lock(&zone->lock);
470 zone->all_unreclaimable = 0;
471 zone->pages_scanned = 0;
472 while (count--) {
473 struct page *page;
475 VM_BUG_ON(list_empty(list));
476 page = list_entry(list->prev, struct page, lru);
477 /* have to delete it as __free_one_page list manipulates */
478 list_del(&page->lru);
479 __free_one_page(page, zone, order);
481 spin_unlock(&zone->lock);
484 static void free_one_page(struct zone *zone, struct page *page, int order)
486 spin_lock(&zone->lock);
487 zone->all_unreclaimable = 0;
488 zone->pages_scanned = 0;
489 __free_one_page(page, zone ,order);
490 spin_unlock(&zone->lock);
493 static void __free_pages_ok(struct page *page, unsigned int order)
495 unsigned long flags;
496 int i;
497 int reserved = 0;
499 for (i = 0 ; i < (1 << order) ; ++i)
500 reserved += free_pages_check(page + i);
501 if (reserved)
502 return;
504 if (!PageHighMem(page))
505 debug_check_no_locks_freed(page_address(page),PAGE_SIZE<<order);
506 arch_free_page(page, order);
507 kernel_map_pages(page, 1 << order, 0);
509 local_irq_save(flags);
510 __count_vm_events(PGFREE, 1 << order);
511 free_one_page(page_zone(page), page, order);
512 local_irq_restore(flags);
516 * permit the bootmem allocator to evade page validation on high-order frees
518 void fastcall __init __free_pages_bootmem(struct page *page, unsigned int order)
520 if (order == 0) {
521 __ClearPageReserved(page);
522 set_page_count(page, 0);
523 set_page_refcounted(page);
524 __free_page(page);
525 } else {
526 int loop;
528 prefetchw(page);
529 for (loop = 0; loop < BITS_PER_LONG; loop++) {
530 struct page *p = &page[loop];
532 if (loop + 1 < BITS_PER_LONG)
533 prefetchw(p + 1);
534 __ClearPageReserved(p);
535 set_page_count(p, 0);
538 set_page_refcounted(page);
539 __free_pages(page, order);
545 * The order of subdivision here is critical for the IO subsystem.
546 * Please do not alter this order without good reasons and regression
547 * testing. Specifically, as large blocks of memory are subdivided,
548 * the order in which smaller blocks are delivered depends on the order
549 * they're subdivided in this function. This is the primary factor
550 * influencing the order in which pages are delivered to the IO
551 * subsystem according to empirical testing, and this is also justified
552 * by considering the behavior of a buddy system containing a single
553 * large block of memory acted on by a series of small allocations.
554 * This behavior is a critical factor in sglist merging's success.
556 * -- wli
558 static inline void expand(struct zone *zone, struct page *page,
559 int low, int high, struct free_area *area)
561 unsigned long size = 1 << high;
563 while (high > low) {
564 area--;
565 high--;
566 size >>= 1;
567 VM_BUG_ON(bad_range(zone, &page[size]));
568 list_add(&page[size].lru, &area->free_list);
569 area->nr_free++;
570 set_page_order(&page[size], high);
575 * This page is about to be returned from the page allocator
577 static int prep_new_page(struct page *page, int order, gfp_t gfp_flags)
579 if (unlikely(page_mapcount(page) |
580 (page->mapping != NULL) |
581 (page_count(page) != 0) |
582 (page->flags & (
583 1 << PG_lru |
584 1 << PG_private |
585 1 << PG_locked |
586 1 << PG_active |
587 1 << PG_dirty |
588 1 << PG_reclaim |
589 1 << PG_slab |
590 1 << PG_swapcache |
591 1 << PG_writeback |
592 1 << PG_reserved |
593 1 << PG_buddy ))))
594 bad_page(page);
597 * For now, we report if PG_reserved was found set, but do not
598 * clear it, and do not allocate the page: as a safety net.
600 if (PageReserved(page))
601 return 1;
603 page->flags &= ~(1 << PG_uptodate | 1 << PG_error |
604 1 << PG_referenced | 1 << PG_arch_1 |
605 1 << PG_checked | 1 << PG_mappedtodisk);
606 set_page_private(page, 0);
607 set_page_refcounted(page);
608 kernel_map_pages(page, 1 << order, 1);
610 if (gfp_flags & __GFP_ZERO)
611 prep_zero_page(page, order, gfp_flags);
613 if (order && (gfp_flags & __GFP_COMP))
614 prep_compound_page(page, order);
616 return 0;
620 * Do the hard work of removing an element from the buddy allocator.
621 * Call me with the zone->lock already held.
623 static struct page *__rmqueue(struct zone *zone, unsigned int order)
625 struct free_area * area;
626 unsigned int current_order;
627 struct page *page;
629 for (current_order = order; current_order < MAX_ORDER; ++current_order) {
630 area = zone->free_area + current_order;
631 if (list_empty(&area->free_list))
632 continue;
634 page = list_entry(area->free_list.next, struct page, lru);
635 list_del(&page->lru);
636 rmv_page_order(page);
637 area->nr_free--;
638 zone->free_pages -= 1UL << order;
639 expand(zone, page, order, current_order, area);
640 return page;
643 return NULL;
647 * Obtain a specified number of elements from the buddy allocator, all under
648 * a single hold of the lock, for efficiency. Add them to the supplied list.
649 * Returns the number of new pages which were placed at *list.
651 static int rmqueue_bulk(struct zone *zone, unsigned int order,
652 unsigned long count, struct list_head *list)
654 int i;
656 spin_lock(&zone->lock);
657 for (i = 0; i < count; ++i) {
658 struct page *page = __rmqueue(zone, order);
659 if (unlikely(page == NULL))
660 break;
661 list_add_tail(&page->lru, list);
663 spin_unlock(&zone->lock);
664 return i;
667 #ifdef CONFIG_NUMA
669 * Called from the slab reaper to drain pagesets on a particular node that
670 * belongs to the currently executing processor.
671 * Note that this function must be called with the thread pinned to
672 * a single processor.
674 void drain_node_pages(int nodeid)
676 int i;
677 enum zone_type z;
678 unsigned long flags;
680 for (z = 0; z < MAX_NR_ZONES; z++) {
681 struct zone *zone = NODE_DATA(nodeid)->node_zones + z;
682 struct per_cpu_pageset *pset;
684 if (!populated_zone(zone))
685 continue;
687 pset = zone_pcp(zone, smp_processor_id());
688 for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
689 struct per_cpu_pages *pcp;
691 pcp = &pset->pcp[i];
692 if (pcp->count) {
693 local_irq_save(flags);
694 free_pages_bulk(zone, pcp->count, &pcp->list, 0);
695 pcp->count = 0;
696 local_irq_restore(flags);
701 #endif
703 #if defined(CONFIG_PM) || defined(CONFIG_HOTPLUG_CPU)
704 static void __drain_pages(unsigned int cpu)
706 unsigned long flags;
707 struct zone *zone;
708 int i;
710 for_each_zone(zone) {
711 struct per_cpu_pageset *pset;
713 if (!populated_zone(zone))
714 continue;
716 pset = zone_pcp(zone, cpu);
717 for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
718 struct per_cpu_pages *pcp;
720 pcp = &pset->pcp[i];
721 local_irq_save(flags);
722 free_pages_bulk(zone, pcp->count, &pcp->list, 0);
723 pcp->count = 0;
724 local_irq_restore(flags);
728 #endif /* CONFIG_PM || CONFIG_HOTPLUG_CPU */
730 #ifdef CONFIG_PM
732 void mark_free_pages(struct zone *zone)
734 unsigned long pfn, max_zone_pfn;
735 unsigned long flags;
736 int order;
737 struct list_head *curr;
739 if (!zone->spanned_pages)
740 return;
742 spin_lock_irqsave(&zone->lock, flags);
744 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
745 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
746 if (pfn_valid(pfn)) {
747 struct page *page = pfn_to_page(pfn);
749 if (!PageNosave(page))
750 ClearPageNosaveFree(page);
753 for (order = MAX_ORDER - 1; order >= 0; --order)
754 list_for_each(curr, &zone->free_area[order].free_list) {
755 unsigned long i;
757 pfn = page_to_pfn(list_entry(curr, struct page, lru));
758 for (i = 0; i < (1UL << order); i++)
759 SetPageNosaveFree(pfn_to_page(pfn + i));
762 spin_unlock_irqrestore(&zone->lock, flags);
766 * Spill all of this CPU's per-cpu pages back into the buddy allocator.
768 void drain_local_pages(void)
770 unsigned long flags;
772 local_irq_save(flags);
773 __drain_pages(smp_processor_id());
774 local_irq_restore(flags);
776 #endif /* CONFIG_PM */
779 * Free a 0-order page
781 static void fastcall free_hot_cold_page(struct page *page, int cold)
783 struct zone *zone = page_zone(page);
784 struct per_cpu_pages *pcp;
785 unsigned long flags;
787 if (PageAnon(page))
788 page->mapping = NULL;
789 if (free_pages_check(page))
790 return;
792 if (!PageHighMem(page))
793 debug_check_no_locks_freed(page_address(page), PAGE_SIZE);
794 arch_free_page(page, 0);
795 kernel_map_pages(page, 1, 0);
797 pcp = &zone_pcp(zone, get_cpu())->pcp[cold];
798 local_irq_save(flags);
799 __count_vm_event(PGFREE);
800 list_add(&page->lru, &pcp->list);
801 pcp->count++;
802 if (pcp->count >= pcp->high) {
803 free_pages_bulk(zone, pcp->batch, &pcp->list, 0);
804 pcp->count -= pcp->batch;
806 local_irq_restore(flags);
807 put_cpu();
810 void fastcall free_hot_page(struct page *page)
812 free_hot_cold_page(page, 0);
815 void fastcall free_cold_page(struct page *page)
817 free_hot_cold_page(page, 1);
821 * split_page takes a non-compound higher-order page, and splits it into
822 * n (1<<order) sub-pages: page[0..n]
823 * Each sub-page must be freed individually.
825 * Note: this is probably too low level an operation for use in drivers.
826 * Please consult with lkml before using this in your driver.
828 void split_page(struct page *page, unsigned int order)
830 int i;
832 VM_BUG_ON(PageCompound(page));
833 VM_BUG_ON(!page_count(page));
834 for (i = 1; i < (1 << order); i++)
835 set_page_refcounted(page + i);
839 * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
840 * we cheat by calling it from here, in the order > 0 path. Saves a branch
841 * or two.
843 static struct page *buffered_rmqueue(struct zonelist *zonelist,
844 struct zone *zone, int order, gfp_t gfp_flags)
846 unsigned long flags;
847 struct page *page;
848 int cold = !!(gfp_flags & __GFP_COLD);
849 int cpu;
851 again:
852 cpu = get_cpu();
853 if (likely(order == 0)) {
854 struct per_cpu_pages *pcp;
856 pcp = &zone_pcp(zone, cpu)->pcp[cold];
857 local_irq_save(flags);
858 if (!pcp->count) {
859 pcp->count = rmqueue_bulk(zone, 0,
860 pcp->batch, &pcp->list);
861 if (unlikely(!pcp->count))
862 goto failed;
864 page = list_entry(pcp->list.next, struct page, lru);
865 list_del(&page->lru);
866 pcp->count--;
867 } else {
868 spin_lock_irqsave(&zone->lock, flags);
869 page = __rmqueue(zone, order);
870 spin_unlock(&zone->lock);
871 if (!page)
872 goto failed;
875 __count_zone_vm_events(PGALLOC, zone, 1 << order);
876 zone_statistics(zonelist, zone);
877 local_irq_restore(flags);
878 put_cpu();
880 VM_BUG_ON(bad_range(zone, page));
881 if (prep_new_page(page, order, gfp_flags))
882 goto again;
883 return page;
885 failed:
886 local_irq_restore(flags);
887 put_cpu();
888 return NULL;
891 #define ALLOC_NO_WATERMARKS 0x01 /* don't check watermarks at all */
892 #define ALLOC_WMARK_MIN 0x02 /* use pages_min watermark */
893 #define ALLOC_WMARK_LOW 0x04 /* use pages_low watermark */
894 #define ALLOC_WMARK_HIGH 0x08 /* use pages_high watermark */
895 #define ALLOC_HARDER 0x10 /* try to alloc harder */
896 #define ALLOC_HIGH 0x20 /* __GFP_HIGH set */
897 #define ALLOC_CPUSET 0x40 /* check for correct cpuset */
900 * Return 1 if free pages are above 'mark'. This takes into account the order
901 * of the allocation.
903 int zone_watermark_ok(struct zone *z, int order, unsigned long mark,
904 int classzone_idx, int alloc_flags)
906 /* free_pages my go negative - that's OK */
907 unsigned long min = mark;
908 long free_pages = z->free_pages - (1 << order) + 1;
909 int o;
911 if (alloc_flags & ALLOC_HIGH)
912 min -= min / 2;
913 if (alloc_flags & ALLOC_HARDER)
914 min -= min / 4;
916 if (free_pages <= min + z->lowmem_reserve[classzone_idx])
917 return 0;
918 for (o = 0; o < order; o++) {
919 /* At the next order, this order's pages become unavailable */
920 free_pages -= z->free_area[o].nr_free << o;
922 /* Require fewer higher order pages to be free */
923 min >>= 1;
925 if (free_pages <= min)
926 return 0;
928 return 1;
932 * get_page_from_freeliest goes through the zonelist trying to allocate
933 * a page.
935 static struct page *
936 get_page_from_freelist(gfp_t gfp_mask, unsigned int order,
937 struct zonelist *zonelist, int alloc_flags)
939 struct zone **z = zonelist->zones;
940 struct page *page = NULL;
941 int classzone_idx = zone_idx(*z);
942 struct zone *zone;
945 * Go through the zonelist once, looking for a zone with enough free.
946 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
948 do {
949 zone = *z;
950 if (unlikely(NUMA_BUILD && (gfp_mask & __GFP_THISNODE) &&
951 zone->zone_pgdat != zonelist->zones[0]->zone_pgdat))
952 break;
953 if ((alloc_flags & ALLOC_CPUSET) &&
954 !cpuset_zone_allowed(zone, gfp_mask))
955 continue;
957 if (!(alloc_flags & ALLOC_NO_WATERMARKS)) {
958 unsigned long mark;
959 if (alloc_flags & ALLOC_WMARK_MIN)
960 mark = zone->pages_min;
961 else if (alloc_flags & ALLOC_WMARK_LOW)
962 mark = zone->pages_low;
963 else
964 mark = zone->pages_high;
965 if (!zone_watermark_ok(zone , order, mark,
966 classzone_idx, alloc_flags))
967 if (!zone_reclaim_mode ||
968 !zone_reclaim(zone, gfp_mask, order))
969 continue;
972 page = buffered_rmqueue(zonelist, zone, order, gfp_mask);
973 if (page) {
974 break;
976 } while (*(++z) != NULL);
977 return page;
981 * This is the 'heart' of the zoned buddy allocator.
983 struct page * fastcall
984 __alloc_pages(gfp_t gfp_mask, unsigned int order,
985 struct zonelist *zonelist)
987 const gfp_t wait = gfp_mask & __GFP_WAIT;
988 struct zone **z;
989 struct page *page;
990 struct reclaim_state reclaim_state;
991 struct task_struct *p = current;
992 int do_retry;
993 int alloc_flags;
994 int did_some_progress;
996 might_sleep_if(wait);
998 restart:
999 z = zonelist->zones; /* the list of zones suitable for gfp_mask */
1001 if (unlikely(*z == NULL)) {
1002 /* Should this ever happen?? */
1003 return NULL;
1006 page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, order,
1007 zonelist, ALLOC_WMARK_LOW|ALLOC_CPUSET);
1008 if (page)
1009 goto got_pg;
1011 do {
1012 wakeup_kswapd(*z, order);
1013 } while (*(++z));
1016 * OK, we're below the kswapd watermark and have kicked background
1017 * reclaim. Now things get more complex, so set up alloc_flags according
1018 * to how we want to proceed.
1020 * The caller may dip into page reserves a bit more if the caller
1021 * cannot run direct reclaim, or if the caller has realtime scheduling
1022 * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will
1023 * set both ALLOC_HARDER (!wait) and ALLOC_HIGH (__GFP_HIGH).
1025 alloc_flags = ALLOC_WMARK_MIN;
1026 if ((unlikely(rt_task(p)) && !in_interrupt()) || !wait)
1027 alloc_flags |= ALLOC_HARDER;
1028 if (gfp_mask & __GFP_HIGH)
1029 alloc_flags |= ALLOC_HIGH;
1030 if (wait)
1031 alloc_flags |= ALLOC_CPUSET;
1034 * Go through the zonelist again. Let __GFP_HIGH and allocations
1035 * coming from realtime tasks go deeper into reserves.
1037 * This is the last chance, in general, before the goto nopage.
1038 * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
1039 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
1041 page = get_page_from_freelist(gfp_mask, order, zonelist, alloc_flags);
1042 if (page)
1043 goto got_pg;
1045 /* This allocation should allow future memory freeing. */
1047 if (((p->flags & PF_MEMALLOC) || unlikely(test_thread_flag(TIF_MEMDIE)))
1048 && !in_interrupt()) {
1049 if (!(gfp_mask & __GFP_NOMEMALLOC)) {
1050 nofail_alloc:
1051 /* go through the zonelist yet again, ignoring mins */
1052 page = get_page_from_freelist(gfp_mask, order,
1053 zonelist, ALLOC_NO_WATERMARKS);
1054 if (page)
1055 goto got_pg;
1056 if (gfp_mask & __GFP_NOFAIL) {
1057 congestion_wait(WRITE, HZ/50);
1058 goto nofail_alloc;
1061 goto nopage;
1064 /* Atomic allocations - we can't balance anything */
1065 if (!wait)
1066 goto nopage;
1068 rebalance:
1069 cond_resched();
1071 /* We now go into synchronous reclaim */
1072 cpuset_memory_pressure_bump();
1073 p->flags |= PF_MEMALLOC;
1074 reclaim_state.reclaimed_slab = 0;
1075 p->reclaim_state = &reclaim_state;
1077 did_some_progress = try_to_free_pages(zonelist->zones, gfp_mask);
1079 p->reclaim_state = NULL;
1080 p->flags &= ~PF_MEMALLOC;
1082 cond_resched();
1084 if (likely(did_some_progress)) {
1085 page = get_page_from_freelist(gfp_mask, order,
1086 zonelist, alloc_flags);
1087 if (page)
1088 goto got_pg;
1089 } else if ((gfp_mask & __GFP_FS) && !(gfp_mask & __GFP_NORETRY)) {
1091 * Go through the zonelist yet one more time, keep
1092 * very high watermark here, this is only to catch
1093 * a parallel oom killing, we must fail if we're still
1094 * under heavy pressure.
1096 page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, order,
1097 zonelist, ALLOC_WMARK_HIGH|ALLOC_CPUSET);
1098 if (page)
1099 goto got_pg;
1101 out_of_memory(zonelist, gfp_mask, order);
1102 goto restart;
1106 * Don't let big-order allocations loop unless the caller explicitly
1107 * requests that. Wait for some write requests to complete then retry.
1109 * In this implementation, __GFP_REPEAT means __GFP_NOFAIL for order
1110 * <= 3, but that may not be true in other implementations.
1112 do_retry = 0;
1113 if (!(gfp_mask & __GFP_NORETRY)) {
1114 if ((order <= 3) || (gfp_mask & __GFP_REPEAT))
1115 do_retry = 1;
1116 if (gfp_mask & __GFP_NOFAIL)
1117 do_retry = 1;
1119 if (do_retry) {
1120 congestion_wait(WRITE, HZ/50);
1121 goto rebalance;
1124 nopage:
1125 if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) {
1126 printk(KERN_WARNING "%s: page allocation failure."
1127 " order:%d, mode:0x%x\n",
1128 p->comm, order, gfp_mask);
1129 dump_stack();
1130 show_mem();
1132 got_pg:
1133 return page;
1136 EXPORT_SYMBOL(__alloc_pages);
1139 * Common helper functions.
1141 fastcall unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
1143 struct page * page;
1144 page = alloc_pages(gfp_mask, order);
1145 if (!page)
1146 return 0;
1147 return (unsigned long) page_address(page);
1150 EXPORT_SYMBOL(__get_free_pages);
1152 fastcall unsigned long get_zeroed_page(gfp_t gfp_mask)
1154 struct page * page;
1157 * get_zeroed_page() returns a 32-bit address, which cannot represent
1158 * a highmem page
1160 VM_BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
1162 page = alloc_pages(gfp_mask | __GFP_ZERO, 0);
1163 if (page)
1164 return (unsigned long) page_address(page);
1165 return 0;
1168 EXPORT_SYMBOL(get_zeroed_page);
1170 void __pagevec_free(struct pagevec *pvec)
1172 int i = pagevec_count(pvec);
1174 while (--i >= 0)
1175 free_hot_cold_page(pvec->pages[i], pvec->cold);
1178 fastcall void __free_pages(struct page *page, unsigned int order)
1180 if (put_page_testzero(page)) {
1181 if (order == 0)
1182 free_hot_page(page);
1183 else
1184 __free_pages_ok(page, order);
1188 EXPORT_SYMBOL(__free_pages);
1190 fastcall void free_pages(unsigned long addr, unsigned int order)
1192 if (addr != 0) {
1193 VM_BUG_ON(!virt_addr_valid((void *)addr));
1194 __free_pages(virt_to_page((void *)addr), order);
1198 EXPORT_SYMBOL(free_pages);
1201 * Total amount of free (allocatable) RAM:
1203 unsigned int nr_free_pages(void)
1205 unsigned int sum = 0;
1206 struct zone *zone;
1208 for_each_zone(zone)
1209 sum += zone->free_pages;
1211 return sum;
1214 EXPORT_SYMBOL(nr_free_pages);
1216 #ifdef CONFIG_NUMA
1217 unsigned int nr_free_pages_pgdat(pg_data_t *pgdat)
1219 unsigned int sum = 0;
1220 enum zone_type i;
1222 for (i = 0; i < MAX_NR_ZONES; i++)
1223 sum += pgdat->node_zones[i].free_pages;
1225 return sum;
1227 #endif
1229 static unsigned int nr_free_zone_pages(int offset)
1231 /* Just pick one node, since fallback list is circular */
1232 pg_data_t *pgdat = NODE_DATA(numa_node_id());
1233 unsigned int sum = 0;
1235 struct zonelist *zonelist = pgdat->node_zonelists + offset;
1236 struct zone **zonep = zonelist->zones;
1237 struct zone *zone;
1239 for (zone = *zonep++; zone; zone = *zonep++) {
1240 unsigned long size = zone->present_pages;
1241 unsigned long high = zone->pages_high;
1242 if (size > high)
1243 sum += size - high;
1246 return sum;
1250 * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
1252 unsigned int nr_free_buffer_pages(void)
1254 return nr_free_zone_pages(gfp_zone(GFP_USER));
1258 * Amount of free RAM allocatable within all zones
1260 unsigned int nr_free_pagecache_pages(void)
1262 return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER));
1265 static inline void show_node(struct zone *zone)
1267 if (NUMA_BUILD)
1268 printk("Node %ld ", zone_to_nid(zone));
1271 void si_meminfo(struct sysinfo *val)
1273 val->totalram = totalram_pages;
1274 val->sharedram = 0;
1275 val->freeram = nr_free_pages();
1276 val->bufferram = nr_blockdev_pages();
1277 val->totalhigh = totalhigh_pages;
1278 val->freehigh = nr_free_highpages();
1279 val->mem_unit = PAGE_SIZE;
1282 EXPORT_SYMBOL(si_meminfo);
1284 #ifdef CONFIG_NUMA
1285 void si_meminfo_node(struct sysinfo *val, int nid)
1287 pg_data_t *pgdat = NODE_DATA(nid);
1289 val->totalram = pgdat->node_present_pages;
1290 val->freeram = nr_free_pages_pgdat(pgdat);
1291 #ifdef CONFIG_HIGHMEM
1292 val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].present_pages;
1293 val->freehigh = pgdat->node_zones[ZONE_HIGHMEM].free_pages;
1294 #else
1295 val->totalhigh = 0;
1296 val->freehigh = 0;
1297 #endif
1298 val->mem_unit = PAGE_SIZE;
1300 #endif
1302 #define K(x) ((x) << (PAGE_SHIFT-10))
1305 * Show free area list (used inside shift_scroll-lock stuff)
1306 * We also calculate the percentage fragmentation. We do this by counting the
1307 * memory on each free list with the exception of the first item on the list.
1309 void show_free_areas(void)
1311 int cpu;
1312 unsigned long active;
1313 unsigned long inactive;
1314 unsigned long free;
1315 struct zone *zone;
1317 for_each_zone(zone) {
1318 if (!populated_zone(zone))
1319 continue;
1321 show_node(zone);
1322 printk("%s per-cpu:\n", zone->name);
1324 for_each_online_cpu(cpu) {
1325 struct per_cpu_pageset *pageset;
1327 pageset = zone_pcp(zone, cpu);
1329 printk("CPU %4d: Hot: hi:%5d, btch:%4d usd:%4d "
1330 "Cold: hi:%5d, btch:%4d usd:%4d\n",
1331 cpu, pageset->pcp[0].high,
1332 pageset->pcp[0].batch, pageset->pcp[0].count,
1333 pageset->pcp[1].high, pageset->pcp[1].batch,
1334 pageset->pcp[1].count);
1338 get_zone_counts(&active, &inactive, &free);
1340 printk("Active:%lu inactive:%lu dirty:%lu writeback:%lu "
1341 "unstable:%lu free:%u slab:%lu mapped:%lu pagetables:%lu\n",
1342 active,
1343 inactive,
1344 global_page_state(NR_FILE_DIRTY),
1345 global_page_state(NR_WRITEBACK),
1346 global_page_state(NR_UNSTABLE_NFS),
1347 nr_free_pages(),
1348 global_page_state(NR_SLAB_RECLAIMABLE) +
1349 global_page_state(NR_SLAB_UNRECLAIMABLE),
1350 global_page_state(NR_FILE_MAPPED),
1351 global_page_state(NR_PAGETABLE));
1353 for_each_zone(zone) {
1354 int i;
1356 if (!populated_zone(zone))
1357 continue;
1359 show_node(zone);
1360 printk("%s"
1361 " free:%lukB"
1362 " min:%lukB"
1363 " low:%lukB"
1364 " high:%lukB"
1365 " active:%lukB"
1366 " inactive:%lukB"
1367 " present:%lukB"
1368 " pages_scanned:%lu"
1369 " all_unreclaimable? %s"
1370 "\n",
1371 zone->name,
1372 K(zone->free_pages),
1373 K(zone->pages_min),
1374 K(zone->pages_low),
1375 K(zone->pages_high),
1376 K(zone->nr_active),
1377 K(zone->nr_inactive),
1378 K(zone->present_pages),
1379 zone->pages_scanned,
1380 (zone->all_unreclaimable ? "yes" : "no")
1382 printk("lowmem_reserve[]:");
1383 for (i = 0; i < MAX_NR_ZONES; i++)
1384 printk(" %lu", zone->lowmem_reserve[i]);
1385 printk("\n");
1388 for_each_zone(zone) {
1389 unsigned long nr[MAX_ORDER], flags, order, total = 0;
1391 if (!populated_zone(zone))
1392 continue;
1394 show_node(zone);
1395 printk("%s: ", zone->name);
1397 spin_lock_irqsave(&zone->lock, flags);
1398 for (order = 0; order < MAX_ORDER; order++) {
1399 nr[order] = zone->free_area[order].nr_free;
1400 total += nr[order] << order;
1402 spin_unlock_irqrestore(&zone->lock, flags);
1403 for (order = 0; order < MAX_ORDER; order++)
1404 printk("%lu*%lukB ", nr[order], K(1UL) << order);
1405 printk("= %lukB\n", K(total));
1408 show_swap_cache_info();
1412 * Builds allocation fallback zone lists.
1414 * Add all populated zones of a node to the zonelist.
1416 static int __meminit build_zonelists_node(pg_data_t *pgdat,
1417 struct zonelist *zonelist, int nr_zones, enum zone_type zone_type)
1419 struct zone *zone;
1421 BUG_ON(zone_type >= MAX_NR_ZONES);
1422 zone_type++;
1424 do {
1425 zone_type--;
1426 zone = pgdat->node_zones + zone_type;
1427 if (populated_zone(zone)) {
1428 zonelist->zones[nr_zones++] = zone;
1429 check_highest_zone(zone_type);
1432 } while (zone_type);
1433 return nr_zones;
1436 #ifdef CONFIG_NUMA
1437 #define MAX_NODE_LOAD (num_online_nodes())
1438 static int __meminitdata node_load[MAX_NUMNODES];
1440 * find_next_best_node - find the next node that should appear in a given node's fallback list
1441 * @node: node whose fallback list we're appending
1442 * @used_node_mask: nodemask_t of already used nodes
1444 * We use a number of factors to determine which is the next node that should
1445 * appear on a given node's fallback list. The node should not have appeared
1446 * already in @node's fallback list, and it should be the next closest node
1447 * according to the distance array (which contains arbitrary distance values
1448 * from each node to each node in the system), and should also prefer nodes
1449 * with no CPUs, since presumably they'll have very little allocation pressure
1450 * on them otherwise.
1451 * It returns -1 if no node is found.
1453 static int __meminit find_next_best_node(int node, nodemask_t *used_node_mask)
1455 int n, val;
1456 int min_val = INT_MAX;
1457 int best_node = -1;
1459 /* Use the local node if we haven't already */
1460 if (!node_isset(node, *used_node_mask)) {
1461 node_set(node, *used_node_mask);
1462 return node;
1465 for_each_online_node(n) {
1466 cpumask_t tmp;
1468 /* Don't want a node to appear more than once */
1469 if (node_isset(n, *used_node_mask))
1470 continue;
1472 /* Use the distance array to find the distance */
1473 val = node_distance(node, n);
1475 /* Penalize nodes under us ("prefer the next node") */
1476 val += (n < node);
1478 /* Give preference to headless and unused nodes */
1479 tmp = node_to_cpumask(n);
1480 if (!cpus_empty(tmp))
1481 val += PENALTY_FOR_NODE_WITH_CPUS;
1483 /* Slight preference for less loaded node */
1484 val *= (MAX_NODE_LOAD*MAX_NUMNODES);
1485 val += node_load[n];
1487 if (val < min_val) {
1488 min_val = val;
1489 best_node = n;
1493 if (best_node >= 0)
1494 node_set(best_node, *used_node_mask);
1496 return best_node;
1499 static void __meminit build_zonelists(pg_data_t *pgdat)
1501 int j, node, local_node;
1502 enum zone_type i;
1503 int prev_node, load;
1504 struct zonelist *zonelist;
1505 nodemask_t used_mask;
1507 /* initialize zonelists */
1508 for (i = 0; i < MAX_NR_ZONES; i++) {
1509 zonelist = pgdat->node_zonelists + i;
1510 zonelist->zones[0] = NULL;
1513 /* NUMA-aware ordering of nodes */
1514 local_node = pgdat->node_id;
1515 load = num_online_nodes();
1516 prev_node = local_node;
1517 nodes_clear(used_mask);
1518 while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
1519 int distance = node_distance(local_node, node);
1522 * If another node is sufficiently far away then it is better
1523 * to reclaim pages in a zone before going off node.
1525 if (distance > RECLAIM_DISTANCE)
1526 zone_reclaim_mode = 1;
1529 * We don't want to pressure a particular node.
1530 * So adding penalty to the first node in same
1531 * distance group to make it round-robin.
1534 if (distance != node_distance(local_node, prev_node))
1535 node_load[node] += load;
1536 prev_node = node;
1537 load--;
1538 for (i = 0; i < MAX_NR_ZONES; i++) {
1539 zonelist = pgdat->node_zonelists + i;
1540 for (j = 0; zonelist->zones[j] != NULL; j++);
1542 j = build_zonelists_node(NODE_DATA(node), zonelist, j, i);
1543 zonelist->zones[j] = NULL;
1548 #else /* CONFIG_NUMA */
1550 static void __meminit build_zonelists(pg_data_t *pgdat)
1552 int node, local_node;
1553 enum zone_type i,j;
1555 local_node = pgdat->node_id;
1556 for (i = 0; i < MAX_NR_ZONES; i++) {
1557 struct zonelist *zonelist;
1559 zonelist = pgdat->node_zonelists + i;
1561 j = build_zonelists_node(pgdat, zonelist, 0, i);
1563 * Now we build the zonelist so that it contains the zones
1564 * of all the other nodes.
1565 * We don't want to pressure a particular node, so when
1566 * building the zones for node N, we make sure that the
1567 * zones coming right after the local ones are those from
1568 * node N+1 (modulo N)
1570 for (node = local_node + 1; node < MAX_NUMNODES; node++) {
1571 if (!node_online(node))
1572 continue;
1573 j = build_zonelists_node(NODE_DATA(node), zonelist, j, i);
1575 for (node = 0; node < local_node; node++) {
1576 if (!node_online(node))
1577 continue;
1578 j = build_zonelists_node(NODE_DATA(node), zonelist, j, i);
1581 zonelist->zones[j] = NULL;
1585 #endif /* CONFIG_NUMA */
1587 /* return values int ....just for stop_machine_run() */
1588 static int __meminit __build_all_zonelists(void *dummy)
1590 int nid;
1591 for_each_online_node(nid)
1592 build_zonelists(NODE_DATA(nid));
1593 return 0;
1596 void __meminit build_all_zonelists(void)
1598 if (system_state == SYSTEM_BOOTING) {
1599 __build_all_zonelists(NULL);
1600 cpuset_init_current_mems_allowed();
1601 } else {
1602 /* we have to stop all cpus to guaranntee there is no user
1603 of zonelist */
1604 stop_machine_run(__build_all_zonelists, NULL, NR_CPUS);
1605 /* cpuset refresh routine should be here */
1607 vm_total_pages = nr_free_pagecache_pages();
1608 printk("Built %i zonelists. Total pages: %ld\n",
1609 num_online_nodes(), vm_total_pages);
1613 * Helper functions to size the waitqueue hash table.
1614 * Essentially these want to choose hash table sizes sufficiently
1615 * large so that collisions trying to wait on pages are rare.
1616 * But in fact, the number of active page waitqueues on typical
1617 * systems is ridiculously low, less than 200. So this is even
1618 * conservative, even though it seems large.
1620 * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
1621 * waitqueues, i.e. the size of the waitq table given the number of pages.
1623 #define PAGES_PER_WAITQUEUE 256
1625 #ifndef CONFIG_MEMORY_HOTPLUG
1626 static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
1628 unsigned long size = 1;
1630 pages /= PAGES_PER_WAITQUEUE;
1632 while (size < pages)
1633 size <<= 1;
1636 * Once we have dozens or even hundreds of threads sleeping
1637 * on IO we've got bigger problems than wait queue collision.
1638 * Limit the size of the wait table to a reasonable size.
1640 size = min(size, 4096UL);
1642 return max(size, 4UL);
1644 #else
1646 * A zone's size might be changed by hot-add, so it is not possible to determine
1647 * a suitable size for its wait_table. So we use the maximum size now.
1649 * The max wait table size = 4096 x sizeof(wait_queue_head_t). ie:
1651 * i386 (preemption config) : 4096 x 16 = 64Kbyte.
1652 * ia64, x86-64 (no preemption): 4096 x 20 = 80Kbyte.
1653 * ia64, x86-64 (preemption) : 4096 x 24 = 96Kbyte.
1655 * The maximum entries are prepared when a zone's memory is (512K + 256) pages
1656 * or more by the traditional way. (See above). It equals:
1658 * i386, x86-64, powerpc(4K page size) : = ( 2G + 1M)byte.
1659 * ia64(16K page size) : = ( 8G + 4M)byte.
1660 * powerpc (64K page size) : = (32G +16M)byte.
1662 static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
1664 return 4096UL;
1666 #endif
1669 * This is an integer logarithm so that shifts can be used later
1670 * to extract the more random high bits from the multiplicative
1671 * hash function before the remainder is taken.
1673 static inline unsigned long wait_table_bits(unsigned long size)
1675 return ffz(~size);
1678 #define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
1681 * Initially all pages are reserved - free ones are freed
1682 * up by free_all_bootmem() once the early boot process is
1683 * done. Non-atomic initialization, single-pass.
1685 void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
1686 unsigned long start_pfn)
1688 struct page *page;
1689 unsigned long end_pfn = start_pfn + size;
1690 unsigned long pfn;
1692 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
1693 if (!early_pfn_valid(pfn))
1694 continue;
1695 if (!early_pfn_in_nid(pfn, nid))
1696 continue;
1697 page = pfn_to_page(pfn);
1698 set_page_links(page, zone, nid, pfn);
1699 init_page_count(page);
1700 reset_page_mapcount(page);
1701 SetPageReserved(page);
1702 INIT_LIST_HEAD(&page->lru);
1703 #ifdef WANT_PAGE_VIRTUAL
1704 /* The shift won't overflow because ZONE_NORMAL is below 4G. */
1705 if (!is_highmem_idx(zone))
1706 set_page_address(page, __va(pfn << PAGE_SHIFT));
1707 #endif
1711 void zone_init_free_lists(struct pglist_data *pgdat, struct zone *zone,
1712 unsigned long size)
1714 int order;
1715 for (order = 0; order < MAX_ORDER ; order++) {
1716 INIT_LIST_HEAD(&zone->free_area[order].free_list);
1717 zone->free_area[order].nr_free = 0;
1721 #define ZONETABLE_INDEX(x, zone_nr) ((x << ZONES_SHIFT) | zone_nr)
1722 void zonetable_add(struct zone *zone, int nid, enum zone_type zid,
1723 unsigned long pfn, unsigned long size)
1725 unsigned long snum = pfn_to_section_nr(pfn);
1726 unsigned long end = pfn_to_section_nr(pfn + size);
1728 if (FLAGS_HAS_NODE)
1729 zone_table[ZONETABLE_INDEX(nid, zid)] = zone;
1730 else
1731 for (; snum <= end; snum++)
1732 zone_table[ZONETABLE_INDEX(snum, zid)] = zone;
1735 #ifndef __HAVE_ARCH_MEMMAP_INIT
1736 #define memmap_init(size, nid, zone, start_pfn) \
1737 memmap_init_zone((size), (nid), (zone), (start_pfn))
1738 #endif
1740 static int __cpuinit zone_batchsize(struct zone *zone)
1742 int batch;
1745 * The per-cpu-pages pools are set to around 1000th of the
1746 * size of the zone. But no more than 1/2 of a meg.
1748 * OK, so we don't know how big the cache is. So guess.
1750 batch = zone->present_pages / 1024;
1751 if (batch * PAGE_SIZE > 512 * 1024)
1752 batch = (512 * 1024) / PAGE_SIZE;
1753 batch /= 4; /* We effectively *= 4 below */
1754 if (batch < 1)
1755 batch = 1;
1758 * Clamp the batch to a 2^n - 1 value. Having a power
1759 * of 2 value was found to be more likely to have
1760 * suboptimal cache aliasing properties in some cases.
1762 * For example if 2 tasks are alternately allocating
1763 * batches of pages, one task can end up with a lot
1764 * of pages of one half of the possible page colors
1765 * and the other with pages of the other colors.
1767 batch = (1 << (fls(batch + batch/2)-1)) - 1;
1769 return batch;
1772 inline void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
1774 struct per_cpu_pages *pcp;
1776 memset(p, 0, sizeof(*p));
1778 pcp = &p->pcp[0]; /* hot */
1779 pcp->count = 0;
1780 pcp->high = 6 * batch;
1781 pcp->batch = max(1UL, 1 * batch);
1782 INIT_LIST_HEAD(&pcp->list);
1784 pcp = &p->pcp[1]; /* cold*/
1785 pcp->count = 0;
1786 pcp->high = 2 * batch;
1787 pcp->batch = max(1UL, batch/2);
1788 INIT_LIST_HEAD(&pcp->list);
1792 * setup_pagelist_highmark() sets the high water mark for hot per_cpu_pagelist
1793 * to the value high for the pageset p.
1796 static void setup_pagelist_highmark(struct per_cpu_pageset *p,
1797 unsigned long high)
1799 struct per_cpu_pages *pcp;
1801 pcp = &p->pcp[0]; /* hot list */
1802 pcp->high = high;
1803 pcp->batch = max(1UL, high/4);
1804 if ((high/4) > (PAGE_SHIFT * 8))
1805 pcp->batch = PAGE_SHIFT * 8;
1809 #ifdef CONFIG_NUMA
1811 * Boot pageset table. One per cpu which is going to be used for all
1812 * zones and all nodes. The parameters will be set in such a way
1813 * that an item put on a list will immediately be handed over to
1814 * the buddy list. This is safe since pageset manipulation is done
1815 * with interrupts disabled.
1817 * Some NUMA counter updates may also be caught by the boot pagesets.
1819 * The boot_pagesets must be kept even after bootup is complete for
1820 * unused processors and/or zones. They do play a role for bootstrapping
1821 * hotplugged processors.
1823 * zoneinfo_show() and maybe other functions do
1824 * not check if the processor is online before following the pageset pointer.
1825 * Other parts of the kernel may not check if the zone is available.
1827 static struct per_cpu_pageset boot_pageset[NR_CPUS];
1830 * Dynamically allocate memory for the
1831 * per cpu pageset array in struct zone.
1833 static int __cpuinit process_zones(int cpu)
1835 struct zone *zone, *dzone;
1837 for_each_zone(zone) {
1839 if (!populated_zone(zone))
1840 continue;
1842 zone_pcp(zone, cpu) = kmalloc_node(sizeof(struct per_cpu_pageset),
1843 GFP_KERNEL, cpu_to_node(cpu));
1844 if (!zone_pcp(zone, cpu))
1845 goto bad;
1847 setup_pageset(zone_pcp(zone, cpu), zone_batchsize(zone));
1849 if (percpu_pagelist_fraction)
1850 setup_pagelist_highmark(zone_pcp(zone, cpu),
1851 (zone->present_pages / percpu_pagelist_fraction));
1854 return 0;
1855 bad:
1856 for_each_zone(dzone) {
1857 if (dzone == zone)
1858 break;
1859 kfree(zone_pcp(dzone, cpu));
1860 zone_pcp(dzone, cpu) = NULL;
1862 return -ENOMEM;
1865 static inline void free_zone_pagesets(int cpu)
1867 struct zone *zone;
1869 for_each_zone(zone) {
1870 struct per_cpu_pageset *pset = zone_pcp(zone, cpu);
1872 /* Free per_cpu_pageset if it is slab allocated */
1873 if (pset != &boot_pageset[cpu])
1874 kfree(pset);
1875 zone_pcp(zone, cpu) = NULL;
1879 static int __cpuinit pageset_cpuup_callback(struct notifier_block *nfb,
1880 unsigned long action,
1881 void *hcpu)
1883 int cpu = (long)hcpu;
1884 int ret = NOTIFY_OK;
1886 switch (action) {
1887 case CPU_UP_PREPARE:
1888 if (process_zones(cpu))
1889 ret = NOTIFY_BAD;
1890 break;
1891 case CPU_UP_CANCELED:
1892 case CPU_DEAD:
1893 free_zone_pagesets(cpu);
1894 break;
1895 default:
1896 break;
1898 return ret;
1901 static struct notifier_block __cpuinitdata pageset_notifier =
1902 { &pageset_cpuup_callback, NULL, 0 };
1904 void __init setup_per_cpu_pageset(void)
1906 int err;
1908 /* Initialize per_cpu_pageset for cpu 0.
1909 * A cpuup callback will do this for every cpu
1910 * as it comes online
1912 err = process_zones(smp_processor_id());
1913 BUG_ON(err);
1914 register_cpu_notifier(&pageset_notifier);
1917 #endif
1919 static __meminit
1920 int zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages)
1922 int i;
1923 struct pglist_data *pgdat = zone->zone_pgdat;
1924 size_t alloc_size;
1927 * The per-page waitqueue mechanism uses hashed waitqueues
1928 * per zone.
1930 zone->wait_table_hash_nr_entries =
1931 wait_table_hash_nr_entries(zone_size_pages);
1932 zone->wait_table_bits =
1933 wait_table_bits(zone->wait_table_hash_nr_entries);
1934 alloc_size = zone->wait_table_hash_nr_entries
1935 * sizeof(wait_queue_head_t);
1937 if (system_state == SYSTEM_BOOTING) {
1938 zone->wait_table = (wait_queue_head_t *)
1939 alloc_bootmem_node(pgdat, alloc_size);
1940 } else {
1942 * This case means that a zone whose size was 0 gets new memory
1943 * via memory hot-add.
1944 * But it may be the case that a new node was hot-added. In
1945 * this case vmalloc() will not be able to use this new node's
1946 * memory - this wait_table must be initialized to use this new
1947 * node itself as well.
1948 * To use this new node's memory, further consideration will be
1949 * necessary.
1951 zone->wait_table = (wait_queue_head_t *)vmalloc(alloc_size);
1953 if (!zone->wait_table)
1954 return -ENOMEM;
1956 for(i = 0; i < zone->wait_table_hash_nr_entries; ++i)
1957 init_waitqueue_head(zone->wait_table + i);
1959 return 0;
1962 static __meminit void zone_pcp_init(struct zone *zone)
1964 int cpu;
1965 unsigned long batch = zone_batchsize(zone);
1967 for (cpu = 0; cpu < NR_CPUS; cpu++) {
1968 #ifdef CONFIG_NUMA
1969 /* Early boot. Slab allocator not functional yet */
1970 zone_pcp(zone, cpu) = &boot_pageset[cpu];
1971 setup_pageset(&boot_pageset[cpu],0);
1972 #else
1973 setup_pageset(zone_pcp(zone,cpu), batch);
1974 #endif
1976 if (zone->present_pages)
1977 printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%lu\n",
1978 zone->name, zone->present_pages, batch);
1981 __meminit int init_currently_empty_zone(struct zone *zone,
1982 unsigned long zone_start_pfn,
1983 unsigned long size)
1985 struct pglist_data *pgdat = zone->zone_pgdat;
1986 int ret;
1987 ret = zone_wait_table_init(zone, size);
1988 if (ret)
1989 return ret;
1990 pgdat->nr_zones = zone_idx(zone) + 1;
1992 zone->zone_start_pfn = zone_start_pfn;
1994 memmap_init(size, pgdat->node_id, zone_idx(zone), zone_start_pfn);
1996 zone_init_free_lists(pgdat, zone, zone->spanned_pages);
1998 return 0;
2001 #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
2003 * Basic iterator support. Return the first range of PFNs for a node
2004 * Note: nid == MAX_NUMNODES returns first region regardless of node
2006 static int __init first_active_region_index_in_nid(int nid)
2008 int i;
2010 for (i = 0; i < nr_nodemap_entries; i++)
2011 if (nid == MAX_NUMNODES || early_node_map[i].nid == nid)
2012 return i;
2014 return -1;
2018 * Basic iterator support. Return the next active range of PFNs for a node
2019 * Note: nid == MAX_NUMNODES returns next region regardles of node
2021 static int __init next_active_region_index_in_nid(int index, int nid)
2023 for (index = index + 1; index < nr_nodemap_entries; index++)
2024 if (nid == MAX_NUMNODES || early_node_map[index].nid == nid)
2025 return index;
2027 return -1;
2030 #ifndef CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID
2032 * Required by SPARSEMEM. Given a PFN, return what node the PFN is on.
2033 * Architectures may implement their own version but if add_active_range()
2034 * was used and there are no special requirements, this is a convenient
2035 * alternative
2037 int __init early_pfn_to_nid(unsigned long pfn)
2039 int i;
2041 for (i = 0; i < nr_nodemap_entries; i++) {
2042 unsigned long start_pfn = early_node_map[i].start_pfn;
2043 unsigned long end_pfn = early_node_map[i].end_pfn;
2045 if (start_pfn <= pfn && pfn < end_pfn)
2046 return early_node_map[i].nid;
2049 return 0;
2051 #endif /* CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID */
2053 /* Basic iterator support to walk early_node_map[] */
2054 #define for_each_active_range_index_in_nid(i, nid) \
2055 for (i = first_active_region_index_in_nid(nid); i != -1; \
2056 i = next_active_region_index_in_nid(i, nid))
2059 * free_bootmem_with_active_regions - Call free_bootmem_node for each active range
2060 * @nid: The node to free memory on. If MAX_NUMNODES, all nodes are freed.
2061 * @max_low_pfn: The highest PFN that will be passed to free_bootmem_node
2063 * If an architecture guarantees that all ranges registered with
2064 * add_active_ranges() contain no holes and may be freed, this
2065 * this function may be used instead of calling free_bootmem() manually.
2067 void __init free_bootmem_with_active_regions(int nid,
2068 unsigned long max_low_pfn)
2070 int i;
2072 for_each_active_range_index_in_nid(i, nid) {
2073 unsigned long size_pages = 0;
2074 unsigned long end_pfn = early_node_map[i].end_pfn;
2076 if (early_node_map[i].start_pfn >= max_low_pfn)
2077 continue;
2079 if (end_pfn > max_low_pfn)
2080 end_pfn = max_low_pfn;
2082 size_pages = end_pfn - early_node_map[i].start_pfn;
2083 free_bootmem_node(NODE_DATA(early_node_map[i].nid),
2084 PFN_PHYS(early_node_map[i].start_pfn),
2085 size_pages << PAGE_SHIFT);
2090 * sparse_memory_present_with_active_regions - Call memory_present for each active range
2091 * @nid: The node to call memory_present for. If MAX_NUMNODES, all nodes will be used.
2093 * If an architecture guarantees that all ranges registered with
2094 * add_active_ranges() contain no holes and may be freed, this
2095 * function may be used instead of calling memory_present() manually.
2097 void __init sparse_memory_present_with_active_regions(int nid)
2099 int i;
2101 for_each_active_range_index_in_nid(i, nid)
2102 memory_present(early_node_map[i].nid,
2103 early_node_map[i].start_pfn,
2104 early_node_map[i].end_pfn);
2108 * push_node_boundaries - Push node boundaries to at least the requested boundary
2109 * @nid: The nid of the node to push the boundary for
2110 * @start_pfn: The start pfn of the node
2111 * @end_pfn: The end pfn of the node
2113 * In reserve-based hot-add, mem_map is allocated that is unused until hotadd
2114 * time. Specifically, on x86_64, SRAT will report ranges that can potentially
2115 * be hotplugged even though no physical memory exists. This function allows
2116 * an arch to push out the node boundaries so mem_map is allocated that can
2117 * be used later.
2119 #ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
2120 void __init push_node_boundaries(unsigned int nid,
2121 unsigned long start_pfn, unsigned long end_pfn)
2123 printk(KERN_DEBUG "Entering push_node_boundaries(%u, %lu, %lu)\n",
2124 nid, start_pfn, end_pfn);
2126 /* Initialise the boundary for this node if necessary */
2127 if (node_boundary_end_pfn[nid] == 0)
2128 node_boundary_start_pfn[nid] = -1UL;
2130 /* Update the boundaries */
2131 if (node_boundary_start_pfn[nid] > start_pfn)
2132 node_boundary_start_pfn[nid] = start_pfn;
2133 if (node_boundary_end_pfn[nid] < end_pfn)
2134 node_boundary_end_pfn[nid] = end_pfn;
2137 /* If necessary, push the node boundary out for reserve hotadd */
2138 static void __init account_node_boundary(unsigned int nid,
2139 unsigned long *start_pfn, unsigned long *end_pfn)
2141 printk(KERN_DEBUG "Entering account_node_boundary(%u, %lu, %lu)\n",
2142 nid, *start_pfn, *end_pfn);
2144 /* Return if boundary information has not been provided */
2145 if (node_boundary_end_pfn[nid] == 0)
2146 return;
2148 /* Check the boundaries and update if necessary */
2149 if (node_boundary_start_pfn[nid] < *start_pfn)
2150 *start_pfn = node_boundary_start_pfn[nid];
2151 if (node_boundary_end_pfn[nid] > *end_pfn)
2152 *end_pfn = node_boundary_end_pfn[nid];
2154 #else
2155 void __init push_node_boundaries(unsigned int nid,
2156 unsigned long start_pfn, unsigned long end_pfn) {}
2158 static void __init account_node_boundary(unsigned int nid,
2159 unsigned long *start_pfn, unsigned long *end_pfn) {}
2160 #endif
2164 * get_pfn_range_for_nid - Return the start and end page frames for a node
2165 * @nid: The nid to return the range for. If MAX_NUMNODES, the min and max PFN are returned.
2166 * @start_pfn: Passed by reference. On return, it will have the node start_pfn.
2167 * @end_pfn: Passed by reference. On return, it will have the node end_pfn.
2169 * It returns the start and end page frame of a node based on information
2170 * provided by an arch calling add_active_range(). If called for a node
2171 * with no available memory, a warning is printed and the start and end
2172 * PFNs will be 0.
2174 void __init get_pfn_range_for_nid(unsigned int nid,
2175 unsigned long *start_pfn, unsigned long *end_pfn)
2177 int i;
2178 *start_pfn = -1UL;
2179 *end_pfn = 0;
2181 for_each_active_range_index_in_nid(i, nid) {
2182 *start_pfn = min(*start_pfn, early_node_map[i].start_pfn);
2183 *end_pfn = max(*end_pfn, early_node_map[i].end_pfn);
2186 if (*start_pfn == -1UL) {
2187 printk(KERN_WARNING "Node %u active with no memory\n", nid);
2188 *start_pfn = 0;
2191 /* Push the node boundaries out if requested */
2192 account_node_boundary(nid, start_pfn, end_pfn);
2196 * Return the number of pages a zone spans in a node, including holes
2197 * present_pages = zone_spanned_pages_in_node() - zone_absent_pages_in_node()
2199 unsigned long __init zone_spanned_pages_in_node(int nid,
2200 unsigned long zone_type,
2201 unsigned long *ignored)
2203 unsigned long node_start_pfn, node_end_pfn;
2204 unsigned long zone_start_pfn, zone_end_pfn;
2206 /* Get the start and end of the node and zone */
2207 get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
2208 zone_start_pfn = arch_zone_lowest_possible_pfn[zone_type];
2209 zone_end_pfn = arch_zone_highest_possible_pfn[zone_type];
2211 /* Check that this node has pages within the zone's required range */
2212 if (zone_end_pfn < node_start_pfn || zone_start_pfn > node_end_pfn)
2213 return 0;
2215 /* Move the zone boundaries inside the node if necessary */
2216 zone_end_pfn = min(zone_end_pfn, node_end_pfn);
2217 zone_start_pfn = max(zone_start_pfn, node_start_pfn);
2219 /* Return the spanned pages */
2220 return zone_end_pfn - zone_start_pfn;
2224 * Return the number of holes in a range on a node. If nid is MAX_NUMNODES,
2225 * then all holes in the requested range will be accounted for.
2227 unsigned long __init __absent_pages_in_range(int nid,
2228 unsigned long range_start_pfn,
2229 unsigned long range_end_pfn)
2231 int i = 0;
2232 unsigned long prev_end_pfn = 0, hole_pages = 0;
2233 unsigned long start_pfn;
2235 /* Find the end_pfn of the first active range of pfns in the node */
2236 i = first_active_region_index_in_nid(nid);
2237 if (i == -1)
2238 return 0;
2240 /* Account for ranges before physical memory on this node */
2241 if (early_node_map[i].start_pfn > range_start_pfn)
2242 hole_pages = early_node_map[i].start_pfn - range_start_pfn;
2244 prev_end_pfn = early_node_map[i].start_pfn;
2246 /* Find all holes for the zone within the node */
2247 for (; i != -1; i = next_active_region_index_in_nid(i, nid)) {
2249 /* No need to continue if prev_end_pfn is outside the zone */
2250 if (prev_end_pfn >= range_end_pfn)
2251 break;
2253 /* Make sure the end of the zone is not within the hole */
2254 start_pfn = min(early_node_map[i].start_pfn, range_end_pfn);
2255 prev_end_pfn = max(prev_end_pfn, range_start_pfn);
2257 /* Update the hole size cound and move on */
2258 if (start_pfn > range_start_pfn) {
2259 BUG_ON(prev_end_pfn > start_pfn);
2260 hole_pages += start_pfn - prev_end_pfn;
2262 prev_end_pfn = early_node_map[i].end_pfn;
2265 /* Account for ranges past physical memory on this node */
2266 if (range_end_pfn > prev_end_pfn)
2267 hole_pages += range_end_pfn -
2268 max(range_start_pfn, prev_end_pfn);
2270 return hole_pages;
2274 * absent_pages_in_range - Return number of page frames in holes within a range
2275 * @start_pfn: The start PFN to start searching for holes
2276 * @end_pfn: The end PFN to stop searching for holes
2278 * It returns the number of pages frames in memory holes within a range.
2280 unsigned long __init absent_pages_in_range(unsigned long start_pfn,
2281 unsigned long end_pfn)
2283 return __absent_pages_in_range(MAX_NUMNODES, start_pfn, end_pfn);
2286 /* Return the number of page frames in holes in a zone on a node */
2287 unsigned long __init zone_absent_pages_in_node(int nid,
2288 unsigned long zone_type,
2289 unsigned long *ignored)
2291 unsigned long node_start_pfn, node_end_pfn;
2292 unsigned long zone_start_pfn, zone_end_pfn;
2294 get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
2295 zone_start_pfn = max(arch_zone_lowest_possible_pfn[zone_type],
2296 node_start_pfn);
2297 zone_end_pfn = min(arch_zone_highest_possible_pfn[zone_type],
2298 node_end_pfn);
2300 return __absent_pages_in_range(nid, zone_start_pfn, zone_end_pfn);
2303 #else
2304 static inline unsigned long zone_spanned_pages_in_node(int nid,
2305 unsigned long zone_type,
2306 unsigned long *zones_size)
2308 return zones_size[zone_type];
2311 static inline unsigned long zone_absent_pages_in_node(int nid,
2312 unsigned long zone_type,
2313 unsigned long *zholes_size)
2315 if (!zholes_size)
2316 return 0;
2318 return zholes_size[zone_type];
2321 #endif
2323 static void __init calculate_node_totalpages(struct pglist_data *pgdat,
2324 unsigned long *zones_size, unsigned long *zholes_size)
2326 unsigned long realtotalpages, totalpages = 0;
2327 enum zone_type i;
2329 for (i = 0; i < MAX_NR_ZONES; i++)
2330 totalpages += zone_spanned_pages_in_node(pgdat->node_id, i,
2331 zones_size);
2332 pgdat->node_spanned_pages = totalpages;
2334 realtotalpages = totalpages;
2335 for (i = 0; i < MAX_NR_ZONES; i++)
2336 realtotalpages -=
2337 zone_absent_pages_in_node(pgdat->node_id, i,
2338 zholes_size);
2339 pgdat->node_present_pages = realtotalpages;
2340 printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id,
2341 realtotalpages);
2345 * Set up the zone data structures:
2346 * - mark all pages reserved
2347 * - mark all memory queues empty
2348 * - clear the memory bitmaps
2350 static void __meminit free_area_init_core(struct pglist_data *pgdat,
2351 unsigned long *zones_size, unsigned long *zholes_size)
2353 enum zone_type j;
2354 int nid = pgdat->node_id;
2355 unsigned long zone_start_pfn = pgdat->node_start_pfn;
2356 int ret;
2358 pgdat_resize_init(pgdat);
2359 pgdat->nr_zones = 0;
2360 init_waitqueue_head(&pgdat->kswapd_wait);
2361 pgdat->kswapd_max_order = 0;
2363 for (j = 0; j < MAX_NR_ZONES; j++) {
2364 struct zone *zone = pgdat->node_zones + j;
2365 unsigned long size, realsize, memmap_pages;
2367 size = zone_spanned_pages_in_node(nid, j, zones_size);
2368 realsize = size - zone_absent_pages_in_node(nid, j,
2369 zholes_size);
2372 * Adjust realsize so that it accounts for how much memory
2373 * is used by this zone for memmap. This affects the watermark
2374 * and per-cpu initialisations
2376 memmap_pages = (size * sizeof(struct page)) >> PAGE_SHIFT;
2377 if (realsize >= memmap_pages) {
2378 realsize -= memmap_pages;
2379 printk(KERN_DEBUG
2380 " %s zone: %lu pages used for memmap\n",
2381 zone_names[j], memmap_pages);
2382 } else
2383 printk(KERN_WARNING
2384 " %s zone: %lu pages exceeds realsize %lu\n",
2385 zone_names[j], memmap_pages, realsize);
2387 /* Account for reserved DMA pages */
2388 if (j == ZONE_DMA && realsize > dma_reserve) {
2389 realsize -= dma_reserve;
2390 printk(KERN_DEBUG " DMA zone: %lu pages reserved\n",
2391 dma_reserve);
2394 if (!is_highmem_idx(j))
2395 nr_kernel_pages += realsize;
2396 nr_all_pages += realsize;
2398 zone->spanned_pages = size;
2399 zone->present_pages = realsize;
2400 #ifdef CONFIG_NUMA
2401 zone->node = nid;
2402 zone->min_unmapped_pages = (realsize*sysctl_min_unmapped_ratio)
2403 / 100;
2404 zone->min_slab_pages = (realsize * sysctl_min_slab_ratio) / 100;
2405 #endif
2406 zone->name = zone_names[j];
2407 spin_lock_init(&zone->lock);
2408 spin_lock_init(&zone->lru_lock);
2409 zone_seqlock_init(zone);
2410 zone->zone_pgdat = pgdat;
2411 zone->free_pages = 0;
2413 zone->prev_priority = DEF_PRIORITY;
2415 zone_pcp_init(zone);
2416 INIT_LIST_HEAD(&zone->active_list);
2417 INIT_LIST_HEAD(&zone->inactive_list);
2418 zone->nr_scan_active = 0;
2419 zone->nr_scan_inactive = 0;
2420 zone->nr_active = 0;
2421 zone->nr_inactive = 0;
2422 zap_zone_vm_stats(zone);
2423 atomic_set(&zone->reclaim_in_progress, 0);
2424 if (!size)
2425 continue;
2427 zonetable_add(zone, nid, j, zone_start_pfn, size);
2428 ret = init_currently_empty_zone(zone, zone_start_pfn, size);
2429 BUG_ON(ret);
2430 zone_start_pfn += size;
2434 static void __init alloc_node_mem_map(struct pglist_data *pgdat)
2436 /* Skip empty nodes */
2437 if (!pgdat->node_spanned_pages)
2438 return;
2440 #ifdef CONFIG_FLAT_NODE_MEM_MAP
2441 /* ia64 gets its own node_mem_map, before this, without bootmem */
2442 if (!pgdat->node_mem_map) {
2443 unsigned long size, start, end;
2444 struct page *map;
2447 * The zone's endpoints aren't required to be MAX_ORDER
2448 * aligned but the node_mem_map endpoints must be in order
2449 * for the buddy allocator to function correctly.
2451 start = pgdat->node_start_pfn & ~(MAX_ORDER_NR_PAGES - 1);
2452 end = pgdat->node_start_pfn + pgdat->node_spanned_pages;
2453 end = ALIGN(end, MAX_ORDER_NR_PAGES);
2454 size = (end - start) * sizeof(struct page);
2455 map = alloc_remap(pgdat->node_id, size);
2456 if (!map)
2457 map = alloc_bootmem_node(pgdat, size);
2458 pgdat->node_mem_map = map + (pgdat->node_start_pfn - start);
2460 #ifdef CONFIG_FLATMEM
2462 * With no DISCONTIG, the global mem_map is just set as node 0's
2464 if (pgdat == NODE_DATA(0)) {
2465 mem_map = NODE_DATA(0)->node_mem_map;
2466 #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
2467 if (page_to_pfn(mem_map) != pgdat->node_start_pfn)
2468 mem_map -= pgdat->node_start_pfn;
2469 #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
2471 #endif
2472 #endif /* CONFIG_FLAT_NODE_MEM_MAP */
2475 void __meminit free_area_init_node(int nid, struct pglist_data *pgdat,
2476 unsigned long *zones_size, unsigned long node_start_pfn,
2477 unsigned long *zholes_size)
2479 pgdat->node_id = nid;
2480 pgdat->node_start_pfn = node_start_pfn;
2481 calculate_node_totalpages(pgdat, zones_size, zholes_size);
2483 alloc_node_mem_map(pgdat);
2485 free_area_init_core(pgdat, zones_size, zholes_size);
2488 #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
2490 * add_active_range - Register a range of PFNs backed by physical memory
2491 * @nid: The node ID the range resides on
2492 * @start_pfn: The start PFN of the available physical memory
2493 * @end_pfn: The end PFN of the available physical memory
2495 * These ranges are stored in an early_node_map[] and later used by
2496 * free_area_init_nodes() to calculate zone sizes and holes. If the
2497 * range spans a memory hole, it is up to the architecture to ensure
2498 * the memory is not freed by the bootmem allocator. If possible
2499 * the range being registered will be merged with existing ranges.
2501 void __init add_active_range(unsigned int nid, unsigned long start_pfn,
2502 unsigned long end_pfn)
2504 int i;
2506 printk(KERN_DEBUG "Entering add_active_range(%d, %lu, %lu) "
2507 "%d entries of %d used\n",
2508 nid, start_pfn, end_pfn,
2509 nr_nodemap_entries, MAX_ACTIVE_REGIONS);
2511 /* Merge with existing active regions if possible */
2512 for (i = 0; i < nr_nodemap_entries; i++) {
2513 if (early_node_map[i].nid != nid)
2514 continue;
2516 /* Skip if an existing region covers this new one */
2517 if (start_pfn >= early_node_map[i].start_pfn &&
2518 end_pfn <= early_node_map[i].end_pfn)
2519 return;
2521 /* Merge forward if suitable */
2522 if (start_pfn <= early_node_map[i].end_pfn &&
2523 end_pfn > early_node_map[i].end_pfn) {
2524 early_node_map[i].end_pfn = end_pfn;
2525 return;
2528 /* Merge backward if suitable */
2529 if (start_pfn < early_node_map[i].end_pfn &&
2530 end_pfn >= early_node_map[i].start_pfn) {
2531 early_node_map[i].start_pfn = start_pfn;
2532 return;
2536 /* Check that early_node_map is large enough */
2537 if (i >= MAX_ACTIVE_REGIONS) {
2538 printk(KERN_CRIT "More than %d memory regions, truncating\n",
2539 MAX_ACTIVE_REGIONS);
2540 return;
2543 early_node_map[i].nid = nid;
2544 early_node_map[i].start_pfn = start_pfn;
2545 early_node_map[i].end_pfn = end_pfn;
2546 nr_nodemap_entries = i + 1;
2550 * shrink_active_range - Shrink an existing registered range of PFNs
2551 * @nid: The node id the range is on that should be shrunk
2552 * @old_end_pfn: The old end PFN of the range
2553 * @new_end_pfn: The new PFN of the range
2555 * i386 with NUMA use alloc_remap() to store a node_mem_map on a local node.
2556 * The map is kept at the end physical page range that has already been
2557 * registered with add_active_range(). This function allows an arch to shrink
2558 * an existing registered range.
2560 void __init shrink_active_range(unsigned int nid, unsigned long old_end_pfn,
2561 unsigned long new_end_pfn)
2563 int i;
2565 /* Find the old active region end and shrink */
2566 for_each_active_range_index_in_nid(i, nid)
2567 if (early_node_map[i].end_pfn == old_end_pfn) {
2568 early_node_map[i].end_pfn = new_end_pfn;
2569 break;
2574 * remove_all_active_ranges - Remove all currently registered regions
2576 * During discovery, it may be found that a table like SRAT is invalid
2577 * and an alternative discovery method must be used. This function removes
2578 * all currently registered regions.
2580 void __init remove_all_active_ranges(void)
2582 memset(early_node_map, 0, sizeof(early_node_map));
2583 nr_nodemap_entries = 0;
2584 #ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
2585 memset(node_boundary_start_pfn, 0, sizeof(node_boundary_start_pfn));
2586 memset(node_boundary_end_pfn, 0, sizeof(node_boundary_end_pfn));
2587 #endif /* CONFIG_MEMORY_HOTPLUG_RESERVE */
2590 /* Compare two active node_active_regions */
2591 static int __init cmp_node_active_region(const void *a, const void *b)
2593 struct node_active_region *arange = (struct node_active_region *)a;
2594 struct node_active_region *brange = (struct node_active_region *)b;
2596 /* Done this way to avoid overflows */
2597 if (arange->start_pfn > brange->start_pfn)
2598 return 1;
2599 if (arange->start_pfn < brange->start_pfn)
2600 return -1;
2602 return 0;
2605 /* sort the node_map by start_pfn */
2606 static void __init sort_node_map(void)
2608 sort(early_node_map, (size_t)nr_nodemap_entries,
2609 sizeof(struct node_active_region),
2610 cmp_node_active_region, NULL);
2613 /* Find the lowest pfn for a node. This depends on a sorted early_node_map */
2614 unsigned long __init find_min_pfn_for_node(unsigned long nid)
2616 int i;
2618 /* Regions in the early_node_map can be in any order */
2619 sort_node_map();
2621 /* Assuming a sorted map, the first range found has the starting pfn */
2622 for_each_active_range_index_in_nid(i, nid)
2623 return early_node_map[i].start_pfn;
2625 printk(KERN_WARNING "Could not find start_pfn for node %lu\n", nid);
2626 return 0;
2630 * find_min_pfn_with_active_regions - Find the minimum PFN registered
2632 * It returns the minimum PFN based on information provided via
2633 * add_active_range().
2635 unsigned long __init find_min_pfn_with_active_regions(void)
2637 return find_min_pfn_for_node(MAX_NUMNODES);
2641 * find_max_pfn_with_active_regions - Find the maximum PFN registered
2643 * It returns the maximum PFN based on information provided via
2644 * add_active_range().
2646 unsigned long __init find_max_pfn_with_active_regions(void)
2648 int i;
2649 unsigned long max_pfn = 0;
2651 for (i = 0; i < nr_nodemap_entries; i++)
2652 max_pfn = max(max_pfn, early_node_map[i].end_pfn);
2654 return max_pfn;
2658 * free_area_init_nodes - Initialise all pg_data_t and zone data
2659 * @max_zone_pfn: an array of max PFNs for each zone
2661 * This will call free_area_init_node() for each active node in the system.
2662 * Using the page ranges provided by add_active_range(), the size of each
2663 * zone in each node and their holes is calculated. If the maximum PFN
2664 * between two adjacent zones match, it is assumed that the zone is empty.
2665 * For example, if arch_max_dma_pfn == arch_max_dma32_pfn, it is assumed
2666 * that arch_max_dma32_pfn has no pages. It is also assumed that a zone
2667 * starts where the previous one ended. For example, ZONE_DMA32 starts
2668 * at arch_max_dma_pfn.
2670 void __init free_area_init_nodes(unsigned long *max_zone_pfn)
2672 unsigned long nid;
2673 enum zone_type i;
2675 /* Record where the zone boundaries are */
2676 memset(arch_zone_lowest_possible_pfn, 0,
2677 sizeof(arch_zone_lowest_possible_pfn));
2678 memset(arch_zone_highest_possible_pfn, 0,
2679 sizeof(arch_zone_highest_possible_pfn));
2680 arch_zone_lowest_possible_pfn[0] = find_min_pfn_with_active_regions();
2681 arch_zone_highest_possible_pfn[0] = max_zone_pfn[0];
2682 for (i = 1; i < MAX_NR_ZONES; i++) {
2683 arch_zone_lowest_possible_pfn[i] =
2684 arch_zone_highest_possible_pfn[i-1];
2685 arch_zone_highest_possible_pfn[i] =
2686 max(max_zone_pfn[i], arch_zone_lowest_possible_pfn[i]);
2689 /* Print out the zone ranges */
2690 printk("Zone PFN ranges:\n");
2691 for (i = 0; i < MAX_NR_ZONES; i++)
2692 printk(" %-8s %8lu -> %8lu\n",
2693 zone_names[i],
2694 arch_zone_lowest_possible_pfn[i],
2695 arch_zone_highest_possible_pfn[i]);
2697 /* Print out the early_node_map[] */
2698 printk("early_node_map[%d] active PFN ranges\n", nr_nodemap_entries);
2699 for (i = 0; i < nr_nodemap_entries; i++)
2700 printk(" %3d: %8lu -> %8lu\n", early_node_map[i].nid,
2701 early_node_map[i].start_pfn,
2702 early_node_map[i].end_pfn);
2704 /* Initialise every node */
2705 for_each_online_node(nid) {
2706 pg_data_t *pgdat = NODE_DATA(nid);
2707 free_area_init_node(nid, pgdat, NULL,
2708 find_min_pfn_for_node(nid), NULL);
2711 #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
2714 * set_dma_reserve - set the specified number of pages reserved in the first zone
2715 * @new_dma_reserve: The number of pages to mark reserved
2717 * The per-cpu batchsize and zone watermarks are determined by present_pages.
2718 * In the DMA zone, a significant percentage may be consumed by kernel image
2719 * and other unfreeable allocations which can skew the watermarks badly. This
2720 * function may optionally be used to account for unfreeable pages in the
2721 * first zone (e.g., ZONE_DMA). The effect will be lower watermarks and
2722 * smaller per-cpu batchsize.
2724 void __init set_dma_reserve(unsigned long new_dma_reserve)
2726 dma_reserve = new_dma_reserve;
2729 #ifndef CONFIG_NEED_MULTIPLE_NODES
2730 static bootmem_data_t contig_bootmem_data;
2731 struct pglist_data contig_page_data = { .bdata = &contig_bootmem_data };
2733 EXPORT_SYMBOL(contig_page_data);
2734 #endif
2736 void __init free_area_init(unsigned long *zones_size)
2738 free_area_init_node(0, NODE_DATA(0), zones_size,
2739 __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
2742 #ifdef CONFIG_HOTPLUG_CPU
2743 static int page_alloc_cpu_notify(struct notifier_block *self,
2744 unsigned long action, void *hcpu)
2746 int cpu = (unsigned long)hcpu;
2748 if (action == CPU_DEAD) {
2749 local_irq_disable();
2750 __drain_pages(cpu);
2751 vm_events_fold_cpu(cpu);
2752 local_irq_enable();
2753 refresh_cpu_vm_stats(cpu);
2755 return NOTIFY_OK;
2757 #endif /* CONFIG_HOTPLUG_CPU */
2759 void __init page_alloc_init(void)
2761 hotcpu_notifier(page_alloc_cpu_notify, 0);
2765 * calculate_totalreserve_pages - called when sysctl_lower_zone_reserve_ratio
2766 * or min_free_kbytes changes.
2768 static void calculate_totalreserve_pages(void)
2770 struct pglist_data *pgdat;
2771 unsigned long reserve_pages = 0;
2772 enum zone_type i, j;
2774 for_each_online_pgdat(pgdat) {
2775 for (i = 0; i < MAX_NR_ZONES; i++) {
2776 struct zone *zone = pgdat->node_zones + i;
2777 unsigned long max = 0;
2779 /* Find valid and maximum lowmem_reserve in the zone */
2780 for (j = i; j < MAX_NR_ZONES; j++) {
2781 if (zone->lowmem_reserve[j] > max)
2782 max = zone->lowmem_reserve[j];
2785 /* we treat pages_high as reserved pages. */
2786 max += zone->pages_high;
2788 if (max > zone->present_pages)
2789 max = zone->present_pages;
2790 reserve_pages += max;
2793 totalreserve_pages = reserve_pages;
2797 * setup_per_zone_lowmem_reserve - called whenever
2798 * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
2799 * has a correct pages reserved value, so an adequate number of
2800 * pages are left in the zone after a successful __alloc_pages().
2802 static void setup_per_zone_lowmem_reserve(void)
2804 struct pglist_data *pgdat;
2805 enum zone_type j, idx;
2807 for_each_online_pgdat(pgdat) {
2808 for (j = 0; j < MAX_NR_ZONES; j++) {
2809 struct zone *zone = pgdat->node_zones + j;
2810 unsigned long present_pages = zone->present_pages;
2812 zone->lowmem_reserve[j] = 0;
2814 idx = j;
2815 while (idx) {
2816 struct zone *lower_zone;
2818 idx--;
2820 if (sysctl_lowmem_reserve_ratio[idx] < 1)
2821 sysctl_lowmem_reserve_ratio[idx] = 1;
2823 lower_zone = pgdat->node_zones + idx;
2824 lower_zone->lowmem_reserve[j] = present_pages /
2825 sysctl_lowmem_reserve_ratio[idx];
2826 present_pages += lower_zone->present_pages;
2831 /* update totalreserve_pages */
2832 calculate_totalreserve_pages();
2836 * setup_per_zone_pages_min - called when min_free_kbytes changes.
2838 * Ensures that the pages_{min,low,high} values for each zone are set correctly
2839 * with respect to min_free_kbytes.
2841 void setup_per_zone_pages_min(void)
2843 unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
2844 unsigned long lowmem_pages = 0;
2845 struct zone *zone;
2846 unsigned long flags;
2848 /* Calculate total number of !ZONE_HIGHMEM pages */
2849 for_each_zone(zone) {
2850 if (!is_highmem(zone))
2851 lowmem_pages += zone->present_pages;
2854 for_each_zone(zone) {
2855 u64 tmp;
2857 spin_lock_irqsave(&zone->lru_lock, flags);
2858 tmp = (u64)pages_min * zone->present_pages;
2859 do_div(tmp, lowmem_pages);
2860 if (is_highmem(zone)) {
2862 * __GFP_HIGH and PF_MEMALLOC allocations usually don't
2863 * need highmem pages, so cap pages_min to a small
2864 * value here.
2866 * The (pages_high-pages_low) and (pages_low-pages_min)
2867 * deltas controls asynch page reclaim, and so should
2868 * not be capped for highmem.
2870 int min_pages;
2872 min_pages = zone->present_pages / 1024;
2873 if (min_pages < SWAP_CLUSTER_MAX)
2874 min_pages = SWAP_CLUSTER_MAX;
2875 if (min_pages > 128)
2876 min_pages = 128;
2877 zone->pages_min = min_pages;
2878 } else {
2880 * If it's a lowmem zone, reserve a number of pages
2881 * proportionate to the zone's size.
2883 zone->pages_min = tmp;
2886 zone->pages_low = zone->pages_min + (tmp >> 2);
2887 zone->pages_high = zone->pages_min + (tmp >> 1);
2888 spin_unlock_irqrestore(&zone->lru_lock, flags);
2891 /* update totalreserve_pages */
2892 calculate_totalreserve_pages();
2896 * Initialise min_free_kbytes.
2898 * For small machines we want it small (128k min). For large machines
2899 * we want it large (64MB max). But it is not linear, because network
2900 * bandwidth does not increase linearly with machine size. We use
2902 * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
2903 * min_free_kbytes = sqrt(lowmem_kbytes * 16)
2905 * which yields
2907 * 16MB: 512k
2908 * 32MB: 724k
2909 * 64MB: 1024k
2910 * 128MB: 1448k
2911 * 256MB: 2048k
2912 * 512MB: 2896k
2913 * 1024MB: 4096k
2914 * 2048MB: 5792k
2915 * 4096MB: 8192k
2916 * 8192MB: 11584k
2917 * 16384MB: 16384k
2919 static int __init init_per_zone_pages_min(void)
2921 unsigned long lowmem_kbytes;
2923 lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
2925 min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
2926 if (min_free_kbytes < 128)
2927 min_free_kbytes = 128;
2928 if (min_free_kbytes > 65536)
2929 min_free_kbytes = 65536;
2930 setup_per_zone_pages_min();
2931 setup_per_zone_lowmem_reserve();
2932 return 0;
2934 module_init(init_per_zone_pages_min)
2937 * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
2938 * that we can call two helper functions whenever min_free_kbytes
2939 * changes.
2941 int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
2942 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
2944 proc_dointvec(table, write, file, buffer, length, ppos);
2945 setup_per_zone_pages_min();
2946 return 0;
2949 #ifdef CONFIG_NUMA
2950 int sysctl_min_unmapped_ratio_sysctl_handler(ctl_table *table, int write,
2951 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
2953 struct zone *zone;
2954 int rc;
2956 rc = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
2957 if (rc)
2958 return rc;
2960 for_each_zone(zone)
2961 zone->min_unmapped_pages = (zone->present_pages *
2962 sysctl_min_unmapped_ratio) / 100;
2963 return 0;
2966 int sysctl_min_slab_ratio_sysctl_handler(ctl_table *table, int write,
2967 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
2969 struct zone *zone;
2970 int rc;
2972 rc = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
2973 if (rc)
2974 return rc;
2976 for_each_zone(zone)
2977 zone->min_slab_pages = (zone->present_pages *
2978 sysctl_min_slab_ratio) / 100;
2979 return 0;
2981 #endif
2984 * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
2985 * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
2986 * whenever sysctl_lowmem_reserve_ratio changes.
2988 * The reserve ratio obviously has absolutely no relation with the
2989 * pages_min watermarks. The lowmem reserve ratio can only make sense
2990 * if in function of the boot time zone sizes.
2992 int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
2993 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
2995 proc_dointvec_minmax(table, write, file, buffer, length, ppos);
2996 setup_per_zone_lowmem_reserve();
2997 return 0;
3001 * percpu_pagelist_fraction - changes the pcp->high for each zone on each
3002 * cpu. It is the fraction of total pages in each zone that a hot per cpu pagelist
3003 * can have before it gets flushed back to buddy allocator.
3006 int percpu_pagelist_fraction_sysctl_handler(ctl_table *table, int write,
3007 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
3009 struct zone *zone;
3010 unsigned int cpu;
3011 int ret;
3013 ret = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
3014 if (!write || (ret == -EINVAL))
3015 return ret;
3016 for_each_zone(zone) {
3017 for_each_online_cpu(cpu) {
3018 unsigned long high;
3019 high = zone->present_pages / percpu_pagelist_fraction;
3020 setup_pagelist_highmark(zone_pcp(zone, cpu), high);
3023 return 0;
3026 int hashdist = HASHDIST_DEFAULT;
3028 #ifdef CONFIG_NUMA
3029 static int __init set_hashdist(char *str)
3031 if (!str)
3032 return 0;
3033 hashdist = simple_strtoul(str, &str, 0);
3034 return 1;
3036 __setup("hashdist=", set_hashdist);
3037 #endif
3040 * allocate a large system hash table from bootmem
3041 * - it is assumed that the hash table must contain an exact power-of-2
3042 * quantity of entries
3043 * - limit is the number of hash buckets, not the total allocation size
3045 void *__init alloc_large_system_hash(const char *tablename,
3046 unsigned long bucketsize,
3047 unsigned long numentries,
3048 int scale,
3049 int flags,
3050 unsigned int *_hash_shift,
3051 unsigned int *_hash_mask,
3052 unsigned long limit)
3054 unsigned long long max = limit;
3055 unsigned long log2qty, size;
3056 void *table = NULL;
3058 /* allow the kernel cmdline to have a say */
3059 if (!numentries) {
3060 /* round applicable memory size up to nearest megabyte */
3061 numentries = (flags & HASH_HIGHMEM) ? nr_all_pages : nr_kernel_pages;
3062 numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
3063 numentries >>= 20 - PAGE_SHIFT;
3064 numentries <<= 20 - PAGE_SHIFT;
3066 /* limit to 1 bucket per 2^scale bytes of low memory */
3067 if (scale > PAGE_SHIFT)
3068 numentries >>= (scale - PAGE_SHIFT);
3069 else
3070 numentries <<= (PAGE_SHIFT - scale);
3072 numentries = roundup_pow_of_two(numentries);
3074 /* limit allocation size to 1/16 total memory by default */
3075 if (max == 0) {
3076 max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
3077 do_div(max, bucketsize);
3080 if (numentries > max)
3081 numentries = max;
3083 log2qty = long_log2(numentries);
3085 do {
3086 size = bucketsize << log2qty;
3087 if (flags & HASH_EARLY)
3088 table = alloc_bootmem(size);
3089 else if (hashdist)
3090 table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
3091 else {
3092 unsigned long order;
3093 for (order = 0; ((1UL << order) << PAGE_SHIFT) < size; order++)
3095 table = (void*) __get_free_pages(GFP_ATOMIC, order);
3097 } while (!table && size > PAGE_SIZE && --log2qty);
3099 if (!table)
3100 panic("Failed to allocate %s hash table\n", tablename);
3102 printk("%s hash table entries: %d (order: %d, %lu bytes)\n",
3103 tablename,
3104 (1U << log2qty),
3105 long_log2(size) - PAGE_SHIFT,
3106 size);
3108 if (_hash_shift)
3109 *_hash_shift = log2qty;
3110 if (_hash_mask)
3111 *_hash_mask = (1 << log2qty) - 1;
3113 return table;
3116 #ifdef CONFIG_OUT_OF_LINE_PFN_TO_PAGE
3117 struct page *pfn_to_page(unsigned long pfn)
3119 return __pfn_to_page(pfn);
3121 unsigned long page_to_pfn(struct page *page)
3123 return __page_to_pfn(page);
3125 EXPORT_SYMBOL(pfn_to_page);
3126 EXPORT_SYMBOL(page_to_pfn);
3127 #endif /* CONFIG_OUT_OF_LINE_PFN_TO_PAGE */
3129 #if MAX_NUMNODES > 1
3131 * Find the highest possible node id.
3133 int highest_possible_node_id(void)
3135 unsigned int node;
3136 unsigned int highest = 0;
3138 for_each_node_mask(node, node_possible_map)
3139 highest = node;
3140 return highest;
3142 EXPORT_SYMBOL(highest_possible_node_id);
3143 #endif