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