]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - mm/page_alloc.c
[PATCH] add OOM debug
[mirror_ubuntu-zesty-kernel.git] / mm / page_alloc.c
CommitLineData
1da177e4
LT
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 */
16
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/module.h>
26#include <linux/suspend.h>
27#include <linux/pagevec.h>
28#include <linux/blkdev.h>
29#include <linux/slab.h>
30#include <linux/notifier.h>
31#include <linux/topology.h>
32#include <linux/sysctl.h>
33#include <linux/cpu.h>
34#include <linux/cpuset.h>
35#include <linux/nodemask.h>
36#include <linux/vmalloc.h>
37
38#include <asm/tlbflush.h>
39#include "internal.h"
40
41/*
42 * MCD - HACK: Find somewhere to initialize this EARLY, or make this
43 * initializer cleaner
44 */
45nodemask_t node_online_map = { { [0] = 1UL } };
7223a93a 46EXPORT_SYMBOL(node_online_map);
1da177e4 47nodemask_t node_possible_map = NODE_MASK_ALL;
7223a93a 48EXPORT_SYMBOL(node_possible_map);
1da177e4
LT
49struct pglist_data *pgdat_list;
50unsigned long totalram_pages;
51unsigned long totalhigh_pages;
52long nr_swap_pages;
53
54/*
55 * results with 256, 32 in the lowmem_reserve sysctl:
56 * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
57 * 1G machine -> (16M dma, 784M normal, 224M high)
58 * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
59 * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
60 * HIGHMEM allocation will (224M+784M)/256 of ram reserved in ZONE_DMA
61 */
62int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1] = { 256, 32 };
63
64EXPORT_SYMBOL(totalram_pages);
65EXPORT_SYMBOL(nr_swap_pages);
66
67/*
68 * Used by page_zone() to look up the address of the struct zone whose
69 * id is encoded in the upper bits of page->flags
70 */
71struct zone *zone_table[1 << (ZONES_SHIFT + NODES_SHIFT)];
72EXPORT_SYMBOL(zone_table);
73
e7c8d5c9
CL
74#ifdef CONFIG_NUMA
75static struct per_cpu_pageset
76 pageset_table[MAX_NR_ZONES*MAX_NUMNODES*NR_CPUS] __initdata;
77#endif
78
1da177e4
LT
79static char *zone_names[MAX_NR_ZONES] = { "DMA", "Normal", "HighMem" };
80int min_free_kbytes = 1024;
81
82unsigned long __initdata nr_kernel_pages;
83unsigned long __initdata nr_all_pages;
84
85/*
86 * Temporary debugging check for pages not lying within a given zone.
87 */
88static int bad_range(struct zone *zone, struct page *page)
89{
90 if (page_to_pfn(page) >= zone->zone_start_pfn + zone->spanned_pages)
91 return 1;
92 if (page_to_pfn(page) < zone->zone_start_pfn)
93 return 1;
94#ifdef CONFIG_HOLES_IN_ZONE
95 if (!pfn_valid(page_to_pfn(page)))
96 return 1;
97#endif
98 if (zone != page_zone(page))
99 return 1;
100 return 0;
101}
102
103static void bad_page(const char *function, struct page *page)
104{
105 printk(KERN_EMERG "Bad page state at %s (in process '%s', page %p)\n",
106 function, current->comm, page);
107 printk(KERN_EMERG "flags:0x%0*lx mapping:%p mapcount:%d count:%d\n",
108 (int)(2*sizeof(page_flags_t)), (unsigned long)page->flags,
109 page->mapping, page_mapcount(page), page_count(page));
110 printk(KERN_EMERG "Backtrace:\n");
111 dump_stack();
112 printk(KERN_EMERG "Trying to fix it up, but a reboot is needed\n");
113 page->flags &= ~(1 << PG_private |
114 1 << PG_locked |
115 1 << PG_lru |
116 1 << PG_active |
117 1 << PG_dirty |
118 1 << PG_swapcache |
119 1 << PG_writeback);
120 set_page_count(page, 0);
121 reset_page_mapcount(page);
122 page->mapping = NULL;
123 tainted |= TAINT_BAD_PAGE;
124}
125
126#ifndef CONFIG_HUGETLB_PAGE
127#define prep_compound_page(page, order) do { } while (0)
128#define destroy_compound_page(page, order) do { } while (0)
129#else
130/*
131 * Higher-order pages are called "compound pages". They are structured thusly:
132 *
133 * The first PAGE_SIZE page is called the "head page".
134 *
135 * The remaining PAGE_SIZE pages are called "tail pages".
136 *
137 * All pages have PG_compound set. All pages have their ->private pointing at
138 * the head page (even the head page has this).
139 *
140 * The first tail page's ->mapping, if non-zero, holds the address of the
141 * compound page's put_page() function.
142 *
143 * The order of the allocation is stored in the first tail page's ->index
144 * This is only for debug at present. This usage means that zero-order pages
145 * may not be compound.
146 */
147static void prep_compound_page(struct page *page, unsigned long order)
148{
149 int i;
150 int nr_pages = 1 << order;
151
152 page[1].mapping = NULL;
153 page[1].index = order;
154 for (i = 0; i < nr_pages; i++) {
155 struct page *p = page + i;
156
157 SetPageCompound(p);
158 p->private = (unsigned long)page;
159 }
160}
161
162static void destroy_compound_page(struct page *page, unsigned long order)
163{
164 int i;
165 int nr_pages = 1 << order;
166
167 if (!PageCompound(page))
168 return;
169
170 if (page[1].index != order)
171 bad_page(__FUNCTION__, page);
172
173 for (i = 0; i < nr_pages; i++) {
174 struct page *p = page + i;
175
176 if (!PageCompound(p))
177 bad_page(__FUNCTION__, page);
178 if (p->private != (unsigned long)page)
179 bad_page(__FUNCTION__, page);
180 ClearPageCompound(p);
181 }
182}
183#endif /* CONFIG_HUGETLB_PAGE */
184
185/*
186 * function for dealing with page's order in buddy system.
187 * zone->lock is already acquired when we use these.
188 * So, we don't need atomic page->flags operations here.
189 */
190static inline unsigned long page_order(struct page *page) {
191 return page->private;
192}
193
194static inline void set_page_order(struct page *page, int order) {
195 page->private = order;
196 __SetPagePrivate(page);
197}
198
199static inline void rmv_page_order(struct page *page)
200{
201 __ClearPagePrivate(page);
202 page->private = 0;
203}
204
205/*
206 * Locate the struct page for both the matching buddy in our
207 * pair (buddy1) and the combined O(n+1) page they form (page).
208 *
209 * 1) Any buddy B1 will have an order O twin B2 which satisfies
210 * the following equation:
211 * B2 = B1 ^ (1 << O)
212 * For example, if the starting buddy (buddy2) is #8 its order
213 * 1 buddy is #10:
214 * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
215 *
216 * 2) Any buddy B will have an order O+1 parent P which
217 * satisfies the following equation:
218 * P = B & ~(1 << O)
219 *
220 * Assumption: *_mem_map is contigious at least up to MAX_ORDER
221 */
222static inline struct page *
223__page_find_buddy(struct page *page, unsigned long page_idx, unsigned int order)
224{
225 unsigned long buddy_idx = page_idx ^ (1 << order);
226
227 return page + (buddy_idx - page_idx);
228}
229
230static inline unsigned long
231__find_combined_index(unsigned long page_idx, unsigned int order)
232{
233 return (page_idx & ~(1 << order));
234}
235
236/*
237 * This function checks whether a page is free && is the buddy
238 * we can do coalesce a page and its buddy if
239 * (a) the buddy is free &&
240 * (b) the buddy is on the buddy system &&
241 * (c) a page and its buddy have the same order.
242 * for recording page's order, we use page->private and PG_private.
243 *
244 */
245static inline int page_is_buddy(struct page *page, int order)
246{
247 if (PagePrivate(page) &&
248 (page_order(page) == order) &&
249 !PageReserved(page) &&
250 page_count(page) == 0)
251 return 1;
252 return 0;
253}
254
255/*
256 * Freeing function for a buddy system allocator.
257 *
258 * The concept of a buddy system is to maintain direct-mapped table
259 * (containing bit values) for memory blocks of various "orders".
260 * The bottom level table contains the map for the smallest allocatable
261 * units of memory (here, pages), and each level above it describes
262 * pairs of units from the levels below, hence, "buddies".
263 * At a high level, all that happens here is marking the table entry
264 * at the bottom level available, and propagating the changes upward
265 * as necessary, plus some accounting needed to play nicely with other
266 * parts of the VM system.
267 * At each level, we keep a list of pages, which are heads of continuous
268 * free pages of length of (1 << order) and marked with PG_Private.Page's
269 * order is recorded in page->private field.
270 * So when we are allocating or freeing one, we can derive the state of the
271 * other. That is, if we allocate a small block, and both were
272 * free, the remainder of the region must be split into blocks.
273 * If a block is freed, and its buddy is also free, then this
274 * triggers coalescing into a block of larger size.
275 *
276 * -- wli
277 */
278
279static inline void __free_pages_bulk (struct page *page,
280 struct zone *zone, unsigned int order)
281{
282 unsigned long page_idx;
283 int order_size = 1 << order;
284
285 if (unlikely(order))
286 destroy_compound_page(page, order);
287
288 page_idx = page_to_pfn(page) & ((1 << MAX_ORDER) - 1);
289
290 BUG_ON(page_idx & (order_size - 1));
291 BUG_ON(bad_range(zone, page));
292
293 zone->free_pages += order_size;
294 while (order < MAX_ORDER-1) {
295 unsigned long combined_idx;
296 struct free_area *area;
297 struct page *buddy;
298
299 combined_idx = __find_combined_index(page_idx, order);
300 buddy = __page_find_buddy(page, page_idx, order);
301
302 if (bad_range(zone, buddy))
303 break;
304 if (!page_is_buddy(buddy, order))
305 break; /* Move the buddy up one level. */
306 list_del(&buddy->lru);
307 area = zone->free_area + order;
308 area->nr_free--;
309 rmv_page_order(buddy);
310 page = page + (combined_idx - page_idx);
311 page_idx = combined_idx;
312 order++;
313 }
314 set_page_order(page, order);
315 list_add(&page->lru, &zone->free_area[order].free_list);
316 zone->free_area[order].nr_free++;
317}
318
319static inline void free_pages_check(const char *function, struct page *page)
320{
321 if ( page_mapcount(page) ||
322 page->mapping != NULL ||
323 page_count(page) != 0 ||
324 (page->flags & (
325 1 << PG_lru |
326 1 << PG_private |
327 1 << PG_locked |
328 1 << PG_active |
329 1 << PG_reclaim |
330 1 << PG_slab |
331 1 << PG_swapcache |
332 1 << PG_writeback )))
333 bad_page(function, page);
334 if (PageDirty(page))
335 ClearPageDirty(page);
336}
337
338/*
339 * Frees a list of pages.
340 * Assumes all pages on list are in same zone, and of same order.
341 * count is the number of pages to free, or 0 for all on the list.
342 *
343 * If the zone was previously in an "all pages pinned" state then look to
344 * see if this freeing clears that state.
345 *
346 * And clear the zone's pages_scanned counter, to hold off the "all pages are
347 * pinned" detection logic.
348 */
349static int
350free_pages_bulk(struct zone *zone, int count,
351 struct list_head *list, unsigned int order)
352{
353 unsigned long flags;
354 struct page *page = NULL;
355 int ret = 0;
356
357 spin_lock_irqsave(&zone->lock, flags);
358 zone->all_unreclaimable = 0;
359 zone->pages_scanned = 0;
360 while (!list_empty(list) && count--) {
361 page = list_entry(list->prev, struct page, lru);
362 /* have to delete it as __free_pages_bulk list manipulates */
363 list_del(&page->lru);
364 __free_pages_bulk(page, zone, order);
365 ret++;
366 }
367 spin_unlock_irqrestore(&zone->lock, flags);
368 return ret;
369}
370
371void __free_pages_ok(struct page *page, unsigned int order)
372{
373 LIST_HEAD(list);
374 int i;
375
376 arch_free_page(page, order);
377
378 mod_page_state(pgfree, 1 << order);
379
380#ifndef CONFIG_MMU
381 if (order > 0)
382 for (i = 1 ; i < (1 << order) ; ++i)
383 __put_page(page + i);
384#endif
385
386 for (i = 0 ; i < (1 << order) ; ++i)
387 free_pages_check(__FUNCTION__, page + i);
388 list_add(&page->lru, &list);
389 kernel_map_pages(page, 1<<order, 0);
390 free_pages_bulk(page_zone(page), 1, &list, order);
391}
392
393
394/*
395 * The order of subdivision here is critical for the IO subsystem.
396 * Please do not alter this order without good reasons and regression
397 * testing. Specifically, as large blocks of memory are subdivided,
398 * the order in which smaller blocks are delivered depends on the order
399 * they're subdivided in this function. This is the primary factor
400 * influencing the order in which pages are delivered to the IO
401 * subsystem according to empirical testing, and this is also justified
402 * by considering the behavior of a buddy system containing a single
403 * large block of memory acted on by a series of small allocations.
404 * This behavior is a critical factor in sglist merging's success.
405 *
406 * -- wli
407 */
408static inline struct page *
409expand(struct zone *zone, struct page *page,
410 int low, int high, struct free_area *area)
411{
412 unsigned long size = 1 << high;
413
414 while (high > low) {
415 area--;
416 high--;
417 size >>= 1;
418 BUG_ON(bad_range(zone, &page[size]));
419 list_add(&page[size].lru, &area->free_list);
420 area->nr_free++;
421 set_page_order(&page[size], high);
422 }
423 return page;
424}
425
426void set_page_refs(struct page *page, int order)
427{
428#ifdef CONFIG_MMU
429 set_page_count(page, 1);
430#else
431 int i;
432
433 /*
434 * We need to reference all the pages for this order, otherwise if
435 * anyone accesses one of the pages with (get/put) it will be freed.
436 * - eg: access_process_vm()
437 */
438 for (i = 0; i < (1 << order); i++)
439 set_page_count(page + i, 1);
440#endif /* CONFIG_MMU */
441}
442
443/*
444 * This page is about to be returned from the page allocator
445 */
446static void prep_new_page(struct page *page, int order)
447{
448 if (page->mapping || page_mapcount(page) ||
449 (page->flags & (
450 1 << PG_private |
451 1 << PG_locked |
452 1 << PG_lru |
453 1 << PG_active |
454 1 << PG_dirty |
455 1 << PG_reclaim |
456 1 << PG_swapcache |
457 1 << PG_writeback )))
458 bad_page(__FUNCTION__, page);
459
460 page->flags &= ~(1 << PG_uptodate | 1 << PG_error |
461 1 << PG_referenced | 1 << PG_arch_1 |
462 1 << PG_checked | 1 << PG_mappedtodisk);
463 page->private = 0;
464 set_page_refs(page, order);
465 kernel_map_pages(page, 1 << order, 1);
466}
467
468/*
469 * Do the hard work of removing an element from the buddy allocator.
470 * Call me with the zone->lock already held.
471 */
472static struct page *__rmqueue(struct zone *zone, unsigned int order)
473{
474 struct free_area * area;
475 unsigned int current_order;
476 struct page *page;
477
478 for (current_order = order; current_order < MAX_ORDER; ++current_order) {
479 area = zone->free_area + current_order;
480 if (list_empty(&area->free_list))
481 continue;
482
483 page = list_entry(area->free_list.next, struct page, lru);
484 list_del(&page->lru);
485 rmv_page_order(page);
486 area->nr_free--;
487 zone->free_pages -= 1UL << order;
488 return expand(zone, page, order, current_order, area);
489 }
490
491 return NULL;
492}
493
494/*
495 * Obtain a specified number of elements from the buddy allocator, all under
496 * a single hold of the lock, for efficiency. Add them to the supplied list.
497 * Returns the number of new pages which were placed at *list.
498 */
499static int rmqueue_bulk(struct zone *zone, unsigned int order,
500 unsigned long count, struct list_head *list)
501{
502 unsigned long flags;
503 int i;
504 int allocated = 0;
505 struct page *page;
506
507 spin_lock_irqsave(&zone->lock, flags);
508 for (i = 0; i < count; ++i) {
509 page = __rmqueue(zone, order);
510 if (page == NULL)
511 break;
512 allocated++;
513 list_add_tail(&page->lru, list);
514 }
515 spin_unlock_irqrestore(&zone->lock, flags);
516 return allocated;
517}
518
519#if defined(CONFIG_PM) || defined(CONFIG_HOTPLUG_CPU)
520static void __drain_pages(unsigned int cpu)
521{
522 struct zone *zone;
523 int i;
524
525 for_each_zone(zone) {
526 struct per_cpu_pageset *pset;
527
e7c8d5c9 528 pset = zone_pcp(zone, cpu);
1da177e4
LT
529 for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
530 struct per_cpu_pages *pcp;
531
532 pcp = &pset->pcp[i];
533 pcp->count -= free_pages_bulk(zone, pcp->count,
534 &pcp->list, 0);
535 }
536 }
537}
538#endif /* CONFIG_PM || CONFIG_HOTPLUG_CPU */
539
540#ifdef CONFIG_PM
541
542void mark_free_pages(struct zone *zone)
543{
544 unsigned long zone_pfn, flags;
545 int order;
546 struct list_head *curr;
547
548 if (!zone->spanned_pages)
549 return;
550
551 spin_lock_irqsave(&zone->lock, flags);
552 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
553 ClearPageNosaveFree(pfn_to_page(zone_pfn + zone->zone_start_pfn));
554
555 for (order = MAX_ORDER - 1; order >= 0; --order)
556 list_for_each(curr, &zone->free_area[order].free_list) {
557 unsigned long start_pfn, i;
558
559 start_pfn = page_to_pfn(list_entry(curr, struct page, lru));
560
561 for (i=0; i < (1<<order); i++)
562 SetPageNosaveFree(pfn_to_page(start_pfn+i));
563 }
564 spin_unlock_irqrestore(&zone->lock, flags);
565}
566
567/*
568 * Spill all of this CPU's per-cpu pages back into the buddy allocator.
569 */
570void drain_local_pages(void)
571{
572 unsigned long flags;
573
574 local_irq_save(flags);
575 __drain_pages(smp_processor_id());
576 local_irq_restore(flags);
577}
578#endif /* CONFIG_PM */
579
580static void zone_statistics(struct zonelist *zonelist, struct zone *z)
581{
582#ifdef CONFIG_NUMA
583 unsigned long flags;
584 int cpu;
585 pg_data_t *pg = z->zone_pgdat;
586 pg_data_t *orig = zonelist->zones[0]->zone_pgdat;
587 struct per_cpu_pageset *p;
588
589 local_irq_save(flags);
590 cpu = smp_processor_id();
e7c8d5c9 591 p = zone_pcp(z,cpu);
1da177e4 592 if (pg == orig) {
e7c8d5c9 593 p->numa_hit++;
1da177e4
LT
594 } else {
595 p->numa_miss++;
e7c8d5c9 596 zone_pcp(zonelist->zones[0], cpu)->numa_foreign++;
1da177e4
LT
597 }
598 if (pg == NODE_DATA(numa_node_id()))
599 p->local_node++;
600 else
601 p->other_node++;
602 local_irq_restore(flags);
603#endif
604}
605
606/*
607 * Free a 0-order page
608 */
609static void FASTCALL(free_hot_cold_page(struct page *page, int cold));
610static void fastcall free_hot_cold_page(struct page *page, int cold)
611{
612 struct zone *zone = page_zone(page);
613 struct per_cpu_pages *pcp;
614 unsigned long flags;
615
616 arch_free_page(page, 0);
617
618 kernel_map_pages(page, 1, 0);
619 inc_page_state(pgfree);
620 if (PageAnon(page))
621 page->mapping = NULL;
622 free_pages_check(__FUNCTION__, page);
e7c8d5c9 623 pcp = &zone_pcp(zone, get_cpu())->pcp[cold];
1da177e4
LT
624 local_irq_save(flags);
625 if (pcp->count >= pcp->high)
626 pcp->count -= free_pages_bulk(zone, pcp->batch, &pcp->list, 0);
627 list_add(&page->lru, &pcp->list);
628 pcp->count++;
629 local_irq_restore(flags);
630 put_cpu();
631}
632
633void fastcall free_hot_page(struct page *page)
634{
635 free_hot_cold_page(page, 0);
636}
637
638void fastcall free_cold_page(struct page *page)
639{
640 free_hot_cold_page(page, 1);
641}
642
643static inline void prep_zero_page(struct page *page, int order, unsigned int __nocast gfp_flags)
644{
645 int i;
646
647 BUG_ON((gfp_flags & (__GFP_WAIT | __GFP_HIGHMEM)) == __GFP_HIGHMEM);
648 for(i = 0; i < (1 << order); i++)
649 clear_highpage(page + i);
650}
651
652/*
653 * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
654 * we cheat by calling it from here, in the order > 0 path. Saves a branch
655 * or two.
656 */
657static struct page *
658buffered_rmqueue(struct zone *zone, int order, unsigned int __nocast gfp_flags)
659{
660 unsigned long flags;
661 struct page *page = NULL;
662 int cold = !!(gfp_flags & __GFP_COLD);
663
664 if (order == 0) {
665 struct per_cpu_pages *pcp;
666
e7c8d5c9 667 pcp = &zone_pcp(zone, get_cpu())->pcp[cold];
1da177e4
LT
668 local_irq_save(flags);
669 if (pcp->count <= pcp->low)
670 pcp->count += rmqueue_bulk(zone, 0,
671 pcp->batch, &pcp->list);
672 if (pcp->count) {
673 page = list_entry(pcp->list.next, struct page, lru);
674 list_del(&page->lru);
675 pcp->count--;
676 }
677 local_irq_restore(flags);
678 put_cpu();
679 }
680
681 if (page == NULL) {
682 spin_lock_irqsave(&zone->lock, flags);
683 page = __rmqueue(zone, order);
684 spin_unlock_irqrestore(&zone->lock, flags);
685 }
686
687 if (page != NULL) {
688 BUG_ON(bad_range(zone, page));
689 mod_page_state_zone(zone, pgalloc, 1 << order);
690 prep_new_page(page, order);
691
692 if (gfp_flags & __GFP_ZERO)
693 prep_zero_page(page, order, gfp_flags);
694
695 if (order && (gfp_flags & __GFP_COMP))
696 prep_compound_page(page, order);
697 }
698 return page;
699}
700
701/*
702 * Return 1 if free pages are above 'mark'. This takes into account the order
703 * of the allocation.
704 */
705int zone_watermark_ok(struct zone *z, int order, unsigned long mark,
706 int classzone_idx, int can_try_harder, int gfp_high)
707{
708 /* free_pages my go negative - that's OK */
709 long min = mark, free_pages = z->free_pages - (1 << order) + 1;
710 int o;
711
712 if (gfp_high)
713 min -= min / 2;
714 if (can_try_harder)
715 min -= min / 4;
716
717 if (free_pages <= min + z->lowmem_reserve[classzone_idx])
718 return 0;
719 for (o = 0; o < order; o++) {
720 /* At the next order, this order's pages become unavailable */
721 free_pages -= z->free_area[o].nr_free << o;
722
723 /* Require fewer higher order pages to be free */
724 min >>= 1;
725
726 if (free_pages <= min)
727 return 0;
728 }
729 return 1;
730}
731
753ee728
MH
732static inline int
733should_reclaim_zone(struct zone *z, unsigned int gfp_mask)
734{
735 if (!z->reclaim_pages)
736 return 0;
0c35bbad
MH
737 if (gfp_mask & __GFP_NORECLAIM)
738 return 0;
753ee728
MH
739 return 1;
740}
741
1da177e4
LT
742/*
743 * This is the 'heart' of the zoned buddy allocator.
744 */
745struct page * fastcall
746__alloc_pages(unsigned int __nocast gfp_mask, unsigned int order,
747 struct zonelist *zonelist)
748{
749 const int wait = gfp_mask & __GFP_WAIT;
750 struct zone **zones, *z;
751 struct page *page;
752 struct reclaim_state reclaim_state;
753 struct task_struct *p = current;
754 int i;
755 int classzone_idx;
756 int do_retry;
757 int can_try_harder;
758 int did_some_progress;
759
760 might_sleep_if(wait);
761
762 /*
763 * The caller may dip into page reserves a bit more if the caller
764 * cannot run direct reclaim, or is the caller has realtime scheduling
765 * policy
766 */
767 can_try_harder = (unlikely(rt_task(p)) && !in_interrupt()) || !wait;
768
769 zones = zonelist->zones; /* the list of zones suitable for gfp_mask */
770
771 if (unlikely(zones[0] == NULL)) {
772 /* Should this ever happen?? */
773 return NULL;
774 }
775
776 classzone_idx = zone_idx(zones[0]);
777
753ee728 778restart:
1da177e4
LT
779 /* Go through the zonelist once, looking for a zone with enough free */
780 for (i = 0; (z = zones[i]) != NULL; i++) {
753ee728 781 int do_reclaim = should_reclaim_zone(z, gfp_mask);
1da177e4
LT
782
783 if (!cpuset_zone_allowed(z))
784 continue;
785
753ee728
MH
786 /*
787 * If the zone is to attempt early page reclaim then this loop
788 * will try to reclaim pages and check the watermark a second
789 * time before giving up and falling back to the next zone.
790 */
791zone_reclaim_retry:
792 if (!zone_watermark_ok(z, order, z->pages_low,
793 classzone_idx, 0, 0)) {
794 if (!do_reclaim)
795 continue;
796 else {
797 zone_reclaim(z, gfp_mask, order);
798 /* Only try reclaim once */
799 do_reclaim = 0;
800 goto zone_reclaim_retry;
801 }
802 }
803
1da177e4
LT
804 page = buffered_rmqueue(z, order, gfp_mask);
805 if (page)
806 goto got_pg;
807 }
808
809 for (i = 0; (z = zones[i]) != NULL; i++)
810 wakeup_kswapd(z, order);
811
812 /*
813 * Go through the zonelist again. Let __GFP_HIGH and allocations
814 * coming from realtime tasks to go deeper into reserves
815 *
816 * This is the last chance, in general, before the goto nopage.
817 * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
818 */
819 for (i = 0; (z = zones[i]) != NULL; i++) {
820 if (!zone_watermark_ok(z, order, z->pages_min,
821 classzone_idx, can_try_harder,
822 gfp_mask & __GFP_HIGH))
823 continue;
824
825 if (wait && !cpuset_zone_allowed(z))
826 continue;
827
828 page = buffered_rmqueue(z, order, gfp_mask);
829 if (page)
830 goto got_pg;
831 }
832
833 /* This allocation should allow future memory freeing. */
b84a35be
NP
834
835 if (((p->flags & PF_MEMALLOC) || unlikely(test_thread_flag(TIF_MEMDIE)))
836 && !in_interrupt()) {
837 if (!(gfp_mask & __GFP_NOMEMALLOC)) {
838 /* go through the zonelist yet again, ignoring mins */
839 for (i = 0; (z = zones[i]) != NULL; i++) {
840 if (!cpuset_zone_allowed(z))
841 continue;
842 page = buffered_rmqueue(z, order, gfp_mask);
843 if (page)
844 goto got_pg;
845 }
1da177e4
LT
846 }
847 goto nopage;
848 }
849
850 /* Atomic allocations - we can't balance anything */
851 if (!wait)
852 goto nopage;
853
854rebalance:
855 cond_resched();
856
857 /* We now go into synchronous reclaim */
858 p->flags |= PF_MEMALLOC;
859 reclaim_state.reclaimed_slab = 0;
860 p->reclaim_state = &reclaim_state;
861
1ad539b2 862 did_some_progress = try_to_free_pages(zones, gfp_mask);
1da177e4
LT
863
864 p->reclaim_state = NULL;
865 p->flags &= ~PF_MEMALLOC;
866
867 cond_resched();
868
869 if (likely(did_some_progress)) {
870 /*
871 * Go through the zonelist yet one more time, keep
872 * very high watermark here, this is only to catch
873 * a parallel oom killing, we must fail if we're still
874 * under heavy pressure.
875 */
876 for (i = 0; (z = zones[i]) != NULL; i++) {
877 if (!zone_watermark_ok(z, order, z->pages_min,
878 classzone_idx, can_try_harder,
879 gfp_mask & __GFP_HIGH))
880 continue;
881
882 if (!cpuset_zone_allowed(z))
883 continue;
884
885 page = buffered_rmqueue(z, order, gfp_mask);
886 if (page)
887 goto got_pg;
888 }
889 } else if ((gfp_mask & __GFP_FS) && !(gfp_mask & __GFP_NORETRY)) {
890 /*
891 * Go through the zonelist yet one more time, keep
892 * very high watermark here, this is only to catch
893 * a parallel oom killing, we must fail if we're still
894 * under heavy pressure.
895 */
896 for (i = 0; (z = zones[i]) != NULL; i++) {
897 if (!zone_watermark_ok(z, order, z->pages_high,
898 classzone_idx, 0, 0))
899 continue;
900
901 if (!cpuset_zone_allowed(z))
902 continue;
903
904 page = buffered_rmqueue(z, order, gfp_mask);
905 if (page)
906 goto got_pg;
907 }
908
909 out_of_memory(gfp_mask);
910 goto restart;
911 }
912
913 /*
914 * Don't let big-order allocations loop unless the caller explicitly
915 * requests that. Wait for some write requests to complete then retry.
916 *
917 * In this implementation, __GFP_REPEAT means __GFP_NOFAIL for order
918 * <= 3, but that may not be true in other implementations.
919 */
920 do_retry = 0;
921 if (!(gfp_mask & __GFP_NORETRY)) {
922 if ((order <= 3) || (gfp_mask & __GFP_REPEAT))
923 do_retry = 1;
924 if (gfp_mask & __GFP_NOFAIL)
925 do_retry = 1;
926 }
927 if (do_retry) {
928 blk_congestion_wait(WRITE, HZ/50);
929 goto rebalance;
930 }
931
932nopage:
933 if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) {
934 printk(KERN_WARNING "%s: page allocation failure."
935 " order:%d, mode:0x%x\n",
936 p->comm, order, gfp_mask);
937 dump_stack();
578c2fd6 938 show_mem();
1da177e4
LT
939 }
940 return NULL;
941got_pg:
942 zone_statistics(zonelist, z);
943 return page;
944}
945
946EXPORT_SYMBOL(__alloc_pages);
947
948/*
949 * Common helper functions.
950 */
951fastcall unsigned long __get_free_pages(unsigned int __nocast gfp_mask, unsigned int order)
952{
953 struct page * page;
954 page = alloc_pages(gfp_mask, order);
955 if (!page)
956 return 0;
957 return (unsigned long) page_address(page);
958}
959
960EXPORT_SYMBOL(__get_free_pages);
961
962fastcall unsigned long get_zeroed_page(unsigned int __nocast gfp_mask)
963{
964 struct page * page;
965
966 /*
967 * get_zeroed_page() returns a 32-bit address, which cannot represent
968 * a highmem page
969 */
970 BUG_ON(gfp_mask & __GFP_HIGHMEM);
971
972 page = alloc_pages(gfp_mask | __GFP_ZERO, 0);
973 if (page)
974 return (unsigned long) page_address(page);
975 return 0;
976}
977
978EXPORT_SYMBOL(get_zeroed_page);
979
980void __pagevec_free(struct pagevec *pvec)
981{
982 int i = pagevec_count(pvec);
983
984 while (--i >= 0)
985 free_hot_cold_page(pvec->pages[i], pvec->cold);
986}
987
988fastcall void __free_pages(struct page *page, unsigned int order)
989{
990 if (!PageReserved(page) && put_page_testzero(page)) {
991 if (order == 0)
992 free_hot_page(page);
993 else
994 __free_pages_ok(page, order);
995 }
996}
997
998EXPORT_SYMBOL(__free_pages);
999
1000fastcall void free_pages(unsigned long addr, unsigned int order)
1001{
1002 if (addr != 0) {
1003 BUG_ON(!virt_addr_valid((void *)addr));
1004 __free_pages(virt_to_page((void *)addr), order);
1005 }
1006}
1007
1008EXPORT_SYMBOL(free_pages);
1009
1010/*
1011 * Total amount of free (allocatable) RAM:
1012 */
1013unsigned int nr_free_pages(void)
1014{
1015 unsigned int sum = 0;
1016 struct zone *zone;
1017
1018 for_each_zone(zone)
1019 sum += zone->free_pages;
1020
1021 return sum;
1022}
1023
1024EXPORT_SYMBOL(nr_free_pages);
1025
1026#ifdef CONFIG_NUMA
1027unsigned int nr_free_pages_pgdat(pg_data_t *pgdat)
1028{
1029 unsigned int i, sum = 0;
1030
1031 for (i = 0; i < MAX_NR_ZONES; i++)
1032 sum += pgdat->node_zones[i].free_pages;
1033
1034 return sum;
1035}
1036#endif
1037
1038static unsigned int nr_free_zone_pages(int offset)
1039{
1040 pg_data_t *pgdat;
1041 unsigned int sum = 0;
1042
1043 for_each_pgdat(pgdat) {
1044 struct zonelist *zonelist = pgdat->node_zonelists + offset;
1045 struct zone **zonep = zonelist->zones;
1046 struct zone *zone;
1047
1048 for (zone = *zonep++; zone; zone = *zonep++) {
1049 unsigned long size = zone->present_pages;
1050 unsigned long high = zone->pages_high;
1051 if (size > high)
1052 sum += size - high;
1053 }
1054 }
1055
1056 return sum;
1057}
1058
1059/*
1060 * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
1061 */
1062unsigned int nr_free_buffer_pages(void)
1063{
1064 return nr_free_zone_pages(GFP_USER & GFP_ZONEMASK);
1065}
1066
1067/*
1068 * Amount of free RAM allocatable within all zones
1069 */
1070unsigned int nr_free_pagecache_pages(void)
1071{
1072 return nr_free_zone_pages(GFP_HIGHUSER & GFP_ZONEMASK);
1073}
1074
1075#ifdef CONFIG_HIGHMEM
1076unsigned int nr_free_highpages (void)
1077{
1078 pg_data_t *pgdat;
1079 unsigned int pages = 0;
1080
1081 for_each_pgdat(pgdat)
1082 pages += pgdat->node_zones[ZONE_HIGHMEM].free_pages;
1083
1084 return pages;
1085}
1086#endif
1087
1088#ifdef CONFIG_NUMA
1089static void show_node(struct zone *zone)
1090{
1091 printk("Node %d ", zone->zone_pgdat->node_id);
1092}
1093#else
1094#define show_node(zone) do { } while (0)
1095#endif
1096
1097/*
1098 * Accumulate the page_state information across all CPUs.
1099 * The result is unavoidably approximate - it can change
1100 * during and after execution of this function.
1101 */
1102static DEFINE_PER_CPU(struct page_state, page_states) = {0};
1103
1104atomic_t nr_pagecache = ATOMIC_INIT(0);
1105EXPORT_SYMBOL(nr_pagecache);
1106#ifdef CONFIG_SMP
1107DEFINE_PER_CPU(long, nr_pagecache_local) = 0;
1108#endif
1109
1110void __get_page_state(struct page_state *ret, int nr)
1111{
1112 int cpu = 0;
1113
1114 memset(ret, 0, sizeof(*ret));
1115
1116 cpu = first_cpu(cpu_online_map);
1117 while (cpu < NR_CPUS) {
1118 unsigned long *in, *out, off;
1119
1120 in = (unsigned long *)&per_cpu(page_states, cpu);
1121
1122 cpu = next_cpu(cpu, cpu_online_map);
1123
1124 if (cpu < NR_CPUS)
1125 prefetch(&per_cpu(page_states, cpu));
1126
1127 out = (unsigned long *)ret;
1128 for (off = 0; off < nr; off++)
1129 *out++ += *in++;
1130 }
1131}
1132
1133void get_page_state(struct page_state *ret)
1134{
1135 int nr;
1136
1137 nr = offsetof(struct page_state, GET_PAGE_STATE_LAST);
1138 nr /= sizeof(unsigned long);
1139
1140 __get_page_state(ret, nr + 1);
1141}
1142
1143void get_full_page_state(struct page_state *ret)
1144{
1145 __get_page_state(ret, sizeof(*ret) / sizeof(unsigned long));
1146}
1147
c2f29ea1 1148unsigned long __read_page_state(unsigned long offset)
1da177e4
LT
1149{
1150 unsigned long ret = 0;
1151 int cpu;
1152
1153 for_each_online_cpu(cpu) {
1154 unsigned long in;
1155
1156 in = (unsigned long)&per_cpu(page_states, cpu) + offset;
1157 ret += *((unsigned long *)in);
1158 }
1159 return ret;
1160}
1161
83e5d8f7 1162void __mod_page_state(unsigned long offset, unsigned long delta)
1da177e4
LT
1163{
1164 unsigned long flags;
1165 void* ptr;
1166
1167 local_irq_save(flags);
1168 ptr = &__get_cpu_var(page_states);
1169 *(unsigned long*)(ptr + offset) += delta;
1170 local_irq_restore(flags);
1171}
1172
1173EXPORT_SYMBOL(__mod_page_state);
1174
1175void __get_zone_counts(unsigned long *active, unsigned long *inactive,
1176 unsigned long *free, struct pglist_data *pgdat)
1177{
1178 struct zone *zones = pgdat->node_zones;
1179 int i;
1180
1181 *active = 0;
1182 *inactive = 0;
1183 *free = 0;
1184 for (i = 0; i < MAX_NR_ZONES; i++) {
1185 *active += zones[i].nr_active;
1186 *inactive += zones[i].nr_inactive;
1187 *free += zones[i].free_pages;
1188 }
1189}
1190
1191void get_zone_counts(unsigned long *active,
1192 unsigned long *inactive, unsigned long *free)
1193{
1194 struct pglist_data *pgdat;
1195
1196 *active = 0;
1197 *inactive = 0;
1198 *free = 0;
1199 for_each_pgdat(pgdat) {
1200 unsigned long l, m, n;
1201 __get_zone_counts(&l, &m, &n, pgdat);
1202 *active += l;
1203 *inactive += m;
1204 *free += n;
1205 }
1206}
1207
1208void si_meminfo(struct sysinfo *val)
1209{
1210 val->totalram = totalram_pages;
1211 val->sharedram = 0;
1212 val->freeram = nr_free_pages();
1213 val->bufferram = nr_blockdev_pages();
1214#ifdef CONFIG_HIGHMEM
1215 val->totalhigh = totalhigh_pages;
1216 val->freehigh = nr_free_highpages();
1217#else
1218 val->totalhigh = 0;
1219 val->freehigh = 0;
1220#endif
1221 val->mem_unit = PAGE_SIZE;
1222}
1223
1224EXPORT_SYMBOL(si_meminfo);
1225
1226#ifdef CONFIG_NUMA
1227void si_meminfo_node(struct sysinfo *val, int nid)
1228{
1229 pg_data_t *pgdat = NODE_DATA(nid);
1230
1231 val->totalram = pgdat->node_present_pages;
1232 val->freeram = nr_free_pages_pgdat(pgdat);
1233 val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].present_pages;
1234 val->freehigh = pgdat->node_zones[ZONE_HIGHMEM].free_pages;
1235 val->mem_unit = PAGE_SIZE;
1236}
1237#endif
1238
1239#define K(x) ((x) << (PAGE_SHIFT-10))
1240
1241/*
1242 * Show free area list (used inside shift_scroll-lock stuff)
1243 * We also calculate the percentage fragmentation. We do this by counting the
1244 * memory on each free list with the exception of the first item on the list.
1245 */
1246void show_free_areas(void)
1247{
1248 struct page_state ps;
1249 int cpu, temperature;
1250 unsigned long active;
1251 unsigned long inactive;
1252 unsigned long free;
1253 struct zone *zone;
1254
1255 for_each_zone(zone) {
1256 show_node(zone);
1257 printk("%s per-cpu:", zone->name);
1258
1259 if (!zone->present_pages) {
1260 printk(" empty\n");
1261 continue;
1262 } else
1263 printk("\n");
1264
1265 for (cpu = 0; cpu < NR_CPUS; ++cpu) {
1266 struct per_cpu_pageset *pageset;
1267
1268 if (!cpu_possible(cpu))
1269 continue;
1270
e7c8d5c9 1271 pageset = zone_pcp(zone, cpu);
1da177e4
LT
1272
1273 for (temperature = 0; temperature < 2; temperature++)
1274 printk("cpu %d %s: low %d, high %d, batch %d\n",
1275 cpu,
1276 temperature ? "cold" : "hot",
1277 pageset->pcp[temperature].low,
1278 pageset->pcp[temperature].high,
1279 pageset->pcp[temperature].batch);
1280 }
1281 }
1282
1283 get_page_state(&ps);
1284 get_zone_counts(&active, &inactive, &free);
1285
1286 printk("\nFree pages: %11ukB (%ukB HighMem)\n",
1287 K(nr_free_pages()),
1288 K(nr_free_highpages()));
1289
1290 printk("Active:%lu inactive:%lu dirty:%lu writeback:%lu "
1291 "unstable:%lu free:%u slab:%lu mapped:%lu pagetables:%lu\n",
1292 active,
1293 inactive,
1294 ps.nr_dirty,
1295 ps.nr_writeback,
1296 ps.nr_unstable,
1297 nr_free_pages(),
1298 ps.nr_slab,
1299 ps.nr_mapped,
1300 ps.nr_page_table_pages);
1301
1302 for_each_zone(zone) {
1303 int i;
1304
1305 show_node(zone);
1306 printk("%s"
1307 " free:%lukB"
1308 " min:%lukB"
1309 " low:%lukB"
1310 " high:%lukB"
1311 " active:%lukB"
1312 " inactive:%lukB"
1313 " present:%lukB"
1314 " pages_scanned:%lu"
1315 " all_unreclaimable? %s"
1316 "\n",
1317 zone->name,
1318 K(zone->free_pages),
1319 K(zone->pages_min),
1320 K(zone->pages_low),
1321 K(zone->pages_high),
1322 K(zone->nr_active),
1323 K(zone->nr_inactive),
1324 K(zone->present_pages),
1325 zone->pages_scanned,
1326 (zone->all_unreclaimable ? "yes" : "no")
1327 );
1328 printk("lowmem_reserve[]:");
1329 for (i = 0; i < MAX_NR_ZONES; i++)
1330 printk(" %lu", zone->lowmem_reserve[i]);
1331 printk("\n");
1332 }
1333
1334 for_each_zone(zone) {
1335 unsigned long nr, flags, order, total = 0;
1336
1337 show_node(zone);
1338 printk("%s: ", zone->name);
1339 if (!zone->present_pages) {
1340 printk("empty\n");
1341 continue;
1342 }
1343
1344 spin_lock_irqsave(&zone->lock, flags);
1345 for (order = 0; order < MAX_ORDER; order++) {
1346 nr = zone->free_area[order].nr_free;
1347 total += nr << order;
1348 printk("%lu*%lukB ", nr, K(1UL) << order);
1349 }
1350 spin_unlock_irqrestore(&zone->lock, flags);
1351 printk("= %lukB\n", K(total));
1352 }
1353
1354 show_swap_cache_info();
1355}
1356
1357/*
1358 * Builds allocation fallback zone lists.
1359 */
1360static int __init build_zonelists_node(pg_data_t *pgdat, struct zonelist *zonelist, int j, int k)
1361{
1362 switch (k) {
1363 struct zone *zone;
1364 default:
1365 BUG();
1366 case ZONE_HIGHMEM:
1367 zone = pgdat->node_zones + ZONE_HIGHMEM;
1368 if (zone->present_pages) {
1369#ifndef CONFIG_HIGHMEM
1370 BUG();
1371#endif
1372 zonelist->zones[j++] = zone;
1373 }
1374 case ZONE_NORMAL:
1375 zone = pgdat->node_zones + ZONE_NORMAL;
1376 if (zone->present_pages)
1377 zonelist->zones[j++] = zone;
1378 case ZONE_DMA:
1379 zone = pgdat->node_zones + ZONE_DMA;
1380 if (zone->present_pages)
1381 zonelist->zones[j++] = zone;
1382 }
1383
1384 return j;
1385}
1386
1387#ifdef CONFIG_NUMA
1388#define MAX_NODE_LOAD (num_online_nodes())
1389static int __initdata node_load[MAX_NUMNODES];
1390/**
4dc3b16b 1391 * find_next_best_node - find the next node that should appear in a given node's fallback list
1da177e4
LT
1392 * @node: node whose fallback list we're appending
1393 * @used_node_mask: nodemask_t of already used nodes
1394 *
1395 * We use a number of factors to determine which is the next node that should
1396 * appear on a given node's fallback list. The node should not have appeared
1397 * already in @node's fallback list, and it should be the next closest node
1398 * according to the distance array (which contains arbitrary distance values
1399 * from each node to each node in the system), and should also prefer nodes
1400 * with no CPUs, since presumably they'll have very little allocation pressure
1401 * on them otherwise.
1402 * It returns -1 if no node is found.
1403 */
1404static int __init find_next_best_node(int node, nodemask_t *used_node_mask)
1405{
1406 int i, n, val;
1407 int min_val = INT_MAX;
1408 int best_node = -1;
1409
1410 for_each_online_node(i) {
1411 cpumask_t tmp;
1412
1413 /* Start from local node */
1414 n = (node+i) % num_online_nodes();
1415
1416 /* Don't want a node to appear more than once */
1417 if (node_isset(n, *used_node_mask))
1418 continue;
1419
1420 /* Use the local node if we haven't already */
1421 if (!node_isset(node, *used_node_mask)) {
1422 best_node = node;
1423 break;
1424 }
1425
1426 /* Use the distance array to find the distance */
1427 val = node_distance(node, n);
1428
1429 /* Give preference to headless and unused nodes */
1430 tmp = node_to_cpumask(n);
1431 if (!cpus_empty(tmp))
1432 val += PENALTY_FOR_NODE_WITH_CPUS;
1433
1434 /* Slight preference for less loaded node */
1435 val *= (MAX_NODE_LOAD*MAX_NUMNODES);
1436 val += node_load[n];
1437
1438 if (val < min_val) {
1439 min_val = val;
1440 best_node = n;
1441 }
1442 }
1443
1444 if (best_node >= 0)
1445 node_set(best_node, *used_node_mask);
1446
1447 return best_node;
1448}
1449
1450static void __init build_zonelists(pg_data_t *pgdat)
1451{
1452 int i, j, k, node, local_node;
1453 int prev_node, load;
1454 struct zonelist *zonelist;
1455 nodemask_t used_mask;
1456
1457 /* initialize zonelists */
1458 for (i = 0; i < GFP_ZONETYPES; i++) {
1459 zonelist = pgdat->node_zonelists + i;
1460 zonelist->zones[0] = NULL;
1461 }
1462
1463 /* NUMA-aware ordering of nodes */
1464 local_node = pgdat->node_id;
1465 load = num_online_nodes();
1466 prev_node = local_node;
1467 nodes_clear(used_mask);
1468 while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
1469 /*
1470 * We don't want to pressure a particular node.
1471 * So adding penalty to the first node in same
1472 * distance group to make it round-robin.
1473 */
1474 if (node_distance(local_node, node) !=
1475 node_distance(local_node, prev_node))
1476 node_load[node] += load;
1477 prev_node = node;
1478 load--;
1479 for (i = 0; i < GFP_ZONETYPES; i++) {
1480 zonelist = pgdat->node_zonelists + i;
1481 for (j = 0; zonelist->zones[j] != NULL; j++);
1482
1483 k = ZONE_NORMAL;
1484 if (i & __GFP_HIGHMEM)
1485 k = ZONE_HIGHMEM;
1486 if (i & __GFP_DMA)
1487 k = ZONE_DMA;
1488
1489 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1490 zonelist->zones[j] = NULL;
1491 }
1492 }
1493}
1494
1495#else /* CONFIG_NUMA */
1496
1497static void __init build_zonelists(pg_data_t *pgdat)
1498{
1499 int i, j, k, node, local_node;
1500
1501 local_node = pgdat->node_id;
1502 for (i = 0; i < GFP_ZONETYPES; i++) {
1503 struct zonelist *zonelist;
1504
1505 zonelist = pgdat->node_zonelists + i;
1506
1507 j = 0;
1508 k = ZONE_NORMAL;
1509 if (i & __GFP_HIGHMEM)
1510 k = ZONE_HIGHMEM;
1511 if (i & __GFP_DMA)
1512 k = ZONE_DMA;
1513
1514 j = build_zonelists_node(pgdat, zonelist, j, k);
1515 /*
1516 * Now we build the zonelist so that it contains the zones
1517 * of all the other nodes.
1518 * We don't want to pressure a particular node, so when
1519 * building the zones for node N, we make sure that the
1520 * zones coming right after the local ones are those from
1521 * node N+1 (modulo N)
1522 */
1523 for (node = local_node + 1; node < MAX_NUMNODES; node++) {
1524 if (!node_online(node))
1525 continue;
1526 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1527 }
1528 for (node = 0; node < local_node; node++) {
1529 if (!node_online(node))
1530 continue;
1531 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1532 }
1533
1534 zonelist->zones[j] = NULL;
1535 }
1536}
1537
1538#endif /* CONFIG_NUMA */
1539
1540void __init build_all_zonelists(void)
1541{
1542 int i;
1543
1544 for_each_online_node(i)
1545 build_zonelists(NODE_DATA(i));
1546 printk("Built %i zonelists\n", num_online_nodes());
1547 cpuset_init_current_mems_allowed();
1548}
1549
1550/*
1551 * Helper functions to size the waitqueue hash table.
1552 * Essentially these want to choose hash table sizes sufficiently
1553 * large so that collisions trying to wait on pages are rare.
1554 * But in fact, the number of active page waitqueues on typical
1555 * systems is ridiculously low, less than 200. So this is even
1556 * conservative, even though it seems large.
1557 *
1558 * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
1559 * waitqueues, i.e. the size of the waitq table given the number of pages.
1560 */
1561#define PAGES_PER_WAITQUEUE 256
1562
1563static inline unsigned long wait_table_size(unsigned long pages)
1564{
1565 unsigned long size = 1;
1566
1567 pages /= PAGES_PER_WAITQUEUE;
1568
1569 while (size < pages)
1570 size <<= 1;
1571
1572 /*
1573 * Once we have dozens or even hundreds of threads sleeping
1574 * on IO we've got bigger problems than wait queue collision.
1575 * Limit the size of the wait table to a reasonable size.
1576 */
1577 size = min(size, 4096UL);
1578
1579 return max(size, 4UL);
1580}
1581
1582/*
1583 * This is an integer logarithm so that shifts can be used later
1584 * to extract the more random high bits from the multiplicative
1585 * hash function before the remainder is taken.
1586 */
1587static inline unsigned long wait_table_bits(unsigned long size)
1588{
1589 return ffz(~size);
1590}
1591
1592#define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
1593
1594static void __init calculate_zone_totalpages(struct pglist_data *pgdat,
1595 unsigned long *zones_size, unsigned long *zholes_size)
1596{
1597 unsigned long realtotalpages, totalpages = 0;
1598 int i;
1599
1600 for (i = 0; i < MAX_NR_ZONES; i++)
1601 totalpages += zones_size[i];
1602 pgdat->node_spanned_pages = totalpages;
1603
1604 realtotalpages = totalpages;
1605 if (zholes_size)
1606 for (i = 0; i < MAX_NR_ZONES; i++)
1607 realtotalpages -= zholes_size[i];
1608 pgdat->node_present_pages = realtotalpages;
1609 printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id, realtotalpages);
1610}
1611
1612
1613/*
1614 * Initially all pages are reserved - free ones are freed
1615 * up by free_all_bootmem() once the early boot process is
1616 * done. Non-atomic initialization, single-pass.
1617 */
1618void __init memmap_init_zone(unsigned long size, int nid, unsigned long zone,
1619 unsigned long start_pfn)
1620{
1621 struct page *start = pfn_to_page(start_pfn);
1622 struct page *page;
1623
1624 for (page = start; page < (start + size); page++) {
1625 set_page_zone(page, NODEZONE(nid, zone));
1626 set_page_count(page, 0);
1627 reset_page_mapcount(page);
1628 SetPageReserved(page);
1629 INIT_LIST_HEAD(&page->lru);
1630#ifdef WANT_PAGE_VIRTUAL
1631 /* The shift won't overflow because ZONE_NORMAL is below 4G. */
1632 if (!is_highmem_idx(zone))
1633 set_page_address(page, __va(start_pfn << PAGE_SHIFT));
1634#endif
1635 start_pfn++;
1636 }
1637}
1638
1639void zone_init_free_lists(struct pglist_data *pgdat, struct zone *zone,
1640 unsigned long size)
1641{
1642 int order;
1643 for (order = 0; order < MAX_ORDER ; order++) {
1644 INIT_LIST_HEAD(&zone->free_area[order].free_list);
1645 zone->free_area[order].nr_free = 0;
1646 }
1647}
1648
1649#ifndef __HAVE_ARCH_MEMMAP_INIT
1650#define memmap_init(size, nid, zone, start_pfn) \
1651 memmap_init_zone((size), (nid), (zone), (start_pfn))
1652#endif
1653
e7c8d5c9
CL
1654static int __devinit zone_batchsize(struct zone *zone)
1655{
1656 int batch;
1657
1658 /*
1659 * The per-cpu-pages pools are set to around 1000th of the
1660 * size of the zone. But no more than 1/4 of a meg - there's
1661 * no point in going beyond the size of L2 cache.
1662 *
1663 * OK, so we don't know how big the cache is. So guess.
1664 */
1665 batch = zone->present_pages / 1024;
1666 if (batch * PAGE_SIZE > 256 * 1024)
1667 batch = (256 * 1024) / PAGE_SIZE;
1668 batch /= 4; /* We effectively *= 4 below */
1669 if (batch < 1)
1670 batch = 1;
1671
1672 /*
1673 * Clamp the batch to a 2^n - 1 value. Having a power
1674 * of 2 value was found to be more likely to have
1675 * suboptimal cache aliasing properties in some cases.
1676 *
1677 * For example if 2 tasks are alternately allocating
1678 * batches of pages, one task can end up with a lot
1679 * of pages of one half of the possible page colors
1680 * and the other with pages of the other colors.
1681 */
1682 batch = (1 << fls(batch + batch/2)) - 1;
1683 return batch;
1684}
1685
1686#ifdef CONFIG_NUMA
1687/*
1688 * Dynamicaly allocate memory for the
1689 * per cpu pageset array in struct zone.
1690 */
1691static int __devinit process_zones(int cpu)
1692{
1693 struct zone *zone, *dzone;
1694 int i;
1695
1696 for_each_zone(zone) {
1697 struct per_cpu_pageset *npageset = NULL;
1698
1699 npageset = kmalloc_node(sizeof(struct per_cpu_pageset),
1700 GFP_KERNEL, cpu_to_node(cpu));
1701 if (!npageset) {
1702 zone->pageset[cpu] = NULL;
1703 goto bad;
1704 }
1705
1706 if (zone->pageset[cpu]) {
1707 memcpy(npageset, zone->pageset[cpu],
1708 sizeof(struct per_cpu_pageset));
1709
1710 /* Relocate lists */
1711 for (i = 0; i < 2; i++) {
1712 INIT_LIST_HEAD(&npageset->pcp[i].list);
1713 list_splice(&zone->pageset[cpu]->pcp[i].list,
1714 &npageset->pcp[i].list);
1715 }
1716 } else {
1717 struct per_cpu_pages *pcp;
1718 unsigned long batch;
1719
1720 batch = zone_batchsize(zone);
1721
1722 pcp = &npageset->pcp[0]; /* hot */
1723 pcp->count = 0;
1724 pcp->low = 2 * batch;
1725 pcp->high = 6 * batch;
1726 pcp->batch = 1 * batch;
1727 INIT_LIST_HEAD(&pcp->list);
1728
1729 pcp = &npageset->pcp[1]; /* cold*/
1730 pcp->count = 0;
1731 pcp->low = 0;
1732 pcp->high = 2 * batch;
1733 pcp->batch = 1 * batch;
1734 INIT_LIST_HEAD(&pcp->list);
1735 }
1736 zone->pageset[cpu] = npageset;
1737 }
1738
1739 return 0;
1740bad:
1741 for_each_zone(dzone) {
1742 if (dzone == zone)
1743 break;
1744 kfree(dzone->pageset[cpu]);
1745 dzone->pageset[cpu] = NULL;
1746 }
1747 return -ENOMEM;
1748}
1749
1750static inline void free_zone_pagesets(int cpu)
1751{
1752#ifdef CONFIG_NUMA
1753 struct zone *zone;
1754
1755 for_each_zone(zone) {
1756 struct per_cpu_pageset *pset = zone_pcp(zone, cpu);
1757
1758 zone_pcp(zone, cpu) = NULL;
1759 kfree(pset);
1760 }
1761#endif
1762}
1763
1764static int __devinit pageset_cpuup_callback(struct notifier_block *nfb,
1765 unsigned long action,
1766 void *hcpu)
1767{
1768 int cpu = (long)hcpu;
1769 int ret = NOTIFY_OK;
1770
1771 switch (action) {
1772 case CPU_UP_PREPARE:
1773 if (process_zones(cpu))
1774 ret = NOTIFY_BAD;
1775 break;
1776#ifdef CONFIG_HOTPLUG_CPU
1777 case CPU_DEAD:
1778 free_zone_pagesets(cpu);
1779 break;
1780#endif
1781 default:
1782 break;
1783 }
1784 return ret;
1785}
1786
1787static struct notifier_block pageset_notifier =
1788 { &pageset_cpuup_callback, NULL, 0 };
1789
1790void __init setup_per_cpu_pageset()
1791{
1792 int err;
1793
1794 /* Initialize per_cpu_pageset for cpu 0.
1795 * A cpuup callback will do this for every cpu
1796 * as it comes online
1797 */
1798 err = process_zones(smp_processor_id());
1799 BUG_ON(err);
1800 register_cpu_notifier(&pageset_notifier);
1801}
1802
1803#endif
1804
1da177e4
LT
1805/*
1806 * Set up the zone data structures:
1807 * - mark all pages reserved
1808 * - mark all memory queues empty
1809 * - clear the memory bitmaps
1810 */
1811static void __init free_area_init_core(struct pglist_data *pgdat,
1812 unsigned long *zones_size, unsigned long *zholes_size)
1813{
1814 unsigned long i, j;
1815 const unsigned long zone_required_alignment = 1UL << (MAX_ORDER-1);
1816 int cpu, nid = pgdat->node_id;
1817 unsigned long zone_start_pfn = pgdat->node_start_pfn;
1818
1819 pgdat->nr_zones = 0;
1820 init_waitqueue_head(&pgdat->kswapd_wait);
1821 pgdat->kswapd_max_order = 0;
1822
1823 for (j = 0; j < MAX_NR_ZONES; j++) {
1824 struct zone *zone = pgdat->node_zones + j;
1825 unsigned long size, realsize;
1826 unsigned long batch;
1827
1828 zone_table[NODEZONE(nid, j)] = zone;
1829 realsize = size = zones_size[j];
1830 if (zholes_size)
1831 realsize -= zholes_size[j];
1832
1833 if (j == ZONE_DMA || j == ZONE_NORMAL)
1834 nr_kernel_pages += realsize;
1835 nr_all_pages += realsize;
1836
1837 zone->spanned_pages = size;
1838 zone->present_pages = realsize;
1839 zone->name = zone_names[j];
1840 spin_lock_init(&zone->lock);
1841 spin_lock_init(&zone->lru_lock);
1842 zone->zone_pgdat = pgdat;
1843 zone->free_pages = 0;
1844
1845 zone->temp_priority = zone->prev_priority = DEF_PRIORITY;
1846
e7c8d5c9 1847 batch = zone_batchsize(zone);
8e30f272 1848
1da177e4
LT
1849 for (cpu = 0; cpu < NR_CPUS; cpu++) {
1850 struct per_cpu_pages *pcp;
e7c8d5c9
CL
1851#ifdef CONFIG_NUMA
1852 struct per_cpu_pageset *pgset;
1853 pgset = &pageset_table[nid*MAX_NR_ZONES*NR_CPUS +
1854 (j * NR_CPUS) + cpu];
1855
1856 zone->pageset[cpu] = pgset;
1857#else
1858 struct per_cpu_pageset *pgset = zone_pcp(zone, cpu);
1859#endif
1da177e4 1860
e7c8d5c9 1861 pcp = &pgset->pcp[0]; /* hot */
1da177e4
LT
1862 pcp->count = 0;
1863 pcp->low = 2 * batch;
1864 pcp->high = 6 * batch;
1865 pcp->batch = 1 * batch;
1866 INIT_LIST_HEAD(&pcp->list);
1867
e7c8d5c9 1868 pcp = &pgset->pcp[1]; /* cold */
1da177e4
LT
1869 pcp->count = 0;
1870 pcp->low = 0;
1871 pcp->high = 2 * batch;
1872 pcp->batch = 1 * batch;
1873 INIT_LIST_HEAD(&pcp->list);
1874 }
1875 printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%lu\n",
1876 zone_names[j], realsize, batch);
1877 INIT_LIST_HEAD(&zone->active_list);
1878 INIT_LIST_HEAD(&zone->inactive_list);
1879 zone->nr_scan_active = 0;
1880 zone->nr_scan_inactive = 0;
1881 zone->nr_active = 0;
1882 zone->nr_inactive = 0;
1e7e5a90 1883 atomic_set(&zone->reclaim_in_progress, -1);
1da177e4
LT
1884 if (!size)
1885 continue;
1886
1887 /*
1888 * The per-page waitqueue mechanism uses hashed waitqueues
1889 * per zone.
1890 */
1891 zone->wait_table_size = wait_table_size(size);
1892 zone->wait_table_bits =
1893 wait_table_bits(zone->wait_table_size);
1894 zone->wait_table = (wait_queue_head_t *)
1895 alloc_bootmem_node(pgdat, zone->wait_table_size
1896 * sizeof(wait_queue_head_t));
1897
1898 for(i = 0; i < zone->wait_table_size; ++i)
1899 init_waitqueue_head(zone->wait_table + i);
1900
1901 pgdat->nr_zones = j+1;
1902
1903 zone->zone_mem_map = pfn_to_page(zone_start_pfn);
1904 zone->zone_start_pfn = zone_start_pfn;
1905
1906 if ((zone_start_pfn) & (zone_required_alignment-1))
1907 printk(KERN_CRIT "BUG: wrong zone alignment, it will crash\n");
1908
1909 memmap_init(size, nid, j, zone_start_pfn);
1910
1911 zone_start_pfn += size;
1912
1913 zone_init_free_lists(pgdat, zone, zone->spanned_pages);
1914 }
1915}
1916
1917static void __init alloc_node_mem_map(struct pglist_data *pgdat)
1918{
1919 unsigned long size;
1920
1921 /* Skip empty nodes */
1922 if (!pgdat->node_spanned_pages)
1923 return;
1924
1925 /* ia64 gets its own node_mem_map, before this, without bootmem */
1926 if (!pgdat->node_mem_map) {
1927 size = (pgdat->node_spanned_pages + 1) * sizeof(struct page);
1928 pgdat->node_mem_map = alloc_bootmem_node(pgdat, size);
1929 }
1930#ifndef CONFIG_DISCONTIGMEM
1931 /*
1932 * With no DISCONTIG, the global mem_map is just set as node 0's
1933 */
1934 if (pgdat == NODE_DATA(0))
1935 mem_map = NODE_DATA(0)->node_mem_map;
1936#endif
1937}
1938
1939void __init free_area_init_node(int nid, struct pglist_data *pgdat,
1940 unsigned long *zones_size, unsigned long node_start_pfn,
1941 unsigned long *zholes_size)
1942{
1943 pgdat->node_id = nid;
1944 pgdat->node_start_pfn = node_start_pfn;
1945 calculate_zone_totalpages(pgdat, zones_size, zholes_size);
1946
1947 alloc_node_mem_map(pgdat);
1948
1949 free_area_init_core(pgdat, zones_size, zholes_size);
1950}
1951
1952#ifndef CONFIG_DISCONTIGMEM
1953static bootmem_data_t contig_bootmem_data;
1954struct pglist_data contig_page_data = { .bdata = &contig_bootmem_data };
1955
1956EXPORT_SYMBOL(contig_page_data);
1957
1958void __init free_area_init(unsigned long *zones_size)
1959{
1960 free_area_init_node(0, &contig_page_data, zones_size,
1961 __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
1962}
1963#endif
1964
1965#ifdef CONFIG_PROC_FS
1966
1967#include <linux/seq_file.h>
1968
1969static void *frag_start(struct seq_file *m, loff_t *pos)
1970{
1971 pg_data_t *pgdat;
1972 loff_t node = *pos;
1973
1974 for (pgdat = pgdat_list; pgdat && node; pgdat = pgdat->pgdat_next)
1975 --node;
1976
1977 return pgdat;
1978}
1979
1980static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
1981{
1982 pg_data_t *pgdat = (pg_data_t *)arg;
1983
1984 (*pos)++;
1985 return pgdat->pgdat_next;
1986}
1987
1988static void frag_stop(struct seq_file *m, void *arg)
1989{
1990}
1991
1992/*
1993 * This walks the free areas for each zone.
1994 */
1995static int frag_show(struct seq_file *m, void *arg)
1996{
1997 pg_data_t *pgdat = (pg_data_t *)arg;
1998 struct zone *zone;
1999 struct zone *node_zones = pgdat->node_zones;
2000 unsigned long flags;
2001 int order;
2002
2003 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
2004 if (!zone->present_pages)
2005 continue;
2006
2007 spin_lock_irqsave(&zone->lock, flags);
2008 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
2009 for (order = 0; order < MAX_ORDER; ++order)
2010 seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
2011 spin_unlock_irqrestore(&zone->lock, flags);
2012 seq_putc(m, '\n');
2013 }
2014 return 0;
2015}
2016
2017struct seq_operations fragmentation_op = {
2018 .start = frag_start,
2019 .next = frag_next,
2020 .stop = frag_stop,
2021 .show = frag_show,
2022};
2023
295ab934
ND
2024/*
2025 * Output information about zones in @pgdat.
2026 */
2027static int zoneinfo_show(struct seq_file *m, void *arg)
2028{
2029 pg_data_t *pgdat = arg;
2030 struct zone *zone;
2031 struct zone *node_zones = pgdat->node_zones;
2032 unsigned long flags;
2033
2034 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; zone++) {
2035 int i;
2036
2037 if (!zone->present_pages)
2038 continue;
2039
2040 spin_lock_irqsave(&zone->lock, flags);
2041 seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
2042 seq_printf(m,
2043 "\n pages free %lu"
2044 "\n min %lu"
2045 "\n low %lu"
2046 "\n high %lu"
2047 "\n active %lu"
2048 "\n inactive %lu"
2049 "\n scanned %lu (a: %lu i: %lu)"
2050 "\n spanned %lu"
2051 "\n present %lu",
2052 zone->free_pages,
2053 zone->pages_min,
2054 zone->pages_low,
2055 zone->pages_high,
2056 zone->nr_active,
2057 zone->nr_inactive,
2058 zone->pages_scanned,
2059 zone->nr_scan_active, zone->nr_scan_inactive,
2060 zone->spanned_pages,
2061 zone->present_pages);
2062 seq_printf(m,
2063 "\n protection: (%lu",
2064 zone->lowmem_reserve[0]);
2065 for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
2066 seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
2067 seq_printf(m,
2068 ")"
2069 "\n pagesets");
2070 for (i = 0; i < ARRAY_SIZE(zone->pageset); i++) {
2071 struct per_cpu_pageset *pageset;
2072 int j;
2073
e7c8d5c9 2074 pageset = zone_pcp(zone, i);
295ab934
ND
2075 for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
2076 if (pageset->pcp[j].count)
2077 break;
2078 }
2079 if (j == ARRAY_SIZE(pageset->pcp))
2080 continue;
2081 for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
2082 seq_printf(m,
2083 "\n cpu: %i pcp: %i"
2084 "\n count: %i"
2085 "\n low: %i"
2086 "\n high: %i"
2087 "\n batch: %i",
2088 i, j,
2089 pageset->pcp[j].count,
2090 pageset->pcp[j].low,
2091 pageset->pcp[j].high,
2092 pageset->pcp[j].batch);
2093 }
2094#ifdef CONFIG_NUMA
2095 seq_printf(m,
2096 "\n numa_hit: %lu"
2097 "\n numa_miss: %lu"
2098 "\n numa_foreign: %lu"
2099 "\n interleave_hit: %lu"
2100 "\n local_node: %lu"
2101 "\n other_node: %lu",
2102 pageset->numa_hit,
2103 pageset->numa_miss,
2104 pageset->numa_foreign,
2105 pageset->interleave_hit,
2106 pageset->local_node,
2107 pageset->other_node);
2108#endif
2109 }
2110 seq_printf(m,
2111 "\n all_unreclaimable: %u"
2112 "\n prev_priority: %i"
2113 "\n temp_priority: %i"
2114 "\n start_pfn: %lu",
2115 zone->all_unreclaimable,
2116 zone->prev_priority,
2117 zone->temp_priority,
2118 zone->zone_start_pfn);
2119 spin_unlock_irqrestore(&zone->lock, flags);
2120 seq_putc(m, '\n');
2121 }
2122 return 0;
2123}
2124
2125struct seq_operations zoneinfo_op = {
2126 .start = frag_start, /* iterate over all zones. The same as in
2127 * fragmentation. */
2128 .next = frag_next,
2129 .stop = frag_stop,
2130 .show = zoneinfo_show,
2131};
2132
1da177e4
LT
2133static char *vmstat_text[] = {
2134 "nr_dirty",
2135 "nr_writeback",
2136 "nr_unstable",
2137 "nr_page_table_pages",
2138 "nr_mapped",
2139 "nr_slab",
2140
2141 "pgpgin",
2142 "pgpgout",
2143 "pswpin",
2144 "pswpout",
2145 "pgalloc_high",
2146
2147 "pgalloc_normal",
2148 "pgalloc_dma",
2149 "pgfree",
2150 "pgactivate",
2151 "pgdeactivate",
2152
2153 "pgfault",
2154 "pgmajfault",
2155 "pgrefill_high",
2156 "pgrefill_normal",
2157 "pgrefill_dma",
2158
2159 "pgsteal_high",
2160 "pgsteal_normal",
2161 "pgsteal_dma",
2162 "pgscan_kswapd_high",
2163 "pgscan_kswapd_normal",
2164
2165 "pgscan_kswapd_dma",
2166 "pgscan_direct_high",
2167 "pgscan_direct_normal",
2168 "pgscan_direct_dma",
2169 "pginodesteal",
2170
2171 "slabs_scanned",
2172 "kswapd_steal",
2173 "kswapd_inodesteal",
2174 "pageoutrun",
2175 "allocstall",
2176
2177 "pgrotated",
edfbe2b0 2178 "nr_bounce",
1da177e4
LT
2179};
2180
2181static void *vmstat_start(struct seq_file *m, loff_t *pos)
2182{
2183 struct page_state *ps;
2184
2185 if (*pos >= ARRAY_SIZE(vmstat_text))
2186 return NULL;
2187
2188 ps = kmalloc(sizeof(*ps), GFP_KERNEL);
2189 m->private = ps;
2190 if (!ps)
2191 return ERR_PTR(-ENOMEM);
2192 get_full_page_state(ps);
2193 ps->pgpgin /= 2; /* sectors -> kbytes */
2194 ps->pgpgout /= 2;
2195 return (unsigned long *)ps + *pos;
2196}
2197
2198static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
2199{
2200 (*pos)++;
2201 if (*pos >= ARRAY_SIZE(vmstat_text))
2202 return NULL;
2203 return (unsigned long *)m->private + *pos;
2204}
2205
2206static int vmstat_show(struct seq_file *m, void *arg)
2207{
2208 unsigned long *l = arg;
2209 unsigned long off = l - (unsigned long *)m->private;
2210
2211 seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
2212 return 0;
2213}
2214
2215static void vmstat_stop(struct seq_file *m, void *arg)
2216{
2217 kfree(m->private);
2218 m->private = NULL;
2219}
2220
2221struct seq_operations vmstat_op = {
2222 .start = vmstat_start,
2223 .next = vmstat_next,
2224 .stop = vmstat_stop,
2225 .show = vmstat_show,
2226};
2227
2228#endif /* CONFIG_PROC_FS */
2229
2230#ifdef CONFIG_HOTPLUG_CPU
2231static int page_alloc_cpu_notify(struct notifier_block *self,
2232 unsigned long action, void *hcpu)
2233{
2234 int cpu = (unsigned long)hcpu;
2235 long *count;
2236 unsigned long *src, *dest;
2237
2238 if (action == CPU_DEAD) {
2239 int i;
2240
2241 /* Drain local pagecache count. */
2242 count = &per_cpu(nr_pagecache_local, cpu);
2243 atomic_add(*count, &nr_pagecache);
2244 *count = 0;
2245 local_irq_disable();
2246 __drain_pages(cpu);
2247
2248 /* Add dead cpu's page_states to our own. */
2249 dest = (unsigned long *)&__get_cpu_var(page_states);
2250 src = (unsigned long *)&per_cpu(page_states, cpu);
2251
2252 for (i = 0; i < sizeof(struct page_state)/sizeof(unsigned long);
2253 i++) {
2254 dest[i] += src[i];
2255 src[i] = 0;
2256 }
2257
2258 local_irq_enable();
2259 }
2260 return NOTIFY_OK;
2261}
2262#endif /* CONFIG_HOTPLUG_CPU */
2263
2264void __init page_alloc_init(void)
2265{
2266 hotcpu_notifier(page_alloc_cpu_notify, 0);
2267}
2268
2269/*
2270 * setup_per_zone_lowmem_reserve - called whenever
2271 * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
2272 * has a correct pages reserved value, so an adequate number of
2273 * pages are left in the zone after a successful __alloc_pages().
2274 */
2275static void setup_per_zone_lowmem_reserve(void)
2276{
2277 struct pglist_data *pgdat;
2278 int j, idx;
2279
2280 for_each_pgdat(pgdat) {
2281 for (j = 0; j < MAX_NR_ZONES; j++) {
2282 struct zone *zone = pgdat->node_zones + j;
2283 unsigned long present_pages = zone->present_pages;
2284
2285 zone->lowmem_reserve[j] = 0;
2286
2287 for (idx = j-1; idx >= 0; idx--) {
2288 struct zone *lower_zone;
2289
2290 if (sysctl_lowmem_reserve_ratio[idx] < 1)
2291 sysctl_lowmem_reserve_ratio[idx] = 1;
2292
2293 lower_zone = pgdat->node_zones + idx;
2294 lower_zone->lowmem_reserve[j] = present_pages /
2295 sysctl_lowmem_reserve_ratio[idx];
2296 present_pages += lower_zone->present_pages;
2297 }
2298 }
2299 }
2300}
2301
2302/*
2303 * setup_per_zone_pages_min - called when min_free_kbytes changes. Ensures
2304 * that the pages_{min,low,high} values for each zone are set correctly
2305 * with respect to min_free_kbytes.
2306 */
2307static void setup_per_zone_pages_min(void)
2308{
2309 unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
2310 unsigned long lowmem_pages = 0;
2311 struct zone *zone;
2312 unsigned long flags;
2313
2314 /* Calculate total number of !ZONE_HIGHMEM pages */
2315 for_each_zone(zone) {
2316 if (!is_highmem(zone))
2317 lowmem_pages += zone->present_pages;
2318 }
2319
2320 for_each_zone(zone) {
2321 spin_lock_irqsave(&zone->lru_lock, flags);
2322 if (is_highmem(zone)) {
2323 /*
2324 * Often, highmem doesn't need to reserve any pages.
2325 * But the pages_min/low/high values are also used for
2326 * batching up page reclaim activity so we need a
2327 * decent value here.
2328 */
2329 int min_pages;
2330
2331 min_pages = zone->present_pages / 1024;
2332 if (min_pages < SWAP_CLUSTER_MAX)
2333 min_pages = SWAP_CLUSTER_MAX;
2334 if (min_pages > 128)
2335 min_pages = 128;
2336 zone->pages_min = min_pages;
2337 } else {
295ab934 2338 /* if it's a lowmem zone, reserve a number of pages
1da177e4
LT
2339 * proportionate to the zone's size.
2340 */
295ab934 2341 zone->pages_min = (pages_min * zone->present_pages) /
1da177e4
LT
2342 lowmem_pages;
2343 }
2344
2345 /*
2346 * When interpreting these watermarks, just keep in mind that:
2347 * zone->pages_min == (zone->pages_min * 4) / 4;
2348 */
2349 zone->pages_low = (zone->pages_min * 5) / 4;
2350 zone->pages_high = (zone->pages_min * 6) / 4;
2351 spin_unlock_irqrestore(&zone->lru_lock, flags);
2352 }
2353}
2354
2355/*
2356 * Initialise min_free_kbytes.
2357 *
2358 * For small machines we want it small (128k min). For large machines
2359 * we want it large (64MB max). But it is not linear, because network
2360 * bandwidth does not increase linearly with machine size. We use
2361 *
2362 * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
2363 * min_free_kbytes = sqrt(lowmem_kbytes * 16)
2364 *
2365 * which yields
2366 *
2367 * 16MB: 512k
2368 * 32MB: 724k
2369 * 64MB: 1024k
2370 * 128MB: 1448k
2371 * 256MB: 2048k
2372 * 512MB: 2896k
2373 * 1024MB: 4096k
2374 * 2048MB: 5792k
2375 * 4096MB: 8192k
2376 * 8192MB: 11584k
2377 * 16384MB: 16384k
2378 */
2379static int __init init_per_zone_pages_min(void)
2380{
2381 unsigned long lowmem_kbytes;
2382
2383 lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
2384
2385 min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
2386 if (min_free_kbytes < 128)
2387 min_free_kbytes = 128;
2388 if (min_free_kbytes > 65536)
2389 min_free_kbytes = 65536;
2390 setup_per_zone_pages_min();
2391 setup_per_zone_lowmem_reserve();
2392 return 0;
2393}
2394module_init(init_per_zone_pages_min)
2395
2396/*
2397 * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
2398 * that we can call two helper functions whenever min_free_kbytes
2399 * changes.
2400 */
2401int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
2402 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
2403{
2404 proc_dointvec(table, write, file, buffer, length, ppos);
2405 setup_per_zone_pages_min();
2406 return 0;
2407}
2408
2409/*
2410 * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
2411 * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
2412 * whenever sysctl_lowmem_reserve_ratio changes.
2413 *
2414 * The reserve ratio obviously has absolutely no relation with the
2415 * pages_min watermarks. The lowmem reserve ratio can only make sense
2416 * if in function of the boot time zone sizes.
2417 */
2418int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
2419 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
2420{
2421 proc_dointvec_minmax(table, write, file, buffer, length, ppos);
2422 setup_per_zone_lowmem_reserve();
2423 return 0;
2424}
2425
2426__initdata int hashdist = HASHDIST_DEFAULT;
2427
2428#ifdef CONFIG_NUMA
2429static int __init set_hashdist(char *str)
2430{
2431 if (!str)
2432 return 0;
2433 hashdist = simple_strtoul(str, &str, 0);
2434 return 1;
2435}
2436__setup("hashdist=", set_hashdist);
2437#endif
2438
2439/*
2440 * allocate a large system hash table from bootmem
2441 * - it is assumed that the hash table must contain an exact power-of-2
2442 * quantity of entries
2443 * - limit is the number of hash buckets, not the total allocation size
2444 */
2445void *__init alloc_large_system_hash(const char *tablename,
2446 unsigned long bucketsize,
2447 unsigned long numentries,
2448 int scale,
2449 int flags,
2450 unsigned int *_hash_shift,
2451 unsigned int *_hash_mask,
2452 unsigned long limit)
2453{
2454 unsigned long long max = limit;
2455 unsigned long log2qty, size;
2456 void *table = NULL;
2457
2458 /* allow the kernel cmdline to have a say */
2459 if (!numentries) {
2460 /* round applicable memory size up to nearest megabyte */
2461 numentries = (flags & HASH_HIGHMEM) ? nr_all_pages : nr_kernel_pages;
2462 numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
2463 numentries >>= 20 - PAGE_SHIFT;
2464 numentries <<= 20 - PAGE_SHIFT;
2465
2466 /* limit to 1 bucket per 2^scale bytes of low memory */
2467 if (scale > PAGE_SHIFT)
2468 numentries >>= (scale - PAGE_SHIFT);
2469 else
2470 numentries <<= (PAGE_SHIFT - scale);
2471 }
2472 /* rounded up to nearest power of 2 in size */
2473 numentries = 1UL << (long_log2(numentries) + 1);
2474
2475 /* limit allocation size to 1/16 total memory by default */
2476 if (max == 0) {
2477 max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
2478 do_div(max, bucketsize);
2479 }
2480
2481 if (numentries > max)
2482 numentries = max;
2483
2484 log2qty = long_log2(numentries);
2485
2486 do {
2487 size = bucketsize << log2qty;
2488 if (flags & HASH_EARLY)
2489 table = alloc_bootmem(size);
2490 else if (hashdist)
2491 table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
2492 else {
2493 unsigned long order;
2494 for (order = 0; ((1UL << order) << PAGE_SHIFT) < size; order++)
2495 ;
2496 table = (void*) __get_free_pages(GFP_ATOMIC, order);
2497 }
2498 } while (!table && size > PAGE_SIZE && --log2qty);
2499
2500 if (!table)
2501 panic("Failed to allocate %s hash table\n", tablename);
2502
2503 printk("%s hash table entries: %d (order: %d, %lu bytes)\n",
2504 tablename,
2505 (1U << log2qty),
2506 long_log2(size) - PAGE_SHIFT,
2507 size);
2508
2509 if (_hash_shift)
2510 *_hash_shift = log2qty;
2511 if (_hash_mask)
2512 *_hash_mask = (1 << log2qty) - 1;
2513
2514 return table;
2515}