[PATCH] ppc64: SMU partition recovery
[pohmelfs.git] / mm / page_alloc.c
blobff81b5c65511df6a1604f486ba6fae44553cb8e4
1 /*
2 * linux/mm/page_alloc.c
4 * Manages the free list, the system allocates free pages here.
5 * Note that kmalloc() lives in slab.c
7 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
8 * Swap reorganised 29.12.95, Stephen Tweedie
9 * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
10 * Reshaped it to be a zoned allocator, Ingo Molnar, Red Hat, 1999
11 * Discontiguous memory support, Kanoj Sarcar, SGI, Nov 1999
12 * Zone balancing, Kanoj Sarcar, SGI, Jan 2000
13 * Per cpu hot/cold page lists, bulk allocation, Martin J. Bligh, Sept 2002
14 * (lots of bits borrowed from Ingo Molnar & Andrew Morton)
17 #include <linux/config.h>
18 #include <linux/stddef.h>
19 #include <linux/mm.h>
20 #include <linux/swap.h>
21 #include <linux/interrupt.h>
22 #include <linux/pagemap.h>
23 #include <linux/bootmem.h>
24 #include <linux/compiler.h>
25 #include <linux/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>
40 #include <asm/tlbflush.h>
41 #include "internal.h"
44 * MCD - HACK: Find somewhere to initialize this EARLY, or make this
45 * initializer cleaner
47 nodemask_t node_online_map __read_mostly = { { [0] = 1UL } };
48 EXPORT_SYMBOL(node_online_map);
49 nodemask_t node_possible_map __read_mostly = NODE_MASK_ALL;
50 EXPORT_SYMBOL(node_possible_map);
51 struct pglist_data *pgdat_list __read_mostly;
52 unsigned long totalram_pages __read_mostly;
53 unsigned long totalhigh_pages __read_mostly;
54 long nr_swap_pages;
57 * results with 256, 32 in the lowmem_reserve sysctl:
58 * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
59 * 1G machine -> (16M dma, 784M normal, 224M high)
60 * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
61 * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
62 * HIGHMEM allocation will (224M+784M)/256 of ram reserved in ZONE_DMA
64 int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1] = { 256, 32 };
66 EXPORT_SYMBOL(totalram_pages);
69 * Used by page_zone() to look up the address of the struct zone whose
70 * id is encoded in the upper bits of page->flags
72 struct zone *zone_table[1 << ZONETABLE_SHIFT] __read_mostly;
73 EXPORT_SYMBOL(zone_table);
75 static char *zone_names[MAX_NR_ZONES] = { "DMA", "Normal", "HighMem" };
76 int min_free_kbytes = 1024;
78 unsigned long __initdata nr_kernel_pages;
79 unsigned long __initdata nr_all_pages;
81 static int page_outside_zone_boundaries(struct zone *zone, struct page *page)
83 int ret = 0;
84 unsigned seq;
85 unsigned long pfn = page_to_pfn(page);
87 do {
88 seq = zone_span_seqbegin(zone);
89 if (pfn >= zone->zone_start_pfn + zone->spanned_pages)
90 ret = 1;
91 else if (pfn < zone->zone_start_pfn)
92 ret = 1;
93 } while (zone_span_seqretry(zone, seq));
95 return ret;
98 static int page_is_consistent(struct zone *zone, struct page *page)
100 #ifdef CONFIG_HOLES_IN_ZONE
101 if (!pfn_valid(page_to_pfn(page)))
102 return 0;
103 #endif
104 if (zone != page_zone(page))
105 return 0;
107 return 1;
110 * Temporary debugging check for pages not lying within a given zone.
112 static int bad_range(struct zone *zone, struct page *page)
114 if (page_outside_zone_boundaries(zone, page))
115 return 1;
116 if (!page_is_consistent(zone, page))
117 return 1;
119 return 0;
122 static void bad_page(const char *function, struct page *page)
124 printk(KERN_EMERG "Bad page state at %s (in process '%s', page %p)\n",
125 function, current->comm, page);
126 printk(KERN_EMERG "flags:0x%0*lx mapping:%p mapcount:%d count:%d\n",
127 (int)(2*sizeof(page_flags_t)), (unsigned long)page->flags,
128 page->mapping, page_mapcount(page), page_count(page));
129 printk(KERN_EMERG "Backtrace:\n");
130 dump_stack();
131 printk(KERN_EMERG "Trying to fix it up, but a reboot is needed\n");
132 page->flags &= ~(1 << PG_lru |
133 1 << PG_private |
134 1 << PG_locked |
135 1 << PG_active |
136 1 << PG_dirty |
137 1 << PG_reclaim |
138 1 << PG_slab |
139 1 << PG_swapcache |
140 1 << PG_writeback |
141 1 << PG_reserved );
142 set_page_count(page, 0);
143 reset_page_mapcount(page);
144 page->mapping = NULL;
145 add_taint(TAINT_BAD_PAGE);
148 #ifndef CONFIG_HUGETLB_PAGE
149 #define prep_compound_page(page, order) do { } while (0)
150 #define destroy_compound_page(page, order) do { } while (0)
151 #else
153 * Higher-order pages are called "compound pages". They are structured thusly:
155 * The first PAGE_SIZE page is called the "head page".
157 * The remaining PAGE_SIZE pages are called "tail pages".
159 * All pages have PG_compound set. All pages have their ->private pointing at
160 * the head page (even the head page has this).
162 * The first tail page's ->mapping, if non-zero, holds the address of the
163 * compound page's put_page() function.
165 * The order of the allocation is stored in the first tail page's ->index
166 * This is only for debug at present. This usage means that zero-order pages
167 * may not be compound.
169 static void prep_compound_page(struct page *page, unsigned long order)
171 int i;
172 int nr_pages = 1 << order;
174 page[1].mapping = NULL;
175 page[1].index = order;
176 for (i = 0; i < nr_pages; i++) {
177 struct page *p = page + i;
179 SetPageCompound(p);
180 set_page_private(p, (unsigned long)page);
184 static void destroy_compound_page(struct page *page, unsigned long order)
186 int i;
187 int nr_pages = 1 << order;
189 if (!PageCompound(page))
190 return;
192 if (page[1].index != order)
193 bad_page(__FUNCTION__, page);
195 for (i = 0; i < nr_pages; i++) {
196 struct page *p = page + i;
198 if (!PageCompound(p))
199 bad_page(__FUNCTION__, page);
200 if (page_private(p) != (unsigned long)page)
201 bad_page(__FUNCTION__, page);
202 ClearPageCompound(p);
205 #endif /* CONFIG_HUGETLB_PAGE */
208 * function for dealing with page's order in buddy system.
209 * zone->lock is already acquired when we use these.
210 * So, we don't need atomic page->flags operations here.
212 static inline unsigned long page_order(struct page *page) {
213 return page_private(page);
216 static inline void set_page_order(struct page *page, int order) {
217 set_page_private(page, order);
218 __SetPagePrivate(page);
221 static inline void rmv_page_order(struct page *page)
223 __ClearPagePrivate(page);
224 set_page_private(page, 0);
228 * Locate the struct page for both the matching buddy in our
229 * pair (buddy1) and the combined O(n+1) page they form (page).
231 * 1) Any buddy B1 will have an order O twin B2 which satisfies
232 * the following equation:
233 * B2 = B1 ^ (1 << O)
234 * For example, if the starting buddy (buddy2) is #8 its order
235 * 1 buddy is #10:
236 * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
238 * 2) Any buddy B will have an order O+1 parent P which
239 * satisfies the following equation:
240 * P = B & ~(1 << O)
242 * Assumption: *_mem_map is contigious at least up to MAX_ORDER
244 static inline struct page *
245 __page_find_buddy(struct page *page, unsigned long page_idx, unsigned int order)
247 unsigned long buddy_idx = page_idx ^ (1 << order);
249 return page + (buddy_idx - page_idx);
252 static inline unsigned long
253 __find_combined_index(unsigned long page_idx, unsigned int order)
255 return (page_idx & ~(1 << order));
259 * This function checks whether a page is free && is the buddy
260 * we can do coalesce a page and its buddy if
261 * (a) the buddy is free &&
262 * (b) the buddy is on the buddy system &&
263 * (c) a page and its buddy have the same order.
264 * for recording page's order, we use page_private(page) and PG_private.
267 static inline int page_is_buddy(struct page *page, int order)
269 if (PagePrivate(page) &&
270 (page_order(page) == order) &&
271 page_count(page) == 0)
272 return 1;
273 return 0;
277 * Freeing function for a buddy system allocator.
279 * The concept of a buddy system is to maintain direct-mapped table
280 * (containing bit values) for memory blocks of various "orders".
281 * The bottom level table contains the map for the smallest allocatable
282 * units of memory (here, pages), and each level above it describes
283 * pairs of units from the levels below, hence, "buddies".
284 * At a high level, all that happens here is marking the table entry
285 * at the bottom level available, and propagating the changes upward
286 * as necessary, plus some accounting needed to play nicely with other
287 * parts of the VM system.
288 * At each level, we keep a list of pages, which are heads of continuous
289 * free pages of length of (1 << order) and marked with PG_Private.Page's
290 * order is recorded in page_private(page) field.
291 * So when we are allocating or freeing one, we can derive the state of the
292 * other. That is, if we allocate a small block, and both were
293 * free, the remainder of the region must be split into blocks.
294 * If a block is freed, and its buddy is also free, then this
295 * triggers coalescing into a block of larger size.
297 * -- wli
300 static inline void __free_pages_bulk (struct page *page,
301 struct zone *zone, unsigned int order)
303 unsigned long page_idx;
304 int order_size = 1 << order;
306 if (unlikely(order))
307 destroy_compound_page(page, order);
309 page_idx = page_to_pfn(page) & ((1 << MAX_ORDER) - 1);
311 BUG_ON(page_idx & (order_size - 1));
312 BUG_ON(bad_range(zone, page));
314 zone->free_pages += order_size;
315 while (order < MAX_ORDER-1) {
316 unsigned long combined_idx;
317 struct free_area *area;
318 struct page *buddy;
320 combined_idx = __find_combined_index(page_idx, order);
321 buddy = __page_find_buddy(page, page_idx, order);
323 if (bad_range(zone, buddy))
324 break;
325 if (!page_is_buddy(buddy, order))
326 break; /* Move the buddy up one level. */
327 list_del(&buddy->lru);
328 area = zone->free_area + order;
329 area->nr_free--;
330 rmv_page_order(buddy);
331 page = page + (combined_idx - page_idx);
332 page_idx = combined_idx;
333 order++;
335 set_page_order(page, order);
336 list_add(&page->lru, &zone->free_area[order].free_list);
337 zone->free_area[order].nr_free++;
340 static inline void free_pages_check(const char *function, struct page *page)
342 if ( page_mapcount(page) ||
343 page->mapping != NULL ||
344 page_count(page) != 0 ||
345 (page->flags & (
346 1 << PG_lru |
347 1 << PG_private |
348 1 << PG_locked |
349 1 << PG_active |
350 1 << PG_reclaim |
351 1 << PG_slab |
352 1 << PG_swapcache |
353 1 << PG_writeback |
354 1 << PG_reserved )))
355 bad_page(function, page);
356 if (PageDirty(page))
357 __ClearPageDirty(page);
361 * Frees a list of pages.
362 * Assumes all pages on list are in same zone, and of same order.
363 * count is the number of pages to free.
365 * If the zone was previously in an "all pages pinned" state then look to
366 * see if this freeing clears that state.
368 * And clear the zone's pages_scanned counter, to hold off the "all pages are
369 * pinned" detection logic.
371 static int
372 free_pages_bulk(struct zone *zone, int count,
373 struct list_head *list, unsigned int order)
375 unsigned long flags;
376 struct page *page = NULL;
377 int ret = 0;
379 spin_lock_irqsave(&zone->lock, flags);
380 zone->all_unreclaimable = 0;
381 zone->pages_scanned = 0;
382 while (!list_empty(list) && count--) {
383 page = list_entry(list->prev, struct page, lru);
384 /* have to delete it as __free_pages_bulk list manipulates */
385 list_del(&page->lru);
386 __free_pages_bulk(page, zone, order);
387 ret++;
389 spin_unlock_irqrestore(&zone->lock, flags);
390 return ret;
393 void __free_pages_ok(struct page *page, unsigned int order)
395 LIST_HEAD(list);
396 int i;
398 arch_free_page(page, order);
400 mod_page_state(pgfree, 1 << order);
402 #ifndef CONFIG_MMU
403 if (order > 0)
404 for (i = 1 ; i < (1 << order) ; ++i)
405 __put_page(page + i);
406 #endif
408 for (i = 0 ; i < (1 << order) ; ++i)
409 free_pages_check(__FUNCTION__, page + i);
410 list_add(&page->lru, &list);
411 kernel_map_pages(page, 1<<order, 0);
412 free_pages_bulk(page_zone(page), 1, &list, order);
417 * The order of subdivision here is critical for the IO subsystem.
418 * Please do not alter this order without good reasons and regression
419 * testing. Specifically, as large blocks of memory are subdivided,
420 * the order in which smaller blocks are delivered depends on the order
421 * they're subdivided in this function. This is the primary factor
422 * influencing the order in which pages are delivered to the IO
423 * subsystem according to empirical testing, and this is also justified
424 * by considering the behavior of a buddy system containing a single
425 * large block of memory acted on by a series of small allocations.
426 * This behavior is a critical factor in sglist merging's success.
428 * -- wli
430 static inline struct page *
431 expand(struct zone *zone, struct page *page,
432 int low, int high, struct free_area *area)
434 unsigned long size = 1 << high;
436 while (high > low) {
437 area--;
438 high--;
439 size >>= 1;
440 BUG_ON(bad_range(zone, &page[size]));
441 list_add(&page[size].lru, &area->free_list);
442 area->nr_free++;
443 set_page_order(&page[size], high);
445 return page;
448 void set_page_refs(struct page *page, int order)
450 #ifdef CONFIG_MMU
451 set_page_count(page, 1);
452 #else
453 int i;
456 * We need to reference all the pages for this order, otherwise if
457 * anyone accesses one of the pages with (get/put) it will be freed.
458 * - eg: access_process_vm()
460 for (i = 0; i < (1 << order); i++)
461 set_page_count(page + i, 1);
462 #endif /* CONFIG_MMU */
466 * This page is about to be returned from the page allocator
468 static void prep_new_page(struct page *page, int order)
470 if ( page_mapcount(page) ||
471 page->mapping != NULL ||
472 page_count(page) != 0 ||
473 (page->flags & (
474 1 << PG_lru |
475 1 << PG_private |
476 1 << PG_locked |
477 1 << PG_active |
478 1 << PG_dirty |
479 1 << PG_reclaim |
480 1 << PG_slab |
481 1 << PG_swapcache |
482 1 << PG_writeback |
483 1 << PG_reserved )))
484 bad_page(__FUNCTION__, page);
486 page->flags &= ~(1 << PG_uptodate | 1 << PG_error |
487 1 << PG_referenced | 1 << PG_arch_1 |
488 1 << PG_checked | 1 << PG_mappedtodisk);
489 set_page_private(page, 0);
490 set_page_refs(page, order);
491 kernel_map_pages(page, 1 << order, 1);
495 * Do the hard work of removing an element from the buddy allocator.
496 * Call me with the zone->lock already held.
498 static struct page *__rmqueue(struct zone *zone, unsigned int order)
500 struct free_area * area;
501 unsigned int current_order;
502 struct page *page;
504 for (current_order = order; current_order < MAX_ORDER; ++current_order) {
505 area = zone->free_area + current_order;
506 if (list_empty(&area->free_list))
507 continue;
509 page = list_entry(area->free_list.next, struct page, lru);
510 list_del(&page->lru);
511 rmv_page_order(page);
512 area->nr_free--;
513 zone->free_pages -= 1UL << order;
514 return expand(zone, page, order, current_order, area);
517 return NULL;
521 * Obtain a specified number of elements from the buddy allocator, all under
522 * a single hold of the lock, for efficiency. Add them to the supplied list.
523 * Returns the number of new pages which were placed at *list.
525 static int rmqueue_bulk(struct zone *zone, unsigned int order,
526 unsigned long count, struct list_head *list)
528 unsigned long flags;
529 int i;
530 int allocated = 0;
531 struct page *page;
533 spin_lock_irqsave(&zone->lock, flags);
534 for (i = 0; i < count; ++i) {
535 page = __rmqueue(zone, order);
536 if (page == NULL)
537 break;
538 allocated++;
539 list_add_tail(&page->lru, list);
541 spin_unlock_irqrestore(&zone->lock, flags);
542 return allocated;
545 #ifdef CONFIG_NUMA
546 /* Called from the slab reaper to drain remote pagesets */
547 void drain_remote_pages(void)
549 struct zone *zone;
550 int i;
551 unsigned long flags;
553 local_irq_save(flags);
554 for_each_zone(zone) {
555 struct per_cpu_pageset *pset;
557 /* Do not drain local pagesets */
558 if (zone->zone_pgdat->node_id == numa_node_id())
559 continue;
561 pset = zone->pageset[smp_processor_id()];
562 for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
563 struct per_cpu_pages *pcp;
565 pcp = &pset->pcp[i];
566 if (pcp->count)
567 pcp->count -= free_pages_bulk(zone, pcp->count,
568 &pcp->list, 0);
571 local_irq_restore(flags);
573 #endif
575 #if defined(CONFIG_PM) || defined(CONFIG_HOTPLUG_CPU)
576 static void __drain_pages(unsigned int cpu)
578 struct zone *zone;
579 int i;
581 for_each_zone(zone) {
582 struct per_cpu_pageset *pset;
584 pset = zone_pcp(zone, cpu);
585 for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
586 struct per_cpu_pages *pcp;
588 pcp = &pset->pcp[i];
589 pcp->count -= free_pages_bulk(zone, pcp->count,
590 &pcp->list, 0);
594 #endif /* CONFIG_PM || CONFIG_HOTPLUG_CPU */
596 #ifdef CONFIG_PM
598 void mark_free_pages(struct zone *zone)
600 unsigned long zone_pfn, flags;
601 int order;
602 struct list_head *curr;
604 if (!zone->spanned_pages)
605 return;
607 spin_lock_irqsave(&zone->lock, flags);
608 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
609 ClearPageNosaveFree(pfn_to_page(zone_pfn + zone->zone_start_pfn));
611 for (order = MAX_ORDER - 1; order >= 0; --order)
612 list_for_each(curr, &zone->free_area[order].free_list) {
613 unsigned long start_pfn, i;
615 start_pfn = page_to_pfn(list_entry(curr, struct page, lru));
617 for (i=0; i < (1<<order); i++)
618 SetPageNosaveFree(pfn_to_page(start_pfn+i));
620 spin_unlock_irqrestore(&zone->lock, flags);
624 * Spill all of this CPU's per-cpu pages back into the buddy allocator.
626 void drain_local_pages(void)
628 unsigned long flags;
630 local_irq_save(flags);
631 __drain_pages(smp_processor_id());
632 local_irq_restore(flags);
634 #endif /* CONFIG_PM */
636 static void zone_statistics(struct zonelist *zonelist, struct zone *z)
638 #ifdef CONFIG_NUMA
639 unsigned long flags;
640 int cpu;
641 pg_data_t *pg = z->zone_pgdat;
642 pg_data_t *orig = zonelist->zones[0]->zone_pgdat;
643 struct per_cpu_pageset *p;
645 local_irq_save(flags);
646 cpu = smp_processor_id();
647 p = zone_pcp(z,cpu);
648 if (pg == orig) {
649 p->numa_hit++;
650 } else {
651 p->numa_miss++;
652 zone_pcp(zonelist->zones[0], cpu)->numa_foreign++;
654 if (pg == NODE_DATA(numa_node_id()))
655 p->local_node++;
656 else
657 p->other_node++;
658 local_irq_restore(flags);
659 #endif
663 * Free a 0-order page
665 static void FASTCALL(free_hot_cold_page(struct page *page, int cold));
666 static void fastcall free_hot_cold_page(struct page *page, int cold)
668 struct zone *zone = page_zone(page);
669 struct per_cpu_pages *pcp;
670 unsigned long flags;
672 arch_free_page(page, 0);
674 kernel_map_pages(page, 1, 0);
675 inc_page_state(pgfree);
676 if (PageAnon(page))
677 page->mapping = NULL;
678 free_pages_check(__FUNCTION__, page);
679 pcp = &zone_pcp(zone, get_cpu())->pcp[cold];
680 local_irq_save(flags);
681 list_add(&page->lru, &pcp->list);
682 pcp->count++;
683 if (pcp->count >= pcp->high)
684 pcp->count -= free_pages_bulk(zone, pcp->batch, &pcp->list, 0);
685 local_irq_restore(flags);
686 put_cpu();
689 void fastcall free_hot_page(struct page *page)
691 free_hot_cold_page(page, 0);
694 void fastcall free_cold_page(struct page *page)
696 free_hot_cold_page(page, 1);
699 static inline void prep_zero_page(struct page *page, int order, gfp_t gfp_flags)
701 int i;
703 BUG_ON((gfp_flags & (__GFP_WAIT | __GFP_HIGHMEM)) == __GFP_HIGHMEM);
704 for(i = 0; i < (1 << order); i++)
705 clear_highpage(page + i);
709 * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
710 * we cheat by calling it from here, in the order > 0 path. Saves a branch
711 * or two.
713 static struct page *
714 buffered_rmqueue(struct zone *zone, int order, gfp_t gfp_flags)
716 unsigned long flags;
717 struct page *page = NULL;
718 int cold = !!(gfp_flags & __GFP_COLD);
720 if (order == 0) {
721 struct per_cpu_pages *pcp;
723 pcp = &zone_pcp(zone, get_cpu())->pcp[cold];
724 local_irq_save(flags);
725 if (pcp->count <= pcp->low)
726 pcp->count += rmqueue_bulk(zone, 0,
727 pcp->batch, &pcp->list);
728 if (pcp->count) {
729 page = list_entry(pcp->list.next, struct page, lru);
730 list_del(&page->lru);
731 pcp->count--;
733 local_irq_restore(flags);
734 put_cpu();
737 if (page == NULL) {
738 spin_lock_irqsave(&zone->lock, flags);
739 page = __rmqueue(zone, order);
740 spin_unlock_irqrestore(&zone->lock, flags);
743 if (page != NULL) {
744 BUG_ON(bad_range(zone, page));
745 mod_page_state_zone(zone, pgalloc, 1 << order);
746 prep_new_page(page, order);
748 if (gfp_flags & __GFP_ZERO)
749 prep_zero_page(page, order, gfp_flags);
751 if (order && (gfp_flags & __GFP_COMP))
752 prep_compound_page(page, order);
754 return page;
758 * Return 1 if free pages are above 'mark'. This takes into account the order
759 * of the allocation.
761 int zone_watermark_ok(struct zone *z, int order, unsigned long mark,
762 int classzone_idx, int can_try_harder, gfp_t gfp_high)
764 /* free_pages my go negative - that's OK */
765 long min = mark, free_pages = z->free_pages - (1 << order) + 1;
766 int o;
768 if (gfp_high)
769 min -= min / 2;
770 if (can_try_harder)
771 min -= min / 4;
773 if (free_pages <= min + z->lowmem_reserve[classzone_idx])
774 return 0;
775 for (o = 0; o < order; o++) {
776 /* At the next order, this order's pages become unavailable */
777 free_pages -= z->free_area[o].nr_free << o;
779 /* Require fewer higher order pages to be free */
780 min >>= 1;
782 if (free_pages <= min)
783 return 0;
785 return 1;
788 static inline int
789 should_reclaim_zone(struct zone *z, gfp_t gfp_mask)
791 if (!z->reclaim_pages)
792 return 0;
793 if (gfp_mask & __GFP_NORECLAIM)
794 return 0;
795 return 1;
799 * This is the 'heart' of the zoned buddy allocator.
801 struct page * fastcall
802 __alloc_pages(gfp_t gfp_mask, unsigned int order,
803 struct zonelist *zonelist)
805 const gfp_t wait = gfp_mask & __GFP_WAIT;
806 struct zone **zones, *z;
807 struct page *page;
808 struct reclaim_state reclaim_state;
809 struct task_struct *p = current;
810 int i;
811 int classzone_idx;
812 int do_retry;
813 int can_try_harder;
814 int did_some_progress;
816 might_sleep_if(wait);
819 * The caller may dip into page reserves a bit more if the caller
820 * cannot run direct reclaim, or is the caller has realtime scheduling
821 * policy
823 can_try_harder = (unlikely(rt_task(p)) && !in_interrupt()) || !wait;
825 zones = zonelist->zones; /* the list of zones suitable for gfp_mask */
827 if (unlikely(zones[0] == NULL)) {
828 /* Should this ever happen?? */
829 return NULL;
832 classzone_idx = zone_idx(zones[0]);
834 restart:
836 * Go through the zonelist once, looking for a zone with enough free.
837 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
839 for (i = 0; (z = zones[i]) != NULL; i++) {
840 int do_reclaim = should_reclaim_zone(z, gfp_mask);
842 if (!cpuset_zone_allowed(z, __GFP_HARDWALL))
843 continue;
846 * If the zone is to attempt early page reclaim then this loop
847 * will try to reclaim pages and check the watermark a second
848 * time before giving up and falling back to the next zone.
850 zone_reclaim_retry:
851 if (!zone_watermark_ok(z, order, z->pages_low,
852 classzone_idx, 0, 0)) {
853 if (!do_reclaim)
854 continue;
855 else {
856 zone_reclaim(z, gfp_mask, order);
857 /* Only try reclaim once */
858 do_reclaim = 0;
859 goto zone_reclaim_retry;
863 page = buffered_rmqueue(z, order, gfp_mask);
864 if (page)
865 goto got_pg;
868 for (i = 0; (z = zones[i]) != NULL; i++)
869 wakeup_kswapd(z, order);
872 * Go through the zonelist again. Let __GFP_HIGH and allocations
873 * coming from realtime tasks to go deeper into reserves
875 * This is the last chance, in general, before the goto nopage.
876 * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
877 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
879 for (i = 0; (z = zones[i]) != NULL; i++) {
880 if (!zone_watermark_ok(z, order, z->pages_min,
881 classzone_idx, can_try_harder,
882 gfp_mask & __GFP_HIGH))
883 continue;
885 if (wait && !cpuset_zone_allowed(z, gfp_mask))
886 continue;
888 page = buffered_rmqueue(z, order, gfp_mask);
889 if (page)
890 goto got_pg;
893 /* This allocation should allow future memory freeing. */
895 if (((p->flags & PF_MEMALLOC) || unlikely(test_thread_flag(TIF_MEMDIE)))
896 && !in_interrupt()) {
897 if (!(gfp_mask & __GFP_NOMEMALLOC)) {
898 /* go through the zonelist yet again, ignoring mins */
899 for (i = 0; (z = zones[i]) != NULL; i++) {
900 if (!cpuset_zone_allowed(z, gfp_mask))
901 continue;
902 page = buffered_rmqueue(z, order, gfp_mask);
903 if (page)
904 goto got_pg;
907 goto nopage;
910 /* Atomic allocations - we can't balance anything */
911 if (!wait)
912 goto nopage;
914 rebalance:
915 cond_resched();
917 /* We now go into synchronous reclaim */
918 p->flags |= PF_MEMALLOC;
919 reclaim_state.reclaimed_slab = 0;
920 p->reclaim_state = &reclaim_state;
922 did_some_progress = try_to_free_pages(zones, gfp_mask);
924 p->reclaim_state = NULL;
925 p->flags &= ~PF_MEMALLOC;
927 cond_resched();
929 if (likely(did_some_progress)) {
930 for (i = 0; (z = zones[i]) != NULL; i++) {
931 if (!zone_watermark_ok(z, order, z->pages_min,
932 classzone_idx, can_try_harder,
933 gfp_mask & __GFP_HIGH))
934 continue;
936 if (!cpuset_zone_allowed(z, gfp_mask))
937 continue;
939 page = buffered_rmqueue(z, order, gfp_mask);
940 if (page)
941 goto got_pg;
943 } else if ((gfp_mask & __GFP_FS) && !(gfp_mask & __GFP_NORETRY)) {
945 * Go through the zonelist yet one more time, keep
946 * very high watermark here, this is only to catch
947 * a parallel oom killing, we must fail if we're still
948 * under heavy pressure.
950 for (i = 0; (z = zones[i]) != NULL; i++) {
951 if (!zone_watermark_ok(z, order, z->pages_high,
952 classzone_idx, 0, 0))
953 continue;
955 if (!cpuset_zone_allowed(z, __GFP_HARDWALL))
956 continue;
958 page = buffered_rmqueue(z, order, gfp_mask);
959 if (page)
960 goto got_pg;
963 out_of_memory(gfp_mask, order);
964 goto restart;
968 * Don't let big-order allocations loop unless the caller explicitly
969 * requests that. Wait for some write requests to complete then retry.
971 * In this implementation, __GFP_REPEAT means __GFP_NOFAIL for order
972 * <= 3, but that may not be true in other implementations.
974 do_retry = 0;
975 if (!(gfp_mask & __GFP_NORETRY)) {
976 if ((order <= 3) || (gfp_mask & __GFP_REPEAT))
977 do_retry = 1;
978 if (gfp_mask & __GFP_NOFAIL)
979 do_retry = 1;
981 if (do_retry) {
982 blk_congestion_wait(WRITE, HZ/50);
983 goto rebalance;
986 nopage:
987 if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) {
988 printk(KERN_WARNING "%s: page allocation failure."
989 " order:%d, mode:0x%x\n",
990 p->comm, order, gfp_mask);
991 dump_stack();
992 show_mem();
994 return NULL;
995 got_pg:
996 zone_statistics(zonelist, z);
997 return page;
1000 EXPORT_SYMBOL(__alloc_pages);
1003 * Common helper functions.
1005 fastcall unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
1007 struct page * page;
1008 page = alloc_pages(gfp_mask, order);
1009 if (!page)
1010 return 0;
1011 return (unsigned long) page_address(page);
1014 EXPORT_SYMBOL(__get_free_pages);
1016 fastcall unsigned long get_zeroed_page(gfp_t gfp_mask)
1018 struct page * page;
1021 * get_zeroed_page() returns a 32-bit address, which cannot represent
1022 * a highmem page
1024 BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
1026 page = alloc_pages(gfp_mask | __GFP_ZERO, 0);
1027 if (page)
1028 return (unsigned long) page_address(page);
1029 return 0;
1032 EXPORT_SYMBOL(get_zeroed_page);
1034 void __pagevec_free(struct pagevec *pvec)
1036 int i = pagevec_count(pvec);
1038 while (--i >= 0)
1039 free_hot_cold_page(pvec->pages[i], pvec->cold);
1042 fastcall void __free_pages(struct page *page, unsigned int order)
1044 if (put_page_testzero(page)) {
1045 if (order == 0)
1046 free_hot_page(page);
1047 else
1048 __free_pages_ok(page, order);
1052 EXPORT_SYMBOL(__free_pages);
1054 fastcall void free_pages(unsigned long addr, unsigned int order)
1056 if (addr != 0) {
1057 BUG_ON(!virt_addr_valid((void *)addr));
1058 __free_pages(virt_to_page((void *)addr), order);
1062 EXPORT_SYMBOL(free_pages);
1065 * Total amount of free (allocatable) RAM:
1067 unsigned int nr_free_pages(void)
1069 unsigned int sum = 0;
1070 struct zone *zone;
1072 for_each_zone(zone)
1073 sum += zone->free_pages;
1075 return sum;
1078 EXPORT_SYMBOL(nr_free_pages);
1080 #ifdef CONFIG_NUMA
1081 unsigned int nr_free_pages_pgdat(pg_data_t *pgdat)
1083 unsigned int i, sum = 0;
1085 for (i = 0; i < MAX_NR_ZONES; i++)
1086 sum += pgdat->node_zones[i].free_pages;
1088 return sum;
1090 #endif
1092 static unsigned int nr_free_zone_pages(int offset)
1094 /* Just pick one node, since fallback list is circular */
1095 pg_data_t *pgdat = NODE_DATA(numa_node_id());
1096 unsigned int sum = 0;
1098 struct zonelist *zonelist = pgdat->node_zonelists + offset;
1099 struct zone **zonep = zonelist->zones;
1100 struct zone *zone;
1102 for (zone = *zonep++; zone; zone = *zonep++) {
1103 unsigned long size = zone->present_pages;
1104 unsigned long high = zone->pages_high;
1105 if (size > high)
1106 sum += size - high;
1109 return sum;
1113 * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
1115 unsigned int nr_free_buffer_pages(void)
1117 return nr_free_zone_pages(gfp_zone(GFP_USER));
1121 * Amount of free RAM allocatable within all zones
1123 unsigned int nr_free_pagecache_pages(void)
1125 return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER));
1128 #ifdef CONFIG_HIGHMEM
1129 unsigned int nr_free_highpages (void)
1131 pg_data_t *pgdat;
1132 unsigned int pages = 0;
1134 for_each_pgdat(pgdat)
1135 pages += pgdat->node_zones[ZONE_HIGHMEM].free_pages;
1137 return pages;
1139 #endif
1141 #ifdef CONFIG_NUMA
1142 static void show_node(struct zone *zone)
1144 printk("Node %d ", zone->zone_pgdat->node_id);
1146 #else
1147 #define show_node(zone) do { } while (0)
1148 #endif
1151 * Accumulate the page_state information across all CPUs.
1152 * The result is unavoidably approximate - it can change
1153 * during and after execution of this function.
1155 static DEFINE_PER_CPU(struct page_state, page_states) = {0};
1157 atomic_t nr_pagecache = ATOMIC_INIT(0);
1158 EXPORT_SYMBOL(nr_pagecache);
1159 #ifdef CONFIG_SMP
1160 DEFINE_PER_CPU(long, nr_pagecache_local) = 0;
1161 #endif
1163 void __get_page_state(struct page_state *ret, int nr, cpumask_t *cpumask)
1165 int cpu = 0;
1167 memset(ret, 0, sizeof(*ret));
1168 cpus_and(*cpumask, *cpumask, cpu_online_map);
1170 cpu = first_cpu(*cpumask);
1171 while (cpu < NR_CPUS) {
1172 unsigned long *in, *out, off;
1174 in = (unsigned long *)&per_cpu(page_states, cpu);
1176 cpu = next_cpu(cpu, *cpumask);
1178 if (cpu < NR_CPUS)
1179 prefetch(&per_cpu(page_states, cpu));
1181 out = (unsigned long *)ret;
1182 for (off = 0; off < nr; off++)
1183 *out++ += *in++;
1187 void get_page_state_node(struct page_state *ret, int node)
1189 int nr;
1190 cpumask_t mask = node_to_cpumask(node);
1192 nr = offsetof(struct page_state, GET_PAGE_STATE_LAST);
1193 nr /= sizeof(unsigned long);
1195 __get_page_state(ret, nr+1, &mask);
1198 void get_page_state(struct page_state *ret)
1200 int nr;
1201 cpumask_t mask = CPU_MASK_ALL;
1203 nr = offsetof(struct page_state, GET_PAGE_STATE_LAST);
1204 nr /= sizeof(unsigned long);
1206 __get_page_state(ret, nr + 1, &mask);
1209 void get_full_page_state(struct page_state *ret)
1211 cpumask_t mask = CPU_MASK_ALL;
1213 __get_page_state(ret, sizeof(*ret) / sizeof(unsigned long), &mask);
1216 unsigned long __read_page_state(unsigned long offset)
1218 unsigned long ret = 0;
1219 int cpu;
1221 for_each_online_cpu(cpu) {
1222 unsigned long in;
1224 in = (unsigned long)&per_cpu(page_states, cpu) + offset;
1225 ret += *((unsigned long *)in);
1227 return ret;
1230 void __mod_page_state(unsigned long offset, unsigned long delta)
1232 unsigned long flags;
1233 void* ptr;
1235 local_irq_save(flags);
1236 ptr = &__get_cpu_var(page_states);
1237 *(unsigned long*)(ptr + offset) += delta;
1238 local_irq_restore(flags);
1241 EXPORT_SYMBOL(__mod_page_state);
1243 void __get_zone_counts(unsigned long *active, unsigned long *inactive,
1244 unsigned long *free, struct pglist_data *pgdat)
1246 struct zone *zones = pgdat->node_zones;
1247 int i;
1249 *active = 0;
1250 *inactive = 0;
1251 *free = 0;
1252 for (i = 0; i < MAX_NR_ZONES; i++) {
1253 *active += zones[i].nr_active;
1254 *inactive += zones[i].nr_inactive;
1255 *free += zones[i].free_pages;
1259 void get_zone_counts(unsigned long *active,
1260 unsigned long *inactive, unsigned long *free)
1262 struct pglist_data *pgdat;
1264 *active = 0;
1265 *inactive = 0;
1266 *free = 0;
1267 for_each_pgdat(pgdat) {
1268 unsigned long l, m, n;
1269 __get_zone_counts(&l, &m, &n, pgdat);
1270 *active += l;
1271 *inactive += m;
1272 *free += n;
1276 void si_meminfo(struct sysinfo *val)
1278 val->totalram = totalram_pages;
1279 val->sharedram = 0;
1280 val->freeram = nr_free_pages();
1281 val->bufferram = nr_blockdev_pages();
1282 #ifdef CONFIG_HIGHMEM
1283 val->totalhigh = totalhigh_pages;
1284 val->freehigh = nr_free_highpages();
1285 #else
1286 val->totalhigh = 0;
1287 val->freehigh = 0;
1288 #endif
1289 val->mem_unit = PAGE_SIZE;
1292 EXPORT_SYMBOL(si_meminfo);
1294 #ifdef CONFIG_NUMA
1295 void si_meminfo_node(struct sysinfo *val, int nid)
1297 pg_data_t *pgdat = NODE_DATA(nid);
1299 val->totalram = pgdat->node_present_pages;
1300 val->freeram = nr_free_pages_pgdat(pgdat);
1301 val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].present_pages;
1302 val->freehigh = pgdat->node_zones[ZONE_HIGHMEM].free_pages;
1303 val->mem_unit = PAGE_SIZE;
1305 #endif
1307 #define K(x) ((x) << (PAGE_SHIFT-10))
1310 * Show free area list (used inside shift_scroll-lock stuff)
1311 * We also calculate the percentage fragmentation. We do this by counting the
1312 * memory on each free list with the exception of the first item on the list.
1314 void show_free_areas(void)
1316 struct page_state ps;
1317 int cpu, temperature;
1318 unsigned long active;
1319 unsigned long inactive;
1320 unsigned long free;
1321 struct zone *zone;
1323 for_each_zone(zone) {
1324 show_node(zone);
1325 printk("%s per-cpu:", zone->name);
1327 if (!zone->present_pages) {
1328 printk(" empty\n");
1329 continue;
1330 } else
1331 printk("\n");
1333 for_each_cpu(cpu) {
1334 struct per_cpu_pageset *pageset;
1336 pageset = zone_pcp(zone, cpu);
1338 for (temperature = 0; temperature < 2; temperature++)
1339 printk("cpu %d %s: low %d, high %d, batch %d used:%d\n",
1340 cpu,
1341 temperature ? "cold" : "hot",
1342 pageset->pcp[temperature].low,
1343 pageset->pcp[temperature].high,
1344 pageset->pcp[temperature].batch,
1345 pageset->pcp[temperature].count);
1349 get_page_state(&ps);
1350 get_zone_counts(&active, &inactive, &free);
1352 printk("Free pages: %11ukB (%ukB HighMem)\n",
1353 K(nr_free_pages()),
1354 K(nr_free_highpages()));
1356 printk("Active:%lu inactive:%lu dirty:%lu writeback:%lu "
1357 "unstable:%lu free:%u slab:%lu mapped:%lu pagetables:%lu\n",
1358 active,
1359 inactive,
1360 ps.nr_dirty,
1361 ps.nr_writeback,
1362 ps.nr_unstable,
1363 nr_free_pages(),
1364 ps.nr_slab,
1365 ps.nr_mapped,
1366 ps.nr_page_table_pages);
1368 for_each_zone(zone) {
1369 int i;
1371 show_node(zone);
1372 printk("%s"
1373 " free:%lukB"
1374 " min:%lukB"
1375 " low:%lukB"
1376 " high:%lukB"
1377 " active:%lukB"
1378 " inactive:%lukB"
1379 " present:%lukB"
1380 " pages_scanned:%lu"
1381 " all_unreclaimable? %s"
1382 "\n",
1383 zone->name,
1384 K(zone->free_pages),
1385 K(zone->pages_min),
1386 K(zone->pages_low),
1387 K(zone->pages_high),
1388 K(zone->nr_active),
1389 K(zone->nr_inactive),
1390 K(zone->present_pages),
1391 zone->pages_scanned,
1392 (zone->all_unreclaimable ? "yes" : "no")
1394 printk("lowmem_reserve[]:");
1395 for (i = 0; i < MAX_NR_ZONES; i++)
1396 printk(" %lu", zone->lowmem_reserve[i]);
1397 printk("\n");
1400 for_each_zone(zone) {
1401 unsigned long nr, flags, order, total = 0;
1403 show_node(zone);
1404 printk("%s: ", zone->name);
1405 if (!zone->present_pages) {
1406 printk("empty\n");
1407 continue;
1410 spin_lock_irqsave(&zone->lock, flags);
1411 for (order = 0; order < MAX_ORDER; order++) {
1412 nr = zone->free_area[order].nr_free;
1413 total += nr << order;
1414 printk("%lu*%lukB ", nr, K(1UL) << order);
1416 spin_unlock_irqrestore(&zone->lock, flags);
1417 printk("= %lukB\n", K(total));
1420 show_swap_cache_info();
1424 * Builds allocation fallback zone lists.
1426 static int __init build_zonelists_node(pg_data_t *pgdat, struct zonelist *zonelist, int j, int k)
1428 switch (k) {
1429 struct zone *zone;
1430 default:
1431 BUG();
1432 case ZONE_HIGHMEM:
1433 zone = pgdat->node_zones + ZONE_HIGHMEM;
1434 if (zone->present_pages) {
1435 #ifndef CONFIG_HIGHMEM
1436 BUG();
1437 #endif
1438 zonelist->zones[j++] = zone;
1440 case ZONE_NORMAL:
1441 zone = pgdat->node_zones + ZONE_NORMAL;
1442 if (zone->present_pages)
1443 zonelist->zones[j++] = zone;
1444 case ZONE_DMA:
1445 zone = pgdat->node_zones + ZONE_DMA;
1446 if (zone->present_pages)
1447 zonelist->zones[j++] = zone;
1450 return j;
1453 static inline int highest_zone(int zone_bits)
1455 int res = ZONE_NORMAL;
1456 if (zone_bits & (__force int)__GFP_HIGHMEM)
1457 res = ZONE_HIGHMEM;
1458 if (zone_bits & (__force int)__GFP_DMA)
1459 res = ZONE_DMA;
1460 return res;
1463 #ifdef CONFIG_NUMA
1464 #define MAX_NODE_LOAD (num_online_nodes())
1465 static int __initdata node_load[MAX_NUMNODES];
1467 * find_next_best_node - find the next node that should appear in a given node's fallback list
1468 * @node: node whose fallback list we're appending
1469 * @used_node_mask: nodemask_t of already used nodes
1471 * We use a number of factors to determine which is the next node that should
1472 * appear on a given node's fallback list. The node should not have appeared
1473 * already in @node's fallback list, and it should be the next closest node
1474 * according to the distance array (which contains arbitrary distance values
1475 * from each node to each node in the system), and should also prefer nodes
1476 * with no CPUs, since presumably they'll have very little allocation pressure
1477 * on them otherwise.
1478 * It returns -1 if no node is found.
1480 static int __init find_next_best_node(int node, nodemask_t *used_node_mask)
1482 int i, n, val;
1483 int min_val = INT_MAX;
1484 int best_node = -1;
1486 for_each_online_node(i) {
1487 cpumask_t tmp;
1489 /* Start from local node */
1490 n = (node+i) % num_online_nodes();
1492 /* Don't want a node to appear more than once */
1493 if (node_isset(n, *used_node_mask))
1494 continue;
1496 /* Use the local node if we haven't already */
1497 if (!node_isset(node, *used_node_mask)) {
1498 best_node = node;
1499 break;
1502 /* Use the distance array to find the distance */
1503 val = node_distance(node, n);
1505 /* Give preference to headless and unused nodes */
1506 tmp = node_to_cpumask(n);
1507 if (!cpus_empty(tmp))
1508 val += PENALTY_FOR_NODE_WITH_CPUS;
1510 /* Slight preference for less loaded node */
1511 val *= (MAX_NODE_LOAD*MAX_NUMNODES);
1512 val += node_load[n];
1514 if (val < min_val) {
1515 min_val = val;
1516 best_node = n;
1520 if (best_node >= 0)
1521 node_set(best_node, *used_node_mask);
1523 return best_node;
1526 static void __init build_zonelists(pg_data_t *pgdat)
1528 int i, j, k, node, local_node;
1529 int prev_node, load;
1530 struct zonelist *zonelist;
1531 nodemask_t used_mask;
1533 /* initialize zonelists */
1534 for (i = 0; i < GFP_ZONETYPES; i++) {
1535 zonelist = pgdat->node_zonelists + i;
1536 zonelist->zones[0] = NULL;
1539 /* NUMA-aware ordering of nodes */
1540 local_node = pgdat->node_id;
1541 load = num_online_nodes();
1542 prev_node = local_node;
1543 nodes_clear(used_mask);
1544 while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
1546 * We don't want to pressure a particular node.
1547 * So adding penalty to the first node in same
1548 * distance group to make it round-robin.
1550 if (node_distance(local_node, node) !=
1551 node_distance(local_node, prev_node))
1552 node_load[node] += load;
1553 prev_node = node;
1554 load--;
1555 for (i = 0; i < GFP_ZONETYPES; i++) {
1556 zonelist = pgdat->node_zonelists + i;
1557 for (j = 0; zonelist->zones[j] != NULL; j++);
1559 k = highest_zone(i);
1561 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1562 zonelist->zones[j] = NULL;
1567 #else /* CONFIG_NUMA */
1569 static void __init build_zonelists(pg_data_t *pgdat)
1571 int i, j, k, node, local_node;
1573 local_node = pgdat->node_id;
1574 for (i = 0; i < GFP_ZONETYPES; i++) {
1575 struct zonelist *zonelist;
1577 zonelist = pgdat->node_zonelists + i;
1579 j = 0;
1580 k = highest_zone(i);
1581 j = build_zonelists_node(pgdat, zonelist, j, k);
1583 * Now we build the zonelist so that it contains the zones
1584 * of all the other nodes.
1585 * We don't want to pressure a particular node, so when
1586 * building the zones for node N, we make sure that the
1587 * zones coming right after the local ones are those from
1588 * node N+1 (modulo N)
1590 for (node = local_node + 1; node < MAX_NUMNODES; node++) {
1591 if (!node_online(node))
1592 continue;
1593 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1595 for (node = 0; node < local_node; node++) {
1596 if (!node_online(node))
1597 continue;
1598 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1601 zonelist->zones[j] = NULL;
1605 #endif /* CONFIG_NUMA */
1607 void __init build_all_zonelists(void)
1609 int i;
1611 for_each_online_node(i)
1612 build_zonelists(NODE_DATA(i));
1613 printk("Built %i zonelists\n", num_online_nodes());
1614 cpuset_init_current_mems_allowed();
1618 * Helper functions to size the waitqueue hash table.
1619 * Essentially these want to choose hash table sizes sufficiently
1620 * large so that collisions trying to wait on pages are rare.
1621 * But in fact, the number of active page waitqueues on typical
1622 * systems is ridiculously low, less than 200. So this is even
1623 * conservative, even though it seems large.
1625 * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
1626 * waitqueues, i.e. the size of the waitq table given the number of pages.
1628 #define PAGES_PER_WAITQUEUE 256
1630 static inline unsigned long wait_table_size(unsigned long pages)
1632 unsigned long size = 1;
1634 pages /= PAGES_PER_WAITQUEUE;
1636 while (size < pages)
1637 size <<= 1;
1640 * Once we have dozens or even hundreds of threads sleeping
1641 * on IO we've got bigger problems than wait queue collision.
1642 * Limit the size of the wait table to a reasonable size.
1644 size = min(size, 4096UL);
1646 return max(size, 4UL);
1650 * This is an integer logarithm so that shifts can be used later
1651 * to extract the more random high bits from the multiplicative
1652 * hash function before the remainder is taken.
1654 static inline unsigned long wait_table_bits(unsigned long size)
1656 return ffz(~size);
1659 #define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
1661 static void __init calculate_zone_totalpages(struct pglist_data *pgdat,
1662 unsigned long *zones_size, unsigned long *zholes_size)
1664 unsigned long realtotalpages, totalpages = 0;
1665 int i;
1667 for (i = 0; i < MAX_NR_ZONES; i++)
1668 totalpages += zones_size[i];
1669 pgdat->node_spanned_pages = totalpages;
1671 realtotalpages = totalpages;
1672 if (zholes_size)
1673 for (i = 0; i < MAX_NR_ZONES; i++)
1674 realtotalpages -= zholes_size[i];
1675 pgdat->node_present_pages = realtotalpages;
1676 printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id, realtotalpages);
1681 * Initially all pages are reserved - free ones are freed
1682 * up by free_all_bootmem() once the early boot process is
1683 * done. Non-atomic initialization, single-pass.
1685 void __devinit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
1686 unsigned long start_pfn)
1688 struct page *page;
1689 unsigned long end_pfn = start_pfn + size;
1690 unsigned long pfn;
1692 for (pfn = start_pfn; pfn < end_pfn; pfn++, page++) {
1693 if (!early_pfn_valid(pfn))
1694 continue;
1695 if (!early_pfn_in_nid(pfn, nid))
1696 continue;
1697 page = pfn_to_page(pfn);
1698 set_page_links(page, zone, nid, pfn);
1699 set_page_count(page, 1);
1700 reset_page_mapcount(page);
1701 SetPageReserved(page);
1702 INIT_LIST_HEAD(&page->lru);
1703 #ifdef WANT_PAGE_VIRTUAL
1704 /* The shift won't overflow because ZONE_NORMAL is below 4G. */
1705 if (!is_highmem_idx(zone))
1706 set_page_address(page, __va(pfn << PAGE_SHIFT));
1707 #endif
1711 void zone_init_free_lists(struct pglist_data *pgdat, struct zone *zone,
1712 unsigned long size)
1714 int order;
1715 for (order = 0; order < MAX_ORDER ; order++) {
1716 INIT_LIST_HEAD(&zone->free_area[order].free_list);
1717 zone->free_area[order].nr_free = 0;
1721 #define ZONETABLE_INDEX(x, zone_nr) ((x << ZONES_SHIFT) | zone_nr)
1722 void zonetable_add(struct zone *zone, int nid, int zid, unsigned long pfn,
1723 unsigned long size)
1725 unsigned long snum = pfn_to_section_nr(pfn);
1726 unsigned long end = pfn_to_section_nr(pfn + size);
1728 if (FLAGS_HAS_NODE)
1729 zone_table[ZONETABLE_INDEX(nid, zid)] = zone;
1730 else
1731 for (; snum <= end; snum++)
1732 zone_table[ZONETABLE_INDEX(snum, zid)] = zone;
1735 #ifndef __HAVE_ARCH_MEMMAP_INIT
1736 #define memmap_init(size, nid, zone, start_pfn) \
1737 memmap_init_zone((size), (nid), (zone), (start_pfn))
1738 #endif
1740 static int __devinit zone_batchsize(struct zone *zone)
1742 int batch;
1745 * The per-cpu-pages pools are set to around 1000th of the
1746 * size of the zone. But no more than 1/2 of a meg.
1748 * OK, so we don't know how big the cache is. So guess.
1750 batch = zone->present_pages / 1024;
1751 if (batch * PAGE_SIZE > 512 * 1024)
1752 batch = (512 * 1024) / PAGE_SIZE;
1753 batch /= 4; /* We effectively *= 4 below */
1754 if (batch < 1)
1755 batch = 1;
1758 * We will be trying to allcoate bigger chunks of contiguous
1759 * memory of the order of fls(batch). This should result in
1760 * better cache coloring.
1762 * A sanity check also to ensure that batch is still in limits.
1764 batch = (1 << fls(batch + batch/2));
1766 if (fls(batch) >= (PAGE_SHIFT + MAX_ORDER - 2))
1767 batch = PAGE_SHIFT + ((MAX_ORDER - 1 - PAGE_SHIFT)/2);
1769 return batch;
1772 inline void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
1774 struct per_cpu_pages *pcp;
1776 memset(p, 0, sizeof(*p));
1778 pcp = &p->pcp[0]; /* hot */
1779 pcp->count = 0;
1780 pcp->low = 0;
1781 pcp->high = 6 * batch;
1782 pcp->batch = max(1UL, 1 * batch);
1783 INIT_LIST_HEAD(&pcp->list);
1785 pcp = &p->pcp[1]; /* cold*/
1786 pcp->count = 0;
1787 pcp->low = 0;
1788 pcp->high = 2 * batch;
1789 pcp->batch = max(1UL, batch/2);
1790 INIT_LIST_HEAD(&pcp->list);
1793 #ifdef CONFIG_NUMA
1795 * Boot pageset table. One per cpu which is going to be used for all
1796 * zones and all nodes. The parameters will be set in such a way
1797 * that an item put on a list will immediately be handed over to
1798 * the buddy list. This is safe since pageset manipulation is done
1799 * with interrupts disabled.
1801 * Some NUMA counter updates may also be caught by the boot pagesets.
1803 * The boot_pagesets must be kept even after bootup is complete for
1804 * unused processors and/or zones. They do play a role for bootstrapping
1805 * hotplugged processors.
1807 * zoneinfo_show() and maybe other functions do
1808 * not check if the processor is online before following the pageset pointer.
1809 * Other parts of the kernel may not check if the zone is available.
1811 static struct per_cpu_pageset
1812 boot_pageset[NR_CPUS];
1815 * Dynamically allocate memory for the
1816 * per cpu pageset array in struct zone.
1818 static int __devinit process_zones(int cpu)
1820 struct zone *zone, *dzone;
1822 for_each_zone(zone) {
1824 zone->pageset[cpu] = kmalloc_node(sizeof(struct per_cpu_pageset),
1825 GFP_KERNEL, cpu_to_node(cpu));
1826 if (!zone->pageset[cpu])
1827 goto bad;
1829 setup_pageset(zone->pageset[cpu], zone_batchsize(zone));
1832 return 0;
1833 bad:
1834 for_each_zone(dzone) {
1835 if (dzone == zone)
1836 break;
1837 kfree(dzone->pageset[cpu]);
1838 dzone->pageset[cpu] = NULL;
1840 return -ENOMEM;
1843 static inline void free_zone_pagesets(int cpu)
1845 #ifdef CONFIG_NUMA
1846 struct zone *zone;
1848 for_each_zone(zone) {
1849 struct per_cpu_pageset *pset = zone_pcp(zone, cpu);
1851 zone_pcp(zone, cpu) = NULL;
1852 kfree(pset);
1854 #endif
1857 static int __devinit pageset_cpuup_callback(struct notifier_block *nfb,
1858 unsigned long action,
1859 void *hcpu)
1861 int cpu = (long)hcpu;
1862 int ret = NOTIFY_OK;
1864 switch (action) {
1865 case CPU_UP_PREPARE:
1866 if (process_zones(cpu))
1867 ret = NOTIFY_BAD;
1868 break;
1869 #ifdef CONFIG_HOTPLUG_CPU
1870 case CPU_DEAD:
1871 free_zone_pagesets(cpu);
1872 break;
1873 #endif
1874 default:
1875 break;
1877 return ret;
1880 static struct notifier_block pageset_notifier =
1881 { &pageset_cpuup_callback, NULL, 0 };
1883 void __init setup_per_cpu_pageset()
1885 int err;
1887 /* Initialize per_cpu_pageset for cpu 0.
1888 * A cpuup callback will do this for every cpu
1889 * as it comes online
1891 err = process_zones(smp_processor_id());
1892 BUG_ON(err);
1893 register_cpu_notifier(&pageset_notifier);
1896 #endif
1898 static __devinit
1899 void zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages)
1901 int i;
1902 struct pglist_data *pgdat = zone->zone_pgdat;
1905 * The per-page waitqueue mechanism uses hashed waitqueues
1906 * per zone.
1908 zone->wait_table_size = wait_table_size(zone_size_pages);
1909 zone->wait_table_bits = wait_table_bits(zone->wait_table_size);
1910 zone->wait_table = (wait_queue_head_t *)
1911 alloc_bootmem_node(pgdat, zone->wait_table_size
1912 * sizeof(wait_queue_head_t));
1914 for(i = 0; i < zone->wait_table_size; ++i)
1915 init_waitqueue_head(zone->wait_table + i);
1918 static __devinit void zone_pcp_init(struct zone *zone)
1920 int cpu;
1921 unsigned long batch = zone_batchsize(zone);
1923 for (cpu = 0; cpu < NR_CPUS; cpu++) {
1924 #ifdef CONFIG_NUMA
1925 /* Early boot. Slab allocator not functional yet */
1926 zone->pageset[cpu] = &boot_pageset[cpu];
1927 setup_pageset(&boot_pageset[cpu],0);
1928 #else
1929 setup_pageset(zone_pcp(zone,cpu), batch);
1930 #endif
1932 printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%lu\n",
1933 zone->name, zone->present_pages, batch);
1936 static __devinit void init_currently_empty_zone(struct zone *zone,
1937 unsigned long zone_start_pfn, unsigned long size)
1939 struct pglist_data *pgdat = zone->zone_pgdat;
1941 zone_wait_table_init(zone, size);
1942 pgdat->nr_zones = zone_idx(zone) + 1;
1944 zone->zone_mem_map = pfn_to_page(zone_start_pfn);
1945 zone->zone_start_pfn = zone_start_pfn;
1947 memmap_init(size, pgdat->node_id, zone_idx(zone), zone_start_pfn);
1949 zone_init_free_lists(pgdat, zone, zone->spanned_pages);
1953 * Set up the zone data structures:
1954 * - mark all pages reserved
1955 * - mark all memory queues empty
1956 * - clear the memory bitmaps
1958 static void __init free_area_init_core(struct pglist_data *pgdat,
1959 unsigned long *zones_size, unsigned long *zholes_size)
1961 unsigned long j;
1962 int nid = pgdat->node_id;
1963 unsigned long zone_start_pfn = pgdat->node_start_pfn;
1965 pgdat_resize_init(pgdat);
1966 pgdat->nr_zones = 0;
1967 init_waitqueue_head(&pgdat->kswapd_wait);
1968 pgdat->kswapd_max_order = 0;
1970 for (j = 0; j < MAX_NR_ZONES; j++) {
1971 struct zone *zone = pgdat->node_zones + j;
1972 unsigned long size, realsize;
1974 realsize = size = zones_size[j];
1975 if (zholes_size)
1976 realsize -= zholes_size[j];
1978 if (j == ZONE_DMA || j == ZONE_NORMAL)
1979 nr_kernel_pages += realsize;
1980 nr_all_pages += realsize;
1982 zone->spanned_pages = size;
1983 zone->present_pages = realsize;
1984 zone->name = zone_names[j];
1985 spin_lock_init(&zone->lock);
1986 spin_lock_init(&zone->lru_lock);
1987 zone_seqlock_init(zone);
1988 zone->zone_pgdat = pgdat;
1989 zone->free_pages = 0;
1991 zone->temp_priority = zone->prev_priority = DEF_PRIORITY;
1993 zone_pcp_init(zone);
1994 INIT_LIST_HEAD(&zone->active_list);
1995 INIT_LIST_HEAD(&zone->inactive_list);
1996 zone->nr_scan_active = 0;
1997 zone->nr_scan_inactive = 0;
1998 zone->nr_active = 0;
1999 zone->nr_inactive = 0;
2000 atomic_set(&zone->reclaim_in_progress, 0);
2001 if (!size)
2002 continue;
2004 zonetable_add(zone, nid, j, zone_start_pfn, size);
2005 init_currently_empty_zone(zone, zone_start_pfn, size);
2006 zone_start_pfn += size;
2010 static void __init alloc_node_mem_map(struct pglist_data *pgdat)
2012 /* Skip empty nodes */
2013 if (!pgdat->node_spanned_pages)
2014 return;
2016 #ifdef CONFIG_FLAT_NODE_MEM_MAP
2017 /* ia64 gets its own node_mem_map, before this, without bootmem */
2018 if (!pgdat->node_mem_map) {
2019 unsigned long size;
2020 struct page *map;
2022 size = (pgdat->node_spanned_pages + 1) * sizeof(struct page);
2023 map = alloc_remap(pgdat->node_id, size);
2024 if (!map)
2025 map = alloc_bootmem_node(pgdat, size);
2026 pgdat->node_mem_map = map;
2028 #ifdef CONFIG_FLATMEM
2030 * With no DISCONTIG, the global mem_map is just set as node 0's
2032 if (pgdat == NODE_DATA(0))
2033 mem_map = NODE_DATA(0)->node_mem_map;
2034 #endif
2035 #endif /* CONFIG_FLAT_NODE_MEM_MAP */
2038 void __init free_area_init_node(int nid, struct pglist_data *pgdat,
2039 unsigned long *zones_size, unsigned long node_start_pfn,
2040 unsigned long *zholes_size)
2042 pgdat->node_id = nid;
2043 pgdat->node_start_pfn = node_start_pfn;
2044 calculate_zone_totalpages(pgdat, zones_size, zholes_size);
2046 alloc_node_mem_map(pgdat);
2048 free_area_init_core(pgdat, zones_size, zholes_size);
2051 #ifndef CONFIG_NEED_MULTIPLE_NODES
2052 static bootmem_data_t contig_bootmem_data;
2053 struct pglist_data contig_page_data = { .bdata = &contig_bootmem_data };
2055 EXPORT_SYMBOL(contig_page_data);
2056 #endif
2058 void __init free_area_init(unsigned long *zones_size)
2060 free_area_init_node(0, NODE_DATA(0), zones_size,
2061 __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
2064 #ifdef CONFIG_PROC_FS
2066 #include <linux/seq_file.h>
2068 static void *frag_start(struct seq_file *m, loff_t *pos)
2070 pg_data_t *pgdat;
2071 loff_t node = *pos;
2073 for (pgdat = pgdat_list; pgdat && node; pgdat = pgdat->pgdat_next)
2074 --node;
2076 return pgdat;
2079 static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
2081 pg_data_t *pgdat = (pg_data_t *)arg;
2083 (*pos)++;
2084 return pgdat->pgdat_next;
2087 static void frag_stop(struct seq_file *m, void *arg)
2092 * This walks the free areas for each zone.
2094 static int frag_show(struct seq_file *m, void *arg)
2096 pg_data_t *pgdat = (pg_data_t *)arg;
2097 struct zone *zone;
2098 struct zone *node_zones = pgdat->node_zones;
2099 unsigned long flags;
2100 int order;
2102 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
2103 if (!zone->present_pages)
2104 continue;
2106 spin_lock_irqsave(&zone->lock, flags);
2107 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
2108 for (order = 0; order < MAX_ORDER; ++order)
2109 seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
2110 spin_unlock_irqrestore(&zone->lock, flags);
2111 seq_putc(m, '\n');
2113 return 0;
2116 struct seq_operations fragmentation_op = {
2117 .start = frag_start,
2118 .next = frag_next,
2119 .stop = frag_stop,
2120 .show = frag_show,
2124 * Output information about zones in @pgdat.
2126 static int zoneinfo_show(struct seq_file *m, void *arg)
2128 pg_data_t *pgdat = arg;
2129 struct zone *zone;
2130 struct zone *node_zones = pgdat->node_zones;
2131 unsigned long flags;
2133 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; zone++) {
2134 int i;
2136 if (!zone->present_pages)
2137 continue;
2139 spin_lock_irqsave(&zone->lock, flags);
2140 seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
2141 seq_printf(m,
2142 "\n pages free %lu"
2143 "\n min %lu"
2144 "\n low %lu"
2145 "\n high %lu"
2146 "\n active %lu"
2147 "\n inactive %lu"
2148 "\n scanned %lu (a: %lu i: %lu)"
2149 "\n spanned %lu"
2150 "\n present %lu",
2151 zone->free_pages,
2152 zone->pages_min,
2153 zone->pages_low,
2154 zone->pages_high,
2155 zone->nr_active,
2156 zone->nr_inactive,
2157 zone->pages_scanned,
2158 zone->nr_scan_active, zone->nr_scan_inactive,
2159 zone->spanned_pages,
2160 zone->present_pages);
2161 seq_printf(m,
2162 "\n protection: (%lu",
2163 zone->lowmem_reserve[0]);
2164 for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
2165 seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
2166 seq_printf(m,
2168 "\n pagesets");
2169 for (i = 0; i < ARRAY_SIZE(zone->pageset); i++) {
2170 struct per_cpu_pageset *pageset;
2171 int j;
2173 pageset = zone_pcp(zone, i);
2174 for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
2175 if (pageset->pcp[j].count)
2176 break;
2178 if (j == ARRAY_SIZE(pageset->pcp))
2179 continue;
2180 for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
2181 seq_printf(m,
2182 "\n cpu: %i pcp: %i"
2183 "\n count: %i"
2184 "\n low: %i"
2185 "\n high: %i"
2186 "\n batch: %i",
2187 i, j,
2188 pageset->pcp[j].count,
2189 pageset->pcp[j].low,
2190 pageset->pcp[j].high,
2191 pageset->pcp[j].batch);
2193 #ifdef CONFIG_NUMA
2194 seq_printf(m,
2195 "\n numa_hit: %lu"
2196 "\n numa_miss: %lu"
2197 "\n numa_foreign: %lu"
2198 "\n interleave_hit: %lu"
2199 "\n local_node: %lu"
2200 "\n other_node: %lu",
2201 pageset->numa_hit,
2202 pageset->numa_miss,
2203 pageset->numa_foreign,
2204 pageset->interleave_hit,
2205 pageset->local_node,
2206 pageset->other_node);
2207 #endif
2209 seq_printf(m,
2210 "\n all_unreclaimable: %u"
2211 "\n prev_priority: %i"
2212 "\n temp_priority: %i"
2213 "\n start_pfn: %lu",
2214 zone->all_unreclaimable,
2215 zone->prev_priority,
2216 zone->temp_priority,
2217 zone->zone_start_pfn);
2218 spin_unlock_irqrestore(&zone->lock, flags);
2219 seq_putc(m, '\n');
2221 return 0;
2224 struct seq_operations zoneinfo_op = {
2225 .start = frag_start, /* iterate over all zones. The same as in
2226 * fragmentation. */
2227 .next = frag_next,
2228 .stop = frag_stop,
2229 .show = zoneinfo_show,
2232 static char *vmstat_text[] = {
2233 "nr_dirty",
2234 "nr_writeback",
2235 "nr_unstable",
2236 "nr_page_table_pages",
2237 "nr_mapped",
2238 "nr_slab",
2240 "pgpgin",
2241 "pgpgout",
2242 "pswpin",
2243 "pswpout",
2244 "pgalloc_high",
2246 "pgalloc_normal",
2247 "pgalloc_dma",
2248 "pgfree",
2249 "pgactivate",
2250 "pgdeactivate",
2252 "pgfault",
2253 "pgmajfault",
2254 "pgrefill_high",
2255 "pgrefill_normal",
2256 "pgrefill_dma",
2258 "pgsteal_high",
2259 "pgsteal_normal",
2260 "pgsteal_dma",
2261 "pgscan_kswapd_high",
2262 "pgscan_kswapd_normal",
2264 "pgscan_kswapd_dma",
2265 "pgscan_direct_high",
2266 "pgscan_direct_normal",
2267 "pgscan_direct_dma",
2268 "pginodesteal",
2270 "slabs_scanned",
2271 "kswapd_steal",
2272 "kswapd_inodesteal",
2273 "pageoutrun",
2274 "allocstall",
2276 "pgrotated",
2277 "nr_bounce",
2280 static void *vmstat_start(struct seq_file *m, loff_t *pos)
2282 struct page_state *ps;
2284 if (*pos >= ARRAY_SIZE(vmstat_text))
2285 return NULL;
2287 ps = kmalloc(sizeof(*ps), GFP_KERNEL);
2288 m->private = ps;
2289 if (!ps)
2290 return ERR_PTR(-ENOMEM);
2291 get_full_page_state(ps);
2292 ps->pgpgin /= 2; /* sectors -> kbytes */
2293 ps->pgpgout /= 2;
2294 return (unsigned long *)ps + *pos;
2297 static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
2299 (*pos)++;
2300 if (*pos >= ARRAY_SIZE(vmstat_text))
2301 return NULL;
2302 return (unsigned long *)m->private + *pos;
2305 static int vmstat_show(struct seq_file *m, void *arg)
2307 unsigned long *l = arg;
2308 unsigned long off = l - (unsigned long *)m->private;
2310 seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
2311 return 0;
2314 static void vmstat_stop(struct seq_file *m, void *arg)
2316 kfree(m->private);
2317 m->private = NULL;
2320 struct seq_operations vmstat_op = {
2321 .start = vmstat_start,
2322 .next = vmstat_next,
2323 .stop = vmstat_stop,
2324 .show = vmstat_show,
2327 #endif /* CONFIG_PROC_FS */
2329 #ifdef CONFIG_HOTPLUG_CPU
2330 static int page_alloc_cpu_notify(struct notifier_block *self,
2331 unsigned long action, void *hcpu)
2333 int cpu = (unsigned long)hcpu;
2334 long *count;
2335 unsigned long *src, *dest;
2337 if (action == CPU_DEAD) {
2338 int i;
2340 /* Drain local pagecache count. */
2341 count = &per_cpu(nr_pagecache_local, cpu);
2342 atomic_add(*count, &nr_pagecache);
2343 *count = 0;
2344 local_irq_disable();
2345 __drain_pages(cpu);
2347 /* Add dead cpu's page_states to our own. */
2348 dest = (unsigned long *)&__get_cpu_var(page_states);
2349 src = (unsigned long *)&per_cpu(page_states, cpu);
2351 for (i = 0; i < sizeof(struct page_state)/sizeof(unsigned long);
2352 i++) {
2353 dest[i] += src[i];
2354 src[i] = 0;
2357 local_irq_enable();
2359 return NOTIFY_OK;
2361 #endif /* CONFIG_HOTPLUG_CPU */
2363 void __init page_alloc_init(void)
2365 hotcpu_notifier(page_alloc_cpu_notify, 0);
2369 * setup_per_zone_lowmem_reserve - called whenever
2370 * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
2371 * has a correct pages reserved value, so an adequate number of
2372 * pages are left in the zone after a successful __alloc_pages().
2374 static void setup_per_zone_lowmem_reserve(void)
2376 struct pglist_data *pgdat;
2377 int j, idx;
2379 for_each_pgdat(pgdat) {
2380 for (j = 0; j < MAX_NR_ZONES; j++) {
2381 struct zone *zone = pgdat->node_zones + j;
2382 unsigned long present_pages = zone->present_pages;
2384 zone->lowmem_reserve[j] = 0;
2386 for (idx = j-1; idx >= 0; idx--) {
2387 struct zone *lower_zone;
2389 if (sysctl_lowmem_reserve_ratio[idx] < 1)
2390 sysctl_lowmem_reserve_ratio[idx] = 1;
2392 lower_zone = pgdat->node_zones + idx;
2393 lower_zone->lowmem_reserve[j] = present_pages /
2394 sysctl_lowmem_reserve_ratio[idx];
2395 present_pages += lower_zone->present_pages;
2402 * setup_per_zone_pages_min - called when min_free_kbytes changes. Ensures
2403 * that the pages_{min,low,high} values for each zone are set correctly
2404 * with respect to min_free_kbytes.
2406 void setup_per_zone_pages_min(void)
2408 unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
2409 unsigned long lowmem_pages = 0;
2410 struct zone *zone;
2411 unsigned long flags;
2413 /* Calculate total number of !ZONE_HIGHMEM pages */
2414 for_each_zone(zone) {
2415 if (!is_highmem(zone))
2416 lowmem_pages += zone->present_pages;
2419 for_each_zone(zone) {
2420 spin_lock_irqsave(&zone->lru_lock, flags);
2421 if (is_highmem(zone)) {
2423 * Often, highmem doesn't need to reserve any pages.
2424 * But the pages_min/low/high values are also used for
2425 * batching up page reclaim activity so we need a
2426 * decent value here.
2428 int min_pages;
2430 min_pages = zone->present_pages / 1024;
2431 if (min_pages < SWAP_CLUSTER_MAX)
2432 min_pages = SWAP_CLUSTER_MAX;
2433 if (min_pages > 128)
2434 min_pages = 128;
2435 zone->pages_min = min_pages;
2436 } else {
2437 /* if it's a lowmem zone, reserve a number of pages
2438 * proportionate to the zone's size.
2440 zone->pages_min = (pages_min * zone->present_pages) /
2441 lowmem_pages;
2445 * When interpreting these watermarks, just keep in mind that:
2446 * zone->pages_min == (zone->pages_min * 4) / 4;
2448 zone->pages_low = (zone->pages_min * 5) / 4;
2449 zone->pages_high = (zone->pages_min * 6) / 4;
2450 spin_unlock_irqrestore(&zone->lru_lock, flags);
2455 * Initialise min_free_kbytes.
2457 * For small machines we want it small (128k min). For large machines
2458 * we want it large (64MB max). But it is not linear, because network
2459 * bandwidth does not increase linearly with machine size. We use
2461 * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
2462 * min_free_kbytes = sqrt(lowmem_kbytes * 16)
2464 * which yields
2466 * 16MB: 512k
2467 * 32MB: 724k
2468 * 64MB: 1024k
2469 * 128MB: 1448k
2470 * 256MB: 2048k
2471 * 512MB: 2896k
2472 * 1024MB: 4096k
2473 * 2048MB: 5792k
2474 * 4096MB: 8192k
2475 * 8192MB: 11584k
2476 * 16384MB: 16384k
2478 static int __init init_per_zone_pages_min(void)
2480 unsigned long lowmem_kbytes;
2482 lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
2484 min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
2485 if (min_free_kbytes < 128)
2486 min_free_kbytes = 128;
2487 if (min_free_kbytes > 65536)
2488 min_free_kbytes = 65536;
2489 setup_per_zone_pages_min();
2490 setup_per_zone_lowmem_reserve();
2491 return 0;
2493 module_init(init_per_zone_pages_min)
2496 * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
2497 * that we can call two helper functions whenever min_free_kbytes
2498 * changes.
2500 int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
2501 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
2503 proc_dointvec(table, write, file, buffer, length, ppos);
2504 setup_per_zone_pages_min();
2505 return 0;
2509 * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
2510 * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
2511 * whenever sysctl_lowmem_reserve_ratio changes.
2513 * The reserve ratio obviously has absolutely no relation with the
2514 * pages_min watermarks. The lowmem reserve ratio can only make sense
2515 * if in function of the boot time zone sizes.
2517 int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
2518 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
2520 proc_dointvec_minmax(table, write, file, buffer, length, ppos);
2521 setup_per_zone_lowmem_reserve();
2522 return 0;
2525 __initdata int hashdist = HASHDIST_DEFAULT;
2527 #ifdef CONFIG_NUMA
2528 static int __init set_hashdist(char *str)
2530 if (!str)
2531 return 0;
2532 hashdist = simple_strtoul(str, &str, 0);
2533 return 1;
2535 __setup("hashdist=", set_hashdist);
2536 #endif
2539 * allocate a large system hash table from bootmem
2540 * - it is assumed that the hash table must contain an exact power-of-2
2541 * quantity of entries
2542 * - limit is the number of hash buckets, not the total allocation size
2544 void *__init alloc_large_system_hash(const char *tablename,
2545 unsigned long bucketsize,
2546 unsigned long numentries,
2547 int scale,
2548 int flags,
2549 unsigned int *_hash_shift,
2550 unsigned int *_hash_mask,
2551 unsigned long limit)
2553 unsigned long long max = limit;
2554 unsigned long log2qty, size;
2555 void *table = NULL;
2557 /* allow the kernel cmdline to have a say */
2558 if (!numentries) {
2559 /* round applicable memory size up to nearest megabyte */
2560 numentries = (flags & HASH_HIGHMEM) ? nr_all_pages : nr_kernel_pages;
2561 numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
2562 numentries >>= 20 - PAGE_SHIFT;
2563 numentries <<= 20 - PAGE_SHIFT;
2565 /* limit to 1 bucket per 2^scale bytes of low memory */
2566 if (scale > PAGE_SHIFT)
2567 numentries >>= (scale - PAGE_SHIFT);
2568 else
2569 numentries <<= (PAGE_SHIFT - scale);
2571 /* rounded up to nearest power of 2 in size */
2572 numentries = 1UL << (long_log2(numentries) + 1);
2574 /* limit allocation size to 1/16 total memory by default */
2575 if (max == 0) {
2576 max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
2577 do_div(max, bucketsize);
2580 if (numentries > max)
2581 numentries = max;
2583 log2qty = long_log2(numentries);
2585 do {
2586 size = bucketsize << log2qty;
2587 if (flags & HASH_EARLY)
2588 table = alloc_bootmem(size);
2589 else if (hashdist)
2590 table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
2591 else {
2592 unsigned long order;
2593 for (order = 0; ((1UL << order) << PAGE_SHIFT) < size; order++)
2595 table = (void*) __get_free_pages(GFP_ATOMIC, order);
2597 } while (!table && size > PAGE_SIZE && --log2qty);
2599 if (!table)
2600 panic("Failed to allocate %s hash table\n", tablename);
2602 printk("%s hash table entries: %d (order: %d, %lu bytes)\n",
2603 tablename,
2604 (1U << log2qty),
2605 long_log2(size) - PAGE_SHIFT,
2606 size);
2608 if (_hash_shift)
2609 *_hash_shift = log2qty;
2610 if (_hash_mask)
2611 *_hash_mask = (1 << log2qty) - 1;
2613 return table;