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