]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - mm/page_alloc.c
[PATCH] mm: ZAP_BLOCK causes redundant work
[mirror_ubuntu-bionic-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>
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)) {
885036d3 898nofail_alloc:
b84a35be
NP
899 /* go through the zonelist yet again, ignoring mins */
900 for (i = 0; (z = zones[i]) != NULL; i++) {
9bf2229f 901 if (!cpuset_zone_allowed(z, gfp_mask))
b84a35be
NP
902 continue;
903 page = buffered_rmqueue(z, order, gfp_mask);
904 if (page)
905 goto got_pg;
906 }
885036d3
KK
907 if (gfp_mask & __GFP_NOFAIL) {
908 blk_congestion_wait(WRITE, HZ/50);
909 goto nofail_alloc;
910 }
1da177e4
LT
911 }
912 goto nopage;
913 }
914
915 /* Atomic allocations - we can't balance anything */
916 if (!wait)
917 goto nopage;
918
919rebalance:
920 cond_resched();
921
922 /* We now go into synchronous reclaim */
923 p->flags |= PF_MEMALLOC;
924 reclaim_state.reclaimed_slab = 0;
925 p->reclaim_state = &reclaim_state;
926
1ad539b2 927 did_some_progress = try_to_free_pages(zones, gfp_mask);
1da177e4
LT
928
929 p->reclaim_state = NULL;
930 p->flags &= ~PF_MEMALLOC;
931
932 cond_resched();
933
934 if (likely(did_some_progress)) {
1da177e4
LT
935 for (i = 0; (z = zones[i]) != NULL; i++) {
936 if (!zone_watermark_ok(z, order, z->pages_min,
937 classzone_idx, can_try_harder,
938 gfp_mask & __GFP_HIGH))
939 continue;
940
9bf2229f 941 if (!cpuset_zone_allowed(z, gfp_mask))
1da177e4
LT
942 continue;
943
944 page = buffered_rmqueue(z, order, gfp_mask);
945 if (page)
946 goto got_pg;
947 }
948 } else if ((gfp_mask & __GFP_FS) && !(gfp_mask & __GFP_NORETRY)) {
949 /*
950 * Go through the zonelist yet one more time, keep
951 * very high watermark here, this is only to catch
952 * a parallel oom killing, we must fail if we're still
953 * under heavy pressure.
954 */
955 for (i = 0; (z = zones[i]) != NULL; i++) {
956 if (!zone_watermark_ok(z, order, z->pages_high,
957 classzone_idx, 0, 0))
958 continue;
959
9bf2229f 960 if (!cpuset_zone_allowed(z, __GFP_HARDWALL))
1da177e4
LT
961 continue;
962
963 page = buffered_rmqueue(z, order, gfp_mask);
964 if (page)
965 goto got_pg;
966 }
967
79b9ce31 968 out_of_memory(gfp_mask, order);
1da177e4
LT
969 goto restart;
970 }
971
972 /*
973 * Don't let big-order allocations loop unless the caller explicitly
974 * requests that. Wait for some write requests to complete then retry.
975 *
976 * In this implementation, __GFP_REPEAT means __GFP_NOFAIL for order
977 * <= 3, but that may not be true in other implementations.
978 */
979 do_retry = 0;
980 if (!(gfp_mask & __GFP_NORETRY)) {
981 if ((order <= 3) || (gfp_mask & __GFP_REPEAT))
982 do_retry = 1;
983 if (gfp_mask & __GFP_NOFAIL)
984 do_retry = 1;
985 }
986 if (do_retry) {
987 blk_congestion_wait(WRITE, HZ/50);
988 goto rebalance;
989 }
990
991nopage:
992 if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) {
993 printk(KERN_WARNING "%s: page allocation failure."
994 " order:%d, mode:0x%x\n",
995 p->comm, order, gfp_mask);
996 dump_stack();
578c2fd6 997 show_mem();
1da177e4
LT
998 }
999 return NULL;
1000got_pg:
1001 zone_statistics(zonelist, z);
1002 return page;
1003}
1004
1005EXPORT_SYMBOL(__alloc_pages);
1006
1007/*
1008 * Common helper functions.
1009 */
dd0fc66f 1010fastcall unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
1da177e4
LT
1011{
1012 struct page * page;
1013 page = alloc_pages(gfp_mask, order);
1014 if (!page)
1015 return 0;
1016 return (unsigned long) page_address(page);
1017}
1018
1019EXPORT_SYMBOL(__get_free_pages);
1020
dd0fc66f 1021fastcall unsigned long get_zeroed_page(gfp_t gfp_mask)
1da177e4
LT
1022{
1023 struct page * page;
1024
1025 /*
1026 * get_zeroed_page() returns a 32-bit address, which cannot represent
1027 * a highmem page
1028 */
260b2367 1029 BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
1da177e4
LT
1030
1031 page = alloc_pages(gfp_mask | __GFP_ZERO, 0);
1032 if (page)
1033 return (unsigned long) page_address(page);
1034 return 0;
1035}
1036
1037EXPORT_SYMBOL(get_zeroed_page);
1038
1039void __pagevec_free(struct pagevec *pvec)
1040{
1041 int i = pagevec_count(pvec);
1042
1043 while (--i >= 0)
1044 free_hot_cold_page(pvec->pages[i], pvec->cold);
1045}
1046
1047fastcall void __free_pages(struct page *page, unsigned int order)
1048{
b5810039 1049 if (put_page_testzero(page)) {
1da177e4
LT
1050 if (order == 0)
1051 free_hot_page(page);
1052 else
1053 __free_pages_ok(page, order);
1054 }
1055}
1056
1057EXPORT_SYMBOL(__free_pages);
1058
1059fastcall void free_pages(unsigned long addr, unsigned int order)
1060{
1061 if (addr != 0) {
1062 BUG_ON(!virt_addr_valid((void *)addr));
1063 __free_pages(virt_to_page((void *)addr), order);
1064 }
1065}
1066
1067EXPORT_SYMBOL(free_pages);
1068
1069/*
1070 * Total amount of free (allocatable) RAM:
1071 */
1072unsigned int nr_free_pages(void)
1073{
1074 unsigned int sum = 0;
1075 struct zone *zone;
1076
1077 for_each_zone(zone)
1078 sum += zone->free_pages;
1079
1080 return sum;
1081}
1082
1083EXPORT_SYMBOL(nr_free_pages);
1084
1085#ifdef CONFIG_NUMA
1086unsigned int nr_free_pages_pgdat(pg_data_t *pgdat)
1087{
1088 unsigned int i, sum = 0;
1089
1090 for (i = 0; i < MAX_NR_ZONES; i++)
1091 sum += pgdat->node_zones[i].free_pages;
1092
1093 return sum;
1094}
1095#endif
1096
1097static unsigned int nr_free_zone_pages(int offset)
1098{
e310fd43
MB
1099 /* Just pick one node, since fallback list is circular */
1100 pg_data_t *pgdat = NODE_DATA(numa_node_id());
1da177e4
LT
1101 unsigned int sum = 0;
1102
e310fd43
MB
1103 struct zonelist *zonelist = pgdat->node_zonelists + offset;
1104 struct zone **zonep = zonelist->zones;
1105 struct zone *zone;
1da177e4 1106
e310fd43
MB
1107 for (zone = *zonep++; zone; zone = *zonep++) {
1108 unsigned long size = zone->present_pages;
1109 unsigned long high = zone->pages_high;
1110 if (size > high)
1111 sum += size - high;
1da177e4
LT
1112 }
1113
1114 return sum;
1115}
1116
1117/*
1118 * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
1119 */
1120unsigned int nr_free_buffer_pages(void)
1121{
af4ca457 1122 return nr_free_zone_pages(gfp_zone(GFP_USER));
1da177e4
LT
1123}
1124
1125/*
1126 * Amount of free RAM allocatable within all zones
1127 */
1128unsigned int nr_free_pagecache_pages(void)
1129{
af4ca457 1130 return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER));
1da177e4
LT
1131}
1132
1133#ifdef CONFIG_HIGHMEM
1134unsigned int nr_free_highpages (void)
1135{
1136 pg_data_t *pgdat;
1137 unsigned int pages = 0;
1138
1139 for_each_pgdat(pgdat)
1140 pages += pgdat->node_zones[ZONE_HIGHMEM].free_pages;
1141
1142 return pages;
1143}
1144#endif
1145
1146#ifdef CONFIG_NUMA
1147static void show_node(struct zone *zone)
1148{
1149 printk("Node %d ", zone->zone_pgdat->node_id);
1150}
1151#else
1152#define show_node(zone) do { } while (0)
1153#endif
1154
1155/*
1156 * Accumulate the page_state information across all CPUs.
1157 * The result is unavoidably approximate - it can change
1158 * during and after execution of this function.
1159 */
1160static DEFINE_PER_CPU(struct page_state, page_states) = {0};
1161
1162atomic_t nr_pagecache = ATOMIC_INIT(0);
1163EXPORT_SYMBOL(nr_pagecache);
1164#ifdef CONFIG_SMP
1165DEFINE_PER_CPU(long, nr_pagecache_local) = 0;
1166#endif
1167
c07e02db 1168void __get_page_state(struct page_state *ret, int nr, cpumask_t *cpumask)
1da177e4
LT
1169{
1170 int cpu = 0;
1171
1172 memset(ret, 0, sizeof(*ret));
c07e02db 1173 cpus_and(*cpumask, *cpumask, cpu_online_map);
1da177e4 1174
c07e02db 1175 cpu = first_cpu(*cpumask);
1da177e4
LT
1176 while (cpu < NR_CPUS) {
1177 unsigned long *in, *out, off;
1178
1179 in = (unsigned long *)&per_cpu(page_states, cpu);
1180
c07e02db 1181 cpu = next_cpu(cpu, *cpumask);
1da177e4
LT
1182
1183 if (cpu < NR_CPUS)
1184 prefetch(&per_cpu(page_states, cpu));
1185
1186 out = (unsigned long *)ret;
1187 for (off = 0; off < nr; off++)
1188 *out++ += *in++;
1189 }
1190}
1191
c07e02db
MH
1192void get_page_state_node(struct page_state *ret, int node)
1193{
1194 int nr;
1195 cpumask_t mask = node_to_cpumask(node);
1196
1197 nr = offsetof(struct page_state, GET_PAGE_STATE_LAST);
1198 nr /= sizeof(unsigned long);
1199
1200 __get_page_state(ret, nr+1, &mask);
1201}
1202
1da177e4
LT
1203void get_page_state(struct page_state *ret)
1204{
1205 int nr;
c07e02db 1206 cpumask_t mask = CPU_MASK_ALL;
1da177e4
LT
1207
1208 nr = offsetof(struct page_state, GET_PAGE_STATE_LAST);
1209 nr /= sizeof(unsigned long);
1210
c07e02db 1211 __get_page_state(ret, nr + 1, &mask);
1da177e4
LT
1212}
1213
1214void get_full_page_state(struct page_state *ret)
1215{
c07e02db
MH
1216 cpumask_t mask = CPU_MASK_ALL;
1217
1218 __get_page_state(ret, sizeof(*ret) / sizeof(unsigned long), &mask);
1da177e4
LT
1219}
1220
c2f29ea1 1221unsigned long __read_page_state(unsigned long offset)
1da177e4
LT
1222{
1223 unsigned long ret = 0;
1224 int cpu;
1225
1226 for_each_online_cpu(cpu) {
1227 unsigned long in;
1228
1229 in = (unsigned long)&per_cpu(page_states, cpu) + offset;
1230 ret += *((unsigned long *)in);
1231 }
1232 return ret;
1233}
1234
83e5d8f7 1235void __mod_page_state(unsigned long offset, unsigned long delta)
1da177e4
LT
1236{
1237 unsigned long flags;
1238 void* ptr;
1239
1240 local_irq_save(flags);
1241 ptr = &__get_cpu_var(page_states);
1242 *(unsigned long*)(ptr + offset) += delta;
1243 local_irq_restore(flags);
1244}
1245
1246EXPORT_SYMBOL(__mod_page_state);
1247
1248void __get_zone_counts(unsigned long *active, unsigned long *inactive,
1249 unsigned long *free, struct pglist_data *pgdat)
1250{
1251 struct zone *zones = pgdat->node_zones;
1252 int i;
1253
1254 *active = 0;
1255 *inactive = 0;
1256 *free = 0;
1257 for (i = 0; i < MAX_NR_ZONES; i++) {
1258 *active += zones[i].nr_active;
1259 *inactive += zones[i].nr_inactive;
1260 *free += zones[i].free_pages;
1261 }
1262}
1263
1264void get_zone_counts(unsigned long *active,
1265 unsigned long *inactive, unsigned long *free)
1266{
1267 struct pglist_data *pgdat;
1268
1269 *active = 0;
1270 *inactive = 0;
1271 *free = 0;
1272 for_each_pgdat(pgdat) {
1273 unsigned long l, m, n;
1274 __get_zone_counts(&l, &m, &n, pgdat);
1275 *active += l;
1276 *inactive += m;
1277 *free += n;
1278 }
1279}
1280
1281void si_meminfo(struct sysinfo *val)
1282{
1283 val->totalram = totalram_pages;
1284 val->sharedram = 0;
1285 val->freeram = nr_free_pages();
1286 val->bufferram = nr_blockdev_pages();
1287#ifdef CONFIG_HIGHMEM
1288 val->totalhigh = totalhigh_pages;
1289 val->freehigh = nr_free_highpages();
1290#else
1291 val->totalhigh = 0;
1292 val->freehigh = 0;
1293#endif
1294 val->mem_unit = PAGE_SIZE;
1295}
1296
1297EXPORT_SYMBOL(si_meminfo);
1298
1299#ifdef CONFIG_NUMA
1300void si_meminfo_node(struct sysinfo *val, int nid)
1301{
1302 pg_data_t *pgdat = NODE_DATA(nid);
1303
1304 val->totalram = pgdat->node_present_pages;
1305 val->freeram = nr_free_pages_pgdat(pgdat);
1306 val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].present_pages;
1307 val->freehigh = pgdat->node_zones[ZONE_HIGHMEM].free_pages;
1308 val->mem_unit = PAGE_SIZE;
1309}
1310#endif
1311
1312#define K(x) ((x) << (PAGE_SHIFT-10))
1313
1314/*
1315 * Show free area list (used inside shift_scroll-lock stuff)
1316 * We also calculate the percentage fragmentation. We do this by counting the
1317 * memory on each free list with the exception of the first item on the list.
1318 */
1319void show_free_areas(void)
1320{
1321 struct page_state ps;
1322 int cpu, temperature;
1323 unsigned long active;
1324 unsigned long inactive;
1325 unsigned long free;
1326 struct zone *zone;
1327
1328 for_each_zone(zone) {
1329 show_node(zone);
1330 printk("%s per-cpu:", zone->name);
1331
1332 if (!zone->present_pages) {
1333 printk(" empty\n");
1334 continue;
1335 } else
1336 printk("\n");
1337
6b482c67 1338 for_each_online_cpu(cpu) {
1da177e4
LT
1339 struct per_cpu_pageset *pageset;
1340
e7c8d5c9 1341 pageset = zone_pcp(zone, cpu);
1da177e4
LT
1342
1343 for (temperature = 0; temperature < 2; temperature++)
4ae7c039 1344 printk("cpu %d %s: low %d, high %d, batch %d used:%d\n",
1da177e4
LT
1345 cpu,
1346 temperature ? "cold" : "hot",
1347 pageset->pcp[temperature].low,
1348 pageset->pcp[temperature].high,
4ae7c039
CL
1349 pageset->pcp[temperature].batch,
1350 pageset->pcp[temperature].count);
1da177e4
LT
1351 }
1352 }
1353
1354 get_page_state(&ps);
1355 get_zone_counts(&active, &inactive, &free);
1356
c0d62219 1357 printk("Free pages: %11ukB (%ukB HighMem)\n",
1da177e4
LT
1358 K(nr_free_pages()),
1359 K(nr_free_highpages()));
1360
1361 printk("Active:%lu inactive:%lu dirty:%lu writeback:%lu "
1362 "unstable:%lu free:%u slab:%lu mapped:%lu pagetables:%lu\n",
1363 active,
1364 inactive,
1365 ps.nr_dirty,
1366 ps.nr_writeback,
1367 ps.nr_unstable,
1368 nr_free_pages(),
1369 ps.nr_slab,
1370 ps.nr_mapped,
1371 ps.nr_page_table_pages);
1372
1373 for_each_zone(zone) {
1374 int i;
1375
1376 show_node(zone);
1377 printk("%s"
1378 " free:%lukB"
1379 " min:%lukB"
1380 " low:%lukB"
1381 " high:%lukB"
1382 " active:%lukB"
1383 " inactive:%lukB"
1384 " present:%lukB"
1385 " pages_scanned:%lu"
1386 " all_unreclaimable? %s"
1387 "\n",
1388 zone->name,
1389 K(zone->free_pages),
1390 K(zone->pages_min),
1391 K(zone->pages_low),
1392 K(zone->pages_high),
1393 K(zone->nr_active),
1394 K(zone->nr_inactive),
1395 K(zone->present_pages),
1396 zone->pages_scanned,
1397 (zone->all_unreclaimable ? "yes" : "no")
1398 );
1399 printk("lowmem_reserve[]:");
1400 for (i = 0; i < MAX_NR_ZONES; i++)
1401 printk(" %lu", zone->lowmem_reserve[i]);
1402 printk("\n");
1403 }
1404
1405 for_each_zone(zone) {
1406 unsigned long nr, flags, order, total = 0;
1407
1408 show_node(zone);
1409 printk("%s: ", zone->name);
1410 if (!zone->present_pages) {
1411 printk("empty\n");
1412 continue;
1413 }
1414
1415 spin_lock_irqsave(&zone->lock, flags);
1416 for (order = 0; order < MAX_ORDER; order++) {
1417 nr = zone->free_area[order].nr_free;
1418 total += nr << order;
1419 printk("%lu*%lukB ", nr, K(1UL) << order);
1420 }
1421 spin_unlock_irqrestore(&zone->lock, flags);
1422 printk("= %lukB\n", K(total));
1423 }
1424
1425 show_swap_cache_info();
1426}
1427
1428/*
1429 * Builds allocation fallback zone lists.
1430 */
1431static int __init build_zonelists_node(pg_data_t *pgdat, struct zonelist *zonelist, int j, int k)
1432{
1433 switch (k) {
1434 struct zone *zone;
1435 default:
1436 BUG();
1437 case ZONE_HIGHMEM:
1438 zone = pgdat->node_zones + ZONE_HIGHMEM;
1439 if (zone->present_pages) {
1440#ifndef CONFIG_HIGHMEM
1441 BUG();
1442#endif
1443 zonelist->zones[j++] = zone;
1444 }
1445 case ZONE_NORMAL:
1446 zone = pgdat->node_zones + ZONE_NORMAL;
1447 if (zone->present_pages)
1448 zonelist->zones[j++] = zone;
1449 case ZONE_DMA:
1450 zone = pgdat->node_zones + ZONE_DMA;
1451 if (zone->present_pages)
1452 zonelist->zones[j++] = zone;
1453 }
1454
1455 return j;
1456}
1457
260b2367
AV
1458static inline int highest_zone(int zone_bits)
1459{
1460 int res = ZONE_NORMAL;
1461 if (zone_bits & (__force int)__GFP_HIGHMEM)
1462 res = ZONE_HIGHMEM;
1463 if (zone_bits & (__force int)__GFP_DMA)
1464 res = ZONE_DMA;
1465 return res;
1466}
1467
1da177e4
LT
1468#ifdef CONFIG_NUMA
1469#define MAX_NODE_LOAD (num_online_nodes())
1470static int __initdata node_load[MAX_NUMNODES];
1471/**
4dc3b16b 1472 * find_next_best_node - find the next node that should appear in a given node's fallback list
1da177e4
LT
1473 * @node: node whose fallback list we're appending
1474 * @used_node_mask: nodemask_t of already used nodes
1475 *
1476 * We use a number of factors to determine which is the next node that should
1477 * appear on a given node's fallback list. The node should not have appeared
1478 * already in @node's fallback list, and it should be the next closest node
1479 * according to the distance array (which contains arbitrary distance values
1480 * from each node to each node in the system), and should also prefer nodes
1481 * with no CPUs, since presumably they'll have very little allocation pressure
1482 * on them otherwise.
1483 * It returns -1 if no node is found.
1484 */
1485static int __init find_next_best_node(int node, nodemask_t *used_node_mask)
1486{
1487 int i, n, val;
1488 int min_val = INT_MAX;
1489 int best_node = -1;
1490
1491 for_each_online_node(i) {
1492 cpumask_t tmp;
1493
1494 /* Start from local node */
1495 n = (node+i) % num_online_nodes();
1496
1497 /* Don't want a node to appear more than once */
1498 if (node_isset(n, *used_node_mask))
1499 continue;
1500
1501 /* Use the local node if we haven't already */
1502 if (!node_isset(node, *used_node_mask)) {
1503 best_node = node;
1504 break;
1505 }
1506
1507 /* Use the distance array to find the distance */
1508 val = node_distance(node, n);
1509
1510 /* Give preference to headless and unused nodes */
1511 tmp = node_to_cpumask(n);
1512 if (!cpus_empty(tmp))
1513 val += PENALTY_FOR_NODE_WITH_CPUS;
1514
1515 /* Slight preference for less loaded node */
1516 val *= (MAX_NODE_LOAD*MAX_NUMNODES);
1517 val += node_load[n];
1518
1519 if (val < min_val) {
1520 min_val = val;
1521 best_node = n;
1522 }
1523 }
1524
1525 if (best_node >= 0)
1526 node_set(best_node, *used_node_mask);
1527
1528 return best_node;
1529}
1530
1531static void __init build_zonelists(pg_data_t *pgdat)
1532{
1533 int i, j, k, node, local_node;
1534 int prev_node, load;
1535 struct zonelist *zonelist;
1536 nodemask_t used_mask;
1537
1538 /* initialize zonelists */
1539 for (i = 0; i < GFP_ZONETYPES; i++) {
1540 zonelist = pgdat->node_zonelists + i;
1541 zonelist->zones[0] = NULL;
1542 }
1543
1544 /* NUMA-aware ordering of nodes */
1545 local_node = pgdat->node_id;
1546 load = num_online_nodes();
1547 prev_node = local_node;
1548 nodes_clear(used_mask);
1549 while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
1550 /*
1551 * We don't want to pressure a particular node.
1552 * So adding penalty to the first node in same
1553 * distance group to make it round-robin.
1554 */
1555 if (node_distance(local_node, node) !=
1556 node_distance(local_node, prev_node))
1557 node_load[node] += load;
1558 prev_node = node;
1559 load--;
1560 for (i = 0; i < GFP_ZONETYPES; i++) {
1561 zonelist = pgdat->node_zonelists + i;
1562 for (j = 0; zonelist->zones[j] != NULL; j++);
1563
260b2367 1564 k = highest_zone(i);
1da177e4
LT
1565
1566 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1567 zonelist->zones[j] = NULL;
1568 }
1569 }
1570}
1571
1572#else /* CONFIG_NUMA */
1573
1574static void __init build_zonelists(pg_data_t *pgdat)
1575{
1576 int i, j, k, node, local_node;
1577
1578 local_node = pgdat->node_id;
1579 for (i = 0; i < GFP_ZONETYPES; i++) {
1580 struct zonelist *zonelist;
1581
1582 zonelist = pgdat->node_zonelists + i;
1583
1584 j = 0;
260b2367 1585 k = highest_zone(i);
1da177e4
LT
1586 j = build_zonelists_node(pgdat, zonelist, j, k);
1587 /*
1588 * Now we build the zonelist so that it contains the zones
1589 * of all the other nodes.
1590 * We don't want to pressure a particular node, so when
1591 * building the zones for node N, we make sure that the
1592 * zones coming right after the local ones are those from
1593 * node N+1 (modulo N)
1594 */
1595 for (node = local_node + 1; node < MAX_NUMNODES; node++) {
1596 if (!node_online(node))
1597 continue;
1598 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1599 }
1600 for (node = 0; node < local_node; node++) {
1601 if (!node_online(node))
1602 continue;
1603 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1604 }
1605
1606 zonelist->zones[j] = NULL;
1607 }
1608}
1609
1610#endif /* CONFIG_NUMA */
1611
1612void __init build_all_zonelists(void)
1613{
1614 int i;
1615
1616 for_each_online_node(i)
1617 build_zonelists(NODE_DATA(i));
1618 printk("Built %i zonelists\n", num_online_nodes());
1619 cpuset_init_current_mems_allowed();
1620}
1621
1622/*
1623 * Helper functions to size the waitqueue hash table.
1624 * Essentially these want to choose hash table sizes sufficiently
1625 * large so that collisions trying to wait on pages are rare.
1626 * But in fact, the number of active page waitqueues on typical
1627 * systems is ridiculously low, less than 200. So this is even
1628 * conservative, even though it seems large.
1629 *
1630 * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
1631 * waitqueues, i.e. the size of the waitq table given the number of pages.
1632 */
1633#define PAGES_PER_WAITQUEUE 256
1634
1635static inline unsigned long wait_table_size(unsigned long pages)
1636{
1637 unsigned long size = 1;
1638
1639 pages /= PAGES_PER_WAITQUEUE;
1640
1641 while (size < pages)
1642 size <<= 1;
1643
1644 /*
1645 * Once we have dozens or even hundreds of threads sleeping
1646 * on IO we've got bigger problems than wait queue collision.
1647 * Limit the size of the wait table to a reasonable size.
1648 */
1649 size = min(size, 4096UL);
1650
1651 return max(size, 4UL);
1652}
1653
1654/*
1655 * This is an integer logarithm so that shifts can be used later
1656 * to extract the more random high bits from the multiplicative
1657 * hash function before the remainder is taken.
1658 */
1659static inline unsigned long wait_table_bits(unsigned long size)
1660{
1661 return ffz(~size);
1662}
1663
1664#define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
1665
1666static void __init calculate_zone_totalpages(struct pglist_data *pgdat,
1667 unsigned long *zones_size, unsigned long *zholes_size)
1668{
1669 unsigned long realtotalpages, totalpages = 0;
1670 int i;
1671
1672 for (i = 0; i < MAX_NR_ZONES; i++)
1673 totalpages += zones_size[i];
1674 pgdat->node_spanned_pages = totalpages;
1675
1676 realtotalpages = totalpages;
1677 if (zholes_size)
1678 for (i = 0; i < MAX_NR_ZONES; i++)
1679 realtotalpages -= zholes_size[i];
1680 pgdat->node_present_pages = realtotalpages;
1681 printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id, realtotalpages);
1682}
1683
1684
1685/*
1686 * Initially all pages are reserved - free ones are freed
1687 * up by free_all_bootmem() once the early boot process is
1688 * done. Non-atomic initialization, single-pass.
1689 */
3947be19 1690void __devinit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
1da177e4
LT
1691 unsigned long start_pfn)
1692{
1da177e4 1693 struct page *page;
29751f69
AW
1694 unsigned long end_pfn = start_pfn + size;
1695 unsigned long pfn;
1da177e4 1696
d41dee36
AW
1697 for (pfn = start_pfn; pfn < end_pfn; pfn++, page++) {
1698 if (!early_pfn_valid(pfn))
1699 continue;
641c7673
AW
1700 if (!early_pfn_in_nid(pfn, nid))
1701 continue;
d41dee36
AW
1702 page = pfn_to_page(pfn);
1703 set_page_links(page, zone, nid, pfn);
b5810039 1704 set_page_count(page, 1);
1da177e4
LT
1705 reset_page_mapcount(page);
1706 SetPageReserved(page);
1707 INIT_LIST_HEAD(&page->lru);
1708#ifdef WANT_PAGE_VIRTUAL
1709 /* The shift won't overflow because ZONE_NORMAL is below 4G. */
1710 if (!is_highmem_idx(zone))
3212c6be 1711 set_page_address(page, __va(pfn << PAGE_SHIFT));
1da177e4 1712#endif
1da177e4
LT
1713 }
1714}
1715
1716void zone_init_free_lists(struct pglist_data *pgdat, struct zone *zone,
1717 unsigned long size)
1718{
1719 int order;
1720 for (order = 0; order < MAX_ORDER ; order++) {
1721 INIT_LIST_HEAD(&zone->free_area[order].free_list);
1722 zone->free_area[order].nr_free = 0;
1723 }
1724}
1725
d41dee36
AW
1726#define ZONETABLE_INDEX(x, zone_nr) ((x << ZONES_SHIFT) | zone_nr)
1727void zonetable_add(struct zone *zone, int nid, int zid, unsigned long pfn,
1728 unsigned long size)
1729{
1730 unsigned long snum = pfn_to_section_nr(pfn);
1731 unsigned long end = pfn_to_section_nr(pfn + size);
1732
1733 if (FLAGS_HAS_NODE)
1734 zone_table[ZONETABLE_INDEX(nid, zid)] = zone;
1735 else
1736 for (; snum <= end; snum++)
1737 zone_table[ZONETABLE_INDEX(snum, zid)] = zone;
1738}
1739
1da177e4
LT
1740#ifndef __HAVE_ARCH_MEMMAP_INIT
1741#define memmap_init(size, nid, zone, start_pfn) \
1742 memmap_init_zone((size), (nid), (zone), (start_pfn))
1743#endif
1744
e7c8d5c9
CL
1745static int __devinit zone_batchsize(struct zone *zone)
1746{
1747 int batch;
1748
1749 /*
1750 * The per-cpu-pages pools are set to around 1000th of the
ba56e91c 1751 * size of the zone. But no more than 1/2 of a meg.
e7c8d5c9
CL
1752 *
1753 * OK, so we don't know how big the cache is. So guess.
1754 */
1755 batch = zone->present_pages / 1024;
ba56e91c
SR
1756 if (batch * PAGE_SIZE > 512 * 1024)
1757 batch = (512 * 1024) / PAGE_SIZE;
e7c8d5c9
CL
1758 batch /= 4; /* We effectively *= 4 below */
1759 if (batch < 1)
1760 batch = 1;
1761
1762 /*
ba56e91c
SR
1763 * We will be trying to allcoate bigger chunks of contiguous
1764 * memory of the order of fls(batch). This should result in
1765 * better cache coloring.
e7c8d5c9 1766 *
ba56e91c 1767 * A sanity check also to ensure that batch is still in limits.
e7c8d5c9 1768 */
ba56e91c
SR
1769 batch = (1 << fls(batch + batch/2));
1770
1771 if (fls(batch) >= (PAGE_SHIFT + MAX_ORDER - 2))
1772 batch = PAGE_SHIFT + ((MAX_ORDER - 1 - PAGE_SHIFT)/2);
1773
e7c8d5c9
CL
1774 return batch;
1775}
1776
2caaad41
CL
1777inline void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
1778{
1779 struct per_cpu_pages *pcp;
1780
1c6fe946
MD
1781 memset(p, 0, sizeof(*p));
1782
2caaad41
CL
1783 pcp = &p->pcp[0]; /* hot */
1784 pcp->count = 0;
e46a5e28 1785 pcp->low = 0;
2caaad41
CL
1786 pcp->high = 6 * batch;
1787 pcp->batch = max(1UL, 1 * batch);
1788 INIT_LIST_HEAD(&pcp->list);
1789
1790 pcp = &p->pcp[1]; /* cold*/
1791 pcp->count = 0;
1792 pcp->low = 0;
1793 pcp->high = 2 * batch;
e46a5e28 1794 pcp->batch = max(1UL, batch/2);
2caaad41
CL
1795 INIT_LIST_HEAD(&pcp->list);
1796}
1797
e7c8d5c9
CL
1798#ifdef CONFIG_NUMA
1799/*
2caaad41
CL
1800 * Boot pageset table. One per cpu which is going to be used for all
1801 * zones and all nodes. The parameters will be set in such a way
1802 * that an item put on a list will immediately be handed over to
1803 * the buddy list. This is safe since pageset manipulation is done
1804 * with interrupts disabled.
1805 *
1806 * Some NUMA counter updates may also be caught by the boot pagesets.
b7c84c6a
CL
1807 *
1808 * The boot_pagesets must be kept even after bootup is complete for
1809 * unused processors and/or zones. They do play a role for bootstrapping
1810 * hotplugged processors.
1811 *
1812 * zoneinfo_show() and maybe other functions do
1813 * not check if the processor is online before following the pageset pointer.
1814 * Other parts of the kernel may not check if the zone is available.
2caaad41
CL
1815 */
1816static struct per_cpu_pageset
b7c84c6a 1817 boot_pageset[NR_CPUS];
2caaad41
CL
1818
1819/*
1820 * Dynamically allocate memory for the
e7c8d5c9
CL
1821 * per cpu pageset array in struct zone.
1822 */
1823static int __devinit process_zones(int cpu)
1824{
1825 struct zone *zone, *dzone;
e7c8d5c9
CL
1826
1827 for_each_zone(zone) {
e7c8d5c9 1828
2caaad41 1829 zone->pageset[cpu] = kmalloc_node(sizeof(struct per_cpu_pageset),
e7c8d5c9 1830 GFP_KERNEL, cpu_to_node(cpu));
2caaad41 1831 if (!zone->pageset[cpu])
e7c8d5c9 1832 goto bad;
e7c8d5c9 1833
2caaad41 1834 setup_pageset(zone->pageset[cpu], zone_batchsize(zone));
e7c8d5c9
CL
1835 }
1836
1837 return 0;
1838bad:
1839 for_each_zone(dzone) {
1840 if (dzone == zone)
1841 break;
1842 kfree(dzone->pageset[cpu]);
1843 dzone->pageset[cpu] = NULL;
1844 }
1845 return -ENOMEM;
1846}
1847
1848static inline void free_zone_pagesets(int cpu)
1849{
1850#ifdef CONFIG_NUMA
1851 struct zone *zone;
1852
1853 for_each_zone(zone) {
1854 struct per_cpu_pageset *pset = zone_pcp(zone, cpu);
1855
1856 zone_pcp(zone, cpu) = NULL;
1857 kfree(pset);
1858 }
1859#endif
1860}
1861
1862static int __devinit pageset_cpuup_callback(struct notifier_block *nfb,
1863 unsigned long action,
1864 void *hcpu)
1865{
1866 int cpu = (long)hcpu;
1867 int ret = NOTIFY_OK;
1868
1869 switch (action) {
1870 case CPU_UP_PREPARE:
1871 if (process_zones(cpu))
1872 ret = NOTIFY_BAD;
1873 break;
1874#ifdef CONFIG_HOTPLUG_CPU
1875 case CPU_DEAD:
1876 free_zone_pagesets(cpu);
1877 break;
1878#endif
1879 default:
1880 break;
1881 }
1882 return ret;
1883}
1884
1885static struct notifier_block pageset_notifier =
1886 { &pageset_cpuup_callback, NULL, 0 };
1887
1888void __init setup_per_cpu_pageset()
1889{
1890 int err;
1891
1892 /* Initialize per_cpu_pageset for cpu 0.
1893 * A cpuup callback will do this for every cpu
1894 * as it comes online
1895 */
1896 err = process_zones(smp_processor_id());
1897 BUG_ON(err);
1898 register_cpu_notifier(&pageset_notifier);
1899}
1900
1901#endif
1902
ed8ece2e
DH
1903static __devinit
1904void zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages)
1905{
1906 int i;
1907 struct pglist_data *pgdat = zone->zone_pgdat;
1908
1909 /*
1910 * The per-page waitqueue mechanism uses hashed waitqueues
1911 * per zone.
1912 */
1913 zone->wait_table_size = wait_table_size(zone_size_pages);
1914 zone->wait_table_bits = wait_table_bits(zone->wait_table_size);
1915 zone->wait_table = (wait_queue_head_t *)
1916 alloc_bootmem_node(pgdat, zone->wait_table_size
1917 * sizeof(wait_queue_head_t));
1918
1919 for(i = 0; i < zone->wait_table_size; ++i)
1920 init_waitqueue_head(zone->wait_table + i);
1921}
1922
1923static __devinit void zone_pcp_init(struct zone *zone)
1924{
1925 int cpu;
1926 unsigned long batch = zone_batchsize(zone);
1927
1928 for (cpu = 0; cpu < NR_CPUS; cpu++) {
1929#ifdef CONFIG_NUMA
1930 /* Early boot. Slab allocator not functional yet */
1931 zone->pageset[cpu] = &boot_pageset[cpu];
1932 setup_pageset(&boot_pageset[cpu],0);
1933#else
1934 setup_pageset(zone_pcp(zone,cpu), batch);
1935#endif
1936 }
1937 printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%lu\n",
1938 zone->name, zone->present_pages, batch);
1939}
1940
1941static __devinit void init_currently_empty_zone(struct zone *zone,
1942 unsigned long zone_start_pfn, unsigned long size)
1943{
1944 struct pglist_data *pgdat = zone->zone_pgdat;
1945
1946 zone_wait_table_init(zone, size);
1947 pgdat->nr_zones = zone_idx(zone) + 1;
1948
1949 zone->zone_mem_map = pfn_to_page(zone_start_pfn);
1950 zone->zone_start_pfn = zone_start_pfn;
1951
1952 memmap_init(size, pgdat->node_id, zone_idx(zone), zone_start_pfn);
1953
1954 zone_init_free_lists(pgdat, zone, zone->spanned_pages);
1955}
1956
1da177e4
LT
1957/*
1958 * Set up the zone data structures:
1959 * - mark all pages reserved
1960 * - mark all memory queues empty
1961 * - clear the memory bitmaps
1962 */
1963static void __init free_area_init_core(struct pglist_data *pgdat,
1964 unsigned long *zones_size, unsigned long *zholes_size)
1965{
ed8ece2e
DH
1966 unsigned long j;
1967 int nid = pgdat->node_id;
1da177e4
LT
1968 unsigned long zone_start_pfn = pgdat->node_start_pfn;
1969
208d54e5 1970 pgdat_resize_init(pgdat);
1da177e4
LT
1971 pgdat->nr_zones = 0;
1972 init_waitqueue_head(&pgdat->kswapd_wait);
1973 pgdat->kswapd_max_order = 0;
1974
1975 for (j = 0; j < MAX_NR_ZONES; j++) {
1976 struct zone *zone = pgdat->node_zones + j;
1977 unsigned long size, realsize;
1da177e4 1978
1da177e4
LT
1979 realsize = size = zones_size[j];
1980 if (zholes_size)
1981 realsize -= zholes_size[j];
1982
1983 if (j == ZONE_DMA || j == ZONE_NORMAL)
1984 nr_kernel_pages += realsize;
1985 nr_all_pages += realsize;
1986
1987 zone->spanned_pages = size;
1988 zone->present_pages = realsize;
1989 zone->name = zone_names[j];
1990 spin_lock_init(&zone->lock);
1991 spin_lock_init(&zone->lru_lock);
bdc8cb98 1992 zone_seqlock_init(zone);
1da177e4
LT
1993 zone->zone_pgdat = pgdat;
1994 zone->free_pages = 0;
1995
1996 zone->temp_priority = zone->prev_priority = DEF_PRIORITY;
1997
ed8ece2e 1998 zone_pcp_init(zone);
1da177e4
LT
1999 INIT_LIST_HEAD(&zone->active_list);
2000 INIT_LIST_HEAD(&zone->inactive_list);
2001 zone->nr_scan_active = 0;
2002 zone->nr_scan_inactive = 0;
2003 zone->nr_active = 0;
2004 zone->nr_inactive = 0;
53e9a615 2005 atomic_set(&zone->reclaim_in_progress, 0);
1da177e4
LT
2006 if (!size)
2007 continue;
2008
d41dee36 2009 zonetable_add(zone, nid, j, zone_start_pfn, size);
ed8ece2e 2010 init_currently_empty_zone(zone, zone_start_pfn, size);
1da177e4 2011 zone_start_pfn += size;
1da177e4
LT
2012 }
2013}
2014
2015static void __init alloc_node_mem_map(struct pglist_data *pgdat)
2016{
1da177e4
LT
2017 /* Skip empty nodes */
2018 if (!pgdat->node_spanned_pages)
2019 return;
2020
d41dee36 2021#ifdef CONFIG_FLAT_NODE_MEM_MAP
1da177e4
LT
2022 /* ia64 gets its own node_mem_map, before this, without bootmem */
2023 if (!pgdat->node_mem_map) {
d41dee36
AW
2024 unsigned long size;
2025 struct page *map;
2026
1da177e4 2027 size = (pgdat->node_spanned_pages + 1) * sizeof(struct page);
6f167ec7
DH
2028 map = alloc_remap(pgdat->node_id, size);
2029 if (!map)
2030 map = alloc_bootmem_node(pgdat, size);
2031 pgdat->node_mem_map = map;
1da177e4 2032 }
d41dee36 2033#ifdef CONFIG_FLATMEM
1da177e4
LT
2034 /*
2035 * With no DISCONTIG, the global mem_map is just set as node 0's
2036 */
2037 if (pgdat == NODE_DATA(0))
2038 mem_map = NODE_DATA(0)->node_mem_map;
2039#endif
d41dee36 2040#endif /* CONFIG_FLAT_NODE_MEM_MAP */
1da177e4
LT
2041}
2042
2043void __init free_area_init_node(int nid, struct pglist_data *pgdat,
2044 unsigned long *zones_size, unsigned long node_start_pfn,
2045 unsigned long *zholes_size)
2046{
2047 pgdat->node_id = nid;
2048 pgdat->node_start_pfn = node_start_pfn;
2049 calculate_zone_totalpages(pgdat, zones_size, zholes_size);
2050
2051 alloc_node_mem_map(pgdat);
2052
2053 free_area_init_core(pgdat, zones_size, zholes_size);
2054}
2055
93b7504e 2056#ifndef CONFIG_NEED_MULTIPLE_NODES
1da177e4
LT
2057static bootmem_data_t contig_bootmem_data;
2058struct pglist_data contig_page_data = { .bdata = &contig_bootmem_data };
2059
2060EXPORT_SYMBOL(contig_page_data);
93b7504e 2061#endif
1da177e4
LT
2062
2063void __init free_area_init(unsigned long *zones_size)
2064{
93b7504e 2065 free_area_init_node(0, NODE_DATA(0), zones_size,
1da177e4
LT
2066 __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
2067}
1da177e4
LT
2068
2069#ifdef CONFIG_PROC_FS
2070
2071#include <linux/seq_file.h>
2072
2073static void *frag_start(struct seq_file *m, loff_t *pos)
2074{
2075 pg_data_t *pgdat;
2076 loff_t node = *pos;
2077
2078 for (pgdat = pgdat_list; pgdat && node; pgdat = pgdat->pgdat_next)
2079 --node;
2080
2081 return pgdat;
2082}
2083
2084static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
2085{
2086 pg_data_t *pgdat = (pg_data_t *)arg;
2087
2088 (*pos)++;
2089 return pgdat->pgdat_next;
2090}
2091
2092static void frag_stop(struct seq_file *m, void *arg)
2093{
2094}
2095
2096/*
2097 * This walks the free areas for each zone.
2098 */
2099static int frag_show(struct seq_file *m, void *arg)
2100{
2101 pg_data_t *pgdat = (pg_data_t *)arg;
2102 struct zone *zone;
2103 struct zone *node_zones = pgdat->node_zones;
2104 unsigned long flags;
2105 int order;
2106
2107 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
2108 if (!zone->present_pages)
2109 continue;
2110
2111 spin_lock_irqsave(&zone->lock, flags);
2112 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
2113 for (order = 0; order < MAX_ORDER; ++order)
2114 seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
2115 spin_unlock_irqrestore(&zone->lock, flags);
2116 seq_putc(m, '\n');
2117 }
2118 return 0;
2119}
2120
2121struct seq_operations fragmentation_op = {
2122 .start = frag_start,
2123 .next = frag_next,
2124 .stop = frag_stop,
2125 .show = frag_show,
2126};
2127
295ab934
ND
2128/*
2129 * Output information about zones in @pgdat.
2130 */
2131static int zoneinfo_show(struct seq_file *m, void *arg)
2132{
2133 pg_data_t *pgdat = arg;
2134 struct zone *zone;
2135 struct zone *node_zones = pgdat->node_zones;
2136 unsigned long flags;
2137
2138 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; zone++) {
2139 int i;
2140
2141 if (!zone->present_pages)
2142 continue;
2143
2144 spin_lock_irqsave(&zone->lock, flags);
2145 seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
2146 seq_printf(m,
2147 "\n pages free %lu"
2148 "\n min %lu"
2149 "\n low %lu"
2150 "\n high %lu"
2151 "\n active %lu"
2152 "\n inactive %lu"
2153 "\n scanned %lu (a: %lu i: %lu)"
2154 "\n spanned %lu"
2155 "\n present %lu",
2156 zone->free_pages,
2157 zone->pages_min,
2158 zone->pages_low,
2159 zone->pages_high,
2160 zone->nr_active,
2161 zone->nr_inactive,
2162 zone->pages_scanned,
2163 zone->nr_scan_active, zone->nr_scan_inactive,
2164 zone->spanned_pages,
2165 zone->present_pages);
2166 seq_printf(m,
2167 "\n protection: (%lu",
2168 zone->lowmem_reserve[0]);
2169 for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
2170 seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
2171 seq_printf(m,
2172 ")"
2173 "\n pagesets");
2174 for (i = 0; i < ARRAY_SIZE(zone->pageset); i++) {
2175 struct per_cpu_pageset *pageset;
2176 int j;
2177
e7c8d5c9 2178 pageset = zone_pcp(zone, i);
295ab934
ND
2179 for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
2180 if (pageset->pcp[j].count)
2181 break;
2182 }
2183 if (j == ARRAY_SIZE(pageset->pcp))
2184 continue;
2185 for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
2186 seq_printf(m,
2187 "\n cpu: %i pcp: %i"
2188 "\n count: %i"
2189 "\n low: %i"
2190 "\n high: %i"
2191 "\n batch: %i",
2192 i, j,
2193 pageset->pcp[j].count,
2194 pageset->pcp[j].low,
2195 pageset->pcp[j].high,
2196 pageset->pcp[j].batch);
2197 }
2198#ifdef CONFIG_NUMA
2199 seq_printf(m,
2200 "\n numa_hit: %lu"
2201 "\n numa_miss: %lu"
2202 "\n numa_foreign: %lu"
2203 "\n interleave_hit: %lu"
2204 "\n local_node: %lu"
2205 "\n other_node: %lu",
2206 pageset->numa_hit,
2207 pageset->numa_miss,
2208 pageset->numa_foreign,
2209 pageset->interleave_hit,
2210 pageset->local_node,
2211 pageset->other_node);
2212#endif
2213 }
2214 seq_printf(m,
2215 "\n all_unreclaimable: %u"
2216 "\n prev_priority: %i"
2217 "\n temp_priority: %i"
2218 "\n start_pfn: %lu",
2219 zone->all_unreclaimable,
2220 zone->prev_priority,
2221 zone->temp_priority,
2222 zone->zone_start_pfn);
2223 spin_unlock_irqrestore(&zone->lock, flags);
2224 seq_putc(m, '\n');
2225 }
2226 return 0;
2227}
2228
2229struct seq_operations zoneinfo_op = {
2230 .start = frag_start, /* iterate over all zones. The same as in
2231 * fragmentation. */
2232 .next = frag_next,
2233 .stop = frag_stop,
2234 .show = zoneinfo_show,
2235};
2236
1da177e4
LT
2237static char *vmstat_text[] = {
2238 "nr_dirty",
2239 "nr_writeback",
2240 "nr_unstable",
2241 "nr_page_table_pages",
2242 "nr_mapped",
2243 "nr_slab",
2244
2245 "pgpgin",
2246 "pgpgout",
2247 "pswpin",
2248 "pswpout",
2249 "pgalloc_high",
2250
2251 "pgalloc_normal",
2252 "pgalloc_dma",
2253 "pgfree",
2254 "pgactivate",
2255 "pgdeactivate",
2256
2257 "pgfault",
2258 "pgmajfault",
2259 "pgrefill_high",
2260 "pgrefill_normal",
2261 "pgrefill_dma",
2262
2263 "pgsteal_high",
2264 "pgsteal_normal",
2265 "pgsteal_dma",
2266 "pgscan_kswapd_high",
2267 "pgscan_kswapd_normal",
2268
2269 "pgscan_kswapd_dma",
2270 "pgscan_direct_high",
2271 "pgscan_direct_normal",
2272 "pgscan_direct_dma",
2273 "pginodesteal",
2274
2275 "slabs_scanned",
2276 "kswapd_steal",
2277 "kswapd_inodesteal",
2278 "pageoutrun",
2279 "allocstall",
2280
2281 "pgrotated",
edfbe2b0 2282 "nr_bounce",
1da177e4
LT
2283};
2284
2285static void *vmstat_start(struct seq_file *m, loff_t *pos)
2286{
2287 struct page_state *ps;
2288
2289 if (*pos >= ARRAY_SIZE(vmstat_text))
2290 return NULL;
2291
2292 ps = kmalloc(sizeof(*ps), GFP_KERNEL);
2293 m->private = ps;
2294 if (!ps)
2295 return ERR_PTR(-ENOMEM);
2296 get_full_page_state(ps);
2297 ps->pgpgin /= 2; /* sectors -> kbytes */
2298 ps->pgpgout /= 2;
2299 return (unsigned long *)ps + *pos;
2300}
2301
2302static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
2303{
2304 (*pos)++;
2305 if (*pos >= ARRAY_SIZE(vmstat_text))
2306 return NULL;
2307 return (unsigned long *)m->private + *pos;
2308}
2309
2310static int vmstat_show(struct seq_file *m, void *arg)
2311{
2312 unsigned long *l = arg;
2313 unsigned long off = l - (unsigned long *)m->private;
2314
2315 seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
2316 return 0;
2317}
2318
2319static void vmstat_stop(struct seq_file *m, void *arg)
2320{
2321 kfree(m->private);
2322 m->private = NULL;
2323}
2324
2325struct seq_operations vmstat_op = {
2326 .start = vmstat_start,
2327 .next = vmstat_next,
2328 .stop = vmstat_stop,
2329 .show = vmstat_show,
2330};
2331
2332#endif /* CONFIG_PROC_FS */
2333
2334#ifdef CONFIG_HOTPLUG_CPU
2335static int page_alloc_cpu_notify(struct notifier_block *self,
2336 unsigned long action, void *hcpu)
2337{
2338 int cpu = (unsigned long)hcpu;
2339 long *count;
2340 unsigned long *src, *dest;
2341
2342 if (action == CPU_DEAD) {
2343 int i;
2344
2345 /* Drain local pagecache count. */
2346 count = &per_cpu(nr_pagecache_local, cpu);
2347 atomic_add(*count, &nr_pagecache);
2348 *count = 0;
2349 local_irq_disable();
2350 __drain_pages(cpu);
2351
2352 /* Add dead cpu's page_states to our own. */
2353 dest = (unsigned long *)&__get_cpu_var(page_states);
2354 src = (unsigned long *)&per_cpu(page_states, cpu);
2355
2356 for (i = 0; i < sizeof(struct page_state)/sizeof(unsigned long);
2357 i++) {
2358 dest[i] += src[i];
2359 src[i] = 0;
2360 }
2361
2362 local_irq_enable();
2363 }
2364 return NOTIFY_OK;
2365}
2366#endif /* CONFIG_HOTPLUG_CPU */
2367
2368void __init page_alloc_init(void)
2369{
2370 hotcpu_notifier(page_alloc_cpu_notify, 0);
2371}
2372
2373/*
2374 * setup_per_zone_lowmem_reserve - called whenever
2375 * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
2376 * has a correct pages reserved value, so an adequate number of
2377 * pages are left in the zone after a successful __alloc_pages().
2378 */
2379static void setup_per_zone_lowmem_reserve(void)
2380{
2381 struct pglist_data *pgdat;
2382 int j, idx;
2383
2384 for_each_pgdat(pgdat) {
2385 for (j = 0; j < MAX_NR_ZONES; j++) {
2386 struct zone *zone = pgdat->node_zones + j;
2387 unsigned long present_pages = zone->present_pages;
2388
2389 zone->lowmem_reserve[j] = 0;
2390
2391 for (idx = j-1; idx >= 0; idx--) {
2392 struct zone *lower_zone;
2393
2394 if (sysctl_lowmem_reserve_ratio[idx] < 1)
2395 sysctl_lowmem_reserve_ratio[idx] = 1;
2396
2397 lower_zone = pgdat->node_zones + idx;
2398 lower_zone->lowmem_reserve[j] = present_pages /
2399 sysctl_lowmem_reserve_ratio[idx];
2400 present_pages += lower_zone->present_pages;
2401 }
2402 }
2403 }
2404}
2405
2406/*
2407 * setup_per_zone_pages_min - called when min_free_kbytes changes. Ensures
2408 * that the pages_{min,low,high} values for each zone are set correctly
2409 * with respect to min_free_kbytes.
2410 */
3947be19 2411void setup_per_zone_pages_min(void)
1da177e4
LT
2412{
2413 unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
2414 unsigned long lowmem_pages = 0;
2415 struct zone *zone;
2416 unsigned long flags;
2417
2418 /* Calculate total number of !ZONE_HIGHMEM pages */
2419 for_each_zone(zone) {
2420 if (!is_highmem(zone))
2421 lowmem_pages += zone->present_pages;
2422 }
2423
2424 for_each_zone(zone) {
2425 spin_lock_irqsave(&zone->lru_lock, flags);
2426 if (is_highmem(zone)) {
2427 /*
2428 * Often, highmem doesn't need to reserve any pages.
2429 * But the pages_min/low/high values are also used for
2430 * batching up page reclaim activity so we need a
2431 * decent value here.
2432 */
2433 int min_pages;
2434
2435 min_pages = zone->present_pages / 1024;
2436 if (min_pages < SWAP_CLUSTER_MAX)
2437 min_pages = SWAP_CLUSTER_MAX;
2438 if (min_pages > 128)
2439 min_pages = 128;
2440 zone->pages_min = min_pages;
2441 } else {
295ab934 2442 /* if it's a lowmem zone, reserve a number of pages
1da177e4
LT
2443 * proportionate to the zone's size.
2444 */
295ab934 2445 zone->pages_min = (pages_min * zone->present_pages) /
1da177e4
LT
2446 lowmem_pages;
2447 }
2448
2449 /*
2450 * When interpreting these watermarks, just keep in mind that:
2451 * zone->pages_min == (zone->pages_min * 4) / 4;
2452 */
2453 zone->pages_low = (zone->pages_min * 5) / 4;
2454 zone->pages_high = (zone->pages_min * 6) / 4;
2455 spin_unlock_irqrestore(&zone->lru_lock, flags);
2456 }
2457}
2458
2459/*
2460 * Initialise min_free_kbytes.
2461 *
2462 * For small machines we want it small (128k min). For large machines
2463 * we want it large (64MB max). But it is not linear, because network
2464 * bandwidth does not increase linearly with machine size. We use
2465 *
2466 * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
2467 * min_free_kbytes = sqrt(lowmem_kbytes * 16)
2468 *
2469 * which yields
2470 *
2471 * 16MB: 512k
2472 * 32MB: 724k
2473 * 64MB: 1024k
2474 * 128MB: 1448k
2475 * 256MB: 2048k
2476 * 512MB: 2896k
2477 * 1024MB: 4096k
2478 * 2048MB: 5792k
2479 * 4096MB: 8192k
2480 * 8192MB: 11584k
2481 * 16384MB: 16384k
2482 */
2483static int __init init_per_zone_pages_min(void)
2484{
2485 unsigned long lowmem_kbytes;
2486
2487 lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
2488
2489 min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
2490 if (min_free_kbytes < 128)
2491 min_free_kbytes = 128;
2492 if (min_free_kbytes > 65536)
2493 min_free_kbytes = 65536;
2494 setup_per_zone_pages_min();
2495 setup_per_zone_lowmem_reserve();
2496 return 0;
2497}
2498module_init(init_per_zone_pages_min)
2499
2500/*
2501 * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
2502 * that we can call two helper functions whenever min_free_kbytes
2503 * changes.
2504 */
2505int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
2506 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
2507{
2508 proc_dointvec(table, write, file, buffer, length, ppos);
2509 setup_per_zone_pages_min();
2510 return 0;
2511}
2512
2513/*
2514 * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
2515 * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
2516 * whenever sysctl_lowmem_reserve_ratio changes.
2517 *
2518 * The reserve ratio obviously has absolutely no relation with the
2519 * pages_min watermarks. The lowmem reserve ratio can only make sense
2520 * if in function of the boot time zone sizes.
2521 */
2522int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
2523 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
2524{
2525 proc_dointvec_minmax(table, write, file, buffer, length, ppos);
2526 setup_per_zone_lowmem_reserve();
2527 return 0;
2528}
2529
2530__initdata int hashdist = HASHDIST_DEFAULT;
2531
2532#ifdef CONFIG_NUMA
2533static int __init set_hashdist(char *str)
2534{
2535 if (!str)
2536 return 0;
2537 hashdist = simple_strtoul(str, &str, 0);
2538 return 1;
2539}
2540__setup("hashdist=", set_hashdist);
2541#endif
2542
2543/*
2544 * allocate a large system hash table from bootmem
2545 * - it is assumed that the hash table must contain an exact power-of-2
2546 * quantity of entries
2547 * - limit is the number of hash buckets, not the total allocation size
2548 */
2549void *__init alloc_large_system_hash(const char *tablename,
2550 unsigned long bucketsize,
2551 unsigned long numentries,
2552 int scale,
2553 int flags,
2554 unsigned int *_hash_shift,
2555 unsigned int *_hash_mask,
2556 unsigned long limit)
2557{
2558 unsigned long long max = limit;
2559 unsigned long log2qty, size;
2560 void *table = NULL;
2561
2562 /* allow the kernel cmdline to have a say */
2563 if (!numentries) {
2564 /* round applicable memory size up to nearest megabyte */
2565 numentries = (flags & HASH_HIGHMEM) ? nr_all_pages : nr_kernel_pages;
2566 numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
2567 numentries >>= 20 - PAGE_SHIFT;
2568 numentries <<= 20 - PAGE_SHIFT;
2569
2570 /* limit to 1 bucket per 2^scale bytes of low memory */
2571 if (scale > PAGE_SHIFT)
2572 numentries >>= (scale - PAGE_SHIFT);
2573 else
2574 numentries <<= (PAGE_SHIFT - scale);
2575 }
2576 /* rounded up to nearest power of 2 in size */
2577 numentries = 1UL << (long_log2(numentries) + 1);
2578
2579 /* limit allocation size to 1/16 total memory by default */
2580 if (max == 0) {
2581 max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
2582 do_div(max, bucketsize);
2583 }
2584
2585 if (numentries > max)
2586 numentries = max;
2587
2588 log2qty = long_log2(numentries);
2589
2590 do {
2591 size = bucketsize << log2qty;
2592 if (flags & HASH_EARLY)
2593 table = alloc_bootmem(size);
2594 else if (hashdist)
2595 table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
2596 else {
2597 unsigned long order;
2598 for (order = 0; ((1UL << order) << PAGE_SHIFT) < size; order++)
2599 ;
2600 table = (void*) __get_free_pages(GFP_ATOMIC, order);
2601 }
2602 } while (!table && size > PAGE_SIZE && --log2qty);
2603
2604 if (!table)
2605 panic("Failed to allocate %s hash table\n", tablename);
2606
2607 printk("%s hash table entries: %d (order: %d, %lu bytes)\n",
2608 tablename,
2609 (1U << log2qty),
2610 long_log2(size) - PAGE_SHIFT,
2611 size);
2612
2613 if (_hash_shift)
2614 *_hash_shift = log2qty;
2615 if (_hash_mask)
2616 *_hash_mask = (1 << log2qty) - 1;
2617
2618 return table;
2619}