ia64/xen-unstable

view linux-2.6-xen-sparse/mm/page_alloc.c @ 13197:a9a43705f26b

Fix HVM booting through Xen-API when the kernel is unspecified.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author Ewan Mellor <ewan@xensource.com>
date Wed Dec 27 00:38:01 2006 +0000 (2006-12-27)
parents c5d4d47bbeb8
children 4fad820a2233
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/config.h>
18 #include <linux/stddef.h>
19 #include <linux/mm.h>
20 #include <linux/swap.h>
21 #include <linux/interrupt.h>
22 #include <linux/pagemap.h>
23 #include <linux/bootmem.h>
24 #include <linux/compiler.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/suspend.h>
28 #include <linux/pagevec.h>
29 #include <linux/blkdev.h>
30 #include <linux/slab.h>
31 #include <linux/notifier.h>
32 #include <linux/topology.h>
33 #include <linux/sysctl.h>
34 #include <linux/cpu.h>
35 #include <linux/cpuset.h>
36 #include <linux/memory_hotplug.h>
37 #include <linux/nodemask.h>
38 #include <linux/vmalloc.h>
39 #include <linux/mempolicy.h>
41 #include <asm/tlbflush.h>
42 #include "internal.h"
44 /*
45 * MCD - HACK: Find somewhere to initialize this EARLY, or make this
46 * initializer cleaner
47 */
48 nodemask_t node_online_map __read_mostly = { { [0] = 1UL } };
49 EXPORT_SYMBOL(node_online_map);
50 nodemask_t node_possible_map __read_mostly = NODE_MASK_ALL;
51 EXPORT_SYMBOL(node_possible_map);
52 struct pglist_data *pgdat_list __read_mostly;
53 unsigned long totalram_pages __read_mostly;
54 unsigned long totalhigh_pages __read_mostly;
55 long nr_swap_pages;
56 int percpu_pagelist_fraction;
58 static void fastcall free_hot_cold_page(struct page *page, int cold);
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 __initdata nr_kernel_pages;
87 unsigned long __initdata 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 set_page_count(page, 0);
159 reset_page_mapcount(page);
160 page->mapping = NULL;
161 add_taint(TAINT_BAD_PAGE);
162 }
164 /*
165 * Higher-order pages are called "compound pages". They are structured thusly:
166 *
167 * The first PAGE_SIZE page is called the "head page".
168 *
169 * The remaining PAGE_SIZE pages are called "tail pages".
170 *
171 * All pages have PG_compound set. All pages have their ->private pointing at
172 * the head page (even the head page has this).
173 *
174 * The first tail page's ->lru.next holds the address of the compound page's
175 * put_page() function. Its ->lru.prev holds the order of allocation.
176 * This usage means that zero-order pages may not be compound.
177 */
179 static void free_compound_page(struct page *page)
180 {
181 __free_pages_ok(page, (unsigned long)page[1].lru.prev);
182 }
184 static void prep_compound_page(struct page *page, unsigned long order)
185 {
186 int i;
187 int nr_pages = 1 << order;
189 page[1].lru.next = (void *)free_compound_page; /* set dtor */
190 page[1].lru.prev = (void *)order;
191 for (i = 0; i < nr_pages; i++) {
192 struct page *p = page + i;
194 SetPageCompound(p);
195 set_page_private(p, (unsigned long)page);
196 }
197 }
199 static void destroy_compound_page(struct page *page, unsigned long order)
200 {
201 int i;
202 int nr_pages = 1 << order;
204 if (unlikely((unsigned long)page[1].lru.prev != order))
205 bad_page(page);
207 for (i = 0; i < nr_pages; i++) {
208 struct page *p = page + i;
210 if (unlikely(!PageCompound(p) |
211 (page_private(p) != (unsigned long)page)))
212 bad_page(page);
213 ClearPageCompound(p);
214 }
215 }
217 /*
218 * function for dealing with page's order in buddy system.
219 * zone->lock is already acquired when we use these.
220 * So, we don't need atomic page->flags operations here.
221 */
222 static inline unsigned long page_order(struct page *page) {
223 return page_private(page);
224 }
226 static inline void set_page_order(struct page *page, int order) {
227 set_page_private(page, order);
228 __SetPageBuddy(page);
229 }
231 static inline void rmv_page_order(struct page *page)
232 {
233 __ClearPageBuddy(page);
234 set_page_private(page, 0);
235 }
237 /*
238 * Locate the struct page for both the matching buddy in our
239 * pair (buddy1) and the combined O(n+1) page they form (page).
240 *
241 * 1) Any buddy B1 will have an order O twin B2 which satisfies
242 * the following equation:
243 * B2 = B1 ^ (1 << O)
244 * For example, if the starting buddy (buddy2) is #8 its order
245 * 1 buddy is #10:
246 * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
247 *
248 * 2) Any buddy B will have an order O+1 parent P which
249 * satisfies the following equation:
250 * P = B & ~(1 << O)
251 *
252 * Assumption: *_mem_map is contigious at least up to MAX_ORDER
253 */
254 static inline struct page *
255 __page_find_buddy(struct page *page, unsigned long page_idx, unsigned int order)
256 {
257 unsigned long buddy_idx = page_idx ^ (1 << order);
259 return page + (buddy_idx - page_idx);
260 }
262 static inline unsigned long
263 __find_combined_index(unsigned long page_idx, unsigned int order)
264 {
265 return (page_idx & ~(1 << order));
266 }
268 /*
269 * This function checks whether a page is free && is the buddy
270 * we can do coalesce a page and its buddy if
271 * (a) the buddy is not in a hole &&
272 * (b) the buddy is in the buddy system &&
273 * (c) a page and its buddy have the same order.
274 *
275 * For recording whether a page is in the buddy system, we use PG_buddy.
276 * Setting, clearing, and testing PG_buddy is serialized by zone->lock.
277 *
278 * For recording page's order, we use page_private(page).
279 */
280 static inline int page_is_buddy(struct page *page, int order)
281 {
282 #ifdef CONFIG_HOLES_IN_ZONE
283 if (!pfn_valid(page_to_pfn(page)))
284 return 0;
285 #endif
287 if (PageBuddy(page) && page_order(page) == order) {
288 BUG_ON(page_count(page) != 0);
289 return 1;
290 }
291 return 0;
292 }
294 /*
295 * Freeing function for a buddy system allocator.
296 *
297 * The concept of a buddy system is to maintain direct-mapped table
298 * (containing bit values) for memory blocks of various "orders".
299 * The bottom level table contains the map for the smallest allocatable
300 * units of memory (here, pages), and each level above it describes
301 * pairs of units from the levels below, hence, "buddies".
302 * At a high level, all that happens here is marking the table entry
303 * at the bottom level available, and propagating the changes upward
304 * as necessary, plus some accounting needed to play nicely with other
305 * parts of the VM system.
306 * At each level, we keep a list of pages, which are heads of continuous
307 * free pages of length of (1 << order) and marked with PG_buddy. Page's
308 * order is recorded in page_private(page) field.
309 * So when we are allocating or freeing one, we can derive the state of the
310 * other. That is, if we allocate a small block, and both were
311 * free, the remainder of the region must be split into blocks.
312 * If a block is freed, and its buddy is also free, then this
313 * triggers coalescing into a block of larger size.
314 *
315 * -- wli
316 */
318 static inline void __free_one_page(struct page *page,
319 struct zone *zone, unsigned int order)
320 {
321 unsigned long page_idx;
322 int order_size = 1 << order;
324 if (unlikely(PageCompound(page)))
325 destroy_compound_page(page, order);
327 page_idx = page_to_pfn(page) & ((1 << MAX_ORDER) - 1);
329 BUG_ON(page_idx & (order_size - 1));
330 BUG_ON(bad_range(zone, page));
332 zone->free_pages += order_size;
333 while (order < MAX_ORDER-1) {
334 unsigned long combined_idx;
335 struct free_area *area;
336 struct page *buddy;
338 buddy = __page_find_buddy(page, page_idx, order);
339 if (!page_is_buddy(buddy, order))
340 break; /* Move the buddy up one level. */
342 list_del(&buddy->lru);
343 area = zone->free_area + order;
344 area->nr_free--;
345 rmv_page_order(buddy);
346 combined_idx = __find_combined_index(page_idx, order);
347 page = page + (combined_idx - page_idx);
348 page_idx = combined_idx;
349 order++;
350 }
351 set_page_order(page, order);
352 list_add(&page->lru, &zone->free_area[order].free_list);
353 zone->free_area[order].nr_free++;
354 }
356 static inline int free_pages_check(struct page *page)
357 {
358 if (unlikely(page_mapcount(page) |
359 (page->mapping != NULL) |
360 (page_count(page) != 0) |
361 (page->flags & (
362 1 << PG_lru |
363 1 << PG_private |
364 1 << PG_locked |
365 1 << PG_active |
366 1 << PG_reclaim |
367 1 << PG_slab |
368 1 << PG_swapcache |
369 1 << PG_writeback |
370 1 << PG_reserved |
371 1 << PG_buddy ))))
372 bad_page(page);
373 if (PageDirty(page))
374 __ClearPageDirty(page);
375 /*
376 * For now, we report if PG_reserved was found set, but do not
377 * clear it, and do not free the page. But we shall soon need
378 * to do more, for when the ZERO_PAGE count wraps negative.
379 */
380 return PageReserved(page);
381 }
383 /*
384 * Frees a list of pages.
385 * Assumes all pages on list are in same zone, and of same order.
386 * count is the number of pages to free.
387 *
388 * If the zone was previously in an "all pages pinned" state then look to
389 * see if this freeing clears that state.
390 *
391 * And clear the zone's pages_scanned counter, to hold off the "all pages are
392 * pinned" detection logic.
393 */
394 static void free_pages_bulk(struct zone *zone, int count,
395 struct list_head *list, int order)
396 {
397 spin_lock(&zone->lock);
398 zone->all_unreclaimable = 0;
399 zone->pages_scanned = 0;
400 while (count--) {
401 struct page *page;
403 BUG_ON(list_empty(list));
404 page = list_entry(list->prev, struct page, lru);
405 /* have to delete it as __free_one_page list manipulates */
406 list_del(&page->lru);
407 __free_one_page(page, zone, order);
408 }
409 spin_unlock(&zone->lock);
410 }
412 static void free_one_page(struct zone *zone, struct page *page, int order)
413 {
414 LIST_HEAD(list);
415 list_add(&page->lru, &list);
416 free_pages_bulk(zone, 1, &list, order);
417 }
419 static void __free_pages_ok(struct page *page, unsigned int order)
420 {
421 unsigned long flags;
422 int i;
423 int reserved = 0;
425 if (arch_free_page(page, order))
426 return;
427 if (!PageHighMem(page))
428 mutex_debug_check_no_locks_freed(page_address(page),
429 PAGE_SIZE<<order);
431 #ifndef CONFIG_MMU
432 for (i = 1 ; i < (1 << order) ; ++i)
433 __put_page(page + i);
434 #endif
436 for (i = 0 ; i < (1 << order) ; ++i)
437 reserved += free_pages_check(page + i);
438 if (reserved)
439 return;
441 kernel_map_pages(page, 1 << order, 0);
442 local_irq_save(flags);
443 __mod_page_state(pgfree, 1 << order);
444 free_one_page(page_zone(page), page, order);
445 local_irq_restore(flags);
446 }
448 /*
449 * permit the bootmem allocator to evade page validation on high-order frees
450 */
451 void fastcall __init __free_pages_bootmem(struct page *page, unsigned int order)
452 {
453 if (order == 0) {
454 __ClearPageReserved(page);
455 set_page_count(page, 0);
457 free_hot_cold_page(page, 0);
458 } else {
459 LIST_HEAD(list);
460 int loop;
462 for (loop = 0; loop < BITS_PER_LONG; loop++) {
463 struct page *p = &page[loop];
465 if (loop + 16 < BITS_PER_LONG)
466 prefetchw(p + 16);
467 __ClearPageReserved(p);
468 set_page_count(p, 0);
469 }
471 arch_free_page(page, order);
473 mod_page_state(pgfree, 1 << order);
475 list_add(&page->lru, &list);
476 kernel_map_pages(page, 1 << order, 0);
477 free_pages_bulk(page_zone(page), 1, &list, order);
478 }
479 }
482 /*
483 * The order of subdivision here is critical for the IO subsystem.
484 * Please do not alter this order without good reasons and regression
485 * testing. Specifically, as large blocks of memory are subdivided,
486 * the order in which smaller blocks are delivered depends on the order
487 * they're subdivided in this function. This is the primary factor
488 * influencing the order in which pages are delivered to the IO
489 * subsystem according to empirical testing, and this is also justified
490 * by considering the behavior of a buddy system containing a single
491 * large block of memory acted on by a series of small allocations.
492 * This behavior is a critical factor in sglist merging's success.
493 *
494 * -- wli
495 */
496 static inline void expand(struct zone *zone, struct page *page,
497 int low, int high, struct free_area *area)
498 {
499 unsigned long size = 1 << high;
501 while (high > low) {
502 area--;
503 high--;
504 size >>= 1;
505 BUG_ON(bad_range(zone, &page[size]));
506 list_add(&page[size].lru, &area->free_list);
507 area->nr_free++;
508 set_page_order(&page[size], high);
509 }
510 }
512 /*
513 * This page is about to be returned from the page allocator
514 */
515 static int prep_new_page(struct page *page, int order)
516 {
517 if (unlikely(page_mapcount(page) |
518 (page->mapping != NULL) |
519 (page_count(page) != 0) |
520 (page->flags & (
521 1 << PG_lru |
522 1 << PG_private |
523 1 << PG_locked |
524 1 << PG_active |
525 1 << PG_dirty |
526 1 << PG_reclaim |
527 1 << PG_slab |
528 1 << PG_swapcache |
529 1 << PG_writeback |
530 1 << PG_reserved |
531 1 << PG_buddy ))))
532 bad_page(page);
534 /*
535 * For now, we report if PG_reserved was found set, but do not
536 * clear it, and do not allocate the page: as a safety net.
537 */
538 if (PageReserved(page))
539 return 1;
541 page->flags &= ~(1 << PG_uptodate | 1 << PG_error |
542 1 << PG_referenced | 1 << PG_arch_1 |
543 1 << PG_checked | 1 << PG_mappedtodisk);
544 set_page_private(page, 0);
545 set_page_refs(page, order);
546 kernel_map_pages(page, 1 << order, 1);
547 return 0;
548 }
550 /*
551 * Do the hard work of removing an element from the buddy allocator.
552 * Call me with the zone->lock already held.
553 */
554 static struct page *__rmqueue(struct zone *zone, unsigned int order)
555 {
556 struct free_area * area;
557 unsigned int current_order;
558 struct page *page;
560 for (current_order = order; current_order < MAX_ORDER; ++current_order) {
561 area = zone->free_area + current_order;
562 if (list_empty(&area->free_list))
563 continue;
565 page = list_entry(area->free_list.next, struct page, lru);
566 list_del(&page->lru);
567 rmv_page_order(page);
568 area->nr_free--;
569 zone->free_pages -= 1UL << order;
570 expand(zone, page, order, current_order, area);
571 return page;
572 }
574 return NULL;
575 }
577 /*
578 * Obtain a specified number of elements from the buddy allocator, all under
579 * a single hold of the lock, for efficiency. Add them to the supplied list.
580 * Returns the number of new pages which were placed at *list.
581 */
582 static int rmqueue_bulk(struct zone *zone, unsigned int order,
583 unsigned long count, struct list_head *list)
584 {
585 int i;
587 spin_lock(&zone->lock);
588 for (i = 0; i < count; ++i) {
589 struct page *page = __rmqueue(zone, order);
590 if (unlikely(page == NULL))
591 break;
592 list_add_tail(&page->lru, list);
593 }
594 spin_unlock(&zone->lock);
595 return i;
596 }
598 #ifdef CONFIG_NUMA
599 /*
600 * Called from the slab reaper to drain pagesets on a particular node that
601 * belong to the currently executing processor.
602 */
603 void drain_node_pages(int nodeid)
604 {
605 int i, z;
606 unsigned long flags;
608 local_irq_save(flags);
609 for (z = 0; z < MAX_NR_ZONES; z++) {
610 struct zone *zone = NODE_DATA(nodeid)->node_zones + z;
611 struct per_cpu_pageset *pset;
613 pset = zone_pcp(zone, smp_processor_id());
614 for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
615 struct per_cpu_pages *pcp;
617 pcp = &pset->pcp[i];
618 free_pages_bulk(zone, pcp->count, &pcp->list, 0);
619 pcp->count = 0;
620 }
621 }
622 local_irq_restore(flags);
623 }
624 #endif
626 #if defined(CONFIG_PM) || defined(CONFIG_HOTPLUG_CPU)
627 static void __drain_pages(unsigned int cpu)
628 {
629 unsigned long flags;
630 struct zone *zone;
631 int i;
633 for_each_zone(zone) {
634 struct per_cpu_pageset *pset;
636 pset = zone_pcp(zone, cpu);
637 for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
638 struct per_cpu_pages *pcp;
640 pcp = &pset->pcp[i];
641 local_irq_save(flags);
642 free_pages_bulk(zone, pcp->count, &pcp->list, 0);
643 pcp->count = 0;
644 local_irq_restore(flags);
645 }
646 }
647 }
648 #endif /* CONFIG_PM || CONFIG_HOTPLUG_CPU */
650 #ifdef CONFIG_PM
652 void mark_free_pages(struct zone *zone)
653 {
654 unsigned long zone_pfn, flags;
655 int order;
656 struct list_head *curr;
658 if (!zone->spanned_pages)
659 return;
661 spin_lock_irqsave(&zone->lock, flags);
662 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
663 ClearPageNosaveFree(pfn_to_page(zone_pfn + zone->zone_start_pfn));
665 for (order = MAX_ORDER - 1; order >= 0; --order)
666 list_for_each(curr, &zone->free_area[order].free_list) {
667 unsigned long start_pfn, i;
669 start_pfn = page_to_pfn(list_entry(curr, struct page, lru));
671 for (i=0; i < (1<<order); i++)
672 SetPageNosaveFree(pfn_to_page(start_pfn+i));
673 }
674 spin_unlock_irqrestore(&zone->lock, flags);
675 }
677 /*
678 * Spill all of this CPU's per-cpu pages back into the buddy allocator.
679 */
680 void drain_local_pages(void)
681 {
682 unsigned long flags;
684 local_irq_save(flags);
685 __drain_pages(smp_processor_id());
686 local_irq_restore(flags);
687 }
688 #endif /* CONFIG_PM */
690 static void zone_statistics(struct zonelist *zonelist, struct zone *z, int cpu)
691 {
692 #ifdef CONFIG_NUMA
693 pg_data_t *pg = z->zone_pgdat;
694 pg_data_t *orig = zonelist->zones[0]->zone_pgdat;
695 struct per_cpu_pageset *p;
697 p = zone_pcp(z, cpu);
698 if (pg == orig) {
699 p->numa_hit++;
700 } else {
701 p->numa_miss++;
702 zone_pcp(zonelist->zones[0], cpu)->numa_foreign++;
703 }
704 if (pg == NODE_DATA(numa_node_id()))
705 p->local_node++;
706 else
707 p->other_node++;
708 #endif
709 }
711 /*
712 * Free a 0-order page
713 */
714 static void fastcall free_hot_cold_page(struct page *page, int cold)
715 {
716 struct zone *zone = page_zone(page);
717 struct per_cpu_pages *pcp;
718 unsigned long flags;
720 if (arch_free_page(page, 0))
721 return;
723 if (PageAnon(page))
724 page->mapping = NULL;
725 if (free_pages_check(page))
726 return;
728 kernel_map_pages(page, 1, 0);
730 pcp = &zone_pcp(zone, get_cpu())->pcp[cold];
731 local_irq_save(flags);
732 __inc_page_state(pgfree);
733 list_add(&page->lru, &pcp->list);
734 pcp->count++;
735 if (pcp->count >= pcp->high) {
736 free_pages_bulk(zone, pcp->batch, &pcp->list, 0);
737 pcp->count -= pcp->batch;
738 }
739 local_irq_restore(flags);
740 put_cpu();
741 }
743 void fastcall free_hot_page(struct page *page)
744 {
745 free_hot_cold_page(page, 0);
746 }
748 void fastcall free_cold_page(struct page *page)
749 {
750 free_hot_cold_page(page, 1);
751 }
753 static inline void prep_zero_page(struct page *page, int order, gfp_t gfp_flags)
754 {
755 int i;
757 BUG_ON((gfp_flags & (__GFP_WAIT | __GFP_HIGHMEM)) == __GFP_HIGHMEM);
758 for(i = 0; i < (1 << order); i++)
759 clear_highpage(page + i);
760 }
762 /*
763 * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
764 * we cheat by calling it from here, in the order > 0 path. Saves a branch
765 * or two.
766 */
767 static struct page *buffered_rmqueue(struct zonelist *zonelist,
768 struct zone *zone, int order, gfp_t gfp_flags)
769 {
770 unsigned long flags;
771 struct page *page;
772 int cold = !!(gfp_flags & __GFP_COLD);
773 int cpu;
775 again:
776 cpu = get_cpu();
777 if (likely(order == 0)) {
778 struct per_cpu_pages *pcp;
780 pcp = &zone_pcp(zone, cpu)->pcp[cold];
781 local_irq_save(flags);
782 if (!pcp->count) {
783 pcp->count += rmqueue_bulk(zone, 0,
784 pcp->batch, &pcp->list);
785 if (unlikely(!pcp->count))
786 goto failed;
787 }
788 page = list_entry(pcp->list.next, struct page, lru);
789 list_del(&page->lru);
790 pcp->count--;
791 } else {
792 spin_lock_irqsave(&zone->lock, flags);
793 page = __rmqueue(zone, order);
794 spin_unlock(&zone->lock);
795 if (!page)
796 goto failed;
797 }
799 __mod_page_state_zone(zone, pgalloc, 1 << order);
800 zone_statistics(zonelist, zone, cpu);
801 local_irq_restore(flags);
802 put_cpu();
804 BUG_ON(bad_range(zone, page));
805 if (prep_new_page(page, order))
806 goto again;
808 if (gfp_flags & __GFP_ZERO)
809 prep_zero_page(page, order, gfp_flags);
811 if (order && (gfp_flags & __GFP_COMP))
812 prep_compound_page(page, order);
813 return page;
815 failed:
816 local_irq_restore(flags);
817 put_cpu();
818 return NULL;
819 }
821 #define ALLOC_NO_WATERMARKS 0x01 /* don't check watermarks at all */
822 #define ALLOC_WMARK_MIN 0x02 /* use pages_min watermark */
823 #define ALLOC_WMARK_LOW 0x04 /* use pages_low watermark */
824 #define ALLOC_WMARK_HIGH 0x08 /* use pages_high watermark */
825 #define ALLOC_HARDER 0x10 /* try to alloc harder */
826 #define ALLOC_HIGH 0x20 /* __GFP_HIGH set */
827 #define ALLOC_CPUSET 0x40 /* check for correct cpuset */
829 /*
830 * Return 1 if free pages are above 'mark'. This takes into account the order
831 * of the allocation.
832 */
833 int zone_watermark_ok(struct zone *z, int order, unsigned long mark,
834 int classzone_idx, int alloc_flags)
835 {
836 /* free_pages my go negative - that's OK */
837 long min = mark, free_pages = z->free_pages - (1 << order) + 1;
838 int o;
840 if (alloc_flags & ALLOC_HIGH)
841 min -= min / 2;
842 if (alloc_flags & ALLOC_HARDER)
843 min -= min / 4;
845 if (free_pages <= min + z->lowmem_reserve[classzone_idx])
846 return 0;
847 for (o = 0; o < order; o++) {
848 /* At the next order, this order's pages become unavailable */
849 free_pages -= z->free_area[o].nr_free << o;
851 /* Require fewer higher order pages to be free */
852 min >>= 1;
854 if (free_pages <= min)
855 return 0;
856 }
857 return 1;
858 }
860 /*
861 * get_page_from_freeliest goes through the zonelist trying to allocate
862 * a page.
863 */
864 static struct page *
865 get_page_from_freelist(gfp_t gfp_mask, unsigned int order,
866 struct zonelist *zonelist, int alloc_flags)
867 {
868 struct zone **z = zonelist->zones;
869 struct page *page = NULL;
870 int classzone_idx = zone_idx(*z);
872 /*
873 * Go through the zonelist once, looking for a zone with enough free.
874 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
875 */
876 do {
877 if ((alloc_flags & ALLOC_CPUSET) &&
878 !cpuset_zone_allowed(*z, gfp_mask))
879 continue;
881 if (!(alloc_flags & ALLOC_NO_WATERMARKS)) {
882 unsigned long mark;
883 if (alloc_flags & ALLOC_WMARK_MIN)
884 mark = (*z)->pages_min;
885 else if (alloc_flags & ALLOC_WMARK_LOW)
886 mark = (*z)->pages_low;
887 else
888 mark = (*z)->pages_high;
889 if (!zone_watermark_ok(*z, order, mark,
890 classzone_idx, alloc_flags))
891 if (!zone_reclaim_mode ||
892 !zone_reclaim(*z, gfp_mask, order))
893 continue;
894 }
896 page = buffered_rmqueue(zonelist, *z, order, gfp_mask);
897 if (page) {
898 break;
899 }
900 } while (*(++z) != NULL);
901 return page;
902 }
904 /*
905 * This is the 'heart' of the zoned buddy allocator.
906 */
907 struct page * fastcall
908 __alloc_pages(gfp_t gfp_mask, unsigned int order,
909 struct zonelist *zonelist)
910 {
911 const gfp_t wait = gfp_mask & __GFP_WAIT;
912 struct zone **z;
913 struct page *page;
914 struct reclaim_state reclaim_state;
915 struct task_struct *p = current;
916 int do_retry;
917 int alloc_flags;
918 int did_some_progress;
920 might_sleep_if(wait);
922 restart:
923 z = zonelist->zones; /* the list of zones suitable for gfp_mask */
925 if (unlikely(*z == NULL)) {
926 /* Should this ever happen?? */
927 return NULL;
928 }
930 page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, order,
931 zonelist, ALLOC_WMARK_LOW|ALLOC_CPUSET);
932 if (page)
933 goto got_pg;
935 do {
936 wakeup_kswapd(*z, order);
937 } while (*(++z));
939 /*
940 * OK, we're below the kswapd watermark and have kicked background
941 * reclaim. Now things get more complex, so set up alloc_flags according
942 * to how we want to proceed.
943 *
944 * The caller may dip into page reserves a bit more if the caller
945 * cannot run direct reclaim, or if the caller has realtime scheduling
946 * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will
947 * set both ALLOC_HARDER (!wait) and ALLOC_HIGH (__GFP_HIGH).
948 */
949 alloc_flags = ALLOC_WMARK_MIN;
950 if ((unlikely(rt_task(p)) && !in_interrupt()) || !wait)
951 alloc_flags |= ALLOC_HARDER;
952 if (gfp_mask & __GFP_HIGH)
953 alloc_flags |= ALLOC_HIGH;
954 if (wait)
955 alloc_flags |= ALLOC_CPUSET;
957 /*
958 * Go through the zonelist again. Let __GFP_HIGH and allocations
959 * coming from realtime tasks go deeper into reserves.
960 *
961 * This is the last chance, in general, before the goto nopage.
962 * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
963 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
964 */
965 page = get_page_from_freelist(gfp_mask, order, zonelist, alloc_flags);
966 if (page)
967 goto got_pg;
969 /* This allocation should allow future memory freeing. */
971 if (((p->flags & PF_MEMALLOC) || unlikely(test_thread_flag(TIF_MEMDIE)))
972 && !in_interrupt()) {
973 if (!(gfp_mask & __GFP_NOMEMALLOC)) {
974 nofail_alloc:
975 /* go through the zonelist yet again, ignoring mins */
976 page = get_page_from_freelist(gfp_mask, order,
977 zonelist, ALLOC_NO_WATERMARKS);
978 if (page)
979 goto got_pg;
980 if (gfp_mask & __GFP_NOFAIL) {
981 blk_congestion_wait(WRITE, HZ/50);
982 goto nofail_alloc;
983 }
984 }
985 goto nopage;
986 }
988 /* Atomic allocations - we can't balance anything */
989 if (!wait)
990 goto nopage;
992 rebalance:
993 cond_resched();
995 /* We now go into synchronous reclaim */
996 cpuset_memory_pressure_bump();
997 p->flags |= PF_MEMALLOC;
998 reclaim_state.reclaimed_slab = 0;
999 p->reclaim_state = &reclaim_state;
1001 did_some_progress = try_to_free_pages(zonelist->zones, gfp_mask);
1003 p->reclaim_state = NULL;
1004 p->flags &= ~PF_MEMALLOC;
1006 cond_resched();
1008 if (likely(did_some_progress)) {
1009 page = get_page_from_freelist(gfp_mask, order,
1010 zonelist, alloc_flags);
1011 if (page)
1012 goto got_pg;
1013 } else if ((gfp_mask & __GFP_FS) && !(gfp_mask & __GFP_NORETRY)) {
1014 /*
1015 * Go through the zonelist yet one more time, keep
1016 * very high watermark here, this is only to catch
1017 * a parallel oom killing, we must fail if we're still
1018 * under heavy pressure.
1019 */
1020 page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, order,
1021 zonelist, ALLOC_WMARK_HIGH|ALLOC_CPUSET);
1022 if (page)
1023 goto got_pg;
1025 out_of_memory(zonelist, gfp_mask, order);
1026 goto restart;
1029 /*
1030 * Don't let big-order allocations loop unless the caller explicitly
1031 * requests that. Wait for some write requests to complete then retry.
1033 * In this implementation, __GFP_REPEAT means __GFP_NOFAIL for order
1034 * <= 3, but that may not be true in other implementations.
1035 */
1036 do_retry = 0;
1037 if (!(gfp_mask & __GFP_NORETRY)) {
1038 if ((order <= 3) || (gfp_mask & __GFP_REPEAT))
1039 do_retry = 1;
1040 if (gfp_mask & __GFP_NOFAIL)
1041 do_retry = 1;
1043 if (do_retry) {
1044 blk_congestion_wait(WRITE, HZ/50);
1045 goto rebalance;
1048 nopage:
1049 if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) {
1050 printk(KERN_WARNING "%s: page allocation failure."
1051 " order:%d, mode:0x%x\n",
1052 p->comm, order, gfp_mask);
1053 dump_stack();
1054 show_mem();
1056 got_pg:
1057 return page;
1060 EXPORT_SYMBOL(__alloc_pages);
1062 /*
1063 * Common helper functions.
1064 */
1065 fastcall unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
1067 struct page * page;
1068 page = alloc_pages(gfp_mask, order);
1069 if (!page)
1070 return 0;
1071 return (unsigned long) page_address(page);
1074 EXPORT_SYMBOL(__get_free_pages);
1076 fastcall unsigned long get_zeroed_page(gfp_t gfp_mask)
1078 struct page * page;
1080 /*
1081 * get_zeroed_page() returns a 32-bit address, which cannot represent
1082 * a highmem page
1083 */
1084 BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
1086 page = alloc_pages(gfp_mask | __GFP_ZERO, 0);
1087 if (page)
1088 return (unsigned long) page_address(page);
1089 return 0;
1092 EXPORT_SYMBOL(get_zeroed_page);
1094 void __pagevec_free(struct pagevec *pvec)
1096 int i = pagevec_count(pvec);
1098 while (--i >= 0)
1099 free_hot_cold_page(pvec->pages[i], pvec->cold);
1102 fastcall void __free_pages(struct page *page, unsigned int order)
1104 if (put_page_testzero(page)) {
1105 if (order == 0)
1106 free_hot_page(page);
1107 else
1108 __free_pages_ok(page, order);
1112 EXPORT_SYMBOL(__free_pages);
1114 fastcall void free_pages(unsigned long addr, unsigned int order)
1116 if (addr != 0) {
1117 BUG_ON(!virt_addr_valid((void *)addr));
1118 __free_pages(virt_to_page((void *)addr), order);
1122 EXPORT_SYMBOL(free_pages);
1124 /*
1125 * Total amount of free (allocatable) RAM:
1126 */
1127 unsigned int nr_free_pages(void)
1129 unsigned int sum = 0;
1130 struct zone *zone;
1132 for_each_zone(zone)
1133 sum += zone->free_pages;
1135 return sum;
1138 EXPORT_SYMBOL(nr_free_pages);
1140 #ifdef CONFIG_NUMA
1141 unsigned int nr_free_pages_pgdat(pg_data_t *pgdat)
1143 unsigned int i, sum = 0;
1145 for (i = 0; i < MAX_NR_ZONES; i++)
1146 sum += pgdat->node_zones[i].free_pages;
1148 return sum;
1150 #endif
1152 static unsigned int nr_free_zone_pages(int offset)
1154 /* Just pick one node, since fallback list is circular */
1155 pg_data_t *pgdat = NODE_DATA(numa_node_id());
1156 unsigned int sum = 0;
1158 struct zonelist *zonelist = pgdat->node_zonelists + offset;
1159 struct zone **zonep = zonelist->zones;
1160 struct zone *zone;
1162 for (zone = *zonep++; zone; zone = *zonep++) {
1163 unsigned long size = zone->present_pages;
1164 unsigned long high = zone->pages_high;
1165 if (size > high)
1166 sum += size - high;
1169 return sum;
1172 /*
1173 * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
1174 */
1175 unsigned int nr_free_buffer_pages(void)
1177 return nr_free_zone_pages(gfp_zone(GFP_USER));
1180 /*
1181 * Amount of free RAM allocatable within all zones
1182 */
1183 unsigned int nr_free_pagecache_pages(void)
1185 return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER));
1188 #ifdef CONFIG_HIGHMEM
1189 unsigned int nr_free_highpages (void)
1191 pg_data_t *pgdat;
1192 unsigned int pages = 0;
1194 for_each_pgdat(pgdat)
1195 pages += pgdat->node_zones[ZONE_HIGHMEM].free_pages;
1197 return pages;
1199 #endif
1201 #ifdef CONFIG_NUMA
1202 static void show_node(struct zone *zone)
1204 printk("Node %d ", zone->zone_pgdat->node_id);
1206 #else
1207 #define show_node(zone) do { } while (0)
1208 #endif
1210 /*
1211 * Accumulate the page_state information across all CPUs.
1212 * The result is unavoidably approximate - it can change
1213 * during and after execution of this function.
1214 */
1215 static DEFINE_PER_CPU(struct page_state, page_states) = {0};
1217 atomic_t nr_pagecache = ATOMIC_INIT(0);
1218 EXPORT_SYMBOL(nr_pagecache);
1219 #ifdef CONFIG_SMP
1220 DEFINE_PER_CPU(long, nr_pagecache_local) = 0;
1221 #endif
1223 static void __get_page_state(struct page_state *ret, int nr, cpumask_t *cpumask)
1225 int cpu = 0;
1227 memset(ret, 0, nr * sizeof(unsigned long));
1228 cpus_and(*cpumask, *cpumask, cpu_online_map);
1230 cpu = first_cpu(*cpumask);
1231 while (cpu < NR_CPUS) {
1232 unsigned long *in, *out, off;
1234 if (!cpu_isset(cpu, *cpumask))
1235 continue;
1237 in = (unsigned long *)&per_cpu(page_states, cpu);
1239 cpu = next_cpu(cpu, *cpumask);
1241 if (likely(cpu < NR_CPUS))
1242 prefetch(&per_cpu(page_states, cpu));
1244 out = (unsigned long *)ret;
1245 for (off = 0; off < nr; off++)
1246 *out++ += *in++;
1250 void get_page_state_node(struct page_state *ret, int node)
1252 int nr;
1253 cpumask_t mask = node_to_cpumask(node);
1255 nr = offsetof(struct page_state, GET_PAGE_STATE_LAST);
1256 nr /= sizeof(unsigned long);
1258 __get_page_state(ret, nr+1, &mask);
1261 void get_page_state(struct page_state *ret)
1263 int nr;
1264 cpumask_t mask = CPU_MASK_ALL;
1266 nr = offsetof(struct page_state, GET_PAGE_STATE_LAST);
1267 nr /= sizeof(unsigned long);
1269 __get_page_state(ret, nr + 1, &mask);
1272 void get_full_page_state(struct page_state *ret)
1274 cpumask_t mask = CPU_MASK_ALL;
1276 __get_page_state(ret, sizeof(*ret) / sizeof(unsigned long), &mask);
1279 unsigned long read_page_state_offset(unsigned long offset)
1281 unsigned long ret = 0;
1282 int cpu;
1284 for_each_online_cpu(cpu) {
1285 unsigned long in;
1287 in = (unsigned long)&per_cpu(page_states, cpu) + offset;
1288 ret += *((unsigned long *)in);
1290 return ret;
1293 void __mod_page_state_offset(unsigned long offset, unsigned long delta)
1295 void *ptr;
1297 ptr = &__get_cpu_var(page_states);
1298 *(unsigned long *)(ptr + offset) += delta;
1300 EXPORT_SYMBOL(__mod_page_state_offset);
1302 void mod_page_state_offset(unsigned long offset, unsigned long delta)
1304 unsigned long flags;
1305 void *ptr;
1307 local_irq_save(flags);
1308 ptr = &__get_cpu_var(page_states);
1309 *(unsigned long *)(ptr + offset) += delta;
1310 local_irq_restore(flags);
1312 EXPORT_SYMBOL(mod_page_state_offset);
1314 void __get_zone_counts(unsigned long *active, unsigned long *inactive,
1315 unsigned long *free, struct pglist_data *pgdat)
1317 struct zone *zones = pgdat->node_zones;
1318 int i;
1320 *active = 0;
1321 *inactive = 0;
1322 *free = 0;
1323 for (i = 0; i < MAX_NR_ZONES; i++) {
1324 *active += zones[i].nr_active;
1325 *inactive += zones[i].nr_inactive;
1326 *free += zones[i].free_pages;
1330 void get_zone_counts(unsigned long *active,
1331 unsigned long *inactive, unsigned long *free)
1333 struct pglist_data *pgdat;
1335 *active = 0;
1336 *inactive = 0;
1337 *free = 0;
1338 for_each_pgdat(pgdat) {
1339 unsigned long l, m, n;
1340 __get_zone_counts(&l, &m, &n, pgdat);
1341 *active += l;
1342 *inactive += m;
1343 *free += n;
1347 void si_meminfo(struct sysinfo *val)
1349 val->totalram = totalram_pages;
1350 val->sharedram = 0;
1351 val->freeram = nr_free_pages();
1352 val->bufferram = nr_blockdev_pages();
1353 #ifdef CONFIG_HIGHMEM
1354 val->totalhigh = totalhigh_pages;
1355 val->freehigh = nr_free_highpages();
1356 #else
1357 val->totalhigh = 0;
1358 val->freehigh = 0;
1359 #endif
1360 val->mem_unit = PAGE_SIZE;
1363 EXPORT_SYMBOL(si_meminfo);
1365 #ifdef CONFIG_NUMA
1366 void si_meminfo_node(struct sysinfo *val, int nid)
1368 pg_data_t *pgdat = NODE_DATA(nid);
1370 val->totalram = pgdat->node_present_pages;
1371 val->freeram = nr_free_pages_pgdat(pgdat);
1372 val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].present_pages;
1373 val->freehigh = pgdat->node_zones[ZONE_HIGHMEM].free_pages;
1374 val->mem_unit = PAGE_SIZE;
1376 #endif
1378 #define K(x) ((x) << (PAGE_SHIFT-10))
1380 /*
1381 * Show free area list (used inside shift_scroll-lock stuff)
1382 * We also calculate the percentage fragmentation. We do this by counting the
1383 * memory on each free list with the exception of the first item on the list.
1384 */
1385 void show_free_areas(void)
1387 struct page_state ps;
1388 int cpu, temperature;
1389 unsigned long active;
1390 unsigned long inactive;
1391 unsigned long free;
1392 struct zone *zone;
1394 for_each_zone(zone) {
1395 show_node(zone);
1396 printk("%s per-cpu:", zone->name);
1398 if (!populated_zone(zone)) {
1399 printk(" empty\n");
1400 continue;
1401 } else
1402 printk("\n");
1404 for_each_online_cpu(cpu) {
1405 struct per_cpu_pageset *pageset;
1407 pageset = zone_pcp(zone, cpu);
1409 for (temperature = 0; temperature < 2; temperature++)
1410 printk("cpu %d %s: high %d, batch %d used:%d\n",
1411 cpu,
1412 temperature ? "cold" : "hot",
1413 pageset->pcp[temperature].high,
1414 pageset->pcp[temperature].batch,
1415 pageset->pcp[temperature].count);
1419 get_page_state(&ps);
1420 get_zone_counts(&active, &inactive, &free);
1422 printk("Free pages: %11ukB (%ukB HighMem)\n",
1423 K(nr_free_pages()),
1424 K(nr_free_highpages()));
1426 printk("Active:%lu inactive:%lu dirty:%lu writeback:%lu "
1427 "unstable:%lu free:%u slab:%lu mapped:%lu pagetables:%lu\n",
1428 active,
1429 inactive,
1430 ps.nr_dirty,
1431 ps.nr_writeback,
1432 ps.nr_unstable,
1433 nr_free_pages(),
1434 ps.nr_slab,
1435 ps.nr_mapped,
1436 ps.nr_page_table_pages);
1438 for_each_zone(zone) {
1439 int i;
1441 show_node(zone);
1442 printk("%s"
1443 " free:%lukB"
1444 " min:%lukB"
1445 " low:%lukB"
1446 " high:%lukB"
1447 " active:%lukB"
1448 " inactive:%lukB"
1449 " present:%lukB"
1450 " pages_scanned:%lu"
1451 " all_unreclaimable? %s"
1452 "\n",
1453 zone->name,
1454 K(zone->free_pages),
1455 K(zone->pages_min),
1456 K(zone->pages_low),
1457 K(zone->pages_high),
1458 K(zone->nr_active),
1459 K(zone->nr_inactive),
1460 K(zone->present_pages),
1461 zone->pages_scanned,
1462 (zone->all_unreclaimable ? "yes" : "no")
1463 );
1464 printk("lowmem_reserve[]:");
1465 for (i = 0; i < MAX_NR_ZONES; i++)
1466 printk(" %lu", zone->lowmem_reserve[i]);
1467 printk("\n");
1470 for_each_zone(zone) {
1471 unsigned long nr, flags, order, total = 0;
1473 show_node(zone);
1474 printk("%s: ", zone->name);
1475 if (!populated_zone(zone)) {
1476 printk("empty\n");
1477 continue;
1480 spin_lock_irqsave(&zone->lock, flags);
1481 for (order = 0; order < MAX_ORDER; order++) {
1482 nr = zone->free_area[order].nr_free;
1483 total += nr << order;
1484 printk("%lu*%lukB ", nr, K(1UL) << order);
1486 spin_unlock_irqrestore(&zone->lock, flags);
1487 printk("= %lukB\n", K(total));
1490 show_swap_cache_info();
1493 /*
1494 * Builds allocation fallback zone lists.
1496 * Add all populated zones of a node to the zonelist.
1497 */
1498 static int __init build_zonelists_node(pg_data_t *pgdat,
1499 struct zonelist *zonelist, int nr_zones, int zone_type)
1501 struct zone *zone;
1503 BUG_ON(zone_type > ZONE_HIGHMEM);
1505 do {
1506 zone = pgdat->node_zones + zone_type;
1507 if (populated_zone(zone)) {
1508 #ifndef CONFIG_HIGHMEM
1509 BUG_ON(zone_type > ZONE_NORMAL);
1510 #endif
1511 zonelist->zones[nr_zones++] = zone;
1512 check_highest_zone(zone_type);
1514 zone_type--;
1516 } while (zone_type >= 0);
1517 return nr_zones;
1520 static inline int highest_zone(int zone_bits)
1522 int res = ZONE_NORMAL;
1523 if (zone_bits & (__force int)__GFP_HIGHMEM)
1524 res = ZONE_HIGHMEM;
1525 if (zone_bits & (__force int)__GFP_DMA32)
1526 res = ZONE_DMA32;
1527 if (zone_bits & (__force int)__GFP_DMA)
1528 res = ZONE_DMA;
1529 return res;
1532 #ifdef CONFIG_NUMA
1533 #define MAX_NODE_LOAD (num_online_nodes())
1534 static int __initdata node_load[MAX_NUMNODES];
1535 /**
1536 * find_next_best_node - find the next node that should appear in a given node's fallback list
1537 * @node: node whose fallback list we're appending
1538 * @used_node_mask: nodemask_t of already used nodes
1540 * We use a number of factors to determine which is the next node that should
1541 * appear on a given node's fallback list. The node should not have appeared
1542 * already in @node's fallback list, and it should be the next closest node
1543 * according to the distance array (which contains arbitrary distance values
1544 * from each node to each node in the system), and should also prefer nodes
1545 * with no CPUs, since presumably they'll have very little allocation pressure
1546 * on them otherwise.
1547 * It returns -1 if no node is found.
1548 */
1549 static int __init find_next_best_node(int node, nodemask_t *used_node_mask)
1551 int n, val;
1552 int min_val = INT_MAX;
1553 int best_node = -1;
1555 /* Use the local node if we haven't already */
1556 if (!node_isset(node, *used_node_mask)) {
1557 node_set(node, *used_node_mask);
1558 return node;
1561 for_each_online_node(n) {
1562 cpumask_t tmp;
1564 /* Don't want a node to appear more than once */
1565 if (node_isset(n, *used_node_mask))
1566 continue;
1568 /* Use the distance array to find the distance */
1569 val = node_distance(node, n);
1571 /* Penalize nodes under us ("prefer the next node") */
1572 val += (n < node);
1574 /* Give preference to headless and unused nodes */
1575 tmp = node_to_cpumask(n);
1576 if (!cpus_empty(tmp))
1577 val += PENALTY_FOR_NODE_WITH_CPUS;
1579 /* Slight preference for less loaded node */
1580 val *= (MAX_NODE_LOAD*MAX_NUMNODES);
1581 val += node_load[n];
1583 if (val < min_val) {
1584 min_val = val;
1585 best_node = n;
1589 if (best_node >= 0)
1590 node_set(best_node, *used_node_mask);
1592 return best_node;
1595 static void __init build_zonelists(pg_data_t *pgdat)
1597 int i, j, k, node, local_node;
1598 int prev_node, load;
1599 struct zonelist *zonelist;
1600 nodemask_t used_mask;
1602 /* initialize zonelists */
1603 for (i = 0; i < GFP_ZONETYPES; i++) {
1604 zonelist = pgdat->node_zonelists + i;
1605 zonelist->zones[0] = NULL;
1608 /* NUMA-aware ordering of nodes */
1609 local_node = pgdat->node_id;
1610 load = num_online_nodes();
1611 prev_node = local_node;
1612 nodes_clear(used_mask);
1613 while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
1614 int distance = node_distance(local_node, node);
1616 /*
1617 * If another node is sufficiently far away then it is better
1618 * to reclaim pages in a zone before going off node.
1619 */
1620 if (distance > RECLAIM_DISTANCE)
1621 zone_reclaim_mode = 1;
1623 /*
1624 * We don't want to pressure a particular node.
1625 * So adding penalty to the first node in same
1626 * distance group to make it round-robin.
1627 */
1629 if (distance != node_distance(local_node, prev_node))
1630 node_load[node] += load;
1631 prev_node = node;
1632 load--;
1633 for (i = 0; i < GFP_ZONETYPES; i++) {
1634 zonelist = pgdat->node_zonelists + i;
1635 for (j = 0; zonelist->zones[j] != NULL; j++);
1637 k = highest_zone(i);
1639 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1640 zonelist->zones[j] = NULL;
1645 #else /* CONFIG_NUMA */
1647 static void __init build_zonelists(pg_data_t *pgdat)
1649 int i, j, k, node, local_node;
1651 local_node = pgdat->node_id;
1652 for (i = 0; i < GFP_ZONETYPES; i++) {
1653 struct zonelist *zonelist;
1655 zonelist = pgdat->node_zonelists + i;
1657 j = 0;
1658 k = highest_zone(i);
1659 j = build_zonelists_node(pgdat, zonelist, j, k);
1660 /*
1661 * Now we build the zonelist so that it contains the zones
1662 * of all the other nodes.
1663 * We don't want to pressure a particular node, so when
1664 * building the zones for node N, we make sure that the
1665 * zones coming right after the local ones are those from
1666 * node N+1 (modulo N)
1667 */
1668 for (node = local_node + 1; node < MAX_NUMNODES; node++) {
1669 if (!node_online(node))
1670 continue;
1671 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1673 for (node = 0; node < local_node; node++) {
1674 if (!node_online(node))
1675 continue;
1676 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1679 zonelist->zones[j] = NULL;
1683 #endif /* CONFIG_NUMA */
1685 void __init build_all_zonelists(void)
1687 int i;
1689 for_each_online_node(i)
1690 build_zonelists(NODE_DATA(i));
1691 printk("Built %i zonelists\n", num_online_nodes());
1692 cpuset_init_current_mems_allowed();
1695 /*
1696 * Helper functions to size the waitqueue hash table.
1697 * Essentially these want to choose hash table sizes sufficiently
1698 * large so that collisions trying to wait on pages are rare.
1699 * But in fact, the number of active page waitqueues on typical
1700 * systems is ridiculously low, less than 200. So this is even
1701 * conservative, even though it seems large.
1703 * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
1704 * waitqueues, i.e. the size of the waitq table given the number of pages.
1705 */
1706 #define PAGES_PER_WAITQUEUE 256
1708 static inline unsigned long wait_table_size(unsigned long pages)
1710 unsigned long size = 1;
1712 pages /= PAGES_PER_WAITQUEUE;
1714 while (size < pages)
1715 size <<= 1;
1717 /*
1718 * Once we have dozens or even hundreds of threads sleeping
1719 * on IO we've got bigger problems than wait queue collision.
1720 * Limit the size of the wait table to a reasonable size.
1721 */
1722 size = min(size, 4096UL);
1724 return max(size, 4UL);
1727 /*
1728 * This is an integer logarithm so that shifts can be used later
1729 * to extract the more random high bits from the multiplicative
1730 * hash function before the remainder is taken.
1731 */
1732 static inline unsigned long wait_table_bits(unsigned long size)
1734 return ffz(~size);
1737 #define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
1739 static void __init calculate_zone_totalpages(struct pglist_data *pgdat,
1740 unsigned long *zones_size, unsigned long *zholes_size)
1742 unsigned long realtotalpages, totalpages = 0;
1743 int i;
1745 for (i = 0; i < MAX_NR_ZONES; i++)
1746 totalpages += zones_size[i];
1747 pgdat->node_spanned_pages = totalpages;
1749 realtotalpages = totalpages;
1750 if (zholes_size)
1751 for (i = 0; i < MAX_NR_ZONES; i++)
1752 realtotalpages -= zholes_size[i];
1753 pgdat->node_present_pages = realtotalpages;
1754 printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id, realtotalpages);
1758 /*
1759 * Initially all pages are reserved - free ones are freed
1760 * up by free_all_bootmem() once the early boot process is
1761 * done. Non-atomic initialization, single-pass.
1762 */
1763 void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
1764 unsigned long start_pfn)
1766 struct page *page;
1767 unsigned long end_pfn = start_pfn + size;
1768 unsigned long pfn;
1770 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
1771 if (!early_pfn_valid(pfn))
1772 continue;
1773 page = pfn_to_page(pfn);
1774 set_page_links(page, zone, nid, pfn);
1775 set_page_count(page, 1);
1776 reset_page_mapcount(page);
1777 SetPageReserved(page);
1778 INIT_LIST_HEAD(&page->lru);
1779 #ifdef WANT_PAGE_VIRTUAL
1780 /* The shift won't overflow because ZONE_NORMAL is below 4G. */
1781 if (!is_highmem_idx(zone))
1782 set_page_address(page, __va(pfn << PAGE_SHIFT));
1783 #endif
1787 void zone_init_free_lists(struct pglist_data *pgdat, struct zone *zone,
1788 unsigned long size)
1790 int order;
1791 for (order = 0; order < MAX_ORDER ; order++) {
1792 INIT_LIST_HEAD(&zone->free_area[order].free_list);
1793 zone->free_area[order].nr_free = 0;
1797 #define ZONETABLE_INDEX(x, zone_nr) ((x << ZONES_SHIFT) | zone_nr)
1798 void zonetable_add(struct zone *zone, int nid, int zid, unsigned long pfn,
1799 unsigned long size)
1801 unsigned long snum = pfn_to_section_nr(pfn);
1802 unsigned long end = pfn_to_section_nr(pfn + size);
1804 if (FLAGS_HAS_NODE)
1805 zone_table[ZONETABLE_INDEX(nid, zid)] = zone;
1806 else
1807 for (; snum <= end; snum++)
1808 zone_table[ZONETABLE_INDEX(snum, zid)] = zone;
1811 #ifndef __HAVE_ARCH_MEMMAP_INIT
1812 #define memmap_init(size, nid, zone, start_pfn) \
1813 memmap_init_zone((size), (nid), (zone), (start_pfn))
1814 #endif
1816 static int __cpuinit zone_batchsize(struct zone *zone)
1818 int batch;
1820 /*
1821 * The per-cpu-pages pools are set to around 1000th of the
1822 * size of the zone. But no more than 1/2 of a meg.
1824 * OK, so we don't know how big the cache is. So guess.
1825 */
1826 batch = zone->present_pages / 1024;
1827 if (batch * PAGE_SIZE > 512 * 1024)
1828 batch = (512 * 1024) / PAGE_SIZE;
1829 batch /= 4; /* We effectively *= 4 below */
1830 if (batch < 1)
1831 batch = 1;
1833 /*
1834 * Clamp the batch to a 2^n - 1 value. Having a power
1835 * of 2 value was found to be more likely to have
1836 * suboptimal cache aliasing properties in some cases.
1838 * For example if 2 tasks are alternately allocating
1839 * batches of pages, one task can end up with a lot
1840 * of pages of one half of the possible page colors
1841 * and the other with pages of the other colors.
1842 */
1843 batch = (1 << (fls(batch + batch/2)-1)) - 1;
1845 return batch;
1848 inline void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
1850 struct per_cpu_pages *pcp;
1852 memset(p, 0, sizeof(*p));
1854 pcp = &p->pcp[0]; /* hot */
1855 pcp->count = 0;
1856 pcp->high = 6 * batch;
1857 pcp->batch = max(1UL, 1 * batch);
1858 INIT_LIST_HEAD(&pcp->list);
1860 pcp = &p->pcp[1]; /* cold*/
1861 pcp->count = 0;
1862 pcp->high = 2 * batch;
1863 pcp->batch = max(1UL, batch/2);
1864 INIT_LIST_HEAD(&pcp->list);
1867 /*
1868 * setup_pagelist_highmark() sets the high water mark for hot per_cpu_pagelist
1869 * to the value high for the pageset p.
1870 */
1872 static void setup_pagelist_highmark(struct per_cpu_pageset *p,
1873 unsigned long high)
1875 struct per_cpu_pages *pcp;
1877 pcp = &p->pcp[0]; /* hot list */
1878 pcp->high = high;
1879 pcp->batch = max(1UL, high/4);
1880 if ((high/4) > (PAGE_SHIFT * 8))
1881 pcp->batch = PAGE_SHIFT * 8;
1885 #ifdef CONFIG_NUMA
1886 /*
1887 * Boot pageset table. One per cpu which is going to be used for all
1888 * zones and all nodes. The parameters will be set in such a way
1889 * that an item put on a list will immediately be handed over to
1890 * the buddy list. This is safe since pageset manipulation is done
1891 * with interrupts disabled.
1893 * Some NUMA counter updates may also be caught by the boot pagesets.
1895 * The boot_pagesets must be kept even after bootup is complete for
1896 * unused processors and/or zones. They do play a role for bootstrapping
1897 * hotplugged processors.
1899 * zoneinfo_show() and maybe other functions do
1900 * not check if the processor is online before following the pageset pointer.
1901 * Other parts of the kernel may not check if the zone is available.
1902 */
1903 static struct per_cpu_pageset boot_pageset[NR_CPUS];
1905 /*
1906 * Dynamically allocate memory for the
1907 * per cpu pageset array in struct zone.
1908 */
1909 static int __cpuinit process_zones(int cpu)
1911 struct zone *zone, *dzone;
1913 for_each_zone(zone) {
1915 zone_pcp(zone, cpu) = kmalloc_node(sizeof(struct per_cpu_pageset),
1916 GFP_KERNEL, cpu_to_node(cpu));
1917 if (!zone_pcp(zone, cpu))
1918 goto bad;
1920 setup_pageset(zone_pcp(zone, cpu), zone_batchsize(zone));
1922 if (percpu_pagelist_fraction)
1923 setup_pagelist_highmark(zone_pcp(zone, cpu),
1924 (zone->present_pages / percpu_pagelist_fraction));
1927 return 0;
1928 bad:
1929 for_each_zone(dzone) {
1930 if (dzone == zone)
1931 break;
1932 kfree(zone_pcp(dzone, cpu));
1933 zone_pcp(dzone, cpu) = NULL;
1935 return -ENOMEM;
1938 static inline void free_zone_pagesets(int cpu)
1940 struct zone *zone;
1942 for_each_zone(zone) {
1943 struct per_cpu_pageset *pset = zone_pcp(zone, cpu);
1945 zone_pcp(zone, cpu) = NULL;
1946 kfree(pset);
1950 static int __cpuinit pageset_cpuup_callback(struct notifier_block *nfb,
1951 unsigned long action,
1952 void *hcpu)
1954 int cpu = (long)hcpu;
1955 int ret = NOTIFY_OK;
1957 switch (action) {
1958 case CPU_UP_PREPARE:
1959 if (process_zones(cpu))
1960 ret = NOTIFY_BAD;
1961 break;
1962 case CPU_UP_CANCELED:
1963 case CPU_DEAD:
1964 free_zone_pagesets(cpu);
1965 break;
1966 default:
1967 break;
1969 return ret;
1972 static struct notifier_block pageset_notifier =
1973 { &pageset_cpuup_callback, NULL, 0 };
1975 void __init setup_per_cpu_pageset(void)
1977 int err;
1979 /* Initialize per_cpu_pageset for cpu 0.
1980 * A cpuup callback will do this for every cpu
1981 * as it comes online
1982 */
1983 err = process_zones(smp_processor_id());
1984 BUG_ON(err);
1985 register_cpu_notifier(&pageset_notifier);
1988 #endif
1990 static __meminit
1991 void zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages)
1993 int i;
1994 struct pglist_data *pgdat = zone->zone_pgdat;
1996 /*
1997 * The per-page waitqueue mechanism uses hashed waitqueues
1998 * per zone.
1999 */
2000 zone->wait_table_size = wait_table_size(zone_size_pages);
2001 zone->wait_table_bits = wait_table_bits(zone->wait_table_size);
2002 zone->wait_table = (wait_queue_head_t *)
2003 alloc_bootmem_node(pgdat, zone->wait_table_size
2004 * sizeof(wait_queue_head_t));
2006 for(i = 0; i < zone->wait_table_size; ++i)
2007 init_waitqueue_head(zone->wait_table + i);
2010 static __meminit void zone_pcp_init(struct zone *zone)
2012 int cpu;
2013 unsigned long batch = zone_batchsize(zone);
2015 for (cpu = 0; cpu < NR_CPUS; cpu++) {
2016 #ifdef CONFIG_NUMA
2017 /* Early boot. Slab allocator not functional yet */
2018 zone_pcp(zone, cpu) = &boot_pageset[cpu];
2019 setup_pageset(&boot_pageset[cpu],0);
2020 #else
2021 setup_pageset(zone_pcp(zone,cpu), batch);
2022 #endif
2024 printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%lu\n",
2025 zone->name, zone->present_pages, batch);
2028 static __meminit void init_currently_empty_zone(struct zone *zone,
2029 unsigned long zone_start_pfn, unsigned long size)
2031 struct pglist_data *pgdat = zone->zone_pgdat;
2033 zone_wait_table_init(zone, size);
2034 pgdat->nr_zones = zone_idx(zone) + 1;
2036 zone->zone_mem_map = pfn_to_page(zone_start_pfn);
2037 zone->zone_start_pfn = zone_start_pfn;
2039 memmap_init(size, pgdat->node_id, zone_idx(zone), zone_start_pfn);
2041 zone_init_free_lists(pgdat, zone, zone->spanned_pages);
2044 /*
2045 * Set up the zone data structures:
2046 * - mark all pages reserved
2047 * - mark all memory queues empty
2048 * - clear the memory bitmaps
2049 */
2050 static void __init free_area_init_core(struct pglist_data *pgdat,
2051 unsigned long *zones_size, unsigned long *zholes_size)
2053 unsigned long j;
2054 int nid = pgdat->node_id;
2055 unsigned long zone_start_pfn = pgdat->node_start_pfn;
2057 pgdat_resize_init(pgdat);
2058 pgdat->nr_zones = 0;
2059 init_waitqueue_head(&pgdat->kswapd_wait);
2060 pgdat->kswapd_max_order = 0;
2062 for (j = 0; j < MAX_NR_ZONES; j++) {
2063 struct zone *zone = pgdat->node_zones + j;
2064 unsigned long size, realsize;
2066 realsize = size = zones_size[j];
2067 if (zholes_size)
2068 realsize -= zholes_size[j];
2070 if (j < ZONE_HIGHMEM)
2071 nr_kernel_pages += realsize;
2072 nr_all_pages += realsize;
2074 zone->spanned_pages = size;
2075 zone->present_pages = realsize;
2076 zone->name = zone_names[j];
2077 spin_lock_init(&zone->lock);
2078 spin_lock_init(&zone->lru_lock);
2079 zone_seqlock_init(zone);
2080 zone->zone_pgdat = pgdat;
2081 zone->free_pages = 0;
2083 zone->temp_priority = zone->prev_priority = DEF_PRIORITY;
2085 zone_pcp_init(zone);
2086 INIT_LIST_HEAD(&zone->active_list);
2087 INIT_LIST_HEAD(&zone->inactive_list);
2088 zone->nr_scan_active = 0;
2089 zone->nr_scan_inactive = 0;
2090 zone->nr_active = 0;
2091 zone->nr_inactive = 0;
2092 atomic_set(&zone->reclaim_in_progress, 0);
2093 if (!size)
2094 continue;
2096 zonetable_add(zone, nid, j, zone_start_pfn, size);
2097 init_currently_empty_zone(zone, zone_start_pfn, size);
2098 zone_start_pfn += size;
2102 static void __init alloc_node_mem_map(struct pglist_data *pgdat)
2104 /* Skip empty nodes */
2105 if (!pgdat->node_spanned_pages)
2106 return;
2108 #ifdef CONFIG_FLAT_NODE_MEM_MAP
2109 /* ia64 gets its own node_mem_map, before this, without bootmem */
2110 if (!pgdat->node_mem_map) {
2111 unsigned long size;
2112 struct page *map;
2114 size = (pgdat->node_spanned_pages + 1) * sizeof(struct page);
2115 map = alloc_remap(pgdat->node_id, size);
2116 if (!map)
2117 map = alloc_bootmem_node(pgdat, size);
2118 pgdat->node_mem_map = map;
2120 #ifdef CONFIG_FLATMEM
2121 /*
2122 * With no DISCONTIG, the global mem_map is just set as node 0's
2123 */
2124 if (pgdat == NODE_DATA(0))
2125 mem_map = NODE_DATA(0)->node_mem_map;
2126 #endif
2127 #endif /* CONFIG_FLAT_NODE_MEM_MAP */
2130 void __init free_area_init_node(int nid, struct pglist_data *pgdat,
2131 unsigned long *zones_size, unsigned long node_start_pfn,
2132 unsigned long *zholes_size)
2134 pgdat->node_id = nid;
2135 pgdat->node_start_pfn = node_start_pfn;
2136 calculate_zone_totalpages(pgdat, zones_size, zholes_size);
2138 alloc_node_mem_map(pgdat);
2140 free_area_init_core(pgdat, zones_size, zholes_size);
2143 #ifndef CONFIG_NEED_MULTIPLE_NODES
2144 static bootmem_data_t contig_bootmem_data;
2145 struct pglist_data contig_page_data = { .bdata = &contig_bootmem_data };
2147 EXPORT_SYMBOL(contig_page_data);
2148 #endif
2150 void __init free_area_init(unsigned long *zones_size)
2152 free_area_init_node(0, NODE_DATA(0), zones_size,
2153 __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
2156 #ifdef CONFIG_PROC_FS
2158 #include <linux/seq_file.h>
2160 static void *frag_start(struct seq_file *m, loff_t *pos)
2162 pg_data_t *pgdat;
2163 loff_t node = *pos;
2165 for (pgdat = pgdat_list; pgdat && node; pgdat = pgdat->pgdat_next)
2166 --node;
2168 return pgdat;
2171 static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
2173 pg_data_t *pgdat = (pg_data_t *)arg;
2175 (*pos)++;
2176 return pgdat->pgdat_next;
2179 static void frag_stop(struct seq_file *m, void *arg)
2183 /*
2184 * This walks the free areas for each zone.
2185 */
2186 static int frag_show(struct seq_file *m, void *arg)
2188 pg_data_t *pgdat = (pg_data_t *)arg;
2189 struct zone *zone;
2190 struct zone *node_zones = pgdat->node_zones;
2191 unsigned long flags;
2192 int order;
2194 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
2195 if (!populated_zone(zone))
2196 continue;
2198 spin_lock_irqsave(&zone->lock, flags);
2199 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
2200 for (order = 0; order < MAX_ORDER; ++order)
2201 seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
2202 spin_unlock_irqrestore(&zone->lock, flags);
2203 seq_putc(m, '\n');
2205 return 0;
2208 struct seq_operations fragmentation_op = {
2209 .start = frag_start,
2210 .next = frag_next,
2211 .stop = frag_stop,
2212 .show = frag_show,
2213 };
2215 /*
2216 * Output information about zones in @pgdat.
2217 */
2218 static int zoneinfo_show(struct seq_file *m, void *arg)
2220 pg_data_t *pgdat = arg;
2221 struct zone *zone;
2222 struct zone *node_zones = pgdat->node_zones;
2223 unsigned long flags;
2225 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; zone++) {
2226 int i;
2228 if (!populated_zone(zone))
2229 continue;
2231 spin_lock_irqsave(&zone->lock, flags);
2232 seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
2233 seq_printf(m,
2234 "\n pages free %lu"
2235 "\n min %lu"
2236 "\n low %lu"
2237 "\n high %lu"
2238 "\n active %lu"
2239 "\n inactive %lu"
2240 "\n scanned %lu (a: %lu i: %lu)"
2241 "\n spanned %lu"
2242 "\n present %lu",
2243 zone->free_pages,
2244 zone->pages_min,
2245 zone->pages_low,
2246 zone->pages_high,
2247 zone->nr_active,
2248 zone->nr_inactive,
2249 zone->pages_scanned,
2250 zone->nr_scan_active, zone->nr_scan_inactive,
2251 zone->spanned_pages,
2252 zone->present_pages);
2253 seq_printf(m,
2254 "\n protection: (%lu",
2255 zone->lowmem_reserve[0]);
2256 for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
2257 seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
2258 seq_printf(m,
2259 ")"
2260 "\n pagesets");
2261 for_each_online_cpu(i) {
2262 struct per_cpu_pageset *pageset;
2263 int j;
2265 pageset = zone_pcp(zone, i);
2266 for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
2267 if (pageset->pcp[j].count)
2268 break;
2270 if (j == ARRAY_SIZE(pageset->pcp))
2271 continue;
2272 for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
2273 seq_printf(m,
2274 "\n cpu: %i pcp: %i"
2275 "\n count: %i"
2276 "\n high: %i"
2277 "\n batch: %i",
2278 i, j,
2279 pageset->pcp[j].count,
2280 pageset->pcp[j].high,
2281 pageset->pcp[j].batch);
2283 #ifdef CONFIG_NUMA
2284 seq_printf(m,
2285 "\n numa_hit: %lu"
2286 "\n numa_miss: %lu"
2287 "\n numa_foreign: %lu"
2288 "\n interleave_hit: %lu"
2289 "\n local_node: %lu"
2290 "\n other_node: %lu",
2291 pageset->numa_hit,
2292 pageset->numa_miss,
2293 pageset->numa_foreign,
2294 pageset->interleave_hit,
2295 pageset->local_node,
2296 pageset->other_node);
2297 #endif
2299 seq_printf(m,
2300 "\n all_unreclaimable: %u"
2301 "\n prev_priority: %i"
2302 "\n temp_priority: %i"
2303 "\n start_pfn: %lu",
2304 zone->all_unreclaimable,
2305 zone->prev_priority,
2306 zone->temp_priority,
2307 zone->zone_start_pfn);
2308 spin_unlock_irqrestore(&zone->lock, flags);
2309 seq_putc(m, '\n');
2311 return 0;
2314 struct seq_operations zoneinfo_op = {
2315 .start = frag_start, /* iterate over all zones. The same as in
2316 * fragmentation. */
2317 .next = frag_next,
2318 .stop = frag_stop,
2319 .show = zoneinfo_show,
2320 };
2322 static char *vmstat_text[] = {
2323 "nr_dirty",
2324 "nr_writeback",
2325 "nr_unstable",
2326 "nr_page_table_pages",
2327 "nr_mapped",
2328 "nr_slab",
2330 "pgpgin",
2331 "pgpgout",
2332 "pswpin",
2333 "pswpout",
2335 "pgalloc_high",
2336 "pgalloc_normal",
2337 "pgalloc_dma32",
2338 "pgalloc_dma",
2340 "pgfree",
2341 "pgactivate",
2342 "pgdeactivate",
2344 "pgfault",
2345 "pgmajfault",
2347 "pgrefill_high",
2348 "pgrefill_normal",
2349 "pgrefill_dma32",
2350 "pgrefill_dma",
2352 "pgsteal_high",
2353 "pgsteal_normal",
2354 "pgsteal_dma32",
2355 "pgsteal_dma",
2357 "pgscan_kswapd_high",
2358 "pgscan_kswapd_normal",
2359 "pgscan_kswapd_dma32",
2360 "pgscan_kswapd_dma",
2362 "pgscan_direct_high",
2363 "pgscan_direct_normal",
2364 "pgscan_direct_dma32",
2365 "pgscan_direct_dma",
2367 "pginodesteal",
2368 "slabs_scanned",
2369 "kswapd_steal",
2370 "kswapd_inodesteal",
2371 "pageoutrun",
2372 "allocstall",
2374 "pgrotated",
2375 "nr_bounce",
2376 };
2378 static void *vmstat_start(struct seq_file *m, loff_t *pos)
2380 struct page_state *ps;
2382 if (*pos >= ARRAY_SIZE(vmstat_text))
2383 return NULL;
2385 ps = kmalloc(sizeof(*ps), GFP_KERNEL);
2386 m->private = ps;
2387 if (!ps)
2388 return ERR_PTR(-ENOMEM);
2389 get_full_page_state(ps);
2390 ps->pgpgin /= 2; /* sectors -> kbytes */
2391 ps->pgpgout /= 2;
2392 return (unsigned long *)ps + *pos;
2395 static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
2397 (*pos)++;
2398 if (*pos >= ARRAY_SIZE(vmstat_text))
2399 return NULL;
2400 return (unsigned long *)m->private + *pos;
2403 static int vmstat_show(struct seq_file *m, void *arg)
2405 unsigned long *l = arg;
2406 unsigned long off = l - (unsigned long *)m->private;
2408 seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
2409 return 0;
2412 static void vmstat_stop(struct seq_file *m, void *arg)
2414 kfree(m->private);
2415 m->private = NULL;
2418 struct seq_operations vmstat_op = {
2419 .start = vmstat_start,
2420 .next = vmstat_next,
2421 .stop = vmstat_stop,
2422 .show = vmstat_show,
2423 };
2425 #endif /* CONFIG_PROC_FS */
2427 #ifdef CONFIG_HOTPLUG_CPU
2428 static int page_alloc_cpu_notify(struct notifier_block *self,
2429 unsigned long action, void *hcpu)
2431 int cpu = (unsigned long)hcpu;
2432 long *count;
2433 unsigned long *src, *dest;
2435 if (action == CPU_DEAD) {
2436 int i;
2438 /* Drain local pagecache count. */
2439 count = &per_cpu(nr_pagecache_local, cpu);
2440 atomic_add(*count, &nr_pagecache);
2441 *count = 0;
2442 local_irq_disable();
2443 __drain_pages(cpu);
2445 /* Add dead cpu's page_states to our own. */
2446 dest = (unsigned long *)&__get_cpu_var(page_states);
2447 src = (unsigned long *)&per_cpu(page_states, cpu);
2449 for (i = 0; i < sizeof(struct page_state)/sizeof(unsigned long);
2450 i++) {
2451 dest[i] += src[i];
2452 src[i] = 0;
2455 local_irq_enable();
2457 return NOTIFY_OK;
2459 #endif /* CONFIG_HOTPLUG_CPU */
2461 void __init page_alloc_init(void)
2463 hotcpu_notifier(page_alloc_cpu_notify, 0);
2466 /*
2467 * setup_per_zone_lowmem_reserve - called whenever
2468 * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
2469 * has a correct pages reserved value, so an adequate number of
2470 * pages are left in the zone after a successful __alloc_pages().
2471 */
2472 static void setup_per_zone_lowmem_reserve(void)
2474 struct pglist_data *pgdat;
2475 int j, idx;
2477 for_each_pgdat(pgdat) {
2478 for (j = 0; j < MAX_NR_ZONES; j++) {
2479 struct zone *zone = pgdat->node_zones + j;
2480 unsigned long present_pages = zone->present_pages;
2482 zone->lowmem_reserve[j] = 0;
2484 for (idx = j-1; idx >= 0; idx--) {
2485 struct zone *lower_zone;
2487 if (sysctl_lowmem_reserve_ratio[idx] < 1)
2488 sysctl_lowmem_reserve_ratio[idx] = 1;
2490 lower_zone = pgdat->node_zones + idx;
2491 lower_zone->lowmem_reserve[j] = present_pages /
2492 sysctl_lowmem_reserve_ratio[idx];
2493 present_pages += lower_zone->present_pages;
2499 /*
2500 * setup_per_zone_pages_min - called when min_free_kbytes changes. Ensures
2501 * that the pages_{min,low,high} values for each zone are set correctly
2502 * with respect to min_free_kbytes.
2503 */
2504 void setup_per_zone_pages_min(void)
2506 unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
2507 unsigned long lowmem_pages = 0;
2508 struct zone *zone;
2509 unsigned long flags;
2511 /* Calculate total number of !ZONE_HIGHMEM pages */
2512 for_each_zone(zone) {
2513 if (!is_highmem(zone))
2514 lowmem_pages += zone->present_pages;
2517 for_each_zone(zone) {
2518 unsigned long tmp;
2519 spin_lock_irqsave(&zone->lru_lock, flags);
2520 tmp = (pages_min * zone->present_pages) / lowmem_pages;
2521 if (is_highmem(zone)) {
2522 /*
2523 * __GFP_HIGH and PF_MEMALLOC allocations usually don't
2524 * need highmem pages, so cap pages_min to a small
2525 * value here.
2527 * The (pages_high-pages_low) and (pages_low-pages_min)
2528 * deltas controls asynch page reclaim, and so should
2529 * not be capped for highmem.
2530 */
2531 int min_pages;
2533 min_pages = zone->present_pages / 1024;
2534 if (min_pages < SWAP_CLUSTER_MAX)
2535 min_pages = SWAP_CLUSTER_MAX;
2536 if (min_pages > 128)
2537 min_pages = 128;
2538 zone->pages_min = min_pages;
2539 } else {
2540 /*
2541 * If it's a lowmem zone, reserve a number of pages
2542 * proportionate to the zone's size.
2543 */
2544 zone->pages_min = tmp;
2547 zone->pages_low = zone->pages_min + tmp / 4;
2548 zone->pages_high = zone->pages_min + tmp / 2;
2549 spin_unlock_irqrestore(&zone->lru_lock, flags);
2553 /*
2554 * Initialise min_free_kbytes.
2556 * For small machines we want it small (128k min). For large machines
2557 * we want it large (64MB max). But it is not linear, because network
2558 * bandwidth does not increase linearly with machine size. We use
2560 * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
2561 * min_free_kbytes = sqrt(lowmem_kbytes * 16)
2563 * which yields
2565 * 16MB: 512k
2566 * 32MB: 724k
2567 * 64MB: 1024k
2568 * 128MB: 1448k
2569 * 256MB: 2048k
2570 * 512MB: 2896k
2571 * 1024MB: 4096k
2572 * 2048MB: 5792k
2573 * 4096MB: 8192k
2574 * 8192MB: 11584k
2575 * 16384MB: 16384k
2576 */
2577 static int __init init_per_zone_pages_min(void)
2579 unsigned long lowmem_kbytes;
2581 lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
2583 min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
2584 if (min_free_kbytes < 128)
2585 min_free_kbytes = 128;
2586 if (min_free_kbytes > 65536)
2587 min_free_kbytes = 65536;
2588 setup_per_zone_pages_min();
2589 setup_per_zone_lowmem_reserve();
2590 return 0;
2592 module_init(init_per_zone_pages_min)
2594 /*
2595 * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
2596 * that we can call two helper functions whenever min_free_kbytes
2597 * changes.
2598 */
2599 int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
2600 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
2602 proc_dointvec(table, write, file, buffer, length, ppos);
2603 setup_per_zone_pages_min();
2604 return 0;
2607 /*
2608 * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
2609 * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
2610 * whenever sysctl_lowmem_reserve_ratio changes.
2612 * The reserve ratio obviously has absolutely no relation with the
2613 * pages_min watermarks. The lowmem reserve ratio can only make sense
2614 * if in function of the boot time zone sizes.
2615 */
2616 int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
2617 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
2619 proc_dointvec_minmax(table, write, file, buffer, length, ppos);
2620 setup_per_zone_lowmem_reserve();
2621 return 0;
2624 /*
2625 * percpu_pagelist_fraction - changes the pcp->high for each zone on each
2626 * cpu. It is the fraction of total pages in each zone that a hot per cpu pagelist
2627 * can have before it gets flushed back to buddy allocator.
2628 */
2630 int percpu_pagelist_fraction_sysctl_handler(ctl_table *table, int write,
2631 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
2633 struct zone *zone;
2634 unsigned int cpu;
2635 int ret;
2637 ret = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
2638 if (!write || (ret == -EINVAL))
2639 return ret;
2640 for_each_zone(zone) {
2641 for_each_online_cpu(cpu) {
2642 unsigned long high;
2643 high = zone->present_pages / percpu_pagelist_fraction;
2644 setup_pagelist_highmark(zone_pcp(zone, cpu), high);
2647 return 0;
2650 __initdata int hashdist = HASHDIST_DEFAULT;
2652 #ifdef CONFIG_NUMA
2653 static int __init set_hashdist(char *str)
2655 if (!str)
2656 return 0;
2657 hashdist = simple_strtoul(str, &str, 0);
2658 return 1;
2660 __setup("hashdist=", set_hashdist);
2661 #endif
2663 /*
2664 * allocate a large system hash table from bootmem
2665 * - it is assumed that the hash table must contain an exact power-of-2
2666 * quantity of entries
2667 * - limit is the number of hash buckets, not the total allocation size
2668 */
2669 void *__init alloc_large_system_hash(const char *tablename,
2670 unsigned long bucketsize,
2671 unsigned long numentries,
2672 int scale,
2673 int flags,
2674 unsigned int *_hash_shift,
2675 unsigned int *_hash_mask,
2676 unsigned long limit)
2678 unsigned long long max = limit;
2679 unsigned long log2qty, size;
2680 void *table = NULL;
2682 /* allow the kernel cmdline to have a say */
2683 if (!numentries) {
2684 /* round applicable memory size up to nearest megabyte */
2685 numentries = (flags & HASH_HIGHMEM) ? nr_all_pages : nr_kernel_pages;
2686 numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
2687 numentries >>= 20 - PAGE_SHIFT;
2688 numentries <<= 20 - PAGE_SHIFT;
2690 /* limit to 1 bucket per 2^scale bytes of low memory */
2691 if (scale > PAGE_SHIFT)
2692 numentries >>= (scale - PAGE_SHIFT);
2693 else
2694 numentries <<= (PAGE_SHIFT - scale);
2696 /* rounded up to nearest power of 2 in size */
2697 numentries = 1UL << (long_log2(numentries) + 1);
2699 /* limit allocation size to 1/16 total memory by default */
2700 if (max == 0) {
2701 max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
2702 do_div(max, bucketsize);
2705 if (numentries > max)
2706 numentries = max;
2708 log2qty = long_log2(numentries);
2710 do {
2711 size = bucketsize << log2qty;
2712 if (flags & HASH_EARLY)
2713 table = alloc_bootmem(size);
2714 else if (hashdist)
2715 table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
2716 else {
2717 unsigned long order;
2718 for (order = 0; ((1UL << order) << PAGE_SHIFT) < size; order++)
2720 table = (void*) __get_free_pages(GFP_ATOMIC, order);
2722 } while (!table && size > PAGE_SIZE && --log2qty);
2724 if (!table)
2725 panic("Failed to allocate %s hash table\n", tablename);
2727 printk("%s hash table entries: %d (order: %d, %lu bytes)\n",
2728 tablename,
2729 (1U << log2qty),
2730 long_log2(size) - PAGE_SHIFT,
2731 size);
2733 if (_hash_shift)
2734 *_hash_shift = log2qty;
2735 if (_hash_mask)
2736 *_hash_mask = (1 << log2qty) - 1;
2738 return table;