ia64/linux-2.6.18-xen.hg

view mm/page_alloc.c @ 452:c7ed6fe5dca0

kexec: dont initialise regions in reserve_memory()

There is no need to initialise efi_memmap_res and boot_param_res in
reserve_memory() for the initial xen domain as it is done in
machine_kexec_setup_resources() using values from the kexec hypercall.

Signed-off-by: Simon Horman <horms@verge.net.au>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Feb 28 10:55:18 2008 +0000 (2008-02-28)
parents c8002e78cb1b
children 39a8680e7a70
line source
1 /*
2 * linux/mm/page_alloc.c
3 *
4 * Manages the free list, the system allocates free pages here.
5 * Note that kmalloc() lives in slab.c
6 *
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)
15 */
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>
41 #include <asm/tlbflush.h>
42 #include <asm/div64.h>
43 #include "internal.h"
45 /*
46 * MCD - HACK: Find somewhere to initialize this EARLY, or make this
47 * initializer cleaner
48 */
49 nodemask_t node_online_map __read_mostly = { { [0] = 1UL } };
50 EXPORT_SYMBOL(node_online_map);
51 nodemask_t node_possible_map __read_mostly = NODE_MASK_ALL;
52 EXPORT_SYMBOL(node_possible_map);
53 unsigned long totalram_pages __read_mostly;
54 unsigned long totalhigh_pages __read_mostly;
55 unsigned long totalreserve_pages __read_mostly;
56 long nr_swap_pages;
57 int percpu_pagelist_fraction;
59 static void __free_pages_ok(struct page *page, unsigned int order);
61 /*
62 * results with 256, 32 in the lowmem_reserve sysctl:
63 * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
64 * 1G machine -> (16M dma, 784M normal, 224M high)
65 * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
66 * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
67 * HIGHMEM allocation will (224M+784M)/256 of ram reserved in ZONE_DMA
68 *
69 * TBD: should special case ZONE_DMA32 machines here - in those we normally
70 * don't need any ZONE_NORMAL reservation
71 */
72 int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1] = { 256, 256, 32 };
74 EXPORT_SYMBOL(totalram_pages);
76 /*
77 * Used by page_zone() to look up the address of the struct zone whose
78 * id is encoded in the upper bits of page->flags
79 */
80 struct zone *zone_table[1 << ZONETABLE_SHIFT] __read_mostly;
81 EXPORT_SYMBOL(zone_table);
83 static char *zone_names[MAX_NR_ZONES] = { "DMA", "DMA32", "Normal", "HighMem" };
84 int min_free_kbytes = 1024;
86 unsigned long __meminitdata nr_kernel_pages;
87 unsigned long __meminitdata nr_all_pages;
89 #ifdef CONFIG_DEBUG_VM
90 static int page_outside_zone_boundaries(struct zone *zone, struct page *page)
91 {
92 int ret = 0;
93 unsigned seq;
94 unsigned long pfn = page_to_pfn(page);
96 do {
97 seq = zone_span_seqbegin(zone);
98 if (pfn >= zone->zone_start_pfn + zone->spanned_pages)
99 ret = 1;
100 else if (pfn < zone->zone_start_pfn)
101 ret = 1;
102 } while (zone_span_seqretry(zone, seq));
104 return ret;
105 }
107 static int page_is_consistent(struct zone *zone, struct page *page)
108 {
109 #ifdef CONFIG_HOLES_IN_ZONE
110 if (!pfn_valid(page_to_pfn(page)))
111 return 0;
112 #endif
113 if (zone != page_zone(page))
114 return 0;
116 return 1;
117 }
118 /*
119 * Temporary debugging check for pages not lying within a given zone.
120 */
121 static int bad_range(struct zone *zone, struct page *page)
122 {
123 if (page_outside_zone_boundaries(zone, page))
124 return 1;
125 if (!page_is_consistent(zone, page))
126 return 1;
128 return 0;
129 }
131 #else
132 static inline int bad_range(struct zone *zone, struct page *page)
133 {
134 return 0;
135 }
136 #endif
138 static void bad_page(struct page *page)
139 {
140 printk(KERN_EMERG "Bad page state in process '%s'\n"
141 KERN_EMERG "page:%p flags:0x%0*lx mapping:%p mapcount:%d count:%d\n"
142 KERN_EMERG "Trying to fix it up, but a reboot is needed\n"
143 KERN_EMERG "Backtrace:\n",
144 current->comm, page, (int)(2*sizeof(unsigned long)),
145 (unsigned long)page->flags, page->mapping,
146 page_mapcount(page), page_count(page));
147 dump_stack();
148 page->flags &= ~(1 << PG_lru |
149 1 << PG_private |
150 1 << PG_locked |
151 1 << PG_active |
152 1 << PG_dirty |
153 1 << PG_reclaim |
154 1 << PG_slab |
155 1 << PG_swapcache |
156 1 << PG_writeback |
157 1 << PG_buddy |
158 #ifdef CONFIG_X86_XEN
159 1 << PG_pinned |
160 #endif
161 1 << PG_foreign );
162 set_page_count(page, 0);
163 reset_page_mapcount(page);
164 page->mapping = NULL;
165 add_taint(TAINT_BAD_PAGE);
166 }
168 /*
169 * Higher-order pages are called "compound pages". They are structured thusly:
170 *
171 * The first PAGE_SIZE page is called the "head page".
172 *
173 * The remaining PAGE_SIZE pages are called "tail pages".
174 *
175 * All pages have PG_compound set. All pages have their ->private pointing at
176 * the head page (even the head page has this).
177 *
178 * The first tail page's ->lru.next holds the address of the compound page's
179 * put_page() function. Its ->lru.prev holds the order of allocation.
180 * This usage means that zero-order pages may not be compound.
181 */
183 static void free_compound_page(struct page *page)
184 {
185 __free_pages_ok(page, (unsigned long)page[1].lru.prev);
186 }
188 static void prep_compound_page(struct page *page, unsigned long order)
189 {
190 int i;
191 int nr_pages = 1 << order;
193 page[1].lru.next = (void *)free_compound_page; /* set dtor */
194 page[1].lru.prev = (void *)order;
195 for (i = 0; i < nr_pages; i++) {
196 struct page *p = page + i;
198 __SetPageCompound(p);
199 set_page_private(p, (unsigned long)page);
200 }
201 }
203 static void destroy_compound_page(struct page *page, unsigned long order)
204 {
205 int i;
206 int nr_pages = 1 << order;
208 if (unlikely((unsigned long)page[1].lru.prev != order))
209 bad_page(page);
211 for (i = 0; i < nr_pages; i++) {
212 struct page *p = page + i;
214 if (unlikely(!PageCompound(p) |
215 (page_private(p) != (unsigned long)page)))
216 bad_page(page);
217 __ClearPageCompound(p);
218 }
219 }
221 static inline void prep_zero_page(struct page *page, int order, gfp_t gfp_flags)
222 {
223 int i;
225 BUG_ON((gfp_flags & (__GFP_WAIT | __GFP_HIGHMEM)) == __GFP_HIGHMEM);
226 /*
227 * clear_highpage() will use KM_USER0, so it's a bug to use __GFP_ZERO
228 * and __GFP_HIGHMEM from hard or soft interrupt context.
229 */
230 BUG_ON((gfp_flags & __GFP_HIGHMEM) && in_interrupt());
231 for (i = 0; i < (1 << order); i++)
232 clear_highpage(page + i);
233 }
235 /*
236 * function for dealing with page's order in buddy system.
237 * zone->lock is already acquired when we use these.
238 * So, we don't need atomic page->flags operations here.
239 */
240 static inline unsigned long page_order(struct page *page)
241 {
242 return page_private(page);
243 }
245 static inline void set_page_order(struct page *page, int order)
246 {
247 set_page_private(page, order);
248 __SetPageBuddy(page);
249 }
251 static inline void rmv_page_order(struct page *page)
252 {
253 __ClearPageBuddy(page);
254 set_page_private(page, 0);
255 }
257 /*
258 * Locate the struct page for both the matching buddy in our
259 * pair (buddy1) and the combined O(n+1) page they form (page).
260 *
261 * 1) Any buddy B1 will have an order O twin B2 which satisfies
262 * the following equation:
263 * B2 = B1 ^ (1 << O)
264 * For example, if the starting buddy (buddy2) is #8 its order
265 * 1 buddy is #10:
266 * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
267 *
268 * 2) Any buddy B will have an order O+1 parent P which
269 * satisfies the following equation:
270 * P = B & ~(1 << O)
271 *
272 * Assumption: *_mem_map is contiguous at least up to MAX_ORDER
273 */
274 static inline struct page *
275 __page_find_buddy(struct page *page, unsigned long page_idx, unsigned int order)
276 {
277 unsigned long buddy_idx = page_idx ^ (1 << order);
279 return page + (buddy_idx - page_idx);
280 }
282 static inline unsigned long
283 __find_combined_index(unsigned long page_idx, unsigned int order)
284 {
285 return (page_idx & ~(1 << order));
286 }
288 /*
289 * This function checks whether a page is free && is the buddy
290 * we can do coalesce a page and its buddy if
291 * (a) the buddy is not in a hole &&
292 * (b) the buddy is in the buddy system &&
293 * (c) a page and its buddy have the same order &&
294 * (d) a page and its buddy are in the same zone.
295 *
296 * For recording whether a page is in the buddy system, we use PG_buddy.
297 * Setting, clearing, and testing PG_buddy is serialized by zone->lock.
298 *
299 * For recording page's order, we use page_private(page).
300 */
301 static inline int page_is_buddy(struct page *page, struct page *buddy,
302 int order)
303 {
304 #ifdef CONFIG_HOLES_IN_ZONE
305 if (!pfn_valid(page_to_pfn(buddy)))
306 return 0;
307 #endif
309 if (page_zone_id(page) != page_zone_id(buddy))
310 return 0;
312 if (PageBuddy(buddy) && page_order(buddy) == order) {
313 BUG_ON(page_count(buddy) != 0);
314 return 1;
315 }
316 return 0;
317 }
319 /*
320 * Freeing function for a buddy system allocator.
321 *
322 * The concept of a buddy system is to maintain direct-mapped table
323 * (containing bit values) for memory blocks of various "orders".
324 * The bottom level table contains the map for the smallest allocatable
325 * units of memory (here, pages), and each level above it describes
326 * pairs of units from the levels below, hence, "buddies".
327 * At a high level, all that happens here is marking the table entry
328 * at the bottom level available, and propagating the changes upward
329 * as necessary, plus some accounting needed to play nicely with other
330 * parts of the VM system.
331 * At each level, we keep a list of pages, which are heads of continuous
332 * free pages of length of (1 << order) and marked with PG_buddy. Page's
333 * order is recorded in page_private(page) field.
334 * So when we are allocating or freeing one, we can derive the state of the
335 * other. That is, if we allocate a small block, and both were
336 * free, the remainder of the region must be split into blocks.
337 * If a block is freed, and its buddy is also free, then this
338 * triggers coalescing into a block of larger size.
339 *
340 * -- wli
341 */
343 static inline void __free_one_page(struct page *page,
344 struct zone *zone, unsigned int order)
345 {
346 unsigned long page_idx;
347 int order_size = 1 << order;
349 if (unlikely(PageCompound(page)))
350 destroy_compound_page(page, order);
352 page_idx = page_to_pfn(page) & ((1 << MAX_ORDER) - 1);
354 BUG_ON(page_idx & (order_size - 1));
355 BUG_ON(bad_range(zone, page));
357 zone->free_pages += order_size;
358 while (order < MAX_ORDER-1) {
359 unsigned long combined_idx;
360 struct free_area *area;
361 struct page *buddy;
363 buddy = __page_find_buddy(page, page_idx, order);
364 if (!page_is_buddy(page, buddy, order))
365 break; /* Move the buddy up one level. */
367 list_del(&buddy->lru);
368 area = zone->free_area + order;
369 area->nr_free--;
370 rmv_page_order(buddy);
371 combined_idx = __find_combined_index(page_idx, order);
372 page = page + (combined_idx - page_idx);
373 page_idx = combined_idx;
374 order++;
375 }
376 set_page_order(page, order);
377 list_add(&page->lru, &zone->free_area[order].free_list);
378 zone->free_area[order].nr_free++;
379 }
381 static inline int free_pages_check(struct page *page)
382 {
383 if (unlikely(page_mapcount(page) |
384 (page->mapping != NULL) |
385 (page_count(page) != 0) |
386 (page->flags & (
387 1 << PG_lru |
388 1 << PG_private |
389 1 << PG_locked |
390 1 << PG_active |
391 1 << PG_reclaim |
392 1 << PG_slab |
393 1 << PG_swapcache |
394 1 << PG_writeback |
395 1 << PG_reserved |
396 1 << PG_buddy |
397 #ifdef CONFIG_X86_XEN
398 1 << PG_pinned |
399 #endif
400 1 << PG_foreign ))))
401 bad_page(page);
402 if (PageDirty(page))
403 __ClearPageDirty(page);
404 /*
405 * For now, we report if PG_reserved was found set, but do not
406 * clear it, and do not free the page. But we shall soon need
407 * to do more, for when the ZERO_PAGE count wraps negative.
408 */
409 return PageReserved(page);
410 }
412 /*
413 * Frees a list of pages.
414 * Assumes all pages on list are in same zone, and of same order.
415 * count is the number of pages to free.
416 *
417 * If the zone was previously in an "all pages pinned" state then look to
418 * see if this freeing clears that state.
419 *
420 * And clear the zone's pages_scanned counter, to hold off the "all pages are
421 * pinned" detection logic.
422 */
423 static void free_pages_bulk(struct zone *zone, int count,
424 struct list_head *list, int order)
425 {
426 spin_lock(&zone->lock);
427 zone->all_unreclaimable = 0;
428 zone->pages_scanned = 0;
429 while (count--) {
430 struct page *page;
432 BUG_ON(list_empty(list));
433 page = list_entry(list->prev, struct page, lru);
434 /* have to delete it as __free_one_page list manipulates */
435 list_del(&page->lru);
436 __free_one_page(page, zone, order);
437 }
438 spin_unlock(&zone->lock);
439 }
441 static void free_one_page(struct zone *zone, struct page *page, int order)
442 {
443 LIST_HEAD(list);
444 list_add(&page->lru, &list);
445 free_pages_bulk(zone, 1, &list, order);
446 }
448 static void __free_pages_ok(struct page *page, unsigned int order)
449 {
450 unsigned long flags;
451 int i;
452 int reserved = 0;
454 #ifdef CONFIG_XEN
455 if (PageForeign(page)) {
456 PageForeignDestructor(page);
457 return;
458 }
459 #endif
460 arch_free_page(page, order);
461 if (!PageHighMem(page))
462 debug_check_no_locks_freed(page_address(page),
463 PAGE_SIZE<<order);
465 for (i = 0 ; i < (1 << order) ; ++i)
466 reserved += free_pages_check(page + i);
467 if (reserved)
468 return;
470 kernel_map_pages(page, 1 << order, 0);
471 local_irq_save(flags);
472 __count_vm_events(PGFREE, 1 << order);
473 free_one_page(page_zone(page), page, order);
474 local_irq_restore(flags);
475 }
477 /*
478 * permit the bootmem allocator to evade page validation on high-order frees
479 */
480 void fastcall __init __free_pages_bootmem(struct page *page, unsigned int order)
481 {
482 if (order == 0) {
483 __ClearPageReserved(page);
484 set_page_count(page, 0);
485 set_page_refcounted(page);
486 __free_page(page);
487 } else {
488 int loop;
490 prefetchw(page);
491 for (loop = 0; loop < BITS_PER_LONG; loop++) {
492 struct page *p = &page[loop];
494 if (loop + 1 < BITS_PER_LONG)
495 prefetchw(p + 1);
496 __ClearPageReserved(p);
497 set_page_count(p, 0);
498 }
500 set_page_refcounted(page);
501 __free_pages(page, order);
502 }
503 }
506 /*
507 * The order of subdivision here is critical for the IO subsystem.
508 * Please do not alter this order without good reasons and regression
509 * testing. Specifically, as large blocks of memory are subdivided,
510 * the order in which smaller blocks are delivered depends on the order
511 * they're subdivided in this function. This is the primary factor
512 * influencing the order in which pages are delivered to the IO
513 * subsystem according to empirical testing, and this is also justified
514 * by considering the behavior of a buddy system containing a single
515 * large block of memory acted on by a series of small allocations.
516 * This behavior is a critical factor in sglist merging's success.
517 *
518 * -- wli
519 */
520 static inline void expand(struct zone *zone, struct page *page,
521 int low, int high, struct free_area *area)
522 {
523 unsigned long size = 1 << high;
525 while (high > low) {
526 area--;
527 high--;
528 size >>= 1;
529 BUG_ON(bad_range(zone, &page[size]));
530 list_add(&page[size].lru, &area->free_list);
531 area->nr_free++;
532 set_page_order(&page[size], high);
533 }
534 }
536 /*
537 * This page is about to be returned from the page allocator
538 */
539 static int prep_new_page(struct page *page, int order, gfp_t gfp_flags)
540 {
541 if (unlikely(page_mapcount(page) |
542 (page->mapping != NULL) |
543 (page_count(page) != 0) |
544 (page->flags & (
545 1 << PG_lru |
546 1 << PG_private |
547 1 << PG_locked |
548 1 << PG_active |
549 1 << PG_dirty |
550 1 << PG_reclaim |
551 1 << PG_slab |
552 1 << PG_swapcache |
553 1 << PG_writeback |
554 1 << PG_reserved |
555 1 << PG_buddy |
556 #ifdef CONFIG_X86_XEN
557 1 << PG_pinned |
558 #endif
559 1 << PG_foreign ))))
560 bad_page(page);
562 /*
563 * For now, we report if PG_reserved was found set, but do not
564 * clear it, and do not allocate the page: as a safety net.
565 */
566 if (PageReserved(page))
567 return 1;
569 page->flags &= ~(1 << PG_uptodate | 1 << PG_error |
570 1 << PG_referenced | 1 << PG_arch_1 |
571 1 << PG_checked | 1 << PG_mappedtodisk);
572 set_page_private(page, 0);
573 set_page_refcounted(page);
574 kernel_map_pages(page, 1 << order, 1);
576 if (gfp_flags & __GFP_ZERO)
577 prep_zero_page(page, order, gfp_flags);
579 if (order && (gfp_flags & __GFP_COMP))
580 prep_compound_page(page, order);
582 return 0;
583 }
585 /*
586 * Do the hard work of removing an element from the buddy allocator.
587 * Call me with the zone->lock already held.
588 */
589 static struct page *__rmqueue(struct zone *zone, unsigned int order)
590 {
591 struct free_area * area;
592 unsigned int current_order;
593 struct page *page;
595 for (current_order = order; current_order < MAX_ORDER; ++current_order) {
596 area = zone->free_area + current_order;
597 if (list_empty(&area->free_list))
598 continue;
600 page = list_entry(area->free_list.next, struct page, lru);
601 list_del(&page->lru);
602 rmv_page_order(page);
603 area->nr_free--;
604 zone->free_pages -= 1UL << order;
605 expand(zone, page, order, current_order, area);
606 return page;
607 }
609 return NULL;
610 }
612 /*
613 * Obtain a specified number of elements from the buddy allocator, all under
614 * a single hold of the lock, for efficiency. Add them to the supplied list.
615 * Returns the number of new pages which were placed at *list.
616 */
617 static int rmqueue_bulk(struct zone *zone, unsigned int order,
618 unsigned long count, struct list_head *list)
619 {
620 int i;
622 spin_lock(&zone->lock);
623 for (i = 0; i < count; ++i) {
624 struct page *page = __rmqueue(zone, order);
625 if (unlikely(page == NULL))
626 break;
627 list_add_tail(&page->lru, list);
628 }
629 spin_unlock(&zone->lock);
630 return i;
631 }
633 #ifdef CONFIG_NUMA
634 /*
635 * Called from the slab reaper to drain pagesets on a particular node that
636 * belong to the currently executing processor.
637 * Note that this function must be called with the thread pinned to
638 * a single processor.
639 */
640 void drain_node_pages(int nodeid)
641 {
642 int i, z;
643 unsigned long flags;
645 for (z = 0; z < MAX_NR_ZONES; z++) {
646 struct zone *zone = NODE_DATA(nodeid)->node_zones + z;
647 struct per_cpu_pageset *pset;
649 pset = zone_pcp(zone, smp_processor_id());
650 for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
651 struct per_cpu_pages *pcp;
653 pcp = &pset->pcp[i];
654 if (pcp->count) {
655 local_irq_save(flags);
656 free_pages_bulk(zone, pcp->count, &pcp->list, 0);
657 pcp->count = 0;
658 local_irq_restore(flags);
659 }
660 }
661 }
662 }
663 #endif
665 #if defined(CONFIG_PM) || defined(CONFIG_HOTPLUG_CPU)
666 static void __drain_pages(unsigned int cpu)
667 {
668 unsigned long flags;
669 struct zone *zone;
670 int i;
672 for_each_zone(zone) {
673 struct per_cpu_pageset *pset;
675 pset = zone_pcp(zone, cpu);
676 for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
677 struct per_cpu_pages *pcp;
679 pcp = &pset->pcp[i];
680 local_irq_save(flags);
681 free_pages_bulk(zone, pcp->count, &pcp->list, 0);
682 pcp->count = 0;
683 local_irq_restore(flags);
684 }
685 }
686 }
687 #endif /* CONFIG_PM || CONFIG_HOTPLUG_CPU */
689 #ifdef CONFIG_PM
691 void mark_free_pages(struct zone *zone)
692 {
693 unsigned long zone_pfn, flags;
694 int order;
695 struct list_head *curr;
697 if (!zone->spanned_pages)
698 return;
700 spin_lock_irqsave(&zone->lock, flags);
701 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
702 ClearPageNosaveFree(pfn_to_page(zone_pfn + zone->zone_start_pfn));
704 for (order = MAX_ORDER - 1; order >= 0; --order)
705 list_for_each(curr, &zone->free_area[order].free_list) {
706 unsigned long start_pfn, i;
708 start_pfn = page_to_pfn(list_entry(curr, struct page, lru));
710 for (i=0; i < (1<<order); i++)
711 SetPageNosaveFree(pfn_to_page(start_pfn+i));
712 }
713 spin_unlock_irqrestore(&zone->lock, flags);
714 }
716 /*
717 * Spill all of this CPU's per-cpu pages back into the buddy allocator.
718 */
719 void drain_local_pages(void)
720 {
721 unsigned long flags;
723 local_irq_save(flags);
724 __drain_pages(smp_processor_id());
725 local_irq_restore(flags);
726 }
727 #endif /* CONFIG_PM */
729 /*
730 * Free a 0-order page
731 */
732 static void fastcall free_hot_cold_page(struct page *page, int cold)
733 {
734 struct zone *zone = page_zone(page);
735 struct per_cpu_pages *pcp;
736 unsigned long flags;
738 #ifdef CONFIG_XEN
739 if (PageForeign(page)) {
740 PageForeignDestructor(page);
741 return;
742 }
743 #endif
744 arch_free_page(page, 0);
746 if (PageAnon(page))
747 page->mapping = NULL;
748 if (free_pages_check(page))
749 return;
751 kernel_map_pages(page, 1, 0);
753 pcp = &zone_pcp(zone, get_cpu())->pcp[cold];
754 local_irq_save(flags);
755 __count_vm_event(PGFREE);
756 list_add(&page->lru, &pcp->list);
757 pcp->count++;
758 if (pcp->count >= pcp->high) {
759 free_pages_bulk(zone, pcp->batch, &pcp->list, 0);
760 pcp->count -= pcp->batch;
761 }
762 local_irq_restore(flags);
763 put_cpu();
764 }
766 void fastcall free_hot_page(struct page *page)
767 {
768 free_hot_cold_page(page, 0);
769 }
771 void fastcall free_cold_page(struct page *page)
772 {
773 free_hot_cold_page(page, 1);
774 }
776 /*
777 * split_page takes a non-compound higher-order page, and splits it into
778 * n (1<<order) sub-pages: page[0..n]
779 * Each sub-page must be freed individually.
780 *
781 * Note: this is probably too low level an operation for use in drivers.
782 * Please consult with lkml before using this in your driver.
783 */
784 void split_page(struct page *page, unsigned int order)
785 {
786 int i;
788 BUG_ON(PageCompound(page));
789 BUG_ON(!page_count(page));
790 for (i = 1; i < (1 << order); i++)
791 set_page_refcounted(page + i);
792 }
794 /*
795 * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
796 * we cheat by calling it from here, in the order > 0 path. Saves a branch
797 * or two.
798 */
799 static struct page *buffered_rmqueue(struct zonelist *zonelist,
800 struct zone *zone, int order, gfp_t gfp_flags)
801 {
802 unsigned long flags;
803 struct page *page;
804 int cold = !!(gfp_flags & __GFP_COLD);
805 int cpu;
807 again:
808 cpu = get_cpu();
809 if (likely(order == 0)) {
810 struct per_cpu_pages *pcp;
812 pcp = &zone_pcp(zone, cpu)->pcp[cold];
813 local_irq_save(flags);
814 if (!pcp->count) {
815 pcp->count += rmqueue_bulk(zone, 0,
816 pcp->batch, &pcp->list);
817 if (unlikely(!pcp->count))
818 goto failed;
819 }
820 page = list_entry(pcp->list.next, struct page, lru);
821 list_del(&page->lru);
822 pcp->count--;
823 } else {
824 spin_lock_irqsave(&zone->lock, flags);
825 page = __rmqueue(zone, order);
826 spin_unlock(&zone->lock);
827 if (!page)
828 goto failed;
829 }
831 __count_zone_vm_events(PGALLOC, zone, 1 << order);
832 zone_statistics(zonelist, zone);
833 local_irq_restore(flags);
834 put_cpu();
836 BUG_ON(bad_range(zone, page));
837 if (prep_new_page(page, order, gfp_flags))
838 goto again;
839 return page;
841 failed:
842 local_irq_restore(flags);
843 put_cpu();
844 return NULL;
845 }
847 #define ALLOC_NO_WATERMARKS 0x01 /* don't check watermarks at all */
848 #define ALLOC_WMARK_MIN 0x02 /* use pages_min watermark */
849 #define ALLOC_WMARK_LOW 0x04 /* use pages_low watermark */
850 #define ALLOC_WMARK_HIGH 0x08 /* use pages_high watermark */
851 #define ALLOC_HARDER 0x10 /* try to alloc harder */
852 #define ALLOC_HIGH 0x20 /* __GFP_HIGH set */
853 #define ALLOC_CPUSET 0x40 /* check for correct cpuset */
855 /*
856 * Return 1 if free pages are above 'mark'. This takes into account the order
857 * of the allocation.
858 */
859 int zone_watermark_ok(struct zone *z, int order, unsigned long mark,
860 int classzone_idx, int alloc_flags)
861 {
862 /* free_pages my go negative - that's OK */
863 long min = mark, free_pages = z->free_pages - (1 << order) + 1;
864 int o;
866 if (alloc_flags & ALLOC_HIGH)
867 min -= min / 2;
868 if (alloc_flags & ALLOC_HARDER)
869 min -= min / 4;
871 if (free_pages <= min + z->lowmem_reserve[classzone_idx])
872 return 0;
873 for (o = 0; o < order; o++) {
874 /* At the next order, this order's pages become unavailable */
875 free_pages -= z->free_area[o].nr_free << o;
877 /* Require fewer higher order pages to be free */
878 min >>= 1;
880 if (free_pages <= min)
881 return 0;
882 }
883 return 1;
884 }
886 /*
887 * get_page_from_freeliest goes through the zonelist trying to allocate
888 * a page.
889 */
890 static struct page *
891 get_page_from_freelist(gfp_t gfp_mask, unsigned int order,
892 struct zonelist *zonelist, int alloc_flags)
893 {
894 struct zone **z = zonelist->zones;
895 struct page *page = NULL;
896 int classzone_idx = zone_idx(*z);
898 /*
899 * Go through the zonelist once, looking for a zone with enough free.
900 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
901 */
902 do {
903 if ((alloc_flags & ALLOC_CPUSET) &&
904 !cpuset_zone_allowed(*z, gfp_mask))
905 continue;
907 if (!(alloc_flags & ALLOC_NO_WATERMARKS)) {
908 unsigned long mark;
909 if (alloc_flags & ALLOC_WMARK_MIN)
910 mark = (*z)->pages_min;
911 else if (alloc_flags & ALLOC_WMARK_LOW)
912 mark = (*z)->pages_low;
913 else
914 mark = (*z)->pages_high;
915 if (!zone_watermark_ok(*z, order, mark,
916 classzone_idx, alloc_flags))
917 if (!zone_reclaim_mode ||
918 !zone_reclaim(*z, gfp_mask, order))
919 continue;
920 }
922 page = buffered_rmqueue(zonelist, *z, order, gfp_mask);
923 if (page) {
924 break;
925 }
926 } while (*(++z) != NULL);
927 return page;
928 }
930 /*
931 * This is the 'heart' of the zoned buddy allocator.
932 */
933 struct page * fastcall
934 __alloc_pages(gfp_t gfp_mask, unsigned int order,
935 struct zonelist *zonelist)
936 {
937 const gfp_t wait = gfp_mask & __GFP_WAIT;
938 struct zone **z;
939 struct page *page;
940 struct reclaim_state reclaim_state;
941 struct task_struct *p = current;
942 int do_retry;
943 int alloc_flags;
944 int did_some_progress;
946 might_sleep_if(wait);
948 restart:
949 z = zonelist->zones; /* the list of zones suitable for gfp_mask */
951 if (unlikely(*z == NULL)) {
952 /* Should this ever happen?? */
953 return NULL;
954 }
956 page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, order,
957 zonelist, ALLOC_WMARK_LOW|ALLOC_CPUSET);
958 if (page)
959 goto got_pg;
961 do {
962 wakeup_kswapd(*z, order);
963 } while (*(++z));
965 /*
966 * OK, we're below the kswapd watermark and have kicked background
967 * reclaim. Now things get more complex, so set up alloc_flags according
968 * to how we want to proceed.
969 *
970 * The caller may dip into page reserves a bit more if the caller
971 * cannot run direct reclaim, or if the caller has realtime scheduling
972 * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will
973 * set both ALLOC_HARDER (!wait) and ALLOC_HIGH (__GFP_HIGH).
974 */
975 alloc_flags = ALLOC_WMARK_MIN;
976 if ((unlikely(rt_task(p)) && !in_interrupt()) || !wait)
977 alloc_flags |= ALLOC_HARDER;
978 if (gfp_mask & __GFP_HIGH)
979 alloc_flags |= ALLOC_HIGH;
980 if (wait)
981 alloc_flags |= ALLOC_CPUSET;
983 /*
984 * Go through the zonelist again. Let __GFP_HIGH and allocations
985 * coming from realtime tasks go deeper into reserves.
986 *
987 * This is the last chance, in general, before the goto nopage.
988 * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
989 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
990 */
991 page = get_page_from_freelist(gfp_mask, order, zonelist, alloc_flags);
992 if (page)
993 goto got_pg;
995 /* This allocation should allow future memory freeing. */
997 if (((p->flags & PF_MEMALLOC) || unlikely(test_thread_flag(TIF_MEMDIE)))
998 && !in_interrupt()) {
999 if (!(gfp_mask & __GFP_NOMEMALLOC)) {
1000 nofail_alloc:
1001 /* go through the zonelist yet again, ignoring mins */
1002 page = get_page_from_freelist(gfp_mask, order,
1003 zonelist, ALLOC_NO_WATERMARKS);
1004 if (page)
1005 goto got_pg;
1006 if (gfp_mask & __GFP_NOFAIL) {
1007 blk_congestion_wait(WRITE, HZ/50);
1008 goto nofail_alloc;
1011 goto nopage;
1014 /* Atomic allocations - we can't balance anything */
1015 if (!wait)
1016 goto nopage;
1018 rebalance:
1019 cond_resched();
1021 /* We now go into synchronous reclaim */
1022 cpuset_memory_pressure_bump();
1023 p->flags |= PF_MEMALLOC;
1024 reclaim_state.reclaimed_slab = 0;
1025 p->reclaim_state = &reclaim_state;
1027 did_some_progress = try_to_free_pages(zonelist->zones, gfp_mask);
1029 p->reclaim_state = NULL;
1030 p->flags &= ~PF_MEMALLOC;
1032 cond_resched();
1034 if (likely(did_some_progress)) {
1035 page = get_page_from_freelist(gfp_mask, order,
1036 zonelist, alloc_flags);
1037 if (page)
1038 goto got_pg;
1039 } else if ((gfp_mask & __GFP_FS) && !(gfp_mask & __GFP_NORETRY)) {
1040 /*
1041 * Go through the zonelist yet one more time, keep
1042 * very high watermark here, this is only to catch
1043 * a parallel oom killing, we must fail if we're still
1044 * under heavy pressure.
1045 */
1046 page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, order,
1047 zonelist, ALLOC_WMARK_HIGH|ALLOC_CPUSET);
1048 if (page)
1049 goto got_pg;
1051 out_of_memory(zonelist, gfp_mask, order);
1052 goto restart;
1055 /*
1056 * Don't let big-order allocations loop unless the caller explicitly
1057 * requests that. Wait for some write requests to complete then retry.
1059 * In this implementation, __GFP_REPEAT means __GFP_NOFAIL for order
1060 * <= 3, but that may not be true in other implementations.
1061 */
1062 do_retry = 0;
1063 if (!(gfp_mask & __GFP_NORETRY)) {
1064 if ((order <= 3) || (gfp_mask & __GFP_REPEAT))
1065 do_retry = 1;
1066 if (gfp_mask & __GFP_NOFAIL)
1067 do_retry = 1;
1069 if (do_retry) {
1070 blk_congestion_wait(WRITE, HZ/50);
1071 goto rebalance;
1074 nopage:
1075 if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) {
1076 printk(KERN_WARNING "%s: page allocation failure."
1077 " order:%d, mode:0x%x\n",
1078 p->comm, order, gfp_mask);
1079 dump_stack();
1080 show_mem();
1082 got_pg:
1083 return page;
1086 EXPORT_SYMBOL(__alloc_pages);
1088 /*
1089 * Common helper functions.
1090 */
1091 fastcall unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
1093 struct page * page;
1094 page = alloc_pages(gfp_mask, order);
1095 if (!page)
1096 return 0;
1097 return (unsigned long) page_address(page);
1100 EXPORT_SYMBOL(__get_free_pages);
1102 fastcall unsigned long get_zeroed_page(gfp_t gfp_mask)
1104 struct page * page;
1106 /*
1107 * get_zeroed_page() returns a 32-bit address, which cannot represent
1108 * a highmem page
1109 */
1110 BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
1112 page = alloc_pages(gfp_mask | __GFP_ZERO, 0);
1113 if (page)
1114 return (unsigned long) page_address(page);
1115 return 0;
1118 EXPORT_SYMBOL(get_zeroed_page);
1120 void __pagevec_free(struct pagevec *pvec)
1122 int i = pagevec_count(pvec);
1124 while (--i >= 0)
1125 free_hot_cold_page(pvec->pages[i], pvec->cold);
1128 fastcall void __free_pages(struct page *page, unsigned int order)
1130 if (put_page_testzero(page)) {
1131 if (order == 0)
1132 free_hot_page(page);
1133 else
1134 __free_pages_ok(page, order);
1138 EXPORT_SYMBOL(__free_pages);
1140 fastcall void free_pages(unsigned long addr, unsigned int order)
1142 if (addr != 0) {
1143 BUG_ON(!virt_addr_valid((void *)addr));
1144 __free_pages(virt_to_page((void *)addr), order);
1148 EXPORT_SYMBOL(free_pages);
1150 /*
1151 * Total amount of free (allocatable) RAM:
1152 */
1153 unsigned int nr_free_pages(void)
1155 unsigned int sum = 0;
1156 struct zone *zone;
1158 for_each_zone(zone)
1159 sum += zone->free_pages;
1161 return sum;
1164 EXPORT_SYMBOL(nr_free_pages);
1166 #ifdef CONFIG_NUMA
1167 unsigned int nr_free_pages_pgdat(pg_data_t *pgdat)
1169 unsigned int i, sum = 0;
1171 for (i = 0; i < MAX_NR_ZONES; i++)
1172 sum += pgdat->node_zones[i].free_pages;
1174 return sum;
1176 #endif
1178 static unsigned int nr_free_zone_pages(int offset)
1180 /* Just pick one node, since fallback list is circular */
1181 pg_data_t *pgdat = NODE_DATA(numa_node_id());
1182 unsigned int sum = 0;
1184 struct zonelist *zonelist = pgdat->node_zonelists + offset;
1185 struct zone **zonep = zonelist->zones;
1186 struct zone *zone;
1188 for (zone = *zonep++; zone; zone = *zonep++) {
1189 unsigned long size = zone->present_pages;
1190 unsigned long high = zone->pages_high;
1191 if (size > high)
1192 sum += size - high;
1195 return sum;
1198 /*
1199 * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
1200 */
1201 unsigned int nr_free_buffer_pages(void)
1203 return nr_free_zone_pages(gfp_zone(GFP_USER));
1206 /*
1207 * Amount of free RAM allocatable within all zones
1208 */
1209 unsigned int nr_free_pagecache_pages(void)
1211 return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER));
1214 #ifdef CONFIG_HIGHMEM
1215 unsigned int nr_free_highpages (void)
1217 pg_data_t *pgdat;
1218 unsigned int pages = 0;
1220 for_each_online_pgdat(pgdat)
1221 pages += pgdat->node_zones[ZONE_HIGHMEM].free_pages;
1223 return pages;
1225 #endif
1227 #ifdef CONFIG_NUMA
1228 static void show_node(struct zone *zone)
1230 printk("Node %d ", zone->zone_pgdat->node_id);
1232 #else
1233 #define show_node(zone) do { } while (0)
1234 #endif
1236 void si_meminfo(struct sysinfo *val)
1238 val->totalram = totalram_pages;
1239 val->sharedram = 0;
1240 val->freeram = nr_free_pages();
1241 val->bufferram = nr_blockdev_pages();
1242 #ifdef CONFIG_HIGHMEM
1243 val->totalhigh = totalhigh_pages;
1244 val->freehigh = nr_free_highpages();
1245 #else
1246 val->totalhigh = 0;
1247 val->freehigh = 0;
1248 #endif
1249 val->mem_unit = PAGE_SIZE;
1252 EXPORT_SYMBOL(si_meminfo);
1254 #ifdef CONFIG_NUMA
1255 void si_meminfo_node(struct sysinfo *val, int nid)
1257 pg_data_t *pgdat = NODE_DATA(nid);
1259 val->totalram = pgdat->node_present_pages;
1260 val->freeram = nr_free_pages_pgdat(pgdat);
1261 val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].present_pages;
1262 val->freehigh = pgdat->node_zones[ZONE_HIGHMEM].free_pages;
1263 val->mem_unit = PAGE_SIZE;
1265 #endif
1267 #define K(x) ((x) << (PAGE_SHIFT-10))
1269 /*
1270 * Show free area list (used inside shift_scroll-lock stuff)
1271 * We also calculate the percentage fragmentation. We do this by counting the
1272 * memory on each free list with the exception of the first item on the list.
1273 */
1274 void show_free_areas(void)
1276 int cpu, temperature;
1277 unsigned long active;
1278 unsigned long inactive;
1279 unsigned long free;
1280 struct zone *zone;
1282 for_each_zone(zone) {
1283 show_node(zone);
1284 printk("%s per-cpu:", zone->name);
1286 if (!populated_zone(zone)) {
1287 printk(" empty\n");
1288 continue;
1289 } else
1290 printk("\n");
1292 for_each_online_cpu(cpu) {
1293 struct per_cpu_pageset *pageset;
1295 pageset = zone_pcp(zone, cpu);
1297 for (temperature = 0; temperature < 2; temperature++)
1298 printk("cpu %d %s: high %d, batch %d used:%d\n",
1299 cpu,
1300 temperature ? "cold" : "hot",
1301 pageset->pcp[temperature].high,
1302 pageset->pcp[temperature].batch,
1303 pageset->pcp[temperature].count);
1307 get_zone_counts(&active, &inactive, &free);
1309 printk("Free pages: %11ukB (%ukB HighMem)\n",
1310 K(nr_free_pages()),
1311 K(nr_free_highpages()));
1313 printk("Active:%lu inactive:%lu dirty:%lu writeback:%lu "
1314 "unstable:%lu free:%u slab:%lu mapped:%lu pagetables:%lu\n",
1315 active,
1316 inactive,
1317 global_page_state(NR_FILE_DIRTY),
1318 global_page_state(NR_WRITEBACK),
1319 global_page_state(NR_UNSTABLE_NFS),
1320 nr_free_pages(),
1321 global_page_state(NR_SLAB),
1322 global_page_state(NR_FILE_MAPPED),
1323 global_page_state(NR_PAGETABLE));
1325 for_each_zone(zone) {
1326 int i;
1328 show_node(zone);
1329 printk("%s"
1330 " free:%lukB"
1331 " min:%lukB"
1332 " low:%lukB"
1333 " high:%lukB"
1334 " active:%lukB"
1335 " inactive:%lukB"
1336 " present:%lukB"
1337 " pages_scanned:%lu"
1338 " all_unreclaimable? %s"
1339 "\n",
1340 zone->name,
1341 K(zone->free_pages),
1342 K(zone->pages_min),
1343 K(zone->pages_low),
1344 K(zone->pages_high),
1345 K(zone->nr_active),
1346 K(zone->nr_inactive),
1347 K(zone->present_pages),
1348 zone->pages_scanned,
1349 (zone->all_unreclaimable ? "yes" : "no")
1350 );
1351 printk("lowmem_reserve[]:");
1352 for (i = 0; i < MAX_NR_ZONES; i++)
1353 printk(" %lu", zone->lowmem_reserve[i]);
1354 printk("\n");
1357 for_each_zone(zone) {
1358 unsigned long nr[MAX_ORDER], flags, order, total = 0;
1360 show_node(zone);
1361 printk("%s: ", zone->name);
1362 if (!populated_zone(zone)) {
1363 printk("empty\n");
1364 continue;
1367 spin_lock_irqsave(&zone->lock, flags);
1368 for (order = 0; order < MAX_ORDER; order++) {
1369 nr[order] = zone->free_area[order].nr_free;
1370 total += nr[order] << order;
1372 spin_unlock_irqrestore(&zone->lock, flags);
1373 for (order = 0; order < MAX_ORDER; order++)
1374 printk("%lu*%lukB ", nr[order], K(1UL) << order);
1375 printk("= %lukB\n", K(total));
1378 show_swap_cache_info();
1381 /*
1382 * Builds allocation fallback zone lists.
1384 * Add all populated zones of a node to the zonelist.
1385 */
1386 static int __meminit build_zonelists_node(pg_data_t *pgdat,
1387 struct zonelist *zonelist, int nr_zones, int zone_type)
1389 struct zone *zone;
1391 BUG_ON(zone_type > ZONE_HIGHMEM);
1393 do {
1394 zone = pgdat->node_zones + zone_type;
1395 if (populated_zone(zone)) {
1396 #ifndef CONFIG_HIGHMEM
1397 BUG_ON(zone_type > ZONE_NORMAL);
1398 #endif
1399 zonelist->zones[nr_zones++] = zone;
1400 check_highest_zone(zone_type);
1402 zone_type--;
1404 } while (zone_type >= 0);
1405 return nr_zones;
1408 static inline int highest_zone(int zone_bits)
1410 int res = ZONE_NORMAL;
1411 if (zone_bits & (__force int)__GFP_HIGHMEM)
1412 res = ZONE_HIGHMEM;
1413 if (zone_bits & (__force int)__GFP_DMA32)
1414 res = ZONE_DMA32;
1415 if (zone_bits & (__force int)__GFP_DMA)
1416 res = ZONE_DMA;
1417 return res;
1420 #ifdef CONFIG_NUMA
1421 #define MAX_NODE_LOAD (num_online_nodes())
1422 static int __meminitdata node_load[MAX_NUMNODES];
1423 /**
1424 * find_next_best_node - find the next node that should appear in a given node's fallback list
1425 * @node: node whose fallback list we're appending
1426 * @used_node_mask: nodemask_t of already used nodes
1428 * We use a number of factors to determine which is the next node that should
1429 * appear on a given node's fallback list. The node should not have appeared
1430 * already in @node's fallback list, and it should be the next closest node
1431 * according to the distance array (which contains arbitrary distance values
1432 * from each node to each node in the system), and should also prefer nodes
1433 * with no CPUs, since presumably they'll have very little allocation pressure
1434 * on them otherwise.
1435 * It returns -1 if no node is found.
1436 */
1437 static int __meminit find_next_best_node(int node, nodemask_t *used_node_mask)
1439 int n, val;
1440 int min_val = INT_MAX;
1441 int best_node = -1;
1443 /* Use the local node if we haven't already */
1444 if (!node_isset(node, *used_node_mask)) {
1445 node_set(node, *used_node_mask);
1446 return node;
1449 for_each_online_node(n) {
1450 cpumask_t tmp;
1452 /* Don't want a node to appear more than once */
1453 if (node_isset(n, *used_node_mask))
1454 continue;
1456 /* Use the distance array to find the distance */
1457 val = node_distance(node, n);
1459 /* Penalize nodes under us ("prefer the next node") */
1460 val += (n < node);
1462 /* Give preference to headless and unused nodes */
1463 tmp = node_to_cpumask(n);
1464 if (!cpus_empty(tmp))
1465 val += PENALTY_FOR_NODE_WITH_CPUS;
1467 /* Slight preference for less loaded node */
1468 val *= (MAX_NODE_LOAD*MAX_NUMNODES);
1469 val += node_load[n];
1471 if (val < min_val) {
1472 min_val = val;
1473 best_node = n;
1477 if (best_node >= 0)
1478 node_set(best_node, *used_node_mask);
1480 return best_node;
1483 static void __meminit build_zonelists(pg_data_t *pgdat)
1485 int i, j, k, node, local_node;
1486 int prev_node, load;
1487 struct zonelist *zonelist;
1488 nodemask_t used_mask;
1490 /* initialize zonelists */
1491 for (i = 0; i < GFP_ZONETYPES; i++) {
1492 zonelist = pgdat->node_zonelists + i;
1493 zonelist->zones[0] = NULL;
1496 /* NUMA-aware ordering of nodes */
1497 local_node = pgdat->node_id;
1498 load = num_online_nodes();
1499 prev_node = local_node;
1500 nodes_clear(used_mask);
1501 while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
1502 int distance = node_distance(local_node, node);
1504 /*
1505 * If another node is sufficiently far away then it is better
1506 * to reclaim pages in a zone before going off node.
1507 */
1508 if (distance > RECLAIM_DISTANCE)
1509 zone_reclaim_mode = 1;
1511 /*
1512 * We don't want to pressure a particular node.
1513 * So adding penalty to the first node in same
1514 * distance group to make it round-robin.
1515 */
1517 if (distance != node_distance(local_node, prev_node))
1518 node_load[node] += load;
1519 prev_node = node;
1520 load--;
1521 for (i = 0; i < GFP_ZONETYPES; i++) {
1522 zonelist = pgdat->node_zonelists + i;
1523 for (j = 0; zonelist->zones[j] != NULL; j++);
1525 k = highest_zone(i);
1527 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1528 zonelist->zones[j] = NULL;
1533 #else /* CONFIG_NUMA */
1535 static void __meminit build_zonelists(pg_data_t *pgdat)
1537 int i, j, k, node, local_node;
1539 local_node = pgdat->node_id;
1540 for (i = 0; i < GFP_ZONETYPES; i++) {
1541 struct zonelist *zonelist;
1543 zonelist = pgdat->node_zonelists + i;
1545 j = 0;
1546 k = highest_zone(i);
1547 j = build_zonelists_node(pgdat, zonelist, j, k);
1548 /*
1549 * Now we build the zonelist so that it contains the zones
1550 * of all the other nodes.
1551 * We don't want to pressure a particular node, so when
1552 * building the zones for node N, we make sure that the
1553 * zones coming right after the local ones are those from
1554 * node N+1 (modulo N)
1555 */
1556 for (node = local_node + 1; node < MAX_NUMNODES; node++) {
1557 if (!node_online(node))
1558 continue;
1559 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1561 for (node = 0; node < local_node; node++) {
1562 if (!node_online(node))
1563 continue;
1564 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1567 zonelist->zones[j] = NULL;
1571 #endif /* CONFIG_NUMA */
1573 /* return values int ....just for stop_machine_run() */
1574 static int __meminit __build_all_zonelists(void *dummy)
1576 int nid;
1577 for_each_online_node(nid)
1578 build_zonelists(NODE_DATA(nid));
1579 return 0;
1582 void __meminit build_all_zonelists(void)
1584 if (system_state == SYSTEM_BOOTING) {
1585 __build_all_zonelists(0);
1586 cpuset_init_current_mems_allowed();
1587 } else {
1588 /* we have to stop all cpus to guaranntee there is no user
1589 of zonelist */
1590 stop_machine_run(__build_all_zonelists, NULL, NR_CPUS);
1591 /* cpuset refresh routine should be here */
1593 vm_total_pages = nr_free_pagecache_pages();
1594 printk("Built %i zonelists. Total pages: %ld\n",
1595 num_online_nodes(), vm_total_pages);
1598 /*
1599 * Helper functions to size the waitqueue hash table.
1600 * Essentially these want to choose hash table sizes sufficiently
1601 * large so that collisions trying to wait on pages are rare.
1602 * But in fact, the number of active page waitqueues on typical
1603 * systems is ridiculously low, less than 200. So this is even
1604 * conservative, even though it seems large.
1606 * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
1607 * waitqueues, i.e. the size of the waitq table given the number of pages.
1608 */
1609 #define PAGES_PER_WAITQUEUE 256
1611 #ifndef CONFIG_MEMORY_HOTPLUG
1612 static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
1614 unsigned long size = 1;
1616 pages /= PAGES_PER_WAITQUEUE;
1618 while (size < pages)
1619 size <<= 1;
1621 /*
1622 * Once we have dozens or even hundreds of threads sleeping
1623 * on IO we've got bigger problems than wait queue collision.
1624 * Limit the size of the wait table to a reasonable size.
1625 */
1626 size = min(size, 4096UL);
1628 return max(size, 4UL);
1630 #else
1631 /*
1632 * A zone's size might be changed by hot-add, so it is not possible to determine
1633 * a suitable size for its wait_table. So we use the maximum size now.
1635 * The max wait table size = 4096 x sizeof(wait_queue_head_t). ie:
1637 * i386 (preemption config) : 4096 x 16 = 64Kbyte.
1638 * ia64, x86-64 (no preemption): 4096 x 20 = 80Kbyte.
1639 * ia64, x86-64 (preemption) : 4096 x 24 = 96Kbyte.
1641 * The maximum entries are prepared when a zone's memory is (512K + 256) pages
1642 * or more by the traditional way. (See above). It equals:
1644 * i386, x86-64, powerpc(4K page size) : = ( 2G + 1M)byte.
1645 * ia64(16K page size) : = ( 8G + 4M)byte.
1646 * powerpc (64K page size) : = (32G +16M)byte.
1647 */
1648 static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
1650 return 4096UL;
1652 #endif
1654 /*
1655 * This is an integer logarithm so that shifts can be used later
1656 * to extract the more random high bits from the multiplicative
1657 * hash function before the remainder is taken.
1658 */
1659 static inline unsigned long wait_table_bits(unsigned long size)
1661 return ffz(~size);
1664 #define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
1666 static void __init calculate_zone_totalpages(struct pglist_data *pgdat,
1667 unsigned long *zones_size, unsigned long *zholes_size)
1669 unsigned long realtotalpages, totalpages = 0;
1670 int i;
1672 for (i = 0; i < MAX_NR_ZONES; i++)
1673 totalpages += zones_size[i];
1674 pgdat->node_spanned_pages = totalpages;
1676 realtotalpages = totalpages;
1677 if (zholes_size)
1678 for (i = 0; i < MAX_NR_ZONES; i++)
1679 realtotalpages -= zholes_size[i];
1680 pgdat->node_present_pages = realtotalpages;
1681 printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id, realtotalpages);
1685 /*
1686 * Initially all pages are reserved - free ones are freed
1687 * up by free_all_bootmem() once the early boot process is
1688 * done. Non-atomic initialization, single-pass.
1689 */
1690 void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
1691 unsigned long start_pfn)
1693 struct page *page;
1694 unsigned long end_pfn = start_pfn + size;
1695 unsigned long pfn;
1697 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
1698 if (!early_pfn_valid(pfn))
1699 continue;
1700 if (!early_pfn_in_nid(pfn, nid))
1701 continue;
1702 page = pfn_to_page(pfn);
1703 set_page_links(page, zone, nid, pfn);
1704 init_page_count(page);
1705 reset_page_mapcount(page);
1706 SetPageReserved(page);
1707 INIT_LIST_HEAD(&page->lru);
1708 #ifdef WANT_PAGE_VIRTUAL
1709 /* The shift won't overflow because ZONE_NORMAL is below 4G. */
1710 if (!is_highmem_idx(zone))
1711 set_page_address(page, __va(pfn << PAGE_SHIFT));
1712 #endif
1716 void zone_init_free_lists(struct pglist_data *pgdat, struct zone *zone,
1717 unsigned long size)
1719 int order;
1720 for (order = 0; order < MAX_ORDER ; order++) {
1721 INIT_LIST_HEAD(&zone->free_area[order].free_list);
1722 zone->free_area[order].nr_free = 0;
1726 #define ZONETABLE_INDEX(x, zone_nr) ((x << ZONES_SHIFT) | zone_nr)
1727 void zonetable_add(struct zone *zone, int nid, int zid, unsigned long pfn,
1728 unsigned long size)
1730 unsigned long snum = pfn_to_section_nr(pfn);
1731 unsigned long end = pfn_to_section_nr(pfn + size);
1733 if (FLAGS_HAS_NODE)
1734 zone_table[ZONETABLE_INDEX(nid, zid)] = zone;
1735 else
1736 for (; snum <= end; snum++)
1737 zone_table[ZONETABLE_INDEX(snum, zid)] = zone;
1740 #ifndef __HAVE_ARCH_MEMMAP_INIT
1741 #define memmap_init(size, nid, zone, start_pfn) \
1742 memmap_init_zone((size), (nid), (zone), (start_pfn))
1743 #endif
1745 static int __cpuinit zone_batchsize(struct zone *zone)
1747 int batch;
1749 /*
1750 * The per-cpu-pages pools are set to around 1000th of the
1751 * size of the zone. But no more than 1/2 of a meg.
1753 * OK, so we don't know how big the cache is. So guess.
1754 */
1755 batch = zone->present_pages / 1024;
1756 if (batch * PAGE_SIZE > 512 * 1024)
1757 batch = (512 * 1024) / PAGE_SIZE;
1758 batch /= 4; /* We effectively *= 4 below */
1759 if (batch < 1)
1760 batch = 1;
1762 /*
1763 * Clamp the batch to a 2^n - 1 value. Having a power
1764 * of 2 value was found to be more likely to have
1765 * suboptimal cache aliasing properties in some cases.
1767 * For example if 2 tasks are alternately allocating
1768 * batches of pages, one task can end up with a lot
1769 * of pages of one half of the possible page colors
1770 * and the other with pages of the other colors.
1771 */
1772 batch = (1 << (fls(batch + batch/2)-1)) - 1;
1774 return batch;
1777 inline void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
1779 struct per_cpu_pages *pcp;
1781 memset(p, 0, sizeof(*p));
1783 pcp = &p->pcp[0]; /* hot */
1784 pcp->count = 0;
1785 pcp->high = 6 * batch;
1786 pcp->batch = max(1UL, 1 * batch);
1787 INIT_LIST_HEAD(&pcp->list);
1789 pcp = &p->pcp[1]; /* cold*/
1790 pcp->count = 0;
1791 pcp->high = 2 * batch;
1792 pcp->batch = max(1UL, batch/2);
1793 INIT_LIST_HEAD(&pcp->list);
1796 /*
1797 * setup_pagelist_highmark() sets the high water mark for hot per_cpu_pagelist
1798 * to the value high for the pageset p.
1799 */
1801 static void setup_pagelist_highmark(struct per_cpu_pageset *p,
1802 unsigned long high)
1804 struct per_cpu_pages *pcp;
1806 pcp = &p->pcp[0]; /* hot list */
1807 pcp->high = high;
1808 pcp->batch = max(1UL, high/4);
1809 if ((high/4) > (PAGE_SHIFT * 8))
1810 pcp->batch = PAGE_SHIFT * 8;
1814 #ifdef CONFIG_NUMA
1815 /*
1816 * Boot pageset table. One per cpu which is going to be used for all
1817 * zones and all nodes. The parameters will be set in such a way
1818 * that an item put on a list will immediately be handed over to
1819 * the buddy list. This is safe since pageset manipulation is done
1820 * with interrupts disabled.
1822 * Some NUMA counter updates may also be caught by the boot pagesets.
1824 * The boot_pagesets must be kept even after bootup is complete for
1825 * unused processors and/or zones. They do play a role for bootstrapping
1826 * hotplugged processors.
1828 * zoneinfo_show() and maybe other functions do
1829 * not check if the processor is online before following the pageset pointer.
1830 * Other parts of the kernel may not check if the zone is available.
1831 */
1832 static struct per_cpu_pageset boot_pageset[NR_CPUS];
1834 /*
1835 * Dynamically allocate memory for the
1836 * per cpu pageset array in struct zone.
1837 */
1838 static int __cpuinit process_zones(int cpu)
1840 struct zone *zone, *dzone;
1842 for_each_zone(zone) {
1844 zone_pcp(zone, cpu) = kmalloc_node(sizeof(struct per_cpu_pageset),
1845 GFP_KERNEL, cpu_to_node(cpu));
1846 if (!zone_pcp(zone, cpu))
1847 goto bad;
1849 setup_pageset(zone_pcp(zone, cpu), zone_batchsize(zone));
1851 if (percpu_pagelist_fraction)
1852 setup_pagelist_highmark(zone_pcp(zone, cpu),
1853 (zone->present_pages / percpu_pagelist_fraction));
1856 return 0;
1857 bad:
1858 for_each_zone(dzone) {
1859 if (dzone == zone)
1860 break;
1861 kfree(zone_pcp(dzone, cpu));
1862 zone_pcp(dzone, cpu) = NULL;
1864 return -ENOMEM;
1867 static inline void free_zone_pagesets(int cpu)
1869 struct zone *zone;
1871 for_each_zone(zone) {
1872 struct per_cpu_pageset *pset = zone_pcp(zone, cpu);
1874 /* Free per_cpu_pageset if it is slab allocated */
1875 if (pset != &boot_pageset[cpu])
1876 kfree(pset);
1877 zone_pcp(zone, cpu) = NULL;
1881 static int __cpuinit pageset_cpuup_callback(struct notifier_block *nfb,
1882 unsigned long action,
1883 void *hcpu)
1885 int cpu = (long)hcpu;
1886 int ret = NOTIFY_OK;
1888 switch (action) {
1889 case CPU_UP_PREPARE:
1890 if (process_zones(cpu))
1891 ret = NOTIFY_BAD;
1892 break;
1893 case CPU_UP_CANCELED:
1894 case CPU_DEAD:
1895 free_zone_pagesets(cpu);
1896 break;
1897 default:
1898 break;
1900 return ret;
1903 static struct notifier_block __cpuinitdata pageset_notifier =
1904 { &pageset_cpuup_callback, NULL, 0 };
1906 void __init setup_per_cpu_pageset(void)
1908 int err;
1910 /* Initialize per_cpu_pageset for cpu 0.
1911 * A cpuup callback will do this for every cpu
1912 * as it comes online
1913 */
1914 err = process_zones(smp_processor_id());
1915 BUG_ON(err);
1916 register_cpu_notifier(&pageset_notifier);
1919 #endif
1921 static __meminit
1922 int zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages)
1924 int i;
1925 struct pglist_data *pgdat = zone->zone_pgdat;
1926 size_t alloc_size;
1928 /*
1929 * The per-page waitqueue mechanism uses hashed waitqueues
1930 * per zone.
1931 */
1932 zone->wait_table_hash_nr_entries =
1933 wait_table_hash_nr_entries(zone_size_pages);
1934 zone->wait_table_bits =
1935 wait_table_bits(zone->wait_table_hash_nr_entries);
1936 alloc_size = zone->wait_table_hash_nr_entries
1937 * sizeof(wait_queue_head_t);
1939 if (system_state == SYSTEM_BOOTING) {
1940 zone->wait_table = (wait_queue_head_t *)
1941 alloc_bootmem_node(pgdat, alloc_size);
1942 } else {
1943 /*
1944 * This case means that a zone whose size was 0 gets new memory
1945 * via memory hot-add.
1946 * But it may be the case that a new node was hot-added. In
1947 * this case vmalloc() will not be able to use this new node's
1948 * memory - this wait_table must be initialized to use this new
1949 * node itself as well.
1950 * To use this new node's memory, further consideration will be
1951 * necessary.
1952 */
1953 zone->wait_table = (wait_queue_head_t *)vmalloc(alloc_size);
1955 if (!zone->wait_table)
1956 return -ENOMEM;
1958 for(i = 0; i < zone->wait_table_hash_nr_entries; ++i)
1959 init_waitqueue_head(zone->wait_table + i);
1961 return 0;
1964 static __meminit void zone_pcp_init(struct zone *zone)
1966 int cpu;
1967 unsigned long batch = zone_batchsize(zone);
1969 for (cpu = 0; cpu < NR_CPUS; cpu++) {
1970 #ifdef CONFIG_NUMA
1971 /* Early boot. Slab allocator not functional yet */
1972 zone_pcp(zone, cpu) = &boot_pageset[cpu];
1973 setup_pageset(&boot_pageset[cpu],0);
1974 #else
1975 setup_pageset(zone_pcp(zone,cpu), batch);
1976 #endif
1978 if (zone->present_pages)
1979 printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%lu\n",
1980 zone->name, zone->present_pages, batch);
1983 __meminit int init_currently_empty_zone(struct zone *zone,
1984 unsigned long zone_start_pfn,
1985 unsigned long size)
1987 struct pglist_data *pgdat = zone->zone_pgdat;
1988 int ret;
1989 ret = zone_wait_table_init(zone, size);
1990 if (ret)
1991 return ret;
1992 pgdat->nr_zones = zone_idx(zone) + 1;
1994 zone->zone_start_pfn = zone_start_pfn;
1996 memmap_init(size, pgdat->node_id, zone_idx(zone), zone_start_pfn);
1998 zone_init_free_lists(pgdat, zone, zone->spanned_pages);
2000 return 0;
2003 /*
2004 * Set up the zone data structures:
2005 * - mark all pages reserved
2006 * - mark all memory queues empty
2007 * - clear the memory bitmaps
2008 */
2009 static void __meminit free_area_init_core(struct pglist_data *pgdat,
2010 unsigned long *zones_size, unsigned long *zholes_size)
2012 unsigned long j;
2013 int nid = pgdat->node_id;
2014 unsigned long zone_start_pfn = pgdat->node_start_pfn;
2015 int ret;
2017 pgdat_resize_init(pgdat);
2018 pgdat->nr_zones = 0;
2019 init_waitqueue_head(&pgdat->kswapd_wait);
2020 pgdat->kswapd_max_order = 0;
2022 for (j = 0; j < MAX_NR_ZONES; j++) {
2023 struct zone *zone = pgdat->node_zones + j;
2024 unsigned long size, realsize;
2026 realsize = size = zones_size[j];
2027 if (zholes_size)
2028 realsize -= zholes_size[j];
2030 if (j < ZONE_HIGHMEM)
2031 nr_kernel_pages += realsize;
2032 nr_all_pages += realsize;
2034 zone->spanned_pages = size;
2035 zone->present_pages = realsize;
2036 #ifdef CONFIG_NUMA
2037 zone->min_unmapped_ratio = (realsize*sysctl_min_unmapped_ratio)
2038 / 100;
2039 zone->min_slab_pages = (realsize * sysctl_min_slab_ratio) / 100;
2040 #endif
2041 zone->name = zone_names[j];
2042 spin_lock_init(&zone->lock);
2043 spin_lock_init(&zone->lru_lock);
2044 zone_seqlock_init(zone);
2045 zone->zone_pgdat = pgdat;
2046 zone->free_pages = 0;
2048 zone->prev_priority = DEF_PRIORITY;
2050 zone_pcp_init(zone);
2051 INIT_LIST_HEAD(&zone->active_list);
2052 INIT_LIST_HEAD(&zone->inactive_list);
2053 zone->nr_scan_active = 0;
2054 zone->nr_scan_inactive = 0;
2055 zone->nr_active = 0;
2056 zone->nr_inactive = 0;
2057 zap_zone_vm_stats(zone);
2058 atomic_set(&zone->reclaim_in_progress, 0);
2059 if (!size)
2060 continue;
2062 zonetable_add(zone, nid, j, zone_start_pfn, size);
2063 ret = init_currently_empty_zone(zone, zone_start_pfn, size);
2064 BUG_ON(ret);
2065 zone_start_pfn += size;
2069 static void __init alloc_node_mem_map(struct pglist_data *pgdat)
2071 /* Skip empty nodes */
2072 if (!pgdat->node_spanned_pages)
2073 return;
2075 #ifdef CONFIG_FLAT_NODE_MEM_MAP
2076 /* ia64 gets its own node_mem_map, before this, without bootmem */
2077 if (!pgdat->node_mem_map) {
2078 unsigned long size, start, end;
2079 struct page *map;
2081 /*
2082 * The zone's endpoints aren't required to be MAX_ORDER
2083 * aligned but the node_mem_map endpoints must be in order
2084 * for the buddy allocator to function correctly.
2085 */
2086 start = pgdat->node_start_pfn & ~(MAX_ORDER_NR_PAGES - 1);
2087 end = pgdat->node_start_pfn + pgdat->node_spanned_pages;
2088 end = ALIGN(end, MAX_ORDER_NR_PAGES);
2089 size = (end - start) * sizeof(struct page);
2090 map = alloc_remap(pgdat->node_id, size);
2091 if (!map)
2092 map = alloc_bootmem_node(pgdat, size);
2093 pgdat->node_mem_map = map + (pgdat->node_start_pfn - start);
2095 #ifdef CONFIG_FLATMEM
2096 /*
2097 * With no DISCONTIG, the global mem_map is just set as node 0's
2098 */
2099 if (pgdat == NODE_DATA(0))
2100 mem_map = NODE_DATA(0)->node_mem_map;
2101 #endif
2102 #endif /* CONFIG_FLAT_NODE_MEM_MAP */
2105 void __meminit free_area_init_node(int nid, struct pglist_data *pgdat,
2106 unsigned long *zones_size, unsigned long node_start_pfn,
2107 unsigned long *zholes_size)
2109 pgdat->node_id = nid;
2110 pgdat->node_start_pfn = node_start_pfn;
2111 calculate_zone_totalpages(pgdat, zones_size, zholes_size);
2113 alloc_node_mem_map(pgdat);
2115 free_area_init_core(pgdat, zones_size, zholes_size);
2118 #ifndef CONFIG_NEED_MULTIPLE_NODES
2119 static bootmem_data_t contig_bootmem_data;
2120 struct pglist_data contig_page_data = { .bdata = &contig_bootmem_data };
2122 EXPORT_SYMBOL(contig_page_data);
2123 #endif
2125 void __init free_area_init(unsigned long *zones_size)
2127 free_area_init_node(0, NODE_DATA(0), zones_size,
2128 __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
2131 #ifdef CONFIG_HOTPLUG_CPU
2132 static int page_alloc_cpu_notify(struct notifier_block *self,
2133 unsigned long action, void *hcpu)
2135 int cpu = (unsigned long)hcpu;
2137 if (action == CPU_DEAD) {
2138 local_irq_disable();
2139 __drain_pages(cpu);
2140 vm_events_fold_cpu(cpu);
2141 local_irq_enable();
2142 refresh_cpu_vm_stats(cpu);
2144 return NOTIFY_OK;
2146 #endif /* CONFIG_HOTPLUG_CPU */
2148 void __init page_alloc_init(void)
2150 hotcpu_notifier(page_alloc_cpu_notify, 0);
2153 /*
2154 * calculate_totalreserve_pages - called when sysctl_lower_zone_reserve_ratio
2155 * or min_free_kbytes changes.
2156 */
2157 static void calculate_totalreserve_pages(void)
2159 struct pglist_data *pgdat;
2160 unsigned long reserve_pages = 0;
2161 int i, j;
2163 for_each_online_pgdat(pgdat) {
2164 for (i = 0; i < MAX_NR_ZONES; i++) {
2165 struct zone *zone = pgdat->node_zones + i;
2166 unsigned long max = 0;
2168 /* Find valid and maximum lowmem_reserve in the zone */
2169 for (j = i; j < MAX_NR_ZONES; j++) {
2170 if (zone->lowmem_reserve[j] > max)
2171 max = zone->lowmem_reserve[j];
2174 /* we treat pages_high as reserved pages. */
2175 max += zone->pages_high;
2177 if (max > zone->present_pages)
2178 max = zone->present_pages;
2179 reserve_pages += max;
2182 totalreserve_pages = reserve_pages;
2185 /*
2186 * setup_per_zone_lowmem_reserve - called whenever
2187 * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
2188 * has a correct pages reserved value, so an adequate number of
2189 * pages are left in the zone after a successful __alloc_pages().
2190 */
2191 static void setup_per_zone_lowmem_reserve(void)
2193 struct pglist_data *pgdat;
2194 int j, idx;
2196 for_each_online_pgdat(pgdat) {
2197 for (j = 0; j < MAX_NR_ZONES; j++) {
2198 struct zone *zone = pgdat->node_zones + j;
2199 unsigned long present_pages = zone->present_pages;
2201 zone->lowmem_reserve[j] = 0;
2203 for (idx = j-1; idx >= 0; idx--) {
2204 struct zone *lower_zone;
2206 if (sysctl_lowmem_reserve_ratio[idx] < 1)
2207 sysctl_lowmem_reserve_ratio[idx] = 1;
2209 lower_zone = pgdat->node_zones + idx;
2210 lower_zone->lowmem_reserve[j] = present_pages /
2211 sysctl_lowmem_reserve_ratio[idx];
2212 present_pages += lower_zone->present_pages;
2217 /* update totalreserve_pages */
2218 calculate_totalreserve_pages();
2221 /*
2222 * setup_per_zone_pages_min - called when min_free_kbytes changes. Ensures
2223 * that the pages_{min,low,high} values for each zone are set correctly
2224 * with respect to min_free_kbytes.
2225 */
2226 void setup_per_zone_pages_min(void)
2228 unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
2229 unsigned long lowmem_pages = 0;
2230 struct zone *zone;
2231 unsigned long flags;
2233 /* Calculate total number of !ZONE_HIGHMEM pages */
2234 for_each_zone(zone) {
2235 if (!is_highmem(zone))
2236 lowmem_pages += zone->present_pages;
2239 for_each_zone(zone) {
2240 u64 tmp;
2242 spin_lock_irqsave(&zone->lru_lock, flags);
2243 tmp = (u64)pages_min * zone->present_pages;
2244 do_div(tmp, lowmem_pages);
2245 if (is_highmem(zone)) {
2246 /*
2247 * __GFP_HIGH and PF_MEMALLOC allocations usually don't
2248 * need highmem pages, so cap pages_min to a small
2249 * value here.
2251 * The (pages_high-pages_low) and (pages_low-pages_min)
2252 * deltas controls asynch page reclaim, and so should
2253 * not be capped for highmem.
2254 */
2255 int min_pages;
2257 min_pages = zone->present_pages / 1024;
2258 if (min_pages < SWAP_CLUSTER_MAX)
2259 min_pages = SWAP_CLUSTER_MAX;
2260 if (min_pages > 128)
2261 min_pages = 128;
2262 zone->pages_min = min_pages;
2263 } else {
2264 /*
2265 * If it's a lowmem zone, reserve a number of pages
2266 * proportionate to the zone's size.
2267 */
2268 zone->pages_min = tmp;
2271 zone->pages_low = zone->pages_min + (tmp >> 2);
2272 zone->pages_high = zone->pages_min + (tmp >> 1);
2273 spin_unlock_irqrestore(&zone->lru_lock, flags);
2276 /* update totalreserve_pages */
2277 calculate_totalreserve_pages();
2280 /*
2281 * Initialise min_free_kbytes.
2283 * For small machines we want it small (128k min). For large machines
2284 * we want it large (64MB max). But it is not linear, because network
2285 * bandwidth does not increase linearly with machine size. We use
2287 * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
2288 * min_free_kbytes = sqrt(lowmem_kbytes * 16)
2290 * which yields
2292 * 16MB: 512k
2293 * 32MB: 724k
2294 * 64MB: 1024k
2295 * 128MB: 1448k
2296 * 256MB: 2048k
2297 * 512MB: 2896k
2298 * 1024MB: 4096k
2299 * 2048MB: 5792k
2300 * 4096MB: 8192k
2301 * 8192MB: 11584k
2302 * 16384MB: 16384k
2303 */
2304 static int __init init_per_zone_pages_min(void)
2306 unsigned long lowmem_kbytes;
2308 lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
2310 min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
2311 if (min_free_kbytes < 128)
2312 min_free_kbytes = 128;
2313 if (min_free_kbytes > 65536)
2314 min_free_kbytes = 65536;
2315 setup_per_zone_pages_min();
2316 setup_per_zone_lowmem_reserve();
2317 return 0;
2319 module_init(init_per_zone_pages_min)
2321 /*
2322 * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
2323 * that we can call two helper functions whenever min_free_kbytes
2324 * changes.
2325 */
2326 int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
2327 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
2329 proc_dointvec(table, write, file, buffer, length, ppos);
2330 setup_per_zone_pages_min();
2331 return 0;
2334 #ifdef CONFIG_NUMA
2335 int sysctl_min_unmapped_ratio_sysctl_handler(ctl_table *table, int write,
2336 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
2338 struct zone *zone;
2339 int rc;
2341 rc = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
2342 if (rc)
2343 return rc;
2345 for_each_zone(zone)
2346 zone->min_unmapped_ratio = (zone->present_pages *
2347 sysctl_min_unmapped_ratio) / 100;
2348 return 0;
2351 int sysctl_min_slab_ratio_sysctl_handler(ctl_table *table, int write,
2352 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
2354 struct zone *zone;
2355 int rc;
2357 rc = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
2358 if (rc)
2359 return rc;
2361 for_each_zone(zone)
2362 zone->min_slab_pages = (zone->present_pages *
2363 sysctl_min_slab_ratio) / 100;
2364 return 0;
2366 #endif
2368 /*
2369 * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
2370 * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
2371 * whenever sysctl_lowmem_reserve_ratio changes.
2373 * The reserve ratio obviously has absolutely no relation with the
2374 * pages_min watermarks. The lowmem reserve ratio can only make sense
2375 * if in function of the boot time zone sizes.
2376 */
2377 int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
2378 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
2380 proc_dointvec_minmax(table, write, file, buffer, length, ppos);
2381 setup_per_zone_lowmem_reserve();
2382 return 0;
2385 /*
2386 * percpu_pagelist_fraction - changes the pcp->high for each zone on each
2387 * cpu. It is the fraction of total pages in each zone that a hot per cpu pagelist
2388 * can have before it gets flushed back to buddy allocator.
2389 */
2391 int percpu_pagelist_fraction_sysctl_handler(ctl_table *table, int write,
2392 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
2394 struct zone *zone;
2395 unsigned int cpu;
2396 int ret;
2398 ret = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
2399 if (!write || (ret == -EINVAL))
2400 return ret;
2401 for_each_zone(zone) {
2402 for_each_online_cpu(cpu) {
2403 unsigned long high;
2404 high = zone->present_pages / percpu_pagelist_fraction;
2405 setup_pagelist_highmark(zone_pcp(zone, cpu), high);
2408 return 0;
2411 __initdata int hashdist = HASHDIST_DEFAULT;
2413 #ifdef CONFIG_NUMA
2414 static int __init set_hashdist(char *str)
2416 if (!str)
2417 return 0;
2418 hashdist = simple_strtoul(str, &str, 0);
2419 return 1;
2421 __setup("hashdist=", set_hashdist);
2422 #endif
2424 /*
2425 * allocate a large system hash table from bootmem
2426 * - it is assumed that the hash table must contain an exact power-of-2
2427 * quantity of entries
2428 * - limit is the number of hash buckets, not the total allocation size
2429 */
2430 void *__init alloc_large_system_hash(const char *tablename,
2431 unsigned long bucketsize,
2432 unsigned long numentries,
2433 int scale,
2434 int flags,
2435 unsigned int *_hash_shift,
2436 unsigned int *_hash_mask,
2437 unsigned long limit)
2439 unsigned long long max = limit;
2440 unsigned long log2qty, size;
2441 void *table = NULL;
2443 /* allow the kernel cmdline to have a say */
2444 if (!numentries) {
2445 /* round applicable memory size up to nearest megabyte */
2446 numentries = (flags & HASH_HIGHMEM) ? nr_all_pages : nr_kernel_pages;
2447 numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
2448 numentries >>= 20 - PAGE_SHIFT;
2449 numentries <<= 20 - PAGE_SHIFT;
2451 /* limit to 1 bucket per 2^scale bytes of low memory */
2452 if (scale > PAGE_SHIFT)
2453 numentries >>= (scale - PAGE_SHIFT);
2454 else
2455 numentries <<= (PAGE_SHIFT - scale);
2457 numentries = roundup_pow_of_two(numentries);
2459 /* limit allocation size to 1/16 total memory by default */
2460 if (max == 0) {
2461 max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
2462 do_div(max, bucketsize);
2465 if (numentries > max)
2466 numentries = max;
2468 log2qty = long_log2(numentries);
2470 do {
2471 size = bucketsize << log2qty;
2472 if (flags & HASH_EARLY)
2473 table = alloc_bootmem(size);
2474 else if (hashdist)
2475 table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
2476 else {
2477 unsigned long order;
2478 for (order = 0; ((1UL << order) << PAGE_SHIFT) < size; order++)
2480 table = (void*) __get_free_pages(GFP_ATOMIC, order);
2482 } while (!table && size > PAGE_SIZE && --log2qty);
2484 if (!table)
2485 panic("Failed to allocate %s hash table\n", tablename);
2487 printk("%s hash table entries: %d (order: %d, %lu bytes)\n",
2488 tablename,
2489 (1U << log2qty),
2490 long_log2(size) - PAGE_SHIFT,
2491 size);
2493 if (_hash_shift)
2494 *_hash_shift = log2qty;
2495 if (_hash_mask)
2496 *_hash_mask = (1 << log2qty) - 1;
2498 return table;
2501 #ifdef CONFIG_OUT_OF_LINE_PFN_TO_PAGE
2502 struct page *pfn_to_page(unsigned long pfn)
2504 return __pfn_to_page(pfn);
2506 unsigned long page_to_pfn(struct page *page)
2508 return __page_to_pfn(page);
2510 EXPORT_SYMBOL(pfn_to_page);
2511 EXPORT_SYMBOL(page_to_pfn);
2512 #endif /* CONFIG_OUT_OF_LINE_PFN_TO_PAGE */