[SPARC64]: Re-export uts_sem for solaris compat module.
[linux-2.6-block.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>
9f158333 25#include <linux/kernel.h>
1da177e4
LT
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>
bdc8cb98 36#include <linux/memory_hotplug.h>
1da177e4
LT
37#include <linux/nodemask.h>
38#include <linux/vmalloc.h>
39
40#include <asm/tlbflush.h>
41#include "internal.h"
42
43/*
44 * MCD - HACK: Find somewhere to initialize this EARLY, or make this
45 * initializer cleaner
46 */
c3d8c141 47nodemask_t node_online_map __read_mostly = { { [0] = 1UL } };
7223a93a 48EXPORT_SYMBOL(node_online_map);
c3d8c141 49nodemask_t node_possible_map __read_mostly = NODE_MASK_ALL;
7223a93a 50EXPORT_SYMBOL(node_possible_map);
c3d8c141 51struct pglist_data *pgdat_list __read_mostly;
6c231b7b
RT
52unsigned long totalram_pages __read_mostly;
53unsigned long totalhigh_pages __read_mostly;
1da177e4
LT
54long nr_swap_pages;
55
56/*
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
63 */
64int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1] = { 256, 32 };
65
66EXPORT_SYMBOL(totalram_pages);
1da177e4
LT
67
68/*
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
71 */
c3d8c141 72struct zone *zone_table[1 << ZONETABLE_SHIFT] __read_mostly;
1da177e4
LT
73EXPORT_SYMBOL(zone_table);
74
75static char *zone_names[MAX_NR_ZONES] = { "DMA", "Normal", "HighMem" };
76int min_free_kbytes = 1024;
77
78unsigned long __initdata nr_kernel_pages;
79unsigned long __initdata nr_all_pages;
80
c6a57e19 81static int page_outside_zone_boundaries(struct zone *zone, struct page *page)
1da177e4 82{
bdc8cb98
DH
83 int ret = 0;
84 unsigned seq;
85 unsigned long pfn = page_to_pfn(page);
c6a57e19 86
bdc8cb98
DH
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));
94
95 return ret;
c6a57e19
DH
96}
97
98static int page_is_consistent(struct zone *zone, struct page *page)
99{
1da177e4
LT
100#ifdef CONFIG_HOLES_IN_ZONE
101 if (!pfn_valid(page_to_pfn(page)))
c6a57e19 102 return 0;
1da177e4
LT
103#endif
104 if (zone != page_zone(page))
c6a57e19
DH
105 return 0;
106
107 return 1;
108}
109/*
110 * Temporary debugging check for pages not lying within a given zone.
111 */
112static int bad_range(struct zone *zone, struct page *page)
113{
114 if (page_outside_zone_boundaries(zone, page))
1da177e4 115 return 1;
c6a57e19
DH
116 if (!page_is_consistent(zone, page))
117 return 1;
118
1da177e4
LT
119 return 0;
120}
121
122static void bad_page(const char *function, struct page *page)
123{
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");
334795ec
HD
132 page->flags &= ~(1 << PG_lru |
133 1 << PG_private |
1da177e4 134 1 << PG_locked |
1da177e4
LT
135 1 << PG_active |
136 1 << PG_dirty |
334795ec
HD
137 1 << PG_reclaim |
138 1 << PG_slab |
1da177e4 139 1 << PG_swapcache |
b5810039
NP
140 1 << PG_writeback |
141 1 << PG_reserved );
1da177e4
LT
142 set_page_count(page, 0);
143 reset_page_mapcount(page);
144 page->mapping = NULL;
9f158333 145 add_taint(TAINT_BAD_PAGE);
1da177e4
LT
146}
147
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
152/*
153 * Higher-order pages are called "compound pages". They are structured thusly:
154 *
155 * The first PAGE_SIZE page is called the "head page".
156 *
157 * The remaining PAGE_SIZE pages are called "tail pages".
158 *
159 * All pages have PG_compound set. All pages have their ->private pointing at
160 * the head page (even the head page has this).
161 *
162 * The first tail page's ->mapping, if non-zero, holds the address of the
163 * compound page's put_page() function.
164 *
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.
168 */
169static void prep_compound_page(struct page *page, unsigned long order)
170{
171 int i;
172 int nr_pages = 1 << order;
173
174 page[1].mapping = NULL;
175 page[1].index = order;
176 for (i = 0; i < nr_pages; i++) {
177 struct page *p = page + i;
178
179 SetPageCompound(p);
4c21e2f2 180 set_page_private(p, (unsigned long)page);
1da177e4
LT
181 }
182}
183
184static void destroy_compound_page(struct page *page, unsigned long order)
185{
186 int i;
187 int nr_pages = 1 << order;
188
189 if (!PageCompound(page))
190 return;
191
192 if (page[1].index != order)
193 bad_page(__FUNCTION__, page);
194
195 for (i = 0; i < nr_pages; i++) {
196 struct page *p = page + i;
197
198 if (!PageCompound(p))
199 bad_page(__FUNCTION__, page);
4c21e2f2 200 if (page_private(p) != (unsigned long)page)
1da177e4
LT
201 bad_page(__FUNCTION__, page);
202 ClearPageCompound(p);
203 }
204}
205#endif /* CONFIG_HUGETLB_PAGE */
206
207/*
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.
211 */
212static inline unsigned long page_order(struct page *page) {
4c21e2f2 213 return page_private(page);
1da177e4
LT
214}
215
216static inline void set_page_order(struct page *page, int order) {
4c21e2f2 217 set_page_private(page, order);
1da177e4
LT
218 __SetPagePrivate(page);
219}
220
221static inline void rmv_page_order(struct page *page)
222{
223 __ClearPagePrivate(page);
4c21e2f2 224 set_page_private(page, 0);
1da177e4
LT
225}
226
227/*
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).
230 *
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
237 *
238 * 2) Any buddy B will have an order O+1 parent P which
239 * satisfies the following equation:
240 * P = B & ~(1 << O)
241 *
242 * Assumption: *_mem_map is contigious at least up to MAX_ORDER
243 */
244static inline struct page *
245__page_find_buddy(struct page *page, unsigned long page_idx, unsigned int order)
246{
247 unsigned long buddy_idx = page_idx ^ (1 << order);
248
249 return page + (buddy_idx - page_idx);
250}
251
252static inline unsigned long
253__find_combined_index(unsigned long page_idx, unsigned int order)
254{
255 return (page_idx & ~(1 << order));
256}
257
258/*
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.
4c21e2f2 264 * for recording page's order, we use page_private(page) and PG_private.
1da177e4
LT
265 *
266 */
267static inline int page_is_buddy(struct page *page, int order)
268{
269 if (PagePrivate(page) &&
270 (page_order(page) == order) &&
1da177e4
LT
271 page_count(page) == 0)
272 return 1;
273 return 0;
274}
275
276/*
277 * Freeing function for a buddy system allocator.
278 *
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
4c21e2f2 290 * order is recorded in page_private(page) field.
1da177e4
LT
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.
296 *
297 * -- wli
298 */
299
300static inline void __free_pages_bulk (struct page *page,
301 struct zone *zone, unsigned int order)
302{
303 unsigned long page_idx;
304 int order_size = 1 << order;
305
306 if (unlikely(order))
307 destroy_compound_page(page, order);
308
309 page_idx = page_to_pfn(page) & ((1 << MAX_ORDER) - 1);
310
311 BUG_ON(page_idx & (order_size - 1));
312 BUG_ON(bad_range(zone, page));
313
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;
319
320 combined_idx = __find_combined_index(page_idx, order);
321 buddy = __page_find_buddy(page, page_idx, order);
322
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++;
334 }
335 set_page_order(page, order);
336 list_add(&page->lru, &zone->free_area[order].free_list);
337 zone->free_area[order].nr_free++;
338}
339
340static inline void free_pages_check(const char *function, struct page *page)
341{
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 |
b5810039
NP
353 1 << PG_writeback |
354 1 << PG_reserved )))
1da177e4
LT
355 bad_page(function, page);
356 if (PageDirty(page))
242e5468 357 __ClearPageDirty(page);
1da177e4
LT
358}
359
360/*
361 * Frees a list of pages.
362 * Assumes all pages on list are in same zone, and of same order.
207f36ee 363 * count is the number of pages to free.
1da177e4
LT
364 *
365 * If the zone was previously in an "all pages pinned" state then look to
366 * see if this freeing clears that state.
367 *
368 * And clear the zone's pages_scanned counter, to hold off the "all pages are
369 * pinned" detection logic.
370 */
371static int
372free_pages_bulk(struct zone *zone, int count,
373 struct list_head *list, unsigned int order)
374{
375 unsigned long flags;
376 struct page *page = NULL;
377 int ret = 0;
378
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++;
388 }
389 spin_unlock_irqrestore(&zone->lock, flags);
390 return ret;
391}
392
393void __free_pages_ok(struct page *page, unsigned int order)
394{
395 LIST_HEAD(list);
396 int i;
397
398 arch_free_page(page, order);
399
400 mod_page_state(pgfree, 1 << order);
401
402#ifndef CONFIG_MMU
403 if (order > 0)
404 for (i = 1 ; i < (1 << order) ; ++i)
405 __put_page(page + i);
406#endif
407
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);
413}
414
415
416/*
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.
427 *
428 * -- wli
429 */
430static inline struct page *
431expand(struct zone *zone, struct page *page,
432 int low, int high, struct free_area *area)
433{
434 unsigned long size = 1 << high;
435
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);
444 }
445 return page;
446}
447
448void set_page_refs(struct page *page, int order)
449{
450#ifdef CONFIG_MMU
451 set_page_count(page, 1);
452#else
453 int i;
454
455 /*
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()
459 */
460 for (i = 0; i < (1 << order); i++)
461 set_page_count(page + i, 1);
462#endif /* CONFIG_MMU */
463}
464
465/*
466 * This page is about to be returned from the page allocator
467 */
468static void prep_new_page(struct page *page, int order)
469{
334795ec
HD
470 if ( page_mapcount(page) ||
471 page->mapping != NULL ||
472 page_count(page) != 0 ||
473 (page->flags & (
474 1 << PG_lru |
1da177e4
LT
475 1 << PG_private |
476 1 << PG_locked |
1da177e4
LT
477 1 << PG_active |
478 1 << PG_dirty |
479 1 << PG_reclaim |
334795ec 480 1 << PG_slab |
1da177e4 481 1 << PG_swapcache |
b5810039
NP
482 1 << PG_writeback |
483 1 << PG_reserved )))
1da177e4
LT
484 bad_page(__FUNCTION__, page);
485
486 page->flags &= ~(1 << PG_uptodate | 1 << PG_error |
487 1 << PG_referenced | 1 << PG_arch_1 |
488 1 << PG_checked | 1 << PG_mappedtodisk);
4c21e2f2 489 set_page_private(page, 0);
1da177e4
LT
490 set_page_refs(page, order);
491 kernel_map_pages(page, 1 << order, 1);
492}
493
494/*
495 * Do the hard work of removing an element from the buddy allocator.
496 * Call me with the zone->lock already held.
497 */
498static struct page *__rmqueue(struct zone *zone, unsigned int order)
499{
500 struct free_area * area;
501 unsigned int current_order;
502 struct page *page;
503
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;
508
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);
515 }
516
517 return NULL;
518}
519
520/*
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.
524 */
525static int rmqueue_bulk(struct zone *zone, unsigned int order,
526 unsigned long count, struct list_head *list)
527{
528 unsigned long flags;
529 int i;
530 int allocated = 0;
531 struct page *page;
532
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);
540 }
541 spin_unlock_irqrestore(&zone->lock, flags);
542 return allocated;
543}
544
4ae7c039
CL
545#ifdef CONFIG_NUMA
546/* Called from the slab reaper to drain remote pagesets */
547void drain_remote_pages(void)
548{
549 struct zone *zone;
550 int i;
551 unsigned long flags;
552
553 local_irq_save(flags);
554 for_each_zone(zone) {
555 struct per_cpu_pageset *pset;
556
557 /* Do not drain local pagesets */
558 if (zone->zone_pgdat->node_id == numa_node_id())
559 continue;
560
561 pset = zone->pageset[smp_processor_id()];
562 for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
563 struct per_cpu_pages *pcp;
564
565 pcp = &pset->pcp[i];
566 if (pcp->count)
567 pcp->count -= free_pages_bulk(zone, pcp->count,
568 &pcp->list, 0);
569 }
570 }
571 local_irq_restore(flags);
572}
573#endif
574
1da177e4
LT
575#if defined(CONFIG_PM) || defined(CONFIG_HOTPLUG_CPU)
576static void __drain_pages(unsigned int cpu)
577{
578 struct zone *zone;
579 int i;
580
581 for_each_zone(zone) {
582 struct per_cpu_pageset *pset;
583
e7c8d5c9 584 pset = zone_pcp(zone, cpu);
1da177e4
LT
585 for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
586 struct per_cpu_pages *pcp;
587
588 pcp = &pset->pcp[i];
589 pcp->count -= free_pages_bulk(zone, pcp->count,
590 &pcp->list, 0);
591 }
592 }
593}
594#endif /* CONFIG_PM || CONFIG_HOTPLUG_CPU */
595
596#ifdef CONFIG_PM
597
598void mark_free_pages(struct zone *zone)
599{
600 unsigned long zone_pfn, flags;
601 int order;
602 struct list_head *curr;
603
604 if (!zone->spanned_pages)
605 return;
606
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));
610
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;
614
615 start_pfn = page_to_pfn(list_entry(curr, struct page, lru));
616
617 for (i=0; i < (1<<order); i++)
618 SetPageNosaveFree(pfn_to_page(start_pfn+i));
619 }
620 spin_unlock_irqrestore(&zone->lock, flags);
621}
622
623/*
624 * Spill all of this CPU's per-cpu pages back into the buddy allocator.
625 */
626void drain_local_pages(void)
627{
628 unsigned long flags;
629
630 local_irq_save(flags);
631 __drain_pages(smp_processor_id());
632 local_irq_restore(flags);
633}
634#endif /* CONFIG_PM */
635
636static void zone_statistics(struct zonelist *zonelist, struct zone *z)
637{
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;
644
645 local_irq_save(flags);
646 cpu = smp_processor_id();
e7c8d5c9 647 p = zone_pcp(z,cpu);
1da177e4 648 if (pg == orig) {
e7c8d5c9 649 p->numa_hit++;
1da177e4
LT
650 } else {
651 p->numa_miss++;
e7c8d5c9 652 zone_pcp(zonelist->zones[0], cpu)->numa_foreign++;
1da177e4
LT
653 }
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
660}
661
662/*
663 * Free a 0-order page
664 */
665static void FASTCALL(free_hot_cold_page(struct page *page, int cold));
666static void fastcall free_hot_cold_page(struct page *page, int cold)
667{
668 struct zone *zone = page_zone(page);
669 struct per_cpu_pages *pcp;
670 unsigned long flags;
671
672 arch_free_page(page, 0);
673
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);
e7c8d5c9 679 pcp = &zone_pcp(zone, get_cpu())->pcp[cold];
1da177e4 680 local_irq_save(flags);
1da177e4
LT
681 list_add(&page->lru, &pcp->list);
682 pcp->count++;
2caaad41
CL
683 if (pcp->count >= pcp->high)
684 pcp->count -= free_pages_bulk(zone, pcp->batch, &pcp->list, 0);
1da177e4
LT
685 local_irq_restore(flags);
686 put_cpu();
687}
688
689void fastcall free_hot_page(struct page *page)
690{
691 free_hot_cold_page(page, 0);
692}
693
694void fastcall free_cold_page(struct page *page)
695{
696 free_hot_cold_page(page, 1);
697}
698
dd0fc66f 699static inline void prep_zero_page(struct page *page, int order, gfp_t gfp_flags)
1da177e4
LT
700{
701 int i;
702
703 BUG_ON((gfp_flags & (__GFP_WAIT | __GFP_HIGHMEM)) == __GFP_HIGHMEM);
704 for(i = 0; i < (1 << order); i++)
705 clear_highpage(page + i);
706}
707
708/*
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.
712 */
713static struct page *
dd0fc66f 714buffered_rmqueue(struct zone *zone, int order, gfp_t gfp_flags)
1da177e4
LT
715{
716 unsigned long flags;
717 struct page *page = NULL;
718 int cold = !!(gfp_flags & __GFP_COLD);
719
720 if (order == 0) {
721 struct per_cpu_pages *pcp;
722
e7c8d5c9 723 pcp = &zone_pcp(zone, get_cpu())->pcp[cold];
1da177e4
LT
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--;
732 }
733 local_irq_restore(flags);
734 put_cpu();
735 }
736
737 if (page == NULL) {
738 spin_lock_irqsave(&zone->lock, flags);
739 page = __rmqueue(zone, order);
740 spin_unlock_irqrestore(&zone->lock, flags);
741 }
742
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);
747
748 if (gfp_flags & __GFP_ZERO)
749 prep_zero_page(page, order, gfp_flags);
750
751 if (order && (gfp_flags & __GFP_COMP))
752 prep_compound_page(page, order);
753 }
754 return page;
755}
756
757/*
758 * Return 1 if free pages are above 'mark'. This takes into account the order
759 * of the allocation.
760 */
761int zone_watermark_ok(struct zone *z, int order, unsigned long mark,
260b2367 762 int classzone_idx, int can_try_harder, gfp_t gfp_high)
1da177e4
LT
763{
764 /* free_pages my go negative - that's OK */
765 long min = mark, free_pages = z->free_pages - (1 << order) + 1;
766 int o;
767
768 if (gfp_high)
769 min -= min / 2;
770 if (can_try_harder)
771 min -= min / 4;
772
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;
778
779 /* Require fewer higher order pages to be free */
780 min >>= 1;
781
782 if (free_pages <= min)
783 return 0;
784 }
785 return 1;
786}
787
753ee728 788static inline int
dd0fc66f 789should_reclaim_zone(struct zone *z, gfp_t gfp_mask)
753ee728
MH
790{
791 if (!z->reclaim_pages)
792 return 0;
0c35bbad
MH
793 if (gfp_mask & __GFP_NORECLAIM)
794 return 0;
753ee728
MH
795 return 1;
796}
797
1da177e4
LT
798/*
799 * This is the 'heart' of the zoned buddy allocator.
800 */
801struct page * fastcall
dd0fc66f 802__alloc_pages(gfp_t gfp_mask, unsigned int order,
1da177e4
LT
803 struct zonelist *zonelist)
804{
260b2367 805 const gfp_t wait = gfp_mask & __GFP_WAIT;
1da177e4
LT
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;
815
816 might_sleep_if(wait);
817
818 /*
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
822 */
823 can_try_harder = (unlikely(rt_task(p)) && !in_interrupt()) || !wait;
824
825 zones = zonelist->zones; /* the list of zones suitable for gfp_mask */
826
827 if (unlikely(zones[0] == NULL)) {
828 /* Should this ever happen?? */
829 return NULL;
830 }
831
832 classzone_idx = zone_idx(zones[0]);
833
753ee728 834restart:
9bf2229f
PJ
835 /*
836 * Go through the zonelist once, looking for a zone with enough free.
837 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
838 */
1da177e4 839 for (i = 0; (z = zones[i]) != NULL; i++) {
753ee728 840 int do_reclaim = should_reclaim_zone(z, gfp_mask);
1da177e4 841
9bf2229f 842 if (!cpuset_zone_allowed(z, __GFP_HARDWALL))
1da177e4
LT
843 continue;
844
753ee728
MH
845 /*
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.
849 */
850zone_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;
860 }
861 }
862
1da177e4
LT
863 page = buffered_rmqueue(z, order, gfp_mask);
864 if (page)
865 goto got_pg;
866 }
867
868 for (i = 0; (z = zones[i]) != NULL; i++)
869 wakeup_kswapd(z, order);
870
871 /*
872 * Go through the zonelist again. Let __GFP_HIGH and allocations
873 * coming from realtime tasks to go deeper into reserves
874 *
875 * This is the last chance, in general, before the goto nopage.
876 * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
9bf2229f 877 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
1da177e4
LT
878 */
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;
884
9bf2229f 885 if (wait && !cpuset_zone_allowed(z, gfp_mask))
1da177e4
LT
886 continue;
887
888 page = buffered_rmqueue(z, order, gfp_mask);
889 if (page)
890 goto got_pg;
891 }
892
893 /* This allocation should allow future memory freeing. */
b84a35be
NP
894
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++) {
9bf2229f 900 if (!cpuset_zone_allowed(z, gfp_mask))
b84a35be
NP
901 continue;
902 page = buffered_rmqueue(z, order, gfp_mask);
903 if (page)
904 goto got_pg;
905 }
1da177e4
LT
906 }
907 goto nopage;
908 }
909
910 /* Atomic allocations - we can't balance anything */
911 if (!wait)
912 goto nopage;
913
914rebalance:
915 cond_resched();
916
917 /* We now go into synchronous reclaim */
918 p->flags |= PF_MEMALLOC;
919 reclaim_state.reclaimed_slab = 0;
920 p->reclaim_state = &reclaim_state;
921
1ad539b2 922 did_some_progress = try_to_free_pages(zones, gfp_mask);
1da177e4
LT
923
924 p->reclaim_state = NULL;
925 p->flags &= ~PF_MEMALLOC;
926
927 cond_resched();
928
929 if (likely(did_some_progress)) {
1da177e4
LT
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;
935
9bf2229f 936 if (!cpuset_zone_allowed(z, gfp_mask))
1da177e4
LT
937 continue;
938
939 page = buffered_rmqueue(z, order, gfp_mask);
940 if (page)
941 goto got_pg;
942 }
943 } else if ((gfp_mask & __GFP_FS) && !(gfp_mask & __GFP_NORETRY)) {
944 /*
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.
949 */
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;
954
9bf2229f 955 if (!cpuset_zone_allowed(z, __GFP_HARDWALL))
1da177e4
LT
956 continue;
957
958 page = buffered_rmqueue(z, order, gfp_mask);
959 if (page)
960 goto got_pg;
961 }
962
79b9ce31 963 out_of_memory(gfp_mask, order);
1da177e4
LT
964 goto restart;
965 }
966
967 /*
968 * Don't let big-order allocations loop unless the caller explicitly
969 * requests that. Wait for some write requests to complete then retry.
970 *
971 * In this implementation, __GFP_REPEAT means __GFP_NOFAIL for order
972 * <= 3, but that may not be true in other implementations.
973 */
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;
980 }
981 if (do_retry) {
982 blk_congestion_wait(WRITE, HZ/50);
983 goto rebalance;
984 }
985
986nopage:
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();
578c2fd6 992 show_mem();
1da177e4
LT
993 }
994 return NULL;
995got_pg:
996 zone_statistics(zonelist, z);
997 return page;
998}
999
1000EXPORT_SYMBOL(__alloc_pages);
1001
1002/*
1003 * Common helper functions.
1004 */
dd0fc66f 1005fastcall unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
1da177e4
LT
1006{
1007 struct page * page;
1008 page = alloc_pages(gfp_mask, order);
1009 if (!page)
1010 return 0;
1011 return (unsigned long) page_address(page);
1012}
1013
1014EXPORT_SYMBOL(__get_free_pages);
1015
dd0fc66f 1016fastcall unsigned long get_zeroed_page(gfp_t gfp_mask)
1da177e4
LT
1017{
1018 struct page * page;
1019
1020 /*
1021 * get_zeroed_page() returns a 32-bit address, which cannot represent
1022 * a highmem page
1023 */
260b2367 1024 BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
1da177e4
LT
1025
1026 page = alloc_pages(gfp_mask | __GFP_ZERO, 0);
1027 if (page)
1028 return (unsigned long) page_address(page);
1029 return 0;
1030}
1031
1032EXPORT_SYMBOL(get_zeroed_page);
1033
1034void __pagevec_free(struct pagevec *pvec)
1035{
1036 int i = pagevec_count(pvec);
1037
1038 while (--i >= 0)
1039 free_hot_cold_page(pvec->pages[i], pvec->cold);
1040}
1041
1042fastcall void __free_pages(struct page *page, unsigned int order)
1043{
b5810039 1044 if (put_page_testzero(page)) {
1da177e4
LT
1045 if (order == 0)
1046 free_hot_page(page);
1047 else
1048 __free_pages_ok(page, order);
1049 }
1050}
1051
1052EXPORT_SYMBOL(__free_pages);
1053
1054fastcall void free_pages(unsigned long addr, unsigned int order)
1055{
1056 if (addr != 0) {
1057 BUG_ON(!virt_addr_valid((void *)addr));
1058 __free_pages(virt_to_page((void *)addr), order);
1059 }
1060}
1061
1062EXPORT_SYMBOL(free_pages);
1063
1064/*
1065 * Total amount of free (allocatable) RAM:
1066 */
1067unsigned int nr_free_pages(void)
1068{
1069 unsigned int sum = 0;
1070 struct zone *zone;
1071
1072 for_each_zone(zone)
1073 sum += zone->free_pages;
1074
1075 return sum;
1076}
1077
1078EXPORT_SYMBOL(nr_free_pages);
1079
1080#ifdef CONFIG_NUMA
1081unsigned int nr_free_pages_pgdat(pg_data_t *pgdat)
1082{
1083 unsigned int i, sum = 0;
1084
1085 for (i = 0; i < MAX_NR_ZONES; i++)
1086 sum += pgdat->node_zones[i].free_pages;
1087
1088 return sum;
1089}
1090#endif
1091
1092static unsigned int nr_free_zone_pages(int offset)
1093{
e310fd43
MB
1094 /* Just pick one node, since fallback list is circular */
1095 pg_data_t *pgdat = NODE_DATA(numa_node_id());
1da177e4
LT
1096 unsigned int sum = 0;
1097
e310fd43
MB
1098 struct zonelist *zonelist = pgdat->node_zonelists + offset;
1099 struct zone **zonep = zonelist->zones;
1100 struct zone *zone;
1da177e4 1101
e310fd43
MB
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;
1da177e4
LT
1107 }
1108
1109 return sum;
1110}
1111
1112/*
1113 * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
1114 */
1115unsigned int nr_free_buffer_pages(void)
1116{
af4ca457 1117 return nr_free_zone_pages(gfp_zone(GFP_USER));
1da177e4
LT
1118}
1119
1120/*
1121 * Amount of free RAM allocatable within all zones
1122 */
1123unsigned int nr_free_pagecache_pages(void)
1124{
af4ca457 1125 return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER));
1da177e4
LT
1126}
1127
1128#ifdef CONFIG_HIGHMEM
1129unsigned int nr_free_highpages (void)
1130{
1131 pg_data_t *pgdat;
1132 unsigned int pages = 0;
1133
1134 for_each_pgdat(pgdat)
1135 pages += pgdat->node_zones[ZONE_HIGHMEM].free_pages;
1136
1137 return pages;
1138}
1139#endif
1140
1141#ifdef CONFIG_NUMA
1142static void show_node(struct zone *zone)
1143{
1144 printk("Node %d ", zone->zone_pgdat->node_id);
1145}
1146#else
1147#define show_node(zone) do { } while (0)
1148#endif
1149
1150/*
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.
1154 */
1155static DEFINE_PER_CPU(struct page_state, page_states) = {0};
1156
1157atomic_t nr_pagecache = ATOMIC_INIT(0);
1158EXPORT_SYMBOL(nr_pagecache);
1159#ifdef CONFIG_SMP
1160DEFINE_PER_CPU(long, nr_pagecache_local) = 0;
1161#endif
1162
c07e02db 1163void __get_page_state(struct page_state *ret, int nr, cpumask_t *cpumask)
1da177e4
LT
1164{
1165 int cpu = 0;
1166
1167 memset(ret, 0, sizeof(*ret));
c07e02db 1168 cpus_and(*cpumask, *cpumask, cpu_online_map);
1da177e4 1169
c07e02db 1170 cpu = first_cpu(*cpumask);
1da177e4
LT
1171 while (cpu < NR_CPUS) {
1172 unsigned long *in, *out, off;
1173
1174 in = (unsigned long *)&per_cpu(page_states, cpu);
1175
c07e02db 1176 cpu = next_cpu(cpu, *cpumask);
1da177e4
LT
1177
1178 if (cpu < NR_CPUS)
1179 prefetch(&per_cpu(page_states, cpu));
1180
1181 out = (unsigned long *)ret;
1182 for (off = 0; off < nr; off++)
1183 *out++ += *in++;
1184 }
1185}
1186
c07e02db
MH
1187void get_page_state_node(struct page_state *ret, int node)
1188{
1189 int nr;
1190 cpumask_t mask = node_to_cpumask(node);
1191
1192 nr = offsetof(struct page_state, GET_PAGE_STATE_LAST);
1193 nr /= sizeof(unsigned long);
1194
1195 __get_page_state(ret, nr+1, &mask);
1196}
1197
1da177e4
LT
1198void get_page_state(struct page_state *ret)
1199{
1200 int nr;
c07e02db 1201 cpumask_t mask = CPU_MASK_ALL;
1da177e4
LT
1202
1203 nr = offsetof(struct page_state, GET_PAGE_STATE_LAST);
1204 nr /= sizeof(unsigned long);
1205
c07e02db 1206 __get_page_state(ret, nr + 1, &mask);
1da177e4
LT
1207}
1208
1209void get_full_page_state(struct page_state *ret)
1210{
c07e02db
MH
1211 cpumask_t mask = CPU_MASK_ALL;
1212
1213 __get_page_state(ret, sizeof(*ret) / sizeof(unsigned long), &mask);
1da177e4
LT
1214}
1215
c2f29ea1 1216unsigned long __read_page_state(unsigned long offset)
1da177e4
LT
1217{
1218 unsigned long ret = 0;
1219 int cpu;
1220
1221 for_each_online_cpu(cpu) {
1222 unsigned long in;
1223
1224 in = (unsigned long)&per_cpu(page_states, cpu) + offset;
1225 ret += *((unsigned long *)in);
1226 }
1227 return ret;
1228}
1229
83e5d8f7 1230void __mod_page_state(unsigned long offset, unsigned long delta)
1da177e4
LT
1231{
1232 unsigned long flags;
1233 void* ptr;
1234
1235 local_irq_save(flags);
1236 ptr = &__get_cpu_var(page_states);
1237 *(unsigned long*)(ptr + offset) += delta;
1238 local_irq_restore(flags);
1239}
1240
1241EXPORT_SYMBOL(__mod_page_state);
1242
1243void __get_zone_counts(unsigned long *active, unsigned long *inactive,
1244 unsigned long *free, struct pglist_data *pgdat)
1245{
1246 struct zone *zones = pgdat->node_zones;
1247 int i;
1248
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;
1256 }
1257}
1258
1259void get_zone_counts(unsigned long *active,
1260 unsigned long *inactive, unsigned long *free)
1261{
1262 struct pglist_data *pgdat;
1263
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;
1273 }
1274}
1275
1276void si_meminfo(struct sysinfo *val)
1277{
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;
1290}
1291
1292EXPORT_SYMBOL(si_meminfo);
1293
1294#ifdef CONFIG_NUMA
1295void si_meminfo_node(struct sysinfo *val, int nid)
1296{
1297 pg_data_t *pgdat = NODE_DATA(nid);
1298
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;
1304}
1305#endif
1306
1307#define K(x) ((x) << (PAGE_SHIFT-10))
1308
1309/*
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.
1313 */
1314void show_free_areas(void)
1315{
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;
1322
1323 for_each_zone(zone) {
1324 show_node(zone);
1325 printk("%s per-cpu:", zone->name);
1326
1327 if (!zone->present_pages) {
1328 printk(" empty\n");
1329 continue;
1330 } else
1331 printk("\n");
1332
2f96996d 1333 for_each_cpu(cpu) {
1da177e4
LT
1334 struct per_cpu_pageset *pageset;
1335
e7c8d5c9 1336 pageset = zone_pcp(zone, cpu);
1da177e4
LT
1337
1338 for (temperature = 0; temperature < 2; temperature++)
4ae7c039 1339 printk("cpu %d %s: low %d, high %d, batch %d used:%d\n",
1da177e4
LT
1340 cpu,
1341 temperature ? "cold" : "hot",
1342 pageset->pcp[temperature].low,
1343 pageset->pcp[temperature].high,
4ae7c039
CL
1344 pageset->pcp[temperature].batch,
1345 pageset->pcp[temperature].count);
1da177e4
LT
1346 }
1347 }
1348
1349 get_page_state(&ps);
1350 get_zone_counts(&active, &inactive, &free);
1351
c0d62219 1352 printk("Free pages: %11ukB (%ukB HighMem)\n",
1da177e4
LT
1353 K(nr_free_pages()),
1354 K(nr_free_highpages()));
1355
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);
1367
1368 for_each_zone(zone) {
1369 int i;
1370
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")
1393 );
1394 printk("lowmem_reserve[]:");
1395 for (i = 0; i < MAX_NR_ZONES; i++)
1396 printk(" %lu", zone->lowmem_reserve[i]);
1397 printk("\n");
1398 }
1399
1400 for_each_zone(zone) {
1401 unsigned long nr, flags, order, total = 0;
1402
1403 show_node(zone);
1404 printk("%s: ", zone->name);
1405 if (!zone->present_pages) {
1406 printk("empty\n");
1407 continue;
1408 }
1409
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);
1415 }
1416 spin_unlock_irqrestore(&zone->lock, flags);
1417 printk("= %lukB\n", K(total));
1418 }
1419
1420 show_swap_cache_info();
1421}
1422
1423/*
1424 * Builds allocation fallback zone lists.
1425 */
1426static int __init build_zonelists_node(pg_data_t *pgdat, struct zonelist *zonelist, int j, int k)
1427{
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;
1439 }
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;
1448 }
1449
1450 return j;
1451}
1452
260b2367
AV
1453static inline int highest_zone(int zone_bits)
1454{
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;
1461}
1462
1da177e4
LT
1463#ifdef CONFIG_NUMA
1464#define MAX_NODE_LOAD (num_online_nodes())
1465static int __initdata node_load[MAX_NUMNODES];
1466/**
4dc3b16b 1467 * find_next_best_node - find the next node that should appear in a given node's fallback list
1da177e4
LT
1468 * @node: node whose fallback list we're appending
1469 * @used_node_mask: nodemask_t of already used nodes
1470 *
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.
1479 */
1480static int __init find_next_best_node(int node, nodemask_t *used_node_mask)
1481{
1482 int i, n, val;
1483 int min_val = INT_MAX;
1484 int best_node = -1;
1485
1486 for_each_online_node(i) {
1487 cpumask_t tmp;
1488
1489 /* Start from local node */
1490 n = (node+i) % num_online_nodes();
1491
1492 /* Don't want a node to appear more than once */
1493 if (node_isset(n, *used_node_mask))
1494 continue;
1495
1496 /* Use the local node if we haven't already */
1497 if (!node_isset(node, *used_node_mask)) {
1498 best_node = node;
1499 break;
1500 }
1501
1502 /* Use the distance array to find the distance */
1503 val = node_distance(node, n);
1504
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;
1509
1510 /* Slight preference for less loaded node */
1511 val *= (MAX_NODE_LOAD*MAX_NUMNODES);
1512 val += node_load[n];
1513
1514 if (val < min_val) {
1515 min_val = val;
1516 best_node = n;
1517 }
1518 }
1519
1520 if (best_node >= 0)
1521 node_set(best_node, *used_node_mask);
1522
1523 return best_node;
1524}
1525
1526static void __init build_zonelists(pg_data_t *pgdat)
1527{
1528 int i, j, k, node, local_node;
1529 int prev_node, load;
1530 struct zonelist *zonelist;
1531 nodemask_t used_mask;
1532
1533 /* initialize zonelists */
1534 for (i = 0; i < GFP_ZONETYPES; i++) {
1535 zonelist = pgdat->node_zonelists + i;
1536 zonelist->zones[0] = NULL;
1537 }
1538
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) {
1545 /*
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.
1549 */
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++);
1558
260b2367 1559 k = highest_zone(i);
1da177e4
LT
1560
1561 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1562 zonelist->zones[j] = NULL;
1563 }
1564 }
1565}
1566
1567#else /* CONFIG_NUMA */
1568
1569static void __init build_zonelists(pg_data_t *pgdat)
1570{
1571 int i, j, k, node, local_node;
1572
1573 local_node = pgdat->node_id;
1574 for (i = 0; i < GFP_ZONETYPES; i++) {
1575 struct zonelist *zonelist;
1576
1577 zonelist = pgdat->node_zonelists + i;
1578
1579 j = 0;
260b2367 1580 k = highest_zone(i);
1da177e4
LT
1581 j = build_zonelists_node(pgdat, zonelist, j, k);
1582 /*
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)
1589 */
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);
1594 }
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);
1599 }
1600
1601 zonelist->zones[j] = NULL;
1602 }
1603}
1604
1605#endif /* CONFIG_NUMA */
1606
1607void __init build_all_zonelists(void)
1608{
1609 int i;
1610
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();
1615}
1616
1617/*
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.
1624 *
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.
1627 */
1628#define PAGES_PER_WAITQUEUE 256
1629
1630static inline unsigned long wait_table_size(unsigned long pages)
1631{
1632 unsigned long size = 1;
1633
1634 pages /= PAGES_PER_WAITQUEUE;
1635
1636 while (size < pages)
1637 size <<= 1;
1638
1639 /*
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.
1643 */
1644 size = min(size, 4096UL);
1645
1646 return max(size, 4UL);
1647}
1648
1649/*
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.
1653 */
1654static inline unsigned long wait_table_bits(unsigned long size)
1655{
1656 return ffz(~size);
1657}
1658
1659#define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
1660
1661static void __init calculate_zone_totalpages(struct pglist_data *pgdat,
1662 unsigned long *zones_size, unsigned long *zholes_size)
1663{
1664 unsigned long realtotalpages, totalpages = 0;
1665 int i;
1666
1667 for (i = 0; i < MAX_NR_ZONES; i++)
1668 totalpages += zones_size[i];
1669 pgdat->node_spanned_pages = totalpages;
1670
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);
1677}
1678
1679
1680/*
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.
1684 */
3947be19 1685void __devinit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
1da177e4
LT
1686 unsigned long start_pfn)
1687{
1da177e4 1688 struct page *page;
29751f69
AW
1689 unsigned long end_pfn = start_pfn + size;
1690 unsigned long pfn;
1da177e4 1691
d41dee36
AW
1692 for (pfn = start_pfn; pfn < end_pfn; pfn++, page++) {
1693 if (!early_pfn_valid(pfn))
1694 continue;
641c7673
AW
1695 if (!early_pfn_in_nid(pfn, nid))
1696 continue;
d41dee36
AW
1697 page = pfn_to_page(pfn);
1698 set_page_links(page, zone, nid, pfn);
b5810039 1699 set_page_count(page, 1);
1da177e4
LT
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))
3212c6be 1706 set_page_address(page, __va(pfn << PAGE_SHIFT));
1da177e4 1707#endif
1da177e4
LT
1708 }
1709}
1710
1711void zone_init_free_lists(struct pglist_data *pgdat, struct zone *zone,
1712 unsigned long size)
1713{
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;
1718 }
1719}
1720
d41dee36
AW
1721#define ZONETABLE_INDEX(x, zone_nr) ((x << ZONES_SHIFT) | zone_nr)
1722void zonetable_add(struct zone *zone, int nid, int zid, unsigned long pfn,
1723 unsigned long size)
1724{
1725 unsigned long snum = pfn_to_section_nr(pfn);
1726 unsigned long end = pfn_to_section_nr(pfn + size);
1727
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;
1733}
1734
1da177e4
LT
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
1739
e7c8d5c9
CL
1740static int __devinit zone_batchsize(struct zone *zone)
1741{
1742 int batch;
1743
1744 /*
1745 * The per-cpu-pages pools are set to around 1000th of the
ba56e91c 1746 * size of the zone. But no more than 1/2 of a meg.
e7c8d5c9
CL
1747 *
1748 * OK, so we don't know how big the cache is. So guess.
1749 */
1750 batch = zone->present_pages / 1024;
ba56e91c
SR
1751 if (batch * PAGE_SIZE > 512 * 1024)
1752 batch = (512 * 1024) / PAGE_SIZE;
e7c8d5c9
CL
1753 batch /= 4; /* We effectively *= 4 below */
1754 if (batch < 1)
1755 batch = 1;
1756
1757 /*
ba56e91c
SR
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.
e7c8d5c9 1761 *
ba56e91c 1762 * A sanity check also to ensure that batch is still in limits.
e7c8d5c9 1763 */
ba56e91c
SR
1764 batch = (1 << fls(batch + batch/2));
1765
1766 if (fls(batch) >= (PAGE_SHIFT + MAX_ORDER - 2))
1767 batch = PAGE_SHIFT + ((MAX_ORDER - 1 - PAGE_SHIFT)/2);
1768
e7c8d5c9
CL
1769 return batch;
1770}
1771
2caaad41
CL
1772inline void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
1773{
1774 struct per_cpu_pages *pcp;
1775
1c6fe946
MD
1776 memset(p, 0, sizeof(*p));
1777
2caaad41
CL
1778 pcp = &p->pcp[0]; /* hot */
1779 pcp->count = 0;
e46a5e28 1780 pcp->low = 0;
2caaad41
CL
1781 pcp->high = 6 * batch;
1782 pcp->batch = max(1UL, 1 * batch);
1783 INIT_LIST_HEAD(&pcp->list);
1784
1785 pcp = &p->pcp[1]; /* cold*/
1786 pcp->count = 0;
1787 pcp->low = 0;
1788 pcp->high = 2 * batch;
e46a5e28 1789 pcp->batch = max(1UL, batch/2);
2caaad41
CL
1790 INIT_LIST_HEAD(&pcp->list);
1791}
1792
e7c8d5c9
CL
1793#ifdef CONFIG_NUMA
1794/*
2caaad41
CL
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.
1800 *
1801 * Some NUMA counter updates may also be caught by the boot pagesets.
b7c84c6a
CL
1802 *
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.
1806 *
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.
2caaad41
CL
1810 */
1811static struct per_cpu_pageset
b7c84c6a 1812 boot_pageset[NR_CPUS];
2caaad41
CL
1813
1814/*
1815 * Dynamically allocate memory for the
e7c8d5c9
CL
1816 * per cpu pageset array in struct zone.
1817 */
1818static int __devinit process_zones(int cpu)
1819{
1820 struct zone *zone, *dzone;
e7c8d5c9
CL
1821
1822 for_each_zone(zone) {
e7c8d5c9 1823
2caaad41 1824 zone->pageset[cpu] = kmalloc_node(sizeof(struct per_cpu_pageset),
e7c8d5c9 1825 GFP_KERNEL, cpu_to_node(cpu));
2caaad41 1826 if (!zone->pageset[cpu])
e7c8d5c9 1827 goto bad;
e7c8d5c9 1828
2caaad41 1829 setup_pageset(zone->pageset[cpu], zone_batchsize(zone));
e7c8d5c9
CL
1830 }
1831
1832 return 0;
1833bad:
1834 for_each_zone(dzone) {
1835 if (dzone == zone)
1836 break;
1837 kfree(dzone->pageset[cpu]);
1838 dzone->pageset[cpu] = NULL;
1839 }
1840 return -ENOMEM;
1841}
1842
1843static inline void free_zone_pagesets(int cpu)
1844{
1845#ifdef CONFIG_NUMA
1846 struct zone *zone;
1847
1848 for_each_zone(zone) {
1849 struct per_cpu_pageset *pset = zone_pcp(zone, cpu);
1850
1851 zone_pcp(zone, cpu) = NULL;
1852 kfree(pset);
1853 }
1854#endif
1855}
1856
1857static int __devinit pageset_cpuup_callback(struct notifier_block *nfb,
1858 unsigned long action,
1859 void *hcpu)
1860{
1861 int cpu = (long)hcpu;
1862 int ret = NOTIFY_OK;
1863
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;
1876 }
1877 return ret;
1878}
1879
1880static struct notifier_block pageset_notifier =
1881 { &pageset_cpuup_callback, NULL, 0 };
1882
1883void __init setup_per_cpu_pageset()
1884{
1885 int err;
1886
1887 /* Initialize per_cpu_pageset for cpu 0.
1888 * A cpuup callback will do this for every cpu
1889 * as it comes online
1890 */
1891 err = process_zones(smp_processor_id());
1892 BUG_ON(err);
1893 register_cpu_notifier(&pageset_notifier);
1894}
1895
1896#endif
1897
ed8ece2e
DH
1898static __devinit
1899void zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages)
1900{
1901 int i;
1902 struct pglist_data *pgdat = zone->zone_pgdat;
1903
1904 /*
1905 * The per-page waitqueue mechanism uses hashed waitqueues
1906 * per zone.
1907 */
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));
1913
1914 for(i = 0; i < zone->wait_table_size; ++i)
1915 init_waitqueue_head(zone->wait_table + i);
1916}
1917
1918static __devinit void zone_pcp_init(struct zone *zone)
1919{
1920 int cpu;
1921 unsigned long batch = zone_batchsize(zone);
1922
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
1931 }
1932 printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%lu\n",
1933 zone->name, zone->present_pages, batch);
1934}
1935
1936static __devinit void init_currently_empty_zone(struct zone *zone,
1937 unsigned long zone_start_pfn, unsigned long size)
1938{
1939 struct pglist_data *pgdat = zone->zone_pgdat;
1940
1941 zone_wait_table_init(zone, size);
1942 pgdat->nr_zones = zone_idx(zone) + 1;
1943
1944 zone->zone_mem_map = pfn_to_page(zone_start_pfn);
1945 zone->zone_start_pfn = zone_start_pfn;
1946
1947 memmap_init(size, pgdat->node_id, zone_idx(zone), zone_start_pfn);
1948
1949 zone_init_free_lists(pgdat, zone, zone->spanned_pages);
1950}
1951
1da177e4
LT
1952/*
1953 * Set up the zone data structures:
1954 * - mark all pages reserved
1955 * - mark all memory queues empty
1956 * - clear the memory bitmaps
1957 */
1958static void __init free_area_init_core(struct pglist_data *pgdat,
1959 unsigned long *zones_size, unsigned long *zholes_size)
1960{
ed8ece2e
DH
1961 unsigned long j;
1962 int nid = pgdat->node_id;
1da177e4
LT
1963 unsigned long zone_start_pfn = pgdat->node_start_pfn;
1964
208d54e5 1965 pgdat_resize_init(pgdat);
1da177e4
LT
1966 pgdat->nr_zones = 0;
1967 init_waitqueue_head(&pgdat->kswapd_wait);
1968 pgdat->kswapd_max_order = 0;
1969
1970 for (j = 0; j < MAX_NR_ZONES; j++) {
1971 struct zone *zone = pgdat->node_zones + j;
1972 unsigned long size, realsize;
1da177e4 1973
1da177e4
LT
1974 realsize = size = zones_size[j];
1975 if (zholes_size)
1976 realsize -= zholes_size[j];
1977
1978 if (j == ZONE_DMA || j == ZONE_NORMAL)
1979 nr_kernel_pages += realsize;
1980 nr_all_pages += realsize;
1981
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);
bdc8cb98 1987 zone_seqlock_init(zone);
1da177e4
LT
1988 zone->zone_pgdat = pgdat;
1989 zone->free_pages = 0;
1990
1991 zone->temp_priority = zone->prev_priority = DEF_PRIORITY;
1992
ed8ece2e 1993 zone_pcp_init(zone);
1da177e4
LT
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;
53e9a615 2000 atomic_set(&zone->reclaim_in_progress, 0);
1da177e4
LT
2001 if (!size)
2002 continue;
2003
d41dee36 2004 zonetable_add(zone, nid, j, zone_start_pfn, size);
ed8ece2e 2005 init_currently_empty_zone(zone, zone_start_pfn, size);
1da177e4 2006 zone_start_pfn += size;
1da177e4
LT
2007 }
2008}
2009
2010static void __init alloc_node_mem_map(struct pglist_data *pgdat)
2011{
1da177e4
LT
2012 /* Skip empty nodes */
2013 if (!pgdat->node_spanned_pages)
2014 return;
2015
d41dee36 2016#ifdef CONFIG_FLAT_NODE_MEM_MAP
1da177e4
LT
2017 /* ia64 gets its own node_mem_map, before this, without bootmem */
2018 if (!pgdat->node_mem_map) {
d41dee36
AW
2019 unsigned long size;
2020 struct page *map;
2021
1da177e4 2022 size = (pgdat->node_spanned_pages + 1) * sizeof(struct page);
6f167ec7
DH
2023 map = alloc_remap(pgdat->node_id, size);
2024 if (!map)
2025 map = alloc_bootmem_node(pgdat, size);
2026 pgdat->node_mem_map = map;
1da177e4 2027 }
d41dee36 2028#ifdef CONFIG_FLATMEM
1da177e4
LT
2029 /*
2030 * With no DISCONTIG, the global mem_map is just set as node 0's
2031 */
2032 if (pgdat == NODE_DATA(0))
2033 mem_map = NODE_DATA(0)->node_mem_map;
2034#endif
d41dee36 2035#endif /* CONFIG_FLAT_NODE_MEM_MAP */
1da177e4
LT
2036}
2037
2038void __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)
2041{
2042 pgdat->node_id = nid;
2043 pgdat->node_start_pfn = node_start_pfn;
2044 calculate_zone_totalpages(pgdat, zones_size, zholes_size);
2045
2046 alloc_node_mem_map(pgdat);
2047
2048 free_area_init_core(pgdat, zones_size, zholes_size);
2049}
2050
93b7504e 2051#ifndef CONFIG_NEED_MULTIPLE_NODES
1da177e4
LT
2052static bootmem_data_t contig_bootmem_data;
2053struct pglist_data contig_page_data = { .bdata = &contig_bootmem_data };
2054
2055EXPORT_SYMBOL(contig_page_data);
93b7504e 2056#endif
1da177e4
LT
2057
2058void __init free_area_init(unsigned long *zones_size)
2059{
93b7504e 2060 free_area_init_node(0, NODE_DATA(0), zones_size,
1da177e4
LT
2061 __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
2062}
1da177e4
LT
2063
2064#ifdef CONFIG_PROC_FS
2065
2066#include <linux/seq_file.h>
2067
2068static void *frag_start(struct seq_file *m, loff_t *pos)
2069{
2070 pg_data_t *pgdat;
2071 loff_t node = *pos;
2072
2073 for (pgdat = pgdat_list; pgdat && node; pgdat = pgdat->pgdat_next)
2074 --node;
2075
2076 return pgdat;
2077}
2078
2079static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
2080{
2081 pg_data_t *pgdat = (pg_data_t *)arg;
2082
2083 (*pos)++;
2084 return pgdat->pgdat_next;
2085}
2086
2087static void frag_stop(struct seq_file *m, void *arg)
2088{
2089}
2090
2091/*
2092 * This walks the free areas for each zone.
2093 */
2094static int frag_show(struct seq_file *m, void *arg)
2095{
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;
2101
2102 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
2103 if (!zone->present_pages)
2104 continue;
2105
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');
2112 }
2113 return 0;
2114}
2115
2116struct seq_operations fragmentation_op = {
2117 .start = frag_start,
2118 .next = frag_next,
2119 .stop = frag_stop,
2120 .show = frag_show,
2121};
2122
295ab934
ND
2123/*
2124 * Output information about zones in @pgdat.
2125 */
2126static int zoneinfo_show(struct seq_file *m, void *arg)
2127{
2128 pg_data_t *pgdat = arg;
2129 struct zone *zone;
2130 struct zone *node_zones = pgdat->node_zones;
2131 unsigned long flags;
2132
2133 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; zone++) {
2134 int i;
2135
2136 if (!zone->present_pages)
2137 continue;
2138
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,
2167 ")"
2168 "\n pagesets");
2169 for (i = 0; i < ARRAY_SIZE(zone->pageset); i++) {
2170 struct per_cpu_pageset *pageset;
2171 int j;
2172
e7c8d5c9 2173 pageset = zone_pcp(zone, i);
295ab934
ND
2174 for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
2175 if (pageset->pcp[j].count)
2176 break;
2177 }
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);
2192 }
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
2208 }
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');
2220 }
2221 return 0;
2222}
2223
2224struct 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,
2230};
2231
1da177e4
LT
2232static char *vmstat_text[] = {
2233 "nr_dirty",
2234 "nr_writeback",
2235 "nr_unstable",
2236 "nr_page_table_pages",
2237 "nr_mapped",
2238 "nr_slab",
2239
2240 "pgpgin",
2241 "pgpgout",
2242 "pswpin",
2243 "pswpout",
2244 "pgalloc_high",
2245
2246 "pgalloc_normal",
2247 "pgalloc_dma",
2248 "pgfree",
2249 "pgactivate",
2250 "pgdeactivate",
2251
2252 "pgfault",
2253 "pgmajfault",
2254 "pgrefill_high",
2255 "pgrefill_normal",
2256 "pgrefill_dma",
2257
2258 "pgsteal_high",
2259 "pgsteal_normal",
2260 "pgsteal_dma",
2261 "pgscan_kswapd_high",
2262 "pgscan_kswapd_normal",
2263
2264 "pgscan_kswapd_dma",
2265 "pgscan_direct_high",
2266 "pgscan_direct_normal",
2267 "pgscan_direct_dma",
2268 "pginodesteal",
2269
2270 "slabs_scanned",
2271 "kswapd_steal",
2272 "kswapd_inodesteal",
2273 "pageoutrun",
2274 "allocstall",
2275
2276 "pgrotated",
edfbe2b0 2277 "nr_bounce",
1da177e4
LT
2278};
2279
2280static void *vmstat_start(struct seq_file *m, loff_t *pos)
2281{
2282 struct page_state *ps;
2283
2284 if (*pos >= ARRAY_SIZE(vmstat_text))
2285 return NULL;
2286
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;
2295}
2296
2297static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
2298{
2299 (*pos)++;
2300 if (*pos >= ARRAY_SIZE(vmstat_text))
2301 return NULL;
2302 return (unsigned long *)m->private + *pos;
2303}
2304
2305static int vmstat_show(struct seq_file *m, void *arg)
2306{
2307 unsigned long *l = arg;
2308 unsigned long off = l - (unsigned long *)m->private;
2309
2310 seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
2311 return 0;
2312}
2313
2314static void vmstat_stop(struct seq_file *m, void *arg)
2315{
2316 kfree(m->private);
2317 m->private = NULL;
2318}
2319
2320struct seq_operations vmstat_op = {
2321 .start = vmstat_start,
2322 .next = vmstat_next,
2323 .stop = vmstat_stop,
2324 .show = vmstat_show,
2325};
2326
2327#endif /* CONFIG_PROC_FS */
2328
2329#ifdef CONFIG_HOTPLUG_CPU
2330static int page_alloc_cpu_notify(struct notifier_block *self,
2331 unsigned long action, void *hcpu)
2332{
2333 int cpu = (unsigned long)hcpu;
2334 long *count;
2335 unsigned long *src, *dest;
2336
2337 if (action == CPU_DEAD) {
2338 int i;
2339
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);
2346
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);
2350
2351 for (i = 0; i < sizeof(struct page_state)/sizeof(unsigned long);
2352 i++) {
2353 dest[i] += src[i];
2354 src[i] = 0;
2355 }
2356
2357 local_irq_enable();
2358 }
2359 return NOTIFY_OK;
2360}
2361#endif /* CONFIG_HOTPLUG_CPU */
2362
2363void __init page_alloc_init(void)
2364{
2365 hotcpu_notifier(page_alloc_cpu_notify, 0);
2366}
2367
2368/*
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().
2373 */
2374static void setup_per_zone_lowmem_reserve(void)
2375{
2376 struct pglist_data *pgdat;
2377 int j, idx;
2378
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;
2383
2384 zone->lowmem_reserve[j] = 0;
2385
2386 for (idx = j-1; idx >= 0; idx--) {
2387 struct zone *lower_zone;
2388
2389 if (sysctl_lowmem_reserve_ratio[idx] < 1)
2390 sysctl_lowmem_reserve_ratio[idx] = 1;
2391
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;
2396 }
2397 }
2398 }
2399}
2400
2401/*
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.
2405 */
3947be19 2406void setup_per_zone_pages_min(void)
1da177e4
LT
2407{
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;
2412
2413 /* Calculate total number of !ZONE_HIGHMEM pages */
2414 for_each_zone(zone) {
2415 if (!is_highmem(zone))
2416 lowmem_pages += zone->present_pages;
2417 }
2418
2419 for_each_zone(zone) {
2420 spin_lock_irqsave(&zone->lru_lock, flags);
2421 if (is_highmem(zone)) {
2422 /*
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.
2427 */
2428 int min_pages;
2429
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 {
295ab934 2437 /* if it's a lowmem zone, reserve a number of pages
1da177e4
LT
2438 * proportionate to the zone's size.
2439 */
295ab934 2440 zone->pages_min = (pages_min * zone->present_pages) /
1da177e4
LT
2441 lowmem_pages;
2442 }
2443
2444 /*
2445 * When interpreting these watermarks, just keep in mind that:
2446 * zone->pages_min == (zone->pages_min * 4) / 4;
2447 */
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);
2451 }
2452}
2453
2454/*
2455 * Initialise min_free_kbytes.
2456 *
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
2460 *
2461 * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
2462 * min_free_kbytes = sqrt(lowmem_kbytes * 16)
2463 *
2464 * which yields
2465 *
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
2477 */
2478static int __init init_per_zone_pages_min(void)
2479{
2480 unsigned long lowmem_kbytes;
2481
2482 lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
2483
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;
2492}
2493module_init(init_per_zone_pages_min)
2494
2495/*
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.
2499 */
2500int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
2501 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
2502{
2503 proc_dointvec(table, write, file, buffer, length, ppos);
2504 setup_per_zone_pages_min();
2505 return 0;
2506}
2507
2508/*
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.
2512 *
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.
2516 */
2517int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
2518 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
2519{
2520 proc_dointvec_minmax(table, write, file, buffer, length, ppos);
2521 setup_per_zone_lowmem_reserve();
2522 return 0;
2523}
2524
2525__initdata int hashdist = HASHDIST_DEFAULT;
2526
2527#ifdef CONFIG_NUMA
2528static int __init set_hashdist(char *str)
2529{
2530 if (!str)
2531 return 0;
2532 hashdist = simple_strtoul(str, &str, 0);
2533 return 1;
2534}
2535__setup("hashdist=", set_hashdist);
2536#endif
2537
2538/*
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
2543 */
2544void *__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)
2552{
2553 unsigned long long max = limit;
2554 unsigned long log2qty, size;
2555 void *table = NULL;
2556
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;
2564
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);
2570 }
2571 /* rounded up to nearest power of 2 in size */
2572 numentries = 1UL << (long_log2(numentries) + 1);
2573
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);
2578 }
2579
2580 if (numentries > max)
2581 numentries = max;
2582
2583 log2qty = long_log2(numentries);
2584
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++)
2594 ;
2595 table = (void*) __get_free_pages(GFP_ATOMIC, order);
2596 }
2597 } while (!table && size > PAGE_SIZE && --log2qty);
2598
2599 if (!table)
2600 panic("Failed to allocate %s hash table\n", tablename);
2601
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);
2607
2608 if (_hash_shift)
2609 *_hash_shift = log2qty;
2610 if (_hash_mask)
2611 *_hash_mask = (1 << log2qty) - 1;
2612
2613 return table;
2614}