MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / mm / page_alloc.c
blob180a5d35e6a2760cf90a44c93102f796d2b87e63
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 #if 0 // mask by Victor Yu. 02-12-2007
192 printk(KERN_EMERG "Bad page state in process '%s'\n"
193 KERN_EMERG "page:%p flags:0x%0*lx mapping:%p mapcount:%d count:%d\n"
194 KERN_EMERG "Trying to fix it up, but a reboot is needed\n"
195 KERN_EMERG "Backtrace:\n",
196 current->comm, page, (int)(2*sizeof(unsigned long)),
197 (unsigned long)page->flags, page->mapping,
198 page_mapcount(page), page_count(page));
199 #else
200 printk(KERN_EMERG "Bad page state in process '%s'\n"
201 KERN_EMERG "page:%p flags:0x%0*lx mapping:%p mapcount:%d count:%d\n"
202 KERN_EMERG "Trying to fix it up, but a reboot is needed\n"
203 KERN_EMERG "Backtrace:\n",
204 current->comm, page, (int)(2*sizeof(unsigned long)),
205 (unsigned long)page->flags, page->u.xx.mapping,
206 page_mapcount(page), page_count(page));
207 #endif
208 dump_stack();
209 page->flags &= ~(1 << PG_lru |
210 1 << PG_private |
211 1 << PG_locked |
212 1 << PG_active |
213 1 << PG_dirty |
214 1 << PG_reclaim |
215 1 << PG_slab |
216 1 << PG_swapcache |
217 1 << PG_writeback |
218 1 << PG_buddy );
219 set_page_count(page, 0);
220 reset_page_mapcount(page);
221 #if 0 // mask by Victor Yu. 02-12-2007
222 page->mapping = NULL;
223 #else
224 page->u.xx.mapping = NULL;
225 #endif
226 add_taint(TAINT_BAD_PAGE);
230 * Higher-order pages are called "compound pages". They are structured thusly:
232 * The first PAGE_SIZE page is called the "head page".
234 * The remaining PAGE_SIZE pages are called "tail pages".
236 * All pages have PG_compound set. All pages have their ->private pointing at
237 * the head page (even the head page has this).
239 * The first tail page's ->lru.next holds the address of the compound page's
240 * put_page() function. Its ->lru.prev holds the order of allocation.
241 * This usage means that zero-order pages may not be compound.
244 static void free_compound_page(struct page *page)
246 __free_pages_ok(page, (unsigned long)page[1].lru.prev);
249 static void prep_compound_page(struct page *page, unsigned long order)
251 int i;
252 int nr_pages = 1 << order;
254 page[1].lru.next = (void *)free_compound_page; /* set dtor */
255 page[1].lru.prev = (void *)order;
256 for (i = 0; i < nr_pages; i++) {
257 struct page *p = page + i;
259 __SetPageCompound(p);
260 set_page_private(p, (unsigned long)page);
264 static void destroy_compound_page(struct page *page, unsigned long order)
266 int i;
267 int nr_pages = 1 << order;
269 if (unlikely((unsigned long)page[1].lru.prev != order))
270 bad_page(page);
272 for (i = 0; i < nr_pages; i++) {
273 struct page *p = page + i;
275 if (unlikely(!PageCompound(p) |
276 (page_private(p) != (unsigned long)page)))
277 bad_page(page);
278 __ClearPageCompound(p);
282 static inline void prep_zero_page(struct page *page, int order, gfp_t gfp_flags)
284 int i;
286 VM_BUG_ON((gfp_flags & (__GFP_WAIT | __GFP_HIGHMEM)) == __GFP_HIGHMEM);
288 * clear_highpage() will use KM_USER0, so it's a bug to use __GFP_ZERO
289 * and __GFP_HIGHMEM from hard or soft interrupt context.
291 VM_BUG_ON((gfp_flags & __GFP_HIGHMEM) && in_interrupt());
292 for (i = 0; i < (1 << order); i++)
293 clear_highpage(page + i);
297 * function for dealing with page's order in buddy system.
298 * zone->lock is already acquired when we use these.
299 * So, we don't need atomic page->flags operations here.
301 static inline unsigned long page_order(struct page *page)
303 return page_private(page);
306 static inline void set_page_order(struct page *page, int order)
308 set_page_private(page, order);
309 __SetPageBuddy(page);
312 static inline void rmv_page_order(struct page *page)
314 __ClearPageBuddy(page);
315 set_page_private(page, 0);
319 * Locate the struct page for both the matching buddy in our
320 * pair (buddy1) and the combined O(n+1) page they form (page).
322 * 1) Any buddy B1 will have an order O twin B2 which satisfies
323 * the following equation:
324 * B2 = B1 ^ (1 << O)
325 * For example, if the starting buddy (buddy2) is #8 its order
326 * 1 buddy is #10:
327 * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
329 * 2) Any buddy B will have an order O+1 parent P which
330 * satisfies the following equation:
331 * P = B & ~(1 << O)
333 * Assumption: *_mem_map is contiguous at least up to MAX_ORDER
335 static inline struct page *
336 __page_find_buddy(struct page *page, unsigned long page_idx, unsigned int order)
338 unsigned long buddy_idx = page_idx ^ (1 << order);
340 return page + (buddy_idx - page_idx);
343 static inline unsigned long
344 __find_combined_index(unsigned long page_idx, unsigned int order)
346 return (page_idx & ~(1 << order));
350 * This function checks whether a page is free && is the buddy
351 * we can do coalesce a page and its buddy if
352 * (a) the buddy is not in a hole &&
353 * (b) the buddy is in the buddy system &&
354 * (c) a page and its buddy have the same order &&
355 * (d) a page and its buddy are in the same zone.
357 * For recording whether a page is in the buddy system, we use PG_buddy.
358 * Setting, clearing, and testing PG_buddy is serialized by zone->lock.
360 * For recording page's order, we use page_private(page).
362 static inline int page_is_buddy(struct page *page, struct page *buddy,
363 int order)
365 #ifdef CONFIG_HOLES_IN_ZONE
366 if (!pfn_valid(page_to_pfn(buddy)))
367 return 0;
368 #endif
370 if (page_zone_id(page) != page_zone_id(buddy))
371 return 0;
373 if (PageBuddy(buddy) && page_order(buddy) == order) {
374 BUG_ON(page_count(buddy) != 0);
375 return 1;
377 return 0;
381 * Freeing function for a buddy system allocator.
383 * The concept of a buddy system is to maintain direct-mapped table
384 * (containing bit values) for memory blocks of various "orders".
385 * The bottom level table contains the map for the smallest allocatable
386 * units of memory (here, pages), and each level above it describes
387 * pairs of units from the levels below, hence, "buddies".
388 * At a high level, all that happens here is marking the table entry
389 * at the bottom level available, and propagating the changes upward
390 * as necessary, plus some accounting needed to play nicely with other
391 * parts of the VM system.
392 * At each level, we keep a list of pages, which are heads of continuous
393 * free pages of length of (1 << order) and marked with PG_buddy. Page's
394 * order is recorded in page_private(page) field.
395 * So when we are allocating or freeing one, we can derive the state of the
396 * other. That is, if we allocate a small block, and both were
397 * free, the remainder of the region must be split into blocks.
398 * If a block is freed, and its buddy is also free, then this
399 * triggers coalescing into a block of larger size.
401 * -- wli
404 static inline void __free_one_page(struct page *page,
405 struct zone *zone, unsigned int order)
407 unsigned long page_idx;
408 int order_size = 1 << order;
410 if (unlikely(PageCompound(page)))
411 destroy_compound_page(page, order);
413 page_idx = page_to_pfn(page) & ((1 << MAX_ORDER) - 1);
415 VM_BUG_ON(page_idx & (order_size - 1));
416 VM_BUG_ON(bad_range(zone, page));
418 zone->free_pages += order_size;
419 while (order < MAX_ORDER-1) {
420 unsigned long combined_idx;
421 struct free_area *area;
422 struct page *buddy;
424 buddy = __page_find_buddy(page, page_idx, order);
425 if (!page_is_buddy(page, buddy, order))
426 break; /* Move the buddy up one level. */
428 list_del(&buddy->lru);
429 area = zone->free_area + order;
430 area->nr_free--;
431 rmv_page_order(buddy);
432 combined_idx = __find_combined_index(page_idx, order);
433 page = page + (combined_idx - page_idx);
434 page_idx = combined_idx;
435 order++;
437 set_page_order(page, order);
438 list_add(&page->lru, &zone->free_area[order].free_list);
439 zone->free_area[order].nr_free++;
442 static inline int free_pages_check(struct page *page)
444 #if 0 // mask by Victor Yu. 02-12-2007
445 if (unlikely(page_mapcount(page) |
446 (page->mapping != NULL) |
447 (page_count(page) != 0) |
448 (page->flags & (
449 1 << PG_lru |
450 1 << PG_private |
451 1 << PG_locked |
452 1 << PG_active |
453 1 << PG_reclaim |
454 1 << PG_slab |
455 1 << PG_swapcache |
456 1 << PG_writeback |
457 1 << PG_reserved |
458 1 << PG_buddy ))))
459 #else
460 if (unlikely(page_mapcount(page) |
461 (page->u.xx.mapping != NULL) |
462 (page_count(page) != 0) |
463 (page->flags & (
464 1 << PG_lru |
465 1 << PG_private |
466 1 << PG_locked |
467 1 << PG_active |
468 1 << PG_reclaim |
469 1 << PG_slab |
470 1 << PG_swapcache |
471 1 << PG_writeback |
472 1 << PG_reserved |
473 1 << PG_buddy ))))
474 #endif
475 bad_page(page);
476 if (PageDirty(page))
477 __ClearPageDirty(page);
479 * For now, we report if PG_reserved was found set, but do not
480 * clear it, and do not free the page. But we shall soon need
481 * to do more, for when the ZERO_PAGE count wraps negative.
483 return PageReserved(page);
487 * Frees a list of pages.
488 * Assumes all pages on list are in same zone, and of same order.
489 * count is the number of pages to free.
491 * If the zone was previously in an "all pages pinned" state then look to
492 * see if this freeing clears that state.
494 * And clear the zone's pages_scanned counter, to hold off the "all pages are
495 * pinned" detection logic.
497 static void free_pages_bulk(struct zone *zone, int count,
498 struct list_head *list, int order)
500 spin_lock(&zone->lock);
501 zone->all_unreclaimable = 0;
502 zone->pages_scanned = 0;
503 while (count--) {
504 struct page *page;
506 VM_BUG_ON(list_empty(list));
507 page = list_entry(list->prev, struct page, lru);
508 /* have to delete it as __free_one_page list manipulates */
509 list_del(&page->lru);
510 __free_one_page(page, zone, order);
512 spin_unlock(&zone->lock);
515 static void free_one_page(struct zone *zone, struct page *page, int order)
517 spin_lock(&zone->lock);
518 zone->all_unreclaimable = 0;
519 zone->pages_scanned = 0;
520 __free_one_page(page, zone ,order);
521 spin_unlock(&zone->lock);
524 static void __free_pages_ok(struct page *page, unsigned int order)
526 unsigned long flags;
527 int i;
528 int reserved = 0;
530 for (i = 0 ; i < (1 << order) ; ++i)
531 reserved += free_pages_check(page + i);
532 if (reserved)
533 return;
535 if (!PageHighMem(page))
536 debug_check_no_locks_freed(page_address(page),PAGE_SIZE<<order);
537 arch_free_page(page, order);
538 kernel_map_pages(page, 1 << order, 0);
540 local_irq_save(flags);
541 __count_vm_events(PGFREE, 1 << order);
542 free_one_page(page_zone(page), page, order);
543 local_irq_restore(flags);
547 * permit the bootmem allocator to evade page validation on high-order frees
549 void fastcall __init __free_pages_bootmem(struct page *page, unsigned int order)
551 if (order == 0) {
552 __ClearPageReserved(page);
553 set_page_count(page, 0);
554 set_page_refcounted(page);
555 __free_page(page);
556 } else {
557 int loop;
559 prefetchw(page);
560 for (loop = 0; loop < BITS_PER_LONG; loop++) {
561 struct page *p = &page[loop];
563 if (loop + 1 < BITS_PER_LONG)
564 prefetchw(p + 1);
565 __ClearPageReserved(p);
566 set_page_count(p, 0);
569 set_page_refcounted(page);
570 __free_pages(page, order);
576 * The order of subdivision here is critical for the IO subsystem.
577 * Please do not alter this order without good reasons and regression
578 * testing. Specifically, as large blocks of memory are subdivided,
579 * the order in which smaller blocks are delivered depends on the order
580 * they're subdivided in this function. This is the primary factor
581 * influencing the order in which pages are delivered to the IO
582 * subsystem according to empirical testing, and this is also justified
583 * by considering the behavior of a buddy system containing a single
584 * large block of memory acted on by a series of small allocations.
585 * This behavior is a critical factor in sglist merging's success.
587 * -- wli
589 static inline void expand(struct zone *zone, struct page *page,
590 int low, int high, struct free_area *area)
592 unsigned long size = 1 << high;
594 while (high > low) {
595 area--;
596 high--;
597 size >>= 1;
598 VM_BUG_ON(bad_range(zone, &page[size]));
599 list_add(&page[size].lru, &area->free_list);
600 area->nr_free++;
601 set_page_order(&page[size], high);
606 * This page is about to be returned from the page allocator
608 static int prep_new_page(struct page *page, int order, gfp_t gfp_flags)
610 #if 0 // mask by Victor Yu. 02-12-2007
611 if (unlikely(page_mapcount(page) |
612 (page->mapping != NULL) |
613 (page_count(page) != 0) |
614 (page->flags & (
615 1 << PG_lru |
616 1 << PG_private |
617 1 << PG_locked |
618 1 << PG_active |
619 1 << PG_dirty |
620 1 << PG_reclaim |
621 1 << PG_slab |
622 1 << PG_swapcache |
623 1 << PG_writeback |
624 1 << PG_reserved |
625 1 << PG_buddy ))))
626 #else
627 if (unlikely(page_mapcount(page) |
628 (page->u.xx.mapping != NULL) |
629 (page_count(page) != 0) |
630 (page->flags & (
631 1 << PG_lru |
632 1 << PG_private |
633 1 << PG_locked |
634 1 << PG_active |
635 1 << PG_dirty |
636 1 << PG_reclaim |
637 1 << PG_slab |
638 1 << PG_swapcache |
639 1 << PG_writeback |
640 1 << PG_reserved |
641 1 << PG_buddy ))))
642 #endif
643 bad_page(page);
646 * For now, we report if PG_reserved was found set, but do not
647 * clear it, and do not allocate the page: as a safety net.
649 if (PageReserved(page))
650 return 1;
652 page->flags &= ~(1 << PG_uptodate | 1 << PG_error |
653 1 << PG_referenced | 1 << PG_arch_1 |
654 1 << PG_checked | 1 << PG_mappedtodisk);
655 set_page_private(page, 0);
656 set_page_refcounted(page);
657 kernel_map_pages(page, 1 << order, 1);
659 if (gfp_flags & __GFP_ZERO)
660 prep_zero_page(page, order, gfp_flags);
662 if (order && (gfp_flags & __GFP_COMP))
663 prep_compound_page(page, order);
665 return 0;
669 * Do the hard work of removing an element from the buddy allocator.
670 * Call me with the zone->lock already held.
672 static struct page *__rmqueue(struct zone *zone, unsigned int order)
674 struct free_area * area;
675 unsigned int current_order;
676 struct page *page;
678 for (current_order = order; current_order < MAX_ORDER; ++current_order) {
679 area = zone->free_area + current_order;
680 if (list_empty(&area->free_list))
681 continue;
683 page = list_entry(area->free_list.next, struct page, lru);
684 list_del(&page->lru);
685 rmv_page_order(page);
686 area->nr_free--;
687 zone->free_pages -= 1UL << order;
688 expand(zone, page, order, current_order, area);
689 return page;
692 return NULL;
696 * Obtain a specified number of elements from the buddy allocator, all under
697 * a single hold of the lock, for efficiency. Add them to the supplied list.
698 * Returns the number of new pages which were placed at *list.
700 static int rmqueue_bulk(struct zone *zone, unsigned int order,
701 unsigned long count, struct list_head *list)
703 int i;
705 spin_lock(&zone->lock);
706 for (i = 0; i < count; ++i) {
707 struct page *page = __rmqueue(zone, order);
708 if (unlikely(page == NULL))
709 break;
710 list_add_tail(&page->lru, list);
712 spin_unlock(&zone->lock);
713 return i;
716 #ifdef CONFIG_NUMA
718 * Called from the slab reaper to drain pagesets on a particular node that
719 * belongs to the currently executing processor.
720 * Note that this function must be called with the thread pinned to
721 * a single processor.
723 void drain_node_pages(int nodeid)
725 int i;
726 enum zone_type z;
727 unsigned long flags;
729 for (z = 0; z < MAX_NR_ZONES; z++) {
730 struct zone *zone = NODE_DATA(nodeid)->node_zones + z;
731 struct per_cpu_pageset *pset;
733 if (!populated_zone(zone))
734 continue;
736 pset = zone_pcp(zone, smp_processor_id());
737 for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
738 struct per_cpu_pages *pcp;
740 pcp = &pset->pcp[i];
741 if (pcp->count) {
742 local_irq_save(flags);
743 free_pages_bulk(zone, pcp->count, &pcp->list, 0);
744 pcp->count = 0;
745 local_irq_restore(flags);
750 #endif
752 #if defined(CONFIG_PM) || defined(CONFIG_HOTPLUG_CPU)
753 static void __drain_pages(unsigned int cpu)
755 unsigned long flags;
756 struct zone *zone;
757 int i;
759 for_each_zone(zone) {
760 struct per_cpu_pageset *pset;
762 pset = zone_pcp(zone, cpu);
763 for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
764 struct per_cpu_pages *pcp;
766 pcp = &pset->pcp[i];
767 local_irq_save(flags);
768 free_pages_bulk(zone, pcp->count, &pcp->list, 0);
769 pcp->count = 0;
770 local_irq_restore(flags);
774 #endif /* CONFIG_PM || CONFIG_HOTPLUG_CPU */
776 #ifdef CONFIG_PM
778 void mark_free_pages(struct zone *zone)
780 unsigned long pfn, max_zone_pfn;
781 unsigned long flags;
782 int order;
783 struct list_head *curr;
785 if (!zone->spanned_pages)
786 return;
788 spin_lock_irqsave(&zone->lock, flags);
790 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
791 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
792 if (pfn_valid(pfn)) {
793 struct page *page = pfn_to_page(pfn);
795 if (!PageNosave(page))
796 ClearPageNosaveFree(page);
799 for (order = MAX_ORDER - 1; order >= 0; --order)
800 list_for_each(curr, &zone->free_area[order].free_list) {
801 unsigned long i;
803 pfn = page_to_pfn(list_entry(curr, struct page, lru));
804 for (i = 0; i < (1UL << order); i++)
805 SetPageNosaveFree(pfn_to_page(pfn + i));
808 spin_unlock_irqrestore(&zone->lock, flags);
812 * Spill all of this CPU's per-cpu pages back into the buddy allocator.
814 void drain_local_pages(void)
816 unsigned long flags;
818 local_irq_save(flags);
819 __drain_pages(smp_processor_id());
820 local_irq_restore(flags);
822 #endif /* CONFIG_PM */
825 * Free a 0-order page
827 static void fastcall free_hot_cold_page(struct page *page, int cold)
829 struct zone *zone = page_zone(page);
830 struct per_cpu_pages *pcp;
831 unsigned long flags;
833 if (PageAnon(page))
834 #if 0 // mask by Victor Yu. 02-12-2007
835 page->mapping = NULL;
836 #else
837 page->u.xx.mapping = NULL;
838 #endif
839 if (free_pages_check(page))
840 return;
842 if (!PageHighMem(page))
843 debug_check_no_locks_freed(page_address(page), PAGE_SIZE);
844 arch_free_page(page, 0);
845 kernel_map_pages(page, 1, 0);
847 pcp = &zone_pcp(zone, get_cpu())->pcp[cold];
848 local_irq_save(flags);
849 __count_vm_event(PGFREE);
850 list_add(&page->lru, &pcp->list);
851 pcp->count++;
852 if (pcp->count >= pcp->high) {
853 free_pages_bulk(zone, pcp->batch, &pcp->list, 0);
854 pcp->count -= pcp->batch;
856 local_irq_restore(flags);
857 put_cpu();
860 void fastcall free_hot_page(struct page *page)
862 free_hot_cold_page(page, 0);
865 void fastcall free_cold_page(struct page *page)
867 free_hot_cold_page(page, 1);
871 * split_page takes a non-compound higher-order page, and splits it into
872 * n (1<<order) sub-pages: page[0..n]
873 * Each sub-page must be freed individually.
875 * Note: this is probably too low level an operation for use in drivers.
876 * Please consult with lkml before using this in your driver.
878 void split_page(struct page *page, unsigned int order)
880 int i;
882 VM_BUG_ON(PageCompound(page));
883 VM_BUG_ON(!page_count(page));
884 for (i = 1; i < (1 << order); i++)
885 set_page_refcounted(page + i);
889 * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
890 * we cheat by calling it from here, in the order > 0 path. Saves a branch
891 * or two.
893 static struct page *buffered_rmqueue(struct zonelist *zonelist,
894 struct zone *zone, int order, gfp_t gfp_flags)
896 unsigned long flags;
897 struct page *page;
898 int cold = !!(gfp_flags & __GFP_COLD);
899 int cpu;
901 again:
902 cpu = get_cpu();
903 if (likely(order == 0)) {
904 struct per_cpu_pages *pcp;
906 pcp = &zone_pcp(zone, cpu)->pcp[cold];
907 local_irq_save(flags);
908 if (!pcp->count) {
909 pcp->count = rmqueue_bulk(zone, 0,
910 pcp->batch, &pcp->list);
911 if (unlikely(!pcp->count))
912 goto failed;
914 page = list_entry(pcp->list.next, struct page, lru);
915 list_del(&page->lru);
916 pcp->count--;
917 } else {
918 spin_lock_irqsave(&zone->lock, flags);
919 page = __rmqueue(zone, order);
920 spin_unlock(&zone->lock);
921 if (!page)
922 goto failed;
925 __count_zone_vm_events(PGALLOC, zone, 1 << order);
926 zone_statistics(zonelist, zone);
927 local_irq_restore(flags);
928 put_cpu();
930 VM_BUG_ON(bad_range(zone, page));
931 if (prep_new_page(page, order, gfp_flags))
932 goto again;
933 return page;
935 failed:
936 local_irq_restore(flags);
937 put_cpu();
938 return NULL;
941 #define ALLOC_NO_WATERMARKS 0x01 /* don't check watermarks at all */
942 #define ALLOC_WMARK_MIN 0x02 /* use pages_min watermark */
943 #define ALLOC_WMARK_LOW 0x04 /* use pages_low watermark */
944 #define ALLOC_WMARK_HIGH 0x08 /* use pages_high watermark */
945 #define ALLOC_HARDER 0x10 /* try to alloc harder */
946 #define ALLOC_HIGH 0x20 /* __GFP_HIGH set */
947 #define ALLOC_CPUSET 0x40 /* check for correct cpuset */
950 * Return 1 if free pages are above 'mark'. This takes into account the order
951 * of the allocation.
953 int zone_watermark_ok(struct zone *z, int order, unsigned long mark,
954 int classzone_idx, int alloc_flags)
956 /* free_pages my go negative - that's OK */
957 unsigned long min = mark;
958 long free_pages = z->free_pages - (1 << order) + 1;
959 int o;
961 if (alloc_flags & ALLOC_HIGH)
962 min -= min / 2;
963 if (alloc_flags & ALLOC_HARDER)
964 min -= min / 4;
966 if (free_pages <= min + z->lowmem_reserve[classzone_idx])
967 return 0;
968 for (o = 0; o < order; o++) {
969 /* At the next order, this order's pages become unavailable */
970 free_pages -= z->free_area[o].nr_free << o;
972 /* Require fewer higher order pages to be free */
973 min >>= 1;
975 if (free_pages <= min)
976 return 0;
978 return 1;
982 * get_page_from_freeliest goes through the zonelist trying to allocate
983 * a page.
985 static struct page *
986 get_page_from_freelist(gfp_t gfp_mask, unsigned int order,
987 struct zonelist *zonelist, int alloc_flags)
989 struct zone **z = zonelist->zones;
990 struct page *page = NULL;
991 int classzone_idx = zone_idx(*z);
992 struct zone *zone;
995 * Go through the zonelist once, looking for a zone with enough free.
996 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
998 do {
999 zone = *z;
1000 if (unlikely(NUMA_BUILD && (gfp_mask & __GFP_THISNODE) &&
1001 zone->zone_pgdat != zonelist->zones[0]->zone_pgdat))
1002 break;
1003 if ((alloc_flags & ALLOC_CPUSET) &&
1004 !cpuset_zone_allowed(zone, gfp_mask))
1005 continue;
1007 if (!(alloc_flags & ALLOC_NO_WATERMARKS)) {
1008 unsigned long mark;
1009 if (alloc_flags & ALLOC_WMARK_MIN)
1010 mark = zone->pages_min;
1011 else if (alloc_flags & ALLOC_WMARK_LOW)
1012 mark = zone->pages_low;
1013 else
1014 mark = zone->pages_high;
1015 if (!zone_watermark_ok(zone , order, mark,
1016 classzone_idx, alloc_flags))
1017 if (!zone_reclaim_mode ||
1018 !zone_reclaim(zone, gfp_mask, order))
1019 continue;
1022 page = buffered_rmqueue(zonelist, zone, order, gfp_mask);
1023 if (page) {
1024 break;
1026 } while (*(++z) != NULL);
1027 return page;
1031 * This is the 'heart' of the zoned buddy allocator.
1033 struct page * fastcall
1034 __alloc_pages(gfp_t gfp_mask, unsigned int order,
1035 struct zonelist *zonelist)
1037 const gfp_t wait = gfp_mask & __GFP_WAIT;
1038 struct zone **z;
1039 struct page *page;
1040 struct reclaim_state reclaim_state;
1041 struct task_struct *p = current;
1042 int do_retry;
1043 int alloc_flags;
1044 int did_some_progress;
1046 might_sleep_if(wait);
1048 restart:
1049 z = zonelist->zones; /* the list of zones suitable for gfp_mask */
1051 if (unlikely(*z == NULL)) {
1052 /* Should this ever happen?? */
1053 return NULL;
1056 page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, order,
1057 zonelist, ALLOC_WMARK_LOW|ALLOC_CPUSET);
1058 if (page)
1059 goto got_pg;
1061 do {
1062 wakeup_kswapd(*z, order);
1063 } while (*(++z));
1066 * OK, we're below the kswapd watermark and have kicked background
1067 * reclaim. Now things get more complex, so set up alloc_flags according
1068 * to how we want to proceed.
1070 * The caller may dip into page reserves a bit more if the caller
1071 * cannot run direct reclaim, or if the caller has realtime scheduling
1072 * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will
1073 * set both ALLOC_HARDER (!wait) and ALLOC_HIGH (__GFP_HIGH).
1075 alloc_flags = ALLOC_WMARK_MIN;
1076 if ((unlikely(rt_task(p)) && !in_interrupt()) || !wait)
1077 alloc_flags |= ALLOC_HARDER;
1078 if (gfp_mask & __GFP_HIGH)
1079 alloc_flags |= ALLOC_HIGH;
1080 if (wait)
1081 alloc_flags |= ALLOC_CPUSET;
1084 * Go through the zonelist again. Let __GFP_HIGH and allocations
1085 * coming from realtime tasks go deeper into reserves.
1087 * This is the last chance, in general, before the goto nopage.
1088 * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
1089 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
1091 page = get_page_from_freelist(gfp_mask, order, zonelist, alloc_flags);
1092 if (page)
1093 goto got_pg;
1095 /* This allocation should allow future memory freeing. */
1097 if (((p->flags & PF_MEMALLOC) || unlikely(test_thread_flag(TIF_MEMDIE)))
1098 && !in_interrupt()) {
1099 if (!(gfp_mask & __GFP_NOMEMALLOC)) {
1100 nofail_alloc:
1101 /* go through the zonelist yet again, ignoring mins */
1102 page = get_page_from_freelist(gfp_mask, order,
1103 zonelist, ALLOC_NO_WATERMARKS);
1104 if (page)
1105 goto got_pg;
1106 if (gfp_mask & __GFP_NOFAIL) {
1107 congestion_wait(WRITE, HZ/50);
1108 goto nofail_alloc;
1111 goto nopage;
1114 /* Atomic allocations - we can't balance anything */
1115 if (!wait)
1116 goto nopage;
1118 rebalance:
1119 cond_resched();
1121 /* We now go into synchronous reclaim */
1122 cpuset_memory_pressure_bump();
1123 p->flags |= PF_MEMALLOC;
1124 reclaim_state.reclaimed_slab = 0;
1125 p->reclaim_state = &reclaim_state;
1127 did_some_progress = try_to_free_pages(zonelist->zones, gfp_mask);
1129 p->reclaim_state = NULL;
1130 p->flags &= ~PF_MEMALLOC;
1132 cond_resched();
1134 if (likely(did_some_progress)) {
1135 page = get_page_from_freelist(gfp_mask, order,
1136 zonelist, alloc_flags);
1137 if (page)
1138 goto got_pg;
1139 } else if ((gfp_mask & __GFP_FS) && !(gfp_mask & __GFP_NORETRY)) {
1141 * Go through the zonelist yet one more time, keep
1142 * very high watermark here, this is only to catch
1143 * a parallel oom killing, we must fail if we're still
1144 * under heavy pressure.
1146 page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, order,
1147 zonelist, ALLOC_WMARK_HIGH|ALLOC_CPUSET);
1148 if (page)
1149 goto got_pg;
1151 #if 1 // mask by Victor Yu. 03-15-2007, I don't know why it will let system dead.
1152 out_of_memory(zonelist, gfp_mask, order);
1153 goto restart;
1154 #endif
1158 * Don't let big-order allocations loop unless the caller explicitly
1159 * requests that. Wait for some write requests to complete then retry.
1161 * In this implementation, __GFP_REPEAT means __GFP_NOFAIL for order
1162 * <= 3, but that may not be true in other implementations.
1164 do_retry = 0;
1165 if (!(gfp_mask & __GFP_NORETRY)) {
1166 if ((order <= 3) || (gfp_mask & __GFP_REPEAT))
1167 do_retry = 1;
1168 if (gfp_mask & __GFP_NOFAIL)
1169 do_retry = 1;
1171 if (do_retry) {
1172 congestion_wait(WRITE, HZ/50);
1173 goto rebalance;
1176 nopage:
1177 #if 0 // mask by Victor Yu. 03-14-2007
1178 if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) {
1179 printk(KERN_WARNING "%s: page allocation failure."
1180 " order:%d, mode:0x%x\n",
1181 p->comm, order, gfp_mask);
1182 dump_stack();
1183 show_mem();
1185 #endif
1186 got_pg:
1188 return page;
1191 EXPORT_SYMBOL(__alloc_pages);
1194 * Common helper functions.
1196 fastcall unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
1198 struct page * page;
1199 page = alloc_pages(gfp_mask, order);
1200 if (!page)
1201 return 0;
1202 return (unsigned long) page_address(page);
1205 EXPORT_SYMBOL(__get_free_pages);
1207 fastcall unsigned long get_zeroed_page(gfp_t gfp_mask)
1209 struct page * page;
1212 * get_zeroed_page() returns a 32-bit address, which cannot represent
1213 * a highmem page
1215 VM_BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
1217 page = alloc_pages(gfp_mask | __GFP_ZERO, 0);
1218 if (page)
1219 return (unsigned long) page_address(page);
1220 return 0;
1223 EXPORT_SYMBOL(get_zeroed_page);
1225 void __pagevec_free(struct pagevec *pvec)
1227 int i = pagevec_count(pvec);
1229 while (--i >= 0)
1230 free_hot_cold_page(pvec->pages[i], pvec->cold);
1233 fastcall void __free_pages(struct page *page, unsigned int order)
1235 if (put_page_testzero(page)) {
1236 if (order == 0)
1237 free_hot_page(page);
1238 else
1239 __free_pages_ok(page, order);
1243 EXPORT_SYMBOL(__free_pages);
1245 fastcall void free_pages(unsigned long addr, unsigned int order)
1247 if (addr != 0) {
1248 VM_BUG_ON(!virt_addr_valid((void *)addr));
1249 __free_pages(virt_to_page((void *)addr), order);
1253 EXPORT_SYMBOL(free_pages);
1256 * Total amount of free (allocatable) RAM:
1258 unsigned int nr_free_pages(void)
1260 unsigned int sum = 0;
1261 struct zone *zone;
1263 for_each_zone(zone)
1264 sum += zone->free_pages;
1266 return sum;
1269 EXPORT_SYMBOL(nr_free_pages);
1271 #ifdef CONFIG_NUMA
1272 unsigned int nr_free_pages_pgdat(pg_data_t *pgdat)
1274 unsigned int sum = 0;
1275 enum zone_type i;
1277 for (i = 0; i < MAX_NR_ZONES; i++)
1278 sum += pgdat->node_zones[i].free_pages;
1280 return sum;
1282 #endif
1284 static unsigned int nr_free_zone_pages(int offset)
1286 /* Just pick one node, since fallback list is circular */
1287 pg_data_t *pgdat = NODE_DATA(numa_node_id());
1288 unsigned int sum = 0;
1290 struct zonelist *zonelist = pgdat->node_zonelists + offset;
1291 struct zone **zonep = zonelist->zones;
1292 struct zone *zone;
1294 for (zone = *zonep++; zone; zone = *zonep++) {
1295 unsigned long size = zone->present_pages;
1296 unsigned long high = zone->pages_high;
1297 if (size > high)
1298 sum += size - high;
1301 return sum;
1305 * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
1307 unsigned int nr_free_buffer_pages(void)
1309 return nr_free_zone_pages(gfp_zone(GFP_USER));
1313 * Amount of free RAM allocatable within all zones
1315 unsigned int nr_free_pagecache_pages(void)
1317 return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER));
1320 static inline void show_node(struct zone *zone)
1322 if (NUMA_BUILD)
1323 printk("Node %ld ", zone_to_nid(zone));
1326 void si_meminfo(struct sysinfo *val)
1328 val->totalram = totalram_pages;
1329 val->sharedram = 0;
1330 val->freeram = nr_free_pages();
1331 val->bufferram = nr_blockdev_pages();
1332 val->totalhigh = totalhigh_pages;
1333 val->freehigh = nr_free_highpages();
1334 val->mem_unit = PAGE_SIZE;
1337 EXPORT_SYMBOL(si_meminfo);
1339 #ifdef CONFIG_NUMA
1340 void si_meminfo_node(struct sysinfo *val, int nid)
1342 pg_data_t *pgdat = NODE_DATA(nid);
1344 val->totalram = pgdat->node_present_pages;
1345 val->freeram = nr_free_pages_pgdat(pgdat);
1346 #ifdef CONFIG_HIGHMEM
1347 val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].present_pages;
1348 val->freehigh = pgdat->node_zones[ZONE_HIGHMEM].free_pages;
1349 #else
1350 val->totalhigh = 0;
1351 val->freehigh = 0;
1352 #endif
1353 val->mem_unit = PAGE_SIZE;
1355 #endif
1357 #define K(x) ((x) << (PAGE_SHIFT-10))
1360 * Show free area list (used inside shift_scroll-lock stuff)
1361 * We also calculate the percentage fragmentation. We do this by counting the
1362 * memory on each free list with the exception of the first item on the list.
1364 void show_free_areas(void)
1366 int cpu;
1367 unsigned long active;
1368 unsigned long inactive;
1369 unsigned long free;
1370 struct zone *zone;
1372 for_each_zone(zone) {
1373 if (!populated_zone(zone))
1374 continue;
1376 show_node(zone);
1377 printk("%s per-cpu:\n", zone->name);
1379 for_each_online_cpu(cpu) {
1380 struct per_cpu_pageset *pageset;
1382 pageset = zone_pcp(zone, cpu);
1384 printk("CPU %4d: Hot: hi:%5d, btch:%4d usd:%4d "
1385 "Cold: hi:%5d, btch:%4d usd:%4d\n",
1386 cpu, pageset->pcp[0].high,
1387 pageset->pcp[0].batch, pageset->pcp[0].count,
1388 pageset->pcp[1].high, pageset->pcp[1].batch,
1389 pageset->pcp[1].count);
1393 get_zone_counts(&active, &inactive, &free);
1395 printk("Active:%lu inactive:%lu dirty:%lu writeback:%lu "
1396 "unstable:%lu free:%u slab:%lu mapped:%lu pagetables:%lu\n",
1397 active,
1398 inactive,
1399 global_page_state(NR_FILE_DIRTY),
1400 global_page_state(NR_WRITEBACK),
1401 global_page_state(NR_UNSTABLE_NFS),
1402 nr_free_pages(),
1403 global_page_state(NR_SLAB_RECLAIMABLE) +
1404 global_page_state(NR_SLAB_UNRECLAIMABLE),
1405 global_page_state(NR_FILE_MAPPED),
1406 global_page_state(NR_PAGETABLE));
1408 for_each_zone(zone) {
1409 int i;
1411 if (!populated_zone(zone))
1412 continue;
1414 show_node(zone);
1415 printk("%s"
1416 " free:%lukB"
1417 " min:%lukB"
1418 " low:%lukB"
1419 " high:%lukB"
1420 " active:%lukB"
1421 " inactive:%lukB"
1422 " present:%lukB"
1423 " pages_scanned:%lu"
1424 " all_unreclaimable? %s"
1425 "\n",
1426 zone->name,
1427 K(zone->free_pages),
1428 K(zone->pages_min),
1429 K(zone->pages_low),
1430 K(zone->pages_high),
1431 K(zone->nr_active),
1432 K(zone->nr_inactive),
1433 K(zone->present_pages),
1434 zone->pages_scanned,
1435 (zone->all_unreclaimable ? "yes" : "no")
1437 printk("lowmem_reserve[]:");
1438 for (i = 0; i < MAX_NR_ZONES; i++)
1439 printk(" %lu", zone->lowmem_reserve[i]);
1440 printk("\n");
1443 for_each_zone(zone) {
1444 unsigned long nr[MAX_ORDER], flags, order, total = 0;
1446 if (!populated_zone(zone))
1447 continue;
1449 show_node(zone);
1450 printk("%s: ", zone->name);
1452 spin_lock_irqsave(&zone->lock, flags);
1453 for (order = 0; order < MAX_ORDER; order++) {
1454 nr[order] = zone->free_area[order].nr_free;
1455 total += nr[order] << order;
1457 spin_unlock_irqrestore(&zone->lock, flags);
1458 for (order = 0; order < MAX_ORDER; order++)
1459 printk("%lu*%lukB ", nr[order], K(1UL) << order);
1460 printk("= %lukB\n", K(total));
1463 show_swap_cache_info();
1467 * Builds allocation fallback zone lists.
1469 * Add all populated zones of a node to the zonelist.
1471 static int __meminit build_zonelists_node(pg_data_t *pgdat,
1472 struct zonelist *zonelist, int nr_zones, enum zone_type zone_type)
1474 struct zone *zone;
1476 BUG_ON(zone_type >= MAX_NR_ZONES);
1477 zone_type++;
1479 do {
1480 zone_type--;
1481 zone = pgdat->node_zones + zone_type;
1482 if (populated_zone(zone)) {
1483 zonelist->zones[nr_zones++] = zone;
1484 check_highest_zone(zone_type);
1487 } while (zone_type);
1488 return nr_zones;
1491 #ifdef CONFIG_NUMA
1492 #define MAX_NODE_LOAD (num_online_nodes())
1493 static int __meminitdata node_load[MAX_NUMNODES];
1495 * find_next_best_node - find the next node that should appear in a given node's fallback list
1496 * @node: node whose fallback list we're appending
1497 * @used_node_mask: nodemask_t of already used nodes
1499 * We use a number of factors to determine which is the next node that should
1500 * appear on a given node's fallback list. The node should not have appeared
1501 * already in @node's fallback list, and it should be the next closest node
1502 * according to the distance array (which contains arbitrary distance values
1503 * from each node to each node in the system), and should also prefer nodes
1504 * with no CPUs, since presumably they'll have very little allocation pressure
1505 * on them otherwise.
1506 * It returns -1 if no node is found.
1508 static int __meminit find_next_best_node(int node, nodemask_t *used_node_mask)
1510 int n, val;
1511 int min_val = INT_MAX;
1512 int best_node = -1;
1514 /* Use the local node if we haven't already */
1515 if (!node_isset(node, *used_node_mask)) {
1516 node_set(node, *used_node_mask);
1517 return node;
1520 for_each_online_node(n) {
1521 cpumask_t tmp;
1523 /* Don't want a node to appear more than once */
1524 if (node_isset(n, *used_node_mask))
1525 continue;
1527 /* Use the distance array to find the distance */
1528 val = node_distance(node, n);
1530 /* Penalize nodes under us ("prefer the next node") */
1531 val += (n < node);
1533 /* Give preference to headless and unused nodes */
1534 tmp = node_to_cpumask(n);
1535 if (!cpus_empty(tmp))
1536 val += PENALTY_FOR_NODE_WITH_CPUS;
1538 /* Slight preference for less loaded node */
1539 val *= (MAX_NODE_LOAD*MAX_NUMNODES);
1540 val += node_load[n];
1542 if (val < min_val) {
1543 min_val = val;
1544 best_node = n;
1548 if (best_node >= 0)
1549 node_set(best_node, *used_node_mask);
1551 return best_node;
1554 static void __meminit build_zonelists(pg_data_t *pgdat)
1556 int j, node, local_node;
1557 enum zone_type i;
1558 int prev_node, load;
1559 struct zonelist *zonelist;
1560 nodemask_t used_mask;
1562 /* initialize zonelists */
1563 for (i = 0; i < MAX_NR_ZONES; i++) {
1564 zonelist = pgdat->node_zonelists + i;
1565 zonelist->zones[0] = NULL;
1568 /* NUMA-aware ordering of nodes */
1569 local_node = pgdat->node_id;
1570 load = num_online_nodes();
1571 prev_node = local_node;
1572 nodes_clear(used_mask);
1573 while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
1574 int distance = node_distance(local_node, node);
1577 * If another node is sufficiently far away then it is better
1578 * to reclaim pages in a zone before going off node.
1580 if (distance > RECLAIM_DISTANCE)
1581 zone_reclaim_mode = 1;
1584 * We don't want to pressure a particular node.
1585 * So adding penalty to the first node in same
1586 * distance group to make it round-robin.
1589 if (distance != node_distance(local_node, prev_node))
1590 node_load[node] += load;
1591 prev_node = node;
1592 load--;
1593 for (i = 0; i < MAX_NR_ZONES; i++) {
1594 zonelist = pgdat->node_zonelists + i;
1595 for (j = 0; zonelist->zones[j] != NULL; j++);
1597 j = build_zonelists_node(NODE_DATA(node), zonelist, j, i);
1598 zonelist->zones[j] = NULL;
1603 #else /* CONFIG_NUMA */
1605 static void __meminit build_zonelists(pg_data_t *pgdat)
1607 int node, local_node;
1608 enum zone_type i,j;
1610 local_node = pgdat->node_id;
1611 for (i = 0; i < MAX_NR_ZONES; i++) {
1612 struct zonelist *zonelist;
1614 zonelist = pgdat->node_zonelists + i;
1616 j = build_zonelists_node(pgdat, zonelist, 0, i);
1618 * Now we build the zonelist so that it contains the zones
1619 * of all the other nodes.
1620 * We don't want to pressure a particular node, so when
1621 * building the zones for node N, we make sure that the
1622 * zones coming right after the local ones are those from
1623 * node N+1 (modulo N)
1625 for (node = local_node + 1; node < MAX_NUMNODES; node++) {
1626 if (!node_online(node))
1627 continue;
1628 j = build_zonelists_node(NODE_DATA(node), zonelist, j, i);
1630 for (node = 0; node < local_node; node++) {
1631 if (!node_online(node))
1632 continue;
1633 j = build_zonelists_node(NODE_DATA(node), zonelist, j, i);
1636 zonelist->zones[j] = NULL;
1640 #endif /* CONFIG_NUMA */
1642 /* return values int ....just for stop_machine_run() */
1643 static int __meminit __build_all_zonelists(void *dummy)
1645 int nid;
1646 for_each_online_node(nid)
1647 build_zonelists(NODE_DATA(nid));
1648 return 0;
1651 void __meminit build_all_zonelists(void)
1653 if (system_state == SYSTEM_BOOTING) {
1654 __build_all_zonelists(NULL);
1655 cpuset_init_current_mems_allowed();
1656 } else {
1657 /* we have to stop all cpus to guaranntee there is no user
1658 of zonelist */
1659 stop_machine_run(__build_all_zonelists, NULL, NR_CPUS);
1660 /* cpuset refresh routine should be here */
1662 vm_total_pages = nr_free_pagecache_pages();
1663 printk("Built %i zonelists. Total pages: %ld\n",
1664 num_online_nodes(), vm_total_pages);
1668 * Helper functions to size the waitqueue hash table.
1669 * Essentially these want to choose hash table sizes sufficiently
1670 * large so that collisions trying to wait on pages are rare.
1671 * But in fact, the number of active page waitqueues on typical
1672 * systems is ridiculously low, less than 200. So this is even
1673 * conservative, even though it seems large.
1675 * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
1676 * waitqueues, i.e. the size of the waitq table given the number of pages.
1678 #define PAGES_PER_WAITQUEUE 256
1680 #ifndef CONFIG_MEMORY_HOTPLUG
1681 static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
1683 unsigned long size = 1;
1685 pages /= PAGES_PER_WAITQUEUE;
1687 while (size < pages)
1688 size <<= 1;
1691 * Once we have dozens or even hundreds of threads sleeping
1692 * on IO we've got bigger problems than wait queue collision.
1693 * Limit the size of the wait table to a reasonable size.
1695 size = min(size, 4096UL);
1697 return max(size, 4UL);
1699 #else
1701 * A zone's size might be changed by hot-add, so it is not possible to determine
1702 * a suitable size for its wait_table. So we use the maximum size now.
1704 * The max wait table size = 4096 x sizeof(wait_queue_head_t). ie:
1706 * i386 (preemption config) : 4096 x 16 = 64Kbyte.
1707 * ia64, x86-64 (no preemption): 4096 x 20 = 80Kbyte.
1708 * ia64, x86-64 (preemption) : 4096 x 24 = 96Kbyte.
1710 * The maximum entries are prepared when a zone's memory is (512K + 256) pages
1711 * or more by the traditional way. (See above). It equals:
1713 * i386, x86-64, powerpc(4K page size) : = ( 2G + 1M)byte.
1714 * ia64(16K page size) : = ( 8G + 4M)byte.
1715 * powerpc (64K page size) : = (32G +16M)byte.
1717 static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
1719 return 4096UL;
1721 #endif
1724 * This is an integer logarithm so that shifts can be used later
1725 * to extract the more random high bits from the multiplicative
1726 * hash function before the remainder is taken.
1728 static inline unsigned long wait_table_bits(unsigned long size)
1730 return ffz(~size);
1733 #define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
1736 * Initially all pages are reserved - free ones are freed
1737 * up by free_all_bootmem() once the early boot process is
1738 * done. Non-atomic initialization, single-pass.
1740 void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
1741 unsigned long start_pfn)
1743 struct page *page;
1744 unsigned long end_pfn = start_pfn + size;
1745 unsigned long pfn;
1747 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
1748 if (!early_pfn_valid(pfn))
1749 continue;
1750 if (!early_pfn_in_nid(pfn, nid))
1751 continue;
1752 page = pfn_to_page(pfn);
1753 set_page_links(page, zone, nid, pfn);
1754 init_page_count(page);
1755 reset_page_mapcount(page);
1756 SetPageReserved(page);
1757 INIT_LIST_HEAD(&page->lru);
1758 #ifdef WANT_PAGE_VIRTUAL
1759 /* The shift won't overflow because ZONE_NORMAL is below 4G. */
1760 if (!is_highmem_idx(zone))
1761 set_page_address(page, __va(pfn << PAGE_SHIFT));
1762 #endif
1766 void zone_init_free_lists(struct pglist_data *pgdat, struct zone *zone,
1767 unsigned long size)
1769 int order;
1770 for (order = 0; order < MAX_ORDER ; order++) {
1771 INIT_LIST_HEAD(&zone->free_area[order].free_list);
1772 zone->free_area[order].nr_free = 0;
1776 #define ZONETABLE_INDEX(x, zone_nr) ((x << ZONES_SHIFT) | zone_nr)
1777 void zonetable_add(struct zone *zone, int nid, enum zone_type zid,
1778 unsigned long pfn, unsigned long size)
1780 unsigned long snum = pfn_to_section_nr(pfn);
1781 unsigned long end = pfn_to_section_nr(pfn + size);
1783 if (FLAGS_HAS_NODE)
1784 zone_table[ZONETABLE_INDEX(nid, zid)] = zone;
1785 else
1786 for (; snum <= end; snum++)
1787 zone_table[ZONETABLE_INDEX(snum, zid)] = zone;
1790 #ifndef __HAVE_ARCH_MEMMAP_INIT
1791 #define memmap_init(size, nid, zone, start_pfn) \
1792 memmap_init_zone((size), (nid), (zone), (start_pfn))
1793 #endif
1795 static int __cpuinit zone_batchsize(struct zone *zone)
1797 int batch;
1800 * The per-cpu-pages pools are set to around 1000th of the
1801 * size of the zone. But no more than 1/2 of a meg.
1803 * OK, so we don't know how big the cache is. So guess.
1805 batch = zone->present_pages / 1024;
1806 if (batch * PAGE_SIZE > 512 * 1024)
1807 batch = (512 * 1024) / PAGE_SIZE;
1808 batch /= 4; /* We effectively *= 4 below */
1809 if (batch < 1)
1810 batch = 1;
1813 * Clamp the batch to a 2^n - 1 value. Having a power
1814 * of 2 value was found to be more likely to have
1815 * suboptimal cache aliasing properties in some cases.
1817 * For example if 2 tasks are alternately allocating
1818 * batches of pages, one task can end up with a lot
1819 * of pages of one half of the possible page colors
1820 * and the other with pages of the other colors.
1822 batch = (1 << (fls(batch + batch/2)-1)) - 1;
1824 return batch;
1827 inline void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
1829 struct per_cpu_pages *pcp;
1831 memset(p, 0, sizeof(*p));
1833 pcp = &p->pcp[0]; /* hot */
1834 pcp->count = 0;
1835 pcp->high = 6 * batch;
1836 pcp->batch = max(1UL, 1 * batch);
1837 INIT_LIST_HEAD(&pcp->list);
1839 pcp = &p->pcp[1]; /* cold*/
1840 pcp->count = 0;
1841 pcp->high = 2 * batch;
1842 pcp->batch = max(1UL, batch/2);
1843 INIT_LIST_HEAD(&pcp->list);
1847 * setup_pagelist_highmark() sets the high water mark for hot per_cpu_pagelist
1848 * to the value high for the pageset p.
1851 static void setup_pagelist_highmark(struct per_cpu_pageset *p,
1852 unsigned long high)
1854 struct per_cpu_pages *pcp;
1856 pcp = &p->pcp[0]; /* hot list */
1857 pcp->high = high;
1858 pcp->batch = max(1UL, high/4);
1859 if ((high/4) > (PAGE_SHIFT * 8))
1860 pcp->batch = PAGE_SHIFT * 8;
1864 #ifdef CONFIG_NUMA
1866 * Boot pageset table. One per cpu which is going to be used for all
1867 * zones and all nodes. The parameters will be set in such a way
1868 * that an item put on a list will immediately be handed over to
1869 * the buddy list. This is safe since pageset manipulation is done
1870 * with interrupts disabled.
1872 * Some NUMA counter updates may also be caught by the boot pagesets.
1874 * The boot_pagesets must be kept even after bootup is complete for
1875 * unused processors and/or zones. They do play a role for bootstrapping
1876 * hotplugged processors.
1878 * zoneinfo_show() and maybe other functions do
1879 * not check if the processor is online before following the pageset pointer.
1880 * Other parts of the kernel may not check if the zone is available.
1882 static struct per_cpu_pageset boot_pageset[NR_CPUS];
1885 * Dynamically allocate memory for the
1886 * per cpu pageset array in struct zone.
1888 static int __cpuinit process_zones(int cpu)
1890 struct zone *zone, *dzone;
1892 for_each_zone(zone) {
1894 if (!populated_zone(zone))
1895 continue;
1897 zone_pcp(zone, cpu) = kmalloc_node(sizeof(struct per_cpu_pageset),
1898 GFP_KERNEL, cpu_to_node(cpu));
1899 if (!zone_pcp(zone, cpu))
1900 goto bad;
1902 setup_pageset(zone_pcp(zone, cpu), zone_batchsize(zone));
1904 if (percpu_pagelist_fraction)
1905 setup_pagelist_highmark(zone_pcp(zone, cpu),
1906 (zone->present_pages / percpu_pagelist_fraction));
1909 return 0;
1910 bad:
1911 for_each_zone(dzone) {
1912 if (dzone == zone)
1913 break;
1914 kfree(zone_pcp(dzone, cpu));
1915 zone_pcp(dzone, cpu) = NULL;
1917 return -ENOMEM;
1920 static inline void free_zone_pagesets(int cpu)
1922 struct zone *zone;
1924 for_each_zone(zone) {
1925 struct per_cpu_pageset *pset = zone_pcp(zone, cpu);
1927 /* Free per_cpu_pageset if it is slab allocated */
1928 if (pset != &boot_pageset[cpu])
1929 kfree(pset);
1930 zone_pcp(zone, cpu) = NULL;
1934 static int __cpuinit pageset_cpuup_callback(struct notifier_block *nfb,
1935 unsigned long action,
1936 void *hcpu)
1938 int cpu = (long)hcpu;
1939 int ret = NOTIFY_OK;
1941 switch (action) {
1942 case CPU_UP_PREPARE:
1943 if (process_zones(cpu))
1944 ret = NOTIFY_BAD;
1945 break;
1946 case CPU_UP_CANCELED:
1947 case CPU_DEAD:
1948 free_zone_pagesets(cpu);
1949 break;
1950 default:
1951 break;
1953 return ret;
1956 static struct notifier_block __cpuinitdata pageset_notifier =
1957 { &pageset_cpuup_callback, NULL, 0 };
1959 void __init setup_per_cpu_pageset(void)
1961 int err;
1963 /* Initialize per_cpu_pageset for cpu 0.
1964 * A cpuup callback will do this for every cpu
1965 * as it comes online
1967 err = process_zones(smp_processor_id());
1968 BUG_ON(err);
1969 register_cpu_notifier(&pageset_notifier);
1972 #endif
1974 static __meminit
1975 int zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages)
1977 int i;
1978 struct pglist_data *pgdat = zone->zone_pgdat;
1979 size_t alloc_size;
1982 * The per-page waitqueue mechanism uses hashed waitqueues
1983 * per zone.
1985 zone->wait_table_hash_nr_entries =
1986 wait_table_hash_nr_entries(zone_size_pages);
1987 zone->wait_table_bits =
1988 wait_table_bits(zone->wait_table_hash_nr_entries);
1989 alloc_size = zone->wait_table_hash_nr_entries
1990 * sizeof(wait_queue_head_t);
1992 if (system_state == SYSTEM_BOOTING) {
1993 zone->wait_table = (wait_queue_head_t *)
1994 alloc_bootmem_node(pgdat, alloc_size);
1995 } else {
1997 * This case means that a zone whose size was 0 gets new memory
1998 * via memory hot-add.
1999 * But it may be the case that a new node was hot-added. In
2000 * this case vmalloc() will not be able to use this new node's
2001 * memory - this wait_table must be initialized to use this new
2002 * node itself as well.
2003 * To use this new node's memory, further consideration will be
2004 * necessary.
2006 zone->wait_table = (wait_queue_head_t *)vmalloc(alloc_size);
2008 if (!zone->wait_table)
2009 return -ENOMEM;
2011 for(i = 0; i < zone->wait_table_hash_nr_entries; ++i)
2012 init_waitqueue_head(zone->wait_table + i);
2014 return 0;
2017 static __meminit void zone_pcp_init(struct zone *zone)
2019 int cpu;
2020 unsigned long batch = zone_batchsize(zone);
2022 for (cpu = 0; cpu < NR_CPUS; cpu++) {
2023 #ifdef CONFIG_NUMA
2024 /* Early boot. Slab allocator not functional yet */
2025 zone_pcp(zone, cpu) = &boot_pageset[cpu];
2026 setup_pageset(&boot_pageset[cpu],0);
2027 #else
2028 setup_pageset(zone_pcp(zone,cpu), batch);
2029 #endif
2031 if (zone->present_pages)
2032 printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%lu\n",
2033 zone->name, zone->present_pages, batch);
2036 __meminit int init_currently_empty_zone(struct zone *zone,
2037 unsigned long zone_start_pfn,
2038 unsigned long size)
2040 struct pglist_data *pgdat = zone->zone_pgdat;
2041 int ret;
2042 ret = zone_wait_table_init(zone, size);
2043 if (ret)
2044 return ret;
2045 pgdat->nr_zones = zone_idx(zone) + 1;
2047 zone->zone_start_pfn = zone_start_pfn;
2049 memmap_init(size, pgdat->node_id, zone_idx(zone), zone_start_pfn);
2051 zone_init_free_lists(pgdat, zone, zone->spanned_pages);
2053 return 0;
2056 #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
2058 * Basic iterator support. Return the first range of PFNs for a node
2059 * Note: nid == MAX_NUMNODES returns first region regardless of node
2061 static int __init first_active_region_index_in_nid(int nid)
2063 int i;
2065 for (i = 0; i < nr_nodemap_entries; i++)
2066 if (nid == MAX_NUMNODES || early_node_map[i].nid == nid)
2067 return i;
2069 return -1;
2073 * Basic iterator support. Return the next active range of PFNs for a node
2074 * Note: nid == MAX_NUMNODES returns next region regardles of node
2076 static int __init next_active_region_index_in_nid(int index, int nid)
2078 for (index = index + 1; index < nr_nodemap_entries; index++)
2079 if (nid == MAX_NUMNODES || early_node_map[index].nid == nid)
2080 return index;
2082 return -1;
2085 #ifndef CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID
2087 * Required by SPARSEMEM. Given a PFN, return what node the PFN is on.
2088 * Architectures may implement their own version but if add_active_range()
2089 * was used and there are no special requirements, this is a convenient
2090 * alternative
2092 int __init early_pfn_to_nid(unsigned long pfn)
2094 int i;
2096 for (i = 0; i < nr_nodemap_entries; i++) {
2097 unsigned long start_pfn = early_node_map[i].start_pfn;
2098 unsigned long end_pfn = early_node_map[i].end_pfn;
2100 if (start_pfn <= pfn && pfn < end_pfn)
2101 return early_node_map[i].nid;
2104 return 0;
2106 #endif /* CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID */
2108 /* Basic iterator support to walk early_node_map[] */
2109 #define for_each_active_range_index_in_nid(i, nid) \
2110 for (i = first_active_region_index_in_nid(nid); i != -1; \
2111 i = next_active_region_index_in_nid(i, nid))
2114 * free_bootmem_with_active_regions - Call free_bootmem_node for each active range
2115 * @nid: The node to free memory on. If MAX_NUMNODES, all nodes are freed.
2116 * @max_low_pfn: The highest PFN that will be passed to free_bootmem_node
2118 * If an architecture guarantees that all ranges registered with
2119 * add_active_ranges() contain no holes and may be freed, this
2120 * this function may be used instead of calling free_bootmem() manually.
2122 void __init free_bootmem_with_active_regions(int nid,
2123 unsigned long max_low_pfn)
2125 int i;
2127 for_each_active_range_index_in_nid(i, nid) {
2128 unsigned long size_pages = 0;
2129 unsigned long end_pfn = early_node_map[i].end_pfn;
2131 if (early_node_map[i].start_pfn >= max_low_pfn)
2132 continue;
2134 if (end_pfn > max_low_pfn)
2135 end_pfn = max_low_pfn;
2137 size_pages = end_pfn - early_node_map[i].start_pfn;
2138 free_bootmem_node(NODE_DATA(early_node_map[i].nid),
2139 PFN_PHYS(early_node_map[i].start_pfn),
2140 size_pages << PAGE_SHIFT);
2145 * sparse_memory_present_with_active_regions - Call memory_present for each active range
2146 * @nid: The node to call memory_present for. If MAX_NUMNODES, all nodes will be used.
2148 * If an architecture guarantees that all ranges registered with
2149 * add_active_ranges() contain no holes and may be freed, this
2150 * function may be used instead of calling memory_present() manually.
2152 void __init sparse_memory_present_with_active_regions(int nid)
2154 int i;
2156 for_each_active_range_index_in_nid(i, nid)
2157 memory_present(early_node_map[i].nid,
2158 early_node_map[i].start_pfn,
2159 early_node_map[i].end_pfn);
2163 * push_node_boundaries - Push node boundaries to at least the requested boundary
2164 * @nid: The nid of the node to push the boundary for
2165 * @start_pfn: The start pfn of the node
2166 * @end_pfn: The end pfn of the node
2168 * In reserve-based hot-add, mem_map is allocated that is unused until hotadd
2169 * time. Specifically, on x86_64, SRAT will report ranges that can potentially
2170 * be hotplugged even though no physical memory exists. This function allows
2171 * an arch to push out the node boundaries so mem_map is allocated that can
2172 * be used later.
2174 #ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
2175 void __init push_node_boundaries(unsigned int nid,
2176 unsigned long start_pfn, unsigned long end_pfn)
2178 printk(KERN_DEBUG "Entering push_node_boundaries(%u, %lu, %lu)\n",
2179 nid, start_pfn, end_pfn);
2181 /* Initialise the boundary for this node if necessary */
2182 if (node_boundary_end_pfn[nid] == 0)
2183 node_boundary_start_pfn[nid] = -1UL;
2185 /* Update the boundaries */
2186 if (node_boundary_start_pfn[nid] > start_pfn)
2187 node_boundary_start_pfn[nid] = start_pfn;
2188 if (node_boundary_end_pfn[nid] < end_pfn)
2189 node_boundary_end_pfn[nid] = end_pfn;
2192 /* If necessary, push the node boundary out for reserve hotadd */
2193 static void __init account_node_boundary(unsigned int nid,
2194 unsigned long *start_pfn, unsigned long *end_pfn)
2196 printk(KERN_DEBUG "Entering account_node_boundary(%u, %lu, %lu)\n",
2197 nid, *start_pfn, *end_pfn);
2199 /* Return if boundary information has not been provided */
2200 if (node_boundary_end_pfn[nid] == 0)
2201 return;
2203 /* Check the boundaries and update if necessary */
2204 if (node_boundary_start_pfn[nid] < *start_pfn)
2205 *start_pfn = node_boundary_start_pfn[nid];
2206 if (node_boundary_end_pfn[nid] > *end_pfn)
2207 *end_pfn = node_boundary_end_pfn[nid];
2209 #else
2210 void __init push_node_boundaries(unsigned int nid,
2211 unsigned long start_pfn, unsigned long end_pfn) {}
2213 static void __init account_node_boundary(unsigned int nid,
2214 unsigned long *start_pfn, unsigned long *end_pfn) {}
2215 #endif
2219 * get_pfn_range_for_nid - Return the start and end page frames for a node
2220 * @nid: The nid to return the range for. If MAX_NUMNODES, the min and max PFN are returned.
2221 * @start_pfn: Passed by reference. On return, it will have the node start_pfn.
2222 * @end_pfn: Passed by reference. On return, it will have the node end_pfn.
2224 * It returns the start and end page frame of a node based on information
2225 * provided by an arch calling add_active_range(). If called for a node
2226 * with no available memory, a warning is printed and the start and end
2227 * PFNs will be 0.
2229 void __init get_pfn_range_for_nid(unsigned int nid,
2230 unsigned long *start_pfn, unsigned long *end_pfn)
2232 int i;
2233 *start_pfn = -1UL;
2234 *end_pfn = 0;
2236 for_each_active_range_index_in_nid(i, nid) {
2237 *start_pfn = min(*start_pfn, early_node_map[i].start_pfn);
2238 *end_pfn = max(*end_pfn, early_node_map[i].end_pfn);
2241 if (*start_pfn == -1UL) {
2242 printk(KERN_WARNING "Node %u active with no memory\n", nid);
2243 *start_pfn = 0;
2246 /* Push the node boundaries out if requested */
2247 account_node_boundary(nid, start_pfn, end_pfn);
2251 * Return the number of pages a zone spans in a node, including holes
2252 * present_pages = zone_spanned_pages_in_node() - zone_absent_pages_in_node()
2254 unsigned long __init zone_spanned_pages_in_node(int nid,
2255 unsigned long zone_type,
2256 unsigned long *ignored)
2258 unsigned long node_start_pfn, node_end_pfn;
2259 unsigned long zone_start_pfn, zone_end_pfn;
2261 /* Get the start and end of the node and zone */
2262 get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
2263 zone_start_pfn = arch_zone_lowest_possible_pfn[zone_type];
2264 zone_end_pfn = arch_zone_highest_possible_pfn[zone_type];
2266 /* Check that this node has pages within the zone's required range */
2267 if (zone_end_pfn < node_start_pfn || zone_start_pfn > node_end_pfn)
2268 return 0;
2270 /* Move the zone boundaries inside the node if necessary */
2271 zone_end_pfn = min(zone_end_pfn, node_end_pfn);
2272 zone_start_pfn = max(zone_start_pfn, node_start_pfn);
2274 /* Return the spanned pages */
2275 return zone_end_pfn - zone_start_pfn;
2279 * Return the number of holes in a range on a node. If nid is MAX_NUMNODES,
2280 * then all holes in the requested range will be accounted for.
2282 unsigned long __init __absent_pages_in_range(int nid,
2283 unsigned long range_start_pfn,
2284 unsigned long range_end_pfn)
2286 int i = 0;
2287 unsigned long prev_end_pfn = 0, hole_pages = 0;
2288 unsigned long start_pfn;
2290 /* Find the end_pfn of the first active range of pfns in the node */
2291 i = first_active_region_index_in_nid(nid);
2292 if (i == -1)
2293 return 0;
2295 /* Account for ranges before physical memory on this node */
2296 if (early_node_map[i].start_pfn > range_start_pfn)
2297 hole_pages = early_node_map[i].start_pfn - range_start_pfn;
2299 prev_end_pfn = early_node_map[i].start_pfn;
2301 /* Find all holes for the zone within the node */
2302 for (; i != -1; i = next_active_region_index_in_nid(i, nid)) {
2304 /* No need to continue if prev_end_pfn is outside the zone */
2305 if (prev_end_pfn >= range_end_pfn)
2306 break;
2308 /* Make sure the end of the zone is not within the hole */
2309 start_pfn = min(early_node_map[i].start_pfn, range_end_pfn);
2310 prev_end_pfn = max(prev_end_pfn, range_start_pfn);
2312 /* Update the hole size cound and move on */
2313 if (start_pfn > range_start_pfn) {
2314 BUG_ON(prev_end_pfn > start_pfn);
2315 hole_pages += start_pfn - prev_end_pfn;
2317 prev_end_pfn = early_node_map[i].end_pfn;
2320 /* Account for ranges past physical memory on this node */
2321 if (range_end_pfn > prev_end_pfn)
2322 hole_pages += range_end_pfn -
2323 max(range_start_pfn, prev_end_pfn);
2325 return hole_pages;
2329 * absent_pages_in_range - Return number of page frames in holes within a range
2330 * @start_pfn: The start PFN to start searching for holes
2331 * @end_pfn: The end PFN to stop searching for holes
2333 * It returns the number of pages frames in memory holes within a range.
2335 unsigned long __init absent_pages_in_range(unsigned long start_pfn,
2336 unsigned long end_pfn)
2338 return __absent_pages_in_range(MAX_NUMNODES, start_pfn, end_pfn);
2341 /* Return the number of page frames in holes in a zone on a node */
2342 unsigned long __init zone_absent_pages_in_node(int nid,
2343 unsigned long zone_type,
2344 unsigned long *ignored)
2346 unsigned long node_start_pfn, node_end_pfn;
2347 unsigned long zone_start_pfn, zone_end_pfn;
2349 get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
2350 zone_start_pfn = max(arch_zone_lowest_possible_pfn[zone_type],
2351 node_start_pfn);
2352 zone_end_pfn = min(arch_zone_highest_possible_pfn[zone_type],
2353 node_end_pfn);
2355 return __absent_pages_in_range(nid, zone_start_pfn, zone_end_pfn);
2358 #else
2359 static inline unsigned long zone_spanned_pages_in_node(int nid,
2360 unsigned long zone_type,
2361 unsigned long *zones_size)
2363 return zones_size[zone_type];
2366 static inline unsigned long zone_absent_pages_in_node(int nid,
2367 unsigned long zone_type,
2368 unsigned long *zholes_size)
2370 if (!zholes_size)
2371 return 0;
2373 return zholes_size[zone_type];
2376 #endif
2378 static void __init calculate_node_totalpages(struct pglist_data *pgdat,
2379 unsigned long *zones_size, unsigned long *zholes_size)
2381 unsigned long realtotalpages, totalpages = 0;
2382 enum zone_type i;
2384 for (i = 0; i < MAX_NR_ZONES; i++)
2385 totalpages += zone_spanned_pages_in_node(pgdat->node_id, i,
2386 zones_size);
2387 pgdat->node_spanned_pages = totalpages;
2389 realtotalpages = totalpages;
2390 for (i = 0; i < MAX_NR_ZONES; i++)
2391 realtotalpages -=
2392 zone_absent_pages_in_node(pgdat->node_id, i,
2393 zholes_size);
2394 pgdat->node_present_pages = realtotalpages;
2395 printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id,
2396 realtotalpages);
2400 * Set up the zone data structures:
2401 * - mark all pages reserved
2402 * - mark all memory queues empty
2403 * - clear the memory bitmaps
2405 static void __meminit free_area_init_core(struct pglist_data *pgdat,
2406 unsigned long *zones_size, unsigned long *zholes_size)
2408 enum zone_type j;
2409 int nid = pgdat->node_id;
2410 unsigned long zone_start_pfn = pgdat->node_start_pfn;
2411 int ret;
2413 pgdat_resize_init(pgdat);
2414 pgdat->nr_zones = 0;
2415 init_waitqueue_head(&pgdat->kswapd_wait);
2416 pgdat->kswapd_max_order = 0;
2418 for (j = 0; j < MAX_NR_ZONES; j++) {
2419 struct zone *zone = pgdat->node_zones + j;
2420 unsigned long size, realsize, memmap_pages;
2422 size = zone_spanned_pages_in_node(nid, j, zones_size);
2423 realsize = size - zone_absent_pages_in_node(nid, j,
2424 zholes_size);
2427 * Adjust realsize so that it accounts for how much memory
2428 * is used by this zone for memmap. This affects the watermark
2429 * and per-cpu initialisations
2431 memmap_pages = (size * sizeof(struct page)) >> PAGE_SHIFT;
2432 if (realsize >= memmap_pages) {
2433 realsize -= memmap_pages;
2434 printk(KERN_DEBUG
2435 " %s zone: %lu pages used for memmap\n",
2436 zone_names[j], memmap_pages);
2437 } else
2438 printk(KERN_WARNING
2439 " %s zone: %lu pages exceeds realsize %lu\n",
2440 zone_names[j], memmap_pages, realsize);
2442 /* Account for reserved DMA pages */
2443 if (j == ZONE_DMA && realsize > dma_reserve) {
2444 realsize -= dma_reserve;
2445 printk(KERN_DEBUG " DMA zone: %lu pages reserved\n",
2446 dma_reserve);
2449 if (!is_highmem_idx(j))
2450 nr_kernel_pages += realsize;
2451 nr_all_pages += realsize;
2453 zone->spanned_pages = size;
2454 zone->present_pages = realsize;
2455 #ifdef CONFIG_NUMA
2456 zone->node = nid;
2457 zone->min_unmapped_pages = (realsize*sysctl_min_unmapped_ratio)
2458 / 100;
2459 zone->min_slab_pages = (realsize * sysctl_min_slab_ratio) / 100;
2460 #endif
2461 zone->name = zone_names[j];
2462 spin_lock_init(&zone->lock);
2463 spin_lock_init(&zone->lru_lock);
2464 zone_seqlock_init(zone);
2465 zone->zone_pgdat = pgdat;
2466 zone->free_pages = 0;
2468 zone->prev_priority = DEF_PRIORITY;
2470 zone_pcp_init(zone);
2471 INIT_LIST_HEAD(&zone->active_list);
2472 INIT_LIST_HEAD(&zone->inactive_list);
2473 zone->nr_scan_active = 0;
2474 zone->nr_scan_inactive = 0;
2475 zone->nr_active = 0;
2476 zone->nr_inactive = 0;
2477 zap_zone_vm_stats(zone);
2478 atomic_set(&zone->reclaim_in_progress, 0);
2479 if (!size)
2480 continue;
2482 zonetable_add(zone, nid, j, zone_start_pfn, size);
2483 ret = init_currently_empty_zone(zone, zone_start_pfn, size);
2484 BUG_ON(ret);
2485 zone_start_pfn += size;
2489 static void __init alloc_node_mem_map(struct pglist_data *pgdat)
2491 /* Skip empty nodes */
2492 if (!pgdat->node_spanned_pages)
2493 return;
2495 #ifdef CONFIG_FLAT_NODE_MEM_MAP
2496 /* ia64 gets its own node_mem_map, before this, without bootmem */
2497 if (!pgdat->node_mem_map) {
2498 unsigned long size, start, end;
2499 struct page *map;
2502 * The zone's endpoints aren't required to be MAX_ORDER
2503 * aligned but the node_mem_map endpoints must be in order
2504 * for the buddy allocator to function correctly.
2506 start = pgdat->node_start_pfn & ~(MAX_ORDER_NR_PAGES - 1);
2507 end = pgdat->node_start_pfn + pgdat->node_spanned_pages;
2508 end = ALIGN(end, MAX_ORDER_NR_PAGES);
2509 size = (end - start) * sizeof(struct page);
2510 map = alloc_remap(pgdat->node_id, size);
2511 if (!map)
2512 map = alloc_bootmem_node(pgdat, size);
2513 pgdat->node_mem_map = map + (pgdat->node_start_pfn - start);
2515 #ifdef CONFIG_FLATMEM
2517 * With no DISCONTIG, the global mem_map is just set as node 0's
2519 if (pgdat == NODE_DATA(0)) {
2520 mem_map = NODE_DATA(0)->node_mem_map;
2521 #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
2522 if (page_to_pfn(mem_map) != pgdat->node_start_pfn)
2523 mem_map -= pgdat->node_start_pfn;
2524 #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
2526 #endif
2527 #endif /* CONFIG_FLAT_NODE_MEM_MAP */
2530 void __meminit free_area_init_node(int nid, struct pglist_data *pgdat,
2531 unsigned long *zones_size, unsigned long node_start_pfn,
2532 unsigned long *zholes_size)
2534 pgdat->node_id = nid;
2535 pgdat->node_start_pfn = node_start_pfn;
2536 calculate_node_totalpages(pgdat, zones_size, zholes_size);
2538 alloc_node_mem_map(pgdat);
2540 free_area_init_core(pgdat, zones_size, zholes_size);
2543 #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
2545 * add_active_range - Register a range of PFNs backed by physical memory
2546 * @nid: The node ID the range resides on
2547 * @start_pfn: The start PFN of the available physical memory
2548 * @end_pfn: The end PFN of the available physical memory
2550 * These ranges are stored in an early_node_map[] and later used by
2551 * free_area_init_nodes() to calculate zone sizes and holes. If the
2552 * range spans a memory hole, it is up to the architecture to ensure
2553 * the memory is not freed by the bootmem allocator. If possible
2554 * the range being registered will be merged with existing ranges.
2556 void __init add_active_range(unsigned int nid, unsigned long start_pfn,
2557 unsigned long end_pfn)
2559 int i;
2561 printk(KERN_DEBUG "Entering add_active_range(%d, %lu, %lu) "
2562 "%d entries of %d used\n",
2563 nid, start_pfn, end_pfn,
2564 nr_nodemap_entries, MAX_ACTIVE_REGIONS);
2566 /* Merge with existing active regions if possible */
2567 for (i = 0; i < nr_nodemap_entries; i++) {
2568 if (early_node_map[i].nid != nid)
2569 continue;
2571 /* Skip if an existing region covers this new one */
2572 if (start_pfn >= early_node_map[i].start_pfn &&
2573 end_pfn <= early_node_map[i].end_pfn)
2574 return;
2576 /* Merge forward if suitable */
2577 if (start_pfn <= early_node_map[i].end_pfn &&
2578 end_pfn > early_node_map[i].end_pfn) {
2579 early_node_map[i].end_pfn = end_pfn;
2580 return;
2583 /* Merge backward if suitable */
2584 if (start_pfn < early_node_map[i].end_pfn &&
2585 end_pfn >= early_node_map[i].start_pfn) {
2586 early_node_map[i].start_pfn = start_pfn;
2587 return;
2591 /* Check that early_node_map is large enough */
2592 if (i >= MAX_ACTIVE_REGIONS) {
2593 printk(KERN_CRIT "More than %d memory regions, truncating\n",
2594 MAX_ACTIVE_REGIONS);
2595 return;
2598 early_node_map[i].nid = nid;
2599 early_node_map[i].start_pfn = start_pfn;
2600 early_node_map[i].end_pfn = end_pfn;
2601 nr_nodemap_entries = i + 1;
2605 * shrink_active_range - Shrink an existing registered range of PFNs
2606 * @nid: The node id the range is on that should be shrunk
2607 * @old_end_pfn: The old end PFN of the range
2608 * @new_end_pfn: The new PFN of the range
2610 * i386 with NUMA use alloc_remap() to store a node_mem_map on a local node.
2611 * The map is kept at the end physical page range that has already been
2612 * registered with add_active_range(). This function allows an arch to shrink
2613 * an existing registered range.
2615 void __init shrink_active_range(unsigned int nid, unsigned long old_end_pfn,
2616 unsigned long new_end_pfn)
2618 int i;
2620 /* Find the old active region end and shrink */
2621 for_each_active_range_index_in_nid(i, nid)
2622 if (early_node_map[i].end_pfn == old_end_pfn) {
2623 early_node_map[i].end_pfn = new_end_pfn;
2624 break;
2629 * remove_all_active_ranges - Remove all currently registered regions
2631 * During discovery, it may be found that a table like SRAT is invalid
2632 * and an alternative discovery method must be used. This function removes
2633 * all currently registered regions.
2635 void __init remove_all_active_ranges(void)
2637 memset(early_node_map, 0, sizeof(early_node_map));
2638 nr_nodemap_entries = 0;
2639 #ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
2640 memset(node_boundary_start_pfn, 0, sizeof(node_boundary_start_pfn));
2641 memset(node_boundary_end_pfn, 0, sizeof(node_boundary_end_pfn));
2642 #endif /* CONFIG_MEMORY_HOTPLUG_RESERVE */
2645 /* Compare two active node_active_regions */
2646 static int __init cmp_node_active_region(const void *a, const void *b)
2648 struct node_active_region *arange = (struct node_active_region *)a;
2649 struct node_active_region *brange = (struct node_active_region *)b;
2651 /* Done this way to avoid overflows */
2652 if (arange->start_pfn > brange->start_pfn)
2653 return 1;
2654 if (arange->start_pfn < brange->start_pfn)
2655 return -1;
2657 return 0;
2660 /* sort the node_map by start_pfn */
2661 static void __init sort_node_map(void)
2663 sort(early_node_map, (size_t)nr_nodemap_entries,
2664 sizeof(struct node_active_region),
2665 cmp_node_active_region, NULL);
2668 /* Find the lowest pfn for a node. This depends on a sorted early_node_map */
2669 unsigned long __init find_min_pfn_for_node(unsigned long nid)
2671 int i;
2673 /* Regions in the early_node_map can be in any order */
2674 sort_node_map();
2676 /* Assuming a sorted map, the first range found has the starting pfn */
2677 for_each_active_range_index_in_nid(i, nid)
2678 return early_node_map[i].start_pfn;
2680 printk(KERN_WARNING "Could not find start_pfn for node %lu\n", nid);
2681 return 0;
2685 * find_min_pfn_with_active_regions - Find the minimum PFN registered
2687 * It returns the minimum PFN based on information provided via
2688 * add_active_range().
2690 unsigned long __init find_min_pfn_with_active_regions(void)
2692 return find_min_pfn_for_node(MAX_NUMNODES);
2696 * find_max_pfn_with_active_regions - Find the maximum PFN registered
2698 * It returns the maximum PFN based on information provided via
2699 * add_active_range().
2701 unsigned long __init find_max_pfn_with_active_regions(void)
2703 int i;
2704 unsigned long max_pfn = 0;
2706 for (i = 0; i < nr_nodemap_entries; i++)
2707 max_pfn = max(max_pfn, early_node_map[i].end_pfn);
2709 return max_pfn;
2713 * free_area_init_nodes - Initialise all pg_data_t and zone data
2714 * @max_zone_pfn: an array of max PFNs for each zone
2716 * This will call free_area_init_node() for each active node in the system.
2717 * Using the page ranges provided by add_active_range(), the size of each
2718 * zone in each node and their holes is calculated. If the maximum PFN
2719 * between two adjacent zones match, it is assumed that the zone is empty.
2720 * For example, if arch_max_dma_pfn == arch_max_dma32_pfn, it is assumed
2721 * that arch_max_dma32_pfn has no pages. It is also assumed that a zone
2722 * starts where the previous one ended. For example, ZONE_DMA32 starts
2723 * at arch_max_dma_pfn.
2725 void __init free_area_init_nodes(unsigned long *max_zone_pfn)
2727 unsigned long nid;
2728 enum zone_type i;
2730 /* Record where the zone boundaries are */
2731 memset(arch_zone_lowest_possible_pfn, 0,
2732 sizeof(arch_zone_lowest_possible_pfn));
2733 memset(arch_zone_highest_possible_pfn, 0,
2734 sizeof(arch_zone_highest_possible_pfn));
2735 arch_zone_lowest_possible_pfn[0] = find_min_pfn_with_active_regions();
2736 arch_zone_highest_possible_pfn[0] = max_zone_pfn[0];
2737 for (i = 1; i < MAX_NR_ZONES; i++) {
2738 arch_zone_lowest_possible_pfn[i] =
2739 arch_zone_highest_possible_pfn[i-1];
2740 arch_zone_highest_possible_pfn[i] =
2741 max(max_zone_pfn[i], arch_zone_lowest_possible_pfn[i]);
2744 /* Print out the zone ranges */
2745 printk("Zone PFN ranges:\n");
2746 for (i = 0; i < MAX_NR_ZONES; i++)
2747 printk(" %-8s %8lu -> %8lu\n",
2748 zone_names[i],
2749 arch_zone_lowest_possible_pfn[i],
2750 arch_zone_highest_possible_pfn[i]);
2752 /* Print out the early_node_map[] */
2753 printk("early_node_map[%d] active PFN ranges\n", nr_nodemap_entries);
2754 for (i = 0; i < nr_nodemap_entries; i++)
2755 printk(" %3d: %8lu -> %8lu\n", early_node_map[i].nid,
2756 early_node_map[i].start_pfn,
2757 early_node_map[i].end_pfn);
2759 /* Initialise every node */
2760 for_each_online_node(nid) {
2761 pg_data_t *pgdat = NODE_DATA(nid);
2762 free_area_init_node(nid, pgdat, NULL,
2763 find_min_pfn_for_node(nid), NULL);
2766 #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
2769 * set_dma_reserve - set the specified number of pages reserved in the first zone
2770 * @new_dma_reserve: The number of pages to mark reserved
2772 * The per-cpu batchsize and zone watermarks are determined by present_pages.
2773 * In the DMA zone, a significant percentage may be consumed by kernel image
2774 * and other unfreeable allocations which can skew the watermarks badly. This
2775 * function may optionally be used to account for unfreeable pages in the
2776 * first zone (e.g., ZONE_DMA). The effect will be lower watermarks and
2777 * smaller per-cpu batchsize.
2779 void __init set_dma_reserve(unsigned long new_dma_reserve)
2781 dma_reserve = new_dma_reserve;
2784 #ifndef CONFIG_NEED_MULTIPLE_NODES
2785 static bootmem_data_t contig_bootmem_data;
2786 struct pglist_data contig_page_data = { .bdata = &contig_bootmem_data };
2788 EXPORT_SYMBOL(contig_page_data);
2789 #endif
2791 void __init free_area_init(unsigned long *zones_size)
2793 free_area_init_node(0, NODE_DATA(0), zones_size,
2794 __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
2797 #ifdef CONFIG_HOTPLUG_CPU
2798 static int page_alloc_cpu_notify(struct notifier_block *self,
2799 unsigned long action, void *hcpu)
2801 int cpu = (unsigned long)hcpu;
2803 if (action == CPU_DEAD) {
2804 local_irq_disable();
2805 __drain_pages(cpu);
2806 vm_events_fold_cpu(cpu);
2807 local_irq_enable();
2808 refresh_cpu_vm_stats(cpu);
2810 return NOTIFY_OK;
2812 #endif /* CONFIG_HOTPLUG_CPU */
2814 void __init page_alloc_init(void)
2816 hotcpu_notifier(page_alloc_cpu_notify, 0);
2820 * calculate_totalreserve_pages - called when sysctl_lower_zone_reserve_ratio
2821 * or min_free_kbytes changes.
2823 static void calculate_totalreserve_pages(void)
2825 struct pglist_data *pgdat;
2826 unsigned long reserve_pages = 0;
2827 enum zone_type i, j;
2829 for_each_online_pgdat(pgdat) {
2830 for (i = 0; i < MAX_NR_ZONES; i++) {
2831 struct zone *zone = pgdat->node_zones + i;
2832 unsigned long max = 0;
2834 /* Find valid and maximum lowmem_reserve in the zone */
2835 for (j = i; j < MAX_NR_ZONES; j++) {
2836 if (zone->lowmem_reserve[j] > max)
2837 max = zone->lowmem_reserve[j];
2840 /* we treat pages_high as reserved pages. */
2841 max += zone->pages_high;
2843 if (max > zone->present_pages)
2844 max = zone->present_pages;
2845 reserve_pages += max;
2848 totalreserve_pages = reserve_pages;
2852 * setup_per_zone_lowmem_reserve - called whenever
2853 * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
2854 * has a correct pages reserved value, so an adequate number of
2855 * pages are left in the zone after a successful __alloc_pages().
2857 static void setup_per_zone_lowmem_reserve(void)
2859 struct pglist_data *pgdat;
2860 enum zone_type j, idx;
2862 for_each_online_pgdat(pgdat) {
2863 for (j = 0; j < MAX_NR_ZONES; j++) {
2864 struct zone *zone = pgdat->node_zones + j;
2865 unsigned long present_pages = zone->present_pages;
2867 zone->lowmem_reserve[j] = 0;
2869 idx = j;
2870 while (idx) {
2871 struct zone *lower_zone;
2873 idx--;
2875 if (sysctl_lowmem_reserve_ratio[idx] < 1)
2876 sysctl_lowmem_reserve_ratio[idx] = 1;
2878 lower_zone = pgdat->node_zones + idx;
2879 lower_zone->lowmem_reserve[j] = present_pages /
2880 sysctl_lowmem_reserve_ratio[idx];
2881 present_pages += lower_zone->present_pages;
2886 /* update totalreserve_pages */
2887 calculate_totalreserve_pages();
2890 #if 1 // add by Victor Yu. 02-08-2007
2891 static u64 victor_do_div(u64 n, u32 base)
2893 do_div(n, base);
2894 return n;
2896 #endif
2898 * setup_per_zone_pages_min - called when min_free_kbytes changes.
2900 * Ensures that the pages_{min,low,high} values for each zone are set correctly
2901 * with respect to min_free_kbytes.
2903 void setup_per_zone_pages_min(void)
2905 unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
2906 unsigned long lowmem_pages = 0;
2907 struct zone *zone;
2908 unsigned long flags;
2910 /* Calculate total number of !ZONE_HIGHMEM pages */
2911 for_each_zone(zone) {
2912 if (!is_highmem(zone))
2913 lowmem_pages += zone->present_pages;
2917 for_each_zone(zone) {
2918 u64 tmp;
2920 spin_lock_irqsave(&zone->lru_lock, flags);
2921 tmp = (u64)pages_min * zone->present_pages;
2922 #if 0 // mask by Victor Yu. 02-08-2007
2923 do_div(tmp, lowmem_pages);
2924 #else
2925 tmp = victor_do_div(tmp, lowmem_pages);
2926 #endif
2927 if (is_highmem(zone)) {
2929 * __GFP_HIGH and PF_MEMALLOC allocations usually don't
2930 * need highmem pages, so cap pages_min to a small
2931 * value here.
2933 * The (pages_high-pages_low) and (pages_low-pages_min)
2934 * deltas controls asynch page reclaim, and so should
2935 * not be capped for highmem.
2937 int min_pages;
2939 min_pages = zone->present_pages / 1024;
2940 if (min_pages < SWAP_CLUSTER_MAX)
2941 min_pages = SWAP_CLUSTER_MAX;
2942 if (min_pages > 128)
2943 min_pages = 128;
2944 zone->pages_min = min_pages;
2945 } else {
2947 * If it's a lowmem zone, reserve a number of pages
2948 * proportionate to the zone's size.
2950 zone->pages_min = tmp;
2953 zone->pages_low = zone->pages_min + (tmp >> 2);
2954 zone->pages_high = zone->pages_min + (tmp >> 1);
2955 spin_unlock_irqrestore(&zone->lru_lock, flags);
2958 /* update totalreserve_pages */
2959 calculate_totalreserve_pages();
2963 * Initialise min_free_kbytes.
2965 * For small machines we want it small (128k min). For large machines
2966 * we want it large (64MB max). But it is not linear, because network
2967 * bandwidth does not increase linearly with machine size. We use
2969 * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
2970 * min_free_kbytes = sqrt(lowmem_kbytes * 16)
2972 * which yields
2974 * 16MB: 512k
2975 * 32MB: 724k
2976 * 64MB: 1024k
2977 * 128MB: 1448k
2978 * 256MB: 2048k
2979 * 512MB: 2896k
2980 * 1024MB: 4096k
2981 * 2048MB: 5792k
2982 * 4096MB: 8192k
2983 * 8192MB: 11584k
2984 * 16384MB: 16384k
2986 static int __init init_per_zone_pages_min(void)
2988 unsigned long lowmem_kbytes;
2990 lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
2992 min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
2993 if (min_free_kbytes < 128)
2994 min_free_kbytes = 128;
2995 if (min_free_kbytes > 65536)
2996 min_free_kbytes = 65536;
2997 setup_per_zone_pages_min();
2998 setup_per_zone_lowmem_reserve();
2999 return 0;
3001 module_init(init_per_zone_pages_min)
3004 * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
3005 * that we can call two helper functions whenever min_free_kbytes
3006 * changes.
3008 int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
3009 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
3011 proc_dointvec(table, write, file, buffer, length, ppos);
3012 setup_per_zone_pages_min();
3013 return 0;
3016 #ifdef CONFIG_NUMA
3017 int sysctl_min_unmapped_ratio_sysctl_handler(ctl_table *table, int write,
3018 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
3020 struct zone *zone;
3021 int rc;
3023 rc = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
3024 if (rc)
3025 return rc;
3027 for_each_zone(zone)
3028 zone->min_unmapped_pages = (zone->present_pages *
3029 sysctl_min_unmapped_ratio) / 100;
3030 return 0;
3033 int sysctl_min_slab_ratio_sysctl_handler(ctl_table *table, int write,
3034 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
3036 struct zone *zone;
3037 int rc;
3039 rc = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
3040 if (rc)
3041 return rc;
3043 for_each_zone(zone)
3044 zone->min_slab_pages = (zone->present_pages *
3045 sysctl_min_slab_ratio) / 100;
3046 return 0;
3048 #endif
3051 * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
3052 * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
3053 * whenever sysctl_lowmem_reserve_ratio changes.
3055 * The reserve ratio obviously has absolutely no relation with the
3056 * pages_min watermarks. The lowmem reserve ratio can only make sense
3057 * if in function of the boot time zone sizes.
3059 int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
3060 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
3062 proc_dointvec_minmax(table, write, file, buffer, length, ppos);
3063 setup_per_zone_lowmem_reserve();
3064 return 0;
3068 * percpu_pagelist_fraction - changes the pcp->high for each zone on each
3069 * cpu. It is the fraction of total pages in each zone that a hot per cpu pagelist
3070 * can have before it gets flushed back to buddy allocator.
3073 int percpu_pagelist_fraction_sysctl_handler(ctl_table *table, int write,
3074 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
3076 struct zone *zone;
3077 unsigned int cpu;
3078 int ret;
3080 ret = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
3081 if (!write || (ret == -EINVAL))
3082 return ret;
3083 for_each_zone(zone) {
3084 for_each_online_cpu(cpu) {
3085 unsigned long high;
3086 high = zone->present_pages / percpu_pagelist_fraction;
3087 setup_pagelist_highmark(zone_pcp(zone, cpu), high);
3090 return 0;
3093 int hashdist = HASHDIST_DEFAULT;
3095 #ifdef CONFIG_NUMA
3096 static int __init set_hashdist(char *str)
3098 if (!str)
3099 return 0;
3100 hashdist = simple_strtoul(str, &str, 0);
3101 return 1;
3103 __setup("hashdist=", set_hashdist);
3104 #endif
3107 * allocate a large system hash table from bootmem
3108 * - it is assumed that the hash table must contain an exact power-of-2
3109 * quantity of entries
3110 * - limit is the number of hash buckets, not the total allocation size
3112 void *__init alloc_large_system_hash(const char *tablename,
3113 unsigned long bucketsize,
3114 unsigned long numentries,
3115 int scale,
3116 int flags,
3117 unsigned int *_hash_shift,
3118 unsigned int *_hash_mask,
3119 unsigned long limit)
3121 unsigned long long max = limit;
3122 unsigned long log2qty, size;
3123 void *table = NULL;
3125 /* allow the kernel cmdline to have a say */
3126 if (!numentries) {
3127 /* round applicable memory size up to nearest megabyte */
3128 numentries = (flags & HASH_HIGHMEM) ? nr_all_pages : nr_kernel_pages;
3129 numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
3130 numentries >>= 20 - PAGE_SHIFT;
3131 numentries <<= 20 - PAGE_SHIFT;
3133 /* limit to 1 bucket per 2^scale bytes of low memory */
3134 if (scale > PAGE_SHIFT)
3135 numentries >>= (scale - PAGE_SHIFT);
3136 else
3137 numentries <<= (PAGE_SHIFT - scale);
3139 numentries = roundup_pow_of_two(numentries);
3141 /* limit allocation size to 1/16 total memory by default */
3142 if (max == 0) {
3143 max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
3144 #if 0 // mask by Victor Yu. 03-16-2007
3145 do_div(max, bucketsize);
3146 #else
3147 max = victor_do_div(max, bucketsize);
3148 #endif
3151 if (numentries > max)
3152 numentries = max;
3155 * we will allocate at least a page (even on low memory systems)
3156 * so do a fixup here to ensure we utilise the space that will be
3157 * allocated, this also prevents us reporting -ve orders
3159 if (bucketsize * numentries < PAGE_SIZE)
3160 numentries = (PAGE_SIZE + bucketsize - 1) / bucketsize;
3162 log2qty = long_log2(numentries);
3164 do {
3165 size = bucketsize << log2qty;
3166 if (flags & HASH_EARLY)
3167 table = alloc_bootmem(size);
3168 else if (hashdist)
3169 table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
3170 else {
3171 unsigned long order;
3172 for (order = 0; ((1UL << order) << PAGE_SHIFT) < size; order++)
3174 table = (void*) __get_free_pages(GFP_ATOMIC, order);
3176 } while (!table && size > PAGE_SIZE && --log2qty);
3178 if (!table)
3179 panic("Failed to allocate %s hash table\n", tablename);
3181 printk("%s hash table entries: %d (order: %d, %lu bytes)\n",
3182 tablename,
3183 (1U << log2qty),
3184 long_log2(size) - PAGE_SHIFT,
3185 size);
3187 if (_hash_shift)
3188 *_hash_shift = log2qty;
3189 if (_hash_mask)
3190 *_hash_mask = (1 << log2qty) - 1;
3192 return table;
3195 #ifdef CONFIG_OUT_OF_LINE_PFN_TO_PAGE
3196 struct page *pfn_to_page(unsigned long pfn)
3198 return __pfn_to_page(pfn);
3200 unsigned long page_to_pfn(struct page *page)
3202 return __page_to_pfn(page);
3204 EXPORT_SYMBOL(pfn_to_page);
3205 EXPORT_SYMBOL(page_to_pfn);
3206 #endif /* CONFIG_OUT_OF_LINE_PFN_TO_PAGE */
3208 #if MAX_NUMNODES > 1
3210 * Find the highest possible node id.
3212 int highest_possible_node_id(void)
3214 unsigned int node;
3215 unsigned int highest = 0;
3217 for_each_node_mask(node, node_possible_map)
3218 highest = node;
3219 return highest;
3221 EXPORT_SYMBOL(highest_possible_node_id);
3222 #endif