blob: bd4de592dc238fd3939c16b276cc7e10f7d1e21d [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>
Dave Hansenbdc8cb92005-10-29 18:16:53 -070036#include <linux/memory_hotplug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#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 */
Christoph Lameterc3d8c142005-09-06 15:16:33 -070047nodemask_t node_online_map __read_mostly = { { [0] = 1UL } };
Dean Nelson7223a932005-03-23 19:00:00 -070048EXPORT_SYMBOL(node_online_map);
Christoph Lameterc3d8c142005-09-06 15:16:33 -070049nodemask_t node_possible_map __read_mostly = NODE_MASK_ALL;
Dean Nelson7223a932005-03-23 19:00:00 -070050EXPORT_SYMBOL(node_possible_map);
Christoph Lameterc3d8c142005-09-06 15:16:33 -070051struct pglist_data *pgdat_list __read_mostly;
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -070052unsigned long totalram_pages __read_mostly;
53unsigned long totalhigh_pages __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054long 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
Andi Kleena2f1b422005-11-05 17:25:53 +010063 *
64 * TBD: should special case ZONE_DMA32 machines here - in those we normally
65 * don't need any ZONE_NORMAL reservation
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 */
Andi Kleena2f1b422005-11-05 17:25:53 +010067int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1] = { 256, 256, 32 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69EXPORT_SYMBOL(totalram_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
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 */
Christoph Lameterc3d8c142005-09-06 15:16:33 -070075struct zone *zone_table[1 << ZONETABLE_SHIFT] __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076EXPORT_SYMBOL(zone_table);
77
Andi Kleena2f1b422005-11-05 17:25:53 +010078static char *zone_names[MAX_NR_ZONES] = { "DMA", "DMA32", "Normal", "HighMem" };
Linus Torvalds1da177e2005-04-16 15:20:36 -070079int min_free_kbytes = 1024;
80
81unsigned long __initdata nr_kernel_pages;
82unsigned long __initdata nr_all_pages;
83
Dave Hansenc6a57e12005-10-29 18:16:52 -070084static int page_outside_zone_boundaries(struct zone *zone, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Dave Hansenbdc8cb92005-10-29 18:16:53 -070086 int ret = 0;
87 unsigned seq;
88 unsigned long pfn = page_to_pfn(page);
Dave Hansenc6a57e12005-10-29 18:16:52 -070089
Dave Hansenbdc8cb92005-10-29 18:16:53 -070090 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;
Dave Hansenc6a57e12005-10-29 18:16:52 -070099}
100
101static int page_is_consistent(struct zone *zone, struct page *page)
102{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#ifdef CONFIG_HOLES_IN_ZONE
104 if (!pfn_valid(page_to_pfn(page)))
Dave Hansenc6a57e12005-10-29 18:16:52 -0700105 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#endif
107 if (zone != page_zone(page))
Dave Hansenc6a57e12005-10-29 18:16:52 -0700108 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))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return 1;
Dave Hansenc6a57e12005-10-29 18:16:52 -0700119 if (!page_is_consistent(zone, page))
120 return 1;
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 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",
Andi Kleen07808b72005-11-05 17:25:53 +0100130 (int)(2*sizeof(unsigned long)), (unsigned long)page->flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 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");
Hugh Dickins334795e2005-06-21 17:15:08 -0700135 page->flags &= ~(1 << PG_lru |
136 1 << PG_private |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 1 << PG_locked |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 1 << PG_active |
139 1 << PG_dirty |
Hugh Dickins334795e2005-06-21 17:15:08 -0700140 1 << PG_reclaim |
141 1 << PG_slab |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 1 << PG_swapcache |
Nick Pigginb5810032005-10-29 18:16:12 -0700143 1 << PG_writeback |
144 1 << PG_reserved );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 set_page_count(page, 0);
146 reset_page_mapcount(page);
147 page->mapping = NULL;
Randy Dunlap9f158332005-09-13 01:25:16 -0700148 add_taint(TAINT_BAD_PAGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
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);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700183 set_page_private(p, (unsigned long)page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
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);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700203 if (page_private(p) != (unsigned long)page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 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) {
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700216 return page_private(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
219static inline void set_page_order(struct page *page, int order) {
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700220 set_page_private(page, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 __SetPagePrivate(page);
222}
223
224static inline void rmv_page_order(struct page *page)
225{
226 __ClearPagePrivate(page);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700227 set_page_private(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
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.
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700267 * for recording page's order, we use page_private(page) and PG_private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 *
269 */
270static inline int page_is_buddy(struct page *page, int order)
271{
272 if (PagePrivate(page) &&
273 (page_order(page) == order) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 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
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700293 * order is recorded in page_private(page) field.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 * 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 |
Nick Pigginb5810032005-10-29 18:16:12 -0700356 1 << PG_writeback |
357 1 << PG_reserved )))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 bad_page(function, page);
359 if (PageDirty(page))
Nick Piggin242e5462005-09-03 15:54:50 -0700360 __ClearPageDirty(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
363/*
364 * Frees a list of pages.
365 * Assumes all pages on list are in same zone, and of same order.
Renaud Lienhart207f36e2005-09-10 00:26:59 -0700366 * count is the number of pages to free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 *
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{
Hugh Dickins334795e2005-06-21 17:15:08 -0700473 if ( page_mapcount(page) ||
474 page->mapping != NULL ||
475 page_count(page) != 0 ||
476 (page->flags & (
477 1 << PG_lru |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 1 << PG_private |
479 1 << PG_locked |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 1 << PG_active |
481 1 << PG_dirty |
482 1 << PG_reclaim |
Hugh Dickins334795e2005-06-21 17:15:08 -0700483 1 << PG_slab |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 1 << PG_swapcache |
Nick Pigginb5810032005-10-29 18:16:12 -0700485 1 << PG_writeback |
486 1 << PG_reserved )))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 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);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700492 set_page_private(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 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
Christoph Lameter4ae7c032005-06-21 17:14:57 -0700548#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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578#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
Christoph Lametere7c8d5c2005-06-21 17:14:47 -0700587 pset = zone_pcp(zone, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 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();
Christoph Lametere7c8d5c2005-06-21 17:14:47 -0700650 p = zone_pcp(z,cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (pg == orig) {
Christoph Lametere7c8d5c2005-06-21 17:14:47 -0700652 p->numa_hit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 } else {
654 p->numa_miss++;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -0700655 zone_pcp(zonelist->zones[0], cpu)->numa_foreign++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
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);
Christoph Lametere7c8d5c2005-06-21 17:14:47 -0700682 pcp = &zone_pcp(zone, get_cpu())->pcp[cold];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 list_add(&page->lru, &pcp->list);
685 pcp->count++;
Christoph Lameter2caaad42005-06-21 17:15:00 -0700686 if (pcp->count >= pcp->high)
687 pcp->count -= free_pages_bulk(zone, pcp->batch, &pcp->list, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 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
Al Virodd0fc662005-10-07 07:46:04 +0100702static inline void prep_zero_page(struct page *page, int order, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
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 *
Al Virodd0fc662005-10-07 07:46:04 +0100717buffered_rmqueue(struct zone *zone, int order, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
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
Christoph Lametere7c8d5c2005-06-21 17:14:47 -0700726 pcp = &zone_pcp(zone, get_cpu())->pcp[cold];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 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();
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800738 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 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
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800758#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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763/*
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,
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800768 int classzone_idx, int alloc_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
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
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800774 if (alloc_flags & ALLOC_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 min -= min / 2;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800776 if (alloc_flags & ALLOC_HARDER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 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
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800794/*
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)
Martin Hicks753ee722005-06-21 17:14:41 -0700801{
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800802 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;
Martin Hicks753ee722005-06-21 17:14:41 -0700828}
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830/*
831 * This is the 'heart' of the zoned buddy allocator.
832 */
833struct page * fastcall
Al Virodd0fc662005-10-07 07:46:04 +0100834__alloc_pages(gfp_t gfp_mask, unsigned int order,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 struct zonelist *zonelist)
836{
Al Viro260b2362005-10-21 03:22:44 -0400837 const gfp_t wait = gfp_mask & __GFP_WAIT;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800838 struct zone **z;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 struct page *page;
840 struct reclaim_state reclaim_state;
841 struct task_struct *p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 int do_retry;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800843 int alloc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 int did_some_progress;
845
846 might_sleep_if(wait);
847
Jens Axboe6b1de912005-11-17 21:35:02 +0100848restart:
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800849 z = zonelist->zones; /* the list of zones suitable for gfp_mask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800851 if (unlikely(*z == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 /* Should this ever happen?? */
853 return NULL;
854 }
Jens Axboe6b1de912005-11-17 21:35:02 +0100855
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800856 page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, order,
857 zonelist, ALLOC_CPUSET);
858 if (page)
859 goto got_pg;
860
Jens Axboe6b1de912005-11-17 21:35:02 +0100861 do {
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800862 wakeup_kswapd(*z, order);
Jens Axboe6b1de912005-11-17 21:35:02 +0100863 } while (*(++z));
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800864
Paul Jackson9bf22292005-09-06 15:18:12 -0700865 /*
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800866 * OK, we're below the kswapd watermark and have kicked background
867 * reclaim. Now things get more complex, so set up alloc_flags according
868 * to how we want to proceed.
869 *
870 * The caller may dip into page reserves a bit more if the caller
871 * cannot run direct reclaim, or if the caller has realtime scheduling
872 * policy.
Paul Jackson9bf22292005-09-06 15:18:12 -0700873 */
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800874 alloc_flags = 0;
875 if ((unlikely(rt_task(p)) && !in_interrupt()) || !wait)
876 alloc_flags |= ALLOC_HARDER;
877 if (gfp_mask & __GFP_HIGH)
878 alloc_flags |= ALLOC_HIGH;
879 if (wait)
880 alloc_flags |= ALLOC_CPUSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 /*
883 * Go through the zonelist again. Let __GFP_HIGH and allocations
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800884 * coming from realtime tasks go deeper into reserves.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 *
886 * This is the last chance, in general, before the goto nopage.
887 * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
Paul Jackson9bf22292005-09-06 15:18:12 -0700888 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 */
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800890 page = get_page_from_freelist(gfp_mask, order, zonelist, alloc_flags);
891 if (page)
892 goto got_pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 /* This allocation should allow future memory freeing. */
Nick Pigginb84a35b2005-05-01 08:58:36 -0700895
896 if (((p->flags & PF_MEMALLOC) || unlikely(test_thread_flag(TIF_MEMDIE)))
897 && !in_interrupt()) {
898 if (!(gfp_mask & __GFP_NOMEMALLOC)) {
Kirill Korotaev885036d2005-11-13 16:06:41 -0800899nofail_alloc:
Nick Pigginb84a35b2005-05-01 08:58:36 -0700900 /* go through the zonelist yet again, ignoring mins */
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800901 page = get_page_from_freelist(gfp_mask, order,
902 zonelist, ALLOC_NO_WATERMARKS|ALLOC_CPUSET);
903 if (page)
904 goto got_pg;
Kirill Korotaev885036d2005-11-13 16:06:41 -0800905 if (gfp_mask & __GFP_NOFAIL) {
906 blk_congestion_wait(WRITE, HZ/50);
907 goto nofail_alloc;
908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
910 goto nopage;
911 }
912
913 /* Atomic allocations - we can't balance anything */
914 if (!wait)
915 goto nopage;
916
917rebalance:
918 cond_resched();
919
920 /* We now go into synchronous reclaim */
921 p->flags |= PF_MEMALLOC;
922 reclaim_state.reclaimed_slab = 0;
923 p->reclaim_state = &reclaim_state;
924
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800925 did_some_progress = try_to_free_pages(zonelist->zones, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 p->reclaim_state = NULL;
928 p->flags &= ~PF_MEMALLOC;
929
930 cond_resched();
931
932 if (likely(did_some_progress)) {
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800933 page = get_page_from_freelist(gfp_mask, order,
934 zonelist, alloc_flags);
935 if (page)
936 goto got_pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 } else if ((gfp_mask & __GFP_FS) && !(gfp_mask & __GFP_NORETRY)) {
938 /*
939 * Go through the zonelist yet one more time, keep
940 * very high watermark here, this is only to catch
941 * a parallel oom killing, we must fail if we're still
942 * under heavy pressure.
943 */
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800944 page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, order,
945 zonelist, ALLOC_CPUSET);
946 if (page)
947 goto got_pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Marcelo Tosatti79b9ce32005-07-07 17:56:04 -0700949 out_of_memory(gfp_mask, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 goto restart;
951 }
952
953 /*
954 * Don't let big-order allocations loop unless the caller explicitly
955 * requests that. Wait for some write requests to complete then retry.
956 *
957 * In this implementation, __GFP_REPEAT means __GFP_NOFAIL for order
958 * <= 3, but that may not be true in other implementations.
959 */
960 do_retry = 0;
961 if (!(gfp_mask & __GFP_NORETRY)) {
962 if ((order <= 3) || (gfp_mask & __GFP_REPEAT))
963 do_retry = 1;
964 if (gfp_mask & __GFP_NOFAIL)
965 do_retry = 1;
966 }
967 if (do_retry) {
968 blk_congestion_wait(WRITE, HZ/50);
969 goto rebalance;
970 }
971
972nopage:
973 if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) {
974 printk(KERN_WARNING "%s: page allocation failure."
975 " order:%d, mode:0x%x\n",
976 p->comm, order, gfp_mask);
977 dump_stack();
Janet Morgan578c2fd2005-06-21 17:14:56 -0700978 show_mem();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980got_pg:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return page;
982}
983
984EXPORT_SYMBOL(__alloc_pages);
985
986/*
987 * Common helper functions.
988 */
Al Virodd0fc662005-10-07 07:46:04 +0100989fastcall unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
991 struct page * page;
992 page = alloc_pages(gfp_mask, order);
993 if (!page)
994 return 0;
995 return (unsigned long) page_address(page);
996}
997
998EXPORT_SYMBOL(__get_free_pages);
999
Al Virodd0fc662005-10-07 07:46:04 +01001000fastcall unsigned long get_zeroed_page(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
1002 struct page * page;
1003
1004 /*
1005 * get_zeroed_page() returns a 32-bit address, which cannot represent
1006 * a highmem page
1007 */
Al Viro260b2362005-10-21 03:22:44 -04001008 BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 page = alloc_pages(gfp_mask | __GFP_ZERO, 0);
1011 if (page)
1012 return (unsigned long) page_address(page);
1013 return 0;
1014}
1015
1016EXPORT_SYMBOL(get_zeroed_page);
1017
1018void __pagevec_free(struct pagevec *pvec)
1019{
1020 int i = pagevec_count(pvec);
1021
1022 while (--i >= 0)
1023 free_hot_cold_page(pvec->pages[i], pvec->cold);
1024}
1025
1026fastcall void __free_pages(struct page *page, unsigned int order)
1027{
Nick Pigginb5810032005-10-29 18:16:12 -07001028 if (put_page_testzero(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (order == 0)
1030 free_hot_page(page);
1031 else
1032 __free_pages_ok(page, order);
1033 }
1034}
1035
1036EXPORT_SYMBOL(__free_pages);
1037
1038fastcall void free_pages(unsigned long addr, unsigned int order)
1039{
1040 if (addr != 0) {
1041 BUG_ON(!virt_addr_valid((void *)addr));
1042 __free_pages(virt_to_page((void *)addr), order);
1043 }
1044}
1045
1046EXPORT_SYMBOL(free_pages);
1047
1048/*
1049 * Total amount of free (allocatable) RAM:
1050 */
1051unsigned int nr_free_pages(void)
1052{
1053 unsigned int sum = 0;
1054 struct zone *zone;
1055
1056 for_each_zone(zone)
1057 sum += zone->free_pages;
1058
1059 return sum;
1060}
1061
1062EXPORT_SYMBOL(nr_free_pages);
1063
1064#ifdef CONFIG_NUMA
1065unsigned int nr_free_pages_pgdat(pg_data_t *pgdat)
1066{
1067 unsigned int i, sum = 0;
1068
1069 for (i = 0; i < MAX_NR_ZONES; i++)
1070 sum += pgdat->node_zones[i].free_pages;
1071
1072 return sum;
1073}
1074#endif
1075
1076static unsigned int nr_free_zone_pages(int offset)
1077{
Martin J. Blighe310fd42005-07-29 22:59:18 -07001078 /* Just pick one node, since fallback list is circular */
1079 pg_data_t *pgdat = NODE_DATA(numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 unsigned int sum = 0;
1081
Martin J. Blighe310fd42005-07-29 22:59:18 -07001082 struct zonelist *zonelist = pgdat->node_zonelists + offset;
1083 struct zone **zonep = zonelist->zones;
1084 struct zone *zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Martin J. Blighe310fd42005-07-29 22:59:18 -07001086 for (zone = *zonep++; zone; zone = *zonep++) {
1087 unsigned long size = zone->present_pages;
1088 unsigned long high = zone->pages_high;
1089 if (size > high)
1090 sum += size - high;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
1092
1093 return sum;
1094}
1095
1096/*
1097 * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
1098 */
1099unsigned int nr_free_buffer_pages(void)
1100{
Al Viroaf4ca452005-10-21 02:55:38 -04001101 return nr_free_zone_pages(gfp_zone(GFP_USER));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102}
1103
1104/*
1105 * Amount of free RAM allocatable within all zones
1106 */
1107unsigned int nr_free_pagecache_pages(void)
1108{
Al Viroaf4ca452005-10-21 02:55:38 -04001109 return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
1111
1112#ifdef CONFIG_HIGHMEM
1113unsigned int nr_free_highpages (void)
1114{
1115 pg_data_t *pgdat;
1116 unsigned int pages = 0;
1117
1118 for_each_pgdat(pgdat)
1119 pages += pgdat->node_zones[ZONE_HIGHMEM].free_pages;
1120
1121 return pages;
1122}
1123#endif
1124
1125#ifdef CONFIG_NUMA
1126static void show_node(struct zone *zone)
1127{
1128 printk("Node %d ", zone->zone_pgdat->node_id);
1129}
1130#else
1131#define show_node(zone) do { } while (0)
1132#endif
1133
1134/*
1135 * Accumulate the page_state information across all CPUs.
1136 * The result is unavoidably approximate - it can change
1137 * during and after execution of this function.
1138 */
1139static DEFINE_PER_CPU(struct page_state, page_states) = {0};
1140
1141atomic_t nr_pagecache = ATOMIC_INIT(0);
1142EXPORT_SYMBOL(nr_pagecache);
1143#ifdef CONFIG_SMP
1144DEFINE_PER_CPU(long, nr_pagecache_local) = 0;
1145#endif
1146
Martin Hicksc07e02d2005-09-03 15:55:11 -07001147void __get_page_state(struct page_state *ret, int nr, cpumask_t *cpumask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
1149 int cpu = 0;
1150
1151 memset(ret, 0, sizeof(*ret));
Martin Hicksc07e02d2005-09-03 15:55:11 -07001152 cpus_and(*cpumask, *cpumask, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Martin Hicksc07e02d2005-09-03 15:55:11 -07001154 cpu = first_cpu(*cpumask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 while (cpu < NR_CPUS) {
1156 unsigned long *in, *out, off;
1157
1158 in = (unsigned long *)&per_cpu(page_states, cpu);
1159
Martin Hicksc07e02d2005-09-03 15:55:11 -07001160 cpu = next_cpu(cpu, *cpumask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 if (cpu < NR_CPUS)
1163 prefetch(&per_cpu(page_states, cpu));
1164
1165 out = (unsigned long *)ret;
1166 for (off = 0; off < nr; off++)
1167 *out++ += *in++;
1168 }
1169}
1170
Martin Hicksc07e02d2005-09-03 15:55:11 -07001171void get_page_state_node(struct page_state *ret, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
1173 int nr;
Martin Hicksc07e02d2005-09-03 15:55:11 -07001174 cpumask_t mask = node_to_cpumask(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 nr = offsetof(struct page_state, GET_PAGE_STATE_LAST);
1177 nr /= sizeof(unsigned long);
1178
Martin Hicksc07e02d2005-09-03 15:55:11 -07001179 __get_page_state(ret, nr+1, &mask);
1180}
1181
1182void get_page_state(struct page_state *ret)
1183{
1184 int nr;
1185 cpumask_t mask = CPU_MASK_ALL;
1186
1187 nr = offsetof(struct page_state, GET_PAGE_STATE_LAST);
1188 nr /= sizeof(unsigned long);
1189
1190 __get_page_state(ret, nr + 1, &mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191}
1192
1193void get_full_page_state(struct page_state *ret)
1194{
Martin Hicksc07e02d2005-09-03 15:55:11 -07001195 cpumask_t mask = CPU_MASK_ALL;
1196
1197 __get_page_state(ret, sizeof(*ret) / sizeof(unsigned long), &mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198}
1199
Benjamin LaHaisec2f29ea2005-06-21 17:14:55 -07001200unsigned long __read_page_state(unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
1202 unsigned long ret = 0;
1203 int cpu;
1204
1205 for_each_online_cpu(cpu) {
1206 unsigned long in;
1207
1208 in = (unsigned long)&per_cpu(page_states, cpu) + offset;
1209 ret += *((unsigned long *)in);
1210 }
1211 return ret;
1212}
1213
Benjamin LaHaise83e5d8f2005-06-21 17:14:54 -07001214void __mod_page_state(unsigned long offset, unsigned long delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
1216 unsigned long flags;
1217 void* ptr;
1218
1219 local_irq_save(flags);
1220 ptr = &__get_cpu_var(page_states);
1221 *(unsigned long*)(ptr + offset) += delta;
1222 local_irq_restore(flags);
1223}
1224
1225EXPORT_SYMBOL(__mod_page_state);
1226
1227void __get_zone_counts(unsigned long *active, unsigned long *inactive,
1228 unsigned long *free, struct pglist_data *pgdat)
1229{
1230 struct zone *zones = pgdat->node_zones;
1231 int i;
1232
1233 *active = 0;
1234 *inactive = 0;
1235 *free = 0;
1236 for (i = 0; i < MAX_NR_ZONES; i++) {
1237 *active += zones[i].nr_active;
1238 *inactive += zones[i].nr_inactive;
1239 *free += zones[i].free_pages;
1240 }
1241}
1242
1243void get_zone_counts(unsigned long *active,
1244 unsigned long *inactive, unsigned long *free)
1245{
1246 struct pglist_data *pgdat;
1247
1248 *active = 0;
1249 *inactive = 0;
1250 *free = 0;
1251 for_each_pgdat(pgdat) {
1252 unsigned long l, m, n;
1253 __get_zone_counts(&l, &m, &n, pgdat);
1254 *active += l;
1255 *inactive += m;
1256 *free += n;
1257 }
1258}
1259
1260void si_meminfo(struct sysinfo *val)
1261{
1262 val->totalram = totalram_pages;
1263 val->sharedram = 0;
1264 val->freeram = nr_free_pages();
1265 val->bufferram = nr_blockdev_pages();
1266#ifdef CONFIG_HIGHMEM
1267 val->totalhigh = totalhigh_pages;
1268 val->freehigh = nr_free_highpages();
1269#else
1270 val->totalhigh = 0;
1271 val->freehigh = 0;
1272#endif
1273 val->mem_unit = PAGE_SIZE;
1274}
1275
1276EXPORT_SYMBOL(si_meminfo);
1277
1278#ifdef CONFIG_NUMA
1279void si_meminfo_node(struct sysinfo *val, int nid)
1280{
1281 pg_data_t *pgdat = NODE_DATA(nid);
1282
1283 val->totalram = pgdat->node_present_pages;
1284 val->freeram = nr_free_pages_pgdat(pgdat);
1285 val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].present_pages;
1286 val->freehigh = pgdat->node_zones[ZONE_HIGHMEM].free_pages;
1287 val->mem_unit = PAGE_SIZE;
1288}
1289#endif
1290
1291#define K(x) ((x) << (PAGE_SHIFT-10))
1292
1293/*
1294 * Show free area list (used inside shift_scroll-lock stuff)
1295 * We also calculate the percentage fragmentation. We do this by counting the
1296 * memory on each free list with the exception of the first item on the list.
1297 */
1298void show_free_areas(void)
1299{
1300 struct page_state ps;
1301 int cpu, temperature;
1302 unsigned long active;
1303 unsigned long inactive;
1304 unsigned long free;
1305 struct zone *zone;
1306
1307 for_each_zone(zone) {
1308 show_node(zone);
1309 printk("%s per-cpu:", zone->name);
1310
1311 if (!zone->present_pages) {
1312 printk(" empty\n");
1313 continue;
1314 } else
1315 printk("\n");
1316
Dave Jones6b482c62005-11-10 15:45:56 -05001317 for_each_online_cpu(cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 struct per_cpu_pageset *pageset;
1319
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001320 pageset = zone_pcp(zone, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 for (temperature = 0; temperature < 2; temperature++)
Christoph Lameter4ae7c032005-06-21 17:14:57 -07001323 printk("cpu %d %s: low %d, high %d, batch %d used:%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 cpu,
1325 temperature ? "cold" : "hot",
1326 pageset->pcp[temperature].low,
1327 pageset->pcp[temperature].high,
Christoph Lameter4ae7c032005-06-21 17:14:57 -07001328 pageset->pcp[temperature].batch,
1329 pageset->pcp[temperature].count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
1331 }
1332
1333 get_page_state(&ps);
1334 get_zone_counts(&active, &inactive, &free);
1335
Denis Vlasenkoc0d62212005-06-21 17:15:14 -07001336 printk("Free pages: %11ukB (%ukB HighMem)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 K(nr_free_pages()),
1338 K(nr_free_highpages()));
1339
1340 printk("Active:%lu inactive:%lu dirty:%lu writeback:%lu "
1341 "unstable:%lu free:%u slab:%lu mapped:%lu pagetables:%lu\n",
1342 active,
1343 inactive,
1344 ps.nr_dirty,
1345 ps.nr_writeback,
1346 ps.nr_unstable,
1347 nr_free_pages(),
1348 ps.nr_slab,
1349 ps.nr_mapped,
1350 ps.nr_page_table_pages);
1351
1352 for_each_zone(zone) {
1353 int i;
1354
1355 show_node(zone);
1356 printk("%s"
1357 " free:%lukB"
1358 " min:%lukB"
1359 " low:%lukB"
1360 " high:%lukB"
1361 " active:%lukB"
1362 " inactive:%lukB"
1363 " present:%lukB"
1364 " pages_scanned:%lu"
1365 " all_unreclaimable? %s"
1366 "\n",
1367 zone->name,
1368 K(zone->free_pages),
1369 K(zone->pages_min),
1370 K(zone->pages_low),
1371 K(zone->pages_high),
1372 K(zone->nr_active),
1373 K(zone->nr_inactive),
1374 K(zone->present_pages),
1375 zone->pages_scanned,
1376 (zone->all_unreclaimable ? "yes" : "no")
1377 );
1378 printk("lowmem_reserve[]:");
1379 for (i = 0; i < MAX_NR_ZONES; i++)
1380 printk(" %lu", zone->lowmem_reserve[i]);
1381 printk("\n");
1382 }
1383
1384 for_each_zone(zone) {
1385 unsigned long nr, flags, order, total = 0;
1386
1387 show_node(zone);
1388 printk("%s: ", zone->name);
1389 if (!zone->present_pages) {
1390 printk("empty\n");
1391 continue;
1392 }
1393
1394 spin_lock_irqsave(&zone->lock, flags);
1395 for (order = 0; order < MAX_ORDER; order++) {
1396 nr = zone->free_area[order].nr_free;
1397 total += nr << order;
1398 printk("%lu*%lukB ", nr, K(1UL) << order);
1399 }
1400 spin_unlock_irqrestore(&zone->lock, flags);
1401 printk("= %lukB\n", K(total));
1402 }
1403
1404 show_swap_cache_info();
1405}
1406
1407/*
1408 * Builds allocation fallback zone lists.
1409 */
1410static int __init build_zonelists_node(pg_data_t *pgdat, struct zonelist *zonelist, int j, int k)
1411{
1412 switch (k) {
1413 struct zone *zone;
1414 default:
1415 BUG();
1416 case ZONE_HIGHMEM:
1417 zone = pgdat->node_zones + ZONE_HIGHMEM;
1418 if (zone->present_pages) {
1419#ifndef CONFIG_HIGHMEM
1420 BUG();
1421#endif
1422 zonelist->zones[j++] = zone;
1423 }
1424 case ZONE_NORMAL:
1425 zone = pgdat->node_zones + ZONE_NORMAL;
1426 if (zone->present_pages)
1427 zonelist->zones[j++] = zone;
Andi Kleena2f1b422005-11-05 17:25:53 +01001428 case ZONE_DMA32:
1429 zone = pgdat->node_zones + ZONE_DMA32;
1430 if (zone->present_pages)
1431 zonelist->zones[j++] = zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 case ZONE_DMA:
1433 zone = pgdat->node_zones + ZONE_DMA;
1434 if (zone->present_pages)
1435 zonelist->zones[j++] = zone;
1436 }
1437
1438 return j;
1439}
1440
Al Viro260b2362005-10-21 03:22:44 -04001441static inline int highest_zone(int zone_bits)
1442{
1443 int res = ZONE_NORMAL;
1444 if (zone_bits & (__force int)__GFP_HIGHMEM)
1445 res = ZONE_HIGHMEM;
Andi Kleena2f1b422005-11-05 17:25:53 +01001446 if (zone_bits & (__force int)__GFP_DMA32)
1447 res = ZONE_DMA32;
Al Viro260b2362005-10-21 03:22:44 -04001448 if (zone_bits & (__force int)__GFP_DMA)
1449 res = ZONE_DMA;
1450 return res;
1451}
1452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453#ifdef CONFIG_NUMA
1454#define MAX_NODE_LOAD (num_online_nodes())
1455static int __initdata node_load[MAX_NUMNODES];
1456/**
Pavel Pisa4dc3b162005-05-01 08:59:25 -07001457 * 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 -07001458 * @node: node whose fallback list we're appending
1459 * @used_node_mask: nodemask_t of already used nodes
1460 *
1461 * We use a number of factors to determine which is the next node that should
1462 * appear on a given node's fallback list. The node should not have appeared
1463 * already in @node's fallback list, and it should be the next closest node
1464 * according to the distance array (which contains arbitrary distance values
1465 * from each node to each node in the system), and should also prefer nodes
1466 * with no CPUs, since presumably they'll have very little allocation pressure
1467 * on them otherwise.
1468 * It returns -1 if no node is found.
1469 */
1470static int __init find_next_best_node(int node, nodemask_t *used_node_mask)
1471{
1472 int i, n, val;
1473 int min_val = INT_MAX;
1474 int best_node = -1;
1475
1476 for_each_online_node(i) {
1477 cpumask_t tmp;
1478
1479 /* Start from local node */
1480 n = (node+i) % num_online_nodes();
1481
1482 /* Don't want a node to appear more than once */
1483 if (node_isset(n, *used_node_mask))
1484 continue;
1485
1486 /* Use the local node if we haven't already */
1487 if (!node_isset(node, *used_node_mask)) {
1488 best_node = node;
1489 break;
1490 }
1491
1492 /* Use the distance array to find the distance */
1493 val = node_distance(node, n);
1494
1495 /* Give preference to headless and unused nodes */
1496 tmp = node_to_cpumask(n);
1497 if (!cpus_empty(tmp))
1498 val += PENALTY_FOR_NODE_WITH_CPUS;
1499
1500 /* Slight preference for less loaded node */
1501 val *= (MAX_NODE_LOAD*MAX_NUMNODES);
1502 val += node_load[n];
1503
1504 if (val < min_val) {
1505 min_val = val;
1506 best_node = n;
1507 }
1508 }
1509
1510 if (best_node >= 0)
1511 node_set(best_node, *used_node_mask);
1512
1513 return best_node;
1514}
1515
1516static void __init build_zonelists(pg_data_t *pgdat)
1517{
1518 int i, j, k, node, local_node;
1519 int prev_node, load;
1520 struct zonelist *zonelist;
1521 nodemask_t used_mask;
1522
1523 /* initialize zonelists */
1524 for (i = 0; i < GFP_ZONETYPES; i++) {
1525 zonelist = pgdat->node_zonelists + i;
1526 zonelist->zones[0] = NULL;
1527 }
1528
1529 /* NUMA-aware ordering of nodes */
1530 local_node = pgdat->node_id;
1531 load = num_online_nodes();
1532 prev_node = local_node;
1533 nodes_clear(used_mask);
1534 while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
1535 /*
1536 * We don't want to pressure a particular node.
1537 * So adding penalty to the first node in same
1538 * distance group to make it round-robin.
1539 */
1540 if (node_distance(local_node, node) !=
1541 node_distance(local_node, prev_node))
1542 node_load[node] += load;
1543 prev_node = node;
1544 load--;
1545 for (i = 0; i < GFP_ZONETYPES; i++) {
1546 zonelist = pgdat->node_zonelists + i;
1547 for (j = 0; zonelist->zones[j] != NULL; j++);
1548
Al Viro260b2362005-10-21 03:22:44 -04001549 k = highest_zone(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1552 zonelist->zones[j] = NULL;
1553 }
1554 }
1555}
1556
1557#else /* CONFIG_NUMA */
1558
1559static void __init build_zonelists(pg_data_t *pgdat)
1560{
1561 int i, j, k, node, local_node;
1562
1563 local_node = pgdat->node_id;
1564 for (i = 0; i < GFP_ZONETYPES; i++) {
1565 struct zonelist *zonelist;
1566
1567 zonelist = pgdat->node_zonelists + i;
1568
1569 j = 0;
Al Viro260b2362005-10-21 03:22:44 -04001570 k = highest_zone(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 j = build_zonelists_node(pgdat, zonelist, j, k);
1572 /*
1573 * Now we build the zonelist so that it contains the zones
1574 * of all the other nodes.
1575 * We don't want to pressure a particular node, so when
1576 * building the zones for node N, we make sure that the
1577 * zones coming right after the local ones are those from
1578 * node N+1 (modulo N)
1579 */
1580 for (node = local_node + 1; node < MAX_NUMNODES; node++) {
1581 if (!node_online(node))
1582 continue;
1583 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1584 }
1585 for (node = 0; node < local_node; node++) {
1586 if (!node_online(node))
1587 continue;
1588 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1589 }
1590
1591 zonelist->zones[j] = NULL;
1592 }
1593}
1594
1595#endif /* CONFIG_NUMA */
1596
1597void __init build_all_zonelists(void)
1598{
1599 int i;
1600
1601 for_each_online_node(i)
1602 build_zonelists(NODE_DATA(i));
1603 printk("Built %i zonelists\n", num_online_nodes());
1604 cpuset_init_current_mems_allowed();
1605}
1606
1607/*
1608 * Helper functions to size the waitqueue hash table.
1609 * Essentially these want to choose hash table sizes sufficiently
1610 * large so that collisions trying to wait on pages are rare.
1611 * But in fact, the number of active page waitqueues on typical
1612 * systems is ridiculously low, less than 200. So this is even
1613 * conservative, even though it seems large.
1614 *
1615 * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
1616 * waitqueues, i.e. the size of the waitq table given the number of pages.
1617 */
1618#define PAGES_PER_WAITQUEUE 256
1619
1620static inline unsigned long wait_table_size(unsigned long pages)
1621{
1622 unsigned long size = 1;
1623
1624 pages /= PAGES_PER_WAITQUEUE;
1625
1626 while (size < pages)
1627 size <<= 1;
1628
1629 /*
1630 * Once we have dozens or even hundreds of threads sleeping
1631 * on IO we've got bigger problems than wait queue collision.
1632 * Limit the size of the wait table to a reasonable size.
1633 */
1634 size = min(size, 4096UL);
1635
1636 return max(size, 4UL);
1637}
1638
1639/*
1640 * This is an integer logarithm so that shifts can be used later
1641 * to extract the more random high bits from the multiplicative
1642 * hash function before the remainder is taken.
1643 */
1644static inline unsigned long wait_table_bits(unsigned long size)
1645{
1646 return ffz(~size);
1647}
1648
1649#define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
1650
1651static void __init calculate_zone_totalpages(struct pglist_data *pgdat,
1652 unsigned long *zones_size, unsigned long *zholes_size)
1653{
1654 unsigned long realtotalpages, totalpages = 0;
1655 int i;
1656
1657 for (i = 0; i < MAX_NR_ZONES; i++)
1658 totalpages += zones_size[i];
1659 pgdat->node_spanned_pages = totalpages;
1660
1661 realtotalpages = totalpages;
1662 if (zholes_size)
1663 for (i = 0; i < MAX_NR_ZONES; i++)
1664 realtotalpages -= zholes_size[i];
1665 pgdat->node_present_pages = realtotalpages;
1666 printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id, realtotalpages);
1667}
1668
1669
1670/*
1671 * Initially all pages are reserved - free ones are freed
1672 * up by free_all_bootmem() once the early boot process is
1673 * done. Non-atomic initialization, single-pass.
1674 */
Dave Hansen3947be12005-10-29 18:16:54 -07001675void __devinit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 unsigned long start_pfn)
1677{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 struct page *page;
Andy Whitcroft29751f62005-06-23 00:08:00 -07001679 unsigned long end_pfn = start_pfn + size;
1680 unsigned long pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Andy Whitcroftd41dee32005-06-23 00:07:54 -07001682 for (pfn = start_pfn; pfn < end_pfn; pfn++, page++) {
1683 if (!early_pfn_valid(pfn))
1684 continue;
Andy Whitcroft641c7672005-06-23 00:07:59 -07001685 if (!early_pfn_in_nid(pfn, nid))
1686 continue;
Andy Whitcroftd41dee32005-06-23 00:07:54 -07001687 page = pfn_to_page(pfn);
1688 set_page_links(page, zone, nid, pfn);
Nick Pigginb5810032005-10-29 18:16:12 -07001689 set_page_count(page, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 reset_page_mapcount(page);
1691 SetPageReserved(page);
1692 INIT_LIST_HEAD(&page->lru);
1693#ifdef WANT_PAGE_VIRTUAL
1694 /* The shift won't overflow because ZONE_NORMAL is below 4G. */
1695 if (!is_highmem_idx(zone))
Bob Picco3212c6b2005-06-27 14:36:28 -07001696 set_page_address(page, __va(pfn << PAGE_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
1699}
1700
1701void zone_init_free_lists(struct pglist_data *pgdat, struct zone *zone,
1702 unsigned long size)
1703{
1704 int order;
1705 for (order = 0; order < MAX_ORDER ; order++) {
1706 INIT_LIST_HEAD(&zone->free_area[order].free_list);
1707 zone->free_area[order].nr_free = 0;
1708 }
1709}
1710
Andy Whitcroftd41dee32005-06-23 00:07:54 -07001711#define ZONETABLE_INDEX(x, zone_nr) ((x << ZONES_SHIFT) | zone_nr)
1712void zonetable_add(struct zone *zone, int nid, int zid, unsigned long pfn,
1713 unsigned long size)
1714{
1715 unsigned long snum = pfn_to_section_nr(pfn);
1716 unsigned long end = pfn_to_section_nr(pfn + size);
1717
1718 if (FLAGS_HAS_NODE)
1719 zone_table[ZONETABLE_INDEX(nid, zid)] = zone;
1720 else
1721 for (; snum <= end; snum++)
1722 zone_table[ZONETABLE_INDEX(snum, zid)] = zone;
1723}
1724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725#ifndef __HAVE_ARCH_MEMMAP_INIT
1726#define memmap_init(size, nid, zone, start_pfn) \
1727 memmap_init_zone((size), (nid), (zone), (start_pfn))
1728#endif
1729
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001730static int __devinit zone_batchsize(struct zone *zone)
1731{
1732 int batch;
1733
1734 /*
1735 * The per-cpu-pages pools are set to around 1000th of the
Seth, Rohitba56e912005-10-29 18:15:47 -07001736 * size of the zone. But no more than 1/2 of a meg.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001737 *
1738 * OK, so we don't know how big the cache is. So guess.
1739 */
1740 batch = zone->present_pages / 1024;
Seth, Rohitba56e912005-10-29 18:15:47 -07001741 if (batch * PAGE_SIZE > 512 * 1024)
1742 batch = (512 * 1024) / PAGE_SIZE;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001743 batch /= 4; /* We effectively *= 4 below */
1744 if (batch < 1)
1745 batch = 1;
1746
1747 /*
Seth, Rohitba56e912005-10-29 18:15:47 -07001748 * We will be trying to allcoate bigger chunks of contiguous
1749 * memory of the order of fls(batch). This should result in
1750 * better cache coloring.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001751 *
Seth, Rohitba56e912005-10-29 18:15:47 -07001752 * A sanity check also to ensure that batch is still in limits.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001753 */
Seth, Rohitba56e912005-10-29 18:15:47 -07001754 batch = (1 << fls(batch + batch/2));
1755
1756 if (fls(batch) >= (PAGE_SHIFT + MAX_ORDER - 2))
1757 batch = PAGE_SHIFT + ((MAX_ORDER - 1 - PAGE_SHIFT)/2);
1758
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001759 return batch;
1760}
1761
Christoph Lameter2caaad42005-06-21 17:15:00 -07001762inline void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
1763{
1764 struct per_cpu_pages *pcp;
1765
Magnus Damm1c6fe942005-10-26 01:58:59 -07001766 memset(p, 0, sizeof(*p));
1767
Christoph Lameter2caaad42005-06-21 17:15:00 -07001768 pcp = &p->pcp[0]; /* hot */
1769 pcp->count = 0;
Seth, Rohite46a5e22005-10-29 18:15:48 -07001770 pcp->low = 0;
Christoph Lameter2caaad42005-06-21 17:15:00 -07001771 pcp->high = 6 * batch;
1772 pcp->batch = max(1UL, 1 * batch);
1773 INIT_LIST_HEAD(&pcp->list);
1774
1775 pcp = &p->pcp[1]; /* cold*/
1776 pcp->count = 0;
1777 pcp->low = 0;
1778 pcp->high = 2 * batch;
Seth, Rohite46a5e22005-10-29 18:15:48 -07001779 pcp->batch = max(1UL, batch/2);
Christoph Lameter2caaad42005-06-21 17:15:00 -07001780 INIT_LIST_HEAD(&pcp->list);
1781}
1782
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001783#ifdef CONFIG_NUMA
1784/*
Christoph Lameter2caaad42005-06-21 17:15:00 -07001785 * Boot pageset table. One per cpu which is going to be used for all
1786 * zones and all nodes. The parameters will be set in such a way
1787 * that an item put on a list will immediately be handed over to
1788 * the buddy list. This is safe since pageset manipulation is done
1789 * with interrupts disabled.
1790 *
1791 * Some NUMA counter updates may also be caught by the boot pagesets.
Christoph Lameterb7c84c62005-06-22 20:26:07 -07001792 *
1793 * The boot_pagesets must be kept even after bootup is complete for
1794 * unused processors and/or zones. They do play a role for bootstrapping
1795 * hotplugged processors.
1796 *
1797 * zoneinfo_show() and maybe other functions do
1798 * not check if the processor is online before following the pageset pointer.
1799 * Other parts of the kernel may not check if the zone is available.
Christoph Lameter2caaad42005-06-21 17:15:00 -07001800 */
1801static struct per_cpu_pageset
Christoph Lameterb7c84c62005-06-22 20:26:07 -07001802 boot_pageset[NR_CPUS];
Christoph Lameter2caaad42005-06-21 17:15:00 -07001803
1804/*
1805 * Dynamically allocate memory for the
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001806 * per cpu pageset array in struct zone.
1807 */
1808static int __devinit process_zones(int cpu)
1809{
1810 struct zone *zone, *dzone;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001811
1812 for_each_zone(zone) {
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001813
Christoph Lameter2caaad42005-06-21 17:15:00 -07001814 zone->pageset[cpu] = kmalloc_node(sizeof(struct per_cpu_pageset),
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001815 GFP_KERNEL, cpu_to_node(cpu));
Christoph Lameter2caaad42005-06-21 17:15:00 -07001816 if (!zone->pageset[cpu])
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001817 goto bad;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001818
Christoph Lameter2caaad42005-06-21 17:15:00 -07001819 setup_pageset(zone->pageset[cpu], zone_batchsize(zone));
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001820 }
1821
1822 return 0;
1823bad:
1824 for_each_zone(dzone) {
1825 if (dzone == zone)
1826 break;
1827 kfree(dzone->pageset[cpu]);
1828 dzone->pageset[cpu] = NULL;
1829 }
1830 return -ENOMEM;
1831}
1832
1833static inline void free_zone_pagesets(int cpu)
1834{
1835#ifdef CONFIG_NUMA
1836 struct zone *zone;
1837
1838 for_each_zone(zone) {
1839 struct per_cpu_pageset *pset = zone_pcp(zone, cpu);
1840
1841 zone_pcp(zone, cpu) = NULL;
1842 kfree(pset);
1843 }
1844#endif
1845}
1846
1847static int __devinit pageset_cpuup_callback(struct notifier_block *nfb,
1848 unsigned long action,
1849 void *hcpu)
1850{
1851 int cpu = (long)hcpu;
1852 int ret = NOTIFY_OK;
1853
1854 switch (action) {
1855 case CPU_UP_PREPARE:
1856 if (process_zones(cpu))
1857 ret = NOTIFY_BAD;
1858 break;
Andi Kleenb0d41692005-11-05 17:25:53 +01001859 case CPU_UP_CANCELED:
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001860 case CPU_DEAD:
1861 free_zone_pagesets(cpu);
1862 break;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001863 default:
1864 break;
1865 }
1866 return ret;
1867}
1868
1869static struct notifier_block pageset_notifier =
1870 { &pageset_cpuup_callback, NULL, 0 };
1871
1872void __init setup_per_cpu_pageset()
1873{
1874 int err;
1875
1876 /* Initialize per_cpu_pageset for cpu 0.
1877 * A cpuup callback will do this for every cpu
1878 * as it comes online
1879 */
1880 err = process_zones(smp_processor_id());
1881 BUG_ON(err);
1882 register_cpu_notifier(&pageset_notifier);
1883}
1884
1885#endif
1886
Dave Hansened8ece22005-10-29 18:16:50 -07001887static __devinit
1888void zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages)
1889{
1890 int i;
1891 struct pglist_data *pgdat = zone->zone_pgdat;
1892
1893 /*
1894 * The per-page waitqueue mechanism uses hashed waitqueues
1895 * per zone.
1896 */
1897 zone->wait_table_size = wait_table_size(zone_size_pages);
1898 zone->wait_table_bits = wait_table_bits(zone->wait_table_size);
1899 zone->wait_table = (wait_queue_head_t *)
1900 alloc_bootmem_node(pgdat, zone->wait_table_size
1901 * sizeof(wait_queue_head_t));
1902
1903 for(i = 0; i < zone->wait_table_size; ++i)
1904 init_waitqueue_head(zone->wait_table + i);
1905}
1906
1907static __devinit void zone_pcp_init(struct zone *zone)
1908{
1909 int cpu;
1910 unsigned long batch = zone_batchsize(zone);
1911
1912 for (cpu = 0; cpu < NR_CPUS; cpu++) {
1913#ifdef CONFIG_NUMA
1914 /* Early boot. Slab allocator not functional yet */
1915 zone->pageset[cpu] = &boot_pageset[cpu];
1916 setup_pageset(&boot_pageset[cpu],0);
1917#else
1918 setup_pageset(zone_pcp(zone,cpu), batch);
1919#endif
1920 }
1921 printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%lu\n",
1922 zone->name, zone->present_pages, batch);
1923}
1924
1925static __devinit void init_currently_empty_zone(struct zone *zone,
1926 unsigned long zone_start_pfn, unsigned long size)
1927{
1928 struct pglist_data *pgdat = zone->zone_pgdat;
1929
1930 zone_wait_table_init(zone, size);
1931 pgdat->nr_zones = zone_idx(zone) + 1;
1932
1933 zone->zone_mem_map = pfn_to_page(zone_start_pfn);
1934 zone->zone_start_pfn = zone_start_pfn;
1935
1936 memmap_init(size, pgdat->node_id, zone_idx(zone), zone_start_pfn);
1937
1938 zone_init_free_lists(pgdat, zone, zone->spanned_pages);
1939}
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941/*
1942 * Set up the zone data structures:
1943 * - mark all pages reserved
1944 * - mark all memory queues empty
1945 * - clear the memory bitmaps
1946 */
1947static void __init free_area_init_core(struct pglist_data *pgdat,
1948 unsigned long *zones_size, unsigned long *zholes_size)
1949{
Dave Hansened8ece22005-10-29 18:16:50 -07001950 unsigned long j;
1951 int nid = pgdat->node_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 unsigned long zone_start_pfn = pgdat->node_start_pfn;
1953
Dave Hansen208d54e2005-10-29 18:16:52 -07001954 pgdat_resize_init(pgdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 pgdat->nr_zones = 0;
1956 init_waitqueue_head(&pgdat->kswapd_wait);
1957 pgdat->kswapd_max_order = 0;
1958
1959 for (j = 0; j < MAX_NR_ZONES; j++) {
1960 struct zone *zone = pgdat->node_zones + j;
1961 unsigned long size, realsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 realsize = size = zones_size[j];
1964 if (zholes_size)
1965 realsize -= zholes_size[j];
1966
Andi Kleena2f1b422005-11-05 17:25:53 +01001967 if (j < ZONE_HIGHMEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 nr_kernel_pages += realsize;
1969 nr_all_pages += realsize;
1970
1971 zone->spanned_pages = size;
1972 zone->present_pages = realsize;
1973 zone->name = zone_names[j];
1974 spin_lock_init(&zone->lock);
1975 spin_lock_init(&zone->lru_lock);
Dave Hansenbdc8cb92005-10-29 18:16:53 -07001976 zone_seqlock_init(zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 zone->zone_pgdat = pgdat;
1978 zone->free_pages = 0;
1979
1980 zone->temp_priority = zone->prev_priority = DEF_PRIORITY;
1981
Dave Hansened8ece22005-10-29 18:16:50 -07001982 zone_pcp_init(zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 INIT_LIST_HEAD(&zone->active_list);
1984 INIT_LIST_HEAD(&zone->inactive_list);
1985 zone->nr_scan_active = 0;
1986 zone->nr_scan_inactive = 0;
1987 zone->nr_active = 0;
1988 zone->nr_inactive = 0;
Martin Hicks53e9a612005-09-03 15:54:51 -07001989 atomic_set(&zone->reclaim_in_progress, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 if (!size)
1991 continue;
1992
Andy Whitcroftd41dee32005-06-23 00:07:54 -07001993 zonetable_add(zone, nid, j, zone_start_pfn, size);
Dave Hansened8ece22005-10-29 18:16:50 -07001994 init_currently_empty_zone(zone, zone_start_pfn, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 zone_start_pfn += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
1997}
1998
1999static void __init alloc_node_mem_map(struct pglist_data *pgdat)
2000{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 /* Skip empty nodes */
2002 if (!pgdat->node_spanned_pages)
2003 return;
2004
Andy Whitcroftd41dee32005-06-23 00:07:54 -07002005#ifdef CONFIG_FLAT_NODE_MEM_MAP
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 /* ia64 gets its own node_mem_map, before this, without bootmem */
2007 if (!pgdat->node_mem_map) {
Andy Whitcroftd41dee32005-06-23 00:07:54 -07002008 unsigned long size;
2009 struct page *map;
2010
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 size = (pgdat->node_spanned_pages + 1) * sizeof(struct page);
Dave Hansen6f167ec2005-06-23 00:07:39 -07002012 map = alloc_remap(pgdat->node_id, size);
2013 if (!map)
2014 map = alloc_bootmem_node(pgdat, size);
2015 pgdat->node_mem_map = map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 }
Andy Whitcroftd41dee32005-06-23 00:07:54 -07002017#ifdef CONFIG_FLATMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 /*
2019 * With no DISCONTIG, the global mem_map is just set as node 0's
2020 */
2021 if (pgdat == NODE_DATA(0))
2022 mem_map = NODE_DATA(0)->node_mem_map;
2023#endif
Andy Whitcroftd41dee32005-06-23 00:07:54 -07002024#endif /* CONFIG_FLAT_NODE_MEM_MAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
2026
2027void __init free_area_init_node(int nid, struct pglist_data *pgdat,
2028 unsigned long *zones_size, unsigned long node_start_pfn,
2029 unsigned long *zholes_size)
2030{
2031 pgdat->node_id = nid;
2032 pgdat->node_start_pfn = node_start_pfn;
2033 calculate_zone_totalpages(pgdat, zones_size, zholes_size);
2034
2035 alloc_node_mem_map(pgdat);
2036
2037 free_area_init_core(pgdat, zones_size, zholes_size);
2038}
2039
Dave Hansen93b75042005-06-23 00:07:47 -07002040#ifndef CONFIG_NEED_MULTIPLE_NODES
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041static bootmem_data_t contig_bootmem_data;
2042struct pglist_data contig_page_data = { .bdata = &contig_bootmem_data };
2043
2044EXPORT_SYMBOL(contig_page_data);
Dave Hansen93b75042005-06-23 00:07:47 -07002045#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
2047void __init free_area_init(unsigned long *zones_size)
2048{
Dave Hansen93b75042005-06-23 00:07:47 -07002049 free_area_init_node(0, NODE_DATA(0), zones_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
2051}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
2053#ifdef CONFIG_PROC_FS
2054
2055#include <linux/seq_file.h>
2056
2057static void *frag_start(struct seq_file *m, loff_t *pos)
2058{
2059 pg_data_t *pgdat;
2060 loff_t node = *pos;
2061
2062 for (pgdat = pgdat_list; pgdat && node; pgdat = pgdat->pgdat_next)
2063 --node;
2064
2065 return pgdat;
2066}
2067
2068static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
2069{
2070 pg_data_t *pgdat = (pg_data_t *)arg;
2071
2072 (*pos)++;
2073 return pgdat->pgdat_next;
2074}
2075
2076static void frag_stop(struct seq_file *m, void *arg)
2077{
2078}
2079
2080/*
2081 * This walks the free areas for each zone.
2082 */
2083static int frag_show(struct seq_file *m, void *arg)
2084{
2085 pg_data_t *pgdat = (pg_data_t *)arg;
2086 struct zone *zone;
2087 struct zone *node_zones = pgdat->node_zones;
2088 unsigned long flags;
2089 int order;
2090
2091 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
2092 if (!zone->present_pages)
2093 continue;
2094
2095 spin_lock_irqsave(&zone->lock, flags);
2096 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
2097 for (order = 0; order < MAX_ORDER; ++order)
2098 seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
2099 spin_unlock_irqrestore(&zone->lock, flags);
2100 seq_putc(m, '\n');
2101 }
2102 return 0;
2103}
2104
2105struct seq_operations fragmentation_op = {
2106 .start = frag_start,
2107 .next = frag_next,
2108 .stop = frag_stop,
2109 .show = frag_show,
2110};
2111
Nikita Danilov295ab932005-06-21 17:14:38 -07002112/*
2113 * Output information about zones in @pgdat.
2114 */
2115static int zoneinfo_show(struct seq_file *m, void *arg)
2116{
2117 pg_data_t *pgdat = arg;
2118 struct zone *zone;
2119 struct zone *node_zones = pgdat->node_zones;
2120 unsigned long flags;
2121
2122 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; zone++) {
2123 int i;
2124
2125 if (!zone->present_pages)
2126 continue;
2127
2128 spin_lock_irqsave(&zone->lock, flags);
2129 seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
2130 seq_printf(m,
2131 "\n pages free %lu"
2132 "\n min %lu"
2133 "\n low %lu"
2134 "\n high %lu"
2135 "\n active %lu"
2136 "\n inactive %lu"
2137 "\n scanned %lu (a: %lu i: %lu)"
2138 "\n spanned %lu"
2139 "\n present %lu",
2140 zone->free_pages,
2141 zone->pages_min,
2142 zone->pages_low,
2143 zone->pages_high,
2144 zone->nr_active,
2145 zone->nr_inactive,
2146 zone->pages_scanned,
2147 zone->nr_scan_active, zone->nr_scan_inactive,
2148 zone->spanned_pages,
2149 zone->present_pages);
2150 seq_printf(m,
2151 "\n protection: (%lu",
2152 zone->lowmem_reserve[0]);
2153 for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
2154 seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
2155 seq_printf(m,
2156 ")"
2157 "\n pagesets");
2158 for (i = 0; i < ARRAY_SIZE(zone->pageset); i++) {
2159 struct per_cpu_pageset *pageset;
2160 int j;
2161
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002162 pageset = zone_pcp(zone, i);
Nikita Danilov295ab932005-06-21 17:14:38 -07002163 for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
2164 if (pageset->pcp[j].count)
2165 break;
2166 }
2167 if (j == ARRAY_SIZE(pageset->pcp))
2168 continue;
2169 for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
2170 seq_printf(m,
2171 "\n cpu: %i pcp: %i"
2172 "\n count: %i"
2173 "\n low: %i"
2174 "\n high: %i"
2175 "\n batch: %i",
2176 i, j,
2177 pageset->pcp[j].count,
2178 pageset->pcp[j].low,
2179 pageset->pcp[j].high,
2180 pageset->pcp[j].batch);
2181 }
2182#ifdef CONFIG_NUMA
2183 seq_printf(m,
2184 "\n numa_hit: %lu"
2185 "\n numa_miss: %lu"
2186 "\n numa_foreign: %lu"
2187 "\n interleave_hit: %lu"
2188 "\n local_node: %lu"
2189 "\n other_node: %lu",
2190 pageset->numa_hit,
2191 pageset->numa_miss,
2192 pageset->numa_foreign,
2193 pageset->interleave_hit,
2194 pageset->local_node,
2195 pageset->other_node);
2196#endif
2197 }
2198 seq_printf(m,
2199 "\n all_unreclaimable: %u"
2200 "\n prev_priority: %i"
2201 "\n temp_priority: %i"
2202 "\n start_pfn: %lu",
2203 zone->all_unreclaimable,
2204 zone->prev_priority,
2205 zone->temp_priority,
2206 zone->zone_start_pfn);
2207 spin_unlock_irqrestore(&zone->lock, flags);
2208 seq_putc(m, '\n');
2209 }
2210 return 0;
2211}
2212
2213struct seq_operations zoneinfo_op = {
2214 .start = frag_start, /* iterate over all zones. The same as in
2215 * fragmentation. */
2216 .next = frag_next,
2217 .stop = frag_stop,
2218 .show = zoneinfo_show,
2219};
2220
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221static char *vmstat_text[] = {
2222 "nr_dirty",
2223 "nr_writeback",
2224 "nr_unstable",
2225 "nr_page_table_pages",
2226 "nr_mapped",
2227 "nr_slab",
2228
2229 "pgpgin",
2230 "pgpgout",
2231 "pswpin",
2232 "pswpout",
2233 "pgalloc_high",
2234
2235 "pgalloc_normal",
2236 "pgalloc_dma",
2237 "pgfree",
2238 "pgactivate",
2239 "pgdeactivate",
2240
2241 "pgfault",
2242 "pgmajfault",
2243 "pgrefill_high",
2244 "pgrefill_normal",
2245 "pgrefill_dma",
2246
2247 "pgsteal_high",
2248 "pgsteal_normal",
2249 "pgsteal_dma",
2250 "pgscan_kswapd_high",
2251 "pgscan_kswapd_normal",
2252
2253 "pgscan_kswapd_dma",
2254 "pgscan_direct_high",
2255 "pgscan_direct_normal",
2256 "pgscan_direct_dma",
2257 "pginodesteal",
2258
2259 "slabs_scanned",
2260 "kswapd_steal",
2261 "kswapd_inodesteal",
2262 "pageoutrun",
2263 "allocstall",
2264
2265 "pgrotated",
KAMEZAWA Hiroyukiedfbe2b2005-05-01 08:58:37 -07002266 "nr_bounce",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267};
2268
2269static void *vmstat_start(struct seq_file *m, loff_t *pos)
2270{
2271 struct page_state *ps;
2272
2273 if (*pos >= ARRAY_SIZE(vmstat_text))
2274 return NULL;
2275
2276 ps = kmalloc(sizeof(*ps), GFP_KERNEL);
2277 m->private = ps;
2278 if (!ps)
2279 return ERR_PTR(-ENOMEM);
2280 get_full_page_state(ps);
2281 ps->pgpgin /= 2; /* sectors -> kbytes */
2282 ps->pgpgout /= 2;
2283 return (unsigned long *)ps + *pos;
2284}
2285
2286static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
2287{
2288 (*pos)++;
2289 if (*pos >= ARRAY_SIZE(vmstat_text))
2290 return NULL;
2291 return (unsigned long *)m->private + *pos;
2292}
2293
2294static int vmstat_show(struct seq_file *m, void *arg)
2295{
2296 unsigned long *l = arg;
2297 unsigned long off = l - (unsigned long *)m->private;
2298
2299 seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
2300 return 0;
2301}
2302
2303static void vmstat_stop(struct seq_file *m, void *arg)
2304{
2305 kfree(m->private);
2306 m->private = NULL;
2307}
2308
2309struct seq_operations vmstat_op = {
2310 .start = vmstat_start,
2311 .next = vmstat_next,
2312 .stop = vmstat_stop,
2313 .show = vmstat_show,
2314};
2315
2316#endif /* CONFIG_PROC_FS */
2317
2318#ifdef CONFIG_HOTPLUG_CPU
2319static int page_alloc_cpu_notify(struct notifier_block *self,
2320 unsigned long action, void *hcpu)
2321{
2322 int cpu = (unsigned long)hcpu;
2323 long *count;
2324 unsigned long *src, *dest;
2325
2326 if (action == CPU_DEAD) {
2327 int i;
2328
2329 /* Drain local pagecache count. */
2330 count = &per_cpu(nr_pagecache_local, cpu);
2331 atomic_add(*count, &nr_pagecache);
2332 *count = 0;
2333 local_irq_disable();
2334 __drain_pages(cpu);
2335
2336 /* Add dead cpu's page_states to our own. */
2337 dest = (unsigned long *)&__get_cpu_var(page_states);
2338 src = (unsigned long *)&per_cpu(page_states, cpu);
2339
2340 for (i = 0; i < sizeof(struct page_state)/sizeof(unsigned long);
2341 i++) {
2342 dest[i] += src[i];
2343 src[i] = 0;
2344 }
2345
2346 local_irq_enable();
2347 }
2348 return NOTIFY_OK;
2349}
2350#endif /* CONFIG_HOTPLUG_CPU */
2351
2352void __init page_alloc_init(void)
2353{
2354 hotcpu_notifier(page_alloc_cpu_notify, 0);
2355}
2356
2357/*
2358 * setup_per_zone_lowmem_reserve - called whenever
2359 * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
2360 * has a correct pages reserved value, so an adequate number of
2361 * pages are left in the zone after a successful __alloc_pages().
2362 */
2363static void setup_per_zone_lowmem_reserve(void)
2364{
2365 struct pglist_data *pgdat;
2366 int j, idx;
2367
2368 for_each_pgdat(pgdat) {
2369 for (j = 0; j < MAX_NR_ZONES; j++) {
2370 struct zone *zone = pgdat->node_zones + j;
2371 unsigned long present_pages = zone->present_pages;
2372
2373 zone->lowmem_reserve[j] = 0;
2374
2375 for (idx = j-1; idx >= 0; idx--) {
2376 struct zone *lower_zone;
2377
2378 if (sysctl_lowmem_reserve_ratio[idx] < 1)
2379 sysctl_lowmem_reserve_ratio[idx] = 1;
2380
2381 lower_zone = pgdat->node_zones + idx;
2382 lower_zone->lowmem_reserve[j] = present_pages /
2383 sysctl_lowmem_reserve_ratio[idx];
2384 present_pages += lower_zone->present_pages;
2385 }
2386 }
2387 }
2388}
2389
2390/*
2391 * setup_per_zone_pages_min - called when min_free_kbytes changes. Ensures
2392 * that the pages_{min,low,high} values for each zone are set correctly
2393 * with respect to min_free_kbytes.
2394 */
Dave Hansen3947be12005-10-29 18:16:54 -07002395void setup_per_zone_pages_min(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396{
2397 unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
2398 unsigned long lowmem_pages = 0;
2399 struct zone *zone;
2400 unsigned long flags;
2401
2402 /* Calculate total number of !ZONE_HIGHMEM pages */
2403 for_each_zone(zone) {
2404 if (!is_highmem(zone))
2405 lowmem_pages += zone->present_pages;
2406 }
2407
2408 for_each_zone(zone) {
Nick Piggin669ed172005-11-13 16:06:45 -08002409 unsigned long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 spin_lock_irqsave(&zone->lru_lock, flags);
Nick Piggin669ed172005-11-13 16:06:45 -08002411 tmp = (pages_min * zone->present_pages) / lowmem_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 if (is_highmem(zone)) {
2413 /*
Nick Piggin669ed172005-11-13 16:06:45 -08002414 * __GFP_HIGH and PF_MEMALLOC allocations usually don't
2415 * need highmem pages, so cap pages_min to a small
2416 * value here.
2417 *
2418 * The (pages_high-pages_low) and (pages_low-pages_min)
2419 * deltas controls asynch page reclaim, and so should
2420 * not be capped for highmem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 */
2422 int min_pages;
2423
2424 min_pages = zone->present_pages / 1024;
2425 if (min_pages < SWAP_CLUSTER_MAX)
2426 min_pages = SWAP_CLUSTER_MAX;
2427 if (min_pages > 128)
2428 min_pages = 128;
2429 zone->pages_min = min_pages;
2430 } else {
Nick Piggin669ed172005-11-13 16:06:45 -08002431 /*
2432 * If it's a lowmem zone, reserve a number of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 * proportionate to the zone's size.
2434 */
Nick Piggin669ed172005-11-13 16:06:45 -08002435 zone->pages_min = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 }
2437
Nick Piggin669ed172005-11-13 16:06:45 -08002438 zone->pages_low = zone->pages_min + tmp / 4;
2439 zone->pages_high = zone->pages_min + tmp / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 spin_unlock_irqrestore(&zone->lru_lock, flags);
2441 }
2442}
2443
2444/*
2445 * Initialise min_free_kbytes.
2446 *
2447 * For small machines we want it small (128k min). For large machines
2448 * we want it large (64MB max). But it is not linear, because network
2449 * bandwidth does not increase linearly with machine size. We use
2450 *
2451 * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
2452 * min_free_kbytes = sqrt(lowmem_kbytes * 16)
2453 *
2454 * which yields
2455 *
2456 * 16MB: 512k
2457 * 32MB: 724k
2458 * 64MB: 1024k
2459 * 128MB: 1448k
2460 * 256MB: 2048k
2461 * 512MB: 2896k
2462 * 1024MB: 4096k
2463 * 2048MB: 5792k
2464 * 4096MB: 8192k
2465 * 8192MB: 11584k
2466 * 16384MB: 16384k
2467 */
2468static int __init init_per_zone_pages_min(void)
2469{
2470 unsigned long lowmem_kbytes;
2471
2472 lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
2473
2474 min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
2475 if (min_free_kbytes < 128)
2476 min_free_kbytes = 128;
2477 if (min_free_kbytes > 65536)
2478 min_free_kbytes = 65536;
2479 setup_per_zone_pages_min();
2480 setup_per_zone_lowmem_reserve();
2481 return 0;
2482}
2483module_init(init_per_zone_pages_min)
2484
2485/*
2486 * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
2487 * that we can call two helper functions whenever min_free_kbytes
2488 * changes.
2489 */
2490int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
2491 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
2492{
2493 proc_dointvec(table, write, file, buffer, length, ppos);
2494 setup_per_zone_pages_min();
2495 return 0;
2496}
2497
2498/*
2499 * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
2500 * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
2501 * whenever sysctl_lowmem_reserve_ratio changes.
2502 *
2503 * The reserve ratio obviously has absolutely no relation with the
2504 * pages_min watermarks. The lowmem reserve ratio can only make sense
2505 * if in function of the boot time zone sizes.
2506 */
2507int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
2508 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
2509{
2510 proc_dointvec_minmax(table, write, file, buffer, length, ppos);
2511 setup_per_zone_lowmem_reserve();
2512 return 0;
2513}
2514
2515__initdata int hashdist = HASHDIST_DEFAULT;
2516
2517#ifdef CONFIG_NUMA
2518static int __init set_hashdist(char *str)
2519{
2520 if (!str)
2521 return 0;
2522 hashdist = simple_strtoul(str, &str, 0);
2523 return 1;
2524}
2525__setup("hashdist=", set_hashdist);
2526#endif
2527
2528/*
2529 * allocate a large system hash table from bootmem
2530 * - it is assumed that the hash table must contain an exact power-of-2
2531 * quantity of entries
2532 * - limit is the number of hash buckets, not the total allocation size
2533 */
2534void *__init alloc_large_system_hash(const char *tablename,
2535 unsigned long bucketsize,
2536 unsigned long numentries,
2537 int scale,
2538 int flags,
2539 unsigned int *_hash_shift,
2540 unsigned int *_hash_mask,
2541 unsigned long limit)
2542{
2543 unsigned long long max = limit;
2544 unsigned long log2qty, size;
2545 void *table = NULL;
2546
2547 /* allow the kernel cmdline to have a say */
2548 if (!numentries) {
2549 /* round applicable memory size up to nearest megabyte */
2550 numentries = (flags & HASH_HIGHMEM) ? nr_all_pages : nr_kernel_pages;
2551 numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
2552 numentries >>= 20 - PAGE_SHIFT;
2553 numentries <<= 20 - PAGE_SHIFT;
2554
2555 /* limit to 1 bucket per 2^scale bytes of low memory */
2556 if (scale > PAGE_SHIFT)
2557 numentries >>= (scale - PAGE_SHIFT);
2558 else
2559 numentries <<= (PAGE_SHIFT - scale);
2560 }
2561 /* rounded up to nearest power of 2 in size */
2562 numentries = 1UL << (long_log2(numentries) + 1);
2563
2564 /* limit allocation size to 1/16 total memory by default */
2565 if (max == 0) {
2566 max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
2567 do_div(max, bucketsize);
2568 }
2569
2570 if (numentries > max)
2571 numentries = max;
2572
2573 log2qty = long_log2(numentries);
2574
2575 do {
2576 size = bucketsize << log2qty;
2577 if (flags & HASH_EARLY)
2578 table = alloc_bootmem(size);
2579 else if (hashdist)
2580 table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
2581 else {
2582 unsigned long order;
2583 for (order = 0; ((1UL << order) << PAGE_SHIFT) < size; order++)
2584 ;
2585 table = (void*) __get_free_pages(GFP_ATOMIC, order);
2586 }
2587 } while (!table && size > PAGE_SIZE && --log2qty);
2588
2589 if (!table)
2590 panic("Failed to allocate %s hash table\n", tablename);
2591
2592 printk("%s hash table entries: %d (order: %d, %lu bytes)\n",
2593 tablename,
2594 (1U << log2qty),
2595 long_log2(size) - PAGE_SHIFT,
2596 size);
2597
2598 if (_hash_shift)
2599 *_hash_shift = log2qty;
2600 if (_hash_mask)
2601 *_hash_mask = (1 << log2qty) - 1;
2602
2603 return table;
2604}