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